blob: 19f94dce66730afd19386cb94c933c5f37ba5c6b [file] [log] [blame]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001#include "Python.h"
2#include "structmember.h"
3
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004PyDoc_STRVAR(pickle_module_doc,
5"Optimized C implementation for the Python pickle module.");
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08008module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -08009class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
10class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
11class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
12class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080013[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030014/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000016/* Bump this when new opcodes are added to the pickle protocol. */
17enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010018 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000019 DEFAULT_PROTOCOL = 3
20};
21
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000022/* Pickle opcodes. These must be kept updated with pickle.py.
23 Extensive docs are in pickletools.py. */
24enum opcode {
25 MARK = '(',
26 STOP = '.',
27 POP = '0',
28 POP_MARK = '1',
29 DUP = '2',
30 FLOAT = 'F',
31 INT = 'I',
32 BININT = 'J',
33 BININT1 = 'K',
34 LONG = 'L',
35 BININT2 = 'M',
36 NONE = 'N',
37 PERSID = 'P',
38 BINPERSID = 'Q',
39 REDUCE = 'R',
40 STRING = 'S',
41 BINSTRING = 'T',
42 SHORT_BINSTRING = 'U',
43 UNICODE = 'V',
44 BINUNICODE = 'X',
45 APPEND = 'a',
46 BUILD = 'b',
47 GLOBAL = 'c',
48 DICT = 'd',
49 EMPTY_DICT = '}',
50 APPENDS = 'e',
51 GET = 'g',
52 BINGET = 'h',
53 INST = 'i',
54 LONG_BINGET = 'j',
55 LIST = 'l',
56 EMPTY_LIST = ']',
57 OBJ = 'o',
58 PUT = 'p',
59 BINPUT = 'q',
60 LONG_BINPUT = 'r',
61 SETITEM = 's',
62 TUPLE = 't',
63 EMPTY_TUPLE = ')',
64 SETITEMS = 'u',
65 BINFLOAT = 'G',
66
67 /* Protocol 2. */
68 PROTO = '\x80',
69 NEWOBJ = '\x81',
70 EXT1 = '\x82',
71 EXT2 = '\x83',
72 EXT4 = '\x84',
73 TUPLE1 = '\x85',
74 TUPLE2 = '\x86',
75 TUPLE3 = '\x87',
76 NEWTRUE = '\x88',
77 NEWFALSE = '\x89',
78 LONG1 = '\x8a',
79 LONG4 = '\x8b',
80
81 /* Protocol 3 (Python 3.x) */
82 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010083 SHORT_BINBYTES = 'C',
84
85 /* Protocol 4 */
86 SHORT_BINUNICODE = '\x8c',
87 BINUNICODE8 = '\x8d',
88 BINBYTES8 = '\x8e',
89 EMPTY_SET = '\x8f',
90 ADDITEMS = '\x90',
91 FROZENSET = '\x91',
92 NEWOBJ_EX = '\x92',
93 STACK_GLOBAL = '\x93',
94 MEMOIZE = '\x94',
95 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000096};
97
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000098enum {
99 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
100 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
101 break if this gets out of synch with pickle.py, but it's unclear that would
102 help anything either. */
103 BATCHSIZE = 1000,
104
105 /* Nesting limit until Pickler, when running in "fast mode", starts
106 checking for self-referential data-structures. */
107 FAST_NESTING_LIMIT = 50,
108
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000109 /* Initial size of the write buffer of Pickler. */
110 WRITE_BUF_SIZE = 4096,
111
Antoine Pitrou04248a82010-10-12 20:51:21 +0000112 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100113 PREFETCH = 8192 * 16,
114
115 FRAME_SIZE_TARGET = 64 * 1024,
116
117 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000118};
119
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800120/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000121
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800122/* State of the pickle module, per PEP 3121. */
123typedef struct {
124 /* Exception classes for pickle. */
125 PyObject *PickleError;
126 PyObject *PicklingError;
127 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129 /* copyreg.dispatch_table, {type_object: pickling_function} */
130 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000131
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800132 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800134 /* copyreg._extension_registry, {(module_name, function_name): code} */
135 PyObject *extension_registry;
136 /* copyreg._extension_cache, {code: object} */
137 PyObject *extension_cache;
138 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
139 PyObject *inverted_registry;
140
141 /* Import mappings for compatibility with Python 2.x */
142
143 /* _compat_pickle.NAME_MAPPING,
144 {(oldmodule, oldname): (newmodule, newname)} */
145 PyObject *name_mapping_2to3;
146 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
147 PyObject *import_mapping_2to3;
148 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
149 PyObject *name_mapping_3to2;
150 PyObject *import_mapping_3to2;
151
152 /* codecs.encode, used for saving bytes in older protocols */
153 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300154 /* builtins.getattr, used for saving nested names with protocol < 4 */
155 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300156 /* functools.partial, used for implementing __newobj_ex__ with protocols
157 2 and 3 */
158 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800159} PickleState;
160
161/* Forward declaration of the _pickle module definition. */
162static struct PyModuleDef _picklemodule;
163
164/* Given a module object, get its per-module state. */
165static PickleState *
166_Pickle_GetState(PyObject *module)
167{
168 return (PickleState *)PyModule_GetState(module);
169}
170
171/* Find the module instance imported in the currently running sub-interpreter
172 and get its state. */
173static PickleState *
174_Pickle_GetGlobalState(void)
175{
176 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
177}
178
179/* Clear the given pickle module state. */
180static void
181_Pickle_ClearState(PickleState *st)
182{
183 Py_CLEAR(st->PickleError);
184 Py_CLEAR(st->PicklingError);
185 Py_CLEAR(st->UnpicklingError);
186 Py_CLEAR(st->dispatch_table);
187 Py_CLEAR(st->extension_registry);
188 Py_CLEAR(st->extension_cache);
189 Py_CLEAR(st->inverted_registry);
190 Py_CLEAR(st->name_mapping_2to3);
191 Py_CLEAR(st->import_mapping_2to3);
192 Py_CLEAR(st->name_mapping_3to2);
193 Py_CLEAR(st->import_mapping_3to2);
194 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300195 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100196 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800197}
198
199/* Initialize the given pickle module state. */
200static int
201_Pickle_InitState(PickleState *st)
202{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300203 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204 PyObject *copyreg = NULL;
205 PyObject *compat_pickle = NULL;
206 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300207 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800208
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300209 builtins = PyEval_GetBuiltins();
210 if (builtins == NULL)
211 goto error;
212 st->getattr = PyDict_GetItemString(builtins, "getattr");
213 if (st->getattr == NULL)
214 goto error;
215 Py_INCREF(st->getattr);
216
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800217 copyreg = PyImport_ImportModule("copyreg");
218 if (!copyreg)
219 goto error;
220 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
221 if (!st->dispatch_table)
222 goto error;
223 if (!PyDict_CheckExact(st->dispatch_table)) {
224 PyErr_Format(PyExc_RuntimeError,
225 "copyreg.dispatch_table should be a dict, not %.200s",
226 Py_TYPE(st->dispatch_table)->tp_name);
227 goto error;
228 }
229 st->extension_registry = \
230 PyObject_GetAttrString(copyreg, "_extension_registry");
231 if (!st->extension_registry)
232 goto error;
233 if (!PyDict_CheckExact(st->extension_registry)) {
234 PyErr_Format(PyExc_RuntimeError,
235 "copyreg._extension_registry should be a dict, "
236 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
237 goto error;
238 }
239 st->inverted_registry = \
240 PyObject_GetAttrString(copyreg, "_inverted_registry");
241 if (!st->inverted_registry)
242 goto error;
243 if (!PyDict_CheckExact(st->inverted_registry)) {
244 PyErr_Format(PyExc_RuntimeError,
245 "copyreg._inverted_registry should be a dict, "
246 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
247 goto error;
248 }
249 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
250 if (!st->extension_cache)
251 goto error;
252 if (!PyDict_CheckExact(st->extension_cache)) {
253 PyErr_Format(PyExc_RuntimeError,
254 "copyreg._extension_cache should be a dict, "
255 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
256 goto error;
257 }
258 Py_CLEAR(copyreg);
259
260 /* Load the 2.x -> 3.x stdlib module mapping tables */
261 compat_pickle = PyImport_ImportModule("_compat_pickle");
262 if (!compat_pickle)
263 goto error;
264 st->name_mapping_2to3 = \
265 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
266 if (!st->name_mapping_2to3)
267 goto error;
268 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
269 PyErr_Format(PyExc_RuntimeError,
270 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
271 Py_TYPE(st->name_mapping_2to3)->tp_name);
272 goto error;
273 }
274 st->import_mapping_2to3 = \
275 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
276 if (!st->import_mapping_2to3)
277 goto error;
278 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
279 PyErr_Format(PyExc_RuntimeError,
280 "_compat_pickle.IMPORT_MAPPING should be a dict, "
281 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
282 goto error;
283 }
284 /* ... and the 3.x -> 2.x mapping tables */
285 st->name_mapping_3to2 = \
286 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
287 if (!st->name_mapping_3to2)
288 goto error;
289 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
290 PyErr_Format(PyExc_RuntimeError,
291 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
292 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
293 goto error;
294 }
295 st->import_mapping_3to2 = \
296 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
297 if (!st->import_mapping_3to2)
298 goto error;
299 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
300 PyErr_Format(PyExc_RuntimeError,
301 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
302 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
303 goto error;
304 }
305 Py_CLEAR(compat_pickle);
306
307 codecs = PyImport_ImportModule("codecs");
308 if (codecs == NULL)
309 goto error;
310 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
311 if (st->codecs_encode == NULL) {
312 goto error;
313 }
314 if (!PyCallable_Check(st->codecs_encode)) {
315 PyErr_Format(PyExc_RuntimeError,
316 "codecs.encode should be a callable, not %.200s",
317 Py_TYPE(st->codecs_encode)->tp_name);
318 goto error;
319 }
320 Py_CLEAR(codecs);
321
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300322 functools = PyImport_ImportModule("functools");
323 if (!functools)
324 goto error;
325 st->partial = PyObject_GetAttrString(functools, "partial");
326 if (!st->partial)
327 goto error;
328 Py_CLEAR(functools);
329
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800330 return 0;
331
332 error:
333 Py_CLEAR(copyreg);
334 Py_CLEAR(compat_pickle);
335 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300336 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 _Pickle_ClearState(st);
338 return -1;
339}
340
341/* Helper for calling a function with a single argument quickly.
342
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800343 This function steals the reference of the given argument. */
344static PyObject *
345_Pickle_FastCall(PyObject *func, PyObject *obj)
346{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800347 PyObject *result;
348
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100349 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200350 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800351 return result;
352}
353
354/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000355
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000356/* Internal data type used as the unpickling stack. */
357typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000358 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000359 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200360 int mark_set; /* is MARK set? */
361 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000362 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000363} Pdata;
364
365static void
366Pdata_dealloc(Pdata *self)
367{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200368 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000369 while (--i >= 0) {
370 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000371 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000372 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000373 PyObject_Del(self);
374}
375
376static PyTypeObject Pdata_Type = {
377 PyVarObject_HEAD_INIT(NULL, 0)
378 "_pickle.Pdata", /*tp_name*/
379 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200380 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000381 (destructor)Pdata_dealloc, /*tp_dealloc*/
382};
383
384static PyObject *
385Pdata_New(void)
386{
387 Pdata *self;
388
389 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
390 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000391 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200392 self->mark_set = 0;
393 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000394 self->allocated = 8;
395 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000396 if (self->data)
397 return (PyObject *)self;
398 Py_DECREF(self);
399 return PyErr_NoMemory();
400}
401
402
403/* Retain only the initial clearto items. If clearto >= the current
404 * number of items, this is a (non-erroneous) NOP.
405 */
406static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200407Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000408{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200409 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000410
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200411 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000412 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000413 return 0;
414
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000415 while (--i >= clearto) {
416 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000417 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000418 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000419 return 0;
420}
421
422static int
423Pdata_grow(Pdata *self)
424{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000425 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200426 size_t allocated = (size_t)self->allocated;
427 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000428
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000429 new_allocated = (allocated >> 3) + 6;
430 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200431 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000432 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000433 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500434 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000435 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000437
438 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200439 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000440 return 0;
441
442 nomemory:
443 PyErr_NoMemory();
444 return -1;
445}
446
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200447static int
448Pdata_stack_underflow(Pdata *self)
449{
450 PickleState *st = _Pickle_GetGlobalState();
451 PyErr_SetString(st->UnpicklingError,
452 self->mark_set ?
453 "unexpected MARK found" :
454 "unpickling stack underflow");
455 return -1;
456}
457
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000458/* D is a Pdata*. Pop the topmost element and store it into V, which
459 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
460 * is raised and V is set to NULL.
461 */
462static PyObject *
463Pdata_pop(Pdata *self)
464{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200465 if (Py_SIZE(self) <= self->fence) {
466 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000467 return NULL;
468 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000469 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000470}
471#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
472
473static int
474Pdata_push(Pdata *self, PyObject *obj)
475{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000476 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477 return -1;
478 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000479 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 return 0;
481}
482
483/* Push an object on stack, transferring its ownership to the stack. */
484#define PDATA_PUSH(D, O, ER) do { \
485 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
486
487/* Push an object on stack, adding a new reference to the object. */
488#define PDATA_APPEND(D, O, ER) do { \
489 Py_INCREF((O)); \
490 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
491
492static PyObject *
493Pdata_poptuple(Pdata *self, Py_ssize_t start)
494{
495 PyObject *tuple;
496 Py_ssize_t len, i, j;
497
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200498 if (start < self->fence) {
499 Pdata_stack_underflow(self);
500 return NULL;
501 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000502 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000503 tuple = PyTuple_New(len);
504 if (tuple == NULL)
505 return NULL;
506 for (i = start, j = 0; j < len; i++, j++)
507 PyTuple_SET_ITEM(tuple, j, self->data[i]);
508
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000509 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000510 return tuple;
511}
512
513static PyObject *
514Pdata_poplist(Pdata *self, Py_ssize_t start)
515{
516 PyObject *list;
517 Py_ssize_t len, i, j;
518
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000519 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000520 list = PyList_New(len);
521 if (list == NULL)
522 return NULL;
523 for (i = start, j = 0; j < len; i++, j++)
524 PyList_SET_ITEM(list, j, self->data[i]);
525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000526 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000527 return list;
528}
529
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000530typedef struct {
531 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200532 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000533} PyMemoEntry;
534
535typedef struct {
536 Py_ssize_t mt_mask;
537 Py_ssize_t mt_used;
538 Py_ssize_t mt_allocated;
539 PyMemoEntry *mt_table;
540} PyMemoTable;
541
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000542typedef struct PicklerObject {
543 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000544 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000545 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000546 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000547 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100548 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000549
550 PyObject *write; /* write() method of the output stream. */
551 PyObject *output_buffer; /* Write into a local bytearray buffer before
552 flushing to the stream. */
553 Py_ssize_t output_len; /* Length of output_buffer. */
554 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000555 int proto; /* Pickle protocol number, >= 0 */
556 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100557 int framing; /* True when framing is enabled, proto >= 4 */
558 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000559 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100560 is no frame currently open. */
561
562 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000563 int fast; /* Enable fast mode if set to a true value.
564 The fast mode disable the usage of memo,
565 therefore speeding the pickling process by
566 not generating superfluous PUT opcodes. It
567 should not be used if with self-referential
568 objects. */
569 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000570 int fix_imports; /* Indicate whether Pickler should fix
571 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000572 PyObject *fast_memo;
573} PicklerObject;
574
575typedef struct UnpicklerObject {
576 PyObject_HEAD
577 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000578
579 /* The unpickler memo is just an array of PyObject *s. Using a dict
580 is unnecessary, since the keys are contiguous ints. */
581 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100582 Py_ssize_t memo_size; /* Capacity of the memo array */
583 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000584
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000585 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000586
587 Py_buffer buffer;
588 char *input_buffer;
589 char *input_line;
590 Py_ssize_t input_len;
591 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000592 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100593
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000594 PyObject *read; /* read() method of the input stream. */
595 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000596 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000597
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000598 char *encoding; /* Name of the encoding to be used for
599 decoding strings pickled using Python
600 2.x. The default value is "ASCII" */
601 char *errors; /* Name of errors handling scheme to used when
602 decoding strings. The default value is
603 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500604 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000605 objects. */
606 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
607 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000608 int proto; /* Protocol of the pickle loaded. */
609 int fix_imports; /* Indicate whether Unpickler should fix
610 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000611} UnpicklerObject;
612
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200613typedef struct {
614 PyObject_HEAD
615 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
616} PicklerMemoProxyObject;
617
618typedef struct {
619 PyObject_HEAD
620 UnpicklerObject *unpickler;
621} UnpicklerMemoProxyObject;
622
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000623/* Forward declarations */
624static int save(PicklerObject *, PyObject *, int);
625static int save_reduce(PicklerObject *, PyObject *, PyObject *);
626static PyTypeObject Pickler_Type;
627static PyTypeObject Unpickler_Type;
628
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200629#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000630
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000631/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300632 A custom hashtable mapping void* to Python ints. This is used by the pickler
633 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000634 a bunch of unnecessary object creation. This makes a huge performance
635 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000636
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000637#define MT_MINSIZE 8
638#define PERTURB_SHIFT 5
639
640
641static PyMemoTable *
642PyMemoTable_New(void)
643{
644 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
645 if (memo == NULL) {
646 PyErr_NoMemory();
647 return NULL;
648 }
649
650 memo->mt_used = 0;
651 memo->mt_allocated = MT_MINSIZE;
652 memo->mt_mask = MT_MINSIZE - 1;
653 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
654 if (memo->mt_table == NULL) {
655 PyMem_FREE(memo);
656 PyErr_NoMemory();
657 return NULL;
658 }
659 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
660
661 return memo;
662}
663
664static PyMemoTable *
665PyMemoTable_Copy(PyMemoTable *self)
666{
667 Py_ssize_t i;
668 PyMemoTable *new = PyMemoTable_New();
669 if (new == NULL)
670 return NULL;
671
672 new->mt_used = self->mt_used;
673 new->mt_allocated = self->mt_allocated;
674 new->mt_mask = self->mt_mask;
675 /* The table we get from _New() is probably smaller than we wanted.
676 Free it and allocate one that's the right size. */
677 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500678 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000679 if (new->mt_table == NULL) {
680 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200681 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000682 return NULL;
683 }
684 for (i = 0; i < self->mt_allocated; i++) {
685 Py_XINCREF(self->mt_table[i].me_key);
686 }
687 memcpy(new->mt_table, self->mt_table,
688 sizeof(PyMemoEntry) * self->mt_allocated);
689
690 return new;
691}
692
693static Py_ssize_t
694PyMemoTable_Size(PyMemoTable *self)
695{
696 return self->mt_used;
697}
698
699static int
700PyMemoTable_Clear(PyMemoTable *self)
701{
702 Py_ssize_t i = self->mt_allocated;
703
704 while (--i >= 0) {
705 Py_XDECREF(self->mt_table[i].me_key);
706 }
707 self->mt_used = 0;
708 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
709 return 0;
710}
711
712static void
713PyMemoTable_Del(PyMemoTable *self)
714{
715 if (self == NULL)
716 return;
717 PyMemoTable_Clear(self);
718
719 PyMem_FREE(self->mt_table);
720 PyMem_FREE(self);
721}
722
723/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
724 can be considerably simpler than dictobject.c's lookdict(). */
725static PyMemoEntry *
726_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
727{
728 size_t i;
729 size_t perturb;
730 size_t mask = (size_t)self->mt_mask;
731 PyMemoEntry *table = self->mt_table;
732 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000733 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000734
735 i = hash & mask;
736 entry = &table[i];
737 if (entry->me_key == NULL || entry->me_key == key)
738 return entry;
739
740 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
741 i = (i << 2) + i + perturb + 1;
742 entry = &table[i & mask];
743 if (entry->me_key == NULL || entry->me_key == key)
744 return entry;
745 }
746 assert(0); /* Never reached */
747 return NULL;
748}
749
750/* Returns -1 on failure, 0 on success. */
751static int
752_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
753{
754 PyMemoEntry *oldtable = NULL;
755 PyMemoEntry *oldentry, *newentry;
756 Py_ssize_t new_size = MT_MINSIZE;
757 Py_ssize_t to_process;
758
759 assert(min_size > 0);
760
761 /* Find the smallest valid table size >= min_size. */
762 while (new_size < min_size && new_size > 0)
763 new_size <<= 1;
764 if (new_size <= 0) {
765 PyErr_NoMemory();
766 return -1;
767 }
768 /* new_size needs to be a power of two. */
769 assert((new_size & (new_size - 1)) == 0);
770
771 /* Allocate new table. */
772 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500773 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000774 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200775 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000776 PyErr_NoMemory();
777 return -1;
778 }
779 self->mt_allocated = new_size;
780 self->mt_mask = new_size - 1;
781 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
782
783 /* Copy entries from the old table. */
784 to_process = self->mt_used;
785 for (oldentry = oldtable; to_process > 0; oldentry++) {
786 if (oldentry->me_key != NULL) {
787 to_process--;
788 /* newentry is a pointer to a chunk of the new
789 mt_table, so we're setting the key:value pair
790 in-place. */
791 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
792 newentry->me_key = oldentry->me_key;
793 newentry->me_value = oldentry->me_value;
794 }
795 }
796
797 /* Deallocate the old table. */
798 PyMem_FREE(oldtable);
799 return 0;
800}
801
802/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200803static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000804PyMemoTable_Get(PyMemoTable *self, PyObject *key)
805{
806 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
807 if (entry->me_key == NULL)
808 return NULL;
809 return &entry->me_value;
810}
811
812/* Returns -1 on failure, 0 on success. */
813static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200814PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000815{
816 PyMemoEntry *entry;
817
818 assert(key != NULL);
819
820 entry = _PyMemoTable_Lookup(self, key);
821 if (entry->me_key != NULL) {
822 entry->me_value = value;
823 return 0;
824 }
825 Py_INCREF(key);
826 entry->me_key = key;
827 entry->me_value = value;
828 self->mt_used++;
829
830 /* If we added a key, we can safely resize. Otherwise just return!
831 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
832 *
833 * Quadrupling the size improves average table sparseness
834 * (reducing collisions) at the cost of some memory. It also halves
835 * the number of expensive resize operations in a growing memo table.
836 *
837 * Very large memo tables (over 50K items) use doubling instead.
838 * This may help applications with severe memory constraints.
839 */
840 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
841 return 0;
842 return _PyMemoTable_ResizeTable(self,
843 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
844}
845
846#undef MT_MINSIZE
847#undef PERTURB_SHIFT
848
849/*************************************************************************/
850
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000851
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000852static int
853_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000854{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300855 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200856 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000857 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000858 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000859 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100860 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000861 return 0;
862}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000863
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100864static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100865_write_size64(char *out, size_t value)
866{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200867 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800868
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200869 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800870
871 for (i = 0; i < sizeof(size_t); i++) {
872 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
873 }
874 for (i = sizeof(size_t); i < 8; i++) {
875 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800876 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100877}
878
879static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100880_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
881{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100882 qdata[0] = FRAME;
883 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100884}
885
886static int
887_Pickler_CommitFrame(PicklerObject *self)
888{
889 size_t frame_len;
890 char *qdata;
891
892 if (!self->framing || self->frame_start == -1)
893 return 0;
894 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
895 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
896 _Pickler_WriteFrameHeader(self, qdata, frame_len);
897 self->frame_start = -1;
898 return 0;
899}
900
901static int
902_Pickler_OpcodeBoundary(PicklerObject *self)
903{
904 Py_ssize_t frame_len;
905
906 if (!self->framing || self->frame_start == -1)
907 return 0;
908 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
909 if (frame_len >= FRAME_SIZE_TARGET)
910 return _Pickler_CommitFrame(self);
911 else
912 return 0;
913}
914
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000915static PyObject *
916_Pickler_GetString(PicklerObject *self)
917{
918 PyObject *output_buffer = self->output_buffer;
919
920 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100921
922 if (_Pickler_CommitFrame(self))
923 return NULL;
924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000925 self->output_buffer = NULL;
926 /* Resize down to exact size */
927 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
928 return NULL;
929 return output_buffer;
930}
931
932static int
933_Pickler_FlushToFile(PicklerObject *self)
934{
935 PyObject *output, *result;
936
937 assert(self->write != NULL);
938
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100939 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000940 output = _Pickler_GetString(self);
941 if (output == NULL)
942 return -1;
943
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800944 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000945 Py_XDECREF(result);
946 return (result == NULL) ? -1 : 0;
947}
948
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200949static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100950_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000951{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100952 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000953 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100954 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000955
956 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100957 need_new_frame = (self->framing && self->frame_start == -1);
958
959 if (need_new_frame)
960 n = data_len + FRAME_HEADER_SIZE;
961 else
962 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000963
964 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100965 if (required > self->max_output_len) {
966 /* Make place in buffer for the pickle chunk */
967 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
968 PyErr_NoMemory();
969 return -1;
970 }
971 self->max_output_len = (self->output_len + n) / 2 * 3;
972 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
973 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000974 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000975 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100976 if (need_new_frame) {
977 /* Setup new frame */
978 Py_ssize_t frame_start = self->output_len;
979 self->frame_start = frame_start;
980 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
981 /* Write an invalid value, for debugging */
982 buffer[frame_start + i] = 0xFE;
983 }
984 self->output_len += FRAME_HEADER_SIZE;
985 }
986 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000987 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100988 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000989 buffer[self->output_len + i] = s[i];
990 }
991 }
992 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100993 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000994 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100995 self->output_len += data_len;
996 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000997}
998
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999static PicklerObject *
1000_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001001{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001002 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001003
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001004 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1005 if (self == NULL)
1006 return NULL;
1007
1008 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001009 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001010 self->write = NULL;
1011 self->proto = 0;
1012 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001013 self->framing = 0;
1014 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001015 self->fast = 0;
1016 self->fast_nesting = 0;
1017 self->fix_imports = 0;
1018 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001019 self->max_output_len = WRITE_BUF_SIZE;
1020 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001021
1022 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001023 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1024 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001025
1026 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001027 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001028 return NULL;
1029 }
1030 return self;
1031}
1032
1033static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001034_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001035{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001036 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001037
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001038 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001039 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001040 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001042 proto = PyLong_AsLong(protocol);
1043 if (proto < 0) {
1044 if (proto == -1 && PyErr_Occurred())
1045 return -1;
1046 proto = HIGHEST_PROTOCOL;
1047 }
1048 else if (proto > HIGHEST_PROTOCOL) {
1049 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1050 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001051 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001052 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001053 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001054 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001055 self->bin = proto > 0;
1056 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001057 return 0;
1058}
1059
1060/* Returns -1 (with an exception set) on failure, 0 on success. This may
1061 be called once on a freshly created Pickler. */
1062static int
1063_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1064{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001065 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001066 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001067 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001068 if (self->write == NULL) {
1069 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1070 PyErr_SetString(PyExc_TypeError,
1071 "file must have a 'write' attribute");
1072 return -1;
1073 }
1074
1075 return 0;
1076}
1077
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001078/* Returns the size of the input on success, -1 on failure. This takes its
1079 own reference to `input`. */
1080static Py_ssize_t
1081_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1082{
1083 if (self->buffer.buf != NULL)
1084 PyBuffer_Release(&self->buffer);
1085 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1086 return -1;
1087 self->input_buffer = self->buffer.buf;
1088 self->input_len = self->buffer.len;
1089 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001090 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001091 return self->input_len;
1092}
1093
Antoine Pitrou04248a82010-10-12 20:51:21 +00001094static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001095bad_readline(void)
1096{
1097 PickleState *st = _Pickle_GetGlobalState();
1098 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1099 return -1;
1100}
1101
1102static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001103_Unpickler_SkipConsumed(UnpicklerObject *self)
1104{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001105 Py_ssize_t consumed;
1106 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001107
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001108 consumed = self->next_read_idx - self->prefetched_idx;
1109 if (consumed <= 0)
1110 return 0;
1111
1112 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001113 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001114 r = PyObject_CallFunction(self->read, "n", consumed);
1115 if (r == NULL)
1116 return -1;
1117 Py_DECREF(r);
1118
1119 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001120 return 0;
1121}
1122
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001123static const Py_ssize_t READ_WHOLE_LINE = -1;
1124
1125/* If reading from a file, we need to only pull the bytes we need, since there
1126 may be multiple pickle objects arranged contiguously in the same input
1127 buffer.
1128
1129 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1130 bytes from the input stream/buffer.
1131
1132 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1133 failure; on success, returns the number of bytes read from the file.
1134
1135 On success, self->input_len will be 0; this is intentional so that when
1136 unpickling from a file, the "we've run out of data" code paths will trigger,
1137 causing the Unpickler to go back to the file for more data. Use the returned
1138 size to tell you how much data you can process. */
1139static Py_ssize_t
1140_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1141{
1142 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001143 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144
1145 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001146
Antoine Pitrou04248a82010-10-12 20:51:21 +00001147 if (_Unpickler_SkipConsumed(self) < 0)
1148 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001149
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001150 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001151 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001152 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001153 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001154 PyObject *len;
1155 /* Prefetch some data without advancing the file pointer, if possible */
1156 if (self->peek && n < PREFETCH) {
1157 len = PyLong_FromSsize_t(PREFETCH);
1158 if (len == NULL)
1159 return -1;
1160 data = _Pickle_FastCall(self->peek, len);
1161 if (data == NULL) {
1162 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1163 return -1;
1164 /* peek() is probably not supported by the given file object */
1165 PyErr_Clear();
1166 Py_CLEAR(self->peek);
1167 }
1168 else {
1169 read_size = _Unpickler_SetStringInput(self, data);
1170 Py_DECREF(data);
1171 self->prefetched_idx = 0;
1172 if (n <= read_size)
1173 return n;
1174 }
1175 }
1176 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001177 if (len == NULL)
1178 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001179 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001180 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001181 if (data == NULL)
1182 return -1;
1183
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001184 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001185 Py_DECREF(data);
1186 return read_size;
1187}
1188
Victor Stinner19ed27e2016-05-20 11:42:37 +02001189/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001190static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001191_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001192{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001193 Py_ssize_t num_read;
1194
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001195 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001196 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1197 PickleState *st = _Pickle_GetGlobalState();
1198 PyErr_SetString(st->UnpicklingError,
1199 "read would overflow (invalid bytecode)");
1200 return -1;
1201 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001202
1203 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1204 assert(self->next_read_idx + n > self->input_len);
1205
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001206 if (!self->read)
1207 return bad_readline();
1208
Antoine Pitrou04248a82010-10-12 20:51:21 +00001209 num_read = _Unpickler_ReadFromFile(self, n);
1210 if (num_read < 0)
1211 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001212 if (num_read < n)
1213 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001214 *s = self->input_buffer;
1215 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001216 return n;
1217}
1218
Victor Stinner19ed27e2016-05-20 11:42:37 +02001219/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1220
1221 This should be used for all data reads, rather than accessing the unpickler's
1222 input buffer directly. This method deals correctly with reading from input
1223 streams, which the input buffer doesn't deal with.
1224
1225 Note that when reading from a file-like object, self->next_read_idx won't
1226 be updated (it should remain at 0 for the entire unpickling process). You
1227 should use this function's return value to know how many bytes you can
1228 consume.
1229
1230 Returns -1 (with an exception set) on failure. On success, return the
1231 number of chars read. */
1232#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001233 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001234 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1235 (self)->next_read_idx += (n), \
1236 (n)) \
1237 : _Unpickler_ReadImpl(self, (s), (n)))
1238
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001239static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001240_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1241 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001242{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001243 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001244 if (input_line == NULL) {
1245 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001246 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001247 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001248
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001249 memcpy(input_line, line, len);
1250 input_line[len] = '\0';
1251 self->input_line = input_line;
1252 *result = self->input_line;
1253 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001254}
1255
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001256/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001257 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001258
1259 Returns the number of chars read, or -1 on failure. */
1260static Py_ssize_t
1261_Unpickler_Readline(UnpicklerObject *self, char **result)
1262{
1263 Py_ssize_t i, num_read;
1264
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001265 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001266 if (self->input_buffer[i] == '\n') {
1267 char *line_start = self->input_buffer + self->next_read_idx;
1268 num_read = i - self->next_read_idx + 1;
1269 self->next_read_idx = i + 1;
1270 return _Unpickler_CopyLine(self, line_start, num_read, result);
1271 }
1272 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001273 if (!self->read)
1274 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001275
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001276 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1277 if (num_read < 0)
1278 return -1;
1279 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1280 return bad_readline();
1281 self->next_read_idx = num_read;
1282 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001283}
1284
1285/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1286 will be modified in place. */
1287static int
1288_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1289{
1290 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001291
1292 assert(new_size > self->memo_size);
1293
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001294 PyMem_RESIZE(self->memo, PyObject *, new_size);
1295 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001296 PyErr_NoMemory();
1297 return -1;
1298 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001299 for (i = self->memo_size; i < new_size; i++)
1300 self->memo[i] = NULL;
1301 self->memo_size = new_size;
1302 return 0;
1303}
1304
1305/* Returns NULL if idx is out of bounds. */
1306static PyObject *
1307_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1308{
1309 if (idx < 0 || idx >= self->memo_size)
1310 return NULL;
1311
1312 return self->memo[idx];
1313}
1314
1315/* Returns -1 (with an exception set) on failure, 0 on success.
1316 This takes its own reference to `value`. */
1317static int
1318_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1319{
1320 PyObject *old_item;
1321
1322 if (idx >= self->memo_size) {
1323 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1324 return -1;
1325 assert(idx < self->memo_size);
1326 }
1327 Py_INCREF(value);
1328 old_item = self->memo[idx];
1329 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001330 if (old_item != NULL) {
1331 Py_DECREF(old_item);
1332 }
1333 else {
1334 self->memo_len++;
1335 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001336 return 0;
1337}
1338
1339static PyObject **
1340_Unpickler_NewMemo(Py_ssize_t new_size)
1341{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001342 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001343 if (memo == NULL) {
1344 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001345 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001346 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001347 memset(memo, 0, new_size * sizeof(PyObject *));
1348 return memo;
1349}
1350
1351/* Free the unpickler's memo, taking care to decref any items left in it. */
1352static void
1353_Unpickler_MemoCleanup(UnpicklerObject *self)
1354{
1355 Py_ssize_t i;
1356 PyObject **memo = self->memo;
1357
1358 if (self->memo == NULL)
1359 return;
1360 self->memo = NULL;
1361 i = self->memo_size;
1362 while (--i >= 0) {
1363 Py_XDECREF(memo[i]);
1364 }
1365 PyMem_FREE(memo);
1366}
1367
1368static UnpicklerObject *
1369_Unpickler_New(void)
1370{
1371 UnpicklerObject *self;
1372
1373 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1374 if (self == NULL)
1375 return NULL;
1376
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001377 self->pers_func = NULL;
1378 self->input_buffer = NULL;
1379 self->input_line = NULL;
1380 self->input_len = 0;
1381 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001382 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001383 self->read = NULL;
1384 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001385 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001386 self->encoding = NULL;
1387 self->errors = NULL;
1388 self->marks = NULL;
1389 self->num_marks = 0;
1390 self->marks_size = 0;
1391 self->proto = 0;
1392 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001393 memset(&self->buffer, 0, sizeof(Py_buffer));
1394 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001395 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001396 self->memo = _Unpickler_NewMemo(self->memo_size);
1397 self->stack = (Pdata *)Pdata_New();
1398
1399 if (self->memo == NULL || self->stack == NULL) {
1400 Py_DECREF(self);
1401 return NULL;
1402 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001403
1404 return self;
1405}
1406
1407/* Returns -1 (with an exception set) on failure, 0 on success. This may
1408 be called once on a freshly created Pickler. */
1409static int
1410_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1411{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001412 _Py_IDENTIFIER(peek);
1413 _Py_IDENTIFIER(read);
1414 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001415
1416 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001417 if (self->peek == NULL) {
1418 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1419 PyErr_Clear();
1420 else
1421 return -1;
1422 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001423 self->read = _PyObject_GetAttrId(file, &PyId_read);
1424 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001425 if (self->readline == NULL || self->read == NULL) {
1426 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1427 PyErr_SetString(PyExc_TypeError,
1428 "file must have 'read' and 'readline' attributes");
1429 Py_CLEAR(self->read);
1430 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001431 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001432 return -1;
1433 }
1434 return 0;
1435}
1436
1437/* Returns -1 (with an exception set) on failure, 0 on success. This may
1438 be called once on a freshly created Pickler. */
1439static int
1440_Unpickler_SetInputEncoding(UnpicklerObject *self,
1441 const char *encoding,
1442 const char *errors)
1443{
1444 if (encoding == NULL)
1445 encoding = "ASCII";
1446 if (errors == NULL)
1447 errors = "strict";
1448
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001449 self->encoding = _PyMem_Strdup(encoding);
1450 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001451 if (self->encoding == NULL || self->errors == NULL) {
1452 PyErr_NoMemory();
1453 return -1;
1454 }
1455 return 0;
1456}
1457
1458/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001459static int
1460memo_get(PicklerObject *self, PyObject *key)
1461{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001462 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001463 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001464 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001465
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001466 value = PyMemoTable_Get(self->memo, key);
1467 if (value == NULL) {
1468 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001469 return -1;
1470 }
1471
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001472 if (!self->bin) {
1473 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001474 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1475 "%" PY_FORMAT_SIZE_T "d\n", *value);
1476 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001477 }
1478 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001479 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001480 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001481 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001482 len = 2;
1483 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001484 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001485 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001486 pdata[1] = (unsigned char)(*value & 0xff);
1487 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1488 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1489 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001490 len = 5;
1491 }
1492 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001493 PickleState *st = _Pickle_GetGlobalState();
1494 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001495 "memo id too large for LONG_BINGET");
1496 return -1;
1497 }
1498 }
1499
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001500 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001501 return -1;
1502
1503 return 0;
1504}
1505
1506/* Store an object in the memo, assign it a new unique ID based on the number
1507 of objects currently stored in the memo and generate a PUT opcode. */
1508static int
1509memo_put(PicklerObject *self, PyObject *obj)
1510{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001511 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001512 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001513 Py_ssize_t idx;
1514
1515 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001516
1517 if (self->fast)
1518 return 0;
1519
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001520 idx = PyMemoTable_Size(self->memo);
1521 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1522 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001523
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001524 if (self->proto >= 4) {
1525 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1526 return -1;
1527 return 0;
1528 }
1529 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001530 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001531 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001532 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001533 len = strlen(pdata);
1534 }
1535 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001536 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001537 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001538 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001539 len = 2;
1540 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001541 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001542 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001543 pdata[1] = (unsigned char)(idx & 0xff);
1544 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1545 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1546 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001547 len = 5;
1548 }
1549 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001550 PickleState *st = _Pickle_GetGlobalState();
1551 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001552 "memo id too large for LONG_BINPUT");
1553 return -1;
1554 }
1555 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001556 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001557 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001559 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001560}
1561
1562static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001563get_dotted_path(PyObject *obj, PyObject *name)
1564{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001565 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001566 PyObject *dotted_path;
1567 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001568
1569 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001570 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001571 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001572 n = PyList_GET_SIZE(dotted_path);
1573 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001574 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001575 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001576 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001577 if (obj == NULL)
1578 PyErr_Format(PyExc_AttributeError,
1579 "Can't pickle local object %R", name);
1580 else
1581 PyErr_Format(PyExc_AttributeError,
1582 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001583 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001584 return NULL;
1585 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001586 }
1587 return dotted_path;
1588}
1589
1590static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001591get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001592{
1593 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001594 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001595
1596 assert(PyList_CheckExact(names));
1597 Py_INCREF(obj);
1598 n = PyList_GET_SIZE(names);
1599 for (i = 0; i < n; i++) {
1600 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001601 Py_XDECREF(parent);
1602 parent = obj;
1603 obj = PyObject_GetAttr(parent, name);
1604 if (obj == NULL) {
1605 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001606 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001607 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001608 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001609 if (pparent != NULL)
1610 *pparent = parent;
1611 else
1612 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001613 return obj;
1614}
1615
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001616static void
1617reformat_attribute_error(PyObject *obj, PyObject *name)
1618{
1619 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1620 PyErr_Clear();
1621 PyErr_Format(PyExc_AttributeError,
1622 "Can't get attribute %R on %R", name, obj);
1623 }
1624}
1625
1626
1627static PyObject *
1628getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1629{
1630 PyObject *dotted_path, *attr;
1631
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001632 if (allow_qualname) {
1633 dotted_path = get_dotted_path(obj, name);
1634 if (dotted_path == NULL)
1635 return NULL;
1636 attr = get_deep_attribute(obj, dotted_path, NULL);
1637 Py_DECREF(dotted_path);
1638 }
1639 else
1640 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001641 if (attr == NULL)
1642 reformat_attribute_error(obj, name);
1643 return attr;
1644}
1645
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001646static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001647whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001648{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649 PyObject *module_name;
1650 PyObject *modules_dict;
1651 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001652 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001653 _Py_IDENTIFIER(__module__);
1654 _Py_IDENTIFIER(modules);
1655 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001656
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001657 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1658
1659 if (module_name == NULL) {
1660 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001661 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001662 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001663 }
1664 else {
1665 /* In some rare cases (e.g., bound methods of extension types),
1666 __module__ can be None. If it is so, then search sys.modules for
1667 the module of global. */
1668 if (module_name != Py_None)
1669 return module_name;
1670 Py_CLEAR(module_name);
1671 }
1672 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001673
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001674 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001675 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001676 if (modules_dict == NULL) {
1677 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001678 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001679 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001680
1681 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001682 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1683 PyObject *candidate;
1684 if (PyUnicode_Check(module_name) &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001685 _PyUnicode_EqualToASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001686 continue;
1687 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001688 continue;
1689
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001690 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001691 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001692 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001693 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001694 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 continue;
1696 }
1697
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001698 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001699 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001700 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001701 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001702 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001703 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001704 }
1705
1706 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001707 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001708 Py_INCREF(module_name);
1709 return module_name;
1710}
1711
1712/* fast_save_enter() and fast_save_leave() are guards against recursive
1713 objects when Pickler is used with the "fast mode" (i.e., with object
1714 memoization disabled). If the nesting of a list or dict object exceed
1715 FAST_NESTING_LIMIT, these guards will start keeping an internal
1716 reference to the seen list or dict objects and check whether these objects
1717 are recursive. These are not strictly necessary, since save() has a
1718 hard-coded recursion limit, but they give a nicer error message than the
1719 typical RuntimeError. */
1720static int
1721fast_save_enter(PicklerObject *self, PyObject *obj)
1722{
1723 /* if fast_nesting < 0, we're doing an error exit. */
1724 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1725 PyObject *key = NULL;
1726 if (self->fast_memo == NULL) {
1727 self->fast_memo = PyDict_New();
1728 if (self->fast_memo == NULL) {
1729 self->fast_nesting = -1;
1730 return 0;
1731 }
1732 }
1733 key = PyLong_FromVoidPtr(obj);
1734 if (key == NULL)
1735 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001736 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001737 Py_DECREF(key);
1738 PyErr_Format(PyExc_ValueError,
1739 "fast mode: can't pickle cyclic objects "
1740 "including object type %.200s at %p",
1741 obj->ob_type->tp_name, obj);
1742 self->fast_nesting = -1;
1743 return 0;
1744 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001745 if (PyErr_Occurred()) {
1746 return 0;
1747 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001748 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1749 Py_DECREF(key);
1750 self->fast_nesting = -1;
1751 return 0;
1752 }
1753 Py_DECREF(key);
1754 }
1755 return 1;
1756}
1757
1758static int
1759fast_save_leave(PicklerObject *self, PyObject *obj)
1760{
1761 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1762 PyObject *key = PyLong_FromVoidPtr(obj);
1763 if (key == NULL)
1764 return 0;
1765 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1766 Py_DECREF(key);
1767 return 0;
1768 }
1769 Py_DECREF(key);
1770 }
1771 return 1;
1772}
1773
1774static int
1775save_none(PicklerObject *self, PyObject *obj)
1776{
1777 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001778 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001779 return -1;
1780
1781 return 0;
1782}
1783
1784static int
1785save_bool(PicklerObject *self, PyObject *obj)
1786{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001787 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001788 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001789 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001790 return -1;
1791 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001792 else {
1793 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1794 * so that unpicklers written before bools were introduced unpickle them
1795 * as ints, but unpicklers after can recognize that bools were intended.
1796 * Note that protocol 2 added direct ways to pickle bools.
1797 */
1798 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1799 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1800 return -1;
1801 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001802 return 0;
1803}
1804
1805static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001806save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001807{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001808 PyObject *repr = NULL;
1809 Py_ssize_t size;
1810 long val;
1811 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001812
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001813 const char long_op = LONG;
1814
1815 val= PyLong_AsLong(obj);
1816 if (val == -1 && PyErr_Occurred()) {
1817 /* out of range for int pickling */
1818 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001819 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001820 else if (self->bin &&
1821 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001822 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001823 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001824
1825 Note: we can't use -0x80000000L in the above condition because some
1826 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1827 before applying the unary minus when sizeof(long) <= 4. The
1828 resulting value stays unsigned which is commonly not what we want,
1829 so MSVC happily warns us about it. However, that result would have
1830 been fine because we guard for sizeof(long) <= 4 which turns the
1831 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001832 char pdata[32];
1833 Py_ssize_t len = 0;
1834
1835 pdata[1] = (unsigned char)(val & 0xff);
1836 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1837 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1838 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001839
1840 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1841 if (pdata[2] == 0) {
1842 pdata[0] = BININT1;
1843 len = 2;
1844 }
1845 else {
1846 pdata[0] = BININT2;
1847 len = 3;
1848 }
1849 }
1850 else {
1851 pdata[0] = BININT;
1852 len = 5;
1853 }
1854
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001855 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001856 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001857
1858 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001859 }
1860
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001861 if (self->proto >= 2) {
1862 /* Linear-time pickling. */
1863 size_t nbits;
1864 size_t nbytes;
1865 unsigned char *pdata;
1866 char header[5];
1867 int i;
1868 int sign = _PyLong_Sign(obj);
1869
1870 if (sign == 0) {
1871 header[0] = LONG1;
1872 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001873 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001874 goto error;
1875 return 0;
1876 }
1877 nbits = _PyLong_NumBits(obj);
1878 if (nbits == (size_t)-1 && PyErr_Occurred())
1879 goto error;
1880 /* How many bytes do we need? There are nbits >> 3 full
1881 * bytes of data, and nbits & 7 leftover bits. If there
1882 * are any leftover bits, then we clearly need another
1883 * byte. Wnat's not so obvious is that we *probably*
1884 * need another byte even if there aren't any leftovers:
1885 * the most-significant bit of the most-significant byte
1886 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001887 * opposite of the one we need. The exception is ints
1888 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001889 * its own 256's-complement, so has the right sign bit
1890 * even without the extra byte. That's a pain to check
1891 * for in advance, though, so we always grab an extra
1892 * byte at the start, and cut it back later if possible.
1893 */
1894 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001895 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001896 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001897 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001898 goto error;
1899 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001900 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001901 if (repr == NULL)
1902 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001903 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001904 i = _PyLong_AsByteArray((PyLongObject *)obj,
1905 pdata, nbytes,
1906 1 /* little endian */ , 1 /* signed */ );
1907 if (i < 0)
1908 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001909 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001910 * needed. This is so iff the MSB is all redundant sign
1911 * bits.
1912 */
1913 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001914 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001915 pdata[nbytes - 1] == 0xff &&
1916 (pdata[nbytes - 2] & 0x80) != 0) {
1917 nbytes--;
1918 }
1919
1920 if (nbytes < 256) {
1921 header[0] = LONG1;
1922 header[1] = (unsigned char)nbytes;
1923 size = 2;
1924 }
1925 else {
1926 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001927 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001928 for (i = 1; i < 5; i++) {
1929 header[i] = (unsigned char)(size & 0xff);
1930 size >>= 8;
1931 }
1932 size = 5;
1933 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001934 if (_Pickler_Write(self, header, size) < 0 ||
1935 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001936 goto error;
1937 }
1938 else {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001939 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001940
Mark Dickinson8dd05142009-01-20 20:43:58 +00001941 /* proto < 2: write the repr and newline. This is quadratic-time (in
1942 the number of digits), in both directions. We add a trailing 'L'
1943 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001944
1945 repr = PyObject_Repr(obj);
1946 if (repr == NULL)
1947 goto error;
1948
Serhiy Storchaka06515832016-11-20 09:13:07 +02001949 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001950 if (string == NULL)
1951 goto error;
1952
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001953 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1954 _Pickler_Write(self, string, size) < 0 ||
1955 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001956 goto error;
1957 }
1958
1959 if (0) {
1960 error:
1961 status = -1;
1962 }
1963 Py_XDECREF(repr);
1964
1965 return status;
1966}
1967
1968static int
1969save_float(PicklerObject *self, PyObject *obj)
1970{
1971 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1972
1973 if (self->bin) {
1974 char pdata[9];
1975 pdata[0] = BINFLOAT;
1976 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1977 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001978 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001979 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001980 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001981 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001982 int result = -1;
1983 char *buf = NULL;
1984 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001985
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001986 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001987 goto done;
1988
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001989 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001990 if (!buf) {
1991 PyErr_NoMemory();
1992 goto done;
1993 }
1994
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001995 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001996 goto done;
1997
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001998 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001999 goto done;
2000
2001 result = 0;
2002done:
2003 PyMem_Free(buf);
2004 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002005 }
2006
2007 return 0;
2008}
2009
2010static int
2011save_bytes(PicklerObject *self, PyObject *obj)
2012{
2013 if (self->proto < 3) {
2014 /* Older pickle protocols do not have an opcode for pickling bytes
2015 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002016 the __reduce__ method) to permit bytes object unpickling.
2017
2018 Here we use a hack to be compatible with Python 2. Since in Python
2019 2 'bytes' is just an alias for 'str' (which has different
2020 parameters than the actual bytes object), we use codecs.encode
2021 to create the appropriate 'str' object when unpickled using
2022 Python 2 *and* the appropriate 'bytes' object when unpickled
2023 using Python 3. Again this is a hack and we don't need to do this
2024 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002025 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002026 int status;
2027
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002028 if (PyBytes_GET_SIZE(obj) == 0) {
2029 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2030 }
2031 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002032 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002033 PyObject *unicode_str =
2034 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2035 PyBytes_GET_SIZE(obj),
2036 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002037 _Py_IDENTIFIER(latin1);
2038
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002039 if (unicode_str == NULL)
2040 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002041 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002042 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002043 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002044 Py_DECREF(unicode_str);
2045 }
2046
2047 if (reduce_value == NULL)
2048 return -1;
2049
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002050 /* save_reduce() will memoize the object automatically. */
2051 status = save_reduce(self, reduce_value, obj);
2052 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002053 return status;
2054 }
2055 else {
2056 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002057 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002058 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002059
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002060 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061 if (size < 0)
2062 return -1;
2063
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002064 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002065 header[0] = SHORT_BINBYTES;
2066 header[1] = (unsigned char)size;
2067 len = 2;
2068 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002069 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002070 header[0] = BINBYTES;
2071 header[1] = (unsigned char)(size & 0xff);
2072 header[2] = (unsigned char)((size >> 8) & 0xff);
2073 header[3] = (unsigned char)((size >> 16) & 0xff);
2074 header[4] = (unsigned char)((size >> 24) & 0xff);
2075 len = 5;
2076 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002077 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002078 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002079 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002080 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002081 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002082 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002083 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002084 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002085 return -1; /* string too large */
2086 }
2087
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002088 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002089 return -1;
2090
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002091 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002092 return -1;
2093
2094 if (memo_put(self, obj) < 0)
2095 return -1;
2096
2097 return 0;
2098 }
2099}
2100
2101/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2102 backslash and newline characters to \uXXXX escapes. */
2103static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002104raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002105{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002106 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002107 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002108 void *data;
2109 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002110 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002111
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002112 if (PyUnicode_READY(obj))
2113 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002114
Victor Stinner358af132015-10-12 22:36:57 +02002115 _PyBytesWriter_Init(&writer);
2116
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002117 size = PyUnicode_GET_LENGTH(obj);
2118 data = PyUnicode_DATA(obj);
2119 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002120
Victor Stinner358af132015-10-12 22:36:57 +02002121 p = _PyBytesWriter_Alloc(&writer, size);
2122 if (p == NULL)
2123 goto error;
2124 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002125
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002126 for (i=0; i < size; i++) {
2127 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002128 /* Map 32-bit characters to '\Uxxxxxxxx' */
2129 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002130 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002131 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2132 if (p == NULL)
2133 goto error;
2134
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002135 *p++ = '\\';
2136 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002137 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2138 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2139 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2140 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2141 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2142 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2143 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2144 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002145 }
Victor Stinner358af132015-10-12 22:36:57 +02002146 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002147 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002148 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002149 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2150 if (p == NULL)
2151 goto error;
2152
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002153 *p++ = '\\';
2154 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002155 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2156 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2157 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2158 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002159 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002160 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002161 else
2162 *p++ = (char) ch;
2163 }
Victor Stinner358af132015-10-12 22:36:57 +02002164
2165 return _PyBytesWriter_Finish(&writer, p);
2166
2167error:
2168 _PyBytesWriter_Dealloc(&writer);
2169 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002170}
2171
2172static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002173write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002174{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002175 char header[9];
2176 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002177
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002178 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002179 if (size <= 0xff && self->proto >= 4) {
2180 header[0] = SHORT_BINUNICODE;
2181 header[1] = (unsigned char)(size & 0xff);
2182 len = 2;
2183 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002184 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002185 header[0] = BINUNICODE;
2186 header[1] = (unsigned char)(size & 0xff);
2187 header[2] = (unsigned char)((size >> 8) & 0xff);
2188 header[3] = (unsigned char)((size >> 16) & 0xff);
2189 header[4] = (unsigned char)((size >> 24) & 0xff);
2190 len = 5;
2191 }
2192 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002193 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002194 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002195 len = 9;
2196 }
2197 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002198 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002199 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002200 return -1;
2201 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002202
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002203 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002204 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002205 if (_Pickler_Write(self, data, size) < 0)
2206 return -1;
2207
2208 return 0;
2209}
2210
2211static int
2212write_unicode_binary(PicklerObject *self, PyObject *obj)
2213{
2214 PyObject *encoded = NULL;
2215 Py_ssize_t size;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002216 const char *data;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002217 int r;
2218
2219 if (PyUnicode_READY(obj))
2220 return -1;
2221
2222 data = PyUnicode_AsUTF8AndSize(obj, &size);
2223 if (data != NULL)
2224 return write_utf8(self, data, size);
2225
2226 /* Issue #8383: for strings with lone surrogates, fallback on the
2227 "surrogatepass" error handler. */
2228 PyErr_Clear();
2229 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2230 if (encoded == NULL)
2231 return -1;
2232
2233 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2234 PyBytes_GET_SIZE(encoded));
2235 Py_DECREF(encoded);
2236 return r;
2237}
2238
2239static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002240save_unicode(PicklerObject *self, PyObject *obj)
2241{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002242 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002243 if (write_unicode_binary(self, obj) < 0)
2244 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002245 }
2246 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002247 PyObject *encoded;
2248 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002249 const char unicode_op = UNICODE;
2250
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002251 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002253 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002254
Antoine Pitrou299978d2013-04-07 17:38:11 +02002255 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2256 Py_DECREF(encoded);
2257 return -1;
2258 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259
2260 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002261 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2262 Py_DECREF(encoded);
2263 return -1;
2264 }
2265 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002266
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002267 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002268 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002269 }
2270 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002271 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002272
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002273 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002274}
2275
2276/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2277static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002278store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002280 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002281
2282 assert(PyTuple_Size(t) == len);
2283
2284 for (i = 0; i < len; i++) {
2285 PyObject *element = PyTuple_GET_ITEM(t, i);
2286
2287 if (element == NULL)
2288 return -1;
2289 if (save(self, element, 0) < 0)
2290 return -1;
2291 }
2292
2293 return 0;
2294}
2295
2296/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2297 * used across protocols to minimize the space needed to pickle them.
2298 * Tuples are also the only builtin immutable type that can be recursive
2299 * (a tuple can be reached from itself), and that requires some subtle
2300 * magic so that it works in all cases. IOW, this is a long routine.
2301 */
2302static int
2303save_tuple(PicklerObject *self, PyObject *obj)
2304{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002305 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002306
2307 const char mark_op = MARK;
2308 const char tuple_op = TUPLE;
2309 const char pop_op = POP;
2310 const char pop_mark_op = POP_MARK;
2311 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2312
2313 if ((len = PyTuple_Size(obj)) < 0)
2314 return -1;
2315
2316 if (len == 0) {
2317 char pdata[2];
2318
2319 if (self->proto) {
2320 pdata[0] = EMPTY_TUPLE;
2321 len = 1;
2322 }
2323 else {
2324 pdata[0] = MARK;
2325 pdata[1] = TUPLE;
2326 len = 2;
2327 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002328 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002329 return -1;
2330 return 0;
2331 }
2332
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002333 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002334 * saving the tuple elements, the tuple must be recursive, in
2335 * which case we'll pop everything we put on the stack, and fetch
2336 * its value from the memo.
2337 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002338 if (len <= 3 && self->proto >= 2) {
2339 /* Use TUPLE{1,2,3} opcodes. */
2340 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002341 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002342
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002343 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002344 /* pop the len elements */
2345 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002346 if (_Pickler_Write(self, &pop_op, 1) < 0)
2347 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002348 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002349 if (memo_get(self, obj) < 0)
2350 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002352 return 0;
2353 }
2354 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002355 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2356 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002357 }
2358 goto memoize;
2359 }
2360
2361 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2362 * Generate MARK e1 e2 ... TUPLE
2363 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002364 if (_Pickler_Write(self, &mark_op, 1) < 0)
2365 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002366
2367 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002368 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002369
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002370 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002371 /* pop the stack stuff we pushed */
2372 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002373 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2374 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002375 }
2376 else {
2377 /* Note that we pop one more than len, to remove
2378 * the MARK too.
2379 */
2380 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002381 if (_Pickler_Write(self, &pop_op, 1) < 0)
2382 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002383 }
2384 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002385 if (memo_get(self, obj) < 0)
2386 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002387
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002388 return 0;
2389 }
2390 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002391 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2392 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002393 }
2394
2395 memoize:
2396 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002397 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002398
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002399 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002400}
2401
2402/* iter is an iterator giving items, and we batch up chunks of
2403 * MARK item item ... item APPENDS
2404 * opcode sequences. Calling code should have arranged to first create an
2405 * empty list, or list-like object, for the APPENDS to operate on.
2406 * Returns 0 on success, <0 on error.
2407 */
2408static int
2409batch_list(PicklerObject *self, PyObject *iter)
2410{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002411 PyObject *obj = NULL;
2412 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002413 int i, n;
2414
2415 const char mark_op = MARK;
2416 const char append_op = APPEND;
2417 const char appends_op = APPENDS;
2418
2419 assert(iter != NULL);
2420
2421 /* XXX: I think this function could be made faster by avoiding the
2422 iterator interface and fetching objects directly from list using
2423 PyList_GET_ITEM.
2424 */
2425
2426 if (self->proto == 0) {
2427 /* APPENDS isn't available; do one at a time. */
2428 for (;;) {
2429 obj = PyIter_Next(iter);
2430 if (obj == NULL) {
2431 if (PyErr_Occurred())
2432 return -1;
2433 break;
2434 }
2435 i = save(self, obj, 0);
2436 Py_DECREF(obj);
2437 if (i < 0)
2438 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002439 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002440 return -1;
2441 }
2442 return 0;
2443 }
2444
2445 /* proto > 0: write in batches of BATCHSIZE. */
2446 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002447 /* Get first item */
2448 firstitem = PyIter_Next(iter);
2449 if (firstitem == NULL) {
2450 if (PyErr_Occurred())
2451 goto error;
2452
2453 /* nothing more to add */
2454 break;
2455 }
2456
2457 /* Try to get a second item */
2458 obj = PyIter_Next(iter);
2459 if (obj == NULL) {
2460 if (PyErr_Occurred())
2461 goto error;
2462
2463 /* Only one item to write */
2464 if (save(self, firstitem, 0) < 0)
2465 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002466 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002467 goto error;
2468 Py_CLEAR(firstitem);
2469 break;
2470 }
2471
2472 /* More than one item to write */
2473
2474 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002475 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002476 goto error;
2477
2478 if (save(self, firstitem, 0) < 0)
2479 goto error;
2480 Py_CLEAR(firstitem);
2481 n = 1;
2482
2483 /* Fetch and save up to BATCHSIZE items */
2484 while (obj) {
2485 if (save(self, obj, 0) < 0)
2486 goto error;
2487 Py_CLEAR(obj);
2488 n += 1;
2489
2490 if (n == BATCHSIZE)
2491 break;
2492
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002493 obj = PyIter_Next(iter);
2494 if (obj == NULL) {
2495 if (PyErr_Occurred())
2496 goto error;
2497 break;
2498 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002499 }
2500
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002501 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002502 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002503
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002504 } while (n == BATCHSIZE);
2505 return 0;
2506
2507 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002508 Py_XDECREF(firstitem);
2509 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002510 return -1;
2511}
2512
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002513/* This is a variant of batch_list() above, specialized for lists (with no
2514 * support for list subclasses). Like batch_list(), we batch up chunks of
2515 * MARK item item ... item APPENDS
2516 * opcode sequences. Calling code should have arranged to first create an
2517 * empty list, or list-like object, for the APPENDS to operate on.
2518 * Returns 0 on success, -1 on error.
2519 *
2520 * This version is considerably faster than batch_list(), if less general.
2521 *
2522 * Note that this only works for protocols > 0.
2523 */
2524static int
2525batch_list_exact(PicklerObject *self, PyObject *obj)
2526{
2527 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002528 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002529
2530 const char append_op = APPEND;
2531 const char appends_op = APPENDS;
2532 const char mark_op = MARK;
2533
2534 assert(obj != NULL);
2535 assert(self->proto > 0);
2536 assert(PyList_CheckExact(obj));
2537
2538 if (PyList_GET_SIZE(obj) == 1) {
2539 item = PyList_GET_ITEM(obj, 0);
2540 if (save(self, item, 0) < 0)
2541 return -1;
2542 if (_Pickler_Write(self, &append_op, 1) < 0)
2543 return -1;
2544 return 0;
2545 }
2546
2547 /* Write in batches of BATCHSIZE. */
2548 total = 0;
2549 do {
2550 this_batch = 0;
2551 if (_Pickler_Write(self, &mark_op, 1) < 0)
2552 return -1;
2553 while (total < PyList_GET_SIZE(obj)) {
2554 item = PyList_GET_ITEM(obj, total);
2555 if (save(self, item, 0) < 0)
2556 return -1;
2557 total++;
2558 if (++this_batch == BATCHSIZE)
2559 break;
2560 }
2561 if (_Pickler_Write(self, &appends_op, 1) < 0)
2562 return -1;
2563
2564 } while (total < PyList_GET_SIZE(obj));
2565
2566 return 0;
2567}
2568
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002569static int
2570save_list(PicklerObject *self, PyObject *obj)
2571{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002572 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002573 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002574 int status = 0;
2575
2576 if (self->fast && !fast_save_enter(self, obj))
2577 goto error;
2578
2579 /* Create an empty list. */
2580 if (self->bin) {
2581 header[0] = EMPTY_LIST;
2582 len = 1;
2583 }
2584 else {
2585 header[0] = MARK;
2586 header[1] = LIST;
2587 len = 2;
2588 }
2589
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002590 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002591 goto error;
2592
2593 /* Get list length, and bow out early if empty. */
2594 if ((len = PyList_Size(obj)) < 0)
2595 goto error;
2596
2597 if (memo_put(self, obj) < 0)
2598 goto error;
2599
2600 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002601 /* Materialize the list elements. */
2602 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002603 if (Py_EnterRecursiveCall(" while pickling an object"))
2604 goto error;
2605 status = batch_list_exact(self, obj);
2606 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002607 } else {
2608 PyObject *iter = PyObject_GetIter(obj);
2609 if (iter == NULL)
2610 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002611
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002612 if (Py_EnterRecursiveCall(" while pickling an object")) {
2613 Py_DECREF(iter);
2614 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002615 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002616 status = batch_list(self, iter);
2617 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002618 Py_DECREF(iter);
2619 }
2620 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002621 if (0) {
2622 error:
2623 status = -1;
2624 }
2625
2626 if (self->fast && !fast_save_leave(self, obj))
2627 status = -1;
2628
2629 return status;
2630}
2631
2632/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2633 * MARK key value ... key value SETITEMS
2634 * opcode sequences. Calling code should have arranged to first create an
2635 * empty dict, or dict-like object, for the SETITEMS to operate on.
2636 * Returns 0 on success, <0 on error.
2637 *
2638 * This is very much like batch_list(). The difference between saving
2639 * elements directly, and picking apart two-tuples, is so long-winded at
2640 * the C level, though, that attempts to combine these routines were too
2641 * ugly to bear.
2642 */
2643static int
2644batch_dict(PicklerObject *self, PyObject *iter)
2645{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002646 PyObject *obj = NULL;
2647 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002648 int i, n;
2649
2650 const char mark_op = MARK;
2651 const char setitem_op = SETITEM;
2652 const char setitems_op = SETITEMS;
2653
2654 assert(iter != NULL);
2655
2656 if (self->proto == 0) {
2657 /* SETITEMS isn't available; do one at a time. */
2658 for (;;) {
2659 obj = PyIter_Next(iter);
2660 if (obj == NULL) {
2661 if (PyErr_Occurred())
2662 return -1;
2663 break;
2664 }
2665 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2666 PyErr_SetString(PyExc_TypeError, "dict items "
2667 "iterator must return 2-tuples");
2668 return -1;
2669 }
2670 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2671 if (i >= 0)
2672 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2673 Py_DECREF(obj);
2674 if (i < 0)
2675 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002676 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002677 return -1;
2678 }
2679 return 0;
2680 }
2681
2682 /* proto > 0: write in batches of BATCHSIZE. */
2683 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002684 /* Get first item */
2685 firstitem = PyIter_Next(iter);
2686 if (firstitem == NULL) {
2687 if (PyErr_Occurred())
2688 goto error;
2689
2690 /* nothing more to add */
2691 break;
2692 }
2693 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2694 PyErr_SetString(PyExc_TypeError, "dict items "
2695 "iterator must return 2-tuples");
2696 goto error;
2697 }
2698
2699 /* Try to get a second item */
2700 obj = PyIter_Next(iter);
2701 if (obj == NULL) {
2702 if (PyErr_Occurred())
2703 goto error;
2704
2705 /* Only one item to write */
2706 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2707 goto error;
2708 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2709 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002710 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002711 goto error;
2712 Py_CLEAR(firstitem);
2713 break;
2714 }
2715
2716 /* More than one item to write */
2717
2718 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002719 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002720 goto error;
2721
2722 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2723 goto error;
2724 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2725 goto error;
2726 Py_CLEAR(firstitem);
2727 n = 1;
2728
2729 /* Fetch and save up to BATCHSIZE items */
2730 while (obj) {
2731 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2732 PyErr_SetString(PyExc_TypeError, "dict items "
2733 "iterator must return 2-tuples");
2734 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002735 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002736 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2737 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2738 goto error;
2739 Py_CLEAR(obj);
2740 n += 1;
2741
2742 if (n == BATCHSIZE)
2743 break;
2744
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002745 obj = PyIter_Next(iter);
2746 if (obj == NULL) {
2747 if (PyErr_Occurred())
2748 goto error;
2749 break;
2750 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002751 }
2752
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002753 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002754 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002755
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002756 } while (n == BATCHSIZE);
2757 return 0;
2758
2759 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002760 Py_XDECREF(firstitem);
2761 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002762 return -1;
2763}
2764
Collin Winter5c9b02d2009-05-25 05:43:30 +00002765/* This is a variant of batch_dict() above that specializes for dicts, with no
2766 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2767 * MARK key value ... key value SETITEMS
2768 * opcode sequences. Calling code should have arranged to first create an
2769 * empty dict, or dict-like object, for the SETITEMS to operate on.
2770 * Returns 0 on success, -1 on error.
2771 *
2772 * Note that this currently doesn't work for protocol 0.
2773 */
2774static int
2775batch_dict_exact(PicklerObject *self, PyObject *obj)
2776{
2777 PyObject *key = NULL, *value = NULL;
2778 int i;
2779 Py_ssize_t dict_size, ppos = 0;
2780
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002781 const char mark_op = MARK;
2782 const char setitem_op = SETITEM;
2783 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002784
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002785 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002786 assert(self->proto > 0);
2787
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002788 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002789
2790 /* Special-case len(d) == 1 to save space. */
2791 if (dict_size == 1) {
2792 PyDict_Next(obj, &ppos, &key, &value);
2793 if (save(self, key, 0) < 0)
2794 return -1;
2795 if (save(self, value, 0) < 0)
2796 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002797 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002798 return -1;
2799 return 0;
2800 }
2801
2802 /* Write in batches of BATCHSIZE. */
2803 do {
2804 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002805 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002806 return -1;
2807 while (PyDict_Next(obj, &ppos, &key, &value)) {
2808 if (save(self, key, 0) < 0)
2809 return -1;
2810 if (save(self, value, 0) < 0)
2811 return -1;
2812 if (++i == BATCHSIZE)
2813 break;
2814 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002815 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002816 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002817 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00002818 PyErr_Format(
2819 PyExc_RuntimeError,
2820 "dictionary changed size during iteration");
2821 return -1;
2822 }
2823
2824 } while (i == BATCHSIZE);
2825 return 0;
2826}
2827
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002828static int
2829save_dict(PicklerObject *self, PyObject *obj)
2830{
2831 PyObject *items, *iter;
2832 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002833 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002834 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002835 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002836
2837 if (self->fast && !fast_save_enter(self, obj))
2838 goto error;
2839
2840 /* Create an empty dict. */
2841 if (self->bin) {
2842 header[0] = EMPTY_DICT;
2843 len = 1;
2844 }
2845 else {
2846 header[0] = MARK;
2847 header[1] = DICT;
2848 len = 2;
2849 }
2850
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002851 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002852 goto error;
2853
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002854 if (memo_put(self, obj) < 0)
2855 goto error;
2856
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002857 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002858 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002859 if (PyDict_CheckExact(obj) && self->proto > 0) {
2860 /* We can take certain shortcuts if we know this is a dict and
2861 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002862 if (Py_EnterRecursiveCall(" while pickling an object"))
2863 goto error;
2864 status = batch_dict_exact(self, obj);
2865 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002866 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002867 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002868
Victor Stinnerad8c83a2016-09-05 17:53:15 -07002869 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002870 if (items == NULL)
2871 goto error;
2872 iter = PyObject_GetIter(items);
2873 Py_DECREF(items);
2874 if (iter == NULL)
2875 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002876 if (Py_EnterRecursiveCall(" while pickling an object")) {
2877 Py_DECREF(iter);
2878 goto error;
2879 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002880 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002881 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002882 Py_DECREF(iter);
2883 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002884 }
2885
2886 if (0) {
2887 error:
2888 status = -1;
2889 }
2890
2891 if (self->fast && !fast_save_leave(self, obj))
2892 status = -1;
2893
2894 return status;
2895}
2896
2897static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002898save_set(PicklerObject *self, PyObject *obj)
2899{
2900 PyObject *item;
2901 int i;
2902 Py_ssize_t set_size, ppos = 0;
2903 Py_hash_t hash;
2904
2905 const char empty_set_op = EMPTY_SET;
2906 const char mark_op = MARK;
2907 const char additems_op = ADDITEMS;
2908
2909 if (self->proto < 4) {
2910 PyObject *items;
2911 PyObject *reduce_value;
2912 int status;
2913
2914 items = PySequence_List(obj);
2915 if (items == NULL) {
2916 return -1;
2917 }
2918 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2919 Py_DECREF(items);
2920 if (reduce_value == NULL) {
2921 return -1;
2922 }
2923 /* save_reduce() will memoize the object automatically. */
2924 status = save_reduce(self, reduce_value, obj);
2925 Py_DECREF(reduce_value);
2926 return status;
2927 }
2928
2929 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2930 return -1;
2931
2932 if (memo_put(self, obj) < 0)
2933 return -1;
2934
2935 set_size = PySet_GET_SIZE(obj);
2936 if (set_size == 0)
2937 return 0; /* nothing to do */
2938
2939 /* Write in batches of BATCHSIZE. */
2940 do {
2941 i = 0;
2942 if (_Pickler_Write(self, &mark_op, 1) < 0)
2943 return -1;
2944 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2945 if (save(self, item, 0) < 0)
2946 return -1;
2947 if (++i == BATCHSIZE)
2948 break;
2949 }
2950 if (_Pickler_Write(self, &additems_op, 1) < 0)
2951 return -1;
2952 if (PySet_GET_SIZE(obj) != set_size) {
2953 PyErr_Format(
2954 PyExc_RuntimeError,
2955 "set changed size during iteration");
2956 return -1;
2957 }
2958 } while (i == BATCHSIZE);
2959
2960 return 0;
2961}
2962
2963static int
2964save_frozenset(PicklerObject *self, PyObject *obj)
2965{
2966 PyObject *iter;
2967
2968 const char mark_op = MARK;
2969 const char frozenset_op = FROZENSET;
2970
2971 if (self->fast && !fast_save_enter(self, obj))
2972 return -1;
2973
2974 if (self->proto < 4) {
2975 PyObject *items;
2976 PyObject *reduce_value;
2977 int status;
2978
2979 items = PySequence_List(obj);
2980 if (items == NULL) {
2981 return -1;
2982 }
2983 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2984 items);
2985 Py_DECREF(items);
2986 if (reduce_value == NULL) {
2987 return -1;
2988 }
2989 /* save_reduce() will memoize the object automatically. */
2990 status = save_reduce(self, reduce_value, obj);
2991 Py_DECREF(reduce_value);
2992 return status;
2993 }
2994
2995 if (_Pickler_Write(self, &mark_op, 1) < 0)
2996 return -1;
2997
2998 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002999 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003000 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003001 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003002 for (;;) {
3003 PyObject *item;
3004
3005 item = PyIter_Next(iter);
3006 if (item == NULL) {
3007 if (PyErr_Occurred()) {
3008 Py_DECREF(iter);
3009 return -1;
3010 }
3011 break;
3012 }
3013 if (save(self, item, 0) < 0) {
3014 Py_DECREF(item);
3015 Py_DECREF(iter);
3016 return -1;
3017 }
3018 Py_DECREF(item);
3019 }
3020 Py_DECREF(iter);
3021
3022 /* If the object is already in the memo, this means it is
3023 recursive. In this case, throw away everything we put on the
3024 stack, and fetch the object back from the memo. */
3025 if (PyMemoTable_Get(self->memo, obj)) {
3026 const char pop_mark_op = POP_MARK;
3027
3028 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3029 return -1;
3030 if (memo_get(self, obj) < 0)
3031 return -1;
3032 return 0;
3033 }
3034
3035 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3036 return -1;
3037 if (memo_put(self, obj) < 0)
3038 return -1;
3039
3040 return 0;
3041}
3042
3043static int
3044fix_imports(PyObject **module_name, PyObject **global_name)
3045{
3046 PyObject *key;
3047 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003048 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003049
3050 key = PyTuple_Pack(2, *module_name, *global_name);
3051 if (key == NULL)
3052 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003053 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003054 Py_DECREF(key);
3055 if (item) {
3056 PyObject *fixed_module_name;
3057 PyObject *fixed_global_name;
3058
3059 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3060 PyErr_Format(PyExc_RuntimeError,
3061 "_compat_pickle.REVERSE_NAME_MAPPING values "
3062 "should be 2-tuples, not %.200s",
3063 Py_TYPE(item)->tp_name);
3064 return -1;
3065 }
3066 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3067 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3068 if (!PyUnicode_Check(fixed_module_name) ||
3069 !PyUnicode_Check(fixed_global_name)) {
3070 PyErr_Format(PyExc_RuntimeError,
3071 "_compat_pickle.REVERSE_NAME_MAPPING values "
3072 "should be pairs of str, not (%.200s, %.200s)",
3073 Py_TYPE(fixed_module_name)->tp_name,
3074 Py_TYPE(fixed_global_name)->tp_name);
3075 return -1;
3076 }
3077
3078 Py_CLEAR(*module_name);
3079 Py_CLEAR(*global_name);
3080 Py_INCREF(fixed_module_name);
3081 Py_INCREF(fixed_global_name);
3082 *module_name = fixed_module_name;
3083 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003084 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003085 }
3086 else if (PyErr_Occurred()) {
3087 return -1;
3088 }
3089
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003090 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003091 if (item) {
3092 if (!PyUnicode_Check(item)) {
3093 PyErr_Format(PyExc_RuntimeError,
3094 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3095 "should be strings, not %.200s",
3096 Py_TYPE(item)->tp_name);
3097 return -1;
3098 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003099 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003100 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003101 }
3102 else if (PyErr_Occurred()) {
3103 return -1;
3104 }
3105
3106 return 0;
3107}
3108
3109static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003110save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3111{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003112 PyObject *global_name = NULL;
3113 PyObject *module_name = NULL;
3114 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003115 PyObject *parent = NULL;
3116 PyObject *dotted_path = NULL;
3117 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003118 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003119 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003120 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003121 _Py_IDENTIFIER(__name__);
3122 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003123
3124 const char global_op = GLOBAL;
3125
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003126 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003127 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003128 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003129 }
3130 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003131 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3132 if (global_name == NULL) {
3133 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3134 goto error;
3135 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003136 }
3137 if (global_name == NULL) {
3138 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3139 if (global_name == NULL)
3140 goto error;
3141 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003142 }
3143
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003144 dotted_path = get_dotted_path(module, global_name);
3145 if (dotted_path == NULL)
3146 goto error;
3147 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003148 if (module_name == NULL)
3149 goto error;
3150
3151 /* XXX: Change to use the import C API directly with level=0 to disallow
3152 relative imports.
3153
3154 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3155 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3156 custom import functions (IMHO, this would be a nice security
3157 feature). The import C API would need to be extended to support the
3158 extra parameters of __import__ to fix that. */
3159 module = PyImport_Import(module_name);
3160 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003161 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003162 "Can't pickle %R: import of module %R failed",
3163 obj, module_name);
3164 goto error;
3165 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003166 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3167 Py_INCREF(lastname);
3168 cls = get_deep_attribute(module, dotted_path, &parent);
3169 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003170 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003171 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003172 "Can't pickle %R: attribute lookup %S on %S failed",
3173 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003174 goto error;
3175 }
3176 if (cls != obj) {
3177 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003178 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003179 "Can't pickle %R: it's not the same object as %S.%S",
3180 obj, module_name, global_name);
3181 goto error;
3182 }
3183 Py_DECREF(cls);
3184
3185 if (self->proto >= 2) {
3186 /* See whether this is in the extension registry, and if
3187 * so generate an EXT opcode.
3188 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003189 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003190 PyObject *code_obj; /* extension code as Python object */
3191 long code; /* extension code as C value */
3192 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003193 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003194
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003195 extension_key = PyTuple_Pack(2, module_name, global_name);
3196 if (extension_key == NULL) {
3197 goto error;
3198 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003199 code_obj = PyDict_GetItemWithError(st->extension_registry,
3200 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003201 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003202 /* The object is not registered in the extension registry.
3203 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003204 if (code_obj == NULL) {
3205 if (PyErr_Occurred()) {
3206 goto error;
3207 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003208 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003209 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003210
3211 /* XXX: pickle.py doesn't check neither the type, nor the range
3212 of the value returned by the extension_registry. It should for
3213 consistency. */
3214
3215 /* Verify code_obj has the right type and value. */
3216 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003217 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003218 "Can't pickle %R: extension code %R isn't an integer",
3219 obj, code_obj);
3220 goto error;
3221 }
3222 code = PyLong_AS_LONG(code_obj);
3223 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003224 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003225 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3226 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003227 goto error;
3228 }
3229
3230 /* Generate an EXT opcode. */
3231 if (code <= 0xff) {
3232 pdata[0] = EXT1;
3233 pdata[1] = (unsigned char)code;
3234 n = 2;
3235 }
3236 else if (code <= 0xffff) {
3237 pdata[0] = EXT2;
3238 pdata[1] = (unsigned char)(code & 0xff);
3239 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3240 n = 3;
3241 }
3242 else {
3243 pdata[0] = EXT4;
3244 pdata[1] = (unsigned char)(code & 0xff);
3245 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3246 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3247 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3248 n = 5;
3249 }
3250
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003251 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003252 goto error;
3253 }
3254 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003255 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003256 if (parent == module) {
3257 Py_INCREF(lastname);
3258 Py_DECREF(global_name);
3259 global_name = lastname;
3260 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003261 if (self->proto >= 4) {
3262 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003263
Christian Heimese8b1ba12013-11-23 21:13:39 +01003264 if (save(self, module_name, 0) < 0)
3265 goto error;
3266 if (save(self, global_name, 0) < 0)
3267 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003268
3269 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3270 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003271 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003272 else if (parent != module) {
3273 PickleState *st = _Pickle_GetGlobalState();
3274 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3275 st->getattr, parent, lastname);
3276 status = save_reduce(self, reduce_value, NULL);
3277 Py_DECREF(reduce_value);
3278 if (status < 0)
3279 goto error;
3280 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003281 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003282 /* Generate a normal global opcode if we are using a pickle
3283 protocol < 4, or if the object is not registered in the
3284 extension registry. */
3285 PyObject *encoded;
3286 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003287
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003288 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003289 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003290
3291 /* For protocol < 3 and if the user didn't request against doing
3292 so, we convert module names to the old 2.x module names. */
3293 if (self->proto < 3 && self->fix_imports) {
3294 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003295 goto error;
3296 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003297 }
3298
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003299 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3300 both the module name and the global name using UTF-8. We do so
3301 only when we are using the pickle protocol newer than version
3302 3. This is to ensure compatibility with older Unpickler running
3303 on Python 2.x. */
3304 if (self->proto == 3) {
3305 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003306 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003307 else {
3308 unicode_encoder = PyUnicode_AsASCIIString;
3309 }
3310 encoded = unicode_encoder(module_name);
3311 if (encoded == NULL) {
3312 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003313 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003314 "can't pickle module identifier '%S' using "
3315 "pickle protocol %i",
3316 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003317 goto error;
3318 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003319 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3320 PyBytes_GET_SIZE(encoded)) < 0) {
3321 Py_DECREF(encoded);
3322 goto error;
3323 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003324 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003325 if(_Pickler_Write(self, "\n", 1) < 0)
3326 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003327
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003328 /* Save the name of the module. */
3329 encoded = unicode_encoder(global_name);
3330 if (encoded == NULL) {
3331 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003332 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003333 "can't pickle global identifier '%S' using "
3334 "pickle protocol %i",
3335 global_name, self->proto);
3336 goto error;
3337 }
3338 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3339 PyBytes_GET_SIZE(encoded)) < 0) {
3340 Py_DECREF(encoded);
3341 goto error;
3342 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003343 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003344 if (_Pickler_Write(self, "\n", 1) < 0)
3345 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003346 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003347 /* Memoize the object. */
3348 if (memo_put(self, obj) < 0)
3349 goto error;
3350 }
3351
3352 if (0) {
3353 error:
3354 status = -1;
3355 }
3356 Py_XDECREF(module_name);
3357 Py_XDECREF(global_name);
3358 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003359 Py_XDECREF(parent);
3360 Py_XDECREF(dotted_path);
3361 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003362
3363 return status;
3364}
3365
3366static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003367save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3368{
3369 PyObject *reduce_value;
3370 int status;
3371
3372 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3373 if (reduce_value == NULL) {
3374 return -1;
3375 }
3376 status = save_reduce(self, reduce_value, obj);
3377 Py_DECREF(reduce_value);
3378 return status;
3379}
3380
3381static int
3382save_type(PicklerObject *self, PyObject *obj)
3383{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003384 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003385 return save_singleton_type(self, obj, Py_None);
3386 }
3387 else if (obj == (PyObject *)&PyEllipsis_Type) {
3388 return save_singleton_type(self, obj, Py_Ellipsis);
3389 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003390 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003391 return save_singleton_type(self, obj, Py_NotImplemented);
3392 }
3393 return save_global(self, obj, NULL);
3394}
3395
3396static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003397save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3398{
3399 PyObject *pid = NULL;
3400 int status = 0;
3401
3402 const char persid_op = PERSID;
3403 const char binpersid_op = BINPERSID;
3404
3405 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003406 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003407 if (pid == NULL)
3408 return -1;
3409
3410 if (pid != Py_None) {
3411 if (self->bin) {
3412 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003413 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003414 goto error;
3415 }
3416 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003417 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003418
3419 pid_str = PyObject_Str(pid);
3420 if (pid_str == NULL)
3421 goto error;
3422
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003423 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003424 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003425 if (!PyUnicode_IS_ASCII(pid_str)) {
3426 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3427 "persistent IDs in protocol 0 must be "
3428 "ASCII strings");
3429 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003430 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003431 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003432
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003433 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003434 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3435 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3436 _Pickler_Write(self, "\n", 1) < 0) {
3437 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003438 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003439 }
3440 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003441 }
3442 status = 1;
3443 }
3444
3445 if (0) {
3446 error:
3447 status = -1;
3448 }
3449 Py_XDECREF(pid);
3450
3451 return status;
3452}
3453
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003454static PyObject *
3455get_class(PyObject *obj)
3456{
3457 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003458 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003459
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003460 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003461 if (cls == NULL) {
3462 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3463 PyErr_Clear();
3464 cls = (PyObject *) Py_TYPE(obj);
3465 Py_INCREF(cls);
3466 }
3467 }
3468 return cls;
3469}
3470
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003471/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3472 * appropriate __reduce__ method for obj.
3473 */
3474static int
3475save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3476{
3477 PyObject *callable;
3478 PyObject *argtup;
3479 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003480 PyObject *listitems = Py_None;
3481 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003482 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003483 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003484 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003485
3486 const char reduce_op = REDUCE;
3487 const char build_op = BUILD;
3488 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003489 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003490
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003491 size = PyTuple_Size(args);
3492 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003493 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003494 "__reduce__ must contain 2 through 5 elements");
3495 return -1;
3496 }
3497
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003498 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3499 &callable, &argtup, &state, &listitems, &dictitems))
3500 return -1;
3501
3502 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003503 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003504 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003505 return -1;
3506 }
3507 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003508 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003509 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003510 return -1;
3511 }
3512
3513 if (state == Py_None)
3514 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003515
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003516 if (listitems == Py_None)
3517 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003518 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003519 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003520 "returned by __reduce__ must be an iterator, not %s",
3521 Py_TYPE(listitems)->tp_name);
3522 return -1;
3523 }
3524
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003525 if (dictitems == Py_None)
3526 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003527 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003528 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003529 "returned by __reduce__ must be an iterator, not %s",
3530 Py_TYPE(dictitems)->tp_name);
3531 return -1;
3532 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003533
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003534 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003535 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003536 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003537
Victor Stinner804e05e2013-11-14 01:26:17 +01003538 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003539 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003540 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003541 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003542 }
3543 PyErr_Clear();
3544 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003545 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003546 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003547 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3548 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003549 if (!use_newobj_ex) {
3550 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003551 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003552 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003553 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003554 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003555 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003556
3557 if (use_newobj_ex) {
3558 PyObject *cls;
3559 PyObject *args;
3560 PyObject *kwargs;
3561
3562 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003563 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003564 "length of the NEWOBJ_EX argument tuple must be "
3565 "exactly 3, not %zd", Py_SIZE(argtup));
3566 return -1;
3567 }
3568
3569 cls = PyTuple_GET_ITEM(argtup, 0);
3570 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003571 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003572 "first item from NEWOBJ_EX argument tuple must "
3573 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3574 return -1;
3575 }
3576 args = PyTuple_GET_ITEM(argtup, 1);
3577 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003578 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003579 "second item from NEWOBJ_EX argument tuple must "
3580 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3581 return -1;
3582 }
3583 kwargs = PyTuple_GET_ITEM(argtup, 2);
3584 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003585 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003586 "third item from NEWOBJ_EX argument tuple must "
3587 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3588 return -1;
3589 }
3590
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003591 if (self->proto >= 4) {
3592 if (save(self, cls, 0) < 0 ||
3593 save(self, args, 0) < 0 ||
3594 save(self, kwargs, 0) < 0 ||
3595 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3596 return -1;
3597 }
3598 }
3599 else {
3600 PyObject *newargs;
3601 PyObject *cls_new;
3602 Py_ssize_t i;
3603 _Py_IDENTIFIER(__new__);
3604
3605 newargs = PyTuple_New(Py_SIZE(args) + 2);
3606 if (newargs == NULL)
3607 return -1;
3608
3609 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3610 if (cls_new == NULL) {
3611 Py_DECREF(newargs);
3612 return -1;
3613 }
3614 PyTuple_SET_ITEM(newargs, 0, cls_new);
3615 Py_INCREF(cls);
3616 PyTuple_SET_ITEM(newargs, 1, cls);
3617 for (i = 0; i < Py_SIZE(args); i++) {
3618 PyObject *item = PyTuple_GET_ITEM(args, i);
3619 Py_INCREF(item);
3620 PyTuple_SET_ITEM(newargs, i + 2, item);
3621 }
3622
3623 callable = PyObject_Call(st->partial, newargs, kwargs);
3624 Py_DECREF(newargs);
3625 if (callable == NULL)
3626 return -1;
3627
3628 newargs = PyTuple_New(0);
3629 if (newargs == NULL) {
3630 Py_DECREF(callable);
3631 return -1;
3632 }
3633
3634 if (save(self, callable, 0) < 0 ||
3635 save(self, newargs, 0) < 0 ||
3636 _Pickler_Write(self, &reduce_op, 1) < 0) {
3637 Py_DECREF(newargs);
3638 Py_DECREF(callable);
3639 return -1;
3640 }
3641 Py_DECREF(newargs);
3642 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003643 }
3644 }
3645 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003646 PyObject *cls;
3647 PyObject *newargtup;
3648 PyObject *obj_class;
3649 int p;
3650
3651 /* Sanity checks. */
3652 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003653 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003654 return -1;
3655 }
3656
3657 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003658 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003659 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003660 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003661 return -1;
3662 }
3663
3664 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003665 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003666 p = obj_class != cls; /* true iff a problem */
3667 Py_DECREF(obj_class);
3668 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003669 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003670 "__newobj__ args has the wrong class");
3671 return -1;
3672 }
3673 }
3674 /* XXX: These calls save() are prone to infinite recursion. Imagine
3675 what happen if the value returned by the __reduce__() method of
3676 some extension type contains another object of the same type. Ouch!
3677
3678 Here is a quick example, that I ran into, to illustrate what I
3679 mean:
3680
3681 >>> import pickle, copyreg
3682 >>> copyreg.dispatch_table.pop(complex)
3683 >>> pickle.dumps(1+2j)
3684 Traceback (most recent call last):
3685 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003686 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003687
3688 Removing the complex class from copyreg.dispatch_table made the
3689 __reduce_ex__() method emit another complex object:
3690
3691 >>> (1+1j).__reduce_ex__(2)
3692 (<function __newobj__ at 0xb7b71c3c>,
3693 (<class 'complex'>, (1+1j)), None, None, None)
3694
3695 Thus when save() was called on newargstup (the 2nd item) recursion
3696 ensued. Of course, the bug was in the complex class which had a
3697 broken __getnewargs__() that emitted another complex object. But,
3698 the point, here, is it is quite easy to end up with a broken reduce
3699 function. */
3700
3701 /* Save the class and its __new__ arguments. */
3702 if (save(self, cls, 0) < 0)
3703 return -1;
3704
3705 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3706 if (newargtup == NULL)
3707 return -1;
3708
3709 p = save(self, newargtup, 0);
3710 Py_DECREF(newargtup);
3711 if (p < 0)
3712 return -1;
3713
3714 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003715 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003716 return -1;
3717 }
3718 else { /* Not using NEWOBJ. */
3719 if (save(self, callable, 0) < 0 ||
3720 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003721 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003722 return -1;
3723 }
3724
3725 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3726 the caller do not want to memoize the object. Not particularly useful,
3727 but that is to mimic the behavior save_reduce() in pickle.py when
3728 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003729 if (obj != NULL) {
3730 /* If the object is already in the memo, this means it is
3731 recursive. In this case, throw away everything we put on the
3732 stack, and fetch the object back from the memo. */
3733 if (PyMemoTable_Get(self->memo, obj)) {
3734 const char pop_op = POP;
3735
3736 if (_Pickler_Write(self, &pop_op, 1) < 0)
3737 return -1;
3738 if (memo_get(self, obj) < 0)
3739 return -1;
3740
3741 return 0;
3742 }
3743 else if (memo_put(self, obj) < 0)
3744 return -1;
3745 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003746
3747 if (listitems && batch_list(self, listitems) < 0)
3748 return -1;
3749
3750 if (dictitems && batch_dict(self, dictitems) < 0)
3751 return -1;
3752
3753 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003754 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003755 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003756 return -1;
3757 }
3758
3759 return 0;
3760}
3761
3762static int
3763save(PicklerObject *self, PyObject *obj, int pers_save)
3764{
3765 PyTypeObject *type;
3766 PyObject *reduce_func = NULL;
3767 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003768 int status = 0;
3769
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003770 if (_Pickler_OpcodeBoundary(self) < 0)
3771 return -1;
3772
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003773 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003774 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003775
3776 /* The extra pers_save argument is necessary to avoid calling save_pers()
3777 on its returned object. */
3778 if (!pers_save && self->pers_func) {
3779 /* save_pers() returns:
3780 -1 to signal an error;
3781 0 if it did nothing successfully;
3782 1 if a persistent id was saved.
3783 */
3784 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3785 goto done;
3786 }
3787
3788 type = Py_TYPE(obj);
3789
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003790 /* The old cPickle had an optimization that used switch-case statement
3791 dispatching on the first letter of the type name. This has was removed
3792 since benchmarks shown that this optimization was actually slowing
3793 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003794
3795 /* Atom types; these aren't memoized, so don't check the memo. */
3796
3797 if (obj == Py_None) {
3798 status = save_none(self, obj);
3799 goto done;
3800 }
3801 else if (obj == Py_False || obj == Py_True) {
3802 status = save_bool(self, obj);
3803 goto done;
3804 }
3805 else if (type == &PyLong_Type) {
3806 status = save_long(self, obj);
3807 goto done;
3808 }
3809 else if (type == &PyFloat_Type) {
3810 status = save_float(self, obj);
3811 goto done;
3812 }
3813
3814 /* Check the memo to see if it has the object. If so, generate
3815 a GET (or BINGET) opcode, instead of pickling the object
3816 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003817 if (PyMemoTable_Get(self->memo, obj)) {
3818 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003819 goto error;
3820 goto done;
3821 }
3822
3823 if (type == &PyBytes_Type) {
3824 status = save_bytes(self, obj);
3825 goto done;
3826 }
3827 else if (type == &PyUnicode_Type) {
3828 status = save_unicode(self, obj);
3829 goto done;
3830 }
3831 else if (type == &PyDict_Type) {
3832 status = save_dict(self, obj);
3833 goto done;
3834 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003835 else if (type == &PySet_Type) {
3836 status = save_set(self, obj);
3837 goto done;
3838 }
3839 else if (type == &PyFrozenSet_Type) {
3840 status = save_frozenset(self, obj);
3841 goto done;
3842 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003843 else if (type == &PyList_Type) {
3844 status = save_list(self, obj);
3845 goto done;
3846 }
3847 else if (type == &PyTuple_Type) {
3848 status = save_tuple(self, obj);
3849 goto done;
3850 }
3851 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003852 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003853 goto done;
3854 }
3855 else if (type == &PyFunction_Type) {
3856 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003857 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003858 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003859
3860 /* XXX: This part needs some unit tests. */
3861
3862 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003863 * self.dispatch_table, copyreg.dispatch_table, the object's
3864 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003865 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003866 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003867 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003868 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3869 (PyObject *)type);
3870 if (reduce_func == NULL) {
3871 if (PyErr_Occurred()) {
3872 goto error;
3873 }
3874 } else {
3875 /* PyDict_GetItemWithError() returns a borrowed reference.
3876 Increase the reference count to be consistent with
3877 PyObject_GetItem and _PyObject_GetAttrId used below. */
3878 Py_INCREF(reduce_func);
3879 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003880 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003881 reduce_func = PyObject_GetItem(self->dispatch_table,
3882 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003883 if (reduce_func == NULL) {
3884 if (PyErr_ExceptionMatches(PyExc_KeyError))
3885 PyErr_Clear();
3886 else
3887 goto error;
3888 }
3889 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003890 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003891 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003892 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003893 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003894 else if (PyType_IsSubtype(type, &PyType_Type)) {
3895 status = save_global(self, obj, NULL);
3896 goto done;
3897 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003898 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003899 _Py_IDENTIFIER(__reduce__);
3900 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003901
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003902
3903 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3904 automatically defined as __reduce__. While this is convenient, this
3905 make it impossible to know which method was actually called. Of
3906 course, this is not a big deal. But still, it would be nice to let
3907 the user know which method was called when something go
3908 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3909 don't actually have to check for a __reduce__ method. */
3910
3911 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003912 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003913 if (reduce_func != NULL) {
3914 PyObject *proto;
3915 proto = PyLong_FromLong(self->proto);
3916 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003917 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003918 }
3919 }
3920 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003921 PickleState *st = _Pickle_GetGlobalState();
3922
3923 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003924 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003925 }
3926 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003927 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003928 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003929 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003930 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003931 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02003932 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 }
3934 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003935 PyErr_Format(st->PicklingError,
3936 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003937 type->tp_name, obj);
3938 goto error;
3939 }
3940 }
3941 }
3942
3943 if (reduce_value == NULL)
3944 goto error;
3945
3946 if (PyUnicode_Check(reduce_value)) {
3947 status = save_global(self, obj, reduce_value);
3948 goto done;
3949 }
3950
3951 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003952 PickleState *st = _Pickle_GetGlobalState();
3953 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003954 "__reduce__ must return a string or tuple");
3955 goto error;
3956 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003957
3958 status = save_reduce(self, reduce_value, obj);
3959
3960 if (0) {
3961 error:
3962 status = -1;
3963 }
3964 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003965
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003966 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003967 Py_XDECREF(reduce_func);
3968 Py_XDECREF(reduce_value);
3969
3970 return status;
3971}
3972
3973static int
3974dump(PicklerObject *self, PyObject *obj)
3975{
3976 const char stop_op = STOP;
3977
3978 if (self->proto >= 2) {
3979 char header[2];
3980
3981 header[0] = PROTO;
3982 assert(self->proto >= 0 && self->proto < 256);
3983 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003984 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003985 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003986 if (self->proto >= 4)
3987 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003988 }
3989
3990 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003991 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003992 return -1;
3993
3994 return 0;
3995}
3996
Larry Hastings61272b72014-01-07 12:41:53 -08003997/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003998
3999_pickle.Pickler.clear_memo
4000
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004001Clears the pickler's "memo".
4002
4003The memo is the data structure that remembers which objects the
4004pickler has already seen, so that shared or recursive objects are
4005pickled by reference and not by value. This method is useful when
4006re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004007[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004008
Larry Hastings3cceb382014-01-04 11:09:09 -08004009static PyObject *
4010_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004011/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004012{
4013 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004014 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004015
4016 Py_RETURN_NONE;
4017}
4018
Larry Hastings61272b72014-01-07 12:41:53 -08004019/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004020
4021_pickle.Pickler.dump
4022
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004023 obj: object
4024 /
4025
4026Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004027[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004028
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004029static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004030_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004031/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004032{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004033 /* Check whether the Pickler was initialized correctly (issue3664).
4034 Developers often forget to call __init__() in their subclasses, which
4035 would trigger a segfault without this check. */
4036 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004037 PickleState *st = _Pickle_GetGlobalState();
4038 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004039 "Pickler.__init__() was not called by %s.__init__()",
4040 Py_TYPE(self)->tp_name);
4041 return NULL;
4042 }
4043
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004044 if (_Pickler_ClearBuffer(self) < 0)
4045 return NULL;
4046
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004047 if (dump(self, obj) < 0)
4048 return NULL;
4049
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004050 if (_Pickler_FlushToFile(self) < 0)
4051 return NULL;
4052
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004053 Py_RETURN_NONE;
4054}
4055
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004056/*[clinic input]
4057
4058_pickle.Pickler.__sizeof__ -> Py_ssize_t
4059
4060Returns size in memory, in bytes.
4061[clinic start generated code]*/
4062
4063static Py_ssize_t
4064_pickle_Pickler___sizeof___impl(PicklerObject *self)
4065/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4066{
4067 Py_ssize_t res, s;
4068
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004069 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004070 if (self->memo != NULL) {
4071 res += sizeof(PyMemoTable);
4072 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4073 }
4074 if (self->output_buffer != NULL) {
4075 s = _PySys_GetSizeOf(self->output_buffer);
4076 if (s == -1)
4077 return -1;
4078 res += s;
4079 }
4080 return res;
4081}
4082
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004083static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004084 _PICKLE_PICKLER_DUMP_METHODDEF
4085 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004086 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004087 {NULL, NULL} /* sentinel */
4088};
4089
4090static void
4091Pickler_dealloc(PicklerObject *self)
4092{
4093 PyObject_GC_UnTrack(self);
4094
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004095 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004096 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004097 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004098 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099 Py_XDECREF(self->fast_memo);
4100
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004101 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004102
4103 Py_TYPE(self)->tp_free((PyObject *)self);
4104}
4105
4106static int
4107Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4108{
4109 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004110 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004111 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004112 Py_VISIT(self->fast_memo);
4113 return 0;
4114}
4115
4116static int
4117Pickler_clear(PicklerObject *self)
4118{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004119 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004120 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004121 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004122 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004123 Py_CLEAR(self->fast_memo);
4124
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004125 if (self->memo != NULL) {
4126 PyMemoTable *memo = self->memo;
4127 self->memo = NULL;
4128 PyMemoTable_Del(memo);
4129 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004130 return 0;
4131}
4132
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004133
Larry Hastings61272b72014-01-07 12:41:53 -08004134/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004135
4136_pickle.Pickler.__init__
4137
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004138 file: object
4139 protocol: object = NULL
4140 fix_imports: bool = True
4141
4142This takes a binary file for writing a pickle data stream.
4143
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004144The optional *protocol* argument tells the pickler to use the given
4145protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4146protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004147
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004148Specifying a negative protocol version selects the highest protocol
4149version supported. The higher the protocol used, the more recent the
4150version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004151
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004152The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004153bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004154writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004155this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004156
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004157If *fix_imports* is True and protocol is less than 3, pickle will try
4158to map the new Python 3 names to the old module names used in Python
41592, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004160[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004161
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004162static int
Larry Hastings89964c42015-04-14 18:07:59 -04004163_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4164 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004165/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004166{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004167 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004168 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004169
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004170 /* In case of multiple __init__() calls, clear previous content. */
4171 if (self->write != NULL)
4172 (void)Pickler_clear(self);
4173
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004174 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004175 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004176
4177 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004178 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004179
4180 /* memo and output_buffer may have already been created in _Pickler_New */
4181 if (self->memo == NULL) {
4182 self->memo = PyMemoTable_New();
4183 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004184 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004185 }
4186 self->output_len = 0;
4187 if (self->output_buffer == NULL) {
4188 self->max_output_len = WRITE_BUF_SIZE;
4189 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4190 self->max_output_len);
4191 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004192 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004193 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004194
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004195 self->fast = 0;
4196 self->fast_nesting = 0;
4197 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004198 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004199 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4200 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4201 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004202 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004203 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004204 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004205 self->dispatch_table = NULL;
4206 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4207 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4208 &PyId_dispatch_table);
4209 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004210 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004211 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004212
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004213 return 0;
4214}
4215
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004216
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004217/* Define a proxy object for the Pickler's internal memo object. This is to
4218 * avoid breaking code like:
4219 * pickler.memo.clear()
4220 * and
4221 * pickler.memo = saved_memo
4222 * Is this a good idea? Not really, but we don't want to break code that uses
4223 * it. Note that we don't implement the entire mapping API here. This is
4224 * intentional, as these should be treated as black-box implementation details.
4225 */
4226
Larry Hastings61272b72014-01-07 12:41:53 -08004227/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004228_pickle.PicklerMemoProxy.clear
4229
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004230Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004231[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004232
Larry Hastings3cceb382014-01-04 11:09:09 -08004233static PyObject *
4234_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004235/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004236{
4237 if (self->pickler->memo)
4238 PyMemoTable_Clear(self->pickler->memo);
4239 Py_RETURN_NONE;
4240}
4241
Larry Hastings61272b72014-01-07 12:41:53 -08004242/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004243_pickle.PicklerMemoProxy.copy
4244
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004245Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004246[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004247
Larry Hastings3cceb382014-01-04 11:09:09 -08004248static PyObject *
4249_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004250/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004251{
4252 Py_ssize_t i;
4253 PyMemoTable *memo;
4254 PyObject *new_memo = PyDict_New();
4255 if (new_memo == NULL)
4256 return NULL;
4257
4258 memo = self->pickler->memo;
4259 for (i = 0; i < memo->mt_allocated; ++i) {
4260 PyMemoEntry entry = memo->mt_table[i];
4261 if (entry.me_key != NULL) {
4262 int status;
4263 PyObject *key, *value;
4264
4265 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004266 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004267
4268 if (key == NULL || value == NULL) {
4269 Py_XDECREF(key);
4270 Py_XDECREF(value);
4271 goto error;
4272 }
4273 status = PyDict_SetItem(new_memo, key, value);
4274 Py_DECREF(key);
4275 Py_DECREF(value);
4276 if (status < 0)
4277 goto error;
4278 }
4279 }
4280 return new_memo;
4281
4282 error:
4283 Py_XDECREF(new_memo);
4284 return NULL;
4285}
4286
Larry Hastings61272b72014-01-07 12:41:53 -08004287/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004288_pickle.PicklerMemoProxy.__reduce__
4289
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004290Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004291[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004292
Larry Hastings3cceb382014-01-04 11:09:09 -08004293static PyObject *
4294_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004295/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004296{
4297 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004298 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004299 if (contents == NULL)
4300 return NULL;
4301
4302 reduce_value = PyTuple_New(2);
4303 if (reduce_value == NULL) {
4304 Py_DECREF(contents);
4305 return NULL;
4306 }
4307 dict_args = PyTuple_New(1);
4308 if (dict_args == NULL) {
4309 Py_DECREF(contents);
4310 Py_DECREF(reduce_value);
4311 return NULL;
4312 }
4313 PyTuple_SET_ITEM(dict_args, 0, contents);
4314 Py_INCREF((PyObject *)&PyDict_Type);
4315 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4316 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4317 return reduce_value;
4318}
4319
4320static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004321 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4322 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4323 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004324 {NULL, NULL} /* sentinel */
4325};
4326
4327static void
4328PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4329{
4330 PyObject_GC_UnTrack(self);
4331 Py_XDECREF(self->pickler);
4332 PyObject_GC_Del((PyObject *)self);
4333}
4334
4335static int
4336PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4337 visitproc visit, void *arg)
4338{
4339 Py_VISIT(self->pickler);
4340 return 0;
4341}
4342
4343static int
4344PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4345{
4346 Py_CLEAR(self->pickler);
4347 return 0;
4348}
4349
4350static PyTypeObject PicklerMemoProxyType = {
4351 PyVarObject_HEAD_INIT(NULL, 0)
4352 "_pickle.PicklerMemoProxy", /*tp_name*/
4353 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4354 0,
4355 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4356 0, /* tp_print */
4357 0, /* tp_getattr */
4358 0, /* tp_setattr */
4359 0, /* tp_compare */
4360 0, /* tp_repr */
4361 0, /* tp_as_number */
4362 0, /* tp_as_sequence */
4363 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004364 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004365 0, /* tp_call */
4366 0, /* tp_str */
4367 PyObject_GenericGetAttr, /* tp_getattro */
4368 PyObject_GenericSetAttr, /* tp_setattro */
4369 0, /* tp_as_buffer */
4370 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4371 0, /* tp_doc */
4372 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4373 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4374 0, /* tp_richcompare */
4375 0, /* tp_weaklistoffset */
4376 0, /* tp_iter */
4377 0, /* tp_iternext */
4378 picklerproxy_methods, /* tp_methods */
4379};
4380
4381static PyObject *
4382PicklerMemoProxy_New(PicklerObject *pickler)
4383{
4384 PicklerMemoProxyObject *self;
4385
4386 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4387 if (self == NULL)
4388 return NULL;
4389 Py_INCREF(pickler);
4390 self->pickler = pickler;
4391 PyObject_GC_Track(self);
4392 return (PyObject *)self;
4393}
4394
4395/*****************************************************************************/
4396
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004397static PyObject *
4398Pickler_get_memo(PicklerObject *self)
4399{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004400 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004401}
4402
4403static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004404Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004405{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004406 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004407
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004408 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004409 PyErr_SetString(PyExc_TypeError,
4410 "attribute deletion is not supported");
4411 return -1;
4412 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004413
4414 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4415 PicklerObject *pickler =
4416 ((PicklerMemoProxyObject *)obj)->pickler;
4417
4418 new_memo = PyMemoTable_Copy(pickler->memo);
4419 if (new_memo == NULL)
4420 return -1;
4421 }
4422 else if (PyDict_Check(obj)) {
4423 Py_ssize_t i = 0;
4424 PyObject *key, *value;
4425
4426 new_memo = PyMemoTable_New();
4427 if (new_memo == NULL)
4428 return -1;
4429
4430 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004431 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004432 PyObject *memo_obj;
4433
4434 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4435 PyErr_SetString(PyExc_TypeError,
4436 "'memo' values must be 2-item tuples");
4437 goto error;
4438 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004439 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004440 if (memo_id == -1 && PyErr_Occurred())
4441 goto error;
4442 memo_obj = PyTuple_GET_ITEM(value, 1);
4443 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4444 goto error;
4445 }
4446 }
4447 else {
4448 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004449 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004450 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004451 return -1;
4452 }
4453
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004454 PyMemoTable_Del(self->memo);
4455 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004456
4457 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004458
4459 error:
4460 if (new_memo)
4461 PyMemoTable_Del(new_memo);
4462 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004463}
4464
4465static PyObject *
4466Pickler_get_persid(PicklerObject *self)
4467{
4468 if (self->pers_func == NULL)
4469 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4470 else
4471 Py_INCREF(self->pers_func);
4472 return self->pers_func;
4473}
4474
4475static int
4476Pickler_set_persid(PicklerObject *self, PyObject *value)
4477{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004478 if (value == NULL) {
4479 PyErr_SetString(PyExc_TypeError,
4480 "attribute deletion is not supported");
4481 return -1;
4482 }
4483 if (!PyCallable_Check(value)) {
4484 PyErr_SetString(PyExc_TypeError,
4485 "persistent_id must be a callable taking one argument");
4486 return -1;
4487 }
4488
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004489 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004490 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004491
4492 return 0;
4493}
4494
4495static PyMemberDef Pickler_members[] = {
4496 {"bin", T_INT, offsetof(PicklerObject, bin)},
4497 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004498 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004499 {NULL}
4500};
4501
4502static PyGetSetDef Pickler_getsets[] = {
4503 {"memo", (getter)Pickler_get_memo,
4504 (setter)Pickler_set_memo},
4505 {"persistent_id", (getter)Pickler_get_persid,
4506 (setter)Pickler_set_persid},
4507 {NULL}
4508};
4509
4510static PyTypeObject Pickler_Type = {
4511 PyVarObject_HEAD_INIT(NULL, 0)
4512 "_pickle.Pickler" , /*tp_name*/
4513 sizeof(PicklerObject), /*tp_basicsize*/
4514 0, /*tp_itemsize*/
4515 (destructor)Pickler_dealloc, /*tp_dealloc*/
4516 0, /*tp_print*/
4517 0, /*tp_getattr*/
4518 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004519 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004520 0, /*tp_repr*/
4521 0, /*tp_as_number*/
4522 0, /*tp_as_sequence*/
4523 0, /*tp_as_mapping*/
4524 0, /*tp_hash*/
4525 0, /*tp_call*/
4526 0, /*tp_str*/
4527 0, /*tp_getattro*/
4528 0, /*tp_setattro*/
4529 0, /*tp_as_buffer*/
4530 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004531 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004532 (traverseproc)Pickler_traverse, /*tp_traverse*/
4533 (inquiry)Pickler_clear, /*tp_clear*/
4534 0, /*tp_richcompare*/
4535 0, /*tp_weaklistoffset*/
4536 0, /*tp_iter*/
4537 0, /*tp_iternext*/
4538 Pickler_methods, /*tp_methods*/
4539 Pickler_members, /*tp_members*/
4540 Pickler_getsets, /*tp_getset*/
4541 0, /*tp_base*/
4542 0, /*tp_dict*/
4543 0, /*tp_descr_get*/
4544 0, /*tp_descr_set*/
4545 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004546 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004547 PyType_GenericAlloc, /*tp_alloc*/
4548 PyType_GenericNew, /*tp_new*/
4549 PyObject_GC_Del, /*tp_free*/
4550 0, /*tp_is_gc*/
4551};
4552
Victor Stinner121aab42011-09-29 23:40:53 +02004553/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004554
4555 XXX: It would be nice to able to avoid Python function call overhead, by
4556 using directly the C version of find_class(), when find_class() is not
4557 overridden by a subclass. Although, this could become rather hackish. A
4558 simpler optimization would be to call the C function when self is not a
4559 subclass instance. */
4560static PyObject *
4561find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4562{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004563 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004564
Victor Stinner55ba38a2016-12-09 16:09:30 +01004565 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4566 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004567}
4568
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004569static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570marker(UnpicklerObject *self)
4571{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004572 Py_ssize_t mark;
4573
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004574 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004575 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004576 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004577 return -1;
4578 }
4579
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004580 mark = self->marks[--self->num_marks];
4581 self->stack->mark_set = self->num_marks != 0;
4582 self->stack->fence = self->num_marks ?
4583 self->marks[self->num_marks - 1] : 0;
4584 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004585}
4586
4587static int
4588load_none(UnpicklerObject *self)
4589{
4590 PDATA_APPEND(self->stack, Py_None, -1);
4591 return 0;
4592}
4593
4594static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004595load_int(UnpicklerObject *self)
4596{
4597 PyObject *value;
4598 char *endptr, *s;
4599 Py_ssize_t len;
4600 long x;
4601
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004602 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004603 return -1;
4604 if (len < 2)
4605 return bad_readline();
4606
4607 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004608 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004609 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004610 x = strtol(s, &endptr, 0);
4611
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004612 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004613 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004614 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004615 errno = 0;
4616 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004617 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004618 if (value == NULL) {
4619 PyErr_SetString(PyExc_ValueError,
4620 "could not convert string to int");
4621 return -1;
4622 }
4623 }
4624 else {
4625 if (len == 3 && (x == 0 || x == 1)) {
4626 if ((value = PyBool_FromLong(x)) == NULL)
4627 return -1;
4628 }
4629 else {
4630 if ((value = PyLong_FromLong(x)) == NULL)
4631 return -1;
4632 }
4633 }
4634
4635 PDATA_PUSH(self->stack, value, -1);
4636 return 0;
4637}
4638
4639static int
4640load_bool(UnpicklerObject *self, PyObject *boolean)
4641{
4642 assert(boolean == Py_True || boolean == Py_False);
4643 PDATA_APPEND(self->stack, boolean, -1);
4644 return 0;
4645}
4646
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004647/* s contains x bytes of an unsigned little-endian integer. Return its value
4648 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4649 */
4650static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004651calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004652{
4653 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004654 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004655 size_t x = 0;
4656
Serhiy Storchakae0606192015-09-29 22:10:07 +03004657 if (nbytes > (int)sizeof(size_t)) {
4658 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4659 * have 64-bit size that can't be represented on 32-bit platform.
4660 */
4661 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4662 if (s[i])
4663 return -1;
4664 }
4665 nbytes = (int)sizeof(size_t);
4666 }
4667 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004668 x |= (size_t) s[i] << (8 * i);
4669 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004670
4671 if (x > PY_SSIZE_T_MAX)
4672 return -1;
4673 else
4674 return (Py_ssize_t) x;
4675}
4676
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004677/* s contains x bytes of a little-endian integer. Return its value as a
4678 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004679 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004680 * of x-platform bugs.
4681 */
4682static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004683calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004684{
4685 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004686 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004687 long x = 0;
4688
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004689 for (i = 0; i < nbytes; i++) {
4690 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004691 }
4692
4693 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4694 * is signed, so on a box with longs bigger than 4 bytes we need
4695 * to extend a BININT's sign bit to the full width.
4696 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004697 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004698 x |= -(x & (1L << 31));
4699 }
4700
4701 return x;
4702}
4703
4704static int
4705load_binintx(UnpicklerObject *self, char *s, int size)
4706{
4707 PyObject *value;
4708 long x;
4709
4710 x = calc_binint(s, size);
4711
4712 if ((value = PyLong_FromLong(x)) == NULL)
4713 return -1;
4714
4715 PDATA_PUSH(self->stack, value, -1);
4716 return 0;
4717}
4718
4719static int
4720load_binint(UnpicklerObject *self)
4721{
4722 char *s;
4723
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004724 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004725 return -1;
4726
4727 return load_binintx(self, s, 4);
4728}
4729
4730static int
4731load_binint1(UnpicklerObject *self)
4732{
4733 char *s;
4734
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004735 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004736 return -1;
4737
4738 return load_binintx(self, s, 1);
4739}
4740
4741static int
4742load_binint2(UnpicklerObject *self)
4743{
4744 char *s;
4745
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004746 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004747 return -1;
4748
4749 return load_binintx(self, s, 2);
4750}
4751
4752static int
4753load_long(UnpicklerObject *self)
4754{
4755 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004756 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004757 Py_ssize_t len;
4758
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004759 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004760 return -1;
4761 if (len < 2)
4762 return bad_readline();
4763
Mark Dickinson8dd05142009-01-20 20:43:58 +00004764 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4765 the 'L' before calling PyLong_FromString. In order to maintain
4766 compatibility with Python 3.0.0, we don't actually *require*
4767 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004768 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004769 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004770 /* XXX: Should the base argument explicitly set to 10? */
4771 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004772 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004773 return -1;
4774
4775 PDATA_PUSH(self->stack, value, -1);
4776 return 0;
4777}
4778
4779/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4780 * data following.
4781 */
4782static int
4783load_counted_long(UnpicklerObject *self, int size)
4784{
4785 PyObject *value;
4786 char *nbytes;
4787 char *pdata;
4788
4789 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004790 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004791 return -1;
4792
4793 size = calc_binint(nbytes, size);
4794 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004795 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004796 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004797 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004798 "LONG pickle has negative byte count");
4799 return -1;
4800 }
4801
4802 if (size == 0)
4803 value = PyLong_FromLong(0L);
4804 else {
4805 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004806 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004807 return -1;
4808 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4809 1 /* little endian */ , 1 /* signed */ );
4810 }
4811 if (value == NULL)
4812 return -1;
4813 PDATA_PUSH(self->stack, value, -1);
4814 return 0;
4815}
4816
4817static int
4818load_float(UnpicklerObject *self)
4819{
4820 PyObject *value;
4821 char *endptr, *s;
4822 Py_ssize_t len;
4823 double d;
4824
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004825 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004826 return -1;
4827 if (len < 2)
4828 return bad_readline();
4829
4830 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004831 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4832 if (d == -1.0 && PyErr_Occurred())
4833 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004834 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004835 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4836 return -1;
4837 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004838 value = PyFloat_FromDouble(d);
4839 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004840 return -1;
4841
4842 PDATA_PUSH(self->stack, value, -1);
4843 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004844}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004845
4846static int
4847load_binfloat(UnpicklerObject *self)
4848{
4849 PyObject *value;
4850 double x;
4851 char *s;
4852
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004853 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004854 return -1;
4855
4856 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4857 if (x == -1.0 && PyErr_Occurred())
4858 return -1;
4859
4860 if ((value = PyFloat_FromDouble(x)) == NULL)
4861 return -1;
4862
4863 PDATA_PUSH(self->stack, value, -1);
4864 return 0;
4865}
4866
4867static int
4868load_string(UnpicklerObject *self)
4869{
4870 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004871 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004872 Py_ssize_t len;
4873 char *s, *p;
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;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004877 /* Strip the newline */
4878 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004879 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004880 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004881 p = s + 1;
4882 len -= 2;
4883 }
4884 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004885 PickleState *st = _Pickle_GetGlobalState();
4886 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004887 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004888 return -1;
4889 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004890 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004891
4892 /* Use the PyBytes API to decode the string, since that is what is used
4893 to encode, and then coerce the result to Unicode. */
4894 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004895 if (bytes == NULL)
4896 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004897
4898 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4899 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4900 if (strcmp(self->encoding, "bytes") == 0) {
4901 obj = bytes;
4902 }
4903 else {
4904 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4905 Py_DECREF(bytes);
4906 if (obj == NULL) {
4907 return -1;
4908 }
4909 }
4910
4911 PDATA_PUSH(self->stack, obj, -1);
4912 return 0;
4913}
4914
4915static int
4916load_counted_binstring(UnpicklerObject *self, int nbytes)
4917{
4918 PyObject *obj;
4919 Py_ssize_t size;
4920 char *s;
4921
4922 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004923 return -1;
4924
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004925 size = calc_binsize(s, nbytes);
4926 if (size < 0) {
4927 PickleState *st = _Pickle_GetGlobalState();
4928 PyErr_Format(st->UnpicklingError,
4929 "BINSTRING exceeds system's maximum size of %zd bytes",
4930 PY_SSIZE_T_MAX);
4931 return -1;
4932 }
4933
4934 if (_Unpickler_Read(self, &s, size) < 0)
4935 return -1;
4936
4937 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4938 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4939 if (strcmp(self->encoding, "bytes") == 0) {
4940 obj = PyBytes_FromStringAndSize(s, size);
4941 }
4942 else {
4943 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4944 }
4945 if (obj == NULL) {
4946 return -1;
4947 }
4948
4949 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004950 return 0;
4951}
4952
4953static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004954load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004955{
4956 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004957 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004958 char *s;
4959
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004960 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004961 return -1;
4962
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004963 size = calc_binsize(s, nbytes);
4964 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004965 PyErr_Format(PyExc_OverflowError,
4966 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004967 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968 return -1;
4969 }
4970
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004971 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004972 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004973
4974 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004975 if (bytes == NULL)
4976 return -1;
4977
4978 PDATA_PUSH(self->stack, bytes, -1);
4979 return 0;
4980}
4981
4982static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004983load_unicode(UnpicklerObject *self)
4984{
4985 PyObject *str;
4986 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004987 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004988
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004989 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004990 return -1;
4991 if (len < 1)
4992 return bad_readline();
4993
4994 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4995 if (str == NULL)
4996 return -1;
4997
4998 PDATA_PUSH(self->stack, str, -1);
4999 return 0;
5000}
5001
5002static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005003load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005004{
5005 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005006 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005007 char *s;
5008
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005009 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005010 return -1;
5011
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005012 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005013 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005014 PyErr_Format(PyExc_OverflowError,
5015 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005016 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005017 return -1;
5018 }
5019
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005020 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005021 return -1;
5022
Victor Stinner485fb562010-04-13 11:07:24 +00005023 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005024 if (str == NULL)
5025 return -1;
5026
5027 PDATA_PUSH(self->stack, str, -1);
5028 return 0;
5029}
5030
5031static int
Victor Stinner21b47112016-03-14 18:09:39 +01005032load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005033{
5034 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005035
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005036 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005037 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005038
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005039 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040 if (tuple == NULL)
5041 return -1;
5042 PDATA_PUSH(self->stack, tuple, -1);
5043 return 0;
5044}
5045
5046static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005047load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005049 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005050
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005051 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005052 return -1;
5053
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005054 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005055}
5056
5057static int
5058load_empty_list(UnpicklerObject *self)
5059{
5060 PyObject *list;
5061
5062 if ((list = PyList_New(0)) == NULL)
5063 return -1;
5064 PDATA_PUSH(self->stack, list, -1);
5065 return 0;
5066}
5067
5068static int
5069load_empty_dict(UnpicklerObject *self)
5070{
5071 PyObject *dict;
5072
5073 if ((dict = PyDict_New()) == NULL)
5074 return -1;
5075 PDATA_PUSH(self->stack, dict, -1);
5076 return 0;
5077}
5078
5079static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005080load_empty_set(UnpicklerObject *self)
5081{
5082 PyObject *set;
5083
5084 if ((set = PySet_New(NULL)) == NULL)
5085 return -1;
5086 PDATA_PUSH(self->stack, set, -1);
5087 return 0;
5088}
5089
5090static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005091load_list(UnpicklerObject *self)
5092{
5093 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005094 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005095
5096 if ((i = marker(self)) < 0)
5097 return -1;
5098
5099 list = Pdata_poplist(self->stack, i);
5100 if (list == NULL)
5101 return -1;
5102 PDATA_PUSH(self->stack, list, -1);
5103 return 0;
5104}
5105
5106static int
5107load_dict(UnpicklerObject *self)
5108{
5109 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005110 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005111
5112 if ((i = marker(self)) < 0)
5113 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005114 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005115
5116 if ((dict = PyDict_New()) == NULL)
5117 return -1;
5118
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005119 if ((j - i) % 2 != 0) {
5120 PickleState *st = _Pickle_GetGlobalState();
5121 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005122 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005123 return -1;
5124 }
5125
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005126 for (k = i + 1; k < j; k += 2) {
5127 key = self->stack->data[k - 1];
5128 value = self->stack->data[k];
5129 if (PyDict_SetItem(dict, key, value) < 0) {
5130 Py_DECREF(dict);
5131 return -1;
5132 }
5133 }
5134 Pdata_clear(self->stack, i);
5135 PDATA_PUSH(self->stack, dict, -1);
5136 return 0;
5137}
5138
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005139static int
5140load_frozenset(UnpicklerObject *self)
5141{
5142 PyObject *items;
5143 PyObject *frozenset;
5144 Py_ssize_t i;
5145
5146 if ((i = marker(self)) < 0)
5147 return -1;
5148
5149 items = Pdata_poptuple(self->stack, i);
5150 if (items == NULL)
5151 return -1;
5152
5153 frozenset = PyFrozenSet_New(items);
5154 Py_DECREF(items);
5155 if (frozenset == NULL)
5156 return -1;
5157
5158 PDATA_PUSH(self->stack, frozenset, -1);
5159 return 0;
5160}
5161
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005162static PyObject *
5163instantiate(PyObject *cls, PyObject *args)
5164{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005165 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005166 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005167 /* Caller must assure args are a tuple. Normally, args come from
5168 Pdata_poptuple which packs objects from the top of the stack
5169 into a newly created tuple. */
5170 assert(PyTuple_Check(args));
5171 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005172 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005173 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005174 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005175 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005176 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005177
Victor Stinner55ba38a2016-12-09 16:09:30 +01005178 result = _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005179 }
5180 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005181}
5182
5183static int
5184load_obj(UnpicklerObject *self)
5185{
5186 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005187 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005188
5189 if ((i = marker(self)) < 0)
5190 return -1;
5191
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005192 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005193 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005194
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005195 args = Pdata_poptuple(self->stack, i + 1);
5196 if (args == NULL)
5197 return -1;
5198
5199 PDATA_POP(self->stack, cls);
5200 if (cls) {
5201 obj = instantiate(cls, args);
5202 Py_DECREF(cls);
5203 }
5204 Py_DECREF(args);
5205 if (obj == NULL)
5206 return -1;
5207
5208 PDATA_PUSH(self->stack, obj, -1);
5209 return 0;
5210}
5211
5212static int
5213load_inst(UnpicklerObject *self)
5214{
5215 PyObject *cls = NULL;
5216 PyObject *args = NULL;
5217 PyObject *obj = NULL;
5218 PyObject *module_name;
5219 PyObject *class_name;
5220 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005221 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005222 char *s;
5223
5224 if ((i = marker(self)) < 0)
5225 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005226 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005227 return -1;
5228 if (len < 2)
5229 return bad_readline();
5230
5231 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5232 identifiers are permitted in Python 3.0, since the INST opcode is only
5233 supported by older protocols on Python 2.x. */
5234 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5235 if (module_name == NULL)
5236 return -1;
5237
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005238 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005239 if (len < 2) {
5240 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005241 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005242 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005243 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005244 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005245 cls = find_class(self, module_name, class_name);
5246 Py_DECREF(class_name);
5247 }
5248 }
5249 Py_DECREF(module_name);
5250
5251 if (cls == NULL)
5252 return -1;
5253
5254 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5255 obj = instantiate(cls, args);
5256 Py_DECREF(args);
5257 }
5258 Py_DECREF(cls);
5259
5260 if (obj == NULL)
5261 return -1;
5262
5263 PDATA_PUSH(self->stack, obj, -1);
5264 return 0;
5265}
5266
5267static int
5268load_newobj(UnpicklerObject *self)
5269{
5270 PyObject *args = NULL;
5271 PyObject *clsraw = NULL;
5272 PyTypeObject *cls; /* clsraw cast to its true type */
5273 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005274 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005275
5276 /* Stack is ... cls argtuple, and we want to call
5277 * cls.__new__(cls, *argtuple).
5278 */
5279 PDATA_POP(self->stack, args);
5280 if (args == NULL)
5281 goto error;
5282 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005283 PyErr_SetString(st->UnpicklingError,
5284 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005285 goto error;
5286 }
5287
5288 PDATA_POP(self->stack, clsraw);
5289 cls = (PyTypeObject *)clsraw;
5290 if (cls == NULL)
5291 goto error;
5292 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005293 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005294 "isn't a type object");
5295 goto error;
5296 }
5297 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005298 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005299 "has NULL tp_new");
5300 goto error;
5301 }
5302
5303 /* Call __new__. */
5304 obj = cls->tp_new(cls, args, NULL);
5305 if (obj == NULL)
5306 goto error;
5307
5308 Py_DECREF(args);
5309 Py_DECREF(clsraw);
5310 PDATA_PUSH(self->stack, obj, -1);
5311 return 0;
5312
5313 error:
5314 Py_XDECREF(args);
5315 Py_XDECREF(clsraw);
5316 return -1;
5317}
5318
5319static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005320load_newobj_ex(UnpicklerObject *self)
5321{
5322 PyObject *cls, *args, *kwargs;
5323 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005324 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005325
5326 PDATA_POP(self->stack, kwargs);
5327 if (kwargs == NULL) {
5328 return -1;
5329 }
5330 PDATA_POP(self->stack, args);
5331 if (args == NULL) {
5332 Py_DECREF(kwargs);
5333 return -1;
5334 }
5335 PDATA_POP(self->stack, cls);
5336 if (cls == NULL) {
5337 Py_DECREF(kwargs);
5338 Py_DECREF(args);
5339 return -1;
5340 }
Larry Hastings61272b72014-01-07 12:41:53 -08005341
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005342 if (!PyType_Check(cls)) {
5343 Py_DECREF(kwargs);
5344 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005345 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005346 "NEWOBJ_EX class argument must be a type, not %.200s",
5347 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005348 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005349 return -1;
5350 }
5351
5352 if (((PyTypeObject *)cls)->tp_new == NULL) {
5353 Py_DECREF(kwargs);
5354 Py_DECREF(args);
5355 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005356 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005357 "NEWOBJ_EX class argument doesn't have __new__");
5358 return -1;
5359 }
5360 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5361 Py_DECREF(kwargs);
5362 Py_DECREF(args);
5363 Py_DECREF(cls);
5364 if (obj == NULL) {
5365 return -1;
5366 }
5367 PDATA_PUSH(self->stack, obj, -1);
5368 return 0;
5369}
5370
5371static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005372load_global(UnpicklerObject *self)
5373{
5374 PyObject *global = NULL;
5375 PyObject *module_name;
5376 PyObject *global_name;
5377 Py_ssize_t len;
5378 char *s;
5379
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005380 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005381 return -1;
5382 if (len < 2)
5383 return bad_readline();
5384 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5385 if (!module_name)
5386 return -1;
5387
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005388 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005389 if (len < 2) {
5390 Py_DECREF(module_name);
5391 return bad_readline();
5392 }
5393 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5394 if (global_name) {
5395 global = find_class(self, module_name, global_name);
5396 Py_DECREF(global_name);
5397 }
5398 }
5399 Py_DECREF(module_name);
5400
5401 if (global == NULL)
5402 return -1;
5403 PDATA_PUSH(self->stack, global, -1);
5404 return 0;
5405}
5406
5407static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005408load_stack_global(UnpicklerObject *self)
5409{
5410 PyObject *global;
5411 PyObject *module_name;
5412 PyObject *global_name;
5413
5414 PDATA_POP(self->stack, global_name);
5415 PDATA_POP(self->stack, module_name);
5416 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5417 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005418 PickleState *st = _Pickle_GetGlobalState();
5419 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005420 Py_XDECREF(global_name);
5421 Py_XDECREF(module_name);
5422 return -1;
5423 }
5424 global = find_class(self, module_name, global_name);
5425 Py_DECREF(global_name);
5426 Py_DECREF(module_name);
5427 if (global == NULL)
5428 return -1;
5429 PDATA_PUSH(self->stack, global, -1);
5430 return 0;
5431}
5432
5433static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005434load_persid(UnpicklerObject *self)
5435{
5436 PyObject *pid;
5437 Py_ssize_t len;
5438 char *s;
5439
5440 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005441 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005442 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005443 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005444 return bad_readline();
5445
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005446 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5447 if (pid == NULL) {
5448 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5449 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5450 "persistent IDs in protocol 0 must be "
5451 "ASCII strings");
5452 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005453 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005454 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005455
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005456 /* This does not leak since _Pickle_FastCall() steals the reference
5457 to pid first. */
5458 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005459 if (pid == NULL)
5460 return -1;
5461
5462 PDATA_PUSH(self->stack, pid, -1);
5463 return 0;
5464 }
5465 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005466 PickleState *st = _Pickle_GetGlobalState();
5467 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005468 "A load persistent id instruction was encountered,\n"
5469 "but no persistent_load function was specified.");
5470 return -1;
5471 }
5472}
5473
5474static int
5475load_binpersid(UnpicklerObject *self)
5476{
5477 PyObject *pid;
5478
5479 if (self->pers_func) {
5480 PDATA_POP(self->stack, pid);
5481 if (pid == NULL)
5482 return -1;
5483
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005484 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005485 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005486 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005487 if (pid == NULL)
5488 return -1;
5489
5490 PDATA_PUSH(self->stack, pid, -1);
5491 return 0;
5492 }
5493 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005494 PickleState *st = _Pickle_GetGlobalState();
5495 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005496 "A load persistent id instruction was encountered,\n"
5497 "but no persistent_load function was specified.");
5498 return -1;
5499 }
5500}
5501
5502static int
5503load_pop(UnpicklerObject *self)
5504{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005505 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005506
5507 /* Note that we split the (pickle.py) stack into two stacks,
5508 * an object stack and a mark stack. We have to be clever and
5509 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005510 * mark stack first, and only signalling a stack underflow if
5511 * the object stack is empty and the mark stack doesn't match
5512 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005513 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005514 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005515 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005516 self->stack->mark_set = self->num_marks != 0;
5517 self->stack->fence = self->num_marks ?
5518 self->marks[self->num_marks - 1] : 0;
5519 } else if (len <= self->stack->fence)
5520 return Pdata_stack_underflow(self->stack);
5521 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005522 len--;
5523 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005524 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005525 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005526 return 0;
5527}
5528
5529static int
5530load_pop_mark(UnpicklerObject *self)
5531{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005532 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005533
5534 if ((i = marker(self)) < 0)
5535 return -1;
5536
5537 Pdata_clear(self->stack, i);
5538
5539 return 0;
5540}
5541
5542static int
5543load_dup(UnpicklerObject *self)
5544{
5545 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005546 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005547
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005548 if (len <= self->stack->fence)
5549 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005550 last = self->stack->data[len - 1];
5551 PDATA_APPEND(self->stack, last, -1);
5552 return 0;
5553}
5554
5555static int
5556load_get(UnpicklerObject *self)
5557{
5558 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005559 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560 Py_ssize_t len;
5561 char *s;
5562
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005563 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005564 return -1;
5565 if (len < 2)
5566 return bad_readline();
5567
5568 key = PyLong_FromString(s, NULL, 10);
5569 if (key == NULL)
5570 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005571 idx = PyLong_AsSsize_t(key);
5572 if (idx == -1 && PyErr_Occurred()) {
5573 Py_DECREF(key);
5574 return -1;
5575 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005576
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005577 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005578 if (value == NULL) {
5579 if (!PyErr_Occurred())
5580 PyErr_SetObject(PyExc_KeyError, key);
5581 Py_DECREF(key);
5582 return -1;
5583 }
5584 Py_DECREF(key);
5585
5586 PDATA_APPEND(self->stack, value, -1);
5587 return 0;
5588}
5589
5590static int
5591load_binget(UnpicklerObject *self)
5592{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005593 PyObject *value;
5594 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005595 char *s;
5596
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005597 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005598 return -1;
5599
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005600 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005602 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005604 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005605 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005607 Py_DECREF(key);
5608 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609 return -1;
5610 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005611
5612 PDATA_APPEND(self->stack, value, -1);
5613 return 0;
5614}
5615
5616static int
5617load_long_binget(UnpicklerObject *self)
5618{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005619 PyObject *value;
5620 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005621 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005623 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005624 return -1;
5625
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005626 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005628 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005629 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005630 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005631 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005633 Py_DECREF(key);
5634 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635 return -1;
5636 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005637
5638 PDATA_APPEND(self->stack, value, -1);
5639 return 0;
5640}
5641
5642/* Push an object from the extension registry (EXT[124]). nbytes is
5643 * the number of bytes following the opcode, holding the index (code) value.
5644 */
5645static int
5646load_extension(UnpicklerObject *self, int nbytes)
5647{
5648 char *codebytes; /* the nbytes bytes after the opcode */
5649 long code; /* calc_binint returns long */
5650 PyObject *py_code; /* code as a Python int */
5651 PyObject *obj; /* the object to push */
5652 PyObject *pair; /* (module_name, class_name) */
5653 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005654 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005655
5656 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005657 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005658 return -1;
5659 code = calc_binint(codebytes, nbytes);
5660 if (code <= 0) { /* note that 0 is forbidden */
5661 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005662 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005663 return -1;
5664 }
5665
5666 /* Look for the code in the cache. */
5667 py_code = PyLong_FromLong(code);
5668 if (py_code == NULL)
5669 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005670 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005671 if (obj != NULL) {
5672 /* Bingo. */
5673 Py_DECREF(py_code);
5674 PDATA_APPEND(self->stack, obj, -1);
5675 return 0;
5676 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005677 if (PyErr_Occurred()) {
5678 Py_DECREF(py_code);
5679 return -1;
5680 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005681
5682 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005683 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684 if (pair == NULL) {
5685 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005686 if (!PyErr_Occurred()) {
5687 PyErr_Format(PyExc_ValueError, "unregistered extension "
5688 "code %ld", code);
5689 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 return -1;
5691 }
5692 /* Since the extension registry is manipulable via Python code,
5693 * confirm that pair is really a 2-tuple of strings.
5694 */
5695 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5696 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5697 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5698 Py_DECREF(py_code);
5699 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5700 "isn't a 2-tuple of strings", code);
5701 return -1;
5702 }
5703 /* Load the object. */
5704 obj = find_class(self, module_name, class_name);
5705 if (obj == NULL) {
5706 Py_DECREF(py_code);
5707 return -1;
5708 }
5709 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005710 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005711 Py_DECREF(py_code);
5712 if (code < 0) {
5713 Py_DECREF(obj);
5714 return -1;
5715 }
5716 PDATA_PUSH(self->stack, obj, -1);
5717 return 0;
5718}
5719
5720static int
5721load_put(UnpicklerObject *self)
5722{
5723 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005724 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005725 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005726 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005727
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005728 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005729 return -1;
5730 if (len < 2)
5731 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005732 if (Py_SIZE(self->stack) <= self->stack->fence)
5733 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005734 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005735
5736 key = PyLong_FromString(s, NULL, 10);
5737 if (key == NULL)
5738 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005739 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005740 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005741 if (idx < 0) {
5742 if (!PyErr_Occurred())
5743 PyErr_SetString(PyExc_ValueError,
5744 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005745 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005746 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005747
5748 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005749}
5750
5751static int
5752load_binput(UnpicklerObject *self)
5753{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005754 PyObject *value;
5755 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005756 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005757
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005758 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005759 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005760
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005761 if (Py_SIZE(self->stack) <= self->stack->fence)
5762 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005763 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005765 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005767 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005768}
5769
5770static int
5771load_long_binput(UnpicklerObject *self)
5772{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005773 PyObject *value;
5774 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005775 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005777 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005778 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005779
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005780 if (Py_SIZE(self->stack) <= self->stack->fence)
5781 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005782 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005783
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005784 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005785 if (idx < 0) {
5786 PyErr_SetString(PyExc_ValueError,
5787 "negative LONG_BINPUT argument");
5788 return -1;
5789 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005790
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005791 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005792}
5793
5794static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005795load_memoize(UnpicklerObject *self)
5796{
5797 PyObject *value;
5798
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005799 if (Py_SIZE(self->stack) <= self->stack->fence)
5800 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005801 value = self->stack->data[Py_SIZE(self->stack) - 1];
5802
5803 return _Unpickler_MemoPut(self, self->memo_len, value);
5804}
5805
5806static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005807do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005808{
5809 PyObject *value;
5810 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005811 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005812
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005813 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005814 if (x > len || x <= self->stack->fence)
5815 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005816 if (len == x) /* nothing to do */
5817 return 0;
5818
5819 list = self->stack->data[x - 1];
5820
5821 if (PyList_Check(list)) {
5822 PyObject *slice;
5823 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005824 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005825
5826 slice = Pdata_poplist(self->stack, x);
5827 if (!slice)
5828 return -1;
5829 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005830 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005831 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005832 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005833 }
5834 else {
5835 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005836 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005837
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005838 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005839 if (append_func == NULL)
5840 return -1;
5841 for (i = x; i < len; i++) {
5842 PyObject *result;
5843
5844 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005845 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846 if (result == NULL) {
5847 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005848 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005849 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005850 return -1;
5851 }
5852 Py_DECREF(result);
5853 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005854 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005855 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005856 }
5857
5858 return 0;
5859}
5860
5861static int
5862load_append(UnpicklerObject *self)
5863{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005864 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5865 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005866 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005867}
5868
5869static int
5870load_appends(UnpicklerObject *self)
5871{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005872 Py_ssize_t i = marker(self);
5873 if (i < 0)
5874 return -1;
5875 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005876}
5877
5878static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005879do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005880{
5881 PyObject *value, *key;
5882 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005883 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005884 int status = 0;
5885
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005886 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005887 if (x > len || x <= self->stack->fence)
5888 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005889 if (len == x) /* nothing to do */
5890 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005891 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005892 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005893 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005894 PyErr_SetString(st->UnpicklingError,
5895 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005896 return -1;
5897 }
5898
5899 /* Here, dict does not actually need to be a PyDict; it could be anything
5900 that supports the __setitem__ attribute. */
5901 dict = self->stack->data[x - 1];
5902
5903 for (i = x + 1; i < len; i += 2) {
5904 key = self->stack->data[i - 1];
5905 value = self->stack->data[i];
5906 if (PyObject_SetItem(dict, key, value) < 0) {
5907 status = -1;
5908 break;
5909 }
5910 }
5911
5912 Pdata_clear(self->stack, x);
5913 return status;
5914}
5915
5916static int
5917load_setitem(UnpicklerObject *self)
5918{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005919 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005920}
5921
5922static int
5923load_setitems(UnpicklerObject *self)
5924{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005925 Py_ssize_t i = marker(self);
5926 if (i < 0)
5927 return -1;
5928 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005929}
5930
5931static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005932load_additems(UnpicklerObject *self)
5933{
5934 PyObject *set;
5935 Py_ssize_t mark, len, i;
5936
5937 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005938 if (mark < 0)
5939 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005940 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005941 if (mark > len || mark <= self->stack->fence)
5942 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005943 if (len == mark) /* nothing to do */
5944 return 0;
5945
5946 set = self->stack->data[mark - 1];
5947
5948 if (PySet_Check(set)) {
5949 PyObject *items;
5950 int status;
5951
5952 items = Pdata_poptuple(self->stack, mark);
5953 if (items == NULL)
5954 return -1;
5955
5956 status = _PySet_Update(set, items);
5957 Py_DECREF(items);
5958 return status;
5959 }
5960 else {
5961 PyObject *add_func;
5962 _Py_IDENTIFIER(add);
5963
5964 add_func = _PyObject_GetAttrId(set, &PyId_add);
5965 if (add_func == NULL)
5966 return -1;
5967 for (i = mark; i < len; i++) {
5968 PyObject *result;
5969 PyObject *item;
5970
5971 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005972 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005973 if (result == NULL) {
5974 Pdata_clear(self->stack, i + 1);
5975 Py_SIZE(self->stack) = mark;
5976 return -1;
5977 }
5978 Py_DECREF(result);
5979 }
5980 Py_SIZE(self->stack) = mark;
5981 }
5982
5983 return 0;
5984}
5985
5986static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005987load_build(UnpicklerObject *self)
5988{
5989 PyObject *state, *inst, *slotstate;
5990 PyObject *setstate;
5991 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005992 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005993
5994 /* Stack is ... instance, state. We want to leave instance at
5995 * the stack top, possibly mutated via instance.__setstate__(state).
5996 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005997 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
5998 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005999
6000 PDATA_POP(self->stack, state);
6001 if (state == NULL)
6002 return -1;
6003
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006004 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006005
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006006 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006007 if (setstate == NULL) {
6008 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6009 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006010 else {
6011 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006012 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006013 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006014 }
6015 else {
6016 PyObject *result;
6017
6018 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006019 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006020 Py_DECREF(setstate);
6021 if (result == NULL)
6022 return -1;
6023 Py_DECREF(result);
6024 return 0;
6025 }
6026
6027 /* A default __setstate__. First see whether state embeds a
6028 * slot state dict too (a proto 2 addition).
6029 */
6030 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6031 PyObject *tmp = state;
6032
6033 state = PyTuple_GET_ITEM(tmp, 0);
6034 slotstate = PyTuple_GET_ITEM(tmp, 1);
6035 Py_INCREF(state);
6036 Py_INCREF(slotstate);
6037 Py_DECREF(tmp);
6038 }
6039 else
6040 slotstate = NULL;
6041
6042 /* Set inst.__dict__ from the state dict (if any). */
6043 if (state != Py_None) {
6044 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006045 PyObject *d_key, *d_value;
6046 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006047 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006048
6049 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006050 PickleState *st = _Pickle_GetGlobalState();
6051 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006052 goto error;
6053 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006054 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006055 if (dict == NULL)
6056 goto error;
6057
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006058 i = 0;
6059 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6060 /* normally the keys for instance attributes are
6061 interned. we should try to do that here. */
6062 Py_INCREF(d_key);
6063 if (PyUnicode_CheckExact(d_key))
6064 PyUnicode_InternInPlace(&d_key);
6065 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6066 Py_DECREF(d_key);
6067 goto error;
6068 }
6069 Py_DECREF(d_key);
6070 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006071 Py_DECREF(dict);
6072 }
6073
6074 /* Also set instance attributes from the slotstate dict (if any). */
6075 if (slotstate != NULL) {
6076 PyObject *d_key, *d_value;
6077 Py_ssize_t i;
6078
6079 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006080 PickleState *st = _Pickle_GetGlobalState();
6081 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006082 "slot state is not a dictionary");
6083 goto error;
6084 }
6085 i = 0;
6086 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6087 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6088 goto error;
6089 }
6090 }
6091
6092 if (0) {
6093 error:
6094 status = -1;
6095 }
6096
6097 Py_DECREF(state);
6098 Py_XDECREF(slotstate);
6099 return status;
6100}
6101
6102static int
6103load_mark(UnpicklerObject *self)
6104{
6105
6106 /* Note that we split the (pickle.py) stack into two stacks, an
6107 * object stack and a mark stack. Here we push a mark onto the
6108 * mark stack.
6109 */
6110
6111 if ((self->num_marks + 1) >= self->marks_size) {
6112 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006113
6114 /* Use the size_t type to check for overflow. */
6115 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006116 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006117 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006118 PyErr_NoMemory();
6119 return -1;
6120 }
6121
6122 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006123 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006124 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006125 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6126 if (self->marks == NULL) {
6127 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006128 PyErr_NoMemory();
6129 return -1;
6130 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006131 self->marks_size = (Py_ssize_t)alloc;
6132 }
6133
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006134 self->stack->mark_set = 1;
6135 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006136
6137 return 0;
6138}
6139
6140static int
6141load_reduce(UnpicklerObject *self)
6142{
6143 PyObject *callable = NULL;
6144 PyObject *argtup = NULL;
6145 PyObject *obj = NULL;
6146
6147 PDATA_POP(self->stack, argtup);
6148 if (argtup == NULL)
6149 return -1;
6150 PDATA_POP(self->stack, callable);
6151 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006152 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006153 Py_DECREF(callable);
6154 }
6155 Py_DECREF(argtup);
6156
6157 if (obj == NULL)
6158 return -1;
6159
6160 PDATA_PUSH(self->stack, obj, -1);
6161 return 0;
6162}
6163
6164/* Just raises an error if we don't know the protocol specified. PROTO
6165 * is the first opcode for protocols >= 2.
6166 */
6167static int
6168load_proto(UnpicklerObject *self)
6169{
6170 char *s;
6171 int i;
6172
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006173 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006174 return -1;
6175
6176 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006177 if (i <= HIGHEST_PROTOCOL) {
6178 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006179 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006180 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006181
6182 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6183 return -1;
6184}
6185
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006186static int
6187load_frame(UnpicklerObject *self)
6188{
6189 char *s;
6190 Py_ssize_t frame_len;
6191
6192 if (_Unpickler_Read(self, &s, 8) < 0)
6193 return -1;
6194
6195 frame_len = calc_binsize(s, 8);
6196 if (frame_len < 0) {
6197 PyErr_Format(PyExc_OverflowError,
6198 "FRAME length exceeds system's maximum of %zd bytes",
6199 PY_SSIZE_T_MAX);
6200 return -1;
6201 }
6202
6203 if (_Unpickler_Read(self, &s, frame_len) < 0)
6204 return -1;
6205
6206 /* Rewind to start of frame */
6207 self->next_read_idx -= frame_len;
6208 return 0;
6209}
6210
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006211static PyObject *
6212load(UnpicklerObject *self)
6213{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006214 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006215 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006216
6217 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006218 self->stack->mark_set = 0;
6219 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006220 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006221 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006222 Pdata_clear(self->stack, 0);
6223
6224 /* Convenient macros for the dispatch while-switch loop just below. */
6225#define OP(opcode, load_func) \
6226 case opcode: if (load_func(self) < 0) break; continue;
6227
6228#define OP_ARG(opcode, load_func, arg) \
6229 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6230
6231 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006232 if (_Unpickler_Read(self, &s, 1) < 0) {
6233 PickleState *st = _Pickle_GetGlobalState();
6234 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6235 PyErr_Format(PyExc_EOFError, "Ran out of input");
6236 }
6237 return NULL;
6238 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006239
6240 switch ((enum opcode)s[0]) {
6241 OP(NONE, load_none)
6242 OP(BININT, load_binint)
6243 OP(BININT1, load_binint1)
6244 OP(BININT2, load_binint2)
6245 OP(INT, load_int)
6246 OP(LONG, load_long)
6247 OP_ARG(LONG1, load_counted_long, 1)
6248 OP_ARG(LONG4, load_counted_long, 4)
6249 OP(FLOAT, load_float)
6250 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006251 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6252 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6253 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6254 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6255 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006256 OP(STRING, load_string)
6257 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006258 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6259 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6260 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006261 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6262 OP_ARG(TUPLE1, load_counted_tuple, 1)
6263 OP_ARG(TUPLE2, load_counted_tuple, 2)
6264 OP_ARG(TUPLE3, load_counted_tuple, 3)
6265 OP(TUPLE, load_tuple)
6266 OP(EMPTY_LIST, load_empty_list)
6267 OP(LIST, load_list)
6268 OP(EMPTY_DICT, load_empty_dict)
6269 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006270 OP(EMPTY_SET, load_empty_set)
6271 OP(ADDITEMS, load_additems)
6272 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006273 OP(OBJ, load_obj)
6274 OP(INST, load_inst)
6275 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006276 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006277 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006278 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006279 OP(APPEND, load_append)
6280 OP(APPENDS, load_appends)
6281 OP(BUILD, load_build)
6282 OP(DUP, load_dup)
6283 OP(BINGET, load_binget)
6284 OP(LONG_BINGET, load_long_binget)
6285 OP(GET, load_get)
6286 OP(MARK, load_mark)
6287 OP(BINPUT, load_binput)
6288 OP(LONG_BINPUT, load_long_binput)
6289 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006290 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006291 OP(POP, load_pop)
6292 OP(POP_MARK, load_pop_mark)
6293 OP(SETITEM, load_setitem)
6294 OP(SETITEMS, load_setitems)
6295 OP(PERSID, load_persid)
6296 OP(BINPERSID, load_binpersid)
6297 OP(REDUCE, load_reduce)
6298 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006299 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006300 OP_ARG(EXT1, load_extension, 1)
6301 OP_ARG(EXT2, load_extension, 2)
6302 OP_ARG(EXT4, load_extension, 4)
6303 OP_ARG(NEWTRUE, load_bool, Py_True)
6304 OP_ARG(NEWFALSE, load_bool, Py_False)
6305
6306 case STOP:
6307 break;
6308
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006309 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006310 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006311 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006312 unsigned char c = (unsigned char) *s;
6313 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6314 PyErr_Format(st->UnpicklingError,
6315 "invalid load key, '%c'.", c);
6316 }
6317 else {
6318 PyErr_Format(st->UnpicklingError,
6319 "invalid load key, '\\x%02x'.", c);
6320 }
6321 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006322 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006323 }
6324
6325 break; /* and we are done! */
6326 }
6327
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006328 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006329 return NULL;
6330 }
6331
Victor Stinner2ae57e32013-10-31 13:39:23 +01006332 if (_Unpickler_SkipConsumed(self) < 0)
6333 return NULL;
6334
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006335 PDATA_POP(self->stack, value);
6336 return value;
6337}
6338
Larry Hastings61272b72014-01-07 12:41:53 -08006339/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006340
6341_pickle.Unpickler.load
6342
6343Load a pickle.
6344
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006345Read a pickled object representation from the open file object given
6346in the constructor, and return the reconstituted object hierarchy
6347specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006348[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006349
Larry Hastings3cceb382014-01-04 11:09:09 -08006350static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006351_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006352/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006353{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006354 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006355
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006356 /* Check whether the Unpickler was initialized correctly. This prevents
6357 segfaulting if a subclass overridden __init__ with a function that does
6358 not call Unpickler.__init__(). Here, we simply ensure that self->read
6359 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006360 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006361 PickleState *st = _Pickle_GetGlobalState();
6362 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006363 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006364 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006365 return NULL;
6366 }
6367
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006368 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006369}
6370
6371/* The name of find_class() is misleading. In newer pickle protocols, this
6372 function is used for loading any global (i.e., functions), not just
6373 classes. The name is kept only for backward compatibility. */
6374
Larry Hastings61272b72014-01-07 12:41:53 -08006375/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006376
6377_pickle.Unpickler.find_class
6378
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006379 module_name: object
6380 global_name: object
6381 /
6382
6383Return an object from a specified module.
6384
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006385If necessary, the module will be imported. Subclasses may override
6386this method (e.g. to restrict unpickling of arbitrary classes and
6387functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006388
6389This method is called whenever a class or a function object is
6390needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006391[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006392
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006393static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006394_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6395 PyObject *module_name,
6396 PyObject *global_name)
6397/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006398{
6399 PyObject *global;
6400 PyObject *modules_dict;
6401 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006402 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006403
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006404 /* Try to map the old names used in Python 2.x to the new ones used in
6405 Python 3.x. We do this only with old pickle protocols and when the
6406 user has not disabled the feature. */
6407 if (self->proto < 3 && self->fix_imports) {
6408 PyObject *key;
6409 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006410 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006411
6412 /* Check if the global (i.e., a function or a class) was renamed
6413 or moved to another module. */
6414 key = PyTuple_Pack(2, module_name, global_name);
6415 if (key == NULL)
6416 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006417 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006418 Py_DECREF(key);
6419 if (item) {
6420 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6421 PyErr_Format(PyExc_RuntimeError,
6422 "_compat_pickle.NAME_MAPPING values should be "
6423 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6424 return NULL;
6425 }
6426 module_name = PyTuple_GET_ITEM(item, 0);
6427 global_name = PyTuple_GET_ITEM(item, 1);
6428 if (!PyUnicode_Check(module_name) ||
6429 !PyUnicode_Check(global_name)) {
6430 PyErr_Format(PyExc_RuntimeError,
6431 "_compat_pickle.NAME_MAPPING values should be "
6432 "pairs of str, not (%.200s, %.200s)",
6433 Py_TYPE(module_name)->tp_name,
6434 Py_TYPE(global_name)->tp_name);
6435 return NULL;
6436 }
6437 }
6438 else if (PyErr_Occurred()) {
6439 return NULL;
6440 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006441 else {
6442 /* Check if the module was renamed. */
6443 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6444 if (item) {
6445 if (!PyUnicode_Check(item)) {
6446 PyErr_Format(PyExc_RuntimeError,
6447 "_compat_pickle.IMPORT_MAPPING values should be "
6448 "strings, not %.200s", Py_TYPE(item)->tp_name);
6449 return NULL;
6450 }
6451 module_name = item;
6452 }
6453 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006454 return NULL;
6455 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006456 }
6457 }
6458
Victor Stinnerbb520202013-11-06 22:40:41 +01006459 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006460 if (modules_dict == NULL) {
6461 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006462 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006463 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006464
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006465 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006466 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006467 if (PyErr_Occurred())
6468 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006469 module = PyImport_Import(module_name);
6470 if (module == NULL)
6471 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006472 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006473 Py_DECREF(module);
6474 }
Victor Stinner121aab42011-09-29 23:40:53 +02006475 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006476 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006477 }
6478 return global;
6479}
6480
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006481/*[clinic input]
6482
6483_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6484
6485Returns size in memory, in bytes.
6486[clinic start generated code]*/
6487
6488static Py_ssize_t
6489_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6490/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6491{
6492 Py_ssize_t res;
6493
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006494 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006495 if (self->memo != NULL)
6496 res += self->memo_size * sizeof(PyObject *);
6497 if (self->marks != NULL)
6498 res += self->marks_size * sizeof(Py_ssize_t);
6499 if (self->input_line != NULL)
6500 res += strlen(self->input_line) + 1;
6501 if (self->encoding != NULL)
6502 res += strlen(self->encoding) + 1;
6503 if (self->errors != NULL)
6504 res += strlen(self->errors) + 1;
6505 return res;
6506}
6507
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006508static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006509 _PICKLE_UNPICKLER_LOAD_METHODDEF
6510 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006511 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006512 {NULL, NULL} /* sentinel */
6513};
6514
6515static void
6516Unpickler_dealloc(UnpicklerObject *self)
6517{
6518 PyObject_GC_UnTrack((PyObject *)self);
6519 Py_XDECREF(self->readline);
6520 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006521 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006522 Py_XDECREF(self->stack);
6523 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006524 if (self->buffer.buf != NULL) {
6525 PyBuffer_Release(&self->buffer);
6526 self->buffer.buf = NULL;
6527 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006528
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006529 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006530 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006531 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006532 PyMem_Free(self->encoding);
6533 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006534
6535 Py_TYPE(self)->tp_free((PyObject *)self);
6536}
6537
6538static int
6539Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6540{
6541 Py_VISIT(self->readline);
6542 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006543 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006544 Py_VISIT(self->stack);
6545 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546 return 0;
6547}
6548
6549static int
6550Unpickler_clear(UnpicklerObject *self)
6551{
6552 Py_CLEAR(self->readline);
6553 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006554 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006555 Py_CLEAR(self->stack);
6556 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006557 if (self->buffer.buf != NULL) {
6558 PyBuffer_Release(&self->buffer);
6559 self->buffer.buf = NULL;
6560 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006561
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006562 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006563 PyMem_Free(self->marks);
6564 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006565 PyMem_Free(self->input_line);
6566 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006567 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006568 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006569 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006570 self->errors = NULL;
6571
6572 return 0;
6573}
6574
Larry Hastings61272b72014-01-07 12:41:53 -08006575/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006576
6577_pickle.Unpickler.__init__
6578
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006579 file: object
6580 *
6581 fix_imports: bool = True
6582 encoding: str = 'ASCII'
6583 errors: str = 'strict'
6584
6585This takes a binary file for reading a pickle data stream.
6586
6587The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006588protocol argument is needed. Bytes past the pickled object's
6589representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006590
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006591The argument *file* must have two methods, a read() method that takes
6592an integer argument, and a readline() method that requires no
6593arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006594binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006595other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006596
6597Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006598which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006599generated by Python 2. If *fix_imports* is True, pickle will try to
6600map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006601*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006602instances pickled by Python 2; these default to 'ASCII' and 'strict',
6603respectively. The *encoding* can be 'bytes' to read these 8-bit
6604string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006605[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006606
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006607static int
Larry Hastings89964c42015-04-14 18:07:59 -04006608_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6609 int fix_imports, const char *encoding,
6610 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006611/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006612{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006613 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006614
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006615 /* In case of multiple __init__() calls, clear previous content. */
6616 if (self->read != NULL)
6617 (void)Unpickler_clear(self);
6618
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006619 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006620 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006621
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006622 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006623 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006624
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006625 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006626 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006627 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006628
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006629 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006630 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6631 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006632 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006633 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006634 }
6635 else {
6636 self->pers_func = NULL;
6637 }
6638
6639 self->stack = (Pdata *)Pdata_New();
6640 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006641 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006642
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006643 self->memo_size = 32;
6644 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006645 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006646 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006647
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006648 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006649
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650 return 0;
6651}
6652
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006653
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006654/* Define a proxy object for the Unpickler's internal memo object. This is to
6655 * avoid breaking code like:
6656 * unpickler.memo.clear()
6657 * and
6658 * unpickler.memo = saved_memo
6659 * Is this a good idea? Not really, but we don't want to break code that uses
6660 * it. Note that we don't implement the entire mapping API here. This is
6661 * intentional, as these should be treated as black-box implementation details.
6662 *
6663 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006664 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006665 */
6666
Larry Hastings61272b72014-01-07 12:41:53 -08006667/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006668_pickle.UnpicklerMemoProxy.clear
6669
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006670Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006671[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006672
Larry Hastings3cceb382014-01-04 11:09:09 -08006673static PyObject *
6674_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006675/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006676{
6677 _Unpickler_MemoCleanup(self->unpickler);
6678 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6679 if (self->unpickler->memo == NULL)
6680 return NULL;
6681 Py_RETURN_NONE;
6682}
6683
Larry Hastings61272b72014-01-07 12:41:53 -08006684/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006685_pickle.UnpicklerMemoProxy.copy
6686
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006687Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006688[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006689
Larry Hastings3cceb382014-01-04 11:09:09 -08006690static PyObject *
6691_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006692/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006693{
6694 Py_ssize_t i;
6695 PyObject *new_memo = PyDict_New();
6696 if (new_memo == NULL)
6697 return NULL;
6698
6699 for (i = 0; i < self->unpickler->memo_size; i++) {
6700 int status;
6701 PyObject *key, *value;
6702
6703 value = self->unpickler->memo[i];
6704 if (value == NULL)
6705 continue;
6706
6707 key = PyLong_FromSsize_t(i);
6708 if (key == NULL)
6709 goto error;
6710 status = PyDict_SetItem(new_memo, key, value);
6711 Py_DECREF(key);
6712 if (status < 0)
6713 goto error;
6714 }
6715 return new_memo;
6716
6717error:
6718 Py_DECREF(new_memo);
6719 return NULL;
6720}
6721
Larry Hastings61272b72014-01-07 12:41:53 -08006722/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006723_pickle.UnpicklerMemoProxy.__reduce__
6724
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006725Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006726[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006727
Larry Hastings3cceb382014-01-04 11:09:09 -08006728static PyObject *
6729_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006730/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006731{
6732 PyObject *reduce_value;
6733 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006734 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006735 if (contents == NULL)
6736 return NULL;
6737
6738 reduce_value = PyTuple_New(2);
6739 if (reduce_value == NULL) {
6740 Py_DECREF(contents);
6741 return NULL;
6742 }
6743 constructor_args = PyTuple_New(1);
6744 if (constructor_args == NULL) {
6745 Py_DECREF(contents);
6746 Py_DECREF(reduce_value);
6747 return NULL;
6748 }
6749 PyTuple_SET_ITEM(constructor_args, 0, contents);
6750 Py_INCREF((PyObject *)&PyDict_Type);
6751 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6752 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6753 return reduce_value;
6754}
6755
6756static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006757 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6758 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6759 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006760 {NULL, NULL} /* sentinel */
6761};
6762
6763static void
6764UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6765{
6766 PyObject_GC_UnTrack(self);
6767 Py_XDECREF(self->unpickler);
6768 PyObject_GC_Del((PyObject *)self);
6769}
6770
6771static int
6772UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6773 visitproc visit, void *arg)
6774{
6775 Py_VISIT(self->unpickler);
6776 return 0;
6777}
6778
6779static int
6780UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6781{
6782 Py_CLEAR(self->unpickler);
6783 return 0;
6784}
6785
6786static PyTypeObject UnpicklerMemoProxyType = {
6787 PyVarObject_HEAD_INIT(NULL, 0)
6788 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6789 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6790 0,
6791 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6792 0, /* tp_print */
6793 0, /* tp_getattr */
6794 0, /* tp_setattr */
6795 0, /* tp_compare */
6796 0, /* tp_repr */
6797 0, /* tp_as_number */
6798 0, /* tp_as_sequence */
6799 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006800 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006801 0, /* tp_call */
6802 0, /* tp_str */
6803 PyObject_GenericGetAttr, /* tp_getattro */
6804 PyObject_GenericSetAttr, /* tp_setattro */
6805 0, /* tp_as_buffer */
6806 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6807 0, /* tp_doc */
6808 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6809 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6810 0, /* tp_richcompare */
6811 0, /* tp_weaklistoffset */
6812 0, /* tp_iter */
6813 0, /* tp_iternext */
6814 unpicklerproxy_methods, /* tp_methods */
6815};
6816
6817static PyObject *
6818UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6819{
6820 UnpicklerMemoProxyObject *self;
6821
6822 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6823 &UnpicklerMemoProxyType);
6824 if (self == NULL)
6825 return NULL;
6826 Py_INCREF(unpickler);
6827 self->unpickler = unpickler;
6828 PyObject_GC_Track(self);
6829 return (PyObject *)self;
6830}
6831
6832/*****************************************************************************/
6833
6834
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006835static PyObject *
6836Unpickler_get_memo(UnpicklerObject *self)
6837{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006838 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006839}
6840
6841static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006842Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006843{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006844 PyObject **new_memo;
6845 Py_ssize_t new_memo_size = 0;
6846 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006847
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006848 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006849 PyErr_SetString(PyExc_TypeError,
6850 "attribute deletion is not supported");
6851 return -1;
6852 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006853
6854 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6855 UnpicklerObject *unpickler =
6856 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6857
6858 new_memo_size = unpickler->memo_size;
6859 new_memo = _Unpickler_NewMemo(new_memo_size);
6860 if (new_memo == NULL)
6861 return -1;
6862
6863 for (i = 0; i < new_memo_size; i++) {
6864 Py_XINCREF(unpickler->memo[i]);
6865 new_memo[i] = unpickler->memo[i];
6866 }
6867 }
6868 else if (PyDict_Check(obj)) {
6869 Py_ssize_t i = 0;
6870 PyObject *key, *value;
6871
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006872 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006873 new_memo = _Unpickler_NewMemo(new_memo_size);
6874 if (new_memo == NULL)
6875 return -1;
6876
6877 while (PyDict_Next(obj, &i, &key, &value)) {
6878 Py_ssize_t idx;
6879 if (!PyLong_Check(key)) {
6880 PyErr_SetString(PyExc_TypeError,
6881 "memo key must be integers");
6882 goto error;
6883 }
6884 idx = PyLong_AsSsize_t(key);
6885 if (idx == -1 && PyErr_Occurred())
6886 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006887 if (idx < 0) {
6888 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006889 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006890 goto error;
6891 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006892 if (_Unpickler_MemoPut(self, idx, value) < 0)
6893 goto error;
6894 }
6895 }
6896 else {
6897 PyErr_Format(PyExc_TypeError,
6898 "'memo' attribute must be an UnpicklerMemoProxy object"
6899 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006900 return -1;
6901 }
6902
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006903 _Unpickler_MemoCleanup(self);
6904 self->memo_size = new_memo_size;
6905 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006906
6907 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006908
6909 error:
6910 if (new_memo_size) {
6911 i = new_memo_size;
6912 while (--i >= 0) {
6913 Py_XDECREF(new_memo[i]);
6914 }
6915 PyMem_FREE(new_memo);
6916 }
6917 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006918}
6919
6920static PyObject *
6921Unpickler_get_persload(UnpicklerObject *self)
6922{
6923 if (self->pers_func == NULL)
6924 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6925 else
6926 Py_INCREF(self->pers_func);
6927 return self->pers_func;
6928}
6929
6930static int
6931Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6932{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006933 if (value == NULL) {
6934 PyErr_SetString(PyExc_TypeError,
6935 "attribute deletion is not supported");
6936 return -1;
6937 }
6938 if (!PyCallable_Check(value)) {
6939 PyErr_SetString(PyExc_TypeError,
6940 "persistent_load must be a callable taking "
6941 "one argument");
6942 return -1;
6943 }
6944
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006945 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03006946 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006947
6948 return 0;
6949}
6950
6951static PyGetSetDef Unpickler_getsets[] = {
6952 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6953 {"persistent_load", (getter)Unpickler_get_persload,
6954 (setter)Unpickler_set_persload},
6955 {NULL}
6956};
6957
6958static PyTypeObject Unpickler_Type = {
6959 PyVarObject_HEAD_INIT(NULL, 0)
6960 "_pickle.Unpickler", /*tp_name*/
6961 sizeof(UnpicklerObject), /*tp_basicsize*/
6962 0, /*tp_itemsize*/
6963 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6964 0, /*tp_print*/
6965 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006966 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006967 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006968 0, /*tp_repr*/
6969 0, /*tp_as_number*/
6970 0, /*tp_as_sequence*/
6971 0, /*tp_as_mapping*/
6972 0, /*tp_hash*/
6973 0, /*tp_call*/
6974 0, /*tp_str*/
6975 0, /*tp_getattro*/
6976 0, /*tp_setattro*/
6977 0, /*tp_as_buffer*/
6978 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006979 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006980 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6981 (inquiry)Unpickler_clear, /*tp_clear*/
6982 0, /*tp_richcompare*/
6983 0, /*tp_weaklistoffset*/
6984 0, /*tp_iter*/
6985 0, /*tp_iternext*/
6986 Unpickler_methods, /*tp_methods*/
6987 0, /*tp_members*/
6988 Unpickler_getsets, /*tp_getset*/
6989 0, /*tp_base*/
6990 0, /*tp_dict*/
6991 0, /*tp_descr_get*/
6992 0, /*tp_descr_set*/
6993 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006994 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006995 PyType_GenericAlloc, /*tp_alloc*/
6996 PyType_GenericNew, /*tp_new*/
6997 PyObject_GC_Del, /*tp_free*/
6998 0, /*tp_is_gc*/
6999};
7000
Larry Hastings61272b72014-01-07 12:41:53 -08007001/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007002
7003_pickle.dump
7004
7005 obj: object
7006 file: object
7007 protocol: object = NULL
7008 *
7009 fix_imports: bool = True
7010
7011Write a pickled representation of obj to the open file object file.
7012
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007013This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7014be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007015
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007016The optional *protocol* argument tells the pickler to use the given
7017protocol supported protocols are 0, 1, 2, 3 and 4. The default
7018protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007019
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007020Specifying a negative protocol version selects the highest protocol
7021version supported. The higher the protocol used, the more recent the
7022version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007023
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007024The *file* argument must have a write() method that accepts a single
7025bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007026writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007027this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007028
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007029If *fix_imports* is True and protocol is less than 3, pickle will try
7030to map the new Python 3 names to the old module names used in Python
70312, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007032[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007033
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007034static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007035_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007036 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007037/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007038{
7039 PicklerObject *pickler = _Pickler_New();
7040
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007041 if (pickler == NULL)
7042 return NULL;
7043
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007044 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007045 goto error;
7046
7047 if (_Pickler_SetOutputStream(pickler, file) < 0)
7048 goto error;
7049
7050 if (dump(pickler, obj) < 0)
7051 goto error;
7052
7053 if (_Pickler_FlushToFile(pickler) < 0)
7054 goto error;
7055
7056 Py_DECREF(pickler);
7057 Py_RETURN_NONE;
7058
7059 error:
7060 Py_XDECREF(pickler);
7061 return NULL;
7062}
7063
Larry Hastings61272b72014-01-07 12:41:53 -08007064/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007065
7066_pickle.dumps
7067
7068 obj: object
7069 protocol: object = NULL
7070 *
7071 fix_imports: bool = True
7072
7073Return the pickled representation of the object as a bytes object.
7074
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 -08007083If *fix_imports* is True and *protocol* is less than 3, pickle will
7084try to map the new Python 3 names to the old module names used in
7085Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007086[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007087
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007088static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007089_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007090 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007091/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007092{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007093 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007094 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007095
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007096 if (pickler == NULL)
7097 return NULL;
7098
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007099 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007100 goto error;
7101
7102 if (dump(pickler, obj) < 0)
7103 goto error;
7104
7105 result = _Pickler_GetString(pickler);
7106 Py_DECREF(pickler);
7107 return result;
7108
7109 error:
7110 Py_XDECREF(pickler);
7111 return NULL;
7112}
7113
Larry Hastings61272b72014-01-07 12:41:53 -08007114/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007115
7116_pickle.load
7117
7118 file: object
7119 *
7120 fix_imports: bool = True
7121 encoding: str = 'ASCII'
7122 errors: str = 'strict'
7123
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007124Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007125
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007126This is equivalent to ``Unpickler(file).load()``, but may be more
7127efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007128
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007129The protocol version of the pickle is detected automatically, so no
7130protocol argument is needed. Bytes past the pickled object's
7131representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007132
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007133The argument *file* must have two methods, a read() method that takes
7134an integer argument, and a readline() method that requires no
7135arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007136binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007137other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007138
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007139Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007140which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007141generated by Python 2. If *fix_imports* is True, pickle will try to
7142map the old Python 2 names to the new names used in Python 3. The
7143*encoding* and *errors* tell pickle how to decode 8-bit string
7144instances pickled by Python 2; these default to 'ASCII' and 'strict',
7145respectively. The *encoding* can be 'bytes' to read these 8-bit
7146string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007147[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007148
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007149static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007150_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007151 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007152/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007153{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007154 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007155 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007156
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007157 if (unpickler == NULL)
7158 return NULL;
7159
7160 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7161 goto error;
7162
7163 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7164 goto error;
7165
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007166 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007167
7168 result = load(unpickler);
7169 Py_DECREF(unpickler);
7170 return result;
7171
7172 error:
7173 Py_XDECREF(unpickler);
7174 return NULL;
7175}
7176
Larry Hastings61272b72014-01-07 12:41:53 -08007177/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007178
7179_pickle.loads
7180
7181 data: object
7182 *
7183 fix_imports: bool = True
7184 encoding: str = 'ASCII'
7185 errors: str = 'strict'
7186
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007187Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007189The protocol version of the pickle is detected automatically, so no
7190protocol argument is needed. Bytes past the pickled object's
7191representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007192
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007193Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007194which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007195generated by Python 2. If *fix_imports* is True, pickle will try to
7196map the old Python 2 names to the new names used in Python 3. The
7197*encoding* and *errors* tell pickle how to decode 8-bit string
7198instances pickled by Python 2; these default to 'ASCII' and 'strict',
7199respectively. The *encoding* can be 'bytes' to read these 8-bit
7200string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007201[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007202
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007203static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007204_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007205 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007206/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007207{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007208 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007209 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007210
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007211 if (unpickler == NULL)
7212 return NULL;
7213
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007214 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007215 goto error;
7216
7217 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7218 goto error;
7219
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007220 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007221
7222 result = load(unpickler);
7223 Py_DECREF(unpickler);
7224 return result;
7225
7226 error:
7227 Py_XDECREF(unpickler);
7228 return NULL;
7229}
7230
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007231static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007232 _PICKLE_DUMP_METHODDEF
7233 _PICKLE_DUMPS_METHODDEF
7234 _PICKLE_LOAD_METHODDEF
7235 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007236 {NULL, NULL} /* sentinel */
7237};
7238
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007239static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007240pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007241{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007242 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007243 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007244}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007245
Stefan Krahf483b0f2013-12-14 13:43:10 +01007246static void
7247pickle_free(PyObject *m)
7248{
7249 _Pickle_ClearState(_Pickle_GetState(m));
7250}
7251
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007252static int
7253pickle_traverse(PyObject *m, visitproc visit, void *arg)
7254{
7255 PickleState *st = _Pickle_GetState(m);
7256 Py_VISIT(st->PickleError);
7257 Py_VISIT(st->PicklingError);
7258 Py_VISIT(st->UnpicklingError);
7259 Py_VISIT(st->dispatch_table);
7260 Py_VISIT(st->extension_registry);
7261 Py_VISIT(st->extension_cache);
7262 Py_VISIT(st->inverted_registry);
7263 Py_VISIT(st->name_mapping_2to3);
7264 Py_VISIT(st->import_mapping_2to3);
7265 Py_VISIT(st->name_mapping_3to2);
7266 Py_VISIT(st->import_mapping_3to2);
7267 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007268 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007269 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007270}
7271
7272static struct PyModuleDef _picklemodule = {
7273 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007274 "_pickle", /* m_name */
7275 pickle_module_doc, /* m_doc */
7276 sizeof(PickleState), /* m_size */
7277 pickle_methods, /* m_methods */
7278 NULL, /* m_reload */
7279 pickle_traverse, /* m_traverse */
7280 pickle_clear, /* m_clear */
7281 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007282};
7283
7284PyMODINIT_FUNC
7285PyInit__pickle(void)
7286{
7287 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007288 PickleState *st;
7289
7290 m = PyState_FindModule(&_picklemodule);
7291 if (m) {
7292 Py_INCREF(m);
7293 return m;
7294 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007295
7296 if (PyType_Ready(&Unpickler_Type) < 0)
7297 return NULL;
7298 if (PyType_Ready(&Pickler_Type) < 0)
7299 return NULL;
7300 if (PyType_Ready(&Pdata_Type) < 0)
7301 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007302 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7303 return NULL;
7304 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7305 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007306
7307 /* Create the module and add the functions. */
7308 m = PyModule_Create(&_picklemodule);
7309 if (m == NULL)
7310 return NULL;
7311
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007312 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007313 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7314 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007315 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007316 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7317 return NULL;
7318
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007319 st = _Pickle_GetState(m);
7320
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007321 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007322 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7323 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007324 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007325 st->PicklingError = \
7326 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7327 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007328 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007329 st->UnpicklingError = \
7330 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7331 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007332 return NULL;
7333
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007334 Py_INCREF(st->PickleError);
7335 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007336 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007337 Py_INCREF(st->PicklingError);
7338 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007339 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007340 Py_INCREF(st->UnpicklingError);
7341 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007342 return NULL;
7343
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007344 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007345 return NULL;
7346
7347 return m;
7348}