blob: d531dee2565fc2846bf155d851b613ee3abb1f76 [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001
2/* Core extension modules are built-in on some platforms (e.g. Windows). */
3#ifdef Py_BUILD_CORE
Eric Snowfc1bf872017-09-11 18:30:43 -07004#define Py_BUILD_CORE_BUILTIN
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#undef Py_BUILD_CORE
6#endif
7
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008#include "Python.h"
9#include "structmember.h"
10
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -080011PyDoc_STRVAR(pickle_module_doc,
12"Optimized C implementation for the Python pickle module.");
13
Larry Hastings61272b72014-01-07 12:41:53 -080014/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080016class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
17class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
18class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
19class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080020[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080022
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000023/* Bump this when new opcodes are added to the pickle protocol. */
24enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010025 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000026 DEFAULT_PROTOCOL = 3
27};
28
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029/* Pickle opcodes. These must be kept updated with pickle.py.
30 Extensive docs are in pickletools.py. */
31enum opcode {
32 MARK = '(',
33 STOP = '.',
34 POP = '0',
35 POP_MARK = '1',
36 DUP = '2',
37 FLOAT = 'F',
38 INT = 'I',
39 BININT = 'J',
40 BININT1 = 'K',
41 LONG = 'L',
42 BININT2 = 'M',
43 NONE = 'N',
44 PERSID = 'P',
45 BINPERSID = 'Q',
46 REDUCE = 'R',
47 STRING = 'S',
48 BINSTRING = 'T',
49 SHORT_BINSTRING = 'U',
50 UNICODE = 'V',
51 BINUNICODE = 'X',
52 APPEND = 'a',
53 BUILD = 'b',
54 GLOBAL = 'c',
55 DICT = 'd',
56 EMPTY_DICT = '}',
57 APPENDS = 'e',
58 GET = 'g',
59 BINGET = 'h',
60 INST = 'i',
61 LONG_BINGET = 'j',
62 LIST = 'l',
63 EMPTY_LIST = ']',
64 OBJ = 'o',
65 PUT = 'p',
66 BINPUT = 'q',
67 LONG_BINPUT = 'r',
68 SETITEM = 's',
69 TUPLE = 't',
70 EMPTY_TUPLE = ')',
71 SETITEMS = 'u',
72 BINFLOAT = 'G',
73
74 /* Protocol 2. */
75 PROTO = '\x80',
76 NEWOBJ = '\x81',
77 EXT1 = '\x82',
78 EXT2 = '\x83',
79 EXT4 = '\x84',
80 TUPLE1 = '\x85',
81 TUPLE2 = '\x86',
82 TUPLE3 = '\x87',
83 NEWTRUE = '\x88',
84 NEWFALSE = '\x89',
85 LONG1 = '\x8a',
86 LONG4 = '\x8b',
87
88 /* Protocol 3 (Python 3.x) */
89 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010090 SHORT_BINBYTES = 'C',
91
92 /* Protocol 4 */
93 SHORT_BINUNICODE = '\x8c',
94 BINUNICODE8 = '\x8d',
95 BINBYTES8 = '\x8e',
96 EMPTY_SET = '\x8f',
97 ADDITEMS = '\x90',
98 FROZENSET = '\x91',
99 NEWOBJ_EX = '\x92',
100 STACK_GLOBAL = '\x93',
101 MEMOIZE = '\x94',
102 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000103};
104
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000105enum {
106 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
107 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
108 break if this gets out of synch with pickle.py, but it's unclear that would
109 help anything either. */
110 BATCHSIZE = 1000,
111
112 /* Nesting limit until Pickler, when running in "fast mode", starts
113 checking for self-referential data-structures. */
114 FAST_NESTING_LIMIT = 50,
115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000116 /* Initial size of the write buffer of Pickler. */
117 WRITE_BUF_SIZE = 4096,
118
Antoine Pitrou04248a82010-10-12 20:51:21 +0000119 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100120 PREFETCH = 8192 * 16,
121
122 FRAME_SIZE_TARGET = 64 * 1024,
123
124 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000125};
126
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800127/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129/* State of the pickle module, per PEP 3121. */
130typedef struct {
131 /* Exception classes for pickle. */
132 PyObject *PickleError;
133 PyObject *PicklingError;
134 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800136 /* copyreg.dispatch_table, {type_object: pickling_function} */
137 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800139 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* copyreg._extension_registry, {(module_name, function_name): code} */
142 PyObject *extension_registry;
143 /* copyreg._extension_cache, {code: object} */
144 PyObject *extension_cache;
145 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
146 PyObject *inverted_registry;
147
148 /* Import mappings for compatibility with Python 2.x */
149
150 /* _compat_pickle.NAME_MAPPING,
151 {(oldmodule, oldname): (newmodule, newname)} */
152 PyObject *name_mapping_2to3;
153 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
154 PyObject *import_mapping_2to3;
155 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
156 PyObject *name_mapping_3to2;
157 PyObject *import_mapping_3to2;
158
159 /* codecs.encode, used for saving bytes in older protocols */
160 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300161 /* builtins.getattr, used for saving nested names with protocol < 4 */
162 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300163 /* functools.partial, used for implementing __newobj_ex__ with protocols
164 2 and 3 */
165 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800166} PickleState;
167
168/* Forward declaration of the _pickle module definition. */
169static struct PyModuleDef _picklemodule;
170
171/* Given a module object, get its per-module state. */
172static PickleState *
173_Pickle_GetState(PyObject *module)
174{
175 return (PickleState *)PyModule_GetState(module);
176}
177
178/* Find the module instance imported in the currently running sub-interpreter
179 and get its state. */
180static PickleState *
181_Pickle_GetGlobalState(void)
182{
183 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
184}
185
186/* Clear the given pickle module state. */
187static void
188_Pickle_ClearState(PickleState *st)
189{
190 Py_CLEAR(st->PickleError);
191 Py_CLEAR(st->PicklingError);
192 Py_CLEAR(st->UnpicklingError);
193 Py_CLEAR(st->dispatch_table);
194 Py_CLEAR(st->extension_registry);
195 Py_CLEAR(st->extension_cache);
196 Py_CLEAR(st->inverted_registry);
197 Py_CLEAR(st->name_mapping_2to3);
198 Py_CLEAR(st->import_mapping_2to3);
199 Py_CLEAR(st->name_mapping_3to2);
200 Py_CLEAR(st->import_mapping_3to2);
201 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300202 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100203 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204}
205
206/* Initialize the given pickle module state. */
207static int
208_Pickle_InitState(PickleState *st)
209{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300210 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800211 PyObject *copyreg = NULL;
212 PyObject *compat_pickle = NULL;
213 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300214 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800215
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300216 builtins = PyEval_GetBuiltins();
217 if (builtins == NULL)
218 goto error;
219 st->getattr = PyDict_GetItemString(builtins, "getattr");
220 if (st->getattr == NULL)
221 goto error;
222 Py_INCREF(st->getattr);
223
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800224 copyreg = PyImport_ImportModule("copyreg");
225 if (!copyreg)
226 goto error;
227 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
228 if (!st->dispatch_table)
229 goto error;
230 if (!PyDict_CheckExact(st->dispatch_table)) {
231 PyErr_Format(PyExc_RuntimeError,
232 "copyreg.dispatch_table should be a dict, not %.200s",
233 Py_TYPE(st->dispatch_table)->tp_name);
234 goto error;
235 }
236 st->extension_registry = \
237 PyObject_GetAttrString(copyreg, "_extension_registry");
238 if (!st->extension_registry)
239 goto error;
240 if (!PyDict_CheckExact(st->extension_registry)) {
241 PyErr_Format(PyExc_RuntimeError,
242 "copyreg._extension_registry should be a dict, "
243 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
244 goto error;
245 }
246 st->inverted_registry = \
247 PyObject_GetAttrString(copyreg, "_inverted_registry");
248 if (!st->inverted_registry)
249 goto error;
250 if (!PyDict_CheckExact(st->inverted_registry)) {
251 PyErr_Format(PyExc_RuntimeError,
252 "copyreg._inverted_registry should be a dict, "
253 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
254 goto error;
255 }
256 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
257 if (!st->extension_cache)
258 goto error;
259 if (!PyDict_CheckExact(st->extension_cache)) {
260 PyErr_Format(PyExc_RuntimeError,
261 "copyreg._extension_cache should be a dict, "
262 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
263 goto error;
264 }
265 Py_CLEAR(copyreg);
266
267 /* Load the 2.x -> 3.x stdlib module mapping tables */
268 compat_pickle = PyImport_ImportModule("_compat_pickle");
269 if (!compat_pickle)
270 goto error;
271 st->name_mapping_2to3 = \
272 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
273 if (!st->name_mapping_2to3)
274 goto error;
275 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
276 PyErr_Format(PyExc_RuntimeError,
277 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
278 Py_TYPE(st->name_mapping_2to3)->tp_name);
279 goto error;
280 }
281 st->import_mapping_2to3 = \
282 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
283 if (!st->import_mapping_2to3)
284 goto error;
285 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
286 PyErr_Format(PyExc_RuntimeError,
287 "_compat_pickle.IMPORT_MAPPING should be a dict, "
288 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
289 goto error;
290 }
291 /* ... and the 3.x -> 2.x mapping tables */
292 st->name_mapping_3to2 = \
293 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
294 if (!st->name_mapping_3to2)
295 goto error;
296 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
297 PyErr_Format(PyExc_RuntimeError,
298 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
299 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
300 goto error;
301 }
302 st->import_mapping_3to2 = \
303 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
304 if (!st->import_mapping_3to2)
305 goto error;
306 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
307 PyErr_Format(PyExc_RuntimeError,
308 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
309 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
310 goto error;
311 }
312 Py_CLEAR(compat_pickle);
313
314 codecs = PyImport_ImportModule("codecs");
315 if (codecs == NULL)
316 goto error;
317 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
318 if (st->codecs_encode == NULL) {
319 goto error;
320 }
321 if (!PyCallable_Check(st->codecs_encode)) {
322 PyErr_Format(PyExc_RuntimeError,
323 "codecs.encode should be a callable, not %.200s",
324 Py_TYPE(st->codecs_encode)->tp_name);
325 goto error;
326 }
327 Py_CLEAR(codecs);
328
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300329 functools = PyImport_ImportModule("functools");
330 if (!functools)
331 goto error;
332 st->partial = PyObject_GetAttrString(functools, "partial");
333 if (!st->partial)
334 goto error;
335 Py_CLEAR(functools);
336
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 return 0;
338
339 error:
340 Py_CLEAR(copyreg);
341 Py_CLEAR(compat_pickle);
342 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300343 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800344 _Pickle_ClearState(st);
345 return -1;
346}
347
348/* Helper for calling a function with a single argument quickly.
349
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 This function steals the reference of the given argument. */
351static PyObject *
352_Pickle_FastCall(PyObject *func, PyObject *obj)
353{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 PyObject *result;
355
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100356 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200357 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800358 return result;
359}
360
361/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000362
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000363/* Internal data type used as the unpickling stack. */
364typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000365 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000366 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200367 int mark_set; /* is MARK set? */
368 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000369 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000370} Pdata;
371
372static void
373Pdata_dealloc(Pdata *self)
374{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200375 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000376 while (--i >= 0) {
377 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000378 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000379 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000380 PyObject_Del(self);
381}
382
383static PyTypeObject Pdata_Type = {
384 PyVarObject_HEAD_INIT(NULL, 0)
385 "_pickle.Pdata", /*tp_name*/
386 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200387 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000388 (destructor)Pdata_dealloc, /*tp_dealloc*/
389};
390
391static PyObject *
392Pdata_New(void)
393{
394 Pdata *self;
395
396 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
397 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000398 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200399 self->mark_set = 0;
400 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000401 self->allocated = 8;
402 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000403 if (self->data)
404 return (PyObject *)self;
405 Py_DECREF(self);
406 return PyErr_NoMemory();
407}
408
409
410/* Retain only the initial clearto items. If clearto >= the current
411 * number of items, this is a (non-erroneous) NOP.
412 */
413static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200414Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000415{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200416 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000417
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200418 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000419 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000420 return 0;
421
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000422 while (--i >= clearto) {
423 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000424 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000425 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000426 return 0;
427}
428
429static int
430Pdata_grow(Pdata *self)
431{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200433 size_t allocated = (size_t)self->allocated;
434 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000435
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436 new_allocated = (allocated >> 3) + 6;
437 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200438 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000439 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000440 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500441 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000442 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000443 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000444
445 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200446 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000447 return 0;
448
449 nomemory:
450 PyErr_NoMemory();
451 return -1;
452}
453
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200454static int
455Pdata_stack_underflow(Pdata *self)
456{
457 PickleState *st = _Pickle_GetGlobalState();
458 PyErr_SetString(st->UnpicklingError,
459 self->mark_set ?
460 "unexpected MARK found" :
461 "unpickling stack underflow");
462 return -1;
463}
464
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000465/* D is a Pdata*. Pop the topmost element and store it into V, which
466 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
467 * is raised and V is set to NULL.
468 */
469static PyObject *
470Pdata_pop(Pdata *self)
471{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200472 if (Py_SIZE(self) <= self->fence) {
473 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000474 return NULL;
475 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000476 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477}
478#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
479
480static int
481Pdata_push(Pdata *self, PyObject *obj)
482{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000483 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000484 return -1;
485 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000486 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000487 return 0;
488}
489
490/* Push an object on stack, transferring its ownership to the stack. */
491#define PDATA_PUSH(D, O, ER) do { \
492 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
493
494/* Push an object on stack, adding a new reference to the object. */
495#define PDATA_APPEND(D, O, ER) do { \
496 Py_INCREF((O)); \
497 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
498
499static PyObject *
500Pdata_poptuple(Pdata *self, Py_ssize_t start)
501{
502 PyObject *tuple;
503 Py_ssize_t len, i, j;
504
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200505 if (start < self->fence) {
506 Pdata_stack_underflow(self);
507 return NULL;
508 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000509 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000510 tuple = PyTuple_New(len);
511 if (tuple == NULL)
512 return NULL;
513 for (i = start, j = 0; j < len; i++, j++)
514 PyTuple_SET_ITEM(tuple, j, self->data[i]);
515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000516 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000517 return tuple;
518}
519
520static PyObject *
521Pdata_poplist(Pdata *self, Py_ssize_t start)
522{
523 PyObject *list;
524 Py_ssize_t len, i, j;
525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000526 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000527 list = PyList_New(len);
528 if (list == NULL)
529 return NULL;
530 for (i = start, j = 0; j < len; i++, j++)
531 PyList_SET_ITEM(list, j, self->data[i]);
532
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000533 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000534 return list;
535}
536
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000537typedef struct {
538 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200539 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000540} PyMemoEntry;
541
542typedef struct {
543 Py_ssize_t mt_mask;
544 Py_ssize_t mt_used;
545 Py_ssize_t mt_allocated;
546 PyMemoEntry *mt_table;
547} PyMemoTable;
548
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000549typedef struct PicklerObject {
550 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000551 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000552 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000553 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000554 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100555 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000556
557 PyObject *write; /* write() method of the output stream. */
558 PyObject *output_buffer; /* Write into a local bytearray buffer before
559 flushing to the stream. */
560 Py_ssize_t output_len; /* Length of output_buffer. */
561 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 int proto; /* Pickle protocol number, >= 0 */
563 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100564 int framing; /* True when framing is enabled, proto >= 4 */
565 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000566 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100567 is no frame currently open. */
568
569 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000570 int fast; /* Enable fast mode if set to a true value.
571 The fast mode disable the usage of memo,
572 therefore speeding the pickling process by
573 not generating superfluous PUT opcodes. It
574 should not be used if with self-referential
575 objects. */
576 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000577 int fix_imports; /* Indicate whether Pickler should fix
578 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000579 PyObject *fast_memo;
580} PicklerObject;
581
582typedef struct UnpicklerObject {
583 PyObject_HEAD
584 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000585
586 /* The unpickler memo is just an array of PyObject *s. Using a dict
587 is unnecessary, since the keys are contiguous ints. */
588 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100589 Py_ssize_t memo_size; /* Capacity of the memo array */
590 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000591
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000592 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593
594 Py_buffer buffer;
595 char *input_buffer;
596 char *input_line;
597 Py_ssize_t input_len;
598 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000599 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100600
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000601 PyObject *read; /* read() method of the input stream. */
602 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000603 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000604
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000605 char *encoding; /* Name of the encoding to be used for
606 decoding strings pickled using Python
607 2.x. The default value is "ASCII" */
608 char *errors; /* Name of errors handling scheme to used when
609 decoding strings. The default value is
610 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500611 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000612 objects. */
613 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
614 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000615 int proto; /* Protocol of the pickle loaded. */
616 int fix_imports; /* Indicate whether Unpickler should fix
617 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000618} UnpicklerObject;
619
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200620typedef struct {
621 PyObject_HEAD
622 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
623} PicklerMemoProxyObject;
624
625typedef struct {
626 PyObject_HEAD
627 UnpicklerObject *unpickler;
628} UnpicklerMemoProxyObject;
629
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000630/* Forward declarations */
631static int save(PicklerObject *, PyObject *, int);
632static int save_reduce(PicklerObject *, PyObject *, PyObject *);
633static PyTypeObject Pickler_Type;
634static PyTypeObject Unpickler_Type;
635
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200636#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000637
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000638/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300639 A custom hashtable mapping void* to Python ints. This is used by the pickler
640 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000641 a bunch of unnecessary object creation. This makes a huge performance
642 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000643
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000644#define MT_MINSIZE 8
645#define PERTURB_SHIFT 5
646
647
648static PyMemoTable *
649PyMemoTable_New(void)
650{
651 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
652 if (memo == NULL) {
653 PyErr_NoMemory();
654 return NULL;
655 }
656
657 memo->mt_used = 0;
658 memo->mt_allocated = MT_MINSIZE;
659 memo->mt_mask = MT_MINSIZE - 1;
660 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
661 if (memo->mt_table == NULL) {
662 PyMem_FREE(memo);
663 PyErr_NoMemory();
664 return NULL;
665 }
666 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
667
668 return memo;
669}
670
671static PyMemoTable *
672PyMemoTable_Copy(PyMemoTable *self)
673{
674 Py_ssize_t i;
675 PyMemoTable *new = PyMemoTable_New();
676 if (new == NULL)
677 return NULL;
678
679 new->mt_used = self->mt_used;
680 new->mt_allocated = self->mt_allocated;
681 new->mt_mask = self->mt_mask;
682 /* The table we get from _New() is probably smaller than we wanted.
683 Free it and allocate one that's the right size. */
684 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500685 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000686 if (new->mt_table == NULL) {
687 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200688 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000689 return NULL;
690 }
691 for (i = 0; i < self->mt_allocated; i++) {
692 Py_XINCREF(self->mt_table[i].me_key);
693 }
694 memcpy(new->mt_table, self->mt_table,
695 sizeof(PyMemoEntry) * self->mt_allocated);
696
697 return new;
698}
699
700static Py_ssize_t
701PyMemoTable_Size(PyMemoTable *self)
702{
703 return self->mt_used;
704}
705
706static int
707PyMemoTable_Clear(PyMemoTable *self)
708{
709 Py_ssize_t i = self->mt_allocated;
710
711 while (--i >= 0) {
712 Py_XDECREF(self->mt_table[i].me_key);
713 }
714 self->mt_used = 0;
715 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
716 return 0;
717}
718
719static void
720PyMemoTable_Del(PyMemoTable *self)
721{
722 if (self == NULL)
723 return;
724 PyMemoTable_Clear(self);
725
726 PyMem_FREE(self->mt_table);
727 PyMem_FREE(self);
728}
729
730/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
731 can be considerably simpler than dictobject.c's lookdict(). */
732static PyMemoEntry *
733_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
734{
735 size_t i;
736 size_t perturb;
737 size_t mask = (size_t)self->mt_mask;
738 PyMemoEntry *table = self->mt_table;
739 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000740 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000741
742 i = hash & mask;
743 entry = &table[i];
744 if (entry->me_key == NULL || entry->me_key == key)
745 return entry;
746
747 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
748 i = (i << 2) + i + perturb + 1;
749 entry = &table[i & mask];
750 if (entry->me_key == NULL || entry->me_key == key)
751 return entry;
752 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700753 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000754}
755
756/* Returns -1 on failure, 0 on success. */
757static int
758_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
759{
760 PyMemoEntry *oldtable = NULL;
761 PyMemoEntry *oldentry, *newentry;
762 Py_ssize_t new_size = MT_MINSIZE;
763 Py_ssize_t to_process;
764
765 assert(min_size > 0);
766
767 /* Find the smallest valid table size >= min_size. */
768 while (new_size < min_size && new_size > 0)
769 new_size <<= 1;
770 if (new_size <= 0) {
771 PyErr_NoMemory();
772 return -1;
773 }
774 /* new_size needs to be a power of two. */
775 assert((new_size & (new_size - 1)) == 0);
776
777 /* Allocate new table. */
778 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500779 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000780 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200781 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000782 PyErr_NoMemory();
783 return -1;
784 }
785 self->mt_allocated = new_size;
786 self->mt_mask = new_size - 1;
787 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
788
789 /* Copy entries from the old table. */
790 to_process = self->mt_used;
791 for (oldentry = oldtable; to_process > 0; oldentry++) {
792 if (oldentry->me_key != NULL) {
793 to_process--;
794 /* newentry is a pointer to a chunk of the new
795 mt_table, so we're setting the key:value pair
796 in-place. */
797 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
798 newentry->me_key = oldentry->me_key;
799 newentry->me_value = oldentry->me_value;
800 }
801 }
802
803 /* Deallocate the old table. */
804 PyMem_FREE(oldtable);
805 return 0;
806}
807
808/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200809static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000810PyMemoTable_Get(PyMemoTable *self, PyObject *key)
811{
812 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
813 if (entry->me_key == NULL)
814 return NULL;
815 return &entry->me_value;
816}
817
818/* Returns -1 on failure, 0 on success. */
819static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200820PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000821{
822 PyMemoEntry *entry;
823
824 assert(key != NULL);
825
826 entry = _PyMemoTable_Lookup(self, key);
827 if (entry->me_key != NULL) {
828 entry->me_value = value;
829 return 0;
830 }
831 Py_INCREF(key);
832 entry->me_key = key;
833 entry->me_value = value;
834 self->mt_used++;
835
836 /* If we added a key, we can safely resize. Otherwise just return!
837 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
838 *
839 * Quadrupling the size improves average table sparseness
840 * (reducing collisions) at the cost of some memory. It also halves
841 * the number of expensive resize operations in a growing memo table.
842 *
843 * Very large memo tables (over 50K items) use doubling instead.
844 * This may help applications with severe memory constraints.
845 */
846 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
847 return 0;
848 return _PyMemoTable_ResizeTable(self,
849 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
850}
851
852#undef MT_MINSIZE
853#undef PERTURB_SHIFT
854
855/*************************************************************************/
856
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000857
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000858static int
859_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000860{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300861 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200862 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000863 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000864 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000865 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100866 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000867 return 0;
868}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000869
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100870static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100871_write_size64(char *out, size_t value)
872{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200873 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800874
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200875 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800876
877 for (i = 0; i < sizeof(size_t); i++) {
878 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
879 }
880 for (i = sizeof(size_t); i < 8; i++) {
881 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800882 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100883}
884
885static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100886_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
887{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100888 qdata[0] = FRAME;
889 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100890}
891
892static int
893_Pickler_CommitFrame(PicklerObject *self)
894{
895 size_t frame_len;
896 char *qdata;
897
898 if (!self->framing || self->frame_start == -1)
899 return 0;
900 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
901 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
902 _Pickler_WriteFrameHeader(self, qdata, frame_len);
903 self->frame_start = -1;
904 return 0;
905}
906
907static int
908_Pickler_OpcodeBoundary(PicklerObject *self)
909{
910 Py_ssize_t frame_len;
911
912 if (!self->framing || self->frame_start == -1)
913 return 0;
914 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
915 if (frame_len >= FRAME_SIZE_TARGET)
916 return _Pickler_CommitFrame(self);
917 else
918 return 0;
919}
920
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000921static PyObject *
922_Pickler_GetString(PicklerObject *self)
923{
924 PyObject *output_buffer = self->output_buffer;
925
926 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100927
928 if (_Pickler_CommitFrame(self))
929 return NULL;
930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000931 self->output_buffer = NULL;
932 /* Resize down to exact size */
933 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
934 return NULL;
935 return output_buffer;
936}
937
938static int
939_Pickler_FlushToFile(PicklerObject *self)
940{
941 PyObject *output, *result;
942
943 assert(self->write != NULL);
944
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100945 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000946 output = _Pickler_GetString(self);
947 if (output == NULL)
948 return -1;
949
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800950 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000951 Py_XDECREF(result);
952 return (result == NULL) ? -1 : 0;
953}
954
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200955static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100956_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000957{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100958 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000959 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100960 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000961
962 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100963 need_new_frame = (self->framing && self->frame_start == -1);
964
965 if (need_new_frame)
966 n = data_len + FRAME_HEADER_SIZE;
967 else
968 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000969
970 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100971 if (required > self->max_output_len) {
972 /* Make place in buffer for the pickle chunk */
973 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
974 PyErr_NoMemory();
975 return -1;
976 }
977 self->max_output_len = (self->output_len + n) / 2 * 3;
978 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
979 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000980 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000981 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 if (need_new_frame) {
983 /* Setup new frame */
984 Py_ssize_t frame_start = self->output_len;
985 self->frame_start = frame_start;
986 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
987 /* Write an invalid value, for debugging */
988 buffer[frame_start + i] = 0xFE;
989 }
990 self->output_len += FRAME_HEADER_SIZE;
991 }
992 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000993 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100994 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000995 buffer[self->output_len + i] = s[i];
996 }
997 }
998 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100999 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001000 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001001 self->output_len += data_len;
1002 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001003}
1004
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001005static PicklerObject *
1006_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001007{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001008 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001009
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001010 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1011 if (self == NULL)
1012 return NULL;
1013
1014 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001015 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001016 self->write = NULL;
1017 self->proto = 0;
1018 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001019 self->framing = 0;
1020 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021 self->fast = 0;
1022 self->fast_nesting = 0;
1023 self->fix_imports = 0;
1024 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001025 self->max_output_len = WRITE_BUF_SIZE;
1026 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001027
1028 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001029 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1030 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001031
1032 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001033 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001034 return NULL;
1035 }
1036 return self;
1037}
1038
1039static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001040_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001042 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001043
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001044 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001045 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001046 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001047 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001048 proto = PyLong_AsLong(protocol);
1049 if (proto < 0) {
1050 if (proto == -1 && PyErr_Occurred())
1051 return -1;
1052 proto = HIGHEST_PROTOCOL;
1053 }
1054 else if (proto > HIGHEST_PROTOCOL) {
1055 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1056 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001057 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001058 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001059 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001060 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001061 self->bin = proto > 0;
1062 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001063 return 0;
1064}
1065
1066/* Returns -1 (with an exception set) on failure, 0 on success. This may
1067 be called once on a freshly created Pickler. */
1068static int
1069_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1070{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001071 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001072 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001073 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001074 if (self->write == NULL) {
1075 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1076 PyErr_SetString(PyExc_TypeError,
1077 "file must have a 'write' attribute");
1078 return -1;
1079 }
1080
1081 return 0;
1082}
1083
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001084/* Returns the size of the input on success, -1 on failure. This takes its
1085 own reference to `input`. */
1086static Py_ssize_t
1087_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1088{
1089 if (self->buffer.buf != NULL)
1090 PyBuffer_Release(&self->buffer);
1091 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1092 return -1;
1093 self->input_buffer = self->buffer.buf;
1094 self->input_len = self->buffer.len;
1095 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001096 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001097 return self->input_len;
1098}
1099
Antoine Pitrou04248a82010-10-12 20:51:21 +00001100static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001101bad_readline(void)
1102{
1103 PickleState *st = _Pickle_GetGlobalState();
1104 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1105 return -1;
1106}
1107
1108static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001109_Unpickler_SkipConsumed(UnpicklerObject *self)
1110{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001111 Py_ssize_t consumed;
1112 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001113
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001114 consumed = self->next_read_idx - self->prefetched_idx;
1115 if (consumed <= 0)
1116 return 0;
1117
1118 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001119 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001120 r = PyObject_CallFunction(self->read, "n", consumed);
1121 if (r == NULL)
1122 return -1;
1123 Py_DECREF(r);
1124
1125 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001126 return 0;
1127}
1128
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001129static const Py_ssize_t READ_WHOLE_LINE = -1;
1130
1131/* If reading from a file, we need to only pull the bytes we need, since there
1132 may be multiple pickle objects arranged contiguously in the same input
1133 buffer.
1134
1135 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1136 bytes from the input stream/buffer.
1137
1138 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1139 failure; on success, returns the number of bytes read from the file.
1140
1141 On success, self->input_len will be 0; this is intentional so that when
1142 unpickling from a file, the "we've run out of data" code paths will trigger,
1143 causing the Unpickler to go back to the file for more data. Use the returned
1144 size to tell you how much data you can process. */
1145static Py_ssize_t
1146_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1147{
1148 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001149 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001150
1151 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001152
Antoine Pitrou04248a82010-10-12 20:51:21 +00001153 if (_Unpickler_SkipConsumed(self) < 0)
1154 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001155
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001156 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001157 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001158 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001159 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001160 PyObject *len;
1161 /* Prefetch some data without advancing the file pointer, if possible */
1162 if (self->peek && n < PREFETCH) {
1163 len = PyLong_FromSsize_t(PREFETCH);
1164 if (len == NULL)
1165 return -1;
1166 data = _Pickle_FastCall(self->peek, len);
1167 if (data == NULL) {
1168 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1169 return -1;
1170 /* peek() is probably not supported by the given file object */
1171 PyErr_Clear();
1172 Py_CLEAR(self->peek);
1173 }
1174 else {
1175 read_size = _Unpickler_SetStringInput(self, data);
1176 Py_DECREF(data);
1177 self->prefetched_idx = 0;
1178 if (n <= read_size)
1179 return n;
1180 }
1181 }
1182 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001183 if (len == NULL)
1184 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001185 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001186 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001187 if (data == NULL)
1188 return -1;
1189
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001190 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001191 Py_DECREF(data);
1192 return read_size;
1193}
1194
Victor Stinner19ed27e2016-05-20 11:42:37 +02001195/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001196static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001197_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001198{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001199 Py_ssize_t num_read;
1200
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001201 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001202 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1203 PickleState *st = _Pickle_GetGlobalState();
1204 PyErr_SetString(st->UnpicklingError,
1205 "read would overflow (invalid bytecode)");
1206 return -1;
1207 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001208
1209 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1210 assert(self->next_read_idx + n > self->input_len);
1211
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001212 if (!self->read)
1213 return bad_readline();
1214
Antoine Pitrou04248a82010-10-12 20:51:21 +00001215 num_read = _Unpickler_ReadFromFile(self, n);
1216 if (num_read < 0)
1217 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001218 if (num_read < n)
1219 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001220 *s = self->input_buffer;
1221 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001222 return n;
1223}
1224
Victor Stinner19ed27e2016-05-20 11:42:37 +02001225/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1226
1227 This should be used for all data reads, rather than accessing the unpickler's
1228 input buffer directly. This method deals correctly with reading from input
1229 streams, which the input buffer doesn't deal with.
1230
1231 Note that when reading from a file-like object, self->next_read_idx won't
1232 be updated (it should remain at 0 for the entire unpickling process). You
1233 should use this function's return value to know how many bytes you can
1234 consume.
1235
1236 Returns -1 (with an exception set) on failure. On success, return the
1237 number of chars read. */
1238#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001239 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001240 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1241 (self)->next_read_idx += (n), \
1242 (n)) \
1243 : _Unpickler_ReadImpl(self, (s), (n)))
1244
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001245static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001246_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1247 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001248{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001249 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001250 if (input_line == NULL) {
1251 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001252 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001254
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001255 memcpy(input_line, line, len);
1256 input_line[len] = '\0';
1257 self->input_line = input_line;
1258 *result = self->input_line;
1259 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001260}
1261
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001262/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001263 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001264
1265 Returns the number of chars read, or -1 on failure. */
1266static Py_ssize_t
1267_Unpickler_Readline(UnpicklerObject *self, char **result)
1268{
1269 Py_ssize_t i, num_read;
1270
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001271 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001272 if (self->input_buffer[i] == '\n') {
1273 char *line_start = self->input_buffer + self->next_read_idx;
1274 num_read = i - self->next_read_idx + 1;
1275 self->next_read_idx = i + 1;
1276 return _Unpickler_CopyLine(self, line_start, num_read, result);
1277 }
1278 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001279 if (!self->read)
1280 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001281
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001282 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1283 if (num_read < 0)
1284 return -1;
1285 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1286 return bad_readline();
1287 self->next_read_idx = num_read;
1288 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001289}
1290
1291/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1292 will be modified in place. */
1293static int
1294_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1295{
1296 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001297
1298 assert(new_size > self->memo_size);
1299
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001300 PyMem_RESIZE(self->memo, PyObject *, new_size);
1301 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001302 PyErr_NoMemory();
1303 return -1;
1304 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001305 for (i = self->memo_size; i < new_size; i++)
1306 self->memo[i] = NULL;
1307 self->memo_size = new_size;
1308 return 0;
1309}
1310
1311/* Returns NULL if idx is out of bounds. */
1312static PyObject *
1313_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1314{
1315 if (idx < 0 || idx >= self->memo_size)
1316 return NULL;
1317
1318 return self->memo[idx];
1319}
1320
1321/* Returns -1 (with an exception set) on failure, 0 on success.
1322 This takes its own reference to `value`. */
1323static int
1324_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1325{
1326 PyObject *old_item;
1327
1328 if (idx >= self->memo_size) {
1329 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1330 return -1;
1331 assert(idx < self->memo_size);
1332 }
1333 Py_INCREF(value);
1334 old_item = self->memo[idx];
1335 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001336 if (old_item != NULL) {
1337 Py_DECREF(old_item);
1338 }
1339 else {
1340 self->memo_len++;
1341 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001342 return 0;
1343}
1344
1345static PyObject **
1346_Unpickler_NewMemo(Py_ssize_t new_size)
1347{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001348 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001349 if (memo == NULL) {
1350 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001351 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001352 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001353 memset(memo, 0, new_size * sizeof(PyObject *));
1354 return memo;
1355}
1356
1357/* Free the unpickler's memo, taking care to decref any items left in it. */
1358static void
1359_Unpickler_MemoCleanup(UnpicklerObject *self)
1360{
1361 Py_ssize_t i;
1362 PyObject **memo = self->memo;
1363
1364 if (self->memo == NULL)
1365 return;
1366 self->memo = NULL;
1367 i = self->memo_size;
1368 while (--i >= 0) {
1369 Py_XDECREF(memo[i]);
1370 }
1371 PyMem_FREE(memo);
1372}
1373
1374static UnpicklerObject *
1375_Unpickler_New(void)
1376{
1377 UnpicklerObject *self;
1378
1379 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1380 if (self == NULL)
1381 return NULL;
1382
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001383 self->pers_func = NULL;
1384 self->input_buffer = NULL;
1385 self->input_line = NULL;
1386 self->input_len = 0;
1387 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001388 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389 self->read = NULL;
1390 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001391 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001392 self->encoding = NULL;
1393 self->errors = NULL;
1394 self->marks = NULL;
1395 self->num_marks = 0;
1396 self->marks_size = 0;
1397 self->proto = 0;
1398 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001399 memset(&self->buffer, 0, sizeof(Py_buffer));
1400 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001401 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001402 self->memo = _Unpickler_NewMemo(self->memo_size);
1403 self->stack = (Pdata *)Pdata_New();
1404
1405 if (self->memo == NULL || self->stack == NULL) {
1406 Py_DECREF(self);
1407 return NULL;
1408 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001409
1410 return self;
1411}
1412
1413/* Returns -1 (with an exception set) on failure, 0 on success. This may
1414 be called once on a freshly created Pickler. */
1415static int
1416_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1417{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001418 _Py_IDENTIFIER(peek);
1419 _Py_IDENTIFIER(read);
1420 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001421
1422 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001423 if (self->peek == NULL) {
1424 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1425 PyErr_Clear();
1426 else
1427 return -1;
1428 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001429 self->read = _PyObject_GetAttrId(file, &PyId_read);
1430 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001431 if (self->readline == NULL || self->read == NULL) {
1432 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1433 PyErr_SetString(PyExc_TypeError,
1434 "file must have 'read' and 'readline' attributes");
1435 Py_CLEAR(self->read);
1436 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001437 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001438 return -1;
1439 }
1440 return 0;
1441}
1442
1443/* Returns -1 (with an exception set) on failure, 0 on success. This may
1444 be called once on a freshly created Pickler. */
1445static int
1446_Unpickler_SetInputEncoding(UnpicklerObject *self,
1447 const char *encoding,
1448 const char *errors)
1449{
1450 if (encoding == NULL)
1451 encoding = "ASCII";
1452 if (errors == NULL)
1453 errors = "strict";
1454
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001455 self->encoding = _PyMem_Strdup(encoding);
1456 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001457 if (self->encoding == NULL || self->errors == NULL) {
1458 PyErr_NoMemory();
1459 return -1;
1460 }
1461 return 0;
1462}
1463
1464/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001465static int
1466memo_get(PicklerObject *self, PyObject *key)
1467{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001468 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001469 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001470 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001471
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001472 value = PyMemoTable_Get(self->memo, key);
1473 if (value == NULL) {
1474 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001475 return -1;
1476 }
1477
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001478 if (!self->bin) {
1479 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001480 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1481 "%" PY_FORMAT_SIZE_T "d\n", *value);
1482 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001483 }
1484 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001485 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001486 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001487 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001488 len = 2;
1489 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001490 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001491 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001492 pdata[1] = (unsigned char)(*value & 0xff);
1493 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1494 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1495 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001496 len = 5;
1497 }
1498 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001499 PickleState *st = _Pickle_GetGlobalState();
1500 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001501 "memo id too large for LONG_BINGET");
1502 return -1;
1503 }
1504 }
1505
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001506 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001507 return -1;
1508
1509 return 0;
1510}
1511
1512/* Store an object in the memo, assign it a new unique ID based on the number
1513 of objects currently stored in the memo and generate a PUT opcode. */
1514static int
1515memo_put(PicklerObject *self, PyObject *obj)
1516{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001517 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001518 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001519 Py_ssize_t idx;
1520
1521 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001522
1523 if (self->fast)
1524 return 0;
1525
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001526 idx = PyMemoTable_Size(self->memo);
1527 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1528 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001529
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001530 if (self->proto >= 4) {
1531 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1532 return -1;
1533 return 0;
1534 }
1535 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001536 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001537 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001538 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001539 len = strlen(pdata);
1540 }
1541 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001542 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001543 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001544 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001545 len = 2;
1546 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001547 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001548 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001549 pdata[1] = (unsigned char)(idx & 0xff);
1550 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1551 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1552 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001553 len = 5;
1554 }
1555 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001556 PickleState *st = _Pickle_GetGlobalState();
1557 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558 "memo id too large for LONG_BINPUT");
1559 return -1;
1560 }
1561 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001562 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001563 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001564
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001565 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001566}
1567
1568static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001569get_dotted_path(PyObject *obj, PyObject *name)
1570{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001571 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001572 PyObject *dotted_path;
1573 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001574
1575 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001576 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001577 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001578 n = PyList_GET_SIZE(dotted_path);
1579 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001580 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001581 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001582 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001583 if (obj == NULL)
1584 PyErr_Format(PyExc_AttributeError,
1585 "Can't pickle local object %R", name);
1586 else
1587 PyErr_Format(PyExc_AttributeError,
1588 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001589 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001590 return NULL;
1591 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001592 }
1593 return dotted_path;
1594}
1595
1596static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001597get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001598{
1599 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001600 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001601
1602 assert(PyList_CheckExact(names));
1603 Py_INCREF(obj);
1604 n = PyList_GET_SIZE(names);
1605 for (i = 0; i < n; i++) {
1606 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001607 Py_XDECREF(parent);
1608 parent = obj;
1609 obj = PyObject_GetAttr(parent, name);
1610 if (obj == NULL) {
1611 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001612 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001613 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001614 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001615 if (pparent != NULL)
1616 *pparent = parent;
1617 else
1618 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001619 return obj;
1620}
1621
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001622static void
1623reformat_attribute_error(PyObject *obj, PyObject *name)
1624{
1625 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1626 PyErr_Clear();
1627 PyErr_Format(PyExc_AttributeError,
1628 "Can't get attribute %R on %R", name, obj);
1629 }
1630}
1631
1632
1633static PyObject *
1634getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1635{
1636 PyObject *dotted_path, *attr;
1637
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001638 if (allow_qualname) {
1639 dotted_path = get_dotted_path(obj, name);
1640 if (dotted_path == NULL)
1641 return NULL;
1642 attr = get_deep_attribute(obj, dotted_path, NULL);
1643 Py_DECREF(dotted_path);
1644 }
1645 else
1646 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001647 if (attr == NULL)
1648 reformat_attribute_error(obj, name);
1649 return attr;
1650}
1651
Eric Snow3f9eee62017-09-15 16:35:20 -06001652static int
1653_checkmodule(PyObject *module_name, PyObject *module,
1654 PyObject *global, PyObject *dotted_path)
1655{
1656 if (module == Py_None) {
1657 return -1;
1658 }
1659 if (PyUnicode_Check(module_name) &&
1660 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1661 return -1;
1662 }
1663
1664 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1665 if (candidate == NULL) {
1666 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1667 PyErr_Clear();
1668 }
1669 return -1;
1670 }
1671 if (candidate != global) {
1672 Py_DECREF(candidate);
1673 return -1;
1674 }
1675 Py_DECREF(candidate);
1676 return 0;
1677}
1678
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001679static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001680whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001681{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001682 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001683 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001684 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001685 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001686 _Py_IDENTIFIER(__module__);
1687 _Py_IDENTIFIER(modules);
1688 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001689
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001690 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1691
1692 if (module_name == NULL) {
1693 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001694 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001696 }
1697 else {
1698 /* In some rare cases (e.g., bound methods of extension types),
1699 __module__ can be None. If it is so, then search sys.modules for
1700 the module of global. */
1701 if (module_name != Py_None)
1702 return module_name;
1703 Py_CLEAR(module_name);
1704 }
1705 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001706
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001707 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001708 modules = _PySys_GetObjectId(&PyId_modules);
1709 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001710 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001711 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001712 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001713 if (PyDict_CheckExact(modules)) {
1714 i = 0;
1715 while (PyDict_Next(modules, &i, &module_name, &module)) {
1716 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1717 Py_INCREF(module_name);
1718 return module_name;
1719 }
1720 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001721 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001722 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001723 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001724 }
1725 else {
1726 PyObject *iterator = PyObject_GetIter(modules);
1727 if (iterator == NULL) {
1728 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001729 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001730 while ((module_name = PyIter_Next(iterator))) {
1731 module = PyObject_GetItem(modules, module_name);
1732 if (module == NULL) {
1733 Py_DECREF(module_name);
1734 Py_DECREF(iterator);
1735 return NULL;
1736 }
1737 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1738 Py_DECREF(module);
1739 Py_DECREF(iterator);
1740 return module_name;
1741 }
1742 Py_DECREF(module);
1743 Py_DECREF(module_name);
1744 if (PyErr_Occurred()) {
1745 Py_DECREF(iterator);
1746 return NULL;
1747 }
1748 }
1749 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001750 }
1751
1752 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001753 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001754 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001755 return module_name;
1756}
1757
1758/* fast_save_enter() and fast_save_leave() are guards against recursive
1759 objects when Pickler is used with the "fast mode" (i.e., with object
1760 memoization disabled). If the nesting of a list or dict object exceed
1761 FAST_NESTING_LIMIT, these guards will start keeping an internal
1762 reference to the seen list or dict objects and check whether these objects
1763 are recursive. These are not strictly necessary, since save() has a
1764 hard-coded recursion limit, but they give a nicer error message than the
1765 typical RuntimeError. */
1766static int
1767fast_save_enter(PicklerObject *self, PyObject *obj)
1768{
1769 /* if fast_nesting < 0, we're doing an error exit. */
1770 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1771 PyObject *key = NULL;
1772 if (self->fast_memo == NULL) {
1773 self->fast_memo = PyDict_New();
1774 if (self->fast_memo == NULL) {
1775 self->fast_nesting = -1;
1776 return 0;
1777 }
1778 }
1779 key = PyLong_FromVoidPtr(obj);
1780 if (key == NULL)
1781 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001782 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001783 Py_DECREF(key);
1784 PyErr_Format(PyExc_ValueError,
1785 "fast mode: can't pickle cyclic objects "
1786 "including object type %.200s at %p",
1787 obj->ob_type->tp_name, obj);
1788 self->fast_nesting = -1;
1789 return 0;
1790 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001791 if (PyErr_Occurred()) {
1792 return 0;
1793 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001794 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1795 Py_DECREF(key);
1796 self->fast_nesting = -1;
1797 return 0;
1798 }
1799 Py_DECREF(key);
1800 }
1801 return 1;
1802}
1803
1804static int
1805fast_save_leave(PicklerObject *self, PyObject *obj)
1806{
1807 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1808 PyObject *key = PyLong_FromVoidPtr(obj);
1809 if (key == NULL)
1810 return 0;
1811 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1812 Py_DECREF(key);
1813 return 0;
1814 }
1815 Py_DECREF(key);
1816 }
1817 return 1;
1818}
1819
1820static int
1821save_none(PicklerObject *self, PyObject *obj)
1822{
1823 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001824 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001825 return -1;
1826
1827 return 0;
1828}
1829
1830static int
1831save_bool(PicklerObject *self, PyObject *obj)
1832{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001833 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001834 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001835 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001836 return -1;
1837 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001838 else {
1839 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1840 * so that unpicklers written before bools were introduced unpickle them
1841 * as ints, but unpicklers after can recognize that bools were intended.
1842 * Note that protocol 2 added direct ways to pickle bools.
1843 */
1844 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1845 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1846 return -1;
1847 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001848 return 0;
1849}
1850
1851static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001852save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001853{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001854 PyObject *repr = NULL;
1855 Py_ssize_t size;
1856 long val;
1857 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001858
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001859 const char long_op = LONG;
1860
1861 val= PyLong_AsLong(obj);
1862 if (val == -1 && PyErr_Occurred()) {
1863 /* out of range for int pickling */
1864 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001866 else if (self->bin &&
1867 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001868 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001869 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001870
1871 Note: we can't use -0x80000000L in the above condition because some
1872 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1873 before applying the unary minus when sizeof(long) <= 4. The
1874 resulting value stays unsigned which is commonly not what we want,
1875 so MSVC happily warns us about it. However, that result would have
1876 been fine because we guard for sizeof(long) <= 4 which turns the
1877 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001878 char pdata[32];
1879 Py_ssize_t len = 0;
1880
1881 pdata[1] = (unsigned char)(val & 0xff);
1882 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1883 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1884 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001885
1886 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1887 if (pdata[2] == 0) {
1888 pdata[0] = BININT1;
1889 len = 2;
1890 }
1891 else {
1892 pdata[0] = BININT2;
1893 len = 3;
1894 }
1895 }
1896 else {
1897 pdata[0] = BININT;
1898 len = 5;
1899 }
1900
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001901 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001902 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001903
1904 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001905 }
1906
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001907 if (self->proto >= 2) {
1908 /* Linear-time pickling. */
1909 size_t nbits;
1910 size_t nbytes;
1911 unsigned char *pdata;
1912 char header[5];
1913 int i;
1914 int sign = _PyLong_Sign(obj);
1915
1916 if (sign == 0) {
1917 header[0] = LONG1;
1918 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001919 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001920 goto error;
1921 return 0;
1922 }
1923 nbits = _PyLong_NumBits(obj);
1924 if (nbits == (size_t)-1 && PyErr_Occurred())
1925 goto error;
1926 /* How many bytes do we need? There are nbits >> 3 full
1927 * bytes of data, and nbits & 7 leftover bits. If there
1928 * are any leftover bits, then we clearly need another
1929 * byte. Wnat's not so obvious is that we *probably*
1930 * need another byte even if there aren't any leftovers:
1931 * the most-significant bit of the most-significant byte
1932 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001933 * opposite of the one we need. The exception is ints
1934 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001935 * its own 256's-complement, so has the right sign bit
1936 * even without the extra byte. That's a pain to check
1937 * for in advance, though, so we always grab an extra
1938 * byte at the start, and cut it back later if possible.
1939 */
1940 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001941 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001942 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001943 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001944 goto error;
1945 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001946 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001947 if (repr == NULL)
1948 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001949 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001950 i = _PyLong_AsByteArray((PyLongObject *)obj,
1951 pdata, nbytes,
1952 1 /* little endian */ , 1 /* signed */ );
1953 if (i < 0)
1954 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001955 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001956 * needed. This is so iff the MSB is all redundant sign
1957 * bits.
1958 */
1959 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001960 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001961 pdata[nbytes - 1] == 0xff &&
1962 (pdata[nbytes - 2] & 0x80) != 0) {
1963 nbytes--;
1964 }
1965
1966 if (nbytes < 256) {
1967 header[0] = LONG1;
1968 header[1] = (unsigned char)nbytes;
1969 size = 2;
1970 }
1971 else {
1972 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001973 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001974 for (i = 1; i < 5; i++) {
1975 header[i] = (unsigned char)(size & 0xff);
1976 size >>= 8;
1977 }
1978 size = 5;
1979 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001980 if (_Pickler_Write(self, header, size) < 0 ||
1981 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001982 goto error;
1983 }
1984 else {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001985 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001986
Mark Dickinson8dd05142009-01-20 20:43:58 +00001987 /* proto < 2: write the repr and newline. This is quadratic-time (in
1988 the number of digits), in both directions. We add a trailing 'L'
1989 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001990
1991 repr = PyObject_Repr(obj);
1992 if (repr == NULL)
1993 goto error;
1994
Serhiy Storchaka06515832016-11-20 09:13:07 +02001995 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001996 if (string == NULL)
1997 goto error;
1998
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001999 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2000 _Pickler_Write(self, string, size) < 0 ||
2001 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002002 goto error;
2003 }
2004
2005 if (0) {
2006 error:
2007 status = -1;
2008 }
2009 Py_XDECREF(repr);
2010
2011 return status;
2012}
2013
2014static int
2015save_float(PicklerObject *self, PyObject *obj)
2016{
2017 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2018
2019 if (self->bin) {
2020 char pdata[9];
2021 pdata[0] = BINFLOAT;
2022 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2023 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002024 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002025 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002026 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002027 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002028 int result = -1;
2029 char *buf = NULL;
2030 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002031
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002032 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002033 goto done;
2034
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002035 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002036 if (!buf) {
2037 PyErr_NoMemory();
2038 goto done;
2039 }
2040
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002041 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002042 goto done;
2043
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002044 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002045 goto done;
2046
2047 result = 0;
2048done:
2049 PyMem_Free(buf);
2050 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002051 }
2052
2053 return 0;
2054}
2055
2056static int
2057save_bytes(PicklerObject *self, PyObject *obj)
2058{
2059 if (self->proto < 3) {
2060 /* Older pickle protocols do not have an opcode for pickling bytes
2061 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002062 the __reduce__ method) to permit bytes object unpickling.
2063
2064 Here we use a hack to be compatible with Python 2. Since in Python
2065 2 'bytes' is just an alias for 'str' (which has different
2066 parameters than the actual bytes object), we use codecs.encode
2067 to create the appropriate 'str' object when unpickled using
2068 Python 2 *and* the appropriate 'bytes' object when unpickled
2069 using Python 3. Again this is a hack and we don't need to do this
2070 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002071 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002072 int status;
2073
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002074 if (PyBytes_GET_SIZE(obj) == 0) {
2075 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2076 }
2077 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002078 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002079 PyObject *unicode_str =
2080 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2081 PyBytes_GET_SIZE(obj),
2082 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002083 _Py_IDENTIFIER(latin1);
2084
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002085 if (unicode_str == NULL)
2086 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002087 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002088 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002089 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002090 Py_DECREF(unicode_str);
2091 }
2092
2093 if (reduce_value == NULL)
2094 return -1;
2095
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 /* save_reduce() will memoize the object automatically. */
2097 status = save_reduce(self, reduce_value, obj);
2098 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 return status;
2100 }
2101 else {
2102 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002103 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002104 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002105
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002106 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002107 if (size < 0)
2108 return -1;
2109
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002110 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002111 header[0] = SHORT_BINBYTES;
2112 header[1] = (unsigned char)size;
2113 len = 2;
2114 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002115 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002116 header[0] = BINBYTES;
2117 header[1] = (unsigned char)(size & 0xff);
2118 header[2] = (unsigned char)((size >> 8) & 0xff);
2119 header[3] = (unsigned char)((size >> 16) & 0xff);
2120 header[4] = (unsigned char)((size >> 24) & 0xff);
2121 len = 5;
2122 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002123 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002124 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002125 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002126 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002127 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002128 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002129 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002130 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002131 return -1; /* string too large */
2132 }
2133
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002134 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002135 return -1;
2136
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002137 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002138 return -1;
2139
2140 if (memo_put(self, obj) < 0)
2141 return -1;
2142
2143 return 0;
2144 }
2145}
2146
2147/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2148 backslash and newline characters to \uXXXX escapes. */
2149static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002150raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002151{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002152 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002153 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002154 void *data;
2155 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002156 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002157
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002158 if (PyUnicode_READY(obj))
2159 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002160
Victor Stinner358af132015-10-12 22:36:57 +02002161 _PyBytesWriter_Init(&writer);
2162
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002163 size = PyUnicode_GET_LENGTH(obj);
2164 data = PyUnicode_DATA(obj);
2165 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002166
Victor Stinner358af132015-10-12 22:36:57 +02002167 p = _PyBytesWriter_Alloc(&writer, size);
2168 if (p == NULL)
2169 goto error;
2170 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002171
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002172 for (i=0; i < size; i++) {
2173 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002174 /* Map 32-bit characters to '\Uxxxxxxxx' */
2175 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002176 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002177 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2178 if (p == NULL)
2179 goto error;
2180
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002181 *p++ = '\\';
2182 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002183 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2184 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2185 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2186 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2187 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2188 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2189 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2190 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002191 }
Victor Stinner358af132015-10-12 22:36:57 +02002192 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002193 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002194 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002195 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2196 if (p == NULL)
2197 goto error;
2198
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002199 *p++ = '\\';
2200 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002201 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2202 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2203 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2204 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002205 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002206 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002207 else
2208 *p++ = (char) ch;
2209 }
Victor Stinner358af132015-10-12 22:36:57 +02002210
2211 return _PyBytesWriter_Finish(&writer, p);
2212
2213error:
2214 _PyBytesWriter_Dealloc(&writer);
2215 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002216}
2217
2218static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002219write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002220{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002221 char header[9];
2222 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002223
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002224 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002225 if (size <= 0xff && self->proto >= 4) {
2226 header[0] = SHORT_BINUNICODE;
2227 header[1] = (unsigned char)(size & 0xff);
2228 len = 2;
2229 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002230 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002231 header[0] = BINUNICODE;
2232 header[1] = (unsigned char)(size & 0xff);
2233 header[2] = (unsigned char)((size >> 8) & 0xff);
2234 header[3] = (unsigned char)((size >> 16) & 0xff);
2235 header[4] = (unsigned char)((size >> 24) & 0xff);
2236 len = 5;
2237 }
2238 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002239 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002240 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002241 len = 9;
2242 }
2243 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002244 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002245 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002246 return -1;
2247 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002248
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002249 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002250 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002251 if (_Pickler_Write(self, data, size) < 0)
2252 return -1;
2253
2254 return 0;
2255}
2256
2257static int
2258write_unicode_binary(PicklerObject *self, PyObject *obj)
2259{
2260 PyObject *encoded = NULL;
2261 Py_ssize_t size;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002262 const char *data;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002263 int r;
2264
2265 if (PyUnicode_READY(obj))
2266 return -1;
2267
2268 data = PyUnicode_AsUTF8AndSize(obj, &size);
2269 if (data != NULL)
2270 return write_utf8(self, data, size);
2271
2272 /* Issue #8383: for strings with lone surrogates, fallback on the
2273 "surrogatepass" error handler. */
2274 PyErr_Clear();
2275 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2276 if (encoded == NULL)
2277 return -1;
2278
2279 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2280 PyBytes_GET_SIZE(encoded));
2281 Py_DECREF(encoded);
2282 return r;
2283}
2284
2285static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002286save_unicode(PicklerObject *self, PyObject *obj)
2287{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002288 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002289 if (write_unicode_binary(self, obj) < 0)
2290 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002291 }
2292 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002293 PyObject *encoded;
2294 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295 const char unicode_op = UNICODE;
2296
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002297 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002298 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002299 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002300
Antoine Pitrou299978d2013-04-07 17:38:11 +02002301 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2302 Py_DECREF(encoded);
2303 return -1;
2304 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002305
2306 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002307 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2308 Py_DECREF(encoded);
2309 return -1;
2310 }
2311 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002312
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002313 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002314 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002315 }
2316 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002317 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002318
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002319 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002320}
2321
2322/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2323static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002324store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002325{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002326 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002327
2328 assert(PyTuple_Size(t) == len);
2329
2330 for (i = 0; i < len; i++) {
2331 PyObject *element = PyTuple_GET_ITEM(t, i);
2332
2333 if (element == NULL)
2334 return -1;
2335 if (save(self, element, 0) < 0)
2336 return -1;
2337 }
2338
2339 return 0;
2340}
2341
2342/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2343 * used across protocols to minimize the space needed to pickle them.
2344 * Tuples are also the only builtin immutable type that can be recursive
2345 * (a tuple can be reached from itself), and that requires some subtle
2346 * magic so that it works in all cases. IOW, this is a long routine.
2347 */
2348static int
2349save_tuple(PicklerObject *self, PyObject *obj)
2350{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002351 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002352
2353 const char mark_op = MARK;
2354 const char tuple_op = TUPLE;
2355 const char pop_op = POP;
2356 const char pop_mark_op = POP_MARK;
2357 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2358
2359 if ((len = PyTuple_Size(obj)) < 0)
2360 return -1;
2361
2362 if (len == 0) {
2363 char pdata[2];
2364
2365 if (self->proto) {
2366 pdata[0] = EMPTY_TUPLE;
2367 len = 1;
2368 }
2369 else {
2370 pdata[0] = MARK;
2371 pdata[1] = TUPLE;
2372 len = 2;
2373 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002374 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002375 return -1;
2376 return 0;
2377 }
2378
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002379 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002380 * saving the tuple elements, the tuple must be recursive, in
2381 * which case we'll pop everything we put on the stack, and fetch
2382 * its value from the memo.
2383 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002384 if (len <= 3 && self->proto >= 2) {
2385 /* Use TUPLE{1,2,3} opcodes. */
2386 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002387 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002388
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002389 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002390 /* pop the len elements */
2391 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002392 if (_Pickler_Write(self, &pop_op, 1) < 0)
2393 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002394 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002395 if (memo_get(self, obj) < 0)
2396 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002397
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002398 return 0;
2399 }
2400 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002401 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2402 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002403 }
2404 goto memoize;
2405 }
2406
2407 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2408 * Generate MARK e1 e2 ... TUPLE
2409 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002410 if (_Pickler_Write(self, &mark_op, 1) < 0)
2411 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002412
2413 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002414 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002415
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002416 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002417 /* pop the stack stuff we pushed */
2418 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002419 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2420 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002421 }
2422 else {
2423 /* Note that we pop one more than len, to remove
2424 * the MARK too.
2425 */
2426 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002427 if (_Pickler_Write(self, &pop_op, 1) < 0)
2428 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002429 }
2430 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002431 if (memo_get(self, obj) < 0)
2432 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002433
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002434 return 0;
2435 }
2436 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002437 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2438 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002439 }
2440
2441 memoize:
2442 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002443 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002444
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002445 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002446}
2447
2448/* iter is an iterator giving items, and we batch up chunks of
2449 * MARK item item ... item APPENDS
2450 * opcode sequences. Calling code should have arranged to first create an
2451 * empty list, or list-like object, for the APPENDS to operate on.
2452 * Returns 0 on success, <0 on error.
2453 */
2454static int
2455batch_list(PicklerObject *self, PyObject *iter)
2456{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002457 PyObject *obj = NULL;
2458 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002459 int i, n;
2460
2461 const char mark_op = MARK;
2462 const char append_op = APPEND;
2463 const char appends_op = APPENDS;
2464
2465 assert(iter != NULL);
2466
2467 /* XXX: I think this function could be made faster by avoiding the
2468 iterator interface and fetching objects directly from list using
2469 PyList_GET_ITEM.
2470 */
2471
2472 if (self->proto == 0) {
2473 /* APPENDS isn't available; do one at a time. */
2474 for (;;) {
2475 obj = PyIter_Next(iter);
2476 if (obj == NULL) {
2477 if (PyErr_Occurred())
2478 return -1;
2479 break;
2480 }
2481 i = save(self, obj, 0);
2482 Py_DECREF(obj);
2483 if (i < 0)
2484 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002485 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002486 return -1;
2487 }
2488 return 0;
2489 }
2490
2491 /* proto > 0: write in batches of BATCHSIZE. */
2492 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002493 /* Get first item */
2494 firstitem = PyIter_Next(iter);
2495 if (firstitem == NULL) {
2496 if (PyErr_Occurred())
2497 goto error;
2498
2499 /* nothing more to add */
2500 break;
2501 }
2502
2503 /* Try to get a second item */
2504 obj = PyIter_Next(iter);
2505 if (obj == NULL) {
2506 if (PyErr_Occurred())
2507 goto error;
2508
2509 /* Only one item to write */
2510 if (save(self, firstitem, 0) < 0)
2511 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002512 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002513 goto error;
2514 Py_CLEAR(firstitem);
2515 break;
2516 }
2517
2518 /* More than one item to write */
2519
2520 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002521 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002522 goto error;
2523
2524 if (save(self, firstitem, 0) < 0)
2525 goto error;
2526 Py_CLEAR(firstitem);
2527 n = 1;
2528
2529 /* Fetch and save up to BATCHSIZE items */
2530 while (obj) {
2531 if (save(self, obj, 0) < 0)
2532 goto error;
2533 Py_CLEAR(obj);
2534 n += 1;
2535
2536 if (n == BATCHSIZE)
2537 break;
2538
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002539 obj = PyIter_Next(iter);
2540 if (obj == NULL) {
2541 if (PyErr_Occurred())
2542 goto error;
2543 break;
2544 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002545 }
2546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002547 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002548 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002549
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002550 } while (n == BATCHSIZE);
2551 return 0;
2552
2553 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002554 Py_XDECREF(firstitem);
2555 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556 return -1;
2557}
2558
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002559/* This is a variant of batch_list() above, specialized for lists (with no
2560 * support for list subclasses). Like batch_list(), we batch up chunks of
2561 * MARK item item ... item APPENDS
2562 * opcode sequences. Calling code should have arranged to first create an
2563 * empty list, or list-like object, for the APPENDS to operate on.
2564 * Returns 0 on success, -1 on error.
2565 *
2566 * This version is considerably faster than batch_list(), if less general.
2567 *
2568 * Note that this only works for protocols > 0.
2569 */
2570static int
2571batch_list_exact(PicklerObject *self, PyObject *obj)
2572{
2573 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002574 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002575
2576 const char append_op = APPEND;
2577 const char appends_op = APPENDS;
2578 const char mark_op = MARK;
2579
2580 assert(obj != NULL);
2581 assert(self->proto > 0);
2582 assert(PyList_CheckExact(obj));
2583
2584 if (PyList_GET_SIZE(obj) == 1) {
2585 item = PyList_GET_ITEM(obj, 0);
2586 if (save(self, item, 0) < 0)
2587 return -1;
2588 if (_Pickler_Write(self, &append_op, 1) < 0)
2589 return -1;
2590 return 0;
2591 }
2592
2593 /* Write in batches of BATCHSIZE. */
2594 total = 0;
2595 do {
2596 this_batch = 0;
2597 if (_Pickler_Write(self, &mark_op, 1) < 0)
2598 return -1;
2599 while (total < PyList_GET_SIZE(obj)) {
2600 item = PyList_GET_ITEM(obj, total);
2601 if (save(self, item, 0) < 0)
2602 return -1;
2603 total++;
2604 if (++this_batch == BATCHSIZE)
2605 break;
2606 }
2607 if (_Pickler_Write(self, &appends_op, 1) < 0)
2608 return -1;
2609
2610 } while (total < PyList_GET_SIZE(obj));
2611
2612 return 0;
2613}
2614
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002615static int
2616save_list(PicklerObject *self, PyObject *obj)
2617{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002618 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002619 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002620 int status = 0;
2621
2622 if (self->fast && !fast_save_enter(self, obj))
2623 goto error;
2624
2625 /* Create an empty list. */
2626 if (self->bin) {
2627 header[0] = EMPTY_LIST;
2628 len = 1;
2629 }
2630 else {
2631 header[0] = MARK;
2632 header[1] = LIST;
2633 len = 2;
2634 }
2635
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002636 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002637 goto error;
2638
2639 /* Get list length, and bow out early if empty. */
2640 if ((len = PyList_Size(obj)) < 0)
2641 goto error;
2642
2643 if (memo_put(self, obj) < 0)
2644 goto error;
2645
2646 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002647 /* Materialize the list elements. */
2648 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002649 if (Py_EnterRecursiveCall(" while pickling an object"))
2650 goto error;
2651 status = batch_list_exact(self, obj);
2652 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002653 } else {
2654 PyObject *iter = PyObject_GetIter(obj);
2655 if (iter == NULL)
2656 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002657
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002658 if (Py_EnterRecursiveCall(" while pickling an object")) {
2659 Py_DECREF(iter);
2660 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002661 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002662 status = batch_list(self, iter);
2663 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002664 Py_DECREF(iter);
2665 }
2666 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002667 if (0) {
2668 error:
2669 status = -1;
2670 }
2671
2672 if (self->fast && !fast_save_leave(self, obj))
2673 status = -1;
2674
2675 return status;
2676}
2677
2678/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2679 * MARK key value ... key value SETITEMS
2680 * opcode sequences. Calling code should have arranged to first create an
2681 * empty dict, or dict-like object, for the SETITEMS to operate on.
2682 * Returns 0 on success, <0 on error.
2683 *
2684 * This is very much like batch_list(). The difference between saving
2685 * elements directly, and picking apart two-tuples, is so long-winded at
2686 * the C level, though, that attempts to combine these routines were too
2687 * ugly to bear.
2688 */
2689static int
2690batch_dict(PicklerObject *self, PyObject *iter)
2691{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002692 PyObject *obj = NULL;
2693 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002694 int i, n;
2695
2696 const char mark_op = MARK;
2697 const char setitem_op = SETITEM;
2698 const char setitems_op = SETITEMS;
2699
2700 assert(iter != NULL);
2701
2702 if (self->proto == 0) {
2703 /* SETITEMS isn't available; do one at a time. */
2704 for (;;) {
2705 obj = PyIter_Next(iter);
2706 if (obj == NULL) {
2707 if (PyErr_Occurred())
2708 return -1;
2709 break;
2710 }
2711 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2712 PyErr_SetString(PyExc_TypeError, "dict items "
2713 "iterator must return 2-tuples");
2714 return -1;
2715 }
2716 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2717 if (i >= 0)
2718 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2719 Py_DECREF(obj);
2720 if (i < 0)
2721 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002722 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002723 return -1;
2724 }
2725 return 0;
2726 }
2727
2728 /* proto > 0: write in batches of BATCHSIZE. */
2729 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002730 /* Get first item */
2731 firstitem = PyIter_Next(iter);
2732 if (firstitem == NULL) {
2733 if (PyErr_Occurred())
2734 goto error;
2735
2736 /* nothing more to add */
2737 break;
2738 }
2739 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2740 PyErr_SetString(PyExc_TypeError, "dict items "
2741 "iterator must return 2-tuples");
2742 goto error;
2743 }
2744
2745 /* Try to get a second item */
2746 obj = PyIter_Next(iter);
2747 if (obj == NULL) {
2748 if (PyErr_Occurred())
2749 goto error;
2750
2751 /* Only one item to write */
2752 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2753 goto error;
2754 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2755 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002756 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002757 goto error;
2758 Py_CLEAR(firstitem);
2759 break;
2760 }
2761
2762 /* More than one item to write */
2763
2764 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002765 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002766 goto error;
2767
2768 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2769 goto error;
2770 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2771 goto error;
2772 Py_CLEAR(firstitem);
2773 n = 1;
2774
2775 /* Fetch and save up to BATCHSIZE items */
2776 while (obj) {
2777 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2778 PyErr_SetString(PyExc_TypeError, "dict items "
2779 "iterator must return 2-tuples");
2780 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002781 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002782 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2783 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2784 goto error;
2785 Py_CLEAR(obj);
2786 n += 1;
2787
2788 if (n == BATCHSIZE)
2789 break;
2790
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002791 obj = PyIter_Next(iter);
2792 if (obj == NULL) {
2793 if (PyErr_Occurred())
2794 goto error;
2795 break;
2796 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002797 }
2798
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002799 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002800 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002801
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002802 } while (n == BATCHSIZE);
2803 return 0;
2804
2805 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002806 Py_XDECREF(firstitem);
2807 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002808 return -1;
2809}
2810
Collin Winter5c9b02d2009-05-25 05:43:30 +00002811/* This is a variant of batch_dict() above that specializes for dicts, with no
2812 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2813 * MARK key value ... key value SETITEMS
2814 * opcode sequences. Calling code should have arranged to first create an
2815 * empty dict, or dict-like object, for the SETITEMS to operate on.
2816 * Returns 0 on success, -1 on error.
2817 *
2818 * Note that this currently doesn't work for protocol 0.
2819 */
2820static int
2821batch_dict_exact(PicklerObject *self, PyObject *obj)
2822{
2823 PyObject *key = NULL, *value = NULL;
2824 int i;
2825 Py_ssize_t dict_size, ppos = 0;
2826
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002827 const char mark_op = MARK;
2828 const char setitem_op = SETITEM;
2829 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002830
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002831 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002832 assert(self->proto > 0);
2833
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002834 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002835
2836 /* Special-case len(d) == 1 to save space. */
2837 if (dict_size == 1) {
2838 PyDict_Next(obj, &ppos, &key, &value);
2839 if (save(self, key, 0) < 0)
2840 return -1;
2841 if (save(self, value, 0) < 0)
2842 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002843 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002844 return -1;
2845 return 0;
2846 }
2847
2848 /* Write in batches of BATCHSIZE. */
2849 do {
2850 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002851 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002852 return -1;
2853 while (PyDict_Next(obj, &ppos, &key, &value)) {
2854 if (save(self, key, 0) < 0)
2855 return -1;
2856 if (save(self, value, 0) < 0)
2857 return -1;
2858 if (++i == BATCHSIZE)
2859 break;
2860 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002861 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002862 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002863 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00002864 PyErr_Format(
2865 PyExc_RuntimeError,
2866 "dictionary changed size during iteration");
2867 return -1;
2868 }
2869
2870 } while (i == BATCHSIZE);
2871 return 0;
2872}
2873
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002874static int
2875save_dict(PicklerObject *self, PyObject *obj)
2876{
2877 PyObject *items, *iter;
2878 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002879 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002880 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002881 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002882
2883 if (self->fast && !fast_save_enter(self, obj))
2884 goto error;
2885
2886 /* Create an empty dict. */
2887 if (self->bin) {
2888 header[0] = EMPTY_DICT;
2889 len = 1;
2890 }
2891 else {
2892 header[0] = MARK;
2893 header[1] = DICT;
2894 len = 2;
2895 }
2896
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002897 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002898 goto error;
2899
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002900 if (memo_put(self, obj) < 0)
2901 goto error;
2902
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002903 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002904 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002905 if (PyDict_CheckExact(obj) && self->proto > 0) {
2906 /* We can take certain shortcuts if we know this is a dict and
2907 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002908 if (Py_EnterRecursiveCall(" while pickling an object"))
2909 goto error;
2910 status = batch_dict_exact(self, obj);
2911 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002912 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002913 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002914
Victor Stinnerad8c83a2016-09-05 17:53:15 -07002915 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002916 if (items == NULL)
2917 goto error;
2918 iter = PyObject_GetIter(items);
2919 Py_DECREF(items);
2920 if (iter == NULL)
2921 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002922 if (Py_EnterRecursiveCall(" while pickling an object")) {
2923 Py_DECREF(iter);
2924 goto error;
2925 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002926 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002927 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002928 Py_DECREF(iter);
2929 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002930 }
2931
2932 if (0) {
2933 error:
2934 status = -1;
2935 }
2936
2937 if (self->fast && !fast_save_leave(self, obj))
2938 status = -1;
2939
2940 return status;
2941}
2942
2943static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002944save_set(PicklerObject *self, PyObject *obj)
2945{
2946 PyObject *item;
2947 int i;
2948 Py_ssize_t set_size, ppos = 0;
2949 Py_hash_t hash;
2950
2951 const char empty_set_op = EMPTY_SET;
2952 const char mark_op = MARK;
2953 const char additems_op = ADDITEMS;
2954
2955 if (self->proto < 4) {
2956 PyObject *items;
2957 PyObject *reduce_value;
2958 int status;
2959
2960 items = PySequence_List(obj);
2961 if (items == NULL) {
2962 return -1;
2963 }
2964 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2965 Py_DECREF(items);
2966 if (reduce_value == NULL) {
2967 return -1;
2968 }
2969 /* save_reduce() will memoize the object automatically. */
2970 status = save_reduce(self, reduce_value, obj);
2971 Py_DECREF(reduce_value);
2972 return status;
2973 }
2974
2975 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2976 return -1;
2977
2978 if (memo_put(self, obj) < 0)
2979 return -1;
2980
2981 set_size = PySet_GET_SIZE(obj);
2982 if (set_size == 0)
2983 return 0; /* nothing to do */
2984
2985 /* Write in batches of BATCHSIZE. */
2986 do {
2987 i = 0;
2988 if (_Pickler_Write(self, &mark_op, 1) < 0)
2989 return -1;
2990 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2991 if (save(self, item, 0) < 0)
2992 return -1;
2993 if (++i == BATCHSIZE)
2994 break;
2995 }
2996 if (_Pickler_Write(self, &additems_op, 1) < 0)
2997 return -1;
2998 if (PySet_GET_SIZE(obj) != set_size) {
2999 PyErr_Format(
3000 PyExc_RuntimeError,
3001 "set changed size during iteration");
3002 return -1;
3003 }
3004 } while (i == BATCHSIZE);
3005
3006 return 0;
3007}
3008
3009static int
3010save_frozenset(PicklerObject *self, PyObject *obj)
3011{
3012 PyObject *iter;
3013
3014 const char mark_op = MARK;
3015 const char frozenset_op = FROZENSET;
3016
3017 if (self->fast && !fast_save_enter(self, obj))
3018 return -1;
3019
3020 if (self->proto < 4) {
3021 PyObject *items;
3022 PyObject *reduce_value;
3023 int status;
3024
3025 items = PySequence_List(obj);
3026 if (items == NULL) {
3027 return -1;
3028 }
3029 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3030 items);
3031 Py_DECREF(items);
3032 if (reduce_value == NULL) {
3033 return -1;
3034 }
3035 /* save_reduce() will memoize the object automatically. */
3036 status = save_reduce(self, reduce_value, obj);
3037 Py_DECREF(reduce_value);
3038 return status;
3039 }
3040
3041 if (_Pickler_Write(self, &mark_op, 1) < 0)
3042 return -1;
3043
3044 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003045 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003046 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003047 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003048 for (;;) {
3049 PyObject *item;
3050
3051 item = PyIter_Next(iter);
3052 if (item == NULL) {
3053 if (PyErr_Occurred()) {
3054 Py_DECREF(iter);
3055 return -1;
3056 }
3057 break;
3058 }
3059 if (save(self, item, 0) < 0) {
3060 Py_DECREF(item);
3061 Py_DECREF(iter);
3062 return -1;
3063 }
3064 Py_DECREF(item);
3065 }
3066 Py_DECREF(iter);
3067
3068 /* If the object is already in the memo, this means it is
3069 recursive. In this case, throw away everything we put on the
3070 stack, and fetch the object back from the memo. */
3071 if (PyMemoTable_Get(self->memo, obj)) {
3072 const char pop_mark_op = POP_MARK;
3073
3074 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3075 return -1;
3076 if (memo_get(self, obj) < 0)
3077 return -1;
3078 return 0;
3079 }
3080
3081 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3082 return -1;
3083 if (memo_put(self, obj) < 0)
3084 return -1;
3085
3086 return 0;
3087}
3088
3089static int
3090fix_imports(PyObject **module_name, PyObject **global_name)
3091{
3092 PyObject *key;
3093 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003094 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003095
3096 key = PyTuple_Pack(2, *module_name, *global_name);
3097 if (key == NULL)
3098 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003099 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003100 Py_DECREF(key);
3101 if (item) {
3102 PyObject *fixed_module_name;
3103 PyObject *fixed_global_name;
3104
3105 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3106 PyErr_Format(PyExc_RuntimeError,
3107 "_compat_pickle.REVERSE_NAME_MAPPING values "
3108 "should be 2-tuples, not %.200s",
3109 Py_TYPE(item)->tp_name);
3110 return -1;
3111 }
3112 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3113 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3114 if (!PyUnicode_Check(fixed_module_name) ||
3115 !PyUnicode_Check(fixed_global_name)) {
3116 PyErr_Format(PyExc_RuntimeError,
3117 "_compat_pickle.REVERSE_NAME_MAPPING values "
3118 "should be pairs of str, not (%.200s, %.200s)",
3119 Py_TYPE(fixed_module_name)->tp_name,
3120 Py_TYPE(fixed_global_name)->tp_name);
3121 return -1;
3122 }
3123
3124 Py_CLEAR(*module_name);
3125 Py_CLEAR(*global_name);
3126 Py_INCREF(fixed_module_name);
3127 Py_INCREF(fixed_global_name);
3128 *module_name = fixed_module_name;
3129 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003130 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003131 }
3132 else if (PyErr_Occurred()) {
3133 return -1;
3134 }
3135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003136 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003137 if (item) {
3138 if (!PyUnicode_Check(item)) {
3139 PyErr_Format(PyExc_RuntimeError,
3140 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3141 "should be strings, not %.200s",
3142 Py_TYPE(item)->tp_name);
3143 return -1;
3144 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003145 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003146 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003147 }
3148 else if (PyErr_Occurred()) {
3149 return -1;
3150 }
3151
3152 return 0;
3153}
3154
3155static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003156save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3157{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003158 PyObject *global_name = NULL;
3159 PyObject *module_name = NULL;
3160 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003161 PyObject *parent = NULL;
3162 PyObject *dotted_path = NULL;
3163 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003164 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003165 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003166 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003167 _Py_IDENTIFIER(__name__);
3168 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003169
3170 const char global_op = GLOBAL;
3171
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003172 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003173 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003174 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003175 }
3176 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003177 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3178 if (global_name == NULL) {
3179 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3180 goto error;
3181 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003182 }
3183 if (global_name == NULL) {
3184 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3185 if (global_name == NULL)
3186 goto error;
3187 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003188 }
3189
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003190 dotted_path = get_dotted_path(module, global_name);
3191 if (dotted_path == NULL)
3192 goto error;
3193 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003194 if (module_name == NULL)
3195 goto error;
3196
3197 /* XXX: Change to use the import C API directly with level=0 to disallow
3198 relative imports.
3199
3200 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3201 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3202 custom import functions (IMHO, this would be a nice security
3203 feature). The import C API would need to be extended to support the
3204 extra parameters of __import__ to fix that. */
3205 module = PyImport_Import(module_name);
3206 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003207 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003208 "Can't pickle %R: import of module %R failed",
3209 obj, module_name);
3210 goto error;
3211 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003212 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3213 Py_INCREF(lastname);
3214 cls = get_deep_attribute(module, dotted_path, &parent);
3215 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003216 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003217 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003218 "Can't pickle %R: attribute lookup %S on %S failed",
3219 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003220 goto error;
3221 }
3222 if (cls != obj) {
3223 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003224 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003225 "Can't pickle %R: it's not the same object as %S.%S",
3226 obj, module_name, global_name);
3227 goto error;
3228 }
3229 Py_DECREF(cls);
3230
3231 if (self->proto >= 2) {
3232 /* See whether this is in the extension registry, and if
3233 * so generate an EXT opcode.
3234 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003235 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003236 PyObject *code_obj; /* extension code as Python object */
3237 long code; /* extension code as C value */
3238 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003239 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003240
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003241 extension_key = PyTuple_Pack(2, module_name, global_name);
3242 if (extension_key == NULL) {
3243 goto error;
3244 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003245 code_obj = PyDict_GetItemWithError(st->extension_registry,
3246 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003247 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003248 /* The object is not registered in the extension registry.
3249 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003250 if (code_obj == NULL) {
3251 if (PyErr_Occurred()) {
3252 goto error;
3253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003254 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003255 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003256
3257 /* XXX: pickle.py doesn't check neither the type, nor the range
3258 of the value returned by the extension_registry. It should for
3259 consistency. */
3260
3261 /* Verify code_obj has the right type and value. */
3262 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003263 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003264 "Can't pickle %R: extension code %R isn't an integer",
3265 obj, code_obj);
3266 goto error;
3267 }
3268 code = PyLong_AS_LONG(code_obj);
3269 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003270 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003271 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3272 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003273 goto error;
3274 }
3275
3276 /* Generate an EXT opcode. */
3277 if (code <= 0xff) {
3278 pdata[0] = EXT1;
3279 pdata[1] = (unsigned char)code;
3280 n = 2;
3281 }
3282 else if (code <= 0xffff) {
3283 pdata[0] = EXT2;
3284 pdata[1] = (unsigned char)(code & 0xff);
3285 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3286 n = 3;
3287 }
3288 else {
3289 pdata[0] = EXT4;
3290 pdata[1] = (unsigned char)(code & 0xff);
3291 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3292 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3293 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3294 n = 5;
3295 }
3296
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003297 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003298 goto error;
3299 }
3300 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003301 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003302 if (parent == module) {
3303 Py_INCREF(lastname);
3304 Py_DECREF(global_name);
3305 global_name = lastname;
3306 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003307 if (self->proto >= 4) {
3308 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003309
Christian Heimese8b1ba12013-11-23 21:13:39 +01003310 if (save(self, module_name, 0) < 0)
3311 goto error;
3312 if (save(self, global_name, 0) < 0)
3313 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003314
3315 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3316 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003317 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003318 else if (parent != module) {
3319 PickleState *st = _Pickle_GetGlobalState();
3320 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3321 st->getattr, parent, lastname);
3322 status = save_reduce(self, reduce_value, NULL);
3323 Py_DECREF(reduce_value);
3324 if (status < 0)
3325 goto error;
3326 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003327 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003328 /* Generate a normal global opcode if we are using a pickle
3329 protocol < 4, or if the object is not registered in the
3330 extension registry. */
3331 PyObject *encoded;
3332 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003333
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003334 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003335 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003336
3337 /* For protocol < 3 and if the user didn't request against doing
3338 so, we convert module names to the old 2.x module names. */
3339 if (self->proto < 3 && self->fix_imports) {
3340 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003341 goto error;
3342 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003343 }
3344
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003345 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3346 both the module name and the global name using UTF-8. We do so
3347 only when we are using the pickle protocol newer than version
3348 3. This is to ensure compatibility with older Unpickler running
3349 on Python 2.x. */
3350 if (self->proto == 3) {
3351 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003352 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003353 else {
3354 unicode_encoder = PyUnicode_AsASCIIString;
3355 }
3356 encoded = unicode_encoder(module_name);
3357 if (encoded == NULL) {
3358 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003359 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003360 "can't pickle module identifier '%S' using "
3361 "pickle protocol %i",
3362 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003363 goto error;
3364 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003365 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3366 PyBytes_GET_SIZE(encoded)) < 0) {
3367 Py_DECREF(encoded);
3368 goto error;
3369 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003370 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003371 if(_Pickler_Write(self, "\n", 1) < 0)
3372 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003373
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003374 /* Save the name of the module. */
3375 encoded = unicode_encoder(global_name);
3376 if (encoded == NULL) {
3377 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003378 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003379 "can't pickle global identifier '%S' using "
3380 "pickle protocol %i",
3381 global_name, self->proto);
3382 goto error;
3383 }
3384 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3385 PyBytes_GET_SIZE(encoded)) < 0) {
3386 Py_DECREF(encoded);
3387 goto error;
3388 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003389 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003390 if (_Pickler_Write(self, "\n", 1) < 0)
3391 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003392 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003393 /* Memoize the object. */
3394 if (memo_put(self, obj) < 0)
3395 goto error;
3396 }
3397
3398 if (0) {
3399 error:
3400 status = -1;
3401 }
3402 Py_XDECREF(module_name);
3403 Py_XDECREF(global_name);
3404 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003405 Py_XDECREF(parent);
3406 Py_XDECREF(dotted_path);
3407 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003408
3409 return status;
3410}
3411
3412static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003413save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3414{
3415 PyObject *reduce_value;
3416 int status;
3417
3418 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3419 if (reduce_value == NULL) {
3420 return -1;
3421 }
3422 status = save_reduce(self, reduce_value, obj);
3423 Py_DECREF(reduce_value);
3424 return status;
3425}
3426
3427static int
3428save_type(PicklerObject *self, PyObject *obj)
3429{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003430 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003431 return save_singleton_type(self, obj, Py_None);
3432 }
3433 else if (obj == (PyObject *)&PyEllipsis_Type) {
3434 return save_singleton_type(self, obj, Py_Ellipsis);
3435 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003436 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003437 return save_singleton_type(self, obj, Py_NotImplemented);
3438 }
3439 return save_global(self, obj, NULL);
3440}
3441
3442static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003443save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3444{
3445 PyObject *pid = NULL;
3446 int status = 0;
3447
3448 const char persid_op = PERSID;
3449 const char binpersid_op = BINPERSID;
3450
3451 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003452 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003453 if (pid == NULL)
3454 return -1;
3455
3456 if (pid != Py_None) {
3457 if (self->bin) {
3458 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003459 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003460 goto error;
3461 }
3462 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003463 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003464
3465 pid_str = PyObject_Str(pid);
3466 if (pid_str == NULL)
3467 goto error;
3468
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003469 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003470 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003471 if (!PyUnicode_IS_ASCII(pid_str)) {
3472 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3473 "persistent IDs in protocol 0 must be "
3474 "ASCII strings");
3475 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003476 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003477 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003478
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003479 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003480 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3481 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3482 _Pickler_Write(self, "\n", 1) < 0) {
3483 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003484 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003485 }
3486 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003487 }
3488 status = 1;
3489 }
3490
3491 if (0) {
3492 error:
3493 status = -1;
3494 }
3495 Py_XDECREF(pid);
3496
3497 return status;
3498}
3499
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003500static PyObject *
3501get_class(PyObject *obj)
3502{
3503 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003504 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003505
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003506 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003507 if (cls == NULL) {
3508 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3509 PyErr_Clear();
3510 cls = (PyObject *) Py_TYPE(obj);
3511 Py_INCREF(cls);
3512 }
3513 }
3514 return cls;
3515}
3516
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003517/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3518 * appropriate __reduce__ method for obj.
3519 */
3520static int
3521save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3522{
3523 PyObject *callable;
3524 PyObject *argtup;
3525 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003526 PyObject *listitems = Py_None;
3527 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003528 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003529 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003530 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003531
3532 const char reduce_op = REDUCE;
3533 const char build_op = BUILD;
3534 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003535 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003536
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003537 size = PyTuple_Size(args);
3538 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003539 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003540 "__reduce__ must contain 2 through 5 elements");
3541 return -1;
3542 }
3543
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003544 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3545 &callable, &argtup, &state, &listitems, &dictitems))
3546 return -1;
3547
3548 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003549 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003550 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003551 return -1;
3552 }
3553 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003554 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003555 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003556 return -1;
3557 }
3558
3559 if (state == Py_None)
3560 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003561
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003562 if (listitems == Py_None)
3563 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003564 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003565 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003566 "returned by __reduce__ must be an iterator, not %s",
3567 Py_TYPE(listitems)->tp_name);
3568 return -1;
3569 }
3570
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003571 if (dictitems == Py_None)
3572 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003573 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003574 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003575 "returned by __reduce__ must be an iterator, not %s",
3576 Py_TYPE(dictitems)->tp_name);
3577 return -1;
3578 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003579
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003580 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003581 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003582 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003583
Victor Stinner804e05e2013-11-14 01:26:17 +01003584 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003585 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003586 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003587 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003588 }
3589 PyErr_Clear();
3590 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003591 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003592 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003593 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3594 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003595 if (!use_newobj_ex) {
3596 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003597 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003598 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003599 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003600 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003601 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003602
3603 if (use_newobj_ex) {
3604 PyObject *cls;
3605 PyObject *args;
3606 PyObject *kwargs;
3607
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003608 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003609 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003610 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003611 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003612 return -1;
3613 }
3614
3615 cls = PyTuple_GET_ITEM(argtup, 0);
3616 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003617 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003618 "first item from NEWOBJ_EX argument tuple must "
3619 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3620 return -1;
3621 }
3622 args = PyTuple_GET_ITEM(argtup, 1);
3623 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003624 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003625 "second item from NEWOBJ_EX argument tuple must "
3626 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3627 return -1;
3628 }
3629 kwargs = PyTuple_GET_ITEM(argtup, 2);
3630 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003631 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003632 "third item from NEWOBJ_EX argument tuple must "
3633 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3634 return -1;
3635 }
3636
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003637 if (self->proto >= 4) {
3638 if (save(self, cls, 0) < 0 ||
3639 save(self, args, 0) < 0 ||
3640 save(self, kwargs, 0) < 0 ||
3641 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3642 return -1;
3643 }
3644 }
3645 else {
3646 PyObject *newargs;
3647 PyObject *cls_new;
3648 Py_ssize_t i;
3649 _Py_IDENTIFIER(__new__);
3650
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003651 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003652 if (newargs == NULL)
3653 return -1;
3654
3655 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3656 if (cls_new == NULL) {
3657 Py_DECREF(newargs);
3658 return -1;
3659 }
3660 PyTuple_SET_ITEM(newargs, 0, cls_new);
3661 Py_INCREF(cls);
3662 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003663 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003664 PyObject *item = PyTuple_GET_ITEM(args, i);
3665 Py_INCREF(item);
3666 PyTuple_SET_ITEM(newargs, i + 2, item);
3667 }
3668
3669 callable = PyObject_Call(st->partial, newargs, kwargs);
3670 Py_DECREF(newargs);
3671 if (callable == NULL)
3672 return -1;
3673
3674 newargs = PyTuple_New(0);
3675 if (newargs == NULL) {
3676 Py_DECREF(callable);
3677 return -1;
3678 }
3679
3680 if (save(self, callable, 0) < 0 ||
3681 save(self, newargs, 0) < 0 ||
3682 _Pickler_Write(self, &reduce_op, 1) < 0) {
3683 Py_DECREF(newargs);
3684 Py_DECREF(callable);
3685 return -1;
3686 }
3687 Py_DECREF(newargs);
3688 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003689 }
3690 }
3691 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003692 PyObject *cls;
3693 PyObject *newargtup;
3694 PyObject *obj_class;
3695 int p;
3696
3697 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003698 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003699 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003700 return -1;
3701 }
3702
3703 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003704 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003705 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003706 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003707 return -1;
3708 }
3709
3710 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003711 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003712 p = obj_class != cls; /* true iff a problem */
3713 Py_DECREF(obj_class);
3714 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003715 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003716 "__newobj__ args has the wrong class");
3717 return -1;
3718 }
3719 }
3720 /* XXX: These calls save() are prone to infinite recursion. Imagine
3721 what happen if the value returned by the __reduce__() method of
3722 some extension type contains another object of the same type. Ouch!
3723
3724 Here is a quick example, that I ran into, to illustrate what I
3725 mean:
3726
3727 >>> import pickle, copyreg
3728 >>> copyreg.dispatch_table.pop(complex)
3729 >>> pickle.dumps(1+2j)
3730 Traceback (most recent call last):
3731 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003732 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003733
3734 Removing the complex class from copyreg.dispatch_table made the
3735 __reduce_ex__() method emit another complex object:
3736
3737 >>> (1+1j).__reduce_ex__(2)
3738 (<function __newobj__ at 0xb7b71c3c>,
3739 (<class 'complex'>, (1+1j)), None, None, None)
3740
3741 Thus when save() was called on newargstup (the 2nd item) recursion
3742 ensued. Of course, the bug was in the complex class which had a
3743 broken __getnewargs__() that emitted another complex object. But,
3744 the point, here, is it is quite easy to end up with a broken reduce
3745 function. */
3746
3747 /* Save the class and its __new__ arguments. */
3748 if (save(self, cls, 0) < 0)
3749 return -1;
3750
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003751 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003752 if (newargtup == NULL)
3753 return -1;
3754
3755 p = save(self, newargtup, 0);
3756 Py_DECREF(newargtup);
3757 if (p < 0)
3758 return -1;
3759
3760 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003761 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003762 return -1;
3763 }
3764 else { /* Not using NEWOBJ. */
3765 if (save(self, callable, 0) < 0 ||
3766 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003767 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003768 return -1;
3769 }
3770
3771 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3772 the caller do not want to memoize the object. Not particularly useful,
3773 but that is to mimic the behavior save_reduce() in pickle.py when
3774 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003775 if (obj != NULL) {
3776 /* If the object is already in the memo, this means it is
3777 recursive. In this case, throw away everything we put on the
3778 stack, and fetch the object back from the memo. */
3779 if (PyMemoTable_Get(self->memo, obj)) {
3780 const char pop_op = POP;
3781
3782 if (_Pickler_Write(self, &pop_op, 1) < 0)
3783 return -1;
3784 if (memo_get(self, obj) < 0)
3785 return -1;
3786
3787 return 0;
3788 }
3789 else if (memo_put(self, obj) < 0)
3790 return -1;
3791 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003792
3793 if (listitems && batch_list(self, listitems) < 0)
3794 return -1;
3795
3796 if (dictitems && batch_dict(self, dictitems) < 0)
3797 return -1;
3798
3799 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003800 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003801 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802 return -1;
3803 }
3804
3805 return 0;
3806}
3807
3808static int
3809save(PicklerObject *self, PyObject *obj, int pers_save)
3810{
3811 PyTypeObject *type;
3812 PyObject *reduce_func = NULL;
3813 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003814 int status = 0;
3815
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003816 if (_Pickler_OpcodeBoundary(self) < 0)
3817 return -1;
3818
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003819 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003820 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003821
3822 /* The extra pers_save argument is necessary to avoid calling save_pers()
3823 on its returned object. */
3824 if (!pers_save && self->pers_func) {
3825 /* save_pers() returns:
3826 -1 to signal an error;
3827 0 if it did nothing successfully;
3828 1 if a persistent id was saved.
3829 */
3830 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3831 goto done;
3832 }
3833
3834 type = Py_TYPE(obj);
3835
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003836 /* The old cPickle had an optimization that used switch-case statement
3837 dispatching on the first letter of the type name. This has was removed
3838 since benchmarks shown that this optimization was actually slowing
3839 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003840
3841 /* Atom types; these aren't memoized, so don't check the memo. */
3842
3843 if (obj == Py_None) {
3844 status = save_none(self, obj);
3845 goto done;
3846 }
3847 else if (obj == Py_False || obj == Py_True) {
3848 status = save_bool(self, obj);
3849 goto done;
3850 }
3851 else if (type == &PyLong_Type) {
3852 status = save_long(self, obj);
3853 goto done;
3854 }
3855 else if (type == &PyFloat_Type) {
3856 status = save_float(self, obj);
3857 goto done;
3858 }
3859
3860 /* Check the memo to see if it has the object. If so, generate
3861 a GET (or BINGET) opcode, instead of pickling the object
3862 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003863 if (PyMemoTable_Get(self->memo, obj)) {
3864 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003865 goto error;
3866 goto done;
3867 }
3868
3869 if (type == &PyBytes_Type) {
3870 status = save_bytes(self, obj);
3871 goto done;
3872 }
3873 else if (type == &PyUnicode_Type) {
3874 status = save_unicode(self, obj);
3875 goto done;
3876 }
3877 else if (type == &PyDict_Type) {
3878 status = save_dict(self, obj);
3879 goto done;
3880 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003881 else if (type == &PySet_Type) {
3882 status = save_set(self, obj);
3883 goto done;
3884 }
3885 else if (type == &PyFrozenSet_Type) {
3886 status = save_frozenset(self, obj);
3887 goto done;
3888 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003889 else if (type == &PyList_Type) {
3890 status = save_list(self, obj);
3891 goto done;
3892 }
3893 else if (type == &PyTuple_Type) {
3894 status = save_tuple(self, obj);
3895 goto done;
3896 }
3897 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003898 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 goto done;
3900 }
3901 else if (type == &PyFunction_Type) {
3902 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003903 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003904 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003905
3906 /* XXX: This part needs some unit tests. */
3907
3908 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003909 * self.dispatch_table, copyreg.dispatch_table, the object's
3910 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003911 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003912 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003913 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003914 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3915 (PyObject *)type);
3916 if (reduce_func == NULL) {
3917 if (PyErr_Occurred()) {
3918 goto error;
3919 }
3920 } else {
3921 /* PyDict_GetItemWithError() returns a borrowed reference.
3922 Increase the reference count to be consistent with
3923 PyObject_GetItem and _PyObject_GetAttrId used below. */
3924 Py_INCREF(reduce_func);
3925 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003926 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003927 reduce_func = PyObject_GetItem(self->dispatch_table,
3928 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003929 if (reduce_func == NULL) {
3930 if (PyErr_ExceptionMatches(PyExc_KeyError))
3931 PyErr_Clear();
3932 else
3933 goto error;
3934 }
3935 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003936 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003937 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003938 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003939 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003940 else if (PyType_IsSubtype(type, &PyType_Type)) {
3941 status = save_global(self, obj, NULL);
3942 goto done;
3943 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003944 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003945 _Py_IDENTIFIER(__reduce__);
3946 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003947
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003948
3949 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3950 automatically defined as __reduce__. While this is convenient, this
3951 make it impossible to know which method was actually called. Of
3952 course, this is not a big deal. But still, it would be nice to let
3953 the user know which method was called when something go
3954 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3955 don't actually have to check for a __reduce__ method. */
3956
3957 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003958 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003959 if (reduce_func != NULL) {
3960 PyObject *proto;
3961 proto = PyLong_FromLong(self->proto);
3962 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003963 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003964 }
3965 }
3966 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003967 PickleState *st = _Pickle_GetGlobalState();
3968
3969 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003970 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003971 }
3972 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003974 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003975 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003976 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003977 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02003978 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003979 }
3980 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003981 PyErr_Format(st->PicklingError,
3982 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003983 type->tp_name, obj);
3984 goto error;
3985 }
3986 }
3987 }
3988
3989 if (reduce_value == NULL)
3990 goto error;
3991
3992 if (PyUnicode_Check(reduce_value)) {
3993 status = save_global(self, obj, reduce_value);
3994 goto done;
3995 }
3996
3997 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003998 PickleState *st = _Pickle_GetGlobalState();
3999 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004000 "__reduce__ must return a string or tuple");
4001 goto error;
4002 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004003
4004 status = save_reduce(self, reduce_value, obj);
4005
4006 if (0) {
4007 error:
4008 status = -1;
4009 }
4010 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004011
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004012 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004013 Py_XDECREF(reduce_func);
4014 Py_XDECREF(reduce_value);
4015
4016 return status;
4017}
4018
4019static int
4020dump(PicklerObject *self, PyObject *obj)
4021{
4022 const char stop_op = STOP;
4023
4024 if (self->proto >= 2) {
4025 char header[2];
4026
4027 header[0] = PROTO;
4028 assert(self->proto >= 0 && self->proto < 256);
4029 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004030 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004032 if (self->proto >= 4)
4033 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004034 }
4035
4036 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004037 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004038 return -1;
4039
4040 return 0;
4041}
4042
Larry Hastings61272b72014-01-07 12:41:53 -08004043/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004044
4045_pickle.Pickler.clear_memo
4046
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004047Clears the pickler's "memo".
4048
4049The memo is the data structure that remembers which objects the
4050pickler has already seen, so that shared or recursive objects are
4051pickled by reference and not by value. This method is useful when
4052re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004053[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004054
Larry Hastings3cceb382014-01-04 11:09:09 -08004055static PyObject *
4056_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004057/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004058{
4059 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004060 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004061
4062 Py_RETURN_NONE;
4063}
4064
Larry Hastings61272b72014-01-07 12:41:53 -08004065/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004066
4067_pickle.Pickler.dump
4068
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004069 obj: object
4070 /
4071
4072Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004073[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004074
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004075static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004076_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004077/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004078{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004079 /* Check whether the Pickler was initialized correctly (issue3664).
4080 Developers often forget to call __init__() in their subclasses, which
4081 would trigger a segfault without this check. */
4082 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004083 PickleState *st = _Pickle_GetGlobalState();
4084 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004085 "Pickler.__init__() was not called by %s.__init__()",
4086 Py_TYPE(self)->tp_name);
4087 return NULL;
4088 }
4089
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004090 if (_Pickler_ClearBuffer(self) < 0)
4091 return NULL;
4092
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 if (dump(self, obj) < 0)
4094 return NULL;
4095
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004096 if (_Pickler_FlushToFile(self) < 0)
4097 return NULL;
4098
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099 Py_RETURN_NONE;
4100}
4101
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004102/*[clinic input]
4103
4104_pickle.Pickler.__sizeof__ -> Py_ssize_t
4105
4106Returns size in memory, in bytes.
4107[clinic start generated code]*/
4108
4109static Py_ssize_t
4110_pickle_Pickler___sizeof___impl(PicklerObject *self)
4111/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4112{
4113 Py_ssize_t res, s;
4114
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004115 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004116 if (self->memo != NULL) {
4117 res += sizeof(PyMemoTable);
4118 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4119 }
4120 if (self->output_buffer != NULL) {
4121 s = _PySys_GetSizeOf(self->output_buffer);
4122 if (s == -1)
4123 return -1;
4124 res += s;
4125 }
4126 return res;
4127}
4128
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004130 _PICKLE_PICKLER_DUMP_METHODDEF
4131 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004132 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004133 {NULL, NULL} /* sentinel */
4134};
4135
4136static void
4137Pickler_dealloc(PicklerObject *self)
4138{
4139 PyObject_GC_UnTrack(self);
4140
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004141 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004142 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004143 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004144 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004145 Py_XDECREF(self->fast_memo);
4146
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004147 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004148
4149 Py_TYPE(self)->tp_free((PyObject *)self);
4150}
4151
4152static int
4153Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4154{
4155 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004156 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004157 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004158 Py_VISIT(self->fast_memo);
4159 return 0;
4160}
4161
4162static int
4163Pickler_clear(PicklerObject *self)
4164{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004165 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004166 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004167 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004168 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004169 Py_CLEAR(self->fast_memo);
4170
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004171 if (self->memo != NULL) {
4172 PyMemoTable *memo = self->memo;
4173 self->memo = NULL;
4174 PyMemoTable_Del(memo);
4175 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004176 return 0;
4177}
4178
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004179
Larry Hastings61272b72014-01-07 12:41:53 -08004180/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004181
4182_pickle.Pickler.__init__
4183
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004184 file: object
4185 protocol: object = NULL
4186 fix_imports: bool = True
4187
4188This takes a binary file for writing a pickle data stream.
4189
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004190The optional *protocol* argument tells the pickler to use the given
4191protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4192protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004193
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004194Specifying a negative protocol version selects the highest protocol
4195version supported. The higher the protocol used, the more recent the
4196version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004197
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004198The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004199bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004200writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004201this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004202
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004203If *fix_imports* is True and protocol is less than 3, pickle will try
4204to map the new Python 3 names to the old module names used in Python
42052, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004206[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004207
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004208static int
Larry Hastings89964c42015-04-14 18:07:59 -04004209_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4210 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004211/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004212{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004213 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004214 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004215
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004216 /* In case of multiple __init__() calls, clear previous content. */
4217 if (self->write != NULL)
4218 (void)Pickler_clear(self);
4219
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004220 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004221 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004222
4223 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004224 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004225
4226 /* memo and output_buffer may have already been created in _Pickler_New */
4227 if (self->memo == NULL) {
4228 self->memo = PyMemoTable_New();
4229 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004230 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004231 }
4232 self->output_len = 0;
4233 if (self->output_buffer == NULL) {
4234 self->max_output_len = WRITE_BUF_SIZE;
4235 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4236 self->max_output_len);
4237 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004238 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004239 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004240
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004241 self->fast = 0;
4242 self->fast_nesting = 0;
4243 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004244 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004245 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4246 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4247 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004248 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004249 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004250 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004251 self->dispatch_table = NULL;
4252 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4253 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4254 &PyId_dispatch_table);
4255 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004256 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004257 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004258
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004259 return 0;
4260}
4261
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004262
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004263/* Define a proxy object for the Pickler's internal memo object. This is to
4264 * avoid breaking code like:
4265 * pickler.memo.clear()
4266 * and
4267 * pickler.memo = saved_memo
4268 * Is this a good idea? Not really, but we don't want to break code that uses
4269 * it. Note that we don't implement the entire mapping API here. This is
4270 * intentional, as these should be treated as black-box implementation details.
4271 */
4272
Larry Hastings61272b72014-01-07 12:41:53 -08004273/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004274_pickle.PicklerMemoProxy.clear
4275
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004276Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004277[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004278
Larry Hastings3cceb382014-01-04 11:09:09 -08004279static PyObject *
4280_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004281/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004282{
4283 if (self->pickler->memo)
4284 PyMemoTable_Clear(self->pickler->memo);
4285 Py_RETURN_NONE;
4286}
4287
Larry Hastings61272b72014-01-07 12:41:53 -08004288/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004289_pickle.PicklerMemoProxy.copy
4290
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004291Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004292[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004293
Larry Hastings3cceb382014-01-04 11:09:09 -08004294static PyObject *
4295_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004296/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004297{
4298 Py_ssize_t i;
4299 PyMemoTable *memo;
4300 PyObject *new_memo = PyDict_New();
4301 if (new_memo == NULL)
4302 return NULL;
4303
4304 memo = self->pickler->memo;
4305 for (i = 0; i < memo->mt_allocated; ++i) {
4306 PyMemoEntry entry = memo->mt_table[i];
4307 if (entry.me_key != NULL) {
4308 int status;
4309 PyObject *key, *value;
4310
4311 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004312 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004313
4314 if (key == NULL || value == NULL) {
4315 Py_XDECREF(key);
4316 Py_XDECREF(value);
4317 goto error;
4318 }
4319 status = PyDict_SetItem(new_memo, key, value);
4320 Py_DECREF(key);
4321 Py_DECREF(value);
4322 if (status < 0)
4323 goto error;
4324 }
4325 }
4326 return new_memo;
4327
4328 error:
4329 Py_XDECREF(new_memo);
4330 return NULL;
4331}
4332
Larry Hastings61272b72014-01-07 12:41:53 -08004333/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004334_pickle.PicklerMemoProxy.__reduce__
4335
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004336Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004337[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004338
Larry Hastings3cceb382014-01-04 11:09:09 -08004339static PyObject *
4340_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004341/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004342{
4343 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004344 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004345 if (contents == NULL)
4346 return NULL;
4347
4348 reduce_value = PyTuple_New(2);
4349 if (reduce_value == NULL) {
4350 Py_DECREF(contents);
4351 return NULL;
4352 }
4353 dict_args = PyTuple_New(1);
4354 if (dict_args == NULL) {
4355 Py_DECREF(contents);
4356 Py_DECREF(reduce_value);
4357 return NULL;
4358 }
4359 PyTuple_SET_ITEM(dict_args, 0, contents);
4360 Py_INCREF((PyObject *)&PyDict_Type);
4361 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4362 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4363 return reduce_value;
4364}
4365
4366static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004367 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4368 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4369 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004370 {NULL, NULL} /* sentinel */
4371};
4372
4373static void
4374PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4375{
4376 PyObject_GC_UnTrack(self);
4377 Py_XDECREF(self->pickler);
4378 PyObject_GC_Del((PyObject *)self);
4379}
4380
4381static int
4382PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4383 visitproc visit, void *arg)
4384{
4385 Py_VISIT(self->pickler);
4386 return 0;
4387}
4388
4389static int
4390PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4391{
4392 Py_CLEAR(self->pickler);
4393 return 0;
4394}
4395
4396static PyTypeObject PicklerMemoProxyType = {
4397 PyVarObject_HEAD_INIT(NULL, 0)
4398 "_pickle.PicklerMemoProxy", /*tp_name*/
4399 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4400 0,
4401 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4402 0, /* tp_print */
4403 0, /* tp_getattr */
4404 0, /* tp_setattr */
4405 0, /* tp_compare */
4406 0, /* tp_repr */
4407 0, /* tp_as_number */
4408 0, /* tp_as_sequence */
4409 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004410 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004411 0, /* tp_call */
4412 0, /* tp_str */
4413 PyObject_GenericGetAttr, /* tp_getattro */
4414 PyObject_GenericSetAttr, /* tp_setattro */
4415 0, /* tp_as_buffer */
4416 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4417 0, /* tp_doc */
4418 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4419 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4420 0, /* tp_richcompare */
4421 0, /* tp_weaklistoffset */
4422 0, /* tp_iter */
4423 0, /* tp_iternext */
4424 picklerproxy_methods, /* tp_methods */
4425};
4426
4427static PyObject *
4428PicklerMemoProxy_New(PicklerObject *pickler)
4429{
4430 PicklerMemoProxyObject *self;
4431
4432 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4433 if (self == NULL)
4434 return NULL;
4435 Py_INCREF(pickler);
4436 self->pickler = pickler;
4437 PyObject_GC_Track(self);
4438 return (PyObject *)self;
4439}
4440
4441/*****************************************************************************/
4442
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004443static PyObject *
4444Pickler_get_memo(PicklerObject *self)
4445{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004446 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004447}
4448
4449static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004450Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004451{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004452 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004453
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004454 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004455 PyErr_SetString(PyExc_TypeError,
4456 "attribute deletion is not supported");
4457 return -1;
4458 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004459
4460 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4461 PicklerObject *pickler =
4462 ((PicklerMemoProxyObject *)obj)->pickler;
4463
4464 new_memo = PyMemoTable_Copy(pickler->memo);
4465 if (new_memo == NULL)
4466 return -1;
4467 }
4468 else if (PyDict_Check(obj)) {
4469 Py_ssize_t i = 0;
4470 PyObject *key, *value;
4471
4472 new_memo = PyMemoTable_New();
4473 if (new_memo == NULL)
4474 return -1;
4475
4476 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004477 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004478 PyObject *memo_obj;
4479
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004480 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004481 PyErr_SetString(PyExc_TypeError,
4482 "'memo' values must be 2-item tuples");
4483 goto error;
4484 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004485 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004486 if (memo_id == -1 && PyErr_Occurred())
4487 goto error;
4488 memo_obj = PyTuple_GET_ITEM(value, 1);
4489 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4490 goto error;
4491 }
4492 }
4493 else {
4494 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004495 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004496 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004497 return -1;
4498 }
4499
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004500 PyMemoTable_Del(self->memo);
4501 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004502
4503 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004504
4505 error:
4506 if (new_memo)
4507 PyMemoTable_Del(new_memo);
4508 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004509}
4510
4511static PyObject *
4512Pickler_get_persid(PicklerObject *self)
4513{
4514 if (self->pers_func == NULL)
4515 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4516 else
4517 Py_INCREF(self->pers_func);
4518 return self->pers_func;
4519}
4520
4521static int
4522Pickler_set_persid(PicklerObject *self, PyObject *value)
4523{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004524 if (value == NULL) {
4525 PyErr_SetString(PyExc_TypeError,
4526 "attribute deletion is not supported");
4527 return -1;
4528 }
4529 if (!PyCallable_Check(value)) {
4530 PyErr_SetString(PyExc_TypeError,
4531 "persistent_id must be a callable taking one argument");
4532 return -1;
4533 }
4534
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004535 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004536 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004537
4538 return 0;
4539}
4540
4541static PyMemberDef Pickler_members[] = {
4542 {"bin", T_INT, offsetof(PicklerObject, bin)},
4543 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004544 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004545 {NULL}
4546};
4547
4548static PyGetSetDef Pickler_getsets[] = {
4549 {"memo", (getter)Pickler_get_memo,
4550 (setter)Pickler_set_memo},
4551 {"persistent_id", (getter)Pickler_get_persid,
4552 (setter)Pickler_set_persid},
4553 {NULL}
4554};
4555
4556static PyTypeObject Pickler_Type = {
4557 PyVarObject_HEAD_INIT(NULL, 0)
4558 "_pickle.Pickler" , /*tp_name*/
4559 sizeof(PicklerObject), /*tp_basicsize*/
4560 0, /*tp_itemsize*/
4561 (destructor)Pickler_dealloc, /*tp_dealloc*/
4562 0, /*tp_print*/
4563 0, /*tp_getattr*/
4564 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004565 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004566 0, /*tp_repr*/
4567 0, /*tp_as_number*/
4568 0, /*tp_as_sequence*/
4569 0, /*tp_as_mapping*/
4570 0, /*tp_hash*/
4571 0, /*tp_call*/
4572 0, /*tp_str*/
4573 0, /*tp_getattro*/
4574 0, /*tp_setattro*/
4575 0, /*tp_as_buffer*/
4576 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004577 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004578 (traverseproc)Pickler_traverse, /*tp_traverse*/
4579 (inquiry)Pickler_clear, /*tp_clear*/
4580 0, /*tp_richcompare*/
4581 0, /*tp_weaklistoffset*/
4582 0, /*tp_iter*/
4583 0, /*tp_iternext*/
4584 Pickler_methods, /*tp_methods*/
4585 Pickler_members, /*tp_members*/
4586 Pickler_getsets, /*tp_getset*/
4587 0, /*tp_base*/
4588 0, /*tp_dict*/
4589 0, /*tp_descr_get*/
4590 0, /*tp_descr_set*/
4591 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004592 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004593 PyType_GenericAlloc, /*tp_alloc*/
4594 PyType_GenericNew, /*tp_new*/
4595 PyObject_GC_Del, /*tp_free*/
4596 0, /*tp_is_gc*/
4597};
4598
Victor Stinner121aab42011-09-29 23:40:53 +02004599/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004600
4601 XXX: It would be nice to able to avoid Python function call overhead, by
4602 using directly the C version of find_class(), when find_class() is not
4603 overridden by a subclass. Although, this could become rather hackish. A
4604 simpler optimization would be to call the C function when self is not a
4605 subclass instance. */
4606static PyObject *
4607find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4608{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004609 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004610
Victor Stinner55ba38a2016-12-09 16:09:30 +01004611 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4612 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004613}
4614
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004615static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004616marker(UnpicklerObject *self)
4617{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004618 Py_ssize_t mark;
4619
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004620 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004621 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004622 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004623 return -1;
4624 }
4625
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004626 mark = self->marks[--self->num_marks];
4627 self->stack->mark_set = self->num_marks != 0;
4628 self->stack->fence = self->num_marks ?
4629 self->marks[self->num_marks - 1] : 0;
4630 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004631}
4632
4633static int
4634load_none(UnpicklerObject *self)
4635{
4636 PDATA_APPEND(self->stack, Py_None, -1);
4637 return 0;
4638}
4639
4640static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004641load_int(UnpicklerObject *self)
4642{
4643 PyObject *value;
4644 char *endptr, *s;
4645 Py_ssize_t len;
4646 long x;
4647
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004648 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004649 return -1;
4650 if (len < 2)
4651 return bad_readline();
4652
4653 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004654 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004655 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004656 x = strtol(s, &endptr, 0);
4657
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004658 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004659 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004660 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004661 errno = 0;
4662 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004663 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004664 if (value == NULL) {
4665 PyErr_SetString(PyExc_ValueError,
4666 "could not convert string to int");
4667 return -1;
4668 }
4669 }
4670 else {
4671 if (len == 3 && (x == 0 || x == 1)) {
4672 if ((value = PyBool_FromLong(x)) == NULL)
4673 return -1;
4674 }
4675 else {
4676 if ((value = PyLong_FromLong(x)) == NULL)
4677 return -1;
4678 }
4679 }
4680
4681 PDATA_PUSH(self->stack, value, -1);
4682 return 0;
4683}
4684
4685static int
4686load_bool(UnpicklerObject *self, PyObject *boolean)
4687{
4688 assert(boolean == Py_True || boolean == Py_False);
4689 PDATA_APPEND(self->stack, boolean, -1);
4690 return 0;
4691}
4692
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004693/* s contains x bytes of an unsigned little-endian integer. Return its value
4694 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4695 */
4696static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004697calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004698{
4699 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004700 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004701 size_t x = 0;
4702
Serhiy Storchakae0606192015-09-29 22:10:07 +03004703 if (nbytes > (int)sizeof(size_t)) {
4704 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4705 * have 64-bit size that can't be represented on 32-bit platform.
4706 */
4707 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4708 if (s[i])
4709 return -1;
4710 }
4711 nbytes = (int)sizeof(size_t);
4712 }
4713 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004714 x |= (size_t) s[i] << (8 * i);
4715 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004716
4717 if (x > PY_SSIZE_T_MAX)
4718 return -1;
4719 else
4720 return (Py_ssize_t) x;
4721}
4722
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004723/* s contains x bytes of a little-endian integer. Return its value as a
4724 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004725 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004726 * of x-platform bugs.
4727 */
4728static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004729calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004730{
4731 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004732 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733 long x = 0;
4734
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004735 for (i = 0; i < nbytes; i++) {
4736 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004737 }
4738
4739 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4740 * is signed, so on a box with longs bigger than 4 bytes we need
4741 * to extend a BININT's sign bit to the full width.
4742 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004743 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004744 x |= -(x & (1L << 31));
4745 }
4746
4747 return x;
4748}
4749
4750static int
4751load_binintx(UnpicklerObject *self, char *s, int size)
4752{
4753 PyObject *value;
4754 long x;
4755
4756 x = calc_binint(s, size);
4757
4758 if ((value = PyLong_FromLong(x)) == NULL)
4759 return -1;
4760
4761 PDATA_PUSH(self->stack, value, -1);
4762 return 0;
4763}
4764
4765static int
4766load_binint(UnpicklerObject *self)
4767{
4768 char *s;
4769
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004770 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004771 return -1;
4772
4773 return load_binintx(self, s, 4);
4774}
4775
4776static int
4777load_binint1(UnpicklerObject *self)
4778{
4779 char *s;
4780
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004781 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004782 return -1;
4783
4784 return load_binintx(self, s, 1);
4785}
4786
4787static int
4788load_binint2(UnpicklerObject *self)
4789{
4790 char *s;
4791
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004792 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004793 return -1;
4794
4795 return load_binintx(self, s, 2);
4796}
4797
4798static int
4799load_long(UnpicklerObject *self)
4800{
4801 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004802 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004803 Py_ssize_t len;
4804
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004805 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004806 return -1;
4807 if (len < 2)
4808 return bad_readline();
4809
Mark Dickinson8dd05142009-01-20 20:43:58 +00004810 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4811 the 'L' before calling PyLong_FromString. In order to maintain
4812 compatibility with Python 3.0.0, we don't actually *require*
4813 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004814 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004815 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004816 /* XXX: Should the base argument explicitly set to 10? */
4817 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004818 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004819 return -1;
4820
4821 PDATA_PUSH(self->stack, value, -1);
4822 return 0;
4823}
4824
4825/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4826 * data following.
4827 */
4828static int
4829load_counted_long(UnpicklerObject *self, int size)
4830{
4831 PyObject *value;
4832 char *nbytes;
4833 char *pdata;
4834
4835 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004836 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004837 return -1;
4838
4839 size = calc_binint(nbytes, size);
4840 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004841 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004842 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004843 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004844 "LONG pickle has negative byte count");
4845 return -1;
4846 }
4847
4848 if (size == 0)
4849 value = PyLong_FromLong(0L);
4850 else {
4851 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004852 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004853 return -1;
4854 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4855 1 /* little endian */ , 1 /* signed */ );
4856 }
4857 if (value == NULL)
4858 return -1;
4859 PDATA_PUSH(self->stack, value, -1);
4860 return 0;
4861}
4862
4863static int
4864load_float(UnpicklerObject *self)
4865{
4866 PyObject *value;
4867 char *endptr, *s;
4868 Py_ssize_t len;
4869 double d;
4870
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004871 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004872 return -1;
4873 if (len < 2)
4874 return bad_readline();
4875
4876 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004877 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4878 if (d == -1.0 && PyErr_Occurred())
4879 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004880 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004881 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4882 return -1;
4883 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004884 value = PyFloat_FromDouble(d);
4885 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004886 return -1;
4887
4888 PDATA_PUSH(self->stack, value, -1);
4889 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004890}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004891
4892static int
4893load_binfloat(UnpicklerObject *self)
4894{
4895 PyObject *value;
4896 double x;
4897 char *s;
4898
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004899 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004900 return -1;
4901
4902 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4903 if (x == -1.0 && PyErr_Occurred())
4904 return -1;
4905
4906 if ((value = PyFloat_FromDouble(x)) == NULL)
4907 return -1;
4908
4909 PDATA_PUSH(self->stack, value, -1);
4910 return 0;
4911}
4912
4913static int
4914load_string(UnpicklerObject *self)
4915{
4916 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004917 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004918 Py_ssize_t len;
4919 char *s, *p;
4920
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004921 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004922 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004923 /* Strip the newline */
4924 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004925 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004926 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004927 p = s + 1;
4928 len -= 2;
4929 }
4930 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004931 PickleState *st = _Pickle_GetGlobalState();
4932 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004933 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004934 return -1;
4935 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004936 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004937
4938 /* Use the PyBytes API to decode the string, since that is what is used
4939 to encode, and then coerce the result to Unicode. */
4940 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004941 if (bytes == NULL)
4942 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004943
4944 /* Leave the Python 2.x strings as 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 = bytes;
4948 }
4949 else {
4950 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4951 Py_DECREF(bytes);
4952 if (obj == NULL) {
4953 return -1;
4954 }
4955 }
4956
4957 PDATA_PUSH(self->stack, obj, -1);
4958 return 0;
4959}
4960
4961static int
4962load_counted_binstring(UnpicklerObject *self, int nbytes)
4963{
4964 PyObject *obj;
4965 Py_ssize_t size;
4966 char *s;
4967
4968 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004969 return -1;
4970
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004971 size = calc_binsize(s, nbytes);
4972 if (size < 0) {
4973 PickleState *st = _Pickle_GetGlobalState();
4974 PyErr_Format(st->UnpicklingError,
4975 "BINSTRING exceeds system's maximum size of %zd bytes",
4976 PY_SSIZE_T_MAX);
4977 return -1;
4978 }
4979
4980 if (_Unpickler_Read(self, &s, size) < 0)
4981 return -1;
4982
4983 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4984 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4985 if (strcmp(self->encoding, "bytes") == 0) {
4986 obj = PyBytes_FromStringAndSize(s, size);
4987 }
4988 else {
4989 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4990 }
4991 if (obj == NULL) {
4992 return -1;
4993 }
4994
4995 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004996 return 0;
4997}
4998
4999static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005000load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005001{
5002 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005003 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005004 char *s;
5005
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005006 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005007 return -1;
5008
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005009 size = calc_binsize(s, nbytes);
5010 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005011 PyErr_Format(PyExc_OverflowError,
5012 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005013 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005014 return -1;
5015 }
5016
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005017 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005018 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005019
5020 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005021 if (bytes == NULL)
5022 return -1;
5023
5024 PDATA_PUSH(self->stack, bytes, -1);
5025 return 0;
5026}
5027
5028static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005029load_unicode(UnpicklerObject *self)
5030{
5031 PyObject *str;
5032 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005033 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005034
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005035 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005036 return -1;
5037 if (len < 1)
5038 return bad_readline();
5039
5040 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5041 if (str == NULL)
5042 return -1;
5043
5044 PDATA_PUSH(self->stack, str, -1);
5045 return 0;
5046}
5047
5048static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005049load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005050{
5051 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005052 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005053 char *s;
5054
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005055 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005056 return -1;
5057
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005058 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005059 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005060 PyErr_Format(PyExc_OverflowError,
5061 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005062 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005063 return -1;
5064 }
5065
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005066 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005067 return -1;
5068
Victor Stinner485fb562010-04-13 11:07:24 +00005069 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005070 if (str == NULL)
5071 return -1;
5072
5073 PDATA_PUSH(self->stack, str, -1);
5074 return 0;
5075}
5076
5077static int
Victor Stinner21b47112016-03-14 18:09:39 +01005078load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005079{
5080 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005081
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005082 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005083 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005084
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005085 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005086 if (tuple == NULL)
5087 return -1;
5088 PDATA_PUSH(self->stack, tuple, -1);
5089 return 0;
5090}
5091
5092static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005093load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005094{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005095 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005096
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005097 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005098 return -1;
5099
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005100 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005101}
5102
5103static int
5104load_empty_list(UnpicklerObject *self)
5105{
5106 PyObject *list;
5107
5108 if ((list = PyList_New(0)) == NULL)
5109 return -1;
5110 PDATA_PUSH(self->stack, list, -1);
5111 return 0;
5112}
5113
5114static int
5115load_empty_dict(UnpicklerObject *self)
5116{
5117 PyObject *dict;
5118
5119 if ((dict = PyDict_New()) == NULL)
5120 return -1;
5121 PDATA_PUSH(self->stack, dict, -1);
5122 return 0;
5123}
5124
5125static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005126load_empty_set(UnpicklerObject *self)
5127{
5128 PyObject *set;
5129
5130 if ((set = PySet_New(NULL)) == NULL)
5131 return -1;
5132 PDATA_PUSH(self->stack, set, -1);
5133 return 0;
5134}
5135
5136static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005137load_list(UnpicklerObject *self)
5138{
5139 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005140 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005141
5142 if ((i = marker(self)) < 0)
5143 return -1;
5144
5145 list = Pdata_poplist(self->stack, i);
5146 if (list == NULL)
5147 return -1;
5148 PDATA_PUSH(self->stack, list, -1);
5149 return 0;
5150}
5151
5152static int
5153load_dict(UnpicklerObject *self)
5154{
5155 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005156 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005157
5158 if ((i = marker(self)) < 0)
5159 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005160 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005161
5162 if ((dict = PyDict_New()) == NULL)
5163 return -1;
5164
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005165 if ((j - i) % 2 != 0) {
5166 PickleState *st = _Pickle_GetGlobalState();
5167 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005168 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005169 return -1;
5170 }
5171
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005172 for (k = i + 1; k < j; k += 2) {
5173 key = self->stack->data[k - 1];
5174 value = self->stack->data[k];
5175 if (PyDict_SetItem(dict, key, value) < 0) {
5176 Py_DECREF(dict);
5177 return -1;
5178 }
5179 }
5180 Pdata_clear(self->stack, i);
5181 PDATA_PUSH(self->stack, dict, -1);
5182 return 0;
5183}
5184
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005185static int
5186load_frozenset(UnpicklerObject *self)
5187{
5188 PyObject *items;
5189 PyObject *frozenset;
5190 Py_ssize_t i;
5191
5192 if ((i = marker(self)) < 0)
5193 return -1;
5194
5195 items = Pdata_poptuple(self->stack, i);
5196 if (items == NULL)
5197 return -1;
5198
5199 frozenset = PyFrozenSet_New(items);
5200 Py_DECREF(items);
5201 if (frozenset == NULL)
5202 return -1;
5203
5204 PDATA_PUSH(self->stack, frozenset, -1);
5205 return 0;
5206}
5207
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005208static PyObject *
5209instantiate(PyObject *cls, PyObject *args)
5210{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005211 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005212 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005213 /* Caller must assure args are a tuple. Normally, args come from
5214 Pdata_poptuple which packs objects from the top of the stack
5215 into a newly created tuple. */
5216 assert(PyTuple_Check(args));
Serhiy Storchakafff9a312017-03-21 08:53:25 +02005217 if (PyTuple_GET_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005218 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005219 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005220 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005221 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005222 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005223
Victor Stinner55ba38a2016-12-09 16:09:30 +01005224 result = _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005225 }
5226 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005227}
5228
5229static int
5230load_obj(UnpicklerObject *self)
5231{
5232 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005233 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005234
5235 if ((i = marker(self)) < 0)
5236 return -1;
5237
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005238 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005239 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005240
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005241 args = Pdata_poptuple(self->stack, i + 1);
5242 if (args == NULL)
5243 return -1;
5244
5245 PDATA_POP(self->stack, cls);
5246 if (cls) {
5247 obj = instantiate(cls, args);
5248 Py_DECREF(cls);
5249 }
5250 Py_DECREF(args);
5251 if (obj == NULL)
5252 return -1;
5253
5254 PDATA_PUSH(self->stack, obj, -1);
5255 return 0;
5256}
5257
5258static int
5259load_inst(UnpicklerObject *self)
5260{
5261 PyObject *cls = NULL;
5262 PyObject *args = NULL;
5263 PyObject *obj = NULL;
5264 PyObject *module_name;
5265 PyObject *class_name;
5266 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005267 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005268 char *s;
5269
5270 if ((i = marker(self)) < 0)
5271 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005272 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005273 return -1;
5274 if (len < 2)
5275 return bad_readline();
5276
5277 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5278 identifiers are permitted in Python 3.0, since the INST opcode is only
5279 supported by older protocols on Python 2.x. */
5280 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5281 if (module_name == NULL)
5282 return -1;
5283
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005284 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005285 if (len < 2) {
5286 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005287 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005288 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005289 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005290 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005291 cls = find_class(self, module_name, class_name);
5292 Py_DECREF(class_name);
5293 }
5294 }
5295 Py_DECREF(module_name);
5296
5297 if (cls == NULL)
5298 return -1;
5299
5300 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5301 obj = instantiate(cls, args);
5302 Py_DECREF(args);
5303 }
5304 Py_DECREF(cls);
5305
5306 if (obj == NULL)
5307 return -1;
5308
5309 PDATA_PUSH(self->stack, obj, -1);
5310 return 0;
5311}
5312
5313static int
5314load_newobj(UnpicklerObject *self)
5315{
5316 PyObject *args = NULL;
5317 PyObject *clsraw = NULL;
5318 PyTypeObject *cls; /* clsraw cast to its true type */
5319 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005320 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005321
5322 /* Stack is ... cls argtuple, and we want to call
5323 * cls.__new__(cls, *argtuple).
5324 */
5325 PDATA_POP(self->stack, args);
5326 if (args == NULL)
5327 goto error;
5328 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005329 PyErr_SetString(st->UnpicklingError,
5330 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005331 goto error;
5332 }
5333
5334 PDATA_POP(self->stack, clsraw);
5335 cls = (PyTypeObject *)clsraw;
5336 if (cls == NULL)
5337 goto error;
5338 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005339 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005340 "isn't a type object");
5341 goto error;
5342 }
5343 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005344 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005345 "has NULL tp_new");
5346 goto error;
5347 }
5348
5349 /* Call __new__. */
5350 obj = cls->tp_new(cls, args, NULL);
5351 if (obj == NULL)
5352 goto error;
5353
5354 Py_DECREF(args);
5355 Py_DECREF(clsraw);
5356 PDATA_PUSH(self->stack, obj, -1);
5357 return 0;
5358
5359 error:
5360 Py_XDECREF(args);
5361 Py_XDECREF(clsraw);
5362 return -1;
5363}
5364
5365static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005366load_newobj_ex(UnpicklerObject *self)
5367{
5368 PyObject *cls, *args, *kwargs;
5369 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005370 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005371
5372 PDATA_POP(self->stack, kwargs);
5373 if (kwargs == NULL) {
5374 return -1;
5375 }
5376 PDATA_POP(self->stack, args);
5377 if (args == NULL) {
5378 Py_DECREF(kwargs);
5379 return -1;
5380 }
5381 PDATA_POP(self->stack, cls);
5382 if (cls == NULL) {
5383 Py_DECREF(kwargs);
5384 Py_DECREF(args);
5385 return -1;
5386 }
Larry Hastings61272b72014-01-07 12:41:53 -08005387
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005388 if (!PyType_Check(cls)) {
5389 Py_DECREF(kwargs);
5390 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005391 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005392 "NEWOBJ_EX class argument must be a type, not %.200s",
5393 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005394 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005395 return -1;
5396 }
5397
5398 if (((PyTypeObject *)cls)->tp_new == NULL) {
5399 Py_DECREF(kwargs);
5400 Py_DECREF(args);
5401 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005402 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005403 "NEWOBJ_EX class argument doesn't have __new__");
5404 return -1;
5405 }
5406 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5407 Py_DECREF(kwargs);
5408 Py_DECREF(args);
5409 Py_DECREF(cls);
5410 if (obj == NULL) {
5411 return -1;
5412 }
5413 PDATA_PUSH(self->stack, obj, -1);
5414 return 0;
5415}
5416
5417static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005418load_global(UnpicklerObject *self)
5419{
5420 PyObject *global = NULL;
5421 PyObject *module_name;
5422 PyObject *global_name;
5423 Py_ssize_t len;
5424 char *s;
5425
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005426 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005427 return -1;
5428 if (len < 2)
5429 return bad_readline();
5430 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5431 if (!module_name)
5432 return -1;
5433
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005434 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005435 if (len < 2) {
5436 Py_DECREF(module_name);
5437 return bad_readline();
5438 }
5439 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5440 if (global_name) {
5441 global = find_class(self, module_name, global_name);
5442 Py_DECREF(global_name);
5443 }
5444 }
5445 Py_DECREF(module_name);
5446
5447 if (global == NULL)
5448 return -1;
5449 PDATA_PUSH(self->stack, global, -1);
5450 return 0;
5451}
5452
5453static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005454load_stack_global(UnpicklerObject *self)
5455{
5456 PyObject *global;
5457 PyObject *module_name;
5458 PyObject *global_name;
5459
5460 PDATA_POP(self->stack, global_name);
5461 PDATA_POP(self->stack, module_name);
5462 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5463 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005464 PickleState *st = _Pickle_GetGlobalState();
5465 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005466 Py_XDECREF(global_name);
5467 Py_XDECREF(module_name);
5468 return -1;
5469 }
5470 global = find_class(self, module_name, global_name);
5471 Py_DECREF(global_name);
5472 Py_DECREF(module_name);
5473 if (global == NULL)
5474 return -1;
5475 PDATA_PUSH(self->stack, global, -1);
5476 return 0;
5477}
5478
5479static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005480load_persid(UnpicklerObject *self)
5481{
5482 PyObject *pid;
5483 Py_ssize_t len;
5484 char *s;
5485
5486 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005487 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005488 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005489 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005490 return bad_readline();
5491
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005492 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5493 if (pid == NULL) {
5494 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5495 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5496 "persistent IDs in protocol 0 must be "
5497 "ASCII strings");
5498 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005499 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005500 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005501
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005502 /* This does not leak since _Pickle_FastCall() steals the reference
5503 to pid first. */
5504 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005505 if (pid == NULL)
5506 return -1;
5507
5508 PDATA_PUSH(self->stack, pid, -1);
5509 return 0;
5510 }
5511 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005512 PickleState *st = _Pickle_GetGlobalState();
5513 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005514 "A load persistent id instruction was encountered,\n"
5515 "but no persistent_load function was specified.");
5516 return -1;
5517 }
5518}
5519
5520static int
5521load_binpersid(UnpicklerObject *self)
5522{
5523 PyObject *pid;
5524
5525 if (self->pers_func) {
5526 PDATA_POP(self->stack, pid);
5527 if (pid == NULL)
5528 return -1;
5529
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005530 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005531 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005532 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005533 if (pid == NULL)
5534 return -1;
5535
5536 PDATA_PUSH(self->stack, pid, -1);
5537 return 0;
5538 }
5539 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005540 PickleState *st = _Pickle_GetGlobalState();
5541 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005542 "A load persistent id instruction was encountered,\n"
5543 "but no persistent_load function was specified.");
5544 return -1;
5545 }
5546}
5547
5548static int
5549load_pop(UnpicklerObject *self)
5550{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005551 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005552
5553 /* Note that we split the (pickle.py) stack into two stacks,
5554 * an object stack and a mark stack. We have to be clever and
5555 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005556 * mark stack first, and only signalling a stack underflow if
5557 * the object stack is empty and the mark stack doesn't match
5558 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005559 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005560 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005561 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005562 self->stack->mark_set = self->num_marks != 0;
5563 self->stack->fence = self->num_marks ?
5564 self->marks[self->num_marks - 1] : 0;
5565 } else if (len <= self->stack->fence)
5566 return Pdata_stack_underflow(self->stack);
5567 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005568 len--;
5569 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005570 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005571 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005572 return 0;
5573}
5574
5575static int
5576load_pop_mark(UnpicklerObject *self)
5577{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005578 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005579
5580 if ((i = marker(self)) < 0)
5581 return -1;
5582
5583 Pdata_clear(self->stack, i);
5584
5585 return 0;
5586}
5587
5588static int
5589load_dup(UnpicklerObject *self)
5590{
5591 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005592 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005593
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005594 if (len <= self->stack->fence)
5595 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005596 last = self->stack->data[len - 1];
5597 PDATA_APPEND(self->stack, last, -1);
5598 return 0;
5599}
5600
5601static int
5602load_get(UnpicklerObject *self)
5603{
5604 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005605 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 Py_ssize_t len;
5607 char *s;
5608
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005609 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005610 return -1;
5611 if (len < 2)
5612 return bad_readline();
5613
5614 key = PyLong_FromString(s, NULL, 10);
5615 if (key == NULL)
5616 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005617 idx = PyLong_AsSsize_t(key);
5618 if (idx == -1 && PyErr_Occurred()) {
5619 Py_DECREF(key);
5620 return -1;
5621 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005623 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005624 if (value == NULL) {
5625 if (!PyErr_Occurred())
5626 PyErr_SetObject(PyExc_KeyError, key);
5627 Py_DECREF(key);
5628 return -1;
5629 }
5630 Py_DECREF(key);
5631
5632 PDATA_APPEND(self->stack, value, -1);
5633 return 0;
5634}
5635
5636static int
5637load_binget(UnpicklerObject *self)
5638{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005639 PyObject *value;
5640 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005641 char *s;
5642
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005643 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644 return -1;
5645
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005646 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005647
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005648 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005650 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005651 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005652 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005653 Py_DECREF(key);
5654 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005655 return -1;
5656 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657
5658 PDATA_APPEND(self->stack, value, -1);
5659 return 0;
5660}
5661
5662static int
5663load_long_binget(UnpicklerObject *self)
5664{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005665 PyObject *value;
5666 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005667 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005668
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005669 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005670 return -1;
5671
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005672 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005673
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005674 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005675 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005676 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005677 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005678 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005679 Py_DECREF(key);
5680 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005681 return -1;
5682 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005683
5684 PDATA_APPEND(self->stack, value, -1);
5685 return 0;
5686}
5687
5688/* Push an object from the extension registry (EXT[124]). nbytes is
5689 * the number of bytes following the opcode, holding the index (code) value.
5690 */
5691static int
5692load_extension(UnpicklerObject *self, int nbytes)
5693{
5694 char *codebytes; /* the nbytes bytes after the opcode */
5695 long code; /* calc_binint returns long */
5696 PyObject *py_code; /* code as a Python int */
5697 PyObject *obj; /* the object to push */
5698 PyObject *pair; /* (module_name, class_name) */
5699 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005700 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005701
5702 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005703 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005704 return -1;
5705 code = calc_binint(codebytes, nbytes);
5706 if (code <= 0) { /* note that 0 is forbidden */
5707 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005708 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709 return -1;
5710 }
5711
5712 /* Look for the code in the cache. */
5713 py_code = PyLong_FromLong(code);
5714 if (py_code == NULL)
5715 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005716 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005717 if (obj != NULL) {
5718 /* Bingo. */
5719 Py_DECREF(py_code);
5720 PDATA_APPEND(self->stack, obj, -1);
5721 return 0;
5722 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005723 if (PyErr_Occurred()) {
5724 Py_DECREF(py_code);
5725 return -1;
5726 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005727
5728 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005729 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005730 if (pair == NULL) {
5731 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005732 if (!PyErr_Occurred()) {
5733 PyErr_Format(PyExc_ValueError, "unregistered extension "
5734 "code %ld", code);
5735 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005736 return -1;
5737 }
5738 /* Since the extension registry is manipulable via Python code,
5739 * confirm that pair is really a 2-tuple of strings.
5740 */
5741 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5742 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5743 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5744 Py_DECREF(py_code);
5745 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5746 "isn't a 2-tuple of strings", code);
5747 return -1;
5748 }
5749 /* Load the object. */
5750 obj = find_class(self, module_name, class_name);
5751 if (obj == NULL) {
5752 Py_DECREF(py_code);
5753 return -1;
5754 }
5755 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005756 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005757 Py_DECREF(py_code);
5758 if (code < 0) {
5759 Py_DECREF(obj);
5760 return -1;
5761 }
5762 PDATA_PUSH(self->stack, obj, -1);
5763 return 0;
5764}
5765
5766static int
5767load_put(UnpicklerObject *self)
5768{
5769 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005770 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005772 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005774 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005775 return -1;
5776 if (len < 2)
5777 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005778 if (Py_SIZE(self->stack) <= self->stack->fence)
5779 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005780 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781
5782 key = PyLong_FromString(s, NULL, 10);
5783 if (key == NULL)
5784 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005786 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005787 if (idx < 0) {
5788 if (!PyErr_Occurred())
5789 PyErr_SetString(PyExc_ValueError,
5790 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005791 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005792 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005793
5794 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005795}
5796
5797static int
5798load_binput(UnpicklerObject *self)
5799{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005800 PyObject *value;
5801 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005802 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005804 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005805 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005806
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005807 if (Py_SIZE(self->stack) <= self->stack->fence)
5808 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005809 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005810
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005811 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005812
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005813 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005814}
5815
5816static int
5817load_long_binput(UnpicklerObject *self)
5818{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005819 PyObject *value;
5820 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005821 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005822
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005823 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005824 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005825
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005826 if (Py_SIZE(self->stack) <= self->stack->fence)
5827 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005828 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005829
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005830 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005831 if (idx < 0) {
5832 PyErr_SetString(PyExc_ValueError,
5833 "negative LONG_BINPUT argument");
5834 return -1;
5835 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005836
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005837 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005838}
5839
5840static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005841load_memoize(UnpicklerObject *self)
5842{
5843 PyObject *value;
5844
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005845 if (Py_SIZE(self->stack) <= self->stack->fence)
5846 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005847 value = self->stack->data[Py_SIZE(self->stack) - 1];
5848
5849 return _Unpickler_MemoPut(self, self->memo_len, value);
5850}
5851
5852static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005853do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005854{
5855 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005856 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005857 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005858 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005859 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005860
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005861 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005862 if (x > len || x <= self->stack->fence)
5863 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005864 if (len == x) /* nothing to do */
5865 return 0;
5866
5867 list = self->stack->data[x - 1];
5868
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005869 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005870 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005871 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005872
5873 slice = Pdata_poplist(self->stack, x);
5874 if (!slice)
5875 return -1;
5876 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005877 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005878 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005879 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005880 }
5881 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005882 PyObject *extend_func;
5883 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005884
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005885 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
5886 if (extend_func != NULL) {
5887 slice = Pdata_poplist(self->stack, x);
5888 if (!slice) {
5889 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005890 return -1;
5891 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005892 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005893 Py_DECREF(extend_func);
5894 if (result == NULL)
5895 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005896 Py_DECREF(result);
5897 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005898 else {
5899 PyObject *append_func;
5900 _Py_IDENTIFIER(append);
5901
5902 /* Even if the PEP 307 requires extend() and append() methods,
5903 fall back on append() if the object has no extend() method
5904 for backward compatibility. */
5905 PyErr_Clear();
5906 append_func = _PyObject_GetAttrId(list, &PyId_append);
5907 if (append_func == NULL)
5908 return -1;
5909 for (i = x; i < len; i++) {
5910 value = self->stack->data[i];
5911 result = _Pickle_FastCall(append_func, value);
5912 if (result == NULL) {
5913 Pdata_clear(self->stack, i + 1);
5914 Py_SIZE(self->stack) = x;
5915 Py_DECREF(append_func);
5916 return -1;
5917 }
5918 Py_DECREF(result);
5919 }
5920 Py_SIZE(self->stack) = x;
5921 Py_DECREF(append_func);
5922 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923 }
5924
5925 return 0;
5926}
5927
5928static int
5929load_append(UnpicklerObject *self)
5930{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005931 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5932 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005933 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005934}
5935
5936static int
5937load_appends(UnpicklerObject *self)
5938{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005939 Py_ssize_t i = marker(self);
5940 if (i < 0)
5941 return -1;
5942 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005943}
5944
5945static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005946do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005947{
5948 PyObject *value, *key;
5949 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005950 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005951 int status = 0;
5952
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005953 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005954 if (x > len || x <= self->stack->fence)
5955 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005956 if (len == x) /* nothing to do */
5957 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005958 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005959 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005960 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005961 PyErr_SetString(st->UnpicklingError,
5962 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005963 return -1;
5964 }
5965
5966 /* Here, dict does not actually need to be a PyDict; it could be anything
5967 that supports the __setitem__ attribute. */
5968 dict = self->stack->data[x - 1];
5969
5970 for (i = x + 1; i < len; i += 2) {
5971 key = self->stack->data[i - 1];
5972 value = self->stack->data[i];
5973 if (PyObject_SetItem(dict, key, value) < 0) {
5974 status = -1;
5975 break;
5976 }
5977 }
5978
5979 Pdata_clear(self->stack, x);
5980 return status;
5981}
5982
5983static int
5984load_setitem(UnpicklerObject *self)
5985{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005986 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005987}
5988
5989static int
5990load_setitems(UnpicklerObject *self)
5991{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005992 Py_ssize_t i = marker(self);
5993 if (i < 0)
5994 return -1;
5995 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005996}
5997
5998static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005999load_additems(UnpicklerObject *self)
6000{
6001 PyObject *set;
6002 Py_ssize_t mark, len, i;
6003
6004 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006005 if (mark < 0)
6006 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006007 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006008 if (mark > len || mark <= self->stack->fence)
6009 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006010 if (len == mark) /* nothing to do */
6011 return 0;
6012
6013 set = self->stack->data[mark - 1];
6014
6015 if (PySet_Check(set)) {
6016 PyObject *items;
6017 int status;
6018
6019 items = Pdata_poptuple(self->stack, mark);
6020 if (items == NULL)
6021 return -1;
6022
6023 status = _PySet_Update(set, items);
6024 Py_DECREF(items);
6025 return status;
6026 }
6027 else {
6028 PyObject *add_func;
6029 _Py_IDENTIFIER(add);
6030
6031 add_func = _PyObject_GetAttrId(set, &PyId_add);
6032 if (add_func == NULL)
6033 return -1;
6034 for (i = mark; i < len; i++) {
6035 PyObject *result;
6036 PyObject *item;
6037
6038 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006039 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006040 if (result == NULL) {
6041 Pdata_clear(self->stack, i + 1);
6042 Py_SIZE(self->stack) = mark;
6043 return -1;
6044 }
6045 Py_DECREF(result);
6046 }
6047 Py_SIZE(self->stack) = mark;
6048 }
6049
6050 return 0;
6051}
6052
6053static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006054load_build(UnpicklerObject *self)
6055{
6056 PyObject *state, *inst, *slotstate;
6057 PyObject *setstate;
6058 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006059 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006060
6061 /* Stack is ... instance, state. We want to leave instance at
6062 * the stack top, possibly mutated via instance.__setstate__(state).
6063 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006064 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6065 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006066
6067 PDATA_POP(self->stack, state);
6068 if (state == NULL)
6069 return -1;
6070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006071 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006072
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006073 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006074 if (setstate == NULL) {
6075 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6076 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006077 else {
6078 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006079 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006080 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006081 }
6082 else {
6083 PyObject *result;
6084
6085 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006086 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006087 Py_DECREF(setstate);
6088 if (result == NULL)
6089 return -1;
6090 Py_DECREF(result);
6091 return 0;
6092 }
6093
6094 /* A default __setstate__. First see whether state embeds a
6095 * slot state dict too (a proto 2 addition).
6096 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006097 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006098 PyObject *tmp = state;
6099
6100 state = PyTuple_GET_ITEM(tmp, 0);
6101 slotstate = PyTuple_GET_ITEM(tmp, 1);
6102 Py_INCREF(state);
6103 Py_INCREF(slotstate);
6104 Py_DECREF(tmp);
6105 }
6106 else
6107 slotstate = NULL;
6108
6109 /* Set inst.__dict__ from the state dict (if any). */
6110 if (state != Py_None) {
6111 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006112 PyObject *d_key, *d_value;
6113 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006114 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006115
6116 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006117 PickleState *st = _Pickle_GetGlobalState();
6118 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006119 goto error;
6120 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006121 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006122 if (dict == NULL)
6123 goto error;
6124
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006125 i = 0;
6126 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6127 /* normally the keys for instance attributes are
6128 interned. we should try to do that here. */
6129 Py_INCREF(d_key);
6130 if (PyUnicode_CheckExact(d_key))
6131 PyUnicode_InternInPlace(&d_key);
6132 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6133 Py_DECREF(d_key);
6134 goto error;
6135 }
6136 Py_DECREF(d_key);
6137 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006138 Py_DECREF(dict);
6139 }
6140
6141 /* Also set instance attributes from the slotstate dict (if any). */
6142 if (slotstate != NULL) {
6143 PyObject *d_key, *d_value;
6144 Py_ssize_t i;
6145
6146 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006147 PickleState *st = _Pickle_GetGlobalState();
6148 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006149 "slot state is not a dictionary");
6150 goto error;
6151 }
6152 i = 0;
6153 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6154 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6155 goto error;
6156 }
6157 }
6158
6159 if (0) {
6160 error:
6161 status = -1;
6162 }
6163
6164 Py_DECREF(state);
6165 Py_XDECREF(slotstate);
6166 return status;
6167}
6168
6169static int
6170load_mark(UnpicklerObject *self)
6171{
6172
6173 /* Note that we split the (pickle.py) stack into two stacks, an
6174 * object stack and a mark stack. Here we push a mark onto the
6175 * mark stack.
6176 */
6177
6178 if ((self->num_marks + 1) >= self->marks_size) {
6179 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006180
6181 /* Use the size_t type to check for overflow. */
6182 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006183 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006184 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006185 PyErr_NoMemory();
6186 return -1;
6187 }
6188
6189 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006190 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006191 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006192 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6193 if (self->marks == NULL) {
6194 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006195 PyErr_NoMemory();
6196 return -1;
6197 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006198 self->marks_size = (Py_ssize_t)alloc;
6199 }
6200
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006201 self->stack->mark_set = 1;
6202 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006203
6204 return 0;
6205}
6206
6207static int
6208load_reduce(UnpicklerObject *self)
6209{
6210 PyObject *callable = NULL;
6211 PyObject *argtup = NULL;
6212 PyObject *obj = NULL;
6213
6214 PDATA_POP(self->stack, argtup);
6215 if (argtup == NULL)
6216 return -1;
6217 PDATA_POP(self->stack, callable);
6218 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006219 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006220 Py_DECREF(callable);
6221 }
6222 Py_DECREF(argtup);
6223
6224 if (obj == NULL)
6225 return -1;
6226
6227 PDATA_PUSH(self->stack, obj, -1);
6228 return 0;
6229}
6230
6231/* Just raises an error if we don't know the protocol specified. PROTO
6232 * is the first opcode for protocols >= 2.
6233 */
6234static int
6235load_proto(UnpicklerObject *self)
6236{
6237 char *s;
6238 int i;
6239
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006240 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241 return -1;
6242
6243 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006244 if (i <= HIGHEST_PROTOCOL) {
6245 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006246 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006247 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006248
6249 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6250 return -1;
6251}
6252
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006253static int
6254load_frame(UnpicklerObject *self)
6255{
6256 char *s;
6257 Py_ssize_t frame_len;
6258
6259 if (_Unpickler_Read(self, &s, 8) < 0)
6260 return -1;
6261
6262 frame_len = calc_binsize(s, 8);
6263 if (frame_len < 0) {
6264 PyErr_Format(PyExc_OverflowError,
6265 "FRAME length exceeds system's maximum of %zd bytes",
6266 PY_SSIZE_T_MAX);
6267 return -1;
6268 }
6269
6270 if (_Unpickler_Read(self, &s, frame_len) < 0)
6271 return -1;
6272
6273 /* Rewind to start of frame */
6274 self->next_read_idx -= frame_len;
6275 return 0;
6276}
6277
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006278static PyObject *
6279load(UnpicklerObject *self)
6280{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006281 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006282 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006283
6284 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006285 self->stack->mark_set = 0;
6286 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006287 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006288 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006289 Pdata_clear(self->stack, 0);
6290
6291 /* Convenient macros for the dispatch while-switch loop just below. */
6292#define OP(opcode, load_func) \
6293 case opcode: if (load_func(self) < 0) break; continue;
6294
6295#define OP_ARG(opcode, load_func, arg) \
6296 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6297
6298 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006299 if (_Unpickler_Read(self, &s, 1) < 0) {
6300 PickleState *st = _Pickle_GetGlobalState();
6301 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6302 PyErr_Format(PyExc_EOFError, "Ran out of input");
6303 }
6304 return NULL;
6305 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006306
6307 switch ((enum opcode)s[0]) {
6308 OP(NONE, load_none)
6309 OP(BININT, load_binint)
6310 OP(BININT1, load_binint1)
6311 OP(BININT2, load_binint2)
6312 OP(INT, load_int)
6313 OP(LONG, load_long)
6314 OP_ARG(LONG1, load_counted_long, 1)
6315 OP_ARG(LONG4, load_counted_long, 4)
6316 OP(FLOAT, load_float)
6317 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006318 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6319 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6320 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6321 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6322 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006323 OP(STRING, load_string)
6324 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006325 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6326 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6327 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006328 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6329 OP_ARG(TUPLE1, load_counted_tuple, 1)
6330 OP_ARG(TUPLE2, load_counted_tuple, 2)
6331 OP_ARG(TUPLE3, load_counted_tuple, 3)
6332 OP(TUPLE, load_tuple)
6333 OP(EMPTY_LIST, load_empty_list)
6334 OP(LIST, load_list)
6335 OP(EMPTY_DICT, load_empty_dict)
6336 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006337 OP(EMPTY_SET, load_empty_set)
6338 OP(ADDITEMS, load_additems)
6339 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006340 OP(OBJ, load_obj)
6341 OP(INST, load_inst)
6342 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006343 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006344 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006345 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006346 OP(APPEND, load_append)
6347 OP(APPENDS, load_appends)
6348 OP(BUILD, load_build)
6349 OP(DUP, load_dup)
6350 OP(BINGET, load_binget)
6351 OP(LONG_BINGET, load_long_binget)
6352 OP(GET, load_get)
6353 OP(MARK, load_mark)
6354 OP(BINPUT, load_binput)
6355 OP(LONG_BINPUT, load_long_binput)
6356 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006357 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006358 OP(POP, load_pop)
6359 OP(POP_MARK, load_pop_mark)
6360 OP(SETITEM, load_setitem)
6361 OP(SETITEMS, load_setitems)
6362 OP(PERSID, load_persid)
6363 OP(BINPERSID, load_binpersid)
6364 OP(REDUCE, load_reduce)
6365 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006366 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006367 OP_ARG(EXT1, load_extension, 1)
6368 OP_ARG(EXT2, load_extension, 2)
6369 OP_ARG(EXT4, load_extension, 4)
6370 OP_ARG(NEWTRUE, load_bool, Py_True)
6371 OP_ARG(NEWFALSE, load_bool, Py_False)
6372
6373 case STOP:
6374 break;
6375
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006376 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006377 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006378 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006379 unsigned char c = (unsigned char) *s;
6380 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6381 PyErr_Format(st->UnpicklingError,
6382 "invalid load key, '%c'.", c);
6383 }
6384 else {
6385 PyErr_Format(st->UnpicklingError,
6386 "invalid load key, '\\x%02x'.", c);
6387 }
6388 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006389 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006390 }
6391
6392 break; /* and we are done! */
6393 }
6394
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006395 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396 return NULL;
6397 }
6398
Victor Stinner2ae57e32013-10-31 13:39:23 +01006399 if (_Unpickler_SkipConsumed(self) < 0)
6400 return NULL;
6401
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006402 PDATA_POP(self->stack, value);
6403 return value;
6404}
6405
Larry Hastings61272b72014-01-07 12:41:53 -08006406/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006407
6408_pickle.Unpickler.load
6409
6410Load a pickle.
6411
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006412Read a pickled object representation from the open file object given
6413in the constructor, and return the reconstituted object hierarchy
6414specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006415[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006416
Larry Hastings3cceb382014-01-04 11:09:09 -08006417static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006418_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006419/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006420{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006421 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006422
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006423 /* Check whether the Unpickler was initialized correctly. This prevents
6424 segfaulting if a subclass overridden __init__ with a function that does
6425 not call Unpickler.__init__(). Here, we simply ensure that self->read
6426 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006427 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006428 PickleState *st = _Pickle_GetGlobalState();
6429 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006430 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006431 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006432 return NULL;
6433 }
6434
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006435 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006436}
6437
6438/* The name of find_class() is misleading. In newer pickle protocols, this
6439 function is used for loading any global (i.e., functions), not just
6440 classes. The name is kept only for backward compatibility. */
6441
Larry Hastings61272b72014-01-07 12:41:53 -08006442/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006443
6444_pickle.Unpickler.find_class
6445
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006446 module_name: object
6447 global_name: object
6448 /
6449
6450Return an object from a specified module.
6451
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006452If necessary, the module will be imported. Subclasses may override
6453this method (e.g. to restrict unpickling of arbitrary classes and
6454functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006455
6456This method is called whenever a class or a function object is
6457needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006458[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006459
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006460static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006461_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6462 PyObject *module_name,
6463 PyObject *global_name)
6464/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006465{
6466 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006467 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006468
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006469 /* Try to map the old names used in Python 2.x to the new ones used in
6470 Python 3.x. We do this only with old pickle protocols and when the
6471 user has not disabled the feature. */
6472 if (self->proto < 3 && self->fix_imports) {
6473 PyObject *key;
6474 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006475 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006476
6477 /* Check if the global (i.e., a function or a class) was renamed
6478 or moved to another module. */
6479 key = PyTuple_Pack(2, module_name, global_name);
6480 if (key == NULL)
6481 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006482 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006483 Py_DECREF(key);
6484 if (item) {
6485 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6486 PyErr_Format(PyExc_RuntimeError,
6487 "_compat_pickle.NAME_MAPPING values should be "
6488 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6489 return NULL;
6490 }
6491 module_name = PyTuple_GET_ITEM(item, 0);
6492 global_name = PyTuple_GET_ITEM(item, 1);
6493 if (!PyUnicode_Check(module_name) ||
6494 !PyUnicode_Check(global_name)) {
6495 PyErr_Format(PyExc_RuntimeError,
6496 "_compat_pickle.NAME_MAPPING values should be "
6497 "pairs of str, not (%.200s, %.200s)",
6498 Py_TYPE(module_name)->tp_name,
6499 Py_TYPE(global_name)->tp_name);
6500 return NULL;
6501 }
6502 }
6503 else if (PyErr_Occurred()) {
6504 return NULL;
6505 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006506 else {
6507 /* Check if the module was renamed. */
6508 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6509 if (item) {
6510 if (!PyUnicode_Check(item)) {
6511 PyErr_Format(PyExc_RuntimeError,
6512 "_compat_pickle.IMPORT_MAPPING values should be "
6513 "strings, not %.200s", Py_TYPE(item)->tp_name);
6514 return NULL;
6515 }
6516 module_name = item;
6517 }
6518 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006519 return NULL;
6520 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006521 }
6522 }
6523
Eric Snow3f9eee62017-09-15 16:35:20 -06006524 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006525 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006526 if (PyErr_Occurred())
6527 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006528 module = PyImport_Import(module_name);
6529 if (module == NULL)
6530 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006531 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006532 global = getattribute(module, global_name, self->proto >= 4);
6533 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006534 return global;
6535}
6536
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006537/*[clinic input]
6538
6539_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6540
6541Returns size in memory, in bytes.
6542[clinic start generated code]*/
6543
6544static Py_ssize_t
6545_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6546/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6547{
6548 Py_ssize_t res;
6549
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006550 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006551 if (self->memo != NULL)
6552 res += self->memo_size * sizeof(PyObject *);
6553 if (self->marks != NULL)
6554 res += self->marks_size * sizeof(Py_ssize_t);
6555 if (self->input_line != NULL)
6556 res += strlen(self->input_line) + 1;
6557 if (self->encoding != NULL)
6558 res += strlen(self->encoding) + 1;
6559 if (self->errors != NULL)
6560 res += strlen(self->errors) + 1;
6561 return res;
6562}
6563
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006564static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006565 _PICKLE_UNPICKLER_LOAD_METHODDEF
6566 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006567 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006568 {NULL, NULL} /* sentinel */
6569};
6570
6571static void
6572Unpickler_dealloc(UnpicklerObject *self)
6573{
6574 PyObject_GC_UnTrack((PyObject *)self);
6575 Py_XDECREF(self->readline);
6576 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006577 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006578 Py_XDECREF(self->stack);
6579 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006580 if (self->buffer.buf != NULL) {
6581 PyBuffer_Release(&self->buffer);
6582 self->buffer.buf = NULL;
6583 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006584
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006585 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006586 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006587 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006588 PyMem_Free(self->encoding);
6589 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006590
6591 Py_TYPE(self)->tp_free((PyObject *)self);
6592}
6593
6594static int
6595Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6596{
6597 Py_VISIT(self->readline);
6598 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006599 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006600 Py_VISIT(self->stack);
6601 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006602 return 0;
6603}
6604
6605static int
6606Unpickler_clear(UnpicklerObject *self)
6607{
6608 Py_CLEAR(self->readline);
6609 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006610 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006611 Py_CLEAR(self->stack);
6612 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006613 if (self->buffer.buf != NULL) {
6614 PyBuffer_Release(&self->buffer);
6615 self->buffer.buf = NULL;
6616 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006617
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006618 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006619 PyMem_Free(self->marks);
6620 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006621 PyMem_Free(self->input_line);
6622 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006623 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006624 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006625 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006626 self->errors = NULL;
6627
6628 return 0;
6629}
6630
Larry Hastings61272b72014-01-07 12:41:53 -08006631/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006632
6633_pickle.Unpickler.__init__
6634
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006635 file: object
6636 *
6637 fix_imports: bool = True
6638 encoding: str = 'ASCII'
6639 errors: str = 'strict'
6640
6641This takes a binary file for reading a pickle data stream.
6642
6643The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006644protocol argument is needed. Bytes past the pickled object's
6645representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006646
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006647The argument *file* must have two methods, a read() method that takes
6648an integer argument, and a readline() method that requires no
6649arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006650binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006651other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006652
6653Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006654which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006655generated by Python 2. If *fix_imports* is True, pickle will try to
6656map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006657*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006658instances pickled by Python 2; these default to 'ASCII' and 'strict',
6659respectively. The *encoding* can be 'bytes' to read these 8-bit
6660string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006661[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006662
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006663static int
Larry Hastings89964c42015-04-14 18:07:59 -04006664_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6665 int fix_imports, const char *encoding,
6666 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006667/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006668{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006669 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006670
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006671 /* In case of multiple __init__() calls, clear previous content. */
6672 if (self->read != NULL)
6673 (void)Unpickler_clear(self);
6674
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006675 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006676 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006677
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006678 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006679 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006680
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006681 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006682 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006683 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006684
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006685 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006686 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6687 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006688 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006689 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006690 }
6691 else {
6692 self->pers_func = NULL;
6693 }
6694
6695 self->stack = (Pdata *)Pdata_New();
6696 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006697 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006698
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006699 self->memo_size = 32;
6700 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006701 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006702 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006703
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006704 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006705
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006706 return 0;
6707}
6708
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006709
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006710/* Define a proxy object for the Unpickler's internal memo object. This is to
6711 * avoid breaking code like:
6712 * unpickler.memo.clear()
6713 * and
6714 * unpickler.memo = saved_memo
6715 * Is this a good idea? Not really, but we don't want to break code that uses
6716 * it. Note that we don't implement the entire mapping API here. This is
6717 * intentional, as these should be treated as black-box implementation details.
6718 *
6719 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006720 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006721 */
6722
Larry Hastings61272b72014-01-07 12:41:53 -08006723/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006724_pickle.UnpicklerMemoProxy.clear
6725
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006726Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006727[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006728
Larry Hastings3cceb382014-01-04 11:09:09 -08006729static PyObject *
6730_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006731/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006732{
6733 _Unpickler_MemoCleanup(self->unpickler);
6734 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6735 if (self->unpickler->memo == NULL)
6736 return NULL;
6737 Py_RETURN_NONE;
6738}
6739
Larry Hastings61272b72014-01-07 12:41:53 -08006740/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006741_pickle.UnpicklerMemoProxy.copy
6742
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006743Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006744[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006745
Larry Hastings3cceb382014-01-04 11:09:09 -08006746static PyObject *
6747_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006748/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006749{
6750 Py_ssize_t i;
6751 PyObject *new_memo = PyDict_New();
6752 if (new_memo == NULL)
6753 return NULL;
6754
6755 for (i = 0; i < self->unpickler->memo_size; i++) {
6756 int status;
6757 PyObject *key, *value;
6758
6759 value = self->unpickler->memo[i];
6760 if (value == NULL)
6761 continue;
6762
6763 key = PyLong_FromSsize_t(i);
6764 if (key == NULL)
6765 goto error;
6766 status = PyDict_SetItem(new_memo, key, value);
6767 Py_DECREF(key);
6768 if (status < 0)
6769 goto error;
6770 }
6771 return new_memo;
6772
6773error:
6774 Py_DECREF(new_memo);
6775 return NULL;
6776}
6777
Larry Hastings61272b72014-01-07 12:41:53 -08006778/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006779_pickle.UnpicklerMemoProxy.__reduce__
6780
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006781Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006782[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006783
Larry Hastings3cceb382014-01-04 11:09:09 -08006784static PyObject *
6785_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006786/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006787{
6788 PyObject *reduce_value;
6789 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006790 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006791 if (contents == NULL)
6792 return NULL;
6793
6794 reduce_value = PyTuple_New(2);
6795 if (reduce_value == NULL) {
6796 Py_DECREF(contents);
6797 return NULL;
6798 }
6799 constructor_args = PyTuple_New(1);
6800 if (constructor_args == NULL) {
6801 Py_DECREF(contents);
6802 Py_DECREF(reduce_value);
6803 return NULL;
6804 }
6805 PyTuple_SET_ITEM(constructor_args, 0, contents);
6806 Py_INCREF((PyObject *)&PyDict_Type);
6807 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6808 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6809 return reduce_value;
6810}
6811
6812static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006813 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6814 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6815 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006816 {NULL, NULL} /* sentinel */
6817};
6818
6819static void
6820UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6821{
6822 PyObject_GC_UnTrack(self);
6823 Py_XDECREF(self->unpickler);
6824 PyObject_GC_Del((PyObject *)self);
6825}
6826
6827static int
6828UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6829 visitproc visit, void *arg)
6830{
6831 Py_VISIT(self->unpickler);
6832 return 0;
6833}
6834
6835static int
6836UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6837{
6838 Py_CLEAR(self->unpickler);
6839 return 0;
6840}
6841
6842static PyTypeObject UnpicklerMemoProxyType = {
6843 PyVarObject_HEAD_INIT(NULL, 0)
6844 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6845 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6846 0,
6847 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6848 0, /* tp_print */
6849 0, /* tp_getattr */
6850 0, /* tp_setattr */
6851 0, /* tp_compare */
6852 0, /* tp_repr */
6853 0, /* tp_as_number */
6854 0, /* tp_as_sequence */
6855 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006856 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006857 0, /* tp_call */
6858 0, /* tp_str */
6859 PyObject_GenericGetAttr, /* tp_getattro */
6860 PyObject_GenericSetAttr, /* tp_setattro */
6861 0, /* tp_as_buffer */
6862 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6863 0, /* tp_doc */
6864 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6865 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6866 0, /* tp_richcompare */
6867 0, /* tp_weaklistoffset */
6868 0, /* tp_iter */
6869 0, /* tp_iternext */
6870 unpicklerproxy_methods, /* tp_methods */
6871};
6872
6873static PyObject *
6874UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6875{
6876 UnpicklerMemoProxyObject *self;
6877
6878 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6879 &UnpicklerMemoProxyType);
6880 if (self == NULL)
6881 return NULL;
6882 Py_INCREF(unpickler);
6883 self->unpickler = unpickler;
6884 PyObject_GC_Track(self);
6885 return (PyObject *)self;
6886}
6887
6888/*****************************************************************************/
6889
6890
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006891static PyObject *
6892Unpickler_get_memo(UnpicklerObject *self)
6893{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006894 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006895}
6896
6897static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006898Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006899{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006900 PyObject **new_memo;
6901 Py_ssize_t new_memo_size = 0;
6902 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006903
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006904 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006905 PyErr_SetString(PyExc_TypeError,
6906 "attribute deletion is not supported");
6907 return -1;
6908 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006909
6910 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6911 UnpicklerObject *unpickler =
6912 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6913
6914 new_memo_size = unpickler->memo_size;
6915 new_memo = _Unpickler_NewMemo(new_memo_size);
6916 if (new_memo == NULL)
6917 return -1;
6918
6919 for (i = 0; i < new_memo_size; i++) {
6920 Py_XINCREF(unpickler->memo[i]);
6921 new_memo[i] = unpickler->memo[i];
6922 }
6923 }
6924 else if (PyDict_Check(obj)) {
6925 Py_ssize_t i = 0;
6926 PyObject *key, *value;
6927
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006928 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006929 new_memo = _Unpickler_NewMemo(new_memo_size);
6930 if (new_memo == NULL)
6931 return -1;
6932
6933 while (PyDict_Next(obj, &i, &key, &value)) {
6934 Py_ssize_t idx;
6935 if (!PyLong_Check(key)) {
6936 PyErr_SetString(PyExc_TypeError,
6937 "memo key must be integers");
6938 goto error;
6939 }
6940 idx = PyLong_AsSsize_t(key);
6941 if (idx == -1 && PyErr_Occurred())
6942 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006943 if (idx < 0) {
6944 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006945 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006946 goto error;
6947 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006948 if (_Unpickler_MemoPut(self, idx, value) < 0)
6949 goto error;
6950 }
6951 }
6952 else {
6953 PyErr_Format(PyExc_TypeError,
6954 "'memo' attribute must be an UnpicklerMemoProxy object"
6955 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006956 return -1;
6957 }
6958
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006959 _Unpickler_MemoCleanup(self);
6960 self->memo_size = new_memo_size;
6961 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006962
6963 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006964
6965 error:
6966 if (new_memo_size) {
6967 i = new_memo_size;
6968 while (--i >= 0) {
6969 Py_XDECREF(new_memo[i]);
6970 }
6971 PyMem_FREE(new_memo);
6972 }
6973 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006974}
6975
6976static PyObject *
6977Unpickler_get_persload(UnpicklerObject *self)
6978{
6979 if (self->pers_func == NULL)
6980 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6981 else
6982 Py_INCREF(self->pers_func);
6983 return self->pers_func;
6984}
6985
6986static int
6987Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6988{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006989 if (value == NULL) {
6990 PyErr_SetString(PyExc_TypeError,
6991 "attribute deletion is not supported");
6992 return -1;
6993 }
6994 if (!PyCallable_Check(value)) {
6995 PyErr_SetString(PyExc_TypeError,
6996 "persistent_load must be a callable taking "
6997 "one argument");
6998 return -1;
6999 }
7000
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007001 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007002 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007003
7004 return 0;
7005}
7006
7007static PyGetSetDef Unpickler_getsets[] = {
7008 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7009 {"persistent_load", (getter)Unpickler_get_persload,
7010 (setter)Unpickler_set_persload},
7011 {NULL}
7012};
7013
7014static PyTypeObject Unpickler_Type = {
7015 PyVarObject_HEAD_INIT(NULL, 0)
7016 "_pickle.Unpickler", /*tp_name*/
7017 sizeof(UnpicklerObject), /*tp_basicsize*/
7018 0, /*tp_itemsize*/
7019 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7020 0, /*tp_print*/
7021 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007022 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007023 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007024 0, /*tp_repr*/
7025 0, /*tp_as_number*/
7026 0, /*tp_as_sequence*/
7027 0, /*tp_as_mapping*/
7028 0, /*tp_hash*/
7029 0, /*tp_call*/
7030 0, /*tp_str*/
7031 0, /*tp_getattro*/
7032 0, /*tp_setattro*/
7033 0, /*tp_as_buffer*/
7034 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007035 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007036 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7037 (inquiry)Unpickler_clear, /*tp_clear*/
7038 0, /*tp_richcompare*/
7039 0, /*tp_weaklistoffset*/
7040 0, /*tp_iter*/
7041 0, /*tp_iternext*/
7042 Unpickler_methods, /*tp_methods*/
7043 0, /*tp_members*/
7044 Unpickler_getsets, /*tp_getset*/
7045 0, /*tp_base*/
7046 0, /*tp_dict*/
7047 0, /*tp_descr_get*/
7048 0, /*tp_descr_set*/
7049 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007050 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007051 PyType_GenericAlloc, /*tp_alloc*/
7052 PyType_GenericNew, /*tp_new*/
7053 PyObject_GC_Del, /*tp_free*/
7054 0, /*tp_is_gc*/
7055};
7056
Larry Hastings61272b72014-01-07 12:41:53 -08007057/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007058
7059_pickle.dump
7060
7061 obj: object
7062 file: object
7063 protocol: object = NULL
7064 *
7065 fix_imports: bool = True
7066
7067Write a pickled representation of obj to the open file object file.
7068
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007069This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7070be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007071
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007072The optional *protocol* argument tells the pickler to use the given
7073protocol supported protocols are 0, 1, 2, 3 and 4. The default
7074protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007075
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007076Specifying a negative protocol version selects the highest protocol
7077version supported. The higher the protocol used, the more recent the
7078version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007079
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007080The *file* argument must have a write() method that accepts a single
7081bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007082writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007083this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007084
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007085If *fix_imports* is True and protocol is less than 3, pickle will try
7086to map the new Python 3 names to the old module names used in Python
70872, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007088[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007089
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007090static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007091_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007092 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007093/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007094{
7095 PicklerObject *pickler = _Pickler_New();
7096
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007097 if (pickler == NULL)
7098 return NULL;
7099
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007100 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007101 goto error;
7102
7103 if (_Pickler_SetOutputStream(pickler, file) < 0)
7104 goto error;
7105
7106 if (dump(pickler, obj) < 0)
7107 goto error;
7108
7109 if (_Pickler_FlushToFile(pickler) < 0)
7110 goto error;
7111
7112 Py_DECREF(pickler);
7113 Py_RETURN_NONE;
7114
7115 error:
7116 Py_XDECREF(pickler);
7117 return NULL;
7118}
7119
Larry Hastings61272b72014-01-07 12:41:53 -08007120/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007121
7122_pickle.dumps
7123
7124 obj: object
7125 protocol: object = NULL
7126 *
7127 fix_imports: bool = True
7128
7129Return the pickled representation of the object as a bytes object.
7130
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007131The optional *protocol* argument tells the pickler to use the given
7132protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7133protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007134
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007135Specifying a negative protocol version selects the highest protocol
7136version supported. The higher the protocol used, the more recent the
7137version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007138
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007139If *fix_imports* is True and *protocol* is less than 3, pickle will
7140try to map the new Python 3 names to the old module names used in
7141Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007142[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007143
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007144static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007145_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007146 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007147/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007148{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007149 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007150 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007151
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007152 if (pickler == NULL)
7153 return NULL;
7154
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007155 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007156 goto error;
7157
7158 if (dump(pickler, obj) < 0)
7159 goto error;
7160
7161 result = _Pickler_GetString(pickler);
7162 Py_DECREF(pickler);
7163 return result;
7164
7165 error:
7166 Py_XDECREF(pickler);
7167 return NULL;
7168}
7169
Larry Hastings61272b72014-01-07 12:41:53 -08007170/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007171
7172_pickle.load
7173
7174 file: object
7175 *
7176 fix_imports: bool = True
7177 encoding: str = 'ASCII'
7178 errors: str = 'strict'
7179
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007180Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007181
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007182This is equivalent to ``Unpickler(file).load()``, but may be more
7183efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007184
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007185The protocol version of the pickle is detected automatically, so no
7186protocol argument is needed. Bytes past the pickled object's
7187representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007189The argument *file* must have two methods, a read() method that takes
7190an integer argument, and a readline() method that requires no
7191arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007192binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007193other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007194
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007195Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007196which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007197generated by Python 2. If *fix_imports* is True, pickle will try to
7198map the old Python 2 names to the new names used in Python 3. The
7199*encoding* and *errors* tell pickle how to decode 8-bit string
7200instances pickled by Python 2; these default to 'ASCII' and 'strict',
7201respectively. The *encoding* can be 'bytes' to read these 8-bit
7202string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007203[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007204
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007205static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007206_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007207 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007208/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007209{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007210 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007211 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007212
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007213 if (unpickler == NULL)
7214 return NULL;
7215
7216 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7217 goto error;
7218
7219 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7220 goto error;
7221
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007222 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007223
7224 result = load(unpickler);
7225 Py_DECREF(unpickler);
7226 return result;
7227
7228 error:
7229 Py_XDECREF(unpickler);
7230 return NULL;
7231}
7232
Larry Hastings61272b72014-01-07 12:41:53 -08007233/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007234
7235_pickle.loads
7236
7237 data: object
7238 *
7239 fix_imports: bool = True
7240 encoding: str = 'ASCII'
7241 errors: str = 'strict'
7242
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007243Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007244
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007245The protocol version of the pickle is detected automatically, so no
7246protocol argument is needed. Bytes past the pickled object's
7247representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007248
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007249Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007250which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007251generated by Python 2. If *fix_imports* is True, pickle will try to
7252map the old Python 2 names to the new names used in Python 3. The
7253*encoding* and *errors* tell pickle how to decode 8-bit string
7254instances pickled by Python 2; these default to 'ASCII' and 'strict',
7255respectively. The *encoding* can be 'bytes' to read these 8-bit
7256string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007257[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007258
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007260_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007261 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007262/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007263{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007264 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007265 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007266
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007267 if (unpickler == NULL)
7268 return NULL;
7269
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007270 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007271 goto error;
7272
7273 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7274 goto error;
7275
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007276 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007277
7278 result = load(unpickler);
7279 Py_DECREF(unpickler);
7280 return result;
7281
7282 error:
7283 Py_XDECREF(unpickler);
7284 return NULL;
7285}
7286
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007287static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007288 _PICKLE_DUMP_METHODDEF
7289 _PICKLE_DUMPS_METHODDEF
7290 _PICKLE_LOAD_METHODDEF
7291 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007292 {NULL, NULL} /* sentinel */
7293};
7294
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007295static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007296pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007297{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007298 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007299 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007300}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007301
Stefan Krahf483b0f2013-12-14 13:43:10 +01007302static void
7303pickle_free(PyObject *m)
7304{
7305 _Pickle_ClearState(_Pickle_GetState(m));
7306}
7307
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007308static int
7309pickle_traverse(PyObject *m, visitproc visit, void *arg)
7310{
7311 PickleState *st = _Pickle_GetState(m);
7312 Py_VISIT(st->PickleError);
7313 Py_VISIT(st->PicklingError);
7314 Py_VISIT(st->UnpicklingError);
7315 Py_VISIT(st->dispatch_table);
7316 Py_VISIT(st->extension_registry);
7317 Py_VISIT(st->extension_cache);
7318 Py_VISIT(st->inverted_registry);
7319 Py_VISIT(st->name_mapping_2to3);
7320 Py_VISIT(st->import_mapping_2to3);
7321 Py_VISIT(st->name_mapping_3to2);
7322 Py_VISIT(st->import_mapping_3to2);
7323 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007324 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007325 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007326}
7327
7328static struct PyModuleDef _picklemodule = {
7329 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007330 "_pickle", /* m_name */
7331 pickle_module_doc, /* m_doc */
7332 sizeof(PickleState), /* m_size */
7333 pickle_methods, /* m_methods */
7334 NULL, /* m_reload */
7335 pickle_traverse, /* m_traverse */
7336 pickle_clear, /* m_clear */
7337 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007338};
7339
7340PyMODINIT_FUNC
7341PyInit__pickle(void)
7342{
7343 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007344 PickleState *st;
7345
7346 m = PyState_FindModule(&_picklemodule);
7347 if (m) {
7348 Py_INCREF(m);
7349 return m;
7350 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007351
7352 if (PyType_Ready(&Unpickler_Type) < 0)
7353 return NULL;
7354 if (PyType_Ready(&Pickler_Type) < 0)
7355 return NULL;
7356 if (PyType_Ready(&Pdata_Type) < 0)
7357 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007358 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7359 return NULL;
7360 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7361 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007362
7363 /* Create the module and add the functions. */
7364 m = PyModule_Create(&_picklemodule);
7365 if (m == NULL)
7366 return NULL;
7367
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007368 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007369 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7370 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007371 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007372 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7373 return NULL;
7374
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007375 st = _Pickle_GetState(m);
7376
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007377 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007378 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7379 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007380 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007381 st->PicklingError = \
7382 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7383 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007384 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007385 st->UnpicklingError = \
7386 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7387 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007388 return NULL;
7389
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007390 Py_INCREF(st->PickleError);
7391 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007392 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007393 Py_INCREF(st->PicklingError);
7394 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007395 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007396 Py_INCREF(st->UnpicklingError);
7397 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007398 return NULL;
7399
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007400 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007401 return NULL;
7402
7403 return m;
7404}