blob: 79113e0a93bb770ad747dabb5e188285529b5ca7 [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 Stinner559bb6a2016-08-22 22:48:54 +0200349 result = _PyObject_CallArg1(func, obj);
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 {
1944 char *string;
1945
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;
2221 char *data;
2222 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
2790 assert(obj != NULL);
2791 assert(self->proto > 0);
2792
2793 dict_size = PyDict_Size(obj);
2794
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;
2822 if (PyDict_Size(obj) != dict_size) {
2823 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;
2840
2841 if (self->fast && !fast_save_enter(self, obj))
2842 goto error;
2843
2844 /* Create an empty dict. */
2845 if (self->bin) {
2846 header[0] = EMPTY_DICT;
2847 len = 1;
2848 }
2849 else {
2850 header[0] = MARK;
2851 header[1] = DICT;
2852 len = 2;
2853 }
2854
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002855 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002856 goto error;
2857
2858 /* Get dict size, and bow out early if empty. */
2859 if ((len = PyDict_Size(obj)) < 0)
2860 goto error;
2861
2862 if (memo_put(self, obj) < 0)
2863 goto error;
2864
2865 if (len != 0) {
2866 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002867 if (PyDict_CheckExact(obj) && self->proto > 0) {
2868 /* We can take certain shortcuts if we know this is a dict and
2869 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002870 if (Py_EnterRecursiveCall(" while pickling an object"))
2871 goto error;
2872 status = batch_dict_exact(self, obj);
2873 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002874 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002875 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002876
Victor Stinnerad8c83a2016-09-05 17:53:15 -07002877 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002878 if (items == NULL)
2879 goto error;
2880 iter = PyObject_GetIter(items);
2881 Py_DECREF(items);
2882 if (iter == NULL)
2883 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002884 if (Py_EnterRecursiveCall(" while pickling an object")) {
2885 Py_DECREF(iter);
2886 goto error;
2887 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002888 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002889 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002890 Py_DECREF(iter);
2891 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002892 }
2893
2894 if (0) {
2895 error:
2896 status = -1;
2897 }
2898
2899 if (self->fast && !fast_save_leave(self, obj))
2900 status = -1;
2901
2902 return status;
2903}
2904
2905static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002906save_set(PicklerObject *self, PyObject *obj)
2907{
2908 PyObject *item;
2909 int i;
2910 Py_ssize_t set_size, ppos = 0;
2911 Py_hash_t hash;
2912
2913 const char empty_set_op = EMPTY_SET;
2914 const char mark_op = MARK;
2915 const char additems_op = ADDITEMS;
2916
2917 if (self->proto < 4) {
2918 PyObject *items;
2919 PyObject *reduce_value;
2920 int status;
2921
2922 items = PySequence_List(obj);
2923 if (items == NULL) {
2924 return -1;
2925 }
2926 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2927 Py_DECREF(items);
2928 if (reduce_value == NULL) {
2929 return -1;
2930 }
2931 /* save_reduce() will memoize the object automatically. */
2932 status = save_reduce(self, reduce_value, obj);
2933 Py_DECREF(reduce_value);
2934 return status;
2935 }
2936
2937 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2938 return -1;
2939
2940 if (memo_put(self, obj) < 0)
2941 return -1;
2942
2943 set_size = PySet_GET_SIZE(obj);
2944 if (set_size == 0)
2945 return 0; /* nothing to do */
2946
2947 /* Write in batches of BATCHSIZE. */
2948 do {
2949 i = 0;
2950 if (_Pickler_Write(self, &mark_op, 1) < 0)
2951 return -1;
2952 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2953 if (save(self, item, 0) < 0)
2954 return -1;
2955 if (++i == BATCHSIZE)
2956 break;
2957 }
2958 if (_Pickler_Write(self, &additems_op, 1) < 0)
2959 return -1;
2960 if (PySet_GET_SIZE(obj) != set_size) {
2961 PyErr_Format(
2962 PyExc_RuntimeError,
2963 "set changed size during iteration");
2964 return -1;
2965 }
2966 } while (i == BATCHSIZE);
2967
2968 return 0;
2969}
2970
2971static int
2972save_frozenset(PicklerObject *self, PyObject *obj)
2973{
2974 PyObject *iter;
2975
2976 const char mark_op = MARK;
2977 const char frozenset_op = FROZENSET;
2978
2979 if (self->fast && !fast_save_enter(self, obj))
2980 return -1;
2981
2982 if (self->proto < 4) {
2983 PyObject *items;
2984 PyObject *reduce_value;
2985 int status;
2986
2987 items = PySequence_List(obj);
2988 if (items == NULL) {
2989 return -1;
2990 }
2991 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2992 items);
2993 Py_DECREF(items);
2994 if (reduce_value == NULL) {
2995 return -1;
2996 }
2997 /* save_reduce() will memoize the object automatically. */
2998 status = save_reduce(self, reduce_value, obj);
2999 Py_DECREF(reduce_value);
3000 return status;
3001 }
3002
3003 if (_Pickler_Write(self, &mark_op, 1) < 0)
3004 return -1;
3005
3006 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003007 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003008 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003009 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003010 for (;;) {
3011 PyObject *item;
3012
3013 item = PyIter_Next(iter);
3014 if (item == NULL) {
3015 if (PyErr_Occurred()) {
3016 Py_DECREF(iter);
3017 return -1;
3018 }
3019 break;
3020 }
3021 if (save(self, item, 0) < 0) {
3022 Py_DECREF(item);
3023 Py_DECREF(iter);
3024 return -1;
3025 }
3026 Py_DECREF(item);
3027 }
3028 Py_DECREF(iter);
3029
3030 /* If the object is already in the memo, this means it is
3031 recursive. In this case, throw away everything we put on the
3032 stack, and fetch the object back from the memo. */
3033 if (PyMemoTable_Get(self->memo, obj)) {
3034 const char pop_mark_op = POP_MARK;
3035
3036 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3037 return -1;
3038 if (memo_get(self, obj) < 0)
3039 return -1;
3040 return 0;
3041 }
3042
3043 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3044 return -1;
3045 if (memo_put(self, obj) < 0)
3046 return -1;
3047
3048 return 0;
3049}
3050
3051static int
3052fix_imports(PyObject **module_name, PyObject **global_name)
3053{
3054 PyObject *key;
3055 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003056 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003057
3058 key = PyTuple_Pack(2, *module_name, *global_name);
3059 if (key == NULL)
3060 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003061 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003062 Py_DECREF(key);
3063 if (item) {
3064 PyObject *fixed_module_name;
3065 PyObject *fixed_global_name;
3066
3067 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3068 PyErr_Format(PyExc_RuntimeError,
3069 "_compat_pickle.REVERSE_NAME_MAPPING values "
3070 "should be 2-tuples, not %.200s",
3071 Py_TYPE(item)->tp_name);
3072 return -1;
3073 }
3074 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3075 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3076 if (!PyUnicode_Check(fixed_module_name) ||
3077 !PyUnicode_Check(fixed_global_name)) {
3078 PyErr_Format(PyExc_RuntimeError,
3079 "_compat_pickle.REVERSE_NAME_MAPPING values "
3080 "should be pairs of str, not (%.200s, %.200s)",
3081 Py_TYPE(fixed_module_name)->tp_name,
3082 Py_TYPE(fixed_global_name)->tp_name);
3083 return -1;
3084 }
3085
3086 Py_CLEAR(*module_name);
3087 Py_CLEAR(*global_name);
3088 Py_INCREF(fixed_module_name);
3089 Py_INCREF(fixed_global_name);
3090 *module_name = fixed_module_name;
3091 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003092 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003093 }
3094 else if (PyErr_Occurred()) {
3095 return -1;
3096 }
3097
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003098 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003099 if (item) {
3100 if (!PyUnicode_Check(item)) {
3101 PyErr_Format(PyExc_RuntimeError,
3102 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3103 "should be strings, not %.200s",
3104 Py_TYPE(item)->tp_name);
3105 return -1;
3106 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003107 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003108 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003109 }
3110 else if (PyErr_Occurred()) {
3111 return -1;
3112 }
3113
3114 return 0;
3115}
3116
3117static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003118save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3119{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003120 PyObject *global_name = NULL;
3121 PyObject *module_name = NULL;
3122 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003123 PyObject *parent = NULL;
3124 PyObject *dotted_path = NULL;
3125 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003126 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003127 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003128 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003129 _Py_IDENTIFIER(__name__);
3130 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003131
3132 const char global_op = GLOBAL;
3133
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003134 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003135 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003136 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003137 }
3138 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003139 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3140 if (global_name == NULL) {
3141 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3142 goto error;
3143 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003144 }
3145 if (global_name == NULL) {
3146 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3147 if (global_name == NULL)
3148 goto error;
3149 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003150 }
3151
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003152 dotted_path = get_dotted_path(module, global_name);
3153 if (dotted_path == NULL)
3154 goto error;
3155 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003156 if (module_name == NULL)
3157 goto error;
3158
3159 /* XXX: Change to use the import C API directly with level=0 to disallow
3160 relative imports.
3161
3162 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3163 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3164 custom import functions (IMHO, this would be a nice security
3165 feature). The import C API would need to be extended to support the
3166 extra parameters of __import__ to fix that. */
3167 module = PyImport_Import(module_name);
3168 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003169 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003170 "Can't pickle %R: import of module %R failed",
3171 obj, module_name);
3172 goto error;
3173 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003174 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3175 Py_INCREF(lastname);
3176 cls = get_deep_attribute(module, dotted_path, &parent);
3177 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003178 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003179 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003180 "Can't pickle %R: attribute lookup %S on %S failed",
3181 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003182 goto error;
3183 }
3184 if (cls != obj) {
3185 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003186 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003187 "Can't pickle %R: it's not the same object as %S.%S",
3188 obj, module_name, global_name);
3189 goto error;
3190 }
3191 Py_DECREF(cls);
3192
3193 if (self->proto >= 2) {
3194 /* See whether this is in the extension registry, and if
3195 * so generate an EXT opcode.
3196 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003197 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003198 PyObject *code_obj; /* extension code as Python object */
3199 long code; /* extension code as C value */
3200 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003201 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003202
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003203 extension_key = PyTuple_Pack(2, module_name, global_name);
3204 if (extension_key == NULL) {
3205 goto error;
3206 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003207 code_obj = PyDict_GetItemWithError(st->extension_registry,
3208 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003209 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003210 /* The object is not registered in the extension registry.
3211 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003212 if (code_obj == NULL) {
3213 if (PyErr_Occurred()) {
3214 goto error;
3215 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003216 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003217 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003218
3219 /* XXX: pickle.py doesn't check neither the type, nor the range
3220 of the value returned by the extension_registry. It should for
3221 consistency. */
3222
3223 /* Verify code_obj has the right type and value. */
3224 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003225 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003226 "Can't pickle %R: extension code %R isn't an integer",
3227 obj, code_obj);
3228 goto error;
3229 }
3230 code = PyLong_AS_LONG(code_obj);
3231 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003232 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003233 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3234 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003235 goto error;
3236 }
3237
3238 /* Generate an EXT opcode. */
3239 if (code <= 0xff) {
3240 pdata[0] = EXT1;
3241 pdata[1] = (unsigned char)code;
3242 n = 2;
3243 }
3244 else if (code <= 0xffff) {
3245 pdata[0] = EXT2;
3246 pdata[1] = (unsigned char)(code & 0xff);
3247 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3248 n = 3;
3249 }
3250 else {
3251 pdata[0] = EXT4;
3252 pdata[1] = (unsigned char)(code & 0xff);
3253 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3254 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3255 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3256 n = 5;
3257 }
3258
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003259 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003260 goto error;
3261 }
3262 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003263 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003264 if (parent == module) {
3265 Py_INCREF(lastname);
3266 Py_DECREF(global_name);
3267 global_name = lastname;
3268 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003269 if (self->proto >= 4) {
3270 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003271
Christian Heimese8b1ba12013-11-23 21:13:39 +01003272 if (save(self, module_name, 0) < 0)
3273 goto error;
3274 if (save(self, global_name, 0) < 0)
3275 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003276
3277 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3278 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003279 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003280 else if (parent != module) {
3281 PickleState *st = _Pickle_GetGlobalState();
3282 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3283 st->getattr, parent, lastname);
3284 status = save_reduce(self, reduce_value, NULL);
3285 Py_DECREF(reduce_value);
3286 if (status < 0)
3287 goto error;
3288 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003289 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003290 /* Generate a normal global opcode if we are using a pickle
3291 protocol < 4, or if the object is not registered in the
3292 extension registry. */
3293 PyObject *encoded;
3294 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003295
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003296 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003297 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003298
3299 /* For protocol < 3 and if the user didn't request against doing
3300 so, we convert module names to the old 2.x module names. */
3301 if (self->proto < 3 && self->fix_imports) {
3302 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003303 goto error;
3304 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003305 }
3306
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003307 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3308 both the module name and the global name using UTF-8. We do so
3309 only when we are using the pickle protocol newer than version
3310 3. This is to ensure compatibility with older Unpickler running
3311 on Python 2.x. */
3312 if (self->proto == 3) {
3313 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003314 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003315 else {
3316 unicode_encoder = PyUnicode_AsASCIIString;
3317 }
3318 encoded = unicode_encoder(module_name);
3319 if (encoded == NULL) {
3320 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003321 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003322 "can't pickle module identifier '%S' using "
3323 "pickle protocol %i",
3324 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003325 goto error;
3326 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003327 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3328 PyBytes_GET_SIZE(encoded)) < 0) {
3329 Py_DECREF(encoded);
3330 goto error;
3331 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003332 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003333 if(_Pickler_Write(self, "\n", 1) < 0)
3334 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003335
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003336 /* Save the name of the module. */
3337 encoded = unicode_encoder(global_name);
3338 if (encoded == NULL) {
3339 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003340 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003341 "can't pickle global identifier '%S' using "
3342 "pickle protocol %i",
3343 global_name, self->proto);
3344 goto error;
3345 }
3346 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3347 PyBytes_GET_SIZE(encoded)) < 0) {
3348 Py_DECREF(encoded);
3349 goto error;
3350 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003351 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003352 if (_Pickler_Write(self, "\n", 1) < 0)
3353 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003354 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003355 /* Memoize the object. */
3356 if (memo_put(self, obj) < 0)
3357 goto error;
3358 }
3359
3360 if (0) {
3361 error:
3362 status = -1;
3363 }
3364 Py_XDECREF(module_name);
3365 Py_XDECREF(global_name);
3366 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003367 Py_XDECREF(parent);
3368 Py_XDECREF(dotted_path);
3369 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003370
3371 return status;
3372}
3373
3374static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003375save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3376{
3377 PyObject *reduce_value;
3378 int status;
3379
3380 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3381 if (reduce_value == NULL) {
3382 return -1;
3383 }
3384 status = save_reduce(self, reduce_value, obj);
3385 Py_DECREF(reduce_value);
3386 return status;
3387}
3388
3389static int
3390save_type(PicklerObject *self, PyObject *obj)
3391{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003392 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003393 return save_singleton_type(self, obj, Py_None);
3394 }
3395 else if (obj == (PyObject *)&PyEllipsis_Type) {
3396 return save_singleton_type(self, obj, Py_Ellipsis);
3397 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003398 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003399 return save_singleton_type(self, obj, Py_NotImplemented);
3400 }
3401 return save_global(self, obj, NULL);
3402}
3403
3404static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003405save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3406{
3407 PyObject *pid = NULL;
3408 int status = 0;
3409
3410 const char persid_op = PERSID;
3411 const char binpersid_op = BINPERSID;
3412
3413 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003414 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003415 if (pid == NULL)
3416 return -1;
3417
3418 if (pid != Py_None) {
3419 if (self->bin) {
3420 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003421 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003422 goto error;
3423 }
3424 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003425 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003426
3427 pid_str = PyObject_Str(pid);
3428 if (pid_str == NULL)
3429 goto error;
3430
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003431 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003432 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003433 if (!PyUnicode_IS_ASCII(pid_str)) {
3434 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3435 "persistent IDs in protocol 0 must be "
3436 "ASCII strings");
3437 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003438 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003439 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003440
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003441 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003442 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3443 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3444 _Pickler_Write(self, "\n", 1) < 0) {
3445 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003446 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003447 }
3448 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003449 }
3450 status = 1;
3451 }
3452
3453 if (0) {
3454 error:
3455 status = -1;
3456 }
3457 Py_XDECREF(pid);
3458
3459 return status;
3460}
3461
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003462static PyObject *
3463get_class(PyObject *obj)
3464{
3465 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003466 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003467
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003468 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003469 if (cls == NULL) {
3470 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3471 PyErr_Clear();
3472 cls = (PyObject *) Py_TYPE(obj);
3473 Py_INCREF(cls);
3474 }
3475 }
3476 return cls;
3477}
3478
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003479/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3480 * appropriate __reduce__ method for obj.
3481 */
3482static int
3483save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3484{
3485 PyObject *callable;
3486 PyObject *argtup;
3487 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003488 PyObject *listitems = Py_None;
3489 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003490 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003491 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003492 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003493
3494 const char reduce_op = REDUCE;
3495 const char build_op = BUILD;
3496 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003497 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003498
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003499 size = PyTuple_Size(args);
3500 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003501 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003502 "__reduce__ must contain 2 through 5 elements");
3503 return -1;
3504 }
3505
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003506 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3507 &callable, &argtup, &state, &listitems, &dictitems))
3508 return -1;
3509
3510 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003511 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003512 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003513 return -1;
3514 }
3515 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003516 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003517 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003518 return -1;
3519 }
3520
3521 if (state == Py_None)
3522 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003523
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524 if (listitems == Py_None)
3525 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003526 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003527 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003528 "returned by __reduce__ must be an iterator, not %s",
3529 Py_TYPE(listitems)->tp_name);
3530 return -1;
3531 }
3532
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003533 if (dictitems == Py_None)
3534 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003535 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003536 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003537 "returned by __reduce__ must be an iterator, not %s",
3538 Py_TYPE(dictitems)->tp_name);
3539 return -1;
3540 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003541
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003542 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003543 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003544 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003545
Victor Stinner804e05e2013-11-14 01:26:17 +01003546 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003547 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003548 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003549 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003550 }
3551 PyErr_Clear();
3552 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003553 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003554 _Py_IDENTIFIER(__newobj_ex__);
3555 use_newobj_ex = PyUnicode_Compare(
3556 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003557 if (!use_newobj_ex) {
3558 _Py_IDENTIFIER(__newobj__);
3559 use_newobj = PyUnicode_Compare(
3560 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3561 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003562 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003563 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003564 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003565
3566 if (use_newobj_ex) {
3567 PyObject *cls;
3568 PyObject *args;
3569 PyObject *kwargs;
3570
3571 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003572 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003573 "length of the NEWOBJ_EX argument tuple must be "
3574 "exactly 3, not %zd", Py_SIZE(argtup));
3575 return -1;
3576 }
3577
3578 cls = PyTuple_GET_ITEM(argtup, 0);
3579 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003580 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003581 "first item from NEWOBJ_EX argument tuple must "
3582 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3583 return -1;
3584 }
3585 args = PyTuple_GET_ITEM(argtup, 1);
3586 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003587 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003588 "second item from NEWOBJ_EX argument tuple must "
3589 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3590 return -1;
3591 }
3592 kwargs = PyTuple_GET_ITEM(argtup, 2);
3593 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003594 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003595 "third item from NEWOBJ_EX argument tuple must "
3596 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3597 return -1;
3598 }
3599
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003600 if (self->proto >= 4) {
3601 if (save(self, cls, 0) < 0 ||
3602 save(self, args, 0) < 0 ||
3603 save(self, kwargs, 0) < 0 ||
3604 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3605 return -1;
3606 }
3607 }
3608 else {
3609 PyObject *newargs;
3610 PyObject *cls_new;
3611 Py_ssize_t i;
3612 _Py_IDENTIFIER(__new__);
3613
3614 newargs = PyTuple_New(Py_SIZE(args) + 2);
3615 if (newargs == NULL)
3616 return -1;
3617
3618 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3619 if (cls_new == NULL) {
3620 Py_DECREF(newargs);
3621 return -1;
3622 }
3623 PyTuple_SET_ITEM(newargs, 0, cls_new);
3624 Py_INCREF(cls);
3625 PyTuple_SET_ITEM(newargs, 1, cls);
3626 for (i = 0; i < Py_SIZE(args); i++) {
3627 PyObject *item = PyTuple_GET_ITEM(args, i);
3628 Py_INCREF(item);
3629 PyTuple_SET_ITEM(newargs, i + 2, item);
3630 }
3631
3632 callable = PyObject_Call(st->partial, newargs, kwargs);
3633 Py_DECREF(newargs);
3634 if (callable == NULL)
3635 return -1;
3636
3637 newargs = PyTuple_New(0);
3638 if (newargs == NULL) {
3639 Py_DECREF(callable);
3640 return -1;
3641 }
3642
3643 if (save(self, callable, 0) < 0 ||
3644 save(self, newargs, 0) < 0 ||
3645 _Pickler_Write(self, &reduce_op, 1) < 0) {
3646 Py_DECREF(newargs);
3647 Py_DECREF(callable);
3648 return -1;
3649 }
3650 Py_DECREF(newargs);
3651 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003652 }
3653 }
3654 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003655 PyObject *cls;
3656 PyObject *newargtup;
3657 PyObject *obj_class;
3658 int p;
3659
3660 /* Sanity checks. */
3661 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003662 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003663 return -1;
3664 }
3665
3666 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003667 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003668 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003669 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003670 return -1;
3671 }
3672
3673 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003674 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003675 p = obj_class != cls; /* true iff a problem */
3676 Py_DECREF(obj_class);
3677 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003678 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003679 "__newobj__ args has the wrong class");
3680 return -1;
3681 }
3682 }
3683 /* XXX: These calls save() are prone to infinite recursion. Imagine
3684 what happen if the value returned by the __reduce__() method of
3685 some extension type contains another object of the same type. Ouch!
3686
3687 Here is a quick example, that I ran into, to illustrate what I
3688 mean:
3689
3690 >>> import pickle, copyreg
3691 >>> copyreg.dispatch_table.pop(complex)
3692 >>> pickle.dumps(1+2j)
3693 Traceback (most recent call last):
3694 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003695 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003696
3697 Removing the complex class from copyreg.dispatch_table made the
3698 __reduce_ex__() method emit another complex object:
3699
3700 >>> (1+1j).__reduce_ex__(2)
3701 (<function __newobj__ at 0xb7b71c3c>,
3702 (<class 'complex'>, (1+1j)), None, None, None)
3703
3704 Thus when save() was called on newargstup (the 2nd item) recursion
3705 ensued. Of course, the bug was in the complex class which had a
3706 broken __getnewargs__() that emitted another complex object. But,
3707 the point, here, is it is quite easy to end up with a broken reduce
3708 function. */
3709
3710 /* Save the class and its __new__ arguments. */
3711 if (save(self, cls, 0) < 0)
3712 return -1;
3713
3714 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3715 if (newargtup == NULL)
3716 return -1;
3717
3718 p = save(self, newargtup, 0);
3719 Py_DECREF(newargtup);
3720 if (p < 0)
3721 return -1;
3722
3723 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003724 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003725 return -1;
3726 }
3727 else { /* Not using NEWOBJ. */
3728 if (save(self, callable, 0) < 0 ||
3729 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003730 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003731 return -1;
3732 }
3733
3734 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3735 the caller do not want to memoize the object. Not particularly useful,
3736 but that is to mimic the behavior save_reduce() in pickle.py when
3737 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003738 if (obj != NULL) {
3739 /* If the object is already in the memo, this means it is
3740 recursive. In this case, throw away everything we put on the
3741 stack, and fetch the object back from the memo. */
3742 if (PyMemoTable_Get(self->memo, obj)) {
3743 const char pop_op = POP;
3744
3745 if (_Pickler_Write(self, &pop_op, 1) < 0)
3746 return -1;
3747 if (memo_get(self, obj) < 0)
3748 return -1;
3749
3750 return 0;
3751 }
3752 else if (memo_put(self, obj) < 0)
3753 return -1;
3754 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003755
3756 if (listitems && batch_list(self, listitems) < 0)
3757 return -1;
3758
3759 if (dictitems && batch_dict(self, dictitems) < 0)
3760 return -1;
3761
3762 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003763 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003764 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003765 return -1;
3766 }
3767
3768 return 0;
3769}
3770
3771static int
3772save(PicklerObject *self, PyObject *obj, int pers_save)
3773{
3774 PyTypeObject *type;
3775 PyObject *reduce_func = NULL;
3776 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003777 int status = 0;
3778
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003779 if (_Pickler_OpcodeBoundary(self) < 0)
3780 return -1;
3781
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003782 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003783 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003784
3785 /* The extra pers_save argument is necessary to avoid calling save_pers()
3786 on its returned object. */
3787 if (!pers_save && self->pers_func) {
3788 /* save_pers() returns:
3789 -1 to signal an error;
3790 0 if it did nothing successfully;
3791 1 if a persistent id was saved.
3792 */
3793 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3794 goto done;
3795 }
3796
3797 type = Py_TYPE(obj);
3798
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003799 /* The old cPickle had an optimization that used switch-case statement
3800 dispatching on the first letter of the type name. This has was removed
3801 since benchmarks shown that this optimization was actually slowing
3802 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003803
3804 /* Atom types; these aren't memoized, so don't check the memo. */
3805
3806 if (obj == Py_None) {
3807 status = save_none(self, obj);
3808 goto done;
3809 }
3810 else if (obj == Py_False || obj == Py_True) {
3811 status = save_bool(self, obj);
3812 goto done;
3813 }
3814 else if (type == &PyLong_Type) {
3815 status = save_long(self, obj);
3816 goto done;
3817 }
3818 else if (type == &PyFloat_Type) {
3819 status = save_float(self, obj);
3820 goto done;
3821 }
3822
3823 /* Check the memo to see if it has the object. If so, generate
3824 a GET (or BINGET) opcode, instead of pickling the object
3825 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003826 if (PyMemoTable_Get(self->memo, obj)) {
3827 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003828 goto error;
3829 goto done;
3830 }
3831
3832 if (type == &PyBytes_Type) {
3833 status = save_bytes(self, obj);
3834 goto done;
3835 }
3836 else if (type == &PyUnicode_Type) {
3837 status = save_unicode(self, obj);
3838 goto done;
3839 }
3840 else if (type == &PyDict_Type) {
3841 status = save_dict(self, obj);
3842 goto done;
3843 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003844 else if (type == &PySet_Type) {
3845 status = save_set(self, obj);
3846 goto done;
3847 }
3848 else if (type == &PyFrozenSet_Type) {
3849 status = save_frozenset(self, obj);
3850 goto done;
3851 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003852 else if (type == &PyList_Type) {
3853 status = save_list(self, obj);
3854 goto done;
3855 }
3856 else if (type == &PyTuple_Type) {
3857 status = save_tuple(self, obj);
3858 goto done;
3859 }
3860 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003861 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003862 goto done;
3863 }
3864 else if (type == &PyFunction_Type) {
3865 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003866 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003867 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003868
3869 /* XXX: This part needs some unit tests. */
3870
3871 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003872 * self.dispatch_table, copyreg.dispatch_table, the object's
3873 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003874 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003875 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003876 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003877 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3878 (PyObject *)type);
3879 if (reduce_func == NULL) {
3880 if (PyErr_Occurred()) {
3881 goto error;
3882 }
3883 } else {
3884 /* PyDict_GetItemWithError() returns a borrowed reference.
3885 Increase the reference count to be consistent with
3886 PyObject_GetItem and _PyObject_GetAttrId used below. */
3887 Py_INCREF(reduce_func);
3888 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003889 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003890 reduce_func = PyObject_GetItem(self->dispatch_table,
3891 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003892 if (reduce_func == NULL) {
3893 if (PyErr_ExceptionMatches(PyExc_KeyError))
3894 PyErr_Clear();
3895 else
3896 goto error;
3897 }
3898 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003900 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003901 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003902 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003903 else if (PyType_IsSubtype(type, &PyType_Type)) {
3904 status = save_global(self, obj, NULL);
3905 goto done;
3906 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003907 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003908 _Py_IDENTIFIER(__reduce__);
3909 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003910
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003911
3912 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3913 automatically defined as __reduce__. While this is convenient, this
3914 make it impossible to know which method was actually called. Of
3915 course, this is not a big deal. But still, it would be nice to let
3916 the user know which method was called when something go
3917 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3918 don't actually have to check for a __reduce__ method. */
3919
3920 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003921 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003922 if (reduce_func != NULL) {
3923 PyObject *proto;
3924 proto = PyLong_FromLong(self->proto);
3925 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003926 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003927 }
3928 }
3929 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003930 PickleState *st = _Pickle_GetGlobalState();
3931
3932 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003934 }
3935 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003936 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003937 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003938 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003939 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003940 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02003941 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003942 }
3943 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003944 PyErr_Format(st->PicklingError,
3945 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003946 type->tp_name, obj);
3947 goto error;
3948 }
3949 }
3950 }
3951
3952 if (reduce_value == NULL)
3953 goto error;
3954
3955 if (PyUnicode_Check(reduce_value)) {
3956 status = save_global(self, obj, reduce_value);
3957 goto done;
3958 }
3959
3960 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003961 PickleState *st = _Pickle_GetGlobalState();
3962 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003963 "__reduce__ must return a string or tuple");
3964 goto error;
3965 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003966
3967 status = save_reduce(self, reduce_value, obj);
3968
3969 if (0) {
3970 error:
3971 status = -1;
3972 }
3973 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003974
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003975 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003976 Py_XDECREF(reduce_func);
3977 Py_XDECREF(reduce_value);
3978
3979 return status;
3980}
3981
3982static int
3983dump(PicklerObject *self, PyObject *obj)
3984{
3985 const char stop_op = STOP;
3986
3987 if (self->proto >= 2) {
3988 char header[2];
3989
3990 header[0] = PROTO;
3991 assert(self->proto >= 0 && self->proto < 256);
3992 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003993 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003994 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003995 if (self->proto >= 4)
3996 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003997 }
3998
3999 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004000 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004001 return -1;
4002
4003 return 0;
4004}
4005
Larry Hastings61272b72014-01-07 12:41:53 -08004006/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004007
4008_pickle.Pickler.clear_memo
4009
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004010Clears the pickler's "memo".
4011
4012The memo is the data structure that remembers which objects the
4013pickler has already seen, so that shared or recursive objects are
4014pickled by reference and not by value. This method is useful when
4015re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004016[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004017
Larry Hastings3cceb382014-01-04 11:09:09 -08004018static PyObject *
4019_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004020/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004021{
4022 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004023 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004024
4025 Py_RETURN_NONE;
4026}
4027
Larry Hastings61272b72014-01-07 12:41:53 -08004028/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004029
4030_pickle.Pickler.dump
4031
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004032 obj: object
4033 /
4034
4035Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004036[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004037
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004038static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004039_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004040/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004041{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004042 /* Check whether the Pickler was initialized correctly (issue3664).
4043 Developers often forget to call __init__() in their subclasses, which
4044 would trigger a segfault without this check. */
4045 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004046 PickleState *st = _Pickle_GetGlobalState();
4047 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004048 "Pickler.__init__() was not called by %s.__init__()",
4049 Py_TYPE(self)->tp_name);
4050 return NULL;
4051 }
4052
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004053 if (_Pickler_ClearBuffer(self) < 0)
4054 return NULL;
4055
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004056 if (dump(self, obj) < 0)
4057 return NULL;
4058
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004059 if (_Pickler_FlushToFile(self) < 0)
4060 return NULL;
4061
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004062 Py_RETURN_NONE;
4063}
4064
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004065/*[clinic input]
4066
4067_pickle.Pickler.__sizeof__ -> Py_ssize_t
4068
4069Returns size in memory, in bytes.
4070[clinic start generated code]*/
4071
4072static Py_ssize_t
4073_pickle_Pickler___sizeof___impl(PicklerObject *self)
4074/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4075{
4076 Py_ssize_t res, s;
4077
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004078 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004079 if (self->memo != NULL) {
4080 res += sizeof(PyMemoTable);
4081 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4082 }
4083 if (self->output_buffer != NULL) {
4084 s = _PySys_GetSizeOf(self->output_buffer);
4085 if (s == -1)
4086 return -1;
4087 res += s;
4088 }
4089 return res;
4090}
4091
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004092static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004093 _PICKLE_PICKLER_DUMP_METHODDEF
4094 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004095 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004096 {NULL, NULL} /* sentinel */
4097};
4098
4099static void
4100Pickler_dealloc(PicklerObject *self)
4101{
4102 PyObject_GC_UnTrack(self);
4103
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004104 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004105 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004106 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004107 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004108 Py_XDECREF(self->fast_memo);
4109
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004110 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004111
4112 Py_TYPE(self)->tp_free((PyObject *)self);
4113}
4114
4115static int
4116Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4117{
4118 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004119 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004120 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004121 Py_VISIT(self->fast_memo);
4122 return 0;
4123}
4124
4125static int
4126Pickler_clear(PicklerObject *self)
4127{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004128 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004130 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004131 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004132 Py_CLEAR(self->fast_memo);
4133
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004134 if (self->memo != NULL) {
4135 PyMemoTable *memo = self->memo;
4136 self->memo = NULL;
4137 PyMemoTable_Del(memo);
4138 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004139 return 0;
4140}
4141
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004142
Larry Hastings61272b72014-01-07 12:41:53 -08004143/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004144
4145_pickle.Pickler.__init__
4146
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004147 file: object
4148 protocol: object = NULL
4149 fix_imports: bool = True
4150
4151This takes a binary file for writing a pickle data stream.
4152
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004153The optional *protocol* argument tells the pickler to use the given
4154protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4155protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004156
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004157Specifying a negative protocol version selects the highest protocol
4158version supported. The higher the protocol used, the more recent the
4159version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004160
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004161The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004162bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004163writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004164this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004165
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004166If *fix_imports* is True and protocol is less than 3, pickle will try
4167to map the new Python 3 names to the old module names used in Python
41682, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004169[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004171static int
Larry Hastings89964c42015-04-14 18:07:59 -04004172_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4173 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004174/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004175{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004176 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004177 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004178
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004179 /* In case of multiple __init__() calls, clear previous content. */
4180 if (self->write != NULL)
4181 (void)Pickler_clear(self);
4182
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004183 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004184 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004185
4186 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004187 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004188
4189 /* memo and output_buffer may have already been created in _Pickler_New */
4190 if (self->memo == NULL) {
4191 self->memo = PyMemoTable_New();
4192 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004193 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004194 }
4195 self->output_len = 0;
4196 if (self->output_buffer == NULL) {
4197 self->max_output_len = WRITE_BUF_SIZE;
4198 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4199 self->max_output_len);
4200 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004201 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004202 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004203
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004204 self->fast = 0;
4205 self->fast_nesting = 0;
4206 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004207 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004208 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4209 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4210 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004211 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004212 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004213 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004214 self->dispatch_table = NULL;
4215 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4216 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4217 &PyId_dispatch_table);
4218 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004219 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004220 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004221
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004222 return 0;
4223}
4224
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004225
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004226/* Define a proxy object for the Pickler's internal memo object. This is to
4227 * avoid breaking code like:
4228 * pickler.memo.clear()
4229 * and
4230 * pickler.memo = saved_memo
4231 * Is this a good idea? Not really, but we don't want to break code that uses
4232 * it. Note that we don't implement the entire mapping API here. This is
4233 * intentional, as these should be treated as black-box implementation details.
4234 */
4235
Larry Hastings61272b72014-01-07 12:41:53 -08004236/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004237_pickle.PicklerMemoProxy.clear
4238
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004239Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004240[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004241
Larry Hastings3cceb382014-01-04 11:09:09 -08004242static PyObject *
4243_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004244/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004245{
4246 if (self->pickler->memo)
4247 PyMemoTable_Clear(self->pickler->memo);
4248 Py_RETURN_NONE;
4249}
4250
Larry Hastings61272b72014-01-07 12:41:53 -08004251/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004252_pickle.PicklerMemoProxy.copy
4253
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004254Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004255[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004256
Larry Hastings3cceb382014-01-04 11:09:09 -08004257static PyObject *
4258_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004259/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004260{
4261 Py_ssize_t i;
4262 PyMemoTable *memo;
4263 PyObject *new_memo = PyDict_New();
4264 if (new_memo == NULL)
4265 return NULL;
4266
4267 memo = self->pickler->memo;
4268 for (i = 0; i < memo->mt_allocated; ++i) {
4269 PyMemoEntry entry = memo->mt_table[i];
4270 if (entry.me_key != NULL) {
4271 int status;
4272 PyObject *key, *value;
4273
4274 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004275 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004276
4277 if (key == NULL || value == NULL) {
4278 Py_XDECREF(key);
4279 Py_XDECREF(value);
4280 goto error;
4281 }
4282 status = PyDict_SetItem(new_memo, key, value);
4283 Py_DECREF(key);
4284 Py_DECREF(value);
4285 if (status < 0)
4286 goto error;
4287 }
4288 }
4289 return new_memo;
4290
4291 error:
4292 Py_XDECREF(new_memo);
4293 return NULL;
4294}
4295
Larry Hastings61272b72014-01-07 12:41:53 -08004296/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004297_pickle.PicklerMemoProxy.__reduce__
4298
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004299Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004300[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004301
Larry Hastings3cceb382014-01-04 11:09:09 -08004302static PyObject *
4303_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004304/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004305{
4306 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004307 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004308 if (contents == NULL)
4309 return NULL;
4310
4311 reduce_value = PyTuple_New(2);
4312 if (reduce_value == NULL) {
4313 Py_DECREF(contents);
4314 return NULL;
4315 }
4316 dict_args = PyTuple_New(1);
4317 if (dict_args == NULL) {
4318 Py_DECREF(contents);
4319 Py_DECREF(reduce_value);
4320 return NULL;
4321 }
4322 PyTuple_SET_ITEM(dict_args, 0, contents);
4323 Py_INCREF((PyObject *)&PyDict_Type);
4324 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4325 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4326 return reduce_value;
4327}
4328
4329static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004330 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4331 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4332 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004333 {NULL, NULL} /* sentinel */
4334};
4335
4336static void
4337PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4338{
4339 PyObject_GC_UnTrack(self);
4340 Py_XDECREF(self->pickler);
4341 PyObject_GC_Del((PyObject *)self);
4342}
4343
4344static int
4345PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4346 visitproc visit, void *arg)
4347{
4348 Py_VISIT(self->pickler);
4349 return 0;
4350}
4351
4352static int
4353PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4354{
4355 Py_CLEAR(self->pickler);
4356 return 0;
4357}
4358
4359static PyTypeObject PicklerMemoProxyType = {
4360 PyVarObject_HEAD_INIT(NULL, 0)
4361 "_pickle.PicklerMemoProxy", /*tp_name*/
4362 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4363 0,
4364 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4365 0, /* tp_print */
4366 0, /* tp_getattr */
4367 0, /* tp_setattr */
4368 0, /* tp_compare */
4369 0, /* tp_repr */
4370 0, /* tp_as_number */
4371 0, /* tp_as_sequence */
4372 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004373 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004374 0, /* tp_call */
4375 0, /* tp_str */
4376 PyObject_GenericGetAttr, /* tp_getattro */
4377 PyObject_GenericSetAttr, /* tp_setattro */
4378 0, /* tp_as_buffer */
4379 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4380 0, /* tp_doc */
4381 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4382 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4383 0, /* tp_richcompare */
4384 0, /* tp_weaklistoffset */
4385 0, /* tp_iter */
4386 0, /* tp_iternext */
4387 picklerproxy_methods, /* tp_methods */
4388};
4389
4390static PyObject *
4391PicklerMemoProxy_New(PicklerObject *pickler)
4392{
4393 PicklerMemoProxyObject *self;
4394
4395 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4396 if (self == NULL)
4397 return NULL;
4398 Py_INCREF(pickler);
4399 self->pickler = pickler;
4400 PyObject_GC_Track(self);
4401 return (PyObject *)self;
4402}
4403
4404/*****************************************************************************/
4405
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004406static PyObject *
4407Pickler_get_memo(PicklerObject *self)
4408{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004409 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004410}
4411
4412static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004413Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004414{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004415 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004416
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004417 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004418 PyErr_SetString(PyExc_TypeError,
4419 "attribute deletion is not supported");
4420 return -1;
4421 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004422
4423 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4424 PicklerObject *pickler =
4425 ((PicklerMemoProxyObject *)obj)->pickler;
4426
4427 new_memo = PyMemoTable_Copy(pickler->memo);
4428 if (new_memo == NULL)
4429 return -1;
4430 }
4431 else if (PyDict_Check(obj)) {
4432 Py_ssize_t i = 0;
4433 PyObject *key, *value;
4434
4435 new_memo = PyMemoTable_New();
4436 if (new_memo == NULL)
4437 return -1;
4438
4439 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004440 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004441 PyObject *memo_obj;
4442
4443 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4444 PyErr_SetString(PyExc_TypeError,
4445 "'memo' values must be 2-item tuples");
4446 goto error;
4447 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004448 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004449 if (memo_id == -1 && PyErr_Occurred())
4450 goto error;
4451 memo_obj = PyTuple_GET_ITEM(value, 1);
4452 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4453 goto error;
4454 }
4455 }
4456 else {
4457 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004458 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004459 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004460 return -1;
4461 }
4462
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004463 PyMemoTable_Del(self->memo);
4464 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004465
4466 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004467
4468 error:
4469 if (new_memo)
4470 PyMemoTable_Del(new_memo);
4471 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004472}
4473
4474static PyObject *
4475Pickler_get_persid(PicklerObject *self)
4476{
4477 if (self->pers_func == NULL)
4478 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4479 else
4480 Py_INCREF(self->pers_func);
4481 return self->pers_func;
4482}
4483
4484static int
4485Pickler_set_persid(PicklerObject *self, PyObject *value)
4486{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004487 if (value == NULL) {
4488 PyErr_SetString(PyExc_TypeError,
4489 "attribute deletion is not supported");
4490 return -1;
4491 }
4492 if (!PyCallable_Check(value)) {
4493 PyErr_SetString(PyExc_TypeError,
4494 "persistent_id must be a callable taking one argument");
4495 return -1;
4496 }
4497
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004498 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004499 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004500
4501 return 0;
4502}
4503
4504static PyMemberDef Pickler_members[] = {
4505 {"bin", T_INT, offsetof(PicklerObject, bin)},
4506 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004507 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004508 {NULL}
4509};
4510
4511static PyGetSetDef Pickler_getsets[] = {
4512 {"memo", (getter)Pickler_get_memo,
4513 (setter)Pickler_set_memo},
4514 {"persistent_id", (getter)Pickler_get_persid,
4515 (setter)Pickler_set_persid},
4516 {NULL}
4517};
4518
4519static PyTypeObject Pickler_Type = {
4520 PyVarObject_HEAD_INIT(NULL, 0)
4521 "_pickle.Pickler" , /*tp_name*/
4522 sizeof(PicklerObject), /*tp_basicsize*/
4523 0, /*tp_itemsize*/
4524 (destructor)Pickler_dealloc, /*tp_dealloc*/
4525 0, /*tp_print*/
4526 0, /*tp_getattr*/
4527 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004528 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004529 0, /*tp_repr*/
4530 0, /*tp_as_number*/
4531 0, /*tp_as_sequence*/
4532 0, /*tp_as_mapping*/
4533 0, /*tp_hash*/
4534 0, /*tp_call*/
4535 0, /*tp_str*/
4536 0, /*tp_getattro*/
4537 0, /*tp_setattro*/
4538 0, /*tp_as_buffer*/
4539 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004540 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004541 (traverseproc)Pickler_traverse, /*tp_traverse*/
4542 (inquiry)Pickler_clear, /*tp_clear*/
4543 0, /*tp_richcompare*/
4544 0, /*tp_weaklistoffset*/
4545 0, /*tp_iter*/
4546 0, /*tp_iternext*/
4547 Pickler_methods, /*tp_methods*/
4548 Pickler_members, /*tp_members*/
4549 Pickler_getsets, /*tp_getset*/
4550 0, /*tp_base*/
4551 0, /*tp_dict*/
4552 0, /*tp_descr_get*/
4553 0, /*tp_descr_set*/
4554 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004555 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004556 PyType_GenericAlloc, /*tp_alloc*/
4557 PyType_GenericNew, /*tp_new*/
4558 PyObject_GC_Del, /*tp_free*/
4559 0, /*tp_is_gc*/
4560};
4561
Victor Stinner121aab42011-09-29 23:40:53 +02004562/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004563
4564 XXX: It would be nice to able to avoid Python function call overhead, by
4565 using directly the C version of find_class(), when find_class() is not
4566 overridden by a subclass. Although, this could become rather hackish. A
4567 simpler optimization would be to call the C function when self is not a
4568 subclass instance. */
4569static PyObject *
4570find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4571{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004572 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004573
4574 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4575 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004576}
4577
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004578static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004579marker(UnpicklerObject *self)
4580{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004581 Py_ssize_t mark;
4582
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004583 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004584 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004585 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586 return -1;
4587 }
4588
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004589 mark = self->marks[--self->num_marks];
4590 self->stack->mark_set = self->num_marks != 0;
4591 self->stack->fence = self->num_marks ?
4592 self->marks[self->num_marks - 1] : 0;
4593 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004594}
4595
4596static int
4597load_none(UnpicklerObject *self)
4598{
4599 PDATA_APPEND(self->stack, Py_None, -1);
4600 return 0;
4601}
4602
4603static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004604load_int(UnpicklerObject *self)
4605{
4606 PyObject *value;
4607 char *endptr, *s;
4608 Py_ssize_t len;
4609 long x;
4610
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004611 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004612 return -1;
4613 if (len < 2)
4614 return bad_readline();
4615
4616 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004617 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004618 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004619 x = strtol(s, &endptr, 0);
4620
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004621 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004622 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004623 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004624 errno = 0;
4625 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004626 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004627 if (value == NULL) {
4628 PyErr_SetString(PyExc_ValueError,
4629 "could not convert string to int");
4630 return -1;
4631 }
4632 }
4633 else {
4634 if (len == 3 && (x == 0 || x == 1)) {
4635 if ((value = PyBool_FromLong(x)) == NULL)
4636 return -1;
4637 }
4638 else {
4639 if ((value = PyLong_FromLong(x)) == NULL)
4640 return -1;
4641 }
4642 }
4643
4644 PDATA_PUSH(self->stack, value, -1);
4645 return 0;
4646}
4647
4648static int
4649load_bool(UnpicklerObject *self, PyObject *boolean)
4650{
4651 assert(boolean == Py_True || boolean == Py_False);
4652 PDATA_APPEND(self->stack, boolean, -1);
4653 return 0;
4654}
4655
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004656/* s contains x bytes of an unsigned little-endian integer. Return its value
4657 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4658 */
4659static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004660calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004661{
4662 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004663 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004664 size_t x = 0;
4665
Serhiy Storchakae0606192015-09-29 22:10:07 +03004666 if (nbytes > (int)sizeof(size_t)) {
4667 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4668 * have 64-bit size that can't be represented on 32-bit platform.
4669 */
4670 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4671 if (s[i])
4672 return -1;
4673 }
4674 nbytes = (int)sizeof(size_t);
4675 }
4676 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004677 x |= (size_t) s[i] << (8 * i);
4678 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004679
4680 if (x > PY_SSIZE_T_MAX)
4681 return -1;
4682 else
4683 return (Py_ssize_t) x;
4684}
4685
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004686/* s contains x bytes of a little-endian integer. Return its value as a
4687 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004688 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004689 * of x-platform bugs.
4690 */
4691static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004692calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004693{
4694 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004695 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004696 long x = 0;
4697
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004698 for (i = 0; i < nbytes; i++) {
4699 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004700 }
4701
4702 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4703 * is signed, so on a box with longs bigger than 4 bytes we need
4704 * to extend a BININT's sign bit to the full width.
4705 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004706 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004707 x |= -(x & (1L << 31));
4708 }
4709
4710 return x;
4711}
4712
4713static int
4714load_binintx(UnpicklerObject *self, char *s, int size)
4715{
4716 PyObject *value;
4717 long x;
4718
4719 x = calc_binint(s, size);
4720
4721 if ((value = PyLong_FromLong(x)) == NULL)
4722 return -1;
4723
4724 PDATA_PUSH(self->stack, value, -1);
4725 return 0;
4726}
4727
4728static int
4729load_binint(UnpicklerObject *self)
4730{
4731 char *s;
4732
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004733 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004734 return -1;
4735
4736 return load_binintx(self, s, 4);
4737}
4738
4739static int
4740load_binint1(UnpicklerObject *self)
4741{
4742 char *s;
4743
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004744 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004745 return -1;
4746
4747 return load_binintx(self, s, 1);
4748}
4749
4750static int
4751load_binint2(UnpicklerObject *self)
4752{
4753 char *s;
4754
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004755 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004756 return -1;
4757
4758 return load_binintx(self, s, 2);
4759}
4760
4761static int
4762load_long(UnpicklerObject *self)
4763{
4764 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004765 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004766 Py_ssize_t len;
4767
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004768 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004769 return -1;
4770 if (len < 2)
4771 return bad_readline();
4772
Mark Dickinson8dd05142009-01-20 20:43:58 +00004773 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4774 the 'L' before calling PyLong_FromString. In order to maintain
4775 compatibility with Python 3.0.0, we don't actually *require*
4776 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004777 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004778 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004779 /* XXX: Should the base argument explicitly set to 10? */
4780 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004781 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004782 return -1;
4783
4784 PDATA_PUSH(self->stack, value, -1);
4785 return 0;
4786}
4787
4788/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4789 * data following.
4790 */
4791static int
4792load_counted_long(UnpicklerObject *self, int size)
4793{
4794 PyObject *value;
4795 char *nbytes;
4796 char *pdata;
4797
4798 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004799 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004800 return -1;
4801
4802 size = calc_binint(nbytes, size);
4803 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004804 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004805 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004806 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004807 "LONG pickle has negative byte count");
4808 return -1;
4809 }
4810
4811 if (size == 0)
4812 value = PyLong_FromLong(0L);
4813 else {
4814 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004815 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004816 return -1;
4817 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4818 1 /* little endian */ , 1 /* signed */ );
4819 }
4820 if (value == NULL)
4821 return -1;
4822 PDATA_PUSH(self->stack, value, -1);
4823 return 0;
4824}
4825
4826static int
4827load_float(UnpicklerObject *self)
4828{
4829 PyObject *value;
4830 char *endptr, *s;
4831 Py_ssize_t len;
4832 double d;
4833
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004834 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004835 return -1;
4836 if (len < 2)
4837 return bad_readline();
4838
4839 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004840 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4841 if (d == -1.0 && PyErr_Occurred())
4842 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004843 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004844 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4845 return -1;
4846 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004847 value = PyFloat_FromDouble(d);
4848 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004849 return -1;
4850
4851 PDATA_PUSH(self->stack, value, -1);
4852 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004853}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004854
4855static int
4856load_binfloat(UnpicklerObject *self)
4857{
4858 PyObject *value;
4859 double x;
4860 char *s;
4861
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004862 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004863 return -1;
4864
4865 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4866 if (x == -1.0 && PyErr_Occurred())
4867 return -1;
4868
4869 if ((value = PyFloat_FromDouble(x)) == NULL)
4870 return -1;
4871
4872 PDATA_PUSH(self->stack, value, -1);
4873 return 0;
4874}
4875
4876static int
4877load_string(UnpicklerObject *self)
4878{
4879 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004880 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004881 Py_ssize_t len;
4882 char *s, *p;
4883
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004884 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004885 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004886 /* Strip the newline */
4887 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004888 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004889 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004890 p = s + 1;
4891 len -= 2;
4892 }
4893 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004894 PickleState *st = _Pickle_GetGlobalState();
4895 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004896 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004897 return -1;
4898 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004899 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004900
4901 /* Use the PyBytes API to decode the string, since that is what is used
4902 to encode, and then coerce the result to Unicode. */
4903 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004904 if (bytes == NULL)
4905 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004906
4907 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4908 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4909 if (strcmp(self->encoding, "bytes") == 0) {
4910 obj = bytes;
4911 }
4912 else {
4913 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4914 Py_DECREF(bytes);
4915 if (obj == NULL) {
4916 return -1;
4917 }
4918 }
4919
4920 PDATA_PUSH(self->stack, obj, -1);
4921 return 0;
4922}
4923
4924static int
4925load_counted_binstring(UnpicklerObject *self, int nbytes)
4926{
4927 PyObject *obj;
4928 Py_ssize_t size;
4929 char *s;
4930
4931 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004932 return -1;
4933
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004934 size = calc_binsize(s, nbytes);
4935 if (size < 0) {
4936 PickleState *st = _Pickle_GetGlobalState();
4937 PyErr_Format(st->UnpicklingError,
4938 "BINSTRING exceeds system's maximum size of %zd bytes",
4939 PY_SSIZE_T_MAX);
4940 return -1;
4941 }
4942
4943 if (_Unpickler_Read(self, &s, size) < 0)
4944 return -1;
4945
4946 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4947 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4948 if (strcmp(self->encoding, "bytes") == 0) {
4949 obj = PyBytes_FromStringAndSize(s, size);
4950 }
4951 else {
4952 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4953 }
4954 if (obj == NULL) {
4955 return -1;
4956 }
4957
4958 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004959 return 0;
4960}
4961
4962static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004963load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004964{
4965 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004966 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004967 char *s;
4968
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004969 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004970 return -1;
4971
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004972 size = calc_binsize(s, nbytes);
4973 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004974 PyErr_Format(PyExc_OverflowError,
4975 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004976 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004977 return -1;
4978 }
4979
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004980 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004981 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004982
4983 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004984 if (bytes == NULL)
4985 return -1;
4986
4987 PDATA_PUSH(self->stack, bytes, -1);
4988 return 0;
4989}
4990
4991static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004992load_unicode(UnpicklerObject *self)
4993{
4994 PyObject *str;
4995 Py_ssize_t len;
4996 char *s;
4997
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004998 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004999 return -1;
5000 if (len < 1)
5001 return bad_readline();
5002
5003 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5004 if (str == NULL)
5005 return -1;
5006
5007 PDATA_PUSH(self->stack, str, -1);
5008 return 0;
5009}
5010
5011static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005012load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005013{
5014 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005015 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005016 char *s;
5017
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005018 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005019 return -1;
5020
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005021 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005022 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005023 PyErr_Format(PyExc_OverflowError,
5024 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005025 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005026 return -1;
5027 }
5028
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005029 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005030 return -1;
5031
Victor Stinner485fb562010-04-13 11:07:24 +00005032 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005033 if (str == NULL)
5034 return -1;
5035
5036 PDATA_PUSH(self->stack, str, -1);
5037 return 0;
5038}
5039
5040static int
Victor Stinner21b47112016-03-14 18:09:39 +01005041load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042{
5043 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005045 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005046 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005047
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005048 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005049 if (tuple == NULL)
5050 return -1;
5051 PDATA_PUSH(self->stack, tuple, -1);
5052 return 0;
5053}
5054
5055static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005056load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005057{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005058 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005059
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005060 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005061 return -1;
5062
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005063 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005064}
5065
5066static int
5067load_empty_list(UnpicklerObject *self)
5068{
5069 PyObject *list;
5070
5071 if ((list = PyList_New(0)) == NULL)
5072 return -1;
5073 PDATA_PUSH(self->stack, list, -1);
5074 return 0;
5075}
5076
5077static int
5078load_empty_dict(UnpicklerObject *self)
5079{
5080 PyObject *dict;
5081
5082 if ((dict = PyDict_New()) == NULL)
5083 return -1;
5084 PDATA_PUSH(self->stack, dict, -1);
5085 return 0;
5086}
5087
5088static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005089load_empty_set(UnpicklerObject *self)
5090{
5091 PyObject *set;
5092
5093 if ((set = PySet_New(NULL)) == NULL)
5094 return -1;
5095 PDATA_PUSH(self->stack, set, -1);
5096 return 0;
5097}
5098
5099static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005100load_list(UnpicklerObject *self)
5101{
5102 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005103 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005104
5105 if ((i = marker(self)) < 0)
5106 return -1;
5107
5108 list = Pdata_poplist(self->stack, i);
5109 if (list == NULL)
5110 return -1;
5111 PDATA_PUSH(self->stack, list, -1);
5112 return 0;
5113}
5114
5115static int
5116load_dict(UnpicklerObject *self)
5117{
5118 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005119 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005120
5121 if ((i = marker(self)) < 0)
5122 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005123 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005124
5125 if ((dict = PyDict_New()) == NULL)
5126 return -1;
5127
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005128 if ((j - i) % 2 != 0) {
5129 PickleState *st = _Pickle_GetGlobalState();
5130 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005131 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005132 return -1;
5133 }
5134
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005135 for (k = i + 1; k < j; k += 2) {
5136 key = self->stack->data[k - 1];
5137 value = self->stack->data[k];
5138 if (PyDict_SetItem(dict, key, value) < 0) {
5139 Py_DECREF(dict);
5140 return -1;
5141 }
5142 }
5143 Pdata_clear(self->stack, i);
5144 PDATA_PUSH(self->stack, dict, -1);
5145 return 0;
5146}
5147
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005148static int
5149load_frozenset(UnpicklerObject *self)
5150{
5151 PyObject *items;
5152 PyObject *frozenset;
5153 Py_ssize_t i;
5154
5155 if ((i = marker(self)) < 0)
5156 return -1;
5157
5158 items = Pdata_poptuple(self->stack, i);
5159 if (items == NULL)
5160 return -1;
5161
5162 frozenset = PyFrozenSet_New(items);
5163 Py_DECREF(items);
5164 if (frozenset == NULL)
5165 return -1;
5166
5167 PDATA_PUSH(self->stack, frozenset, -1);
5168 return 0;
5169}
5170
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005171static PyObject *
5172instantiate(PyObject *cls, PyObject *args)
5173{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005174 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005175 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005176 /* Caller must assure args are a tuple. Normally, args come from
5177 Pdata_poptuple which packs objects from the top of the stack
5178 into a newly created tuple. */
5179 assert(PyTuple_Check(args));
5180 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005181 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005182 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005183 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005184 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005185 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005186
5187 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005188 }
5189 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005190}
5191
5192static int
5193load_obj(UnpicklerObject *self)
5194{
5195 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005196 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005197
5198 if ((i = marker(self)) < 0)
5199 return -1;
5200
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005201 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005202 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005203
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005204 args = Pdata_poptuple(self->stack, i + 1);
5205 if (args == NULL)
5206 return -1;
5207
5208 PDATA_POP(self->stack, cls);
5209 if (cls) {
5210 obj = instantiate(cls, args);
5211 Py_DECREF(cls);
5212 }
5213 Py_DECREF(args);
5214 if (obj == NULL)
5215 return -1;
5216
5217 PDATA_PUSH(self->stack, obj, -1);
5218 return 0;
5219}
5220
5221static int
5222load_inst(UnpicklerObject *self)
5223{
5224 PyObject *cls = NULL;
5225 PyObject *args = NULL;
5226 PyObject *obj = NULL;
5227 PyObject *module_name;
5228 PyObject *class_name;
5229 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005230 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005231 char *s;
5232
5233 if ((i = marker(self)) < 0)
5234 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005235 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005236 return -1;
5237 if (len < 2)
5238 return bad_readline();
5239
5240 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5241 identifiers are permitted in Python 3.0, since the INST opcode is only
5242 supported by older protocols on Python 2.x. */
5243 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5244 if (module_name == NULL)
5245 return -1;
5246
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005247 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005248 if (len < 2) {
5249 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005250 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005251 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005252 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005253 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005254 cls = find_class(self, module_name, class_name);
5255 Py_DECREF(class_name);
5256 }
5257 }
5258 Py_DECREF(module_name);
5259
5260 if (cls == NULL)
5261 return -1;
5262
5263 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5264 obj = instantiate(cls, args);
5265 Py_DECREF(args);
5266 }
5267 Py_DECREF(cls);
5268
5269 if (obj == NULL)
5270 return -1;
5271
5272 PDATA_PUSH(self->stack, obj, -1);
5273 return 0;
5274}
5275
5276static int
5277load_newobj(UnpicklerObject *self)
5278{
5279 PyObject *args = NULL;
5280 PyObject *clsraw = NULL;
5281 PyTypeObject *cls; /* clsraw cast to its true type */
5282 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005283 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005284
5285 /* Stack is ... cls argtuple, and we want to call
5286 * cls.__new__(cls, *argtuple).
5287 */
5288 PDATA_POP(self->stack, args);
5289 if (args == NULL)
5290 goto error;
5291 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005292 PyErr_SetString(st->UnpicklingError,
5293 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005294 goto error;
5295 }
5296
5297 PDATA_POP(self->stack, clsraw);
5298 cls = (PyTypeObject *)clsraw;
5299 if (cls == NULL)
5300 goto error;
5301 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005302 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005303 "isn't a type object");
5304 goto error;
5305 }
5306 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005307 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005308 "has NULL tp_new");
5309 goto error;
5310 }
5311
5312 /* Call __new__. */
5313 obj = cls->tp_new(cls, args, NULL);
5314 if (obj == NULL)
5315 goto error;
5316
5317 Py_DECREF(args);
5318 Py_DECREF(clsraw);
5319 PDATA_PUSH(self->stack, obj, -1);
5320 return 0;
5321
5322 error:
5323 Py_XDECREF(args);
5324 Py_XDECREF(clsraw);
5325 return -1;
5326}
5327
5328static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005329load_newobj_ex(UnpicklerObject *self)
5330{
5331 PyObject *cls, *args, *kwargs;
5332 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005333 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005334
5335 PDATA_POP(self->stack, kwargs);
5336 if (kwargs == NULL) {
5337 return -1;
5338 }
5339 PDATA_POP(self->stack, args);
5340 if (args == NULL) {
5341 Py_DECREF(kwargs);
5342 return -1;
5343 }
5344 PDATA_POP(self->stack, cls);
5345 if (cls == NULL) {
5346 Py_DECREF(kwargs);
5347 Py_DECREF(args);
5348 return -1;
5349 }
Larry Hastings61272b72014-01-07 12:41:53 -08005350
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005351 if (!PyType_Check(cls)) {
5352 Py_DECREF(kwargs);
5353 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005354 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005355 "NEWOBJ_EX class argument must be a type, not %.200s",
5356 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005357 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005358 return -1;
5359 }
5360
5361 if (((PyTypeObject *)cls)->tp_new == NULL) {
5362 Py_DECREF(kwargs);
5363 Py_DECREF(args);
5364 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005365 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005366 "NEWOBJ_EX class argument doesn't have __new__");
5367 return -1;
5368 }
5369 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5370 Py_DECREF(kwargs);
5371 Py_DECREF(args);
5372 Py_DECREF(cls);
5373 if (obj == NULL) {
5374 return -1;
5375 }
5376 PDATA_PUSH(self->stack, obj, -1);
5377 return 0;
5378}
5379
5380static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005381load_global(UnpicklerObject *self)
5382{
5383 PyObject *global = NULL;
5384 PyObject *module_name;
5385 PyObject *global_name;
5386 Py_ssize_t len;
5387 char *s;
5388
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005389 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005390 return -1;
5391 if (len < 2)
5392 return bad_readline();
5393 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5394 if (!module_name)
5395 return -1;
5396
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005397 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005398 if (len < 2) {
5399 Py_DECREF(module_name);
5400 return bad_readline();
5401 }
5402 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5403 if (global_name) {
5404 global = find_class(self, module_name, global_name);
5405 Py_DECREF(global_name);
5406 }
5407 }
5408 Py_DECREF(module_name);
5409
5410 if (global == NULL)
5411 return -1;
5412 PDATA_PUSH(self->stack, global, -1);
5413 return 0;
5414}
5415
5416static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005417load_stack_global(UnpicklerObject *self)
5418{
5419 PyObject *global;
5420 PyObject *module_name;
5421 PyObject *global_name;
5422
5423 PDATA_POP(self->stack, global_name);
5424 PDATA_POP(self->stack, module_name);
5425 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5426 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005427 PickleState *st = _Pickle_GetGlobalState();
5428 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005429 Py_XDECREF(global_name);
5430 Py_XDECREF(module_name);
5431 return -1;
5432 }
5433 global = find_class(self, module_name, global_name);
5434 Py_DECREF(global_name);
5435 Py_DECREF(module_name);
5436 if (global == NULL)
5437 return -1;
5438 PDATA_PUSH(self->stack, global, -1);
5439 return 0;
5440}
5441
5442static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005443load_persid(UnpicklerObject *self)
5444{
5445 PyObject *pid;
5446 Py_ssize_t len;
5447 char *s;
5448
5449 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005450 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005451 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005452 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005453 return bad_readline();
5454
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005455 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5456 if (pid == NULL) {
5457 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5458 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5459 "persistent IDs in protocol 0 must be "
5460 "ASCII strings");
5461 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005462 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005463 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005464
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005465 /* This does not leak since _Pickle_FastCall() steals the reference
5466 to pid first. */
5467 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005468 if (pid == NULL)
5469 return -1;
5470
5471 PDATA_PUSH(self->stack, pid, -1);
5472 return 0;
5473 }
5474 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005475 PickleState *st = _Pickle_GetGlobalState();
5476 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005477 "A load persistent id instruction was encountered,\n"
5478 "but no persistent_load function was specified.");
5479 return -1;
5480 }
5481}
5482
5483static int
5484load_binpersid(UnpicklerObject *self)
5485{
5486 PyObject *pid;
5487
5488 if (self->pers_func) {
5489 PDATA_POP(self->stack, pid);
5490 if (pid == NULL)
5491 return -1;
5492
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005493 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005494 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005495 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005496 if (pid == NULL)
5497 return -1;
5498
5499 PDATA_PUSH(self->stack, pid, -1);
5500 return 0;
5501 }
5502 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005503 PickleState *st = _Pickle_GetGlobalState();
5504 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005505 "A load persistent id instruction was encountered,\n"
5506 "but no persistent_load function was specified.");
5507 return -1;
5508 }
5509}
5510
5511static int
5512load_pop(UnpicklerObject *self)
5513{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005514 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005515
5516 /* Note that we split the (pickle.py) stack into two stacks,
5517 * an object stack and a mark stack. We have to be clever and
5518 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005519 * mark stack first, and only signalling a stack underflow if
5520 * the object stack is empty and the mark stack doesn't match
5521 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005522 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005523 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005524 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005525 self->stack->mark_set = self->num_marks != 0;
5526 self->stack->fence = self->num_marks ?
5527 self->marks[self->num_marks - 1] : 0;
5528 } else if (len <= self->stack->fence)
5529 return Pdata_stack_underflow(self->stack);
5530 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005531 len--;
5532 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005533 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005534 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005535 return 0;
5536}
5537
5538static int
5539load_pop_mark(UnpicklerObject *self)
5540{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005541 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005542
5543 if ((i = marker(self)) < 0)
5544 return -1;
5545
5546 Pdata_clear(self->stack, i);
5547
5548 return 0;
5549}
5550
5551static int
5552load_dup(UnpicklerObject *self)
5553{
5554 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005555 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005556
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005557 if (len <= self->stack->fence)
5558 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005559 last = self->stack->data[len - 1];
5560 PDATA_APPEND(self->stack, last, -1);
5561 return 0;
5562}
5563
5564static int
5565load_get(UnpicklerObject *self)
5566{
5567 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005568 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005569 Py_ssize_t len;
5570 char *s;
5571
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005572 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005573 return -1;
5574 if (len < 2)
5575 return bad_readline();
5576
5577 key = PyLong_FromString(s, NULL, 10);
5578 if (key == NULL)
5579 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005580 idx = PyLong_AsSsize_t(key);
5581 if (idx == -1 && PyErr_Occurred()) {
5582 Py_DECREF(key);
5583 return -1;
5584 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005585
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005586 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005587 if (value == NULL) {
5588 if (!PyErr_Occurred())
5589 PyErr_SetObject(PyExc_KeyError, key);
5590 Py_DECREF(key);
5591 return -1;
5592 }
5593 Py_DECREF(key);
5594
5595 PDATA_APPEND(self->stack, value, -1);
5596 return 0;
5597}
5598
5599static int
5600load_binget(UnpicklerObject *self)
5601{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005602 PyObject *value;
5603 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604 char *s;
5605
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005606 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005607 return -1;
5608
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005609 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005610
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005611 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005613 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005614 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005616 Py_DECREF(key);
5617 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005618 return -1;
5619 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005620
5621 PDATA_APPEND(self->stack, value, -1);
5622 return 0;
5623}
5624
5625static int
5626load_long_binget(UnpicklerObject *self)
5627{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005628 PyObject *value;
5629 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005631
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005632 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633 return -1;
5634
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005635 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005636
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005637 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005639 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005640 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005641 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005642 Py_DECREF(key);
5643 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644 return -1;
5645 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005646
5647 PDATA_APPEND(self->stack, value, -1);
5648 return 0;
5649}
5650
5651/* Push an object from the extension registry (EXT[124]). nbytes is
5652 * the number of bytes following the opcode, holding the index (code) value.
5653 */
5654static int
5655load_extension(UnpicklerObject *self, int nbytes)
5656{
5657 char *codebytes; /* the nbytes bytes after the opcode */
5658 long code; /* calc_binint returns long */
5659 PyObject *py_code; /* code as a Python int */
5660 PyObject *obj; /* the object to push */
5661 PyObject *pair; /* (module_name, class_name) */
5662 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005663 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005664
5665 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005666 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005667 return -1;
5668 code = calc_binint(codebytes, nbytes);
5669 if (code <= 0) { /* note that 0 is forbidden */
5670 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005671 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005672 return -1;
5673 }
5674
5675 /* Look for the code in the cache. */
5676 py_code = PyLong_FromLong(code);
5677 if (py_code == NULL)
5678 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005679 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005680 if (obj != NULL) {
5681 /* Bingo. */
5682 Py_DECREF(py_code);
5683 PDATA_APPEND(self->stack, obj, -1);
5684 return 0;
5685 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005686 if (PyErr_Occurred()) {
5687 Py_DECREF(py_code);
5688 return -1;
5689 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690
5691 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005692 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005693 if (pair == NULL) {
5694 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005695 if (!PyErr_Occurred()) {
5696 PyErr_Format(PyExc_ValueError, "unregistered extension "
5697 "code %ld", code);
5698 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005699 return -1;
5700 }
5701 /* Since the extension registry is manipulable via Python code,
5702 * confirm that pair is really a 2-tuple of strings.
5703 */
5704 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5705 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5706 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5707 Py_DECREF(py_code);
5708 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5709 "isn't a 2-tuple of strings", code);
5710 return -1;
5711 }
5712 /* Load the object. */
5713 obj = find_class(self, module_name, class_name);
5714 if (obj == NULL) {
5715 Py_DECREF(py_code);
5716 return -1;
5717 }
5718 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005719 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005720 Py_DECREF(py_code);
5721 if (code < 0) {
5722 Py_DECREF(obj);
5723 return -1;
5724 }
5725 PDATA_PUSH(self->stack, obj, -1);
5726 return 0;
5727}
5728
5729static int
5730load_put(UnpicklerObject *self)
5731{
5732 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005733 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005734 Py_ssize_t len;
5735 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005736
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005737 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005738 return -1;
5739 if (len < 2)
5740 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005741 if (Py_SIZE(self->stack) <= self->stack->fence)
5742 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005743 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005744
5745 key = PyLong_FromString(s, NULL, 10);
5746 if (key == NULL)
5747 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005748 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005749 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005750 if (idx < 0) {
5751 if (!PyErr_Occurred())
5752 PyErr_SetString(PyExc_ValueError,
5753 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005754 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005755 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005756
5757 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005758}
5759
5760static int
5761load_binput(UnpicklerObject *self)
5762{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005763 PyObject *value;
5764 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005767 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005768 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005769
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005770 if (Py_SIZE(self->stack) <= self->stack->fence)
5771 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005772 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005774 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005775
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005776 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005777}
5778
5779static int
5780load_long_binput(UnpicklerObject *self)
5781{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005782 PyObject *value;
5783 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005785
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005786 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005787 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005788
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005789 if (Py_SIZE(self->stack) <= self->stack->fence)
5790 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005791 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005792
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005793 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005794 if (idx < 0) {
5795 PyErr_SetString(PyExc_ValueError,
5796 "negative LONG_BINPUT argument");
5797 return -1;
5798 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005799
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005800 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005801}
5802
5803static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005804load_memoize(UnpicklerObject *self)
5805{
5806 PyObject *value;
5807
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005808 if (Py_SIZE(self->stack) <= self->stack->fence)
5809 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005810 value = self->stack->data[Py_SIZE(self->stack) - 1];
5811
5812 return _Unpickler_MemoPut(self, self->memo_len, value);
5813}
5814
5815static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005816do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817{
5818 PyObject *value;
5819 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005820 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005821
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005822 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005823 if (x > len || x <= self->stack->fence)
5824 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005825 if (len == x) /* nothing to do */
5826 return 0;
5827
5828 list = self->stack->data[x - 1];
5829
5830 if (PyList_Check(list)) {
5831 PyObject *slice;
5832 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005833 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005834
5835 slice = Pdata_poplist(self->stack, x);
5836 if (!slice)
5837 return -1;
5838 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005839 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005840 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005841 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005842 }
5843 else {
5844 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005845 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005847 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005848 if (append_func == NULL)
5849 return -1;
5850 for (i = x; i < len; i++) {
5851 PyObject *result;
5852
5853 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005854 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005855 if (result == NULL) {
5856 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005857 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005858 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005859 return -1;
5860 }
5861 Py_DECREF(result);
5862 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005863 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005864 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005865 }
5866
5867 return 0;
5868}
5869
5870static int
5871load_append(UnpicklerObject *self)
5872{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005873 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5874 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005875 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005876}
5877
5878static int
5879load_appends(UnpicklerObject *self)
5880{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005881 Py_ssize_t i = marker(self);
5882 if (i < 0)
5883 return -1;
5884 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005885}
5886
5887static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005888do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005889{
5890 PyObject *value, *key;
5891 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005892 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005893 int status = 0;
5894
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005895 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005896 if (x > len || x <= self->stack->fence)
5897 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005898 if (len == x) /* nothing to do */
5899 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005900 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005901 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005902 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005903 PyErr_SetString(st->UnpicklingError,
5904 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005905 return -1;
5906 }
5907
5908 /* Here, dict does not actually need to be a PyDict; it could be anything
5909 that supports the __setitem__ attribute. */
5910 dict = self->stack->data[x - 1];
5911
5912 for (i = x + 1; i < len; i += 2) {
5913 key = self->stack->data[i - 1];
5914 value = self->stack->data[i];
5915 if (PyObject_SetItem(dict, key, value) < 0) {
5916 status = -1;
5917 break;
5918 }
5919 }
5920
5921 Pdata_clear(self->stack, x);
5922 return status;
5923}
5924
5925static int
5926load_setitem(UnpicklerObject *self)
5927{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005928 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005929}
5930
5931static int
5932load_setitems(UnpicklerObject *self)
5933{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005934 Py_ssize_t i = marker(self);
5935 if (i < 0)
5936 return -1;
5937 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005938}
5939
5940static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005941load_additems(UnpicklerObject *self)
5942{
5943 PyObject *set;
5944 Py_ssize_t mark, len, i;
5945
5946 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005947 if (mark < 0)
5948 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005949 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005950 if (mark > len || mark <= self->stack->fence)
5951 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005952 if (len == mark) /* nothing to do */
5953 return 0;
5954
5955 set = self->stack->data[mark - 1];
5956
5957 if (PySet_Check(set)) {
5958 PyObject *items;
5959 int status;
5960
5961 items = Pdata_poptuple(self->stack, mark);
5962 if (items == NULL)
5963 return -1;
5964
5965 status = _PySet_Update(set, items);
5966 Py_DECREF(items);
5967 return status;
5968 }
5969 else {
5970 PyObject *add_func;
5971 _Py_IDENTIFIER(add);
5972
5973 add_func = _PyObject_GetAttrId(set, &PyId_add);
5974 if (add_func == NULL)
5975 return -1;
5976 for (i = mark; i < len; i++) {
5977 PyObject *result;
5978 PyObject *item;
5979
5980 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005981 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005982 if (result == NULL) {
5983 Pdata_clear(self->stack, i + 1);
5984 Py_SIZE(self->stack) = mark;
5985 return -1;
5986 }
5987 Py_DECREF(result);
5988 }
5989 Py_SIZE(self->stack) = mark;
5990 }
5991
5992 return 0;
5993}
5994
5995static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005996load_build(UnpicklerObject *self)
5997{
5998 PyObject *state, *inst, *slotstate;
5999 PyObject *setstate;
6000 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006001 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006002
6003 /* Stack is ... instance, state. We want to leave instance at
6004 * the stack top, possibly mutated via instance.__setstate__(state).
6005 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006006 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6007 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006008
6009 PDATA_POP(self->stack, state);
6010 if (state == NULL)
6011 return -1;
6012
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006013 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006014
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006015 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006016 if (setstate == NULL) {
6017 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6018 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006019 else {
6020 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006021 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006022 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006023 }
6024 else {
6025 PyObject *result;
6026
6027 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006028 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006029 Py_DECREF(setstate);
6030 if (result == NULL)
6031 return -1;
6032 Py_DECREF(result);
6033 return 0;
6034 }
6035
6036 /* A default __setstate__. First see whether state embeds a
6037 * slot state dict too (a proto 2 addition).
6038 */
6039 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6040 PyObject *tmp = state;
6041
6042 state = PyTuple_GET_ITEM(tmp, 0);
6043 slotstate = PyTuple_GET_ITEM(tmp, 1);
6044 Py_INCREF(state);
6045 Py_INCREF(slotstate);
6046 Py_DECREF(tmp);
6047 }
6048 else
6049 slotstate = NULL;
6050
6051 /* Set inst.__dict__ from the state dict (if any). */
6052 if (state != Py_None) {
6053 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006054 PyObject *d_key, *d_value;
6055 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006056 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006057
6058 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006059 PickleState *st = _Pickle_GetGlobalState();
6060 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006061 goto error;
6062 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006063 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006064 if (dict == NULL)
6065 goto error;
6066
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006067 i = 0;
6068 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6069 /* normally the keys for instance attributes are
6070 interned. we should try to do that here. */
6071 Py_INCREF(d_key);
6072 if (PyUnicode_CheckExact(d_key))
6073 PyUnicode_InternInPlace(&d_key);
6074 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6075 Py_DECREF(d_key);
6076 goto error;
6077 }
6078 Py_DECREF(d_key);
6079 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006080 Py_DECREF(dict);
6081 }
6082
6083 /* Also set instance attributes from the slotstate dict (if any). */
6084 if (slotstate != NULL) {
6085 PyObject *d_key, *d_value;
6086 Py_ssize_t i;
6087
6088 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006089 PickleState *st = _Pickle_GetGlobalState();
6090 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006091 "slot state is not a dictionary");
6092 goto error;
6093 }
6094 i = 0;
6095 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6096 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6097 goto error;
6098 }
6099 }
6100
6101 if (0) {
6102 error:
6103 status = -1;
6104 }
6105
6106 Py_DECREF(state);
6107 Py_XDECREF(slotstate);
6108 return status;
6109}
6110
6111static int
6112load_mark(UnpicklerObject *self)
6113{
6114
6115 /* Note that we split the (pickle.py) stack into two stacks, an
6116 * object stack and a mark stack. Here we push a mark onto the
6117 * mark stack.
6118 */
6119
6120 if ((self->num_marks + 1) >= self->marks_size) {
6121 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006122
6123 /* Use the size_t type to check for overflow. */
6124 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006125 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006126 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006127 PyErr_NoMemory();
6128 return -1;
6129 }
6130
6131 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006132 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006133 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006134 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6135 if (self->marks == NULL) {
6136 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006137 PyErr_NoMemory();
6138 return -1;
6139 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006140 self->marks_size = (Py_ssize_t)alloc;
6141 }
6142
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006143 self->stack->mark_set = 1;
6144 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006145
6146 return 0;
6147}
6148
6149static int
6150load_reduce(UnpicklerObject *self)
6151{
6152 PyObject *callable = NULL;
6153 PyObject *argtup = NULL;
6154 PyObject *obj = NULL;
6155
6156 PDATA_POP(self->stack, argtup);
6157 if (argtup == NULL)
6158 return -1;
6159 PDATA_POP(self->stack, callable);
6160 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006161 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006162 Py_DECREF(callable);
6163 }
6164 Py_DECREF(argtup);
6165
6166 if (obj == NULL)
6167 return -1;
6168
6169 PDATA_PUSH(self->stack, obj, -1);
6170 return 0;
6171}
6172
6173/* Just raises an error if we don't know the protocol specified. PROTO
6174 * is the first opcode for protocols >= 2.
6175 */
6176static int
6177load_proto(UnpicklerObject *self)
6178{
6179 char *s;
6180 int i;
6181
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006182 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006183 return -1;
6184
6185 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006186 if (i <= HIGHEST_PROTOCOL) {
6187 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006188 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006189 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006190
6191 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6192 return -1;
6193}
6194
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006195static int
6196load_frame(UnpicklerObject *self)
6197{
6198 char *s;
6199 Py_ssize_t frame_len;
6200
6201 if (_Unpickler_Read(self, &s, 8) < 0)
6202 return -1;
6203
6204 frame_len = calc_binsize(s, 8);
6205 if (frame_len < 0) {
6206 PyErr_Format(PyExc_OverflowError,
6207 "FRAME length exceeds system's maximum of %zd bytes",
6208 PY_SSIZE_T_MAX);
6209 return -1;
6210 }
6211
6212 if (_Unpickler_Read(self, &s, frame_len) < 0)
6213 return -1;
6214
6215 /* Rewind to start of frame */
6216 self->next_read_idx -= frame_len;
6217 return 0;
6218}
6219
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006220static PyObject *
6221load(UnpicklerObject *self)
6222{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006223 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006224 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006225
6226 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006227 self->stack->mark_set = 0;
6228 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006229 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006230 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006231 Pdata_clear(self->stack, 0);
6232
6233 /* Convenient macros for the dispatch while-switch loop just below. */
6234#define OP(opcode, load_func) \
6235 case opcode: if (load_func(self) < 0) break; continue;
6236
6237#define OP_ARG(opcode, load_func, arg) \
6238 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6239
6240 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006241 if (_Unpickler_Read(self, &s, 1) < 0) {
6242 PickleState *st = _Pickle_GetGlobalState();
6243 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6244 PyErr_Format(PyExc_EOFError, "Ran out of input");
6245 }
6246 return NULL;
6247 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006248
6249 switch ((enum opcode)s[0]) {
6250 OP(NONE, load_none)
6251 OP(BININT, load_binint)
6252 OP(BININT1, load_binint1)
6253 OP(BININT2, load_binint2)
6254 OP(INT, load_int)
6255 OP(LONG, load_long)
6256 OP_ARG(LONG1, load_counted_long, 1)
6257 OP_ARG(LONG4, load_counted_long, 4)
6258 OP(FLOAT, load_float)
6259 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006260 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6261 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6262 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6263 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6264 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006265 OP(STRING, load_string)
6266 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006267 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6268 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6269 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006270 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6271 OP_ARG(TUPLE1, load_counted_tuple, 1)
6272 OP_ARG(TUPLE2, load_counted_tuple, 2)
6273 OP_ARG(TUPLE3, load_counted_tuple, 3)
6274 OP(TUPLE, load_tuple)
6275 OP(EMPTY_LIST, load_empty_list)
6276 OP(LIST, load_list)
6277 OP(EMPTY_DICT, load_empty_dict)
6278 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006279 OP(EMPTY_SET, load_empty_set)
6280 OP(ADDITEMS, load_additems)
6281 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006282 OP(OBJ, load_obj)
6283 OP(INST, load_inst)
6284 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006285 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006286 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006287 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006288 OP(APPEND, load_append)
6289 OP(APPENDS, load_appends)
6290 OP(BUILD, load_build)
6291 OP(DUP, load_dup)
6292 OP(BINGET, load_binget)
6293 OP(LONG_BINGET, load_long_binget)
6294 OP(GET, load_get)
6295 OP(MARK, load_mark)
6296 OP(BINPUT, load_binput)
6297 OP(LONG_BINPUT, load_long_binput)
6298 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006299 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006300 OP(POP, load_pop)
6301 OP(POP_MARK, load_pop_mark)
6302 OP(SETITEM, load_setitem)
6303 OP(SETITEMS, load_setitems)
6304 OP(PERSID, load_persid)
6305 OP(BINPERSID, load_binpersid)
6306 OP(REDUCE, load_reduce)
6307 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006308 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006309 OP_ARG(EXT1, load_extension, 1)
6310 OP_ARG(EXT2, load_extension, 2)
6311 OP_ARG(EXT4, load_extension, 4)
6312 OP_ARG(NEWTRUE, load_bool, Py_True)
6313 OP_ARG(NEWFALSE, load_bool, Py_False)
6314
6315 case STOP:
6316 break;
6317
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006318 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006319 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006320 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006321 unsigned char c = (unsigned char) *s;
6322 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6323 PyErr_Format(st->UnpicklingError,
6324 "invalid load key, '%c'.", c);
6325 }
6326 else {
6327 PyErr_Format(st->UnpicklingError,
6328 "invalid load key, '\\x%02x'.", c);
6329 }
6330 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006331 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006332 }
6333
6334 break; /* and we are done! */
6335 }
6336
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006337 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006338 return NULL;
6339 }
6340
Victor Stinner2ae57e32013-10-31 13:39:23 +01006341 if (_Unpickler_SkipConsumed(self) < 0)
6342 return NULL;
6343
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006344 PDATA_POP(self->stack, value);
6345 return value;
6346}
6347
Larry Hastings61272b72014-01-07 12:41:53 -08006348/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006349
6350_pickle.Unpickler.load
6351
6352Load a pickle.
6353
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006354Read a pickled object representation from the open file object given
6355in the constructor, and return the reconstituted object hierarchy
6356specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006357[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006358
Larry Hastings3cceb382014-01-04 11:09:09 -08006359static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006360_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006361/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006362{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006363 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006364
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006365 /* Check whether the Unpickler was initialized correctly. This prevents
6366 segfaulting if a subclass overridden __init__ with a function that does
6367 not call Unpickler.__init__(). Here, we simply ensure that self->read
6368 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006369 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006370 PickleState *st = _Pickle_GetGlobalState();
6371 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006372 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006373 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006374 return NULL;
6375 }
6376
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006377 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006378}
6379
6380/* The name of find_class() is misleading. In newer pickle protocols, this
6381 function is used for loading any global (i.e., functions), not just
6382 classes. The name is kept only for backward compatibility. */
6383
Larry Hastings61272b72014-01-07 12:41:53 -08006384/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006385
6386_pickle.Unpickler.find_class
6387
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006388 module_name: object
6389 global_name: object
6390 /
6391
6392Return an object from a specified module.
6393
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006394If necessary, the module will be imported. Subclasses may override
6395this method (e.g. to restrict unpickling of arbitrary classes and
6396functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006397
6398This method is called whenever a class or a function object is
6399needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006400[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006401
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006402static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006403_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6404 PyObject *module_name,
6405 PyObject *global_name)
6406/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006407{
6408 PyObject *global;
6409 PyObject *modules_dict;
6410 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006411 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006412
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006413 /* Try to map the old names used in Python 2.x to the new ones used in
6414 Python 3.x. We do this only with old pickle protocols and when the
6415 user has not disabled the feature. */
6416 if (self->proto < 3 && self->fix_imports) {
6417 PyObject *key;
6418 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006419 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006420
6421 /* Check if the global (i.e., a function or a class) was renamed
6422 or moved to another module. */
6423 key = PyTuple_Pack(2, module_name, global_name);
6424 if (key == NULL)
6425 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006426 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006427 Py_DECREF(key);
6428 if (item) {
6429 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6430 PyErr_Format(PyExc_RuntimeError,
6431 "_compat_pickle.NAME_MAPPING values should be "
6432 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6433 return NULL;
6434 }
6435 module_name = PyTuple_GET_ITEM(item, 0);
6436 global_name = PyTuple_GET_ITEM(item, 1);
6437 if (!PyUnicode_Check(module_name) ||
6438 !PyUnicode_Check(global_name)) {
6439 PyErr_Format(PyExc_RuntimeError,
6440 "_compat_pickle.NAME_MAPPING values should be "
6441 "pairs of str, not (%.200s, %.200s)",
6442 Py_TYPE(module_name)->tp_name,
6443 Py_TYPE(global_name)->tp_name);
6444 return NULL;
6445 }
6446 }
6447 else if (PyErr_Occurred()) {
6448 return NULL;
6449 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006450 else {
6451 /* Check if the module was renamed. */
6452 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6453 if (item) {
6454 if (!PyUnicode_Check(item)) {
6455 PyErr_Format(PyExc_RuntimeError,
6456 "_compat_pickle.IMPORT_MAPPING values should be "
6457 "strings, not %.200s", Py_TYPE(item)->tp_name);
6458 return NULL;
6459 }
6460 module_name = item;
6461 }
6462 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006463 return NULL;
6464 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006465 }
6466 }
6467
Victor Stinnerbb520202013-11-06 22:40:41 +01006468 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006469 if (modules_dict == NULL) {
6470 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006471 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006472 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006473
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006474 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006475 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006476 if (PyErr_Occurred())
6477 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006478 module = PyImport_Import(module_name);
6479 if (module == NULL)
6480 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006481 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482 Py_DECREF(module);
6483 }
Victor Stinner121aab42011-09-29 23:40:53 +02006484 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006485 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006486 }
6487 return global;
6488}
6489
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006490/*[clinic input]
6491
6492_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6493
6494Returns size in memory, in bytes.
6495[clinic start generated code]*/
6496
6497static Py_ssize_t
6498_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6499/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6500{
6501 Py_ssize_t res;
6502
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006503 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006504 if (self->memo != NULL)
6505 res += self->memo_size * sizeof(PyObject *);
6506 if (self->marks != NULL)
6507 res += self->marks_size * sizeof(Py_ssize_t);
6508 if (self->input_line != NULL)
6509 res += strlen(self->input_line) + 1;
6510 if (self->encoding != NULL)
6511 res += strlen(self->encoding) + 1;
6512 if (self->errors != NULL)
6513 res += strlen(self->errors) + 1;
6514 return res;
6515}
6516
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006517static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006518 _PICKLE_UNPICKLER_LOAD_METHODDEF
6519 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006520 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006521 {NULL, NULL} /* sentinel */
6522};
6523
6524static void
6525Unpickler_dealloc(UnpicklerObject *self)
6526{
6527 PyObject_GC_UnTrack((PyObject *)self);
6528 Py_XDECREF(self->readline);
6529 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006530 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006531 Py_XDECREF(self->stack);
6532 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006533 if (self->buffer.buf != NULL) {
6534 PyBuffer_Release(&self->buffer);
6535 self->buffer.buf = NULL;
6536 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006537
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006538 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006540 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006541 PyMem_Free(self->encoding);
6542 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006543
6544 Py_TYPE(self)->tp_free((PyObject *)self);
6545}
6546
6547static int
6548Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6549{
6550 Py_VISIT(self->readline);
6551 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006552 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006553 Py_VISIT(self->stack);
6554 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006555 return 0;
6556}
6557
6558static int
6559Unpickler_clear(UnpicklerObject *self)
6560{
6561 Py_CLEAR(self->readline);
6562 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006563 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006564 Py_CLEAR(self->stack);
6565 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006566 if (self->buffer.buf != NULL) {
6567 PyBuffer_Release(&self->buffer);
6568 self->buffer.buf = NULL;
6569 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006570
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006571 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006572 PyMem_Free(self->marks);
6573 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006574 PyMem_Free(self->input_line);
6575 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006576 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006577 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006578 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006579 self->errors = NULL;
6580
6581 return 0;
6582}
6583
Larry Hastings61272b72014-01-07 12:41:53 -08006584/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006585
6586_pickle.Unpickler.__init__
6587
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006588 file: object
6589 *
6590 fix_imports: bool = True
6591 encoding: str = 'ASCII'
6592 errors: str = 'strict'
6593
6594This takes a binary file for reading a pickle data stream.
6595
6596The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006597protocol argument is needed. Bytes past the pickled object's
6598representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006599
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006600The argument *file* must have two methods, a read() method that takes
6601an integer argument, and a readline() method that requires no
6602arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006603binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006604other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006605
6606Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006607which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006608generated by Python 2. If *fix_imports* is True, pickle will try to
6609map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006610*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006611instances pickled by Python 2; these default to 'ASCII' and 'strict',
6612respectively. The *encoding* can be 'bytes' to read these 8-bit
6613string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006614[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006615
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006616static int
Larry Hastings89964c42015-04-14 18:07:59 -04006617_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6618 int fix_imports, const char *encoding,
6619 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006620/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006621{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006622 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006623
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006624 /* In case of multiple __init__() calls, clear previous content. */
6625 if (self->read != NULL)
6626 (void)Unpickler_clear(self);
6627
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006628 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006629 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006630
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006631 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006632 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006633
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006634 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006635 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006636 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006637
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006638 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006639 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6640 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006641 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006642 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006643 }
6644 else {
6645 self->pers_func = NULL;
6646 }
6647
6648 self->stack = (Pdata *)Pdata_New();
6649 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006650 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006651
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006652 self->memo_size = 32;
6653 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006654 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006655 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006656
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006657 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006658
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006659 return 0;
6660}
6661
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006662
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006663/* Define a proxy object for the Unpickler's internal memo object. This is to
6664 * avoid breaking code like:
6665 * unpickler.memo.clear()
6666 * and
6667 * unpickler.memo = saved_memo
6668 * Is this a good idea? Not really, but we don't want to break code that uses
6669 * it. Note that we don't implement the entire mapping API here. This is
6670 * intentional, as these should be treated as black-box implementation details.
6671 *
6672 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006673 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006674 */
6675
Larry Hastings61272b72014-01-07 12:41:53 -08006676/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006677_pickle.UnpicklerMemoProxy.clear
6678
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006679Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006680[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006681
Larry Hastings3cceb382014-01-04 11:09:09 -08006682static PyObject *
6683_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006684/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006685{
6686 _Unpickler_MemoCleanup(self->unpickler);
6687 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6688 if (self->unpickler->memo == NULL)
6689 return NULL;
6690 Py_RETURN_NONE;
6691}
6692
Larry Hastings61272b72014-01-07 12:41:53 -08006693/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006694_pickle.UnpicklerMemoProxy.copy
6695
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006696Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006697[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006698
Larry Hastings3cceb382014-01-04 11:09:09 -08006699static PyObject *
6700_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006701/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006702{
6703 Py_ssize_t i;
6704 PyObject *new_memo = PyDict_New();
6705 if (new_memo == NULL)
6706 return NULL;
6707
6708 for (i = 0; i < self->unpickler->memo_size; i++) {
6709 int status;
6710 PyObject *key, *value;
6711
6712 value = self->unpickler->memo[i];
6713 if (value == NULL)
6714 continue;
6715
6716 key = PyLong_FromSsize_t(i);
6717 if (key == NULL)
6718 goto error;
6719 status = PyDict_SetItem(new_memo, key, value);
6720 Py_DECREF(key);
6721 if (status < 0)
6722 goto error;
6723 }
6724 return new_memo;
6725
6726error:
6727 Py_DECREF(new_memo);
6728 return NULL;
6729}
6730
Larry Hastings61272b72014-01-07 12:41:53 -08006731/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006732_pickle.UnpicklerMemoProxy.__reduce__
6733
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006734Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006735[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006736
Larry Hastings3cceb382014-01-04 11:09:09 -08006737static PyObject *
6738_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006739/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006740{
6741 PyObject *reduce_value;
6742 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006743 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006744 if (contents == NULL)
6745 return NULL;
6746
6747 reduce_value = PyTuple_New(2);
6748 if (reduce_value == NULL) {
6749 Py_DECREF(contents);
6750 return NULL;
6751 }
6752 constructor_args = PyTuple_New(1);
6753 if (constructor_args == NULL) {
6754 Py_DECREF(contents);
6755 Py_DECREF(reduce_value);
6756 return NULL;
6757 }
6758 PyTuple_SET_ITEM(constructor_args, 0, contents);
6759 Py_INCREF((PyObject *)&PyDict_Type);
6760 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6761 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6762 return reduce_value;
6763}
6764
6765static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006766 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6767 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6768 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006769 {NULL, NULL} /* sentinel */
6770};
6771
6772static void
6773UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6774{
6775 PyObject_GC_UnTrack(self);
6776 Py_XDECREF(self->unpickler);
6777 PyObject_GC_Del((PyObject *)self);
6778}
6779
6780static int
6781UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6782 visitproc visit, void *arg)
6783{
6784 Py_VISIT(self->unpickler);
6785 return 0;
6786}
6787
6788static int
6789UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6790{
6791 Py_CLEAR(self->unpickler);
6792 return 0;
6793}
6794
6795static PyTypeObject UnpicklerMemoProxyType = {
6796 PyVarObject_HEAD_INIT(NULL, 0)
6797 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6798 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6799 0,
6800 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6801 0, /* tp_print */
6802 0, /* tp_getattr */
6803 0, /* tp_setattr */
6804 0, /* tp_compare */
6805 0, /* tp_repr */
6806 0, /* tp_as_number */
6807 0, /* tp_as_sequence */
6808 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006809 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006810 0, /* tp_call */
6811 0, /* tp_str */
6812 PyObject_GenericGetAttr, /* tp_getattro */
6813 PyObject_GenericSetAttr, /* tp_setattro */
6814 0, /* tp_as_buffer */
6815 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6816 0, /* tp_doc */
6817 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6818 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6819 0, /* tp_richcompare */
6820 0, /* tp_weaklistoffset */
6821 0, /* tp_iter */
6822 0, /* tp_iternext */
6823 unpicklerproxy_methods, /* tp_methods */
6824};
6825
6826static PyObject *
6827UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6828{
6829 UnpicklerMemoProxyObject *self;
6830
6831 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6832 &UnpicklerMemoProxyType);
6833 if (self == NULL)
6834 return NULL;
6835 Py_INCREF(unpickler);
6836 self->unpickler = unpickler;
6837 PyObject_GC_Track(self);
6838 return (PyObject *)self;
6839}
6840
6841/*****************************************************************************/
6842
6843
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006844static PyObject *
6845Unpickler_get_memo(UnpicklerObject *self)
6846{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006847 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006848}
6849
6850static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006851Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006852{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006853 PyObject **new_memo;
6854 Py_ssize_t new_memo_size = 0;
6855 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006856
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006857 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006858 PyErr_SetString(PyExc_TypeError,
6859 "attribute deletion is not supported");
6860 return -1;
6861 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006862
6863 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6864 UnpicklerObject *unpickler =
6865 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6866
6867 new_memo_size = unpickler->memo_size;
6868 new_memo = _Unpickler_NewMemo(new_memo_size);
6869 if (new_memo == NULL)
6870 return -1;
6871
6872 for (i = 0; i < new_memo_size; i++) {
6873 Py_XINCREF(unpickler->memo[i]);
6874 new_memo[i] = unpickler->memo[i];
6875 }
6876 }
6877 else if (PyDict_Check(obj)) {
6878 Py_ssize_t i = 0;
6879 PyObject *key, *value;
6880
6881 new_memo_size = PyDict_Size(obj);
6882 new_memo = _Unpickler_NewMemo(new_memo_size);
6883 if (new_memo == NULL)
6884 return -1;
6885
6886 while (PyDict_Next(obj, &i, &key, &value)) {
6887 Py_ssize_t idx;
6888 if (!PyLong_Check(key)) {
6889 PyErr_SetString(PyExc_TypeError,
6890 "memo key must be integers");
6891 goto error;
6892 }
6893 idx = PyLong_AsSsize_t(key);
6894 if (idx == -1 && PyErr_Occurred())
6895 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006896 if (idx < 0) {
6897 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006898 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006899 goto error;
6900 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006901 if (_Unpickler_MemoPut(self, idx, value) < 0)
6902 goto error;
6903 }
6904 }
6905 else {
6906 PyErr_Format(PyExc_TypeError,
6907 "'memo' attribute must be an UnpicklerMemoProxy object"
6908 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006909 return -1;
6910 }
6911
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006912 _Unpickler_MemoCleanup(self);
6913 self->memo_size = new_memo_size;
6914 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006915
6916 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006917
6918 error:
6919 if (new_memo_size) {
6920 i = new_memo_size;
6921 while (--i >= 0) {
6922 Py_XDECREF(new_memo[i]);
6923 }
6924 PyMem_FREE(new_memo);
6925 }
6926 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006927}
6928
6929static PyObject *
6930Unpickler_get_persload(UnpicklerObject *self)
6931{
6932 if (self->pers_func == NULL)
6933 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6934 else
6935 Py_INCREF(self->pers_func);
6936 return self->pers_func;
6937}
6938
6939static int
6940Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6941{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006942 if (value == NULL) {
6943 PyErr_SetString(PyExc_TypeError,
6944 "attribute deletion is not supported");
6945 return -1;
6946 }
6947 if (!PyCallable_Check(value)) {
6948 PyErr_SetString(PyExc_TypeError,
6949 "persistent_load must be a callable taking "
6950 "one argument");
6951 return -1;
6952 }
6953
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006954 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03006955 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006956
6957 return 0;
6958}
6959
6960static PyGetSetDef Unpickler_getsets[] = {
6961 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6962 {"persistent_load", (getter)Unpickler_get_persload,
6963 (setter)Unpickler_set_persload},
6964 {NULL}
6965};
6966
6967static PyTypeObject Unpickler_Type = {
6968 PyVarObject_HEAD_INIT(NULL, 0)
6969 "_pickle.Unpickler", /*tp_name*/
6970 sizeof(UnpicklerObject), /*tp_basicsize*/
6971 0, /*tp_itemsize*/
6972 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6973 0, /*tp_print*/
6974 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006975 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006976 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006977 0, /*tp_repr*/
6978 0, /*tp_as_number*/
6979 0, /*tp_as_sequence*/
6980 0, /*tp_as_mapping*/
6981 0, /*tp_hash*/
6982 0, /*tp_call*/
6983 0, /*tp_str*/
6984 0, /*tp_getattro*/
6985 0, /*tp_setattro*/
6986 0, /*tp_as_buffer*/
6987 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006988 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006989 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6990 (inquiry)Unpickler_clear, /*tp_clear*/
6991 0, /*tp_richcompare*/
6992 0, /*tp_weaklistoffset*/
6993 0, /*tp_iter*/
6994 0, /*tp_iternext*/
6995 Unpickler_methods, /*tp_methods*/
6996 0, /*tp_members*/
6997 Unpickler_getsets, /*tp_getset*/
6998 0, /*tp_base*/
6999 0, /*tp_dict*/
7000 0, /*tp_descr_get*/
7001 0, /*tp_descr_set*/
7002 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007003 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007004 PyType_GenericAlloc, /*tp_alloc*/
7005 PyType_GenericNew, /*tp_new*/
7006 PyObject_GC_Del, /*tp_free*/
7007 0, /*tp_is_gc*/
7008};
7009
Larry Hastings61272b72014-01-07 12:41:53 -08007010/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007011
7012_pickle.dump
7013
7014 obj: object
7015 file: object
7016 protocol: object = NULL
7017 *
7018 fix_imports: bool = True
7019
7020Write a pickled representation of obj to the open file object file.
7021
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007022This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7023be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007024
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007025The optional *protocol* argument tells the pickler to use the given
7026protocol supported protocols are 0, 1, 2, 3 and 4. The default
7027protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007028
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007029Specifying a negative protocol version selects the highest protocol
7030version supported. The higher the protocol used, the more recent the
7031version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007032
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007033The *file* argument must have a write() method that accepts a single
7034bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007035writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007036this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007037
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007038If *fix_imports* is True and protocol is less than 3, pickle will try
7039to map the new Python 3 names to the old module names used in Python
70402, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007041[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007042
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007043static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007044_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007045 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007046/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007047{
7048 PicklerObject *pickler = _Pickler_New();
7049
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007050 if (pickler == NULL)
7051 return NULL;
7052
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007053 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007054 goto error;
7055
7056 if (_Pickler_SetOutputStream(pickler, file) < 0)
7057 goto error;
7058
7059 if (dump(pickler, obj) < 0)
7060 goto error;
7061
7062 if (_Pickler_FlushToFile(pickler) < 0)
7063 goto error;
7064
7065 Py_DECREF(pickler);
7066 Py_RETURN_NONE;
7067
7068 error:
7069 Py_XDECREF(pickler);
7070 return NULL;
7071}
7072
Larry Hastings61272b72014-01-07 12:41:53 -08007073/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007074
7075_pickle.dumps
7076
7077 obj: object
7078 protocol: object = NULL
7079 *
7080 fix_imports: bool = True
7081
7082Return the pickled representation of the object as a bytes object.
7083
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007084The optional *protocol* argument tells the pickler to use the given
7085protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7086protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007087
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007088Specifying a negative protocol version selects the highest protocol
7089version supported. The higher the protocol used, the more recent the
7090version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007091
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007092If *fix_imports* is True and *protocol* is less than 3, pickle will
7093try to map the new Python 3 names to the old module names used in
7094Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007095[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007096
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007097static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007098_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007099 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007100/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007101{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007102 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007103 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007104
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007105 if (pickler == NULL)
7106 return NULL;
7107
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007108 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007109 goto error;
7110
7111 if (dump(pickler, obj) < 0)
7112 goto error;
7113
7114 result = _Pickler_GetString(pickler);
7115 Py_DECREF(pickler);
7116 return result;
7117
7118 error:
7119 Py_XDECREF(pickler);
7120 return NULL;
7121}
7122
Larry Hastings61272b72014-01-07 12:41:53 -08007123/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007124
7125_pickle.load
7126
7127 file: object
7128 *
7129 fix_imports: bool = True
7130 encoding: str = 'ASCII'
7131 errors: str = 'strict'
7132
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007133Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007134
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007135This is equivalent to ``Unpickler(file).load()``, but may be more
7136efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007137
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007138The protocol version of the pickle is detected automatically, so no
7139protocol argument is needed. Bytes past the pickled object's
7140representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007141
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007142The argument *file* must have two methods, a read() method that takes
7143an integer argument, and a readline() method that requires no
7144arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007145binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007146other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007147
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007148Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007149which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007150generated by Python 2. If *fix_imports* is True, pickle will try to
7151map the old Python 2 names to the new names used in Python 3. The
7152*encoding* and *errors* tell pickle how to decode 8-bit string
7153instances pickled by Python 2; these default to 'ASCII' and 'strict',
7154respectively. The *encoding* can be 'bytes' to read these 8-bit
7155string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007156[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007157
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007158static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007159_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007160 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007161/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007162{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007163 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007164 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007165
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007166 if (unpickler == NULL)
7167 return NULL;
7168
7169 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7170 goto error;
7171
7172 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7173 goto error;
7174
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007175 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007176
7177 result = load(unpickler);
7178 Py_DECREF(unpickler);
7179 return result;
7180
7181 error:
7182 Py_XDECREF(unpickler);
7183 return NULL;
7184}
7185
Larry Hastings61272b72014-01-07 12:41:53 -08007186/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007187
7188_pickle.loads
7189
7190 data: object
7191 *
7192 fix_imports: bool = True
7193 encoding: str = 'ASCII'
7194 errors: str = 'strict'
7195
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007196Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007197
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007198The protocol version of the pickle is detected automatically, so no
7199protocol argument is needed. Bytes past the pickled object's
7200representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007201
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007202Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007203which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007204generated by Python 2. If *fix_imports* is True, pickle will try to
7205map the old Python 2 names to the new names used in Python 3. The
7206*encoding* and *errors* tell pickle how to decode 8-bit string
7207instances pickled by Python 2; these default to 'ASCII' and 'strict',
7208respectively. The *encoding* can be 'bytes' to read these 8-bit
7209string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007210[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007211
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007212static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007213_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007214 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007215/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007216{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007217 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007218 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007219
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007220 if (unpickler == NULL)
7221 return NULL;
7222
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007223 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007224 goto error;
7225
7226 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7227 goto error;
7228
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007229 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007230
7231 result = load(unpickler);
7232 Py_DECREF(unpickler);
7233 return result;
7234
7235 error:
7236 Py_XDECREF(unpickler);
7237 return NULL;
7238}
7239
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007240static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007241 _PICKLE_DUMP_METHODDEF
7242 _PICKLE_DUMPS_METHODDEF
7243 _PICKLE_LOAD_METHODDEF
7244 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007245 {NULL, NULL} /* sentinel */
7246};
7247
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007248static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007249pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007250{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007251 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007252 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007253}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007254
Stefan Krahf483b0f2013-12-14 13:43:10 +01007255static void
7256pickle_free(PyObject *m)
7257{
7258 _Pickle_ClearState(_Pickle_GetState(m));
7259}
7260
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007261static int
7262pickle_traverse(PyObject *m, visitproc visit, void *arg)
7263{
7264 PickleState *st = _Pickle_GetState(m);
7265 Py_VISIT(st->PickleError);
7266 Py_VISIT(st->PicklingError);
7267 Py_VISIT(st->UnpicklingError);
7268 Py_VISIT(st->dispatch_table);
7269 Py_VISIT(st->extension_registry);
7270 Py_VISIT(st->extension_cache);
7271 Py_VISIT(st->inverted_registry);
7272 Py_VISIT(st->name_mapping_2to3);
7273 Py_VISIT(st->import_mapping_2to3);
7274 Py_VISIT(st->name_mapping_3to2);
7275 Py_VISIT(st->import_mapping_3to2);
7276 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007277 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007278 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007279}
7280
7281static struct PyModuleDef _picklemodule = {
7282 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007283 "_pickle", /* m_name */
7284 pickle_module_doc, /* m_doc */
7285 sizeof(PickleState), /* m_size */
7286 pickle_methods, /* m_methods */
7287 NULL, /* m_reload */
7288 pickle_traverse, /* m_traverse */
7289 pickle_clear, /* m_clear */
7290 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007291};
7292
7293PyMODINIT_FUNC
7294PyInit__pickle(void)
7295{
7296 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007297 PickleState *st;
7298
7299 m = PyState_FindModule(&_picklemodule);
7300 if (m) {
7301 Py_INCREF(m);
7302 return m;
7303 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007304
7305 if (PyType_Ready(&Unpickler_Type) < 0)
7306 return NULL;
7307 if (PyType_Ready(&Pickler_Type) < 0)
7308 return NULL;
7309 if (PyType_Ready(&Pdata_Type) < 0)
7310 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007311 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7312 return NULL;
7313 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7314 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007315
7316 /* Create the module and add the functions. */
7317 m = PyModule_Create(&_picklemodule);
7318 if (m == NULL)
7319 return NULL;
7320
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007321 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007322 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7323 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007324 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007325 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7326 return NULL;
7327
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007328 st = _Pickle_GetState(m);
7329
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007330 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007331 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7332 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007333 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007334 st->PicklingError = \
7335 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7336 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007337 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007338 st->UnpicklingError = \
7339 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7340 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007341 return NULL;
7342
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007343 Py_INCREF(st->PickleError);
7344 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007345 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007346 Py_INCREF(st->PicklingError);
7347 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007348 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007349 Py_INCREF(st->UnpicklingError);
7350 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007351 return NULL;
7352
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007353 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007354 return NULL;
7355
7356 return m;
7357}