blob: b71fb9350e62b300ade62a3d9452d47b3b4e3200 [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001
2/* Core extension modules are built-in on some platforms (e.g. Windows). */
3#ifdef Py_BUILD_CORE
Eric Snowfc1bf872017-09-11 18:30:43 -07004#define Py_BUILD_CORE_BUILTIN
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#undef Py_BUILD_CORE
6#endif
7
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008#include "Python.h"
9#include "structmember.h"
10
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -080011PyDoc_STRVAR(pickle_module_doc,
12"Optimized C implementation for the Python pickle module.");
13
Larry Hastings61272b72014-01-07 12:41:53 -080014/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080016class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
17class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
18class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
19class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080020[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080022
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000023/* Bump this when new opcodes are added to the pickle protocol. */
24enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010025 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000026 DEFAULT_PROTOCOL = 3
27};
28
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029/* Pickle opcodes. These must be kept updated with pickle.py.
30 Extensive docs are in pickletools.py. */
31enum opcode {
32 MARK = '(',
33 STOP = '.',
34 POP = '0',
35 POP_MARK = '1',
36 DUP = '2',
37 FLOAT = 'F',
38 INT = 'I',
39 BININT = 'J',
40 BININT1 = 'K',
41 LONG = 'L',
42 BININT2 = 'M',
43 NONE = 'N',
44 PERSID = 'P',
45 BINPERSID = 'Q',
46 REDUCE = 'R',
47 STRING = 'S',
48 BINSTRING = 'T',
49 SHORT_BINSTRING = 'U',
50 UNICODE = 'V',
51 BINUNICODE = 'X',
52 APPEND = 'a',
53 BUILD = 'b',
54 GLOBAL = 'c',
55 DICT = 'd',
56 EMPTY_DICT = '}',
57 APPENDS = 'e',
58 GET = 'g',
59 BINGET = 'h',
60 INST = 'i',
61 LONG_BINGET = 'j',
62 LIST = 'l',
63 EMPTY_LIST = ']',
64 OBJ = 'o',
65 PUT = 'p',
66 BINPUT = 'q',
67 LONG_BINPUT = 'r',
68 SETITEM = 's',
69 TUPLE = 't',
70 EMPTY_TUPLE = ')',
71 SETITEMS = 'u',
72 BINFLOAT = 'G',
73
74 /* Protocol 2. */
75 PROTO = '\x80',
76 NEWOBJ = '\x81',
77 EXT1 = '\x82',
78 EXT2 = '\x83',
79 EXT4 = '\x84',
80 TUPLE1 = '\x85',
81 TUPLE2 = '\x86',
82 TUPLE3 = '\x87',
83 NEWTRUE = '\x88',
84 NEWFALSE = '\x89',
85 LONG1 = '\x8a',
86 LONG4 = '\x8b',
87
88 /* Protocol 3 (Python 3.x) */
89 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010090 SHORT_BINBYTES = 'C',
91
92 /* Protocol 4 */
93 SHORT_BINUNICODE = '\x8c',
94 BINUNICODE8 = '\x8d',
95 BINBYTES8 = '\x8e',
96 EMPTY_SET = '\x8f',
97 ADDITEMS = '\x90',
98 FROZENSET = '\x91',
99 NEWOBJ_EX = '\x92',
100 STACK_GLOBAL = '\x93',
101 MEMOIZE = '\x94',
102 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000103};
104
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000105enum {
106 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
107 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
108 break if this gets out of synch with pickle.py, but it's unclear that would
109 help anything either. */
110 BATCHSIZE = 1000,
111
112 /* Nesting limit until Pickler, when running in "fast mode", starts
113 checking for self-referential data-structures. */
114 FAST_NESTING_LIMIT = 50,
115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000116 /* Initial size of the write buffer of Pickler. */
117 WRITE_BUF_SIZE = 4096,
118
Antoine Pitrou04248a82010-10-12 20:51:21 +0000119 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100120 PREFETCH = 8192 * 16,
121
122 FRAME_SIZE_TARGET = 64 * 1024,
123
124 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000125};
126
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800127/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129/* State of the pickle module, per PEP 3121. */
130typedef struct {
131 /* Exception classes for pickle. */
132 PyObject *PickleError;
133 PyObject *PicklingError;
134 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800136 /* copyreg.dispatch_table, {type_object: pickling_function} */
137 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800139 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* copyreg._extension_registry, {(module_name, function_name): code} */
142 PyObject *extension_registry;
143 /* copyreg._extension_cache, {code: object} */
144 PyObject *extension_cache;
145 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
146 PyObject *inverted_registry;
147
148 /* Import mappings for compatibility with Python 2.x */
149
150 /* _compat_pickle.NAME_MAPPING,
151 {(oldmodule, oldname): (newmodule, newname)} */
152 PyObject *name_mapping_2to3;
153 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
154 PyObject *import_mapping_2to3;
155 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
156 PyObject *name_mapping_3to2;
157 PyObject *import_mapping_3to2;
158
159 /* codecs.encode, used for saving bytes in older protocols */
160 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300161 /* builtins.getattr, used for saving nested names with protocol < 4 */
162 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300163 /* functools.partial, used for implementing __newobj_ex__ with protocols
164 2 and 3 */
165 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800166} PickleState;
167
168/* Forward declaration of the _pickle module definition. */
169static struct PyModuleDef _picklemodule;
170
171/* Given a module object, get its per-module state. */
172static PickleState *
173_Pickle_GetState(PyObject *module)
174{
175 return (PickleState *)PyModule_GetState(module);
176}
177
178/* Find the module instance imported in the currently running sub-interpreter
179 and get its state. */
180static PickleState *
181_Pickle_GetGlobalState(void)
182{
183 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
184}
185
186/* Clear the given pickle module state. */
187static void
188_Pickle_ClearState(PickleState *st)
189{
190 Py_CLEAR(st->PickleError);
191 Py_CLEAR(st->PicklingError);
192 Py_CLEAR(st->UnpicklingError);
193 Py_CLEAR(st->dispatch_table);
194 Py_CLEAR(st->extension_registry);
195 Py_CLEAR(st->extension_cache);
196 Py_CLEAR(st->inverted_registry);
197 Py_CLEAR(st->name_mapping_2to3);
198 Py_CLEAR(st->import_mapping_2to3);
199 Py_CLEAR(st->name_mapping_3to2);
200 Py_CLEAR(st->import_mapping_3to2);
201 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300202 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100203 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204}
205
206/* Initialize the given pickle module state. */
207static int
208_Pickle_InitState(PickleState *st)
209{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300210 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800211 PyObject *copyreg = NULL;
212 PyObject *compat_pickle = NULL;
213 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300214 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800215
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300216 builtins = PyEval_GetBuiltins();
217 if (builtins == NULL)
218 goto error;
219 st->getattr = PyDict_GetItemString(builtins, "getattr");
220 if (st->getattr == NULL)
221 goto error;
222 Py_INCREF(st->getattr);
223
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800224 copyreg = PyImport_ImportModule("copyreg");
225 if (!copyreg)
226 goto error;
227 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
228 if (!st->dispatch_table)
229 goto error;
230 if (!PyDict_CheckExact(st->dispatch_table)) {
231 PyErr_Format(PyExc_RuntimeError,
232 "copyreg.dispatch_table should be a dict, not %.200s",
233 Py_TYPE(st->dispatch_table)->tp_name);
234 goto error;
235 }
236 st->extension_registry = \
237 PyObject_GetAttrString(copyreg, "_extension_registry");
238 if (!st->extension_registry)
239 goto error;
240 if (!PyDict_CheckExact(st->extension_registry)) {
241 PyErr_Format(PyExc_RuntimeError,
242 "copyreg._extension_registry should be a dict, "
243 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
244 goto error;
245 }
246 st->inverted_registry = \
247 PyObject_GetAttrString(copyreg, "_inverted_registry");
248 if (!st->inverted_registry)
249 goto error;
250 if (!PyDict_CheckExact(st->inverted_registry)) {
251 PyErr_Format(PyExc_RuntimeError,
252 "copyreg._inverted_registry should be a dict, "
253 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
254 goto error;
255 }
256 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
257 if (!st->extension_cache)
258 goto error;
259 if (!PyDict_CheckExact(st->extension_cache)) {
260 PyErr_Format(PyExc_RuntimeError,
261 "copyreg._extension_cache should be a dict, "
262 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
263 goto error;
264 }
265 Py_CLEAR(copyreg);
266
267 /* Load the 2.x -> 3.x stdlib module mapping tables */
268 compat_pickle = PyImport_ImportModule("_compat_pickle");
269 if (!compat_pickle)
270 goto error;
271 st->name_mapping_2to3 = \
272 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
273 if (!st->name_mapping_2to3)
274 goto error;
275 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
276 PyErr_Format(PyExc_RuntimeError,
277 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
278 Py_TYPE(st->name_mapping_2to3)->tp_name);
279 goto error;
280 }
281 st->import_mapping_2to3 = \
282 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
283 if (!st->import_mapping_2to3)
284 goto error;
285 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
286 PyErr_Format(PyExc_RuntimeError,
287 "_compat_pickle.IMPORT_MAPPING should be a dict, "
288 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
289 goto error;
290 }
291 /* ... and the 3.x -> 2.x mapping tables */
292 st->name_mapping_3to2 = \
293 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
294 if (!st->name_mapping_3to2)
295 goto error;
296 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
297 PyErr_Format(PyExc_RuntimeError,
298 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
299 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
300 goto error;
301 }
302 st->import_mapping_3to2 = \
303 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
304 if (!st->import_mapping_3to2)
305 goto error;
306 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
307 PyErr_Format(PyExc_RuntimeError,
308 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
309 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
310 goto error;
311 }
312 Py_CLEAR(compat_pickle);
313
314 codecs = PyImport_ImportModule("codecs");
315 if (codecs == NULL)
316 goto error;
317 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
318 if (st->codecs_encode == NULL) {
319 goto error;
320 }
321 if (!PyCallable_Check(st->codecs_encode)) {
322 PyErr_Format(PyExc_RuntimeError,
323 "codecs.encode should be a callable, not %.200s",
324 Py_TYPE(st->codecs_encode)->tp_name);
325 goto error;
326 }
327 Py_CLEAR(codecs);
328
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300329 functools = PyImport_ImportModule("functools");
330 if (!functools)
331 goto error;
332 st->partial = PyObject_GetAttrString(functools, "partial");
333 if (!st->partial)
334 goto error;
335 Py_CLEAR(functools);
336
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 return 0;
338
339 error:
340 Py_CLEAR(copyreg);
341 Py_CLEAR(compat_pickle);
342 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300343 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800344 _Pickle_ClearState(st);
345 return -1;
346}
347
348/* Helper for calling a function with a single argument quickly.
349
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 This function steals the reference of the given argument. */
351static PyObject *
352_Pickle_FastCall(PyObject *func, PyObject *obj)
353{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 PyObject *result;
355
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100356 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200357 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800358 return result;
359}
360
361/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000362
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000363/* Internal data type used as the unpickling stack. */
364typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000365 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000366 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200367 int mark_set; /* is MARK set? */
368 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000369 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000370} Pdata;
371
372static void
373Pdata_dealloc(Pdata *self)
374{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200375 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000376 while (--i >= 0) {
377 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000378 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000379 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000380 PyObject_Del(self);
381}
382
383static PyTypeObject Pdata_Type = {
384 PyVarObject_HEAD_INIT(NULL, 0)
385 "_pickle.Pdata", /*tp_name*/
386 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200387 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000388 (destructor)Pdata_dealloc, /*tp_dealloc*/
389};
390
391static PyObject *
392Pdata_New(void)
393{
394 Pdata *self;
395
396 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
397 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000398 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200399 self->mark_set = 0;
400 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000401 self->allocated = 8;
402 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000403 if (self->data)
404 return (PyObject *)self;
405 Py_DECREF(self);
406 return PyErr_NoMemory();
407}
408
409
410/* Retain only the initial clearto items. If clearto >= the current
411 * number of items, this is a (non-erroneous) NOP.
412 */
413static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200414Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000415{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200416 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000417
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200418 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000419 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000420 return 0;
421
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000422 while (--i >= clearto) {
423 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000424 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000425 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000426 return 0;
427}
428
429static int
430Pdata_grow(Pdata *self)
431{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200433 size_t allocated = (size_t)self->allocated;
434 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000435
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436 new_allocated = (allocated >> 3) + 6;
437 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200438 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000439 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000440 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500441 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000442 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000443 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000444
445 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200446 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000447 return 0;
448
449 nomemory:
450 PyErr_NoMemory();
451 return -1;
452}
453
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200454static int
455Pdata_stack_underflow(Pdata *self)
456{
457 PickleState *st = _Pickle_GetGlobalState();
458 PyErr_SetString(st->UnpicklingError,
459 self->mark_set ?
460 "unexpected MARK found" :
461 "unpickling stack underflow");
462 return -1;
463}
464
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000465/* D is a Pdata*. Pop the topmost element and store it into V, which
466 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
467 * is raised and V is set to NULL.
468 */
469static PyObject *
470Pdata_pop(Pdata *self)
471{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200472 if (Py_SIZE(self) <= self->fence) {
473 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000474 return NULL;
475 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000476 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477}
478#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
479
480static int
481Pdata_push(Pdata *self, PyObject *obj)
482{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000483 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000484 return -1;
485 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000486 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000487 return 0;
488}
489
490/* Push an object on stack, transferring its ownership to the stack. */
491#define PDATA_PUSH(D, O, ER) do { \
492 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
493
494/* Push an object on stack, adding a new reference to the object. */
495#define PDATA_APPEND(D, O, ER) do { \
496 Py_INCREF((O)); \
497 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
498
499static PyObject *
500Pdata_poptuple(Pdata *self, Py_ssize_t start)
501{
502 PyObject *tuple;
503 Py_ssize_t len, i, j;
504
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200505 if (start < self->fence) {
506 Pdata_stack_underflow(self);
507 return NULL;
508 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000509 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000510 tuple = PyTuple_New(len);
511 if (tuple == NULL)
512 return NULL;
513 for (i = start, j = 0; j < len; i++, j++)
514 PyTuple_SET_ITEM(tuple, j, self->data[i]);
515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000516 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000517 return tuple;
518}
519
520static PyObject *
521Pdata_poplist(Pdata *self, Py_ssize_t start)
522{
523 PyObject *list;
524 Py_ssize_t len, i, j;
525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000526 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000527 list = PyList_New(len);
528 if (list == NULL)
529 return NULL;
530 for (i = start, j = 0; j < len; i++, j++)
531 PyList_SET_ITEM(list, j, self->data[i]);
532
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000533 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000534 return list;
535}
536
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000537typedef struct {
538 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200539 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000540} PyMemoEntry;
541
542typedef struct {
543 Py_ssize_t mt_mask;
544 Py_ssize_t mt_used;
545 Py_ssize_t mt_allocated;
546 PyMemoEntry *mt_table;
547} PyMemoTable;
548
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000549typedef struct PicklerObject {
550 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000551 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000552 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000553 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000554 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100555 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000556
557 PyObject *write; /* write() method of the output stream. */
558 PyObject *output_buffer; /* Write into a local bytearray buffer before
559 flushing to the stream. */
560 Py_ssize_t output_len; /* Length of output_buffer. */
561 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 int proto; /* Pickle protocol number, >= 0 */
563 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100564 int framing; /* True when framing is enabled, proto >= 4 */
565 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000566 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100567 is no frame currently open. */
568
569 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000570 int fast; /* Enable fast mode if set to a true value.
571 The fast mode disable the usage of memo,
572 therefore speeding the pickling process by
573 not generating superfluous PUT opcodes. It
574 should not be used if with self-referential
575 objects. */
576 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000577 int fix_imports; /* Indicate whether Pickler should fix
578 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000579 PyObject *fast_memo;
580} PicklerObject;
581
582typedef struct UnpicklerObject {
583 PyObject_HEAD
584 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000585
586 /* The unpickler memo is just an array of PyObject *s. Using a dict
587 is unnecessary, since the keys are contiguous ints. */
588 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100589 Py_ssize_t memo_size; /* Capacity of the memo array */
590 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000591
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000592 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593
594 Py_buffer buffer;
595 char *input_buffer;
596 char *input_line;
597 Py_ssize_t input_len;
598 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000599 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100600
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000601 PyObject *read; /* read() method of the input stream. */
602 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000603 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000604
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000605 char *encoding; /* Name of the encoding to be used for
606 decoding strings pickled using Python
607 2.x. The default value is "ASCII" */
608 char *errors; /* Name of errors handling scheme to used when
609 decoding strings. The default value is
610 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500611 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000612 objects. */
613 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
614 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000615 int proto; /* Protocol of the pickle loaded. */
616 int fix_imports; /* Indicate whether Unpickler should fix
617 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000618} UnpicklerObject;
619
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200620typedef struct {
621 PyObject_HEAD
622 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
623} PicklerMemoProxyObject;
624
625typedef struct {
626 PyObject_HEAD
627 UnpicklerObject *unpickler;
628} UnpicklerMemoProxyObject;
629
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000630/* Forward declarations */
631static int save(PicklerObject *, PyObject *, int);
632static int save_reduce(PicklerObject *, PyObject *, PyObject *);
633static PyTypeObject Pickler_Type;
634static PyTypeObject Unpickler_Type;
635
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200636#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000637
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000638/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300639 A custom hashtable mapping void* to Python ints. This is used by the pickler
640 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000641 a bunch of unnecessary object creation. This makes a huge performance
642 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000643
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000644#define MT_MINSIZE 8
645#define PERTURB_SHIFT 5
646
647
648static PyMemoTable *
649PyMemoTable_New(void)
650{
651 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
652 if (memo == NULL) {
653 PyErr_NoMemory();
654 return NULL;
655 }
656
657 memo->mt_used = 0;
658 memo->mt_allocated = MT_MINSIZE;
659 memo->mt_mask = MT_MINSIZE - 1;
660 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
661 if (memo->mt_table == NULL) {
662 PyMem_FREE(memo);
663 PyErr_NoMemory();
664 return NULL;
665 }
666 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
667
668 return memo;
669}
670
671static PyMemoTable *
672PyMemoTable_Copy(PyMemoTable *self)
673{
674 Py_ssize_t i;
675 PyMemoTable *new = PyMemoTable_New();
676 if (new == NULL)
677 return NULL;
678
679 new->mt_used = self->mt_used;
680 new->mt_allocated = self->mt_allocated;
681 new->mt_mask = self->mt_mask;
682 /* The table we get from _New() is probably smaller than we wanted.
683 Free it and allocate one that's the right size. */
684 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500685 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000686 if (new->mt_table == NULL) {
687 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200688 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000689 return NULL;
690 }
691 for (i = 0; i < self->mt_allocated; i++) {
692 Py_XINCREF(self->mt_table[i].me_key);
693 }
694 memcpy(new->mt_table, self->mt_table,
695 sizeof(PyMemoEntry) * self->mt_allocated);
696
697 return new;
698}
699
700static Py_ssize_t
701PyMemoTable_Size(PyMemoTable *self)
702{
703 return self->mt_used;
704}
705
706static int
707PyMemoTable_Clear(PyMemoTable *self)
708{
709 Py_ssize_t i = self->mt_allocated;
710
711 while (--i >= 0) {
712 Py_XDECREF(self->mt_table[i].me_key);
713 }
714 self->mt_used = 0;
715 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
716 return 0;
717}
718
719static void
720PyMemoTable_Del(PyMemoTable *self)
721{
722 if (self == NULL)
723 return;
724 PyMemoTable_Clear(self);
725
726 PyMem_FREE(self->mt_table);
727 PyMem_FREE(self);
728}
729
730/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
731 can be considerably simpler than dictobject.c's lookdict(). */
732static PyMemoEntry *
733_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
734{
735 size_t i;
736 size_t perturb;
737 size_t mask = (size_t)self->mt_mask;
738 PyMemoEntry *table = self->mt_table;
739 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000740 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000741
742 i = hash & mask;
743 entry = &table[i];
744 if (entry->me_key == NULL || entry->me_key == key)
745 return entry;
746
747 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
748 i = (i << 2) + i + perturb + 1;
749 entry = &table[i & mask];
750 if (entry->me_key == NULL || entry->me_key == key)
751 return entry;
752 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700753 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000754}
755
756/* Returns -1 on failure, 0 on success. */
757static int
758_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
759{
760 PyMemoEntry *oldtable = NULL;
761 PyMemoEntry *oldentry, *newentry;
762 Py_ssize_t new_size = MT_MINSIZE;
763 Py_ssize_t to_process;
764
765 assert(min_size > 0);
766
767 /* Find the smallest valid table size >= min_size. */
768 while (new_size < min_size && new_size > 0)
769 new_size <<= 1;
770 if (new_size <= 0) {
771 PyErr_NoMemory();
772 return -1;
773 }
774 /* new_size needs to be a power of two. */
775 assert((new_size & (new_size - 1)) == 0);
776
777 /* Allocate new table. */
778 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500779 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000780 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200781 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000782 PyErr_NoMemory();
783 return -1;
784 }
785 self->mt_allocated = new_size;
786 self->mt_mask = new_size - 1;
787 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
788
789 /* Copy entries from the old table. */
790 to_process = self->mt_used;
791 for (oldentry = oldtable; to_process > 0; oldentry++) {
792 if (oldentry->me_key != NULL) {
793 to_process--;
794 /* newentry is a pointer to a chunk of the new
795 mt_table, so we're setting the key:value pair
796 in-place. */
797 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
798 newentry->me_key = oldentry->me_key;
799 newentry->me_value = oldentry->me_value;
800 }
801 }
802
803 /* Deallocate the old table. */
804 PyMem_FREE(oldtable);
805 return 0;
806}
807
808/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200809static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000810PyMemoTable_Get(PyMemoTable *self, PyObject *key)
811{
812 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
813 if (entry->me_key == NULL)
814 return NULL;
815 return &entry->me_value;
816}
817
818/* Returns -1 on failure, 0 on success. */
819static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200820PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000821{
822 PyMemoEntry *entry;
823
824 assert(key != NULL);
825
826 entry = _PyMemoTable_Lookup(self, key);
827 if (entry->me_key != NULL) {
828 entry->me_value = value;
829 return 0;
830 }
831 Py_INCREF(key);
832 entry->me_key = key;
833 entry->me_value = value;
834 self->mt_used++;
835
836 /* If we added a key, we can safely resize. Otherwise just return!
837 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
838 *
839 * Quadrupling the size improves average table sparseness
840 * (reducing collisions) at the cost of some memory. It also halves
841 * the number of expensive resize operations in a growing memo table.
842 *
843 * Very large memo tables (over 50K items) use doubling instead.
844 * This may help applications with severe memory constraints.
845 */
846 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
847 return 0;
848 return _PyMemoTable_ResizeTable(self,
849 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
850}
851
852#undef MT_MINSIZE
853#undef PERTURB_SHIFT
854
855/*************************************************************************/
856
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000857
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000858static int
859_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000860{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300861 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200862 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000863 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000864 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000865 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100866 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000867 return 0;
868}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000869
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100870static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100871_write_size64(char *out, size_t value)
872{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200873 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800874
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200875 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800876
877 for (i = 0; i < sizeof(size_t); i++) {
878 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
879 }
880 for (i = sizeof(size_t); i < 8; i++) {
881 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800882 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100883}
884
885static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100886_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
887{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100888 qdata[0] = FRAME;
889 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100890}
891
892static int
893_Pickler_CommitFrame(PicklerObject *self)
894{
895 size_t frame_len;
896 char *qdata;
897
898 if (!self->framing || self->frame_start == -1)
899 return 0;
900 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
901 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
902 _Pickler_WriteFrameHeader(self, qdata, frame_len);
903 self->frame_start = -1;
904 return 0;
905}
906
907static int
908_Pickler_OpcodeBoundary(PicklerObject *self)
909{
910 Py_ssize_t frame_len;
911
912 if (!self->framing || self->frame_start == -1)
913 return 0;
914 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
915 if (frame_len >= FRAME_SIZE_TARGET)
916 return _Pickler_CommitFrame(self);
917 else
918 return 0;
919}
920
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000921static PyObject *
922_Pickler_GetString(PicklerObject *self)
923{
924 PyObject *output_buffer = self->output_buffer;
925
926 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100927
928 if (_Pickler_CommitFrame(self))
929 return NULL;
930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000931 self->output_buffer = NULL;
932 /* Resize down to exact size */
933 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
934 return NULL;
935 return output_buffer;
936}
937
938static int
939_Pickler_FlushToFile(PicklerObject *self)
940{
941 PyObject *output, *result;
942
943 assert(self->write != NULL);
944
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100945 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000946 output = _Pickler_GetString(self);
947 if (output == NULL)
948 return -1;
949
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800950 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000951 Py_XDECREF(result);
952 return (result == NULL) ? -1 : 0;
953}
954
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200955static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100956_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000957{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100958 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000959 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100960 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000961
962 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100963 need_new_frame = (self->framing && self->frame_start == -1);
964
965 if (need_new_frame)
966 n = data_len + FRAME_HEADER_SIZE;
967 else
968 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000969
970 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100971 if (required > self->max_output_len) {
972 /* Make place in buffer for the pickle chunk */
973 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
974 PyErr_NoMemory();
975 return -1;
976 }
977 self->max_output_len = (self->output_len + n) / 2 * 3;
978 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
979 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000980 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000981 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 if (need_new_frame) {
983 /* Setup new frame */
984 Py_ssize_t frame_start = self->output_len;
985 self->frame_start = frame_start;
986 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
987 /* Write an invalid value, for debugging */
988 buffer[frame_start + i] = 0xFE;
989 }
990 self->output_len += FRAME_HEADER_SIZE;
991 }
992 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000993 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100994 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000995 buffer[self->output_len + i] = s[i];
996 }
997 }
998 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100999 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001000 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001001 self->output_len += data_len;
1002 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001003}
1004
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001005static PicklerObject *
1006_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001007{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001008 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001009
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001010 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1011 if (self == NULL)
1012 return NULL;
1013
1014 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001015 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001016 self->write = NULL;
1017 self->proto = 0;
1018 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001019 self->framing = 0;
1020 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021 self->fast = 0;
1022 self->fast_nesting = 0;
1023 self->fix_imports = 0;
1024 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001025 self->max_output_len = WRITE_BUF_SIZE;
1026 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001027
1028 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001029 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1030 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001031
1032 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001033 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001034 return NULL;
1035 }
1036 return self;
1037}
1038
1039static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001040_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001042 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001043
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001044 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001045 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001046 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001047 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001048 proto = PyLong_AsLong(protocol);
1049 if (proto < 0) {
1050 if (proto == -1 && PyErr_Occurred())
1051 return -1;
1052 proto = HIGHEST_PROTOCOL;
1053 }
1054 else if (proto > HIGHEST_PROTOCOL) {
1055 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1056 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001057 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001058 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001059 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001060 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001061 self->bin = proto > 0;
1062 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001063 return 0;
1064}
1065
1066/* Returns -1 (with an exception set) on failure, 0 on success. This may
1067 be called once on a freshly created Pickler. */
1068static int
1069_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1070{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001071 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001072 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001073 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001074 if (self->write == NULL) {
1075 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1076 PyErr_SetString(PyExc_TypeError,
1077 "file must have a 'write' attribute");
1078 return -1;
1079 }
1080
1081 return 0;
1082}
1083
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001084/* Returns the size of the input on success, -1 on failure. This takes its
1085 own reference to `input`. */
1086static Py_ssize_t
1087_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1088{
1089 if (self->buffer.buf != NULL)
1090 PyBuffer_Release(&self->buffer);
1091 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1092 return -1;
1093 self->input_buffer = self->buffer.buf;
1094 self->input_len = self->buffer.len;
1095 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001096 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001097 return self->input_len;
1098}
1099
Antoine Pitrou04248a82010-10-12 20:51:21 +00001100static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001101bad_readline(void)
1102{
1103 PickleState *st = _Pickle_GetGlobalState();
1104 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1105 return -1;
1106}
1107
1108static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001109_Unpickler_SkipConsumed(UnpicklerObject *self)
1110{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001111 Py_ssize_t consumed;
1112 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001113
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001114 consumed = self->next_read_idx - self->prefetched_idx;
1115 if (consumed <= 0)
1116 return 0;
1117
1118 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001119 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001120 r = PyObject_CallFunction(self->read, "n", consumed);
1121 if (r == NULL)
1122 return -1;
1123 Py_DECREF(r);
1124
1125 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001126 return 0;
1127}
1128
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001129static const Py_ssize_t READ_WHOLE_LINE = -1;
1130
1131/* If reading from a file, we need to only pull the bytes we need, since there
1132 may be multiple pickle objects arranged contiguously in the same input
1133 buffer.
1134
1135 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1136 bytes from the input stream/buffer.
1137
1138 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1139 failure; on success, returns the number of bytes read from the file.
1140
1141 On success, self->input_len will be 0; this is intentional so that when
1142 unpickling from a file, the "we've run out of data" code paths will trigger,
1143 causing the Unpickler to go back to the file for more data. Use the returned
1144 size to tell you how much data you can process. */
1145static Py_ssize_t
1146_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1147{
1148 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001149 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001150
1151 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001152
Antoine Pitrou04248a82010-10-12 20:51:21 +00001153 if (_Unpickler_SkipConsumed(self) < 0)
1154 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001155
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001156 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001157 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001158 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001159 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001160 PyObject *len;
1161 /* Prefetch some data without advancing the file pointer, if possible */
1162 if (self->peek && n < PREFETCH) {
1163 len = PyLong_FromSsize_t(PREFETCH);
1164 if (len == NULL)
1165 return -1;
1166 data = _Pickle_FastCall(self->peek, len);
1167 if (data == NULL) {
1168 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1169 return -1;
1170 /* peek() is probably not supported by the given file object */
1171 PyErr_Clear();
1172 Py_CLEAR(self->peek);
1173 }
1174 else {
1175 read_size = _Unpickler_SetStringInput(self, data);
1176 Py_DECREF(data);
1177 self->prefetched_idx = 0;
1178 if (n <= read_size)
1179 return n;
1180 }
1181 }
1182 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001183 if (len == NULL)
1184 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001185 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001186 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001187 if (data == NULL)
1188 return -1;
1189
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001190 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001191 Py_DECREF(data);
1192 return read_size;
1193}
1194
Victor Stinner19ed27e2016-05-20 11:42:37 +02001195/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001196static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001197_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001198{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001199 Py_ssize_t num_read;
1200
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001201 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001202 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1203 PickleState *st = _Pickle_GetGlobalState();
1204 PyErr_SetString(st->UnpicklingError,
1205 "read would overflow (invalid bytecode)");
1206 return -1;
1207 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001208
1209 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1210 assert(self->next_read_idx + n > self->input_len);
1211
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001212 if (!self->read)
1213 return bad_readline();
1214
Antoine Pitrou04248a82010-10-12 20:51:21 +00001215 num_read = _Unpickler_ReadFromFile(self, n);
1216 if (num_read < 0)
1217 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001218 if (num_read < n)
1219 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001220 *s = self->input_buffer;
1221 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001222 return n;
1223}
1224
Victor Stinner19ed27e2016-05-20 11:42:37 +02001225/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1226
1227 This should be used for all data reads, rather than accessing the unpickler's
1228 input buffer directly. This method deals correctly with reading from input
1229 streams, which the input buffer doesn't deal with.
1230
1231 Note that when reading from a file-like object, self->next_read_idx won't
1232 be updated (it should remain at 0 for the entire unpickling process). You
1233 should use this function's return value to know how many bytes you can
1234 consume.
1235
1236 Returns -1 (with an exception set) on failure. On success, return the
1237 number of chars read. */
1238#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001239 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001240 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1241 (self)->next_read_idx += (n), \
1242 (n)) \
1243 : _Unpickler_ReadImpl(self, (s), (n)))
1244
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001245static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001246_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1247 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001248{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001249 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001250 if (input_line == NULL) {
1251 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001252 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001254
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001255 memcpy(input_line, line, len);
1256 input_line[len] = '\0';
1257 self->input_line = input_line;
1258 *result = self->input_line;
1259 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001260}
1261
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001262/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001263 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001264
1265 Returns the number of chars read, or -1 on failure. */
1266static Py_ssize_t
1267_Unpickler_Readline(UnpicklerObject *self, char **result)
1268{
1269 Py_ssize_t i, num_read;
1270
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001271 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001272 if (self->input_buffer[i] == '\n') {
1273 char *line_start = self->input_buffer + self->next_read_idx;
1274 num_read = i - self->next_read_idx + 1;
1275 self->next_read_idx = i + 1;
1276 return _Unpickler_CopyLine(self, line_start, num_read, result);
1277 }
1278 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001279 if (!self->read)
1280 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001281
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001282 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1283 if (num_read < 0)
1284 return -1;
1285 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1286 return bad_readline();
1287 self->next_read_idx = num_read;
1288 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001289}
1290
1291/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1292 will be modified in place. */
1293static int
1294_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1295{
1296 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001297
1298 assert(new_size > self->memo_size);
1299
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001300 PyMem_RESIZE(self->memo, PyObject *, new_size);
1301 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001302 PyErr_NoMemory();
1303 return -1;
1304 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001305 for (i = self->memo_size; i < new_size; i++)
1306 self->memo[i] = NULL;
1307 self->memo_size = new_size;
1308 return 0;
1309}
1310
1311/* Returns NULL if idx is out of bounds. */
1312static PyObject *
1313_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1314{
1315 if (idx < 0 || idx >= self->memo_size)
1316 return NULL;
1317
1318 return self->memo[idx];
1319}
1320
1321/* Returns -1 (with an exception set) on failure, 0 on success.
1322 This takes its own reference to `value`. */
1323static int
1324_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1325{
1326 PyObject *old_item;
1327
1328 if (idx >= self->memo_size) {
1329 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1330 return -1;
1331 assert(idx < self->memo_size);
1332 }
1333 Py_INCREF(value);
1334 old_item = self->memo[idx];
1335 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001336 if (old_item != NULL) {
1337 Py_DECREF(old_item);
1338 }
1339 else {
1340 self->memo_len++;
1341 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001342 return 0;
1343}
1344
1345static PyObject **
1346_Unpickler_NewMemo(Py_ssize_t new_size)
1347{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001348 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001349 if (memo == NULL) {
1350 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001351 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001352 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001353 memset(memo, 0, new_size * sizeof(PyObject *));
1354 return memo;
1355}
1356
1357/* Free the unpickler's memo, taking care to decref any items left in it. */
1358static void
1359_Unpickler_MemoCleanup(UnpicklerObject *self)
1360{
1361 Py_ssize_t i;
1362 PyObject **memo = self->memo;
1363
1364 if (self->memo == NULL)
1365 return;
1366 self->memo = NULL;
1367 i = self->memo_size;
1368 while (--i >= 0) {
1369 Py_XDECREF(memo[i]);
1370 }
1371 PyMem_FREE(memo);
1372}
1373
1374static UnpicklerObject *
1375_Unpickler_New(void)
1376{
1377 UnpicklerObject *self;
1378
1379 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1380 if (self == NULL)
1381 return NULL;
1382
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001383 self->pers_func = NULL;
1384 self->input_buffer = NULL;
1385 self->input_line = NULL;
1386 self->input_len = 0;
1387 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001388 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389 self->read = NULL;
1390 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001391 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001392 self->encoding = NULL;
1393 self->errors = NULL;
1394 self->marks = NULL;
1395 self->num_marks = 0;
1396 self->marks_size = 0;
1397 self->proto = 0;
1398 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001399 memset(&self->buffer, 0, sizeof(Py_buffer));
1400 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001401 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001402 self->memo = _Unpickler_NewMemo(self->memo_size);
1403 self->stack = (Pdata *)Pdata_New();
1404
1405 if (self->memo == NULL || self->stack == NULL) {
1406 Py_DECREF(self);
1407 return NULL;
1408 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001409
1410 return self;
1411}
1412
1413/* Returns -1 (with an exception set) on failure, 0 on success. This may
1414 be called once on a freshly created Pickler. */
1415static int
1416_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1417{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001418 _Py_IDENTIFIER(peek);
1419 _Py_IDENTIFIER(read);
1420 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001421
1422 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001423 if (self->peek == NULL) {
1424 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1425 PyErr_Clear();
1426 else
1427 return -1;
1428 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001429 self->read = _PyObject_GetAttrId(file, &PyId_read);
1430 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001431 if (self->readline == NULL || self->read == NULL) {
1432 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1433 PyErr_SetString(PyExc_TypeError,
1434 "file must have 'read' and 'readline' attributes");
1435 Py_CLEAR(self->read);
1436 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001437 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001438 return -1;
1439 }
1440 return 0;
1441}
1442
1443/* Returns -1 (with an exception set) on failure, 0 on success. This may
1444 be called once on a freshly created Pickler. */
1445static int
1446_Unpickler_SetInputEncoding(UnpicklerObject *self,
1447 const char *encoding,
1448 const char *errors)
1449{
1450 if (encoding == NULL)
1451 encoding = "ASCII";
1452 if (errors == NULL)
1453 errors = "strict";
1454
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001455 self->encoding = _PyMem_Strdup(encoding);
1456 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001457 if (self->encoding == NULL || self->errors == NULL) {
1458 PyErr_NoMemory();
1459 return -1;
1460 }
1461 return 0;
1462}
1463
1464/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001465static int
1466memo_get(PicklerObject *self, PyObject *key)
1467{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001468 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001469 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001470 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001471
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001472 value = PyMemoTable_Get(self->memo, key);
1473 if (value == NULL) {
1474 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001475 return -1;
1476 }
1477
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001478 if (!self->bin) {
1479 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001480 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1481 "%" PY_FORMAT_SIZE_T "d\n", *value);
1482 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001483 }
1484 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001485 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001486 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001487 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001488 len = 2;
1489 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001490 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001491 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001492 pdata[1] = (unsigned char)(*value & 0xff);
1493 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1494 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1495 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001496 len = 5;
1497 }
1498 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001499 PickleState *st = _Pickle_GetGlobalState();
1500 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001501 "memo id too large for LONG_BINGET");
1502 return -1;
1503 }
1504 }
1505
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001506 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001507 return -1;
1508
1509 return 0;
1510}
1511
1512/* Store an object in the memo, assign it a new unique ID based on the number
1513 of objects currently stored in the memo and generate a PUT opcode. */
1514static int
1515memo_put(PicklerObject *self, PyObject *obj)
1516{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001517 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001518 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001519 Py_ssize_t idx;
1520
1521 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001522
1523 if (self->fast)
1524 return 0;
1525
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001526 idx = PyMemoTable_Size(self->memo);
1527 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1528 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001529
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001530 if (self->proto >= 4) {
1531 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1532 return -1;
1533 return 0;
1534 }
1535 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001536 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001537 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001538 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001539 len = strlen(pdata);
1540 }
1541 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001542 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001543 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001544 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001545 len = 2;
1546 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001547 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001548 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001549 pdata[1] = (unsigned char)(idx & 0xff);
1550 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1551 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1552 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001553 len = 5;
1554 }
1555 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001556 PickleState *st = _Pickle_GetGlobalState();
1557 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558 "memo id too large for LONG_BINPUT");
1559 return -1;
1560 }
1561 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001562 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001563 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001564
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001565 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001566}
1567
1568static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001569get_dotted_path(PyObject *obj, PyObject *name)
1570{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001571 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001572 PyObject *dotted_path;
1573 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001574
1575 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001576 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001577 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001578 n = PyList_GET_SIZE(dotted_path);
1579 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001580 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001581 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001582 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001583 if (obj == NULL)
1584 PyErr_Format(PyExc_AttributeError,
1585 "Can't pickle local object %R", name);
1586 else
1587 PyErr_Format(PyExc_AttributeError,
1588 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001589 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001590 return NULL;
1591 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001592 }
1593 return dotted_path;
1594}
1595
1596static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001597get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001598{
1599 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001600 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001601
1602 assert(PyList_CheckExact(names));
1603 Py_INCREF(obj);
1604 n = PyList_GET_SIZE(names);
1605 for (i = 0; i < n; i++) {
1606 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001607 Py_XDECREF(parent);
1608 parent = obj;
1609 obj = PyObject_GetAttr(parent, name);
1610 if (obj == NULL) {
1611 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001612 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001613 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001614 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001615 if (pparent != NULL)
1616 *pparent = parent;
1617 else
1618 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001619 return obj;
1620}
1621
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001622static void
1623reformat_attribute_error(PyObject *obj, PyObject *name)
1624{
1625 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1626 PyErr_Clear();
1627 PyErr_Format(PyExc_AttributeError,
1628 "Can't get attribute %R on %R", name, obj);
1629 }
1630}
1631
1632
1633static PyObject *
1634getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1635{
1636 PyObject *dotted_path, *attr;
1637
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001638 if (allow_qualname) {
1639 dotted_path = get_dotted_path(obj, name);
1640 if (dotted_path == NULL)
1641 return NULL;
1642 attr = get_deep_attribute(obj, dotted_path, NULL);
1643 Py_DECREF(dotted_path);
1644 }
1645 else
1646 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001647 if (attr == NULL)
1648 reformat_attribute_error(obj, name);
1649 return attr;
1650}
1651
Eric Snow3f9eee62017-09-15 16:35:20 -06001652static int
1653_checkmodule(PyObject *module_name, PyObject *module,
1654 PyObject *global, PyObject *dotted_path)
1655{
1656 if (module == Py_None) {
1657 return -1;
1658 }
1659 if (PyUnicode_Check(module_name) &&
1660 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1661 return -1;
1662 }
1663
1664 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1665 if (candidate == NULL) {
1666 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1667 PyErr_Clear();
1668 }
1669 return -1;
1670 }
1671 if (candidate != global) {
1672 Py_DECREF(candidate);
1673 return -1;
1674 }
1675 Py_DECREF(candidate);
1676 return 0;
1677}
1678
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001679static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001680whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001681{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001682 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001683 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001684 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001685 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001686 _Py_IDENTIFIER(__module__);
1687 _Py_IDENTIFIER(modules);
1688 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001689
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001690 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1691
1692 if (module_name == NULL) {
1693 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001694 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001696 }
1697 else {
1698 /* In some rare cases (e.g., bound methods of extension types),
1699 __module__ can be None. If it is so, then search sys.modules for
1700 the module of global. */
1701 if (module_name != Py_None)
1702 return module_name;
1703 Py_CLEAR(module_name);
1704 }
1705 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001706
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001707 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001708 modules = _PySys_GetObjectId(&PyId_modules);
1709 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001710 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001711 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001712 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001713 if (PyDict_CheckExact(modules)) {
1714 i = 0;
1715 while (PyDict_Next(modules, &i, &module_name, &module)) {
1716 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1717 Py_INCREF(module_name);
1718 return module_name;
1719 }
1720 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001721 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001722 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001723 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001724 }
1725 else {
1726 PyObject *iterator = PyObject_GetIter(modules);
1727 if (iterator == NULL) {
1728 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001729 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001730 while ((module_name = PyIter_Next(iterator))) {
1731 module = PyObject_GetItem(modules, module_name);
1732 if (module == NULL) {
1733 Py_DECREF(module_name);
1734 Py_DECREF(iterator);
1735 return NULL;
1736 }
1737 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1738 Py_DECREF(module);
1739 Py_DECREF(iterator);
1740 return module_name;
1741 }
1742 Py_DECREF(module);
1743 Py_DECREF(module_name);
1744 if (PyErr_Occurred()) {
1745 Py_DECREF(iterator);
1746 return NULL;
1747 }
1748 }
1749 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001750 }
1751
1752 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001753 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001754 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001755 return module_name;
1756}
1757
1758/* fast_save_enter() and fast_save_leave() are guards against recursive
1759 objects when Pickler is used with the "fast mode" (i.e., with object
1760 memoization disabled). If the nesting of a list or dict object exceed
1761 FAST_NESTING_LIMIT, these guards will start keeping an internal
1762 reference to the seen list or dict objects and check whether these objects
1763 are recursive. These are not strictly necessary, since save() has a
1764 hard-coded recursion limit, but they give a nicer error message than the
1765 typical RuntimeError. */
1766static int
1767fast_save_enter(PicklerObject *self, PyObject *obj)
1768{
1769 /* if fast_nesting < 0, we're doing an error exit. */
1770 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1771 PyObject *key = NULL;
1772 if (self->fast_memo == NULL) {
1773 self->fast_memo = PyDict_New();
1774 if (self->fast_memo == NULL) {
1775 self->fast_nesting = -1;
1776 return 0;
1777 }
1778 }
1779 key = PyLong_FromVoidPtr(obj);
1780 if (key == NULL)
1781 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001782 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001783 Py_DECREF(key);
1784 PyErr_Format(PyExc_ValueError,
1785 "fast mode: can't pickle cyclic objects "
1786 "including object type %.200s at %p",
1787 obj->ob_type->tp_name, obj);
1788 self->fast_nesting = -1;
1789 return 0;
1790 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001791 if (PyErr_Occurred()) {
1792 return 0;
1793 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001794 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1795 Py_DECREF(key);
1796 self->fast_nesting = -1;
1797 return 0;
1798 }
1799 Py_DECREF(key);
1800 }
1801 return 1;
1802}
1803
1804static int
1805fast_save_leave(PicklerObject *self, PyObject *obj)
1806{
1807 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1808 PyObject *key = PyLong_FromVoidPtr(obj);
1809 if (key == NULL)
1810 return 0;
1811 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1812 Py_DECREF(key);
1813 return 0;
1814 }
1815 Py_DECREF(key);
1816 }
1817 return 1;
1818}
1819
1820static int
1821save_none(PicklerObject *self, PyObject *obj)
1822{
1823 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001824 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001825 return -1;
1826
1827 return 0;
1828}
1829
1830static int
1831save_bool(PicklerObject *self, PyObject *obj)
1832{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001833 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001834 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001835 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001836 return -1;
1837 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001838 else {
1839 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1840 * so that unpicklers written before bools were introduced unpickle them
1841 * as ints, but unpicklers after can recognize that bools were intended.
1842 * Note that protocol 2 added direct ways to pickle bools.
1843 */
1844 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1845 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1846 return -1;
1847 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001848 return 0;
1849}
1850
1851static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001852save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001853{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001854 PyObject *repr = NULL;
1855 Py_ssize_t size;
1856 long val;
1857 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001858
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001859 const char long_op = LONG;
1860
1861 val= PyLong_AsLong(obj);
1862 if (val == -1 && PyErr_Occurred()) {
1863 /* out of range for int pickling */
1864 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001866 else if (self->bin &&
1867 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001868 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001869 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001870
1871 Note: we can't use -0x80000000L in the above condition because some
1872 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1873 before applying the unary minus when sizeof(long) <= 4. The
1874 resulting value stays unsigned which is commonly not what we want,
1875 so MSVC happily warns us about it. However, that result would have
1876 been fine because we guard for sizeof(long) <= 4 which turns the
1877 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001878 char pdata[32];
1879 Py_ssize_t len = 0;
1880
1881 pdata[1] = (unsigned char)(val & 0xff);
1882 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1883 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1884 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001885
1886 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1887 if (pdata[2] == 0) {
1888 pdata[0] = BININT1;
1889 len = 2;
1890 }
1891 else {
1892 pdata[0] = BININT2;
1893 len = 3;
1894 }
1895 }
1896 else {
1897 pdata[0] = BININT;
1898 len = 5;
1899 }
1900
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001901 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001902 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001903
1904 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001905 }
1906
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001907 if (self->proto >= 2) {
1908 /* Linear-time pickling. */
1909 size_t nbits;
1910 size_t nbytes;
1911 unsigned char *pdata;
1912 char header[5];
1913 int i;
1914 int sign = _PyLong_Sign(obj);
1915
1916 if (sign == 0) {
1917 header[0] = LONG1;
1918 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001919 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001920 goto error;
1921 return 0;
1922 }
1923 nbits = _PyLong_NumBits(obj);
1924 if (nbits == (size_t)-1 && PyErr_Occurred())
1925 goto error;
1926 /* How many bytes do we need? There are nbits >> 3 full
1927 * bytes of data, and nbits & 7 leftover bits. If there
1928 * are any leftover bits, then we clearly need another
1929 * byte. Wnat's not so obvious is that we *probably*
1930 * need another byte even if there aren't any leftovers:
1931 * the most-significant bit of the most-significant byte
1932 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001933 * opposite of the one we need. The exception is ints
1934 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001935 * its own 256's-complement, so has the right sign bit
1936 * even without the extra byte. That's a pain to check
1937 * for in advance, though, so we always grab an extra
1938 * byte at the start, and cut it back later if possible.
1939 */
1940 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001941 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001942 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001943 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001944 goto error;
1945 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001946 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001947 if (repr == NULL)
1948 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001949 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001950 i = _PyLong_AsByteArray((PyLongObject *)obj,
1951 pdata, nbytes,
1952 1 /* little endian */ , 1 /* signed */ );
1953 if (i < 0)
1954 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001955 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001956 * needed. This is so iff the MSB is all redundant sign
1957 * bits.
1958 */
1959 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001960 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001961 pdata[nbytes - 1] == 0xff &&
1962 (pdata[nbytes - 2] & 0x80) != 0) {
1963 nbytes--;
1964 }
1965
1966 if (nbytes < 256) {
1967 header[0] = LONG1;
1968 header[1] = (unsigned char)nbytes;
1969 size = 2;
1970 }
1971 else {
1972 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001973 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001974 for (i = 1; i < 5; i++) {
1975 header[i] = (unsigned char)(size & 0xff);
1976 size >>= 8;
1977 }
1978 size = 5;
1979 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001980 if (_Pickler_Write(self, header, size) < 0 ||
1981 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001982 goto error;
1983 }
1984 else {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001985 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001986
Mark Dickinson8dd05142009-01-20 20:43:58 +00001987 /* proto < 2: write the repr and newline. This is quadratic-time (in
1988 the number of digits), in both directions. We add a trailing 'L'
1989 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001990
1991 repr = PyObject_Repr(obj);
1992 if (repr == NULL)
1993 goto error;
1994
Serhiy Storchaka06515832016-11-20 09:13:07 +02001995 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001996 if (string == NULL)
1997 goto error;
1998
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001999 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2000 _Pickler_Write(self, string, size) < 0 ||
2001 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002002 goto error;
2003 }
2004
2005 if (0) {
2006 error:
2007 status = -1;
2008 }
2009 Py_XDECREF(repr);
2010
2011 return status;
2012}
2013
2014static int
2015save_float(PicklerObject *self, PyObject *obj)
2016{
2017 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2018
2019 if (self->bin) {
2020 char pdata[9];
2021 pdata[0] = BINFLOAT;
2022 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2023 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002024 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002025 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002026 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002027 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002028 int result = -1;
2029 char *buf = NULL;
2030 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002031
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002032 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002033 goto done;
2034
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002035 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002036 if (!buf) {
2037 PyErr_NoMemory();
2038 goto done;
2039 }
2040
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002041 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002042 goto done;
2043
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002044 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002045 goto done;
2046
2047 result = 0;
2048done:
2049 PyMem_Free(buf);
2050 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002051 }
2052
2053 return 0;
2054}
2055
2056static int
2057save_bytes(PicklerObject *self, PyObject *obj)
2058{
2059 if (self->proto < 3) {
2060 /* Older pickle protocols do not have an opcode for pickling bytes
2061 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002062 the __reduce__ method) to permit bytes object unpickling.
2063
2064 Here we use a hack to be compatible with Python 2. Since in Python
2065 2 'bytes' is just an alias for 'str' (which has different
2066 parameters than the actual bytes object), we use codecs.encode
2067 to create the appropriate 'str' object when unpickled using
2068 Python 2 *and* the appropriate 'bytes' object when unpickled
2069 using Python 3. Again this is a hack and we don't need to do this
2070 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002071 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002072 int status;
2073
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002074 if (PyBytes_GET_SIZE(obj) == 0) {
2075 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2076 }
2077 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002078 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002079 PyObject *unicode_str =
2080 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2081 PyBytes_GET_SIZE(obj),
2082 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002083 _Py_IDENTIFIER(latin1);
2084
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002085 if (unicode_str == NULL)
2086 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002087 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002088 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002089 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002090 Py_DECREF(unicode_str);
2091 }
2092
2093 if (reduce_value == NULL)
2094 return -1;
2095
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 /* save_reduce() will memoize the object automatically. */
2097 status = save_reduce(self, reduce_value, obj);
2098 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 return status;
2100 }
2101 else {
2102 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002103 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002104 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002105
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002106 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002107 if (size < 0)
2108 return -1;
2109
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002110 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002111 header[0] = SHORT_BINBYTES;
2112 header[1] = (unsigned char)size;
2113 len = 2;
2114 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002115 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002116 header[0] = BINBYTES;
2117 header[1] = (unsigned char)(size & 0xff);
2118 header[2] = (unsigned char)((size >> 8) & 0xff);
2119 header[3] = (unsigned char)((size >> 16) & 0xff);
2120 header[4] = (unsigned char)((size >> 24) & 0xff);
2121 len = 5;
2122 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002123 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002124 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002125 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002126 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002127 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002128 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002129 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002130 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002131 return -1; /* string too large */
2132 }
2133
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002134 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002135 return -1;
2136
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002137 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002138 return -1;
2139
2140 if (memo_put(self, obj) < 0)
2141 return -1;
2142
2143 return 0;
2144 }
2145}
2146
2147/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2148 backslash and newline characters to \uXXXX escapes. */
2149static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002150raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002151{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002152 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002153 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002154 void *data;
2155 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002156 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002157
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002158 if (PyUnicode_READY(obj))
2159 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002160
Victor Stinner358af132015-10-12 22:36:57 +02002161 _PyBytesWriter_Init(&writer);
2162
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002163 size = PyUnicode_GET_LENGTH(obj);
2164 data = PyUnicode_DATA(obj);
2165 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002166
Victor Stinner358af132015-10-12 22:36:57 +02002167 p = _PyBytesWriter_Alloc(&writer, size);
2168 if (p == NULL)
2169 goto error;
2170 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002171
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002172 for (i=0; i < size; i++) {
2173 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002174 /* Map 32-bit characters to '\Uxxxxxxxx' */
2175 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002176 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002177 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2178 if (p == NULL)
2179 goto error;
2180
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002181 *p++ = '\\';
2182 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002183 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2184 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2185 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2186 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2187 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2188 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2189 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2190 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002191 }
Victor Stinner358af132015-10-12 22:36:57 +02002192 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002193 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002194 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002195 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2196 if (p == NULL)
2197 goto error;
2198
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002199 *p++ = '\\';
2200 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002201 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2202 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2203 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2204 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002205 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002206 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002207 else
2208 *p++ = (char) ch;
2209 }
Victor Stinner358af132015-10-12 22:36:57 +02002210
2211 return _PyBytesWriter_Finish(&writer, p);
2212
2213error:
2214 _PyBytesWriter_Dealloc(&writer);
2215 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002216}
2217
2218static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002219write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002220{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002221 char header[9];
2222 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002223
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002224 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002225 if (size <= 0xff && self->proto >= 4) {
2226 header[0] = SHORT_BINUNICODE;
2227 header[1] = (unsigned char)(size & 0xff);
2228 len = 2;
2229 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002230 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002231 header[0] = BINUNICODE;
2232 header[1] = (unsigned char)(size & 0xff);
2233 header[2] = (unsigned char)((size >> 8) & 0xff);
2234 header[3] = (unsigned char)((size >> 16) & 0xff);
2235 header[4] = (unsigned char)((size >> 24) & 0xff);
2236 len = 5;
2237 }
2238 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002239 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002240 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002241 len = 9;
2242 }
2243 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002244 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002245 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002246 return -1;
2247 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002248
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002249 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002250 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002251 if (_Pickler_Write(self, data, size) < 0)
2252 return -1;
2253
2254 return 0;
2255}
2256
2257static int
2258write_unicode_binary(PicklerObject *self, PyObject *obj)
2259{
2260 PyObject *encoded = NULL;
2261 Py_ssize_t size;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002262 const char *data;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002263 int r;
2264
2265 if (PyUnicode_READY(obj))
2266 return -1;
2267
2268 data = PyUnicode_AsUTF8AndSize(obj, &size);
2269 if (data != NULL)
2270 return write_utf8(self, data, size);
2271
2272 /* Issue #8383: for strings with lone surrogates, fallback on the
2273 "surrogatepass" error handler. */
2274 PyErr_Clear();
2275 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2276 if (encoded == NULL)
2277 return -1;
2278
2279 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2280 PyBytes_GET_SIZE(encoded));
2281 Py_DECREF(encoded);
2282 return r;
2283}
2284
2285static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002286save_unicode(PicklerObject *self, PyObject *obj)
2287{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002288 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002289 if (write_unicode_binary(self, obj) < 0)
2290 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002291 }
2292 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002293 PyObject *encoded;
2294 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295 const char unicode_op = UNICODE;
2296
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002297 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002298 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002299 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002300
Antoine Pitrou299978d2013-04-07 17:38:11 +02002301 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2302 Py_DECREF(encoded);
2303 return -1;
2304 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002305
2306 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002307 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2308 Py_DECREF(encoded);
2309 return -1;
2310 }
2311 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002312
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002313 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002314 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002315 }
2316 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002317 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002318
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002319 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002320}
2321
2322/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2323static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002324store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002325{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002326 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002327
2328 assert(PyTuple_Size(t) == len);
2329
2330 for (i = 0; i < len; i++) {
2331 PyObject *element = PyTuple_GET_ITEM(t, i);
2332
2333 if (element == NULL)
2334 return -1;
2335 if (save(self, element, 0) < 0)
2336 return -1;
2337 }
2338
2339 return 0;
2340}
2341
2342/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2343 * used across protocols to minimize the space needed to pickle them.
2344 * Tuples are also the only builtin immutable type that can be recursive
2345 * (a tuple can be reached from itself), and that requires some subtle
2346 * magic so that it works in all cases. IOW, this is a long routine.
2347 */
2348static int
2349save_tuple(PicklerObject *self, PyObject *obj)
2350{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002351 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002352
2353 const char mark_op = MARK;
2354 const char tuple_op = TUPLE;
2355 const char pop_op = POP;
2356 const char pop_mark_op = POP_MARK;
2357 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2358
2359 if ((len = PyTuple_Size(obj)) < 0)
2360 return -1;
2361
2362 if (len == 0) {
2363 char pdata[2];
2364
2365 if (self->proto) {
2366 pdata[0] = EMPTY_TUPLE;
2367 len = 1;
2368 }
2369 else {
2370 pdata[0] = MARK;
2371 pdata[1] = TUPLE;
2372 len = 2;
2373 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002374 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002375 return -1;
2376 return 0;
2377 }
2378
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002379 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002380 * saving the tuple elements, the tuple must be recursive, in
2381 * which case we'll pop everything we put on the stack, and fetch
2382 * its value from the memo.
2383 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002384 if (len <= 3 && self->proto >= 2) {
2385 /* Use TUPLE{1,2,3} opcodes. */
2386 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002387 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002388
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002389 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002390 /* pop the len elements */
2391 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002392 if (_Pickler_Write(self, &pop_op, 1) < 0)
2393 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002394 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002395 if (memo_get(self, obj) < 0)
2396 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002397
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002398 return 0;
2399 }
2400 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002401 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2402 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002403 }
2404 goto memoize;
2405 }
2406
2407 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2408 * Generate MARK e1 e2 ... TUPLE
2409 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002410 if (_Pickler_Write(self, &mark_op, 1) < 0)
2411 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002412
2413 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002414 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002415
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002416 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002417 /* pop the stack stuff we pushed */
2418 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002419 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2420 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002421 }
2422 else {
2423 /* Note that we pop one more than len, to remove
2424 * the MARK too.
2425 */
2426 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002427 if (_Pickler_Write(self, &pop_op, 1) < 0)
2428 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002429 }
2430 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002431 if (memo_get(self, obj) < 0)
2432 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002433
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002434 return 0;
2435 }
2436 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002437 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2438 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002439 }
2440
2441 memoize:
2442 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002443 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002444
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002445 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002446}
2447
2448/* iter is an iterator giving items, and we batch up chunks of
2449 * MARK item item ... item APPENDS
2450 * opcode sequences. Calling code should have arranged to first create an
2451 * empty list, or list-like object, for the APPENDS to operate on.
2452 * Returns 0 on success, <0 on error.
2453 */
2454static int
2455batch_list(PicklerObject *self, PyObject *iter)
2456{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002457 PyObject *obj = NULL;
2458 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002459 int i, n;
2460
2461 const char mark_op = MARK;
2462 const char append_op = APPEND;
2463 const char appends_op = APPENDS;
2464
2465 assert(iter != NULL);
2466
2467 /* XXX: I think this function could be made faster by avoiding the
2468 iterator interface and fetching objects directly from list using
2469 PyList_GET_ITEM.
2470 */
2471
2472 if (self->proto == 0) {
2473 /* APPENDS isn't available; do one at a time. */
2474 for (;;) {
2475 obj = PyIter_Next(iter);
2476 if (obj == NULL) {
2477 if (PyErr_Occurred())
2478 return -1;
2479 break;
2480 }
2481 i = save(self, obj, 0);
2482 Py_DECREF(obj);
2483 if (i < 0)
2484 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002485 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002486 return -1;
2487 }
2488 return 0;
2489 }
2490
2491 /* proto > 0: write in batches of BATCHSIZE. */
2492 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002493 /* Get first item */
2494 firstitem = PyIter_Next(iter);
2495 if (firstitem == NULL) {
2496 if (PyErr_Occurred())
2497 goto error;
2498
2499 /* nothing more to add */
2500 break;
2501 }
2502
2503 /* Try to get a second item */
2504 obj = PyIter_Next(iter);
2505 if (obj == NULL) {
2506 if (PyErr_Occurred())
2507 goto error;
2508
2509 /* Only one item to write */
2510 if (save(self, firstitem, 0) < 0)
2511 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002512 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002513 goto error;
2514 Py_CLEAR(firstitem);
2515 break;
2516 }
2517
2518 /* More than one item to write */
2519
2520 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002521 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002522 goto error;
2523
2524 if (save(self, firstitem, 0) < 0)
2525 goto error;
2526 Py_CLEAR(firstitem);
2527 n = 1;
2528
2529 /* Fetch and save up to BATCHSIZE items */
2530 while (obj) {
2531 if (save(self, obj, 0) < 0)
2532 goto error;
2533 Py_CLEAR(obj);
2534 n += 1;
2535
2536 if (n == BATCHSIZE)
2537 break;
2538
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002539 obj = PyIter_Next(iter);
2540 if (obj == NULL) {
2541 if (PyErr_Occurred())
2542 goto error;
2543 break;
2544 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002545 }
2546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002547 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002548 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002549
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002550 } while (n == BATCHSIZE);
2551 return 0;
2552
2553 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002554 Py_XDECREF(firstitem);
2555 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556 return -1;
2557}
2558
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002559/* This is a variant of batch_list() above, specialized for lists (with no
2560 * support for list subclasses). Like batch_list(), we batch up chunks of
2561 * MARK item item ... item APPENDS
2562 * opcode sequences. Calling code should have arranged to first create an
2563 * empty list, or list-like object, for the APPENDS to operate on.
2564 * Returns 0 on success, -1 on error.
2565 *
2566 * This version is considerably faster than batch_list(), if less general.
2567 *
2568 * Note that this only works for protocols > 0.
2569 */
2570static int
2571batch_list_exact(PicklerObject *self, PyObject *obj)
2572{
2573 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002574 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002575
2576 const char append_op = APPEND;
2577 const char appends_op = APPENDS;
2578 const char mark_op = MARK;
2579
2580 assert(obj != NULL);
2581 assert(self->proto > 0);
2582 assert(PyList_CheckExact(obj));
2583
2584 if (PyList_GET_SIZE(obj) == 1) {
2585 item = PyList_GET_ITEM(obj, 0);
2586 if (save(self, item, 0) < 0)
2587 return -1;
2588 if (_Pickler_Write(self, &append_op, 1) < 0)
2589 return -1;
2590 return 0;
2591 }
2592
2593 /* Write in batches of BATCHSIZE. */
2594 total = 0;
2595 do {
2596 this_batch = 0;
2597 if (_Pickler_Write(self, &mark_op, 1) < 0)
2598 return -1;
2599 while (total < PyList_GET_SIZE(obj)) {
2600 item = PyList_GET_ITEM(obj, total);
2601 if (save(self, item, 0) < 0)
2602 return -1;
2603 total++;
2604 if (++this_batch == BATCHSIZE)
2605 break;
2606 }
2607 if (_Pickler_Write(self, &appends_op, 1) < 0)
2608 return -1;
2609
2610 } while (total < PyList_GET_SIZE(obj));
2611
2612 return 0;
2613}
2614
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002615static int
2616save_list(PicklerObject *self, PyObject *obj)
2617{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002618 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002619 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002620 int status = 0;
2621
2622 if (self->fast && !fast_save_enter(self, obj))
2623 goto error;
2624
2625 /* Create an empty list. */
2626 if (self->bin) {
2627 header[0] = EMPTY_LIST;
2628 len = 1;
2629 }
2630 else {
2631 header[0] = MARK;
2632 header[1] = LIST;
2633 len = 2;
2634 }
2635
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002636 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002637 goto error;
2638
2639 /* Get list length, and bow out early if empty. */
2640 if ((len = PyList_Size(obj)) < 0)
2641 goto error;
2642
2643 if (memo_put(self, obj) < 0)
2644 goto error;
2645
2646 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002647 /* Materialize the list elements. */
2648 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002649 if (Py_EnterRecursiveCall(" while pickling an object"))
2650 goto error;
2651 status = batch_list_exact(self, obj);
2652 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002653 } else {
2654 PyObject *iter = PyObject_GetIter(obj);
2655 if (iter == NULL)
2656 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002657
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002658 if (Py_EnterRecursiveCall(" while pickling an object")) {
2659 Py_DECREF(iter);
2660 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002661 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002662 status = batch_list(self, iter);
2663 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002664 Py_DECREF(iter);
2665 }
2666 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002667 if (0) {
2668 error:
2669 status = -1;
2670 }
2671
2672 if (self->fast && !fast_save_leave(self, obj))
2673 status = -1;
2674
2675 return status;
2676}
2677
2678/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2679 * MARK key value ... key value SETITEMS
2680 * opcode sequences. Calling code should have arranged to first create an
2681 * empty dict, or dict-like object, for the SETITEMS to operate on.
2682 * Returns 0 on success, <0 on error.
2683 *
2684 * This is very much like batch_list(). The difference between saving
2685 * elements directly, and picking apart two-tuples, is so long-winded at
2686 * the C level, though, that attempts to combine these routines were too
2687 * ugly to bear.
2688 */
2689static int
2690batch_dict(PicklerObject *self, PyObject *iter)
2691{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002692 PyObject *obj = NULL;
2693 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002694 int i, n;
2695
2696 const char mark_op = MARK;
2697 const char setitem_op = SETITEM;
2698 const char setitems_op = SETITEMS;
2699
2700 assert(iter != NULL);
2701
2702 if (self->proto == 0) {
2703 /* SETITEMS isn't available; do one at a time. */
2704 for (;;) {
2705 obj = PyIter_Next(iter);
2706 if (obj == NULL) {
2707 if (PyErr_Occurred())
2708 return -1;
2709 break;
2710 }
2711 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2712 PyErr_SetString(PyExc_TypeError, "dict items "
2713 "iterator must return 2-tuples");
2714 return -1;
2715 }
2716 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2717 if (i >= 0)
2718 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2719 Py_DECREF(obj);
2720 if (i < 0)
2721 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002722 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002723 return -1;
2724 }
2725 return 0;
2726 }
2727
2728 /* proto > 0: write in batches of BATCHSIZE. */
2729 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002730 /* Get first item */
2731 firstitem = PyIter_Next(iter);
2732 if (firstitem == NULL) {
2733 if (PyErr_Occurred())
2734 goto error;
2735
2736 /* nothing more to add */
2737 break;
2738 }
2739 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2740 PyErr_SetString(PyExc_TypeError, "dict items "
2741 "iterator must return 2-tuples");
2742 goto error;
2743 }
2744
2745 /* Try to get a second item */
2746 obj = PyIter_Next(iter);
2747 if (obj == NULL) {
2748 if (PyErr_Occurred())
2749 goto error;
2750
2751 /* Only one item to write */
2752 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2753 goto error;
2754 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2755 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002756 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002757 goto error;
2758 Py_CLEAR(firstitem);
2759 break;
2760 }
2761
2762 /* More than one item to write */
2763
2764 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002765 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002766 goto error;
2767
2768 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2769 goto error;
2770 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2771 goto error;
2772 Py_CLEAR(firstitem);
2773 n = 1;
2774
2775 /* Fetch and save up to BATCHSIZE items */
2776 while (obj) {
2777 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2778 PyErr_SetString(PyExc_TypeError, "dict items "
2779 "iterator must return 2-tuples");
2780 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002781 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002782 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2783 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2784 goto error;
2785 Py_CLEAR(obj);
2786 n += 1;
2787
2788 if (n == BATCHSIZE)
2789 break;
2790
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002791 obj = PyIter_Next(iter);
2792 if (obj == NULL) {
2793 if (PyErr_Occurred())
2794 goto error;
2795 break;
2796 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002797 }
2798
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002799 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002800 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002801
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002802 } while (n == BATCHSIZE);
2803 return 0;
2804
2805 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002806 Py_XDECREF(firstitem);
2807 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002808 return -1;
2809}
2810
Collin Winter5c9b02d2009-05-25 05:43:30 +00002811/* This is a variant of batch_dict() above that specializes for dicts, with no
2812 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2813 * MARK key value ... key value SETITEMS
2814 * opcode sequences. Calling code should have arranged to first create an
2815 * empty dict, or dict-like object, for the SETITEMS to operate on.
2816 * Returns 0 on success, -1 on error.
2817 *
2818 * Note that this currently doesn't work for protocol 0.
2819 */
2820static int
2821batch_dict_exact(PicklerObject *self, PyObject *obj)
2822{
2823 PyObject *key = NULL, *value = NULL;
2824 int i;
2825 Py_ssize_t dict_size, ppos = 0;
2826
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002827 const char mark_op = MARK;
2828 const char setitem_op = SETITEM;
2829 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002830
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002831 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002832 assert(self->proto > 0);
2833
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002834 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002835
2836 /* Special-case len(d) == 1 to save space. */
2837 if (dict_size == 1) {
2838 PyDict_Next(obj, &ppos, &key, &value);
2839 if (save(self, key, 0) < 0)
2840 return -1;
2841 if (save(self, value, 0) < 0)
2842 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002843 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002844 return -1;
2845 return 0;
2846 }
2847
2848 /* Write in batches of BATCHSIZE. */
2849 do {
2850 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002851 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002852 return -1;
2853 while (PyDict_Next(obj, &ppos, &key, &value)) {
2854 if (save(self, key, 0) < 0)
2855 return -1;
2856 if (save(self, value, 0) < 0)
2857 return -1;
2858 if (++i == BATCHSIZE)
2859 break;
2860 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002861 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002862 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002863 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00002864 PyErr_Format(
2865 PyExc_RuntimeError,
2866 "dictionary changed size during iteration");
2867 return -1;
2868 }
2869
2870 } while (i == BATCHSIZE);
2871 return 0;
2872}
2873
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002874static int
2875save_dict(PicklerObject *self, PyObject *obj)
2876{
2877 PyObject *items, *iter;
2878 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002879 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002880 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002881 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002882
2883 if (self->fast && !fast_save_enter(self, obj))
2884 goto error;
2885
2886 /* Create an empty dict. */
2887 if (self->bin) {
2888 header[0] = EMPTY_DICT;
2889 len = 1;
2890 }
2891 else {
2892 header[0] = MARK;
2893 header[1] = DICT;
2894 len = 2;
2895 }
2896
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002897 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002898 goto error;
2899
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002900 if (memo_put(self, obj) < 0)
2901 goto error;
2902
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002903 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002904 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002905 if (PyDict_CheckExact(obj) && self->proto > 0) {
2906 /* We can take certain shortcuts if we know this is a dict and
2907 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002908 if (Py_EnterRecursiveCall(" while pickling an object"))
2909 goto error;
2910 status = batch_dict_exact(self, obj);
2911 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002912 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002913 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002914
Victor Stinnerad8c83a2016-09-05 17:53:15 -07002915 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002916 if (items == NULL)
2917 goto error;
2918 iter = PyObject_GetIter(items);
2919 Py_DECREF(items);
2920 if (iter == NULL)
2921 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002922 if (Py_EnterRecursiveCall(" while pickling an object")) {
2923 Py_DECREF(iter);
2924 goto error;
2925 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002926 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002927 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002928 Py_DECREF(iter);
2929 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002930 }
2931
2932 if (0) {
2933 error:
2934 status = -1;
2935 }
2936
2937 if (self->fast && !fast_save_leave(self, obj))
2938 status = -1;
2939
2940 return status;
2941}
2942
2943static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002944save_set(PicklerObject *self, PyObject *obj)
2945{
2946 PyObject *item;
2947 int i;
2948 Py_ssize_t set_size, ppos = 0;
2949 Py_hash_t hash;
2950
2951 const char empty_set_op = EMPTY_SET;
2952 const char mark_op = MARK;
2953 const char additems_op = ADDITEMS;
2954
2955 if (self->proto < 4) {
2956 PyObject *items;
2957 PyObject *reduce_value;
2958 int status;
2959
2960 items = PySequence_List(obj);
2961 if (items == NULL) {
2962 return -1;
2963 }
2964 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2965 Py_DECREF(items);
2966 if (reduce_value == NULL) {
2967 return -1;
2968 }
2969 /* save_reduce() will memoize the object automatically. */
2970 status = save_reduce(self, reduce_value, obj);
2971 Py_DECREF(reduce_value);
2972 return status;
2973 }
2974
2975 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2976 return -1;
2977
2978 if (memo_put(self, obj) < 0)
2979 return -1;
2980
2981 set_size = PySet_GET_SIZE(obj);
2982 if (set_size == 0)
2983 return 0; /* nothing to do */
2984
2985 /* Write in batches of BATCHSIZE. */
2986 do {
2987 i = 0;
2988 if (_Pickler_Write(self, &mark_op, 1) < 0)
2989 return -1;
2990 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2991 if (save(self, item, 0) < 0)
2992 return -1;
2993 if (++i == BATCHSIZE)
2994 break;
2995 }
2996 if (_Pickler_Write(self, &additems_op, 1) < 0)
2997 return -1;
2998 if (PySet_GET_SIZE(obj) != set_size) {
2999 PyErr_Format(
3000 PyExc_RuntimeError,
3001 "set changed size during iteration");
3002 return -1;
3003 }
3004 } while (i == BATCHSIZE);
3005
3006 return 0;
3007}
3008
3009static int
3010save_frozenset(PicklerObject *self, PyObject *obj)
3011{
3012 PyObject *iter;
3013
3014 const char mark_op = MARK;
3015 const char frozenset_op = FROZENSET;
3016
3017 if (self->fast && !fast_save_enter(self, obj))
3018 return -1;
3019
3020 if (self->proto < 4) {
3021 PyObject *items;
3022 PyObject *reduce_value;
3023 int status;
3024
3025 items = PySequence_List(obj);
3026 if (items == NULL) {
3027 return -1;
3028 }
3029 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3030 items);
3031 Py_DECREF(items);
3032 if (reduce_value == NULL) {
3033 return -1;
3034 }
3035 /* save_reduce() will memoize the object automatically. */
3036 status = save_reduce(self, reduce_value, obj);
3037 Py_DECREF(reduce_value);
3038 return status;
3039 }
3040
3041 if (_Pickler_Write(self, &mark_op, 1) < 0)
3042 return -1;
3043
3044 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003045 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003046 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003047 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003048 for (;;) {
3049 PyObject *item;
3050
3051 item = PyIter_Next(iter);
3052 if (item == NULL) {
3053 if (PyErr_Occurred()) {
3054 Py_DECREF(iter);
3055 return -1;
3056 }
3057 break;
3058 }
3059 if (save(self, item, 0) < 0) {
3060 Py_DECREF(item);
3061 Py_DECREF(iter);
3062 return -1;
3063 }
3064 Py_DECREF(item);
3065 }
3066 Py_DECREF(iter);
3067
3068 /* If the object is already in the memo, this means it is
3069 recursive. In this case, throw away everything we put on the
3070 stack, and fetch the object back from the memo. */
3071 if (PyMemoTable_Get(self->memo, obj)) {
3072 const char pop_mark_op = POP_MARK;
3073
3074 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3075 return -1;
3076 if (memo_get(self, obj) < 0)
3077 return -1;
3078 return 0;
3079 }
3080
3081 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3082 return -1;
3083 if (memo_put(self, obj) < 0)
3084 return -1;
3085
3086 return 0;
3087}
3088
3089static int
3090fix_imports(PyObject **module_name, PyObject **global_name)
3091{
3092 PyObject *key;
3093 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003094 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003095
3096 key = PyTuple_Pack(2, *module_name, *global_name);
3097 if (key == NULL)
3098 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003099 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003100 Py_DECREF(key);
3101 if (item) {
3102 PyObject *fixed_module_name;
3103 PyObject *fixed_global_name;
3104
3105 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3106 PyErr_Format(PyExc_RuntimeError,
3107 "_compat_pickle.REVERSE_NAME_MAPPING values "
3108 "should be 2-tuples, not %.200s",
3109 Py_TYPE(item)->tp_name);
3110 return -1;
3111 }
3112 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3113 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3114 if (!PyUnicode_Check(fixed_module_name) ||
3115 !PyUnicode_Check(fixed_global_name)) {
3116 PyErr_Format(PyExc_RuntimeError,
3117 "_compat_pickle.REVERSE_NAME_MAPPING values "
3118 "should be pairs of str, not (%.200s, %.200s)",
3119 Py_TYPE(fixed_module_name)->tp_name,
3120 Py_TYPE(fixed_global_name)->tp_name);
3121 return -1;
3122 }
3123
3124 Py_CLEAR(*module_name);
3125 Py_CLEAR(*global_name);
3126 Py_INCREF(fixed_module_name);
3127 Py_INCREF(fixed_global_name);
3128 *module_name = fixed_module_name;
3129 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003130 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003131 }
3132 else if (PyErr_Occurred()) {
3133 return -1;
3134 }
3135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003136 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003137 if (item) {
3138 if (!PyUnicode_Check(item)) {
3139 PyErr_Format(PyExc_RuntimeError,
3140 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3141 "should be strings, not %.200s",
3142 Py_TYPE(item)->tp_name);
3143 return -1;
3144 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003145 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003146 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003147 }
3148 else if (PyErr_Occurred()) {
3149 return -1;
3150 }
3151
3152 return 0;
3153}
3154
3155static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003156save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3157{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003158 PyObject *global_name = NULL;
3159 PyObject *module_name = NULL;
3160 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003161 PyObject *parent = NULL;
3162 PyObject *dotted_path = NULL;
3163 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003164 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003165 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003166 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003167 _Py_IDENTIFIER(__name__);
3168 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003169
3170 const char global_op = GLOBAL;
3171
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003172 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003173 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003174 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003175 }
3176 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003177 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3178 if (global_name == NULL) {
3179 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3180 goto error;
3181 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003182 }
3183 if (global_name == NULL) {
3184 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3185 if (global_name == NULL)
3186 goto error;
3187 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003188 }
3189
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003190 dotted_path = get_dotted_path(module, global_name);
3191 if (dotted_path == NULL)
3192 goto error;
3193 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003194 if (module_name == NULL)
3195 goto error;
3196
3197 /* XXX: Change to use the import C API directly with level=0 to disallow
3198 relative imports.
3199
3200 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3201 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3202 custom import functions (IMHO, this would be a nice security
3203 feature). The import C API would need to be extended to support the
3204 extra parameters of __import__ to fix that. */
3205 module = PyImport_Import(module_name);
3206 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003207 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003208 "Can't pickle %R: import of module %R failed",
3209 obj, module_name);
3210 goto error;
3211 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003212 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3213 Py_INCREF(lastname);
3214 cls = get_deep_attribute(module, dotted_path, &parent);
3215 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003216 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003217 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003218 "Can't pickle %R: attribute lookup %S on %S failed",
3219 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003220 goto error;
3221 }
3222 if (cls != obj) {
3223 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003224 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003225 "Can't pickle %R: it's not the same object as %S.%S",
3226 obj, module_name, global_name);
3227 goto error;
3228 }
3229 Py_DECREF(cls);
3230
3231 if (self->proto >= 2) {
3232 /* See whether this is in the extension registry, and if
3233 * so generate an EXT opcode.
3234 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003235 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003236 PyObject *code_obj; /* extension code as Python object */
3237 long code; /* extension code as C value */
3238 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003239 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003240
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003241 extension_key = PyTuple_Pack(2, module_name, global_name);
3242 if (extension_key == NULL) {
3243 goto error;
3244 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003245 code_obj = PyDict_GetItemWithError(st->extension_registry,
3246 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003247 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003248 /* The object is not registered in the extension registry.
3249 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003250 if (code_obj == NULL) {
3251 if (PyErr_Occurred()) {
3252 goto error;
3253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003254 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003255 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003256
3257 /* XXX: pickle.py doesn't check neither the type, nor the range
3258 of the value returned by the extension_registry. It should for
3259 consistency. */
3260
3261 /* Verify code_obj has the right type and value. */
3262 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003263 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003264 "Can't pickle %R: extension code %R isn't an integer",
3265 obj, code_obj);
3266 goto error;
3267 }
3268 code = PyLong_AS_LONG(code_obj);
3269 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003270 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003271 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3272 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003273 goto error;
3274 }
3275
3276 /* Generate an EXT opcode. */
3277 if (code <= 0xff) {
3278 pdata[0] = EXT1;
3279 pdata[1] = (unsigned char)code;
3280 n = 2;
3281 }
3282 else if (code <= 0xffff) {
3283 pdata[0] = EXT2;
3284 pdata[1] = (unsigned char)(code & 0xff);
3285 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3286 n = 3;
3287 }
3288 else {
3289 pdata[0] = EXT4;
3290 pdata[1] = (unsigned char)(code & 0xff);
3291 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3292 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3293 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3294 n = 5;
3295 }
3296
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003297 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003298 goto error;
3299 }
3300 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003301 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003302 if (parent == module) {
3303 Py_INCREF(lastname);
3304 Py_DECREF(global_name);
3305 global_name = lastname;
3306 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003307 if (self->proto >= 4) {
3308 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003309
Christian Heimese8b1ba12013-11-23 21:13:39 +01003310 if (save(self, module_name, 0) < 0)
3311 goto error;
3312 if (save(self, global_name, 0) < 0)
3313 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003314
3315 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3316 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003317 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003318 else if (parent != module) {
3319 PickleState *st = _Pickle_GetGlobalState();
3320 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3321 st->getattr, parent, lastname);
3322 status = save_reduce(self, reduce_value, NULL);
3323 Py_DECREF(reduce_value);
3324 if (status < 0)
3325 goto error;
3326 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003327 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003328 /* Generate a normal global opcode if we are using a pickle
3329 protocol < 4, or if the object is not registered in the
3330 extension registry. */
3331 PyObject *encoded;
3332 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003333
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003334 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003335 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003336
3337 /* For protocol < 3 and if the user didn't request against doing
3338 so, we convert module names to the old 2.x module names. */
3339 if (self->proto < 3 && self->fix_imports) {
3340 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003341 goto error;
3342 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003343 }
3344
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003345 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3346 both the module name and the global name using UTF-8. We do so
3347 only when we are using the pickle protocol newer than version
3348 3. This is to ensure compatibility with older Unpickler running
3349 on Python 2.x. */
3350 if (self->proto == 3) {
3351 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003352 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003353 else {
3354 unicode_encoder = PyUnicode_AsASCIIString;
3355 }
3356 encoded = unicode_encoder(module_name);
3357 if (encoded == NULL) {
3358 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003359 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003360 "can't pickle module identifier '%S' using "
3361 "pickle protocol %i",
3362 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003363 goto error;
3364 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003365 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3366 PyBytes_GET_SIZE(encoded)) < 0) {
3367 Py_DECREF(encoded);
3368 goto error;
3369 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003370 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003371 if(_Pickler_Write(self, "\n", 1) < 0)
3372 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003373
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003374 /* Save the name of the module. */
3375 encoded = unicode_encoder(global_name);
3376 if (encoded == NULL) {
3377 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003378 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003379 "can't pickle global identifier '%S' using "
3380 "pickle protocol %i",
3381 global_name, self->proto);
3382 goto error;
3383 }
3384 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3385 PyBytes_GET_SIZE(encoded)) < 0) {
3386 Py_DECREF(encoded);
3387 goto error;
3388 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003389 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003390 if (_Pickler_Write(self, "\n", 1) < 0)
3391 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003392 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003393 /* Memoize the object. */
3394 if (memo_put(self, obj) < 0)
3395 goto error;
3396 }
3397
3398 if (0) {
3399 error:
3400 status = -1;
3401 }
3402 Py_XDECREF(module_name);
3403 Py_XDECREF(global_name);
3404 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003405 Py_XDECREF(parent);
3406 Py_XDECREF(dotted_path);
3407 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003408
3409 return status;
3410}
3411
3412static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003413save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3414{
3415 PyObject *reduce_value;
3416 int status;
3417
3418 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3419 if (reduce_value == NULL) {
3420 return -1;
3421 }
3422 status = save_reduce(self, reduce_value, obj);
3423 Py_DECREF(reduce_value);
3424 return status;
3425}
3426
3427static int
3428save_type(PicklerObject *self, PyObject *obj)
3429{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003430 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003431 return save_singleton_type(self, obj, Py_None);
3432 }
3433 else if (obj == (PyObject *)&PyEllipsis_Type) {
3434 return save_singleton_type(self, obj, Py_Ellipsis);
3435 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003436 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003437 return save_singleton_type(self, obj, Py_NotImplemented);
3438 }
3439 return save_global(self, obj, NULL);
3440}
3441
3442static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003443save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3444{
3445 PyObject *pid = NULL;
3446 int status = 0;
3447
3448 const char persid_op = PERSID;
3449 const char binpersid_op = BINPERSID;
3450
3451 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003452 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003453 if (pid == NULL)
3454 return -1;
3455
3456 if (pid != Py_None) {
3457 if (self->bin) {
3458 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003459 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003460 goto error;
3461 }
3462 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003463 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003464
3465 pid_str = PyObject_Str(pid);
3466 if (pid_str == NULL)
3467 goto error;
3468
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003469 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003470 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003471 if (!PyUnicode_IS_ASCII(pid_str)) {
3472 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3473 "persistent IDs in protocol 0 must be "
3474 "ASCII strings");
3475 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003476 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003477 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003478
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003479 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003480 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3481 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3482 _Pickler_Write(self, "\n", 1) < 0) {
3483 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003484 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003485 }
3486 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003487 }
3488 status = 1;
3489 }
3490
3491 if (0) {
3492 error:
3493 status = -1;
3494 }
3495 Py_XDECREF(pid);
3496
3497 return status;
3498}
3499
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003500static PyObject *
3501get_class(PyObject *obj)
3502{
3503 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003504 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003505
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003506 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003507 if (cls == NULL) {
3508 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3509 PyErr_Clear();
3510 cls = (PyObject *) Py_TYPE(obj);
3511 Py_INCREF(cls);
3512 }
3513 }
3514 return cls;
3515}
3516
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003517/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3518 * appropriate __reduce__ method for obj.
3519 */
3520static int
3521save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3522{
3523 PyObject *callable;
3524 PyObject *argtup;
3525 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003526 PyObject *listitems = Py_None;
3527 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003528 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003529 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003530 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003531
3532 const char reduce_op = REDUCE;
3533 const char build_op = BUILD;
3534 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003535 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003536
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003537 size = PyTuple_Size(args);
3538 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003539 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003540 "__reduce__ must contain 2 through 5 elements");
3541 return -1;
3542 }
3543
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003544 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3545 &callable, &argtup, &state, &listitems, &dictitems))
3546 return -1;
3547
3548 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003549 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003550 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003551 return -1;
3552 }
3553 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003554 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003555 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003556 return -1;
3557 }
3558
3559 if (state == Py_None)
3560 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003561
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003562 if (listitems == Py_None)
3563 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003564 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003565 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003566 "returned by __reduce__ must be an iterator, not %s",
3567 Py_TYPE(listitems)->tp_name);
3568 return -1;
3569 }
3570
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003571 if (dictitems == Py_None)
3572 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003573 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003574 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003575 "returned by __reduce__ must be an iterator, not %s",
3576 Py_TYPE(dictitems)->tp_name);
3577 return -1;
3578 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003579
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003580 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003581 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003582 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003583
Victor Stinner804e05e2013-11-14 01:26:17 +01003584 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003585 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003586 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003587 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003588 }
3589 PyErr_Clear();
3590 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003591 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003592 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003593 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3594 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003595 if (!use_newobj_ex) {
3596 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003597 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003598 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003599 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003600 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003601 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003602
3603 if (use_newobj_ex) {
3604 PyObject *cls;
3605 PyObject *args;
3606 PyObject *kwargs;
3607
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003608 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003609 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003610 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003611 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003612 return -1;
3613 }
3614
3615 cls = PyTuple_GET_ITEM(argtup, 0);
3616 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003617 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003618 "first item from NEWOBJ_EX argument tuple must "
3619 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3620 return -1;
3621 }
3622 args = PyTuple_GET_ITEM(argtup, 1);
3623 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003624 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003625 "second item from NEWOBJ_EX argument tuple must "
3626 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3627 return -1;
3628 }
3629 kwargs = PyTuple_GET_ITEM(argtup, 2);
3630 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003631 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003632 "third item from NEWOBJ_EX argument tuple must "
3633 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3634 return -1;
3635 }
3636
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003637 if (self->proto >= 4) {
3638 if (save(self, cls, 0) < 0 ||
3639 save(self, args, 0) < 0 ||
3640 save(self, kwargs, 0) < 0 ||
3641 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3642 return -1;
3643 }
3644 }
3645 else {
3646 PyObject *newargs;
3647 PyObject *cls_new;
3648 Py_ssize_t i;
3649 _Py_IDENTIFIER(__new__);
3650
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003651 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003652 if (newargs == NULL)
3653 return -1;
3654
3655 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3656 if (cls_new == NULL) {
3657 Py_DECREF(newargs);
3658 return -1;
3659 }
3660 PyTuple_SET_ITEM(newargs, 0, cls_new);
3661 Py_INCREF(cls);
3662 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003663 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003664 PyObject *item = PyTuple_GET_ITEM(args, i);
3665 Py_INCREF(item);
3666 PyTuple_SET_ITEM(newargs, i + 2, item);
3667 }
3668
3669 callable = PyObject_Call(st->partial, newargs, kwargs);
3670 Py_DECREF(newargs);
3671 if (callable == NULL)
3672 return -1;
3673
3674 newargs = PyTuple_New(0);
3675 if (newargs == NULL) {
3676 Py_DECREF(callable);
3677 return -1;
3678 }
3679
3680 if (save(self, callable, 0) < 0 ||
3681 save(self, newargs, 0) < 0 ||
3682 _Pickler_Write(self, &reduce_op, 1) < 0) {
3683 Py_DECREF(newargs);
3684 Py_DECREF(callable);
3685 return -1;
3686 }
3687 Py_DECREF(newargs);
3688 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003689 }
3690 }
3691 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003692 PyObject *cls;
3693 PyObject *newargtup;
3694 PyObject *obj_class;
3695 int p;
3696
3697 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003698 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003699 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003700 return -1;
3701 }
3702
3703 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003704 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003705 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003706 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003707 return -1;
3708 }
3709
3710 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003711 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003712 p = obj_class != cls; /* true iff a problem */
3713 Py_DECREF(obj_class);
3714 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003715 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003716 "__newobj__ args has the wrong class");
3717 return -1;
3718 }
3719 }
3720 /* XXX: These calls save() are prone to infinite recursion. Imagine
3721 what happen if the value returned by the __reduce__() method of
3722 some extension type contains another object of the same type. Ouch!
3723
3724 Here is a quick example, that I ran into, to illustrate what I
3725 mean:
3726
3727 >>> import pickle, copyreg
3728 >>> copyreg.dispatch_table.pop(complex)
3729 >>> pickle.dumps(1+2j)
3730 Traceback (most recent call last):
3731 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003732 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003733
3734 Removing the complex class from copyreg.dispatch_table made the
3735 __reduce_ex__() method emit another complex object:
3736
3737 >>> (1+1j).__reduce_ex__(2)
3738 (<function __newobj__ at 0xb7b71c3c>,
3739 (<class 'complex'>, (1+1j)), None, None, None)
3740
3741 Thus when save() was called on newargstup (the 2nd item) recursion
3742 ensued. Of course, the bug was in the complex class which had a
3743 broken __getnewargs__() that emitted another complex object. But,
3744 the point, here, is it is quite easy to end up with a broken reduce
3745 function. */
3746
3747 /* Save the class and its __new__ arguments. */
3748 if (save(self, cls, 0) < 0)
3749 return -1;
3750
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003751 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003752 if (newargtup == NULL)
3753 return -1;
3754
3755 p = save(self, newargtup, 0);
3756 Py_DECREF(newargtup);
3757 if (p < 0)
3758 return -1;
3759
3760 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003761 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003762 return -1;
3763 }
3764 else { /* Not using NEWOBJ. */
3765 if (save(self, callable, 0) < 0 ||
3766 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003767 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003768 return -1;
3769 }
3770
3771 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3772 the caller do not want to memoize the object. Not particularly useful,
3773 but that is to mimic the behavior save_reduce() in pickle.py when
3774 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003775 if (obj != NULL) {
3776 /* If the object is already in the memo, this means it is
3777 recursive. In this case, throw away everything we put on the
3778 stack, and fetch the object back from the memo. */
3779 if (PyMemoTable_Get(self->memo, obj)) {
3780 const char pop_op = POP;
3781
3782 if (_Pickler_Write(self, &pop_op, 1) < 0)
3783 return -1;
3784 if (memo_get(self, obj) < 0)
3785 return -1;
3786
3787 return 0;
3788 }
3789 else if (memo_put(self, obj) < 0)
3790 return -1;
3791 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003792
3793 if (listitems && batch_list(self, listitems) < 0)
3794 return -1;
3795
3796 if (dictitems && batch_dict(self, dictitems) < 0)
3797 return -1;
3798
3799 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003800 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003801 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802 return -1;
3803 }
3804
3805 return 0;
3806}
3807
3808static int
3809save(PicklerObject *self, PyObject *obj, int pers_save)
3810{
3811 PyTypeObject *type;
3812 PyObject *reduce_func = NULL;
3813 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003814 int status = 0;
3815
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003816 if (_Pickler_OpcodeBoundary(self) < 0)
3817 return -1;
3818
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003819 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003820 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003821
3822 /* The extra pers_save argument is necessary to avoid calling save_pers()
3823 on its returned object. */
3824 if (!pers_save && self->pers_func) {
3825 /* save_pers() returns:
3826 -1 to signal an error;
3827 0 if it did nothing successfully;
3828 1 if a persistent id was saved.
3829 */
3830 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3831 goto done;
3832 }
3833
3834 type = Py_TYPE(obj);
3835
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003836 /* The old cPickle had an optimization that used switch-case statement
3837 dispatching on the first letter of the type name. This has was removed
3838 since benchmarks shown that this optimization was actually slowing
3839 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003840
3841 /* Atom types; these aren't memoized, so don't check the memo. */
3842
3843 if (obj == Py_None) {
3844 status = save_none(self, obj);
3845 goto done;
3846 }
3847 else if (obj == Py_False || obj == Py_True) {
3848 status = save_bool(self, obj);
3849 goto done;
3850 }
3851 else if (type == &PyLong_Type) {
3852 status = save_long(self, obj);
3853 goto done;
3854 }
3855 else if (type == &PyFloat_Type) {
3856 status = save_float(self, obj);
3857 goto done;
3858 }
3859
3860 /* Check the memo to see if it has the object. If so, generate
3861 a GET (or BINGET) opcode, instead of pickling the object
3862 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003863 if (PyMemoTable_Get(self->memo, obj)) {
3864 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003865 goto error;
3866 goto done;
3867 }
3868
3869 if (type == &PyBytes_Type) {
3870 status = save_bytes(self, obj);
3871 goto done;
3872 }
3873 else if (type == &PyUnicode_Type) {
3874 status = save_unicode(self, obj);
3875 goto done;
3876 }
3877 else if (type == &PyDict_Type) {
3878 status = save_dict(self, obj);
3879 goto done;
3880 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003881 else if (type == &PySet_Type) {
3882 status = save_set(self, obj);
3883 goto done;
3884 }
3885 else if (type == &PyFrozenSet_Type) {
3886 status = save_frozenset(self, obj);
3887 goto done;
3888 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003889 else if (type == &PyList_Type) {
3890 status = save_list(self, obj);
3891 goto done;
3892 }
3893 else if (type == &PyTuple_Type) {
3894 status = save_tuple(self, obj);
3895 goto done;
3896 }
3897 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003898 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 goto done;
3900 }
3901 else if (type == &PyFunction_Type) {
3902 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003903 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003904 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003905
3906 /* XXX: This part needs some unit tests. */
3907
3908 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003909 * self.dispatch_table, copyreg.dispatch_table, the object's
3910 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003911 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003912 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003913 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003914 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3915 (PyObject *)type);
3916 if (reduce_func == NULL) {
3917 if (PyErr_Occurred()) {
3918 goto error;
3919 }
3920 } else {
3921 /* PyDict_GetItemWithError() returns a borrowed reference.
3922 Increase the reference count to be consistent with
3923 PyObject_GetItem and _PyObject_GetAttrId used below. */
3924 Py_INCREF(reduce_func);
3925 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003926 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003927 reduce_func = PyObject_GetItem(self->dispatch_table,
3928 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003929 if (reduce_func == NULL) {
3930 if (PyErr_ExceptionMatches(PyExc_KeyError))
3931 PyErr_Clear();
3932 else
3933 goto error;
3934 }
3935 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003936 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003937 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003938 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003939 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003940 else if (PyType_IsSubtype(type, &PyType_Type)) {
3941 status = save_global(self, obj, NULL);
3942 goto done;
3943 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003944 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003945 _Py_IDENTIFIER(__reduce__);
3946 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003947
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003948
3949 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3950 automatically defined as __reduce__. While this is convenient, this
3951 make it impossible to know which method was actually called. Of
3952 course, this is not a big deal. But still, it would be nice to let
3953 the user know which method was called when something go
3954 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3955 don't actually have to check for a __reduce__ method. */
3956
3957 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003958 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003959 if (reduce_func != NULL) {
3960 PyObject *proto;
3961 proto = PyLong_FromLong(self->proto);
3962 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003963 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003964 }
3965 }
3966 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003967 PickleState *st = _Pickle_GetGlobalState();
3968
3969 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003970 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003971 }
3972 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003974 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003975 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003976 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003977 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02003978 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003979 }
3980 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003981 PyErr_Format(st->PicklingError,
3982 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003983 type->tp_name, obj);
3984 goto error;
3985 }
3986 }
3987 }
3988
3989 if (reduce_value == NULL)
3990 goto error;
3991
3992 if (PyUnicode_Check(reduce_value)) {
3993 status = save_global(self, obj, reduce_value);
3994 goto done;
3995 }
3996
3997 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003998 PickleState *st = _Pickle_GetGlobalState();
3999 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004000 "__reduce__ must return a string or tuple");
4001 goto error;
4002 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004003
4004 status = save_reduce(self, reduce_value, obj);
4005
4006 if (0) {
4007 error:
4008 status = -1;
4009 }
4010 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004011
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004012 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004013 Py_XDECREF(reduce_func);
4014 Py_XDECREF(reduce_value);
4015
4016 return status;
4017}
4018
4019static int
4020dump(PicklerObject *self, PyObject *obj)
4021{
4022 const char stop_op = STOP;
4023
4024 if (self->proto >= 2) {
4025 char header[2];
4026
4027 header[0] = PROTO;
4028 assert(self->proto >= 0 && self->proto < 256);
4029 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004030 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004032 if (self->proto >= 4)
4033 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004034 }
4035
4036 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004037 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004038 return -1;
4039
4040 return 0;
4041}
4042
Larry Hastings61272b72014-01-07 12:41:53 -08004043/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004044
4045_pickle.Pickler.clear_memo
4046
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004047Clears the pickler's "memo".
4048
4049The memo is the data structure that remembers which objects the
4050pickler has already seen, so that shared or recursive objects are
4051pickled by reference and not by value. This method is useful when
4052re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004053[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004054
Larry Hastings3cceb382014-01-04 11:09:09 -08004055static PyObject *
4056_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004057/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004058{
4059 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004060 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004061
4062 Py_RETURN_NONE;
4063}
4064
Larry Hastings61272b72014-01-07 12:41:53 -08004065/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004066
4067_pickle.Pickler.dump
4068
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004069 obj: object
4070 /
4071
4072Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004073[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004074
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004075static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004076_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004077/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004078{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004079 /* Check whether the Pickler was initialized correctly (issue3664).
4080 Developers often forget to call __init__() in their subclasses, which
4081 would trigger a segfault without this check. */
4082 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004083 PickleState *st = _Pickle_GetGlobalState();
4084 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004085 "Pickler.__init__() was not called by %s.__init__()",
4086 Py_TYPE(self)->tp_name);
4087 return NULL;
4088 }
4089
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004090 if (_Pickler_ClearBuffer(self) < 0)
4091 return NULL;
4092
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 if (dump(self, obj) < 0)
4094 return NULL;
4095
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004096 if (_Pickler_FlushToFile(self) < 0)
4097 return NULL;
4098
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099 Py_RETURN_NONE;
4100}
4101
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004102/*[clinic input]
4103
4104_pickle.Pickler.__sizeof__ -> Py_ssize_t
4105
4106Returns size in memory, in bytes.
4107[clinic start generated code]*/
4108
4109static Py_ssize_t
4110_pickle_Pickler___sizeof___impl(PicklerObject *self)
4111/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4112{
4113 Py_ssize_t res, s;
4114
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004115 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004116 if (self->memo != NULL) {
4117 res += sizeof(PyMemoTable);
4118 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4119 }
4120 if (self->output_buffer != NULL) {
4121 s = _PySys_GetSizeOf(self->output_buffer);
4122 if (s == -1)
4123 return -1;
4124 res += s;
4125 }
4126 return res;
4127}
4128
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004130 _PICKLE_PICKLER_DUMP_METHODDEF
4131 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004132 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004133 {NULL, NULL} /* sentinel */
4134};
4135
4136static void
4137Pickler_dealloc(PicklerObject *self)
4138{
4139 PyObject_GC_UnTrack(self);
4140
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004141 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004142 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004143 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004144 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004145 Py_XDECREF(self->fast_memo);
4146
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004147 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004148
4149 Py_TYPE(self)->tp_free((PyObject *)self);
4150}
4151
4152static int
4153Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4154{
4155 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004156 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004157 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004158 Py_VISIT(self->fast_memo);
4159 return 0;
4160}
4161
4162static int
4163Pickler_clear(PicklerObject *self)
4164{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004165 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004166 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004167 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004168 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004169 Py_CLEAR(self->fast_memo);
4170
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004171 if (self->memo != NULL) {
4172 PyMemoTable *memo = self->memo;
4173 self->memo = NULL;
4174 PyMemoTable_Del(memo);
4175 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004176 return 0;
4177}
4178
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004179
Larry Hastings61272b72014-01-07 12:41:53 -08004180/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004181
4182_pickle.Pickler.__init__
4183
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004184 file: object
4185 protocol: object = NULL
4186 fix_imports: bool = True
4187
4188This takes a binary file for writing a pickle data stream.
4189
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004190The optional *protocol* argument tells the pickler to use the given
4191protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4192protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004193
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004194Specifying a negative protocol version selects the highest protocol
4195version supported. The higher the protocol used, the more recent the
4196version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004197
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004198The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004199bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004200writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004201this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004202
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004203If *fix_imports* is True and protocol is less than 3, pickle will try
4204to map the new Python 3 names to the old module names used in Python
42052, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004206[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004207
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004208static int
Larry Hastings89964c42015-04-14 18:07:59 -04004209_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4210 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004211/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004212{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004213 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004214 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004215
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004216 /* In case of multiple __init__() calls, clear previous content. */
4217 if (self->write != NULL)
4218 (void)Pickler_clear(self);
4219
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004220 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004221 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004222
4223 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004224 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004225
4226 /* memo and output_buffer may have already been created in _Pickler_New */
4227 if (self->memo == NULL) {
4228 self->memo = PyMemoTable_New();
4229 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004230 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004231 }
4232 self->output_len = 0;
4233 if (self->output_buffer == NULL) {
4234 self->max_output_len = WRITE_BUF_SIZE;
4235 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4236 self->max_output_len);
4237 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004238 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004239 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004240
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004241 self->fast = 0;
4242 self->fast_nesting = 0;
4243 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004244
4245 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4246 &PyId_persistent_id);
4247 if (self->pers_func == NULL) {
4248 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004249 return -1;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004250 }
4251 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004252 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004253
4254 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4255 &PyId_dispatch_table);
4256 if (self->dispatch_table == NULL) {
4257 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004258 return -1;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004259 }
4260 PyErr_Clear();
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004261 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004262
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004263 return 0;
4264}
4265
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004266
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004267/* Define a proxy object for the Pickler's internal memo object. This is to
4268 * avoid breaking code like:
4269 * pickler.memo.clear()
4270 * and
4271 * pickler.memo = saved_memo
4272 * Is this a good idea? Not really, but we don't want to break code that uses
4273 * it. Note that we don't implement the entire mapping API here. This is
4274 * intentional, as these should be treated as black-box implementation details.
4275 */
4276
Larry Hastings61272b72014-01-07 12:41:53 -08004277/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004278_pickle.PicklerMemoProxy.clear
4279
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004280Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004281[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004282
Larry Hastings3cceb382014-01-04 11:09:09 -08004283static PyObject *
4284_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004285/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004286{
4287 if (self->pickler->memo)
4288 PyMemoTable_Clear(self->pickler->memo);
4289 Py_RETURN_NONE;
4290}
4291
Larry Hastings61272b72014-01-07 12:41:53 -08004292/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004293_pickle.PicklerMemoProxy.copy
4294
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004295Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004296[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004297
Larry Hastings3cceb382014-01-04 11:09:09 -08004298static PyObject *
4299_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004300/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004301{
4302 Py_ssize_t i;
4303 PyMemoTable *memo;
4304 PyObject *new_memo = PyDict_New();
4305 if (new_memo == NULL)
4306 return NULL;
4307
4308 memo = self->pickler->memo;
4309 for (i = 0; i < memo->mt_allocated; ++i) {
4310 PyMemoEntry entry = memo->mt_table[i];
4311 if (entry.me_key != NULL) {
4312 int status;
4313 PyObject *key, *value;
4314
4315 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004316 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004317
4318 if (key == NULL || value == NULL) {
4319 Py_XDECREF(key);
4320 Py_XDECREF(value);
4321 goto error;
4322 }
4323 status = PyDict_SetItem(new_memo, key, value);
4324 Py_DECREF(key);
4325 Py_DECREF(value);
4326 if (status < 0)
4327 goto error;
4328 }
4329 }
4330 return new_memo;
4331
4332 error:
4333 Py_XDECREF(new_memo);
4334 return NULL;
4335}
4336
Larry Hastings61272b72014-01-07 12:41:53 -08004337/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004338_pickle.PicklerMemoProxy.__reduce__
4339
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004340Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004341[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004342
Larry Hastings3cceb382014-01-04 11:09:09 -08004343static PyObject *
4344_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004345/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004346{
4347 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004348 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004349 if (contents == NULL)
4350 return NULL;
4351
4352 reduce_value = PyTuple_New(2);
4353 if (reduce_value == NULL) {
4354 Py_DECREF(contents);
4355 return NULL;
4356 }
4357 dict_args = PyTuple_New(1);
4358 if (dict_args == NULL) {
4359 Py_DECREF(contents);
4360 Py_DECREF(reduce_value);
4361 return NULL;
4362 }
4363 PyTuple_SET_ITEM(dict_args, 0, contents);
4364 Py_INCREF((PyObject *)&PyDict_Type);
4365 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4366 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4367 return reduce_value;
4368}
4369
4370static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004371 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4372 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4373 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004374 {NULL, NULL} /* sentinel */
4375};
4376
4377static void
4378PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4379{
4380 PyObject_GC_UnTrack(self);
4381 Py_XDECREF(self->pickler);
4382 PyObject_GC_Del((PyObject *)self);
4383}
4384
4385static int
4386PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4387 visitproc visit, void *arg)
4388{
4389 Py_VISIT(self->pickler);
4390 return 0;
4391}
4392
4393static int
4394PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4395{
4396 Py_CLEAR(self->pickler);
4397 return 0;
4398}
4399
4400static PyTypeObject PicklerMemoProxyType = {
4401 PyVarObject_HEAD_INIT(NULL, 0)
4402 "_pickle.PicklerMemoProxy", /*tp_name*/
4403 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4404 0,
4405 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4406 0, /* tp_print */
4407 0, /* tp_getattr */
4408 0, /* tp_setattr */
4409 0, /* tp_compare */
4410 0, /* tp_repr */
4411 0, /* tp_as_number */
4412 0, /* tp_as_sequence */
4413 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004414 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004415 0, /* tp_call */
4416 0, /* tp_str */
4417 PyObject_GenericGetAttr, /* tp_getattro */
4418 PyObject_GenericSetAttr, /* tp_setattro */
4419 0, /* tp_as_buffer */
4420 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4421 0, /* tp_doc */
4422 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4423 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4424 0, /* tp_richcompare */
4425 0, /* tp_weaklistoffset */
4426 0, /* tp_iter */
4427 0, /* tp_iternext */
4428 picklerproxy_methods, /* tp_methods */
4429};
4430
4431static PyObject *
4432PicklerMemoProxy_New(PicklerObject *pickler)
4433{
4434 PicklerMemoProxyObject *self;
4435
4436 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4437 if (self == NULL)
4438 return NULL;
4439 Py_INCREF(pickler);
4440 self->pickler = pickler;
4441 PyObject_GC_Track(self);
4442 return (PyObject *)self;
4443}
4444
4445/*****************************************************************************/
4446
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004447static PyObject *
4448Pickler_get_memo(PicklerObject *self)
4449{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004450 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004451}
4452
4453static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004454Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004455{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004456 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004457
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004458 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004459 PyErr_SetString(PyExc_TypeError,
4460 "attribute deletion is not supported");
4461 return -1;
4462 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004463
4464 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4465 PicklerObject *pickler =
4466 ((PicklerMemoProxyObject *)obj)->pickler;
4467
4468 new_memo = PyMemoTable_Copy(pickler->memo);
4469 if (new_memo == NULL)
4470 return -1;
4471 }
4472 else if (PyDict_Check(obj)) {
4473 Py_ssize_t i = 0;
4474 PyObject *key, *value;
4475
4476 new_memo = PyMemoTable_New();
4477 if (new_memo == NULL)
4478 return -1;
4479
4480 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004481 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004482 PyObject *memo_obj;
4483
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004484 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004485 PyErr_SetString(PyExc_TypeError,
4486 "'memo' values must be 2-item tuples");
4487 goto error;
4488 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004489 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004490 if (memo_id == -1 && PyErr_Occurred())
4491 goto error;
4492 memo_obj = PyTuple_GET_ITEM(value, 1);
4493 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4494 goto error;
4495 }
4496 }
4497 else {
4498 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004499 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004500 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004501 return -1;
4502 }
4503
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004504 PyMemoTable_Del(self->memo);
4505 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004506
4507 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004508
4509 error:
4510 if (new_memo)
4511 PyMemoTable_Del(new_memo);
4512 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004513}
4514
4515static PyObject *
4516Pickler_get_persid(PicklerObject *self)
4517{
4518 if (self->pers_func == NULL)
4519 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4520 else
4521 Py_INCREF(self->pers_func);
4522 return self->pers_func;
4523}
4524
4525static int
4526Pickler_set_persid(PicklerObject *self, PyObject *value)
4527{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004528 if (value == NULL) {
4529 PyErr_SetString(PyExc_TypeError,
4530 "attribute deletion is not supported");
4531 return -1;
4532 }
4533 if (!PyCallable_Check(value)) {
4534 PyErr_SetString(PyExc_TypeError,
4535 "persistent_id must be a callable taking one argument");
4536 return -1;
4537 }
4538
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004539 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004540 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004541
4542 return 0;
4543}
4544
4545static PyMemberDef Pickler_members[] = {
4546 {"bin", T_INT, offsetof(PicklerObject, bin)},
4547 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004548 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004549 {NULL}
4550};
4551
4552static PyGetSetDef Pickler_getsets[] = {
4553 {"memo", (getter)Pickler_get_memo,
4554 (setter)Pickler_set_memo},
4555 {"persistent_id", (getter)Pickler_get_persid,
4556 (setter)Pickler_set_persid},
4557 {NULL}
4558};
4559
4560static PyTypeObject Pickler_Type = {
4561 PyVarObject_HEAD_INIT(NULL, 0)
4562 "_pickle.Pickler" , /*tp_name*/
4563 sizeof(PicklerObject), /*tp_basicsize*/
4564 0, /*tp_itemsize*/
4565 (destructor)Pickler_dealloc, /*tp_dealloc*/
4566 0, /*tp_print*/
4567 0, /*tp_getattr*/
4568 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004569 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570 0, /*tp_repr*/
4571 0, /*tp_as_number*/
4572 0, /*tp_as_sequence*/
4573 0, /*tp_as_mapping*/
4574 0, /*tp_hash*/
4575 0, /*tp_call*/
4576 0, /*tp_str*/
4577 0, /*tp_getattro*/
4578 0, /*tp_setattro*/
4579 0, /*tp_as_buffer*/
4580 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004581 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004582 (traverseproc)Pickler_traverse, /*tp_traverse*/
4583 (inquiry)Pickler_clear, /*tp_clear*/
4584 0, /*tp_richcompare*/
4585 0, /*tp_weaklistoffset*/
4586 0, /*tp_iter*/
4587 0, /*tp_iternext*/
4588 Pickler_methods, /*tp_methods*/
4589 Pickler_members, /*tp_members*/
4590 Pickler_getsets, /*tp_getset*/
4591 0, /*tp_base*/
4592 0, /*tp_dict*/
4593 0, /*tp_descr_get*/
4594 0, /*tp_descr_set*/
4595 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004596 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004597 PyType_GenericAlloc, /*tp_alloc*/
4598 PyType_GenericNew, /*tp_new*/
4599 PyObject_GC_Del, /*tp_free*/
4600 0, /*tp_is_gc*/
4601};
4602
Victor Stinner121aab42011-09-29 23:40:53 +02004603/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004604
4605 XXX: It would be nice to able to avoid Python function call overhead, by
4606 using directly the C version of find_class(), when find_class() is not
4607 overridden by a subclass. Although, this could become rather hackish. A
4608 simpler optimization would be to call the C function when self is not a
4609 subclass instance. */
4610static PyObject *
4611find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4612{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004613 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004614
Victor Stinner55ba38a2016-12-09 16:09:30 +01004615 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4616 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004617}
4618
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004619static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004620marker(UnpicklerObject *self)
4621{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004622 Py_ssize_t mark;
4623
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004624 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004625 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004626 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004627 return -1;
4628 }
4629
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004630 mark = self->marks[--self->num_marks];
4631 self->stack->mark_set = self->num_marks != 0;
4632 self->stack->fence = self->num_marks ?
4633 self->marks[self->num_marks - 1] : 0;
4634 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004635}
4636
4637static int
4638load_none(UnpicklerObject *self)
4639{
4640 PDATA_APPEND(self->stack, Py_None, -1);
4641 return 0;
4642}
4643
4644static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004645load_int(UnpicklerObject *self)
4646{
4647 PyObject *value;
4648 char *endptr, *s;
4649 Py_ssize_t len;
4650 long x;
4651
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004652 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004653 return -1;
4654 if (len < 2)
4655 return bad_readline();
4656
4657 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004658 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004659 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004660 x = strtol(s, &endptr, 0);
4661
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004662 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004663 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004664 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004665 errno = 0;
4666 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004667 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004668 if (value == NULL) {
4669 PyErr_SetString(PyExc_ValueError,
4670 "could not convert string to int");
4671 return -1;
4672 }
4673 }
4674 else {
4675 if (len == 3 && (x == 0 || x == 1)) {
4676 if ((value = PyBool_FromLong(x)) == NULL)
4677 return -1;
4678 }
4679 else {
4680 if ((value = PyLong_FromLong(x)) == NULL)
4681 return -1;
4682 }
4683 }
4684
4685 PDATA_PUSH(self->stack, value, -1);
4686 return 0;
4687}
4688
4689static int
4690load_bool(UnpicklerObject *self, PyObject *boolean)
4691{
4692 assert(boolean == Py_True || boolean == Py_False);
4693 PDATA_APPEND(self->stack, boolean, -1);
4694 return 0;
4695}
4696
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004697/* s contains x bytes of an unsigned little-endian integer. Return its value
4698 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4699 */
4700static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004701calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004702{
4703 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004704 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004705 size_t x = 0;
4706
Serhiy Storchakae0606192015-09-29 22:10:07 +03004707 if (nbytes > (int)sizeof(size_t)) {
4708 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4709 * have 64-bit size that can't be represented on 32-bit platform.
4710 */
4711 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4712 if (s[i])
4713 return -1;
4714 }
4715 nbytes = (int)sizeof(size_t);
4716 }
4717 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004718 x |= (size_t) s[i] << (8 * i);
4719 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004720
4721 if (x > PY_SSIZE_T_MAX)
4722 return -1;
4723 else
4724 return (Py_ssize_t) x;
4725}
4726
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004727/* s contains x bytes of a little-endian integer. Return its value as a
4728 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004729 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004730 * of x-platform bugs.
4731 */
4732static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004733calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004734{
4735 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004736 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004737 long x = 0;
4738
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004739 for (i = 0; i < nbytes; i++) {
4740 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004741 }
4742
4743 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4744 * is signed, so on a box with longs bigger than 4 bytes we need
4745 * to extend a BININT's sign bit to the full width.
4746 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004747 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004748 x |= -(x & (1L << 31));
4749 }
4750
4751 return x;
4752}
4753
4754static int
4755load_binintx(UnpicklerObject *self, char *s, int size)
4756{
4757 PyObject *value;
4758 long x;
4759
4760 x = calc_binint(s, size);
4761
4762 if ((value = PyLong_FromLong(x)) == NULL)
4763 return -1;
4764
4765 PDATA_PUSH(self->stack, value, -1);
4766 return 0;
4767}
4768
4769static int
4770load_binint(UnpicklerObject *self)
4771{
4772 char *s;
4773
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004774 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004775 return -1;
4776
4777 return load_binintx(self, s, 4);
4778}
4779
4780static int
4781load_binint1(UnpicklerObject *self)
4782{
4783 char *s;
4784
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004785 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004786 return -1;
4787
4788 return load_binintx(self, s, 1);
4789}
4790
4791static int
4792load_binint2(UnpicklerObject *self)
4793{
4794 char *s;
4795
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004796 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004797 return -1;
4798
4799 return load_binintx(self, s, 2);
4800}
4801
4802static int
4803load_long(UnpicklerObject *self)
4804{
4805 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004806 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004807 Py_ssize_t len;
4808
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004809 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004810 return -1;
4811 if (len < 2)
4812 return bad_readline();
4813
Mark Dickinson8dd05142009-01-20 20:43:58 +00004814 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4815 the 'L' before calling PyLong_FromString. In order to maintain
4816 compatibility with Python 3.0.0, we don't actually *require*
4817 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004818 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004819 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004820 /* XXX: Should the base argument explicitly set to 10? */
4821 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004822 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004823 return -1;
4824
4825 PDATA_PUSH(self->stack, value, -1);
4826 return 0;
4827}
4828
4829/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4830 * data following.
4831 */
4832static int
4833load_counted_long(UnpicklerObject *self, int size)
4834{
4835 PyObject *value;
4836 char *nbytes;
4837 char *pdata;
4838
4839 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004840 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004841 return -1;
4842
4843 size = calc_binint(nbytes, size);
4844 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004845 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004846 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004847 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004848 "LONG pickle has negative byte count");
4849 return -1;
4850 }
4851
4852 if (size == 0)
4853 value = PyLong_FromLong(0L);
4854 else {
4855 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004856 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004857 return -1;
4858 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4859 1 /* little endian */ , 1 /* signed */ );
4860 }
4861 if (value == NULL)
4862 return -1;
4863 PDATA_PUSH(self->stack, value, -1);
4864 return 0;
4865}
4866
4867static int
4868load_float(UnpicklerObject *self)
4869{
4870 PyObject *value;
4871 char *endptr, *s;
4872 Py_ssize_t len;
4873 double d;
4874
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004875 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004876 return -1;
4877 if (len < 2)
4878 return bad_readline();
4879
4880 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004881 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4882 if (d == -1.0 && PyErr_Occurred())
4883 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004884 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004885 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4886 return -1;
4887 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004888 value = PyFloat_FromDouble(d);
4889 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004890 return -1;
4891
4892 PDATA_PUSH(self->stack, value, -1);
4893 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004894}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004895
4896static int
4897load_binfloat(UnpicklerObject *self)
4898{
4899 PyObject *value;
4900 double x;
4901 char *s;
4902
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004903 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004904 return -1;
4905
4906 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4907 if (x == -1.0 && PyErr_Occurred())
4908 return -1;
4909
4910 if ((value = PyFloat_FromDouble(x)) == NULL)
4911 return -1;
4912
4913 PDATA_PUSH(self->stack, value, -1);
4914 return 0;
4915}
4916
4917static int
4918load_string(UnpicklerObject *self)
4919{
4920 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004921 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004922 Py_ssize_t len;
4923 char *s, *p;
4924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004925 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004926 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004927 /* Strip the newline */
4928 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004929 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004930 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004931 p = s + 1;
4932 len -= 2;
4933 }
4934 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004935 PickleState *st = _Pickle_GetGlobalState();
4936 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004937 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004938 return -1;
4939 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004940 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004941
4942 /* Use the PyBytes API to decode the string, since that is what is used
4943 to encode, and then coerce the result to Unicode. */
4944 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004945 if (bytes == NULL)
4946 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004947
4948 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4949 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4950 if (strcmp(self->encoding, "bytes") == 0) {
4951 obj = bytes;
4952 }
4953 else {
4954 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4955 Py_DECREF(bytes);
4956 if (obj == NULL) {
4957 return -1;
4958 }
4959 }
4960
4961 PDATA_PUSH(self->stack, obj, -1);
4962 return 0;
4963}
4964
4965static int
4966load_counted_binstring(UnpicklerObject *self, int nbytes)
4967{
4968 PyObject *obj;
4969 Py_ssize_t size;
4970 char *s;
4971
4972 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004973 return -1;
4974
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004975 size = calc_binsize(s, nbytes);
4976 if (size < 0) {
4977 PickleState *st = _Pickle_GetGlobalState();
4978 PyErr_Format(st->UnpicklingError,
4979 "BINSTRING exceeds system's maximum size of %zd bytes",
4980 PY_SSIZE_T_MAX);
4981 return -1;
4982 }
4983
4984 if (_Unpickler_Read(self, &s, size) < 0)
4985 return -1;
4986
4987 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4988 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4989 if (strcmp(self->encoding, "bytes") == 0) {
4990 obj = PyBytes_FromStringAndSize(s, size);
4991 }
4992 else {
4993 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4994 }
4995 if (obj == NULL) {
4996 return -1;
4997 }
4998
4999 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005000 return 0;
5001}
5002
5003static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005004load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005005{
5006 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005007 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005008 char *s;
5009
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005010 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005011 return -1;
5012
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005013 size = calc_binsize(s, nbytes);
5014 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005015 PyErr_Format(PyExc_OverflowError,
5016 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005017 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005018 return -1;
5019 }
5020
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005021 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005022 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005023
5024 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005025 if (bytes == NULL)
5026 return -1;
5027
5028 PDATA_PUSH(self->stack, bytes, -1);
5029 return 0;
5030}
5031
5032static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005033load_unicode(UnpicklerObject *self)
5034{
5035 PyObject *str;
5036 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005037 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005038
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005039 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040 return -1;
5041 if (len < 1)
5042 return bad_readline();
5043
5044 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5045 if (str == NULL)
5046 return -1;
5047
5048 PDATA_PUSH(self->stack, str, -1);
5049 return 0;
5050}
5051
5052static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005053load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005054{
5055 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005056 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005057 char *s;
5058
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005059 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005060 return -1;
5061
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005062 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005063 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005064 PyErr_Format(PyExc_OverflowError,
5065 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005066 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005067 return -1;
5068 }
5069
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005070 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005071 return -1;
5072
Victor Stinner485fb562010-04-13 11:07:24 +00005073 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005074 if (str == NULL)
5075 return -1;
5076
5077 PDATA_PUSH(self->stack, str, -1);
5078 return 0;
5079}
5080
5081static int
Victor Stinner21b47112016-03-14 18:09:39 +01005082load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005083{
5084 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005085
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005086 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005087 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005088
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005089 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005090 if (tuple == NULL)
5091 return -1;
5092 PDATA_PUSH(self->stack, tuple, -1);
5093 return 0;
5094}
5095
5096static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005097load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005098{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005099 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005100
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005101 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005102 return -1;
5103
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005104 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005105}
5106
5107static int
5108load_empty_list(UnpicklerObject *self)
5109{
5110 PyObject *list;
5111
5112 if ((list = PyList_New(0)) == NULL)
5113 return -1;
5114 PDATA_PUSH(self->stack, list, -1);
5115 return 0;
5116}
5117
5118static int
5119load_empty_dict(UnpicklerObject *self)
5120{
5121 PyObject *dict;
5122
5123 if ((dict = PyDict_New()) == NULL)
5124 return -1;
5125 PDATA_PUSH(self->stack, dict, -1);
5126 return 0;
5127}
5128
5129static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005130load_empty_set(UnpicklerObject *self)
5131{
5132 PyObject *set;
5133
5134 if ((set = PySet_New(NULL)) == NULL)
5135 return -1;
5136 PDATA_PUSH(self->stack, set, -1);
5137 return 0;
5138}
5139
5140static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005141load_list(UnpicklerObject *self)
5142{
5143 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005144 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005145
5146 if ((i = marker(self)) < 0)
5147 return -1;
5148
5149 list = Pdata_poplist(self->stack, i);
5150 if (list == NULL)
5151 return -1;
5152 PDATA_PUSH(self->stack, list, -1);
5153 return 0;
5154}
5155
5156static int
5157load_dict(UnpicklerObject *self)
5158{
5159 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005160 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005161
5162 if ((i = marker(self)) < 0)
5163 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005164 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005165
5166 if ((dict = PyDict_New()) == NULL)
5167 return -1;
5168
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005169 if ((j - i) % 2 != 0) {
5170 PickleState *st = _Pickle_GetGlobalState();
5171 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005172 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005173 return -1;
5174 }
5175
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005176 for (k = i + 1; k < j; k += 2) {
5177 key = self->stack->data[k - 1];
5178 value = self->stack->data[k];
5179 if (PyDict_SetItem(dict, key, value) < 0) {
5180 Py_DECREF(dict);
5181 return -1;
5182 }
5183 }
5184 Pdata_clear(self->stack, i);
5185 PDATA_PUSH(self->stack, dict, -1);
5186 return 0;
5187}
5188
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005189static int
5190load_frozenset(UnpicklerObject *self)
5191{
5192 PyObject *items;
5193 PyObject *frozenset;
5194 Py_ssize_t i;
5195
5196 if ((i = marker(self)) < 0)
5197 return -1;
5198
5199 items = Pdata_poptuple(self->stack, i);
5200 if (items == NULL)
5201 return -1;
5202
5203 frozenset = PyFrozenSet_New(items);
5204 Py_DECREF(items);
5205 if (frozenset == NULL)
5206 return -1;
5207
5208 PDATA_PUSH(self->stack, frozenset, -1);
5209 return 0;
5210}
5211
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005212static PyObject *
5213instantiate(PyObject *cls, PyObject *args)
5214{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005215 /* Caller must assure args are a tuple. Normally, args come from
5216 Pdata_poptuple which packs objects from the top of the stack
5217 into a newly created tuple. */
5218 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005219 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5220 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005221 _Py_IDENTIFIER(__new__);
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005222 PyObject *func = _PyObject_GetAttrId(cls, &PyId___getinitargs__);
5223 if (func == NULL) {
5224 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
5225 return NULL;
5226 }
5227 PyErr_Clear();
5228 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5229 }
5230 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005231 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005232 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005233}
5234
5235static int
5236load_obj(UnpicklerObject *self)
5237{
5238 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005239 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005240
5241 if ((i = marker(self)) < 0)
5242 return -1;
5243
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005244 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005245 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005246
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005247 args = Pdata_poptuple(self->stack, i + 1);
5248 if (args == NULL)
5249 return -1;
5250
5251 PDATA_POP(self->stack, cls);
5252 if (cls) {
5253 obj = instantiate(cls, args);
5254 Py_DECREF(cls);
5255 }
5256 Py_DECREF(args);
5257 if (obj == NULL)
5258 return -1;
5259
5260 PDATA_PUSH(self->stack, obj, -1);
5261 return 0;
5262}
5263
5264static int
5265load_inst(UnpicklerObject *self)
5266{
5267 PyObject *cls = NULL;
5268 PyObject *args = NULL;
5269 PyObject *obj = NULL;
5270 PyObject *module_name;
5271 PyObject *class_name;
5272 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005273 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005274 char *s;
5275
5276 if ((i = marker(self)) < 0)
5277 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005278 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005279 return -1;
5280 if (len < 2)
5281 return bad_readline();
5282
5283 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5284 identifiers are permitted in Python 3.0, since the INST opcode is only
5285 supported by older protocols on Python 2.x. */
5286 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5287 if (module_name == NULL)
5288 return -1;
5289
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005290 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005291 if (len < 2) {
5292 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005293 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005294 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005295 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005296 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005297 cls = find_class(self, module_name, class_name);
5298 Py_DECREF(class_name);
5299 }
5300 }
5301 Py_DECREF(module_name);
5302
5303 if (cls == NULL)
5304 return -1;
5305
5306 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5307 obj = instantiate(cls, args);
5308 Py_DECREF(args);
5309 }
5310 Py_DECREF(cls);
5311
5312 if (obj == NULL)
5313 return -1;
5314
5315 PDATA_PUSH(self->stack, obj, -1);
5316 return 0;
5317}
5318
5319static int
5320load_newobj(UnpicklerObject *self)
5321{
5322 PyObject *args = NULL;
5323 PyObject *clsraw = NULL;
5324 PyTypeObject *cls; /* clsraw cast to its true type */
5325 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005326 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005327
5328 /* Stack is ... cls argtuple, and we want to call
5329 * cls.__new__(cls, *argtuple).
5330 */
5331 PDATA_POP(self->stack, args);
5332 if (args == NULL)
5333 goto error;
5334 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005335 PyErr_SetString(st->UnpicklingError,
5336 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005337 goto error;
5338 }
5339
5340 PDATA_POP(self->stack, clsraw);
5341 cls = (PyTypeObject *)clsraw;
5342 if (cls == NULL)
5343 goto error;
5344 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005345 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005346 "isn't a type object");
5347 goto error;
5348 }
5349 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005350 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005351 "has NULL tp_new");
5352 goto error;
5353 }
5354
5355 /* Call __new__. */
5356 obj = cls->tp_new(cls, args, NULL);
5357 if (obj == NULL)
5358 goto error;
5359
5360 Py_DECREF(args);
5361 Py_DECREF(clsraw);
5362 PDATA_PUSH(self->stack, obj, -1);
5363 return 0;
5364
5365 error:
5366 Py_XDECREF(args);
5367 Py_XDECREF(clsraw);
5368 return -1;
5369}
5370
5371static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005372load_newobj_ex(UnpicklerObject *self)
5373{
5374 PyObject *cls, *args, *kwargs;
5375 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005376 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005377
5378 PDATA_POP(self->stack, kwargs);
5379 if (kwargs == NULL) {
5380 return -1;
5381 }
5382 PDATA_POP(self->stack, args);
5383 if (args == NULL) {
5384 Py_DECREF(kwargs);
5385 return -1;
5386 }
5387 PDATA_POP(self->stack, cls);
5388 if (cls == NULL) {
5389 Py_DECREF(kwargs);
5390 Py_DECREF(args);
5391 return -1;
5392 }
Larry Hastings61272b72014-01-07 12:41:53 -08005393
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005394 if (!PyType_Check(cls)) {
5395 Py_DECREF(kwargs);
5396 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005397 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005398 "NEWOBJ_EX class argument must be a type, not %.200s",
5399 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005400 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005401 return -1;
5402 }
5403
5404 if (((PyTypeObject *)cls)->tp_new == NULL) {
5405 Py_DECREF(kwargs);
5406 Py_DECREF(args);
5407 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005408 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005409 "NEWOBJ_EX class argument doesn't have __new__");
5410 return -1;
5411 }
5412 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5413 Py_DECREF(kwargs);
5414 Py_DECREF(args);
5415 Py_DECREF(cls);
5416 if (obj == NULL) {
5417 return -1;
5418 }
5419 PDATA_PUSH(self->stack, obj, -1);
5420 return 0;
5421}
5422
5423static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005424load_global(UnpicklerObject *self)
5425{
5426 PyObject *global = NULL;
5427 PyObject *module_name;
5428 PyObject *global_name;
5429 Py_ssize_t len;
5430 char *s;
5431
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005432 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005433 return -1;
5434 if (len < 2)
5435 return bad_readline();
5436 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5437 if (!module_name)
5438 return -1;
5439
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005440 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005441 if (len < 2) {
5442 Py_DECREF(module_name);
5443 return bad_readline();
5444 }
5445 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5446 if (global_name) {
5447 global = find_class(self, module_name, global_name);
5448 Py_DECREF(global_name);
5449 }
5450 }
5451 Py_DECREF(module_name);
5452
5453 if (global == NULL)
5454 return -1;
5455 PDATA_PUSH(self->stack, global, -1);
5456 return 0;
5457}
5458
5459static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005460load_stack_global(UnpicklerObject *self)
5461{
5462 PyObject *global;
5463 PyObject *module_name;
5464 PyObject *global_name;
5465
5466 PDATA_POP(self->stack, global_name);
5467 PDATA_POP(self->stack, module_name);
5468 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5469 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005470 PickleState *st = _Pickle_GetGlobalState();
5471 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005472 Py_XDECREF(global_name);
5473 Py_XDECREF(module_name);
5474 return -1;
5475 }
5476 global = find_class(self, module_name, global_name);
5477 Py_DECREF(global_name);
5478 Py_DECREF(module_name);
5479 if (global == NULL)
5480 return -1;
5481 PDATA_PUSH(self->stack, global, -1);
5482 return 0;
5483}
5484
5485static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005486load_persid(UnpicklerObject *self)
5487{
5488 PyObject *pid;
5489 Py_ssize_t len;
5490 char *s;
5491
5492 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005493 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005494 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005495 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005496 return bad_readline();
5497
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005498 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5499 if (pid == NULL) {
5500 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5501 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5502 "persistent IDs in protocol 0 must be "
5503 "ASCII strings");
5504 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005505 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005506 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005507
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005508 /* This does not leak since _Pickle_FastCall() steals the reference
5509 to pid first. */
5510 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005511 if (pid == NULL)
5512 return -1;
5513
5514 PDATA_PUSH(self->stack, pid, -1);
5515 return 0;
5516 }
5517 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005518 PickleState *st = _Pickle_GetGlobalState();
5519 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005520 "A load persistent id instruction was encountered,\n"
5521 "but no persistent_load function was specified.");
5522 return -1;
5523 }
5524}
5525
5526static int
5527load_binpersid(UnpicklerObject *self)
5528{
5529 PyObject *pid;
5530
5531 if (self->pers_func) {
5532 PDATA_POP(self->stack, pid);
5533 if (pid == NULL)
5534 return -1;
5535
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005536 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005537 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005538 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005539 if (pid == NULL)
5540 return -1;
5541
5542 PDATA_PUSH(self->stack, pid, -1);
5543 return 0;
5544 }
5545 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005546 PickleState *st = _Pickle_GetGlobalState();
5547 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005548 "A load persistent id instruction was encountered,\n"
5549 "but no persistent_load function was specified.");
5550 return -1;
5551 }
5552}
5553
5554static int
5555load_pop(UnpicklerObject *self)
5556{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005557 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005558
5559 /* Note that we split the (pickle.py) stack into two stacks,
5560 * an object stack and a mark stack. We have to be clever and
5561 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005562 * mark stack first, and only signalling a stack underflow if
5563 * the object stack is empty and the mark stack doesn't match
5564 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005565 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005566 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005567 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005568 self->stack->mark_set = self->num_marks != 0;
5569 self->stack->fence = self->num_marks ?
5570 self->marks[self->num_marks - 1] : 0;
5571 } else if (len <= self->stack->fence)
5572 return Pdata_stack_underflow(self->stack);
5573 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005574 len--;
5575 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005576 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005577 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005578 return 0;
5579}
5580
5581static int
5582load_pop_mark(UnpicklerObject *self)
5583{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005584 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005585
5586 if ((i = marker(self)) < 0)
5587 return -1;
5588
5589 Pdata_clear(self->stack, i);
5590
5591 return 0;
5592}
5593
5594static int
5595load_dup(UnpicklerObject *self)
5596{
5597 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005598 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005600 if (len <= self->stack->fence)
5601 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005602 last = self->stack->data[len - 1];
5603 PDATA_APPEND(self->stack, last, -1);
5604 return 0;
5605}
5606
5607static int
5608load_get(UnpicklerObject *self)
5609{
5610 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005611 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612 Py_ssize_t len;
5613 char *s;
5614
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005615 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 return -1;
5617 if (len < 2)
5618 return bad_readline();
5619
5620 key = PyLong_FromString(s, NULL, 10);
5621 if (key == NULL)
5622 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005623 idx = PyLong_AsSsize_t(key);
5624 if (idx == -1 && PyErr_Occurred()) {
5625 Py_DECREF(key);
5626 return -1;
5627 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005629 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 if (value == NULL) {
5631 if (!PyErr_Occurred())
5632 PyErr_SetObject(PyExc_KeyError, key);
5633 Py_DECREF(key);
5634 return -1;
5635 }
5636 Py_DECREF(key);
5637
5638 PDATA_APPEND(self->stack, value, -1);
5639 return 0;
5640}
5641
5642static int
5643load_binget(UnpicklerObject *self)
5644{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005645 PyObject *value;
5646 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005647 char *s;
5648
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005649 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005650 return -1;
5651
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005652 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005653
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005654 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005655 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005656 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005657 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005658 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005659 Py_DECREF(key);
5660 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005661 return -1;
5662 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005663
5664 PDATA_APPEND(self->stack, value, -1);
5665 return 0;
5666}
5667
5668static int
5669load_long_binget(UnpicklerObject *self)
5670{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005671 PyObject *value;
5672 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005673 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005674
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005675 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005676 return -1;
5677
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005678 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005679
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005680 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005681 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005682 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005683 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005685 Py_DECREF(key);
5686 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687 return -1;
5688 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005689
5690 PDATA_APPEND(self->stack, value, -1);
5691 return 0;
5692}
5693
5694/* Push an object from the extension registry (EXT[124]). nbytes is
5695 * the number of bytes following the opcode, holding the index (code) value.
5696 */
5697static int
5698load_extension(UnpicklerObject *self, int nbytes)
5699{
5700 char *codebytes; /* the nbytes bytes after the opcode */
5701 long code; /* calc_binint returns long */
5702 PyObject *py_code; /* code as a Python int */
5703 PyObject *obj; /* the object to push */
5704 PyObject *pair; /* (module_name, class_name) */
5705 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005706 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005707
5708 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005709 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005710 return -1;
5711 code = calc_binint(codebytes, nbytes);
5712 if (code <= 0) { /* note that 0 is forbidden */
5713 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005714 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005715 return -1;
5716 }
5717
5718 /* Look for the code in the cache. */
5719 py_code = PyLong_FromLong(code);
5720 if (py_code == NULL)
5721 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005722 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005723 if (obj != NULL) {
5724 /* Bingo. */
5725 Py_DECREF(py_code);
5726 PDATA_APPEND(self->stack, obj, -1);
5727 return 0;
5728 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005729 if (PyErr_Occurred()) {
5730 Py_DECREF(py_code);
5731 return -1;
5732 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005733
5734 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005735 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005736 if (pair == NULL) {
5737 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005738 if (!PyErr_Occurred()) {
5739 PyErr_Format(PyExc_ValueError, "unregistered extension "
5740 "code %ld", code);
5741 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005742 return -1;
5743 }
5744 /* Since the extension registry is manipulable via Python code,
5745 * confirm that pair is really a 2-tuple of strings.
5746 */
5747 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5748 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5749 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5750 Py_DECREF(py_code);
5751 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5752 "isn't a 2-tuple of strings", code);
5753 return -1;
5754 }
5755 /* Load the object. */
5756 obj = find_class(self, module_name, class_name);
5757 if (obj == NULL) {
5758 Py_DECREF(py_code);
5759 return -1;
5760 }
5761 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005762 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763 Py_DECREF(py_code);
5764 if (code < 0) {
5765 Py_DECREF(obj);
5766 return -1;
5767 }
5768 PDATA_PUSH(self->stack, obj, -1);
5769 return 0;
5770}
5771
5772static int
5773load_put(UnpicklerObject *self)
5774{
5775 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005776 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005777 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005778 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005779
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005780 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781 return -1;
5782 if (len < 2)
5783 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005784 if (Py_SIZE(self->stack) <= self->stack->fence)
5785 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005786 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005787
5788 key = PyLong_FromString(s, NULL, 10);
5789 if (key == NULL)
5790 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005791 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005792 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005793 if (idx < 0) {
5794 if (!PyErr_Occurred())
5795 PyErr_SetString(PyExc_ValueError,
5796 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005797 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005798 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005799
5800 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005801}
5802
5803static int
5804load_binput(UnpicklerObject *self)
5805{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005806 PyObject *value;
5807 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005808 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005809
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005810 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005811 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005812
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005813 if (Py_SIZE(self->stack) <= self->stack->fence)
5814 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005815 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005816
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005817 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005818
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005819 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005820}
5821
5822static int
5823load_long_binput(UnpicklerObject *self)
5824{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005825 PyObject *value;
5826 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005827 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005828
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005829 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005830 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005831
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005832 if (Py_SIZE(self->stack) <= self->stack->fence)
5833 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005834 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005835
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005836 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005837 if (idx < 0) {
5838 PyErr_SetString(PyExc_ValueError,
5839 "negative LONG_BINPUT argument");
5840 return -1;
5841 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005842
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005843 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005844}
5845
5846static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005847load_memoize(UnpicklerObject *self)
5848{
5849 PyObject *value;
5850
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005851 if (Py_SIZE(self->stack) <= self->stack->fence)
5852 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005853 value = self->stack->data[Py_SIZE(self->stack) - 1];
5854
5855 return _Unpickler_MemoPut(self, self->memo_len, value);
5856}
5857
5858static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005859do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005860{
5861 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005862 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005863 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005864 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005865 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005866
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005867 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005868 if (x > len || x <= self->stack->fence)
5869 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005870 if (len == x) /* nothing to do */
5871 return 0;
5872
5873 list = self->stack->data[x - 1];
5874
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005875 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005876 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005877 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005878
5879 slice = Pdata_poplist(self->stack, x);
5880 if (!slice)
5881 return -1;
5882 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005883 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005884 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005885 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005886 }
5887 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005888 PyObject *extend_func;
5889 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005890
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005891 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
5892 if (extend_func != NULL) {
5893 slice = Pdata_poplist(self->stack, x);
5894 if (!slice) {
5895 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005896 return -1;
5897 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005898 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005899 Py_DECREF(extend_func);
5900 if (result == NULL)
5901 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005902 Py_DECREF(result);
5903 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005904 else {
5905 PyObject *append_func;
5906 _Py_IDENTIFIER(append);
5907
5908 /* Even if the PEP 307 requires extend() and append() methods,
5909 fall back on append() if the object has no extend() method
5910 for backward compatibility. */
5911 PyErr_Clear();
5912 append_func = _PyObject_GetAttrId(list, &PyId_append);
5913 if (append_func == NULL)
5914 return -1;
5915 for (i = x; i < len; i++) {
5916 value = self->stack->data[i];
5917 result = _Pickle_FastCall(append_func, value);
5918 if (result == NULL) {
5919 Pdata_clear(self->stack, i + 1);
5920 Py_SIZE(self->stack) = x;
5921 Py_DECREF(append_func);
5922 return -1;
5923 }
5924 Py_DECREF(result);
5925 }
5926 Py_SIZE(self->stack) = x;
5927 Py_DECREF(append_func);
5928 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005929 }
5930
5931 return 0;
5932}
5933
5934static int
5935load_append(UnpicklerObject *self)
5936{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005937 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5938 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005939 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005940}
5941
5942static int
5943load_appends(UnpicklerObject *self)
5944{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005945 Py_ssize_t i = marker(self);
5946 if (i < 0)
5947 return -1;
5948 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005949}
5950
5951static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005952do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005953{
5954 PyObject *value, *key;
5955 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005956 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005957 int status = 0;
5958
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005959 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005960 if (x > len || x <= self->stack->fence)
5961 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005962 if (len == x) /* nothing to do */
5963 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005964 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005965 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005966 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005967 PyErr_SetString(st->UnpicklingError,
5968 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005969 return -1;
5970 }
5971
5972 /* Here, dict does not actually need to be a PyDict; it could be anything
5973 that supports the __setitem__ attribute. */
5974 dict = self->stack->data[x - 1];
5975
5976 for (i = x + 1; i < len; i += 2) {
5977 key = self->stack->data[i - 1];
5978 value = self->stack->data[i];
5979 if (PyObject_SetItem(dict, key, value) < 0) {
5980 status = -1;
5981 break;
5982 }
5983 }
5984
5985 Pdata_clear(self->stack, x);
5986 return status;
5987}
5988
5989static int
5990load_setitem(UnpicklerObject *self)
5991{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005992 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005993}
5994
5995static int
5996load_setitems(UnpicklerObject *self)
5997{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005998 Py_ssize_t i = marker(self);
5999 if (i < 0)
6000 return -1;
6001 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006002}
6003
6004static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006005load_additems(UnpicklerObject *self)
6006{
6007 PyObject *set;
6008 Py_ssize_t mark, len, i;
6009
6010 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006011 if (mark < 0)
6012 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006013 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006014 if (mark > len || mark <= self->stack->fence)
6015 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006016 if (len == mark) /* nothing to do */
6017 return 0;
6018
6019 set = self->stack->data[mark - 1];
6020
6021 if (PySet_Check(set)) {
6022 PyObject *items;
6023 int status;
6024
6025 items = Pdata_poptuple(self->stack, mark);
6026 if (items == NULL)
6027 return -1;
6028
6029 status = _PySet_Update(set, items);
6030 Py_DECREF(items);
6031 return status;
6032 }
6033 else {
6034 PyObject *add_func;
6035 _Py_IDENTIFIER(add);
6036
6037 add_func = _PyObject_GetAttrId(set, &PyId_add);
6038 if (add_func == NULL)
6039 return -1;
6040 for (i = mark; i < len; i++) {
6041 PyObject *result;
6042 PyObject *item;
6043
6044 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006045 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006046 if (result == NULL) {
6047 Pdata_clear(self->stack, i + 1);
6048 Py_SIZE(self->stack) = mark;
6049 return -1;
6050 }
6051 Py_DECREF(result);
6052 }
6053 Py_SIZE(self->stack) = mark;
6054 }
6055
6056 return 0;
6057}
6058
6059static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006060load_build(UnpicklerObject *self)
6061{
6062 PyObject *state, *inst, *slotstate;
6063 PyObject *setstate;
6064 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006065 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006066
6067 /* Stack is ... instance, state. We want to leave instance at
6068 * the stack top, possibly mutated via instance.__setstate__(state).
6069 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006070 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6071 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006072
6073 PDATA_POP(self->stack, state);
6074 if (state == NULL)
6075 return -1;
6076
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006077 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006078
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006079 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006080 if (setstate == NULL) {
6081 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6082 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006083 else {
6084 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006085 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006086 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006087 }
6088 else {
6089 PyObject *result;
6090
6091 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006092 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006093 Py_DECREF(setstate);
6094 if (result == NULL)
6095 return -1;
6096 Py_DECREF(result);
6097 return 0;
6098 }
6099
6100 /* A default __setstate__. First see whether state embeds a
6101 * slot state dict too (a proto 2 addition).
6102 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006103 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006104 PyObject *tmp = state;
6105
6106 state = PyTuple_GET_ITEM(tmp, 0);
6107 slotstate = PyTuple_GET_ITEM(tmp, 1);
6108 Py_INCREF(state);
6109 Py_INCREF(slotstate);
6110 Py_DECREF(tmp);
6111 }
6112 else
6113 slotstate = NULL;
6114
6115 /* Set inst.__dict__ from the state dict (if any). */
6116 if (state != Py_None) {
6117 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006118 PyObject *d_key, *d_value;
6119 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006120 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006121
6122 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006123 PickleState *st = _Pickle_GetGlobalState();
6124 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006125 goto error;
6126 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006127 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006128 if (dict == NULL)
6129 goto error;
6130
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006131 i = 0;
6132 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6133 /* normally the keys for instance attributes are
6134 interned. we should try to do that here. */
6135 Py_INCREF(d_key);
6136 if (PyUnicode_CheckExact(d_key))
6137 PyUnicode_InternInPlace(&d_key);
6138 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6139 Py_DECREF(d_key);
6140 goto error;
6141 }
6142 Py_DECREF(d_key);
6143 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006144 Py_DECREF(dict);
6145 }
6146
6147 /* Also set instance attributes from the slotstate dict (if any). */
6148 if (slotstate != NULL) {
6149 PyObject *d_key, *d_value;
6150 Py_ssize_t i;
6151
6152 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006153 PickleState *st = _Pickle_GetGlobalState();
6154 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006155 "slot state is not a dictionary");
6156 goto error;
6157 }
6158 i = 0;
6159 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6160 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6161 goto error;
6162 }
6163 }
6164
6165 if (0) {
6166 error:
6167 status = -1;
6168 }
6169
6170 Py_DECREF(state);
6171 Py_XDECREF(slotstate);
6172 return status;
6173}
6174
6175static int
6176load_mark(UnpicklerObject *self)
6177{
6178
6179 /* Note that we split the (pickle.py) stack into two stacks, an
6180 * object stack and a mark stack. Here we push a mark onto the
6181 * mark stack.
6182 */
6183
6184 if ((self->num_marks + 1) >= self->marks_size) {
6185 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006186
6187 /* Use the size_t type to check for overflow. */
6188 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006189 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006190 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006191 PyErr_NoMemory();
6192 return -1;
6193 }
6194
6195 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006196 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006197 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006198 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6199 if (self->marks == NULL) {
6200 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006201 PyErr_NoMemory();
6202 return -1;
6203 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006204 self->marks_size = (Py_ssize_t)alloc;
6205 }
6206
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006207 self->stack->mark_set = 1;
6208 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006209
6210 return 0;
6211}
6212
6213static int
6214load_reduce(UnpicklerObject *self)
6215{
6216 PyObject *callable = NULL;
6217 PyObject *argtup = NULL;
6218 PyObject *obj = NULL;
6219
6220 PDATA_POP(self->stack, argtup);
6221 if (argtup == NULL)
6222 return -1;
6223 PDATA_POP(self->stack, callable);
6224 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006225 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006226 Py_DECREF(callable);
6227 }
6228 Py_DECREF(argtup);
6229
6230 if (obj == NULL)
6231 return -1;
6232
6233 PDATA_PUSH(self->stack, obj, -1);
6234 return 0;
6235}
6236
6237/* Just raises an error if we don't know the protocol specified. PROTO
6238 * is the first opcode for protocols >= 2.
6239 */
6240static int
6241load_proto(UnpicklerObject *self)
6242{
6243 char *s;
6244 int i;
6245
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006246 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006247 return -1;
6248
6249 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006250 if (i <= HIGHEST_PROTOCOL) {
6251 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006252 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006254
6255 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6256 return -1;
6257}
6258
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006259static int
6260load_frame(UnpicklerObject *self)
6261{
6262 char *s;
6263 Py_ssize_t frame_len;
6264
6265 if (_Unpickler_Read(self, &s, 8) < 0)
6266 return -1;
6267
6268 frame_len = calc_binsize(s, 8);
6269 if (frame_len < 0) {
6270 PyErr_Format(PyExc_OverflowError,
6271 "FRAME length exceeds system's maximum of %zd bytes",
6272 PY_SSIZE_T_MAX);
6273 return -1;
6274 }
6275
6276 if (_Unpickler_Read(self, &s, frame_len) < 0)
6277 return -1;
6278
6279 /* Rewind to start of frame */
6280 self->next_read_idx -= frame_len;
6281 return 0;
6282}
6283
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006284static PyObject *
6285load(UnpicklerObject *self)
6286{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006287 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006288 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006289
6290 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006291 self->stack->mark_set = 0;
6292 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006293 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006294 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006295 Pdata_clear(self->stack, 0);
6296
6297 /* Convenient macros for the dispatch while-switch loop just below. */
6298#define OP(opcode, load_func) \
6299 case opcode: if (load_func(self) < 0) break; continue;
6300
6301#define OP_ARG(opcode, load_func, arg) \
6302 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6303
6304 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006305 if (_Unpickler_Read(self, &s, 1) < 0) {
6306 PickleState *st = _Pickle_GetGlobalState();
6307 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6308 PyErr_Format(PyExc_EOFError, "Ran out of input");
6309 }
6310 return NULL;
6311 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006312
6313 switch ((enum opcode)s[0]) {
6314 OP(NONE, load_none)
6315 OP(BININT, load_binint)
6316 OP(BININT1, load_binint1)
6317 OP(BININT2, load_binint2)
6318 OP(INT, load_int)
6319 OP(LONG, load_long)
6320 OP_ARG(LONG1, load_counted_long, 1)
6321 OP_ARG(LONG4, load_counted_long, 4)
6322 OP(FLOAT, load_float)
6323 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006324 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6325 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6326 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6327 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6328 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006329 OP(STRING, load_string)
6330 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006331 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6332 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6333 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006334 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6335 OP_ARG(TUPLE1, load_counted_tuple, 1)
6336 OP_ARG(TUPLE2, load_counted_tuple, 2)
6337 OP_ARG(TUPLE3, load_counted_tuple, 3)
6338 OP(TUPLE, load_tuple)
6339 OP(EMPTY_LIST, load_empty_list)
6340 OP(LIST, load_list)
6341 OP(EMPTY_DICT, load_empty_dict)
6342 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006343 OP(EMPTY_SET, load_empty_set)
6344 OP(ADDITEMS, load_additems)
6345 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006346 OP(OBJ, load_obj)
6347 OP(INST, load_inst)
6348 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006349 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006350 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006351 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006352 OP(APPEND, load_append)
6353 OP(APPENDS, load_appends)
6354 OP(BUILD, load_build)
6355 OP(DUP, load_dup)
6356 OP(BINGET, load_binget)
6357 OP(LONG_BINGET, load_long_binget)
6358 OP(GET, load_get)
6359 OP(MARK, load_mark)
6360 OP(BINPUT, load_binput)
6361 OP(LONG_BINPUT, load_long_binput)
6362 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006363 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006364 OP(POP, load_pop)
6365 OP(POP_MARK, load_pop_mark)
6366 OP(SETITEM, load_setitem)
6367 OP(SETITEMS, load_setitems)
6368 OP(PERSID, load_persid)
6369 OP(BINPERSID, load_binpersid)
6370 OP(REDUCE, load_reduce)
6371 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006372 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006373 OP_ARG(EXT1, load_extension, 1)
6374 OP_ARG(EXT2, load_extension, 2)
6375 OP_ARG(EXT4, load_extension, 4)
6376 OP_ARG(NEWTRUE, load_bool, Py_True)
6377 OP_ARG(NEWFALSE, load_bool, Py_False)
6378
6379 case STOP:
6380 break;
6381
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006382 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006383 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006384 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006385 unsigned char c = (unsigned char) *s;
6386 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6387 PyErr_Format(st->UnpicklingError,
6388 "invalid load key, '%c'.", c);
6389 }
6390 else {
6391 PyErr_Format(st->UnpicklingError,
6392 "invalid load key, '\\x%02x'.", c);
6393 }
6394 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006395 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396 }
6397
6398 break; /* and we are done! */
6399 }
6400
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006401 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006402 return NULL;
6403 }
6404
Victor Stinner2ae57e32013-10-31 13:39:23 +01006405 if (_Unpickler_SkipConsumed(self) < 0)
6406 return NULL;
6407
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006408 PDATA_POP(self->stack, value);
6409 return value;
6410}
6411
Larry Hastings61272b72014-01-07 12:41:53 -08006412/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006413
6414_pickle.Unpickler.load
6415
6416Load a pickle.
6417
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006418Read a pickled object representation from the open file object given
6419in the constructor, and return the reconstituted object hierarchy
6420specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006421[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006422
Larry Hastings3cceb382014-01-04 11:09:09 -08006423static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006424_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006425/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006426{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006427 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006428
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006429 /* Check whether the Unpickler was initialized correctly. This prevents
6430 segfaulting if a subclass overridden __init__ with a function that does
6431 not call Unpickler.__init__(). Here, we simply ensure that self->read
6432 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006433 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006434 PickleState *st = _Pickle_GetGlobalState();
6435 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006436 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006437 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006438 return NULL;
6439 }
6440
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006441 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006442}
6443
6444/* The name of find_class() is misleading. In newer pickle protocols, this
6445 function is used for loading any global (i.e., functions), not just
6446 classes. The name is kept only for backward compatibility. */
6447
Larry Hastings61272b72014-01-07 12:41:53 -08006448/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006449
6450_pickle.Unpickler.find_class
6451
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006452 module_name: object
6453 global_name: object
6454 /
6455
6456Return an object from a specified module.
6457
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006458If necessary, the module will be imported. Subclasses may override
6459this method (e.g. to restrict unpickling of arbitrary classes and
6460functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006461
6462This method is called whenever a class or a function object is
6463needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006464[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006465
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006466static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006467_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6468 PyObject *module_name,
6469 PyObject *global_name)
6470/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006471{
6472 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006473 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006474
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006475 /* Try to map the old names used in Python 2.x to the new ones used in
6476 Python 3.x. We do this only with old pickle protocols and when the
6477 user has not disabled the feature. */
6478 if (self->proto < 3 && self->fix_imports) {
6479 PyObject *key;
6480 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006481 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006482
6483 /* Check if the global (i.e., a function or a class) was renamed
6484 or moved to another module. */
6485 key = PyTuple_Pack(2, module_name, global_name);
6486 if (key == NULL)
6487 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006488 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006489 Py_DECREF(key);
6490 if (item) {
6491 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6492 PyErr_Format(PyExc_RuntimeError,
6493 "_compat_pickle.NAME_MAPPING values should be "
6494 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6495 return NULL;
6496 }
6497 module_name = PyTuple_GET_ITEM(item, 0);
6498 global_name = PyTuple_GET_ITEM(item, 1);
6499 if (!PyUnicode_Check(module_name) ||
6500 !PyUnicode_Check(global_name)) {
6501 PyErr_Format(PyExc_RuntimeError,
6502 "_compat_pickle.NAME_MAPPING values should be "
6503 "pairs of str, not (%.200s, %.200s)",
6504 Py_TYPE(module_name)->tp_name,
6505 Py_TYPE(global_name)->tp_name);
6506 return NULL;
6507 }
6508 }
6509 else if (PyErr_Occurred()) {
6510 return NULL;
6511 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006512 else {
6513 /* Check if the module was renamed. */
6514 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6515 if (item) {
6516 if (!PyUnicode_Check(item)) {
6517 PyErr_Format(PyExc_RuntimeError,
6518 "_compat_pickle.IMPORT_MAPPING values should be "
6519 "strings, not %.200s", Py_TYPE(item)->tp_name);
6520 return NULL;
6521 }
6522 module_name = item;
6523 }
6524 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006525 return NULL;
6526 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006527 }
6528 }
6529
Eric Snow3f9eee62017-09-15 16:35:20 -06006530 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006531 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006532 if (PyErr_Occurred())
6533 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006534 module = PyImport_Import(module_name);
6535 if (module == NULL)
6536 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006537 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006538 global = getattribute(module, global_name, self->proto >= 4);
6539 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540 return global;
6541}
6542
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006543/*[clinic input]
6544
6545_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6546
6547Returns size in memory, in bytes.
6548[clinic start generated code]*/
6549
6550static Py_ssize_t
6551_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6552/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6553{
6554 Py_ssize_t res;
6555
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006556 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006557 if (self->memo != NULL)
6558 res += self->memo_size * sizeof(PyObject *);
6559 if (self->marks != NULL)
6560 res += self->marks_size * sizeof(Py_ssize_t);
6561 if (self->input_line != NULL)
6562 res += strlen(self->input_line) + 1;
6563 if (self->encoding != NULL)
6564 res += strlen(self->encoding) + 1;
6565 if (self->errors != NULL)
6566 res += strlen(self->errors) + 1;
6567 return res;
6568}
6569
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006570static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006571 _PICKLE_UNPICKLER_LOAD_METHODDEF
6572 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006573 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006574 {NULL, NULL} /* sentinel */
6575};
6576
6577static void
6578Unpickler_dealloc(UnpicklerObject *self)
6579{
6580 PyObject_GC_UnTrack((PyObject *)self);
6581 Py_XDECREF(self->readline);
6582 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006583 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006584 Py_XDECREF(self->stack);
6585 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006586 if (self->buffer.buf != NULL) {
6587 PyBuffer_Release(&self->buffer);
6588 self->buffer.buf = NULL;
6589 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006590
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006591 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006592 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006593 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006594 PyMem_Free(self->encoding);
6595 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006596
6597 Py_TYPE(self)->tp_free((PyObject *)self);
6598}
6599
6600static int
6601Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6602{
6603 Py_VISIT(self->readline);
6604 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006605 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006606 Py_VISIT(self->stack);
6607 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006608 return 0;
6609}
6610
6611static int
6612Unpickler_clear(UnpicklerObject *self)
6613{
6614 Py_CLEAR(self->readline);
6615 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006616 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006617 Py_CLEAR(self->stack);
6618 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006619 if (self->buffer.buf != NULL) {
6620 PyBuffer_Release(&self->buffer);
6621 self->buffer.buf = NULL;
6622 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006623
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006624 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006625 PyMem_Free(self->marks);
6626 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006627 PyMem_Free(self->input_line);
6628 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006629 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006630 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006631 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006632 self->errors = NULL;
6633
6634 return 0;
6635}
6636
Larry Hastings61272b72014-01-07 12:41:53 -08006637/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006638
6639_pickle.Unpickler.__init__
6640
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006641 file: object
6642 *
6643 fix_imports: bool = True
6644 encoding: str = 'ASCII'
6645 errors: str = 'strict'
6646
6647This takes a binary file for reading a pickle data stream.
6648
6649The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006650protocol argument is needed. Bytes past the pickled object's
6651representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006652
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006653The argument *file* must have two methods, a read() method that takes
6654an integer argument, and a readline() method that requires no
6655arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006656binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006657other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006658
6659Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006660which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006661generated by Python 2. If *fix_imports* is True, pickle will try to
6662map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006663*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006664instances pickled by Python 2; these default to 'ASCII' and 'strict',
6665respectively. The *encoding* can be 'bytes' to read these 8-bit
6666string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006667[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006668
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006669static int
Larry Hastings89964c42015-04-14 18:07:59 -04006670_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6671 int fix_imports, const char *encoding,
6672 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006673/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006674{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006675 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006676
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006677 /* In case of multiple __init__() calls, clear previous content. */
6678 if (self->read != NULL)
6679 (void)Unpickler_clear(self);
6680
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006681 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006682 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006683
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006684 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006685 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006686
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006687 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006688
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03006689 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6690 &PyId_persistent_load);
6691 if (self->pers_func == NULL) {
6692 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
6693 return -1;
6694 }
6695 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006696 }
6697
6698 self->stack = (Pdata *)Pdata_New();
6699 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006700 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006701
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006702 self->memo_size = 32;
6703 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006704 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006705 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006706
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006707 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006708
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006709 return 0;
6710}
6711
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006712
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006713/* Define a proxy object for the Unpickler's internal memo object. This is to
6714 * avoid breaking code like:
6715 * unpickler.memo.clear()
6716 * and
6717 * unpickler.memo = saved_memo
6718 * Is this a good idea? Not really, but we don't want to break code that uses
6719 * it. Note that we don't implement the entire mapping API here. This is
6720 * intentional, as these should be treated as black-box implementation details.
6721 *
6722 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006723 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006724 */
6725
Larry Hastings61272b72014-01-07 12:41:53 -08006726/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006727_pickle.UnpicklerMemoProxy.clear
6728
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006729Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006730[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006731
Larry Hastings3cceb382014-01-04 11:09:09 -08006732static PyObject *
6733_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006734/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006735{
6736 _Unpickler_MemoCleanup(self->unpickler);
6737 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6738 if (self->unpickler->memo == NULL)
6739 return NULL;
6740 Py_RETURN_NONE;
6741}
6742
Larry Hastings61272b72014-01-07 12:41:53 -08006743/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006744_pickle.UnpicklerMemoProxy.copy
6745
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006746Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006747[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006748
Larry Hastings3cceb382014-01-04 11:09:09 -08006749static PyObject *
6750_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006751/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006752{
6753 Py_ssize_t i;
6754 PyObject *new_memo = PyDict_New();
6755 if (new_memo == NULL)
6756 return NULL;
6757
6758 for (i = 0; i < self->unpickler->memo_size; i++) {
6759 int status;
6760 PyObject *key, *value;
6761
6762 value = self->unpickler->memo[i];
6763 if (value == NULL)
6764 continue;
6765
6766 key = PyLong_FromSsize_t(i);
6767 if (key == NULL)
6768 goto error;
6769 status = PyDict_SetItem(new_memo, key, value);
6770 Py_DECREF(key);
6771 if (status < 0)
6772 goto error;
6773 }
6774 return new_memo;
6775
6776error:
6777 Py_DECREF(new_memo);
6778 return NULL;
6779}
6780
Larry Hastings61272b72014-01-07 12:41:53 -08006781/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006782_pickle.UnpicklerMemoProxy.__reduce__
6783
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006784Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006785[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006786
Larry Hastings3cceb382014-01-04 11:09:09 -08006787static PyObject *
6788_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006789/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006790{
6791 PyObject *reduce_value;
6792 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006793 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006794 if (contents == NULL)
6795 return NULL;
6796
6797 reduce_value = PyTuple_New(2);
6798 if (reduce_value == NULL) {
6799 Py_DECREF(contents);
6800 return NULL;
6801 }
6802 constructor_args = PyTuple_New(1);
6803 if (constructor_args == NULL) {
6804 Py_DECREF(contents);
6805 Py_DECREF(reduce_value);
6806 return NULL;
6807 }
6808 PyTuple_SET_ITEM(constructor_args, 0, contents);
6809 Py_INCREF((PyObject *)&PyDict_Type);
6810 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6811 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6812 return reduce_value;
6813}
6814
6815static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006816 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6817 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6818 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006819 {NULL, NULL} /* sentinel */
6820};
6821
6822static void
6823UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6824{
6825 PyObject_GC_UnTrack(self);
6826 Py_XDECREF(self->unpickler);
6827 PyObject_GC_Del((PyObject *)self);
6828}
6829
6830static int
6831UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6832 visitproc visit, void *arg)
6833{
6834 Py_VISIT(self->unpickler);
6835 return 0;
6836}
6837
6838static int
6839UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6840{
6841 Py_CLEAR(self->unpickler);
6842 return 0;
6843}
6844
6845static PyTypeObject UnpicklerMemoProxyType = {
6846 PyVarObject_HEAD_INIT(NULL, 0)
6847 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6848 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6849 0,
6850 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6851 0, /* tp_print */
6852 0, /* tp_getattr */
6853 0, /* tp_setattr */
6854 0, /* tp_compare */
6855 0, /* tp_repr */
6856 0, /* tp_as_number */
6857 0, /* tp_as_sequence */
6858 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006859 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006860 0, /* tp_call */
6861 0, /* tp_str */
6862 PyObject_GenericGetAttr, /* tp_getattro */
6863 PyObject_GenericSetAttr, /* tp_setattro */
6864 0, /* tp_as_buffer */
6865 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6866 0, /* tp_doc */
6867 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6868 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6869 0, /* tp_richcompare */
6870 0, /* tp_weaklistoffset */
6871 0, /* tp_iter */
6872 0, /* tp_iternext */
6873 unpicklerproxy_methods, /* tp_methods */
6874};
6875
6876static PyObject *
6877UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6878{
6879 UnpicklerMemoProxyObject *self;
6880
6881 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6882 &UnpicklerMemoProxyType);
6883 if (self == NULL)
6884 return NULL;
6885 Py_INCREF(unpickler);
6886 self->unpickler = unpickler;
6887 PyObject_GC_Track(self);
6888 return (PyObject *)self;
6889}
6890
6891/*****************************************************************************/
6892
6893
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006894static PyObject *
6895Unpickler_get_memo(UnpicklerObject *self)
6896{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006897 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006898}
6899
6900static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006901Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006902{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006903 PyObject **new_memo;
6904 Py_ssize_t new_memo_size = 0;
6905 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006906
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006907 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006908 PyErr_SetString(PyExc_TypeError,
6909 "attribute deletion is not supported");
6910 return -1;
6911 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006912
6913 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6914 UnpicklerObject *unpickler =
6915 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6916
6917 new_memo_size = unpickler->memo_size;
6918 new_memo = _Unpickler_NewMemo(new_memo_size);
6919 if (new_memo == NULL)
6920 return -1;
6921
6922 for (i = 0; i < new_memo_size; i++) {
6923 Py_XINCREF(unpickler->memo[i]);
6924 new_memo[i] = unpickler->memo[i];
6925 }
6926 }
6927 else if (PyDict_Check(obj)) {
6928 Py_ssize_t i = 0;
6929 PyObject *key, *value;
6930
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006931 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006932 new_memo = _Unpickler_NewMemo(new_memo_size);
6933 if (new_memo == NULL)
6934 return -1;
6935
6936 while (PyDict_Next(obj, &i, &key, &value)) {
6937 Py_ssize_t idx;
6938 if (!PyLong_Check(key)) {
6939 PyErr_SetString(PyExc_TypeError,
6940 "memo key must be integers");
6941 goto error;
6942 }
6943 idx = PyLong_AsSsize_t(key);
6944 if (idx == -1 && PyErr_Occurred())
6945 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006946 if (idx < 0) {
6947 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006948 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006949 goto error;
6950 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006951 if (_Unpickler_MemoPut(self, idx, value) < 0)
6952 goto error;
6953 }
6954 }
6955 else {
6956 PyErr_Format(PyExc_TypeError,
6957 "'memo' attribute must be an UnpicklerMemoProxy object"
6958 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006959 return -1;
6960 }
6961
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006962 _Unpickler_MemoCleanup(self);
6963 self->memo_size = new_memo_size;
6964 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006965
6966 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006967
6968 error:
6969 if (new_memo_size) {
6970 i = new_memo_size;
6971 while (--i >= 0) {
6972 Py_XDECREF(new_memo[i]);
6973 }
6974 PyMem_FREE(new_memo);
6975 }
6976 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006977}
6978
6979static PyObject *
6980Unpickler_get_persload(UnpicklerObject *self)
6981{
6982 if (self->pers_func == NULL)
6983 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6984 else
6985 Py_INCREF(self->pers_func);
6986 return self->pers_func;
6987}
6988
6989static int
6990Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6991{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006992 if (value == NULL) {
6993 PyErr_SetString(PyExc_TypeError,
6994 "attribute deletion is not supported");
6995 return -1;
6996 }
6997 if (!PyCallable_Check(value)) {
6998 PyErr_SetString(PyExc_TypeError,
6999 "persistent_load must be a callable taking "
7000 "one argument");
7001 return -1;
7002 }
7003
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007004 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007005 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007006
7007 return 0;
7008}
7009
7010static PyGetSetDef Unpickler_getsets[] = {
7011 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7012 {"persistent_load", (getter)Unpickler_get_persload,
7013 (setter)Unpickler_set_persload},
7014 {NULL}
7015};
7016
7017static PyTypeObject Unpickler_Type = {
7018 PyVarObject_HEAD_INIT(NULL, 0)
7019 "_pickle.Unpickler", /*tp_name*/
7020 sizeof(UnpicklerObject), /*tp_basicsize*/
7021 0, /*tp_itemsize*/
7022 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7023 0, /*tp_print*/
7024 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007025 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007026 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007027 0, /*tp_repr*/
7028 0, /*tp_as_number*/
7029 0, /*tp_as_sequence*/
7030 0, /*tp_as_mapping*/
7031 0, /*tp_hash*/
7032 0, /*tp_call*/
7033 0, /*tp_str*/
7034 0, /*tp_getattro*/
7035 0, /*tp_setattro*/
7036 0, /*tp_as_buffer*/
7037 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007038 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007039 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7040 (inquiry)Unpickler_clear, /*tp_clear*/
7041 0, /*tp_richcompare*/
7042 0, /*tp_weaklistoffset*/
7043 0, /*tp_iter*/
7044 0, /*tp_iternext*/
7045 Unpickler_methods, /*tp_methods*/
7046 0, /*tp_members*/
7047 Unpickler_getsets, /*tp_getset*/
7048 0, /*tp_base*/
7049 0, /*tp_dict*/
7050 0, /*tp_descr_get*/
7051 0, /*tp_descr_set*/
7052 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007053 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007054 PyType_GenericAlloc, /*tp_alloc*/
7055 PyType_GenericNew, /*tp_new*/
7056 PyObject_GC_Del, /*tp_free*/
7057 0, /*tp_is_gc*/
7058};
7059
Larry Hastings61272b72014-01-07 12:41:53 -08007060/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007061
7062_pickle.dump
7063
7064 obj: object
7065 file: object
7066 protocol: object = NULL
7067 *
7068 fix_imports: bool = True
7069
7070Write a pickled representation of obj to the open file object file.
7071
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007072This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7073be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007074
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007075The optional *protocol* argument tells the pickler to use the given
7076protocol supported protocols are 0, 1, 2, 3 and 4. The default
7077protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007078
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007079Specifying a negative protocol version selects the highest protocol
7080version supported. The higher the protocol used, the more recent the
7081version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007082
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007083The *file* argument must have a write() method that accepts a single
7084bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007085writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007086this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007087
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007088If *fix_imports* is True and protocol is less than 3, pickle will try
7089to map the new Python 3 names to the old module names used in Python
70902, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007091[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007092
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007093static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007094_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007095 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007096/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007097{
7098 PicklerObject *pickler = _Pickler_New();
7099
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007100 if (pickler == NULL)
7101 return NULL;
7102
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007103 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007104 goto error;
7105
7106 if (_Pickler_SetOutputStream(pickler, file) < 0)
7107 goto error;
7108
7109 if (dump(pickler, obj) < 0)
7110 goto error;
7111
7112 if (_Pickler_FlushToFile(pickler) < 0)
7113 goto error;
7114
7115 Py_DECREF(pickler);
7116 Py_RETURN_NONE;
7117
7118 error:
7119 Py_XDECREF(pickler);
7120 return NULL;
7121}
7122
Larry Hastings61272b72014-01-07 12:41:53 -08007123/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007124
7125_pickle.dumps
7126
7127 obj: object
7128 protocol: object = NULL
7129 *
7130 fix_imports: bool = True
7131
7132Return the pickled representation of the object as a bytes object.
7133
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007134The optional *protocol* argument tells the pickler to use the given
7135protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7136protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007137
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007138Specifying a negative protocol version selects the highest protocol
7139version supported. The higher the protocol used, the more recent the
7140version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007141
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007142If *fix_imports* is True and *protocol* is less than 3, pickle will
7143try to map the new Python 3 names to the old module names used in
7144Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007145[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007146
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007147static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007148_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007149 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007150/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007151{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007152 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007153 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007154
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007155 if (pickler == NULL)
7156 return NULL;
7157
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007158 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007159 goto error;
7160
7161 if (dump(pickler, obj) < 0)
7162 goto error;
7163
7164 result = _Pickler_GetString(pickler);
7165 Py_DECREF(pickler);
7166 return result;
7167
7168 error:
7169 Py_XDECREF(pickler);
7170 return NULL;
7171}
7172
Larry Hastings61272b72014-01-07 12:41:53 -08007173/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007174
7175_pickle.load
7176
7177 file: object
7178 *
7179 fix_imports: bool = True
7180 encoding: str = 'ASCII'
7181 errors: str = 'strict'
7182
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007183Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007184
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007185This is equivalent to ``Unpickler(file).load()``, but may be more
7186efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007187
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007188The protocol version of the pickle is detected automatically, so no
7189protocol argument is needed. Bytes past the pickled object's
7190representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007191
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007192The argument *file* must have two methods, a read() method that takes
7193an integer argument, and a readline() method that requires no
7194arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007195binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007196other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007197
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007198Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007199which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007200generated by Python 2. If *fix_imports* is True, pickle will try to
7201map the old Python 2 names to the new names used in Python 3. The
7202*encoding* and *errors* tell pickle how to decode 8-bit string
7203instances pickled by Python 2; these default to 'ASCII' and 'strict',
7204respectively. The *encoding* can be 'bytes' to read these 8-bit
7205string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007206[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007207
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007208static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007209_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007210 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007211/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007212{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007213 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007214 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007215
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007216 if (unpickler == NULL)
7217 return NULL;
7218
7219 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7220 goto error;
7221
7222 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7223 goto error;
7224
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007225 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007226
7227 result = load(unpickler);
7228 Py_DECREF(unpickler);
7229 return result;
7230
7231 error:
7232 Py_XDECREF(unpickler);
7233 return NULL;
7234}
7235
Larry Hastings61272b72014-01-07 12:41:53 -08007236/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007237
7238_pickle.loads
7239
7240 data: object
7241 *
7242 fix_imports: bool = True
7243 encoding: str = 'ASCII'
7244 errors: str = 'strict'
7245
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007246Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007247
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007248The protocol version of the pickle is detected automatically, so no
7249protocol argument is needed. Bytes past the pickled object's
7250representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007251
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007252Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007253which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007254generated by Python 2. If *fix_imports* is True, pickle will try to
7255map the old Python 2 names to the new names used in Python 3. The
7256*encoding* and *errors* tell pickle how to decode 8-bit string
7257instances pickled by Python 2; these default to 'ASCII' and 'strict',
7258respectively. The *encoding* can be 'bytes' to read these 8-bit
7259string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007260[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007261
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007262static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007263_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007264 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007265/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007266{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007267 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007268 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007269
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007270 if (unpickler == NULL)
7271 return NULL;
7272
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007273 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007274 goto error;
7275
7276 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7277 goto error;
7278
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007279 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007280
7281 result = load(unpickler);
7282 Py_DECREF(unpickler);
7283 return result;
7284
7285 error:
7286 Py_XDECREF(unpickler);
7287 return NULL;
7288}
7289
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007290static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007291 _PICKLE_DUMP_METHODDEF
7292 _PICKLE_DUMPS_METHODDEF
7293 _PICKLE_LOAD_METHODDEF
7294 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007295 {NULL, NULL} /* sentinel */
7296};
7297
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007298static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007299pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007300{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007301 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007302 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007303}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007304
Stefan Krahf483b0f2013-12-14 13:43:10 +01007305static void
7306pickle_free(PyObject *m)
7307{
7308 _Pickle_ClearState(_Pickle_GetState(m));
7309}
7310
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007311static int
7312pickle_traverse(PyObject *m, visitproc visit, void *arg)
7313{
7314 PickleState *st = _Pickle_GetState(m);
7315 Py_VISIT(st->PickleError);
7316 Py_VISIT(st->PicklingError);
7317 Py_VISIT(st->UnpicklingError);
7318 Py_VISIT(st->dispatch_table);
7319 Py_VISIT(st->extension_registry);
7320 Py_VISIT(st->extension_cache);
7321 Py_VISIT(st->inverted_registry);
7322 Py_VISIT(st->name_mapping_2to3);
7323 Py_VISIT(st->import_mapping_2to3);
7324 Py_VISIT(st->name_mapping_3to2);
7325 Py_VISIT(st->import_mapping_3to2);
7326 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007327 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007328 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007329}
7330
7331static struct PyModuleDef _picklemodule = {
7332 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007333 "_pickle", /* m_name */
7334 pickle_module_doc, /* m_doc */
7335 sizeof(PickleState), /* m_size */
7336 pickle_methods, /* m_methods */
7337 NULL, /* m_reload */
7338 pickle_traverse, /* m_traverse */
7339 pickle_clear, /* m_clear */
7340 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007341};
7342
7343PyMODINIT_FUNC
7344PyInit__pickle(void)
7345{
7346 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007347 PickleState *st;
7348
7349 m = PyState_FindModule(&_picklemodule);
7350 if (m) {
7351 Py_INCREF(m);
7352 return m;
7353 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007354
7355 if (PyType_Ready(&Unpickler_Type) < 0)
7356 return NULL;
7357 if (PyType_Ready(&Pickler_Type) < 0)
7358 return NULL;
7359 if (PyType_Ready(&Pdata_Type) < 0)
7360 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007361 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7362 return NULL;
7363 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7364 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007365
7366 /* Create the module and add the functions. */
7367 m = PyModule_Create(&_picklemodule);
7368 if (m == NULL)
7369 return NULL;
7370
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007371 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007372 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7373 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007374 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007375 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7376 return NULL;
7377
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007378 st = _Pickle_GetState(m);
7379
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007380 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007381 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7382 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007383 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007384 st->PicklingError = \
7385 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7386 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007387 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007388 st->UnpicklingError = \
7389 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7390 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007391 return NULL;
7392
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007393 Py_INCREF(st->PickleError);
7394 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007395 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007396 Py_INCREF(st->PicklingError);
7397 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007398 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007399 Py_INCREF(st->UnpicklingError);
7400 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007401 return NULL;
7402
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007403 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007404 return NULL;
7405
7406 return m;
7407}