blob: 943c70112b79175acc30d87457a1c7f70d1ecf06 [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001
2/* Core extension modules are built-in on some platforms (e.g. Windows). */
3#ifdef Py_BUILD_CORE
Eric Snowfc1bf872017-09-11 18:30:43 -07004#define Py_BUILD_CORE_BUILTIN
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#undef Py_BUILD_CORE
6#endif
7
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008#include "Python.h"
9#include "structmember.h"
10
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -080011PyDoc_STRVAR(pickle_module_doc,
12"Optimized C implementation for the Python pickle module.");
13
Larry Hastings61272b72014-01-07 12:41:53 -080014/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080016class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
17class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
18class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
19class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080020[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080022
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000023/* Bump this when new opcodes are added to the pickle protocol. */
24enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010025 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000026 DEFAULT_PROTOCOL = 3
27};
28
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029/* Pickle opcodes. These must be kept updated with pickle.py.
30 Extensive docs are in pickletools.py. */
31enum opcode {
32 MARK = '(',
33 STOP = '.',
34 POP = '0',
35 POP_MARK = '1',
36 DUP = '2',
37 FLOAT = 'F',
38 INT = 'I',
39 BININT = 'J',
40 BININT1 = 'K',
41 LONG = 'L',
42 BININT2 = 'M',
43 NONE = 'N',
44 PERSID = 'P',
45 BINPERSID = 'Q',
46 REDUCE = 'R',
47 STRING = 'S',
48 BINSTRING = 'T',
49 SHORT_BINSTRING = 'U',
50 UNICODE = 'V',
51 BINUNICODE = 'X',
52 APPEND = 'a',
53 BUILD = 'b',
54 GLOBAL = 'c',
55 DICT = 'd',
56 EMPTY_DICT = '}',
57 APPENDS = 'e',
58 GET = 'g',
59 BINGET = 'h',
60 INST = 'i',
61 LONG_BINGET = 'j',
62 LIST = 'l',
63 EMPTY_LIST = ']',
64 OBJ = 'o',
65 PUT = 'p',
66 BINPUT = 'q',
67 LONG_BINPUT = 'r',
68 SETITEM = 's',
69 TUPLE = 't',
70 EMPTY_TUPLE = ')',
71 SETITEMS = 'u',
72 BINFLOAT = 'G',
73
74 /* Protocol 2. */
75 PROTO = '\x80',
76 NEWOBJ = '\x81',
77 EXT1 = '\x82',
78 EXT2 = '\x83',
79 EXT4 = '\x84',
80 TUPLE1 = '\x85',
81 TUPLE2 = '\x86',
82 TUPLE3 = '\x87',
83 NEWTRUE = '\x88',
84 NEWFALSE = '\x89',
85 LONG1 = '\x8a',
86 LONG4 = '\x8b',
87
88 /* Protocol 3 (Python 3.x) */
89 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010090 SHORT_BINBYTES = 'C',
91
92 /* Protocol 4 */
93 SHORT_BINUNICODE = '\x8c',
94 BINUNICODE8 = '\x8d',
95 BINBYTES8 = '\x8e',
96 EMPTY_SET = '\x8f',
97 ADDITEMS = '\x90',
98 FROZENSET = '\x91',
99 NEWOBJ_EX = '\x92',
100 STACK_GLOBAL = '\x93',
101 MEMOIZE = '\x94',
102 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000103};
104
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000105enum {
106 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
107 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
108 break if this gets out of synch with pickle.py, but it's unclear that would
109 help anything either. */
110 BATCHSIZE = 1000,
111
112 /* Nesting limit until Pickler, when running in "fast mode", starts
113 checking for self-referential data-structures. */
114 FAST_NESTING_LIMIT = 50,
115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000116 /* Initial size of the write buffer of Pickler. */
117 WRITE_BUF_SIZE = 4096,
118
Antoine Pitrou04248a82010-10-12 20:51:21 +0000119 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100120 PREFETCH = 8192 * 16,
121
122 FRAME_SIZE_TARGET = 64 * 1024,
123
124 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000125};
126
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800127/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129/* State of the pickle module, per PEP 3121. */
130typedef struct {
131 /* Exception classes for pickle. */
132 PyObject *PickleError;
133 PyObject *PicklingError;
134 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800136 /* copyreg.dispatch_table, {type_object: pickling_function} */
137 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800139 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* copyreg._extension_registry, {(module_name, function_name): code} */
142 PyObject *extension_registry;
143 /* copyreg._extension_cache, {code: object} */
144 PyObject *extension_cache;
145 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
146 PyObject *inverted_registry;
147
148 /* Import mappings for compatibility with Python 2.x */
149
150 /* _compat_pickle.NAME_MAPPING,
151 {(oldmodule, oldname): (newmodule, newname)} */
152 PyObject *name_mapping_2to3;
153 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
154 PyObject *import_mapping_2to3;
155 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
156 PyObject *name_mapping_3to2;
157 PyObject *import_mapping_3to2;
158
159 /* codecs.encode, used for saving bytes in older protocols */
160 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300161 /* builtins.getattr, used for saving nested names with protocol < 4 */
162 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300163 /* functools.partial, used for implementing __newobj_ex__ with protocols
164 2 and 3 */
165 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800166} PickleState;
167
168/* Forward declaration of the _pickle module definition. */
169static struct PyModuleDef _picklemodule;
170
171/* Given a module object, get its per-module state. */
172static PickleState *
173_Pickle_GetState(PyObject *module)
174{
175 return (PickleState *)PyModule_GetState(module);
176}
177
178/* Find the module instance imported in the currently running sub-interpreter
179 and get its state. */
180static PickleState *
181_Pickle_GetGlobalState(void)
182{
183 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
184}
185
186/* Clear the given pickle module state. */
187static void
188_Pickle_ClearState(PickleState *st)
189{
190 Py_CLEAR(st->PickleError);
191 Py_CLEAR(st->PicklingError);
192 Py_CLEAR(st->UnpicklingError);
193 Py_CLEAR(st->dispatch_table);
194 Py_CLEAR(st->extension_registry);
195 Py_CLEAR(st->extension_cache);
196 Py_CLEAR(st->inverted_registry);
197 Py_CLEAR(st->name_mapping_2to3);
198 Py_CLEAR(st->import_mapping_2to3);
199 Py_CLEAR(st->name_mapping_3to2);
200 Py_CLEAR(st->import_mapping_3to2);
201 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300202 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100203 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204}
205
206/* Initialize the given pickle module state. */
207static int
208_Pickle_InitState(PickleState *st)
209{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300210 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800211 PyObject *copyreg = NULL;
212 PyObject *compat_pickle = NULL;
213 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300214 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800215
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300216 builtins = PyEval_GetBuiltins();
217 if (builtins == NULL)
218 goto error;
219 st->getattr = PyDict_GetItemString(builtins, "getattr");
220 if (st->getattr == NULL)
221 goto error;
222 Py_INCREF(st->getattr);
223
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800224 copyreg = PyImport_ImportModule("copyreg");
225 if (!copyreg)
226 goto error;
227 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
228 if (!st->dispatch_table)
229 goto error;
230 if (!PyDict_CheckExact(st->dispatch_table)) {
231 PyErr_Format(PyExc_RuntimeError,
232 "copyreg.dispatch_table should be a dict, not %.200s",
233 Py_TYPE(st->dispatch_table)->tp_name);
234 goto error;
235 }
236 st->extension_registry = \
237 PyObject_GetAttrString(copyreg, "_extension_registry");
238 if (!st->extension_registry)
239 goto error;
240 if (!PyDict_CheckExact(st->extension_registry)) {
241 PyErr_Format(PyExc_RuntimeError,
242 "copyreg._extension_registry should be a dict, "
243 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
244 goto error;
245 }
246 st->inverted_registry = \
247 PyObject_GetAttrString(copyreg, "_inverted_registry");
248 if (!st->inverted_registry)
249 goto error;
250 if (!PyDict_CheckExact(st->inverted_registry)) {
251 PyErr_Format(PyExc_RuntimeError,
252 "copyreg._inverted_registry should be a dict, "
253 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
254 goto error;
255 }
256 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
257 if (!st->extension_cache)
258 goto error;
259 if (!PyDict_CheckExact(st->extension_cache)) {
260 PyErr_Format(PyExc_RuntimeError,
261 "copyreg._extension_cache should be a dict, "
262 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
263 goto error;
264 }
265 Py_CLEAR(copyreg);
266
267 /* Load the 2.x -> 3.x stdlib module mapping tables */
268 compat_pickle = PyImport_ImportModule("_compat_pickle");
269 if (!compat_pickle)
270 goto error;
271 st->name_mapping_2to3 = \
272 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
273 if (!st->name_mapping_2to3)
274 goto error;
275 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
276 PyErr_Format(PyExc_RuntimeError,
277 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
278 Py_TYPE(st->name_mapping_2to3)->tp_name);
279 goto error;
280 }
281 st->import_mapping_2to3 = \
282 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
283 if (!st->import_mapping_2to3)
284 goto error;
285 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
286 PyErr_Format(PyExc_RuntimeError,
287 "_compat_pickle.IMPORT_MAPPING should be a dict, "
288 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
289 goto error;
290 }
291 /* ... and the 3.x -> 2.x mapping tables */
292 st->name_mapping_3to2 = \
293 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
294 if (!st->name_mapping_3to2)
295 goto error;
296 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
297 PyErr_Format(PyExc_RuntimeError,
298 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
299 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
300 goto error;
301 }
302 st->import_mapping_3to2 = \
303 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
304 if (!st->import_mapping_3to2)
305 goto error;
306 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
307 PyErr_Format(PyExc_RuntimeError,
308 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
309 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
310 goto error;
311 }
312 Py_CLEAR(compat_pickle);
313
314 codecs = PyImport_ImportModule("codecs");
315 if (codecs == NULL)
316 goto error;
317 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
318 if (st->codecs_encode == NULL) {
319 goto error;
320 }
321 if (!PyCallable_Check(st->codecs_encode)) {
322 PyErr_Format(PyExc_RuntimeError,
323 "codecs.encode should be a callable, not %.200s",
324 Py_TYPE(st->codecs_encode)->tp_name);
325 goto error;
326 }
327 Py_CLEAR(codecs);
328
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300329 functools = PyImport_ImportModule("functools");
330 if (!functools)
331 goto error;
332 st->partial = PyObject_GetAttrString(functools, "partial");
333 if (!st->partial)
334 goto error;
335 Py_CLEAR(functools);
336
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 return 0;
338
339 error:
340 Py_CLEAR(copyreg);
341 Py_CLEAR(compat_pickle);
342 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300343 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800344 _Pickle_ClearState(st);
345 return -1;
346}
347
348/* Helper for calling a function with a single argument quickly.
349
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 This function steals the reference of the given argument. */
351static PyObject *
352_Pickle_FastCall(PyObject *func, PyObject *obj)
353{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 PyObject *result;
355
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100356 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200357 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800358 return result;
359}
360
361/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000362
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000363/* Internal data type used as the unpickling stack. */
364typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000365 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000366 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200367 int mark_set; /* is MARK set? */
368 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000369 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000370} Pdata;
371
372static void
373Pdata_dealloc(Pdata *self)
374{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200375 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000376 while (--i >= 0) {
377 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000378 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000379 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000380 PyObject_Del(self);
381}
382
383static PyTypeObject Pdata_Type = {
384 PyVarObject_HEAD_INIT(NULL, 0)
385 "_pickle.Pdata", /*tp_name*/
386 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200387 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000388 (destructor)Pdata_dealloc, /*tp_dealloc*/
389};
390
391static PyObject *
392Pdata_New(void)
393{
394 Pdata *self;
395
396 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
397 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000398 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200399 self->mark_set = 0;
400 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000401 self->allocated = 8;
402 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000403 if (self->data)
404 return (PyObject *)self;
405 Py_DECREF(self);
406 return PyErr_NoMemory();
407}
408
409
410/* Retain only the initial clearto items. If clearto >= the current
411 * number of items, this is a (non-erroneous) NOP.
412 */
413static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200414Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000415{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200416 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000417
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200418 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000419 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000420 return 0;
421
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000422 while (--i >= clearto) {
423 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000424 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000425 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000426 return 0;
427}
428
429static int
430Pdata_grow(Pdata *self)
431{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200433 size_t allocated = (size_t)self->allocated;
434 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000435
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436 new_allocated = (allocated >> 3) + 6;
437 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200438 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000439 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000440 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500441 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000442 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000443 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000444
445 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200446 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000447 return 0;
448
449 nomemory:
450 PyErr_NoMemory();
451 return -1;
452}
453
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200454static int
455Pdata_stack_underflow(Pdata *self)
456{
457 PickleState *st = _Pickle_GetGlobalState();
458 PyErr_SetString(st->UnpicklingError,
459 self->mark_set ?
460 "unexpected MARK found" :
461 "unpickling stack underflow");
462 return -1;
463}
464
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000465/* D is a Pdata*. Pop the topmost element and store it into V, which
466 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
467 * is raised and V is set to NULL.
468 */
469static PyObject *
470Pdata_pop(Pdata *self)
471{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200472 if (Py_SIZE(self) <= self->fence) {
473 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000474 return NULL;
475 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000476 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477}
478#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
479
480static int
481Pdata_push(Pdata *self, PyObject *obj)
482{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000483 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000484 return -1;
485 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000486 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000487 return 0;
488}
489
490/* Push an object on stack, transferring its ownership to the stack. */
491#define PDATA_PUSH(D, O, ER) do { \
492 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
493
494/* Push an object on stack, adding a new reference to the object. */
495#define PDATA_APPEND(D, O, ER) do { \
496 Py_INCREF((O)); \
497 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
498
499static PyObject *
500Pdata_poptuple(Pdata *self, Py_ssize_t start)
501{
502 PyObject *tuple;
503 Py_ssize_t len, i, j;
504
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200505 if (start < self->fence) {
506 Pdata_stack_underflow(self);
507 return NULL;
508 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000509 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000510 tuple = PyTuple_New(len);
511 if (tuple == NULL)
512 return NULL;
513 for (i = start, j = 0; j < len; i++, j++)
514 PyTuple_SET_ITEM(tuple, j, self->data[i]);
515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000516 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000517 return tuple;
518}
519
520static PyObject *
521Pdata_poplist(Pdata *self, Py_ssize_t start)
522{
523 PyObject *list;
524 Py_ssize_t len, i, j;
525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000526 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000527 list = PyList_New(len);
528 if (list == NULL)
529 return NULL;
530 for (i = start, j = 0; j < len; i++, j++)
531 PyList_SET_ITEM(list, j, self->data[i]);
532
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000533 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000534 return list;
535}
536
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000537typedef struct {
538 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200539 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000540} PyMemoEntry;
541
542typedef struct {
543 Py_ssize_t mt_mask;
544 Py_ssize_t mt_used;
545 Py_ssize_t mt_allocated;
546 PyMemoEntry *mt_table;
547} PyMemoTable;
548
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000549typedef struct PicklerObject {
550 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000551 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000552 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000553 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000554 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100555 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000556
557 PyObject *write; /* write() method of the output stream. */
558 PyObject *output_buffer; /* Write into a local bytearray buffer before
559 flushing to the stream. */
560 Py_ssize_t output_len; /* Length of output_buffer. */
561 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 int proto; /* Pickle protocol number, >= 0 */
563 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100564 int framing; /* True when framing is enabled, proto >= 4 */
565 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000566 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100567 is no frame currently open. */
568
569 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000570 int fast; /* Enable fast mode if set to a true value.
571 The fast mode disable the usage of memo,
572 therefore speeding the pickling process by
573 not generating superfluous PUT opcodes. It
574 should not be used if with self-referential
575 objects. */
576 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000577 int fix_imports; /* Indicate whether Pickler should fix
578 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000579 PyObject *fast_memo;
580} PicklerObject;
581
582typedef struct UnpicklerObject {
583 PyObject_HEAD
584 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000585
586 /* The unpickler memo is just an array of PyObject *s. Using a dict
587 is unnecessary, since the keys are contiguous ints. */
588 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100589 Py_ssize_t memo_size; /* Capacity of the memo array */
590 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000591
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000592 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593
594 Py_buffer buffer;
595 char *input_buffer;
596 char *input_line;
597 Py_ssize_t input_len;
598 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000599 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100600
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000601 PyObject *read; /* read() method of the input stream. */
602 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000603 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000604
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000605 char *encoding; /* Name of the encoding to be used for
606 decoding strings pickled using Python
607 2.x. The default value is "ASCII" */
608 char *errors; /* Name of errors handling scheme to used when
609 decoding strings. The default value is
610 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500611 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000612 objects. */
613 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
614 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000615 int proto; /* Protocol of the pickle loaded. */
616 int fix_imports; /* Indicate whether Unpickler should fix
617 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000618} UnpicklerObject;
619
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200620typedef struct {
621 PyObject_HEAD
622 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
623} PicklerMemoProxyObject;
624
625typedef struct {
626 PyObject_HEAD
627 UnpicklerObject *unpickler;
628} UnpicklerMemoProxyObject;
629
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000630/* Forward declarations */
631static int save(PicklerObject *, PyObject *, int);
632static int save_reduce(PicklerObject *, PyObject *, PyObject *);
633static PyTypeObject Pickler_Type;
634static PyTypeObject Unpickler_Type;
635
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200636#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000637
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000638/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300639 A custom hashtable mapping void* to Python ints. This is used by the pickler
640 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000641 a bunch of unnecessary object creation. This makes a huge performance
642 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000643
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000644#define MT_MINSIZE 8
645#define PERTURB_SHIFT 5
646
647
648static PyMemoTable *
649PyMemoTable_New(void)
650{
651 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
652 if (memo == NULL) {
653 PyErr_NoMemory();
654 return NULL;
655 }
656
657 memo->mt_used = 0;
658 memo->mt_allocated = MT_MINSIZE;
659 memo->mt_mask = MT_MINSIZE - 1;
660 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
661 if (memo->mt_table == NULL) {
662 PyMem_FREE(memo);
663 PyErr_NoMemory();
664 return NULL;
665 }
666 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
667
668 return memo;
669}
670
671static PyMemoTable *
672PyMemoTable_Copy(PyMemoTable *self)
673{
674 Py_ssize_t i;
675 PyMemoTable *new = PyMemoTable_New();
676 if (new == NULL)
677 return NULL;
678
679 new->mt_used = self->mt_used;
680 new->mt_allocated = self->mt_allocated;
681 new->mt_mask = self->mt_mask;
682 /* The table we get from _New() is probably smaller than we wanted.
683 Free it and allocate one that's the right size. */
684 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500685 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000686 if (new->mt_table == NULL) {
687 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200688 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000689 return NULL;
690 }
691 for (i = 0; i < self->mt_allocated; i++) {
692 Py_XINCREF(self->mt_table[i].me_key);
693 }
694 memcpy(new->mt_table, self->mt_table,
695 sizeof(PyMemoEntry) * self->mt_allocated);
696
697 return new;
698}
699
700static Py_ssize_t
701PyMemoTable_Size(PyMemoTable *self)
702{
703 return self->mt_used;
704}
705
706static int
707PyMemoTable_Clear(PyMemoTable *self)
708{
709 Py_ssize_t i = self->mt_allocated;
710
711 while (--i >= 0) {
712 Py_XDECREF(self->mt_table[i].me_key);
713 }
714 self->mt_used = 0;
715 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
716 return 0;
717}
718
719static void
720PyMemoTable_Del(PyMemoTable *self)
721{
722 if (self == NULL)
723 return;
724 PyMemoTable_Clear(self);
725
726 PyMem_FREE(self->mt_table);
727 PyMem_FREE(self);
728}
729
730/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
731 can be considerably simpler than dictobject.c's lookdict(). */
732static PyMemoEntry *
733_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
734{
735 size_t i;
736 size_t perturb;
737 size_t mask = (size_t)self->mt_mask;
738 PyMemoEntry *table = self->mt_table;
739 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000740 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000741
742 i = hash & mask;
743 entry = &table[i];
744 if (entry->me_key == NULL || entry->me_key == key)
745 return entry;
746
747 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
748 i = (i << 2) + i + perturb + 1;
749 entry = &table[i & mask];
750 if (entry->me_key == NULL || entry->me_key == key)
751 return entry;
752 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700753 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000754}
755
756/* Returns -1 on failure, 0 on success. */
757static int
758_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
759{
760 PyMemoEntry *oldtable = NULL;
761 PyMemoEntry *oldentry, *newentry;
762 Py_ssize_t new_size = MT_MINSIZE;
763 Py_ssize_t to_process;
764
765 assert(min_size > 0);
766
767 /* Find the smallest valid table size >= min_size. */
768 while (new_size < min_size && new_size > 0)
769 new_size <<= 1;
770 if (new_size <= 0) {
771 PyErr_NoMemory();
772 return -1;
773 }
774 /* new_size needs to be a power of two. */
775 assert((new_size & (new_size - 1)) == 0);
776
777 /* Allocate new table. */
778 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500779 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000780 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200781 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000782 PyErr_NoMemory();
783 return -1;
784 }
785 self->mt_allocated = new_size;
786 self->mt_mask = new_size - 1;
787 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
788
789 /* Copy entries from the old table. */
790 to_process = self->mt_used;
791 for (oldentry = oldtable; to_process > 0; oldentry++) {
792 if (oldentry->me_key != NULL) {
793 to_process--;
794 /* newentry is a pointer to a chunk of the new
795 mt_table, so we're setting the key:value pair
796 in-place. */
797 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
798 newentry->me_key = oldentry->me_key;
799 newentry->me_value = oldentry->me_value;
800 }
801 }
802
803 /* Deallocate the old table. */
804 PyMem_FREE(oldtable);
805 return 0;
806}
807
808/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200809static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000810PyMemoTable_Get(PyMemoTable *self, PyObject *key)
811{
812 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
813 if (entry->me_key == NULL)
814 return NULL;
815 return &entry->me_value;
816}
817
818/* Returns -1 on failure, 0 on success. */
819static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200820PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000821{
822 PyMemoEntry *entry;
823
824 assert(key != NULL);
825
826 entry = _PyMemoTable_Lookup(self, key);
827 if (entry->me_key != NULL) {
828 entry->me_value = value;
829 return 0;
830 }
831 Py_INCREF(key);
832 entry->me_key = key;
833 entry->me_value = value;
834 self->mt_used++;
835
836 /* If we added a key, we can safely resize. Otherwise just return!
837 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
838 *
839 * Quadrupling the size improves average table sparseness
840 * (reducing collisions) at the cost of some memory. It also halves
841 * the number of expensive resize operations in a growing memo table.
842 *
843 * Very large memo tables (over 50K items) use doubling instead.
844 * This may help applications with severe memory constraints.
845 */
846 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
847 return 0;
848 return _PyMemoTable_ResizeTable(self,
849 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
850}
851
852#undef MT_MINSIZE
853#undef PERTURB_SHIFT
854
855/*************************************************************************/
856
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000857
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000858static int
859_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000860{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300861 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200862 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000863 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000864 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000865 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100866 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000867 return 0;
868}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000869
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100870static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100871_write_size64(char *out, size_t value)
872{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200873 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800874
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200875 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800876
877 for (i = 0; i < sizeof(size_t); i++) {
878 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
879 }
880 for (i = sizeof(size_t); i < 8; i++) {
881 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800882 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100883}
884
885static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100886_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
887{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100888 qdata[0] = FRAME;
889 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100890}
891
892static int
893_Pickler_CommitFrame(PicklerObject *self)
894{
895 size_t frame_len;
896 char *qdata;
897
898 if (!self->framing || self->frame_start == -1)
899 return 0;
900 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
901 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
902 _Pickler_WriteFrameHeader(self, qdata, frame_len);
903 self->frame_start = -1;
904 return 0;
905}
906
907static int
908_Pickler_OpcodeBoundary(PicklerObject *self)
909{
910 Py_ssize_t frame_len;
911
912 if (!self->framing || self->frame_start == -1)
913 return 0;
914 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
915 if (frame_len >= FRAME_SIZE_TARGET)
916 return _Pickler_CommitFrame(self);
917 else
918 return 0;
919}
920
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000921static PyObject *
922_Pickler_GetString(PicklerObject *self)
923{
924 PyObject *output_buffer = self->output_buffer;
925
926 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100927
928 if (_Pickler_CommitFrame(self))
929 return NULL;
930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000931 self->output_buffer = NULL;
932 /* Resize down to exact size */
933 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
934 return NULL;
935 return output_buffer;
936}
937
938static int
939_Pickler_FlushToFile(PicklerObject *self)
940{
941 PyObject *output, *result;
942
943 assert(self->write != NULL);
944
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100945 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000946 output = _Pickler_GetString(self);
947 if (output == NULL)
948 return -1;
949
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800950 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000951 Py_XDECREF(result);
952 return (result == NULL) ? -1 : 0;
953}
954
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200955static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100956_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000957{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100958 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000959 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100960 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000961
962 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100963 need_new_frame = (self->framing && self->frame_start == -1);
964
965 if (need_new_frame)
966 n = data_len + FRAME_HEADER_SIZE;
967 else
968 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000969
970 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100971 if (required > self->max_output_len) {
972 /* Make place in buffer for the pickle chunk */
973 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
974 PyErr_NoMemory();
975 return -1;
976 }
977 self->max_output_len = (self->output_len + n) / 2 * 3;
978 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
979 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000980 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000981 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 if (need_new_frame) {
983 /* Setup new frame */
984 Py_ssize_t frame_start = self->output_len;
985 self->frame_start = frame_start;
986 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
987 /* Write an invalid value, for debugging */
988 buffer[frame_start + i] = 0xFE;
989 }
990 self->output_len += FRAME_HEADER_SIZE;
991 }
992 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000993 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100994 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000995 buffer[self->output_len + i] = s[i];
996 }
997 }
998 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100999 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001000 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001001 self->output_len += data_len;
1002 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001003}
1004
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001005static PicklerObject *
1006_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001007{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001008 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001009
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001010 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1011 if (self == NULL)
1012 return NULL;
1013
1014 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001015 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001016 self->write = NULL;
1017 self->proto = 0;
1018 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001019 self->framing = 0;
1020 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021 self->fast = 0;
1022 self->fast_nesting = 0;
1023 self->fix_imports = 0;
1024 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001025 self->max_output_len = WRITE_BUF_SIZE;
1026 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001027
1028 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001029 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1030 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001031
1032 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001033 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001034 return NULL;
1035 }
1036 return self;
1037}
1038
1039static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001040_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001042 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001043
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001044 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001045 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001046 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001047 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001048 proto = PyLong_AsLong(protocol);
1049 if (proto < 0) {
1050 if (proto == -1 && PyErr_Occurred())
1051 return -1;
1052 proto = HIGHEST_PROTOCOL;
1053 }
1054 else if (proto > HIGHEST_PROTOCOL) {
1055 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1056 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001057 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001058 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001059 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001060 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001061 self->bin = proto > 0;
1062 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001063 return 0;
1064}
1065
1066/* Returns -1 (with an exception set) on failure, 0 on success. This may
1067 be called once on a freshly created Pickler. */
1068static int
1069_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1070{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001071 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001072 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001073 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001074 if (self->write == NULL) {
1075 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1076 PyErr_SetString(PyExc_TypeError,
1077 "file must have a 'write' attribute");
1078 return -1;
1079 }
1080
1081 return 0;
1082}
1083
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001084/* Returns the size of the input on success, -1 on failure. This takes its
1085 own reference to `input`. */
1086static Py_ssize_t
1087_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1088{
1089 if (self->buffer.buf != NULL)
1090 PyBuffer_Release(&self->buffer);
1091 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1092 return -1;
1093 self->input_buffer = self->buffer.buf;
1094 self->input_len = self->buffer.len;
1095 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001096 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001097 return self->input_len;
1098}
1099
Antoine Pitrou04248a82010-10-12 20:51:21 +00001100static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001101bad_readline(void)
1102{
1103 PickleState *st = _Pickle_GetGlobalState();
1104 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1105 return -1;
1106}
1107
1108static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001109_Unpickler_SkipConsumed(UnpicklerObject *self)
1110{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001111 Py_ssize_t consumed;
1112 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001113
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001114 consumed = self->next_read_idx - self->prefetched_idx;
1115 if (consumed <= 0)
1116 return 0;
1117
1118 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001119 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001120 r = PyObject_CallFunction(self->read, "n", consumed);
1121 if (r == NULL)
1122 return -1;
1123 Py_DECREF(r);
1124
1125 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001126 return 0;
1127}
1128
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001129static const Py_ssize_t READ_WHOLE_LINE = -1;
1130
1131/* If reading from a file, we need to only pull the bytes we need, since there
1132 may be multiple pickle objects arranged contiguously in the same input
1133 buffer.
1134
1135 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1136 bytes from the input stream/buffer.
1137
1138 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1139 failure; on success, returns the number of bytes read from the file.
1140
1141 On success, self->input_len will be 0; this is intentional so that when
1142 unpickling from a file, the "we've run out of data" code paths will trigger,
1143 causing the Unpickler to go back to the file for more data. Use the returned
1144 size to tell you how much data you can process. */
1145static Py_ssize_t
1146_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1147{
1148 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001149 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001150
1151 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001152
Antoine Pitrou04248a82010-10-12 20:51:21 +00001153 if (_Unpickler_SkipConsumed(self) < 0)
1154 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001155
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001156 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001157 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001158 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001159 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001160 PyObject *len;
1161 /* Prefetch some data without advancing the file pointer, if possible */
1162 if (self->peek && n < PREFETCH) {
1163 len = PyLong_FromSsize_t(PREFETCH);
1164 if (len == NULL)
1165 return -1;
1166 data = _Pickle_FastCall(self->peek, len);
1167 if (data == NULL) {
1168 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1169 return -1;
1170 /* peek() is probably not supported by the given file object */
1171 PyErr_Clear();
1172 Py_CLEAR(self->peek);
1173 }
1174 else {
1175 read_size = _Unpickler_SetStringInput(self, data);
1176 Py_DECREF(data);
1177 self->prefetched_idx = 0;
1178 if (n <= read_size)
1179 return n;
1180 }
1181 }
1182 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001183 if (len == NULL)
1184 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001185 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001186 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001187 if (data == NULL)
1188 return -1;
1189
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001190 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001191 Py_DECREF(data);
1192 return read_size;
1193}
1194
Victor Stinner19ed27e2016-05-20 11:42:37 +02001195/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001196static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001197_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001198{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001199 Py_ssize_t num_read;
1200
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001201 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001202 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1203 PickleState *st = _Pickle_GetGlobalState();
1204 PyErr_SetString(st->UnpicklingError,
1205 "read would overflow (invalid bytecode)");
1206 return -1;
1207 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001208
1209 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1210 assert(self->next_read_idx + n > self->input_len);
1211
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001212 if (!self->read)
1213 return bad_readline();
1214
Antoine Pitrou04248a82010-10-12 20:51:21 +00001215 num_read = _Unpickler_ReadFromFile(self, n);
1216 if (num_read < 0)
1217 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001218 if (num_read < n)
1219 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001220 *s = self->input_buffer;
1221 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001222 return n;
1223}
1224
Victor Stinner19ed27e2016-05-20 11:42:37 +02001225/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1226
1227 This should be used for all data reads, rather than accessing the unpickler's
1228 input buffer directly. This method deals correctly with reading from input
1229 streams, which the input buffer doesn't deal with.
1230
1231 Note that when reading from a file-like object, self->next_read_idx won't
1232 be updated (it should remain at 0 for the entire unpickling process). You
1233 should use this function's return value to know how many bytes you can
1234 consume.
1235
1236 Returns -1 (with an exception set) on failure. On success, return the
1237 number of chars read. */
1238#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001239 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001240 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1241 (self)->next_read_idx += (n), \
1242 (n)) \
1243 : _Unpickler_ReadImpl(self, (s), (n)))
1244
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001245static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001246_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1247 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001248{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001249 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001250 if (input_line == NULL) {
1251 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001252 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001254
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001255 memcpy(input_line, line, len);
1256 input_line[len] = '\0';
1257 self->input_line = input_line;
1258 *result = self->input_line;
1259 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001260}
1261
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001262/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001263 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001264
1265 Returns the number of chars read, or -1 on failure. */
1266static Py_ssize_t
1267_Unpickler_Readline(UnpicklerObject *self, char **result)
1268{
1269 Py_ssize_t i, num_read;
1270
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001271 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001272 if (self->input_buffer[i] == '\n') {
1273 char *line_start = self->input_buffer + self->next_read_idx;
1274 num_read = i - self->next_read_idx + 1;
1275 self->next_read_idx = i + 1;
1276 return _Unpickler_CopyLine(self, line_start, num_read, result);
1277 }
1278 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001279 if (!self->read)
1280 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001281
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001282 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1283 if (num_read < 0)
1284 return -1;
1285 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1286 return bad_readline();
1287 self->next_read_idx = num_read;
1288 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001289}
1290
1291/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1292 will be modified in place. */
1293static int
1294_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1295{
1296 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001297
1298 assert(new_size > self->memo_size);
1299
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001300 PyMem_RESIZE(self->memo, PyObject *, new_size);
1301 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001302 PyErr_NoMemory();
1303 return -1;
1304 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001305 for (i = self->memo_size; i < new_size; i++)
1306 self->memo[i] = NULL;
1307 self->memo_size = new_size;
1308 return 0;
1309}
1310
1311/* Returns NULL if idx is out of bounds. */
1312static PyObject *
1313_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1314{
1315 if (idx < 0 || idx >= self->memo_size)
1316 return NULL;
1317
1318 return self->memo[idx];
1319}
1320
1321/* Returns -1 (with an exception set) on failure, 0 on success.
1322 This takes its own reference to `value`. */
1323static int
1324_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1325{
1326 PyObject *old_item;
1327
1328 if (idx >= self->memo_size) {
1329 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1330 return -1;
1331 assert(idx < self->memo_size);
1332 }
1333 Py_INCREF(value);
1334 old_item = self->memo[idx];
1335 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001336 if (old_item != NULL) {
1337 Py_DECREF(old_item);
1338 }
1339 else {
1340 self->memo_len++;
1341 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001342 return 0;
1343}
1344
1345static PyObject **
1346_Unpickler_NewMemo(Py_ssize_t new_size)
1347{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001348 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001349 if (memo == NULL) {
1350 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001351 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001352 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001353 memset(memo, 0, new_size * sizeof(PyObject *));
1354 return memo;
1355}
1356
1357/* Free the unpickler's memo, taking care to decref any items left in it. */
1358static void
1359_Unpickler_MemoCleanup(UnpicklerObject *self)
1360{
1361 Py_ssize_t i;
1362 PyObject **memo = self->memo;
1363
1364 if (self->memo == NULL)
1365 return;
1366 self->memo = NULL;
1367 i = self->memo_size;
1368 while (--i >= 0) {
1369 Py_XDECREF(memo[i]);
1370 }
1371 PyMem_FREE(memo);
1372}
1373
1374static UnpicklerObject *
1375_Unpickler_New(void)
1376{
1377 UnpicklerObject *self;
1378
1379 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1380 if (self == NULL)
1381 return NULL;
1382
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001383 self->pers_func = NULL;
1384 self->input_buffer = NULL;
1385 self->input_line = NULL;
1386 self->input_len = 0;
1387 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001388 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389 self->read = NULL;
1390 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001391 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001392 self->encoding = NULL;
1393 self->errors = NULL;
1394 self->marks = NULL;
1395 self->num_marks = 0;
1396 self->marks_size = 0;
1397 self->proto = 0;
1398 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001399 memset(&self->buffer, 0, sizeof(Py_buffer));
1400 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001401 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001402 self->memo = _Unpickler_NewMemo(self->memo_size);
1403 self->stack = (Pdata *)Pdata_New();
1404
1405 if (self->memo == NULL || self->stack == NULL) {
1406 Py_DECREF(self);
1407 return NULL;
1408 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001409
1410 return self;
1411}
1412
1413/* Returns -1 (with an exception set) on failure, 0 on success. This may
1414 be called once on a freshly created Pickler. */
1415static int
1416_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1417{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001418 _Py_IDENTIFIER(peek);
1419 _Py_IDENTIFIER(read);
1420 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001421
1422 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001423 if (self->peek == NULL) {
1424 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1425 PyErr_Clear();
1426 else
1427 return -1;
1428 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001429 self->read = _PyObject_GetAttrId(file, &PyId_read);
1430 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001431 if (self->readline == NULL || self->read == NULL) {
1432 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1433 PyErr_SetString(PyExc_TypeError,
1434 "file must have 'read' and 'readline' attributes");
1435 Py_CLEAR(self->read);
1436 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001437 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001438 return -1;
1439 }
1440 return 0;
1441}
1442
1443/* Returns -1 (with an exception set) on failure, 0 on success. This may
1444 be called once on a freshly created Pickler. */
1445static int
1446_Unpickler_SetInputEncoding(UnpicklerObject *self,
1447 const char *encoding,
1448 const char *errors)
1449{
1450 if (encoding == NULL)
1451 encoding = "ASCII";
1452 if (errors == NULL)
1453 errors = "strict";
1454
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001455 self->encoding = _PyMem_Strdup(encoding);
1456 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001457 if (self->encoding == NULL || self->errors == NULL) {
1458 PyErr_NoMemory();
1459 return -1;
1460 }
1461 return 0;
1462}
1463
1464/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001465static int
1466memo_get(PicklerObject *self, PyObject *key)
1467{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001468 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001469 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001470 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001471
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001472 value = PyMemoTable_Get(self->memo, key);
1473 if (value == NULL) {
1474 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001475 return -1;
1476 }
1477
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001478 if (!self->bin) {
1479 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001480 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1481 "%" PY_FORMAT_SIZE_T "d\n", *value);
1482 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001483 }
1484 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001485 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001486 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001487 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001488 len = 2;
1489 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001490 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001491 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001492 pdata[1] = (unsigned char)(*value & 0xff);
1493 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1494 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1495 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001496 len = 5;
1497 }
1498 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001499 PickleState *st = _Pickle_GetGlobalState();
1500 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001501 "memo id too large for LONG_BINGET");
1502 return -1;
1503 }
1504 }
1505
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001506 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001507 return -1;
1508
1509 return 0;
1510}
1511
1512/* Store an object in the memo, assign it a new unique ID based on the number
1513 of objects currently stored in the memo and generate a PUT opcode. */
1514static int
1515memo_put(PicklerObject *self, PyObject *obj)
1516{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001517 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001518 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001519 Py_ssize_t idx;
1520
1521 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001522
1523 if (self->fast)
1524 return 0;
1525
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001526 idx = PyMemoTable_Size(self->memo);
1527 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1528 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001529
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001530 if (self->proto >= 4) {
1531 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1532 return -1;
1533 return 0;
1534 }
1535 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001536 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001537 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001538 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001539 len = strlen(pdata);
1540 }
1541 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001542 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001543 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001544 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001545 len = 2;
1546 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001547 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001548 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001549 pdata[1] = (unsigned char)(idx & 0xff);
1550 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1551 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1552 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001553 len = 5;
1554 }
1555 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001556 PickleState *st = _Pickle_GetGlobalState();
1557 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558 "memo id too large for LONG_BINPUT");
1559 return -1;
1560 }
1561 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001562 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001563 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001564
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001565 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001566}
1567
1568static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001569get_dotted_path(PyObject *obj, PyObject *name)
1570{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001571 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001572 PyObject *dotted_path;
1573 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001574
1575 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001576 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001577 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001578 n = PyList_GET_SIZE(dotted_path);
1579 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001580 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001581 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001582 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001583 if (obj == NULL)
1584 PyErr_Format(PyExc_AttributeError,
1585 "Can't pickle local object %R", name);
1586 else
1587 PyErr_Format(PyExc_AttributeError,
1588 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001589 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001590 return NULL;
1591 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001592 }
1593 return dotted_path;
1594}
1595
1596static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001597get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001598{
1599 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001600 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001601
1602 assert(PyList_CheckExact(names));
1603 Py_INCREF(obj);
1604 n = PyList_GET_SIZE(names);
1605 for (i = 0; i < n; i++) {
1606 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001607 Py_XDECREF(parent);
1608 parent = obj;
1609 obj = PyObject_GetAttr(parent, name);
1610 if (obj == NULL) {
1611 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001612 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001613 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001614 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001615 if (pparent != NULL)
1616 *pparent = parent;
1617 else
1618 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001619 return obj;
1620}
1621
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001622static void
1623reformat_attribute_error(PyObject *obj, PyObject *name)
1624{
1625 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1626 PyErr_Clear();
1627 PyErr_Format(PyExc_AttributeError,
1628 "Can't get attribute %R on %R", name, obj);
1629 }
1630}
1631
1632
1633static PyObject *
1634getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1635{
1636 PyObject *dotted_path, *attr;
1637
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001638 if (allow_qualname) {
1639 dotted_path = get_dotted_path(obj, name);
1640 if (dotted_path == NULL)
1641 return NULL;
1642 attr = get_deep_attribute(obj, dotted_path, NULL);
1643 Py_DECREF(dotted_path);
1644 }
1645 else
1646 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001647 if (attr == NULL)
1648 reformat_attribute_error(obj, name);
1649 return attr;
1650}
1651
Eric Snow3f9eee62017-09-15 16:35:20 -06001652static int
1653_checkmodule(PyObject *module_name, PyObject *module,
1654 PyObject *global, PyObject *dotted_path)
1655{
1656 if (module == Py_None) {
1657 return -1;
1658 }
1659 if (PyUnicode_Check(module_name) &&
1660 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1661 return -1;
1662 }
1663
1664 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1665 if (candidate == NULL) {
1666 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1667 PyErr_Clear();
1668 }
1669 return -1;
1670 }
1671 if (candidate != global) {
1672 Py_DECREF(candidate);
1673 return -1;
1674 }
1675 Py_DECREF(candidate);
1676 return 0;
1677}
1678
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001679static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001680whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001681{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001682 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001683 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001684 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001685 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001686 _Py_IDENTIFIER(__module__);
1687 _Py_IDENTIFIER(modules);
1688 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001689
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001690 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1691
1692 if (module_name == NULL) {
1693 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001694 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001696 }
1697 else {
1698 /* In some rare cases (e.g., bound methods of extension types),
1699 __module__ can be None. If it is so, then search sys.modules for
1700 the module of global. */
1701 if (module_name != Py_None)
1702 return module_name;
1703 Py_CLEAR(module_name);
1704 }
1705 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001706
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001707 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001708 modules = _PySys_GetObjectId(&PyId_modules);
1709 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001710 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001711 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001712 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001713 if (PyDict_CheckExact(modules)) {
1714 i = 0;
1715 while (PyDict_Next(modules, &i, &module_name, &module)) {
1716 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1717 Py_INCREF(module_name);
1718 return module_name;
1719 }
1720 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001721 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001722 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001723 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001724 }
1725 else {
1726 PyObject *iterator = PyObject_GetIter(modules);
1727 if (iterator == NULL) {
1728 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001729 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001730 while ((module_name = PyIter_Next(iterator))) {
1731 module = PyObject_GetItem(modules, module_name);
1732 if (module == NULL) {
1733 Py_DECREF(module_name);
1734 Py_DECREF(iterator);
1735 return NULL;
1736 }
1737 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1738 Py_DECREF(module);
1739 Py_DECREF(iterator);
1740 return module_name;
1741 }
1742 Py_DECREF(module);
1743 Py_DECREF(module_name);
1744 if (PyErr_Occurred()) {
1745 Py_DECREF(iterator);
1746 return NULL;
1747 }
1748 }
1749 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001750 }
1751
1752 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001753 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001754 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001755 return module_name;
1756}
1757
1758/* fast_save_enter() and fast_save_leave() are guards against recursive
1759 objects when Pickler is used with the "fast mode" (i.e., with object
1760 memoization disabled). If the nesting of a list or dict object exceed
1761 FAST_NESTING_LIMIT, these guards will start keeping an internal
1762 reference to the seen list or dict objects and check whether these objects
1763 are recursive. These are not strictly necessary, since save() has a
1764 hard-coded recursion limit, but they give a nicer error message than the
1765 typical RuntimeError. */
1766static int
1767fast_save_enter(PicklerObject *self, PyObject *obj)
1768{
1769 /* if fast_nesting < 0, we're doing an error exit. */
1770 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1771 PyObject *key = NULL;
1772 if (self->fast_memo == NULL) {
1773 self->fast_memo = PyDict_New();
1774 if (self->fast_memo == NULL) {
1775 self->fast_nesting = -1;
1776 return 0;
1777 }
1778 }
1779 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001780 if (key == NULL) {
1781 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001782 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001783 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001784 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001785 Py_DECREF(key);
1786 PyErr_Format(PyExc_ValueError,
1787 "fast mode: can't pickle cyclic objects "
1788 "including object type %.200s at %p",
1789 obj->ob_type->tp_name, obj);
1790 self->fast_nesting = -1;
1791 return 0;
1792 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001793 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001794 Py_DECREF(key);
1795 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001796 return 0;
1797 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001798 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1799 Py_DECREF(key);
1800 self->fast_nesting = -1;
1801 return 0;
1802 }
1803 Py_DECREF(key);
1804 }
1805 return 1;
1806}
1807
1808static int
1809fast_save_leave(PicklerObject *self, PyObject *obj)
1810{
1811 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1812 PyObject *key = PyLong_FromVoidPtr(obj);
1813 if (key == NULL)
1814 return 0;
1815 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1816 Py_DECREF(key);
1817 return 0;
1818 }
1819 Py_DECREF(key);
1820 }
1821 return 1;
1822}
1823
1824static int
1825save_none(PicklerObject *self, PyObject *obj)
1826{
1827 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001828 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001829 return -1;
1830
1831 return 0;
1832}
1833
1834static int
1835save_bool(PicklerObject *self, PyObject *obj)
1836{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001837 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001838 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001839 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001840 return -1;
1841 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001842 else {
1843 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1844 * so that unpicklers written before bools were introduced unpickle them
1845 * as ints, but unpicklers after can recognize that bools were intended.
1846 * Note that protocol 2 added direct ways to pickle bools.
1847 */
1848 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1849 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1850 return -1;
1851 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001852 return 0;
1853}
1854
1855static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001856save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001857{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001858 PyObject *repr = NULL;
1859 Py_ssize_t size;
1860 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001861 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001862 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001863
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001864 val= PyLong_AsLongAndOverflow(obj, &overflow);
1865 if (!overflow && (sizeof(long) <= 4 ||
1866 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1867 {
Larry Hastings61272b72014-01-07 12:41:53 -08001868 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001869
1870 Note: we can't use -0x80000000L in the above condition because some
1871 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1872 before applying the unary minus when sizeof(long) <= 4. The
1873 resulting value stays unsigned which is commonly not what we want,
1874 so MSVC happily warns us about it. However, that result would have
1875 been fine because we guard for sizeof(long) <= 4 which turns the
1876 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001877 char pdata[32];
1878 Py_ssize_t len = 0;
1879
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001880 if (self->bin) {
1881 pdata[1] = (unsigned char)(val & 0xff);
1882 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1883 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1884 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001885
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001886 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1887 pdata[0] = BININT;
1888 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001889 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001890 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001891 pdata[0] = BININT2;
1892 len = 3;
1893 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001894 else {
1895 pdata[0] = BININT1;
1896 len = 2;
1897 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001898 }
1899 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001900 sprintf(pdata, "%c%ld\n", INT, val);
1901 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001902 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001903 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001904 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001905
1906 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001907 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001908 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001909
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001910 if (self->proto >= 2) {
1911 /* Linear-time pickling. */
1912 size_t nbits;
1913 size_t nbytes;
1914 unsigned char *pdata;
1915 char header[5];
1916 int i;
1917 int sign = _PyLong_Sign(obj);
1918
1919 if (sign == 0) {
1920 header[0] = LONG1;
1921 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001922 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001923 goto error;
1924 return 0;
1925 }
1926 nbits = _PyLong_NumBits(obj);
1927 if (nbits == (size_t)-1 && PyErr_Occurred())
1928 goto error;
1929 /* How many bytes do we need? There are nbits >> 3 full
1930 * bytes of data, and nbits & 7 leftover bits. If there
1931 * are any leftover bits, then we clearly need another
1932 * byte. Wnat's not so obvious is that we *probably*
1933 * need another byte even if there aren't any leftovers:
1934 * the most-significant bit of the most-significant byte
1935 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001936 * opposite of the one we need. The exception is ints
1937 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001938 * its own 256's-complement, so has the right sign bit
1939 * even without the extra byte. That's a pain to check
1940 * for in advance, though, so we always grab an extra
1941 * byte at the start, and cut it back later if possible.
1942 */
1943 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001944 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001945 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001946 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001947 goto error;
1948 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001949 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001950 if (repr == NULL)
1951 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001952 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001953 i = _PyLong_AsByteArray((PyLongObject *)obj,
1954 pdata, nbytes,
1955 1 /* little endian */ , 1 /* signed */ );
1956 if (i < 0)
1957 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001958 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001959 * needed. This is so iff the MSB is all redundant sign
1960 * bits.
1961 */
1962 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001963 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001964 pdata[nbytes - 1] == 0xff &&
1965 (pdata[nbytes - 2] & 0x80) != 0) {
1966 nbytes--;
1967 }
1968
1969 if (nbytes < 256) {
1970 header[0] = LONG1;
1971 header[1] = (unsigned char)nbytes;
1972 size = 2;
1973 }
1974 else {
1975 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001976 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001977 for (i = 1; i < 5; i++) {
1978 header[i] = (unsigned char)(size & 0xff);
1979 size >>= 8;
1980 }
1981 size = 5;
1982 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001983 if (_Pickler_Write(self, header, size) < 0 ||
1984 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001985 goto error;
1986 }
1987 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001988 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001989 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001990
Mark Dickinson8dd05142009-01-20 20:43:58 +00001991 /* proto < 2: write the repr and newline. This is quadratic-time (in
1992 the number of digits), in both directions. We add a trailing 'L'
1993 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001994
1995 repr = PyObject_Repr(obj);
1996 if (repr == NULL)
1997 goto error;
1998
Serhiy Storchaka06515832016-11-20 09:13:07 +02001999 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002000 if (string == NULL)
2001 goto error;
2002
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002003 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2004 _Pickler_Write(self, string, size) < 0 ||
2005 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002006 goto error;
2007 }
2008
2009 if (0) {
2010 error:
2011 status = -1;
2012 }
2013 Py_XDECREF(repr);
2014
2015 return status;
2016}
2017
2018static int
2019save_float(PicklerObject *self, PyObject *obj)
2020{
2021 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2022
2023 if (self->bin) {
2024 char pdata[9];
2025 pdata[0] = BINFLOAT;
2026 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2027 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002028 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002029 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002030 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002031 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002032 int result = -1;
2033 char *buf = NULL;
2034 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002035
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002036 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002037 goto done;
2038
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002039 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002040 if (!buf) {
2041 PyErr_NoMemory();
2042 goto done;
2043 }
2044
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002045 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002046 goto done;
2047
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002048 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002049 goto done;
2050
2051 result = 0;
2052done:
2053 PyMem_Free(buf);
2054 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002055 }
2056
2057 return 0;
2058}
2059
2060static int
2061save_bytes(PicklerObject *self, PyObject *obj)
2062{
2063 if (self->proto < 3) {
2064 /* Older pickle protocols do not have an opcode for pickling bytes
2065 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002066 the __reduce__ method) to permit bytes object unpickling.
2067
2068 Here we use a hack to be compatible with Python 2. Since in Python
2069 2 'bytes' is just an alias for 'str' (which has different
2070 parameters than the actual bytes object), we use codecs.encode
2071 to create the appropriate 'str' object when unpickled using
2072 Python 2 *and* the appropriate 'bytes' object when unpickled
2073 using Python 3. Again this is a hack and we don't need to do this
2074 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002075 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 int status;
2077
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002078 if (PyBytes_GET_SIZE(obj) == 0) {
2079 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2080 }
2081 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002082 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002083 PyObject *unicode_str =
2084 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2085 PyBytes_GET_SIZE(obj),
2086 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002087 _Py_IDENTIFIER(latin1);
2088
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002089 if (unicode_str == NULL)
2090 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002091 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002092 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002093 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002094 Py_DECREF(unicode_str);
2095 }
2096
2097 if (reduce_value == NULL)
2098 return -1;
2099
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002100 /* save_reduce() will memoize the object automatically. */
2101 status = save_reduce(self, reduce_value, obj);
2102 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002103 return status;
2104 }
2105 else {
2106 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002107 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002108 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002109
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002110 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002111 if (size < 0)
2112 return -1;
2113
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002114 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002115 header[0] = SHORT_BINBYTES;
2116 header[1] = (unsigned char)size;
2117 len = 2;
2118 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002119 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002120 header[0] = BINBYTES;
2121 header[1] = (unsigned char)(size & 0xff);
2122 header[2] = (unsigned char)((size >> 8) & 0xff);
2123 header[3] = (unsigned char)((size >> 16) & 0xff);
2124 header[4] = (unsigned char)((size >> 24) & 0xff);
2125 len = 5;
2126 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002127 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002128 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002129 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002130 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002131 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002132 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002133 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002134 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002135 return -1; /* string too large */
2136 }
2137
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002138 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002139 return -1;
2140
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002141 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002142 return -1;
2143
2144 if (memo_put(self, obj) < 0)
2145 return -1;
2146
2147 return 0;
2148 }
2149}
2150
2151/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2152 backslash and newline characters to \uXXXX escapes. */
2153static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002154raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002155{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002156 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002157 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002158 void *data;
2159 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002160 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002161
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002162 if (PyUnicode_READY(obj))
2163 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002164
Victor Stinner358af132015-10-12 22:36:57 +02002165 _PyBytesWriter_Init(&writer);
2166
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002167 size = PyUnicode_GET_LENGTH(obj);
2168 data = PyUnicode_DATA(obj);
2169 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002170
Victor Stinner358af132015-10-12 22:36:57 +02002171 p = _PyBytesWriter_Alloc(&writer, size);
2172 if (p == NULL)
2173 goto error;
2174 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002175
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002176 for (i=0; i < size; i++) {
2177 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002178 /* Map 32-bit characters to '\Uxxxxxxxx' */
2179 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002180 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002181 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2182 if (p == NULL)
2183 goto error;
2184
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002185 *p++ = '\\';
2186 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002187 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2188 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2189 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2190 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2191 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2192 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2193 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2194 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002195 }
Victor Stinner358af132015-10-12 22:36:57 +02002196 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002197 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002198 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002199 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2200 if (p == NULL)
2201 goto error;
2202
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002203 *p++ = '\\';
2204 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002205 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2206 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2207 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2208 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002209 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002210 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002211 else
2212 *p++ = (char) ch;
2213 }
Victor Stinner358af132015-10-12 22:36:57 +02002214
2215 return _PyBytesWriter_Finish(&writer, p);
2216
2217error:
2218 _PyBytesWriter_Dealloc(&writer);
2219 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002220}
2221
2222static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002223write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002224{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002225 char header[9];
2226 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002227
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002228 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002229 if (size <= 0xff && self->proto >= 4) {
2230 header[0] = SHORT_BINUNICODE;
2231 header[1] = (unsigned char)(size & 0xff);
2232 len = 2;
2233 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002234 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002235 header[0] = BINUNICODE;
2236 header[1] = (unsigned char)(size & 0xff);
2237 header[2] = (unsigned char)((size >> 8) & 0xff);
2238 header[3] = (unsigned char)((size >> 16) & 0xff);
2239 header[4] = (unsigned char)((size >> 24) & 0xff);
2240 len = 5;
2241 }
2242 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002243 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002244 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002245 len = 9;
2246 }
2247 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002248 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002249 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002250 return -1;
2251 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002252
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002253 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002254 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002255 if (_Pickler_Write(self, data, size) < 0)
2256 return -1;
2257
2258 return 0;
2259}
2260
2261static int
2262write_unicode_binary(PicklerObject *self, PyObject *obj)
2263{
2264 PyObject *encoded = NULL;
2265 Py_ssize_t size;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002266 const char *data;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002267 int r;
2268
2269 if (PyUnicode_READY(obj))
2270 return -1;
2271
2272 data = PyUnicode_AsUTF8AndSize(obj, &size);
2273 if (data != NULL)
2274 return write_utf8(self, data, size);
2275
2276 /* Issue #8383: for strings with lone surrogates, fallback on the
2277 "surrogatepass" error handler. */
2278 PyErr_Clear();
2279 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2280 if (encoded == NULL)
2281 return -1;
2282
2283 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2284 PyBytes_GET_SIZE(encoded));
2285 Py_DECREF(encoded);
2286 return r;
2287}
2288
2289static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002290save_unicode(PicklerObject *self, PyObject *obj)
2291{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002292 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002293 if (write_unicode_binary(self, obj) < 0)
2294 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295 }
2296 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002297 PyObject *encoded;
2298 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002299 const char unicode_op = UNICODE;
2300
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002301 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002302 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002303 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002304
Antoine Pitrou299978d2013-04-07 17:38:11 +02002305 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2306 Py_DECREF(encoded);
2307 return -1;
2308 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002309
2310 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002311 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2312 Py_DECREF(encoded);
2313 return -1;
2314 }
2315 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002316
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002317 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002318 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002319 }
2320 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002321 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002322
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002323 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002324}
2325
2326/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2327static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002328store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002329{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002330 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002331
2332 assert(PyTuple_Size(t) == len);
2333
2334 for (i = 0; i < len; i++) {
2335 PyObject *element = PyTuple_GET_ITEM(t, i);
2336
2337 if (element == NULL)
2338 return -1;
2339 if (save(self, element, 0) < 0)
2340 return -1;
2341 }
2342
2343 return 0;
2344}
2345
2346/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2347 * used across protocols to minimize the space needed to pickle them.
2348 * Tuples are also the only builtin immutable type that can be recursive
2349 * (a tuple can be reached from itself), and that requires some subtle
2350 * magic so that it works in all cases. IOW, this is a long routine.
2351 */
2352static int
2353save_tuple(PicklerObject *self, PyObject *obj)
2354{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002355 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002356
2357 const char mark_op = MARK;
2358 const char tuple_op = TUPLE;
2359 const char pop_op = POP;
2360 const char pop_mark_op = POP_MARK;
2361 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2362
2363 if ((len = PyTuple_Size(obj)) < 0)
2364 return -1;
2365
2366 if (len == 0) {
2367 char pdata[2];
2368
2369 if (self->proto) {
2370 pdata[0] = EMPTY_TUPLE;
2371 len = 1;
2372 }
2373 else {
2374 pdata[0] = MARK;
2375 pdata[1] = TUPLE;
2376 len = 2;
2377 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002378 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002379 return -1;
2380 return 0;
2381 }
2382
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002383 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002384 * saving the tuple elements, the tuple must be recursive, in
2385 * which case we'll pop everything we put on the stack, and fetch
2386 * its value from the memo.
2387 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002388 if (len <= 3 && self->proto >= 2) {
2389 /* Use TUPLE{1,2,3} opcodes. */
2390 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002391 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002392
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002393 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002394 /* pop the len elements */
2395 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002396 if (_Pickler_Write(self, &pop_op, 1) < 0)
2397 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002398 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002399 if (memo_get(self, obj) < 0)
2400 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002401
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002402 return 0;
2403 }
2404 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002405 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2406 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002407 }
2408 goto memoize;
2409 }
2410
2411 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2412 * Generate MARK e1 e2 ... TUPLE
2413 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002414 if (_Pickler_Write(self, &mark_op, 1) < 0)
2415 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002416
2417 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002418 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002419
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002420 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002421 /* pop the stack stuff we pushed */
2422 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002423 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2424 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002425 }
2426 else {
2427 /* Note that we pop one more than len, to remove
2428 * the MARK too.
2429 */
2430 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002431 if (_Pickler_Write(self, &pop_op, 1) < 0)
2432 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002433 }
2434 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002435 if (memo_get(self, obj) < 0)
2436 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002437
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002438 return 0;
2439 }
2440 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002441 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2442 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002443 }
2444
2445 memoize:
2446 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002447 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002448
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002449 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002450}
2451
2452/* iter is an iterator giving items, and we batch up chunks of
2453 * MARK item item ... item APPENDS
2454 * opcode sequences. Calling code should have arranged to first create an
2455 * empty list, or list-like object, for the APPENDS to operate on.
2456 * Returns 0 on success, <0 on error.
2457 */
2458static int
2459batch_list(PicklerObject *self, PyObject *iter)
2460{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002461 PyObject *obj = NULL;
2462 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002463 int i, n;
2464
2465 const char mark_op = MARK;
2466 const char append_op = APPEND;
2467 const char appends_op = APPENDS;
2468
2469 assert(iter != NULL);
2470
2471 /* XXX: I think this function could be made faster by avoiding the
2472 iterator interface and fetching objects directly from list using
2473 PyList_GET_ITEM.
2474 */
2475
2476 if (self->proto == 0) {
2477 /* APPENDS isn't available; do one at a time. */
2478 for (;;) {
2479 obj = PyIter_Next(iter);
2480 if (obj == NULL) {
2481 if (PyErr_Occurred())
2482 return -1;
2483 break;
2484 }
2485 i = save(self, obj, 0);
2486 Py_DECREF(obj);
2487 if (i < 0)
2488 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002489 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002490 return -1;
2491 }
2492 return 0;
2493 }
2494
2495 /* proto > 0: write in batches of BATCHSIZE. */
2496 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002497 /* Get first item */
2498 firstitem = PyIter_Next(iter);
2499 if (firstitem == NULL) {
2500 if (PyErr_Occurred())
2501 goto error;
2502
2503 /* nothing more to add */
2504 break;
2505 }
2506
2507 /* Try to get a second item */
2508 obj = PyIter_Next(iter);
2509 if (obj == NULL) {
2510 if (PyErr_Occurred())
2511 goto error;
2512
2513 /* Only one item to write */
2514 if (save(self, firstitem, 0) < 0)
2515 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002516 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002517 goto error;
2518 Py_CLEAR(firstitem);
2519 break;
2520 }
2521
2522 /* More than one item to write */
2523
2524 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002525 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002526 goto error;
2527
2528 if (save(self, firstitem, 0) < 0)
2529 goto error;
2530 Py_CLEAR(firstitem);
2531 n = 1;
2532
2533 /* Fetch and save up to BATCHSIZE items */
2534 while (obj) {
2535 if (save(self, obj, 0) < 0)
2536 goto error;
2537 Py_CLEAR(obj);
2538 n += 1;
2539
2540 if (n == BATCHSIZE)
2541 break;
2542
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002543 obj = PyIter_Next(iter);
2544 if (obj == NULL) {
2545 if (PyErr_Occurred())
2546 goto error;
2547 break;
2548 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002549 }
2550
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002551 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002552 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002553
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002554 } while (n == BATCHSIZE);
2555 return 0;
2556
2557 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002558 Py_XDECREF(firstitem);
2559 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002560 return -1;
2561}
2562
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002563/* This is a variant of batch_list() above, specialized for lists (with no
2564 * support for list subclasses). Like batch_list(), we batch up chunks of
2565 * MARK item item ... item APPENDS
2566 * opcode sequences. Calling code should have arranged to first create an
2567 * empty list, or list-like object, for the APPENDS to operate on.
2568 * Returns 0 on success, -1 on error.
2569 *
2570 * This version is considerably faster than batch_list(), if less general.
2571 *
2572 * Note that this only works for protocols > 0.
2573 */
2574static int
2575batch_list_exact(PicklerObject *self, PyObject *obj)
2576{
2577 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002578 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002579
2580 const char append_op = APPEND;
2581 const char appends_op = APPENDS;
2582 const char mark_op = MARK;
2583
2584 assert(obj != NULL);
2585 assert(self->proto > 0);
2586 assert(PyList_CheckExact(obj));
2587
2588 if (PyList_GET_SIZE(obj) == 1) {
2589 item = PyList_GET_ITEM(obj, 0);
2590 if (save(self, item, 0) < 0)
2591 return -1;
2592 if (_Pickler_Write(self, &append_op, 1) < 0)
2593 return -1;
2594 return 0;
2595 }
2596
2597 /* Write in batches of BATCHSIZE. */
2598 total = 0;
2599 do {
2600 this_batch = 0;
2601 if (_Pickler_Write(self, &mark_op, 1) < 0)
2602 return -1;
2603 while (total < PyList_GET_SIZE(obj)) {
2604 item = PyList_GET_ITEM(obj, total);
2605 if (save(self, item, 0) < 0)
2606 return -1;
2607 total++;
2608 if (++this_batch == BATCHSIZE)
2609 break;
2610 }
2611 if (_Pickler_Write(self, &appends_op, 1) < 0)
2612 return -1;
2613
2614 } while (total < PyList_GET_SIZE(obj));
2615
2616 return 0;
2617}
2618
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002619static int
2620save_list(PicklerObject *self, PyObject *obj)
2621{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002622 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002623 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002624 int status = 0;
2625
2626 if (self->fast && !fast_save_enter(self, obj))
2627 goto error;
2628
2629 /* Create an empty list. */
2630 if (self->bin) {
2631 header[0] = EMPTY_LIST;
2632 len = 1;
2633 }
2634 else {
2635 header[0] = MARK;
2636 header[1] = LIST;
2637 len = 2;
2638 }
2639
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002640 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002641 goto error;
2642
2643 /* Get list length, and bow out early if empty. */
2644 if ((len = PyList_Size(obj)) < 0)
2645 goto error;
2646
2647 if (memo_put(self, obj) < 0)
2648 goto error;
2649
2650 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002651 /* Materialize the list elements. */
2652 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002653 if (Py_EnterRecursiveCall(" while pickling an object"))
2654 goto error;
2655 status = batch_list_exact(self, obj);
2656 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002657 } else {
2658 PyObject *iter = PyObject_GetIter(obj);
2659 if (iter == NULL)
2660 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002661
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002662 if (Py_EnterRecursiveCall(" while pickling an object")) {
2663 Py_DECREF(iter);
2664 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002665 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002666 status = batch_list(self, iter);
2667 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002668 Py_DECREF(iter);
2669 }
2670 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002671 if (0) {
2672 error:
2673 status = -1;
2674 }
2675
2676 if (self->fast && !fast_save_leave(self, obj))
2677 status = -1;
2678
2679 return status;
2680}
2681
2682/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2683 * MARK key value ... key value SETITEMS
2684 * opcode sequences. Calling code should have arranged to first create an
2685 * empty dict, or dict-like object, for the SETITEMS to operate on.
2686 * Returns 0 on success, <0 on error.
2687 *
2688 * This is very much like batch_list(). The difference between saving
2689 * elements directly, and picking apart two-tuples, is so long-winded at
2690 * the C level, though, that attempts to combine these routines were too
2691 * ugly to bear.
2692 */
2693static int
2694batch_dict(PicklerObject *self, PyObject *iter)
2695{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002696 PyObject *obj = NULL;
2697 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002698 int i, n;
2699
2700 const char mark_op = MARK;
2701 const char setitem_op = SETITEM;
2702 const char setitems_op = SETITEMS;
2703
2704 assert(iter != NULL);
2705
2706 if (self->proto == 0) {
2707 /* SETITEMS isn't available; do one at a time. */
2708 for (;;) {
2709 obj = PyIter_Next(iter);
2710 if (obj == NULL) {
2711 if (PyErr_Occurred())
2712 return -1;
2713 break;
2714 }
2715 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2716 PyErr_SetString(PyExc_TypeError, "dict items "
2717 "iterator must return 2-tuples");
2718 return -1;
2719 }
2720 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2721 if (i >= 0)
2722 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2723 Py_DECREF(obj);
2724 if (i < 0)
2725 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002726 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002727 return -1;
2728 }
2729 return 0;
2730 }
2731
2732 /* proto > 0: write in batches of BATCHSIZE. */
2733 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002734 /* Get first item */
2735 firstitem = PyIter_Next(iter);
2736 if (firstitem == NULL) {
2737 if (PyErr_Occurred())
2738 goto error;
2739
2740 /* nothing more to add */
2741 break;
2742 }
2743 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2744 PyErr_SetString(PyExc_TypeError, "dict items "
2745 "iterator must return 2-tuples");
2746 goto error;
2747 }
2748
2749 /* Try to get a second item */
2750 obj = PyIter_Next(iter);
2751 if (obj == NULL) {
2752 if (PyErr_Occurred())
2753 goto error;
2754
2755 /* Only one item to write */
2756 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2757 goto error;
2758 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2759 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002760 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002761 goto error;
2762 Py_CLEAR(firstitem);
2763 break;
2764 }
2765
2766 /* More than one item to write */
2767
2768 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002769 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002770 goto error;
2771
2772 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2773 goto error;
2774 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2775 goto error;
2776 Py_CLEAR(firstitem);
2777 n = 1;
2778
2779 /* Fetch and save up to BATCHSIZE items */
2780 while (obj) {
2781 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2782 PyErr_SetString(PyExc_TypeError, "dict items "
2783 "iterator must return 2-tuples");
2784 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002785 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002786 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2787 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2788 goto error;
2789 Py_CLEAR(obj);
2790 n += 1;
2791
2792 if (n == BATCHSIZE)
2793 break;
2794
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002795 obj = PyIter_Next(iter);
2796 if (obj == NULL) {
2797 if (PyErr_Occurred())
2798 goto error;
2799 break;
2800 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002801 }
2802
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002803 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002804 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002805
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002806 } while (n == BATCHSIZE);
2807 return 0;
2808
2809 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002810 Py_XDECREF(firstitem);
2811 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002812 return -1;
2813}
2814
Collin Winter5c9b02d2009-05-25 05:43:30 +00002815/* This is a variant of batch_dict() above that specializes for dicts, with no
2816 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2817 * MARK key value ... key value SETITEMS
2818 * opcode sequences. Calling code should have arranged to first create an
2819 * empty dict, or dict-like object, for the SETITEMS to operate on.
2820 * Returns 0 on success, -1 on error.
2821 *
2822 * Note that this currently doesn't work for protocol 0.
2823 */
2824static int
2825batch_dict_exact(PicklerObject *self, PyObject *obj)
2826{
2827 PyObject *key = NULL, *value = NULL;
2828 int i;
2829 Py_ssize_t dict_size, ppos = 0;
2830
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002831 const char mark_op = MARK;
2832 const char setitem_op = SETITEM;
2833 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002834
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002835 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002836 assert(self->proto > 0);
2837
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002838 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002839
2840 /* Special-case len(d) == 1 to save space. */
2841 if (dict_size == 1) {
2842 PyDict_Next(obj, &ppos, &key, &value);
2843 if (save(self, key, 0) < 0)
2844 return -1;
2845 if (save(self, value, 0) < 0)
2846 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002847 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002848 return -1;
2849 return 0;
2850 }
2851
2852 /* Write in batches of BATCHSIZE. */
2853 do {
2854 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002855 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002856 return -1;
2857 while (PyDict_Next(obj, &ppos, &key, &value)) {
2858 if (save(self, key, 0) < 0)
2859 return -1;
2860 if (save(self, value, 0) < 0)
2861 return -1;
2862 if (++i == BATCHSIZE)
2863 break;
2864 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002865 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002866 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002867 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00002868 PyErr_Format(
2869 PyExc_RuntimeError,
2870 "dictionary changed size during iteration");
2871 return -1;
2872 }
2873
2874 } while (i == BATCHSIZE);
2875 return 0;
2876}
2877
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002878static int
2879save_dict(PicklerObject *self, PyObject *obj)
2880{
2881 PyObject *items, *iter;
2882 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002883 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002884 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002885 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002886
2887 if (self->fast && !fast_save_enter(self, obj))
2888 goto error;
2889
2890 /* Create an empty dict. */
2891 if (self->bin) {
2892 header[0] = EMPTY_DICT;
2893 len = 1;
2894 }
2895 else {
2896 header[0] = MARK;
2897 header[1] = DICT;
2898 len = 2;
2899 }
2900
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002901 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002902 goto error;
2903
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002904 if (memo_put(self, obj) < 0)
2905 goto error;
2906
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002907 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002908 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002909 if (PyDict_CheckExact(obj) && self->proto > 0) {
2910 /* We can take certain shortcuts if we know this is a dict and
2911 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002912 if (Py_EnterRecursiveCall(" while pickling an object"))
2913 goto error;
2914 status = batch_dict_exact(self, obj);
2915 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002916 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002917 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002918
Victor Stinnerad8c83a2016-09-05 17:53:15 -07002919 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002920 if (items == NULL)
2921 goto error;
2922 iter = PyObject_GetIter(items);
2923 Py_DECREF(items);
2924 if (iter == NULL)
2925 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002926 if (Py_EnterRecursiveCall(" while pickling an object")) {
2927 Py_DECREF(iter);
2928 goto error;
2929 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002930 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002931 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002932 Py_DECREF(iter);
2933 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002934 }
2935
2936 if (0) {
2937 error:
2938 status = -1;
2939 }
2940
2941 if (self->fast && !fast_save_leave(self, obj))
2942 status = -1;
2943
2944 return status;
2945}
2946
2947static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002948save_set(PicklerObject *self, PyObject *obj)
2949{
2950 PyObject *item;
2951 int i;
2952 Py_ssize_t set_size, ppos = 0;
2953 Py_hash_t hash;
2954
2955 const char empty_set_op = EMPTY_SET;
2956 const char mark_op = MARK;
2957 const char additems_op = ADDITEMS;
2958
2959 if (self->proto < 4) {
2960 PyObject *items;
2961 PyObject *reduce_value;
2962 int status;
2963
2964 items = PySequence_List(obj);
2965 if (items == NULL) {
2966 return -1;
2967 }
2968 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2969 Py_DECREF(items);
2970 if (reduce_value == NULL) {
2971 return -1;
2972 }
2973 /* save_reduce() will memoize the object automatically. */
2974 status = save_reduce(self, reduce_value, obj);
2975 Py_DECREF(reduce_value);
2976 return status;
2977 }
2978
2979 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2980 return -1;
2981
2982 if (memo_put(self, obj) < 0)
2983 return -1;
2984
2985 set_size = PySet_GET_SIZE(obj);
2986 if (set_size == 0)
2987 return 0; /* nothing to do */
2988
2989 /* Write in batches of BATCHSIZE. */
2990 do {
2991 i = 0;
2992 if (_Pickler_Write(self, &mark_op, 1) < 0)
2993 return -1;
2994 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2995 if (save(self, item, 0) < 0)
2996 return -1;
2997 if (++i == BATCHSIZE)
2998 break;
2999 }
3000 if (_Pickler_Write(self, &additems_op, 1) < 0)
3001 return -1;
3002 if (PySet_GET_SIZE(obj) != set_size) {
3003 PyErr_Format(
3004 PyExc_RuntimeError,
3005 "set changed size during iteration");
3006 return -1;
3007 }
3008 } while (i == BATCHSIZE);
3009
3010 return 0;
3011}
3012
3013static int
3014save_frozenset(PicklerObject *self, PyObject *obj)
3015{
3016 PyObject *iter;
3017
3018 const char mark_op = MARK;
3019 const char frozenset_op = FROZENSET;
3020
3021 if (self->fast && !fast_save_enter(self, obj))
3022 return -1;
3023
3024 if (self->proto < 4) {
3025 PyObject *items;
3026 PyObject *reduce_value;
3027 int status;
3028
3029 items = PySequence_List(obj);
3030 if (items == NULL) {
3031 return -1;
3032 }
3033 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3034 items);
3035 Py_DECREF(items);
3036 if (reduce_value == NULL) {
3037 return -1;
3038 }
3039 /* save_reduce() will memoize the object automatically. */
3040 status = save_reduce(self, reduce_value, obj);
3041 Py_DECREF(reduce_value);
3042 return status;
3043 }
3044
3045 if (_Pickler_Write(self, &mark_op, 1) < 0)
3046 return -1;
3047
3048 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003049 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003050 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003051 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003052 for (;;) {
3053 PyObject *item;
3054
3055 item = PyIter_Next(iter);
3056 if (item == NULL) {
3057 if (PyErr_Occurred()) {
3058 Py_DECREF(iter);
3059 return -1;
3060 }
3061 break;
3062 }
3063 if (save(self, item, 0) < 0) {
3064 Py_DECREF(item);
3065 Py_DECREF(iter);
3066 return -1;
3067 }
3068 Py_DECREF(item);
3069 }
3070 Py_DECREF(iter);
3071
3072 /* If the object is already in the memo, this means it is
3073 recursive. In this case, throw away everything we put on the
3074 stack, and fetch the object back from the memo. */
3075 if (PyMemoTable_Get(self->memo, obj)) {
3076 const char pop_mark_op = POP_MARK;
3077
3078 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3079 return -1;
3080 if (memo_get(self, obj) < 0)
3081 return -1;
3082 return 0;
3083 }
3084
3085 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3086 return -1;
3087 if (memo_put(self, obj) < 0)
3088 return -1;
3089
3090 return 0;
3091}
3092
3093static int
3094fix_imports(PyObject **module_name, PyObject **global_name)
3095{
3096 PyObject *key;
3097 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003098 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003099
3100 key = PyTuple_Pack(2, *module_name, *global_name);
3101 if (key == NULL)
3102 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003103 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003104 Py_DECREF(key);
3105 if (item) {
3106 PyObject *fixed_module_name;
3107 PyObject *fixed_global_name;
3108
3109 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3110 PyErr_Format(PyExc_RuntimeError,
3111 "_compat_pickle.REVERSE_NAME_MAPPING values "
3112 "should be 2-tuples, not %.200s",
3113 Py_TYPE(item)->tp_name);
3114 return -1;
3115 }
3116 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3117 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3118 if (!PyUnicode_Check(fixed_module_name) ||
3119 !PyUnicode_Check(fixed_global_name)) {
3120 PyErr_Format(PyExc_RuntimeError,
3121 "_compat_pickle.REVERSE_NAME_MAPPING values "
3122 "should be pairs of str, not (%.200s, %.200s)",
3123 Py_TYPE(fixed_module_name)->tp_name,
3124 Py_TYPE(fixed_global_name)->tp_name);
3125 return -1;
3126 }
3127
3128 Py_CLEAR(*module_name);
3129 Py_CLEAR(*global_name);
3130 Py_INCREF(fixed_module_name);
3131 Py_INCREF(fixed_global_name);
3132 *module_name = fixed_module_name;
3133 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003134 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003135 }
3136 else if (PyErr_Occurred()) {
3137 return -1;
3138 }
3139
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003140 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003141 if (item) {
3142 if (!PyUnicode_Check(item)) {
3143 PyErr_Format(PyExc_RuntimeError,
3144 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3145 "should be strings, not %.200s",
3146 Py_TYPE(item)->tp_name);
3147 return -1;
3148 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003149 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003150 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003151 }
3152 else if (PyErr_Occurred()) {
3153 return -1;
3154 }
3155
3156 return 0;
3157}
3158
3159static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003160save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3161{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003162 PyObject *global_name = NULL;
3163 PyObject *module_name = NULL;
3164 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003165 PyObject *parent = NULL;
3166 PyObject *dotted_path = NULL;
3167 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003168 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003169 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003170 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003171 _Py_IDENTIFIER(__name__);
3172 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003173
3174 const char global_op = GLOBAL;
3175
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003176 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003177 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003178 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003179 }
3180 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003181 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3182 if (global_name == NULL) {
3183 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3184 goto error;
3185 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003186 }
3187 if (global_name == NULL) {
3188 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3189 if (global_name == NULL)
3190 goto error;
3191 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003192 }
3193
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003194 dotted_path = get_dotted_path(module, global_name);
3195 if (dotted_path == NULL)
3196 goto error;
3197 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003198 if (module_name == NULL)
3199 goto error;
3200
3201 /* XXX: Change to use the import C API directly with level=0 to disallow
3202 relative imports.
3203
3204 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3205 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3206 custom import functions (IMHO, this would be a nice security
3207 feature). The import C API would need to be extended to support the
3208 extra parameters of __import__ to fix that. */
3209 module = PyImport_Import(module_name);
3210 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003211 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003212 "Can't pickle %R: import of module %R failed",
3213 obj, module_name);
3214 goto error;
3215 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003216 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3217 Py_INCREF(lastname);
3218 cls = get_deep_attribute(module, dotted_path, &parent);
3219 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003220 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003221 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003222 "Can't pickle %R: attribute lookup %S on %S failed",
3223 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003224 goto error;
3225 }
3226 if (cls != obj) {
3227 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003228 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003229 "Can't pickle %R: it's not the same object as %S.%S",
3230 obj, module_name, global_name);
3231 goto error;
3232 }
3233 Py_DECREF(cls);
3234
3235 if (self->proto >= 2) {
3236 /* See whether this is in the extension registry, and if
3237 * so generate an EXT opcode.
3238 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003239 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003240 PyObject *code_obj; /* extension code as Python object */
3241 long code; /* extension code as C value */
3242 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003243 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003244
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003245 extension_key = PyTuple_Pack(2, module_name, global_name);
3246 if (extension_key == NULL) {
3247 goto error;
3248 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003249 code_obj = PyDict_GetItemWithError(st->extension_registry,
3250 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003251 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003252 /* The object is not registered in the extension registry.
3253 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003254 if (code_obj == NULL) {
3255 if (PyErr_Occurred()) {
3256 goto error;
3257 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003258 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003259 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003260
3261 /* XXX: pickle.py doesn't check neither the type, nor the range
3262 of the value returned by the extension_registry. It should for
3263 consistency. */
3264
3265 /* Verify code_obj has the right type and value. */
3266 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003267 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003268 "Can't pickle %R: extension code %R isn't an integer",
3269 obj, code_obj);
3270 goto error;
3271 }
3272 code = PyLong_AS_LONG(code_obj);
3273 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003274 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003275 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3276 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003277 goto error;
3278 }
3279
3280 /* Generate an EXT opcode. */
3281 if (code <= 0xff) {
3282 pdata[0] = EXT1;
3283 pdata[1] = (unsigned char)code;
3284 n = 2;
3285 }
3286 else if (code <= 0xffff) {
3287 pdata[0] = EXT2;
3288 pdata[1] = (unsigned char)(code & 0xff);
3289 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3290 n = 3;
3291 }
3292 else {
3293 pdata[0] = EXT4;
3294 pdata[1] = (unsigned char)(code & 0xff);
3295 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3296 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3297 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3298 n = 5;
3299 }
3300
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003301 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003302 goto error;
3303 }
3304 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003305 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003306 if (parent == module) {
3307 Py_INCREF(lastname);
3308 Py_DECREF(global_name);
3309 global_name = lastname;
3310 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003311 if (self->proto >= 4) {
3312 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003313
Christian Heimese8b1ba12013-11-23 21:13:39 +01003314 if (save(self, module_name, 0) < 0)
3315 goto error;
3316 if (save(self, global_name, 0) < 0)
3317 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003318
3319 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3320 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003321 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003322 else if (parent != module) {
3323 PickleState *st = _Pickle_GetGlobalState();
3324 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3325 st->getattr, parent, lastname);
3326 status = save_reduce(self, reduce_value, NULL);
3327 Py_DECREF(reduce_value);
3328 if (status < 0)
3329 goto error;
3330 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003331 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003332 /* Generate a normal global opcode if we are using a pickle
3333 protocol < 4, or if the object is not registered in the
3334 extension registry. */
3335 PyObject *encoded;
3336 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003337
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003338 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003339 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003340
3341 /* For protocol < 3 and if the user didn't request against doing
3342 so, we convert module names to the old 2.x module names. */
3343 if (self->proto < 3 && self->fix_imports) {
3344 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003345 goto error;
3346 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003347 }
3348
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003349 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3350 both the module name and the global name using UTF-8. We do so
3351 only when we are using the pickle protocol newer than version
3352 3. This is to ensure compatibility with older Unpickler running
3353 on Python 2.x. */
3354 if (self->proto == 3) {
3355 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003356 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003357 else {
3358 unicode_encoder = PyUnicode_AsASCIIString;
3359 }
3360 encoded = unicode_encoder(module_name);
3361 if (encoded == NULL) {
3362 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003363 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003364 "can't pickle module identifier '%S' using "
3365 "pickle protocol %i",
3366 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003367 goto error;
3368 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003369 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3370 PyBytes_GET_SIZE(encoded)) < 0) {
3371 Py_DECREF(encoded);
3372 goto error;
3373 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003374 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003375 if(_Pickler_Write(self, "\n", 1) < 0)
3376 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003377
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003378 /* Save the name of the module. */
3379 encoded = unicode_encoder(global_name);
3380 if (encoded == NULL) {
3381 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003382 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003383 "can't pickle global identifier '%S' using "
3384 "pickle protocol %i",
3385 global_name, self->proto);
3386 goto error;
3387 }
3388 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3389 PyBytes_GET_SIZE(encoded)) < 0) {
3390 Py_DECREF(encoded);
3391 goto error;
3392 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003393 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003394 if (_Pickler_Write(self, "\n", 1) < 0)
3395 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003396 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003397 /* Memoize the object. */
3398 if (memo_put(self, obj) < 0)
3399 goto error;
3400 }
3401
3402 if (0) {
3403 error:
3404 status = -1;
3405 }
3406 Py_XDECREF(module_name);
3407 Py_XDECREF(global_name);
3408 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003409 Py_XDECREF(parent);
3410 Py_XDECREF(dotted_path);
3411 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003412
3413 return status;
3414}
3415
3416static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003417save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3418{
3419 PyObject *reduce_value;
3420 int status;
3421
3422 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3423 if (reduce_value == NULL) {
3424 return -1;
3425 }
3426 status = save_reduce(self, reduce_value, obj);
3427 Py_DECREF(reduce_value);
3428 return status;
3429}
3430
3431static int
3432save_type(PicklerObject *self, PyObject *obj)
3433{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003434 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003435 return save_singleton_type(self, obj, Py_None);
3436 }
3437 else if (obj == (PyObject *)&PyEllipsis_Type) {
3438 return save_singleton_type(self, obj, Py_Ellipsis);
3439 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003440 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003441 return save_singleton_type(self, obj, Py_NotImplemented);
3442 }
3443 return save_global(self, obj, NULL);
3444}
3445
3446static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003447save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3448{
3449 PyObject *pid = NULL;
3450 int status = 0;
3451
3452 const char persid_op = PERSID;
3453 const char binpersid_op = BINPERSID;
3454
3455 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003456 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003457 if (pid == NULL)
3458 return -1;
3459
3460 if (pid != Py_None) {
3461 if (self->bin) {
3462 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003463 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003464 goto error;
3465 }
3466 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003467 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003468
3469 pid_str = PyObject_Str(pid);
3470 if (pid_str == NULL)
3471 goto error;
3472
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003473 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003474 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003475 if (!PyUnicode_IS_ASCII(pid_str)) {
3476 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3477 "persistent IDs in protocol 0 must be "
3478 "ASCII strings");
3479 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003480 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003481 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003482
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003483 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003484 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3485 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3486 _Pickler_Write(self, "\n", 1) < 0) {
3487 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003488 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003489 }
3490 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003491 }
3492 status = 1;
3493 }
3494
3495 if (0) {
3496 error:
3497 status = -1;
3498 }
3499 Py_XDECREF(pid);
3500
3501 return status;
3502}
3503
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003504static PyObject *
3505get_class(PyObject *obj)
3506{
3507 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003508 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003509
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003510 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003511 if (cls == NULL) {
3512 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3513 PyErr_Clear();
3514 cls = (PyObject *) Py_TYPE(obj);
3515 Py_INCREF(cls);
3516 }
3517 }
3518 return cls;
3519}
3520
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003521/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3522 * appropriate __reduce__ method for obj.
3523 */
3524static int
3525save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3526{
3527 PyObject *callable;
3528 PyObject *argtup;
3529 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003530 PyObject *listitems = Py_None;
3531 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003532 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003533 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003534 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003535
3536 const char reduce_op = REDUCE;
3537 const char build_op = BUILD;
3538 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003539 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003540
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003541 size = PyTuple_Size(args);
3542 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003543 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003544 "__reduce__ must contain 2 through 5 elements");
3545 return -1;
3546 }
3547
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003548 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3549 &callable, &argtup, &state, &listitems, &dictitems))
3550 return -1;
3551
3552 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003553 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003554 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003555 return -1;
3556 }
3557 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003558 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003559 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003560 return -1;
3561 }
3562
3563 if (state == Py_None)
3564 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003565
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003566 if (listitems == Py_None)
3567 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003568 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003569 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003570 "returned by __reduce__ must be an iterator, not %s",
3571 Py_TYPE(listitems)->tp_name);
3572 return -1;
3573 }
3574
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003575 if (dictitems == Py_None)
3576 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003577 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003578 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003579 "returned by __reduce__ must be an iterator, not %s",
3580 Py_TYPE(dictitems)->tp_name);
3581 return -1;
3582 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003583
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003584 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003585 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003586 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003587
Victor Stinner804e05e2013-11-14 01:26:17 +01003588 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003589 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003590 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003591 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003592 }
3593 PyErr_Clear();
3594 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003595 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003596 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003597 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3598 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003599 if (!use_newobj_ex) {
3600 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003601 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003602 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003603 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003604 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003605 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003606
3607 if (use_newobj_ex) {
3608 PyObject *cls;
3609 PyObject *args;
3610 PyObject *kwargs;
3611
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003612 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003613 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003614 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003615 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003616 return -1;
3617 }
3618
3619 cls = PyTuple_GET_ITEM(argtup, 0);
3620 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003621 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003622 "first item from NEWOBJ_EX argument tuple must "
3623 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3624 return -1;
3625 }
3626 args = PyTuple_GET_ITEM(argtup, 1);
3627 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003628 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003629 "second item from NEWOBJ_EX argument tuple must "
3630 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3631 return -1;
3632 }
3633 kwargs = PyTuple_GET_ITEM(argtup, 2);
3634 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003635 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003636 "third item from NEWOBJ_EX argument tuple must "
3637 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3638 return -1;
3639 }
3640
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003641 if (self->proto >= 4) {
3642 if (save(self, cls, 0) < 0 ||
3643 save(self, args, 0) < 0 ||
3644 save(self, kwargs, 0) < 0 ||
3645 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3646 return -1;
3647 }
3648 }
3649 else {
3650 PyObject *newargs;
3651 PyObject *cls_new;
3652 Py_ssize_t i;
3653 _Py_IDENTIFIER(__new__);
3654
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003655 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003656 if (newargs == NULL)
3657 return -1;
3658
3659 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3660 if (cls_new == NULL) {
3661 Py_DECREF(newargs);
3662 return -1;
3663 }
3664 PyTuple_SET_ITEM(newargs, 0, cls_new);
3665 Py_INCREF(cls);
3666 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003667 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003668 PyObject *item = PyTuple_GET_ITEM(args, i);
3669 Py_INCREF(item);
3670 PyTuple_SET_ITEM(newargs, i + 2, item);
3671 }
3672
3673 callable = PyObject_Call(st->partial, newargs, kwargs);
3674 Py_DECREF(newargs);
3675 if (callable == NULL)
3676 return -1;
3677
3678 newargs = PyTuple_New(0);
3679 if (newargs == NULL) {
3680 Py_DECREF(callable);
3681 return -1;
3682 }
3683
3684 if (save(self, callable, 0) < 0 ||
3685 save(self, newargs, 0) < 0 ||
3686 _Pickler_Write(self, &reduce_op, 1) < 0) {
3687 Py_DECREF(newargs);
3688 Py_DECREF(callable);
3689 return -1;
3690 }
3691 Py_DECREF(newargs);
3692 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003693 }
3694 }
3695 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003696 PyObject *cls;
3697 PyObject *newargtup;
3698 PyObject *obj_class;
3699 int p;
3700
3701 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003702 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003703 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003704 return -1;
3705 }
3706
3707 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003708 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003709 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003710 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003711 return -1;
3712 }
3713
3714 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003715 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003716 p = obj_class != cls; /* true iff a problem */
3717 Py_DECREF(obj_class);
3718 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003719 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003720 "__newobj__ args has the wrong class");
3721 return -1;
3722 }
3723 }
3724 /* XXX: These calls save() are prone to infinite recursion. Imagine
3725 what happen if the value returned by the __reduce__() method of
3726 some extension type contains another object of the same type. Ouch!
3727
3728 Here is a quick example, that I ran into, to illustrate what I
3729 mean:
3730
3731 >>> import pickle, copyreg
3732 >>> copyreg.dispatch_table.pop(complex)
3733 >>> pickle.dumps(1+2j)
3734 Traceback (most recent call last):
3735 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003736 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003737
3738 Removing the complex class from copyreg.dispatch_table made the
3739 __reduce_ex__() method emit another complex object:
3740
3741 >>> (1+1j).__reduce_ex__(2)
3742 (<function __newobj__ at 0xb7b71c3c>,
3743 (<class 'complex'>, (1+1j)), None, None, None)
3744
3745 Thus when save() was called on newargstup (the 2nd item) recursion
3746 ensued. Of course, the bug was in the complex class which had a
3747 broken __getnewargs__() that emitted another complex object. But,
3748 the point, here, is it is quite easy to end up with a broken reduce
3749 function. */
3750
3751 /* Save the class and its __new__ arguments. */
3752 if (save(self, cls, 0) < 0)
3753 return -1;
3754
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003755 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003756 if (newargtup == NULL)
3757 return -1;
3758
3759 p = save(self, newargtup, 0);
3760 Py_DECREF(newargtup);
3761 if (p < 0)
3762 return -1;
3763
3764 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003765 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003766 return -1;
3767 }
3768 else { /* Not using NEWOBJ. */
3769 if (save(self, callable, 0) < 0 ||
3770 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003771 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003772 return -1;
3773 }
3774
3775 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3776 the caller do not want to memoize the object. Not particularly useful,
3777 but that is to mimic the behavior save_reduce() in pickle.py when
3778 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003779 if (obj != NULL) {
3780 /* If the object is already in the memo, this means it is
3781 recursive. In this case, throw away everything we put on the
3782 stack, and fetch the object back from the memo. */
3783 if (PyMemoTable_Get(self->memo, obj)) {
3784 const char pop_op = POP;
3785
3786 if (_Pickler_Write(self, &pop_op, 1) < 0)
3787 return -1;
3788 if (memo_get(self, obj) < 0)
3789 return -1;
3790
3791 return 0;
3792 }
3793 else if (memo_put(self, obj) < 0)
3794 return -1;
3795 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003796
3797 if (listitems && batch_list(self, listitems) < 0)
3798 return -1;
3799
3800 if (dictitems && batch_dict(self, dictitems) < 0)
3801 return -1;
3802
3803 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003804 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003805 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003806 return -1;
3807 }
3808
3809 return 0;
3810}
3811
3812static int
3813save(PicklerObject *self, PyObject *obj, int pers_save)
3814{
3815 PyTypeObject *type;
3816 PyObject *reduce_func = NULL;
3817 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003818 int status = 0;
3819
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003820 if (_Pickler_OpcodeBoundary(self) < 0)
3821 return -1;
3822
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003823 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003824 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003825
3826 /* The extra pers_save argument is necessary to avoid calling save_pers()
3827 on its returned object. */
3828 if (!pers_save && self->pers_func) {
3829 /* save_pers() returns:
3830 -1 to signal an error;
3831 0 if it did nothing successfully;
3832 1 if a persistent id was saved.
3833 */
3834 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3835 goto done;
3836 }
3837
3838 type = Py_TYPE(obj);
3839
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003840 /* The old cPickle had an optimization that used switch-case statement
3841 dispatching on the first letter of the type name. This has was removed
3842 since benchmarks shown that this optimization was actually slowing
3843 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003844
3845 /* Atom types; these aren't memoized, so don't check the memo. */
3846
3847 if (obj == Py_None) {
3848 status = save_none(self, obj);
3849 goto done;
3850 }
3851 else if (obj == Py_False || obj == Py_True) {
3852 status = save_bool(self, obj);
3853 goto done;
3854 }
3855 else if (type == &PyLong_Type) {
3856 status = save_long(self, obj);
3857 goto done;
3858 }
3859 else if (type == &PyFloat_Type) {
3860 status = save_float(self, obj);
3861 goto done;
3862 }
3863
3864 /* Check the memo to see if it has the object. If so, generate
3865 a GET (or BINGET) opcode, instead of pickling the object
3866 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003867 if (PyMemoTable_Get(self->memo, obj)) {
3868 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003869 goto error;
3870 goto done;
3871 }
3872
3873 if (type == &PyBytes_Type) {
3874 status = save_bytes(self, obj);
3875 goto done;
3876 }
3877 else if (type == &PyUnicode_Type) {
3878 status = save_unicode(self, obj);
3879 goto done;
3880 }
3881 else if (type == &PyDict_Type) {
3882 status = save_dict(self, obj);
3883 goto done;
3884 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003885 else if (type == &PySet_Type) {
3886 status = save_set(self, obj);
3887 goto done;
3888 }
3889 else if (type == &PyFrozenSet_Type) {
3890 status = save_frozenset(self, obj);
3891 goto done;
3892 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003893 else if (type == &PyList_Type) {
3894 status = save_list(self, obj);
3895 goto done;
3896 }
3897 else if (type == &PyTuple_Type) {
3898 status = save_tuple(self, obj);
3899 goto done;
3900 }
3901 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003902 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003903 goto done;
3904 }
3905 else if (type == &PyFunction_Type) {
3906 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003907 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003908 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003909
3910 /* XXX: This part needs some unit tests. */
3911
3912 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003913 * self.dispatch_table, copyreg.dispatch_table, the object's
3914 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003915 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003916 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003917 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003918 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3919 (PyObject *)type);
3920 if (reduce_func == NULL) {
3921 if (PyErr_Occurred()) {
3922 goto error;
3923 }
3924 } else {
3925 /* PyDict_GetItemWithError() returns a borrowed reference.
3926 Increase the reference count to be consistent with
3927 PyObject_GetItem and _PyObject_GetAttrId used below. */
3928 Py_INCREF(reduce_func);
3929 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003930 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003931 reduce_func = PyObject_GetItem(self->dispatch_table,
3932 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003933 if (reduce_func == NULL) {
3934 if (PyErr_ExceptionMatches(PyExc_KeyError))
3935 PyErr_Clear();
3936 else
3937 goto error;
3938 }
3939 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003940 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003941 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003942 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003944 else if (PyType_IsSubtype(type, &PyType_Type)) {
3945 status = save_global(self, obj, NULL);
3946 goto done;
3947 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003948 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003949 _Py_IDENTIFIER(__reduce__);
3950 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003951
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003952
3953 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3954 automatically defined as __reduce__. While this is convenient, this
3955 make it impossible to know which method was actually called. Of
3956 course, this is not a big deal. But still, it would be nice to let
3957 the user know which method was called when something go
3958 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3959 don't actually have to check for a __reduce__ method. */
3960
3961 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003962 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003963 if (reduce_func != NULL) {
3964 PyObject *proto;
3965 proto = PyLong_FromLong(self->proto);
3966 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003967 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003968 }
3969 }
3970 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003971 PickleState *st = _Pickle_GetGlobalState();
3972
3973 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003974 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003975 }
3976 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003977 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003978 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003979 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003980 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003981 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02003982 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003983 }
3984 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003985 PyErr_Format(st->PicklingError,
3986 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003987 type->tp_name, obj);
3988 goto error;
3989 }
3990 }
3991 }
3992
3993 if (reduce_value == NULL)
3994 goto error;
3995
3996 if (PyUnicode_Check(reduce_value)) {
3997 status = save_global(self, obj, reduce_value);
3998 goto done;
3999 }
4000
4001 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004002 PickleState *st = _Pickle_GetGlobalState();
4003 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004004 "__reduce__ must return a string or tuple");
4005 goto error;
4006 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004007
4008 status = save_reduce(self, reduce_value, obj);
4009
4010 if (0) {
4011 error:
4012 status = -1;
4013 }
4014 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004015
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004016 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004017 Py_XDECREF(reduce_func);
4018 Py_XDECREF(reduce_value);
4019
4020 return status;
4021}
4022
4023static int
4024dump(PicklerObject *self, PyObject *obj)
4025{
4026 const char stop_op = STOP;
4027
4028 if (self->proto >= 2) {
4029 char header[2];
4030
4031 header[0] = PROTO;
4032 assert(self->proto >= 0 && self->proto < 256);
4033 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004034 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004035 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004036 if (self->proto >= 4)
4037 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004038 }
4039
4040 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004041 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004042 return -1;
4043
4044 return 0;
4045}
4046
Larry Hastings61272b72014-01-07 12:41:53 -08004047/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004048
4049_pickle.Pickler.clear_memo
4050
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004051Clears the pickler's "memo".
4052
4053The memo is the data structure that remembers which objects the
4054pickler has already seen, so that shared or recursive objects are
4055pickled by reference and not by value. This method is useful when
4056re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004057[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004058
Larry Hastings3cceb382014-01-04 11:09:09 -08004059static PyObject *
4060_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004061/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004062{
4063 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004064 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004065
4066 Py_RETURN_NONE;
4067}
4068
Larry Hastings61272b72014-01-07 12:41:53 -08004069/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004070
4071_pickle.Pickler.dump
4072
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004073 obj: object
4074 /
4075
4076Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004077[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004078
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004079static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004080_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004081/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004082{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004083 /* Check whether the Pickler was initialized correctly (issue3664).
4084 Developers often forget to call __init__() in their subclasses, which
4085 would trigger a segfault without this check. */
4086 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004087 PickleState *st = _Pickle_GetGlobalState();
4088 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004089 "Pickler.__init__() was not called by %s.__init__()",
4090 Py_TYPE(self)->tp_name);
4091 return NULL;
4092 }
4093
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004094 if (_Pickler_ClearBuffer(self) < 0)
4095 return NULL;
4096
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004097 if (dump(self, obj) < 0)
4098 return NULL;
4099
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004100 if (_Pickler_FlushToFile(self) < 0)
4101 return NULL;
4102
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004103 Py_RETURN_NONE;
4104}
4105
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004106/*[clinic input]
4107
4108_pickle.Pickler.__sizeof__ -> Py_ssize_t
4109
4110Returns size in memory, in bytes.
4111[clinic start generated code]*/
4112
4113static Py_ssize_t
4114_pickle_Pickler___sizeof___impl(PicklerObject *self)
4115/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4116{
4117 Py_ssize_t res, s;
4118
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004119 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004120 if (self->memo != NULL) {
4121 res += sizeof(PyMemoTable);
4122 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4123 }
4124 if (self->output_buffer != NULL) {
4125 s = _PySys_GetSizeOf(self->output_buffer);
4126 if (s == -1)
4127 return -1;
4128 res += s;
4129 }
4130 return res;
4131}
4132
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004133static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004134 _PICKLE_PICKLER_DUMP_METHODDEF
4135 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004136 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004137 {NULL, NULL} /* sentinel */
4138};
4139
4140static void
4141Pickler_dealloc(PicklerObject *self)
4142{
4143 PyObject_GC_UnTrack(self);
4144
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004145 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004146 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004147 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004148 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004149 Py_XDECREF(self->fast_memo);
4150
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004151 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004152
4153 Py_TYPE(self)->tp_free((PyObject *)self);
4154}
4155
4156static int
4157Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4158{
4159 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004160 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004161 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004162 Py_VISIT(self->fast_memo);
4163 return 0;
4164}
4165
4166static int
4167Pickler_clear(PicklerObject *self)
4168{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004169 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004170 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004171 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004172 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004173 Py_CLEAR(self->fast_memo);
4174
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004175 if (self->memo != NULL) {
4176 PyMemoTable *memo = self->memo;
4177 self->memo = NULL;
4178 PyMemoTable_Del(memo);
4179 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004180 return 0;
4181}
4182
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004183
Larry Hastings61272b72014-01-07 12:41:53 -08004184/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004185
4186_pickle.Pickler.__init__
4187
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004188 file: object
4189 protocol: object = NULL
4190 fix_imports: bool = True
4191
4192This takes a binary file for writing a pickle data stream.
4193
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004194The optional *protocol* argument tells the pickler to use the given
4195protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4196protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004197
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004198Specifying a negative protocol version selects the highest protocol
4199version supported. The higher the protocol used, the more recent the
4200version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004201
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004202The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004203bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004204writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004205this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004206
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004207If *fix_imports* is True and protocol is less than 3, pickle will try
4208to map the new Python 3 names to the old module names used in Python
42092, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004210[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004211
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004212static int
Larry Hastings89964c42015-04-14 18:07:59 -04004213_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4214 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004215/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004216{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004217 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004218 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004219
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004220 /* In case of multiple __init__() calls, clear previous content. */
4221 if (self->write != NULL)
4222 (void)Pickler_clear(self);
4223
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004224 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004225 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004226
4227 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004228 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004229
4230 /* memo and output_buffer may have already been created in _Pickler_New */
4231 if (self->memo == NULL) {
4232 self->memo = PyMemoTable_New();
4233 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004234 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004235 }
4236 self->output_len = 0;
4237 if (self->output_buffer == NULL) {
4238 self->max_output_len = WRITE_BUF_SIZE;
4239 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4240 self->max_output_len);
4241 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004242 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004243 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004244
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004245 self->fast = 0;
4246 self->fast_nesting = 0;
4247 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004248
4249 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4250 &PyId_persistent_id);
4251 if (self->pers_func == NULL) {
4252 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004253 return -1;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004254 }
4255 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004256 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004257
4258 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4259 &PyId_dispatch_table);
4260 if (self->dispatch_table == NULL) {
4261 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004262 return -1;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004263 }
4264 PyErr_Clear();
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004265 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004266
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004267 return 0;
4268}
4269
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004270
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004271/* Define a proxy object for the Pickler's internal memo object. This is to
4272 * avoid breaking code like:
4273 * pickler.memo.clear()
4274 * and
4275 * pickler.memo = saved_memo
4276 * Is this a good idea? Not really, but we don't want to break code that uses
4277 * it. Note that we don't implement the entire mapping API here. This is
4278 * intentional, as these should be treated as black-box implementation details.
4279 */
4280
Larry Hastings61272b72014-01-07 12:41:53 -08004281/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004282_pickle.PicklerMemoProxy.clear
4283
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004284Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004285[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004286
Larry Hastings3cceb382014-01-04 11:09:09 -08004287static PyObject *
4288_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004289/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004290{
4291 if (self->pickler->memo)
4292 PyMemoTable_Clear(self->pickler->memo);
4293 Py_RETURN_NONE;
4294}
4295
Larry Hastings61272b72014-01-07 12:41:53 -08004296/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004297_pickle.PicklerMemoProxy.copy
4298
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004299Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004300[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004301
Larry Hastings3cceb382014-01-04 11:09:09 -08004302static PyObject *
4303_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004304/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004305{
4306 Py_ssize_t i;
4307 PyMemoTable *memo;
4308 PyObject *new_memo = PyDict_New();
4309 if (new_memo == NULL)
4310 return NULL;
4311
4312 memo = self->pickler->memo;
4313 for (i = 0; i < memo->mt_allocated; ++i) {
4314 PyMemoEntry entry = memo->mt_table[i];
4315 if (entry.me_key != NULL) {
4316 int status;
4317 PyObject *key, *value;
4318
4319 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004320 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004321
4322 if (key == NULL || value == NULL) {
4323 Py_XDECREF(key);
4324 Py_XDECREF(value);
4325 goto error;
4326 }
4327 status = PyDict_SetItem(new_memo, key, value);
4328 Py_DECREF(key);
4329 Py_DECREF(value);
4330 if (status < 0)
4331 goto error;
4332 }
4333 }
4334 return new_memo;
4335
4336 error:
4337 Py_XDECREF(new_memo);
4338 return NULL;
4339}
4340
Larry Hastings61272b72014-01-07 12:41:53 -08004341/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004342_pickle.PicklerMemoProxy.__reduce__
4343
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004344Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004345[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004346
Larry Hastings3cceb382014-01-04 11:09:09 -08004347static PyObject *
4348_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004349/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004350{
4351 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004352 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004353 if (contents == NULL)
4354 return NULL;
4355
4356 reduce_value = PyTuple_New(2);
4357 if (reduce_value == NULL) {
4358 Py_DECREF(contents);
4359 return NULL;
4360 }
4361 dict_args = PyTuple_New(1);
4362 if (dict_args == NULL) {
4363 Py_DECREF(contents);
4364 Py_DECREF(reduce_value);
4365 return NULL;
4366 }
4367 PyTuple_SET_ITEM(dict_args, 0, contents);
4368 Py_INCREF((PyObject *)&PyDict_Type);
4369 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4370 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4371 return reduce_value;
4372}
4373
4374static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004375 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4376 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4377 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004378 {NULL, NULL} /* sentinel */
4379};
4380
4381static void
4382PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4383{
4384 PyObject_GC_UnTrack(self);
4385 Py_XDECREF(self->pickler);
4386 PyObject_GC_Del((PyObject *)self);
4387}
4388
4389static int
4390PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4391 visitproc visit, void *arg)
4392{
4393 Py_VISIT(self->pickler);
4394 return 0;
4395}
4396
4397static int
4398PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4399{
4400 Py_CLEAR(self->pickler);
4401 return 0;
4402}
4403
4404static PyTypeObject PicklerMemoProxyType = {
4405 PyVarObject_HEAD_INIT(NULL, 0)
4406 "_pickle.PicklerMemoProxy", /*tp_name*/
4407 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4408 0,
4409 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4410 0, /* tp_print */
4411 0, /* tp_getattr */
4412 0, /* tp_setattr */
4413 0, /* tp_compare */
4414 0, /* tp_repr */
4415 0, /* tp_as_number */
4416 0, /* tp_as_sequence */
4417 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004418 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004419 0, /* tp_call */
4420 0, /* tp_str */
4421 PyObject_GenericGetAttr, /* tp_getattro */
4422 PyObject_GenericSetAttr, /* tp_setattro */
4423 0, /* tp_as_buffer */
4424 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4425 0, /* tp_doc */
4426 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4427 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4428 0, /* tp_richcompare */
4429 0, /* tp_weaklistoffset */
4430 0, /* tp_iter */
4431 0, /* tp_iternext */
4432 picklerproxy_methods, /* tp_methods */
4433};
4434
4435static PyObject *
4436PicklerMemoProxy_New(PicklerObject *pickler)
4437{
4438 PicklerMemoProxyObject *self;
4439
4440 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4441 if (self == NULL)
4442 return NULL;
4443 Py_INCREF(pickler);
4444 self->pickler = pickler;
4445 PyObject_GC_Track(self);
4446 return (PyObject *)self;
4447}
4448
4449/*****************************************************************************/
4450
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004451static PyObject *
4452Pickler_get_memo(PicklerObject *self)
4453{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004454 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004455}
4456
4457static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004458Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004459{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004460 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004461
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004462 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004463 PyErr_SetString(PyExc_TypeError,
4464 "attribute deletion is not supported");
4465 return -1;
4466 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004467
4468 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4469 PicklerObject *pickler =
4470 ((PicklerMemoProxyObject *)obj)->pickler;
4471
4472 new_memo = PyMemoTable_Copy(pickler->memo);
4473 if (new_memo == NULL)
4474 return -1;
4475 }
4476 else if (PyDict_Check(obj)) {
4477 Py_ssize_t i = 0;
4478 PyObject *key, *value;
4479
4480 new_memo = PyMemoTable_New();
4481 if (new_memo == NULL)
4482 return -1;
4483
4484 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004485 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004486 PyObject *memo_obj;
4487
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004488 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004489 PyErr_SetString(PyExc_TypeError,
4490 "'memo' values must be 2-item tuples");
4491 goto error;
4492 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004493 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004494 if (memo_id == -1 && PyErr_Occurred())
4495 goto error;
4496 memo_obj = PyTuple_GET_ITEM(value, 1);
4497 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4498 goto error;
4499 }
4500 }
4501 else {
4502 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004503 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004504 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004505 return -1;
4506 }
4507
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004508 PyMemoTable_Del(self->memo);
4509 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004510
4511 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004512
4513 error:
4514 if (new_memo)
4515 PyMemoTable_Del(new_memo);
4516 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004517}
4518
4519static PyObject *
4520Pickler_get_persid(PicklerObject *self)
4521{
4522 if (self->pers_func == NULL)
4523 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4524 else
4525 Py_INCREF(self->pers_func);
4526 return self->pers_func;
4527}
4528
4529static int
4530Pickler_set_persid(PicklerObject *self, PyObject *value)
4531{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004532 if (value == NULL) {
4533 PyErr_SetString(PyExc_TypeError,
4534 "attribute deletion is not supported");
4535 return -1;
4536 }
4537 if (!PyCallable_Check(value)) {
4538 PyErr_SetString(PyExc_TypeError,
4539 "persistent_id must be a callable taking one argument");
4540 return -1;
4541 }
4542
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004543 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004544 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004545
4546 return 0;
4547}
4548
4549static PyMemberDef Pickler_members[] = {
4550 {"bin", T_INT, offsetof(PicklerObject, bin)},
4551 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004552 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004553 {NULL}
4554};
4555
4556static PyGetSetDef Pickler_getsets[] = {
4557 {"memo", (getter)Pickler_get_memo,
4558 (setter)Pickler_set_memo},
4559 {"persistent_id", (getter)Pickler_get_persid,
4560 (setter)Pickler_set_persid},
4561 {NULL}
4562};
4563
4564static PyTypeObject Pickler_Type = {
4565 PyVarObject_HEAD_INIT(NULL, 0)
4566 "_pickle.Pickler" , /*tp_name*/
4567 sizeof(PicklerObject), /*tp_basicsize*/
4568 0, /*tp_itemsize*/
4569 (destructor)Pickler_dealloc, /*tp_dealloc*/
4570 0, /*tp_print*/
4571 0, /*tp_getattr*/
4572 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004573 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004574 0, /*tp_repr*/
4575 0, /*tp_as_number*/
4576 0, /*tp_as_sequence*/
4577 0, /*tp_as_mapping*/
4578 0, /*tp_hash*/
4579 0, /*tp_call*/
4580 0, /*tp_str*/
4581 0, /*tp_getattro*/
4582 0, /*tp_setattro*/
4583 0, /*tp_as_buffer*/
4584 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004585 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586 (traverseproc)Pickler_traverse, /*tp_traverse*/
4587 (inquiry)Pickler_clear, /*tp_clear*/
4588 0, /*tp_richcompare*/
4589 0, /*tp_weaklistoffset*/
4590 0, /*tp_iter*/
4591 0, /*tp_iternext*/
4592 Pickler_methods, /*tp_methods*/
4593 Pickler_members, /*tp_members*/
4594 Pickler_getsets, /*tp_getset*/
4595 0, /*tp_base*/
4596 0, /*tp_dict*/
4597 0, /*tp_descr_get*/
4598 0, /*tp_descr_set*/
4599 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004600 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004601 PyType_GenericAlloc, /*tp_alloc*/
4602 PyType_GenericNew, /*tp_new*/
4603 PyObject_GC_Del, /*tp_free*/
4604 0, /*tp_is_gc*/
4605};
4606
Victor Stinner121aab42011-09-29 23:40:53 +02004607/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004608
4609 XXX: It would be nice to able to avoid Python function call overhead, by
4610 using directly the C version of find_class(), when find_class() is not
4611 overridden by a subclass. Although, this could become rather hackish. A
4612 simpler optimization would be to call the C function when self is not a
4613 subclass instance. */
4614static PyObject *
4615find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4616{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004617 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004618
Victor Stinner55ba38a2016-12-09 16:09:30 +01004619 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4620 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004621}
4622
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004623static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004624marker(UnpicklerObject *self)
4625{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004626 Py_ssize_t mark;
4627
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004628 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004629 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004630 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004631 return -1;
4632 }
4633
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004634 mark = self->marks[--self->num_marks];
4635 self->stack->mark_set = self->num_marks != 0;
4636 self->stack->fence = self->num_marks ?
4637 self->marks[self->num_marks - 1] : 0;
4638 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004639}
4640
4641static int
4642load_none(UnpicklerObject *self)
4643{
4644 PDATA_APPEND(self->stack, Py_None, -1);
4645 return 0;
4646}
4647
4648static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004649load_int(UnpicklerObject *self)
4650{
4651 PyObject *value;
4652 char *endptr, *s;
4653 Py_ssize_t len;
4654 long x;
4655
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004656 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004657 return -1;
4658 if (len < 2)
4659 return bad_readline();
4660
4661 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004662 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004663 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004664 x = strtol(s, &endptr, 0);
4665
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004666 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004667 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004668 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004669 errno = 0;
4670 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004671 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004672 if (value == NULL) {
4673 PyErr_SetString(PyExc_ValueError,
4674 "could not convert string to int");
4675 return -1;
4676 }
4677 }
4678 else {
4679 if (len == 3 && (x == 0 || x == 1)) {
4680 if ((value = PyBool_FromLong(x)) == NULL)
4681 return -1;
4682 }
4683 else {
4684 if ((value = PyLong_FromLong(x)) == NULL)
4685 return -1;
4686 }
4687 }
4688
4689 PDATA_PUSH(self->stack, value, -1);
4690 return 0;
4691}
4692
4693static int
4694load_bool(UnpicklerObject *self, PyObject *boolean)
4695{
4696 assert(boolean == Py_True || boolean == Py_False);
4697 PDATA_APPEND(self->stack, boolean, -1);
4698 return 0;
4699}
4700
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004701/* s contains x bytes of an unsigned little-endian integer. Return its value
4702 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4703 */
4704static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004705calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004706{
4707 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004708 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004709 size_t x = 0;
4710
Serhiy Storchakae0606192015-09-29 22:10:07 +03004711 if (nbytes > (int)sizeof(size_t)) {
4712 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4713 * have 64-bit size that can't be represented on 32-bit platform.
4714 */
4715 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4716 if (s[i])
4717 return -1;
4718 }
4719 nbytes = (int)sizeof(size_t);
4720 }
4721 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004722 x |= (size_t) s[i] << (8 * i);
4723 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004724
4725 if (x > PY_SSIZE_T_MAX)
4726 return -1;
4727 else
4728 return (Py_ssize_t) x;
4729}
4730
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004731/* s contains x bytes of a little-endian integer. Return its value as a
4732 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004733 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004734 * of x-platform bugs.
4735 */
4736static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004737calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004738{
4739 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004740 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004741 long x = 0;
4742
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004743 for (i = 0; i < nbytes; i++) {
4744 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004745 }
4746
4747 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4748 * is signed, so on a box with longs bigger than 4 bytes we need
4749 * to extend a BININT's sign bit to the full width.
4750 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004751 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004752 x |= -(x & (1L << 31));
4753 }
4754
4755 return x;
4756}
4757
4758static int
4759load_binintx(UnpicklerObject *self, char *s, int size)
4760{
4761 PyObject *value;
4762 long x;
4763
4764 x = calc_binint(s, size);
4765
4766 if ((value = PyLong_FromLong(x)) == NULL)
4767 return -1;
4768
4769 PDATA_PUSH(self->stack, value, -1);
4770 return 0;
4771}
4772
4773static int
4774load_binint(UnpicklerObject *self)
4775{
4776 char *s;
4777
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004778 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004779 return -1;
4780
4781 return load_binintx(self, s, 4);
4782}
4783
4784static int
4785load_binint1(UnpicklerObject *self)
4786{
4787 char *s;
4788
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004789 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004790 return -1;
4791
4792 return load_binintx(self, s, 1);
4793}
4794
4795static int
4796load_binint2(UnpicklerObject *self)
4797{
4798 char *s;
4799
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004800 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004801 return -1;
4802
4803 return load_binintx(self, s, 2);
4804}
4805
4806static int
4807load_long(UnpicklerObject *self)
4808{
4809 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004810 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004811 Py_ssize_t len;
4812
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004813 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004814 return -1;
4815 if (len < 2)
4816 return bad_readline();
4817
Mark Dickinson8dd05142009-01-20 20:43:58 +00004818 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4819 the 'L' before calling PyLong_FromString. In order to maintain
4820 compatibility with Python 3.0.0, we don't actually *require*
4821 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004822 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004823 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004824 /* XXX: Should the base argument explicitly set to 10? */
4825 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004826 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004827 return -1;
4828
4829 PDATA_PUSH(self->stack, value, -1);
4830 return 0;
4831}
4832
4833/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4834 * data following.
4835 */
4836static int
4837load_counted_long(UnpicklerObject *self, int size)
4838{
4839 PyObject *value;
4840 char *nbytes;
4841 char *pdata;
4842
4843 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004844 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004845 return -1;
4846
4847 size = calc_binint(nbytes, size);
4848 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004849 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004850 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004851 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004852 "LONG pickle has negative byte count");
4853 return -1;
4854 }
4855
4856 if (size == 0)
4857 value = PyLong_FromLong(0L);
4858 else {
4859 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004860 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004861 return -1;
4862 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4863 1 /* little endian */ , 1 /* signed */ );
4864 }
4865 if (value == NULL)
4866 return -1;
4867 PDATA_PUSH(self->stack, value, -1);
4868 return 0;
4869}
4870
4871static int
4872load_float(UnpicklerObject *self)
4873{
4874 PyObject *value;
4875 char *endptr, *s;
4876 Py_ssize_t len;
4877 double d;
4878
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004879 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004880 return -1;
4881 if (len < 2)
4882 return bad_readline();
4883
4884 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004885 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4886 if (d == -1.0 && PyErr_Occurred())
4887 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004888 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004889 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4890 return -1;
4891 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004892 value = PyFloat_FromDouble(d);
4893 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004894 return -1;
4895
4896 PDATA_PUSH(self->stack, value, -1);
4897 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004898}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899
4900static int
4901load_binfloat(UnpicklerObject *self)
4902{
4903 PyObject *value;
4904 double x;
4905 char *s;
4906
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004907 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004908 return -1;
4909
4910 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4911 if (x == -1.0 && PyErr_Occurred())
4912 return -1;
4913
4914 if ((value = PyFloat_FromDouble(x)) == NULL)
4915 return -1;
4916
4917 PDATA_PUSH(self->stack, value, -1);
4918 return 0;
4919}
4920
4921static int
4922load_string(UnpicklerObject *self)
4923{
4924 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004925 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004926 Py_ssize_t len;
4927 char *s, *p;
4928
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004929 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004930 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004931 /* Strip the newline */
4932 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004933 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004934 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004935 p = s + 1;
4936 len -= 2;
4937 }
4938 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004939 PickleState *st = _Pickle_GetGlobalState();
4940 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004941 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004942 return -1;
4943 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004944 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004945
4946 /* Use the PyBytes API to decode the string, since that is what is used
4947 to encode, and then coerce the result to Unicode. */
4948 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004949 if (bytes == NULL)
4950 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004951
4952 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4953 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4954 if (strcmp(self->encoding, "bytes") == 0) {
4955 obj = bytes;
4956 }
4957 else {
4958 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4959 Py_DECREF(bytes);
4960 if (obj == NULL) {
4961 return -1;
4962 }
4963 }
4964
4965 PDATA_PUSH(self->stack, obj, -1);
4966 return 0;
4967}
4968
4969static int
4970load_counted_binstring(UnpicklerObject *self, int nbytes)
4971{
4972 PyObject *obj;
4973 Py_ssize_t size;
4974 char *s;
4975
4976 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004977 return -1;
4978
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004979 size = calc_binsize(s, nbytes);
4980 if (size < 0) {
4981 PickleState *st = _Pickle_GetGlobalState();
4982 PyErr_Format(st->UnpicklingError,
4983 "BINSTRING exceeds system's maximum size of %zd bytes",
4984 PY_SSIZE_T_MAX);
4985 return -1;
4986 }
4987
4988 if (_Unpickler_Read(self, &s, size) < 0)
4989 return -1;
4990
4991 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4992 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4993 if (strcmp(self->encoding, "bytes") == 0) {
4994 obj = PyBytes_FromStringAndSize(s, size);
4995 }
4996 else {
4997 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4998 }
4999 if (obj == NULL) {
5000 return -1;
5001 }
5002
5003 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005004 return 0;
5005}
5006
5007static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005008load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005009{
5010 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005011 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005012 char *s;
5013
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005014 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005015 return -1;
5016
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005017 size = calc_binsize(s, nbytes);
5018 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005019 PyErr_Format(PyExc_OverflowError,
5020 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005021 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005022 return -1;
5023 }
5024
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005025 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005026 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005027
5028 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005029 if (bytes == NULL)
5030 return -1;
5031
5032 PDATA_PUSH(self->stack, bytes, -1);
5033 return 0;
5034}
5035
5036static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005037load_unicode(UnpicklerObject *self)
5038{
5039 PyObject *str;
5040 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005041 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005043 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044 return -1;
5045 if (len < 1)
5046 return bad_readline();
5047
5048 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5049 if (str == NULL)
5050 return -1;
5051
5052 PDATA_PUSH(self->stack, str, -1);
5053 return 0;
5054}
5055
5056static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005057load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005058{
5059 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005060 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005061 char *s;
5062
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005063 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005064 return -1;
5065
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005066 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005067 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005068 PyErr_Format(PyExc_OverflowError,
5069 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005070 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005071 return -1;
5072 }
5073
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005074 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005075 return -1;
5076
Victor Stinner485fb562010-04-13 11:07:24 +00005077 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005078 if (str == NULL)
5079 return -1;
5080
5081 PDATA_PUSH(self->stack, str, -1);
5082 return 0;
5083}
5084
5085static int
Victor Stinner21b47112016-03-14 18:09:39 +01005086load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005087{
5088 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005089
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005090 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005091 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005092
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005093 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005094 if (tuple == NULL)
5095 return -1;
5096 PDATA_PUSH(self->stack, tuple, -1);
5097 return 0;
5098}
5099
5100static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005101load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005102{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005103 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005104
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005105 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005106 return -1;
5107
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005108 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005109}
5110
5111static int
5112load_empty_list(UnpicklerObject *self)
5113{
5114 PyObject *list;
5115
5116 if ((list = PyList_New(0)) == NULL)
5117 return -1;
5118 PDATA_PUSH(self->stack, list, -1);
5119 return 0;
5120}
5121
5122static int
5123load_empty_dict(UnpicklerObject *self)
5124{
5125 PyObject *dict;
5126
5127 if ((dict = PyDict_New()) == NULL)
5128 return -1;
5129 PDATA_PUSH(self->stack, dict, -1);
5130 return 0;
5131}
5132
5133static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005134load_empty_set(UnpicklerObject *self)
5135{
5136 PyObject *set;
5137
5138 if ((set = PySet_New(NULL)) == NULL)
5139 return -1;
5140 PDATA_PUSH(self->stack, set, -1);
5141 return 0;
5142}
5143
5144static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005145load_list(UnpicklerObject *self)
5146{
5147 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005148 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005149
5150 if ((i = marker(self)) < 0)
5151 return -1;
5152
5153 list = Pdata_poplist(self->stack, i);
5154 if (list == NULL)
5155 return -1;
5156 PDATA_PUSH(self->stack, list, -1);
5157 return 0;
5158}
5159
5160static int
5161load_dict(UnpicklerObject *self)
5162{
5163 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005164 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005165
5166 if ((i = marker(self)) < 0)
5167 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005168 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005169
5170 if ((dict = PyDict_New()) == NULL)
5171 return -1;
5172
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005173 if ((j - i) % 2 != 0) {
5174 PickleState *st = _Pickle_GetGlobalState();
5175 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005176 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005177 return -1;
5178 }
5179
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005180 for (k = i + 1; k < j; k += 2) {
5181 key = self->stack->data[k - 1];
5182 value = self->stack->data[k];
5183 if (PyDict_SetItem(dict, key, value) < 0) {
5184 Py_DECREF(dict);
5185 return -1;
5186 }
5187 }
5188 Pdata_clear(self->stack, i);
5189 PDATA_PUSH(self->stack, dict, -1);
5190 return 0;
5191}
5192
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005193static int
5194load_frozenset(UnpicklerObject *self)
5195{
5196 PyObject *items;
5197 PyObject *frozenset;
5198 Py_ssize_t i;
5199
5200 if ((i = marker(self)) < 0)
5201 return -1;
5202
5203 items = Pdata_poptuple(self->stack, i);
5204 if (items == NULL)
5205 return -1;
5206
5207 frozenset = PyFrozenSet_New(items);
5208 Py_DECREF(items);
5209 if (frozenset == NULL)
5210 return -1;
5211
5212 PDATA_PUSH(self->stack, frozenset, -1);
5213 return 0;
5214}
5215
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005216static PyObject *
5217instantiate(PyObject *cls, PyObject *args)
5218{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005219 /* Caller must assure args are a tuple. Normally, args come from
5220 Pdata_poptuple which packs objects from the top of the stack
5221 into a newly created tuple. */
5222 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005223 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5224 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005225 _Py_IDENTIFIER(__new__);
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005226 PyObject *func = _PyObject_GetAttrId(cls, &PyId___getinitargs__);
5227 if (func == NULL) {
5228 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
5229 return NULL;
5230 }
5231 PyErr_Clear();
5232 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5233 }
5234 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005235 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005236 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005237}
5238
5239static int
5240load_obj(UnpicklerObject *self)
5241{
5242 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005243 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005244
5245 if ((i = marker(self)) < 0)
5246 return -1;
5247
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005248 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005249 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005250
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005251 args = Pdata_poptuple(self->stack, i + 1);
5252 if (args == NULL)
5253 return -1;
5254
5255 PDATA_POP(self->stack, cls);
5256 if (cls) {
5257 obj = instantiate(cls, args);
5258 Py_DECREF(cls);
5259 }
5260 Py_DECREF(args);
5261 if (obj == NULL)
5262 return -1;
5263
5264 PDATA_PUSH(self->stack, obj, -1);
5265 return 0;
5266}
5267
5268static int
5269load_inst(UnpicklerObject *self)
5270{
5271 PyObject *cls = NULL;
5272 PyObject *args = NULL;
5273 PyObject *obj = NULL;
5274 PyObject *module_name;
5275 PyObject *class_name;
5276 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005277 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005278 char *s;
5279
5280 if ((i = marker(self)) < 0)
5281 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005282 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005283 return -1;
5284 if (len < 2)
5285 return bad_readline();
5286
5287 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5288 identifiers are permitted in Python 3.0, since the INST opcode is only
5289 supported by older protocols on Python 2.x. */
5290 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5291 if (module_name == NULL)
5292 return -1;
5293
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005294 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005295 if (len < 2) {
5296 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005297 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005298 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005299 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005300 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005301 cls = find_class(self, module_name, class_name);
5302 Py_DECREF(class_name);
5303 }
5304 }
5305 Py_DECREF(module_name);
5306
5307 if (cls == NULL)
5308 return -1;
5309
5310 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5311 obj = instantiate(cls, args);
5312 Py_DECREF(args);
5313 }
5314 Py_DECREF(cls);
5315
5316 if (obj == NULL)
5317 return -1;
5318
5319 PDATA_PUSH(self->stack, obj, -1);
5320 return 0;
5321}
5322
5323static int
5324load_newobj(UnpicklerObject *self)
5325{
5326 PyObject *args = NULL;
5327 PyObject *clsraw = NULL;
5328 PyTypeObject *cls; /* clsraw cast to its true type */
5329 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005330 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005331
5332 /* Stack is ... cls argtuple, and we want to call
5333 * cls.__new__(cls, *argtuple).
5334 */
5335 PDATA_POP(self->stack, args);
5336 if (args == NULL)
5337 goto error;
5338 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005339 PyErr_SetString(st->UnpicklingError,
5340 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005341 goto error;
5342 }
5343
5344 PDATA_POP(self->stack, clsraw);
5345 cls = (PyTypeObject *)clsraw;
5346 if (cls == NULL)
5347 goto error;
5348 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005349 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005350 "isn't a type object");
5351 goto error;
5352 }
5353 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005354 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005355 "has NULL tp_new");
5356 goto error;
5357 }
5358
5359 /* Call __new__. */
5360 obj = cls->tp_new(cls, args, NULL);
5361 if (obj == NULL)
5362 goto error;
5363
5364 Py_DECREF(args);
5365 Py_DECREF(clsraw);
5366 PDATA_PUSH(self->stack, obj, -1);
5367 return 0;
5368
5369 error:
5370 Py_XDECREF(args);
5371 Py_XDECREF(clsraw);
5372 return -1;
5373}
5374
5375static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005376load_newobj_ex(UnpicklerObject *self)
5377{
5378 PyObject *cls, *args, *kwargs;
5379 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005380 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005381
5382 PDATA_POP(self->stack, kwargs);
5383 if (kwargs == NULL) {
5384 return -1;
5385 }
5386 PDATA_POP(self->stack, args);
5387 if (args == NULL) {
5388 Py_DECREF(kwargs);
5389 return -1;
5390 }
5391 PDATA_POP(self->stack, cls);
5392 if (cls == NULL) {
5393 Py_DECREF(kwargs);
5394 Py_DECREF(args);
5395 return -1;
5396 }
Larry Hastings61272b72014-01-07 12:41:53 -08005397
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005398 if (!PyType_Check(cls)) {
5399 Py_DECREF(kwargs);
5400 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005401 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005402 "NEWOBJ_EX class argument must be a type, not %.200s",
5403 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005404 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005405 return -1;
5406 }
5407
5408 if (((PyTypeObject *)cls)->tp_new == NULL) {
5409 Py_DECREF(kwargs);
5410 Py_DECREF(args);
5411 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005412 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005413 "NEWOBJ_EX class argument doesn't have __new__");
5414 return -1;
5415 }
5416 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5417 Py_DECREF(kwargs);
5418 Py_DECREF(args);
5419 Py_DECREF(cls);
5420 if (obj == NULL) {
5421 return -1;
5422 }
5423 PDATA_PUSH(self->stack, obj, -1);
5424 return 0;
5425}
5426
5427static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005428load_global(UnpicklerObject *self)
5429{
5430 PyObject *global = NULL;
5431 PyObject *module_name;
5432 PyObject *global_name;
5433 Py_ssize_t len;
5434 char *s;
5435
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005436 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005437 return -1;
5438 if (len < 2)
5439 return bad_readline();
5440 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5441 if (!module_name)
5442 return -1;
5443
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005444 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005445 if (len < 2) {
5446 Py_DECREF(module_name);
5447 return bad_readline();
5448 }
5449 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5450 if (global_name) {
5451 global = find_class(self, module_name, global_name);
5452 Py_DECREF(global_name);
5453 }
5454 }
5455 Py_DECREF(module_name);
5456
5457 if (global == NULL)
5458 return -1;
5459 PDATA_PUSH(self->stack, global, -1);
5460 return 0;
5461}
5462
5463static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005464load_stack_global(UnpicklerObject *self)
5465{
5466 PyObject *global;
5467 PyObject *module_name;
5468 PyObject *global_name;
5469
5470 PDATA_POP(self->stack, global_name);
5471 PDATA_POP(self->stack, module_name);
5472 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5473 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005474 PickleState *st = _Pickle_GetGlobalState();
5475 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005476 Py_XDECREF(global_name);
5477 Py_XDECREF(module_name);
5478 return -1;
5479 }
5480 global = find_class(self, module_name, global_name);
5481 Py_DECREF(global_name);
5482 Py_DECREF(module_name);
5483 if (global == NULL)
5484 return -1;
5485 PDATA_PUSH(self->stack, global, -1);
5486 return 0;
5487}
5488
5489static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005490load_persid(UnpicklerObject *self)
5491{
5492 PyObject *pid;
5493 Py_ssize_t len;
5494 char *s;
5495
5496 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005497 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005498 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005499 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005500 return bad_readline();
5501
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005502 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5503 if (pid == NULL) {
5504 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5505 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5506 "persistent IDs in protocol 0 must be "
5507 "ASCII strings");
5508 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005509 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005510 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005511
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005512 /* This does not leak since _Pickle_FastCall() steals the reference
5513 to pid first. */
5514 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005515 if (pid == NULL)
5516 return -1;
5517
5518 PDATA_PUSH(self->stack, pid, -1);
5519 return 0;
5520 }
5521 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005522 PickleState *st = _Pickle_GetGlobalState();
5523 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005524 "A load persistent id instruction was encountered,\n"
5525 "but no persistent_load function was specified.");
5526 return -1;
5527 }
5528}
5529
5530static int
5531load_binpersid(UnpicklerObject *self)
5532{
5533 PyObject *pid;
5534
5535 if (self->pers_func) {
5536 PDATA_POP(self->stack, pid);
5537 if (pid == NULL)
5538 return -1;
5539
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005540 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005541 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005542 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005543 if (pid == NULL)
5544 return -1;
5545
5546 PDATA_PUSH(self->stack, pid, -1);
5547 return 0;
5548 }
5549 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005550 PickleState *st = _Pickle_GetGlobalState();
5551 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005552 "A load persistent id instruction was encountered,\n"
5553 "but no persistent_load function was specified.");
5554 return -1;
5555 }
5556}
5557
5558static int
5559load_pop(UnpicklerObject *self)
5560{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005561 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005562
5563 /* Note that we split the (pickle.py) stack into two stacks,
5564 * an object stack and a mark stack. We have to be clever and
5565 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005566 * mark stack first, and only signalling a stack underflow if
5567 * the object stack is empty and the mark stack doesn't match
5568 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005569 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005570 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005571 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005572 self->stack->mark_set = self->num_marks != 0;
5573 self->stack->fence = self->num_marks ?
5574 self->marks[self->num_marks - 1] : 0;
5575 } else if (len <= self->stack->fence)
5576 return Pdata_stack_underflow(self->stack);
5577 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005578 len--;
5579 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005580 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005581 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005582 return 0;
5583}
5584
5585static int
5586load_pop_mark(UnpicklerObject *self)
5587{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005588 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005589
5590 if ((i = marker(self)) < 0)
5591 return -1;
5592
5593 Pdata_clear(self->stack, i);
5594
5595 return 0;
5596}
5597
5598static int
5599load_dup(UnpicklerObject *self)
5600{
5601 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005602 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005604 if (len <= self->stack->fence)
5605 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 last = self->stack->data[len - 1];
5607 PDATA_APPEND(self->stack, last, -1);
5608 return 0;
5609}
5610
5611static int
5612load_get(UnpicklerObject *self)
5613{
5614 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005615 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 Py_ssize_t len;
5617 char *s;
5618
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005619 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005620 return -1;
5621 if (len < 2)
5622 return bad_readline();
5623
5624 key = PyLong_FromString(s, NULL, 10);
5625 if (key == NULL)
5626 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005627 idx = PyLong_AsSsize_t(key);
5628 if (idx == -1 && PyErr_Occurred()) {
5629 Py_DECREF(key);
5630 return -1;
5631 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005633 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634 if (value == NULL) {
5635 if (!PyErr_Occurred())
5636 PyErr_SetObject(PyExc_KeyError, key);
5637 Py_DECREF(key);
5638 return -1;
5639 }
5640 Py_DECREF(key);
5641
5642 PDATA_APPEND(self->stack, value, -1);
5643 return 0;
5644}
5645
5646static int
5647load_binget(UnpicklerObject *self)
5648{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005649 PyObject *value;
5650 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005651 char *s;
5652
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005653 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005654 return -1;
5655
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005656 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005658 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005659 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005660 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005661 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005663 Py_DECREF(key);
5664 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005665 return -1;
5666 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005667
5668 PDATA_APPEND(self->stack, value, -1);
5669 return 0;
5670}
5671
5672static int
5673load_long_binget(UnpicklerObject *self)
5674{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005675 PyObject *value;
5676 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005677 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005678
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005679 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005680 return -1;
5681
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005682 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005683
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005684 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005685 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005686 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005687 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005689 Py_DECREF(key);
5690 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005691 return -1;
5692 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005693
5694 PDATA_APPEND(self->stack, value, -1);
5695 return 0;
5696}
5697
5698/* Push an object from the extension registry (EXT[124]). nbytes is
5699 * the number of bytes following the opcode, holding the index (code) value.
5700 */
5701static int
5702load_extension(UnpicklerObject *self, int nbytes)
5703{
5704 char *codebytes; /* the nbytes bytes after the opcode */
5705 long code; /* calc_binint returns long */
5706 PyObject *py_code; /* code as a Python int */
5707 PyObject *obj; /* the object to push */
5708 PyObject *pair; /* (module_name, class_name) */
5709 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005710 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005711
5712 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005713 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005714 return -1;
5715 code = calc_binint(codebytes, nbytes);
5716 if (code <= 0) { /* note that 0 is forbidden */
5717 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005718 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005719 return -1;
5720 }
5721
5722 /* Look for the code in the cache. */
5723 py_code = PyLong_FromLong(code);
5724 if (py_code == NULL)
5725 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005726 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005727 if (obj != NULL) {
5728 /* Bingo. */
5729 Py_DECREF(py_code);
5730 PDATA_APPEND(self->stack, obj, -1);
5731 return 0;
5732 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005733 if (PyErr_Occurred()) {
5734 Py_DECREF(py_code);
5735 return -1;
5736 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005737
5738 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005739 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005740 if (pair == NULL) {
5741 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005742 if (!PyErr_Occurred()) {
5743 PyErr_Format(PyExc_ValueError, "unregistered extension "
5744 "code %ld", code);
5745 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005746 return -1;
5747 }
5748 /* Since the extension registry is manipulable via Python code,
5749 * confirm that pair is really a 2-tuple of strings.
5750 */
5751 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5752 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5753 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5754 Py_DECREF(py_code);
5755 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5756 "isn't a 2-tuple of strings", code);
5757 return -1;
5758 }
5759 /* Load the object. */
5760 obj = find_class(self, module_name, class_name);
5761 if (obj == NULL) {
5762 Py_DECREF(py_code);
5763 return -1;
5764 }
5765 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005766 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767 Py_DECREF(py_code);
5768 if (code < 0) {
5769 Py_DECREF(obj);
5770 return -1;
5771 }
5772 PDATA_PUSH(self->stack, obj, -1);
5773 return 0;
5774}
5775
5776static int
5777load_put(UnpicklerObject *self)
5778{
5779 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005780 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005782 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005783
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005784 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005785 return -1;
5786 if (len < 2)
5787 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005788 if (Py_SIZE(self->stack) <= self->stack->fence)
5789 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791
5792 key = PyLong_FromString(s, NULL, 10);
5793 if (key == NULL)
5794 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005795 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005796 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005797 if (idx < 0) {
5798 if (!PyErr_Occurred())
5799 PyErr_SetString(PyExc_ValueError,
5800 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005801 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005802 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005803
5804 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005805}
5806
5807static int
5808load_binput(UnpicklerObject *self)
5809{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005810 PyObject *value;
5811 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005812 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005813
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005814 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005815 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005816
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005817 if (Py_SIZE(self->stack) <= self->stack->fence)
5818 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005819 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005820
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005821 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005822
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005823 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005824}
5825
5826static int
5827load_long_binput(UnpicklerObject *self)
5828{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005829 PyObject *value;
5830 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005831 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005832
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005833 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005834 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005835
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005836 if (Py_SIZE(self->stack) <= self->stack->fence)
5837 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005838 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005839
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005840 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005841 if (idx < 0) {
5842 PyErr_SetString(PyExc_ValueError,
5843 "negative LONG_BINPUT argument");
5844 return -1;
5845 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005847 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005848}
5849
5850static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005851load_memoize(UnpicklerObject *self)
5852{
5853 PyObject *value;
5854
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005855 if (Py_SIZE(self->stack) <= self->stack->fence)
5856 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005857 value = self->stack->data[Py_SIZE(self->stack) - 1];
5858
5859 return _Unpickler_MemoPut(self, self->memo_len, value);
5860}
5861
5862static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005863do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005864{
5865 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005866 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005867 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005868 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005869 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005870
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005871 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005872 if (x > len || x <= self->stack->fence)
5873 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005874 if (len == x) /* nothing to do */
5875 return 0;
5876
5877 list = self->stack->data[x - 1];
5878
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005879 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005880 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005881 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005882
5883 slice = Pdata_poplist(self->stack, x);
5884 if (!slice)
5885 return -1;
5886 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005887 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005888 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005889 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005890 }
5891 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005892 PyObject *extend_func;
5893 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005894
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005895 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
5896 if (extend_func != NULL) {
5897 slice = Pdata_poplist(self->stack, x);
5898 if (!slice) {
5899 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005900 return -1;
5901 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005902 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005903 Py_DECREF(extend_func);
5904 if (result == NULL)
5905 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005906 Py_DECREF(result);
5907 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005908 else {
5909 PyObject *append_func;
5910 _Py_IDENTIFIER(append);
5911
5912 /* Even if the PEP 307 requires extend() and append() methods,
5913 fall back on append() if the object has no extend() method
5914 for backward compatibility. */
5915 PyErr_Clear();
5916 append_func = _PyObject_GetAttrId(list, &PyId_append);
5917 if (append_func == NULL)
5918 return -1;
5919 for (i = x; i < len; i++) {
5920 value = self->stack->data[i];
5921 result = _Pickle_FastCall(append_func, value);
5922 if (result == NULL) {
5923 Pdata_clear(self->stack, i + 1);
5924 Py_SIZE(self->stack) = x;
5925 Py_DECREF(append_func);
5926 return -1;
5927 }
5928 Py_DECREF(result);
5929 }
5930 Py_SIZE(self->stack) = x;
5931 Py_DECREF(append_func);
5932 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005933 }
5934
5935 return 0;
5936}
5937
5938static int
5939load_append(UnpicklerObject *self)
5940{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005941 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5942 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005943 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005944}
5945
5946static int
5947load_appends(UnpicklerObject *self)
5948{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005949 Py_ssize_t i = marker(self);
5950 if (i < 0)
5951 return -1;
5952 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005953}
5954
5955static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005956do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005957{
5958 PyObject *value, *key;
5959 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005960 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005961 int status = 0;
5962
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005963 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005964 if (x > len || x <= self->stack->fence)
5965 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005966 if (len == x) /* nothing to do */
5967 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005968 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005969 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005970 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005971 PyErr_SetString(st->UnpicklingError,
5972 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005973 return -1;
5974 }
5975
5976 /* Here, dict does not actually need to be a PyDict; it could be anything
5977 that supports the __setitem__ attribute. */
5978 dict = self->stack->data[x - 1];
5979
5980 for (i = x + 1; i < len; i += 2) {
5981 key = self->stack->data[i - 1];
5982 value = self->stack->data[i];
5983 if (PyObject_SetItem(dict, key, value) < 0) {
5984 status = -1;
5985 break;
5986 }
5987 }
5988
5989 Pdata_clear(self->stack, x);
5990 return status;
5991}
5992
5993static int
5994load_setitem(UnpicklerObject *self)
5995{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005996 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005997}
5998
5999static int
6000load_setitems(UnpicklerObject *self)
6001{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006002 Py_ssize_t i = marker(self);
6003 if (i < 0)
6004 return -1;
6005 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006006}
6007
6008static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006009load_additems(UnpicklerObject *self)
6010{
6011 PyObject *set;
6012 Py_ssize_t mark, len, i;
6013
6014 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006015 if (mark < 0)
6016 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006017 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006018 if (mark > len || mark <= self->stack->fence)
6019 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006020 if (len == mark) /* nothing to do */
6021 return 0;
6022
6023 set = self->stack->data[mark - 1];
6024
6025 if (PySet_Check(set)) {
6026 PyObject *items;
6027 int status;
6028
6029 items = Pdata_poptuple(self->stack, mark);
6030 if (items == NULL)
6031 return -1;
6032
6033 status = _PySet_Update(set, items);
6034 Py_DECREF(items);
6035 return status;
6036 }
6037 else {
6038 PyObject *add_func;
6039 _Py_IDENTIFIER(add);
6040
6041 add_func = _PyObject_GetAttrId(set, &PyId_add);
6042 if (add_func == NULL)
6043 return -1;
6044 for (i = mark; i < len; i++) {
6045 PyObject *result;
6046 PyObject *item;
6047
6048 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006049 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006050 if (result == NULL) {
6051 Pdata_clear(self->stack, i + 1);
6052 Py_SIZE(self->stack) = mark;
6053 return -1;
6054 }
6055 Py_DECREF(result);
6056 }
6057 Py_SIZE(self->stack) = mark;
6058 }
6059
6060 return 0;
6061}
6062
6063static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006064load_build(UnpicklerObject *self)
6065{
6066 PyObject *state, *inst, *slotstate;
6067 PyObject *setstate;
6068 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006069 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006070
6071 /* Stack is ... instance, state. We want to leave instance at
6072 * the stack top, possibly mutated via instance.__setstate__(state).
6073 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006074 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6075 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006076
6077 PDATA_POP(self->stack, state);
6078 if (state == NULL)
6079 return -1;
6080
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006081 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006082
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006083 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006084 if (setstate == NULL) {
6085 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6086 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006087 else {
6088 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006089 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006090 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006091 }
6092 else {
6093 PyObject *result;
6094
6095 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006096 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006097 Py_DECREF(setstate);
6098 if (result == NULL)
6099 return -1;
6100 Py_DECREF(result);
6101 return 0;
6102 }
6103
6104 /* A default __setstate__. First see whether state embeds a
6105 * slot state dict too (a proto 2 addition).
6106 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006107 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006108 PyObject *tmp = state;
6109
6110 state = PyTuple_GET_ITEM(tmp, 0);
6111 slotstate = PyTuple_GET_ITEM(tmp, 1);
6112 Py_INCREF(state);
6113 Py_INCREF(slotstate);
6114 Py_DECREF(tmp);
6115 }
6116 else
6117 slotstate = NULL;
6118
6119 /* Set inst.__dict__ from the state dict (if any). */
6120 if (state != Py_None) {
6121 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006122 PyObject *d_key, *d_value;
6123 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006124 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006125
6126 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006127 PickleState *st = _Pickle_GetGlobalState();
6128 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006129 goto error;
6130 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006131 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006132 if (dict == NULL)
6133 goto error;
6134
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006135 i = 0;
6136 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6137 /* normally the keys for instance attributes are
6138 interned. we should try to do that here. */
6139 Py_INCREF(d_key);
6140 if (PyUnicode_CheckExact(d_key))
6141 PyUnicode_InternInPlace(&d_key);
6142 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6143 Py_DECREF(d_key);
6144 goto error;
6145 }
6146 Py_DECREF(d_key);
6147 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006148 Py_DECREF(dict);
6149 }
6150
6151 /* Also set instance attributes from the slotstate dict (if any). */
6152 if (slotstate != NULL) {
6153 PyObject *d_key, *d_value;
6154 Py_ssize_t i;
6155
6156 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006157 PickleState *st = _Pickle_GetGlobalState();
6158 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006159 "slot state is not a dictionary");
6160 goto error;
6161 }
6162 i = 0;
6163 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6164 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6165 goto error;
6166 }
6167 }
6168
6169 if (0) {
6170 error:
6171 status = -1;
6172 }
6173
6174 Py_DECREF(state);
6175 Py_XDECREF(slotstate);
6176 return status;
6177}
6178
6179static int
6180load_mark(UnpicklerObject *self)
6181{
6182
6183 /* Note that we split the (pickle.py) stack into two stacks, an
6184 * object stack and a mark stack. Here we push a mark onto the
6185 * mark stack.
6186 */
6187
6188 if ((self->num_marks + 1) >= self->marks_size) {
6189 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006190
6191 /* Use the size_t type to check for overflow. */
6192 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006193 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006194 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006195 PyErr_NoMemory();
6196 return -1;
6197 }
6198
6199 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006200 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006201 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006202 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6203 if (self->marks == NULL) {
6204 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006205 PyErr_NoMemory();
6206 return -1;
6207 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006208 self->marks_size = (Py_ssize_t)alloc;
6209 }
6210
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006211 self->stack->mark_set = 1;
6212 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006213
6214 return 0;
6215}
6216
6217static int
6218load_reduce(UnpicklerObject *self)
6219{
6220 PyObject *callable = NULL;
6221 PyObject *argtup = NULL;
6222 PyObject *obj = NULL;
6223
6224 PDATA_POP(self->stack, argtup);
6225 if (argtup == NULL)
6226 return -1;
6227 PDATA_POP(self->stack, callable);
6228 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006229 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006230 Py_DECREF(callable);
6231 }
6232 Py_DECREF(argtup);
6233
6234 if (obj == NULL)
6235 return -1;
6236
6237 PDATA_PUSH(self->stack, obj, -1);
6238 return 0;
6239}
6240
6241/* Just raises an error if we don't know the protocol specified. PROTO
6242 * is the first opcode for protocols >= 2.
6243 */
6244static int
6245load_proto(UnpicklerObject *self)
6246{
6247 char *s;
6248 int i;
6249
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006250 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006251 return -1;
6252
6253 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006254 if (i <= HIGHEST_PROTOCOL) {
6255 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006256 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006257 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006258
6259 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6260 return -1;
6261}
6262
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006263static int
6264load_frame(UnpicklerObject *self)
6265{
6266 char *s;
6267 Py_ssize_t frame_len;
6268
6269 if (_Unpickler_Read(self, &s, 8) < 0)
6270 return -1;
6271
6272 frame_len = calc_binsize(s, 8);
6273 if (frame_len < 0) {
6274 PyErr_Format(PyExc_OverflowError,
6275 "FRAME length exceeds system's maximum of %zd bytes",
6276 PY_SSIZE_T_MAX);
6277 return -1;
6278 }
6279
6280 if (_Unpickler_Read(self, &s, frame_len) < 0)
6281 return -1;
6282
6283 /* Rewind to start of frame */
6284 self->next_read_idx -= frame_len;
6285 return 0;
6286}
6287
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006288static PyObject *
6289load(UnpicklerObject *self)
6290{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006291 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006292 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006293
6294 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006295 self->stack->mark_set = 0;
6296 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006297 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006298 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006299 Pdata_clear(self->stack, 0);
6300
6301 /* Convenient macros for the dispatch while-switch loop just below. */
6302#define OP(opcode, load_func) \
6303 case opcode: if (load_func(self) < 0) break; continue;
6304
6305#define OP_ARG(opcode, load_func, arg) \
6306 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6307
6308 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006309 if (_Unpickler_Read(self, &s, 1) < 0) {
6310 PickleState *st = _Pickle_GetGlobalState();
6311 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6312 PyErr_Format(PyExc_EOFError, "Ran out of input");
6313 }
6314 return NULL;
6315 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006316
6317 switch ((enum opcode)s[0]) {
6318 OP(NONE, load_none)
6319 OP(BININT, load_binint)
6320 OP(BININT1, load_binint1)
6321 OP(BININT2, load_binint2)
6322 OP(INT, load_int)
6323 OP(LONG, load_long)
6324 OP_ARG(LONG1, load_counted_long, 1)
6325 OP_ARG(LONG4, load_counted_long, 4)
6326 OP(FLOAT, load_float)
6327 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006328 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6329 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6330 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6331 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6332 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006333 OP(STRING, load_string)
6334 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006335 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6336 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6337 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006338 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6339 OP_ARG(TUPLE1, load_counted_tuple, 1)
6340 OP_ARG(TUPLE2, load_counted_tuple, 2)
6341 OP_ARG(TUPLE3, load_counted_tuple, 3)
6342 OP(TUPLE, load_tuple)
6343 OP(EMPTY_LIST, load_empty_list)
6344 OP(LIST, load_list)
6345 OP(EMPTY_DICT, load_empty_dict)
6346 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006347 OP(EMPTY_SET, load_empty_set)
6348 OP(ADDITEMS, load_additems)
6349 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006350 OP(OBJ, load_obj)
6351 OP(INST, load_inst)
6352 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006353 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006354 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006355 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006356 OP(APPEND, load_append)
6357 OP(APPENDS, load_appends)
6358 OP(BUILD, load_build)
6359 OP(DUP, load_dup)
6360 OP(BINGET, load_binget)
6361 OP(LONG_BINGET, load_long_binget)
6362 OP(GET, load_get)
6363 OP(MARK, load_mark)
6364 OP(BINPUT, load_binput)
6365 OP(LONG_BINPUT, load_long_binput)
6366 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006367 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006368 OP(POP, load_pop)
6369 OP(POP_MARK, load_pop_mark)
6370 OP(SETITEM, load_setitem)
6371 OP(SETITEMS, load_setitems)
6372 OP(PERSID, load_persid)
6373 OP(BINPERSID, load_binpersid)
6374 OP(REDUCE, load_reduce)
6375 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006376 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006377 OP_ARG(EXT1, load_extension, 1)
6378 OP_ARG(EXT2, load_extension, 2)
6379 OP_ARG(EXT4, load_extension, 4)
6380 OP_ARG(NEWTRUE, load_bool, Py_True)
6381 OP_ARG(NEWFALSE, load_bool, Py_False)
6382
6383 case STOP:
6384 break;
6385
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006386 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006387 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006388 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006389 unsigned char c = (unsigned char) *s;
6390 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6391 PyErr_Format(st->UnpicklingError,
6392 "invalid load key, '%c'.", c);
6393 }
6394 else {
6395 PyErr_Format(st->UnpicklingError,
6396 "invalid load key, '\\x%02x'.", c);
6397 }
6398 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006399 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006400 }
6401
6402 break; /* and we are done! */
6403 }
6404
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006405 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006406 return NULL;
6407 }
6408
Victor Stinner2ae57e32013-10-31 13:39:23 +01006409 if (_Unpickler_SkipConsumed(self) < 0)
6410 return NULL;
6411
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006412 PDATA_POP(self->stack, value);
6413 return value;
6414}
6415
Larry Hastings61272b72014-01-07 12:41:53 -08006416/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006417
6418_pickle.Unpickler.load
6419
6420Load a pickle.
6421
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006422Read a pickled object representation from the open file object given
6423in the constructor, and return the reconstituted object hierarchy
6424specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006425[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006426
Larry Hastings3cceb382014-01-04 11:09:09 -08006427static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006428_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006429/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006430{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006431 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006432
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006433 /* Check whether the Unpickler was initialized correctly. This prevents
6434 segfaulting if a subclass overridden __init__ with a function that does
6435 not call Unpickler.__init__(). Here, we simply ensure that self->read
6436 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006437 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006438 PickleState *st = _Pickle_GetGlobalState();
6439 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006440 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006441 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006442 return NULL;
6443 }
6444
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006445 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006446}
6447
6448/* The name of find_class() is misleading. In newer pickle protocols, this
6449 function is used for loading any global (i.e., functions), not just
6450 classes. The name is kept only for backward compatibility. */
6451
Larry Hastings61272b72014-01-07 12:41:53 -08006452/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006453
6454_pickle.Unpickler.find_class
6455
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006456 module_name: object
6457 global_name: object
6458 /
6459
6460Return an object from a specified module.
6461
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006462If necessary, the module will be imported. Subclasses may override
6463this method (e.g. to restrict unpickling of arbitrary classes and
6464functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006465
6466This method is called whenever a class or a function object is
6467needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006468[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006469
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006470static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006471_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6472 PyObject *module_name,
6473 PyObject *global_name)
6474/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006475{
6476 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006477 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006478
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006479 /* Try to map the old names used in Python 2.x to the new ones used in
6480 Python 3.x. We do this only with old pickle protocols and when the
6481 user has not disabled the feature. */
6482 if (self->proto < 3 && self->fix_imports) {
6483 PyObject *key;
6484 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006485 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006486
6487 /* Check if the global (i.e., a function or a class) was renamed
6488 or moved to another module. */
6489 key = PyTuple_Pack(2, module_name, global_name);
6490 if (key == NULL)
6491 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006492 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006493 Py_DECREF(key);
6494 if (item) {
6495 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6496 PyErr_Format(PyExc_RuntimeError,
6497 "_compat_pickle.NAME_MAPPING values should be "
6498 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6499 return NULL;
6500 }
6501 module_name = PyTuple_GET_ITEM(item, 0);
6502 global_name = PyTuple_GET_ITEM(item, 1);
6503 if (!PyUnicode_Check(module_name) ||
6504 !PyUnicode_Check(global_name)) {
6505 PyErr_Format(PyExc_RuntimeError,
6506 "_compat_pickle.NAME_MAPPING values should be "
6507 "pairs of str, not (%.200s, %.200s)",
6508 Py_TYPE(module_name)->tp_name,
6509 Py_TYPE(global_name)->tp_name);
6510 return NULL;
6511 }
6512 }
6513 else if (PyErr_Occurred()) {
6514 return NULL;
6515 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006516 else {
6517 /* Check if the module was renamed. */
6518 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6519 if (item) {
6520 if (!PyUnicode_Check(item)) {
6521 PyErr_Format(PyExc_RuntimeError,
6522 "_compat_pickle.IMPORT_MAPPING values should be "
6523 "strings, not %.200s", Py_TYPE(item)->tp_name);
6524 return NULL;
6525 }
6526 module_name = item;
6527 }
6528 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006529 return NULL;
6530 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006531 }
6532 }
6533
Eric Snow3f9eee62017-09-15 16:35:20 -06006534 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006535 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006536 if (PyErr_Occurred())
6537 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006538 module = PyImport_Import(module_name);
6539 if (module == NULL)
6540 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006541 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006542 global = getattribute(module, global_name, self->proto >= 4);
6543 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006544 return global;
6545}
6546
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006547/*[clinic input]
6548
6549_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6550
6551Returns size in memory, in bytes.
6552[clinic start generated code]*/
6553
6554static Py_ssize_t
6555_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6556/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6557{
6558 Py_ssize_t res;
6559
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006560 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006561 if (self->memo != NULL)
6562 res += self->memo_size * sizeof(PyObject *);
6563 if (self->marks != NULL)
6564 res += self->marks_size * sizeof(Py_ssize_t);
6565 if (self->input_line != NULL)
6566 res += strlen(self->input_line) + 1;
6567 if (self->encoding != NULL)
6568 res += strlen(self->encoding) + 1;
6569 if (self->errors != NULL)
6570 res += strlen(self->errors) + 1;
6571 return res;
6572}
6573
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006574static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006575 _PICKLE_UNPICKLER_LOAD_METHODDEF
6576 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006577 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006578 {NULL, NULL} /* sentinel */
6579};
6580
6581static void
6582Unpickler_dealloc(UnpicklerObject *self)
6583{
6584 PyObject_GC_UnTrack((PyObject *)self);
6585 Py_XDECREF(self->readline);
6586 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006587 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006588 Py_XDECREF(self->stack);
6589 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006590 if (self->buffer.buf != NULL) {
6591 PyBuffer_Release(&self->buffer);
6592 self->buffer.buf = NULL;
6593 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006594
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006595 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006596 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006597 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006598 PyMem_Free(self->encoding);
6599 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006600
6601 Py_TYPE(self)->tp_free((PyObject *)self);
6602}
6603
6604static int
6605Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6606{
6607 Py_VISIT(self->readline);
6608 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006609 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006610 Py_VISIT(self->stack);
6611 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006612 return 0;
6613}
6614
6615static int
6616Unpickler_clear(UnpicklerObject *self)
6617{
6618 Py_CLEAR(self->readline);
6619 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006620 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006621 Py_CLEAR(self->stack);
6622 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006623 if (self->buffer.buf != NULL) {
6624 PyBuffer_Release(&self->buffer);
6625 self->buffer.buf = NULL;
6626 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006627
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006628 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006629 PyMem_Free(self->marks);
6630 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006631 PyMem_Free(self->input_line);
6632 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006633 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006634 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006635 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006636 self->errors = NULL;
6637
6638 return 0;
6639}
6640
Larry Hastings61272b72014-01-07 12:41:53 -08006641/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006642
6643_pickle.Unpickler.__init__
6644
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006645 file: object
6646 *
6647 fix_imports: bool = True
6648 encoding: str = 'ASCII'
6649 errors: str = 'strict'
6650
6651This takes a binary file for reading a pickle data stream.
6652
6653The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006654protocol argument is needed. Bytes past the pickled object's
6655representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006656
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006657The argument *file* must have two methods, a read() method that takes
6658an integer argument, and a readline() method that requires no
6659arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006660binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006661other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006662
6663Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006664which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006665generated by Python 2. If *fix_imports* is True, pickle will try to
6666map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006667*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006668instances pickled by Python 2; these default to 'ASCII' and 'strict',
6669respectively. The *encoding* can be 'bytes' to read these 8-bit
6670string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006671[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006672
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006673static int
Larry Hastings89964c42015-04-14 18:07:59 -04006674_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6675 int fix_imports, const char *encoding,
6676 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006677/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006678{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006679 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006680
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006681 /* In case of multiple __init__() calls, clear previous content. */
6682 if (self->read != NULL)
6683 (void)Unpickler_clear(self);
6684
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006685 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006686 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006687
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006688 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006689 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006690
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006691 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006692
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03006693 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6694 &PyId_persistent_load);
6695 if (self->pers_func == NULL) {
6696 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
6697 return -1;
6698 }
6699 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006700 }
6701
6702 self->stack = (Pdata *)Pdata_New();
6703 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006704 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006705
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006706 self->memo_size = 32;
6707 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006708 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006709 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006710
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006711 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006712
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006713 return 0;
6714}
6715
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006716
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006717/* Define a proxy object for the Unpickler's internal memo object. This is to
6718 * avoid breaking code like:
6719 * unpickler.memo.clear()
6720 * and
6721 * unpickler.memo = saved_memo
6722 * Is this a good idea? Not really, but we don't want to break code that uses
6723 * it. Note that we don't implement the entire mapping API here. This is
6724 * intentional, as these should be treated as black-box implementation details.
6725 *
6726 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006727 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006728 */
6729
Larry Hastings61272b72014-01-07 12:41:53 -08006730/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006731_pickle.UnpicklerMemoProxy.clear
6732
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006733Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006734[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006735
Larry Hastings3cceb382014-01-04 11:09:09 -08006736static PyObject *
6737_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006738/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006739{
6740 _Unpickler_MemoCleanup(self->unpickler);
6741 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6742 if (self->unpickler->memo == NULL)
6743 return NULL;
6744 Py_RETURN_NONE;
6745}
6746
Larry Hastings61272b72014-01-07 12:41:53 -08006747/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006748_pickle.UnpicklerMemoProxy.copy
6749
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006750Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006751[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006752
Larry Hastings3cceb382014-01-04 11:09:09 -08006753static PyObject *
6754_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006755/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006756{
6757 Py_ssize_t i;
6758 PyObject *new_memo = PyDict_New();
6759 if (new_memo == NULL)
6760 return NULL;
6761
6762 for (i = 0; i < self->unpickler->memo_size; i++) {
6763 int status;
6764 PyObject *key, *value;
6765
6766 value = self->unpickler->memo[i];
6767 if (value == NULL)
6768 continue;
6769
6770 key = PyLong_FromSsize_t(i);
6771 if (key == NULL)
6772 goto error;
6773 status = PyDict_SetItem(new_memo, key, value);
6774 Py_DECREF(key);
6775 if (status < 0)
6776 goto error;
6777 }
6778 return new_memo;
6779
6780error:
6781 Py_DECREF(new_memo);
6782 return NULL;
6783}
6784
Larry Hastings61272b72014-01-07 12:41:53 -08006785/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006786_pickle.UnpicklerMemoProxy.__reduce__
6787
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006788Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006789[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006790
Larry Hastings3cceb382014-01-04 11:09:09 -08006791static PyObject *
6792_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006793/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006794{
6795 PyObject *reduce_value;
6796 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006797 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006798 if (contents == NULL)
6799 return NULL;
6800
6801 reduce_value = PyTuple_New(2);
6802 if (reduce_value == NULL) {
6803 Py_DECREF(contents);
6804 return NULL;
6805 }
6806 constructor_args = PyTuple_New(1);
6807 if (constructor_args == NULL) {
6808 Py_DECREF(contents);
6809 Py_DECREF(reduce_value);
6810 return NULL;
6811 }
6812 PyTuple_SET_ITEM(constructor_args, 0, contents);
6813 Py_INCREF((PyObject *)&PyDict_Type);
6814 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6815 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6816 return reduce_value;
6817}
6818
6819static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006820 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6821 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6822 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006823 {NULL, NULL} /* sentinel */
6824};
6825
6826static void
6827UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6828{
6829 PyObject_GC_UnTrack(self);
6830 Py_XDECREF(self->unpickler);
6831 PyObject_GC_Del((PyObject *)self);
6832}
6833
6834static int
6835UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6836 visitproc visit, void *arg)
6837{
6838 Py_VISIT(self->unpickler);
6839 return 0;
6840}
6841
6842static int
6843UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6844{
6845 Py_CLEAR(self->unpickler);
6846 return 0;
6847}
6848
6849static PyTypeObject UnpicklerMemoProxyType = {
6850 PyVarObject_HEAD_INIT(NULL, 0)
6851 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6852 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6853 0,
6854 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6855 0, /* tp_print */
6856 0, /* tp_getattr */
6857 0, /* tp_setattr */
6858 0, /* tp_compare */
6859 0, /* tp_repr */
6860 0, /* tp_as_number */
6861 0, /* tp_as_sequence */
6862 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006863 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006864 0, /* tp_call */
6865 0, /* tp_str */
6866 PyObject_GenericGetAttr, /* tp_getattro */
6867 PyObject_GenericSetAttr, /* tp_setattro */
6868 0, /* tp_as_buffer */
6869 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6870 0, /* tp_doc */
6871 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6872 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6873 0, /* tp_richcompare */
6874 0, /* tp_weaklistoffset */
6875 0, /* tp_iter */
6876 0, /* tp_iternext */
6877 unpicklerproxy_methods, /* tp_methods */
6878};
6879
6880static PyObject *
6881UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6882{
6883 UnpicklerMemoProxyObject *self;
6884
6885 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6886 &UnpicklerMemoProxyType);
6887 if (self == NULL)
6888 return NULL;
6889 Py_INCREF(unpickler);
6890 self->unpickler = unpickler;
6891 PyObject_GC_Track(self);
6892 return (PyObject *)self;
6893}
6894
6895/*****************************************************************************/
6896
6897
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006898static PyObject *
6899Unpickler_get_memo(UnpicklerObject *self)
6900{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006901 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006902}
6903
6904static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006905Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006906{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006907 PyObject **new_memo;
6908 Py_ssize_t new_memo_size = 0;
6909 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006910
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006911 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006912 PyErr_SetString(PyExc_TypeError,
6913 "attribute deletion is not supported");
6914 return -1;
6915 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006916
6917 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6918 UnpicklerObject *unpickler =
6919 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6920
6921 new_memo_size = unpickler->memo_size;
6922 new_memo = _Unpickler_NewMemo(new_memo_size);
6923 if (new_memo == NULL)
6924 return -1;
6925
6926 for (i = 0; i < new_memo_size; i++) {
6927 Py_XINCREF(unpickler->memo[i]);
6928 new_memo[i] = unpickler->memo[i];
6929 }
6930 }
6931 else if (PyDict_Check(obj)) {
6932 Py_ssize_t i = 0;
6933 PyObject *key, *value;
6934
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006935 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006936 new_memo = _Unpickler_NewMemo(new_memo_size);
6937 if (new_memo == NULL)
6938 return -1;
6939
6940 while (PyDict_Next(obj, &i, &key, &value)) {
6941 Py_ssize_t idx;
6942 if (!PyLong_Check(key)) {
6943 PyErr_SetString(PyExc_TypeError,
6944 "memo key must be integers");
6945 goto error;
6946 }
6947 idx = PyLong_AsSsize_t(key);
6948 if (idx == -1 && PyErr_Occurred())
6949 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006950 if (idx < 0) {
6951 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006952 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006953 goto error;
6954 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006955 if (_Unpickler_MemoPut(self, idx, value) < 0)
6956 goto error;
6957 }
6958 }
6959 else {
6960 PyErr_Format(PyExc_TypeError,
6961 "'memo' attribute must be an UnpicklerMemoProxy object"
6962 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006963 return -1;
6964 }
6965
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006966 _Unpickler_MemoCleanup(self);
6967 self->memo_size = new_memo_size;
6968 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006969
6970 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006971
6972 error:
6973 if (new_memo_size) {
6974 i = new_memo_size;
6975 while (--i >= 0) {
6976 Py_XDECREF(new_memo[i]);
6977 }
6978 PyMem_FREE(new_memo);
6979 }
6980 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006981}
6982
6983static PyObject *
6984Unpickler_get_persload(UnpicklerObject *self)
6985{
6986 if (self->pers_func == NULL)
6987 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6988 else
6989 Py_INCREF(self->pers_func);
6990 return self->pers_func;
6991}
6992
6993static int
6994Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6995{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006996 if (value == NULL) {
6997 PyErr_SetString(PyExc_TypeError,
6998 "attribute deletion is not supported");
6999 return -1;
7000 }
7001 if (!PyCallable_Check(value)) {
7002 PyErr_SetString(PyExc_TypeError,
7003 "persistent_load must be a callable taking "
7004 "one argument");
7005 return -1;
7006 }
7007
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007008 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007009 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007010
7011 return 0;
7012}
7013
7014static PyGetSetDef Unpickler_getsets[] = {
7015 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7016 {"persistent_load", (getter)Unpickler_get_persload,
7017 (setter)Unpickler_set_persload},
7018 {NULL}
7019};
7020
7021static PyTypeObject Unpickler_Type = {
7022 PyVarObject_HEAD_INIT(NULL, 0)
7023 "_pickle.Unpickler", /*tp_name*/
7024 sizeof(UnpicklerObject), /*tp_basicsize*/
7025 0, /*tp_itemsize*/
7026 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7027 0, /*tp_print*/
7028 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007029 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007030 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007031 0, /*tp_repr*/
7032 0, /*tp_as_number*/
7033 0, /*tp_as_sequence*/
7034 0, /*tp_as_mapping*/
7035 0, /*tp_hash*/
7036 0, /*tp_call*/
7037 0, /*tp_str*/
7038 0, /*tp_getattro*/
7039 0, /*tp_setattro*/
7040 0, /*tp_as_buffer*/
7041 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007042 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007043 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7044 (inquiry)Unpickler_clear, /*tp_clear*/
7045 0, /*tp_richcompare*/
7046 0, /*tp_weaklistoffset*/
7047 0, /*tp_iter*/
7048 0, /*tp_iternext*/
7049 Unpickler_methods, /*tp_methods*/
7050 0, /*tp_members*/
7051 Unpickler_getsets, /*tp_getset*/
7052 0, /*tp_base*/
7053 0, /*tp_dict*/
7054 0, /*tp_descr_get*/
7055 0, /*tp_descr_set*/
7056 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007057 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007058 PyType_GenericAlloc, /*tp_alloc*/
7059 PyType_GenericNew, /*tp_new*/
7060 PyObject_GC_Del, /*tp_free*/
7061 0, /*tp_is_gc*/
7062};
7063
Larry Hastings61272b72014-01-07 12:41:53 -08007064/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007065
7066_pickle.dump
7067
7068 obj: object
7069 file: object
7070 protocol: object = NULL
7071 *
7072 fix_imports: bool = True
7073
7074Write a pickled representation of obj to the open file object file.
7075
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007076This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7077be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007078
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007079The optional *protocol* argument tells the pickler to use the given
7080protocol supported protocols are 0, 1, 2, 3 and 4. The default
7081protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007082
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007083Specifying a negative protocol version selects the highest protocol
7084version supported. The higher the protocol used, the more recent the
7085version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007086
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007087The *file* argument must have a write() method that accepts a single
7088bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007089writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007090this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007091
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007092If *fix_imports* is True and protocol is less than 3, pickle will try
7093to map the new Python 3 names to the old module names used in Python
70942, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007095[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007096
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007097static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007098_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007099 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007100/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007101{
7102 PicklerObject *pickler = _Pickler_New();
7103
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007104 if (pickler == NULL)
7105 return NULL;
7106
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007107 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007108 goto error;
7109
7110 if (_Pickler_SetOutputStream(pickler, file) < 0)
7111 goto error;
7112
7113 if (dump(pickler, obj) < 0)
7114 goto error;
7115
7116 if (_Pickler_FlushToFile(pickler) < 0)
7117 goto error;
7118
7119 Py_DECREF(pickler);
7120 Py_RETURN_NONE;
7121
7122 error:
7123 Py_XDECREF(pickler);
7124 return NULL;
7125}
7126
Larry Hastings61272b72014-01-07 12:41:53 -08007127/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007128
7129_pickle.dumps
7130
7131 obj: object
7132 protocol: object = NULL
7133 *
7134 fix_imports: bool = True
7135
7136Return the pickled representation of the object as a bytes object.
7137
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007138The optional *protocol* argument tells the pickler to use the given
7139protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7140protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007141
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007142Specifying a negative protocol version selects the highest protocol
7143version supported. The higher the protocol used, the more recent the
7144version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007145
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007146If *fix_imports* is True and *protocol* is less than 3, pickle will
7147try to map the new Python 3 names to the old module names used in
7148Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007149[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007150
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007151static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007152_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007153 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007154/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007155{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007156 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007157 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007158
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007159 if (pickler == NULL)
7160 return NULL;
7161
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007162 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007163 goto error;
7164
7165 if (dump(pickler, obj) < 0)
7166 goto error;
7167
7168 result = _Pickler_GetString(pickler);
7169 Py_DECREF(pickler);
7170 return result;
7171
7172 error:
7173 Py_XDECREF(pickler);
7174 return NULL;
7175}
7176
Larry Hastings61272b72014-01-07 12:41:53 -08007177/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007178
7179_pickle.load
7180
7181 file: object
7182 *
7183 fix_imports: bool = True
7184 encoding: str = 'ASCII'
7185 errors: str = 'strict'
7186
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007187Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007189This is equivalent to ``Unpickler(file).load()``, but may be more
7190efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007191
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007192The protocol version of the pickle is detected automatically, so no
7193protocol argument is needed. Bytes past the pickled object's
7194representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007195
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007196The argument *file* must have two methods, a read() method that takes
7197an integer argument, and a readline() method that requires no
7198arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007199binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007200other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007201
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007202Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007203which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007204generated by Python 2. If *fix_imports* is True, pickle will try to
7205map the old Python 2 names to the new names used in Python 3. The
7206*encoding* and *errors* tell pickle how to decode 8-bit string
7207instances pickled by Python 2; these default to 'ASCII' and 'strict',
7208respectively. The *encoding* can be 'bytes' to read these 8-bit
7209string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007210[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007211
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007212static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007213_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007214 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007215/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007216{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007217 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007218 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007219
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007220 if (unpickler == NULL)
7221 return NULL;
7222
7223 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7224 goto error;
7225
7226 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7227 goto error;
7228
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007229 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007230
7231 result = load(unpickler);
7232 Py_DECREF(unpickler);
7233 return result;
7234
7235 error:
7236 Py_XDECREF(unpickler);
7237 return NULL;
7238}
7239
Larry Hastings61272b72014-01-07 12:41:53 -08007240/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007241
7242_pickle.loads
7243
7244 data: object
7245 *
7246 fix_imports: bool = True
7247 encoding: str = 'ASCII'
7248 errors: str = 'strict'
7249
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007250Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007251
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007252The protocol version of the pickle is detected automatically, so no
7253protocol argument is needed. Bytes past the pickled object's
7254representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007255
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007256Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007257which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007258generated by Python 2. If *fix_imports* is True, pickle will try to
7259map the old Python 2 names to the new names used in Python 3. The
7260*encoding* and *errors* tell pickle how to decode 8-bit string
7261instances pickled by Python 2; these default to 'ASCII' and 'strict',
7262respectively. The *encoding* can be 'bytes' to read these 8-bit
7263string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007264[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007265
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007266static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007267_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007268 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007269/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007270{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007271 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007272 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007273
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007274 if (unpickler == NULL)
7275 return NULL;
7276
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007277 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007278 goto error;
7279
7280 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7281 goto error;
7282
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007283 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007284
7285 result = load(unpickler);
7286 Py_DECREF(unpickler);
7287 return result;
7288
7289 error:
7290 Py_XDECREF(unpickler);
7291 return NULL;
7292}
7293
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007294static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007295 _PICKLE_DUMP_METHODDEF
7296 _PICKLE_DUMPS_METHODDEF
7297 _PICKLE_LOAD_METHODDEF
7298 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007299 {NULL, NULL} /* sentinel */
7300};
7301
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007302static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007303pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007304{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007305 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007306 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007307}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007308
Stefan Krahf483b0f2013-12-14 13:43:10 +01007309static void
7310pickle_free(PyObject *m)
7311{
7312 _Pickle_ClearState(_Pickle_GetState(m));
7313}
7314
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007315static int
7316pickle_traverse(PyObject *m, visitproc visit, void *arg)
7317{
7318 PickleState *st = _Pickle_GetState(m);
7319 Py_VISIT(st->PickleError);
7320 Py_VISIT(st->PicklingError);
7321 Py_VISIT(st->UnpicklingError);
7322 Py_VISIT(st->dispatch_table);
7323 Py_VISIT(st->extension_registry);
7324 Py_VISIT(st->extension_cache);
7325 Py_VISIT(st->inverted_registry);
7326 Py_VISIT(st->name_mapping_2to3);
7327 Py_VISIT(st->import_mapping_2to3);
7328 Py_VISIT(st->name_mapping_3to2);
7329 Py_VISIT(st->import_mapping_3to2);
7330 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007331 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007332 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007333}
7334
7335static struct PyModuleDef _picklemodule = {
7336 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007337 "_pickle", /* m_name */
7338 pickle_module_doc, /* m_doc */
7339 sizeof(PickleState), /* m_size */
7340 pickle_methods, /* m_methods */
7341 NULL, /* m_reload */
7342 pickle_traverse, /* m_traverse */
7343 pickle_clear, /* m_clear */
7344 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007345};
7346
7347PyMODINIT_FUNC
7348PyInit__pickle(void)
7349{
7350 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007351 PickleState *st;
7352
7353 m = PyState_FindModule(&_picklemodule);
7354 if (m) {
7355 Py_INCREF(m);
7356 return m;
7357 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007358
7359 if (PyType_Ready(&Unpickler_Type) < 0)
7360 return NULL;
7361 if (PyType_Ready(&Pickler_Type) < 0)
7362 return NULL;
7363 if (PyType_Ready(&Pdata_Type) < 0)
7364 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007365 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7366 return NULL;
7367 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7368 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007369
7370 /* Create the module and add the functions. */
7371 m = PyModule_Create(&_picklemodule);
7372 if (m == NULL)
7373 return NULL;
7374
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007375 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007376 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7377 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007378 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007379 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7380 return NULL;
7381
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007382 st = _Pickle_GetState(m);
7383
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007384 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007385 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7386 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007387 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007388 st->PicklingError = \
7389 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7390 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007391 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007392 st->UnpicklingError = \
7393 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7394 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007395 return NULL;
7396
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007397 Py_INCREF(st->PickleError);
7398 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007399 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007400 Py_INCREF(st->PicklingError);
7401 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007402 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007403 Py_INCREF(st->UnpicklingError);
7404 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007405 return NULL;
7406
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007407 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007408 return NULL;
7409
7410 return m;
7411}