blob: 3165b4e6d686dee77a291179b6ba78d172e5e89b [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 }
753 assert(0); /* Never reached */
754 return NULL;
755}
756
757/* Returns -1 on failure, 0 on success. */
758static int
759_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
760{
761 PyMemoEntry *oldtable = NULL;
762 PyMemoEntry *oldentry, *newentry;
763 Py_ssize_t new_size = MT_MINSIZE;
764 Py_ssize_t to_process;
765
766 assert(min_size > 0);
767
768 /* Find the smallest valid table size >= min_size. */
769 while (new_size < min_size && new_size > 0)
770 new_size <<= 1;
771 if (new_size <= 0) {
772 PyErr_NoMemory();
773 return -1;
774 }
775 /* new_size needs to be a power of two. */
776 assert((new_size & (new_size - 1)) == 0);
777
778 /* Allocate new table. */
779 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500780 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000781 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200782 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000783 PyErr_NoMemory();
784 return -1;
785 }
786 self->mt_allocated = new_size;
787 self->mt_mask = new_size - 1;
788 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
789
790 /* Copy entries from the old table. */
791 to_process = self->mt_used;
792 for (oldentry = oldtable; to_process > 0; oldentry++) {
793 if (oldentry->me_key != NULL) {
794 to_process--;
795 /* newentry is a pointer to a chunk of the new
796 mt_table, so we're setting the key:value pair
797 in-place. */
798 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
799 newentry->me_key = oldentry->me_key;
800 newentry->me_value = oldentry->me_value;
801 }
802 }
803
804 /* Deallocate the old table. */
805 PyMem_FREE(oldtable);
806 return 0;
807}
808
809/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200810static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000811PyMemoTable_Get(PyMemoTable *self, PyObject *key)
812{
813 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
814 if (entry->me_key == NULL)
815 return NULL;
816 return &entry->me_value;
817}
818
819/* Returns -1 on failure, 0 on success. */
820static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200821PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000822{
823 PyMemoEntry *entry;
824
825 assert(key != NULL);
826
827 entry = _PyMemoTable_Lookup(self, key);
828 if (entry->me_key != NULL) {
829 entry->me_value = value;
830 return 0;
831 }
832 Py_INCREF(key);
833 entry->me_key = key;
834 entry->me_value = value;
835 self->mt_used++;
836
837 /* If we added a key, we can safely resize. Otherwise just return!
838 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
839 *
840 * Quadrupling the size improves average table sparseness
841 * (reducing collisions) at the cost of some memory. It also halves
842 * the number of expensive resize operations in a growing memo table.
843 *
844 * Very large memo tables (over 50K items) use doubling instead.
845 * This may help applications with severe memory constraints.
846 */
847 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
848 return 0;
849 return _PyMemoTable_ResizeTable(self,
850 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
851}
852
853#undef MT_MINSIZE
854#undef PERTURB_SHIFT
855
856/*************************************************************************/
857
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000858
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000859static int
860_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000861{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300862 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200863 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000864 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000865 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000866 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100867 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000868 return 0;
869}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000870
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100871static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100872_write_size64(char *out, size_t value)
873{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200874 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800875
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200876 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800877
878 for (i = 0; i < sizeof(size_t); i++) {
879 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
880 }
881 for (i = sizeof(size_t); i < 8; i++) {
882 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800883 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100884}
885
886static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100887_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
888{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100889 qdata[0] = FRAME;
890 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100891}
892
893static int
894_Pickler_CommitFrame(PicklerObject *self)
895{
896 size_t frame_len;
897 char *qdata;
898
899 if (!self->framing || self->frame_start == -1)
900 return 0;
901 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
902 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
903 _Pickler_WriteFrameHeader(self, qdata, frame_len);
904 self->frame_start = -1;
905 return 0;
906}
907
908static int
909_Pickler_OpcodeBoundary(PicklerObject *self)
910{
911 Py_ssize_t frame_len;
912
913 if (!self->framing || self->frame_start == -1)
914 return 0;
915 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
916 if (frame_len >= FRAME_SIZE_TARGET)
917 return _Pickler_CommitFrame(self);
918 else
919 return 0;
920}
921
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000922static PyObject *
923_Pickler_GetString(PicklerObject *self)
924{
925 PyObject *output_buffer = self->output_buffer;
926
927 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100928
929 if (_Pickler_CommitFrame(self))
930 return NULL;
931
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000932 self->output_buffer = NULL;
933 /* Resize down to exact size */
934 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
935 return NULL;
936 return output_buffer;
937}
938
939static int
940_Pickler_FlushToFile(PicklerObject *self)
941{
942 PyObject *output, *result;
943
944 assert(self->write != NULL);
945
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100946 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000947 output = _Pickler_GetString(self);
948 if (output == NULL)
949 return -1;
950
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800951 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000952 Py_XDECREF(result);
953 return (result == NULL) ? -1 : 0;
954}
955
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200956static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100957_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000958{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100959 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000960 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100961 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000962
963 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100964 need_new_frame = (self->framing && self->frame_start == -1);
965
966 if (need_new_frame)
967 n = data_len + FRAME_HEADER_SIZE;
968 else
969 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000970
971 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100972 if (required > self->max_output_len) {
973 /* Make place in buffer for the pickle chunk */
974 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
975 PyErr_NoMemory();
976 return -1;
977 }
978 self->max_output_len = (self->output_len + n) / 2 * 3;
979 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
980 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000981 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000982 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100983 if (need_new_frame) {
984 /* Setup new frame */
985 Py_ssize_t frame_start = self->output_len;
986 self->frame_start = frame_start;
987 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
988 /* Write an invalid value, for debugging */
989 buffer[frame_start + i] = 0xFE;
990 }
991 self->output_len += FRAME_HEADER_SIZE;
992 }
993 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000994 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100995 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000996 buffer[self->output_len + i] = s[i];
997 }
998 }
999 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001000 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001001 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001002 self->output_len += data_len;
1003 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001004}
1005
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001006static PicklerObject *
1007_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001008{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001009 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001010
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001011 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1012 if (self == NULL)
1013 return NULL;
1014
1015 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001016 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001017 self->write = NULL;
1018 self->proto = 0;
1019 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001020 self->framing = 0;
1021 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001022 self->fast = 0;
1023 self->fast_nesting = 0;
1024 self->fix_imports = 0;
1025 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001026 self->max_output_len = WRITE_BUF_SIZE;
1027 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001028
1029 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001030 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1031 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001032
1033 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001034 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001035 return NULL;
1036 }
1037 return self;
1038}
1039
1040static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001041_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001043 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001045 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001047 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001048 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001049 proto = PyLong_AsLong(protocol);
1050 if (proto < 0) {
1051 if (proto == -1 && PyErr_Occurred())
1052 return -1;
1053 proto = HIGHEST_PROTOCOL;
1054 }
1055 else if (proto > HIGHEST_PROTOCOL) {
1056 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1057 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001058 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001059 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001060 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001061 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001062 self->bin = proto > 0;
1063 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001064 return 0;
1065}
1066
1067/* Returns -1 (with an exception set) on failure, 0 on success. This may
1068 be called once on a freshly created Pickler. */
1069static int
1070_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1071{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001072 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001073 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001074 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001075 if (self->write == NULL) {
1076 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1077 PyErr_SetString(PyExc_TypeError,
1078 "file must have a 'write' attribute");
1079 return -1;
1080 }
1081
1082 return 0;
1083}
1084
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001085/* Returns the size of the input on success, -1 on failure. This takes its
1086 own reference to `input`. */
1087static Py_ssize_t
1088_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1089{
1090 if (self->buffer.buf != NULL)
1091 PyBuffer_Release(&self->buffer);
1092 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1093 return -1;
1094 self->input_buffer = self->buffer.buf;
1095 self->input_len = self->buffer.len;
1096 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001097 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001098 return self->input_len;
1099}
1100
Antoine Pitrou04248a82010-10-12 20:51:21 +00001101static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001102bad_readline(void)
1103{
1104 PickleState *st = _Pickle_GetGlobalState();
1105 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1106 return -1;
1107}
1108
1109static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001110_Unpickler_SkipConsumed(UnpicklerObject *self)
1111{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001112 Py_ssize_t consumed;
1113 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001114
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001115 consumed = self->next_read_idx - self->prefetched_idx;
1116 if (consumed <= 0)
1117 return 0;
1118
1119 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001120 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001121 r = PyObject_CallFunction(self->read, "n", consumed);
1122 if (r == NULL)
1123 return -1;
1124 Py_DECREF(r);
1125
1126 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001127 return 0;
1128}
1129
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130static const Py_ssize_t READ_WHOLE_LINE = -1;
1131
1132/* If reading from a file, we need to only pull the bytes we need, since there
1133 may be multiple pickle objects arranged contiguously in the same input
1134 buffer.
1135
1136 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1137 bytes from the input stream/buffer.
1138
1139 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1140 failure; on success, returns the number of bytes read from the file.
1141
1142 On success, self->input_len will be 0; this is intentional so that when
1143 unpickling from a file, the "we've run out of data" code paths will trigger,
1144 causing the Unpickler to go back to the file for more data. Use the returned
1145 size to tell you how much data you can process. */
1146static Py_ssize_t
1147_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1148{
1149 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001150 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001151
1152 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001153
Antoine Pitrou04248a82010-10-12 20:51:21 +00001154 if (_Unpickler_SkipConsumed(self) < 0)
1155 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001156
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001157 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001158 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001159 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001160 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001161 PyObject *len;
1162 /* Prefetch some data without advancing the file pointer, if possible */
1163 if (self->peek && n < PREFETCH) {
1164 len = PyLong_FromSsize_t(PREFETCH);
1165 if (len == NULL)
1166 return -1;
1167 data = _Pickle_FastCall(self->peek, len);
1168 if (data == NULL) {
1169 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1170 return -1;
1171 /* peek() is probably not supported by the given file object */
1172 PyErr_Clear();
1173 Py_CLEAR(self->peek);
1174 }
1175 else {
1176 read_size = _Unpickler_SetStringInput(self, data);
1177 Py_DECREF(data);
1178 self->prefetched_idx = 0;
1179 if (n <= read_size)
1180 return n;
1181 }
1182 }
1183 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001184 if (len == NULL)
1185 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001186 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001187 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001188 if (data == NULL)
1189 return -1;
1190
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001191 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001192 Py_DECREF(data);
1193 return read_size;
1194}
1195
Victor Stinner19ed27e2016-05-20 11:42:37 +02001196/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001197static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001198_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001199{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001200 Py_ssize_t num_read;
1201
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001202 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001203 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1204 PickleState *st = _Pickle_GetGlobalState();
1205 PyErr_SetString(st->UnpicklingError,
1206 "read would overflow (invalid bytecode)");
1207 return -1;
1208 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001209
1210 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1211 assert(self->next_read_idx + n > self->input_len);
1212
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001213 if (!self->read)
1214 return bad_readline();
1215
Antoine Pitrou04248a82010-10-12 20:51:21 +00001216 num_read = _Unpickler_ReadFromFile(self, n);
1217 if (num_read < 0)
1218 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001219 if (num_read < n)
1220 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001221 *s = self->input_buffer;
1222 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001223 return n;
1224}
1225
Victor Stinner19ed27e2016-05-20 11:42:37 +02001226/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1227
1228 This should be used for all data reads, rather than accessing the unpickler's
1229 input buffer directly. This method deals correctly with reading from input
1230 streams, which the input buffer doesn't deal with.
1231
1232 Note that when reading from a file-like object, self->next_read_idx won't
1233 be updated (it should remain at 0 for the entire unpickling process). You
1234 should use this function's return value to know how many bytes you can
1235 consume.
1236
1237 Returns -1 (with an exception set) on failure. On success, return the
1238 number of chars read. */
1239#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001240 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001241 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1242 (self)->next_read_idx += (n), \
1243 (n)) \
1244 : _Unpickler_ReadImpl(self, (s), (n)))
1245
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001246static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001247_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1248 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001249{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001250 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001251 if (input_line == NULL) {
1252 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001253 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001254 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001255
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001256 memcpy(input_line, line, len);
1257 input_line[len] = '\0';
1258 self->input_line = input_line;
1259 *result = self->input_line;
1260 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001261}
1262
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001263/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001264 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001265
1266 Returns the number of chars read, or -1 on failure. */
1267static Py_ssize_t
1268_Unpickler_Readline(UnpicklerObject *self, char **result)
1269{
1270 Py_ssize_t i, num_read;
1271
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001272 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001273 if (self->input_buffer[i] == '\n') {
1274 char *line_start = self->input_buffer + self->next_read_idx;
1275 num_read = i - self->next_read_idx + 1;
1276 self->next_read_idx = i + 1;
1277 return _Unpickler_CopyLine(self, line_start, num_read, result);
1278 }
1279 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001280 if (!self->read)
1281 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001282
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001283 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1284 if (num_read < 0)
1285 return -1;
1286 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1287 return bad_readline();
1288 self->next_read_idx = num_read;
1289 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001290}
1291
1292/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1293 will be modified in place. */
1294static int
1295_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1296{
1297 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001298
1299 assert(new_size > self->memo_size);
1300
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001301 PyMem_RESIZE(self->memo, PyObject *, new_size);
1302 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001303 PyErr_NoMemory();
1304 return -1;
1305 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001306 for (i = self->memo_size; i < new_size; i++)
1307 self->memo[i] = NULL;
1308 self->memo_size = new_size;
1309 return 0;
1310}
1311
1312/* Returns NULL if idx is out of bounds. */
1313static PyObject *
1314_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1315{
1316 if (idx < 0 || idx >= self->memo_size)
1317 return NULL;
1318
1319 return self->memo[idx];
1320}
1321
1322/* Returns -1 (with an exception set) on failure, 0 on success.
1323 This takes its own reference to `value`. */
1324static int
1325_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1326{
1327 PyObject *old_item;
1328
1329 if (idx >= self->memo_size) {
1330 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1331 return -1;
1332 assert(idx < self->memo_size);
1333 }
1334 Py_INCREF(value);
1335 old_item = self->memo[idx];
1336 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001337 if (old_item != NULL) {
1338 Py_DECREF(old_item);
1339 }
1340 else {
1341 self->memo_len++;
1342 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001343 return 0;
1344}
1345
1346static PyObject **
1347_Unpickler_NewMemo(Py_ssize_t new_size)
1348{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001349 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001350 if (memo == NULL) {
1351 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001352 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001353 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001354 memset(memo, 0, new_size * sizeof(PyObject *));
1355 return memo;
1356}
1357
1358/* Free the unpickler's memo, taking care to decref any items left in it. */
1359static void
1360_Unpickler_MemoCleanup(UnpicklerObject *self)
1361{
1362 Py_ssize_t i;
1363 PyObject **memo = self->memo;
1364
1365 if (self->memo == NULL)
1366 return;
1367 self->memo = NULL;
1368 i = self->memo_size;
1369 while (--i >= 0) {
1370 Py_XDECREF(memo[i]);
1371 }
1372 PyMem_FREE(memo);
1373}
1374
1375static UnpicklerObject *
1376_Unpickler_New(void)
1377{
1378 UnpicklerObject *self;
1379
1380 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1381 if (self == NULL)
1382 return NULL;
1383
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001384 self->pers_func = NULL;
1385 self->input_buffer = NULL;
1386 self->input_line = NULL;
1387 self->input_len = 0;
1388 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001389 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001390 self->read = NULL;
1391 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001392 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001393 self->encoding = NULL;
1394 self->errors = NULL;
1395 self->marks = NULL;
1396 self->num_marks = 0;
1397 self->marks_size = 0;
1398 self->proto = 0;
1399 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001400 memset(&self->buffer, 0, sizeof(Py_buffer));
1401 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001402 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001403 self->memo = _Unpickler_NewMemo(self->memo_size);
1404 self->stack = (Pdata *)Pdata_New();
1405
1406 if (self->memo == NULL || self->stack == NULL) {
1407 Py_DECREF(self);
1408 return NULL;
1409 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001410
1411 return self;
1412}
1413
1414/* Returns -1 (with an exception set) on failure, 0 on success. This may
1415 be called once on a freshly created Pickler. */
1416static int
1417_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1418{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001419 _Py_IDENTIFIER(peek);
1420 _Py_IDENTIFIER(read);
1421 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001422
1423 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001424 if (self->peek == NULL) {
1425 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1426 PyErr_Clear();
1427 else
1428 return -1;
1429 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001430 self->read = _PyObject_GetAttrId(file, &PyId_read);
1431 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001432 if (self->readline == NULL || self->read == NULL) {
1433 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1434 PyErr_SetString(PyExc_TypeError,
1435 "file must have 'read' and 'readline' attributes");
1436 Py_CLEAR(self->read);
1437 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001438 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001439 return -1;
1440 }
1441 return 0;
1442}
1443
1444/* Returns -1 (with an exception set) on failure, 0 on success. This may
1445 be called once on a freshly created Pickler. */
1446static int
1447_Unpickler_SetInputEncoding(UnpicklerObject *self,
1448 const char *encoding,
1449 const char *errors)
1450{
1451 if (encoding == NULL)
1452 encoding = "ASCII";
1453 if (errors == NULL)
1454 errors = "strict";
1455
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001456 self->encoding = _PyMem_Strdup(encoding);
1457 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001458 if (self->encoding == NULL || self->errors == NULL) {
1459 PyErr_NoMemory();
1460 return -1;
1461 }
1462 return 0;
1463}
1464
1465/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001466static int
1467memo_get(PicklerObject *self, PyObject *key)
1468{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001469 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001470 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001471 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001472
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001473 value = PyMemoTable_Get(self->memo, key);
1474 if (value == NULL) {
1475 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001476 return -1;
1477 }
1478
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001479 if (!self->bin) {
1480 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001481 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1482 "%" PY_FORMAT_SIZE_T "d\n", *value);
1483 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001484 }
1485 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001486 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001487 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001488 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001489 len = 2;
1490 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001491 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001492 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001493 pdata[1] = (unsigned char)(*value & 0xff);
1494 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1495 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1496 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001497 len = 5;
1498 }
1499 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001500 PickleState *st = _Pickle_GetGlobalState();
1501 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001502 "memo id too large for LONG_BINGET");
1503 return -1;
1504 }
1505 }
1506
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001507 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001508 return -1;
1509
1510 return 0;
1511}
1512
1513/* Store an object in the memo, assign it a new unique ID based on the number
1514 of objects currently stored in the memo and generate a PUT opcode. */
1515static int
1516memo_put(PicklerObject *self, PyObject *obj)
1517{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001518 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001519 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001520 Py_ssize_t idx;
1521
1522 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001523
1524 if (self->fast)
1525 return 0;
1526
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001527 idx = PyMemoTable_Size(self->memo);
1528 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1529 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001530
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001531 if (self->proto >= 4) {
1532 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1533 return -1;
1534 return 0;
1535 }
1536 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001537 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001538 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001539 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001540 len = strlen(pdata);
1541 }
1542 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001543 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001544 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001545 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001546 len = 2;
1547 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001548 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001549 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001550 pdata[1] = (unsigned char)(idx & 0xff);
1551 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1552 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1553 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001554 len = 5;
1555 }
1556 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001557 PickleState *st = _Pickle_GetGlobalState();
1558 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001559 "memo id too large for LONG_BINPUT");
1560 return -1;
1561 }
1562 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001563 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001564 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001565
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001566 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001567}
1568
1569static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001570get_dotted_path(PyObject *obj, PyObject *name)
1571{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001572 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001573 PyObject *dotted_path;
1574 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001575
1576 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001577 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001578 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001579 n = PyList_GET_SIZE(dotted_path);
1580 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001581 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001582 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001583 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001584 if (obj == NULL)
1585 PyErr_Format(PyExc_AttributeError,
1586 "Can't pickle local object %R", name);
1587 else
1588 PyErr_Format(PyExc_AttributeError,
1589 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001590 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001591 return NULL;
1592 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001593 }
1594 return dotted_path;
1595}
1596
1597static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001598get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001599{
1600 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001601 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001602
1603 assert(PyList_CheckExact(names));
1604 Py_INCREF(obj);
1605 n = PyList_GET_SIZE(names);
1606 for (i = 0; i < n; i++) {
1607 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001608 Py_XDECREF(parent);
1609 parent = obj;
1610 obj = PyObject_GetAttr(parent, name);
1611 if (obj == NULL) {
1612 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001613 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001614 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001615 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001616 if (pparent != NULL)
1617 *pparent = parent;
1618 else
1619 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001620 return obj;
1621}
1622
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001623static void
1624reformat_attribute_error(PyObject *obj, PyObject *name)
1625{
1626 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1627 PyErr_Clear();
1628 PyErr_Format(PyExc_AttributeError,
1629 "Can't get attribute %R on %R", name, obj);
1630 }
1631}
1632
1633
1634static PyObject *
1635getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1636{
1637 PyObject *dotted_path, *attr;
1638
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001639 if (allow_qualname) {
1640 dotted_path = get_dotted_path(obj, name);
1641 if (dotted_path == NULL)
1642 return NULL;
1643 attr = get_deep_attribute(obj, dotted_path, NULL);
1644 Py_DECREF(dotted_path);
1645 }
1646 else
1647 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001648 if (attr == NULL)
1649 reformat_attribute_error(obj, name);
1650 return attr;
1651}
1652
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001653static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001654whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001655{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001656 PyObject *module_name;
1657 PyObject *modules_dict;
1658 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001659 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001660 _Py_IDENTIFIER(__module__);
1661 _Py_IDENTIFIER(modules);
1662 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001663
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001664 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1665
1666 if (module_name == NULL) {
1667 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001668 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001669 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001670 }
1671 else {
1672 /* In some rare cases (e.g., bound methods of extension types),
1673 __module__ can be None. If it is so, then search sys.modules for
1674 the module of global. */
1675 if (module_name != Py_None)
1676 return module_name;
1677 Py_CLEAR(module_name);
1678 }
1679 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001680
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001681 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001682 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001683 if (modules_dict == NULL) {
1684 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001685 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001686 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001687
1688 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001689 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1690 PyObject *candidate;
1691 if (PyUnicode_Check(module_name) &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001692 _PyUnicode_EqualToASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001693 continue;
1694 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 continue;
1696
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001697 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001698 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001699 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001700 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001701 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001702 continue;
1703 }
1704
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001705 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001706 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001707 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001708 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001709 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001710 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001711 }
1712
1713 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001714 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001715 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001716 return module_name;
1717}
1718
1719/* fast_save_enter() and fast_save_leave() are guards against recursive
1720 objects when Pickler is used with the "fast mode" (i.e., with object
1721 memoization disabled). If the nesting of a list or dict object exceed
1722 FAST_NESTING_LIMIT, these guards will start keeping an internal
1723 reference to the seen list or dict objects and check whether these objects
1724 are recursive. These are not strictly necessary, since save() has a
1725 hard-coded recursion limit, but they give a nicer error message than the
1726 typical RuntimeError. */
1727static int
1728fast_save_enter(PicklerObject *self, PyObject *obj)
1729{
1730 /* if fast_nesting < 0, we're doing an error exit. */
1731 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1732 PyObject *key = NULL;
1733 if (self->fast_memo == NULL) {
1734 self->fast_memo = PyDict_New();
1735 if (self->fast_memo == NULL) {
1736 self->fast_nesting = -1;
1737 return 0;
1738 }
1739 }
1740 key = PyLong_FromVoidPtr(obj);
1741 if (key == NULL)
1742 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001743 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001744 Py_DECREF(key);
1745 PyErr_Format(PyExc_ValueError,
1746 "fast mode: can't pickle cyclic objects "
1747 "including object type %.200s at %p",
1748 obj->ob_type->tp_name, obj);
1749 self->fast_nesting = -1;
1750 return 0;
1751 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001752 if (PyErr_Occurred()) {
1753 return 0;
1754 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001755 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1756 Py_DECREF(key);
1757 self->fast_nesting = -1;
1758 return 0;
1759 }
1760 Py_DECREF(key);
1761 }
1762 return 1;
1763}
1764
1765static int
1766fast_save_leave(PicklerObject *self, PyObject *obj)
1767{
1768 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1769 PyObject *key = PyLong_FromVoidPtr(obj);
1770 if (key == NULL)
1771 return 0;
1772 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1773 Py_DECREF(key);
1774 return 0;
1775 }
1776 Py_DECREF(key);
1777 }
1778 return 1;
1779}
1780
1781static int
1782save_none(PicklerObject *self, PyObject *obj)
1783{
1784 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001785 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001786 return -1;
1787
1788 return 0;
1789}
1790
1791static int
1792save_bool(PicklerObject *self, PyObject *obj)
1793{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001794 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001795 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001796 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001797 return -1;
1798 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001799 else {
1800 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1801 * so that unpicklers written before bools were introduced unpickle them
1802 * as ints, but unpicklers after can recognize that bools were intended.
1803 * Note that protocol 2 added direct ways to pickle bools.
1804 */
1805 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1806 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1807 return -1;
1808 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001809 return 0;
1810}
1811
1812static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001813save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001814{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001815 PyObject *repr = NULL;
1816 Py_ssize_t size;
1817 long val;
1818 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001819
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001820 const char long_op = LONG;
1821
1822 val= PyLong_AsLong(obj);
1823 if (val == -1 && PyErr_Occurred()) {
1824 /* out of range for int pickling */
1825 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001826 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001827 else if (self->bin &&
1828 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001829 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001830 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001831
1832 Note: we can't use -0x80000000L in the above condition because some
1833 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1834 before applying the unary minus when sizeof(long) <= 4. The
1835 resulting value stays unsigned which is commonly not what we want,
1836 so MSVC happily warns us about it. However, that result would have
1837 been fine because we guard for sizeof(long) <= 4 which turns the
1838 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001839 char pdata[32];
1840 Py_ssize_t len = 0;
1841
1842 pdata[1] = (unsigned char)(val & 0xff);
1843 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1844 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1845 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001846
1847 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1848 if (pdata[2] == 0) {
1849 pdata[0] = BININT1;
1850 len = 2;
1851 }
1852 else {
1853 pdata[0] = BININT2;
1854 len = 3;
1855 }
1856 }
1857 else {
1858 pdata[0] = BININT;
1859 len = 5;
1860 }
1861
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001862 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001863 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001864
1865 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001866 }
1867
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001868 if (self->proto >= 2) {
1869 /* Linear-time pickling. */
1870 size_t nbits;
1871 size_t nbytes;
1872 unsigned char *pdata;
1873 char header[5];
1874 int i;
1875 int sign = _PyLong_Sign(obj);
1876
1877 if (sign == 0) {
1878 header[0] = LONG1;
1879 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001880 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001881 goto error;
1882 return 0;
1883 }
1884 nbits = _PyLong_NumBits(obj);
1885 if (nbits == (size_t)-1 && PyErr_Occurred())
1886 goto error;
1887 /* How many bytes do we need? There are nbits >> 3 full
1888 * bytes of data, and nbits & 7 leftover bits. If there
1889 * are any leftover bits, then we clearly need another
1890 * byte. Wnat's not so obvious is that we *probably*
1891 * need another byte even if there aren't any leftovers:
1892 * the most-significant bit of the most-significant byte
1893 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001894 * opposite of the one we need. The exception is ints
1895 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001896 * its own 256's-complement, so has the right sign bit
1897 * even without the extra byte. That's a pain to check
1898 * for in advance, though, so we always grab an extra
1899 * byte at the start, and cut it back later if possible.
1900 */
1901 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001902 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001903 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001904 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001905 goto error;
1906 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001907 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001908 if (repr == NULL)
1909 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001910 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001911 i = _PyLong_AsByteArray((PyLongObject *)obj,
1912 pdata, nbytes,
1913 1 /* little endian */ , 1 /* signed */ );
1914 if (i < 0)
1915 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001916 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001917 * needed. This is so iff the MSB is all redundant sign
1918 * bits.
1919 */
1920 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001921 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001922 pdata[nbytes - 1] == 0xff &&
1923 (pdata[nbytes - 2] & 0x80) != 0) {
1924 nbytes--;
1925 }
1926
1927 if (nbytes < 256) {
1928 header[0] = LONG1;
1929 header[1] = (unsigned char)nbytes;
1930 size = 2;
1931 }
1932 else {
1933 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001934 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001935 for (i = 1; i < 5; i++) {
1936 header[i] = (unsigned char)(size & 0xff);
1937 size >>= 8;
1938 }
1939 size = 5;
1940 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001941 if (_Pickler_Write(self, header, size) < 0 ||
1942 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001943 goto error;
1944 }
1945 else {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001946 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001947
Mark Dickinson8dd05142009-01-20 20:43:58 +00001948 /* proto < 2: write the repr and newline. This is quadratic-time (in
1949 the number of digits), in both directions. We add a trailing 'L'
1950 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001951
1952 repr = PyObject_Repr(obj);
1953 if (repr == NULL)
1954 goto error;
1955
Serhiy Storchaka06515832016-11-20 09:13:07 +02001956 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001957 if (string == NULL)
1958 goto error;
1959
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001960 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1961 _Pickler_Write(self, string, size) < 0 ||
1962 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001963 goto error;
1964 }
1965
1966 if (0) {
1967 error:
1968 status = -1;
1969 }
1970 Py_XDECREF(repr);
1971
1972 return status;
1973}
1974
1975static int
1976save_float(PicklerObject *self, PyObject *obj)
1977{
1978 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1979
1980 if (self->bin) {
1981 char pdata[9];
1982 pdata[0] = BINFLOAT;
1983 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1984 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001985 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001986 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001987 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001988 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001989 int result = -1;
1990 char *buf = NULL;
1991 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001992
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001993 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001994 goto done;
1995
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001996 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001997 if (!buf) {
1998 PyErr_NoMemory();
1999 goto done;
2000 }
2001
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002002 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002003 goto done;
2004
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002005 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002006 goto done;
2007
2008 result = 0;
2009done:
2010 PyMem_Free(buf);
2011 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002012 }
2013
2014 return 0;
2015}
2016
2017static int
2018save_bytes(PicklerObject *self, PyObject *obj)
2019{
2020 if (self->proto < 3) {
2021 /* Older pickle protocols do not have an opcode for pickling bytes
2022 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002023 the __reduce__ method) to permit bytes object unpickling.
2024
2025 Here we use a hack to be compatible with Python 2. Since in Python
2026 2 'bytes' is just an alias for 'str' (which has different
2027 parameters than the actual bytes object), we use codecs.encode
2028 to create the appropriate 'str' object when unpickled using
2029 Python 2 *and* the appropriate 'bytes' object when unpickled
2030 using Python 3. Again this is a hack and we don't need to do this
2031 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002033 int status;
2034
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002035 if (PyBytes_GET_SIZE(obj) == 0) {
2036 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2037 }
2038 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002039 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002040 PyObject *unicode_str =
2041 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2042 PyBytes_GET_SIZE(obj),
2043 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002044 _Py_IDENTIFIER(latin1);
2045
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002046 if (unicode_str == NULL)
2047 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002048 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002049 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002050 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002051 Py_DECREF(unicode_str);
2052 }
2053
2054 if (reduce_value == NULL)
2055 return -1;
2056
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002057 /* save_reduce() will memoize the object automatically. */
2058 status = save_reduce(self, reduce_value, obj);
2059 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002060 return status;
2061 }
2062 else {
2063 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002064 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002065 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002066
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002067 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002068 if (size < 0)
2069 return -1;
2070
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002071 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002072 header[0] = SHORT_BINBYTES;
2073 header[1] = (unsigned char)size;
2074 len = 2;
2075 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002076 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002077 header[0] = BINBYTES;
2078 header[1] = (unsigned char)(size & 0xff);
2079 header[2] = (unsigned char)((size >> 8) & 0xff);
2080 header[3] = (unsigned char)((size >> 16) & 0xff);
2081 header[4] = (unsigned char)((size >> 24) & 0xff);
2082 len = 5;
2083 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002084 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002085 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002086 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002087 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002088 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002089 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002090 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002091 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002092 return -1; /* string too large */
2093 }
2094
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002095 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 return -1;
2097
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002098 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 return -1;
2100
2101 if (memo_put(self, obj) < 0)
2102 return -1;
2103
2104 return 0;
2105 }
2106}
2107
2108/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2109 backslash and newline characters to \uXXXX escapes. */
2110static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002111raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002112{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002113 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002114 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002115 void *data;
2116 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002117 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002118
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002119 if (PyUnicode_READY(obj))
2120 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002121
Victor Stinner358af132015-10-12 22:36:57 +02002122 _PyBytesWriter_Init(&writer);
2123
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002124 size = PyUnicode_GET_LENGTH(obj);
2125 data = PyUnicode_DATA(obj);
2126 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002127
Victor Stinner358af132015-10-12 22:36:57 +02002128 p = _PyBytesWriter_Alloc(&writer, size);
2129 if (p == NULL)
2130 goto error;
2131 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002132
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002133 for (i=0; i < size; i++) {
2134 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002135 /* Map 32-bit characters to '\Uxxxxxxxx' */
2136 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002137 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002138 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2139 if (p == NULL)
2140 goto error;
2141
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002142 *p++ = '\\';
2143 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002144 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2145 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2146 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2147 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2148 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2149 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2150 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2151 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002152 }
Victor Stinner358af132015-10-12 22:36:57 +02002153 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002154 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002155 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002156 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2157 if (p == NULL)
2158 goto error;
2159
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002160 *p++ = '\\';
2161 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002162 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2163 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2164 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2165 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002166 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002167 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002168 else
2169 *p++ = (char) ch;
2170 }
Victor Stinner358af132015-10-12 22:36:57 +02002171
2172 return _PyBytesWriter_Finish(&writer, p);
2173
2174error:
2175 _PyBytesWriter_Dealloc(&writer);
2176 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002177}
2178
2179static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002180write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002181{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002182 char header[9];
2183 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002184
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002185 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002186 if (size <= 0xff && self->proto >= 4) {
2187 header[0] = SHORT_BINUNICODE;
2188 header[1] = (unsigned char)(size & 0xff);
2189 len = 2;
2190 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002191 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002192 header[0] = BINUNICODE;
2193 header[1] = (unsigned char)(size & 0xff);
2194 header[2] = (unsigned char)((size >> 8) & 0xff);
2195 header[3] = (unsigned char)((size >> 16) & 0xff);
2196 header[4] = (unsigned char)((size >> 24) & 0xff);
2197 len = 5;
2198 }
2199 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002200 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002201 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002202 len = 9;
2203 }
2204 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002205 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002206 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002207 return -1;
2208 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002209
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002210 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002211 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002212 if (_Pickler_Write(self, data, size) < 0)
2213 return -1;
2214
2215 return 0;
2216}
2217
2218static int
2219write_unicode_binary(PicklerObject *self, PyObject *obj)
2220{
2221 PyObject *encoded = NULL;
2222 Py_ssize_t size;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002223 const char *data;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002224 int r;
2225
2226 if (PyUnicode_READY(obj))
2227 return -1;
2228
2229 data = PyUnicode_AsUTF8AndSize(obj, &size);
2230 if (data != NULL)
2231 return write_utf8(self, data, size);
2232
2233 /* Issue #8383: for strings with lone surrogates, fallback on the
2234 "surrogatepass" error handler. */
2235 PyErr_Clear();
2236 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2237 if (encoded == NULL)
2238 return -1;
2239
2240 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2241 PyBytes_GET_SIZE(encoded));
2242 Py_DECREF(encoded);
2243 return r;
2244}
2245
2246static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002247save_unicode(PicklerObject *self, PyObject *obj)
2248{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002249 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002250 if (write_unicode_binary(self, obj) < 0)
2251 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 }
2253 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002254 PyObject *encoded;
2255 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256 const char unicode_op = UNICODE;
2257
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002258 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002260 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002261
Antoine Pitrou299978d2013-04-07 17:38:11 +02002262 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2263 Py_DECREF(encoded);
2264 return -1;
2265 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002266
2267 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002268 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2269 Py_DECREF(encoded);
2270 return -1;
2271 }
2272 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002273
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002274 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002275 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 }
2277 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002278 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002280 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002281}
2282
2283/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2284static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002285store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002286{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002287 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002288
2289 assert(PyTuple_Size(t) == len);
2290
2291 for (i = 0; i < len; i++) {
2292 PyObject *element = PyTuple_GET_ITEM(t, i);
2293
2294 if (element == NULL)
2295 return -1;
2296 if (save(self, element, 0) < 0)
2297 return -1;
2298 }
2299
2300 return 0;
2301}
2302
2303/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2304 * used across protocols to minimize the space needed to pickle them.
2305 * Tuples are also the only builtin immutable type that can be recursive
2306 * (a tuple can be reached from itself), and that requires some subtle
2307 * magic so that it works in all cases. IOW, this is a long routine.
2308 */
2309static int
2310save_tuple(PicklerObject *self, PyObject *obj)
2311{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002312 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002313
2314 const char mark_op = MARK;
2315 const char tuple_op = TUPLE;
2316 const char pop_op = POP;
2317 const char pop_mark_op = POP_MARK;
2318 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2319
2320 if ((len = PyTuple_Size(obj)) < 0)
2321 return -1;
2322
2323 if (len == 0) {
2324 char pdata[2];
2325
2326 if (self->proto) {
2327 pdata[0] = EMPTY_TUPLE;
2328 len = 1;
2329 }
2330 else {
2331 pdata[0] = MARK;
2332 pdata[1] = TUPLE;
2333 len = 2;
2334 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002335 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002336 return -1;
2337 return 0;
2338 }
2339
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002340 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002341 * saving the tuple elements, the tuple must be recursive, in
2342 * which case we'll pop everything we put on the stack, and fetch
2343 * its value from the memo.
2344 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002345 if (len <= 3 && self->proto >= 2) {
2346 /* Use TUPLE{1,2,3} opcodes. */
2347 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002348 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002349
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002350 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351 /* pop the len elements */
2352 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002353 if (_Pickler_Write(self, &pop_op, 1) < 0)
2354 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002355 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002356 if (memo_get(self, obj) < 0)
2357 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002359 return 0;
2360 }
2361 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002362 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2363 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002364 }
2365 goto memoize;
2366 }
2367
2368 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2369 * Generate MARK e1 e2 ... TUPLE
2370 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002371 if (_Pickler_Write(self, &mark_op, 1) < 0)
2372 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002373
2374 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002375 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002376
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002377 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002378 /* pop the stack stuff we pushed */
2379 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002380 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2381 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002382 }
2383 else {
2384 /* Note that we pop one more than len, to remove
2385 * the MARK too.
2386 */
2387 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002388 if (_Pickler_Write(self, &pop_op, 1) < 0)
2389 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002390 }
2391 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002392 if (memo_get(self, obj) < 0)
2393 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002394
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002395 return 0;
2396 }
2397 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002398 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2399 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002400 }
2401
2402 memoize:
2403 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002404 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002405
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002406 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002407}
2408
2409/* iter is an iterator giving items, and we batch up chunks of
2410 * MARK item item ... item APPENDS
2411 * opcode sequences. Calling code should have arranged to first create an
2412 * empty list, or list-like object, for the APPENDS to operate on.
2413 * Returns 0 on success, <0 on error.
2414 */
2415static int
2416batch_list(PicklerObject *self, PyObject *iter)
2417{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002418 PyObject *obj = NULL;
2419 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002420 int i, n;
2421
2422 const char mark_op = MARK;
2423 const char append_op = APPEND;
2424 const char appends_op = APPENDS;
2425
2426 assert(iter != NULL);
2427
2428 /* XXX: I think this function could be made faster by avoiding the
2429 iterator interface and fetching objects directly from list using
2430 PyList_GET_ITEM.
2431 */
2432
2433 if (self->proto == 0) {
2434 /* APPENDS isn't available; do one at a time. */
2435 for (;;) {
2436 obj = PyIter_Next(iter);
2437 if (obj == NULL) {
2438 if (PyErr_Occurred())
2439 return -1;
2440 break;
2441 }
2442 i = save(self, obj, 0);
2443 Py_DECREF(obj);
2444 if (i < 0)
2445 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002446 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002447 return -1;
2448 }
2449 return 0;
2450 }
2451
2452 /* proto > 0: write in batches of BATCHSIZE. */
2453 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002454 /* Get first item */
2455 firstitem = PyIter_Next(iter);
2456 if (firstitem == NULL) {
2457 if (PyErr_Occurred())
2458 goto error;
2459
2460 /* nothing more to add */
2461 break;
2462 }
2463
2464 /* Try to get a second item */
2465 obj = PyIter_Next(iter);
2466 if (obj == NULL) {
2467 if (PyErr_Occurred())
2468 goto error;
2469
2470 /* Only one item to write */
2471 if (save(self, firstitem, 0) < 0)
2472 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002473 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002474 goto error;
2475 Py_CLEAR(firstitem);
2476 break;
2477 }
2478
2479 /* More than one item to write */
2480
2481 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002482 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002483 goto error;
2484
2485 if (save(self, firstitem, 0) < 0)
2486 goto error;
2487 Py_CLEAR(firstitem);
2488 n = 1;
2489
2490 /* Fetch and save up to BATCHSIZE items */
2491 while (obj) {
2492 if (save(self, obj, 0) < 0)
2493 goto error;
2494 Py_CLEAR(obj);
2495 n += 1;
2496
2497 if (n == BATCHSIZE)
2498 break;
2499
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002500 obj = PyIter_Next(iter);
2501 if (obj == NULL) {
2502 if (PyErr_Occurred())
2503 goto error;
2504 break;
2505 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002506 }
2507
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002508 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002509 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002510
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002511 } while (n == BATCHSIZE);
2512 return 0;
2513
2514 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002515 Py_XDECREF(firstitem);
2516 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517 return -1;
2518}
2519
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002520/* This is a variant of batch_list() above, specialized for lists (with no
2521 * support for list subclasses). Like batch_list(), we batch up chunks of
2522 * MARK item item ... item APPENDS
2523 * opcode sequences. Calling code should have arranged to first create an
2524 * empty list, or list-like object, for the APPENDS to operate on.
2525 * Returns 0 on success, -1 on error.
2526 *
2527 * This version is considerably faster than batch_list(), if less general.
2528 *
2529 * Note that this only works for protocols > 0.
2530 */
2531static int
2532batch_list_exact(PicklerObject *self, PyObject *obj)
2533{
2534 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002535 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002536
2537 const char append_op = APPEND;
2538 const char appends_op = APPENDS;
2539 const char mark_op = MARK;
2540
2541 assert(obj != NULL);
2542 assert(self->proto > 0);
2543 assert(PyList_CheckExact(obj));
2544
2545 if (PyList_GET_SIZE(obj) == 1) {
2546 item = PyList_GET_ITEM(obj, 0);
2547 if (save(self, item, 0) < 0)
2548 return -1;
2549 if (_Pickler_Write(self, &append_op, 1) < 0)
2550 return -1;
2551 return 0;
2552 }
2553
2554 /* Write in batches of BATCHSIZE. */
2555 total = 0;
2556 do {
2557 this_batch = 0;
2558 if (_Pickler_Write(self, &mark_op, 1) < 0)
2559 return -1;
2560 while (total < PyList_GET_SIZE(obj)) {
2561 item = PyList_GET_ITEM(obj, total);
2562 if (save(self, item, 0) < 0)
2563 return -1;
2564 total++;
2565 if (++this_batch == BATCHSIZE)
2566 break;
2567 }
2568 if (_Pickler_Write(self, &appends_op, 1) < 0)
2569 return -1;
2570
2571 } while (total < PyList_GET_SIZE(obj));
2572
2573 return 0;
2574}
2575
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002576static int
2577save_list(PicklerObject *self, PyObject *obj)
2578{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002579 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002580 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002581 int status = 0;
2582
2583 if (self->fast && !fast_save_enter(self, obj))
2584 goto error;
2585
2586 /* Create an empty list. */
2587 if (self->bin) {
2588 header[0] = EMPTY_LIST;
2589 len = 1;
2590 }
2591 else {
2592 header[0] = MARK;
2593 header[1] = LIST;
2594 len = 2;
2595 }
2596
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002597 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002598 goto error;
2599
2600 /* Get list length, and bow out early if empty. */
2601 if ((len = PyList_Size(obj)) < 0)
2602 goto error;
2603
2604 if (memo_put(self, obj) < 0)
2605 goto error;
2606
2607 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002608 /* Materialize the list elements. */
2609 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002610 if (Py_EnterRecursiveCall(" while pickling an object"))
2611 goto error;
2612 status = batch_list_exact(self, obj);
2613 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002614 } else {
2615 PyObject *iter = PyObject_GetIter(obj);
2616 if (iter == NULL)
2617 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002618
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002619 if (Py_EnterRecursiveCall(" while pickling an object")) {
2620 Py_DECREF(iter);
2621 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002622 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002623 status = batch_list(self, iter);
2624 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002625 Py_DECREF(iter);
2626 }
2627 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002628 if (0) {
2629 error:
2630 status = -1;
2631 }
2632
2633 if (self->fast && !fast_save_leave(self, obj))
2634 status = -1;
2635
2636 return status;
2637}
2638
2639/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2640 * MARK key value ... key value SETITEMS
2641 * opcode sequences. Calling code should have arranged to first create an
2642 * empty dict, or dict-like object, for the SETITEMS to operate on.
2643 * Returns 0 on success, <0 on error.
2644 *
2645 * This is very much like batch_list(). The difference between saving
2646 * elements directly, and picking apart two-tuples, is so long-winded at
2647 * the C level, though, that attempts to combine these routines were too
2648 * ugly to bear.
2649 */
2650static int
2651batch_dict(PicklerObject *self, PyObject *iter)
2652{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002653 PyObject *obj = NULL;
2654 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002655 int i, n;
2656
2657 const char mark_op = MARK;
2658 const char setitem_op = SETITEM;
2659 const char setitems_op = SETITEMS;
2660
2661 assert(iter != NULL);
2662
2663 if (self->proto == 0) {
2664 /* SETITEMS isn't available; do one at a time. */
2665 for (;;) {
2666 obj = PyIter_Next(iter);
2667 if (obj == NULL) {
2668 if (PyErr_Occurred())
2669 return -1;
2670 break;
2671 }
2672 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2673 PyErr_SetString(PyExc_TypeError, "dict items "
2674 "iterator must return 2-tuples");
2675 return -1;
2676 }
2677 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2678 if (i >= 0)
2679 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2680 Py_DECREF(obj);
2681 if (i < 0)
2682 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002683 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002684 return -1;
2685 }
2686 return 0;
2687 }
2688
2689 /* proto > 0: write in batches of BATCHSIZE. */
2690 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002691 /* Get first item */
2692 firstitem = PyIter_Next(iter);
2693 if (firstitem == NULL) {
2694 if (PyErr_Occurred())
2695 goto error;
2696
2697 /* nothing more to add */
2698 break;
2699 }
2700 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2701 PyErr_SetString(PyExc_TypeError, "dict items "
2702 "iterator must return 2-tuples");
2703 goto error;
2704 }
2705
2706 /* Try to get a second item */
2707 obj = PyIter_Next(iter);
2708 if (obj == NULL) {
2709 if (PyErr_Occurred())
2710 goto error;
2711
2712 /* Only one item to write */
2713 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2714 goto error;
2715 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2716 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002717 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002718 goto error;
2719 Py_CLEAR(firstitem);
2720 break;
2721 }
2722
2723 /* More than one item to write */
2724
2725 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002726 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002727 goto error;
2728
2729 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2730 goto error;
2731 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2732 goto error;
2733 Py_CLEAR(firstitem);
2734 n = 1;
2735
2736 /* Fetch and save up to BATCHSIZE items */
2737 while (obj) {
2738 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2739 PyErr_SetString(PyExc_TypeError, "dict items "
2740 "iterator must return 2-tuples");
2741 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002742 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002743 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2744 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2745 goto error;
2746 Py_CLEAR(obj);
2747 n += 1;
2748
2749 if (n == BATCHSIZE)
2750 break;
2751
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002752 obj = PyIter_Next(iter);
2753 if (obj == NULL) {
2754 if (PyErr_Occurred())
2755 goto error;
2756 break;
2757 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002758 }
2759
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002760 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002761 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002762
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002763 } while (n == BATCHSIZE);
2764 return 0;
2765
2766 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002767 Py_XDECREF(firstitem);
2768 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002769 return -1;
2770}
2771
Collin Winter5c9b02d2009-05-25 05:43:30 +00002772/* This is a variant of batch_dict() above that specializes for dicts, with no
2773 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2774 * MARK key value ... key value SETITEMS
2775 * opcode sequences. Calling code should have arranged to first create an
2776 * empty dict, or dict-like object, for the SETITEMS to operate on.
2777 * Returns 0 on success, -1 on error.
2778 *
2779 * Note that this currently doesn't work for protocol 0.
2780 */
2781static int
2782batch_dict_exact(PicklerObject *self, PyObject *obj)
2783{
2784 PyObject *key = NULL, *value = NULL;
2785 int i;
2786 Py_ssize_t dict_size, ppos = 0;
2787
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002788 const char mark_op = MARK;
2789 const char setitem_op = SETITEM;
2790 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002791
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002792 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002793 assert(self->proto > 0);
2794
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002795 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002796
2797 /* Special-case len(d) == 1 to save space. */
2798 if (dict_size == 1) {
2799 PyDict_Next(obj, &ppos, &key, &value);
2800 if (save(self, key, 0) < 0)
2801 return -1;
2802 if (save(self, value, 0) < 0)
2803 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002804 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002805 return -1;
2806 return 0;
2807 }
2808
2809 /* Write in batches of BATCHSIZE. */
2810 do {
2811 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002812 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002813 return -1;
2814 while (PyDict_Next(obj, &ppos, &key, &value)) {
2815 if (save(self, key, 0) < 0)
2816 return -1;
2817 if (save(self, value, 0) < 0)
2818 return -1;
2819 if (++i == BATCHSIZE)
2820 break;
2821 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002822 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002823 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002824 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00002825 PyErr_Format(
2826 PyExc_RuntimeError,
2827 "dictionary changed size during iteration");
2828 return -1;
2829 }
2830
2831 } while (i == BATCHSIZE);
2832 return 0;
2833}
2834
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002835static int
2836save_dict(PicklerObject *self, PyObject *obj)
2837{
2838 PyObject *items, *iter;
2839 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002840 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002841 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002842 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002843
2844 if (self->fast && !fast_save_enter(self, obj))
2845 goto error;
2846
2847 /* Create an empty dict. */
2848 if (self->bin) {
2849 header[0] = EMPTY_DICT;
2850 len = 1;
2851 }
2852 else {
2853 header[0] = MARK;
2854 header[1] = DICT;
2855 len = 2;
2856 }
2857
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002858 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002859 goto error;
2860
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002861 if (memo_put(self, obj) < 0)
2862 goto error;
2863
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002864 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002865 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002866 if (PyDict_CheckExact(obj) && self->proto > 0) {
2867 /* We can take certain shortcuts if we know this is a dict and
2868 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002869 if (Py_EnterRecursiveCall(" while pickling an object"))
2870 goto error;
2871 status = batch_dict_exact(self, obj);
2872 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002873 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002874 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002875
Victor Stinnerad8c83a2016-09-05 17:53:15 -07002876 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002877 if (items == NULL)
2878 goto error;
2879 iter = PyObject_GetIter(items);
2880 Py_DECREF(items);
2881 if (iter == NULL)
2882 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002883 if (Py_EnterRecursiveCall(" while pickling an object")) {
2884 Py_DECREF(iter);
2885 goto error;
2886 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002887 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002888 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002889 Py_DECREF(iter);
2890 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002891 }
2892
2893 if (0) {
2894 error:
2895 status = -1;
2896 }
2897
2898 if (self->fast && !fast_save_leave(self, obj))
2899 status = -1;
2900
2901 return status;
2902}
2903
2904static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002905save_set(PicklerObject *self, PyObject *obj)
2906{
2907 PyObject *item;
2908 int i;
2909 Py_ssize_t set_size, ppos = 0;
2910 Py_hash_t hash;
2911
2912 const char empty_set_op = EMPTY_SET;
2913 const char mark_op = MARK;
2914 const char additems_op = ADDITEMS;
2915
2916 if (self->proto < 4) {
2917 PyObject *items;
2918 PyObject *reduce_value;
2919 int status;
2920
2921 items = PySequence_List(obj);
2922 if (items == NULL) {
2923 return -1;
2924 }
2925 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2926 Py_DECREF(items);
2927 if (reduce_value == NULL) {
2928 return -1;
2929 }
2930 /* save_reduce() will memoize the object automatically. */
2931 status = save_reduce(self, reduce_value, obj);
2932 Py_DECREF(reduce_value);
2933 return status;
2934 }
2935
2936 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2937 return -1;
2938
2939 if (memo_put(self, obj) < 0)
2940 return -1;
2941
2942 set_size = PySet_GET_SIZE(obj);
2943 if (set_size == 0)
2944 return 0; /* nothing to do */
2945
2946 /* Write in batches of BATCHSIZE. */
2947 do {
2948 i = 0;
2949 if (_Pickler_Write(self, &mark_op, 1) < 0)
2950 return -1;
2951 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2952 if (save(self, item, 0) < 0)
2953 return -1;
2954 if (++i == BATCHSIZE)
2955 break;
2956 }
2957 if (_Pickler_Write(self, &additems_op, 1) < 0)
2958 return -1;
2959 if (PySet_GET_SIZE(obj) != set_size) {
2960 PyErr_Format(
2961 PyExc_RuntimeError,
2962 "set changed size during iteration");
2963 return -1;
2964 }
2965 } while (i == BATCHSIZE);
2966
2967 return 0;
2968}
2969
2970static int
2971save_frozenset(PicklerObject *self, PyObject *obj)
2972{
2973 PyObject *iter;
2974
2975 const char mark_op = MARK;
2976 const char frozenset_op = FROZENSET;
2977
2978 if (self->fast && !fast_save_enter(self, obj))
2979 return -1;
2980
2981 if (self->proto < 4) {
2982 PyObject *items;
2983 PyObject *reduce_value;
2984 int status;
2985
2986 items = PySequence_List(obj);
2987 if (items == NULL) {
2988 return -1;
2989 }
2990 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2991 items);
2992 Py_DECREF(items);
2993 if (reduce_value == NULL) {
2994 return -1;
2995 }
2996 /* save_reduce() will memoize the object automatically. */
2997 status = save_reduce(self, reduce_value, obj);
2998 Py_DECREF(reduce_value);
2999 return status;
3000 }
3001
3002 if (_Pickler_Write(self, &mark_op, 1) < 0)
3003 return -1;
3004
3005 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003006 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003007 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003008 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003009 for (;;) {
3010 PyObject *item;
3011
3012 item = PyIter_Next(iter);
3013 if (item == NULL) {
3014 if (PyErr_Occurred()) {
3015 Py_DECREF(iter);
3016 return -1;
3017 }
3018 break;
3019 }
3020 if (save(self, item, 0) < 0) {
3021 Py_DECREF(item);
3022 Py_DECREF(iter);
3023 return -1;
3024 }
3025 Py_DECREF(item);
3026 }
3027 Py_DECREF(iter);
3028
3029 /* If the object is already in the memo, this means it is
3030 recursive. In this case, throw away everything we put on the
3031 stack, and fetch the object back from the memo. */
3032 if (PyMemoTable_Get(self->memo, obj)) {
3033 const char pop_mark_op = POP_MARK;
3034
3035 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3036 return -1;
3037 if (memo_get(self, obj) < 0)
3038 return -1;
3039 return 0;
3040 }
3041
3042 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3043 return -1;
3044 if (memo_put(self, obj) < 0)
3045 return -1;
3046
3047 return 0;
3048}
3049
3050static int
3051fix_imports(PyObject **module_name, PyObject **global_name)
3052{
3053 PyObject *key;
3054 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003055 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003056
3057 key = PyTuple_Pack(2, *module_name, *global_name);
3058 if (key == NULL)
3059 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003060 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003061 Py_DECREF(key);
3062 if (item) {
3063 PyObject *fixed_module_name;
3064 PyObject *fixed_global_name;
3065
3066 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3067 PyErr_Format(PyExc_RuntimeError,
3068 "_compat_pickle.REVERSE_NAME_MAPPING values "
3069 "should be 2-tuples, not %.200s",
3070 Py_TYPE(item)->tp_name);
3071 return -1;
3072 }
3073 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3074 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3075 if (!PyUnicode_Check(fixed_module_name) ||
3076 !PyUnicode_Check(fixed_global_name)) {
3077 PyErr_Format(PyExc_RuntimeError,
3078 "_compat_pickle.REVERSE_NAME_MAPPING values "
3079 "should be pairs of str, not (%.200s, %.200s)",
3080 Py_TYPE(fixed_module_name)->tp_name,
3081 Py_TYPE(fixed_global_name)->tp_name);
3082 return -1;
3083 }
3084
3085 Py_CLEAR(*module_name);
3086 Py_CLEAR(*global_name);
3087 Py_INCREF(fixed_module_name);
3088 Py_INCREF(fixed_global_name);
3089 *module_name = fixed_module_name;
3090 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003091 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003092 }
3093 else if (PyErr_Occurred()) {
3094 return -1;
3095 }
3096
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003097 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003098 if (item) {
3099 if (!PyUnicode_Check(item)) {
3100 PyErr_Format(PyExc_RuntimeError,
3101 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3102 "should be strings, not %.200s",
3103 Py_TYPE(item)->tp_name);
3104 return -1;
3105 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003106 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003107 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003108 }
3109 else if (PyErr_Occurred()) {
3110 return -1;
3111 }
3112
3113 return 0;
3114}
3115
3116static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003117save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3118{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003119 PyObject *global_name = NULL;
3120 PyObject *module_name = NULL;
3121 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003122 PyObject *parent = NULL;
3123 PyObject *dotted_path = NULL;
3124 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003125 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003126 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003127 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003128 _Py_IDENTIFIER(__name__);
3129 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003130
3131 const char global_op = GLOBAL;
3132
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003133 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003134 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003135 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003136 }
3137 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003138 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3139 if (global_name == NULL) {
3140 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3141 goto error;
3142 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003143 }
3144 if (global_name == NULL) {
3145 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3146 if (global_name == NULL)
3147 goto error;
3148 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003149 }
3150
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003151 dotted_path = get_dotted_path(module, global_name);
3152 if (dotted_path == NULL)
3153 goto error;
3154 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003155 if (module_name == NULL)
3156 goto error;
3157
3158 /* XXX: Change to use the import C API directly with level=0 to disallow
3159 relative imports.
3160
3161 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3162 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3163 custom import functions (IMHO, this would be a nice security
3164 feature). The import C API would need to be extended to support the
3165 extra parameters of __import__ to fix that. */
3166 module = PyImport_Import(module_name);
3167 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003168 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003169 "Can't pickle %R: import of module %R failed",
3170 obj, module_name);
3171 goto error;
3172 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003173 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3174 Py_INCREF(lastname);
3175 cls = get_deep_attribute(module, dotted_path, &parent);
3176 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003177 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003178 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003179 "Can't pickle %R: attribute lookup %S on %S failed",
3180 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003181 goto error;
3182 }
3183 if (cls != obj) {
3184 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003185 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003186 "Can't pickle %R: it's not the same object as %S.%S",
3187 obj, module_name, global_name);
3188 goto error;
3189 }
3190 Py_DECREF(cls);
3191
3192 if (self->proto >= 2) {
3193 /* See whether this is in the extension registry, and if
3194 * so generate an EXT opcode.
3195 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003196 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003197 PyObject *code_obj; /* extension code as Python object */
3198 long code; /* extension code as C value */
3199 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003200 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003201
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003202 extension_key = PyTuple_Pack(2, module_name, global_name);
3203 if (extension_key == NULL) {
3204 goto error;
3205 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003206 code_obj = PyDict_GetItemWithError(st->extension_registry,
3207 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003208 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003209 /* The object is not registered in the extension registry.
3210 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003211 if (code_obj == NULL) {
3212 if (PyErr_Occurred()) {
3213 goto error;
3214 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003215 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003216 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003217
3218 /* XXX: pickle.py doesn't check neither the type, nor the range
3219 of the value returned by the extension_registry. It should for
3220 consistency. */
3221
3222 /* Verify code_obj has the right type and value. */
3223 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003224 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003225 "Can't pickle %R: extension code %R isn't an integer",
3226 obj, code_obj);
3227 goto error;
3228 }
3229 code = PyLong_AS_LONG(code_obj);
3230 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003231 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003232 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3233 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003234 goto error;
3235 }
3236
3237 /* Generate an EXT opcode. */
3238 if (code <= 0xff) {
3239 pdata[0] = EXT1;
3240 pdata[1] = (unsigned char)code;
3241 n = 2;
3242 }
3243 else if (code <= 0xffff) {
3244 pdata[0] = EXT2;
3245 pdata[1] = (unsigned char)(code & 0xff);
3246 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3247 n = 3;
3248 }
3249 else {
3250 pdata[0] = EXT4;
3251 pdata[1] = (unsigned char)(code & 0xff);
3252 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3253 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3254 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3255 n = 5;
3256 }
3257
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003258 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003259 goto error;
3260 }
3261 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003262 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003263 if (parent == module) {
3264 Py_INCREF(lastname);
3265 Py_DECREF(global_name);
3266 global_name = lastname;
3267 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003268 if (self->proto >= 4) {
3269 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003270
Christian Heimese8b1ba12013-11-23 21:13:39 +01003271 if (save(self, module_name, 0) < 0)
3272 goto error;
3273 if (save(self, global_name, 0) < 0)
3274 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003275
3276 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3277 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003278 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003279 else if (parent != module) {
3280 PickleState *st = _Pickle_GetGlobalState();
3281 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3282 st->getattr, parent, lastname);
3283 status = save_reduce(self, reduce_value, NULL);
3284 Py_DECREF(reduce_value);
3285 if (status < 0)
3286 goto error;
3287 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003288 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003289 /* Generate a normal global opcode if we are using a pickle
3290 protocol < 4, or if the object is not registered in the
3291 extension registry. */
3292 PyObject *encoded;
3293 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003294
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003295 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003296 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003297
3298 /* For protocol < 3 and if the user didn't request against doing
3299 so, we convert module names to the old 2.x module names. */
3300 if (self->proto < 3 && self->fix_imports) {
3301 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003302 goto error;
3303 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003304 }
3305
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003306 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3307 both the module name and the global name using UTF-8. We do so
3308 only when we are using the pickle protocol newer than version
3309 3. This is to ensure compatibility with older Unpickler running
3310 on Python 2.x. */
3311 if (self->proto == 3) {
3312 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003313 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003314 else {
3315 unicode_encoder = PyUnicode_AsASCIIString;
3316 }
3317 encoded = unicode_encoder(module_name);
3318 if (encoded == NULL) {
3319 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003320 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003321 "can't pickle module identifier '%S' using "
3322 "pickle protocol %i",
3323 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003324 goto error;
3325 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003326 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3327 PyBytes_GET_SIZE(encoded)) < 0) {
3328 Py_DECREF(encoded);
3329 goto error;
3330 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003331 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003332 if(_Pickler_Write(self, "\n", 1) < 0)
3333 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003334
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003335 /* Save the name of the module. */
3336 encoded = unicode_encoder(global_name);
3337 if (encoded == NULL) {
3338 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003339 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003340 "can't pickle global identifier '%S' using "
3341 "pickle protocol %i",
3342 global_name, self->proto);
3343 goto error;
3344 }
3345 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3346 PyBytes_GET_SIZE(encoded)) < 0) {
3347 Py_DECREF(encoded);
3348 goto error;
3349 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003350 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003351 if (_Pickler_Write(self, "\n", 1) < 0)
3352 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003353 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003354 /* Memoize the object. */
3355 if (memo_put(self, obj) < 0)
3356 goto error;
3357 }
3358
3359 if (0) {
3360 error:
3361 status = -1;
3362 }
3363 Py_XDECREF(module_name);
3364 Py_XDECREF(global_name);
3365 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003366 Py_XDECREF(parent);
3367 Py_XDECREF(dotted_path);
3368 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003369
3370 return status;
3371}
3372
3373static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003374save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3375{
3376 PyObject *reduce_value;
3377 int status;
3378
3379 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3380 if (reduce_value == NULL) {
3381 return -1;
3382 }
3383 status = save_reduce(self, reduce_value, obj);
3384 Py_DECREF(reduce_value);
3385 return status;
3386}
3387
3388static int
3389save_type(PicklerObject *self, PyObject *obj)
3390{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003391 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003392 return save_singleton_type(self, obj, Py_None);
3393 }
3394 else if (obj == (PyObject *)&PyEllipsis_Type) {
3395 return save_singleton_type(self, obj, Py_Ellipsis);
3396 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003397 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003398 return save_singleton_type(self, obj, Py_NotImplemented);
3399 }
3400 return save_global(self, obj, NULL);
3401}
3402
3403static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003404save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3405{
3406 PyObject *pid = NULL;
3407 int status = 0;
3408
3409 const char persid_op = PERSID;
3410 const char binpersid_op = BINPERSID;
3411
3412 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003413 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003414 if (pid == NULL)
3415 return -1;
3416
3417 if (pid != Py_None) {
3418 if (self->bin) {
3419 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003420 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003421 goto error;
3422 }
3423 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003424 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003425
3426 pid_str = PyObject_Str(pid);
3427 if (pid_str == NULL)
3428 goto error;
3429
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003430 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003431 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003432 if (!PyUnicode_IS_ASCII(pid_str)) {
3433 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3434 "persistent IDs in protocol 0 must be "
3435 "ASCII strings");
3436 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003437 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003438 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003439
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003440 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003441 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3442 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3443 _Pickler_Write(self, "\n", 1) < 0) {
3444 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003445 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003446 }
3447 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003448 }
3449 status = 1;
3450 }
3451
3452 if (0) {
3453 error:
3454 status = -1;
3455 }
3456 Py_XDECREF(pid);
3457
3458 return status;
3459}
3460
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003461static PyObject *
3462get_class(PyObject *obj)
3463{
3464 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003465 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003466
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003467 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003468 if (cls == NULL) {
3469 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3470 PyErr_Clear();
3471 cls = (PyObject *) Py_TYPE(obj);
3472 Py_INCREF(cls);
3473 }
3474 }
3475 return cls;
3476}
3477
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003478/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3479 * appropriate __reduce__ method for obj.
3480 */
3481static int
3482save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3483{
3484 PyObject *callable;
3485 PyObject *argtup;
3486 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003487 PyObject *listitems = Py_None;
3488 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003489 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003490 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003491 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003492
3493 const char reduce_op = REDUCE;
3494 const char build_op = BUILD;
3495 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003496 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003497
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003498 size = PyTuple_Size(args);
3499 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003500 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003501 "__reduce__ must contain 2 through 5 elements");
3502 return -1;
3503 }
3504
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003505 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3506 &callable, &argtup, &state, &listitems, &dictitems))
3507 return -1;
3508
3509 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003510 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003511 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003512 return -1;
3513 }
3514 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003515 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003516 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003517 return -1;
3518 }
3519
3520 if (state == Py_None)
3521 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003522
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003523 if (listitems == Py_None)
3524 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003525 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003526 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003527 "returned by __reduce__ must be an iterator, not %s",
3528 Py_TYPE(listitems)->tp_name);
3529 return -1;
3530 }
3531
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003532 if (dictitems == Py_None)
3533 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003534 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003535 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003536 "returned by __reduce__ must be an iterator, not %s",
3537 Py_TYPE(dictitems)->tp_name);
3538 return -1;
3539 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003540
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003541 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003542 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003543 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003544
Victor Stinner804e05e2013-11-14 01:26:17 +01003545 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003546 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003547 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003548 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003549 }
3550 PyErr_Clear();
3551 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003552 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003553 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003554 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3555 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003556 if (!use_newobj_ex) {
3557 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003558 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003559 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003560 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003561 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003562 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003563
3564 if (use_newobj_ex) {
3565 PyObject *cls;
3566 PyObject *args;
3567 PyObject *kwargs;
3568
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003569 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003570 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003571 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003572 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003573 return -1;
3574 }
3575
3576 cls = PyTuple_GET_ITEM(argtup, 0);
3577 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003578 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003579 "first item from NEWOBJ_EX argument tuple must "
3580 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3581 return -1;
3582 }
3583 args = PyTuple_GET_ITEM(argtup, 1);
3584 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003585 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003586 "second item from NEWOBJ_EX argument tuple must "
3587 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3588 return -1;
3589 }
3590 kwargs = PyTuple_GET_ITEM(argtup, 2);
3591 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003592 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003593 "third item from NEWOBJ_EX argument tuple must "
3594 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3595 return -1;
3596 }
3597
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003598 if (self->proto >= 4) {
3599 if (save(self, cls, 0) < 0 ||
3600 save(self, args, 0) < 0 ||
3601 save(self, kwargs, 0) < 0 ||
3602 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3603 return -1;
3604 }
3605 }
3606 else {
3607 PyObject *newargs;
3608 PyObject *cls_new;
3609 Py_ssize_t i;
3610 _Py_IDENTIFIER(__new__);
3611
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003612 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003613 if (newargs == NULL)
3614 return -1;
3615
3616 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3617 if (cls_new == NULL) {
3618 Py_DECREF(newargs);
3619 return -1;
3620 }
3621 PyTuple_SET_ITEM(newargs, 0, cls_new);
3622 Py_INCREF(cls);
3623 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003624 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003625 PyObject *item = PyTuple_GET_ITEM(args, i);
3626 Py_INCREF(item);
3627 PyTuple_SET_ITEM(newargs, i + 2, item);
3628 }
3629
3630 callable = PyObject_Call(st->partial, newargs, kwargs);
3631 Py_DECREF(newargs);
3632 if (callable == NULL)
3633 return -1;
3634
3635 newargs = PyTuple_New(0);
3636 if (newargs == NULL) {
3637 Py_DECREF(callable);
3638 return -1;
3639 }
3640
3641 if (save(self, callable, 0) < 0 ||
3642 save(self, newargs, 0) < 0 ||
3643 _Pickler_Write(self, &reduce_op, 1) < 0) {
3644 Py_DECREF(newargs);
3645 Py_DECREF(callable);
3646 return -1;
3647 }
3648 Py_DECREF(newargs);
3649 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003650 }
3651 }
3652 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003653 PyObject *cls;
3654 PyObject *newargtup;
3655 PyObject *obj_class;
3656 int p;
3657
3658 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003659 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003660 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003661 return -1;
3662 }
3663
3664 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003665 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003666 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003667 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003668 return -1;
3669 }
3670
3671 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003672 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003673 p = obj_class != cls; /* true iff a problem */
3674 Py_DECREF(obj_class);
3675 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003676 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003677 "__newobj__ args has the wrong class");
3678 return -1;
3679 }
3680 }
3681 /* XXX: These calls save() are prone to infinite recursion. Imagine
3682 what happen if the value returned by the __reduce__() method of
3683 some extension type contains another object of the same type. Ouch!
3684
3685 Here is a quick example, that I ran into, to illustrate what I
3686 mean:
3687
3688 >>> import pickle, copyreg
3689 >>> copyreg.dispatch_table.pop(complex)
3690 >>> pickle.dumps(1+2j)
3691 Traceback (most recent call last):
3692 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003693 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003694
3695 Removing the complex class from copyreg.dispatch_table made the
3696 __reduce_ex__() method emit another complex object:
3697
3698 >>> (1+1j).__reduce_ex__(2)
3699 (<function __newobj__ at 0xb7b71c3c>,
3700 (<class 'complex'>, (1+1j)), None, None, None)
3701
3702 Thus when save() was called on newargstup (the 2nd item) recursion
3703 ensued. Of course, the bug was in the complex class which had a
3704 broken __getnewargs__() that emitted another complex object. But,
3705 the point, here, is it is quite easy to end up with a broken reduce
3706 function. */
3707
3708 /* Save the class and its __new__ arguments. */
3709 if (save(self, cls, 0) < 0)
3710 return -1;
3711
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003712 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003713 if (newargtup == NULL)
3714 return -1;
3715
3716 p = save(self, newargtup, 0);
3717 Py_DECREF(newargtup);
3718 if (p < 0)
3719 return -1;
3720
3721 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003722 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003723 return -1;
3724 }
3725 else { /* Not using NEWOBJ. */
3726 if (save(self, callable, 0) < 0 ||
3727 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003728 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003729 return -1;
3730 }
3731
3732 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3733 the caller do not want to memoize the object. Not particularly useful,
3734 but that is to mimic the behavior save_reduce() in pickle.py when
3735 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003736 if (obj != NULL) {
3737 /* If the object is already in the memo, this means it is
3738 recursive. In this case, throw away everything we put on the
3739 stack, and fetch the object back from the memo. */
3740 if (PyMemoTable_Get(self->memo, obj)) {
3741 const char pop_op = POP;
3742
3743 if (_Pickler_Write(self, &pop_op, 1) < 0)
3744 return -1;
3745 if (memo_get(self, obj) < 0)
3746 return -1;
3747
3748 return 0;
3749 }
3750 else if (memo_put(self, obj) < 0)
3751 return -1;
3752 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003753
3754 if (listitems && batch_list(self, listitems) < 0)
3755 return -1;
3756
3757 if (dictitems && batch_dict(self, dictitems) < 0)
3758 return -1;
3759
3760 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003761 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003762 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003763 return -1;
3764 }
3765
3766 return 0;
3767}
3768
3769static int
3770save(PicklerObject *self, PyObject *obj, int pers_save)
3771{
3772 PyTypeObject *type;
3773 PyObject *reduce_func = NULL;
3774 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003775 int status = 0;
3776
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003777 if (_Pickler_OpcodeBoundary(self) < 0)
3778 return -1;
3779
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003780 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003781 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003782
3783 /* The extra pers_save argument is necessary to avoid calling save_pers()
3784 on its returned object. */
3785 if (!pers_save && self->pers_func) {
3786 /* save_pers() returns:
3787 -1 to signal an error;
3788 0 if it did nothing successfully;
3789 1 if a persistent id was saved.
3790 */
3791 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3792 goto done;
3793 }
3794
3795 type = Py_TYPE(obj);
3796
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003797 /* The old cPickle had an optimization that used switch-case statement
3798 dispatching on the first letter of the type name. This has was removed
3799 since benchmarks shown that this optimization was actually slowing
3800 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003801
3802 /* Atom types; these aren't memoized, so don't check the memo. */
3803
3804 if (obj == Py_None) {
3805 status = save_none(self, obj);
3806 goto done;
3807 }
3808 else if (obj == Py_False || obj == Py_True) {
3809 status = save_bool(self, obj);
3810 goto done;
3811 }
3812 else if (type == &PyLong_Type) {
3813 status = save_long(self, obj);
3814 goto done;
3815 }
3816 else if (type == &PyFloat_Type) {
3817 status = save_float(self, obj);
3818 goto done;
3819 }
3820
3821 /* Check the memo to see if it has the object. If so, generate
3822 a GET (or BINGET) opcode, instead of pickling the object
3823 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003824 if (PyMemoTable_Get(self->memo, obj)) {
3825 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003826 goto error;
3827 goto done;
3828 }
3829
3830 if (type == &PyBytes_Type) {
3831 status = save_bytes(self, obj);
3832 goto done;
3833 }
3834 else if (type == &PyUnicode_Type) {
3835 status = save_unicode(self, obj);
3836 goto done;
3837 }
3838 else if (type == &PyDict_Type) {
3839 status = save_dict(self, obj);
3840 goto done;
3841 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003842 else if (type == &PySet_Type) {
3843 status = save_set(self, obj);
3844 goto done;
3845 }
3846 else if (type == &PyFrozenSet_Type) {
3847 status = save_frozenset(self, obj);
3848 goto done;
3849 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003850 else if (type == &PyList_Type) {
3851 status = save_list(self, obj);
3852 goto done;
3853 }
3854 else if (type == &PyTuple_Type) {
3855 status = save_tuple(self, obj);
3856 goto done;
3857 }
3858 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003859 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003860 goto done;
3861 }
3862 else if (type == &PyFunction_Type) {
3863 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003864 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003865 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003866
3867 /* XXX: This part needs some unit tests. */
3868
3869 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003870 * self.dispatch_table, copyreg.dispatch_table, the object's
3871 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003872 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003873 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003874 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003875 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3876 (PyObject *)type);
3877 if (reduce_func == NULL) {
3878 if (PyErr_Occurred()) {
3879 goto error;
3880 }
3881 } else {
3882 /* PyDict_GetItemWithError() returns a borrowed reference.
3883 Increase the reference count to be consistent with
3884 PyObject_GetItem and _PyObject_GetAttrId used below. */
3885 Py_INCREF(reduce_func);
3886 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003887 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003888 reduce_func = PyObject_GetItem(self->dispatch_table,
3889 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003890 if (reduce_func == NULL) {
3891 if (PyErr_ExceptionMatches(PyExc_KeyError))
3892 PyErr_Clear();
3893 else
3894 goto error;
3895 }
3896 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003897 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003898 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003899 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003900 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003901 else if (PyType_IsSubtype(type, &PyType_Type)) {
3902 status = save_global(self, obj, NULL);
3903 goto done;
3904 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003905 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003906 _Py_IDENTIFIER(__reduce__);
3907 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003908
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003909
3910 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3911 automatically defined as __reduce__. While this is convenient, this
3912 make it impossible to know which method was actually called. Of
3913 course, this is not a big deal. But still, it would be nice to let
3914 the user know which method was called when something go
3915 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3916 don't actually have to check for a __reduce__ method. */
3917
3918 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003919 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003920 if (reduce_func != NULL) {
3921 PyObject *proto;
3922 proto = PyLong_FromLong(self->proto);
3923 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003924 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003925 }
3926 }
3927 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003928 PickleState *st = _Pickle_GetGlobalState();
3929
3930 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003931 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003932 }
3933 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003934 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003935 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003936 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003937 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003938 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02003939 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003940 }
3941 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003942 PyErr_Format(st->PicklingError,
3943 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003944 type->tp_name, obj);
3945 goto error;
3946 }
3947 }
3948 }
3949
3950 if (reduce_value == NULL)
3951 goto error;
3952
3953 if (PyUnicode_Check(reduce_value)) {
3954 status = save_global(self, obj, reduce_value);
3955 goto done;
3956 }
3957
3958 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003959 PickleState *st = _Pickle_GetGlobalState();
3960 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003961 "__reduce__ must return a string or tuple");
3962 goto error;
3963 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003964
3965 status = save_reduce(self, reduce_value, obj);
3966
3967 if (0) {
3968 error:
3969 status = -1;
3970 }
3971 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003972
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003973 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003974 Py_XDECREF(reduce_func);
3975 Py_XDECREF(reduce_value);
3976
3977 return status;
3978}
3979
3980static int
3981dump(PicklerObject *self, PyObject *obj)
3982{
3983 const char stop_op = STOP;
3984
3985 if (self->proto >= 2) {
3986 char header[2];
3987
3988 header[0] = PROTO;
3989 assert(self->proto >= 0 && self->proto < 256);
3990 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003991 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003992 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003993 if (self->proto >= 4)
3994 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003995 }
3996
3997 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003998 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003999 return -1;
4000
4001 return 0;
4002}
4003
Larry Hastings61272b72014-01-07 12:41:53 -08004004/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004005
4006_pickle.Pickler.clear_memo
4007
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004008Clears the pickler's "memo".
4009
4010The memo is the data structure that remembers which objects the
4011pickler has already seen, so that shared or recursive objects are
4012pickled by reference and not by value. This method is useful when
4013re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004014[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004015
Larry Hastings3cceb382014-01-04 11:09:09 -08004016static PyObject *
4017_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004018/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004019{
4020 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004021 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004022
4023 Py_RETURN_NONE;
4024}
4025
Larry Hastings61272b72014-01-07 12:41:53 -08004026/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004027
4028_pickle.Pickler.dump
4029
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004030 obj: object
4031 /
4032
4033Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004034[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004035
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004036static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004037_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004038/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004039{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004040 /* Check whether the Pickler was initialized correctly (issue3664).
4041 Developers often forget to call __init__() in their subclasses, which
4042 would trigger a segfault without this check. */
4043 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004044 PickleState *st = _Pickle_GetGlobalState();
4045 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004046 "Pickler.__init__() was not called by %s.__init__()",
4047 Py_TYPE(self)->tp_name);
4048 return NULL;
4049 }
4050
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004051 if (_Pickler_ClearBuffer(self) < 0)
4052 return NULL;
4053
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004054 if (dump(self, obj) < 0)
4055 return NULL;
4056
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004057 if (_Pickler_FlushToFile(self) < 0)
4058 return NULL;
4059
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004060 Py_RETURN_NONE;
4061}
4062
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004063/*[clinic input]
4064
4065_pickle.Pickler.__sizeof__ -> Py_ssize_t
4066
4067Returns size in memory, in bytes.
4068[clinic start generated code]*/
4069
4070static Py_ssize_t
4071_pickle_Pickler___sizeof___impl(PicklerObject *self)
4072/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4073{
4074 Py_ssize_t res, s;
4075
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004076 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004077 if (self->memo != NULL) {
4078 res += sizeof(PyMemoTable);
4079 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4080 }
4081 if (self->output_buffer != NULL) {
4082 s = _PySys_GetSizeOf(self->output_buffer);
4083 if (s == -1)
4084 return -1;
4085 res += s;
4086 }
4087 return res;
4088}
4089
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004090static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004091 _PICKLE_PICKLER_DUMP_METHODDEF
4092 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004093 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004094 {NULL, NULL} /* sentinel */
4095};
4096
4097static void
4098Pickler_dealloc(PicklerObject *self)
4099{
4100 PyObject_GC_UnTrack(self);
4101
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004102 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004103 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004104 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004105 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004106 Py_XDECREF(self->fast_memo);
4107
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004108 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004109
4110 Py_TYPE(self)->tp_free((PyObject *)self);
4111}
4112
4113static int
4114Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4115{
4116 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004117 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004118 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004119 Py_VISIT(self->fast_memo);
4120 return 0;
4121}
4122
4123static int
4124Pickler_clear(PicklerObject *self)
4125{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004126 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004127 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004128 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004129 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004130 Py_CLEAR(self->fast_memo);
4131
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004132 if (self->memo != NULL) {
4133 PyMemoTable *memo = self->memo;
4134 self->memo = NULL;
4135 PyMemoTable_Del(memo);
4136 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004137 return 0;
4138}
4139
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004140
Larry Hastings61272b72014-01-07 12:41:53 -08004141/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004142
4143_pickle.Pickler.__init__
4144
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004145 file: object
4146 protocol: object = NULL
4147 fix_imports: bool = True
4148
4149This takes a binary file for writing a pickle data stream.
4150
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004151The optional *protocol* argument tells the pickler to use the given
4152protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4153protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004154
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004155Specifying a negative protocol version selects the highest protocol
4156version supported. The higher the protocol used, the more recent the
4157version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004158
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004159The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004160bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004161writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004162this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004163
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004164If *fix_imports* is True and protocol is less than 3, pickle will try
4165to map the new Python 3 names to the old module names used in Python
41662, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004167[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004168
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004169static int
Larry Hastings89964c42015-04-14 18:07:59 -04004170_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4171 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004172/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004173{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004174 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004175 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004176
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004177 /* In case of multiple __init__() calls, clear previous content. */
4178 if (self->write != NULL)
4179 (void)Pickler_clear(self);
4180
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004181 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004182 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004183
4184 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004185 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004186
4187 /* memo and output_buffer may have already been created in _Pickler_New */
4188 if (self->memo == NULL) {
4189 self->memo = PyMemoTable_New();
4190 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004191 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004192 }
4193 self->output_len = 0;
4194 if (self->output_buffer == NULL) {
4195 self->max_output_len = WRITE_BUF_SIZE;
4196 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4197 self->max_output_len);
4198 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004199 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004200 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004201
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004202 self->fast = 0;
4203 self->fast_nesting = 0;
4204 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004205 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004206 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4207 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4208 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004209 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004210 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004211 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004212 self->dispatch_table = NULL;
4213 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4214 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4215 &PyId_dispatch_table);
4216 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004217 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004218 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004219
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004220 return 0;
4221}
4222
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004223
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004224/* Define a proxy object for the Pickler's internal memo object. This is to
4225 * avoid breaking code like:
4226 * pickler.memo.clear()
4227 * and
4228 * pickler.memo = saved_memo
4229 * Is this a good idea? Not really, but we don't want to break code that uses
4230 * it. Note that we don't implement the entire mapping API here. This is
4231 * intentional, as these should be treated as black-box implementation details.
4232 */
4233
Larry Hastings61272b72014-01-07 12:41:53 -08004234/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004235_pickle.PicklerMemoProxy.clear
4236
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004237Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004238[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004239
Larry Hastings3cceb382014-01-04 11:09:09 -08004240static PyObject *
4241_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004242/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004243{
4244 if (self->pickler->memo)
4245 PyMemoTable_Clear(self->pickler->memo);
4246 Py_RETURN_NONE;
4247}
4248
Larry Hastings61272b72014-01-07 12:41:53 -08004249/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004250_pickle.PicklerMemoProxy.copy
4251
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004252Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004253[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004254
Larry Hastings3cceb382014-01-04 11:09:09 -08004255static PyObject *
4256_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004257/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004258{
4259 Py_ssize_t i;
4260 PyMemoTable *memo;
4261 PyObject *new_memo = PyDict_New();
4262 if (new_memo == NULL)
4263 return NULL;
4264
4265 memo = self->pickler->memo;
4266 for (i = 0; i < memo->mt_allocated; ++i) {
4267 PyMemoEntry entry = memo->mt_table[i];
4268 if (entry.me_key != NULL) {
4269 int status;
4270 PyObject *key, *value;
4271
4272 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004273 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004274
4275 if (key == NULL || value == NULL) {
4276 Py_XDECREF(key);
4277 Py_XDECREF(value);
4278 goto error;
4279 }
4280 status = PyDict_SetItem(new_memo, key, value);
4281 Py_DECREF(key);
4282 Py_DECREF(value);
4283 if (status < 0)
4284 goto error;
4285 }
4286 }
4287 return new_memo;
4288
4289 error:
4290 Py_XDECREF(new_memo);
4291 return NULL;
4292}
4293
Larry Hastings61272b72014-01-07 12:41:53 -08004294/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004295_pickle.PicklerMemoProxy.__reduce__
4296
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004297Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004298[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004299
Larry Hastings3cceb382014-01-04 11:09:09 -08004300static PyObject *
4301_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004302/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004303{
4304 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004305 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004306 if (contents == NULL)
4307 return NULL;
4308
4309 reduce_value = PyTuple_New(2);
4310 if (reduce_value == NULL) {
4311 Py_DECREF(contents);
4312 return NULL;
4313 }
4314 dict_args = PyTuple_New(1);
4315 if (dict_args == NULL) {
4316 Py_DECREF(contents);
4317 Py_DECREF(reduce_value);
4318 return NULL;
4319 }
4320 PyTuple_SET_ITEM(dict_args, 0, contents);
4321 Py_INCREF((PyObject *)&PyDict_Type);
4322 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4323 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4324 return reduce_value;
4325}
4326
4327static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004328 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4329 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4330 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004331 {NULL, NULL} /* sentinel */
4332};
4333
4334static void
4335PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4336{
4337 PyObject_GC_UnTrack(self);
4338 Py_XDECREF(self->pickler);
4339 PyObject_GC_Del((PyObject *)self);
4340}
4341
4342static int
4343PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4344 visitproc visit, void *arg)
4345{
4346 Py_VISIT(self->pickler);
4347 return 0;
4348}
4349
4350static int
4351PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4352{
4353 Py_CLEAR(self->pickler);
4354 return 0;
4355}
4356
4357static PyTypeObject PicklerMemoProxyType = {
4358 PyVarObject_HEAD_INIT(NULL, 0)
4359 "_pickle.PicklerMemoProxy", /*tp_name*/
4360 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4361 0,
4362 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4363 0, /* tp_print */
4364 0, /* tp_getattr */
4365 0, /* tp_setattr */
4366 0, /* tp_compare */
4367 0, /* tp_repr */
4368 0, /* tp_as_number */
4369 0, /* tp_as_sequence */
4370 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004371 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004372 0, /* tp_call */
4373 0, /* tp_str */
4374 PyObject_GenericGetAttr, /* tp_getattro */
4375 PyObject_GenericSetAttr, /* tp_setattro */
4376 0, /* tp_as_buffer */
4377 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4378 0, /* tp_doc */
4379 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4380 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4381 0, /* tp_richcompare */
4382 0, /* tp_weaklistoffset */
4383 0, /* tp_iter */
4384 0, /* tp_iternext */
4385 picklerproxy_methods, /* tp_methods */
4386};
4387
4388static PyObject *
4389PicklerMemoProxy_New(PicklerObject *pickler)
4390{
4391 PicklerMemoProxyObject *self;
4392
4393 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4394 if (self == NULL)
4395 return NULL;
4396 Py_INCREF(pickler);
4397 self->pickler = pickler;
4398 PyObject_GC_Track(self);
4399 return (PyObject *)self;
4400}
4401
4402/*****************************************************************************/
4403
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004404static PyObject *
4405Pickler_get_memo(PicklerObject *self)
4406{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004407 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004408}
4409
4410static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004411Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004412{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004413 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004414
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004415 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004416 PyErr_SetString(PyExc_TypeError,
4417 "attribute deletion is not supported");
4418 return -1;
4419 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004420
4421 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4422 PicklerObject *pickler =
4423 ((PicklerMemoProxyObject *)obj)->pickler;
4424
4425 new_memo = PyMemoTable_Copy(pickler->memo);
4426 if (new_memo == NULL)
4427 return -1;
4428 }
4429 else if (PyDict_Check(obj)) {
4430 Py_ssize_t i = 0;
4431 PyObject *key, *value;
4432
4433 new_memo = PyMemoTable_New();
4434 if (new_memo == NULL)
4435 return -1;
4436
4437 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004438 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004439 PyObject *memo_obj;
4440
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004441 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004442 PyErr_SetString(PyExc_TypeError,
4443 "'memo' values must be 2-item tuples");
4444 goto error;
4445 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004446 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004447 if (memo_id == -1 && PyErr_Occurred())
4448 goto error;
4449 memo_obj = PyTuple_GET_ITEM(value, 1);
4450 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4451 goto error;
4452 }
4453 }
4454 else {
4455 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004456 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004457 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004458 return -1;
4459 }
4460
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004461 PyMemoTable_Del(self->memo);
4462 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004463
4464 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004465
4466 error:
4467 if (new_memo)
4468 PyMemoTable_Del(new_memo);
4469 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004470}
4471
4472static PyObject *
4473Pickler_get_persid(PicklerObject *self)
4474{
4475 if (self->pers_func == NULL)
4476 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4477 else
4478 Py_INCREF(self->pers_func);
4479 return self->pers_func;
4480}
4481
4482static int
4483Pickler_set_persid(PicklerObject *self, PyObject *value)
4484{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004485 if (value == NULL) {
4486 PyErr_SetString(PyExc_TypeError,
4487 "attribute deletion is not supported");
4488 return -1;
4489 }
4490 if (!PyCallable_Check(value)) {
4491 PyErr_SetString(PyExc_TypeError,
4492 "persistent_id must be a callable taking one argument");
4493 return -1;
4494 }
4495
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004496 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004497 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004498
4499 return 0;
4500}
4501
4502static PyMemberDef Pickler_members[] = {
4503 {"bin", T_INT, offsetof(PicklerObject, bin)},
4504 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004505 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004506 {NULL}
4507};
4508
4509static PyGetSetDef Pickler_getsets[] = {
4510 {"memo", (getter)Pickler_get_memo,
4511 (setter)Pickler_set_memo},
4512 {"persistent_id", (getter)Pickler_get_persid,
4513 (setter)Pickler_set_persid},
4514 {NULL}
4515};
4516
4517static PyTypeObject Pickler_Type = {
4518 PyVarObject_HEAD_INIT(NULL, 0)
4519 "_pickle.Pickler" , /*tp_name*/
4520 sizeof(PicklerObject), /*tp_basicsize*/
4521 0, /*tp_itemsize*/
4522 (destructor)Pickler_dealloc, /*tp_dealloc*/
4523 0, /*tp_print*/
4524 0, /*tp_getattr*/
4525 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004526 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004527 0, /*tp_repr*/
4528 0, /*tp_as_number*/
4529 0, /*tp_as_sequence*/
4530 0, /*tp_as_mapping*/
4531 0, /*tp_hash*/
4532 0, /*tp_call*/
4533 0, /*tp_str*/
4534 0, /*tp_getattro*/
4535 0, /*tp_setattro*/
4536 0, /*tp_as_buffer*/
4537 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004538 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004539 (traverseproc)Pickler_traverse, /*tp_traverse*/
4540 (inquiry)Pickler_clear, /*tp_clear*/
4541 0, /*tp_richcompare*/
4542 0, /*tp_weaklistoffset*/
4543 0, /*tp_iter*/
4544 0, /*tp_iternext*/
4545 Pickler_methods, /*tp_methods*/
4546 Pickler_members, /*tp_members*/
4547 Pickler_getsets, /*tp_getset*/
4548 0, /*tp_base*/
4549 0, /*tp_dict*/
4550 0, /*tp_descr_get*/
4551 0, /*tp_descr_set*/
4552 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004553 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004554 PyType_GenericAlloc, /*tp_alloc*/
4555 PyType_GenericNew, /*tp_new*/
4556 PyObject_GC_Del, /*tp_free*/
4557 0, /*tp_is_gc*/
4558};
4559
Victor Stinner121aab42011-09-29 23:40:53 +02004560/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004561
4562 XXX: It would be nice to able to avoid Python function call overhead, by
4563 using directly the C version of find_class(), when find_class() is not
4564 overridden by a subclass. Although, this could become rather hackish. A
4565 simpler optimization would be to call the C function when self is not a
4566 subclass instance. */
4567static PyObject *
4568find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4569{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004570 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004571
Victor Stinner55ba38a2016-12-09 16:09:30 +01004572 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4573 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004574}
4575
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004576static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004577marker(UnpicklerObject *self)
4578{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004579 Py_ssize_t mark;
4580
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004581 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004582 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004583 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004584 return -1;
4585 }
4586
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004587 mark = self->marks[--self->num_marks];
4588 self->stack->mark_set = self->num_marks != 0;
4589 self->stack->fence = self->num_marks ?
4590 self->marks[self->num_marks - 1] : 0;
4591 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004592}
4593
4594static int
4595load_none(UnpicklerObject *self)
4596{
4597 PDATA_APPEND(self->stack, Py_None, -1);
4598 return 0;
4599}
4600
4601static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004602load_int(UnpicklerObject *self)
4603{
4604 PyObject *value;
4605 char *endptr, *s;
4606 Py_ssize_t len;
4607 long x;
4608
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004609 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004610 return -1;
4611 if (len < 2)
4612 return bad_readline();
4613
4614 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004615 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004616 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004617 x = strtol(s, &endptr, 0);
4618
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004619 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004620 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004621 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004622 errno = 0;
4623 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004624 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004625 if (value == NULL) {
4626 PyErr_SetString(PyExc_ValueError,
4627 "could not convert string to int");
4628 return -1;
4629 }
4630 }
4631 else {
4632 if (len == 3 && (x == 0 || x == 1)) {
4633 if ((value = PyBool_FromLong(x)) == NULL)
4634 return -1;
4635 }
4636 else {
4637 if ((value = PyLong_FromLong(x)) == NULL)
4638 return -1;
4639 }
4640 }
4641
4642 PDATA_PUSH(self->stack, value, -1);
4643 return 0;
4644}
4645
4646static int
4647load_bool(UnpicklerObject *self, PyObject *boolean)
4648{
4649 assert(boolean == Py_True || boolean == Py_False);
4650 PDATA_APPEND(self->stack, boolean, -1);
4651 return 0;
4652}
4653
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004654/* s contains x bytes of an unsigned little-endian integer. Return its value
4655 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4656 */
4657static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004658calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004659{
4660 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004661 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004662 size_t x = 0;
4663
Serhiy Storchakae0606192015-09-29 22:10:07 +03004664 if (nbytes > (int)sizeof(size_t)) {
4665 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4666 * have 64-bit size that can't be represented on 32-bit platform.
4667 */
4668 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4669 if (s[i])
4670 return -1;
4671 }
4672 nbytes = (int)sizeof(size_t);
4673 }
4674 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004675 x |= (size_t) s[i] << (8 * i);
4676 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004677
4678 if (x > PY_SSIZE_T_MAX)
4679 return -1;
4680 else
4681 return (Py_ssize_t) x;
4682}
4683
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004684/* s contains x bytes of a little-endian integer. Return its value as a
4685 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004686 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004687 * of x-platform bugs.
4688 */
4689static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004690calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004691{
4692 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004693 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004694 long x = 0;
4695
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004696 for (i = 0; i < nbytes; i++) {
4697 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004698 }
4699
4700 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4701 * is signed, so on a box with longs bigger than 4 bytes we need
4702 * to extend a BININT's sign bit to the full width.
4703 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004704 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004705 x |= -(x & (1L << 31));
4706 }
4707
4708 return x;
4709}
4710
4711static int
4712load_binintx(UnpicklerObject *self, char *s, int size)
4713{
4714 PyObject *value;
4715 long x;
4716
4717 x = calc_binint(s, size);
4718
4719 if ((value = PyLong_FromLong(x)) == NULL)
4720 return -1;
4721
4722 PDATA_PUSH(self->stack, value, -1);
4723 return 0;
4724}
4725
4726static int
4727load_binint(UnpicklerObject *self)
4728{
4729 char *s;
4730
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004731 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004732 return -1;
4733
4734 return load_binintx(self, s, 4);
4735}
4736
4737static int
4738load_binint1(UnpicklerObject *self)
4739{
4740 char *s;
4741
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004742 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004743 return -1;
4744
4745 return load_binintx(self, s, 1);
4746}
4747
4748static int
4749load_binint2(UnpicklerObject *self)
4750{
4751 char *s;
4752
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004753 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004754 return -1;
4755
4756 return load_binintx(self, s, 2);
4757}
4758
4759static int
4760load_long(UnpicklerObject *self)
4761{
4762 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004763 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004764 Py_ssize_t len;
4765
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004766 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004767 return -1;
4768 if (len < 2)
4769 return bad_readline();
4770
Mark Dickinson8dd05142009-01-20 20:43:58 +00004771 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4772 the 'L' before calling PyLong_FromString. In order to maintain
4773 compatibility with Python 3.0.0, we don't actually *require*
4774 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004775 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004776 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004777 /* XXX: Should the base argument explicitly set to 10? */
4778 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004779 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004780 return -1;
4781
4782 PDATA_PUSH(self->stack, value, -1);
4783 return 0;
4784}
4785
4786/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4787 * data following.
4788 */
4789static int
4790load_counted_long(UnpicklerObject *self, int size)
4791{
4792 PyObject *value;
4793 char *nbytes;
4794 char *pdata;
4795
4796 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004797 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004798 return -1;
4799
4800 size = calc_binint(nbytes, size);
4801 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004802 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004803 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004804 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004805 "LONG pickle has negative byte count");
4806 return -1;
4807 }
4808
4809 if (size == 0)
4810 value = PyLong_FromLong(0L);
4811 else {
4812 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004813 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004814 return -1;
4815 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4816 1 /* little endian */ , 1 /* signed */ );
4817 }
4818 if (value == NULL)
4819 return -1;
4820 PDATA_PUSH(self->stack, value, -1);
4821 return 0;
4822}
4823
4824static int
4825load_float(UnpicklerObject *self)
4826{
4827 PyObject *value;
4828 char *endptr, *s;
4829 Py_ssize_t len;
4830 double d;
4831
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004832 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004833 return -1;
4834 if (len < 2)
4835 return bad_readline();
4836
4837 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004838 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4839 if (d == -1.0 && PyErr_Occurred())
4840 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004841 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004842 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4843 return -1;
4844 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004845 value = PyFloat_FromDouble(d);
4846 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004847 return -1;
4848
4849 PDATA_PUSH(self->stack, value, -1);
4850 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004851}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004852
4853static int
4854load_binfloat(UnpicklerObject *self)
4855{
4856 PyObject *value;
4857 double x;
4858 char *s;
4859
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004860 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004861 return -1;
4862
4863 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4864 if (x == -1.0 && PyErr_Occurred())
4865 return -1;
4866
4867 if ((value = PyFloat_FromDouble(x)) == NULL)
4868 return -1;
4869
4870 PDATA_PUSH(self->stack, value, -1);
4871 return 0;
4872}
4873
4874static int
4875load_string(UnpicklerObject *self)
4876{
4877 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004878 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004879 Py_ssize_t len;
4880 char *s, *p;
4881
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004882 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004883 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004884 /* Strip the newline */
4885 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004886 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004887 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004888 p = s + 1;
4889 len -= 2;
4890 }
4891 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004892 PickleState *st = _Pickle_GetGlobalState();
4893 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004894 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004895 return -1;
4896 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004897 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004898
4899 /* Use the PyBytes API to decode the string, since that is what is used
4900 to encode, and then coerce the result to Unicode. */
4901 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004902 if (bytes == NULL)
4903 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004904
4905 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4906 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4907 if (strcmp(self->encoding, "bytes") == 0) {
4908 obj = bytes;
4909 }
4910 else {
4911 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4912 Py_DECREF(bytes);
4913 if (obj == NULL) {
4914 return -1;
4915 }
4916 }
4917
4918 PDATA_PUSH(self->stack, obj, -1);
4919 return 0;
4920}
4921
4922static int
4923load_counted_binstring(UnpicklerObject *self, int nbytes)
4924{
4925 PyObject *obj;
4926 Py_ssize_t size;
4927 char *s;
4928
4929 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004930 return -1;
4931
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004932 size = calc_binsize(s, nbytes);
4933 if (size < 0) {
4934 PickleState *st = _Pickle_GetGlobalState();
4935 PyErr_Format(st->UnpicklingError,
4936 "BINSTRING exceeds system's maximum size of %zd bytes",
4937 PY_SSIZE_T_MAX);
4938 return -1;
4939 }
4940
4941 if (_Unpickler_Read(self, &s, size) < 0)
4942 return -1;
4943
4944 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4945 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4946 if (strcmp(self->encoding, "bytes") == 0) {
4947 obj = PyBytes_FromStringAndSize(s, size);
4948 }
4949 else {
4950 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4951 }
4952 if (obj == NULL) {
4953 return -1;
4954 }
4955
4956 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004957 return 0;
4958}
4959
4960static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004961load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004962{
4963 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004964 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004965 char *s;
4966
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004967 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968 return -1;
4969
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004970 size = calc_binsize(s, nbytes);
4971 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004972 PyErr_Format(PyExc_OverflowError,
4973 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004974 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004975 return -1;
4976 }
4977
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004978 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004979 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004980
4981 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004982 if (bytes == NULL)
4983 return -1;
4984
4985 PDATA_PUSH(self->stack, bytes, -1);
4986 return 0;
4987}
4988
4989static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004990load_unicode(UnpicklerObject *self)
4991{
4992 PyObject *str;
4993 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004994 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004995
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004996 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004997 return -1;
4998 if (len < 1)
4999 return bad_readline();
5000
5001 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5002 if (str == NULL)
5003 return -1;
5004
5005 PDATA_PUSH(self->stack, str, -1);
5006 return 0;
5007}
5008
5009static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005010load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005011{
5012 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005013 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005014 char *s;
5015
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005016 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005017 return -1;
5018
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005019 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005020 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005021 PyErr_Format(PyExc_OverflowError,
5022 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005023 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005024 return -1;
5025 }
5026
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005027 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005028 return -1;
5029
Victor Stinner485fb562010-04-13 11:07:24 +00005030 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005031 if (str == NULL)
5032 return -1;
5033
5034 PDATA_PUSH(self->stack, str, -1);
5035 return 0;
5036}
5037
5038static int
Victor Stinner21b47112016-03-14 18:09:39 +01005039load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040{
5041 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005043 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005044 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005045
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005046 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005047 if (tuple == NULL)
5048 return -1;
5049 PDATA_PUSH(self->stack, tuple, -1);
5050 return 0;
5051}
5052
5053static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005054load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005055{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005056 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005057
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005058 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005059 return -1;
5060
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005061 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005062}
5063
5064static int
5065load_empty_list(UnpicklerObject *self)
5066{
5067 PyObject *list;
5068
5069 if ((list = PyList_New(0)) == NULL)
5070 return -1;
5071 PDATA_PUSH(self->stack, list, -1);
5072 return 0;
5073}
5074
5075static int
5076load_empty_dict(UnpicklerObject *self)
5077{
5078 PyObject *dict;
5079
5080 if ((dict = PyDict_New()) == NULL)
5081 return -1;
5082 PDATA_PUSH(self->stack, dict, -1);
5083 return 0;
5084}
5085
5086static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005087load_empty_set(UnpicklerObject *self)
5088{
5089 PyObject *set;
5090
5091 if ((set = PySet_New(NULL)) == NULL)
5092 return -1;
5093 PDATA_PUSH(self->stack, set, -1);
5094 return 0;
5095}
5096
5097static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005098load_list(UnpicklerObject *self)
5099{
5100 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005101 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005102
5103 if ((i = marker(self)) < 0)
5104 return -1;
5105
5106 list = Pdata_poplist(self->stack, i);
5107 if (list == NULL)
5108 return -1;
5109 PDATA_PUSH(self->stack, list, -1);
5110 return 0;
5111}
5112
5113static int
5114load_dict(UnpicklerObject *self)
5115{
5116 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005117 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005118
5119 if ((i = marker(self)) < 0)
5120 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005121 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005122
5123 if ((dict = PyDict_New()) == NULL)
5124 return -1;
5125
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005126 if ((j - i) % 2 != 0) {
5127 PickleState *st = _Pickle_GetGlobalState();
5128 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005129 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005130 return -1;
5131 }
5132
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005133 for (k = i + 1; k < j; k += 2) {
5134 key = self->stack->data[k - 1];
5135 value = self->stack->data[k];
5136 if (PyDict_SetItem(dict, key, value) < 0) {
5137 Py_DECREF(dict);
5138 return -1;
5139 }
5140 }
5141 Pdata_clear(self->stack, i);
5142 PDATA_PUSH(self->stack, dict, -1);
5143 return 0;
5144}
5145
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005146static int
5147load_frozenset(UnpicklerObject *self)
5148{
5149 PyObject *items;
5150 PyObject *frozenset;
5151 Py_ssize_t i;
5152
5153 if ((i = marker(self)) < 0)
5154 return -1;
5155
5156 items = Pdata_poptuple(self->stack, i);
5157 if (items == NULL)
5158 return -1;
5159
5160 frozenset = PyFrozenSet_New(items);
5161 Py_DECREF(items);
5162 if (frozenset == NULL)
5163 return -1;
5164
5165 PDATA_PUSH(self->stack, frozenset, -1);
5166 return 0;
5167}
5168
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005169static PyObject *
5170instantiate(PyObject *cls, PyObject *args)
5171{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005172 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005173 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005174 /* Caller must assure args are a tuple. Normally, args come from
5175 Pdata_poptuple which packs objects from the top of the stack
5176 into a newly created tuple. */
5177 assert(PyTuple_Check(args));
Serhiy Storchakafff9a312017-03-21 08:53:25 +02005178 if (PyTuple_GET_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005179 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005180 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005181 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005182 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005183 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005184
Victor Stinner55ba38a2016-12-09 16:09:30 +01005185 result = _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005186 }
5187 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005188}
5189
5190static int
5191load_obj(UnpicklerObject *self)
5192{
5193 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005194 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005195
5196 if ((i = marker(self)) < 0)
5197 return -1;
5198
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005199 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005200 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005201
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005202 args = Pdata_poptuple(self->stack, i + 1);
5203 if (args == NULL)
5204 return -1;
5205
5206 PDATA_POP(self->stack, cls);
5207 if (cls) {
5208 obj = instantiate(cls, args);
5209 Py_DECREF(cls);
5210 }
5211 Py_DECREF(args);
5212 if (obj == NULL)
5213 return -1;
5214
5215 PDATA_PUSH(self->stack, obj, -1);
5216 return 0;
5217}
5218
5219static int
5220load_inst(UnpicklerObject *self)
5221{
5222 PyObject *cls = NULL;
5223 PyObject *args = NULL;
5224 PyObject *obj = NULL;
5225 PyObject *module_name;
5226 PyObject *class_name;
5227 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005228 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005229 char *s;
5230
5231 if ((i = marker(self)) < 0)
5232 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005233 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005234 return -1;
5235 if (len < 2)
5236 return bad_readline();
5237
5238 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5239 identifiers are permitted in Python 3.0, since the INST opcode is only
5240 supported by older protocols on Python 2.x. */
5241 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5242 if (module_name == NULL)
5243 return -1;
5244
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005245 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005246 if (len < 2) {
5247 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005248 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005249 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005250 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005251 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005252 cls = find_class(self, module_name, class_name);
5253 Py_DECREF(class_name);
5254 }
5255 }
5256 Py_DECREF(module_name);
5257
5258 if (cls == NULL)
5259 return -1;
5260
5261 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5262 obj = instantiate(cls, args);
5263 Py_DECREF(args);
5264 }
5265 Py_DECREF(cls);
5266
5267 if (obj == NULL)
5268 return -1;
5269
5270 PDATA_PUSH(self->stack, obj, -1);
5271 return 0;
5272}
5273
5274static int
5275load_newobj(UnpicklerObject *self)
5276{
5277 PyObject *args = NULL;
5278 PyObject *clsraw = NULL;
5279 PyTypeObject *cls; /* clsraw cast to its true type */
5280 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005281 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005282
5283 /* Stack is ... cls argtuple, and we want to call
5284 * cls.__new__(cls, *argtuple).
5285 */
5286 PDATA_POP(self->stack, args);
5287 if (args == NULL)
5288 goto error;
5289 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005290 PyErr_SetString(st->UnpicklingError,
5291 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005292 goto error;
5293 }
5294
5295 PDATA_POP(self->stack, clsraw);
5296 cls = (PyTypeObject *)clsraw;
5297 if (cls == NULL)
5298 goto error;
5299 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005300 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005301 "isn't a type object");
5302 goto error;
5303 }
5304 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005305 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005306 "has NULL tp_new");
5307 goto error;
5308 }
5309
5310 /* Call __new__. */
5311 obj = cls->tp_new(cls, args, NULL);
5312 if (obj == NULL)
5313 goto error;
5314
5315 Py_DECREF(args);
5316 Py_DECREF(clsraw);
5317 PDATA_PUSH(self->stack, obj, -1);
5318 return 0;
5319
5320 error:
5321 Py_XDECREF(args);
5322 Py_XDECREF(clsraw);
5323 return -1;
5324}
5325
5326static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005327load_newobj_ex(UnpicklerObject *self)
5328{
5329 PyObject *cls, *args, *kwargs;
5330 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005331 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005332
5333 PDATA_POP(self->stack, kwargs);
5334 if (kwargs == NULL) {
5335 return -1;
5336 }
5337 PDATA_POP(self->stack, args);
5338 if (args == NULL) {
5339 Py_DECREF(kwargs);
5340 return -1;
5341 }
5342 PDATA_POP(self->stack, cls);
5343 if (cls == NULL) {
5344 Py_DECREF(kwargs);
5345 Py_DECREF(args);
5346 return -1;
5347 }
Larry Hastings61272b72014-01-07 12:41:53 -08005348
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005349 if (!PyType_Check(cls)) {
5350 Py_DECREF(kwargs);
5351 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005352 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005353 "NEWOBJ_EX class argument must be a type, not %.200s",
5354 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005355 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005356 return -1;
5357 }
5358
5359 if (((PyTypeObject *)cls)->tp_new == NULL) {
5360 Py_DECREF(kwargs);
5361 Py_DECREF(args);
5362 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005363 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005364 "NEWOBJ_EX class argument doesn't have __new__");
5365 return -1;
5366 }
5367 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5368 Py_DECREF(kwargs);
5369 Py_DECREF(args);
5370 Py_DECREF(cls);
5371 if (obj == NULL) {
5372 return -1;
5373 }
5374 PDATA_PUSH(self->stack, obj, -1);
5375 return 0;
5376}
5377
5378static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005379load_global(UnpicklerObject *self)
5380{
5381 PyObject *global = NULL;
5382 PyObject *module_name;
5383 PyObject *global_name;
5384 Py_ssize_t len;
5385 char *s;
5386
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005387 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005388 return -1;
5389 if (len < 2)
5390 return bad_readline();
5391 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5392 if (!module_name)
5393 return -1;
5394
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005395 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005396 if (len < 2) {
5397 Py_DECREF(module_name);
5398 return bad_readline();
5399 }
5400 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5401 if (global_name) {
5402 global = find_class(self, module_name, global_name);
5403 Py_DECREF(global_name);
5404 }
5405 }
5406 Py_DECREF(module_name);
5407
5408 if (global == NULL)
5409 return -1;
5410 PDATA_PUSH(self->stack, global, -1);
5411 return 0;
5412}
5413
5414static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005415load_stack_global(UnpicklerObject *self)
5416{
5417 PyObject *global;
5418 PyObject *module_name;
5419 PyObject *global_name;
5420
5421 PDATA_POP(self->stack, global_name);
5422 PDATA_POP(self->stack, module_name);
5423 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5424 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005425 PickleState *st = _Pickle_GetGlobalState();
5426 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005427 Py_XDECREF(global_name);
5428 Py_XDECREF(module_name);
5429 return -1;
5430 }
5431 global = find_class(self, module_name, global_name);
5432 Py_DECREF(global_name);
5433 Py_DECREF(module_name);
5434 if (global == NULL)
5435 return -1;
5436 PDATA_PUSH(self->stack, global, -1);
5437 return 0;
5438}
5439
5440static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005441load_persid(UnpicklerObject *self)
5442{
5443 PyObject *pid;
5444 Py_ssize_t len;
5445 char *s;
5446
5447 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005448 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005449 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005450 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005451 return bad_readline();
5452
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005453 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5454 if (pid == NULL) {
5455 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5456 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5457 "persistent IDs in protocol 0 must be "
5458 "ASCII strings");
5459 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005460 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005461 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005462
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005463 /* This does not leak since _Pickle_FastCall() steals the reference
5464 to pid first. */
5465 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005466 if (pid == NULL)
5467 return -1;
5468
5469 PDATA_PUSH(self->stack, pid, -1);
5470 return 0;
5471 }
5472 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005473 PickleState *st = _Pickle_GetGlobalState();
5474 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005475 "A load persistent id instruction was encountered,\n"
5476 "but no persistent_load function was specified.");
5477 return -1;
5478 }
5479}
5480
5481static int
5482load_binpersid(UnpicklerObject *self)
5483{
5484 PyObject *pid;
5485
5486 if (self->pers_func) {
5487 PDATA_POP(self->stack, pid);
5488 if (pid == NULL)
5489 return -1;
5490
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005491 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005492 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005493 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005494 if (pid == NULL)
5495 return -1;
5496
5497 PDATA_PUSH(self->stack, pid, -1);
5498 return 0;
5499 }
5500 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005501 PickleState *st = _Pickle_GetGlobalState();
5502 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005503 "A load persistent id instruction was encountered,\n"
5504 "but no persistent_load function was specified.");
5505 return -1;
5506 }
5507}
5508
5509static int
5510load_pop(UnpicklerObject *self)
5511{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005512 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005513
5514 /* Note that we split the (pickle.py) stack into two stacks,
5515 * an object stack and a mark stack. We have to be clever and
5516 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005517 * mark stack first, and only signalling a stack underflow if
5518 * the object stack is empty and the mark stack doesn't match
5519 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005520 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005521 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005522 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005523 self->stack->mark_set = self->num_marks != 0;
5524 self->stack->fence = self->num_marks ?
5525 self->marks[self->num_marks - 1] : 0;
5526 } else if (len <= self->stack->fence)
5527 return Pdata_stack_underflow(self->stack);
5528 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005529 len--;
5530 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005531 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005532 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005533 return 0;
5534}
5535
5536static int
5537load_pop_mark(UnpicklerObject *self)
5538{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005539 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005540
5541 if ((i = marker(self)) < 0)
5542 return -1;
5543
5544 Pdata_clear(self->stack, i);
5545
5546 return 0;
5547}
5548
5549static int
5550load_dup(UnpicklerObject *self)
5551{
5552 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005553 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005554
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005555 if (len <= self->stack->fence)
5556 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005557 last = self->stack->data[len - 1];
5558 PDATA_APPEND(self->stack, last, -1);
5559 return 0;
5560}
5561
5562static int
5563load_get(UnpicklerObject *self)
5564{
5565 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005566 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005567 Py_ssize_t len;
5568 char *s;
5569
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005570 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005571 return -1;
5572 if (len < 2)
5573 return bad_readline();
5574
5575 key = PyLong_FromString(s, NULL, 10);
5576 if (key == NULL)
5577 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005578 idx = PyLong_AsSsize_t(key);
5579 if (idx == -1 && PyErr_Occurred()) {
5580 Py_DECREF(key);
5581 return -1;
5582 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005583
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005584 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005585 if (value == NULL) {
5586 if (!PyErr_Occurred())
5587 PyErr_SetObject(PyExc_KeyError, key);
5588 Py_DECREF(key);
5589 return -1;
5590 }
5591 Py_DECREF(key);
5592
5593 PDATA_APPEND(self->stack, value, -1);
5594 return 0;
5595}
5596
5597static int
5598load_binget(UnpicklerObject *self)
5599{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005600 PyObject *value;
5601 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005602 char *s;
5603
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005604 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005605 return -1;
5606
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005607 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005608
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005609 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005610 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005611 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005612 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005613 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005614 Py_DECREF(key);
5615 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 return -1;
5617 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005618
5619 PDATA_APPEND(self->stack, value, -1);
5620 return 0;
5621}
5622
5623static int
5624load_long_binget(UnpicklerObject *self)
5625{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005626 PyObject *value;
5627 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005629
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005630 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005631 return -1;
5632
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005633 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005635 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005636 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005637 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005638 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005639 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005640 Py_DECREF(key);
5641 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005642 return -1;
5643 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644
5645 PDATA_APPEND(self->stack, value, -1);
5646 return 0;
5647}
5648
5649/* Push an object from the extension registry (EXT[124]). nbytes is
5650 * the number of bytes following the opcode, holding the index (code) value.
5651 */
5652static int
5653load_extension(UnpicklerObject *self, int nbytes)
5654{
5655 char *codebytes; /* the nbytes bytes after the opcode */
5656 long code; /* calc_binint returns long */
5657 PyObject *py_code; /* code as a Python int */
5658 PyObject *obj; /* the object to push */
5659 PyObject *pair; /* (module_name, class_name) */
5660 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005661 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662
5663 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005664 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005665 return -1;
5666 code = calc_binint(codebytes, nbytes);
5667 if (code <= 0) { /* note that 0 is forbidden */
5668 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005669 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005670 return -1;
5671 }
5672
5673 /* Look for the code in the cache. */
5674 py_code = PyLong_FromLong(code);
5675 if (py_code == NULL)
5676 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005677 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005678 if (obj != NULL) {
5679 /* Bingo. */
5680 Py_DECREF(py_code);
5681 PDATA_APPEND(self->stack, obj, -1);
5682 return 0;
5683 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005684 if (PyErr_Occurred()) {
5685 Py_DECREF(py_code);
5686 return -1;
5687 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688
5689 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005690 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005691 if (pair == NULL) {
5692 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005693 if (!PyErr_Occurred()) {
5694 PyErr_Format(PyExc_ValueError, "unregistered extension "
5695 "code %ld", code);
5696 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005697 return -1;
5698 }
5699 /* Since the extension registry is manipulable via Python code,
5700 * confirm that pair is really a 2-tuple of strings.
5701 */
5702 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5703 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5704 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5705 Py_DECREF(py_code);
5706 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5707 "isn't a 2-tuple of strings", code);
5708 return -1;
5709 }
5710 /* Load the object. */
5711 obj = find_class(self, module_name, class_name);
5712 if (obj == NULL) {
5713 Py_DECREF(py_code);
5714 return -1;
5715 }
5716 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005717 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005718 Py_DECREF(py_code);
5719 if (code < 0) {
5720 Py_DECREF(obj);
5721 return -1;
5722 }
5723 PDATA_PUSH(self->stack, obj, -1);
5724 return 0;
5725}
5726
5727static int
5728load_put(UnpicklerObject *self)
5729{
5730 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005731 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005732 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005733 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005734
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005735 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005736 return -1;
5737 if (len < 2)
5738 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005739 if (Py_SIZE(self->stack) <= self->stack->fence)
5740 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005741 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005742
5743 key = PyLong_FromString(s, NULL, 10);
5744 if (key == NULL)
5745 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005746 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005747 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005748 if (idx < 0) {
5749 if (!PyErr_Occurred())
5750 PyErr_SetString(PyExc_ValueError,
5751 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005752 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005753 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005754
5755 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005756}
5757
5758static int
5759load_binput(UnpicklerObject *self)
5760{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005761 PyObject *value;
5762 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005765 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005767
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005768 if (Py_SIZE(self->stack) <= self->stack->fence)
5769 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005770 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005772 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005774 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005775}
5776
5777static int
5778load_long_binput(UnpicklerObject *self)
5779{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005780 PyObject *value;
5781 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005782 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005783
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005784 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005785 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005786
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005787 if (Py_SIZE(self->stack) <= self->stack->fence)
5788 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005789 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005790
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005791 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005792 if (idx < 0) {
5793 PyErr_SetString(PyExc_ValueError,
5794 "negative LONG_BINPUT argument");
5795 return -1;
5796 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005798 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005799}
5800
5801static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005802load_memoize(UnpicklerObject *self)
5803{
5804 PyObject *value;
5805
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005806 if (Py_SIZE(self->stack) <= self->stack->fence)
5807 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005808 value = self->stack->data[Py_SIZE(self->stack) - 1];
5809
5810 return _Unpickler_MemoPut(self, self->memo_len, value);
5811}
5812
5813static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005814do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005815{
5816 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005817 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005818 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005819 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005820 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005821
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005822 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005823 if (x > len || x <= self->stack->fence)
5824 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005825 if (len == x) /* nothing to do */
5826 return 0;
5827
5828 list = self->stack->data[x - 1];
5829
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005830 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005831 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005832 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005833
5834 slice = Pdata_poplist(self->stack, x);
5835 if (!slice)
5836 return -1;
5837 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005838 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005839 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005840 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005841 }
5842 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005843 PyObject *extend_func;
5844 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005845
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005846 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
5847 if (extend_func != NULL) {
5848 slice = Pdata_poplist(self->stack, x);
5849 if (!slice) {
5850 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005851 return -1;
5852 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005853 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005854 Py_DECREF(extend_func);
5855 if (result == NULL)
5856 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005857 Py_DECREF(result);
5858 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005859 else {
5860 PyObject *append_func;
5861 _Py_IDENTIFIER(append);
5862
5863 /* Even if the PEP 307 requires extend() and append() methods,
5864 fall back on append() if the object has no extend() method
5865 for backward compatibility. */
5866 PyErr_Clear();
5867 append_func = _PyObject_GetAttrId(list, &PyId_append);
5868 if (append_func == NULL)
5869 return -1;
5870 for (i = x; i < len; i++) {
5871 value = self->stack->data[i];
5872 result = _Pickle_FastCall(append_func, value);
5873 if (result == NULL) {
5874 Pdata_clear(self->stack, i + 1);
5875 Py_SIZE(self->stack) = x;
5876 Py_DECREF(append_func);
5877 return -1;
5878 }
5879 Py_DECREF(result);
5880 }
5881 Py_SIZE(self->stack) = x;
5882 Py_DECREF(append_func);
5883 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005884 }
5885
5886 return 0;
5887}
5888
5889static int
5890load_append(UnpicklerObject *self)
5891{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005892 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5893 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005894 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005895}
5896
5897static int
5898load_appends(UnpicklerObject *self)
5899{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005900 Py_ssize_t i = marker(self);
5901 if (i < 0)
5902 return -1;
5903 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005904}
5905
5906static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005907do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005908{
5909 PyObject *value, *key;
5910 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005911 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005912 int status = 0;
5913
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005914 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005915 if (x > len || x <= self->stack->fence)
5916 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005917 if (len == x) /* nothing to do */
5918 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005919 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005920 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005921 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005922 PyErr_SetString(st->UnpicklingError,
5923 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005924 return -1;
5925 }
5926
5927 /* Here, dict does not actually need to be a PyDict; it could be anything
5928 that supports the __setitem__ attribute. */
5929 dict = self->stack->data[x - 1];
5930
5931 for (i = x + 1; i < len; i += 2) {
5932 key = self->stack->data[i - 1];
5933 value = self->stack->data[i];
5934 if (PyObject_SetItem(dict, key, value) < 0) {
5935 status = -1;
5936 break;
5937 }
5938 }
5939
5940 Pdata_clear(self->stack, x);
5941 return status;
5942}
5943
5944static int
5945load_setitem(UnpicklerObject *self)
5946{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005947 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005948}
5949
5950static int
5951load_setitems(UnpicklerObject *self)
5952{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005953 Py_ssize_t i = marker(self);
5954 if (i < 0)
5955 return -1;
5956 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005957}
5958
5959static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005960load_additems(UnpicklerObject *self)
5961{
5962 PyObject *set;
5963 Py_ssize_t mark, len, i;
5964
5965 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005966 if (mark < 0)
5967 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005968 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005969 if (mark > len || mark <= self->stack->fence)
5970 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005971 if (len == mark) /* nothing to do */
5972 return 0;
5973
5974 set = self->stack->data[mark - 1];
5975
5976 if (PySet_Check(set)) {
5977 PyObject *items;
5978 int status;
5979
5980 items = Pdata_poptuple(self->stack, mark);
5981 if (items == NULL)
5982 return -1;
5983
5984 status = _PySet_Update(set, items);
5985 Py_DECREF(items);
5986 return status;
5987 }
5988 else {
5989 PyObject *add_func;
5990 _Py_IDENTIFIER(add);
5991
5992 add_func = _PyObject_GetAttrId(set, &PyId_add);
5993 if (add_func == NULL)
5994 return -1;
5995 for (i = mark; i < len; i++) {
5996 PyObject *result;
5997 PyObject *item;
5998
5999 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006000 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006001 if (result == NULL) {
6002 Pdata_clear(self->stack, i + 1);
6003 Py_SIZE(self->stack) = mark;
6004 return -1;
6005 }
6006 Py_DECREF(result);
6007 }
6008 Py_SIZE(self->stack) = mark;
6009 }
6010
6011 return 0;
6012}
6013
6014static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006015load_build(UnpicklerObject *self)
6016{
6017 PyObject *state, *inst, *slotstate;
6018 PyObject *setstate;
6019 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006020 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006021
6022 /* Stack is ... instance, state. We want to leave instance at
6023 * the stack top, possibly mutated via instance.__setstate__(state).
6024 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006025 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6026 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006027
6028 PDATA_POP(self->stack, state);
6029 if (state == NULL)
6030 return -1;
6031
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006032 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006033
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006034 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006035 if (setstate == NULL) {
6036 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6037 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006038 else {
6039 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006040 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006041 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006042 }
6043 else {
6044 PyObject *result;
6045
6046 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006047 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006048 Py_DECREF(setstate);
6049 if (result == NULL)
6050 return -1;
6051 Py_DECREF(result);
6052 return 0;
6053 }
6054
6055 /* A default __setstate__. First see whether state embeds a
6056 * slot state dict too (a proto 2 addition).
6057 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006058 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006059 PyObject *tmp = state;
6060
6061 state = PyTuple_GET_ITEM(tmp, 0);
6062 slotstate = PyTuple_GET_ITEM(tmp, 1);
6063 Py_INCREF(state);
6064 Py_INCREF(slotstate);
6065 Py_DECREF(tmp);
6066 }
6067 else
6068 slotstate = NULL;
6069
6070 /* Set inst.__dict__ from the state dict (if any). */
6071 if (state != Py_None) {
6072 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006073 PyObject *d_key, *d_value;
6074 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006075 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006076
6077 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006078 PickleState *st = _Pickle_GetGlobalState();
6079 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006080 goto error;
6081 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006082 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006083 if (dict == NULL)
6084 goto error;
6085
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006086 i = 0;
6087 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6088 /* normally the keys for instance attributes are
6089 interned. we should try to do that here. */
6090 Py_INCREF(d_key);
6091 if (PyUnicode_CheckExact(d_key))
6092 PyUnicode_InternInPlace(&d_key);
6093 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6094 Py_DECREF(d_key);
6095 goto error;
6096 }
6097 Py_DECREF(d_key);
6098 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006099 Py_DECREF(dict);
6100 }
6101
6102 /* Also set instance attributes from the slotstate dict (if any). */
6103 if (slotstate != NULL) {
6104 PyObject *d_key, *d_value;
6105 Py_ssize_t i;
6106
6107 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006108 PickleState *st = _Pickle_GetGlobalState();
6109 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006110 "slot state is not a dictionary");
6111 goto error;
6112 }
6113 i = 0;
6114 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6115 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6116 goto error;
6117 }
6118 }
6119
6120 if (0) {
6121 error:
6122 status = -1;
6123 }
6124
6125 Py_DECREF(state);
6126 Py_XDECREF(slotstate);
6127 return status;
6128}
6129
6130static int
6131load_mark(UnpicklerObject *self)
6132{
6133
6134 /* Note that we split the (pickle.py) stack into two stacks, an
6135 * object stack and a mark stack. Here we push a mark onto the
6136 * mark stack.
6137 */
6138
6139 if ((self->num_marks + 1) >= self->marks_size) {
6140 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006141
6142 /* Use the size_t type to check for overflow. */
6143 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006144 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006145 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006146 PyErr_NoMemory();
6147 return -1;
6148 }
6149
6150 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006151 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006152 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006153 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6154 if (self->marks == NULL) {
6155 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006156 PyErr_NoMemory();
6157 return -1;
6158 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006159 self->marks_size = (Py_ssize_t)alloc;
6160 }
6161
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006162 self->stack->mark_set = 1;
6163 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006164
6165 return 0;
6166}
6167
6168static int
6169load_reduce(UnpicklerObject *self)
6170{
6171 PyObject *callable = NULL;
6172 PyObject *argtup = NULL;
6173 PyObject *obj = NULL;
6174
6175 PDATA_POP(self->stack, argtup);
6176 if (argtup == NULL)
6177 return -1;
6178 PDATA_POP(self->stack, callable);
6179 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006180 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006181 Py_DECREF(callable);
6182 }
6183 Py_DECREF(argtup);
6184
6185 if (obj == NULL)
6186 return -1;
6187
6188 PDATA_PUSH(self->stack, obj, -1);
6189 return 0;
6190}
6191
6192/* Just raises an error if we don't know the protocol specified. PROTO
6193 * is the first opcode for protocols >= 2.
6194 */
6195static int
6196load_proto(UnpicklerObject *self)
6197{
6198 char *s;
6199 int i;
6200
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006201 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006202 return -1;
6203
6204 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006205 if (i <= HIGHEST_PROTOCOL) {
6206 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006208 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006209
6210 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6211 return -1;
6212}
6213
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006214static int
6215load_frame(UnpicklerObject *self)
6216{
6217 char *s;
6218 Py_ssize_t frame_len;
6219
6220 if (_Unpickler_Read(self, &s, 8) < 0)
6221 return -1;
6222
6223 frame_len = calc_binsize(s, 8);
6224 if (frame_len < 0) {
6225 PyErr_Format(PyExc_OverflowError,
6226 "FRAME length exceeds system's maximum of %zd bytes",
6227 PY_SSIZE_T_MAX);
6228 return -1;
6229 }
6230
6231 if (_Unpickler_Read(self, &s, frame_len) < 0)
6232 return -1;
6233
6234 /* Rewind to start of frame */
6235 self->next_read_idx -= frame_len;
6236 return 0;
6237}
6238
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006239static PyObject *
6240load(UnpicklerObject *self)
6241{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006242 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006243 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006244
6245 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006246 self->stack->mark_set = 0;
6247 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006248 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006249 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006250 Pdata_clear(self->stack, 0);
6251
6252 /* Convenient macros for the dispatch while-switch loop just below. */
6253#define OP(opcode, load_func) \
6254 case opcode: if (load_func(self) < 0) break; continue;
6255
6256#define OP_ARG(opcode, load_func, arg) \
6257 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6258
6259 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006260 if (_Unpickler_Read(self, &s, 1) < 0) {
6261 PickleState *st = _Pickle_GetGlobalState();
6262 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6263 PyErr_Format(PyExc_EOFError, "Ran out of input");
6264 }
6265 return NULL;
6266 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006267
6268 switch ((enum opcode)s[0]) {
6269 OP(NONE, load_none)
6270 OP(BININT, load_binint)
6271 OP(BININT1, load_binint1)
6272 OP(BININT2, load_binint2)
6273 OP(INT, load_int)
6274 OP(LONG, load_long)
6275 OP_ARG(LONG1, load_counted_long, 1)
6276 OP_ARG(LONG4, load_counted_long, 4)
6277 OP(FLOAT, load_float)
6278 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006279 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6280 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6281 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6282 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6283 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006284 OP(STRING, load_string)
6285 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006286 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6287 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6288 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006289 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6290 OP_ARG(TUPLE1, load_counted_tuple, 1)
6291 OP_ARG(TUPLE2, load_counted_tuple, 2)
6292 OP_ARG(TUPLE3, load_counted_tuple, 3)
6293 OP(TUPLE, load_tuple)
6294 OP(EMPTY_LIST, load_empty_list)
6295 OP(LIST, load_list)
6296 OP(EMPTY_DICT, load_empty_dict)
6297 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006298 OP(EMPTY_SET, load_empty_set)
6299 OP(ADDITEMS, load_additems)
6300 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006301 OP(OBJ, load_obj)
6302 OP(INST, load_inst)
6303 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006304 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006305 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006306 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006307 OP(APPEND, load_append)
6308 OP(APPENDS, load_appends)
6309 OP(BUILD, load_build)
6310 OP(DUP, load_dup)
6311 OP(BINGET, load_binget)
6312 OP(LONG_BINGET, load_long_binget)
6313 OP(GET, load_get)
6314 OP(MARK, load_mark)
6315 OP(BINPUT, load_binput)
6316 OP(LONG_BINPUT, load_long_binput)
6317 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006318 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006319 OP(POP, load_pop)
6320 OP(POP_MARK, load_pop_mark)
6321 OP(SETITEM, load_setitem)
6322 OP(SETITEMS, load_setitems)
6323 OP(PERSID, load_persid)
6324 OP(BINPERSID, load_binpersid)
6325 OP(REDUCE, load_reduce)
6326 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006327 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006328 OP_ARG(EXT1, load_extension, 1)
6329 OP_ARG(EXT2, load_extension, 2)
6330 OP_ARG(EXT4, load_extension, 4)
6331 OP_ARG(NEWTRUE, load_bool, Py_True)
6332 OP_ARG(NEWFALSE, load_bool, Py_False)
6333
6334 case STOP:
6335 break;
6336
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006337 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006338 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006339 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006340 unsigned char c = (unsigned char) *s;
6341 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6342 PyErr_Format(st->UnpicklingError,
6343 "invalid load key, '%c'.", c);
6344 }
6345 else {
6346 PyErr_Format(st->UnpicklingError,
6347 "invalid load key, '\\x%02x'.", c);
6348 }
6349 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006350 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006351 }
6352
6353 break; /* and we are done! */
6354 }
6355
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006356 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006357 return NULL;
6358 }
6359
Victor Stinner2ae57e32013-10-31 13:39:23 +01006360 if (_Unpickler_SkipConsumed(self) < 0)
6361 return NULL;
6362
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006363 PDATA_POP(self->stack, value);
6364 return value;
6365}
6366
Larry Hastings61272b72014-01-07 12:41:53 -08006367/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006368
6369_pickle.Unpickler.load
6370
6371Load a pickle.
6372
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006373Read a pickled object representation from the open file object given
6374in the constructor, and return the reconstituted object hierarchy
6375specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006376[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006377
Larry Hastings3cceb382014-01-04 11:09:09 -08006378static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006379_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006380/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006381{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006382 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006383
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006384 /* Check whether the Unpickler was initialized correctly. This prevents
6385 segfaulting if a subclass overridden __init__ with a function that does
6386 not call Unpickler.__init__(). Here, we simply ensure that self->read
6387 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006388 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006389 PickleState *st = _Pickle_GetGlobalState();
6390 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006391 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006392 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006393 return NULL;
6394 }
6395
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006396 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006397}
6398
6399/* The name of find_class() is misleading. In newer pickle protocols, this
6400 function is used for loading any global (i.e., functions), not just
6401 classes. The name is kept only for backward compatibility. */
6402
Larry Hastings61272b72014-01-07 12:41:53 -08006403/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006404
6405_pickle.Unpickler.find_class
6406
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006407 module_name: object
6408 global_name: object
6409 /
6410
6411Return an object from a specified module.
6412
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006413If necessary, the module will be imported. Subclasses may override
6414this method (e.g. to restrict unpickling of arbitrary classes and
6415functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006416
6417This method is called whenever a class or a function object is
6418needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006419[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006420
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006421static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006422_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6423 PyObject *module_name,
6424 PyObject *global_name)
6425/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006426{
6427 PyObject *global;
Eric Snow93c92f72017-09-13 23:46:04 -07006428 PyObject *modules_dict;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006429 PyObject *module;
Eric Snow93c92f72017-09-13 23:46:04 -07006430 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006431
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006432 /* Try to map the old names used in Python 2.x to the new ones used in
6433 Python 3.x. We do this only with old pickle protocols and when the
6434 user has not disabled the feature. */
6435 if (self->proto < 3 && self->fix_imports) {
6436 PyObject *key;
6437 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006438 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006439
6440 /* Check if the global (i.e., a function or a class) was renamed
6441 or moved to another module. */
6442 key = PyTuple_Pack(2, module_name, global_name);
6443 if (key == NULL)
6444 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006445 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006446 Py_DECREF(key);
6447 if (item) {
6448 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6449 PyErr_Format(PyExc_RuntimeError,
6450 "_compat_pickle.NAME_MAPPING values should be "
6451 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6452 return NULL;
6453 }
6454 module_name = PyTuple_GET_ITEM(item, 0);
6455 global_name = PyTuple_GET_ITEM(item, 1);
6456 if (!PyUnicode_Check(module_name) ||
6457 !PyUnicode_Check(global_name)) {
6458 PyErr_Format(PyExc_RuntimeError,
6459 "_compat_pickle.NAME_MAPPING values should be "
6460 "pairs of str, not (%.200s, %.200s)",
6461 Py_TYPE(module_name)->tp_name,
6462 Py_TYPE(global_name)->tp_name);
6463 return NULL;
6464 }
6465 }
6466 else if (PyErr_Occurred()) {
6467 return NULL;
6468 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006469 else {
6470 /* Check if the module was renamed. */
6471 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6472 if (item) {
6473 if (!PyUnicode_Check(item)) {
6474 PyErr_Format(PyExc_RuntimeError,
6475 "_compat_pickle.IMPORT_MAPPING values should be "
6476 "strings, not %.200s", Py_TYPE(item)->tp_name);
6477 return NULL;
6478 }
6479 module_name = item;
6480 }
6481 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006482 return NULL;
6483 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006484 }
6485 }
6486
Eric Snow93c92f72017-09-13 23:46:04 -07006487 modules_dict = _PySys_GetObjectId(&PyId_modules);
6488 if (modules_dict == NULL) {
6489 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
6490 return NULL;
6491 }
6492
6493 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006494 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006495 if (PyErr_Occurred())
6496 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006497 module = PyImport_Import(module_name);
6498 if (module == NULL)
6499 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006500 global = getattribute(module, global_name, self->proto >= 4);
Eric Snow93c92f72017-09-13 23:46:04 -07006501 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006502 }
Victor Stinner121aab42011-09-29 23:40:53 +02006503 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006504 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006505 }
6506 return global;
6507}
6508
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006509/*[clinic input]
6510
6511_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6512
6513Returns size in memory, in bytes.
6514[clinic start generated code]*/
6515
6516static Py_ssize_t
6517_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6518/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6519{
6520 Py_ssize_t res;
6521
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006522 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006523 if (self->memo != NULL)
6524 res += self->memo_size * sizeof(PyObject *);
6525 if (self->marks != NULL)
6526 res += self->marks_size * sizeof(Py_ssize_t);
6527 if (self->input_line != NULL)
6528 res += strlen(self->input_line) + 1;
6529 if (self->encoding != NULL)
6530 res += strlen(self->encoding) + 1;
6531 if (self->errors != NULL)
6532 res += strlen(self->errors) + 1;
6533 return res;
6534}
6535
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006536static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006537 _PICKLE_UNPICKLER_LOAD_METHODDEF
6538 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006539 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540 {NULL, NULL} /* sentinel */
6541};
6542
6543static void
6544Unpickler_dealloc(UnpicklerObject *self)
6545{
6546 PyObject_GC_UnTrack((PyObject *)self);
6547 Py_XDECREF(self->readline);
6548 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006549 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006550 Py_XDECREF(self->stack);
6551 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006552 if (self->buffer.buf != NULL) {
6553 PyBuffer_Release(&self->buffer);
6554 self->buffer.buf = NULL;
6555 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006556
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006557 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006558 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006559 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006560 PyMem_Free(self->encoding);
6561 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006562
6563 Py_TYPE(self)->tp_free((PyObject *)self);
6564}
6565
6566static int
6567Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6568{
6569 Py_VISIT(self->readline);
6570 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006571 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006572 Py_VISIT(self->stack);
6573 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006574 return 0;
6575}
6576
6577static int
6578Unpickler_clear(UnpicklerObject *self)
6579{
6580 Py_CLEAR(self->readline);
6581 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006582 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006583 Py_CLEAR(self->stack);
6584 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006585 if (self->buffer.buf != NULL) {
6586 PyBuffer_Release(&self->buffer);
6587 self->buffer.buf = NULL;
6588 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006589
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006590 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006591 PyMem_Free(self->marks);
6592 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006593 PyMem_Free(self->input_line);
6594 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006595 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006596 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006597 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006598 self->errors = NULL;
6599
6600 return 0;
6601}
6602
Larry Hastings61272b72014-01-07 12:41:53 -08006603/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006604
6605_pickle.Unpickler.__init__
6606
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006607 file: object
6608 *
6609 fix_imports: bool = True
6610 encoding: str = 'ASCII'
6611 errors: str = 'strict'
6612
6613This takes a binary file for reading a pickle data stream.
6614
6615The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006616protocol argument is needed. Bytes past the pickled object's
6617representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006618
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006619The argument *file* must have two methods, a read() method that takes
6620an integer argument, and a readline() method that requires no
6621arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006622binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006623other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006624
6625Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006626which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006627generated by Python 2. If *fix_imports* is True, pickle will try to
6628map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006629*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006630instances pickled by Python 2; these default to 'ASCII' and 'strict',
6631respectively. The *encoding* can be 'bytes' to read these 8-bit
6632string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006633[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006634
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006635static int
Larry Hastings89964c42015-04-14 18:07:59 -04006636_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6637 int fix_imports, const char *encoding,
6638 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006639/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006640{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006641 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006642
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006643 /* In case of multiple __init__() calls, clear previous content. */
6644 if (self->read != NULL)
6645 (void)Unpickler_clear(self);
6646
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006647 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006648 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006649
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006650 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006651 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006652
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006653 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006654 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006655 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006656
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006657 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006658 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6659 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006660 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006661 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006662 }
6663 else {
6664 self->pers_func = NULL;
6665 }
6666
6667 self->stack = (Pdata *)Pdata_New();
6668 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006669 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006670
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006671 self->memo_size = 32;
6672 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006673 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006674 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006675
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006676 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006677
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006678 return 0;
6679}
6680
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006681
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006682/* Define a proxy object for the Unpickler's internal memo object. This is to
6683 * avoid breaking code like:
6684 * unpickler.memo.clear()
6685 * and
6686 * unpickler.memo = saved_memo
6687 * Is this a good idea? Not really, but we don't want to break code that uses
6688 * it. Note that we don't implement the entire mapping API here. This is
6689 * intentional, as these should be treated as black-box implementation details.
6690 *
6691 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006692 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006693 */
6694
Larry Hastings61272b72014-01-07 12:41:53 -08006695/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006696_pickle.UnpicklerMemoProxy.clear
6697
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006698Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006699[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006700
Larry Hastings3cceb382014-01-04 11:09:09 -08006701static PyObject *
6702_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006703/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006704{
6705 _Unpickler_MemoCleanup(self->unpickler);
6706 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6707 if (self->unpickler->memo == NULL)
6708 return NULL;
6709 Py_RETURN_NONE;
6710}
6711
Larry Hastings61272b72014-01-07 12:41:53 -08006712/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006713_pickle.UnpicklerMemoProxy.copy
6714
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006715Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006716[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006717
Larry Hastings3cceb382014-01-04 11:09:09 -08006718static PyObject *
6719_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006720/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006721{
6722 Py_ssize_t i;
6723 PyObject *new_memo = PyDict_New();
6724 if (new_memo == NULL)
6725 return NULL;
6726
6727 for (i = 0; i < self->unpickler->memo_size; i++) {
6728 int status;
6729 PyObject *key, *value;
6730
6731 value = self->unpickler->memo[i];
6732 if (value == NULL)
6733 continue;
6734
6735 key = PyLong_FromSsize_t(i);
6736 if (key == NULL)
6737 goto error;
6738 status = PyDict_SetItem(new_memo, key, value);
6739 Py_DECREF(key);
6740 if (status < 0)
6741 goto error;
6742 }
6743 return new_memo;
6744
6745error:
6746 Py_DECREF(new_memo);
6747 return NULL;
6748}
6749
Larry Hastings61272b72014-01-07 12:41:53 -08006750/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006751_pickle.UnpicklerMemoProxy.__reduce__
6752
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006753Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006754[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006755
Larry Hastings3cceb382014-01-04 11:09:09 -08006756static PyObject *
6757_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006758/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006759{
6760 PyObject *reduce_value;
6761 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006762 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006763 if (contents == NULL)
6764 return NULL;
6765
6766 reduce_value = PyTuple_New(2);
6767 if (reduce_value == NULL) {
6768 Py_DECREF(contents);
6769 return NULL;
6770 }
6771 constructor_args = PyTuple_New(1);
6772 if (constructor_args == NULL) {
6773 Py_DECREF(contents);
6774 Py_DECREF(reduce_value);
6775 return NULL;
6776 }
6777 PyTuple_SET_ITEM(constructor_args, 0, contents);
6778 Py_INCREF((PyObject *)&PyDict_Type);
6779 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6780 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6781 return reduce_value;
6782}
6783
6784static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006785 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6786 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6787 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006788 {NULL, NULL} /* sentinel */
6789};
6790
6791static void
6792UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6793{
6794 PyObject_GC_UnTrack(self);
6795 Py_XDECREF(self->unpickler);
6796 PyObject_GC_Del((PyObject *)self);
6797}
6798
6799static int
6800UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6801 visitproc visit, void *arg)
6802{
6803 Py_VISIT(self->unpickler);
6804 return 0;
6805}
6806
6807static int
6808UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6809{
6810 Py_CLEAR(self->unpickler);
6811 return 0;
6812}
6813
6814static PyTypeObject UnpicklerMemoProxyType = {
6815 PyVarObject_HEAD_INIT(NULL, 0)
6816 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6817 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6818 0,
6819 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6820 0, /* tp_print */
6821 0, /* tp_getattr */
6822 0, /* tp_setattr */
6823 0, /* tp_compare */
6824 0, /* tp_repr */
6825 0, /* tp_as_number */
6826 0, /* tp_as_sequence */
6827 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006828 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006829 0, /* tp_call */
6830 0, /* tp_str */
6831 PyObject_GenericGetAttr, /* tp_getattro */
6832 PyObject_GenericSetAttr, /* tp_setattro */
6833 0, /* tp_as_buffer */
6834 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6835 0, /* tp_doc */
6836 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6837 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6838 0, /* tp_richcompare */
6839 0, /* tp_weaklistoffset */
6840 0, /* tp_iter */
6841 0, /* tp_iternext */
6842 unpicklerproxy_methods, /* tp_methods */
6843};
6844
6845static PyObject *
6846UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6847{
6848 UnpicklerMemoProxyObject *self;
6849
6850 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6851 &UnpicklerMemoProxyType);
6852 if (self == NULL)
6853 return NULL;
6854 Py_INCREF(unpickler);
6855 self->unpickler = unpickler;
6856 PyObject_GC_Track(self);
6857 return (PyObject *)self;
6858}
6859
6860/*****************************************************************************/
6861
6862
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006863static PyObject *
6864Unpickler_get_memo(UnpicklerObject *self)
6865{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006866 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006867}
6868
6869static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006870Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006871{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006872 PyObject **new_memo;
6873 Py_ssize_t new_memo_size = 0;
6874 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006875
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006876 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006877 PyErr_SetString(PyExc_TypeError,
6878 "attribute deletion is not supported");
6879 return -1;
6880 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006881
6882 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6883 UnpicklerObject *unpickler =
6884 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6885
6886 new_memo_size = unpickler->memo_size;
6887 new_memo = _Unpickler_NewMemo(new_memo_size);
6888 if (new_memo == NULL)
6889 return -1;
6890
6891 for (i = 0; i < new_memo_size; i++) {
6892 Py_XINCREF(unpickler->memo[i]);
6893 new_memo[i] = unpickler->memo[i];
6894 }
6895 }
6896 else if (PyDict_Check(obj)) {
6897 Py_ssize_t i = 0;
6898 PyObject *key, *value;
6899
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006900 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006901 new_memo = _Unpickler_NewMemo(new_memo_size);
6902 if (new_memo == NULL)
6903 return -1;
6904
6905 while (PyDict_Next(obj, &i, &key, &value)) {
6906 Py_ssize_t idx;
6907 if (!PyLong_Check(key)) {
6908 PyErr_SetString(PyExc_TypeError,
6909 "memo key must be integers");
6910 goto error;
6911 }
6912 idx = PyLong_AsSsize_t(key);
6913 if (idx == -1 && PyErr_Occurred())
6914 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006915 if (idx < 0) {
6916 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006917 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006918 goto error;
6919 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006920 if (_Unpickler_MemoPut(self, idx, value) < 0)
6921 goto error;
6922 }
6923 }
6924 else {
6925 PyErr_Format(PyExc_TypeError,
6926 "'memo' attribute must be an UnpicklerMemoProxy object"
6927 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006928 return -1;
6929 }
6930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006931 _Unpickler_MemoCleanup(self);
6932 self->memo_size = new_memo_size;
6933 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006934
6935 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006936
6937 error:
6938 if (new_memo_size) {
6939 i = new_memo_size;
6940 while (--i >= 0) {
6941 Py_XDECREF(new_memo[i]);
6942 }
6943 PyMem_FREE(new_memo);
6944 }
6945 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006946}
6947
6948static PyObject *
6949Unpickler_get_persload(UnpicklerObject *self)
6950{
6951 if (self->pers_func == NULL)
6952 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6953 else
6954 Py_INCREF(self->pers_func);
6955 return self->pers_func;
6956}
6957
6958static int
6959Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6960{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006961 if (value == NULL) {
6962 PyErr_SetString(PyExc_TypeError,
6963 "attribute deletion is not supported");
6964 return -1;
6965 }
6966 if (!PyCallable_Check(value)) {
6967 PyErr_SetString(PyExc_TypeError,
6968 "persistent_load must be a callable taking "
6969 "one argument");
6970 return -1;
6971 }
6972
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006973 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03006974 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006975
6976 return 0;
6977}
6978
6979static PyGetSetDef Unpickler_getsets[] = {
6980 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6981 {"persistent_load", (getter)Unpickler_get_persload,
6982 (setter)Unpickler_set_persload},
6983 {NULL}
6984};
6985
6986static PyTypeObject Unpickler_Type = {
6987 PyVarObject_HEAD_INIT(NULL, 0)
6988 "_pickle.Unpickler", /*tp_name*/
6989 sizeof(UnpicklerObject), /*tp_basicsize*/
6990 0, /*tp_itemsize*/
6991 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6992 0, /*tp_print*/
6993 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006994 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006995 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006996 0, /*tp_repr*/
6997 0, /*tp_as_number*/
6998 0, /*tp_as_sequence*/
6999 0, /*tp_as_mapping*/
7000 0, /*tp_hash*/
7001 0, /*tp_call*/
7002 0, /*tp_str*/
7003 0, /*tp_getattro*/
7004 0, /*tp_setattro*/
7005 0, /*tp_as_buffer*/
7006 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007007 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007008 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7009 (inquiry)Unpickler_clear, /*tp_clear*/
7010 0, /*tp_richcompare*/
7011 0, /*tp_weaklistoffset*/
7012 0, /*tp_iter*/
7013 0, /*tp_iternext*/
7014 Unpickler_methods, /*tp_methods*/
7015 0, /*tp_members*/
7016 Unpickler_getsets, /*tp_getset*/
7017 0, /*tp_base*/
7018 0, /*tp_dict*/
7019 0, /*tp_descr_get*/
7020 0, /*tp_descr_set*/
7021 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007022 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007023 PyType_GenericAlloc, /*tp_alloc*/
7024 PyType_GenericNew, /*tp_new*/
7025 PyObject_GC_Del, /*tp_free*/
7026 0, /*tp_is_gc*/
7027};
7028
Larry Hastings61272b72014-01-07 12:41:53 -08007029/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007030
7031_pickle.dump
7032
7033 obj: object
7034 file: object
7035 protocol: object = NULL
7036 *
7037 fix_imports: bool = True
7038
7039Write a pickled representation of obj to the open file object file.
7040
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007041This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7042be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007043
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007044The optional *protocol* argument tells the pickler to use the given
7045protocol supported protocols are 0, 1, 2, 3 and 4. The default
7046protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007047
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007048Specifying a negative protocol version selects the highest protocol
7049version supported. The higher the protocol used, the more recent the
7050version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007051
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007052The *file* argument must have a write() method that accepts a single
7053bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007054writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007055this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007056
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007057If *fix_imports* is True and protocol is less than 3, pickle will try
7058to map the new Python 3 names to the old module names used in Python
70592, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007060[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007061
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007062static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007063_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007064 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007065/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007066{
7067 PicklerObject *pickler = _Pickler_New();
7068
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007069 if (pickler == NULL)
7070 return NULL;
7071
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007072 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007073 goto error;
7074
7075 if (_Pickler_SetOutputStream(pickler, file) < 0)
7076 goto error;
7077
7078 if (dump(pickler, obj) < 0)
7079 goto error;
7080
7081 if (_Pickler_FlushToFile(pickler) < 0)
7082 goto error;
7083
7084 Py_DECREF(pickler);
7085 Py_RETURN_NONE;
7086
7087 error:
7088 Py_XDECREF(pickler);
7089 return NULL;
7090}
7091
Larry Hastings61272b72014-01-07 12:41:53 -08007092/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007093
7094_pickle.dumps
7095
7096 obj: object
7097 protocol: object = NULL
7098 *
7099 fix_imports: bool = True
7100
7101Return the pickled representation of the object as a bytes object.
7102
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007103The optional *protocol* argument tells the pickler to use the given
7104protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7105protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007106
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007107Specifying a negative protocol version selects the highest protocol
7108version supported. The higher the protocol used, the more recent the
7109version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007110
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007111If *fix_imports* is True and *protocol* is less than 3, pickle will
7112try to map the new Python 3 names to the old module names used in
7113Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007114[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007115
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007116static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007117_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007118 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007119/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007120{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007121 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007122 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007123
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007124 if (pickler == NULL)
7125 return NULL;
7126
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007127 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007128 goto error;
7129
7130 if (dump(pickler, obj) < 0)
7131 goto error;
7132
7133 result = _Pickler_GetString(pickler);
7134 Py_DECREF(pickler);
7135 return result;
7136
7137 error:
7138 Py_XDECREF(pickler);
7139 return NULL;
7140}
7141
Larry Hastings61272b72014-01-07 12:41:53 -08007142/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007143
7144_pickle.load
7145
7146 file: object
7147 *
7148 fix_imports: bool = True
7149 encoding: str = 'ASCII'
7150 errors: str = 'strict'
7151
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007152Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007153
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007154This is equivalent to ``Unpickler(file).load()``, but may be more
7155efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007156
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007157The protocol version of the pickle is detected automatically, so no
7158protocol argument is needed. Bytes past the pickled object's
7159representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007160
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007161The argument *file* must have two methods, a read() method that takes
7162an integer argument, and a readline() method that requires no
7163arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007164binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007165other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007166
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007167Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007168which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007169generated by Python 2. If *fix_imports* is True, pickle will try to
7170map the old Python 2 names to the new names used in Python 3. The
7171*encoding* and *errors* tell pickle how to decode 8-bit string
7172instances pickled by Python 2; these default to 'ASCII' and 'strict',
7173respectively. The *encoding* can be 'bytes' to read these 8-bit
7174string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007175[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007176
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007177static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007178_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007179 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007180/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007181{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007182 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007183 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007184
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007185 if (unpickler == NULL)
7186 return NULL;
7187
7188 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7189 goto error;
7190
7191 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7192 goto error;
7193
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007194 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007195
7196 result = load(unpickler);
7197 Py_DECREF(unpickler);
7198 return result;
7199
7200 error:
7201 Py_XDECREF(unpickler);
7202 return NULL;
7203}
7204
Larry Hastings61272b72014-01-07 12:41:53 -08007205/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007206
7207_pickle.loads
7208
7209 data: object
7210 *
7211 fix_imports: bool = True
7212 encoding: str = 'ASCII'
7213 errors: str = 'strict'
7214
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007215Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007216
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007217The protocol version of the pickle is detected automatically, so no
7218protocol argument is needed. Bytes past the pickled object's
7219representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007220
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007221Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007222which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007223generated by Python 2. If *fix_imports* is True, pickle will try to
7224map the old Python 2 names to the new names used in Python 3. The
7225*encoding* and *errors* tell pickle how to decode 8-bit string
7226instances pickled by Python 2; these default to 'ASCII' and 'strict',
7227respectively. The *encoding* can be 'bytes' to read these 8-bit
7228string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007229[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007230
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007231static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007232_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007233 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007234/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007235{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007236 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007237 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007238
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007239 if (unpickler == NULL)
7240 return NULL;
7241
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007242 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007243 goto error;
7244
7245 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7246 goto error;
7247
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007248 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007249
7250 result = load(unpickler);
7251 Py_DECREF(unpickler);
7252 return result;
7253
7254 error:
7255 Py_XDECREF(unpickler);
7256 return NULL;
7257}
7258
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007259static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007260 _PICKLE_DUMP_METHODDEF
7261 _PICKLE_DUMPS_METHODDEF
7262 _PICKLE_LOAD_METHODDEF
7263 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007264 {NULL, NULL} /* sentinel */
7265};
7266
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007267static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007268pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007269{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007270 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007271 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007272}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007273
Stefan Krahf483b0f2013-12-14 13:43:10 +01007274static void
7275pickle_free(PyObject *m)
7276{
7277 _Pickle_ClearState(_Pickle_GetState(m));
7278}
7279
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007280static int
7281pickle_traverse(PyObject *m, visitproc visit, void *arg)
7282{
7283 PickleState *st = _Pickle_GetState(m);
7284 Py_VISIT(st->PickleError);
7285 Py_VISIT(st->PicklingError);
7286 Py_VISIT(st->UnpicklingError);
7287 Py_VISIT(st->dispatch_table);
7288 Py_VISIT(st->extension_registry);
7289 Py_VISIT(st->extension_cache);
7290 Py_VISIT(st->inverted_registry);
7291 Py_VISIT(st->name_mapping_2to3);
7292 Py_VISIT(st->import_mapping_2to3);
7293 Py_VISIT(st->name_mapping_3to2);
7294 Py_VISIT(st->import_mapping_3to2);
7295 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007296 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007297 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007298}
7299
7300static struct PyModuleDef _picklemodule = {
7301 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007302 "_pickle", /* m_name */
7303 pickle_module_doc, /* m_doc */
7304 sizeof(PickleState), /* m_size */
7305 pickle_methods, /* m_methods */
7306 NULL, /* m_reload */
7307 pickle_traverse, /* m_traverse */
7308 pickle_clear, /* m_clear */
7309 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007310};
7311
7312PyMODINIT_FUNC
7313PyInit__pickle(void)
7314{
7315 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007316 PickleState *st;
7317
7318 m = PyState_FindModule(&_picklemodule);
7319 if (m) {
7320 Py_INCREF(m);
7321 return m;
7322 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007323
7324 if (PyType_Ready(&Unpickler_Type) < 0)
7325 return NULL;
7326 if (PyType_Ready(&Pickler_Type) < 0)
7327 return NULL;
7328 if (PyType_Ready(&Pdata_Type) < 0)
7329 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007330 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7331 return NULL;
7332 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7333 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007334
7335 /* Create the module and add the functions. */
7336 m = PyModule_Create(&_picklemodule);
7337 if (m == NULL)
7338 return NULL;
7339
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007340 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007341 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7342 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007343 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007344 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7345 return NULL;
7346
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007347 st = _Pickle_GetState(m);
7348
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007349 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007350 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7351 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007352 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007353 st->PicklingError = \
7354 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7355 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007356 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007357 st->UnpicklingError = \
7358 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7359 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007360 return NULL;
7361
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007362 Py_INCREF(st->PickleError);
7363 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007364 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007365 Py_INCREF(st->PicklingError);
7366 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007367 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007368 Py_INCREF(st->UnpicklingError);
7369 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007370 return NULL;
7371
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007372 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007373 return NULL;
7374
7375 return m;
7376}