blob: b1e1d49635721ee501936f941047a669468bd1ae [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 Storchaka58e41342015-03-31 14:07:24 +03001563get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001564 _Py_static_string(PyId_dot, ".");
1565 _Py_static_string(PyId_locals, "<locals>");
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);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001576 PyObject *result = PyUnicode_RichCompare(
1577 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1578 int is_equal = (result == Py_True);
1579 assert(PyBool_Check(result));
1580 Py_DECREF(result);
1581 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001582 if (obj == NULL)
1583 PyErr_Format(PyExc_AttributeError,
1584 "Can't pickle local object %R", name);
1585 else
1586 PyErr_Format(PyExc_AttributeError,
1587 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001588 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001589 return NULL;
1590 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001591 }
1592 return dotted_path;
1593}
1594
1595static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001596get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001597{
1598 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001599 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001600
1601 assert(PyList_CheckExact(names));
1602 Py_INCREF(obj);
1603 n = PyList_GET_SIZE(names);
1604 for (i = 0; i < n; i++) {
1605 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001606 Py_XDECREF(parent);
1607 parent = obj;
1608 obj = PyObject_GetAttr(parent, name);
1609 if (obj == NULL) {
1610 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001612 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001613 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001614 if (pparent != NULL)
1615 *pparent = parent;
1616 else
1617 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001618 return obj;
1619}
1620
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001621static void
1622reformat_attribute_error(PyObject *obj, PyObject *name)
1623{
1624 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1625 PyErr_Clear();
1626 PyErr_Format(PyExc_AttributeError,
1627 "Can't get attribute %R on %R", name, obj);
1628 }
1629}
1630
1631
1632static PyObject *
1633getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1634{
1635 PyObject *dotted_path, *attr;
1636
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001637 if (allow_qualname) {
1638 dotted_path = get_dotted_path(obj, name);
1639 if (dotted_path == NULL)
1640 return NULL;
1641 attr = get_deep_attribute(obj, dotted_path, NULL);
1642 Py_DECREF(dotted_path);
1643 }
1644 else
1645 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001646 if (attr == NULL)
1647 reformat_attribute_error(obj, name);
1648 return attr;
1649}
1650
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001651static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001652whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001653{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001654 PyObject *module_name;
1655 PyObject *modules_dict;
1656 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001657 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001658 _Py_IDENTIFIER(__module__);
1659 _Py_IDENTIFIER(modules);
1660 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001661
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001662 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1663
1664 if (module_name == NULL) {
1665 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001666 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001667 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001668 }
1669 else {
1670 /* In some rare cases (e.g., bound methods of extension types),
1671 __module__ can be None. If it is so, then search sys.modules for
1672 the module of global. */
1673 if (module_name != Py_None)
1674 return module_name;
1675 Py_CLEAR(module_name);
1676 }
1677 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001678
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001679 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001680 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001681 if (modules_dict == NULL) {
1682 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001683 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001684 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001685
1686 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001687 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1688 PyObject *candidate;
1689 if (PyUnicode_Check(module_name) &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001690 _PyUnicode_EqualToASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001691 continue;
1692 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001693 continue;
1694
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001695 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001696 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001697 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001698 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001699 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001700 continue;
1701 }
1702
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001703 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001704 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001705 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001706 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001707 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001708 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001709 }
1710
1711 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001712 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001713 Py_INCREF(module_name);
1714 return module_name;
1715}
1716
1717/* fast_save_enter() and fast_save_leave() are guards against recursive
1718 objects when Pickler is used with the "fast mode" (i.e., with object
1719 memoization disabled). If the nesting of a list or dict object exceed
1720 FAST_NESTING_LIMIT, these guards will start keeping an internal
1721 reference to the seen list or dict objects and check whether these objects
1722 are recursive. These are not strictly necessary, since save() has a
1723 hard-coded recursion limit, but they give a nicer error message than the
1724 typical RuntimeError. */
1725static int
1726fast_save_enter(PicklerObject *self, PyObject *obj)
1727{
1728 /* if fast_nesting < 0, we're doing an error exit. */
1729 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1730 PyObject *key = NULL;
1731 if (self->fast_memo == NULL) {
1732 self->fast_memo = PyDict_New();
1733 if (self->fast_memo == NULL) {
1734 self->fast_nesting = -1;
1735 return 0;
1736 }
1737 }
1738 key = PyLong_FromVoidPtr(obj);
1739 if (key == NULL)
1740 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001741 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001742 Py_DECREF(key);
1743 PyErr_Format(PyExc_ValueError,
1744 "fast mode: can't pickle cyclic objects "
1745 "including object type %.200s at %p",
1746 obj->ob_type->tp_name, obj);
1747 self->fast_nesting = -1;
1748 return 0;
1749 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001750 if (PyErr_Occurred()) {
1751 return 0;
1752 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001753 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1754 Py_DECREF(key);
1755 self->fast_nesting = -1;
1756 return 0;
1757 }
1758 Py_DECREF(key);
1759 }
1760 return 1;
1761}
1762
1763static int
1764fast_save_leave(PicklerObject *self, PyObject *obj)
1765{
1766 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1767 PyObject *key = PyLong_FromVoidPtr(obj);
1768 if (key == NULL)
1769 return 0;
1770 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1771 Py_DECREF(key);
1772 return 0;
1773 }
1774 Py_DECREF(key);
1775 }
1776 return 1;
1777}
1778
1779static int
1780save_none(PicklerObject *self, PyObject *obj)
1781{
1782 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001783 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001784 return -1;
1785
1786 return 0;
1787}
1788
1789static int
1790save_bool(PicklerObject *self, PyObject *obj)
1791{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001792 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001793 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001794 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001795 return -1;
1796 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001797 else {
1798 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1799 * so that unpicklers written before bools were introduced unpickle them
1800 * as ints, but unpicklers after can recognize that bools were intended.
1801 * Note that protocol 2 added direct ways to pickle bools.
1802 */
1803 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1804 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1805 return -1;
1806 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001807 return 0;
1808}
1809
1810static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001811save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001812{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001813 PyObject *repr = NULL;
1814 Py_ssize_t size;
1815 long val;
1816 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001817
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001818 const char long_op = LONG;
1819
1820 val= PyLong_AsLong(obj);
1821 if (val == -1 && PyErr_Occurred()) {
1822 /* out of range for int pickling */
1823 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001824 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001825 else if (self->bin &&
1826 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001827 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001828 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001829
1830 Note: we can't use -0x80000000L in the above condition because some
1831 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1832 before applying the unary minus when sizeof(long) <= 4. The
1833 resulting value stays unsigned which is commonly not what we want,
1834 so MSVC happily warns us about it. However, that result would have
1835 been fine because we guard for sizeof(long) <= 4 which turns the
1836 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001837 char pdata[32];
1838 Py_ssize_t len = 0;
1839
1840 pdata[1] = (unsigned char)(val & 0xff);
1841 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1842 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1843 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001844
1845 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1846 if (pdata[2] == 0) {
1847 pdata[0] = BININT1;
1848 len = 2;
1849 }
1850 else {
1851 pdata[0] = BININT2;
1852 len = 3;
1853 }
1854 }
1855 else {
1856 pdata[0] = BININT;
1857 len = 5;
1858 }
1859
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001860 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001861 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001862
1863 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001864 }
1865
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001866 if (self->proto >= 2) {
1867 /* Linear-time pickling. */
1868 size_t nbits;
1869 size_t nbytes;
1870 unsigned char *pdata;
1871 char header[5];
1872 int i;
1873 int sign = _PyLong_Sign(obj);
1874
1875 if (sign == 0) {
1876 header[0] = LONG1;
1877 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001878 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001879 goto error;
1880 return 0;
1881 }
1882 nbits = _PyLong_NumBits(obj);
1883 if (nbits == (size_t)-1 && PyErr_Occurred())
1884 goto error;
1885 /* How many bytes do we need? There are nbits >> 3 full
1886 * bytes of data, and nbits & 7 leftover bits. If there
1887 * are any leftover bits, then we clearly need another
1888 * byte. Wnat's not so obvious is that we *probably*
1889 * need another byte even if there aren't any leftovers:
1890 * the most-significant bit of the most-significant byte
1891 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001892 * opposite of the one we need. The exception is ints
1893 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001894 * its own 256's-complement, so has the right sign bit
1895 * even without the extra byte. That's a pain to check
1896 * for in advance, though, so we always grab an extra
1897 * byte at the start, and cut it back later if possible.
1898 */
1899 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001900 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001901 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001902 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001903 goto error;
1904 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001905 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 if (repr == NULL)
1907 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001908 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001909 i = _PyLong_AsByteArray((PyLongObject *)obj,
1910 pdata, nbytes,
1911 1 /* little endian */ , 1 /* signed */ );
1912 if (i < 0)
1913 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001914 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001915 * needed. This is so iff the MSB is all redundant sign
1916 * bits.
1917 */
1918 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001919 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001920 pdata[nbytes - 1] == 0xff &&
1921 (pdata[nbytes - 2] & 0x80) != 0) {
1922 nbytes--;
1923 }
1924
1925 if (nbytes < 256) {
1926 header[0] = LONG1;
1927 header[1] = (unsigned char)nbytes;
1928 size = 2;
1929 }
1930 else {
1931 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001932 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001933 for (i = 1; i < 5; i++) {
1934 header[i] = (unsigned char)(size & 0xff);
1935 size >>= 8;
1936 }
1937 size = 5;
1938 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001939 if (_Pickler_Write(self, header, size) < 0 ||
1940 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001941 goto error;
1942 }
1943 else {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001944 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001945
Mark Dickinson8dd05142009-01-20 20:43:58 +00001946 /* proto < 2: write the repr and newline. This is quadratic-time (in
1947 the number of digits), in both directions. We add a trailing 'L'
1948 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001949
1950 repr = PyObject_Repr(obj);
1951 if (repr == NULL)
1952 goto error;
1953
Serhiy Storchaka06515832016-11-20 09:13:07 +02001954 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001955 if (string == NULL)
1956 goto error;
1957
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001958 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1959 _Pickler_Write(self, string, size) < 0 ||
1960 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001961 goto error;
1962 }
1963
1964 if (0) {
1965 error:
1966 status = -1;
1967 }
1968 Py_XDECREF(repr);
1969
1970 return status;
1971}
1972
1973static int
1974save_float(PicklerObject *self, PyObject *obj)
1975{
1976 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1977
1978 if (self->bin) {
1979 char pdata[9];
1980 pdata[0] = BINFLOAT;
1981 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1982 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001983 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001984 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001985 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001986 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001987 int result = -1;
1988 char *buf = NULL;
1989 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001990
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001991 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001992 goto done;
1993
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001994 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001995 if (!buf) {
1996 PyErr_NoMemory();
1997 goto done;
1998 }
1999
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002000 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002001 goto done;
2002
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002003 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002004 goto done;
2005
2006 result = 0;
2007done:
2008 PyMem_Free(buf);
2009 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002010 }
2011
2012 return 0;
2013}
2014
2015static int
2016save_bytes(PicklerObject *self, PyObject *obj)
2017{
2018 if (self->proto < 3) {
2019 /* Older pickle protocols do not have an opcode for pickling bytes
2020 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002021 the __reduce__ method) to permit bytes object unpickling.
2022
2023 Here we use a hack to be compatible with Python 2. Since in Python
2024 2 'bytes' is just an alias for 'str' (which has different
2025 parameters than the actual bytes object), we use codecs.encode
2026 to create the appropriate 'str' object when unpickled using
2027 Python 2 *and* the appropriate 'bytes' object when unpickled
2028 using Python 3. Again this is a hack and we don't need to do this
2029 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002030 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002031 int status;
2032
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002033 if (PyBytes_GET_SIZE(obj) == 0) {
2034 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2035 }
2036 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002037 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002038 PyObject *unicode_str =
2039 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2040 PyBytes_GET_SIZE(obj),
2041 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002042 _Py_IDENTIFIER(latin1);
2043
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002044 if (unicode_str == NULL)
2045 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002046 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002047 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002048 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002049 Py_DECREF(unicode_str);
2050 }
2051
2052 if (reduce_value == NULL)
2053 return -1;
2054
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002055 /* save_reduce() will memoize the object automatically. */
2056 status = save_reduce(self, reduce_value, obj);
2057 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002058 return status;
2059 }
2060 else {
2061 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002062 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002063 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002064
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002065 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002066 if (size < 0)
2067 return -1;
2068
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002069 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002070 header[0] = SHORT_BINBYTES;
2071 header[1] = (unsigned char)size;
2072 len = 2;
2073 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002074 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002075 header[0] = BINBYTES;
2076 header[1] = (unsigned char)(size & 0xff);
2077 header[2] = (unsigned char)((size >> 8) & 0xff);
2078 header[3] = (unsigned char)((size >> 16) & 0xff);
2079 header[4] = (unsigned char)((size >> 24) & 0xff);
2080 len = 5;
2081 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002082 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002083 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002084 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002085 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002086 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002087 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002088 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002089 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002090 return -1; /* string too large */
2091 }
2092
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002093 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002094 return -1;
2095
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002096 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002097 return -1;
2098
2099 if (memo_put(self, obj) < 0)
2100 return -1;
2101
2102 return 0;
2103 }
2104}
2105
2106/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2107 backslash and newline characters to \uXXXX escapes. */
2108static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002109raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002110{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002111 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002112 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002113 void *data;
2114 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002115 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002116
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002117 if (PyUnicode_READY(obj))
2118 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002119
Victor Stinner358af132015-10-12 22:36:57 +02002120 _PyBytesWriter_Init(&writer);
2121
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002122 size = PyUnicode_GET_LENGTH(obj);
2123 data = PyUnicode_DATA(obj);
2124 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002125
Victor Stinner358af132015-10-12 22:36:57 +02002126 p = _PyBytesWriter_Alloc(&writer, size);
2127 if (p == NULL)
2128 goto error;
2129 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002130
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002131 for (i=0; i < size; i++) {
2132 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002133 /* Map 32-bit characters to '\Uxxxxxxxx' */
2134 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002135 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002136 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2137 if (p == NULL)
2138 goto error;
2139
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002140 *p++ = '\\';
2141 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002142 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2143 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2144 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2145 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2146 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2147 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2148 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2149 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002150 }
Victor Stinner358af132015-10-12 22:36:57 +02002151 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002152 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002153 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002154 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2155 if (p == NULL)
2156 goto error;
2157
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002158 *p++ = '\\';
2159 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002160 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2161 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2162 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2163 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002164 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002165 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002166 else
2167 *p++ = (char) ch;
2168 }
Victor Stinner358af132015-10-12 22:36:57 +02002169
2170 return _PyBytesWriter_Finish(&writer, p);
2171
2172error:
2173 _PyBytesWriter_Dealloc(&writer);
2174 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002175}
2176
2177static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002178write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002179{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002180 char header[9];
2181 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002182
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002183 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002184 if (size <= 0xff && self->proto >= 4) {
2185 header[0] = SHORT_BINUNICODE;
2186 header[1] = (unsigned char)(size & 0xff);
2187 len = 2;
2188 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002189 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002190 header[0] = BINUNICODE;
2191 header[1] = (unsigned char)(size & 0xff);
2192 header[2] = (unsigned char)((size >> 8) & 0xff);
2193 header[3] = (unsigned char)((size >> 16) & 0xff);
2194 header[4] = (unsigned char)((size >> 24) & 0xff);
2195 len = 5;
2196 }
2197 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002198 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002199 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002200 len = 9;
2201 }
2202 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002203 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002204 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002205 return -1;
2206 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002207
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002208 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002209 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002210 if (_Pickler_Write(self, data, size) < 0)
2211 return -1;
2212
2213 return 0;
2214}
2215
2216static int
2217write_unicode_binary(PicklerObject *self, PyObject *obj)
2218{
2219 PyObject *encoded = NULL;
2220 Py_ssize_t size;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002221 const char *data;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002222 int r;
2223
2224 if (PyUnicode_READY(obj))
2225 return -1;
2226
2227 data = PyUnicode_AsUTF8AndSize(obj, &size);
2228 if (data != NULL)
2229 return write_utf8(self, data, size);
2230
2231 /* Issue #8383: for strings with lone surrogates, fallback on the
2232 "surrogatepass" error handler. */
2233 PyErr_Clear();
2234 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2235 if (encoded == NULL)
2236 return -1;
2237
2238 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2239 PyBytes_GET_SIZE(encoded));
2240 Py_DECREF(encoded);
2241 return r;
2242}
2243
2244static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002245save_unicode(PicklerObject *self, PyObject *obj)
2246{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002247 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002248 if (write_unicode_binary(self, obj) < 0)
2249 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002250 }
2251 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002252 PyObject *encoded;
2253 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002254 const char unicode_op = UNICODE;
2255
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002256 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002258 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259
Antoine Pitrou299978d2013-04-07 17:38:11 +02002260 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2261 Py_DECREF(encoded);
2262 return -1;
2263 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002264
2265 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002266 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2267 Py_DECREF(encoded);
2268 return -1;
2269 }
2270 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002271
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002272 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002273 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002274 }
2275 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002276 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002277
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002278 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279}
2280
2281/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2282static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002283store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002284{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002285 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002286
2287 assert(PyTuple_Size(t) == len);
2288
2289 for (i = 0; i < len; i++) {
2290 PyObject *element = PyTuple_GET_ITEM(t, i);
2291
2292 if (element == NULL)
2293 return -1;
2294 if (save(self, element, 0) < 0)
2295 return -1;
2296 }
2297
2298 return 0;
2299}
2300
2301/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2302 * used across protocols to minimize the space needed to pickle them.
2303 * Tuples are also the only builtin immutable type that can be recursive
2304 * (a tuple can be reached from itself), and that requires some subtle
2305 * magic so that it works in all cases. IOW, this is a long routine.
2306 */
2307static int
2308save_tuple(PicklerObject *self, PyObject *obj)
2309{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002310 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002311
2312 const char mark_op = MARK;
2313 const char tuple_op = TUPLE;
2314 const char pop_op = POP;
2315 const char pop_mark_op = POP_MARK;
2316 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2317
2318 if ((len = PyTuple_Size(obj)) < 0)
2319 return -1;
2320
2321 if (len == 0) {
2322 char pdata[2];
2323
2324 if (self->proto) {
2325 pdata[0] = EMPTY_TUPLE;
2326 len = 1;
2327 }
2328 else {
2329 pdata[0] = MARK;
2330 pdata[1] = TUPLE;
2331 len = 2;
2332 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002333 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002334 return -1;
2335 return 0;
2336 }
2337
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002338 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002339 * saving the tuple elements, the tuple must be recursive, in
2340 * which case we'll pop everything we put on the stack, and fetch
2341 * its value from the memo.
2342 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002343 if (len <= 3 && self->proto >= 2) {
2344 /* Use TUPLE{1,2,3} opcodes. */
2345 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002346 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002347
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002348 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002349 /* pop the len elements */
2350 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002351 if (_Pickler_Write(self, &pop_op, 1) < 0)
2352 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002353 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002354 if (memo_get(self, obj) < 0)
2355 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002356
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002357 return 0;
2358 }
2359 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002360 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2361 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002362 }
2363 goto memoize;
2364 }
2365
2366 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2367 * Generate MARK e1 e2 ... TUPLE
2368 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002369 if (_Pickler_Write(self, &mark_op, 1) < 0)
2370 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002371
2372 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002373 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002374
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002375 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002376 /* pop the stack stuff we pushed */
2377 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002378 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2379 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002380 }
2381 else {
2382 /* Note that we pop one more than len, to remove
2383 * the MARK too.
2384 */
2385 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002386 if (_Pickler_Write(self, &pop_op, 1) < 0)
2387 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002388 }
2389 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002390 if (memo_get(self, obj) < 0)
2391 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002392
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002393 return 0;
2394 }
2395 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002396 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2397 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002398 }
2399
2400 memoize:
2401 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002402 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002403
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002404 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002405}
2406
2407/* iter is an iterator giving items, and we batch up chunks of
2408 * MARK item item ... item APPENDS
2409 * opcode sequences. Calling code should have arranged to first create an
2410 * empty list, or list-like object, for the APPENDS to operate on.
2411 * Returns 0 on success, <0 on error.
2412 */
2413static int
2414batch_list(PicklerObject *self, PyObject *iter)
2415{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002416 PyObject *obj = NULL;
2417 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002418 int i, n;
2419
2420 const char mark_op = MARK;
2421 const char append_op = APPEND;
2422 const char appends_op = APPENDS;
2423
2424 assert(iter != NULL);
2425
2426 /* XXX: I think this function could be made faster by avoiding the
2427 iterator interface and fetching objects directly from list using
2428 PyList_GET_ITEM.
2429 */
2430
2431 if (self->proto == 0) {
2432 /* APPENDS isn't available; do one at a time. */
2433 for (;;) {
2434 obj = PyIter_Next(iter);
2435 if (obj == NULL) {
2436 if (PyErr_Occurred())
2437 return -1;
2438 break;
2439 }
2440 i = save(self, obj, 0);
2441 Py_DECREF(obj);
2442 if (i < 0)
2443 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002444 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002445 return -1;
2446 }
2447 return 0;
2448 }
2449
2450 /* proto > 0: write in batches of BATCHSIZE. */
2451 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002452 /* Get first item */
2453 firstitem = PyIter_Next(iter);
2454 if (firstitem == NULL) {
2455 if (PyErr_Occurred())
2456 goto error;
2457
2458 /* nothing more to add */
2459 break;
2460 }
2461
2462 /* Try to get a second item */
2463 obj = PyIter_Next(iter);
2464 if (obj == NULL) {
2465 if (PyErr_Occurred())
2466 goto error;
2467
2468 /* Only one item to write */
2469 if (save(self, firstitem, 0) < 0)
2470 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002471 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002472 goto error;
2473 Py_CLEAR(firstitem);
2474 break;
2475 }
2476
2477 /* More than one item to write */
2478
2479 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002480 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002481 goto error;
2482
2483 if (save(self, firstitem, 0) < 0)
2484 goto error;
2485 Py_CLEAR(firstitem);
2486 n = 1;
2487
2488 /* Fetch and save up to BATCHSIZE items */
2489 while (obj) {
2490 if (save(self, obj, 0) < 0)
2491 goto error;
2492 Py_CLEAR(obj);
2493 n += 1;
2494
2495 if (n == BATCHSIZE)
2496 break;
2497
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002498 obj = PyIter_Next(iter);
2499 if (obj == NULL) {
2500 if (PyErr_Occurred())
2501 goto error;
2502 break;
2503 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002504 }
2505
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002506 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002507 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002508
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002509 } while (n == BATCHSIZE);
2510 return 0;
2511
2512 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002513 Py_XDECREF(firstitem);
2514 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002515 return -1;
2516}
2517
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002518/* This is a variant of batch_list() above, specialized for lists (with no
2519 * support for list subclasses). Like batch_list(), we batch up chunks of
2520 * MARK item item ... item APPENDS
2521 * opcode sequences. Calling code should have arranged to first create an
2522 * empty list, or list-like object, for the APPENDS to operate on.
2523 * Returns 0 on success, -1 on error.
2524 *
2525 * This version is considerably faster than batch_list(), if less general.
2526 *
2527 * Note that this only works for protocols > 0.
2528 */
2529static int
2530batch_list_exact(PicklerObject *self, PyObject *obj)
2531{
2532 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002533 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002534
2535 const char append_op = APPEND;
2536 const char appends_op = APPENDS;
2537 const char mark_op = MARK;
2538
2539 assert(obj != NULL);
2540 assert(self->proto > 0);
2541 assert(PyList_CheckExact(obj));
2542
2543 if (PyList_GET_SIZE(obj) == 1) {
2544 item = PyList_GET_ITEM(obj, 0);
2545 if (save(self, item, 0) < 0)
2546 return -1;
2547 if (_Pickler_Write(self, &append_op, 1) < 0)
2548 return -1;
2549 return 0;
2550 }
2551
2552 /* Write in batches of BATCHSIZE. */
2553 total = 0;
2554 do {
2555 this_batch = 0;
2556 if (_Pickler_Write(self, &mark_op, 1) < 0)
2557 return -1;
2558 while (total < PyList_GET_SIZE(obj)) {
2559 item = PyList_GET_ITEM(obj, total);
2560 if (save(self, item, 0) < 0)
2561 return -1;
2562 total++;
2563 if (++this_batch == BATCHSIZE)
2564 break;
2565 }
2566 if (_Pickler_Write(self, &appends_op, 1) < 0)
2567 return -1;
2568
2569 } while (total < PyList_GET_SIZE(obj));
2570
2571 return 0;
2572}
2573
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002574static int
2575save_list(PicklerObject *self, PyObject *obj)
2576{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002577 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002578 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002579 int status = 0;
2580
2581 if (self->fast && !fast_save_enter(self, obj))
2582 goto error;
2583
2584 /* Create an empty list. */
2585 if (self->bin) {
2586 header[0] = EMPTY_LIST;
2587 len = 1;
2588 }
2589 else {
2590 header[0] = MARK;
2591 header[1] = LIST;
2592 len = 2;
2593 }
2594
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002595 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002596 goto error;
2597
2598 /* Get list length, and bow out early if empty. */
2599 if ((len = PyList_Size(obj)) < 0)
2600 goto error;
2601
2602 if (memo_put(self, obj) < 0)
2603 goto error;
2604
2605 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002606 /* Materialize the list elements. */
2607 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002608 if (Py_EnterRecursiveCall(" while pickling an object"))
2609 goto error;
2610 status = batch_list_exact(self, obj);
2611 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002612 } else {
2613 PyObject *iter = PyObject_GetIter(obj);
2614 if (iter == NULL)
2615 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002616
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002617 if (Py_EnterRecursiveCall(" while pickling an object")) {
2618 Py_DECREF(iter);
2619 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002620 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002621 status = batch_list(self, iter);
2622 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002623 Py_DECREF(iter);
2624 }
2625 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002626 if (0) {
2627 error:
2628 status = -1;
2629 }
2630
2631 if (self->fast && !fast_save_leave(self, obj))
2632 status = -1;
2633
2634 return status;
2635}
2636
2637/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2638 * MARK key value ... key value SETITEMS
2639 * opcode sequences. Calling code should have arranged to first create an
2640 * empty dict, or dict-like object, for the SETITEMS to operate on.
2641 * Returns 0 on success, <0 on error.
2642 *
2643 * This is very much like batch_list(). The difference between saving
2644 * elements directly, and picking apart two-tuples, is so long-winded at
2645 * the C level, though, that attempts to combine these routines were too
2646 * ugly to bear.
2647 */
2648static int
2649batch_dict(PicklerObject *self, PyObject *iter)
2650{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002651 PyObject *obj = NULL;
2652 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002653 int i, n;
2654
2655 const char mark_op = MARK;
2656 const char setitem_op = SETITEM;
2657 const char setitems_op = SETITEMS;
2658
2659 assert(iter != NULL);
2660
2661 if (self->proto == 0) {
2662 /* SETITEMS isn't available; do one at a time. */
2663 for (;;) {
2664 obj = PyIter_Next(iter);
2665 if (obj == NULL) {
2666 if (PyErr_Occurred())
2667 return -1;
2668 break;
2669 }
2670 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2671 PyErr_SetString(PyExc_TypeError, "dict items "
2672 "iterator must return 2-tuples");
2673 return -1;
2674 }
2675 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2676 if (i >= 0)
2677 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2678 Py_DECREF(obj);
2679 if (i < 0)
2680 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002681 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002682 return -1;
2683 }
2684 return 0;
2685 }
2686
2687 /* proto > 0: write in batches of BATCHSIZE. */
2688 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002689 /* Get first item */
2690 firstitem = PyIter_Next(iter);
2691 if (firstitem == NULL) {
2692 if (PyErr_Occurred())
2693 goto error;
2694
2695 /* nothing more to add */
2696 break;
2697 }
2698 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2699 PyErr_SetString(PyExc_TypeError, "dict items "
2700 "iterator must return 2-tuples");
2701 goto error;
2702 }
2703
2704 /* Try to get a second item */
2705 obj = PyIter_Next(iter);
2706 if (obj == NULL) {
2707 if (PyErr_Occurred())
2708 goto error;
2709
2710 /* Only one item to write */
2711 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2712 goto error;
2713 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2714 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002715 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002716 goto error;
2717 Py_CLEAR(firstitem);
2718 break;
2719 }
2720
2721 /* More than one item to write */
2722
2723 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002724 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002725 goto error;
2726
2727 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2728 goto error;
2729 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2730 goto error;
2731 Py_CLEAR(firstitem);
2732 n = 1;
2733
2734 /* Fetch and save up to BATCHSIZE items */
2735 while (obj) {
2736 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2737 PyErr_SetString(PyExc_TypeError, "dict items "
2738 "iterator must return 2-tuples");
2739 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002740 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002741 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2742 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2743 goto error;
2744 Py_CLEAR(obj);
2745 n += 1;
2746
2747 if (n == BATCHSIZE)
2748 break;
2749
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002750 obj = PyIter_Next(iter);
2751 if (obj == NULL) {
2752 if (PyErr_Occurred())
2753 goto error;
2754 break;
2755 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002756 }
2757
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002758 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002759 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002760
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002761 } while (n == BATCHSIZE);
2762 return 0;
2763
2764 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002765 Py_XDECREF(firstitem);
2766 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002767 return -1;
2768}
2769
Collin Winter5c9b02d2009-05-25 05:43:30 +00002770/* This is a variant of batch_dict() above that specializes for dicts, with no
2771 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2772 * MARK key value ... key value SETITEMS
2773 * opcode sequences. Calling code should have arranged to first create an
2774 * empty dict, or dict-like object, for the SETITEMS to operate on.
2775 * Returns 0 on success, -1 on error.
2776 *
2777 * Note that this currently doesn't work for protocol 0.
2778 */
2779static int
2780batch_dict_exact(PicklerObject *self, PyObject *obj)
2781{
2782 PyObject *key = NULL, *value = NULL;
2783 int i;
2784 Py_ssize_t dict_size, ppos = 0;
2785
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002786 const char mark_op = MARK;
2787 const char setitem_op = SETITEM;
2788 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002789
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002790 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002791 assert(self->proto > 0);
2792
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002793 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002794
2795 /* Special-case len(d) == 1 to save space. */
2796 if (dict_size == 1) {
2797 PyDict_Next(obj, &ppos, &key, &value);
2798 if (save(self, key, 0) < 0)
2799 return -1;
2800 if (save(self, value, 0) < 0)
2801 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002802 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002803 return -1;
2804 return 0;
2805 }
2806
2807 /* Write in batches of BATCHSIZE. */
2808 do {
2809 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002810 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002811 return -1;
2812 while (PyDict_Next(obj, &ppos, &key, &value)) {
2813 if (save(self, key, 0) < 0)
2814 return -1;
2815 if (save(self, value, 0) < 0)
2816 return -1;
2817 if (++i == BATCHSIZE)
2818 break;
2819 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002820 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002821 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002822 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00002823 PyErr_Format(
2824 PyExc_RuntimeError,
2825 "dictionary changed size during iteration");
2826 return -1;
2827 }
2828
2829 } while (i == BATCHSIZE);
2830 return 0;
2831}
2832
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002833static int
2834save_dict(PicklerObject *self, PyObject *obj)
2835{
2836 PyObject *items, *iter;
2837 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002838 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002839 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002840 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002841
2842 if (self->fast && !fast_save_enter(self, obj))
2843 goto error;
2844
2845 /* Create an empty dict. */
2846 if (self->bin) {
2847 header[0] = EMPTY_DICT;
2848 len = 1;
2849 }
2850 else {
2851 header[0] = MARK;
2852 header[1] = DICT;
2853 len = 2;
2854 }
2855
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002856 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002857 goto error;
2858
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002859 if (memo_put(self, obj) < 0)
2860 goto error;
2861
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002862 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002863 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002864 if (PyDict_CheckExact(obj) && self->proto > 0) {
2865 /* We can take certain shortcuts if we know this is a dict and
2866 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002867 if (Py_EnterRecursiveCall(" while pickling an object"))
2868 goto error;
2869 status = batch_dict_exact(self, obj);
2870 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002871 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002872 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002873
Victor Stinnerad8c83a2016-09-05 17:53:15 -07002874 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002875 if (items == NULL)
2876 goto error;
2877 iter = PyObject_GetIter(items);
2878 Py_DECREF(items);
2879 if (iter == NULL)
2880 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002881 if (Py_EnterRecursiveCall(" while pickling an object")) {
2882 Py_DECREF(iter);
2883 goto error;
2884 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002885 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002886 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002887 Py_DECREF(iter);
2888 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002889 }
2890
2891 if (0) {
2892 error:
2893 status = -1;
2894 }
2895
2896 if (self->fast && !fast_save_leave(self, obj))
2897 status = -1;
2898
2899 return status;
2900}
2901
2902static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002903save_set(PicklerObject *self, PyObject *obj)
2904{
2905 PyObject *item;
2906 int i;
2907 Py_ssize_t set_size, ppos = 0;
2908 Py_hash_t hash;
2909
2910 const char empty_set_op = EMPTY_SET;
2911 const char mark_op = MARK;
2912 const char additems_op = ADDITEMS;
2913
2914 if (self->proto < 4) {
2915 PyObject *items;
2916 PyObject *reduce_value;
2917 int status;
2918
2919 items = PySequence_List(obj);
2920 if (items == NULL) {
2921 return -1;
2922 }
2923 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2924 Py_DECREF(items);
2925 if (reduce_value == NULL) {
2926 return -1;
2927 }
2928 /* save_reduce() will memoize the object automatically. */
2929 status = save_reduce(self, reduce_value, obj);
2930 Py_DECREF(reduce_value);
2931 return status;
2932 }
2933
2934 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2935 return -1;
2936
2937 if (memo_put(self, obj) < 0)
2938 return -1;
2939
2940 set_size = PySet_GET_SIZE(obj);
2941 if (set_size == 0)
2942 return 0; /* nothing to do */
2943
2944 /* Write in batches of BATCHSIZE. */
2945 do {
2946 i = 0;
2947 if (_Pickler_Write(self, &mark_op, 1) < 0)
2948 return -1;
2949 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2950 if (save(self, item, 0) < 0)
2951 return -1;
2952 if (++i == BATCHSIZE)
2953 break;
2954 }
2955 if (_Pickler_Write(self, &additems_op, 1) < 0)
2956 return -1;
2957 if (PySet_GET_SIZE(obj) != set_size) {
2958 PyErr_Format(
2959 PyExc_RuntimeError,
2960 "set changed size during iteration");
2961 return -1;
2962 }
2963 } while (i == BATCHSIZE);
2964
2965 return 0;
2966}
2967
2968static int
2969save_frozenset(PicklerObject *self, PyObject *obj)
2970{
2971 PyObject *iter;
2972
2973 const char mark_op = MARK;
2974 const char frozenset_op = FROZENSET;
2975
2976 if (self->fast && !fast_save_enter(self, obj))
2977 return -1;
2978
2979 if (self->proto < 4) {
2980 PyObject *items;
2981 PyObject *reduce_value;
2982 int status;
2983
2984 items = PySequence_List(obj);
2985 if (items == NULL) {
2986 return -1;
2987 }
2988 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2989 items);
2990 Py_DECREF(items);
2991 if (reduce_value == NULL) {
2992 return -1;
2993 }
2994 /* save_reduce() will memoize the object automatically. */
2995 status = save_reduce(self, reduce_value, obj);
2996 Py_DECREF(reduce_value);
2997 return status;
2998 }
2999
3000 if (_Pickler_Write(self, &mark_op, 1) < 0)
3001 return -1;
3002
3003 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003004 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003005 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003006 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003007 for (;;) {
3008 PyObject *item;
3009
3010 item = PyIter_Next(iter);
3011 if (item == NULL) {
3012 if (PyErr_Occurred()) {
3013 Py_DECREF(iter);
3014 return -1;
3015 }
3016 break;
3017 }
3018 if (save(self, item, 0) < 0) {
3019 Py_DECREF(item);
3020 Py_DECREF(iter);
3021 return -1;
3022 }
3023 Py_DECREF(item);
3024 }
3025 Py_DECREF(iter);
3026
3027 /* If the object is already in the memo, this means it is
3028 recursive. In this case, throw away everything we put on the
3029 stack, and fetch the object back from the memo. */
3030 if (PyMemoTable_Get(self->memo, obj)) {
3031 const char pop_mark_op = POP_MARK;
3032
3033 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3034 return -1;
3035 if (memo_get(self, obj) < 0)
3036 return -1;
3037 return 0;
3038 }
3039
3040 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3041 return -1;
3042 if (memo_put(self, obj) < 0)
3043 return -1;
3044
3045 return 0;
3046}
3047
3048static int
3049fix_imports(PyObject **module_name, PyObject **global_name)
3050{
3051 PyObject *key;
3052 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003053 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003054
3055 key = PyTuple_Pack(2, *module_name, *global_name);
3056 if (key == NULL)
3057 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003058 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003059 Py_DECREF(key);
3060 if (item) {
3061 PyObject *fixed_module_name;
3062 PyObject *fixed_global_name;
3063
3064 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3065 PyErr_Format(PyExc_RuntimeError,
3066 "_compat_pickle.REVERSE_NAME_MAPPING values "
3067 "should be 2-tuples, not %.200s",
3068 Py_TYPE(item)->tp_name);
3069 return -1;
3070 }
3071 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3072 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3073 if (!PyUnicode_Check(fixed_module_name) ||
3074 !PyUnicode_Check(fixed_global_name)) {
3075 PyErr_Format(PyExc_RuntimeError,
3076 "_compat_pickle.REVERSE_NAME_MAPPING values "
3077 "should be pairs of str, not (%.200s, %.200s)",
3078 Py_TYPE(fixed_module_name)->tp_name,
3079 Py_TYPE(fixed_global_name)->tp_name);
3080 return -1;
3081 }
3082
3083 Py_CLEAR(*module_name);
3084 Py_CLEAR(*global_name);
3085 Py_INCREF(fixed_module_name);
3086 Py_INCREF(fixed_global_name);
3087 *module_name = fixed_module_name;
3088 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003089 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003090 }
3091 else if (PyErr_Occurred()) {
3092 return -1;
3093 }
3094
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003095 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003096 if (item) {
3097 if (!PyUnicode_Check(item)) {
3098 PyErr_Format(PyExc_RuntimeError,
3099 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3100 "should be strings, not %.200s",
3101 Py_TYPE(item)->tp_name);
3102 return -1;
3103 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003104 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003105 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003106 }
3107 else if (PyErr_Occurred()) {
3108 return -1;
3109 }
3110
3111 return 0;
3112}
3113
3114static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003115save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3116{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003117 PyObject *global_name = NULL;
3118 PyObject *module_name = NULL;
3119 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003120 PyObject *parent = NULL;
3121 PyObject *dotted_path = NULL;
3122 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003123 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003124 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003125 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003126 _Py_IDENTIFIER(__name__);
3127 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003128
3129 const char global_op = GLOBAL;
3130
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003131 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003132 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003133 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003134 }
3135 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003136 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3137 if (global_name == NULL) {
3138 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3139 goto error;
3140 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003141 }
3142 if (global_name == NULL) {
3143 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3144 if (global_name == NULL)
3145 goto error;
3146 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003147 }
3148
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003149 dotted_path = get_dotted_path(module, global_name);
3150 if (dotted_path == NULL)
3151 goto error;
3152 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003153 if (module_name == NULL)
3154 goto error;
3155
3156 /* XXX: Change to use the import C API directly with level=0 to disallow
3157 relative imports.
3158
3159 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3160 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3161 custom import functions (IMHO, this would be a nice security
3162 feature). The import C API would need to be extended to support the
3163 extra parameters of __import__ to fix that. */
3164 module = PyImport_Import(module_name);
3165 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003166 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003167 "Can't pickle %R: import of module %R failed",
3168 obj, module_name);
3169 goto error;
3170 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003171 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3172 Py_INCREF(lastname);
3173 cls = get_deep_attribute(module, dotted_path, &parent);
3174 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003175 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003176 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003177 "Can't pickle %R: attribute lookup %S on %S failed",
3178 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003179 goto error;
3180 }
3181 if (cls != obj) {
3182 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003183 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003184 "Can't pickle %R: it's not the same object as %S.%S",
3185 obj, module_name, global_name);
3186 goto error;
3187 }
3188 Py_DECREF(cls);
3189
3190 if (self->proto >= 2) {
3191 /* See whether this is in the extension registry, and if
3192 * so generate an EXT opcode.
3193 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003194 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003195 PyObject *code_obj; /* extension code as Python object */
3196 long code; /* extension code as C value */
3197 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003198 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003199
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003200 extension_key = PyTuple_Pack(2, module_name, global_name);
3201 if (extension_key == NULL) {
3202 goto error;
3203 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003204 code_obj = PyDict_GetItemWithError(st->extension_registry,
3205 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003206 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003207 /* The object is not registered in the extension registry.
3208 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003209 if (code_obj == NULL) {
3210 if (PyErr_Occurred()) {
3211 goto error;
3212 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003213 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003214 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003215
3216 /* XXX: pickle.py doesn't check neither the type, nor the range
3217 of the value returned by the extension_registry. It should for
3218 consistency. */
3219
3220 /* Verify code_obj has the right type and value. */
3221 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003222 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003223 "Can't pickle %R: extension code %R isn't an integer",
3224 obj, code_obj);
3225 goto error;
3226 }
3227 code = PyLong_AS_LONG(code_obj);
3228 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003229 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003230 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3231 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003232 goto error;
3233 }
3234
3235 /* Generate an EXT opcode. */
3236 if (code <= 0xff) {
3237 pdata[0] = EXT1;
3238 pdata[1] = (unsigned char)code;
3239 n = 2;
3240 }
3241 else if (code <= 0xffff) {
3242 pdata[0] = EXT2;
3243 pdata[1] = (unsigned char)(code & 0xff);
3244 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3245 n = 3;
3246 }
3247 else {
3248 pdata[0] = EXT4;
3249 pdata[1] = (unsigned char)(code & 0xff);
3250 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3251 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3252 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3253 n = 5;
3254 }
3255
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003256 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003257 goto error;
3258 }
3259 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003260 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003261 if (parent == module) {
3262 Py_INCREF(lastname);
3263 Py_DECREF(global_name);
3264 global_name = lastname;
3265 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003266 if (self->proto >= 4) {
3267 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003268
Christian Heimese8b1ba12013-11-23 21:13:39 +01003269 if (save(self, module_name, 0) < 0)
3270 goto error;
3271 if (save(self, global_name, 0) < 0)
3272 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003273
3274 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3275 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003276 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003277 else if (parent != module) {
3278 PickleState *st = _Pickle_GetGlobalState();
3279 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3280 st->getattr, parent, lastname);
3281 status = save_reduce(self, reduce_value, NULL);
3282 Py_DECREF(reduce_value);
3283 if (status < 0)
3284 goto error;
3285 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003286 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003287 /* Generate a normal global opcode if we are using a pickle
3288 protocol < 4, or if the object is not registered in the
3289 extension registry. */
3290 PyObject *encoded;
3291 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003292
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003293 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003294 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003295
3296 /* For protocol < 3 and if the user didn't request against doing
3297 so, we convert module names to the old 2.x module names. */
3298 if (self->proto < 3 && self->fix_imports) {
3299 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003300 goto error;
3301 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003302 }
3303
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003304 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3305 both the module name and the global name using UTF-8. We do so
3306 only when we are using the pickle protocol newer than version
3307 3. This is to ensure compatibility with older Unpickler running
3308 on Python 2.x. */
3309 if (self->proto == 3) {
3310 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003311 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003312 else {
3313 unicode_encoder = PyUnicode_AsASCIIString;
3314 }
3315 encoded = unicode_encoder(module_name);
3316 if (encoded == NULL) {
3317 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003318 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003319 "can't pickle module identifier '%S' using "
3320 "pickle protocol %i",
3321 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003322 goto error;
3323 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003324 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3325 PyBytes_GET_SIZE(encoded)) < 0) {
3326 Py_DECREF(encoded);
3327 goto error;
3328 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003329 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003330 if(_Pickler_Write(self, "\n", 1) < 0)
3331 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003332
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003333 /* Save the name of the module. */
3334 encoded = unicode_encoder(global_name);
3335 if (encoded == NULL) {
3336 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003337 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003338 "can't pickle global identifier '%S' using "
3339 "pickle protocol %i",
3340 global_name, self->proto);
3341 goto error;
3342 }
3343 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3344 PyBytes_GET_SIZE(encoded)) < 0) {
3345 Py_DECREF(encoded);
3346 goto error;
3347 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003348 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003349 if (_Pickler_Write(self, "\n", 1) < 0)
3350 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003351 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003352 /* Memoize the object. */
3353 if (memo_put(self, obj) < 0)
3354 goto error;
3355 }
3356
3357 if (0) {
3358 error:
3359 status = -1;
3360 }
3361 Py_XDECREF(module_name);
3362 Py_XDECREF(global_name);
3363 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003364 Py_XDECREF(parent);
3365 Py_XDECREF(dotted_path);
3366 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003367
3368 return status;
3369}
3370
3371static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003372save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3373{
3374 PyObject *reduce_value;
3375 int status;
3376
3377 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3378 if (reduce_value == NULL) {
3379 return -1;
3380 }
3381 status = save_reduce(self, reduce_value, obj);
3382 Py_DECREF(reduce_value);
3383 return status;
3384}
3385
3386static int
3387save_type(PicklerObject *self, PyObject *obj)
3388{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003389 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003390 return save_singleton_type(self, obj, Py_None);
3391 }
3392 else if (obj == (PyObject *)&PyEllipsis_Type) {
3393 return save_singleton_type(self, obj, Py_Ellipsis);
3394 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003395 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003396 return save_singleton_type(self, obj, Py_NotImplemented);
3397 }
3398 return save_global(self, obj, NULL);
3399}
3400
3401static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003402save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3403{
3404 PyObject *pid = NULL;
3405 int status = 0;
3406
3407 const char persid_op = PERSID;
3408 const char binpersid_op = BINPERSID;
3409
3410 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003411 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003412 if (pid == NULL)
3413 return -1;
3414
3415 if (pid != Py_None) {
3416 if (self->bin) {
3417 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003418 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003419 goto error;
3420 }
3421 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003422 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003423
3424 pid_str = PyObject_Str(pid);
3425 if (pid_str == NULL)
3426 goto error;
3427
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003428 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003429 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003430 if (!PyUnicode_IS_ASCII(pid_str)) {
3431 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3432 "persistent IDs in protocol 0 must be "
3433 "ASCII strings");
3434 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003435 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003436 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003437
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003438 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003439 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3440 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3441 _Pickler_Write(self, "\n", 1) < 0) {
3442 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003443 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003444 }
3445 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003446 }
3447 status = 1;
3448 }
3449
3450 if (0) {
3451 error:
3452 status = -1;
3453 }
3454 Py_XDECREF(pid);
3455
3456 return status;
3457}
3458
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003459static PyObject *
3460get_class(PyObject *obj)
3461{
3462 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003463 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003464
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003465 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003466 if (cls == NULL) {
3467 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3468 PyErr_Clear();
3469 cls = (PyObject *) Py_TYPE(obj);
3470 Py_INCREF(cls);
3471 }
3472 }
3473 return cls;
3474}
3475
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003476/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3477 * appropriate __reduce__ method for obj.
3478 */
3479static int
3480save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3481{
3482 PyObject *callable;
3483 PyObject *argtup;
3484 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003485 PyObject *listitems = Py_None;
3486 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003487 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003488 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003489 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003490
3491 const char reduce_op = REDUCE;
3492 const char build_op = BUILD;
3493 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003494 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003495
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003496 size = PyTuple_Size(args);
3497 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003498 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003499 "__reduce__ must contain 2 through 5 elements");
3500 return -1;
3501 }
3502
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003503 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3504 &callable, &argtup, &state, &listitems, &dictitems))
3505 return -1;
3506
3507 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003508 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003509 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003510 return -1;
3511 }
3512 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003513 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003514 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003515 return -1;
3516 }
3517
3518 if (state == Py_None)
3519 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003520
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003521 if (listitems == Py_None)
3522 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003523 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003524 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003525 "returned by __reduce__ must be an iterator, not %s",
3526 Py_TYPE(listitems)->tp_name);
3527 return -1;
3528 }
3529
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003530 if (dictitems == Py_None)
3531 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003532 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003533 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003534 "returned by __reduce__ must be an iterator, not %s",
3535 Py_TYPE(dictitems)->tp_name);
3536 return -1;
3537 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003538
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003539 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003540 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003541 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003542
Victor Stinner804e05e2013-11-14 01:26:17 +01003543 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003544 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003545 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003546 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003547 }
3548 PyErr_Clear();
3549 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003550 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003551 _Py_IDENTIFIER(__newobj_ex__);
3552 use_newobj_ex = PyUnicode_Compare(
3553 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003554 if (!use_newobj_ex) {
3555 _Py_IDENTIFIER(__newobj__);
3556 use_newobj = PyUnicode_Compare(
3557 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3558 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003559 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003560 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003561 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003562
3563 if (use_newobj_ex) {
3564 PyObject *cls;
3565 PyObject *args;
3566 PyObject *kwargs;
3567
3568 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003569 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003570 "length of the NEWOBJ_EX argument tuple must be "
3571 "exactly 3, not %zd", Py_SIZE(argtup));
3572 return -1;
3573 }
3574
3575 cls = PyTuple_GET_ITEM(argtup, 0);
3576 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003577 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003578 "first item from NEWOBJ_EX argument tuple must "
3579 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3580 return -1;
3581 }
3582 args = PyTuple_GET_ITEM(argtup, 1);
3583 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003584 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003585 "second item from NEWOBJ_EX argument tuple must "
3586 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3587 return -1;
3588 }
3589 kwargs = PyTuple_GET_ITEM(argtup, 2);
3590 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003591 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003592 "third item from NEWOBJ_EX argument tuple must "
3593 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3594 return -1;
3595 }
3596
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003597 if (self->proto >= 4) {
3598 if (save(self, cls, 0) < 0 ||
3599 save(self, args, 0) < 0 ||
3600 save(self, kwargs, 0) < 0 ||
3601 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3602 return -1;
3603 }
3604 }
3605 else {
3606 PyObject *newargs;
3607 PyObject *cls_new;
3608 Py_ssize_t i;
3609 _Py_IDENTIFIER(__new__);
3610
3611 newargs = PyTuple_New(Py_SIZE(args) + 2);
3612 if (newargs == NULL)
3613 return -1;
3614
3615 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3616 if (cls_new == NULL) {
3617 Py_DECREF(newargs);
3618 return -1;
3619 }
3620 PyTuple_SET_ITEM(newargs, 0, cls_new);
3621 Py_INCREF(cls);
3622 PyTuple_SET_ITEM(newargs, 1, cls);
3623 for (i = 0; i < Py_SIZE(args); i++) {
3624 PyObject *item = PyTuple_GET_ITEM(args, i);
3625 Py_INCREF(item);
3626 PyTuple_SET_ITEM(newargs, i + 2, item);
3627 }
3628
3629 callable = PyObject_Call(st->partial, newargs, kwargs);
3630 Py_DECREF(newargs);
3631 if (callable == NULL)
3632 return -1;
3633
3634 newargs = PyTuple_New(0);
3635 if (newargs == NULL) {
3636 Py_DECREF(callable);
3637 return -1;
3638 }
3639
3640 if (save(self, callable, 0) < 0 ||
3641 save(self, newargs, 0) < 0 ||
3642 _Pickler_Write(self, &reduce_op, 1) < 0) {
3643 Py_DECREF(newargs);
3644 Py_DECREF(callable);
3645 return -1;
3646 }
3647 Py_DECREF(newargs);
3648 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003649 }
3650 }
3651 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003652 PyObject *cls;
3653 PyObject *newargtup;
3654 PyObject *obj_class;
3655 int p;
3656
3657 /* Sanity checks. */
3658 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003659 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003660 return -1;
3661 }
3662
3663 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003664 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003665 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003666 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003667 return -1;
3668 }
3669
3670 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003671 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003672 p = obj_class != cls; /* true iff a problem */
3673 Py_DECREF(obj_class);
3674 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003675 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003676 "__newobj__ args has the wrong class");
3677 return -1;
3678 }
3679 }
3680 /* XXX: These calls save() are prone to infinite recursion. Imagine
3681 what happen if the value returned by the __reduce__() method of
3682 some extension type contains another object of the same type. Ouch!
3683
3684 Here is a quick example, that I ran into, to illustrate what I
3685 mean:
3686
3687 >>> import pickle, copyreg
3688 >>> copyreg.dispatch_table.pop(complex)
3689 >>> pickle.dumps(1+2j)
3690 Traceback (most recent call last):
3691 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003692 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003693
3694 Removing the complex class from copyreg.dispatch_table made the
3695 __reduce_ex__() method emit another complex object:
3696
3697 >>> (1+1j).__reduce_ex__(2)
3698 (<function __newobj__ at 0xb7b71c3c>,
3699 (<class 'complex'>, (1+1j)), None, None, None)
3700
3701 Thus when save() was called on newargstup (the 2nd item) recursion
3702 ensued. Of course, the bug was in the complex class which had a
3703 broken __getnewargs__() that emitted another complex object. But,
3704 the point, here, is it is quite easy to end up with a broken reduce
3705 function. */
3706
3707 /* Save the class and its __new__ arguments. */
3708 if (save(self, cls, 0) < 0)
3709 return -1;
3710
3711 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3712 if (newargtup == NULL)
3713 return -1;
3714
3715 p = save(self, newargtup, 0);
3716 Py_DECREF(newargtup);
3717 if (p < 0)
3718 return -1;
3719
3720 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003721 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003722 return -1;
3723 }
3724 else { /* Not using NEWOBJ. */
3725 if (save(self, callable, 0) < 0 ||
3726 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003727 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003728 return -1;
3729 }
3730
3731 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3732 the caller do not want to memoize the object. Not particularly useful,
3733 but that is to mimic the behavior save_reduce() in pickle.py when
3734 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003735 if (obj != NULL) {
3736 /* If the object is already in the memo, this means it is
3737 recursive. In this case, throw away everything we put on the
3738 stack, and fetch the object back from the memo. */
3739 if (PyMemoTable_Get(self->memo, obj)) {
3740 const char pop_op = POP;
3741
3742 if (_Pickler_Write(self, &pop_op, 1) < 0)
3743 return -1;
3744 if (memo_get(self, obj) < 0)
3745 return -1;
3746
3747 return 0;
3748 }
3749 else if (memo_put(self, obj) < 0)
3750 return -1;
3751 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003752
3753 if (listitems && batch_list(self, listitems) < 0)
3754 return -1;
3755
3756 if (dictitems && batch_dict(self, dictitems) < 0)
3757 return -1;
3758
3759 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003760 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003761 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003762 return -1;
3763 }
3764
3765 return 0;
3766}
3767
3768static int
3769save(PicklerObject *self, PyObject *obj, int pers_save)
3770{
3771 PyTypeObject *type;
3772 PyObject *reduce_func = NULL;
3773 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003774 int status = 0;
3775
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003776 if (_Pickler_OpcodeBoundary(self) < 0)
3777 return -1;
3778
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003779 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003780 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003781
3782 /* The extra pers_save argument is necessary to avoid calling save_pers()
3783 on its returned object. */
3784 if (!pers_save && self->pers_func) {
3785 /* save_pers() returns:
3786 -1 to signal an error;
3787 0 if it did nothing successfully;
3788 1 if a persistent id was saved.
3789 */
3790 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3791 goto done;
3792 }
3793
3794 type = Py_TYPE(obj);
3795
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003796 /* The old cPickle had an optimization that used switch-case statement
3797 dispatching on the first letter of the type name. This has was removed
3798 since benchmarks shown that this optimization was actually slowing
3799 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003800
3801 /* Atom types; these aren't memoized, so don't check the memo. */
3802
3803 if (obj == Py_None) {
3804 status = save_none(self, obj);
3805 goto done;
3806 }
3807 else if (obj == Py_False || obj == Py_True) {
3808 status = save_bool(self, obj);
3809 goto done;
3810 }
3811 else if (type == &PyLong_Type) {
3812 status = save_long(self, obj);
3813 goto done;
3814 }
3815 else if (type == &PyFloat_Type) {
3816 status = save_float(self, obj);
3817 goto done;
3818 }
3819
3820 /* Check the memo to see if it has the object. If so, generate
3821 a GET (or BINGET) opcode, instead of pickling the object
3822 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003823 if (PyMemoTable_Get(self->memo, obj)) {
3824 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003825 goto error;
3826 goto done;
3827 }
3828
3829 if (type == &PyBytes_Type) {
3830 status = save_bytes(self, obj);
3831 goto done;
3832 }
3833 else if (type == &PyUnicode_Type) {
3834 status = save_unicode(self, obj);
3835 goto done;
3836 }
3837 else if (type == &PyDict_Type) {
3838 status = save_dict(self, obj);
3839 goto done;
3840 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003841 else if (type == &PySet_Type) {
3842 status = save_set(self, obj);
3843 goto done;
3844 }
3845 else if (type == &PyFrozenSet_Type) {
3846 status = save_frozenset(self, obj);
3847 goto done;
3848 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003849 else if (type == &PyList_Type) {
3850 status = save_list(self, obj);
3851 goto done;
3852 }
3853 else if (type == &PyTuple_Type) {
3854 status = save_tuple(self, obj);
3855 goto done;
3856 }
3857 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003858 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003859 goto done;
3860 }
3861 else if (type == &PyFunction_Type) {
3862 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003863 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003864 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003865
3866 /* XXX: This part needs some unit tests. */
3867
3868 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003869 * self.dispatch_table, copyreg.dispatch_table, the object's
3870 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003871 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003872 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003873 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003874 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3875 (PyObject *)type);
3876 if (reduce_func == NULL) {
3877 if (PyErr_Occurred()) {
3878 goto error;
3879 }
3880 } else {
3881 /* PyDict_GetItemWithError() returns a borrowed reference.
3882 Increase the reference count to be consistent with
3883 PyObject_GetItem and _PyObject_GetAttrId used below. */
3884 Py_INCREF(reduce_func);
3885 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003886 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003887 reduce_func = PyObject_GetItem(self->dispatch_table,
3888 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003889 if (reduce_func == NULL) {
3890 if (PyErr_ExceptionMatches(PyExc_KeyError))
3891 PyErr_Clear();
3892 else
3893 goto error;
3894 }
3895 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003896 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003897 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003898 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003900 else if (PyType_IsSubtype(type, &PyType_Type)) {
3901 status = save_global(self, obj, NULL);
3902 goto done;
3903 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003904 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003905 _Py_IDENTIFIER(__reduce__);
3906 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003907
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003908
3909 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3910 automatically defined as __reduce__. While this is convenient, this
3911 make it impossible to know which method was actually called. Of
3912 course, this is not a big deal. But still, it would be nice to let
3913 the user know which method was called when something go
3914 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3915 don't actually have to check for a __reduce__ method. */
3916
3917 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003918 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003919 if (reduce_func != NULL) {
3920 PyObject *proto;
3921 proto = PyLong_FromLong(self->proto);
3922 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003923 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003924 }
3925 }
3926 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003927 PickleState *st = _Pickle_GetGlobalState();
3928
3929 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003930 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003931 }
3932 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003934 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003935 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003936 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003937 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02003938 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003939 }
3940 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003941 PyErr_Format(st->PicklingError,
3942 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943 type->tp_name, obj);
3944 goto error;
3945 }
3946 }
3947 }
3948
3949 if (reduce_value == NULL)
3950 goto error;
3951
3952 if (PyUnicode_Check(reduce_value)) {
3953 status = save_global(self, obj, reduce_value);
3954 goto done;
3955 }
3956
3957 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003958 PickleState *st = _Pickle_GetGlobalState();
3959 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003960 "__reduce__ must return a string or tuple");
3961 goto error;
3962 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003963
3964 status = save_reduce(self, reduce_value, obj);
3965
3966 if (0) {
3967 error:
3968 status = -1;
3969 }
3970 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003971
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003972 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 Py_XDECREF(reduce_func);
3974 Py_XDECREF(reduce_value);
3975
3976 return status;
3977}
3978
3979static int
3980dump(PicklerObject *self, PyObject *obj)
3981{
3982 const char stop_op = STOP;
3983
3984 if (self->proto >= 2) {
3985 char header[2];
3986
3987 header[0] = PROTO;
3988 assert(self->proto >= 0 && self->proto < 256);
3989 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003990 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003991 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003992 if (self->proto >= 4)
3993 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003994 }
3995
3996 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003997 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003998 return -1;
3999
4000 return 0;
4001}
4002
Larry Hastings61272b72014-01-07 12:41:53 -08004003/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004004
4005_pickle.Pickler.clear_memo
4006
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004007Clears the pickler's "memo".
4008
4009The memo is the data structure that remembers which objects the
4010pickler has already seen, so that shared or recursive objects are
4011pickled by reference and not by value. This method is useful when
4012re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004013[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004014
Larry Hastings3cceb382014-01-04 11:09:09 -08004015static PyObject *
4016_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004017/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004018{
4019 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004020 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004021
4022 Py_RETURN_NONE;
4023}
4024
Larry Hastings61272b72014-01-07 12:41:53 -08004025/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004026
4027_pickle.Pickler.dump
4028
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004029 obj: object
4030 /
4031
4032Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004033[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004034
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004035static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004036_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004037/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004038{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004039 /* Check whether the Pickler was initialized correctly (issue3664).
4040 Developers often forget to call __init__() in their subclasses, which
4041 would trigger a segfault without this check. */
4042 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004043 PickleState *st = _Pickle_GetGlobalState();
4044 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004045 "Pickler.__init__() was not called by %s.__init__()",
4046 Py_TYPE(self)->tp_name);
4047 return NULL;
4048 }
4049
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004050 if (_Pickler_ClearBuffer(self) < 0)
4051 return NULL;
4052
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004053 if (dump(self, obj) < 0)
4054 return NULL;
4055
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004056 if (_Pickler_FlushToFile(self) < 0)
4057 return NULL;
4058
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004059 Py_RETURN_NONE;
4060}
4061
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004062/*[clinic input]
4063
4064_pickle.Pickler.__sizeof__ -> Py_ssize_t
4065
4066Returns size in memory, in bytes.
4067[clinic start generated code]*/
4068
4069static Py_ssize_t
4070_pickle_Pickler___sizeof___impl(PicklerObject *self)
4071/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4072{
4073 Py_ssize_t res, s;
4074
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004075 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004076 if (self->memo != NULL) {
4077 res += sizeof(PyMemoTable);
4078 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4079 }
4080 if (self->output_buffer != NULL) {
4081 s = _PySys_GetSizeOf(self->output_buffer);
4082 if (s == -1)
4083 return -1;
4084 res += s;
4085 }
4086 return res;
4087}
4088
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004089static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004090 _PICKLE_PICKLER_DUMP_METHODDEF
4091 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004092 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 {NULL, NULL} /* sentinel */
4094};
4095
4096static void
4097Pickler_dealloc(PicklerObject *self)
4098{
4099 PyObject_GC_UnTrack(self);
4100
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004101 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004102 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004103 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004104 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004105 Py_XDECREF(self->fast_memo);
4106
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004107 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004108
4109 Py_TYPE(self)->tp_free((PyObject *)self);
4110}
4111
4112static int
4113Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4114{
4115 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004116 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004117 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004118 Py_VISIT(self->fast_memo);
4119 return 0;
4120}
4121
4122static int
4123Pickler_clear(PicklerObject *self)
4124{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004125 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004126 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004127 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004128 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129 Py_CLEAR(self->fast_memo);
4130
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004131 if (self->memo != NULL) {
4132 PyMemoTable *memo = self->memo;
4133 self->memo = NULL;
4134 PyMemoTable_Del(memo);
4135 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004136 return 0;
4137}
4138
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004139
Larry Hastings61272b72014-01-07 12:41:53 -08004140/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004141
4142_pickle.Pickler.__init__
4143
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004144 file: object
4145 protocol: object = NULL
4146 fix_imports: bool = True
4147
4148This takes a binary file for writing a pickle data stream.
4149
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004150The optional *protocol* argument tells the pickler to use the given
4151protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4152protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004153
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004154Specifying a negative protocol version selects the highest protocol
4155version supported. The higher the protocol used, the more recent the
4156version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004157
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004158The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004159bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004160writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004161this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004162
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004163If *fix_imports* is True and protocol is less than 3, pickle will try
4164to map the new Python 3 names to the old module names used in Python
41652, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004166[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004167
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004168static int
Larry Hastings89964c42015-04-14 18:07:59 -04004169_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4170 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004171/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004173 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004174 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004175
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004176 /* In case of multiple __init__() calls, clear previous content. */
4177 if (self->write != NULL)
4178 (void)Pickler_clear(self);
4179
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004180 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004181 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004182
4183 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004184 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004185
4186 /* memo and output_buffer may have already been created in _Pickler_New */
4187 if (self->memo == NULL) {
4188 self->memo = PyMemoTable_New();
4189 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004190 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004191 }
4192 self->output_len = 0;
4193 if (self->output_buffer == NULL) {
4194 self->max_output_len = WRITE_BUF_SIZE;
4195 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4196 self->max_output_len);
4197 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004198 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004199 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004200
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004201 self->fast = 0;
4202 self->fast_nesting = 0;
4203 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004204 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004205 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4206 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4207 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004208 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004209 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004210 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004211 self->dispatch_table = NULL;
4212 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4213 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4214 &PyId_dispatch_table);
4215 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004216 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004217 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004218
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004219 return 0;
4220}
4221
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004222
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004223/* Define a proxy object for the Pickler's internal memo object. This is to
4224 * avoid breaking code like:
4225 * pickler.memo.clear()
4226 * and
4227 * pickler.memo = saved_memo
4228 * Is this a good idea? Not really, but we don't want to break code that uses
4229 * it. Note that we don't implement the entire mapping API here. This is
4230 * intentional, as these should be treated as black-box implementation details.
4231 */
4232
Larry Hastings61272b72014-01-07 12:41:53 -08004233/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004234_pickle.PicklerMemoProxy.clear
4235
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004236Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004237[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004238
Larry Hastings3cceb382014-01-04 11:09:09 -08004239static PyObject *
4240_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004241/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004242{
4243 if (self->pickler->memo)
4244 PyMemoTable_Clear(self->pickler->memo);
4245 Py_RETURN_NONE;
4246}
4247
Larry Hastings61272b72014-01-07 12:41:53 -08004248/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004249_pickle.PicklerMemoProxy.copy
4250
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004251Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004252[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004253
Larry Hastings3cceb382014-01-04 11:09:09 -08004254static PyObject *
4255_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004256/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004257{
4258 Py_ssize_t i;
4259 PyMemoTable *memo;
4260 PyObject *new_memo = PyDict_New();
4261 if (new_memo == NULL)
4262 return NULL;
4263
4264 memo = self->pickler->memo;
4265 for (i = 0; i < memo->mt_allocated; ++i) {
4266 PyMemoEntry entry = memo->mt_table[i];
4267 if (entry.me_key != NULL) {
4268 int status;
4269 PyObject *key, *value;
4270
4271 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004272 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004273
4274 if (key == NULL || value == NULL) {
4275 Py_XDECREF(key);
4276 Py_XDECREF(value);
4277 goto error;
4278 }
4279 status = PyDict_SetItem(new_memo, key, value);
4280 Py_DECREF(key);
4281 Py_DECREF(value);
4282 if (status < 0)
4283 goto error;
4284 }
4285 }
4286 return new_memo;
4287
4288 error:
4289 Py_XDECREF(new_memo);
4290 return NULL;
4291}
4292
Larry Hastings61272b72014-01-07 12:41:53 -08004293/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004294_pickle.PicklerMemoProxy.__reduce__
4295
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004296Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004297[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004298
Larry Hastings3cceb382014-01-04 11:09:09 -08004299static PyObject *
4300_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004301/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004302{
4303 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004304 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004305 if (contents == NULL)
4306 return NULL;
4307
4308 reduce_value = PyTuple_New(2);
4309 if (reduce_value == NULL) {
4310 Py_DECREF(contents);
4311 return NULL;
4312 }
4313 dict_args = PyTuple_New(1);
4314 if (dict_args == NULL) {
4315 Py_DECREF(contents);
4316 Py_DECREF(reduce_value);
4317 return NULL;
4318 }
4319 PyTuple_SET_ITEM(dict_args, 0, contents);
4320 Py_INCREF((PyObject *)&PyDict_Type);
4321 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4322 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4323 return reduce_value;
4324}
4325
4326static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004327 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4328 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4329 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004330 {NULL, NULL} /* sentinel */
4331};
4332
4333static void
4334PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4335{
4336 PyObject_GC_UnTrack(self);
4337 Py_XDECREF(self->pickler);
4338 PyObject_GC_Del((PyObject *)self);
4339}
4340
4341static int
4342PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4343 visitproc visit, void *arg)
4344{
4345 Py_VISIT(self->pickler);
4346 return 0;
4347}
4348
4349static int
4350PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4351{
4352 Py_CLEAR(self->pickler);
4353 return 0;
4354}
4355
4356static PyTypeObject PicklerMemoProxyType = {
4357 PyVarObject_HEAD_INIT(NULL, 0)
4358 "_pickle.PicklerMemoProxy", /*tp_name*/
4359 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4360 0,
4361 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4362 0, /* tp_print */
4363 0, /* tp_getattr */
4364 0, /* tp_setattr */
4365 0, /* tp_compare */
4366 0, /* tp_repr */
4367 0, /* tp_as_number */
4368 0, /* tp_as_sequence */
4369 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004370 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004371 0, /* tp_call */
4372 0, /* tp_str */
4373 PyObject_GenericGetAttr, /* tp_getattro */
4374 PyObject_GenericSetAttr, /* tp_setattro */
4375 0, /* tp_as_buffer */
4376 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4377 0, /* tp_doc */
4378 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4379 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4380 0, /* tp_richcompare */
4381 0, /* tp_weaklistoffset */
4382 0, /* tp_iter */
4383 0, /* tp_iternext */
4384 picklerproxy_methods, /* tp_methods */
4385};
4386
4387static PyObject *
4388PicklerMemoProxy_New(PicklerObject *pickler)
4389{
4390 PicklerMemoProxyObject *self;
4391
4392 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4393 if (self == NULL)
4394 return NULL;
4395 Py_INCREF(pickler);
4396 self->pickler = pickler;
4397 PyObject_GC_Track(self);
4398 return (PyObject *)self;
4399}
4400
4401/*****************************************************************************/
4402
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004403static PyObject *
4404Pickler_get_memo(PicklerObject *self)
4405{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004406 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004407}
4408
4409static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004410Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004411{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004412 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004413
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004414 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004415 PyErr_SetString(PyExc_TypeError,
4416 "attribute deletion is not supported");
4417 return -1;
4418 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004419
4420 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4421 PicklerObject *pickler =
4422 ((PicklerMemoProxyObject *)obj)->pickler;
4423
4424 new_memo = PyMemoTable_Copy(pickler->memo);
4425 if (new_memo == NULL)
4426 return -1;
4427 }
4428 else if (PyDict_Check(obj)) {
4429 Py_ssize_t i = 0;
4430 PyObject *key, *value;
4431
4432 new_memo = PyMemoTable_New();
4433 if (new_memo == NULL)
4434 return -1;
4435
4436 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004437 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004438 PyObject *memo_obj;
4439
4440 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4441 PyErr_SetString(PyExc_TypeError,
4442 "'memo' values must be 2-item tuples");
4443 goto error;
4444 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004445 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004446 if (memo_id == -1 && PyErr_Occurred())
4447 goto error;
4448 memo_obj = PyTuple_GET_ITEM(value, 1);
4449 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4450 goto error;
4451 }
4452 }
4453 else {
4454 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004455 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004456 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004457 return -1;
4458 }
4459
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004460 PyMemoTable_Del(self->memo);
4461 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004462
4463 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004464
4465 error:
4466 if (new_memo)
4467 PyMemoTable_Del(new_memo);
4468 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004469}
4470
4471static PyObject *
4472Pickler_get_persid(PicklerObject *self)
4473{
4474 if (self->pers_func == NULL)
4475 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4476 else
4477 Py_INCREF(self->pers_func);
4478 return self->pers_func;
4479}
4480
4481static int
4482Pickler_set_persid(PicklerObject *self, PyObject *value)
4483{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004484 if (value == NULL) {
4485 PyErr_SetString(PyExc_TypeError,
4486 "attribute deletion is not supported");
4487 return -1;
4488 }
4489 if (!PyCallable_Check(value)) {
4490 PyErr_SetString(PyExc_TypeError,
4491 "persistent_id must be a callable taking one argument");
4492 return -1;
4493 }
4494
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004495 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004496 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004497
4498 return 0;
4499}
4500
4501static PyMemberDef Pickler_members[] = {
4502 {"bin", T_INT, offsetof(PicklerObject, bin)},
4503 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004504 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004505 {NULL}
4506};
4507
4508static PyGetSetDef Pickler_getsets[] = {
4509 {"memo", (getter)Pickler_get_memo,
4510 (setter)Pickler_set_memo},
4511 {"persistent_id", (getter)Pickler_get_persid,
4512 (setter)Pickler_set_persid},
4513 {NULL}
4514};
4515
4516static PyTypeObject Pickler_Type = {
4517 PyVarObject_HEAD_INIT(NULL, 0)
4518 "_pickle.Pickler" , /*tp_name*/
4519 sizeof(PicklerObject), /*tp_basicsize*/
4520 0, /*tp_itemsize*/
4521 (destructor)Pickler_dealloc, /*tp_dealloc*/
4522 0, /*tp_print*/
4523 0, /*tp_getattr*/
4524 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004525 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004526 0, /*tp_repr*/
4527 0, /*tp_as_number*/
4528 0, /*tp_as_sequence*/
4529 0, /*tp_as_mapping*/
4530 0, /*tp_hash*/
4531 0, /*tp_call*/
4532 0, /*tp_str*/
4533 0, /*tp_getattro*/
4534 0, /*tp_setattro*/
4535 0, /*tp_as_buffer*/
4536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004537 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004538 (traverseproc)Pickler_traverse, /*tp_traverse*/
4539 (inquiry)Pickler_clear, /*tp_clear*/
4540 0, /*tp_richcompare*/
4541 0, /*tp_weaklistoffset*/
4542 0, /*tp_iter*/
4543 0, /*tp_iternext*/
4544 Pickler_methods, /*tp_methods*/
4545 Pickler_members, /*tp_members*/
4546 Pickler_getsets, /*tp_getset*/
4547 0, /*tp_base*/
4548 0, /*tp_dict*/
4549 0, /*tp_descr_get*/
4550 0, /*tp_descr_set*/
4551 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004552 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004553 PyType_GenericAlloc, /*tp_alloc*/
4554 PyType_GenericNew, /*tp_new*/
4555 PyObject_GC_Del, /*tp_free*/
4556 0, /*tp_is_gc*/
4557};
4558
Victor Stinner121aab42011-09-29 23:40:53 +02004559/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004560
4561 XXX: It would be nice to able to avoid Python function call overhead, by
4562 using directly the C version of find_class(), when find_class() is not
4563 overridden by a subclass. Although, this could become rather hackish. A
4564 simpler optimization would be to call the C function when self is not a
4565 subclass instance. */
4566static PyObject *
4567find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4568{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004569 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004570
Victor Stinner55ba38a2016-12-09 16:09:30 +01004571 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4572 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004573}
4574
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004575static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004576marker(UnpicklerObject *self)
4577{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004578 Py_ssize_t mark;
4579
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004580 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004581 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004582 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004583 return -1;
4584 }
4585
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004586 mark = self->marks[--self->num_marks];
4587 self->stack->mark_set = self->num_marks != 0;
4588 self->stack->fence = self->num_marks ?
4589 self->marks[self->num_marks - 1] : 0;
4590 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004591}
4592
4593static int
4594load_none(UnpicklerObject *self)
4595{
4596 PDATA_APPEND(self->stack, Py_None, -1);
4597 return 0;
4598}
4599
4600static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004601load_int(UnpicklerObject *self)
4602{
4603 PyObject *value;
4604 char *endptr, *s;
4605 Py_ssize_t len;
4606 long x;
4607
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004608 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004609 return -1;
4610 if (len < 2)
4611 return bad_readline();
4612
4613 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004614 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004615 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004616 x = strtol(s, &endptr, 0);
4617
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004618 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004619 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004620 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004621 errno = 0;
4622 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004623 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004624 if (value == NULL) {
4625 PyErr_SetString(PyExc_ValueError,
4626 "could not convert string to int");
4627 return -1;
4628 }
4629 }
4630 else {
4631 if (len == 3 && (x == 0 || x == 1)) {
4632 if ((value = PyBool_FromLong(x)) == NULL)
4633 return -1;
4634 }
4635 else {
4636 if ((value = PyLong_FromLong(x)) == NULL)
4637 return -1;
4638 }
4639 }
4640
4641 PDATA_PUSH(self->stack, value, -1);
4642 return 0;
4643}
4644
4645static int
4646load_bool(UnpicklerObject *self, PyObject *boolean)
4647{
4648 assert(boolean == Py_True || boolean == Py_False);
4649 PDATA_APPEND(self->stack, boolean, -1);
4650 return 0;
4651}
4652
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004653/* s contains x bytes of an unsigned little-endian integer. Return its value
4654 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4655 */
4656static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004657calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004658{
4659 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004660 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004661 size_t x = 0;
4662
Serhiy Storchakae0606192015-09-29 22:10:07 +03004663 if (nbytes > (int)sizeof(size_t)) {
4664 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4665 * have 64-bit size that can't be represented on 32-bit platform.
4666 */
4667 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4668 if (s[i])
4669 return -1;
4670 }
4671 nbytes = (int)sizeof(size_t);
4672 }
4673 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004674 x |= (size_t) s[i] << (8 * i);
4675 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004676
4677 if (x > PY_SSIZE_T_MAX)
4678 return -1;
4679 else
4680 return (Py_ssize_t) x;
4681}
4682
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004683/* s contains x bytes of a little-endian integer. Return its value as a
4684 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004685 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004686 * of x-platform bugs.
4687 */
4688static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004689calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004690{
4691 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004692 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004693 long x = 0;
4694
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004695 for (i = 0; i < nbytes; i++) {
4696 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004697 }
4698
4699 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4700 * is signed, so on a box with longs bigger than 4 bytes we need
4701 * to extend a BININT's sign bit to the full width.
4702 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004703 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004704 x |= -(x & (1L << 31));
4705 }
4706
4707 return x;
4708}
4709
4710static int
4711load_binintx(UnpicklerObject *self, char *s, int size)
4712{
4713 PyObject *value;
4714 long x;
4715
4716 x = calc_binint(s, size);
4717
4718 if ((value = PyLong_FromLong(x)) == NULL)
4719 return -1;
4720
4721 PDATA_PUSH(self->stack, value, -1);
4722 return 0;
4723}
4724
4725static int
4726load_binint(UnpicklerObject *self)
4727{
4728 char *s;
4729
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004730 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004731 return -1;
4732
4733 return load_binintx(self, s, 4);
4734}
4735
4736static int
4737load_binint1(UnpicklerObject *self)
4738{
4739 char *s;
4740
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004741 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004742 return -1;
4743
4744 return load_binintx(self, s, 1);
4745}
4746
4747static int
4748load_binint2(UnpicklerObject *self)
4749{
4750 char *s;
4751
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004752 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004753 return -1;
4754
4755 return load_binintx(self, s, 2);
4756}
4757
4758static int
4759load_long(UnpicklerObject *self)
4760{
4761 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004762 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004763 Py_ssize_t len;
4764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004765 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004766 return -1;
4767 if (len < 2)
4768 return bad_readline();
4769
Mark Dickinson8dd05142009-01-20 20:43:58 +00004770 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4771 the 'L' before calling PyLong_FromString. In order to maintain
4772 compatibility with Python 3.0.0, we don't actually *require*
4773 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004774 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004775 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004776 /* XXX: Should the base argument explicitly set to 10? */
4777 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004778 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004779 return -1;
4780
4781 PDATA_PUSH(self->stack, value, -1);
4782 return 0;
4783}
4784
4785/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4786 * data following.
4787 */
4788static int
4789load_counted_long(UnpicklerObject *self, int size)
4790{
4791 PyObject *value;
4792 char *nbytes;
4793 char *pdata;
4794
4795 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004796 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004797 return -1;
4798
4799 size = calc_binint(nbytes, size);
4800 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004801 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004802 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004803 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004804 "LONG pickle has negative byte count");
4805 return -1;
4806 }
4807
4808 if (size == 0)
4809 value = PyLong_FromLong(0L);
4810 else {
4811 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004812 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004813 return -1;
4814 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4815 1 /* little endian */ , 1 /* signed */ );
4816 }
4817 if (value == NULL)
4818 return -1;
4819 PDATA_PUSH(self->stack, value, -1);
4820 return 0;
4821}
4822
4823static int
4824load_float(UnpicklerObject *self)
4825{
4826 PyObject *value;
4827 char *endptr, *s;
4828 Py_ssize_t len;
4829 double d;
4830
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004831 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004832 return -1;
4833 if (len < 2)
4834 return bad_readline();
4835
4836 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004837 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4838 if (d == -1.0 && PyErr_Occurred())
4839 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004840 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004841 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4842 return -1;
4843 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004844 value = PyFloat_FromDouble(d);
4845 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004846 return -1;
4847
4848 PDATA_PUSH(self->stack, value, -1);
4849 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004850}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004851
4852static int
4853load_binfloat(UnpicklerObject *self)
4854{
4855 PyObject *value;
4856 double x;
4857 char *s;
4858
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004859 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004860 return -1;
4861
4862 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4863 if (x == -1.0 && PyErr_Occurred())
4864 return -1;
4865
4866 if ((value = PyFloat_FromDouble(x)) == NULL)
4867 return -1;
4868
4869 PDATA_PUSH(self->stack, value, -1);
4870 return 0;
4871}
4872
4873static int
4874load_string(UnpicklerObject *self)
4875{
4876 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004877 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004878 Py_ssize_t len;
4879 char *s, *p;
4880
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004881 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004882 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004883 /* Strip the newline */
4884 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004885 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004886 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004887 p = s + 1;
4888 len -= 2;
4889 }
4890 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004891 PickleState *st = _Pickle_GetGlobalState();
4892 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004893 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004894 return -1;
4895 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004896 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004897
4898 /* Use the PyBytes API to decode the string, since that is what is used
4899 to encode, and then coerce the result to Unicode. */
4900 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004901 if (bytes == NULL)
4902 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004903
4904 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4905 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4906 if (strcmp(self->encoding, "bytes") == 0) {
4907 obj = bytes;
4908 }
4909 else {
4910 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4911 Py_DECREF(bytes);
4912 if (obj == NULL) {
4913 return -1;
4914 }
4915 }
4916
4917 PDATA_PUSH(self->stack, obj, -1);
4918 return 0;
4919}
4920
4921static int
4922load_counted_binstring(UnpicklerObject *self, int nbytes)
4923{
4924 PyObject *obj;
4925 Py_ssize_t size;
4926 char *s;
4927
4928 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004929 return -1;
4930
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004931 size = calc_binsize(s, nbytes);
4932 if (size < 0) {
4933 PickleState *st = _Pickle_GetGlobalState();
4934 PyErr_Format(st->UnpicklingError,
4935 "BINSTRING exceeds system's maximum size of %zd bytes",
4936 PY_SSIZE_T_MAX);
4937 return -1;
4938 }
4939
4940 if (_Unpickler_Read(self, &s, size) < 0)
4941 return -1;
4942
4943 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4944 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4945 if (strcmp(self->encoding, "bytes") == 0) {
4946 obj = PyBytes_FromStringAndSize(s, size);
4947 }
4948 else {
4949 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4950 }
4951 if (obj == NULL) {
4952 return -1;
4953 }
4954
4955 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004956 return 0;
4957}
4958
4959static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004960load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004961{
4962 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004963 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004964 char *s;
4965
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004966 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004967 return -1;
4968
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004969 size = calc_binsize(s, nbytes);
4970 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004971 PyErr_Format(PyExc_OverflowError,
4972 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004973 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004974 return -1;
4975 }
4976
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004977 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004978 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004979
4980 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004981 if (bytes == NULL)
4982 return -1;
4983
4984 PDATA_PUSH(self->stack, bytes, -1);
4985 return 0;
4986}
4987
4988static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004989load_unicode(UnpicklerObject *self)
4990{
4991 PyObject *str;
4992 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004993 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004994
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004995 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004996 return -1;
4997 if (len < 1)
4998 return bad_readline();
4999
5000 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5001 if (str == NULL)
5002 return -1;
5003
5004 PDATA_PUSH(self->stack, str, -1);
5005 return 0;
5006}
5007
5008static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005009load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005010{
5011 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005012 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005013 char *s;
5014
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005015 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005016 return -1;
5017
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005018 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005019 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005020 PyErr_Format(PyExc_OverflowError,
5021 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005022 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005023 return -1;
5024 }
5025
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005026 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005027 return -1;
5028
Victor Stinner485fb562010-04-13 11:07:24 +00005029 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005030 if (str == NULL)
5031 return -1;
5032
5033 PDATA_PUSH(self->stack, str, -1);
5034 return 0;
5035}
5036
5037static int
Victor Stinner21b47112016-03-14 18:09:39 +01005038load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005039{
5040 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005041
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005042 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005043 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005045 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005046 if (tuple == NULL)
5047 return -1;
5048 PDATA_PUSH(self->stack, tuple, -1);
5049 return 0;
5050}
5051
5052static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005053load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005054{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005055 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005056
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005057 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005058 return -1;
5059
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005060 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005061}
5062
5063static int
5064load_empty_list(UnpicklerObject *self)
5065{
5066 PyObject *list;
5067
5068 if ((list = PyList_New(0)) == NULL)
5069 return -1;
5070 PDATA_PUSH(self->stack, list, -1);
5071 return 0;
5072}
5073
5074static int
5075load_empty_dict(UnpicklerObject *self)
5076{
5077 PyObject *dict;
5078
5079 if ((dict = PyDict_New()) == NULL)
5080 return -1;
5081 PDATA_PUSH(self->stack, dict, -1);
5082 return 0;
5083}
5084
5085static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005086load_empty_set(UnpicklerObject *self)
5087{
5088 PyObject *set;
5089
5090 if ((set = PySet_New(NULL)) == NULL)
5091 return -1;
5092 PDATA_PUSH(self->stack, set, -1);
5093 return 0;
5094}
5095
5096static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005097load_list(UnpicklerObject *self)
5098{
5099 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005100 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005101
5102 if ((i = marker(self)) < 0)
5103 return -1;
5104
5105 list = Pdata_poplist(self->stack, i);
5106 if (list == NULL)
5107 return -1;
5108 PDATA_PUSH(self->stack, list, -1);
5109 return 0;
5110}
5111
5112static int
5113load_dict(UnpicklerObject *self)
5114{
5115 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005116 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005117
5118 if ((i = marker(self)) < 0)
5119 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005120 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005121
5122 if ((dict = PyDict_New()) == NULL)
5123 return -1;
5124
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005125 if ((j - i) % 2 != 0) {
5126 PickleState *st = _Pickle_GetGlobalState();
5127 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005128 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005129 return -1;
5130 }
5131
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005132 for (k = i + 1; k < j; k += 2) {
5133 key = self->stack->data[k - 1];
5134 value = self->stack->data[k];
5135 if (PyDict_SetItem(dict, key, value) < 0) {
5136 Py_DECREF(dict);
5137 return -1;
5138 }
5139 }
5140 Pdata_clear(self->stack, i);
5141 PDATA_PUSH(self->stack, dict, -1);
5142 return 0;
5143}
5144
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005145static int
5146load_frozenset(UnpicklerObject *self)
5147{
5148 PyObject *items;
5149 PyObject *frozenset;
5150 Py_ssize_t i;
5151
5152 if ((i = marker(self)) < 0)
5153 return -1;
5154
5155 items = Pdata_poptuple(self->stack, i);
5156 if (items == NULL)
5157 return -1;
5158
5159 frozenset = PyFrozenSet_New(items);
5160 Py_DECREF(items);
5161 if (frozenset == NULL)
5162 return -1;
5163
5164 PDATA_PUSH(self->stack, frozenset, -1);
5165 return 0;
5166}
5167
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005168static PyObject *
5169instantiate(PyObject *cls, PyObject *args)
5170{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005171 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005172 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005173 /* Caller must assure args are a tuple. Normally, args come from
5174 Pdata_poptuple which packs objects from the top of the stack
5175 into a newly created tuple. */
5176 assert(PyTuple_Check(args));
5177 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005178 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005179 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005180 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005181 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005182 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005183
Victor Stinner55ba38a2016-12-09 16:09:30 +01005184 result = _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005185 }
5186 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005187}
5188
5189static int
5190load_obj(UnpicklerObject *self)
5191{
5192 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005193 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005194
5195 if ((i = marker(self)) < 0)
5196 return -1;
5197
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005198 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005199 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005200
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005201 args = Pdata_poptuple(self->stack, i + 1);
5202 if (args == NULL)
5203 return -1;
5204
5205 PDATA_POP(self->stack, cls);
5206 if (cls) {
5207 obj = instantiate(cls, args);
5208 Py_DECREF(cls);
5209 }
5210 Py_DECREF(args);
5211 if (obj == NULL)
5212 return -1;
5213
5214 PDATA_PUSH(self->stack, obj, -1);
5215 return 0;
5216}
5217
5218static int
5219load_inst(UnpicklerObject *self)
5220{
5221 PyObject *cls = NULL;
5222 PyObject *args = NULL;
5223 PyObject *obj = NULL;
5224 PyObject *module_name;
5225 PyObject *class_name;
5226 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005227 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005228 char *s;
5229
5230 if ((i = marker(self)) < 0)
5231 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005232 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005233 return -1;
5234 if (len < 2)
5235 return bad_readline();
5236
5237 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5238 identifiers are permitted in Python 3.0, since the INST opcode is only
5239 supported by older protocols on Python 2.x. */
5240 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5241 if (module_name == NULL)
5242 return -1;
5243
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005244 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005245 if (len < 2) {
5246 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005247 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005248 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005249 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005250 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005251 cls = find_class(self, module_name, class_name);
5252 Py_DECREF(class_name);
5253 }
5254 }
5255 Py_DECREF(module_name);
5256
5257 if (cls == NULL)
5258 return -1;
5259
5260 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5261 obj = instantiate(cls, args);
5262 Py_DECREF(args);
5263 }
5264 Py_DECREF(cls);
5265
5266 if (obj == NULL)
5267 return -1;
5268
5269 PDATA_PUSH(self->stack, obj, -1);
5270 return 0;
5271}
5272
5273static int
5274load_newobj(UnpicklerObject *self)
5275{
5276 PyObject *args = NULL;
5277 PyObject *clsraw = NULL;
5278 PyTypeObject *cls; /* clsraw cast to its true type */
5279 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005280 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005281
5282 /* Stack is ... cls argtuple, and we want to call
5283 * cls.__new__(cls, *argtuple).
5284 */
5285 PDATA_POP(self->stack, args);
5286 if (args == NULL)
5287 goto error;
5288 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005289 PyErr_SetString(st->UnpicklingError,
5290 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005291 goto error;
5292 }
5293
5294 PDATA_POP(self->stack, clsraw);
5295 cls = (PyTypeObject *)clsraw;
5296 if (cls == NULL)
5297 goto error;
5298 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005299 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005300 "isn't a type object");
5301 goto error;
5302 }
5303 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005304 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005305 "has NULL tp_new");
5306 goto error;
5307 }
5308
5309 /* Call __new__. */
5310 obj = cls->tp_new(cls, args, NULL);
5311 if (obj == NULL)
5312 goto error;
5313
5314 Py_DECREF(args);
5315 Py_DECREF(clsraw);
5316 PDATA_PUSH(self->stack, obj, -1);
5317 return 0;
5318
5319 error:
5320 Py_XDECREF(args);
5321 Py_XDECREF(clsraw);
5322 return -1;
5323}
5324
5325static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005326load_newobj_ex(UnpicklerObject *self)
5327{
5328 PyObject *cls, *args, *kwargs;
5329 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005330 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005331
5332 PDATA_POP(self->stack, kwargs);
5333 if (kwargs == NULL) {
5334 return -1;
5335 }
5336 PDATA_POP(self->stack, args);
5337 if (args == NULL) {
5338 Py_DECREF(kwargs);
5339 return -1;
5340 }
5341 PDATA_POP(self->stack, cls);
5342 if (cls == NULL) {
5343 Py_DECREF(kwargs);
5344 Py_DECREF(args);
5345 return -1;
5346 }
Larry Hastings61272b72014-01-07 12:41:53 -08005347
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005348 if (!PyType_Check(cls)) {
5349 Py_DECREF(kwargs);
5350 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005351 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005352 "NEWOBJ_EX class argument must be a type, not %.200s",
5353 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005354 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005355 return -1;
5356 }
5357
5358 if (((PyTypeObject *)cls)->tp_new == NULL) {
5359 Py_DECREF(kwargs);
5360 Py_DECREF(args);
5361 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005362 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005363 "NEWOBJ_EX class argument doesn't have __new__");
5364 return -1;
5365 }
5366 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5367 Py_DECREF(kwargs);
5368 Py_DECREF(args);
5369 Py_DECREF(cls);
5370 if (obj == NULL) {
5371 return -1;
5372 }
5373 PDATA_PUSH(self->stack, obj, -1);
5374 return 0;
5375}
5376
5377static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005378load_global(UnpicklerObject *self)
5379{
5380 PyObject *global = NULL;
5381 PyObject *module_name;
5382 PyObject *global_name;
5383 Py_ssize_t len;
5384 char *s;
5385
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005386 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005387 return -1;
5388 if (len < 2)
5389 return bad_readline();
5390 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5391 if (!module_name)
5392 return -1;
5393
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005394 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005395 if (len < 2) {
5396 Py_DECREF(module_name);
5397 return bad_readline();
5398 }
5399 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5400 if (global_name) {
5401 global = find_class(self, module_name, global_name);
5402 Py_DECREF(global_name);
5403 }
5404 }
5405 Py_DECREF(module_name);
5406
5407 if (global == NULL)
5408 return -1;
5409 PDATA_PUSH(self->stack, global, -1);
5410 return 0;
5411}
5412
5413static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005414load_stack_global(UnpicklerObject *self)
5415{
5416 PyObject *global;
5417 PyObject *module_name;
5418 PyObject *global_name;
5419
5420 PDATA_POP(self->stack, global_name);
5421 PDATA_POP(self->stack, module_name);
5422 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5423 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005424 PickleState *st = _Pickle_GetGlobalState();
5425 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005426 Py_XDECREF(global_name);
5427 Py_XDECREF(module_name);
5428 return -1;
5429 }
5430 global = find_class(self, module_name, global_name);
5431 Py_DECREF(global_name);
5432 Py_DECREF(module_name);
5433 if (global == NULL)
5434 return -1;
5435 PDATA_PUSH(self->stack, global, -1);
5436 return 0;
5437}
5438
5439static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005440load_persid(UnpicklerObject *self)
5441{
5442 PyObject *pid;
5443 Py_ssize_t len;
5444 char *s;
5445
5446 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005447 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005448 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005449 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005450 return bad_readline();
5451
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005452 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5453 if (pid == NULL) {
5454 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5455 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5456 "persistent IDs in protocol 0 must be "
5457 "ASCII strings");
5458 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005459 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005460 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005462 /* This does not leak since _Pickle_FastCall() steals the reference
5463 to pid first. */
5464 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005465 if (pid == NULL)
5466 return -1;
5467
5468 PDATA_PUSH(self->stack, pid, -1);
5469 return 0;
5470 }
5471 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005472 PickleState *st = _Pickle_GetGlobalState();
5473 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005474 "A load persistent id instruction was encountered,\n"
5475 "but no persistent_load function was specified.");
5476 return -1;
5477 }
5478}
5479
5480static int
5481load_binpersid(UnpicklerObject *self)
5482{
5483 PyObject *pid;
5484
5485 if (self->pers_func) {
5486 PDATA_POP(self->stack, pid);
5487 if (pid == NULL)
5488 return -1;
5489
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005490 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005491 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005492 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005493 if (pid == NULL)
5494 return -1;
5495
5496 PDATA_PUSH(self->stack, pid, -1);
5497 return 0;
5498 }
5499 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005500 PickleState *st = _Pickle_GetGlobalState();
5501 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005502 "A load persistent id instruction was encountered,\n"
5503 "but no persistent_load function was specified.");
5504 return -1;
5505 }
5506}
5507
5508static int
5509load_pop(UnpicklerObject *self)
5510{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005511 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005512
5513 /* Note that we split the (pickle.py) stack into two stacks,
5514 * an object stack and a mark stack. We have to be clever and
5515 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005516 * mark stack first, and only signalling a stack underflow if
5517 * the object stack is empty and the mark stack doesn't match
5518 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005519 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005520 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005521 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005522 self->stack->mark_set = self->num_marks != 0;
5523 self->stack->fence = self->num_marks ?
5524 self->marks[self->num_marks - 1] : 0;
5525 } else if (len <= self->stack->fence)
5526 return Pdata_stack_underflow(self->stack);
5527 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005528 len--;
5529 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005530 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005531 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005532 return 0;
5533}
5534
5535static int
5536load_pop_mark(UnpicklerObject *self)
5537{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005538 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005539
5540 if ((i = marker(self)) < 0)
5541 return -1;
5542
5543 Pdata_clear(self->stack, i);
5544
5545 return 0;
5546}
5547
5548static int
5549load_dup(UnpicklerObject *self)
5550{
5551 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005552 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005554 if (len <= self->stack->fence)
5555 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005556 last = self->stack->data[len - 1];
5557 PDATA_APPEND(self->stack, last, -1);
5558 return 0;
5559}
5560
5561static int
5562load_get(UnpicklerObject *self)
5563{
5564 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005565 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005566 Py_ssize_t len;
5567 char *s;
5568
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005569 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005570 return -1;
5571 if (len < 2)
5572 return bad_readline();
5573
5574 key = PyLong_FromString(s, NULL, 10);
5575 if (key == NULL)
5576 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005577 idx = PyLong_AsSsize_t(key);
5578 if (idx == -1 && PyErr_Occurred()) {
5579 Py_DECREF(key);
5580 return -1;
5581 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005582
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005583 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005584 if (value == NULL) {
5585 if (!PyErr_Occurred())
5586 PyErr_SetObject(PyExc_KeyError, key);
5587 Py_DECREF(key);
5588 return -1;
5589 }
5590 Py_DECREF(key);
5591
5592 PDATA_APPEND(self->stack, value, -1);
5593 return 0;
5594}
5595
5596static int
5597load_binget(UnpicklerObject *self)
5598{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005599 PyObject *value;
5600 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601 char *s;
5602
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005603 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604 return -1;
5605
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005606 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005607
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005608 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005610 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005611 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005613 Py_DECREF(key);
5614 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615 return -1;
5616 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005617
5618 PDATA_APPEND(self->stack, value, -1);
5619 return 0;
5620}
5621
5622static int
5623load_long_binget(UnpicklerObject *self)
5624{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005625 PyObject *value;
5626 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005629 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 return -1;
5631
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005632 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005634 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005636 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005637 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005639 Py_DECREF(key);
5640 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005641 return -1;
5642 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005643
5644 PDATA_APPEND(self->stack, value, -1);
5645 return 0;
5646}
5647
5648/* Push an object from the extension registry (EXT[124]). nbytes is
5649 * the number of bytes following the opcode, holding the index (code) value.
5650 */
5651static int
5652load_extension(UnpicklerObject *self, int nbytes)
5653{
5654 char *codebytes; /* the nbytes bytes after the opcode */
5655 long code; /* calc_binint returns long */
5656 PyObject *py_code; /* code as a Python int */
5657 PyObject *obj; /* the object to push */
5658 PyObject *pair; /* (module_name, class_name) */
5659 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005660 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005661
5662 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005663 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005664 return -1;
5665 code = calc_binint(codebytes, nbytes);
5666 if (code <= 0) { /* note that 0 is forbidden */
5667 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005668 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005669 return -1;
5670 }
5671
5672 /* Look for the code in the cache. */
5673 py_code = PyLong_FromLong(code);
5674 if (py_code == NULL)
5675 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005676 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005677 if (obj != NULL) {
5678 /* Bingo. */
5679 Py_DECREF(py_code);
5680 PDATA_APPEND(self->stack, obj, -1);
5681 return 0;
5682 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005683 if (PyErr_Occurred()) {
5684 Py_DECREF(py_code);
5685 return -1;
5686 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687
5688 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005689 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 if (pair == NULL) {
5691 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005692 if (!PyErr_Occurred()) {
5693 PyErr_Format(PyExc_ValueError, "unregistered extension "
5694 "code %ld", code);
5695 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696 return -1;
5697 }
5698 /* Since the extension registry is manipulable via Python code,
5699 * confirm that pair is really a 2-tuple of strings.
5700 */
5701 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5702 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5703 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5704 Py_DECREF(py_code);
5705 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5706 "isn't a 2-tuple of strings", code);
5707 return -1;
5708 }
5709 /* Load the object. */
5710 obj = find_class(self, module_name, class_name);
5711 if (obj == NULL) {
5712 Py_DECREF(py_code);
5713 return -1;
5714 }
5715 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005716 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005717 Py_DECREF(py_code);
5718 if (code < 0) {
5719 Py_DECREF(obj);
5720 return -1;
5721 }
5722 PDATA_PUSH(self->stack, obj, -1);
5723 return 0;
5724}
5725
5726static int
5727load_put(UnpicklerObject *self)
5728{
5729 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005730 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005731 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005732 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005733
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005734 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005735 return -1;
5736 if (len < 2)
5737 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005738 if (Py_SIZE(self->stack) <= self->stack->fence)
5739 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005740 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005741
5742 key = PyLong_FromString(s, NULL, 10);
5743 if (key == NULL)
5744 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005745 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005746 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005747 if (idx < 0) {
5748 if (!PyErr_Occurred())
5749 PyErr_SetString(PyExc_ValueError,
5750 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005751 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005752 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005753
5754 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005755}
5756
5757static int
5758load_binput(UnpicklerObject *self)
5759{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005760 PyObject *value;
5761 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005762 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005764 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005767 if (Py_SIZE(self->stack) <= self->stack->fence)
5768 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005769 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005770
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005771 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005773 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774}
5775
5776static int
5777load_long_binput(UnpicklerObject *self)
5778{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005779 PyObject *value;
5780 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005782
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005783 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005786 if (Py_SIZE(self->stack) <= self->stack->fence)
5787 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005788 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005790 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005791 if (idx < 0) {
5792 PyErr_SetString(PyExc_ValueError,
5793 "negative LONG_BINPUT argument");
5794 return -1;
5795 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005796
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005797 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005798}
5799
5800static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005801load_memoize(UnpicklerObject *self)
5802{
5803 PyObject *value;
5804
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005805 if (Py_SIZE(self->stack) <= self->stack->fence)
5806 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005807 value = self->stack->data[Py_SIZE(self->stack) - 1];
5808
5809 return _Unpickler_MemoPut(self, self->memo_len, value);
5810}
5811
5812static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005813do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005814{
5815 PyObject *value;
5816 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005817 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005818
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005819 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005820 if (x > len || x <= self->stack->fence)
5821 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005822 if (len == x) /* nothing to do */
5823 return 0;
5824
5825 list = self->stack->data[x - 1];
5826
5827 if (PyList_Check(list)) {
5828 PyObject *slice;
5829 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005830 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005831
5832 slice = Pdata_poplist(self->stack, x);
5833 if (!slice)
5834 return -1;
5835 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005836 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005837 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005838 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005839 }
5840 else {
5841 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005842 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005843
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005844 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005845 if (append_func == NULL)
5846 return -1;
5847 for (i = x; i < len; i++) {
5848 PyObject *result;
5849
5850 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005851 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005852 if (result == NULL) {
5853 Pdata_clear(self->stack, i + 1);
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 return -1;
5857 }
5858 Py_DECREF(result);
5859 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005860 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005861 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005862 }
5863
5864 return 0;
5865}
5866
5867static int
5868load_append(UnpicklerObject *self)
5869{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005870 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5871 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005872 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005873}
5874
5875static int
5876load_appends(UnpicklerObject *self)
5877{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005878 Py_ssize_t i = marker(self);
5879 if (i < 0)
5880 return -1;
5881 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005882}
5883
5884static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005885do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005886{
5887 PyObject *value, *key;
5888 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005889 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005890 int status = 0;
5891
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005892 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005893 if (x > len || x <= self->stack->fence)
5894 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005895 if (len == x) /* nothing to do */
5896 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005897 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005898 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005899 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005900 PyErr_SetString(st->UnpicklingError,
5901 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005902 return -1;
5903 }
5904
5905 /* Here, dict does not actually need to be a PyDict; it could be anything
5906 that supports the __setitem__ attribute. */
5907 dict = self->stack->data[x - 1];
5908
5909 for (i = x + 1; i < len; i += 2) {
5910 key = self->stack->data[i - 1];
5911 value = self->stack->data[i];
5912 if (PyObject_SetItem(dict, key, value) < 0) {
5913 status = -1;
5914 break;
5915 }
5916 }
5917
5918 Pdata_clear(self->stack, x);
5919 return status;
5920}
5921
5922static int
5923load_setitem(UnpicklerObject *self)
5924{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005925 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005926}
5927
5928static int
5929load_setitems(UnpicklerObject *self)
5930{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005931 Py_ssize_t i = marker(self);
5932 if (i < 0)
5933 return -1;
5934 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005935}
5936
5937static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005938load_additems(UnpicklerObject *self)
5939{
5940 PyObject *set;
5941 Py_ssize_t mark, len, i;
5942
5943 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005944 if (mark < 0)
5945 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005946 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005947 if (mark > len || mark <= self->stack->fence)
5948 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005949 if (len == mark) /* nothing to do */
5950 return 0;
5951
5952 set = self->stack->data[mark - 1];
5953
5954 if (PySet_Check(set)) {
5955 PyObject *items;
5956 int status;
5957
5958 items = Pdata_poptuple(self->stack, mark);
5959 if (items == NULL)
5960 return -1;
5961
5962 status = _PySet_Update(set, items);
5963 Py_DECREF(items);
5964 return status;
5965 }
5966 else {
5967 PyObject *add_func;
5968 _Py_IDENTIFIER(add);
5969
5970 add_func = _PyObject_GetAttrId(set, &PyId_add);
5971 if (add_func == NULL)
5972 return -1;
5973 for (i = mark; i < len; i++) {
5974 PyObject *result;
5975 PyObject *item;
5976
5977 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005978 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005979 if (result == NULL) {
5980 Pdata_clear(self->stack, i + 1);
5981 Py_SIZE(self->stack) = mark;
5982 return -1;
5983 }
5984 Py_DECREF(result);
5985 }
5986 Py_SIZE(self->stack) = mark;
5987 }
5988
5989 return 0;
5990}
5991
5992static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005993load_build(UnpicklerObject *self)
5994{
5995 PyObject *state, *inst, *slotstate;
5996 PyObject *setstate;
5997 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005998 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005999
6000 /* Stack is ... instance, state. We want to leave instance at
6001 * the stack top, possibly mutated via instance.__setstate__(state).
6002 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006003 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6004 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006005
6006 PDATA_POP(self->stack, state);
6007 if (state == NULL)
6008 return -1;
6009
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006010 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006011
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006012 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006013 if (setstate == NULL) {
6014 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6015 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006016 else {
6017 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006018 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006019 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006020 }
6021 else {
6022 PyObject *result;
6023
6024 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006025 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006026 Py_DECREF(setstate);
6027 if (result == NULL)
6028 return -1;
6029 Py_DECREF(result);
6030 return 0;
6031 }
6032
6033 /* A default __setstate__. First see whether state embeds a
6034 * slot state dict too (a proto 2 addition).
6035 */
6036 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6037 PyObject *tmp = state;
6038
6039 state = PyTuple_GET_ITEM(tmp, 0);
6040 slotstate = PyTuple_GET_ITEM(tmp, 1);
6041 Py_INCREF(state);
6042 Py_INCREF(slotstate);
6043 Py_DECREF(tmp);
6044 }
6045 else
6046 slotstate = NULL;
6047
6048 /* Set inst.__dict__ from the state dict (if any). */
6049 if (state != Py_None) {
6050 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006051 PyObject *d_key, *d_value;
6052 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006053 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006054
6055 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006056 PickleState *st = _Pickle_GetGlobalState();
6057 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006058 goto error;
6059 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006060 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006061 if (dict == NULL)
6062 goto error;
6063
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006064 i = 0;
6065 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6066 /* normally the keys for instance attributes are
6067 interned. we should try to do that here. */
6068 Py_INCREF(d_key);
6069 if (PyUnicode_CheckExact(d_key))
6070 PyUnicode_InternInPlace(&d_key);
6071 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6072 Py_DECREF(d_key);
6073 goto error;
6074 }
6075 Py_DECREF(d_key);
6076 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006077 Py_DECREF(dict);
6078 }
6079
6080 /* Also set instance attributes from the slotstate dict (if any). */
6081 if (slotstate != NULL) {
6082 PyObject *d_key, *d_value;
6083 Py_ssize_t i;
6084
6085 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006086 PickleState *st = _Pickle_GetGlobalState();
6087 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006088 "slot state is not a dictionary");
6089 goto error;
6090 }
6091 i = 0;
6092 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6093 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6094 goto error;
6095 }
6096 }
6097
6098 if (0) {
6099 error:
6100 status = -1;
6101 }
6102
6103 Py_DECREF(state);
6104 Py_XDECREF(slotstate);
6105 return status;
6106}
6107
6108static int
6109load_mark(UnpicklerObject *self)
6110{
6111
6112 /* Note that we split the (pickle.py) stack into two stacks, an
6113 * object stack and a mark stack. Here we push a mark onto the
6114 * mark stack.
6115 */
6116
6117 if ((self->num_marks + 1) >= self->marks_size) {
6118 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006119
6120 /* Use the size_t type to check for overflow. */
6121 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006122 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006123 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006124 PyErr_NoMemory();
6125 return -1;
6126 }
6127
6128 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006129 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006130 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006131 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6132 if (self->marks == NULL) {
6133 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006134 PyErr_NoMemory();
6135 return -1;
6136 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006137 self->marks_size = (Py_ssize_t)alloc;
6138 }
6139
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006140 self->stack->mark_set = 1;
6141 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006142
6143 return 0;
6144}
6145
6146static int
6147load_reduce(UnpicklerObject *self)
6148{
6149 PyObject *callable = NULL;
6150 PyObject *argtup = NULL;
6151 PyObject *obj = NULL;
6152
6153 PDATA_POP(self->stack, argtup);
6154 if (argtup == NULL)
6155 return -1;
6156 PDATA_POP(self->stack, callable);
6157 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006158 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006159 Py_DECREF(callable);
6160 }
6161 Py_DECREF(argtup);
6162
6163 if (obj == NULL)
6164 return -1;
6165
6166 PDATA_PUSH(self->stack, obj, -1);
6167 return 0;
6168}
6169
6170/* Just raises an error if we don't know the protocol specified. PROTO
6171 * is the first opcode for protocols >= 2.
6172 */
6173static int
6174load_proto(UnpicklerObject *self)
6175{
6176 char *s;
6177 int i;
6178
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006179 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006180 return -1;
6181
6182 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006183 if (i <= HIGHEST_PROTOCOL) {
6184 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006185 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006186 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006187
6188 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6189 return -1;
6190}
6191
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006192static int
6193load_frame(UnpicklerObject *self)
6194{
6195 char *s;
6196 Py_ssize_t frame_len;
6197
6198 if (_Unpickler_Read(self, &s, 8) < 0)
6199 return -1;
6200
6201 frame_len = calc_binsize(s, 8);
6202 if (frame_len < 0) {
6203 PyErr_Format(PyExc_OverflowError,
6204 "FRAME length exceeds system's maximum of %zd bytes",
6205 PY_SSIZE_T_MAX);
6206 return -1;
6207 }
6208
6209 if (_Unpickler_Read(self, &s, frame_len) < 0)
6210 return -1;
6211
6212 /* Rewind to start of frame */
6213 self->next_read_idx -= frame_len;
6214 return 0;
6215}
6216
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006217static PyObject *
6218load(UnpicklerObject *self)
6219{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006220 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006221 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006222
6223 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006224 self->stack->mark_set = 0;
6225 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006226 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006227 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006228 Pdata_clear(self->stack, 0);
6229
6230 /* Convenient macros for the dispatch while-switch loop just below. */
6231#define OP(opcode, load_func) \
6232 case opcode: if (load_func(self) < 0) break; continue;
6233
6234#define OP_ARG(opcode, load_func, arg) \
6235 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6236
6237 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006238 if (_Unpickler_Read(self, &s, 1) < 0) {
6239 PickleState *st = _Pickle_GetGlobalState();
6240 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6241 PyErr_Format(PyExc_EOFError, "Ran out of input");
6242 }
6243 return NULL;
6244 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006245
6246 switch ((enum opcode)s[0]) {
6247 OP(NONE, load_none)
6248 OP(BININT, load_binint)
6249 OP(BININT1, load_binint1)
6250 OP(BININT2, load_binint2)
6251 OP(INT, load_int)
6252 OP(LONG, load_long)
6253 OP_ARG(LONG1, load_counted_long, 1)
6254 OP_ARG(LONG4, load_counted_long, 4)
6255 OP(FLOAT, load_float)
6256 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006257 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6258 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6259 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6260 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6261 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006262 OP(STRING, load_string)
6263 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006264 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6265 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6266 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006267 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6268 OP_ARG(TUPLE1, load_counted_tuple, 1)
6269 OP_ARG(TUPLE2, load_counted_tuple, 2)
6270 OP_ARG(TUPLE3, load_counted_tuple, 3)
6271 OP(TUPLE, load_tuple)
6272 OP(EMPTY_LIST, load_empty_list)
6273 OP(LIST, load_list)
6274 OP(EMPTY_DICT, load_empty_dict)
6275 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006276 OP(EMPTY_SET, load_empty_set)
6277 OP(ADDITEMS, load_additems)
6278 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006279 OP(OBJ, load_obj)
6280 OP(INST, load_inst)
6281 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006282 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006283 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006284 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006285 OP(APPEND, load_append)
6286 OP(APPENDS, load_appends)
6287 OP(BUILD, load_build)
6288 OP(DUP, load_dup)
6289 OP(BINGET, load_binget)
6290 OP(LONG_BINGET, load_long_binget)
6291 OP(GET, load_get)
6292 OP(MARK, load_mark)
6293 OP(BINPUT, load_binput)
6294 OP(LONG_BINPUT, load_long_binput)
6295 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006296 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006297 OP(POP, load_pop)
6298 OP(POP_MARK, load_pop_mark)
6299 OP(SETITEM, load_setitem)
6300 OP(SETITEMS, load_setitems)
6301 OP(PERSID, load_persid)
6302 OP(BINPERSID, load_binpersid)
6303 OP(REDUCE, load_reduce)
6304 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006305 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006306 OP_ARG(EXT1, load_extension, 1)
6307 OP_ARG(EXT2, load_extension, 2)
6308 OP_ARG(EXT4, load_extension, 4)
6309 OP_ARG(NEWTRUE, load_bool, Py_True)
6310 OP_ARG(NEWFALSE, load_bool, Py_False)
6311
6312 case STOP:
6313 break;
6314
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006315 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006316 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006317 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006318 unsigned char c = (unsigned char) *s;
6319 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6320 PyErr_Format(st->UnpicklingError,
6321 "invalid load key, '%c'.", c);
6322 }
6323 else {
6324 PyErr_Format(st->UnpicklingError,
6325 "invalid load key, '\\x%02x'.", c);
6326 }
6327 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006328 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006329 }
6330
6331 break; /* and we are done! */
6332 }
6333
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006334 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006335 return NULL;
6336 }
6337
Victor Stinner2ae57e32013-10-31 13:39:23 +01006338 if (_Unpickler_SkipConsumed(self) < 0)
6339 return NULL;
6340
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006341 PDATA_POP(self->stack, value);
6342 return value;
6343}
6344
Larry Hastings61272b72014-01-07 12:41:53 -08006345/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006346
6347_pickle.Unpickler.load
6348
6349Load a pickle.
6350
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006351Read a pickled object representation from the open file object given
6352in the constructor, and return the reconstituted object hierarchy
6353specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006354[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006355
Larry Hastings3cceb382014-01-04 11:09:09 -08006356static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006357_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006358/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006359{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006360 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006361
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006362 /* Check whether the Unpickler was initialized correctly. This prevents
6363 segfaulting if a subclass overridden __init__ with a function that does
6364 not call Unpickler.__init__(). Here, we simply ensure that self->read
6365 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006366 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006367 PickleState *st = _Pickle_GetGlobalState();
6368 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006369 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006370 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006371 return NULL;
6372 }
6373
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006374 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006375}
6376
6377/* The name of find_class() is misleading. In newer pickle protocols, this
6378 function is used for loading any global (i.e., functions), not just
6379 classes. The name is kept only for backward compatibility. */
6380
Larry Hastings61272b72014-01-07 12:41:53 -08006381/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006382
6383_pickle.Unpickler.find_class
6384
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006385 module_name: object
6386 global_name: object
6387 /
6388
6389Return an object from a specified module.
6390
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006391If necessary, the module will be imported. Subclasses may override
6392this method (e.g. to restrict unpickling of arbitrary classes and
6393functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006394
6395This method is called whenever a class or a function object is
6396needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006397[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006398
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006399static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006400_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6401 PyObject *module_name,
6402 PyObject *global_name)
6403/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006404{
6405 PyObject *global;
6406 PyObject *modules_dict;
6407 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006408 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006409
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006410 /* Try to map the old names used in Python 2.x to the new ones used in
6411 Python 3.x. We do this only with old pickle protocols and when the
6412 user has not disabled the feature. */
6413 if (self->proto < 3 && self->fix_imports) {
6414 PyObject *key;
6415 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006416 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006417
6418 /* Check if the global (i.e., a function or a class) was renamed
6419 or moved to another module. */
6420 key = PyTuple_Pack(2, module_name, global_name);
6421 if (key == NULL)
6422 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006423 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006424 Py_DECREF(key);
6425 if (item) {
6426 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6427 PyErr_Format(PyExc_RuntimeError,
6428 "_compat_pickle.NAME_MAPPING values should be "
6429 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6430 return NULL;
6431 }
6432 module_name = PyTuple_GET_ITEM(item, 0);
6433 global_name = PyTuple_GET_ITEM(item, 1);
6434 if (!PyUnicode_Check(module_name) ||
6435 !PyUnicode_Check(global_name)) {
6436 PyErr_Format(PyExc_RuntimeError,
6437 "_compat_pickle.NAME_MAPPING values should be "
6438 "pairs of str, not (%.200s, %.200s)",
6439 Py_TYPE(module_name)->tp_name,
6440 Py_TYPE(global_name)->tp_name);
6441 return NULL;
6442 }
6443 }
6444 else if (PyErr_Occurred()) {
6445 return NULL;
6446 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006447 else {
6448 /* Check if the module was renamed. */
6449 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6450 if (item) {
6451 if (!PyUnicode_Check(item)) {
6452 PyErr_Format(PyExc_RuntimeError,
6453 "_compat_pickle.IMPORT_MAPPING values should be "
6454 "strings, not %.200s", Py_TYPE(item)->tp_name);
6455 return NULL;
6456 }
6457 module_name = item;
6458 }
6459 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006460 return NULL;
6461 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006462 }
6463 }
6464
Victor Stinnerbb520202013-11-06 22:40:41 +01006465 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006466 if (modules_dict == NULL) {
6467 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006468 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006469 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006470
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006471 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006472 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006473 if (PyErr_Occurred())
6474 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006475 module = PyImport_Import(module_name);
6476 if (module == NULL)
6477 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006478 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006479 Py_DECREF(module);
6480 }
Victor Stinner121aab42011-09-29 23:40:53 +02006481 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006482 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006483 }
6484 return global;
6485}
6486
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006487/*[clinic input]
6488
6489_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6490
6491Returns size in memory, in bytes.
6492[clinic start generated code]*/
6493
6494static Py_ssize_t
6495_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6496/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6497{
6498 Py_ssize_t res;
6499
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006500 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006501 if (self->memo != NULL)
6502 res += self->memo_size * sizeof(PyObject *);
6503 if (self->marks != NULL)
6504 res += self->marks_size * sizeof(Py_ssize_t);
6505 if (self->input_line != NULL)
6506 res += strlen(self->input_line) + 1;
6507 if (self->encoding != NULL)
6508 res += strlen(self->encoding) + 1;
6509 if (self->errors != NULL)
6510 res += strlen(self->errors) + 1;
6511 return res;
6512}
6513
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006514static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006515 _PICKLE_UNPICKLER_LOAD_METHODDEF
6516 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006517 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006518 {NULL, NULL} /* sentinel */
6519};
6520
6521static void
6522Unpickler_dealloc(UnpicklerObject *self)
6523{
6524 PyObject_GC_UnTrack((PyObject *)self);
6525 Py_XDECREF(self->readline);
6526 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006527 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006528 Py_XDECREF(self->stack);
6529 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006530 if (self->buffer.buf != NULL) {
6531 PyBuffer_Release(&self->buffer);
6532 self->buffer.buf = NULL;
6533 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006534
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006535 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006536 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006537 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006538 PyMem_Free(self->encoding);
6539 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540
6541 Py_TYPE(self)->tp_free((PyObject *)self);
6542}
6543
6544static int
6545Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6546{
6547 Py_VISIT(self->readline);
6548 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006549 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006550 Py_VISIT(self->stack);
6551 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006552 return 0;
6553}
6554
6555static int
6556Unpickler_clear(UnpicklerObject *self)
6557{
6558 Py_CLEAR(self->readline);
6559 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006560 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006561 Py_CLEAR(self->stack);
6562 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006563 if (self->buffer.buf != NULL) {
6564 PyBuffer_Release(&self->buffer);
6565 self->buffer.buf = NULL;
6566 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006567
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006568 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006569 PyMem_Free(self->marks);
6570 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006571 PyMem_Free(self->input_line);
6572 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006573 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006574 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006575 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006576 self->errors = NULL;
6577
6578 return 0;
6579}
6580
Larry Hastings61272b72014-01-07 12:41:53 -08006581/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006582
6583_pickle.Unpickler.__init__
6584
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006585 file: object
6586 *
6587 fix_imports: bool = True
6588 encoding: str = 'ASCII'
6589 errors: str = 'strict'
6590
6591This takes a binary file for reading a pickle data stream.
6592
6593The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006594protocol argument is needed. Bytes past the pickled object's
6595representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006596
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006597The argument *file* must have two methods, a read() method that takes
6598an integer argument, and a readline() method that requires no
6599arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006600binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006601other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006602
6603Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006604which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006605generated by Python 2. If *fix_imports* is True, pickle will try to
6606map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006607*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006608instances pickled by Python 2; these default to 'ASCII' and 'strict',
6609respectively. The *encoding* can be 'bytes' to read these 8-bit
6610string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006611[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006612
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006613static int
Larry Hastings89964c42015-04-14 18:07:59 -04006614_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6615 int fix_imports, const char *encoding,
6616 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006617/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006618{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006619 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006620
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006621 /* In case of multiple __init__() calls, clear previous content. */
6622 if (self->read != NULL)
6623 (void)Unpickler_clear(self);
6624
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006625 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006626 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006627
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006628 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006629 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006630
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006631 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006632 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006633 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006634
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006635 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006636 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6637 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006638 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006639 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006640 }
6641 else {
6642 self->pers_func = NULL;
6643 }
6644
6645 self->stack = (Pdata *)Pdata_New();
6646 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006647 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006648
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006649 self->memo_size = 32;
6650 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006651 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006652 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006653
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006654 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006655
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006656 return 0;
6657}
6658
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006659
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006660/* Define a proxy object for the Unpickler's internal memo object. This is to
6661 * avoid breaking code like:
6662 * unpickler.memo.clear()
6663 * and
6664 * unpickler.memo = saved_memo
6665 * Is this a good idea? Not really, but we don't want to break code that uses
6666 * it. Note that we don't implement the entire mapping API here. This is
6667 * intentional, as these should be treated as black-box implementation details.
6668 *
6669 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006670 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006671 */
6672
Larry Hastings61272b72014-01-07 12:41:53 -08006673/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006674_pickle.UnpicklerMemoProxy.clear
6675
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006676Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006677[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006678
Larry Hastings3cceb382014-01-04 11:09:09 -08006679static PyObject *
6680_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006681/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006682{
6683 _Unpickler_MemoCleanup(self->unpickler);
6684 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6685 if (self->unpickler->memo == NULL)
6686 return NULL;
6687 Py_RETURN_NONE;
6688}
6689
Larry Hastings61272b72014-01-07 12:41:53 -08006690/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006691_pickle.UnpicklerMemoProxy.copy
6692
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006693Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006694[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006695
Larry Hastings3cceb382014-01-04 11:09:09 -08006696static PyObject *
6697_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006698/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006699{
6700 Py_ssize_t i;
6701 PyObject *new_memo = PyDict_New();
6702 if (new_memo == NULL)
6703 return NULL;
6704
6705 for (i = 0; i < self->unpickler->memo_size; i++) {
6706 int status;
6707 PyObject *key, *value;
6708
6709 value = self->unpickler->memo[i];
6710 if (value == NULL)
6711 continue;
6712
6713 key = PyLong_FromSsize_t(i);
6714 if (key == NULL)
6715 goto error;
6716 status = PyDict_SetItem(new_memo, key, value);
6717 Py_DECREF(key);
6718 if (status < 0)
6719 goto error;
6720 }
6721 return new_memo;
6722
6723error:
6724 Py_DECREF(new_memo);
6725 return NULL;
6726}
6727
Larry Hastings61272b72014-01-07 12:41:53 -08006728/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006729_pickle.UnpicklerMemoProxy.__reduce__
6730
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006731Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006732[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006733
Larry Hastings3cceb382014-01-04 11:09:09 -08006734static PyObject *
6735_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006736/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006737{
6738 PyObject *reduce_value;
6739 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006740 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006741 if (contents == NULL)
6742 return NULL;
6743
6744 reduce_value = PyTuple_New(2);
6745 if (reduce_value == NULL) {
6746 Py_DECREF(contents);
6747 return NULL;
6748 }
6749 constructor_args = PyTuple_New(1);
6750 if (constructor_args == NULL) {
6751 Py_DECREF(contents);
6752 Py_DECREF(reduce_value);
6753 return NULL;
6754 }
6755 PyTuple_SET_ITEM(constructor_args, 0, contents);
6756 Py_INCREF((PyObject *)&PyDict_Type);
6757 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6758 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6759 return reduce_value;
6760}
6761
6762static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006763 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6764 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6765 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006766 {NULL, NULL} /* sentinel */
6767};
6768
6769static void
6770UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6771{
6772 PyObject_GC_UnTrack(self);
6773 Py_XDECREF(self->unpickler);
6774 PyObject_GC_Del((PyObject *)self);
6775}
6776
6777static int
6778UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6779 visitproc visit, void *arg)
6780{
6781 Py_VISIT(self->unpickler);
6782 return 0;
6783}
6784
6785static int
6786UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6787{
6788 Py_CLEAR(self->unpickler);
6789 return 0;
6790}
6791
6792static PyTypeObject UnpicklerMemoProxyType = {
6793 PyVarObject_HEAD_INIT(NULL, 0)
6794 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6795 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6796 0,
6797 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6798 0, /* tp_print */
6799 0, /* tp_getattr */
6800 0, /* tp_setattr */
6801 0, /* tp_compare */
6802 0, /* tp_repr */
6803 0, /* tp_as_number */
6804 0, /* tp_as_sequence */
6805 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006806 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006807 0, /* tp_call */
6808 0, /* tp_str */
6809 PyObject_GenericGetAttr, /* tp_getattro */
6810 PyObject_GenericSetAttr, /* tp_setattro */
6811 0, /* tp_as_buffer */
6812 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6813 0, /* tp_doc */
6814 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6815 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6816 0, /* tp_richcompare */
6817 0, /* tp_weaklistoffset */
6818 0, /* tp_iter */
6819 0, /* tp_iternext */
6820 unpicklerproxy_methods, /* tp_methods */
6821};
6822
6823static PyObject *
6824UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6825{
6826 UnpicklerMemoProxyObject *self;
6827
6828 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6829 &UnpicklerMemoProxyType);
6830 if (self == NULL)
6831 return NULL;
6832 Py_INCREF(unpickler);
6833 self->unpickler = unpickler;
6834 PyObject_GC_Track(self);
6835 return (PyObject *)self;
6836}
6837
6838/*****************************************************************************/
6839
6840
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006841static PyObject *
6842Unpickler_get_memo(UnpicklerObject *self)
6843{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006844 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006845}
6846
6847static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006848Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006849{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006850 PyObject **new_memo;
6851 Py_ssize_t new_memo_size = 0;
6852 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006853
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006854 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006855 PyErr_SetString(PyExc_TypeError,
6856 "attribute deletion is not supported");
6857 return -1;
6858 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006859
6860 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6861 UnpicklerObject *unpickler =
6862 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6863
6864 new_memo_size = unpickler->memo_size;
6865 new_memo = _Unpickler_NewMemo(new_memo_size);
6866 if (new_memo == NULL)
6867 return -1;
6868
6869 for (i = 0; i < new_memo_size; i++) {
6870 Py_XINCREF(unpickler->memo[i]);
6871 new_memo[i] = unpickler->memo[i];
6872 }
6873 }
6874 else if (PyDict_Check(obj)) {
6875 Py_ssize_t i = 0;
6876 PyObject *key, *value;
6877
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006878 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006879 new_memo = _Unpickler_NewMemo(new_memo_size);
6880 if (new_memo == NULL)
6881 return -1;
6882
6883 while (PyDict_Next(obj, &i, &key, &value)) {
6884 Py_ssize_t idx;
6885 if (!PyLong_Check(key)) {
6886 PyErr_SetString(PyExc_TypeError,
6887 "memo key must be integers");
6888 goto error;
6889 }
6890 idx = PyLong_AsSsize_t(key);
6891 if (idx == -1 && PyErr_Occurred())
6892 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006893 if (idx < 0) {
6894 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006895 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006896 goto error;
6897 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006898 if (_Unpickler_MemoPut(self, idx, value) < 0)
6899 goto error;
6900 }
6901 }
6902 else {
6903 PyErr_Format(PyExc_TypeError,
6904 "'memo' attribute must be an UnpicklerMemoProxy object"
6905 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006906 return -1;
6907 }
6908
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006909 _Unpickler_MemoCleanup(self);
6910 self->memo_size = new_memo_size;
6911 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006912
6913 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006914
6915 error:
6916 if (new_memo_size) {
6917 i = new_memo_size;
6918 while (--i >= 0) {
6919 Py_XDECREF(new_memo[i]);
6920 }
6921 PyMem_FREE(new_memo);
6922 }
6923 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006924}
6925
6926static PyObject *
6927Unpickler_get_persload(UnpicklerObject *self)
6928{
6929 if (self->pers_func == NULL)
6930 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6931 else
6932 Py_INCREF(self->pers_func);
6933 return self->pers_func;
6934}
6935
6936static int
6937Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6938{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006939 if (value == NULL) {
6940 PyErr_SetString(PyExc_TypeError,
6941 "attribute deletion is not supported");
6942 return -1;
6943 }
6944 if (!PyCallable_Check(value)) {
6945 PyErr_SetString(PyExc_TypeError,
6946 "persistent_load must be a callable taking "
6947 "one argument");
6948 return -1;
6949 }
6950
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006951 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03006952 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006953
6954 return 0;
6955}
6956
6957static PyGetSetDef Unpickler_getsets[] = {
6958 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6959 {"persistent_load", (getter)Unpickler_get_persload,
6960 (setter)Unpickler_set_persload},
6961 {NULL}
6962};
6963
6964static PyTypeObject Unpickler_Type = {
6965 PyVarObject_HEAD_INIT(NULL, 0)
6966 "_pickle.Unpickler", /*tp_name*/
6967 sizeof(UnpicklerObject), /*tp_basicsize*/
6968 0, /*tp_itemsize*/
6969 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6970 0, /*tp_print*/
6971 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006972 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006973 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006974 0, /*tp_repr*/
6975 0, /*tp_as_number*/
6976 0, /*tp_as_sequence*/
6977 0, /*tp_as_mapping*/
6978 0, /*tp_hash*/
6979 0, /*tp_call*/
6980 0, /*tp_str*/
6981 0, /*tp_getattro*/
6982 0, /*tp_setattro*/
6983 0, /*tp_as_buffer*/
6984 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006985 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006986 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6987 (inquiry)Unpickler_clear, /*tp_clear*/
6988 0, /*tp_richcompare*/
6989 0, /*tp_weaklistoffset*/
6990 0, /*tp_iter*/
6991 0, /*tp_iternext*/
6992 Unpickler_methods, /*tp_methods*/
6993 0, /*tp_members*/
6994 Unpickler_getsets, /*tp_getset*/
6995 0, /*tp_base*/
6996 0, /*tp_dict*/
6997 0, /*tp_descr_get*/
6998 0, /*tp_descr_set*/
6999 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007000 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007001 PyType_GenericAlloc, /*tp_alloc*/
7002 PyType_GenericNew, /*tp_new*/
7003 PyObject_GC_Del, /*tp_free*/
7004 0, /*tp_is_gc*/
7005};
7006
Larry Hastings61272b72014-01-07 12:41:53 -08007007/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007008
7009_pickle.dump
7010
7011 obj: object
7012 file: object
7013 protocol: object = NULL
7014 *
7015 fix_imports: bool = True
7016
7017Write a pickled representation of obj to the open file object file.
7018
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007019This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7020be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007021
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007022The optional *protocol* argument tells the pickler to use the given
7023protocol supported protocols are 0, 1, 2, 3 and 4. The default
7024protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007025
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007026Specifying a negative protocol version selects the highest protocol
7027version supported. The higher the protocol used, the more recent the
7028version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007029
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007030The *file* argument must have a write() method that accepts a single
7031bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007032writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007033this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007034
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007035If *fix_imports* is True and protocol is less than 3, pickle will try
7036to map the new Python 3 names to the old module names used in Python
70372, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007038[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007039
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007040static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007041_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007042 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007043/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007044{
7045 PicklerObject *pickler = _Pickler_New();
7046
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007047 if (pickler == NULL)
7048 return NULL;
7049
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007050 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007051 goto error;
7052
7053 if (_Pickler_SetOutputStream(pickler, file) < 0)
7054 goto error;
7055
7056 if (dump(pickler, obj) < 0)
7057 goto error;
7058
7059 if (_Pickler_FlushToFile(pickler) < 0)
7060 goto error;
7061
7062 Py_DECREF(pickler);
7063 Py_RETURN_NONE;
7064
7065 error:
7066 Py_XDECREF(pickler);
7067 return NULL;
7068}
7069
Larry Hastings61272b72014-01-07 12:41:53 -08007070/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007071
7072_pickle.dumps
7073
7074 obj: object
7075 protocol: object = NULL
7076 *
7077 fix_imports: bool = True
7078
7079Return the pickled representation of the object as a bytes object.
7080
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007081The optional *protocol* argument tells the pickler to use the given
7082protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7083protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007084
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007085Specifying a negative protocol version selects the highest protocol
7086version supported. The higher the protocol used, the more recent the
7087version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007088
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007089If *fix_imports* is True and *protocol* is less than 3, pickle will
7090try to map the new Python 3 names to the old module names used in
7091Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007092[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007093
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007094static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007095_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007096 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007097/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007098{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007099 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007100 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007101
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007102 if (pickler == NULL)
7103 return NULL;
7104
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007105 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007106 goto error;
7107
7108 if (dump(pickler, obj) < 0)
7109 goto error;
7110
7111 result = _Pickler_GetString(pickler);
7112 Py_DECREF(pickler);
7113 return result;
7114
7115 error:
7116 Py_XDECREF(pickler);
7117 return NULL;
7118}
7119
Larry Hastings61272b72014-01-07 12:41:53 -08007120/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007121
7122_pickle.load
7123
7124 file: object
7125 *
7126 fix_imports: bool = True
7127 encoding: str = 'ASCII'
7128 errors: str = 'strict'
7129
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007130Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007131
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007132This is equivalent to ``Unpickler(file).load()``, but may be more
7133efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007134
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007135The protocol version of the pickle is detected automatically, so no
7136protocol argument is needed. Bytes past the pickled object's
7137representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007138
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007139The argument *file* must have two methods, a read() method that takes
7140an integer argument, and a readline() method that requires no
7141arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007142binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007143other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007144
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007145Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007146which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007147generated by Python 2. If *fix_imports* is True, pickle will try to
7148map the old Python 2 names to the new names used in Python 3. The
7149*encoding* and *errors* tell pickle how to decode 8-bit string
7150instances pickled by Python 2; these default to 'ASCII' and 'strict',
7151respectively. The *encoding* can be 'bytes' to read these 8-bit
7152string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007153[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007154
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007155static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007156_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007157 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007158/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007159{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007160 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007161 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007162
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007163 if (unpickler == NULL)
7164 return NULL;
7165
7166 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7167 goto error;
7168
7169 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7170 goto error;
7171
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007172 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007173
7174 result = load(unpickler);
7175 Py_DECREF(unpickler);
7176 return result;
7177
7178 error:
7179 Py_XDECREF(unpickler);
7180 return NULL;
7181}
7182
Larry Hastings61272b72014-01-07 12:41:53 -08007183/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007184
7185_pickle.loads
7186
7187 data: object
7188 *
7189 fix_imports: bool = True
7190 encoding: str = 'ASCII'
7191 errors: str = 'strict'
7192
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007193Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007194
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007195The protocol version of the pickle is detected automatically, so no
7196protocol argument is needed. Bytes past the pickled object's
7197representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007198
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007199Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007200which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007201generated by Python 2. If *fix_imports* is True, pickle will try to
7202map the old Python 2 names to the new names used in Python 3. The
7203*encoding* and *errors* tell pickle how to decode 8-bit string
7204instances pickled by Python 2; these default to 'ASCII' and 'strict',
7205respectively. The *encoding* can be 'bytes' to read these 8-bit
7206string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007207[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007208
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007209static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007210_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007211 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007212/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007213{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007214 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007215 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007216
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007217 if (unpickler == NULL)
7218 return NULL;
7219
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007220 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007221 goto error;
7222
7223 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7224 goto error;
7225
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007226 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007227
7228 result = load(unpickler);
7229 Py_DECREF(unpickler);
7230 return result;
7231
7232 error:
7233 Py_XDECREF(unpickler);
7234 return NULL;
7235}
7236
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007237static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007238 _PICKLE_DUMP_METHODDEF
7239 _PICKLE_DUMPS_METHODDEF
7240 _PICKLE_LOAD_METHODDEF
7241 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007242 {NULL, NULL} /* sentinel */
7243};
7244
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007245static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007246pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007247{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007248 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007249 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007250}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007251
Stefan Krahf483b0f2013-12-14 13:43:10 +01007252static void
7253pickle_free(PyObject *m)
7254{
7255 _Pickle_ClearState(_Pickle_GetState(m));
7256}
7257
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007258static int
7259pickle_traverse(PyObject *m, visitproc visit, void *arg)
7260{
7261 PickleState *st = _Pickle_GetState(m);
7262 Py_VISIT(st->PickleError);
7263 Py_VISIT(st->PicklingError);
7264 Py_VISIT(st->UnpicklingError);
7265 Py_VISIT(st->dispatch_table);
7266 Py_VISIT(st->extension_registry);
7267 Py_VISIT(st->extension_cache);
7268 Py_VISIT(st->inverted_registry);
7269 Py_VISIT(st->name_mapping_2to3);
7270 Py_VISIT(st->import_mapping_2to3);
7271 Py_VISIT(st->name_mapping_3to2);
7272 Py_VISIT(st->import_mapping_3to2);
7273 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007274 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007275 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007276}
7277
7278static struct PyModuleDef _picklemodule = {
7279 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007280 "_pickle", /* m_name */
7281 pickle_module_doc, /* m_doc */
7282 sizeof(PickleState), /* m_size */
7283 pickle_methods, /* m_methods */
7284 NULL, /* m_reload */
7285 pickle_traverse, /* m_traverse */
7286 pickle_clear, /* m_clear */
7287 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007288};
7289
7290PyMODINIT_FUNC
7291PyInit__pickle(void)
7292{
7293 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007294 PickleState *st;
7295
7296 m = PyState_FindModule(&_picklemodule);
7297 if (m) {
7298 Py_INCREF(m);
7299 return m;
7300 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007301
7302 if (PyType_Ready(&Unpickler_Type) < 0)
7303 return NULL;
7304 if (PyType_Ready(&Pickler_Type) < 0)
7305 return NULL;
7306 if (PyType_Ready(&Pdata_Type) < 0)
7307 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007308 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7309 return NULL;
7310 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7311 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007312
7313 /* Create the module and add the functions. */
7314 m = PyModule_Create(&_picklemodule);
7315 if (m == NULL)
7316 return NULL;
7317
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007318 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007319 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7320 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007321 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007322 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7323 return NULL;
7324
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007325 st = _Pickle_GetState(m);
7326
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007327 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007328 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7329 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007330 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007331 st->PicklingError = \
7332 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7333 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007334 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007335 st->UnpicklingError = \
7336 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7337 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007338 return NULL;
7339
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007340 Py_INCREF(st->PickleError);
7341 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007342 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007343 Py_INCREF(st->PicklingError);
7344 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007345 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007346 Py_INCREF(st->UnpicklingError);
7347 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007348 return NULL;
7349
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007350 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007351 return NULL;
7352
7353 return m;
7354}