blob: fb69f14b53dbb4c873bc59ffef30ad2e5d76324b [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
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001652static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001653whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001654{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001655 PyObject *module_name;
1656 PyObject *modules_dict;
1657 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001658 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001659 _Py_IDENTIFIER(__module__);
1660 _Py_IDENTIFIER(modules);
1661 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001662
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001663 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1664
1665 if (module_name == NULL) {
1666 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001667 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001668 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001669 }
1670 else {
1671 /* In some rare cases (e.g., bound methods of extension types),
1672 __module__ can be None. If it is so, then search sys.modules for
1673 the module of global. */
1674 if (module_name != Py_None)
1675 return module_name;
1676 Py_CLEAR(module_name);
1677 }
1678 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001679
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001680 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001681 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001682 if (modules_dict == NULL) {
1683 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001684 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001685 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001686
1687 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001688 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1689 PyObject *candidate;
1690 if (PyUnicode_Check(module_name) &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001691 _PyUnicode_EqualToASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001692 continue;
1693 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001694 continue;
1695
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001696 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001697 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001698 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001699 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001700 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001701 continue;
1702 }
1703
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001704 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001705 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001706 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001707 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001708 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001709 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001710 }
1711
1712 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001713 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001714 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001715 return module_name;
1716}
1717
1718/* fast_save_enter() and fast_save_leave() are guards against recursive
1719 objects when Pickler is used with the "fast mode" (i.e., with object
1720 memoization disabled). If the nesting of a list or dict object exceed
1721 FAST_NESTING_LIMIT, these guards will start keeping an internal
1722 reference to the seen list or dict objects and check whether these objects
1723 are recursive. These are not strictly necessary, since save() has a
1724 hard-coded recursion limit, but they give a nicer error message than the
1725 typical RuntimeError. */
1726static int
1727fast_save_enter(PicklerObject *self, PyObject *obj)
1728{
1729 /* if fast_nesting < 0, we're doing an error exit. */
1730 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1731 PyObject *key = NULL;
1732 if (self->fast_memo == NULL) {
1733 self->fast_memo = PyDict_New();
1734 if (self->fast_memo == NULL) {
1735 self->fast_nesting = -1;
1736 return 0;
1737 }
1738 }
1739 key = PyLong_FromVoidPtr(obj);
1740 if (key == NULL)
1741 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001742 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001743 Py_DECREF(key);
1744 PyErr_Format(PyExc_ValueError,
1745 "fast mode: can't pickle cyclic objects "
1746 "including object type %.200s at %p",
1747 obj->ob_type->tp_name, obj);
1748 self->fast_nesting = -1;
1749 return 0;
1750 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001751 if (PyErr_Occurred()) {
1752 return 0;
1753 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001754 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1755 Py_DECREF(key);
1756 self->fast_nesting = -1;
1757 return 0;
1758 }
1759 Py_DECREF(key);
1760 }
1761 return 1;
1762}
1763
1764static int
1765fast_save_leave(PicklerObject *self, PyObject *obj)
1766{
1767 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1768 PyObject *key = PyLong_FromVoidPtr(obj);
1769 if (key == NULL)
1770 return 0;
1771 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1772 Py_DECREF(key);
1773 return 0;
1774 }
1775 Py_DECREF(key);
1776 }
1777 return 1;
1778}
1779
1780static int
1781save_none(PicklerObject *self, PyObject *obj)
1782{
1783 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001784 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001785 return -1;
1786
1787 return 0;
1788}
1789
1790static int
1791save_bool(PicklerObject *self, PyObject *obj)
1792{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001793 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001794 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001795 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001796 return -1;
1797 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001798 else {
1799 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1800 * so that unpicklers written before bools were introduced unpickle them
1801 * as ints, but unpicklers after can recognize that bools were intended.
1802 * Note that protocol 2 added direct ways to pickle bools.
1803 */
1804 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1805 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1806 return -1;
1807 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001808 return 0;
1809}
1810
1811static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001812save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001813{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001814 PyObject *repr = NULL;
1815 Py_ssize_t size;
1816 long val;
1817 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001818
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001819 const char long_op = LONG;
1820
1821 val= PyLong_AsLong(obj);
1822 if (val == -1 && PyErr_Occurred()) {
1823 /* out of range for int pickling */
1824 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001825 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001826 else if (self->bin &&
1827 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001828 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001829 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001830
1831 Note: we can't use -0x80000000L in the above condition because some
1832 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1833 before applying the unary minus when sizeof(long) <= 4. The
1834 resulting value stays unsigned which is commonly not what we want,
1835 so MSVC happily warns us about it. However, that result would have
1836 been fine because we guard for sizeof(long) <= 4 which turns the
1837 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001838 char pdata[32];
1839 Py_ssize_t len = 0;
1840
1841 pdata[1] = (unsigned char)(val & 0xff);
1842 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1843 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1844 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001845
1846 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1847 if (pdata[2] == 0) {
1848 pdata[0] = BININT1;
1849 len = 2;
1850 }
1851 else {
1852 pdata[0] = BININT2;
1853 len = 3;
1854 }
1855 }
1856 else {
1857 pdata[0] = BININT;
1858 len = 5;
1859 }
1860
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001861 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001862 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001863
1864 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 }
1866
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001867 if (self->proto >= 2) {
1868 /* Linear-time pickling. */
1869 size_t nbits;
1870 size_t nbytes;
1871 unsigned char *pdata;
1872 char header[5];
1873 int i;
1874 int sign = _PyLong_Sign(obj);
1875
1876 if (sign == 0) {
1877 header[0] = LONG1;
1878 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001879 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001880 goto error;
1881 return 0;
1882 }
1883 nbits = _PyLong_NumBits(obj);
1884 if (nbits == (size_t)-1 && PyErr_Occurred())
1885 goto error;
1886 /* How many bytes do we need? There are nbits >> 3 full
1887 * bytes of data, and nbits & 7 leftover bits. If there
1888 * are any leftover bits, then we clearly need another
1889 * byte. Wnat's not so obvious is that we *probably*
1890 * need another byte even if there aren't any leftovers:
1891 * the most-significant bit of the most-significant byte
1892 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001893 * opposite of the one we need. The exception is ints
1894 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001895 * its own 256's-complement, so has the right sign bit
1896 * even without the extra byte. That's a pain to check
1897 * for in advance, though, so we always grab an extra
1898 * byte at the start, and cut it back later if possible.
1899 */
1900 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001901 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001902 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001903 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001904 goto error;
1905 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001906 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001907 if (repr == NULL)
1908 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001909 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001910 i = _PyLong_AsByteArray((PyLongObject *)obj,
1911 pdata, nbytes,
1912 1 /* little endian */ , 1 /* signed */ );
1913 if (i < 0)
1914 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001915 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001916 * needed. This is so iff the MSB is all redundant sign
1917 * bits.
1918 */
1919 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001920 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001921 pdata[nbytes - 1] == 0xff &&
1922 (pdata[nbytes - 2] & 0x80) != 0) {
1923 nbytes--;
1924 }
1925
1926 if (nbytes < 256) {
1927 header[0] = LONG1;
1928 header[1] = (unsigned char)nbytes;
1929 size = 2;
1930 }
1931 else {
1932 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001933 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001934 for (i = 1; i < 5; i++) {
1935 header[i] = (unsigned char)(size & 0xff);
1936 size >>= 8;
1937 }
1938 size = 5;
1939 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001940 if (_Pickler_Write(self, header, size) < 0 ||
1941 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001942 goto error;
1943 }
1944 else {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001945 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001946
Mark Dickinson8dd05142009-01-20 20:43:58 +00001947 /* proto < 2: write the repr and newline. This is quadratic-time (in
1948 the number of digits), in both directions. We add a trailing 'L'
1949 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001950
1951 repr = PyObject_Repr(obj);
1952 if (repr == NULL)
1953 goto error;
1954
Serhiy Storchaka06515832016-11-20 09:13:07 +02001955 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001956 if (string == NULL)
1957 goto error;
1958
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001959 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1960 _Pickler_Write(self, string, size) < 0 ||
1961 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001962 goto error;
1963 }
1964
1965 if (0) {
1966 error:
1967 status = -1;
1968 }
1969 Py_XDECREF(repr);
1970
1971 return status;
1972}
1973
1974static int
1975save_float(PicklerObject *self, PyObject *obj)
1976{
1977 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1978
1979 if (self->bin) {
1980 char pdata[9];
1981 pdata[0] = BINFLOAT;
1982 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1983 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001984 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001985 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001986 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001987 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001988 int result = -1;
1989 char *buf = NULL;
1990 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001991
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001992 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001993 goto done;
1994
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001995 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001996 if (!buf) {
1997 PyErr_NoMemory();
1998 goto done;
1999 }
2000
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002001 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002002 goto done;
2003
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002004 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002005 goto done;
2006
2007 result = 0;
2008done:
2009 PyMem_Free(buf);
2010 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002011 }
2012
2013 return 0;
2014}
2015
2016static int
2017save_bytes(PicklerObject *self, PyObject *obj)
2018{
2019 if (self->proto < 3) {
2020 /* Older pickle protocols do not have an opcode for pickling bytes
2021 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002022 the __reduce__ method) to permit bytes object unpickling.
2023
2024 Here we use a hack to be compatible with Python 2. Since in Python
2025 2 'bytes' is just an alias for 'str' (which has different
2026 parameters than the actual bytes object), we use codecs.encode
2027 to create the appropriate 'str' object when unpickled using
2028 Python 2 *and* the appropriate 'bytes' object when unpickled
2029 using Python 3. Again this is a hack and we don't need to do this
2030 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002031 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 int status;
2033
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002034 if (PyBytes_GET_SIZE(obj) == 0) {
2035 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2036 }
2037 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002038 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002039 PyObject *unicode_str =
2040 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2041 PyBytes_GET_SIZE(obj),
2042 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002043 _Py_IDENTIFIER(latin1);
2044
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002045 if (unicode_str == NULL)
2046 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002047 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002048 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002049 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002050 Py_DECREF(unicode_str);
2051 }
2052
2053 if (reduce_value == NULL)
2054 return -1;
2055
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002056 /* save_reduce() will memoize the object automatically. */
2057 status = save_reduce(self, reduce_value, obj);
2058 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002059 return status;
2060 }
2061 else {
2062 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002063 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002064 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002065
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002066 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002067 if (size < 0)
2068 return -1;
2069
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002070 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002071 header[0] = SHORT_BINBYTES;
2072 header[1] = (unsigned char)size;
2073 len = 2;
2074 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002075 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 header[0] = BINBYTES;
2077 header[1] = (unsigned char)(size & 0xff);
2078 header[2] = (unsigned char)((size >> 8) & 0xff);
2079 header[3] = (unsigned char)((size >> 16) & 0xff);
2080 header[4] = (unsigned char)((size >> 24) & 0xff);
2081 len = 5;
2082 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002083 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002084 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002085 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002086 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002087 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002088 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002089 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002090 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002091 return -1; /* string too large */
2092 }
2093
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002094 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002095 return -1;
2096
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002097 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 return -1;
2099
2100 if (memo_put(self, obj) < 0)
2101 return -1;
2102
2103 return 0;
2104 }
2105}
2106
2107/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2108 backslash and newline characters to \uXXXX escapes. */
2109static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002110raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002111{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002112 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002113 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002114 void *data;
2115 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002116 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002117
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002118 if (PyUnicode_READY(obj))
2119 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002120
Victor Stinner358af132015-10-12 22:36:57 +02002121 _PyBytesWriter_Init(&writer);
2122
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002123 size = PyUnicode_GET_LENGTH(obj);
2124 data = PyUnicode_DATA(obj);
2125 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002126
Victor Stinner358af132015-10-12 22:36:57 +02002127 p = _PyBytesWriter_Alloc(&writer, size);
2128 if (p == NULL)
2129 goto error;
2130 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002131
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002132 for (i=0; i < size; i++) {
2133 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002134 /* Map 32-bit characters to '\Uxxxxxxxx' */
2135 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002136 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002137 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2138 if (p == NULL)
2139 goto error;
2140
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002141 *p++ = '\\';
2142 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002143 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2144 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2145 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2146 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2147 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2148 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2149 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2150 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002151 }
Victor Stinner358af132015-10-12 22:36:57 +02002152 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002153 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002154 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002155 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2156 if (p == NULL)
2157 goto error;
2158
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002159 *p++ = '\\';
2160 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002161 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2162 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2163 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2164 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002165 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002166 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002167 else
2168 *p++ = (char) ch;
2169 }
Victor Stinner358af132015-10-12 22:36:57 +02002170
2171 return _PyBytesWriter_Finish(&writer, p);
2172
2173error:
2174 _PyBytesWriter_Dealloc(&writer);
2175 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002176}
2177
2178static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002179write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002180{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002181 char header[9];
2182 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002183
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002184 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002185 if (size <= 0xff && self->proto >= 4) {
2186 header[0] = SHORT_BINUNICODE;
2187 header[1] = (unsigned char)(size & 0xff);
2188 len = 2;
2189 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002190 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002191 header[0] = BINUNICODE;
2192 header[1] = (unsigned char)(size & 0xff);
2193 header[2] = (unsigned char)((size >> 8) & 0xff);
2194 header[3] = (unsigned char)((size >> 16) & 0xff);
2195 header[4] = (unsigned char)((size >> 24) & 0xff);
2196 len = 5;
2197 }
2198 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002199 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002200 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002201 len = 9;
2202 }
2203 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002204 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002205 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002206 return -1;
2207 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002208
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002209 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002210 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002211 if (_Pickler_Write(self, data, size) < 0)
2212 return -1;
2213
2214 return 0;
2215}
2216
2217static int
2218write_unicode_binary(PicklerObject *self, PyObject *obj)
2219{
2220 PyObject *encoded = NULL;
2221 Py_ssize_t size;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002222 const char *data;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002223 int r;
2224
2225 if (PyUnicode_READY(obj))
2226 return -1;
2227
2228 data = PyUnicode_AsUTF8AndSize(obj, &size);
2229 if (data != NULL)
2230 return write_utf8(self, data, size);
2231
2232 /* Issue #8383: for strings with lone surrogates, fallback on the
2233 "surrogatepass" error handler. */
2234 PyErr_Clear();
2235 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2236 if (encoded == NULL)
2237 return -1;
2238
2239 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2240 PyBytes_GET_SIZE(encoded));
2241 Py_DECREF(encoded);
2242 return r;
2243}
2244
2245static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002246save_unicode(PicklerObject *self, PyObject *obj)
2247{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002248 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002249 if (write_unicode_binary(self, obj) < 0)
2250 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002251 }
2252 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002253 PyObject *encoded;
2254 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255 const char unicode_op = UNICODE;
2256
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002257 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002258 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002259 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002260
Antoine Pitrou299978d2013-04-07 17:38:11 +02002261 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2262 Py_DECREF(encoded);
2263 return -1;
2264 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002265
2266 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002267 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2268 Py_DECREF(encoded);
2269 return -1;
2270 }
2271 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002272
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002273 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002274 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002275 }
2276 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002277 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002278
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002280}
2281
2282/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2283static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002284store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002285{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002286 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287
2288 assert(PyTuple_Size(t) == len);
2289
2290 for (i = 0; i < len; i++) {
2291 PyObject *element = PyTuple_GET_ITEM(t, i);
2292
2293 if (element == NULL)
2294 return -1;
2295 if (save(self, element, 0) < 0)
2296 return -1;
2297 }
2298
2299 return 0;
2300}
2301
2302/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2303 * used across protocols to minimize the space needed to pickle them.
2304 * Tuples are also the only builtin immutable type that can be recursive
2305 * (a tuple can be reached from itself), and that requires some subtle
2306 * magic so that it works in all cases. IOW, this is a long routine.
2307 */
2308static int
2309save_tuple(PicklerObject *self, PyObject *obj)
2310{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002311 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002312
2313 const char mark_op = MARK;
2314 const char tuple_op = TUPLE;
2315 const char pop_op = POP;
2316 const char pop_mark_op = POP_MARK;
2317 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2318
2319 if ((len = PyTuple_Size(obj)) < 0)
2320 return -1;
2321
2322 if (len == 0) {
2323 char pdata[2];
2324
2325 if (self->proto) {
2326 pdata[0] = EMPTY_TUPLE;
2327 len = 1;
2328 }
2329 else {
2330 pdata[0] = MARK;
2331 pdata[1] = TUPLE;
2332 len = 2;
2333 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002334 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002335 return -1;
2336 return 0;
2337 }
2338
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002339 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002340 * saving the tuple elements, the tuple must be recursive, in
2341 * which case we'll pop everything we put on the stack, and fetch
2342 * its value from the memo.
2343 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002344 if (len <= 3 && self->proto >= 2) {
2345 /* Use TUPLE{1,2,3} opcodes. */
2346 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002347 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002348
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002349 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002350 /* pop the len elements */
2351 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002352 if (_Pickler_Write(self, &pop_op, 1) < 0)
2353 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002354 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002355 if (memo_get(self, obj) < 0)
2356 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002357
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358 return 0;
2359 }
2360 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002361 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2362 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002363 }
2364 goto memoize;
2365 }
2366
2367 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2368 * Generate MARK e1 e2 ... TUPLE
2369 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002370 if (_Pickler_Write(self, &mark_op, 1) < 0)
2371 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002372
2373 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002374 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002375
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002376 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002377 /* pop the stack stuff we pushed */
2378 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002379 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2380 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002381 }
2382 else {
2383 /* Note that we pop one more than len, to remove
2384 * the MARK too.
2385 */
2386 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002387 if (_Pickler_Write(self, &pop_op, 1) < 0)
2388 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002389 }
2390 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002391 if (memo_get(self, obj) < 0)
2392 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002393
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002394 return 0;
2395 }
2396 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002397 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2398 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002399 }
2400
2401 memoize:
2402 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002403 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002404
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002405 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002406}
2407
2408/* iter is an iterator giving items, and we batch up chunks of
2409 * MARK item item ... item APPENDS
2410 * opcode sequences. Calling code should have arranged to first create an
2411 * empty list, or list-like object, for the APPENDS to operate on.
2412 * Returns 0 on success, <0 on error.
2413 */
2414static int
2415batch_list(PicklerObject *self, PyObject *iter)
2416{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002417 PyObject *obj = NULL;
2418 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002419 int i, n;
2420
2421 const char mark_op = MARK;
2422 const char append_op = APPEND;
2423 const char appends_op = APPENDS;
2424
2425 assert(iter != NULL);
2426
2427 /* XXX: I think this function could be made faster by avoiding the
2428 iterator interface and fetching objects directly from list using
2429 PyList_GET_ITEM.
2430 */
2431
2432 if (self->proto == 0) {
2433 /* APPENDS isn't available; do one at a time. */
2434 for (;;) {
2435 obj = PyIter_Next(iter);
2436 if (obj == NULL) {
2437 if (PyErr_Occurred())
2438 return -1;
2439 break;
2440 }
2441 i = save(self, obj, 0);
2442 Py_DECREF(obj);
2443 if (i < 0)
2444 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002445 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002446 return -1;
2447 }
2448 return 0;
2449 }
2450
2451 /* proto > 0: write in batches of BATCHSIZE. */
2452 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002453 /* Get first item */
2454 firstitem = PyIter_Next(iter);
2455 if (firstitem == NULL) {
2456 if (PyErr_Occurred())
2457 goto error;
2458
2459 /* nothing more to add */
2460 break;
2461 }
2462
2463 /* Try to get a second item */
2464 obj = PyIter_Next(iter);
2465 if (obj == NULL) {
2466 if (PyErr_Occurred())
2467 goto error;
2468
2469 /* Only one item to write */
2470 if (save(self, firstitem, 0) < 0)
2471 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002472 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002473 goto error;
2474 Py_CLEAR(firstitem);
2475 break;
2476 }
2477
2478 /* More than one item to write */
2479
2480 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002481 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002482 goto error;
2483
2484 if (save(self, firstitem, 0) < 0)
2485 goto error;
2486 Py_CLEAR(firstitem);
2487 n = 1;
2488
2489 /* Fetch and save up to BATCHSIZE items */
2490 while (obj) {
2491 if (save(self, obj, 0) < 0)
2492 goto error;
2493 Py_CLEAR(obj);
2494 n += 1;
2495
2496 if (n == BATCHSIZE)
2497 break;
2498
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002499 obj = PyIter_Next(iter);
2500 if (obj == NULL) {
2501 if (PyErr_Occurred())
2502 goto error;
2503 break;
2504 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002505 }
2506
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002507 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002508 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002509
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002510 } while (n == BATCHSIZE);
2511 return 0;
2512
2513 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002514 Py_XDECREF(firstitem);
2515 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002516 return -1;
2517}
2518
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002519/* This is a variant of batch_list() above, specialized for lists (with no
2520 * support for list subclasses). Like batch_list(), we batch up chunks of
2521 * MARK item item ... item APPENDS
2522 * opcode sequences. Calling code should have arranged to first create an
2523 * empty list, or list-like object, for the APPENDS to operate on.
2524 * Returns 0 on success, -1 on error.
2525 *
2526 * This version is considerably faster than batch_list(), if less general.
2527 *
2528 * Note that this only works for protocols > 0.
2529 */
2530static int
2531batch_list_exact(PicklerObject *self, PyObject *obj)
2532{
2533 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002534 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002535
2536 const char append_op = APPEND;
2537 const char appends_op = APPENDS;
2538 const char mark_op = MARK;
2539
2540 assert(obj != NULL);
2541 assert(self->proto > 0);
2542 assert(PyList_CheckExact(obj));
2543
2544 if (PyList_GET_SIZE(obj) == 1) {
2545 item = PyList_GET_ITEM(obj, 0);
2546 if (save(self, item, 0) < 0)
2547 return -1;
2548 if (_Pickler_Write(self, &append_op, 1) < 0)
2549 return -1;
2550 return 0;
2551 }
2552
2553 /* Write in batches of BATCHSIZE. */
2554 total = 0;
2555 do {
2556 this_batch = 0;
2557 if (_Pickler_Write(self, &mark_op, 1) < 0)
2558 return -1;
2559 while (total < PyList_GET_SIZE(obj)) {
2560 item = PyList_GET_ITEM(obj, total);
2561 if (save(self, item, 0) < 0)
2562 return -1;
2563 total++;
2564 if (++this_batch == BATCHSIZE)
2565 break;
2566 }
2567 if (_Pickler_Write(self, &appends_op, 1) < 0)
2568 return -1;
2569
2570 } while (total < PyList_GET_SIZE(obj));
2571
2572 return 0;
2573}
2574
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002575static int
2576save_list(PicklerObject *self, PyObject *obj)
2577{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002578 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002579 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002580 int status = 0;
2581
2582 if (self->fast && !fast_save_enter(self, obj))
2583 goto error;
2584
2585 /* Create an empty list. */
2586 if (self->bin) {
2587 header[0] = EMPTY_LIST;
2588 len = 1;
2589 }
2590 else {
2591 header[0] = MARK;
2592 header[1] = LIST;
2593 len = 2;
2594 }
2595
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002596 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002597 goto error;
2598
2599 /* Get list length, and bow out early if empty. */
2600 if ((len = PyList_Size(obj)) < 0)
2601 goto error;
2602
2603 if (memo_put(self, obj) < 0)
2604 goto error;
2605
2606 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002607 /* Materialize the list elements. */
2608 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002609 if (Py_EnterRecursiveCall(" while pickling an object"))
2610 goto error;
2611 status = batch_list_exact(self, obj);
2612 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002613 } else {
2614 PyObject *iter = PyObject_GetIter(obj);
2615 if (iter == NULL)
2616 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002617
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002618 if (Py_EnterRecursiveCall(" while pickling an object")) {
2619 Py_DECREF(iter);
2620 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002621 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002622 status = batch_list(self, iter);
2623 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002624 Py_DECREF(iter);
2625 }
2626 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002627 if (0) {
2628 error:
2629 status = -1;
2630 }
2631
2632 if (self->fast && !fast_save_leave(self, obj))
2633 status = -1;
2634
2635 return status;
2636}
2637
2638/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2639 * MARK key value ... key value SETITEMS
2640 * opcode sequences. Calling code should have arranged to first create an
2641 * empty dict, or dict-like object, for the SETITEMS to operate on.
2642 * Returns 0 on success, <0 on error.
2643 *
2644 * This is very much like batch_list(). The difference between saving
2645 * elements directly, and picking apart two-tuples, is so long-winded at
2646 * the C level, though, that attempts to combine these routines were too
2647 * ugly to bear.
2648 */
2649static int
2650batch_dict(PicklerObject *self, PyObject *iter)
2651{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002652 PyObject *obj = NULL;
2653 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002654 int i, n;
2655
2656 const char mark_op = MARK;
2657 const char setitem_op = SETITEM;
2658 const char setitems_op = SETITEMS;
2659
2660 assert(iter != NULL);
2661
2662 if (self->proto == 0) {
2663 /* SETITEMS isn't available; do one at a time. */
2664 for (;;) {
2665 obj = PyIter_Next(iter);
2666 if (obj == NULL) {
2667 if (PyErr_Occurred())
2668 return -1;
2669 break;
2670 }
2671 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2672 PyErr_SetString(PyExc_TypeError, "dict items "
2673 "iterator must return 2-tuples");
2674 return -1;
2675 }
2676 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2677 if (i >= 0)
2678 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2679 Py_DECREF(obj);
2680 if (i < 0)
2681 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002682 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002683 return -1;
2684 }
2685 return 0;
2686 }
2687
2688 /* proto > 0: write in batches of BATCHSIZE. */
2689 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002690 /* Get first item */
2691 firstitem = PyIter_Next(iter);
2692 if (firstitem == NULL) {
2693 if (PyErr_Occurred())
2694 goto error;
2695
2696 /* nothing more to add */
2697 break;
2698 }
2699 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2700 PyErr_SetString(PyExc_TypeError, "dict items "
2701 "iterator must return 2-tuples");
2702 goto error;
2703 }
2704
2705 /* Try to get a second item */
2706 obj = PyIter_Next(iter);
2707 if (obj == NULL) {
2708 if (PyErr_Occurred())
2709 goto error;
2710
2711 /* Only one item to write */
2712 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2713 goto error;
2714 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2715 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002716 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002717 goto error;
2718 Py_CLEAR(firstitem);
2719 break;
2720 }
2721
2722 /* More than one item to write */
2723
2724 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002725 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002726 goto error;
2727
2728 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2729 goto error;
2730 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2731 goto error;
2732 Py_CLEAR(firstitem);
2733 n = 1;
2734
2735 /* Fetch and save up to BATCHSIZE items */
2736 while (obj) {
2737 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2738 PyErr_SetString(PyExc_TypeError, "dict items "
2739 "iterator must return 2-tuples");
2740 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002741 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002742 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2743 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2744 goto error;
2745 Py_CLEAR(obj);
2746 n += 1;
2747
2748 if (n == BATCHSIZE)
2749 break;
2750
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002751 obj = PyIter_Next(iter);
2752 if (obj == NULL) {
2753 if (PyErr_Occurred())
2754 goto error;
2755 break;
2756 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002757 }
2758
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002759 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002760 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002761
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002762 } while (n == BATCHSIZE);
2763 return 0;
2764
2765 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002766 Py_XDECREF(firstitem);
2767 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002768 return -1;
2769}
2770
Collin Winter5c9b02d2009-05-25 05:43:30 +00002771/* This is a variant of batch_dict() above that specializes for dicts, with no
2772 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2773 * MARK key value ... key value SETITEMS
2774 * opcode sequences. Calling code should have arranged to first create an
2775 * empty dict, or dict-like object, for the SETITEMS to operate on.
2776 * Returns 0 on success, -1 on error.
2777 *
2778 * Note that this currently doesn't work for protocol 0.
2779 */
2780static int
2781batch_dict_exact(PicklerObject *self, PyObject *obj)
2782{
2783 PyObject *key = NULL, *value = NULL;
2784 int i;
2785 Py_ssize_t dict_size, ppos = 0;
2786
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002787 const char mark_op = MARK;
2788 const char setitem_op = SETITEM;
2789 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002790
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002791 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002792 assert(self->proto > 0);
2793
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002794 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002795
2796 /* Special-case len(d) == 1 to save space. */
2797 if (dict_size == 1) {
2798 PyDict_Next(obj, &ppos, &key, &value);
2799 if (save(self, key, 0) < 0)
2800 return -1;
2801 if (save(self, value, 0) < 0)
2802 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002803 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002804 return -1;
2805 return 0;
2806 }
2807
2808 /* Write in batches of BATCHSIZE. */
2809 do {
2810 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002811 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002812 return -1;
2813 while (PyDict_Next(obj, &ppos, &key, &value)) {
2814 if (save(self, key, 0) < 0)
2815 return -1;
2816 if (save(self, value, 0) < 0)
2817 return -1;
2818 if (++i == BATCHSIZE)
2819 break;
2820 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002821 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002822 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002823 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00002824 PyErr_Format(
2825 PyExc_RuntimeError,
2826 "dictionary changed size during iteration");
2827 return -1;
2828 }
2829
2830 } while (i == BATCHSIZE);
2831 return 0;
2832}
2833
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002834static int
2835save_dict(PicklerObject *self, PyObject *obj)
2836{
2837 PyObject *items, *iter;
2838 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002839 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002840 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002841 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002842
2843 if (self->fast && !fast_save_enter(self, obj))
2844 goto error;
2845
2846 /* Create an empty dict. */
2847 if (self->bin) {
2848 header[0] = EMPTY_DICT;
2849 len = 1;
2850 }
2851 else {
2852 header[0] = MARK;
2853 header[1] = DICT;
2854 len = 2;
2855 }
2856
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002857 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002858 goto error;
2859
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002860 if (memo_put(self, obj) < 0)
2861 goto error;
2862
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002863 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002864 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002865 if (PyDict_CheckExact(obj) && self->proto > 0) {
2866 /* We can take certain shortcuts if we know this is a dict and
2867 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002868 if (Py_EnterRecursiveCall(" while pickling an object"))
2869 goto error;
2870 status = batch_dict_exact(self, obj);
2871 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002872 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002873 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002874
Victor Stinnerad8c83a2016-09-05 17:53:15 -07002875 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002876 if (items == NULL)
2877 goto error;
2878 iter = PyObject_GetIter(items);
2879 Py_DECREF(items);
2880 if (iter == NULL)
2881 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002882 if (Py_EnterRecursiveCall(" while pickling an object")) {
2883 Py_DECREF(iter);
2884 goto error;
2885 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002886 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002887 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002888 Py_DECREF(iter);
2889 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002890 }
2891
2892 if (0) {
2893 error:
2894 status = -1;
2895 }
2896
2897 if (self->fast && !fast_save_leave(self, obj))
2898 status = -1;
2899
2900 return status;
2901}
2902
2903static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002904save_set(PicklerObject *self, PyObject *obj)
2905{
2906 PyObject *item;
2907 int i;
2908 Py_ssize_t set_size, ppos = 0;
2909 Py_hash_t hash;
2910
2911 const char empty_set_op = EMPTY_SET;
2912 const char mark_op = MARK;
2913 const char additems_op = ADDITEMS;
2914
2915 if (self->proto < 4) {
2916 PyObject *items;
2917 PyObject *reduce_value;
2918 int status;
2919
2920 items = PySequence_List(obj);
2921 if (items == NULL) {
2922 return -1;
2923 }
2924 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2925 Py_DECREF(items);
2926 if (reduce_value == NULL) {
2927 return -1;
2928 }
2929 /* save_reduce() will memoize the object automatically. */
2930 status = save_reduce(self, reduce_value, obj);
2931 Py_DECREF(reduce_value);
2932 return status;
2933 }
2934
2935 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2936 return -1;
2937
2938 if (memo_put(self, obj) < 0)
2939 return -1;
2940
2941 set_size = PySet_GET_SIZE(obj);
2942 if (set_size == 0)
2943 return 0; /* nothing to do */
2944
2945 /* Write in batches of BATCHSIZE. */
2946 do {
2947 i = 0;
2948 if (_Pickler_Write(self, &mark_op, 1) < 0)
2949 return -1;
2950 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2951 if (save(self, item, 0) < 0)
2952 return -1;
2953 if (++i == BATCHSIZE)
2954 break;
2955 }
2956 if (_Pickler_Write(self, &additems_op, 1) < 0)
2957 return -1;
2958 if (PySet_GET_SIZE(obj) != set_size) {
2959 PyErr_Format(
2960 PyExc_RuntimeError,
2961 "set changed size during iteration");
2962 return -1;
2963 }
2964 } while (i == BATCHSIZE);
2965
2966 return 0;
2967}
2968
2969static int
2970save_frozenset(PicklerObject *self, PyObject *obj)
2971{
2972 PyObject *iter;
2973
2974 const char mark_op = MARK;
2975 const char frozenset_op = FROZENSET;
2976
2977 if (self->fast && !fast_save_enter(self, obj))
2978 return -1;
2979
2980 if (self->proto < 4) {
2981 PyObject *items;
2982 PyObject *reduce_value;
2983 int status;
2984
2985 items = PySequence_List(obj);
2986 if (items == NULL) {
2987 return -1;
2988 }
2989 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2990 items);
2991 Py_DECREF(items);
2992 if (reduce_value == NULL) {
2993 return -1;
2994 }
2995 /* save_reduce() will memoize the object automatically. */
2996 status = save_reduce(self, reduce_value, obj);
2997 Py_DECREF(reduce_value);
2998 return status;
2999 }
3000
3001 if (_Pickler_Write(self, &mark_op, 1) < 0)
3002 return -1;
3003
3004 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003005 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003006 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003007 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003008 for (;;) {
3009 PyObject *item;
3010
3011 item = PyIter_Next(iter);
3012 if (item == NULL) {
3013 if (PyErr_Occurred()) {
3014 Py_DECREF(iter);
3015 return -1;
3016 }
3017 break;
3018 }
3019 if (save(self, item, 0) < 0) {
3020 Py_DECREF(item);
3021 Py_DECREF(iter);
3022 return -1;
3023 }
3024 Py_DECREF(item);
3025 }
3026 Py_DECREF(iter);
3027
3028 /* If the object is already in the memo, this means it is
3029 recursive. In this case, throw away everything we put on the
3030 stack, and fetch the object back from the memo. */
3031 if (PyMemoTable_Get(self->memo, obj)) {
3032 const char pop_mark_op = POP_MARK;
3033
3034 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3035 return -1;
3036 if (memo_get(self, obj) < 0)
3037 return -1;
3038 return 0;
3039 }
3040
3041 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3042 return -1;
3043 if (memo_put(self, obj) < 0)
3044 return -1;
3045
3046 return 0;
3047}
3048
3049static int
3050fix_imports(PyObject **module_name, PyObject **global_name)
3051{
3052 PyObject *key;
3053 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003054 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003055
3056 key = PyTuple_Pack(2, *module_name, *global_name);
3057 if (key == NULL)
3058 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003059 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003060 Py_DECREF(key);
3061 if (item) {
3062 PyObject *fixed_module_name;
3063 PyObject *fixed_global_name;
3064
3065 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3066 PyErr_Format(PyExc_RuntimeError,
3067 "_compat_pickle.REVERSE_NAME_MAPPING values "
3068 "should be 2-tuples, not %.200s",
3069 Py_TYPE(item)->tp_name);
3070 return -1;
3071 }
3072 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3073 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3074 if (!PyUnicode_Check(fixed_module_name) ||
3075 !PyUnicode_Check(fixed_global_name)) {
3076 PyErr_Format(PyExc_RuntimeError,
3077 "_compat_pickle.REVERSE_NAME_MAPPING values "
3078 "should be pairs of str, not (%.200s, %.200s)",
3079 Py_TYPE(fixed_module_name)->tp_name,
3080 Py_TYPE(fixed_global_name)->tp_name);
3081 return -1;
3082 }
3083
3084 Py_CLEAR(*module_name);
3085 Py_CLEAR(*global_name);
3086 Py_INCREF(fixed_module_name);
3087 Py_INCREF(fixed_global_name);
3088 *module_name = fixed_module_name;
3089 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003090 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003091 }
3092 else if (PyErr_Occurred()) {
3093 return -1;
3094 }
3095
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003096 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003097 if (item) {
3098 if (!PyUnicode_Check(item)) {
3099 PyErr_Format(PyExc_RuntimeError,
3100 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3101 "should be strings, not %.200s",
3102 Py_TYPE(item)->tp_name);
3103 return -1;
3104 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003105 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003106 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003107 }
3108 else if (PyErr_Occurred()) {
3109 return -1;
3110 }
3111
3112 return 0;
3113}
3114
3115static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003116save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3117{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003118 PyObject *global_name = NULL;
3119 PyObject *module_name = NULL;
3120 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003121 PyObject *parent = NULL;
3122 PyObject *dotted_path = NULL;
3123 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003124 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003125 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003126 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003127 _Py_IDENTIFIER(__name__);
3128 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003129
3130 const char global_op = GLOBAL;
3131
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003132 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003133 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003134 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003135 }
3136 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003137 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3138 if (global_name == NULL) {
3139 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3140 goto error;
3141 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003142 }
3143 if (global_name == NULL) {
3144 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3145 if (global_name == NULL)
3146 goto error;
3147 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003148 }
3149
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003150 dotted_path = get_dotted_path(module, global_name);
3151 if (dotted_path == NULL)
3152 goto error;
3153 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003154 if (module_name == NULL)
3155 goto error;
3156
3157 /* XXX: Change to use the import C API directly with level=0 to disallow
3158 relative imports.
3159
3160 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3161 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3162 custom import functions (IMHO, this would be a nice security
3163 feature). The import C API would need to be extended to support the
3164 extra parameters of __import__ to fix that. */
3165 module = PyImport_Import(module_name);
3166 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003167 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003168 "Can't pickle %R: import of module %R failed",
3169 obj, module_name);
3170 goto error;
3171 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003172 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3173 Py_INCREF(lastname);
3174 cls = get_deep_attribute(module, dotted_path, &parent);
3175 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003176 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003177 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003178 "Can't pickle %R: attribute lookup %S on %S failed",
3179 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003180 goto error;
3181 }
3182 if (cls != obj) {
3183 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003184 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003185 "Can't pickle %R: it's not the same object as %S.%S",
3186 obj, module_name, global_name);
3187 goto error;
3188 }
3189 Py_DECREF(cls);
3190
3191 if (self->proto >= 2) {
3192 /* See whether this is in the extension registry, and if
3193 * so generate an EXT opcode.
3194 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003195 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003196 PyObject *code_obj; /* extension code as Python object */
3197 long code; /* extension code as C value */
3198 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003199 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003200
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003201 extension_key = PyTuple_Pack(2, module_name, global_name);
3202 if (extension_key == NULL) {
3203 goto error;
3204 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003205 code_obj = PyDict_GetItemWithError(st->extension_registry,
3206 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003207 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003208 /* The object is not registered in the extension registry.
3209 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003210 if (code_obj == NULL) {
3211 if (PyErr_Occurred()) {
3212 goto error;
3213 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003214 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003215 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003216
3217 /* XXX: pickle.py doesn't check neither the type, nor the range
3218 of the value returned by the extension_registry. It should for
3219 consistency. */
3220
3221 /* Verify code_obj has the right type and value. */
3222 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003223 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003224 "Can't pickle %R: extension code %R isn't an integer",
3225 obj, code_obj);
3226 goto error;
3227 }
3228 code = PyLong_AS_LONG(code_obj);
3229 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003230 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003231 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3232 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003233 goto error;
3234 }
3235
3236 /* Generate an EXT opcode. */
3237 if (code <= 0xff) {
3238 pdata[0] = EXT1;
3239 pdata[1] = (unsigned char)code;
3240 n = 2;
3241 }
3242 else if (code <= 0xffff) {
3243 pdata[0] = EXT2;
3244 pdata[1] = (unsigned char)(code & 0xff);
3245 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3246 n = 3;
3247 }
3248 else {
3249 pdata[0] = EXT4;
3250 pdata[1] = (unsigned char)(code & 0xff);
3251 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3252 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3253 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3254 n = 5;
3255 }
3256
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003257 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003258 goto error;
3259 }
3260 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003261 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003262 if (parent == module) {
3263 Py_INCREF(lastname);
3264 Py_DECREF(global_name);
3265 global_name = lastname;
3266 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003267 if (self->proto >= 4) {
3268 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003269
Christian Heimese8b1ba12013-11-23 21:13:39 +01003270 if (save(self, module_name, 0) < 0)
3271 goto error;
3272 if (save(self, global_name, 0) < 0)
3273 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003274
3275 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3276 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003277 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003278 else if (parent != module) {
3279 PickleState *st = _Pickle_GetGlobalState();
3280 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3281 st->getattr, parent, lastname);
3282 status = save_reduce(self, reduce_value, NULL);
3283 Py_DECREF(reduce_value);
3284 if (status < 0)
3285 goto error;
3286 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003287 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003288 /* Generate a normal global opcode if we are using a pickle
3289 protocol < 4, or if the object is not registered in the
3290 extension registry. */
3291 PyObject *encoded;
3292 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003293
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003294 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003295 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003296
3297 /* For protocol < 3 and if the user didn't request against doing
3298 so, we convert module names to the old 2.x module names. */
3299 if (self->proto < 3 && self->fix_imports) {
3300 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003301 goto error;
3302 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003303 }
3304
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003305 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3306 both the module name and the global name using UTF-8. We do so
3307 only when we are using the pickle protocol newer than version
3308 3. This is to ensure compatibility with older Unpickler running
3309 on Python 2.x. */
3310 if (self->proto == 3) {
3311 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003312 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003313 else {
3314 unicode_encoder = PyUnicode_AsASCIIString;
3315 }
3316 encoded = unicode_encoder(module_name);
3317 if (encoded == NULL) {
3318 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003319 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003320 "can't pickle module identifier '%S' using "
3321 "pickle protocol %i",
3322 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003323 goto error;
3324 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003325 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3326 PyBytes_GET_SIZE(encoded)) < 0) {
3327 Py_DECREF(encoded);
3328 goto error;
3329 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003330 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003331 if(_Pickler_Write(self, "\n", 1) < 0)
3332 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003333
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003334 /* Save the name of the module. */
3335 encoded = unicode_encoder(global_name);
3336 if (encoded == NULL) {
3337 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003338 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003339 "can't pickle global identifier '%S' using "
3340 "pickle protocol %i",
3341 global_name, self->proto);
3342 goto error;
3343 }
3344 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3345 PyBytes_GET_SIZE(encoded)) < 0) {
3346 Py_DECREF(encoded);
3347 goto error;
3348 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003349 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003350 if (_Pickler_Write(self, "\n", 1) < 0)
3351 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003352 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003353 /* Memoize the object. */
3354 if (memo_put(self, obj) < 0)
3355 goto error;
3356 }
3357
3358 if (0) {
3359 error:
3360 status = -1;
3361 }
3362 Py_XDECREF(module_name);
3363 Py_XDECREF(global_name);
3364 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003365 Py_XDECREF(parent);
3366 Py_XDECREF(dotted_path);
3367 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003368
3369 return status;
3370}
3371
3372static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003373save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3374{
3375 PyObject *reduce_value;
3376 int status;
3377
3378 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3379 if (reduce_value == NULL) {
3380 return -1;
3381 }
3382 status = save_reduce(self, reduce_value, obj);
3383 Py_DECREF(reduce_value);
3384 return status;
3385}
3386
3387static int
3388save_type(PicklerObject *self, PyObject *obj)
3389{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003390 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003391 return save_singleton_type(self, obj, Py_None);
3392 }
3393 else if (obj == (PyObject *)&PyEllipsis_Type) {
3394 return save_singleton_type(self, obj, Py_Ellipsis);
3395 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003396 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003397 return save_singleton_type(self, obj, Py_NotImplemented);
3398 }
3399 return save_global(self, obj, NULL);
3400}
3401
3402static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003403save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3404{
3405 PyObject *pid = NULL;
3406 int status = 0;
3407
3408 const char persid_op = PERSID;
3409 const char binpersid_op = BINPERSID;
3410
3411 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003412 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003413 if (pid == NULL)
3414 return -1;
3415
3416 if (pid != Py_None) {
3417 if (self->bin) {
3418 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003419 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003420 goto error;
3421 }
3422 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003423 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003424
3425 pid_str = PyObject_Str(pid);
3426 if (pid_str == NULL)
3427 goto error;
3428
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003429 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003430 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003431 if (!PyUnicode_IS_ASCII(pid_str)) {
3432 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3433 "persistent IDs in protocol 0 must be "
3434 "ASCII strings");
3435 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003436 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003437 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003438
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003439 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003440 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3441 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3442 _Pickler_Write(self, "\n", 1) < 0) {
3443 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003444 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003445 }
3446 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003447 }
3448 status = 1;
3449 }
3450
3451 if (0) {
3452 error:
3453 status = -1;
3454 }
3455 Py_XDECREF(pid);
3456
3457 return status;
3458}
3459
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003460static PyObject *
3461get_class(PyObject *obj)
3462{
3463 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003464 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003465
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003466 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003467 if (cls == NULL) {
3468 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3469 PyErr_Clear();
3470 cls = (PyObject *) Py_TYPE(obj);
3471 Py_INCREF(cls);
3472 }
3473 }
3474 return cls;
3475}
3476
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003477/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3478 * appropriate __reduce__ method for obj.
3479 */
3480static int
3481save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3482{
3483 PyObject *callable;
3484 PyObject *argtup;
3485 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003486 PyObject *listitems = Py_None;
3487 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003488 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003489 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003490 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003491
3492 const char reduce_op = REDUCE;
3493 const char build_op = BUILD;
3494 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003495 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003496
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003497 size = PyTuple_Size(args);
3498 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003499 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003500 "__reduce__ must contain 2 through 5 elements");
3501 return -1;
3502 }
3503
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003504 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3505 &callable, &argtup, &state, &listitems, &dictitems))
3506 return -1;
3507
3508 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003509 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003510 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003511 return -1;
3512 }
3513 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003514 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003515 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003516 return -1;
3517 }
3518
3519 if (state == Py_None)
3520 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003521
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003522 if (listitems == Py_None)
3523 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003524 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003525 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003526 "returned by __reduce__ must be an iterator, not %s",
3527 Py_TYPE(listitems)->tp_name);
3528 return -1;
3529 }
3530
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003531 if (dictitems == Py_None)
3532 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003533 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003534 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003535 "returned by __reduce__ must be an iterator, not %s",
3536 Py_TYPE(dictitems)->tp_name);
3537 return -1;
3538 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003539
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003540 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003541 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003542 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003543
Victor Stinner804e05e2013-11-14 01:26:17 +01003544 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003545 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003546 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003547 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003548 }
3549 PyErr_Clear();
3550 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003551 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003552 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003553 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3554 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003555 if (!use_newobj_ex) {
3556 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003557 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003558 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003559 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003560 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003561 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003562
3563 if (use_newobj_ex) {
3564 PyObject *cls;
3565 PyObject *args;
3566 PyObject *kwargs;
3567
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003568 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003569 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003570 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003571 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003572 return -1;
3573 }
3574
3575 cls = PyTuple_GET_ITEM(argtup, 0);
3576 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003577 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003578 "first item from NEWOBJ_EX argument tuple must "
3579 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3580 return -1;
3581 }
3582 args = PyTuple_GET_ITEM(argtup, 1);
3583 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003584 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003585 "second item from NEWOBJ_EX argument tuple must "
3586 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3587 return -1;
3588 }
3589 kwargs = PyTuple_GET_ITEM(argtup, 2);
3590 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003591 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003592 "third item from NEWOBJ_EX argument tuple must "
3593 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3594 return -1;
3595 }
3596
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003597 if (self->proto >= 4) {
3598 if (save(self, cls, 0) < 0 ||
3599 save(self, args, 0) < 0 ||
3600 save(self, kwargs, 0) < 0 ||
3601 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3602 return -1;
3603 }
3604 }
3605 else {
3606 PyObject *newargs;
3607 PyObject *cls_new;
3608 Py_ssize_t i;
3609 _Py_IDENTIFIER(__new__);
3610
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003611 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003612 if (newargs == NULL)
3613 return -1;
3614
3615 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3616 if (cls_new == NULL) {
3617 Py_DECREF(newargs);
3618 return -1;
3619 }
3620 PyTuple_SET_ITEM(newargs, 0, cls_new);
3621 Py_INCREF(cls);
3622 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003623 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003624 PyObject *item = PyTuple_GET_ITEM(args, i);
3625 Py_INCREF(item);
3626 PyTuple_SET_ITEM(newargs, i + 2, item);
3627 }
3628
3629 callable = PyObject_Call(st->partial, newargs, kwargs);
3630 Py_DECREF(newargs);
3631 if (callable == NULL)
3632 return -1;
3633
3634 newargs = PyTuple_New(0);
3635 if (newargs == NULL) {
3636 Py_DECREF(callable);
3637 return -1;
3638 }
3639
3640 if (save(self, callable, 0) < 0 ||
3641 save(self, newargs, 0) < 0 ||
3642 _Pickler_Write(self, &reduce_op, 1) < 0) {
3643 Py_DECREF(newargs);
3644 Py_DECREF(callable);
3645 return -1;
3646 }
3647 Py_DECREF(newargs);
3648 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003649 }
3650 }
3651 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003652 PyObject *cls;
3653 PyObject *newargtup;
3654 PyObject *obj_class;
3655 int p;
3656
3657 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003658 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003659 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003660 return -1;
3661 }
3662
3663 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003664 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003665 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003666 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003667 return -1;
3668 }
3669
3670 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003671 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003672 p = obj_class != cls; /* true iff a problem */
3673 Py_DECREF(obj_class);
3674 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003675 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003676 "__newobj__ args has the wrong class");
3677 return -1;
3678 }
3679 }
3680 /* XXX: These calls save() are prone to infinite recursion. Imagine
3681 what happen if the value returned by the __reduce__() method of
3682 some extension type contains another object of the same type. Ouch!
3683
3684 Here is a quick example, that I ran into, to illustrate what I
3685 mean:
3686
3687 >>> import pickle, copyreg
3688 >>> copyreg.dispatch_table.pop(complex)
3689 >>> pickle.dumps(1+2j)
3690 Traceback (most recent call last):
3691 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003692 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003693
3694 Removing the complex class from copyreg.dispatch_table made the
3695 __reduce_ex__() method emit another complex object:
3696
3697 >>> (1+1j).__reduce_ex__(2)
3698 (<function __newobj__ at 0xb7b71c3c>,
3699 (<class 'complex'>, (1+1j)), None, None, None)
3700
3701 Thus when save() was called on newargstup (the 2nd item) recursion
3702 ensued. Of course, the bug was in the complex class which had a
3703 broken __getnewargs__() that emitted another complex object. But,
3704 the point, here, is it is quite easy to end up with a broken reduce
3705 function. */
3706
3707 /* Save the class and its __new__ arguments. */
3708 if (save(self, cls, 0) < 0)
3709 return -1;
3710
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003711 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003712 if (newargtup == NULL)
3713 return -1;
3714
3715 p = save(self, newargtup, 0);
3716 Py_DECREF(newargtup);
3717 if (p < 0)
3718 return -1;
3719
3720 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003721 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003722 return -1;
3723 }
3724 else { /* Not using NEWOBJ. */
3725 if (save(self, callable, 0) < 0 ||
3726 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003727 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003728 return -1;
3729 }
3730
3731 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3732 the caller do not want to memoize the object. Not particularly useful,
3733 but that is to mimic the behavior save_reduce() in pickle.py when
3734 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003735 if (obj != NULL) {
3736 /* If the object is already in the memo, this means it is
3737 recursive. In this case, throw away everything we put on the
3738 stack, and fetch the object back from the memo. */
3739 if (PyMemoTable_Get(self->memo, obj)) {
3740 const char pop_op = POP;
3741
3742 if (_Pickler_Write(self, &pop_op, 1) < 0)
3743 return -1;
3744 if (memo_get(self, obj) < 0)
3745 return -1;
3746
3747 return 0;
3748 }
3749 else if (memo_put(self, obj) < 0)
3750 return -1;
3751 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003752
3753 if (listitems && batch_list(self, listitems) < 0)
3754 return -1;
3755
3756 if (dictitems && batch_dict(self, dictitems) < 0)
3757 return -1;
3758
3759 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003760 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003761 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003762 return -1;
3763 }
3764
3765 return 0;
3766}
3767
3768static int
3769save(PicklerObject *self, PyObject *obj, int pers_save)
3770{
3771 PyTypeObject *type;
3772 PyObject *reduce_func = NULL;
3773 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003774 int status = 0;
3775
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003776 if (_Pickler_OpcodeBoundary(self) < 0)
3777 return -1;
3778
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003779 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003780 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003781
3782 /* The extra pers_save argument is necessary to avoid calling save_pers()
3783 on its returned object. */
3784 if (!pers_save && self->pers_func) {
3785 /* save_pers() returns:
3786 -1 to signal an error;
3787 0 if it did nothing successfully;
3788 1 if a persistent id was saved.
3789 */
3790 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3791 goto done;
3792 }
3793
3794 type = Py_TYPE(obj);
3795
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003796 /* The old cPickle had an optimization that used switch-case statement
3797 dispatching on the first letter of the type name. This has was removed
3798 since benchmarks shown that this optimization was actually slowing
3799 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003800
3801 /* Atom types; these aren't memoized, so don't check the memo. */
3802
3803 if (obj == Py_None) {
3804 status = save_none(self, obj);
3805 goto done;
3806 }
3807 else if (obj == Py_False || obj == Py_True) {
3808 status = save_bool(self, obj);
3809 goto done;
3810 }
3811 else if (type == &PyLong_Type) {
3812 status = save_long(self, obj);
3813 goto done;
3814 }
3815 else if (type == &PyFloat_Type) {
3816 status = save_float(self, obj);
3817 goto done;
3818 }
3819
3820 /* Check the memo to see if it has the object. If so, generate
3821 a GET (or BINGET) opcode, instead of pickling the object
3822 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003823 if (PyMemoTable_Get(self->memo, obj)) {
3824 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003825 goto error;
3826 goto done;
3827 }
3828
3829 if (type == &PyBytes_Type) {
3830 status = save_bytes(self, obj);
3831 goto done;
3832 }
3833 else if (type == &PyUnicode_Type) {
3834 status = save_unicode(self, obj);
3835 goto done;
3836 }
3837 else if (type == &PyDict_Type) {
3838 status = save_dict(self, obj);
3839 goto done;
3840 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003841 else if (type == &PySet_Type) {
3842 status = save_set(self, obj);
3843 goto done;
3844 }
3845 else if (type == &PyFrozenSet_Type) {
3846 status = save_frozenset(self, obj);
3847 goto done;
3848 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003849 else if (type == &PyList_Type) {
3850 status = save_list(self, obj);
3851 goto done;
3852 }
3853 else if (type == &PyTuple_Type) {
3854 status = save_tuple(self, obj);
3855 goto done;
3856 }
3857 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003858 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003859 goto done;
3860 }
3861 else if (type == &PyFunction_Type) {
3862 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003863 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003864 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003865
3866 /* XXX: This part needs some unit tests. */
3867
3868 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003869 * self.dispatch_table, copyreg.dispatch_table, the object's
3870 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003871 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003872 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003873 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003874 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3875 (PyObject *)type);
3876 if (reduce_func == NULL) {
3877 if (PyErr_Occurred()) {
3878 goto error;
3879 }
3880 } else {
3881 /* PyDict_GetItemWithError() returns a borrowed reference.
3882 Increase the reference count to be consistent with
3883 PyObject_GetItem and _PyObject_GetAttrId used below. */
3884 Py_INCREF(reduce_func);
3885 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003886 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003887 reduce_func = PyObject_GetItem(self->dispatch_table,
3888 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003889 if (reduce_func == NULL) {
3890 if (PyErr_ExceptionMatches(PyExc_KeyError))
3891 PyErr_Clear();
3892 else
3893 goto error;
3894 }
3895 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003896 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003897 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003898 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003900 else if (PyType_IsSubtype(type, &PyType_Type)) {
3901 status = save_global(self, obj, NULL);
3902 goto done;
3903 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003904 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003905 _Py_IDENTIFIER(__reduce__);
3906 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003907
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003908
3909 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3910 automatically defined as __reduce__. While this is convenient, this
3911 make it impossible to know which method was actually called. Of
3912 course, this is not a big deal. But still, it would be nice to let
3913 the user know which method was called when something go
3914 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3915 don't actually have to check for a __reduce__ method. */
3916
3917 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003918 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003919 if (reduce_func != NULL) {
3920 PyObject *proto;
3921 proto = PyLong_FromLong(self->proto);
3922 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003923 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003924 }
3925 }
3926 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003927 PickleState *st = _Pickle_GetGlobalState();
3928
3929 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003930 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003931 }
3932 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003934 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003935 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003936 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003937 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02003938 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003939 }
3940 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003941 PyErr_Format(st->PicklingError,
3942 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943 type->tp_name, obj);
3944 goto error;
3945 }
3946 }
3947 }
3948
3949 if (reduce_value == NULL)
3950 goto error;
3951
3952 if (PyUnicode_Check(reduce_value)) {
3953 status = save_global(self, obj, reduce_value);
3954 goto done;
3955 }
3956
3957 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003958 PickleState *st = _Pickle_GetGlobalState();
3959 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003960 "__reduce__ must return a string or tuple");
3961 goto error;
3962 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003963
3964 status = save_reduce(self, reduce_value, obj);
3965
3966 if (0) {
3967 error:
3968 status = -1;
3969 }
3970 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003971
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003972 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 Py_XDECREF(reduce_func);
3974 Py_XDECREF(reduce_value);
3975
3976 return status;
3977}
3978
3979static int
3980dump(PicklerObject *self, PyObject *obj)
3981{
3982 const char stop_op = STOP;
3983
3984 if (self->proto >= 2) {
3985 char header[2];
3986
3987 header[0] = PROTO;
3988 assert(self->proto >= 0 && self->proto < 256);
3989 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003990 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003991 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003992 if (self->proto >= 4)
3993 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003994 }
3995
3996 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003997 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003998 return -1;
3999
4000 return 0;
4001}
4002
Larry Hastings61272b72014-01-07 12:41:53 -08004003/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004004
4005_pickle.Pickler.clear_memo
4006
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004007Clears the pickler's "memo".
4008
4009The memo is the data structure that remembers which objects the
4010pickler has already seen, so that shared or recursive objects are
4011pickled by reference and not by value. This method is useful when
4012re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004013[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004014
Larry Hastings3cceb382014-01-04 11:09:09 -08004015static PyObject *
4016_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004017/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004018{
4019 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004020 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004021
4022 Py_RETURN_NONE;
4023}
4024
Larry Hastings61272b72014-01-07 12:41:53 -08004025/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004026
4027_pickle.Pickler.dump
4028
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004029 obj: object
4030 /
4031
4032Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004033[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004034
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004035static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004036_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004037/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004038{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004039 /* Check whether the Pickler was initialized correctly (issue3664).
4040 Developers often forget to call __init__() in their subclasses, which
4041 would trigger a segfault without this check. */
4042 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004043 PickleState *st = _Pickle_GetGlobalState();
4044 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004045 "Pickler.__init__() was not called by %s.__init__()",
4046 Py_TYPE(self)->tp_name);
4047 return NULL;
4048 }
4049
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004050 if (_Pickler_ClearBuffer(self) < 0)
4051 return NULL;
4052
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004053 if (dump(self, obj) < 0)
4054 return NULL;
4055
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004056 if (_Pickler_FlushToFile(self) < 0)
4057 return NULL;
4058
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004059 Py_RETURN_NONE;
4060}
4061
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004062/*[clinic input]
4063
4064_pickle.Pickler.__sizeof__ -> Py_ssize_t
4065
4066Returns size in memory, in bytes.
4067[clinic start generated code]*/
4068
4069static Py_ssize_t
4070_pickle_Pickler___sizeof___impl(PicklerObject *self)
4071/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4072{
4073 Py_ssize_t res, s;
4074
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004075 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004076 if (self->memo != NULL) {
4077 res += sizeof(PyMemoTable);
4078 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4079 }
4080 if (self->output_buffer != NULL) {
4081 s = _PySys_GetSizeOf(self->output_buffer);
4082 if (s == -1)
4083 return -1;
4084 res += s;
4085 }
4086 return res;
4087}
4088
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004089static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004090 _PICKLE_PICKLER_DUMP_METHODDEF
4091 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004092 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 {NULL, NULL} /* sentinel */
4094};
4095
4096static void
4097Pickler_dealloc(PicklerObject *self)
4098{
4099 PyObject_GC_UnTrack(self);
4100
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004101 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004102 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004103 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004104 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004105 Py_XDECREF(self->fast_memo);
4106
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004107 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004108
4109 Py_TYPE(self)->tp_free((PyObject *)self);
4110}
4111
4112static int
4113Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4114{
4115 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004116 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004117 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004118 Py_VISIT(self->fast_memo);
4119 return 0;
4120}
4121
4122static int
4123Pickler_clear(PicklerObject *self)
4124{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004125 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004126 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004127 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004128 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129 Py_CLEAR(self->fast_memo);
4130
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004131 if (self->memo != NULL) {
4132 PyMemoTable *memo = self->memo;
4133 self->memo = NULL;
4134 PyMemoTable_Del(memo);
4135 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004136 return 0;
4137}
4138
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004139
Larry Hastings61272b72014-01-07 12:41:53 -08004140/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004141
4142_pickle.Pickler.__init__
4143
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004144 file: object
4145 protocol: object = NULL
4146 fix_imports: bool = True
4147
4148This takes a binary file for writing a pickle data stream.
4149
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004150The optional *protocol* argument tells the pickler to use the given
4151protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4152protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004153
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004154Specifying a negative protocol version selects the highest protocol
4155version supported. The higher the protocol used, the more recent the
4156version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004157
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004158The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004159bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004160writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004161this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004162
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004163If *fix_imports* is True and protocol is less than 3, pickle will try
4164to map the new Python 3 names to the old module names used in Python
41652, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004166[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004167
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004168static int
Larry Hastings89964c42015-04-14 18:07:59 -04004169_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4170 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004171/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004173 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004174 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004175
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004176 /* In case of multiple __init__() calls, clear previous content. */
4177 if (self->write != NULL)
4178 (void)Pickler_clear(self);
4179
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004180 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004181 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004182
4183 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004184 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004185
4186 /* memo and output_buffer may have already been created in _Pickler_New */
4187 if (self->memo == NULL) {
4188 self->memo = PyMemoTable_New();
4189 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004190 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004191 }
4192 self->output_len = 0;
4193 if (self->output_buffer == NULL) {
4194 self->max_output_len = WRITE_BUF_SIZE;
4195 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4196 self->max_output_len);
4197 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004198 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004199 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004200
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004201 self->fast = 0;
4202 self->fast_nesting = 0;
4203 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004204 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004205 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4206 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4207 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004208 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004209 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004210 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004211 self->dispatch_table = NULL;
4212 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4213 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4214 &PyId_dispatch_table);
4215 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004216 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004217 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004218
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004219 return 0;
4220}
4221
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004222
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004223/* Define a proxy object for the Pickler's internal memo object. This is to
4224 * avoid breaking code like:
4225 * pickler.memo.clear()
4226 * and
4227 * pickler.memo = saved_memo
4228 * Is this a good idea? Not really, but we don't want to break code that uses
4229 * it. Note that we don't implement the entire mapping API here. This is
4230 * intentional, as these should be treated as black-box implementation details.
4231 */
4232
Larry Hastings61272b72014-01-07 12:41:53 -08004233/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004234_pickle.PicklerMemoProxy.clear
4235
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004236Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004237[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004238
Larry Hastings3cceb382014-01-04 11:09:09 -08004239static PyObject *
4240_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004241/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004242{
4243 if (self->pickler->memo)
4244 PyMemoTable_Clear(self->pickler->memo);
4245 Py_RETURN_NONE;
4246}
4247
Larry Hastings61272b72014-01-07 12:41:53 -08004248/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004249_pickle.PicklerMemoProxy.copy
4250
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004251Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004252[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004253
Larry Hastings3cceb382014-01-04 11:09:09 -08004254static PyObject *
4255_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004256/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004257{
4258 Py_ssize_t i;
4259 PyMemoTable *memo;
4260 PyObject *new_memo = PyDict_New();
4261 if (new_memo == NULL)
4262 return NULL;
4263
4264 memo = self->pickler->memo;
4265 for (i = 0; i < memo->mt_allocated; ++i) {
4266 PyMemoEntry entry = memo->mt_table[i];
4267 if (entry.me_key != NULL) {
4268 int status;
4269 PyObject *key, *value;
4270
4271 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004272 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004273
4274 if (key == NULL || value == NULL) {
4275 Py_XDECREF(key);
4276 Py_XDECREF(value);
4277 goto error;
4278 }
4279 status = PyDict_SetItem(new_memo, key, value);
4280 Py_DECREF(key);
4281 Py_DECREF(value);
4282 if (status < 0)
4283 goto error;
4284 }
4285 }
4286 return new_memo;
4287
4288 error:
4289 Py_XDECREF(new_memo);
4290 return NULL;
4291}
4292
Larry Hastings61272b72014-01-07 12:41:53 -08004293/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004294_pickle.PicklerMemoProxy.__reduce__
4295
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004296Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004297[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004298
Larry Hastings3cceb382014-01-04 11:09:09 -08004299static PyObject *
4300_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004301/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004302{
4303 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004304 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004305 if (contents == NULL)
4306 return NULL;
4307
4308 reduce_value = PyTuple_New(2);
4309 if (reduce_value == NULL) {
4310 Py_DECREF(contents);
4311 return NULL;
4312 }
4313 dict_args = PyTuple_New(1);
4314 if (dict_args == NULL) {
4315 Py_DECREF(contents);
4316 Py_DECREF(reduce_value);
4317 return NULL;
4318 }
4319 PyTuple_SET_ITEM(dict_args, 0, contents);
4320 Py_INCREF((PyObject *)&PyDict_Type);
4321 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4322 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4323 return reduce_value;
4324}
4325
4326static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004327 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4328 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4329 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004330 {NULL, NULL} /* sentinel */
4331};
4332
4333static void
4334PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4335{
4336 PyObject_GC_UnTrack(self);
4337 Py_XDECREF(self->pickler);
4338 PyObject_GC_Del((PyObject *)self);
4339}
4340
4341static int
4342PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4343 visitproc visit, void *arg)
4344{
4345 Py_VISIT(self->pickler);
4346 return 0;
4347}
4348
4349static int
4350PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4351{
4352 Py_CLEAR(self->pickler);
4353 return 0;
4354}
4355
4356static PyTypeObject PicklerMemoProxyType = {
4357 PyVarObject_HEAD_INIT(NULL, 0)
4358 "_pickle.PicklerMemoProxy", /*tp_name*/
4359 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4360 0,
4361 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4362 0, /* tp_print */
4363 0, /* tp_getattr */
4364 0, /* tp_setattr */
4365 0, /* tp_compare */
4366 0, /* tp_repr */
4367 0, /* tp_as_number */
4368 0, /* tp_as_sequence */
4369 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004370 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004371 0, /* tp_call */
4372 0, /* tp_str */
4373 PyObject_GenericGetAttr, /* tp_getattro */
4374 PyObject_GenericSetAttr, /* tp_setattro */
4375 0, /* tp_as_buffer */
4376 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4377 0, /* tp_doc */
4378 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4379 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4380 0, /* tp_richcompare */
4381 0, /* tp_weaklistoffset */
4382 0, /* tp_iter */
4383 0, /* tp_iternext */
4384 picklerproxy_methods, /* tp_methods */
4385};
4386
4387static PyObject *
4388PicklerMemoProxy_New(PicklerObject *pickler)
4389{
4390 PicklerMemoProxyObject *self;
4391
4392 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4393 if (self == NULL)
4394 return NULL;
4395 Py_INCREF(pickler);
4396 self->pickler = pickler;
4397 PyObject_GC_Track(self);
4398 return (PyObject *)self;
4399}
4400
4401/*****************************************************************************/
4402
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004403static PyObject *
4404Pickler_get_memo(PicklerObject *self)
4405{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004406 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004407}
4408
4409static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004410Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004411{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004412 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004413
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004414 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004415 PyErr_SetString(PyExc_TypeError,
4416 "attribute deletion is not supported");
4417 return -1;
4418 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004419
4420 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4421 PicklerObject *pickler =
4422 ((PicklerMemoProxyObject *)obj)->pickler;
4423
4424 new_memo = PyMemoTable_Copy(pickler->memo);
4425 if (new_memo == NULL)
4426 return -1;
4427 }
4428 else if (PyDict_Check(obj)) {
4429 Py_ssize_t i = 0;
4430 PyObject *key, *value;
4431
4432 new_memo = PyMemoTable_New();
4433 if (new_memo == NULL)
4434 return -1;
4435
4436 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004437 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004438 PyObject *memo_obj;
4439
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004440 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004441 PyErr_SetString(PyExc_TypeError,
4442 "'memo' values must be 2-item tuples");
4443 goto error;
4444 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004445 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004446 if (memo_id == -1 && PyErr_Occurred())
4447 goto error;
4448 memo_obj = PyTuple_GET_ITEM(value, 1);
4449 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4450 goto error;
4451 }
4452 }
4453 else {
4454 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004455 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004456 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004457 return -1;
4458 }
4459
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004460 PyMemoTable_Del(self->memo);
4461 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004462
4463 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004464
4465 error:
4466 if (new_memo)
4467 PyMemoTable_Del(new_memo);
4468 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004469}
4470
4471static PyObject *
4472Pickler_get_persid(PicklerObject *self)
4473{
4474 if (self->pers_func == NULL)
4475 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4476 else
4477 Py_INCREF(self->pers_func);
4478 return self->pers_func;
4479}
4480
4481static int
4482Pickler_set_persid(PicklerObject *self, PyObject *value)
4483{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004484 if (value == NULL) {
4485 PyErr_SetString(PyExc_TypeError,
4486 "attribute deletion is not supported");
4487 return -1;
4488 }
4489 if (!PyCallable_Check(value)) {
4490 PyErr_SetString(PyExc_TypeError,
4491 "persistent_id must be a callable taking one argument");
4492 return -1;
4493 }
4494
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004495 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004496 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004497
4498 return 0;
4499}
4500
4501static PyMemberDef Pickler_members[] = {
4502 {"bin", T_INT, offsetof(PicklerObject, bin)},
4503 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004504 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004505 {NULL}
4506};
4507
4508static PyGetSetDef Pickler_getsets[] = {
4509 {"memo", (getter)Pickler_get_memo,
4510 (setter)Pickler_set_memo},
4511 {"persistent_id", (getter)Pickler_get_persid,
4512 (setter)Pickler_set_persid},
4513 {NULL}
4514};
4515
4516static PyTypeObject Pickler_Type = {
4517 PyVarObject_HEAD_INIT(NULL, 0)
4518 "_pickle.Pickler" , /*tp_name*/
4519 sizeof(PicklerObject), /*tp_basicsize*/
4520 0, /*tp_itemsize*/
4521 (destructor)Pickler_dealloc, /*tp_dealloc*/
4522 0, /*tp_print*/
4523 0, /*tp_getattr*/
4524 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004525 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004526 0, /*tp_repr*/
4527 0, /*tp_as_number*/
4528 0, /*tp_as_sequence*/
4529 0, /*tp_as_mapping*/
4530 0, /*tp_hash*/
4531 0, /*tp_call*/
4532 0, /*tp_str*/
4533 0, /*tp_getattro*/
4534 0, /*tp_setattro*/
4535 0, /*tp_as_buffer*/
4536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004537 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004538 (traverseproc)Pickler_traverse, /*tp_traverse*/
4539 (inquiry)Pickler_clear, /*tp_clear*/
4540 0, /*tp_richcompare*/
4541 0, /*tp_weaklistoffset*/
4542 0, /*tp_iter*/
4543 0, /*tp_iternext*/
4544 Pickler_methods, /*tp_methods*/
4545 Pickler_members, /*tp_members*/
4546 Pickler_getsets, /*tp_getset*/
4547 0, /*tp_base*/
4548 0, /*tp_dict*/
4549 0, /*tp_descr_get*/
4550 0, /*tp_descr_set*/
4551 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004552 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004553 PyType_GenericAlloc, /*tp_alloc*/
4554 PyType_GenericNew, /*tp_new*/
4555 PyObject_GC_Del, /*tp_free*/
4556 0, /*tp_is_gc*/
4557};
4558
Victor Stinner121aab42011-09-29 23:40:53 +02004559/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004560
4561 XXX: It would be nice to able to avoid Python function call overhead, by
4562 using directly the C version of find_class(), when find_class() is not
4563 overridden by a subclass. Although, this could become rather hackish. A
4564 simpler optimization would be to call the C function when self is not a
4565 subclass instance. */
4566static PyObject *
4567find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4568{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004569 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004570
Victor Stinner55ba38a2016-12-09 16:09:30 +01004571 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4572 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004573}
4574
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004575static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004576marker(UnpicklerObject *self)
4577{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004578 Py_ssize_t mark;
4579
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004580 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004581 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004582 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004583 return -1;
4584 }
4585
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004586 mark = self->marks[--self->num_marks];
4587 self->stack->mark_set = self->num_marks != 0;
4588 self->stack->fence = self->num_marks ?
4589 self->marks[self->num_marks - 1] : 0;
4590 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004591}
4592
4593static int
4594load_none(UnpicklerObject *self)
4595{
4596 PDATA_APPEND(self->stack, Py_None, -1);
4597 return 0;
4598}
4599
4600static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004601load_int(UnpicklerObject *self)
4602{
4603 PyObject *value;
4604 char *endptr, *s;
4605 Py_ssize_t len;
4606 long x;
4607
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004608 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004609 return -1;
4610 if (len < 2)
4611 return bad_readline();
4612
4613 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004614 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004615 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004616 x = strtol(s, &endptr, 0);
4617
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004618 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004619 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004620 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004621 errno = 0;
4622 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004623 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004624 if (value == NULL) {
4625 PyErr_SetString(PyExc_ValueError,
4626 "could not convert string to int");
4627 return -1;
4628 }
4629 }
4630 else {
4631 if (len == 3 && (x == 0 || x == 1)) {
4632 if ((value = PyBool_FromLong(x)) == NULL)
4633 return -1;
4634 }
4635 else {
4636 if ((value = PyLong_FromLong(x)) == NULL)
4637 return -1;
4638 }
4639 }
4640
4641 PDATA_PUSH(self->stack, value, -1);
4642 return 0;
4643}
4644
4645static int
4646load_bool(UnpicklerObject *self, PyObject *boolean)
4647{
4648 assert(boolean == Py_True || boolean == Py_False);
4649 PDATA_APPEND(self->stack, boolean, -1);
4650 return 0;
4651}
4652
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004653/* s contains x bytes of an unsigned little-endian integer. Return its value
4654 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4655 */
4656static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004657calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004658{
4659 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004660 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004661 size_t x = 0;
4662
Serhiy Storchakae0606192015-09-29 22:10:07 +03004663 if (nbytes > (int)sizeof(size_t)) {
4664 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4665 * have 64-bit size that can't be represented on 32-bit platform.
4666 */
4667 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4668 if (s[i])
4669 return -1;
4670 }
4671 nbytes = (int)sizeof(size_t);
4672 }
4673 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004674 x |= (size_t) s[i] << (8 * i);
4675 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004676
4677 if (x > PY_SSIZE_T_MAX)
4678 return -1;
4679 else
4680 return (Py_ssize_t) x;
4681}
4682
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004683/* s contains x bytes of a little-endian integer. Return its value as a
4684 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004685 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004686 * of x-platform bugs.
4687 */
4688static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004689calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004690{
4691 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004692 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004693 long x = 0;
4694
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004695 for (i = 0; i < nbytes; i++) {
4696 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004697 }
4698
4699 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4700 * is signed, so on a box with longs bigger than 4 bytes we need
4701 * to extend a BININT's sign bit to the full width.
4702 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004703 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004704 x |= -(x & (1L << 31));
4705 }
4706
4707 return x;
4708}
4709
4710static int
4711load_binintx(UnpicklerObject *self, char *s, int size)
4712{
4713 PyObject *value;
4714 long x;
4715
4716 x = calc_binint(s, size);
4717
4718 if ((value = PyLong_FromLong(x)) == NULL)
4719 return -1;
4720
4721 PDATA_PUSH(self->stack, value, -1);
4722 return 0;
4723}
4724
4725static int
4726load_binint(UnpicklerObject *self)
4727{
4728 char *s;
4729
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004730 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004731 return -1;
4732
4733 return load_binintx(self, s, 4);
4734}
4735
4736static int
4737load_binint1(UnpicklerObject *self)
4738{
4739 char *s;
4740
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004741 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004742 return -1;
4743
4744 return load_binintx(self, s, 1);
4745}
4746
4747static int
4748load_binint2(UnpicklerObject *self)
4749{
4750 char *s;
4751
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004752 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004753 return -1;
4754
4755 return load_binintx(self, s, 2);
4756}
4757
4758static int
4759load_long(UnpicklerObject *self)
4760{
4761 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004762 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004763 Py_ssize_t len;
4764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004765 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004766 return -1;
4767 if (len < 2)
4768 return bad_readline();
4769
Mark Dickinson8dd05142009-01-20 20:43:58 +00004770 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4771 the 'L' before calling PyLong_FromString. In order to maintain
4772 compatibility with Python 3.0.0, we don't actually *require*
4773 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004774 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004775 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004776 /* XXX: Should the base argument explicitly set to 10? */
4777 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004778 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004779 return -1;
4780
4781 PDATA_PUSH(self->stack, value, -1);
4782 return 0;
4783}
4784
4785/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4786 * data following.
4787 */
4788static int
4789load_counted_long(UnpicklerObject *self, int size)
4790{
4791 PyObject *value;
4792 char *nbytes;
4793 char *pdata;
4794
4795 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004796 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004797 return -1;
4798
4799 size = calc_binint(nbytes, size);
4800 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004801 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004802 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004803 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004804 "LONG pickle has negative byte count");
4805 return -1;
4806 }
4807
4808 if (size == 0)
4809 value = PyLong_FromLong(0L);
4810 else {
4811 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004812 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004813 return -1;
4814 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4815 1 /* little endian */ , 1 /* signed */ );
4816 }
4817 if (value == NULL)
4818 return -1;
4819 PDATA_PUSH(self->stack, value, -1);
4820 return 0;
4821}
4822
4823static int
4824load_float(UnpicklerObject *self)
4825{
4826 PyObject *value;
4827 char *endptr, *s;
4828 Py_ssize_t len;
4829 double d;
4830
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004831 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004832 return -1;
4833 if (len < 2)
4834 return bad_readline();
4835
4836 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004837 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4838 if (d == -1.0 && PyErr_Occurred())
4839 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004840 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004841 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4842 return -1;
4843 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004844 value = PyFloat_FromDouble(d);
4845 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004846 return -1;
4847
4848 PDATA_PUSH(self->stack, value, -1);
4849 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004850}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004851
4852static int
4853load_binfloat(UnpicklerObject *self)
4854{
4855 PyObject *value;
4856 double x;
4857 char *s;
4858
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004859 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004860 return -1;
4861
4862 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4863 if (x == -1.0 && PyErr_Occurred())
4864 return -1;
4865
4866 if ((value = PyFloat_FromDouble(x)) == NULL)
4867 return -1;
4868
4869 PDATA_PUSH(self->stack, value, -1);
4870 return 0;
4871}
4872
4873static int
4874load_string(UnpicklerObject *self)
4875{
4876 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004877 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004878 Py_ssize_t len;
4879 char *s, *p;
4880
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004881 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004882 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004883 /* Strip the newline */
4884 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004885 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004886 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004887 p = s + 1;
4888 len -= 2;
4889 }
4890 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004891 PickleState *st = _Pickle_GetGlobalState();
4892 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004893 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004894 return -1;
4895 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004896 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004897
4898 /* Use the PyBytes API to decode the string, since that is what is used
4899 to encode, and then coerce the result to Unicode. */
4900 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004901 if (bytes == NULL)
4902 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004903
4904 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4905 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4906 if (strcmp(self->encoding, "bytes") == 0) {
4907 obj = bytes;
4908 }
4909 else {
4910 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4911 Py_DECREF(bytes);
4912 if (obj == NULL) {
4913 return -1;
4914 }
4915 }
4916
4917 PDATA_PUSH(self->stack, obj, -1);
4918 return 0;
4919}
4920
4921static int
4922load_counted_binstring(UnpicklerObject *self, int nbytes)
4923{
4924 PyObject *obj;
4925 Py_ssize_t size;
4926 char *s;
4927
4928 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004929 return -1;
4930
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004931 size = calc_binsize(s, nbytes);
4932 if (size < 0) {
4933 PickleState *st = _Pickle_GetGlobalState();
4934 PyErr_Format(st->UnpicklingError,
4935 "BINSTRING exceeds system's maximum size of %zd bytes",
4936 PY_SSIZE_T_MAX);
4937 return -1;
4938 }
4939
4940 if (_Unpickler_Read(self, &s, size) < 0)
4941 return -1;
4942
4943 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4944 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4945 if (strcmp(self->encoding, "bytes") == 0) {
4946 obj = PyBytes_FromStringAndSize(s, size);
4947 }
4948 else {
4949 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4950 }
4951 if (obj == NULL) {
4952 return -1;
4953 }
4954
4955 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004956 return 0;
4957}
4958
4959static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004960load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004961{
4962 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004963 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004964 char *s;
4965
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004966 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004967 return -1;
4968
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004969 size = calc_binsize(s, nbytes);
4970 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004971 PyErr_Format(PyExc_OverflowError,
4972 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004973 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004974 return -1;
4975 }
4976
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004977 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004978 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004979
4980 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004981 if (bytes == NULL)
4982 return -1;
4983
4984 PDATA_PUSH(self->stack, bytes, -1);
4985 return 0;
4986}
4987
4988static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004989load_unicode(UnpicklerObject *self)
4990{
4991 PyObject *str;
4992 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004993 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004994
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004995 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004996 return -1;
4997 if (len < 1)
4998 return bad_readline();
4999
5000 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5001 if (str == NULL)
5002 return -1;
5003
5004 PDATA_PUSH(self->stack, str, -1);
5005 return 0;
5006}
5007
5008static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005009load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005010{
5011 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005012 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005013 char *s;
5014
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005015 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005016 return -1;
5017
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005018 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005019 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005020 PyErr_Format(PyExc_OverflowError,
5021 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005022 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005023 return -1;
5024 }
5025
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005026 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005027 return -1;
5028
Victor Stinner485fb562010-04-13 11:07:24 +00005029 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005030 if (str == NULL)
5031 return -1;
5032
5033 PDATA_PUSH(self->stack, str, -1);
5034 return 0;
5035}
5036
5037static int
Victor Stinner21b47112016-03-14 18:09:39 +01005038load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005039{
5040 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005041
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005042 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005043 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005045 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005046 if (tuple == NULL)
5047 return -1;
5048 PDATA_PUSH(self->stack, tuple, -1);
5049 return 0;
5050}
5051
5052static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005053load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005054{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005055 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005056
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005057 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005058 return -1;
5059
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005060 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005061}
5062
5063static int
5064load_empty_list(UnpicklerObject *self)
5065{
5066 PyObject *list;
5067
5068 if ((list = PyList_New(0)) == NULL)
5069 return -1;
5070 PDATA_PUSH(self->stack, list, -1);
5071 return 0;
5072}
5073
5074static int
5075load_empty_dict(UnpicklerObject *self)
5076{
5077 PyObject *dict;
5078
5079 if ((dict = PyDict_New()) == NULL)
5080 return -1;
5081 PDATA_PUSH(self->stack, dict, -1);
5082 return 0;
5083}
5084
5085static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005086load_empty_set(UnpicklerObject *self)
5087{
5088 PyObject *set;
5089
5090 if ((set = PySet_New(NULL)) == NULL)
5091 return -1;
5092 PDATA_PUSH(self->stack, set, -1);
5093 return 0;
5094}
5095
5096static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005097load_list(UnpicklerObject *self)
5098{
5099 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005100 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005101
5102 if ((i = marker(self)) < 0)
5103 return -1;
5104
5105 list = Pdata_poplist(self->stack, i);
5106 if (list == NULL)
5107 return -1;
5108 PDATA_PUSH(self->stack, list, -1);
5109 return 0;
5110}
5111
5112static int
5113load_dict(UnpicklerObject *self)
5114{
5115 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005116 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005117
5118 if ((i = marker(self)) < 0)
5119 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005120 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005121
5122 if ((dict = PyDict_New()) == NULL)
5123 return -1;
5124
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005125 if ((j - i) % 2 != 0) {
5126 PickleState *st = _Pickle_GetGlobalState();
5127 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005128 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005129 return -1;
5130 }
5131
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005132 for (k = i + 1; k < j; k += 2) {
5133 key = self->stack->data[k - 1];
5134 value = self->stack->data[k];
5135 if (PyDict_SetItem(dict, key, value) < 0) {
5136 Py_DECREF(dict);
5137 return -1;
5138 }
5139 }
5140 Pdata_clear(self->stack, i);
5141 PDATA_PUSH(self->stack, dict, -1);
5142 return 0;
5143}
5144
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005145static int
5146load_frozenset(UnpicklerObject *self)
5147{
5148 PyObject *items;
5149 PyObject *frozenset;
5150 Py_ssize_t i;
5151
5152 if ((i = marker(self)) < 0)
5153 return -1;
5154
5155 items = Pdata_poptuple(self->stack, i);
5156 if (items == NULL)
5157 return -1;
5158
5159 frozenset = PyFrozenSet_New(items);
5160 Py_DECREF(items);
5161 if (frozenset == NULL)
5162 return -1;
5163
5164 PDATA_PUSH(self->stack, frozenset, -1);
5165 return 0;
5166}
5167
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005168static PyObject *
5169instantiate(PyObject *cls, PyObject *args)
5170{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005171 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005172 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005173 /* Caller must assure args are a tuple. Normally, args come from
5174 Pdata_poptuple which packs objects from the top of the stack
5175 into a newly created tuple. */
5176 assert(PyTuple_Check(args));
Serhiy Storchakafff9a312017-03-21 08:53:25 +02005177 if (PyTuple_GET_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005178 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005179 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005180 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005181 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005182 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005183
Victor Stinner55ba38a2016-12-09 16:09:30 +01005184 result = _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005185 }
5186 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005187}
5188
5189static int
5190load_obj(UnpicklerObject *self)
5191{
5192 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005193 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005194
5195 if ((i = marker(self)) < 0)
5196 return -1;
5197
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005198 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005199 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005200
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005201 args = Pdata_poptuple(self->stack, i + 1);
5202 if (args == NULL)
5203 return -1;
5204
5205 PDATA_POP(self->stack, cls);
5206 if (cls) {
5207 obj = instantiate(cls, args);
5208 Py_DECREF(cls);
5209 }
5210 Py_DECREF(args);
5211 if (obj == NULL)
5212 return -1;
5213
5214 PDATA_PUSH(self->stack, obj, -1);
5215 return 0;
5216}
5217
5218static int
5219load_inst(UnpicklerObject *self)
5220{
5221 PyObject *cls = NULL;
5222 PyObject *args = NULL;
5223 PyObject *obj = NULL;
5224 PyObject *module_name;
5225 PyObject *class_name;
5226 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005227 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005228 char *s;
5229
5230 if ((i = marker(self)) < 0)
5231 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005232 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005233 return -1;
5234 if (len < 2)
5235 return bad_readline();
5236
5237 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5238 identifiers are permitted in Python 3.0, since the INST opcode is only
5239 supported by older protocols on Python 2.x. */
5240 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5241 if (module_name == NULL)
5242 return -1;
5243
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005244 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005245 if (len < 2) {
5246 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005247 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005248 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005249 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005250 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005251 cls = find_class(self, module_name, class_name);
5252 Py_DECREF(class_name);
5253 }
5254 }
5255 Py_DECREF(module_name);
5256
5257 if (cls == NULL)
5258 return -1;
5259
5260 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5261 obj = instantiate(cls, args);
5262 Py_DECREF(args);
5263 }
5264 Py_DECREF(cls);
5265
5266 if (obj == NULL)
5267 return -1;
5268
5269 PDATA_PUSH(self->stack, obj, -1);
5270 return 0;
5271}
5272
5273static int
5274load_newobj(UnpicklerObject *self)
5275{
5276 PyObject *args = NULL;
5277 PyObject *clsraw = NULL;
5278 PyTypeObject *cls; /* clsraw cast to its true type */
5279 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005280 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005281
5282 /* Stack is ... cls argtuple, and we want to call
5283 * cls.__new__(cls, *argtuple).
5284 */
5285 PDATA_POP(self->stack, args);
5286 if (args == NULL)
5287 goto error;
5288 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005289 PyErr_SetString(st->UnpicklingError,
5290 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005291 goto error;
5292 }
5293
5294 PDATA_POP(self->stack, clsraw);
5295 cls = (PyTypeObject *)clsraw;
5296 if (cls == NULL)
5297 goto error;
5298 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005299 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005300 "isn't a type object");
5301 goto error;
5302 }
5303 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005304 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005305 "has NULL tp_new");
5306 goto error;
5307 }
5308
5309 /* Call __new__. */
5310 obj = cls->tp_new(cls, args, NULL);
5311 if (obj == NULL)
5312 goto error;
5313
5314 Py_DECREF(args);
5315 Py_DECREF(clsraw);
5316 PDATA_PUSH(self->stack, obj, -1);
5317 return 0;
5318
5319 error:
5320 Py_XDECREF(args);
5321 Py_XDECREF(clsraw);
5322 return -1;
5323}
5324
5325static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005326load_newobj_ex(UnpicklerObject *self)
5327{
5328 PyObject *cls, *args, *kwargs;
5329 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005330 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005331
5332 PDATA_POP(self->stack, kwargs);
5333 if (kwargs == NULL) {
5334 return -1;
5335 }
5336 PDATA_POP(self->stack, args);
5337 if (args == NULL) {
5338 Py_DECREF(kwargs);
5339 return -1;
5340 }
5341 PDATA_POP(self->stack, cls);
5342 if (cls == NULL) {
5343 Py_DECREF(kwargs);
5344 Py_DECREF(args);
5345 return -1;
5346 }
Larry Hastings61272b72014-01-07 12:41:53 -08005347
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005348 if (!PyType_Check(cls)) {
5349 Py_DECREF(kwargs);
5350 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005351 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005352 "NEWOBJ_EX class argument must be a type, not %.200s",
5353 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005354 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005355 return -1;
5356 }
5357
5358 if (((PyTypeObject *)cls)->tp_new == NULL) {
5359 Py_DECREF(kwargs);
5360 Py_DECREF(args);
5361 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005362 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005363 "NEWOBJ_EX class argument doesn't have __new__");
5364 return -1;
5365 }
5366 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5367 Py_DECREF(kwargs);
5368 Py_DECREF(args);
5369 Py_DECREF(cls);
5370 if (obj == NULL) {
5371 return -1;
5372 }
5373 PDATA_PUSH(self->stack, obj, -1);
5374 return 0;
5375}
5376
5377static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005378load_global(UnpicklerObject *self)
5379{
5380 PyObject *global = NULL;
5381 PyObject *module_name;
5382 PyObject *global_name;
5383 Py_ssize_t len;
5384 char *s;
5385
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005386 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005387 return -1;
5388 if (len < 2)
5389 return bad_readline();
5390 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5391 if (!module_name)
5392 return -1;
5393
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005394 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005395 if (len < 2) {
5396 Py_DECREF(module_name);
5397 return bad_readline();
5398 }
5399 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5400 if (global_name) {
5401 global = find_class(self, module_name, global_name);
5402 Py_DECREF(global_name);
5403 }
5404 }
5405 Py_DECREF(module_name);
5406
5407 if (global == NULL)
5408 return -1;
5409 PDATA_PUSH(self->stack, global, -1);
5410 return 0;
5411}
5412
5413static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005414load_stack_global(UnpicklerObject *self)
5415{
5416 PyObject *global;
5417 PyObject *module_name;
5418 PyObject *global_name;
5419
5420 PDATA_POP(self->stack, global_name);
5421 PDATA_POP(self->stack, module_name);
5422 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5423 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005424 PickleState *st = _Pickle_GetGlobalState();
5425 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005426 Py_XDECREF(global_name);
5427 Py_XDECREF(module_name);
5428 return -1;
5429 }
5430 global = find_class(self, module_name, global_name);
5431 Py_DECREF(global_name);
5432 Py_DECREF(module_name);
5433 if (global == NULL)
5434 return -1;
5435 PDATA_PUSH(self->stack, global, -1);
5436 return 0;
5437}
5438
5439static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005440load_persid(UnpicklerObject *self)
5441{
5442 PyObject *pid;
5443 Py_ssize_t len;
5444 char *s;
5445
5446 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005447 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005448 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005449 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005450 return bad_readline();
5451
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005452 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5453 if (pid == NULL) {
5454 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5455 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5456 "persistent IDs in protocol 0 must be "
5457 "ASCII strings");
5458 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005459 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005460 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005462 /* This does not leak since _Pickle_FastCall() steals the reference
5463 to pid first. */
5464 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005465 if (pid == NULL)
5466 return -1;
5467
5468 PDATA_PUSH(self->stack, pid, -1);
5469 return 0;
5470 }
5471 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005472 PickleState *st = _Pickle_GetGlobalState();
5473 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005474 "A load persistent id instruction was encountered,\n"
5475 "but no persistent_load function was specified.");
5476 return -1;
5477 }
5478}
5479
5480static int
5481load_binpersid(UnpicklerObject *self)
5482{
5483 PyObject *pid;
5484
5485 if (self->pers_func) {
5486 PDATA_POP(self->stack, pid);
5487 if (pid == NULL)
5488 return -1;
5489
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005490 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005491 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005492 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005493 if (pid == NULL)
5494 return -1;
5495
5496 PDATA_PUSH(self->stack, pid, -1);
5497 return 0;
5498 }
5499 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005500 PickleState *st = _Pickle_GetGlobalState();
5501 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005502 "A load persistent id instruction was encountered,\n"
5503 "but no persistent_load function was specified.");
5504 return -1;
5505 }
5506}
5507
5508static int
5509load_pop(UnpicklerObject *self)
5510{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005511 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005512
5513 /* Note that we split the (pickle.py) stack into two stacks,
5514 * an object stack and a mark stack. We have to be clever and
5515 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005516 * mark stack first, and only signalling a stack underflow if
5517 * the object stack is empty and the mark stack doesn't match
5518 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005519 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005520 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005521 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005522 self->stack->mark_set = self->num_marks != 0;
5523 self->stack->fence = self->num_marks ?
5524 self->marks[self->num_marks - 1] : 0;
5525 } else if (len <= self->stack->fence)
5526 return Pdata_stack_underflow(self->stack);
5527 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005528 len--;
5529 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005530 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005531 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005532 return 0;
5533}
5534
5535static int
5536load_pop_mark(UnpicklerObject *self)
5537{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005538 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005539
5540 if ((i = marker(self)) < 0)
5541 return -1;
5542
5543 Pdata_clear(self->stack, i);
5544
5545 return 0;
5546}
5547
5548static int
5549load_dup(UnpicklerObject *self)
5550{
5551 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005552 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005554 if (len <= self->stack->fence)
5555 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005556 last = self->stack->data[len - 1];
5557 PDATA_APPEND(self->stack, last, -1);
5558 return 0;
5559}
5560
5561static int
5562load_get(UnpicklerObject *self)
5563{
5564 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005565 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005566 Py_ssize_t len;
5567 char *s;
5568
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005569 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005570 return -1;
5571 if (len < 2)
5572 return bad_readline();
5573
5574 key = PyLong_FromString(s, NULL, 10);
5575 if (key == NULL)
5576 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005577 idx = PyLong_AsSsize_t(key);
5578 if (idx == -1 && PyErr_Occurred()) {
5579 Py_DECREF(key);
5580 return -1;
5581 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005582
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005583 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005584 if (value == NULL) {
5585 if (!PyErr_Occurred())
5586 PyErr_SetObject(PyExc_KeyError, key);
5587 Py_DECREF(key);
5588 return -1;
5589 }
5590 Py_DECREF(key);
5591
5592 PDATA_APPEND(self->stack, value, -1);
5593 return 0;
5594}
5595
5596static int
5597load_binget(UnpicklerObject *self)
5598{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005599 PyObject *value;
5600 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601 char *s;
5602
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005603 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604 return -1;
5605
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005606 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005607
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005608 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005610 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005611 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005613 Py_DECREF(key);
5614 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615 return -1;
5616 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005617
5618 PDATA_APPEND(self->stack, value, -1);
5619 return 0;
5620}
5621
5622static int
5623load_long_binget(UnpicklerObject *self)
5624{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005625 PyObject *value;
5626 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005629 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 return -1;
5631
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005632 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005634 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005636 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005637 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005639 Py_DECREF(key);
5640 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005641 return -1;
5642 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005643
5644 PDATA_APPEND(self->stack, value, -1);
5645 return 0;
5646}
5647
5648/* Push an object from the extension registry (EXT[124]). nbytes is
5649 * the number of bytes following the opcode, holding the index (code) value.
5650 */
5651static int
5652load_extension(UnpicklerObject *self, int nbytes)
5653{
5654 char *codebytes; /* the nbytes bytes after the opcode */
5655 long code; /* calc_binint returns long */
5656 PyObject *py_code; /* code as a Python int */
5657 PyObject *obj; /* the object to push */
5658 PyObject *pair; /* (module_name, class_name) */
5659 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005660 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005661
5662 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005663 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005664 return -1;
5665 code = calc_binint(codebytes, nbytes);
5666 if (code <= 0) { /* note that 0 is forbidden */
5667 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005668 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005669 return -1;
5670 }
5671
5672 /* Look for the code in the cache. */
5673 py_code = PyLong_FromLong(code);
5674 if (py_code == NULL)
5675 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005676 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005677 if (obj != NULL) {
5678 /* Bingo. */
5679 Py_DECREF(py_code);
5680 PDATA_APPEND(self->stack, obj, -1);
5681 return 0;
5682 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005683 if (PyErr_Occurred()) {
5684 Py_DECREF(py_code);
5685 return -1;
5686 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687
5688 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005689 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 if (pair == NULL) {
5691 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005692 if (!PyErr_Occurred()) {
5693 PyErr_Format(PyExc_ValueError, "unregistered extension "
5694 "code %ld", code);
5695 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696 return -1;
5697 }
5698 /* Since the extension registry is manipulable via Python code,
5699 * confirm that pair is really a 2-tuple of strings.
5700 */
5701 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5702 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5703 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5704 Py_DECREF(py_code);
5705 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5706 "isn't a 2-tuple of strings", code);
5707 return -1;
5708 }
5709 /* Load the object. */
5710 obj = find_class(self, module_name, class_name);
5711 if (obj == NULL) {
5712 Py_DECREF(py_code);
5713 return -1;
5714 }
5715 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005716 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005717 Py_DECREF(py_code);
5718 if (code < 0) {
5719 Py_DECREF(obj);
5720 return -1;
5721 }
5722 PDATA_PUSH(self->stack, obj, -1);
5723 return 0;
5724}
5725
5726static int
5727load_put(UnpicklerObject *self)
5728{
5729 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005730 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005731 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005732 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005733
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005734 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005735 return -1;
5736 if (len < 2)
5737 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005738 if (Py_SIZE(self->stack) <= self->stack->fence)
5739 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005740 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005741
5742 key = PyLong_FromString(s, NULL, 10);
5743 if (key == NULL)
5744 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005745 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005746 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005747 if (idx < 0) {
5748 if (!PyErr_Occurred())
5749 PyErr_SetString(PyExc_ValueError,
5750 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005751 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005752 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005753
5754 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005755}
5756
5757static int
5758load_binput(UnpicklerObject *self)
5759{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005760 PyObject *value;
5761 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005762 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005764 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005767 if (Py_SIZE(self->stack) <= self->stack->fence)
5768 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005769 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005770
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005771 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005773 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774}
5775
5776static int
5777load_long_binput(UnpicklerObject *self)
5778{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005779 PyObject *value;
5780 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005782
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005783 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005786 if (Py_SIZE(self->stack) <= self->stack->fence)
5787 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005788 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005790 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005791 if (idx < 0) {
5792 PyErr_SetString(PyExc_ValueError,
5793 "negative LONG_BINPUT argument");
5794 return -1;
5795 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005796
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005797 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005798}
5799
5800static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005801load_memoize(UnpicklerObject *self)
5802{
5803 PyObject *value;
5804
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005805 if (Py_SIZE(self->stack) <= self->stack->fence)
5806 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005807 value = self->stack->data[Py_SIZE(self->stack) - 1];
5808
5809 return _Unpickler_MemoPut(self, self->memo_len, value);
5810}
5811
5812static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005813do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005814{
5815 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005816 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005818 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005819 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005820
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005821 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005822 if (x > len || x <= self->stack->fence)
5823 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005824 if (len == x) /* nothing to do */
5825 return 0;
5826
5827 list = self->stack->data[x - 1];
5828
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005829 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005830 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005831 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005832
5833 slice = Pdata_poplist(self->stack, x);
5834 if (!slice)
5835 return -1;
5836 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005837 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005838 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005839 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005840 }
5841 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005842 PyObject *extend_func;
5843 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005844
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005845 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
5846 if (extend_func != NULL) {
5847 slice = Pdata_poplist(self->stack, x);
5848 if (!slice) {
5849 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005850 return -1;
5851 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005852 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005853 Py_DECREF(extend_func);
5854 if (result == NULL)
5855 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005856 Py_DECREF(result);
5857 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005858 else {
5859 PyObject *append_func;
5860 _Py_IDENTIFIER(append);
5861
5862 /* Even if the PEP 307 requires extend() and append() methods,
5863 fall back on append() if the object has no extend() method
5864 for backward compatibility. */
5865 PyErr_Clear();
5866 append_func = _PyObject_GetAttrId(list, &PyId_append);
5867 if (append_func == NULL)
5868 return -1;
5869 for (i = x; i < len; i++) {
5870 value = self->stack->data[i];
5871 result = _Pickle_FastCall(append_func, value);
5872 if (result == NULL) {
5873 Pdata_clear(self->stack, i + 1);
5874 Py_SIZE(self->stack) = x;
5875 Py_DECREF(append_func);
5876 return -1;
5877 }
5878 Py_DECREF(result);
5879 }
5880 Py_SIZE(self->stack) = x;
5881 Py_DECREF(append_func);
5882 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005883 }
5884
5885 return 0;
5886}
5887
5888static int
5889load_append(UnpicklerObject *self)
5890{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005891 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5892 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005893 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005894}
5895
5896static int
5897load_appends(UnpicklerObject *self)
5898{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005899 Py_ssize_t i = marker(self);
5900 if (i < 0)
5901 return -1;
5902 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005903}
5904
5905static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005906do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005907{
5908 PyObject *value, *key;
5909 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005910 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005911 int status = 0;
5912
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005913 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005914 if (x > len || x <= self->stack->fence)
5915 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005916 if (len == x) /* nothing to do */
5917 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005918 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005919 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005920 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005921 PyErr_SetString(st->UnpicklingError,
5922 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923 return -1;
5924 }
5925
5926 /* Here, dict does not actually need to be a PyDict; it could be anything
5927 that supports the __setitem__ attribute. */
5928 dict = self->stack->data[x - 1];
5929
5930 for (i = x + 1; i < len; i += 2) {
5931 key = self->stack->data[i - 1];
5932 value = self->stack->data[i];
5933 if (PyObject_SetItem(dict, key, value) < 0) {
5934 status = -1;
5935 break;
5936 }
5937 }
5938
5939 Pdata_clear(self->stack, x);
5940 return status;
5941}
5942
5943static int
5944load_setitem(UnpicklerObject *self)
5945{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005946 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005947}
5948
5949static int
5950load_setitems(UnpicklerObject *self)
5951{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005952 Py_ssize_t i = marker(self);
5953 if (i < 0)
5954 return -1;
5955 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005956}
5957
5958static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005959load_additems(UnpicklerObject *self)
5960{
5961 PyObject *set;
5962 Py_ssize_t mark, len, i;
5963
5964 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005965 if (mark < 0)
5966 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005967 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005968 if (mark > len || mark <= self->stack->fence)
5969 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005970 if (len == mark) /* nothing to do */
5971 return 0;
5972
5973 set = self->stack->data[mark - 1];
5974
5975 if (PySet_Check(set)) {
5976 PyObject *items;
5977 int status;
5978
5979 items = Pdata_poptuple(self->stack, mark);
5980 if (items == NULL)
5981 return -1;
5982
5983 status = _PySet_Update(set, items);
5984 Py_DECREF(items);
5985 return status;
5986 }
5987 else {
5988 PyObject *add_func;
5989 _Py_IDENTIFIER(add);
5990
5991 add_func = _PyObject_GetAttrId(set, &PyId_add);
5992 if (add_func == NULL)
5993 return -1;
5994 for (i = mark; i < len; i++) {
5995 PyObject *result;
5996 PyObject *item;
5997
5998 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005999 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006000 if (result == NULL) {
6001 Pdata_clear(self->stack, i + 1);
6002 Py_SIZE(self->stack) = mark;
6003 return -1;
6004 }
6005 Py_DECREF(result);
6006 }
6007 Py_SIZE(self->stack) = mark;
6008 }
6009
6010 return 0;
6011}
6012
6013static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006014load_build(UnpicklerObject *self)
6015{
6016 PyObject *state, *inst, *slotstate;
6017 PyObject *setstate;
6018 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006019 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006020
6021 /* Stack is ... instance, state. We want to leave instance at
6022 * the stack top, possibly mutated via instance.__setstate__(state).
6023 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006024 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6025 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006026
6027 PDATA_POP(self->stack, state);
6028 if (state == NULL)
6029 return -1;
6030
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006031 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006032
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006033 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006034 if (setstate == NULL) {
6035 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6036 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006037 else {
6038 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006039 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006040 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006041 }
6042 else {
6043 PyObject *result;
6044
6045 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006046 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006047 Py_DECREF(setstate);
6048 if (result == NULL)
6049 return -1;
6050 Py_DECREF(result);
6051 return 0;
6052 }
6053
6054 /* A default __setstate__. First see whether state embeds a
6055 * slot state dict too (a proto 2 addition).
6056 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006057 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006058 PyObject *tmp = state;
6059
6060 state = PyTuple_GET_ITEM(tmp, 0);
6061 slotstate = PyTuple_GET_ITEM(tmp, 1);
6062 Py_INCREF(state);
6063 Py_INCREF(slotstate);
6064 Py_DECREF(tmp);
6065 }
6066 else
6067 slotstate = NULL;
6068
6069 /* Set inst.__dict__ from the state dict (if any). */
6070 if (state != Py_None) {
6071 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006072 PyObject *d_key, *d_value;
6073 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006074 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075
6076 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006077 PickleState *st = _Pickle_GetGlobalState();
6078 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079 goto error;
6080 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006081 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006082 if (dict == NULL)
6083 goto error;
6084
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006085 i = 0;
6086 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6087 /* normally the keys for instance attributes are
6088 interned. we should try to do that here. */
6089 Py_INCREF(d_key);
6090 if (PyUnicode_CheckExact(d_key))
6091 PyUnicode_InternInPlace(&d_key);
6092 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6093 Py_DECREF(d_key);
6094 goto error;
6095 }
6096 Py_DECREF(d_key);
6097 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006098 Py_DECREF(dict);
6099 }
6100
6101 /* Also set instance attributes from the slotstate dict (if any). */
6102 if (slotstate != NULL) {
6103 PyObject *d_key, *d_value;
6104 Py_ssize_t i;
6105
6106 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006107 PickleState *st = _Pickle_GetGlobalState();
6108 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006109 "slot state is not a dictionary");
6110 goto error;
6111 }
6112 i = 0;
6113 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6114 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6115 goto error;
6116 }
6117 }
6118
6119 if (0) {
6120 error:
6121 status = -1;
6122 }
6123
6124 Py_DECREF(state);
6125 Py_XDECREF(slotstate);
6126 return status;
6127}
6128
6129static int
6130load_mark(UnpicklerObject *self)
6131{
6132
6133 /* Note that we split the (pickle.py) stack into two stacks, an
6134 * object stack and a mark stack. Here we push a mark onto the
6135 * mark stack.
6136 */
6137
6138 if ((self->num_marks + 1) >= self->marks_size) {
6139 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006140
6141 /* Use the size_t type to check for overflow. */
6142 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006143 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006144 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006145 PyErr_NoMemory();
6146 return -1;
6147 }
6148
6149 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006150 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006151 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006152 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6153 if (self->marks == NULL) {
6154 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006155 PyErr_NoMemory();
6156 return -1;
6157 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006158 self->marks_size = (Py_ssize_t)alloc;
6159 }
6160
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006161 self->stack->mark_set = 1;
6162 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006163
6164 return 0;
6165}
6166
6167static int
6168load_reduce(UnpicklerObject *self)
6169{
6170 PyObject *callable = NULL;
6171 PyObject *argtup = NULL;
6172 PyObject *obj = NULL;
6173
6174 PDATA_POP(self->stack, argtup);
6175 if (argtup == NULL)
6176 return -1;
6177 PDATA_POP(self->stack, callable);
6178 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006179 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006180 Py_DECREF(callable);
6181 }
6182 Py_DECREF(argtup);
6183
6184 if (obj == NULL)
6185 return -1;
6186
6187 PDATA_PUSH(self->stack, obj, -1);
6188 return 0;
6189}
6190
6191/* Just raises an error if we don't know the protocol specified. PROTO
6192 * is the first opcode for protocols >= 2.
6193 */
6194static int
6195load_proto(UnpicklerObject *self)
6196{
6197 char *s;
6198 int i;
6199
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006200 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006201 return -1;
6202
6203 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006204 if (i <= HIGHEST_PROTOCOL) {
6205 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006207 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006208
6209 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6210 return -1;
6211}
6212
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006213static int
6214load_frame(UnpicklerObject *self)
6215{
6216 char *s;
6217 Py_ssize_t frame_len;
6218
6219 if (_Unpickler_Read(self, &s, 8) < 0)
6220 return -1;
6221
6222 frame_len = calc_binsize(s, 8);
6223 if (frame_len < 0) {
6224 PyErr_Format(PyExc_OverflowError,
6225 "FRAME length exceeds system's maximum of %zd bytes",
6226 PY_SSIZE_T_MAX);
6227 return -1;
6228 }
6229
6230 if (_Unpickler_Read(self, &s, frame_len) < 0)
6231 return -1;
6232
6233 /* Rewind to start of frame */
6234 self->next_read_idx -= frame_len;
6235 return 0;
6236}
6237
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006238static PyObject *
6239load(UnpicklerObject *self)
6240{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006242 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006243
6244 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006245 self->stack->mark_set = 0;
6246 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006247 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006248 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006249 Pdata_clear(self->stack, 0);
6250
6251 /* Convenient macros for the dispatch while-switch loop just below. */
6252#define OP(opcode, load_func) \
6253 case opcode: if (load_func(self) < 0) break; continue;
6254
6255#define OP_ARG(opcode, load_func, arg) \
6256 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6257
6258 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006259 if (_Unpickler_Read(self, &s, 1) < 0) {
6260 PickleState *st = _Pickle_GetGlobalState();
6261 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6262 PyErr_Format(PyExc_EOFError, "Ran out of input");
6263 }
6264 return NULL;
6265 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006266
6267 switch ((enum opcode)s[0]) {
6268 OP(NONE, load_none)
6269 OP(BININT, load_binint)
6270 OP(BININT1, load_binint1)
6271 OP(BININT2, load_binint2)
6272 OP(INT, load_int)
6273 OP(LONG, load_long)
6274 OP_ARG(LONG1, load_counted_long, 1)
6275 OP_ARG(LONG4, load_counted_long, 4)
6276 OP(FLOAT, load_float)
6277 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006278 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6279 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6280 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6281 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6282 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006283 OP(STRING, load_string)
6284 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006285 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6286 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6287 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006288 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6289 OP_ARG(TUPLE1, load_counted_tuple, 1)
6290 OP_ARG(TUPLE2, load_counted_tuple, 2)
6291 OP_ARG(TUPLE3, load_counted_tuple, 3)
6292 OP(TUPLE, load_tuple)
6293 OP(EMPTY_LIST, load_empty_list)
6294 OP(LIST, load_list)
6295 OP(EMPTY_DICT, load_empty_dict)
6296 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006297 OP(EMPTY_SET, load_empty_set)
6298 OP(ADDITEMS, load_additems)
6299 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006300 OP(OBJ, load_obj)
6301 OP(INST, load_inst)
6302 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006303 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006304 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006305 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006306 OP(APPEND, load_append)
6307 OP(APPENDS, load_appends)
6308 OP(BUILD, load_build)
6309 OP(DUP, load_dup)
6310 OP(BINGET, load_binget)
6311 OP(LONG_BINGET, load_long_binget)
6312 OP(GET, load_get)
6313 OP(MARK, load_mark)
6314 OP(BINPUT, load_binput)
6315 OP(LONG_BINPUT, load_long_binput)
6316 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006317 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006318 OP(POP, load_pop)
6319 OP(POP_MARK, load_pop_mark)
6320 OP(SETITEM, load_setitem)
6321 OP(SETITEMS, load_setitems)
6322 OP(PERSID, load_persid)
6323 OP(BINPERSID, load_binpersid)
6324 OP(REDUCE, load_reduce)
6325 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006326 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006327 OP_ARG(EXT1, load_extension, 1)
6328 OP_ARG(EXT2, load_extension, 2)
6329 OP_ARG(EXT4, load_extension, 4)
6330 OP_ARG(NEWTRUE, load_bool, Py_True)
6331 OP_ARG(NEWFALSE, load_bool, Py_False)
6332
6333 case STOP:
6334 break;
6335
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006336 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006337 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006338 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006339 unsigned char c = (unsigned char) *s;
6340 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6341 PyErr_Format(st->UnpicklingError,
6342 "invalid load key, '%c'.", c);
6343 }
6344 else {
6345 PyErr_Format(st->UnpicklingError,
6346 "invalid load key, '\\x%02x'.", c);
6347 }
6348 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006349 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006350 }
6351
6352 break; /* and we are done! */
6353 }
6354
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006355 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006356 return NULL;
6357 }
6358
Victor Stinner2ae57e32013-10-31 13:39:23 +01006359 if (_Unpickler_SkipConsumed(self) < 0)
6360 return NULL;
6361
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006362 PDATA_POP(self->stack, value);
6363 return value;
6364}
6365
Larry Hastings61272b72014-01-07 12:41:53 -08006366/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006367
6368_pickle.Unpickler.load
6369
6370Load a pickle.
6371
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006372Read a pickled object representation from the open file object given
6373in the constructor, and return the reconstituted object hierarchy
6374specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006375[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006376
Larry Hastings3cceb382014-01-04 11:09:09 -08006377static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006378_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006379/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006380{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006381 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006382
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006383 /* Check whether the Unpickler was initialized correctly. This prevents
6384 segfaulting if a subclass overridden __init__ with a function that does
6385 not call Unpickler.__init__(). Here, we simply ensure that self->read
6386 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006387 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006388 PickleState *st = _Pickle_GetGlobalState();
6389 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006390 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006391 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006392 return NULL;
6393 }
6394
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006395 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396}
6397
6398/* The name of find_class() is misleading. In newer pickle protocols, this
6399 function is used for loading any global (i.e., functions), not just
6400 classes. The name is kept only for backward compatibility. */
6401
Larry Hastings61272b72014-01-07 12:41:53 -08006402/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006403
6404_pickle.Unpickler.find_class
6405
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006406 module_name: object
6407 global_name: object
6408 /
6409
6410Return an object from a specified module.
6411
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006412If necessary, the module will be imported. Subclasses may override
6413this method (e.g. to restrict unpickling of arbitrary classes and
6414functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006415
6416This method is called whenever a class or a function object is
6417needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006418[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006419
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006420static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006421_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6422 PyObject *module_name,
6423 PyObject *global_name)
6424/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006425{
6426 PyObject *global;
Eric Snow93c92f72017-09-13 23:46:04 -07006427 PyObject *modules_dict;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006428 PyObject *module;
Eric Snow93c92f72017-09-13 23:46:04 -07006429 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006430
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006431 /* Try to map the old names used in Python 2.x to the new ones used in
6432 Python 3.x. We do this only with old pickle protocols and when the
6433 user has not disabled the feature. */
6434 if (self->proto < 3 && self->fix_imports) {
6435 PyObject *key;
6436 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006437 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006438
6439 /* Check if the global (i.e., a function or a class) was renamed
6440 or moved to another module. */
6441 key = PyTuple_Pack(2, module_name, global_name);
6442 if (key == NULL)
6443 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006444 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006445 Py_DECREF(key);
6446 if (item) {
6447 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6448 PyErr_Format(PyExc_RuntimeError,
6449 "_compat_pickle.NAME_MAPPING values should be "
6450 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6451 return NULL;
6452 }
6453 module_name = PyTuple_GET_ITEM(item, 0);
6454 global_name = PyTuple_GET_ITEM(item, 1);
6455 if (!PyUnicode_Check(module_name) ||
6456 !PyUnicode_Check(global_name)) {
6457 PyErr_Format(PyExc_RuntimeError,
6458 "_compat_pickle.NAME_MAPPING values should be "
6459 "pairs of str, not (%.200s, %.200s)",
6460 Py_TYPE(module_name)->tp_name,
6461 Py_TYPE(global_name)->tp_name);
6462 return NULL;
6463 }
6464 }
6465 else if (PyErr_Occurred()) {
6466 return NULL;
6467 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006468 else {
6469 /* Check if the module was renamed. */
6470 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6471 if (item) {
6472 if (!PyUnicode_Check(item)) {
6473 PyErr_Format(PyExc_RuntimeError,
6474 "_compat_pickle.IMPORT_MAPPING values should be "
6475 "strings, not %.200s", Py_TYPE(item)->tp_name);
6476 return NULL;
6477 }
6478 module_name = item;
6479 }
6480 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006481 return NULL;
6482 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006483 }
6484 }
6485
Eric Snow93c92f72017-09-13 23:46:04 -07006486 modules_dict = _PySys_GetObjectId(&PyId_modules);
6487 if (modules_dict == NULL) {
6488 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
6489 return NULL;
6490 }
6491
6492 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006493 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006494 if (PyErr_Occurred())
6495 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006496 module = PyImport_Import(module_name);
6497 if (module == NULL)
6498 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006499 global = getattribute(module, global_name, self->proto >= 4);
Eric Snow93c92f72017-09-13 23:46:04 -07006500 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006501 }
Victor Stinner121aab42011-09-29 23:40:53 +02006502 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006503 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006504 }
6505 return global;
6506}
6507
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006508/*[clinic input]
6509
6510_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6511
6512Returns size in memory, in bytes.
6513[clinic start generated code]*/
6514
6515static Py_ssize_t
6516_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6517/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6518{
6519 Py_ssize_t res;
6520
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006521 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006522 if (self->memo != NULL)
6523 res += self->memo_size * sizeof(PyObject *);
6524 if (self->marks != NULL)
6525 res += self->marks_size * sizeof(Py_ssize_t);
6526 if (self->input_line != NULL)
6527 res += strlen(self->input_line) + 1;
6528 if (self->encoding != NULL)
6529 res += strlen(self->encoding) + 1;
6530 if (self->errors != NULL)
6531 res += strlen(self->errors) + 1;
6532 return res;
6533}
6534
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006535static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006536 _PICKLE_UNPICKLER_LOAD_METHODDEF
6537 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006538 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539 {NULL, NULL} /* sentinel */
6540};
6541
6542static void
6543Unpickler_dealloc(UnpicklerObject *self)
6544{
6545 PyObject_GC_UnTrack((PyObject *)self);
6546 Py_XDECREF(self->readline);
6547 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006548 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006549 Py_XDECREF(self->stack);
6550 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006551 if (self->buffer.buf != NULL) {
6552 PyBuffer_Release(&self->buffer);
6553 self->buffer.buf = NULL;
6554 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006555
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006556 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006557 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006558 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006559 PyMem_Free(self->encoding);
6560 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006561
6562 Py_TYPE(self)->tp_free((PyObject *)self);
6563}
6564
6565static int
6566Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6567{
6568 Py_VISIT(self->readline);
6569 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006570 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006571 Py_VISIT(self->stack);
6572 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006573 return 0;
6574}
6575
6576static int
6577Unpickler_clear(UnpicklerObject *self)
6578{
6579 Py_CLEAR(self->readline);
6580 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006581 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006582 Py_CLEAR(self->stack);
6583 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006584 if (self->buffer.buf != NULL) {
6585 PyBuffer_Release(&self->buffer);
6586 self->buffer.buf = NULL;
6587 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006588
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006589 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006590 PyMem_Free(self->marks);
6591 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006592 PyMem_Free(self->input_line);
6593 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006594 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006595 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006596 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006597 self->errors = NULL;
6598
6599 return 0;
6600}
6601
Larry Hastings61272b72014-01-07 12:41:53 -08006602/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006603
6604_pickle.Unpickler.__init__
6605
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006606 file: object
6607 *
6608 fix_imports: bool = True
6609 encoding: str = 'ASCII'
6610 errors: str = 'strict'
6611
6612This takes a binary file for reading a pickle data stream.
6613
6614The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006615protocol argument is needed. Bytes past the pickled object's
6616representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006617
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006618The argument *file* must have two methods, a read() method that takes
6619an integer argument, and a readline() method that requires no
6620arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006621binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006622other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006623
6624Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006625which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006626generated by Python 2. If *fix_imports* is True, pickle will try to
6627map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006628*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006629instances pickled by Python 2; these default to 'ASCII' and 'strict',
6630respectively. The *encoding* can be 'bytes' to read these 8-bit
6631string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006632[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006633
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006634static int
Larry Hastings89964c42015-04-14 18:07:59 -04006635_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6636 int fix_imports, const char *encoding,
6637 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006638/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006639{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006640 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006641
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006642 /* In case of multiple __init__() calls, clear previous content. */
6643 if (self->read != NULL)
6644 (void)Unpickler_clear(self);
6645
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006646 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006647 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006648
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006649 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006650 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006651
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006652 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006653 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006654 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006655
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006656 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006657 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6658 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006659 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006660 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006661 }
6662 else {
6663 self->pers_func = NULL;
6664 }
6665
6666 self->stack = (Pdata *)Pdata_New();
6667 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006668 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006669
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006670 self->memo_size = 32;
6671 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006672 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006673 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006674
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006675 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006676
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006677 return 0;
6678}
6679
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006680
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006681/* Define a proxy object for the Unpickler's internal memo object. This is to
6682 * avoid breaking code like:
6683 * unpickler.memo.clear()
6684 * and
6685 * unpickler.memo = saved_memo
6686 * Is this a good idea? Not really, but we don't want to break code that uses
6687 * it. Note that we don't implement the entire mapping API here. This is
6688 * intentional, as these should be treated as black-box implementation details.
6689 *
6690 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006691 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006692 */
6693
Larry Hastings61272b72014-01-07 12:41:53 -08006694/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006695_pickle.UnpicklerMemoProxy.clear
6696
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006697Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006698[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006699
Larry Hastings3cceb382014-01-04 11:09:09 -08006700static PyObject *
6701_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006702/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006703{
6704 _Unpickler_MemoCleanup(self->unpickler);
6705 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6706 if (self->unpickler->memo == NULL)
6707 return NULL;
6708 Py_RETURN_NONE;
6709}
6710
Larry Hastings61272b72014-01-07 12:41:53 -08006711/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006712_pickle.UnpicklerMemoProxy.copy
6713
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006714Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006715[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006716
Larry Hastings3cceb382014-01-04 11:09:09 -08006717static PyObject *
6718_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006719/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006720{
6721 Py_ssize_t i;
6722 PyObject *new_memo = PyDict_New();
6723 if (new_memo == NULL)
6724 return NULL;
6725
6726 for (i = 0; i < self->unpickler->memo_size; i++) {
6727 int status;
6728 PyObject *key, *value;
6729
6730 value = self->unpickler->memo[i];
6731 if (value == NULL)
6732 continue;
6733
6734 key = PyLong_FromSsize_t(i);
6735 if (key == NULL)
6736 goto error;
6737 status = PyDict_SetItem(new_memo, key, value);
6738 Py_DECREF(key);
6739 if (status < 0)
6740 goto error;
6741 }
6742 return new_memo;
6743
6744error:
6745 Py_DECREF(new_memo);
6746 return NULL;
6747}
6748
Larry Hastings61272b72014-01-07 12:41:53 -08006749/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006750_pickle.UnpicklerMemoProxy.__reduce__
6751
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006752Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006753[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006754
Larry Hastings3cceb382014-01-04 11:09:09 -08006755static PyObject *
6756_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006757/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006758{
6759 PyObject *reduce_value;
6760 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006761 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006762 if (contents == NULL)
6763 return NULL;
6764
6765 reduce_value = PyTuple_New(2);
6766 if (reduce_value == NULL) {
6767 Py_DECREF(contents);
6768 return NULL;
6769 }
6770 constructor_args = PyTuple_New(1);
6771 if (constructor_args == NULL) {
6772 Py_DECREF(contents);
6773 Py_DECREF(reduce_value);
6774 return NULL;
6775 }
6776 PyTuple_SET_ITEM(constructor_args, 0, contents);
6777 Py_INCREF((PyObject *)&PyDict_Type);
6778 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6779 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6780 return reduce_value;
6781}
6782
6783static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006784 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6785 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6786 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006787 {NULL, NULL} /* sentinel */
6788};
6789
6790static void
6791UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6792{
6793 PyObject_GC_UnTrack(self);
6794 Py_XDECREF(self->unpickler);
6795 PyObject_GC_Del((PyObject *)self);
6796}
6797
6798static int
6799UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6800 visitproc visit, void *arg)
6801{
6802 Py_VISIT(self->unpickler);
6803 return 0;
6804}
6805
6806static int
6807UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6808{
6809 Py_CLEAR(self->unpickler);
6810 return 0;
6811}
6812
6813static PyTypeObject UnpicklerMemoProxyType = {
6814 PyVarObject_HEAD_INIT(NULL, 0)
6815 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6816 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6817 0,
6818 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6819 0, /* tp_print */
6820 0, /* tp_getattr */
6821 0, /* tp_setattr */
6822 0, /* tp_compare */
6823 0, /* tp_repr */
6824 0, /* tp_as_number */
6825 0, /* tp_as_sequence */
6826 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006827 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006828 0, /* tp_call */
6829 0, /* tp_str */
6830 PyObject_GenericGetAttr, /* tp_getattro */
6831 PyObject_GenericSetAttr, /* tp_setattro */
6832 0, /* tp_as_buffer */
6833 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6834 0, /* tp_doc */
6835 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6836 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6837 0, /* tp_richcompare */
6838 0, /* tp_weaklistoffset */
6839 0, /* tp_iter */
6840 0, /* tp_iternext */
6841 unpicklerproxy_methods, /* tp_methods */
6842};
6843
6844static PyObject *
6845UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6846{
6847 UnpicklerMemoProxyObject *self;
6848
6849 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6850 &UnpicklerMemoProxyType);
6851 if (self == NULL)
6852 return NULL;
6853 Py_INCREF(unpickler);
6854 self->unpickler = unpickler;
6855 PyObject_GC_Track(self);
6856 return (PyObject *)self;
6857}
6858
6859/*****************************************************************************/
6860
6861
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006862static PyObject *
6863Unpickler_get_memo(UnpicklerObject *self)
6864{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006865 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006866}
6867
6868static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006869Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006870{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006871 PyObject **new_memo;
6872 Py_ssize_t new_memo_size = 0;
6873 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006874
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006875 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006876 PyErr_SetString(PyExc_TypeError,
6877 "attribute deletion is not supported");
6878 return -1;
6879 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006880
6881 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6882 UnpicklerObject *unpickler =
6883 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6884
6885 new_memo_size = unpickler->memo_size;
6886 new_memo = _Unpickler_NewMemo(new_memo_size);
6887 if (new_memo == NULL)
6888 return -1;
6889
6890 for (i = 0; i < new_memo_size; i++) {
6891 Py_XINCREF(unpickler->memo[i]);
6892 new_memo[i] = unpickler->memo[i];
6893 }
6894 }
6895 else if (PyDict_Check(obj)) {
6896 Py_ssize_t i = 0;
6897 PyObject *key, *value;
6898
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006899 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006900 new_memo = _Unpickler_NewMemo(new_memo_size);
6901 if (new_memo == NULL)
6902 return -1;
6903
6904 while (PyDict_Next(obj, &i, &key, &value)) {
6905 Py_ssize_t idx;
6906 if (!PyLong_Check(key)) {
6907 PyErr_SetString(PyExc_TypeError,
6908 "memo key must be integers");
6909 goto error;
6910 }
6911 idx = PyLong_AsSsize_t(key);
6912 if (idx == -1 && PyErr_Occurred())
6913 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006914 if (idx < 0) {
6915 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006916 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006917 goto error;
6918 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006919 if (_Unpickler_MemoPut(self, idx, value) < 0)
6920 goto error;
6921 }
6922 }
6923 else {
6924 PyErr_Format(PyExc_TypeError,
6925 "'memo' attribute must be an UnpicklerMemoProxy object"
6926 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006927 return -1;
6928 }
6929
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006930 _Unpickler_MemoCleanup(self);
6931 self->memo_size = new_memo_size;
6932 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006933
6934 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006935
6936 error:
6937 if (new_memo_size) {
6938 i = new_memo_size;
6939 while (--i >= 0) {
6940 Py_XDECREF(new_memo[i]);
6941 }
6942 PyMem_FREE(new_memo);
6943 }
6944 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006945}
6946
6947static PyObject *
6948Unpickler_get_persload(UnpicklerObject *self)
6949{
6950 if (self->pers_func == NULL)
6951 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6952 else
6953 Py_INCREF(self->pers_func);
6954 return self->pers_func;
6955}
6956
6957static int
6958Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6959{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006960 if (value == NULL) {
6961 PyErr_SetString(PyExc_TypeError,
6962 "attribute deletion is not supported");
6963 return -1;
6964 }
6965 if (!PyCallable_Check(value)) {
6966 PyErr_SetString(PyExc_TypeError,
6967 "persistent_load must be a callable taking "
6968 "one argument");
6969 return -1;
6970 }
6971
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006972 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03006973 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006974
6975 return 0;
6976}
6977
6978static PyGetSetDef Unpickler_getsets[] = {
6979 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6980 {"persistent_load", (getter)Unpickler_get_persload,
6981 (setter)Unpickler_set_persload},
6982 {NULL}
6983};
6984
6985static PyTypeObject Unpickler_Type = {
6986 PyVarObject_HEAD_INIT(NULL, 0)
6987 "_pickle.Unpickler", /*tp_name*/
6988 sizeof(UnpicklerObject), /*tp_basicsize*/
6989 0, /*tp_itemsize*/
6990 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6991 0, /*tp_print*/
6992 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006993 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006994 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006995 0, /*tp_repr*/
6996 0, /*tp_as_number*/
6997 0, /*tp_as_sequence*/
6998 0, /*tp_as_mapping*/
6999 0, /*tp_hash*/
7000 0, /*tp_call*/
7001 0, /*tp_str*/
7002 0, /*tp_getattro*/
7003 0, /*tp_setattro*/
7004 0, /*tp_as_buffer*/
7005 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007006 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007007 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7008 (inquiry)Unpickler_clear, /*tp_clear*/
7009 0, /*tp_richcompare*/
7010 0, /*tp_weaklistoffset*/
7011 0, /*tp_iter*/
7012 0, /*tp_iternext*/
7013 Unpickler_methods, /*tp_methods*/
7014 0, /*tp_members*/
7015 Unpickler_getsets, /*tp_getset*/
7016 0, /*tp_base*/
7017 0, /*tp_dict*/
7018 0, /*tp_descr_get*/
7019 0, /*tp_descr_set*/
7020 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007021 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007022 PyType_GenericAlloc, /*tp_alloc*/
7023 PyType_GenericNew, /*tp_new*/
7024 PyObject_GC_Del, /*tp_free*/
7025 0, /*tp_is_gc*/
7026};
7027
Larry Hastings61272b72014-01-07 12:41:53 -08007028/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007029
7030_pickle.dump
7031
7032 obj: object
7033 file: object
7034 protocol: object = NULL
7035 *
7036 fix_imports: bool = True
7037
7038Write a pickled representation of obj to the open file object file.
7039
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007040This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7041be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007042
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007043The optional *protocol* argument tells the pickler to use the given
7044protocol supported protocols are 0, 1, 2, 3 and 4. The default
7045protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007046
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007047Specifying a negative protocol version selects the highest protocol
7048version supported. The higher the protocol used, the more recent the
7049version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007050
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007051The *file* argument must have a write() method that accepts a single
7052bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007053writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007054this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007055
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007056If *fix_imports* is True and protocol is less than 3, pickle will try
7057to map the new Python 3 names to the old module names used in Python
70582, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007059[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007060
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007061static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007062_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007063 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007064/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007065{
7066 PicklerObject *pickler = _Pickler_New();
7067
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007068 if (pickler == NULL)
7069 return NULL;
7070
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007071 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007072 goto error;
7073
7074 if (_Pickler_SetOutputStream(pickler, file) < 0)
7075 goto error;
7076
7077 if (dump(pickler, obj) < 0)
7078 goto error;
7079
7080 if (_Pickler_FlushToFile(pickler) < 0)
7081 goto error;
7082
7083 Py_DECREF(pickler);
7084 Py_RETURN_NONE;
7085
7086 error:
7087 Py_XDECREF(pickler);
7088 return NULL;
7089}
7090
Larry Hastings61272b72014-01-07 12:41:53 -08007091/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007092
7093_pickle.dumps
7094
7095 obj: object
7096 protocol: object = NULL
7097 *
7098 fix_imports: bool = True
7099
7100Return the pickled representation of the object as a bytes object.
7101
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007102The optional *protocol* argument tells the pickler to use the given
7103protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7104protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007105
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007106Specifying a negative protocol version selects the highest protocol
7107version supported. The higher the protocol used, the more recent the
7108version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007109
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007110If *fix_imports* is True and *protocol* is less than 3, pickle will
7111try to map the new Python 3 names to the old module names used in
7112Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007113[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007114
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007115static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007116_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007117 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007118/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007119{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007120 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007121 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007122
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007123 if (pickler == NULL)
7124 return NULL;
7125
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007126 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007127 goto error;
7128
7129 if (dump(pickler, obj) < 0)
7130 goto error;
7131
7132 result = _Pickler_GetString(pickler);
7133 Py_DECREF(pickler);
7134 return result;
7135
7136 error:
7137 Py_XDECREF(pickler);
7138 return NULL;
7139}
7140
Larry Hastings61272b72014-01-07 12:41:53 -08007141/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007142
7143_pickle.load
7144
7145 file: object
7146 *
7147 fix_imports: bool = True
7148 encoding: str = 'ASCII'
7149 errors: str = 'strict'
7150
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007151Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007152
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007153This is equivalent to ``Unpickler(file).load()``, but may be more
7154efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007155
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007156The protocol version of the pickle is detected automatically, so no
7157protocol argument is needed. Bytes past the pickled object's
7158representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007159
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007160The argument *file* must have two methods, a read() method that takes
7161an integer argument, and a readline() method that requires no
7162arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007163binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007164other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007165
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007166Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007167which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007168generated by Python 2. If *fix_imports* is True, pickle will try to
7169map the old Python 2 names to the new names used in Python 3. The
7170*encoding* and *errors* tell pickle how to decode 8-bit string
7171instances pickled by Python 2; these default to 'ASCII' and 'strict',
7172respectively. The *encoding* can be 'bytes' to read these 8-bit
7173string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007174[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007175
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007176static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007177_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007178 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007179/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007180{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007181 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007182 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007183
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007184 if (unpickler == NULL)
7185 return NULL;
7186
7187 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7188 goto error;
7189
7190 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7191 goto error;
7192
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007193 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007194
7195 result = load(unpickler);
7196 Py_DECREF(unpickler);
7197 return result;
7198
7199 error:
7200 Py_XDECREF(unpickler);
7201 return NULL;
7202}
7203
Larry Hastings61272b72014-01-07 12:41:53 -08007204/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007205
7206_pickle.loads
7207
7208 data: object
7209 *
7210 fix_imports: bool = True
7211 encoding: str = 'ASCII'
7212 errors: str = 'strict'
7213
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007214Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007215
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007216The protocol version of the pickle is detected automatically, so no
7217protocol argument is needed. Bytes past the pickled object's
7218representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007219
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007220Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007221which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007222generated by Python 2. If *fix_imports* is True, pickle will try to
7223map the old Python 2 names to the new names used in Python 3. The
7224*encoding* and *errors* tell pickle how to decode 8-bit string
7225instances pickled by Python 2; these default to 'ASCII' and 'strict',
7226respectively. The *encoding* can be 'bytes' to read these 8-bit
7227string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007228[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007229
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007230static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007231_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007232 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007233/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007234{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007235 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007236 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007237
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007238 if (unpickler == NULL)
7239 return NULL;
7240
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007241 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007242 goto error;
7243
7244 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7245 goto error;
7246
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007247 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007248
7249 result = load(unpickler);
7250 Py_DECREF(unpickler);
7251 return result;
7252
7253 error:
7254 Py_XDECREF(unpickler);
7255 return NULL;
7256}
7257
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007258static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007259 _PICKLE_DUMP_METHODDEF
7260 _PICKLE_DUMPS_METHODDEF
7261 _PICKLE_LOAD_METHODDEF
7262 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007263 {NULL, NULL} /* sentinel */
7264};
7265
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007266static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007267pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007268{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007269 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007270 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007271}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007272
Stefan Krahf483b0f2013-12-14 13:43:10 +01007273static void
7274pickle_free(PyObject *m)
7275{
7276 _Pickle_ClearState(_Pickle_GetState(m));
7277}
7278
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007279static int
7280pickle_traverse(PyObject *m, visitproc visit, void *arg)
7281{
7282 PickleState *st = _Pickle_GetState(m);
7283 Py_VISIT(st->PickleError);
7284 Py_VISIT(st->PicklingError);
7285 Py_VISIT(st->UnpicklingError);
7286 Py_VISIT(st->dispatch_table);
7287 Py_VISIT(st->extension_registry);
7288 Py_VISIT(st->extension_cache);
7289 Py_VISIT(st->inverted_registry);
7290 Py_VISIT(st->name_mapping_2to3);
7291 Py_VISIT(st->import_mapping_2to3);
7292 Py_VISIT(st->name_mapping_3to2);
7293 Py_VISIT(st->import_mapping_3to2);
7294 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007295 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007296 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007297}
7298
7299static struct PyModuleDef _picklemodule = {
7300 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007301 "_pickle", /* m_name */
7302 pickle_module_doc, /* m_doc */
7303 sizeof(PickleState), /* m_size */
7304 pickle_methods, /* m_methods */
7305 NULL, /* m_reload */
7306 pickle_traverse, /* m_traverse */
7307 pickle_clear, /* m_clear */
7308 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007309};
7310
7311PyMODINIT_FUNC
7312PyInit__pickle(void)
7313{
7314 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007315 PickleState *st;
7316
7317 m = PyState_FindModule(&_picklemodule);
7318 if (m) {
7319 Py_INCREF(m);
7320 return m;
7321 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007322
7323 if (PyType_Ready(&Unpickler_Type) < 0)
7324 return NULL;
7325 if (PyType_Ready(&Pickler_Type) < 0)
7326 return NULL;
7327 if (PyType_Ready(&Pdata_Type) < 0)
7328 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007329 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7330 return NULL;
7331 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7332 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007333
7334 /* Create the module and add the functions. */
7335 m = PyModule_Create(&_picklemodule);
7336 if (m == NULL)
7337 return NULL;
7338
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007339 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007340 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7341 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007342 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007343 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7344 return NULL;
7345
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007346 st = _Pickle_GetState(m);
7347
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007348 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007349 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7350 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007351 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007352 st->PicklingError = \
7353 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7354 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007355 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007356 st->UnpicklingError = \
7357 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7358 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007359 return NULL;
7360
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007361 Py_INCREF(st->PickleError);
7362 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007363 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007364 Py_INCREF(st->PicklingError);
7365 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007366 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007367 Py_INCREF(st->UnpicklingError);
7368 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007369 return NULL;
7370
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007371 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007372 return NULL;
7373
7374 return m;
7375}