blob: ddac24334d64cf8a2814562138e2ec507addb436 [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;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800348 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800349
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800350 /* Note: this function used to reuse the argument tuple. This used to give
351 a slight performance boost with older pickle implementations where many
352 unbuffered reads occurred (thus needing many function calls).
353
354 However, this optimization was removed because it was too complicated
355 to get right. It abused the C API for tuples to mutate them which led
356 to subtle reference counting and concurrency bugs. Furthermore, the
357 introduction of protocol 4 and the prefetching optimization via peek()
358 significantly reduced the number of function calls we do. Thus, the
359 benefits became marginal at best. */
360
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800361 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800362 Py_DECREF(obj);
363 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800364 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800365 PyTuple_SET_ITEM(arg_tuple, 0, obj);
366 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800367 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800368 return result;
369}
370
371/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000372
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000373/* Internal data type used as the unpickling stack. */
374typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000375 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000376 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200377 int mark_set; /* is MARK set? */
378 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000379 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000380} Pdata;
381
382static void
383Pdata_dealloc(Pdata *self)
384{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200385 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000386 while (--i >= 0) {
387 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000388 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000389 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000390 PyObject_Del(self);
391}
392
393static PyTypeObject Pdata_Type = {
394 PyVarObject_HEAD_INIT(NULL, 0)
395 "_pickle.Pdata", /*tp_name*/
396 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200397 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000398 (destructor)Pdata_dealloc, /*tp_dealloc*/
399};
400
401static PyObject *
402Pdata_New(void)
403{
404 Pdata *self;
405
406 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
407 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000408 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200409 self->mark_set = 0;
410 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000411 self->allocated = 8;
412 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000413 if (self->data)
414 return (PyObject *)self;
415 Py_DECREF(self);
416 return PyErr_NoMemory();
417}
418
419
420/* Retain only the initial clearto items. If clearto >= the current
421 * number of items, this is a (non-erroneous) NOP.
422 */
423static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200424Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000425{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200426 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000427
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200428 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000429 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000430 return 0;
431
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 while (--i >= clearto) {
433 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000434 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000435 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436 return 0;
437}
438
439static int
440Pdata_grow(Pdata *self)
441{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000442 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200443 size_t allocated = (size_t)self->allocated;
444 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000445
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000446 new_allocated = (allocated >> 3) + 6;
447 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200448 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000449 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000450 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500451 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000452 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000453 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000454
455 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200456 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000457 return 0;
458
459 nomemory:
460 PyErr_NoMemory();
461 return -1;
462}
463
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200464static int
465Pdata_stack_underflow(Pdata *self)
466{
467 PickleState *st = _Pickle_GetGlobalState();
468 PyErr_SetString(st->UnpicklingError,
469 self->mark_set ?
470 "unexpected MARK found" :
471 "unpickling stack underflow");
472 return -1;
473}
474
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000475/* D is a Pdata*. Pop the topmost element and store it into V, which
476 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
477 * is raised and V is set to NULL.
478 */
479static PyObject *
480Pdata_pop(Pdata *self)
481{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200482 if (Py_SIZE(self) <= self->fence) {
483 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000484 return NULL;
485 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000486 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000487}
488#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
489
490static int
491Pdata_push(Pdata *self, PyObject *obj)
492{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000493 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000494 return -1;
495 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000496 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000497 return 0;
498}
499
500/* Push an object on stack, transferring its ownership to the stack. */
501#define PDATA_PUSH(D, O, ER) do { \
502 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
503
504/* Push an object on stack, adding a new reference to the object. */
505#define PDATA_APPEND(D, O, ER) do { \
506 Py_INCREF((O)); \
507 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
508
509static PyObject *
510Pdata_poptuple(Pdata *self, Py_ssize_t start)
511{
512 PyObject *tuple;
513 Py_ssize_t len, i, j;
514
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200515 if (start < self->fence) {
516 Pdata_stack_underflow(self);
517 return NULL;
518 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000519 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000520 tuple = PyTuple_New(len);
521 if (tuple == NULL)
522 return NULL;
523 for (i = start, j = 0; j < len; i++, j++)
524 PyTuple_SET_ITEM(tuple, 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 tuple;
528}
529
530static PyObject *
531Pdata_poplist(Pdata *self, Py_ssize_t start)
532{
533 PyObject *list;
534 Py_ssize_t len, i, j;
535
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000536 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000537 list = PyList_New(len);
538 if (list == NULL)
539 return NULL;
540 for (i = start, j = 0; j < len; i++, j++)
541 PyList_SET_ITEM(list, j, self->data[i]);
542
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000543 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000544 return list;
545}
546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000547typedef struct {
548 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200549 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000550} PyMemoEntry;
551
552typedef struct {
553 Py_ssize_t mt_mask;
554 Py_ssize_t mt_used;
555 Py_ssize_t mt_allocated;
556 PyMemoEntry *mt_table;
557} PyMemoTable;
558
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000559typedef struct PicklerObject {
560 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000561 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000563 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000564 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100565 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000566
567 PyObject *write; /* write() method of the output stream. */
568 PyObject *output_buffer; /* Write into a local bytearray buffer before
569 flushing to the stream. */
570 Py_ssize_t output_len; /* Length of output_buffer. */
571 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000572 int proto; /* Pickle protocol number, >= 0 */
573 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100574 int framing; /* True when framing is enabled, proto >= 4 */
575 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000576 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100577 is no frame currently open. */
578
579 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000580 int fast; /* Enable fast mode if set to a true value.
581 The fast mode disable the usage of memo,
582 therefore speeding the pickling process by
583 not generating superfluous PUT opcodes. It
584 should not be used if with self-referential
585 objects. */
586 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000587 int fix_imports; /* Indicate whether Pickler should fix
588 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000589 PyObject *fast_memo;
590} PicklerObject;
591
592typedef struct UnpicklerObject {
593 PyObject_HEAD
594 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000595
596 /* The unpickler memo is just an array of PyObject *s. Using a dict
597 is unnecessary, since the keys are contiguous ints. */
598 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100599 Py_ssize_t memo_size; /* Capacity of the memo array */
600 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000601
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000602 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000603
604 Py_buffer buffer;
605 char *input_buffer;
606 char *input_line;
607 Py_ssize_t input_len;
608 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000609 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100610
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000611 PyObject *read; /* read() method of the input stream. */
612 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000613 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000614
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000615 char *encoding; /* Name of the encoding to be used for
616 decoding strings pickled using Python
617 2.x. The default value is "ASCII" */
618 char *errors; /* Name of errors handling scheme to used when
619 decoding strings. The default value is
620 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500621 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000622 objects. */
623 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
624 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000625 int proto; /* Protocol of the pickle loaded. */
626 int fix_imports; /* Indicate whether Unpickler should fix
627 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000628} UnpicklerObject;
629
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200630typedef struct {
631 PyObject_HEAD
632 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
633} PicklerMemoProxyObject;
634
635typedef struct {
636 PyObject_HEAD
637 UnpicklerObject *unpickler;
638} UnpicklerMemoProxyObject;
639
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000640/* Forward declarations */
641static int save(PicklerObject *, PyObject *, int);
642static int save_reduce(PicklerObject *, PyObject *, PyObject *);
643static PyTypeObject Pickler_Type;
644static PyTypeObject Unpickler_Type;
645
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200646#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000647
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000648/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300649 A custom hashtable mapping void* to Python ints. This is used by the pickler
650 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000651 a bunch of unnecessary object creation. This makes a huge performance
652 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000653
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000654#define MT_MINSIZE 8
655#define PERTURB_SHIFT 5
656
657
658static PyMemoTable *
659PyMemoTable_New(void)
660{
661 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
662 if (memo == NULL) {
663 PyErr_NoMemory();
664 return NULL;
665 }
666
667 memo->mt_used = 0;
668 memo->mt_allocated = MT_MINSIZE;
669 memo->mt_mask = MT_MINSIZE - 1;
670 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
671 if (memo->mt_table == NULL) {
672 PyMem_FREE(memo);
673 PyErr_NoMemory();
674 return NULL;
675 }
676 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
677
678 return memo;
679}
680
681static PyMemoTable *
682PyMemoTable_Copy(PyMemoTable *self)
683{
684 Py_ssize_t i;
685 PyMemoTable *new = PyMemoTable_New();
686 if (new == NULL)
687 return NULL;
688
689 new->mt_used = self->mt_used;
690 new->mt_allocated = self->mt_allocated;
691 new->mt_mask = self->mt_mask;
692 /* The table we get from _New() is probably smaller than we wanted.
693 Free it and allocate one that's the right size. */
694 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500695 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000696 if (new->mt_table == NULL) {
697 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200698 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000699 return NULL;
700 }
701 for (i = 0; i < self->mt_allocated; i++) {
702 Py_XINCREF(self->mt_table[i].me_key);
703 }
704 memcpy(new->mt_table, self->mt_table,
705 sizeof(PyMemoEntry) * self->mt_allocated);
706
707 return new;
708}
709
710static Py_ssize_t
711PyMemoTable_Size(PyMemoTable *self)
712{
713 return self->mt_used;
714}
715
716static int
717PyMemoTable_Clear(PyMemoTable *self)
718{
719 Py_ssize_t i = self->mt_allocated;
720
721 while (--i >= 0) {
722 Py_XDECREF(self->mt_table[i].me_key);
723 }
724 self->mt_used = 0;
725 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
726 return 0;
727}
728
729static void
730PyMemoTable_Del(PyMemoTable *self)
731{
732 if (self == NULL)
733 return;
734 PyMemoTable_Clear(self);
735
736 PyMem_FREE(self->mt_table);
737 PyMem_FREE(self);
738}
739
740/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
741 can be considerably simpler than dictobject.c's lookdict(). */
742static PyMemoEntry *
743_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
744{
745 size_t i;
746 size_t perturb;
747 size_t mask = (size_t)self->mt_mask;
748 PyMemoEntry *table = self->mt_table;
749 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000750 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000751
752 i = hash & mask;
753 entry = &table[i];
754 if (entry->me_key == NULL || entry->me_key == key)
755 return entry;
756
757 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
758 i = (i << 2) + i + perturb + 1;
759 entry = &table[i & mask];
760 if (entry->me_key == NULL || entry->me_key == key)
761 return entry;
762 }
763 assert(0); /* Never reached */
764 return NULL;
765}
766
767/* Returns -1 on failure, 0 on success. */
768static int
769_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
770{
771 PyMemoEntry *oldtable = NULL;
772 PyMemoEntry *oldentry, *newentry;
773 Py_ssize_t new_size = MT_MINSIZE;
774 Py_ssize_t to_process;
775
776 assert(min_size > 0);
777
778 /* Find the smallest valid table size >= min_size. */
779 while (new_size < min_size && new_size > 0)
780 new_size <<= 1;
781 if (new_size <= 0) {
782 PyErr_NoMemory();
783 return -1;
784 }
785 /* new_size needs to be a power of two. */
786 assert((new_size & (new_size - 1)) == 0);
787
788 /* Allocate new table. */
789 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500790 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000791 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200792 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000793 PyErr_NoMemory();
794 return -1;
795 }
796 self->mt_allocated = new_size;
797 self->mt_mask = new_size - 1;
798 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
799
800 /* Copy entries from the old table. */
801 to_process = self->mt_used;
802 for (oldentry = oldtable; to_process > 0; oldentry++) {
803 if (oldentry->me_key != NULL) {
804 to_process--;
805 /* newentry is a pointer to a chunk of the new
806 mt_table, so we're setting the key:value pair
807 in-place. */
808 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
809 newentry->me_key = oldentry->me_key;
810 newentry->me_value = oldentry->me_value;
811 }
812 }
813
814 /* Deallocate the old table. */
815 PyMem_FREE(oldtable);
816 return 0;
817}
818
819/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200820static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000821PyMemoTable_Get(PyMemoTable *self, PyObject *key)
822{
823 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
824 if (entry->me_key == NULL)
825 return NULL;
826 return &entry->me_value;
827}
828
829/* Returns -1 on failure, 0 on success. */
830static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200831PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000832{
833 PyMemoEntry *entry;
834
835 assert(key != NULL);
836
837 entry = _PyMemoTable_Lookup(self, key);
838 if (entry->me_key != NULL) {
839 entry->me_value = value;
840 return 0;
841 }
842 Py_INCREF(key);
843 entry->me_key = key;
844 entry->me_value = value;
845 self->mt_used++;
846
847 /* If we added a key, we can safely resize. Otherwise just return!
848 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
849 *
850 * Quadrupling the size improves average table sparseness
851 * (reducing collisions) at the cost of some memory. It also halves
852 * the number of expensive resize operations in a growing memo table.
853 *
854 * Very large memo tables (over 50K items) use doubling instead.
855 * This may help applications with severe memory constraints.
856 */
857 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
858 return 0;
859 return _PyMemoTable_ResizeTable(self,
860 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
861}
862
863#undef MT_MINSIZE
864#undef PERTURB_SHIFT
865
866/*************************************************************************/
867
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000868
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000869static int
870_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000871{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300872 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200873 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000874 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000875 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000876 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100877 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000878 return 0;
879}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000880
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100881static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100882_write_size64(char *out, size_t value)
883{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200884 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800885
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200886 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800887
888 for (i = 0; i < sizeof(size_t); i++) {
889 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
890 }
891 for (i = sizeof(size_t); i < 8; i++) {
892 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800893 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100894}
895
896static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100897_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
898{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100899 qdata[0] = FRAME;
900 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100901}
902
903static int
904_Pickler_CommitFrame(PicklerObject *self)
905{
906 size_t frame_len;
907 char *qdata;
908
909 if (!self->framing || self->frame_start == -1)
910 return 0;
911 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
912 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
913 _Pickler_WriteFrameHeader(self, qdata, frame_len);
914 self->frame_start = -1;
915 return 0;
916}
917
918static int
919_Pickler_OpcodeBoundary(PicklerObject *self)
920{
921 Py_ssize_t frame_len;
922
923 if (!self->framing || self->frame_start == -1)
924 return 0;
925 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
926 if (frame_len >= FRAME_SIZE_TARGET)
927 return _Pickler_CommitFrame(self);
928 else
929 return 0;
930}
931
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000932static PyObject *
933_Pickler_GetString(PicklerObject *self)
934{
935 PyObject *output_buffer = self->output_buffer;
936
937 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100938
939 if (_Pickler_CommitFrame(self))
940 return NULL;
941
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000942 self->output_buffer = NULL;
943 /* Resize down to exact size */
944 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
945 return NULL;
946 return output_buffer;
947}
948
949static int
950_Pickler_FlushToFile(PicklerObject *self)
951{
952 PyObject *output, *result;
953
954 assert(self->write != NULL);
955
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100956 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000957 output = _Pickler_GetString(self);
958 if (output == NULL)
959 return -1;
960
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800961 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000962 Py_XDECREF(result);
963 return (result == NULL) ? -1 : 0;
964}
965
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200966static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100967_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000968{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100969 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000970 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100971 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000972
973 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100974 need_new_frame = (self->framing && self->frame_start == -1);
975
976 if (need_new_frame)
977 n = data_len + FRAME_HEADER_SIZE;
978 else
979 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000980
981 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 if (required > self->max_output_len) {
983 /* Make place in buffer for the pickle chunk */
984 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
985 PyErr_NoMemory();
986 return -1;
987 }
988 self->max_output_len = (self->output_len + n) / 2 * 3;
989 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
990 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000991 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000992 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100993 if (need_new_frame) {
994 /* Setup new frame */
995 Py_ssize_t frame_start = self->output_len;
996 self->frame_start = frame_start;
997 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
998 /* Write an invalid value, for debugging */
999 buffer[frame_start + i] = 0xFE;
1000 }
1001 self->output_len += FRAME_HEADER_SIZE;
1002 }
1003 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001004 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001005 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001006 buffer[self->output_len + i] = s[i];
1007 }
1008 }
1009 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001010 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001011 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001012 self->output_len += data_len;
1013 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001014}
1015
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001016static PicklerObject *
1017_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001018{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001019 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001020
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1022 if (self == NULL)
1023 return NULL;
1024
1025 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001026 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001027 self->write = NULL;
1028 self->proto = 0;
1029 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001030 self->framing = 0;
1031 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001032 self->fast = 0;
1033 self->fast_nesting = 0;
1034 self->fix_imports = 0;
1035 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001036 self->max_output_len = WRITE_BUF_SIZE;
1037 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001038
1039 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001040 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1041 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001042
1043 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001044 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001045 return NULL;
1046 }
1047 return self;
1048}
1049
1050static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001051_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001053 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001054
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001055 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001056 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001057 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001058 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001059 proto = PyLong_AsLong(protocol);
1060 if (proto < 0) {
1061 if (proto == -1 && PyErr_Occurred())
1062 return -1;
1063 proto = HIGHEST_PROTOCOL;
1064 }
1065 else if (proto > HIGHEST_PROTOCOL) {
1066 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1067 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001068 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001069 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001070 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001071 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001072 self->bin = proto > 0;
1073 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001074 return 0;
1075}
1076
1077/* Returns -1 (with an exception set) on failure, 0 on success. This may
1078 be called once on a freshly created Pickler. */
1079static int
1080_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1081{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001082 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001083 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001084 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001085 if (self->write == NULL) {
1086 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1087 PyErr_SetString(PyExc_TypeError,
1088 "file must have a 'write' attribute");
1089 return -1;
1090 }
1091
1092 return 0;
1093}
1094
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001095/* Returns the size of the input on success, -1 on failure. This takes its
1096 own reference to `input`. */
1097static Py_ssize_t
1098_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1099{
1100 if (self->buffer.buf != NULL)
1101 PyBuffer_Release(&self->buffer);
1102 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1103 return -1;
1104 self->input_buffer = self->buffer.buf;
1105 self->input_len = self->buffer.len;
1106 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001107 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001108 return self->input_len;
1109}
1110
Antoine Pitrou04248a82010-10-12 20:51:21 +00001111static int
1112_Unpickler_SkipConsumed(UnpicklerObject *self)
1113{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001114 Py_ssize_t consumed;
1115 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001116
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001117 consumed = self->next_read_idx - self->prefetched_idx;
1118 if (consumed <= 0)
1119 return 0;
1120
1121 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001122 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001123 r = PyObject_CallFunction(self->read, "n", consumed);
1124 if (r == NULL)
1125 return -1;
1126 Py_DECREF(r);
1127
1128 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001129 return 0;
1130}
1131
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001132static const Py_ssize_t READ_WHOLE_LINE = -1;
1133
1134/* If reading from a file, we need to only pull the bytes we need, since there
1135 may be multiple pickle objects arranged contiguously in the same input
1136 buffer.
1137
1138 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1139 bytes from the input stream/buffer.
1140
1141 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1142 failure; on success, returns the number of bytes read from the file.
1143
1144 On success, self->input_len will be 0; this is intentional so that when
1145 unpickling from a file, the "we've run out of data" code paths will trigger,
1146 causing the Unpickler to go back to the file for more data. Use the returned
1147 size to tell you how much data you can process. */
1148static Py_ssize_t
1149_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1150{
1151 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001152 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001153
1154 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001155
Antoine Pitrou04248a82010-10-12 20:51:21 +00001156 if (_Unpickler_SkipConsumed(self) < 0)
1157 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001158
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001159 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001160 PyObject *empty_tuple = PyTuple_New(0);
1161 data = PyObject_Call(self->readline, empty_tuple, NULL);
1162 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001163 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001164 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001165 PyObject *len;
1166 /* Prefetch some data without advancing the file pointer, if possible */
1167 if (self->peek && n < PREFETCH) {
1168 len = PyLong_FromSsize_t(PREFETCH);
1169 if (len == NULL)
1170 return -1;
1171 data = _Pickle_FastCall(self->peek, len);
1172 if (data == NULL) {
1173 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1174 return -1;
1175 /* peek() is probably not supported by the given file object */
1176 PyErr_Clear();
1177 Py_CLEAR(self->peek);
1178 }
1179 else {
1180 read_size = _Unpickler_SetStringInput(self, data);
1181 Py_DECREF(data);
1182 self->prefetched_idx = 0;
1183 if (n <= read_size)
1184 return n;
1185 }
1186 }
1187 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001188 if (len == NULL)
1189 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001190 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001191 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001192 if (data == NULL)
1193 return -1;
1194
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001195 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001196 Py_DECREF(data);
1197 return read_size;
1198}
1199
Victor Stinner19ed27e2016-05-20 11:42:37 +02001200/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001201static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001202_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001203{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001204 Py_ssize_t num_read;
1205
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001206 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001207 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1208 PickleState *st = _Pickle_GetGlobalState();
1209 PyErr_SetString(st->UnpicklingError,
1210 "read would overflow (invalid bytecode)");
1211 return -1;
1212 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001213
1214 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1215 assert(self->next_read_idx + n > self->input_len);
1216
Antoine Pitrou04248a82010-10-12 20:51:21 +00001217 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001218 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001219 return -1;
1220 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001221 num_read = _Unpickler_ReadFromFile(self, n);
1222 if (num_read < 0)
1223 return -1;
1224 if (num_read < n) {
1225 PyErr_Format(PyExc_EOFError, "Ran out of input");
1226 return -1;
1227 }
1228 *s = self->input_buffer;
1229 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001230 return n;
1231}
1232
Victor Stinner19ed27e2016-05-20 11:42:37 +02001233/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1234
1235 This should be used for all data reads, rather than accessing the unpickler's
1236 input buffer directly. This method deals correctly with reading from input
1237 streams, which the input buffer doesn't deal with.
1238
1239 Note that when reading from a file-like object, self->next_read_idx won't
1240 be updated (it should remain at 0 for the entire unpickling process). You
1241 should use this function's return value to know how many bytes you can
1242 consume.
1243
1244 Returns -1 (with an exception set) on failure. On success, return the
1245 number of chars read. */
1246#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001247 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001248 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1249 (self)->next_read_idx += (n), \
1250 (n)) \
1251 : _Unpickler_ReadImpl(self, (s), (n)))
1252
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001253static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001254_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1255 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001256{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001257 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001258 if (input_line == NULL) {
1259 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001260 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001261 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001262
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001263 memcpy(input_line, line, len);
1264 input_line[len] = '\0';
1265 self->input_line = input_line;
1266 *result = self->input_line;
1267 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001268}
1269
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001270/* Read a line from the input stream/buffer. If we run off the end of the input
1271 before hitting \n, return the data we found.
1272
1273 Returns the number of chars read, or -1 on failure. */
1274static Py_ssize_t
1275_Unpickler_Readline(UnpicklerObject *self, char **result)
1276{
1277 Py_ssize_t i, num_read;
1278
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001279 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001280 if (self->input_buffer[i] == '\n') {
1281 char *line_start = self->input_buffer + self->next_read_idx;
1282 num_read = i - self->next_read_idx + 1;
1283 self->next_read_idx = i + 1;
1284 return _Unpickler_CopyLine(self, line_start, num_read, result);
1285 }
1286 }
1287 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001288 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1289 if (num_read < 0)
1290 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001291 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001292 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001293 }
Victor Stinner121aab42011-09-29 23:40:53 +02001294
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001295 /* If we get here, we've run off the end of the input string. Return the
1296 remaining string and let the caller figure it out. */
1297 *result = self->input_buffer + self->next_read_idx;
1298 num_read = i - self->next_read_idx;
1299 self->next_read_idx = i;
1300 return num_read;
1301}
1302
1303/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1304 will be modified in place. */
1305static int
1306_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1307{
1308 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001309
1310 assert(new_size > self->memo_size);
1311
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001312 PyMem_RESIZE(self->memo, PyObject *, new_size);
1313 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001314 PyErr_NoMemory();
1315 return -1;
1316 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001317 for (i = self->memo_size; i < new_size; i++)
1318 self->memo[i] = NULL;
1319 self->memo_size = new_size;
1320 return 0;
1321}
1322
1323/* Returns NULL if idx is out of bounds. */
1324static PyObject *
1325_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1326{
1327 if (idx < 0 || idx >= self->memo_size)
1328 return NULL;
1329
1330 return self->memo[idx];
1331}
1332
1333/* Returns -1 (with an exception set) on failure, 0 on success.
1334 This takes its own reference to `value`. */
1335static int
1336_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1337{
1338 PyObject *old_item;
1339
1340 if (idx >= self->memo_size) {
1341 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1342 return -1;
1343 assert(idx < self->memo_size);
1344 }
1345 Py_INCREF(value);
1346 old_item = self->memo[idx];
1347 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001348 if (old_item != NULL) {
1349 Py_DECREF(old_item);
1350 }
1351 else {
1352 self->memo_len++;
1353 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001354 return 0;
1355}
1356
1357static PyObject **
1358_Unpickler_NewMemo(Py_ssize_t new_size)
1359{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001360 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001361 if (memo == NULL) {
1362 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001363 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001364 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001365 memset(memo, 0, new_size * sizeof(PyObject *));
1366 return memo;
1367}
1368
1369/* Free the unpickler's memo, taking care to decref any items left in it. */
1370static void
1371_Unpickler_MemoCleanup(UnpicklerObject *self)
1372{
1373 Py_ssize_t i;
1374 PyObject **memo = self->memo;
1375
1376 if (self->memo == NULL)
1377 return;
1378 self->memo = NULL;
1379 i = self->memo_size;
1380 while (--i >= 0) {
1381 Py_XDECREF(memo[i]);
1382 }
1383 PyMem_FREE(memo);
1384}
1385
1386static UnpicklerObject *
1387_Unpickler_New(void)
1388{
1389 UnpicklerObject *self;
1390
1391 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1392 if (self == NULL)
1393 return NULL;
1394
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001395 self->pers_func = NULL;
1396 self->input_buffer = NULL;
1397 self->input_line = NULL;
1398 self->input_len = 0;
1399 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001400 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001401 self->read = NULL;
1402 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001403 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001404 self->encoding = NULL;
1405 self->errors = NULL;
1406 self->marks = NULL;
1407 self->num_marks = 0;
1408 self->marks_size = 0;
1409 self->proto = 0;
1410 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001411 memset(&self->buffer, 0, sizeof(Py_buffer));
1412 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001413 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001414 self->memo = _Unpickler_NewMemo(self->memo_size);
1415 self->stack = (Pdata *)Pdata_New();
1416
1417 if (self->memo == NULL || self->stack == NULL) {
1418 Py_DECREF(self);
1419 return NULL;
1420 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001421
1422 return self;
1423}
1424
1425/* Returns -1 (with an exception set) on failure, 0 on success. This may
1426 be called once on a freshly created Pickler. */
1427static int
1428_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1429{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001430 _Py_IDENTIFIER(peek);
1431 _Py_IDENTIFIER(read);
1432 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001433
1434 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001435 if (self->peek == NULL) {
1436 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1437 PyErr_Clear();
1438 else
1439 return -1;
1440 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001441 self->read = _PyObject_GetAttrId(file, &PyId_read);
1442 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001443 if (self->readline == NULL || self->read == NULL) {
1444 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1445 PyErr_SetString(PyExc_TypeError,
1446 "file must have 'read' and 'readline' attributes");
1447 Py_CLEAR(self->read);
1448 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001449 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001450 return -1;
1451 }
1452 return 0;
1453}
1454
1455/* Returns -1 (with an exception set) on failure, 0 on success. This may
1456 be called once on a freshly created Pickler. */
1457static int
1458_Unpickler_SetInputEncoding(UnpicklerObject *self,
1459 const char *encoding,
1460 const char *errors)
1461{
1462 if (encoding == NULL)
1463 encoding = "ASCII";
1464 if (errors == NULL)
1465 errors = "strict";
1466
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001467 self->encoding = _PyMem_Strdup(encoding);
1468 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001469 if (self->encoding == NULL || self->errors == NULL) {
1470 PyErr_NoMemory();
1471 return -1;
1472 }
1473 return 0;
1474}
1475
1476/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001477static int
1478memo_get(PicklerObject *self, PyObject *key)
1479{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001480 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001481 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001482 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001483
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001484 value = PyMemoTable_Get(self->memo, key);
1485 if (value == NULL) {
1486 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001487 return -1;
1488 }
1489
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001490 if (!self->bin) {
1491 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001492 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1493 "%" PY_FORMAT_SIZE_T "d\n", *value);
1494 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001495 }
1496 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001497 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001498 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001499 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001500 len = 2;
1501 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001502 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001503 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001504 pdata[1] = (unsigned char)(*value & 0xff);
1505 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1506 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1507 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001508 len = 5;
1509 }
1510 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001511 PickleState *st = _Pickle_GetGlobalState();
1512 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001513 "memo id too large for LONG_BINGET");
1514 return -1;
1515 }
1516 }
1517
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001518 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001519 return -1;
1520
1521 return 0;
1522}
1523
1524/* Store an object in the memo, assign it a new unique ID based on the number
1525 of objects currently stored in the memo and generate a PUT opcode. */
1526static int
1527memo_put(PicklerObject *self, PyObject *obj)
1528{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001529 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001530 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001531 Py_ssize_t idx;
1532
1533 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001534
1535 if (self->fast)
1536 return 0;
1537
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001538 idx = PyMemoTable_Size(self->memo);
1539 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1540 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001541
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001542 if (self->proto >= 4) {
1543 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1544 return -1;
1545 return 0;
1546 }
1547 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001548 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001549 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001550 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001551 len = strlen(pdata);
1552 }
1553 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001554 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001555 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001556 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001557 len = 2;
1558 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001559 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001560 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001561 pdata[1] = (unsigned char)(idx & 0xff);
1562 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1563 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1564 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001565 len = 5;
1566 }
1567 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001568 PickleState *st = _Pickle_GetGlobalState();
1569 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001570 "memo id too large for LONG_BINPUT");
1571 return -1;
1572 }
1573 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001574 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001575 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001576
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001577 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001578}
1579
1580static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001581get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001582 _Py_static_string(PyId_dot, ".");
1583 _Py_static_string(PyId_locals, "<locals>");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001584 PyObject *dotted_path;
1585 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001586
1587 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001588 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001589 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001590 n = PyList_GET_SIZE(dotted_path);
1591 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001592 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001593 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001594 PyObject *result = PyUnicode_RichCompare(
1595 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1596 int is_equal = (result == Py_True);
1597 assert(PyBool_Check(result));
1598 Py_DECREF(result);
1599 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001600 if (obj == NULL)
1601 PyErr_Format(PyExc_AttributeError,
1602 "Can't pickle local object %R", name);
1603 else
1604 PyErr_Format(PyExc_AttributeError,
1605 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001606 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001607 return NULL;
1608 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001609 }
1610 return dotted_path;
1611}
1612
1613static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001614get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001615{
1616 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001617 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001618
1619 assert(PyList_CheckExact(names));
1620 Py_INCREF(obj);
1621 n = PyList_GET_SIZE(names);
1622 for (i = 0; i < n; i++) {
1623 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001624 Py_XDECREF(parent);
1625 parent = obj;
1626 obj = PyObject_GetAttr(parent, name);
1627 if (obj == NULL) {
1628 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001629 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001630 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001631 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001632 if (pparent != NULL)
1633 *pparent = parent;
1634 else
1635 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001636 return obj;
1637}
1638
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001639static void
1640reformat_attribute_error(PyObject *obj, PyObject *name)
1641{
1642 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1643 PyErr_Clear();
1644 PyErr_Format(PyExc_AttributeError,
1645 "Can't get attribute %R on %R", name, obj);
1646 }
1647}
1648
1649
1650static PyObject *
1651getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1652{
1653 PyObject *dotted_path, *attr;
1654
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001655 if (allow_qualname) {
1656 dotted_path = get_dotted_path(obj, name);
1657 if (dotted_path == NULL)
1658 return NULL;
1659 attr = get_deep_attribute(obj, dotted_path, NULL);
1660 Py_DECREF(dotted_path);
1661 }
1662 else
1663 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001664 if (attr == NULL)
1665 reformat_attribute_error(obj, name);
1666 return attr;
1667}
1668
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001669static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001670whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001671{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001672 PyObject *module_name;
1673 PyObject *modules_dict;
1674 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001675 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001676 _Py_IDENTIFIER(__module__);
1677 _Py_IDENTIFIER(modules);
1678 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001679
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001680 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1681
1682 if (module_name == NULL) {
1683 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001684 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001685 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001686 }
1687 else {
1688 /* In some rare cases (e.g., bound methods of extension types),
1689 __module__ can be None. If it is so, then search sys.modules for
1690 the module of global. */
1691 if (module_name != Py_None)
1692 return module_name;
1693 Py_CLEAR(module_name);
1694 }
1695 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001696
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001697 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001698 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001699 if (modules_dict == NULL) {
1700 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001701 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001702 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001703
1704 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001705 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1706 PyObject *candidate;
1707 if (PyUnicode_Check(module_name) &&
1708 !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001709 continue;
1710 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001711 continue;
1712
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001713 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001714 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001715 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001716 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001717 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001718 continue;
1719 }
1720
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001721 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001722 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001723 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001724 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001725 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001726 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001727 }
1728
1729 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001730 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001731 Py_INCREF(module_name);
1732 return module_name;
1733}
1734
1735/* fast_save_enter() and fast_save_leave() are guards against recursive
1736 objects when Pickler is used with the "fast mode" (i.e., with object
1737 memoization disabled). If the nesting of a list or dict object exceed
1738 FAST_NESTING_LIMIT, these guards will start keeping an internal
1739 reference to the seen list or dict objects and check whether these objects
1740 are recursive. These are not strictly necessary, since save() has a
1741 hard-coded recursion limit, but they give a nicer error message than the
1742 typical RuntimeError. */
1743static int
1744fast_save_enter(PicklerObject *self, PyObject *obj)
1745{
1746 /* if fast_nesting < 0, we're doing an error exit. */
1747 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1748 PyObject *key = NULL;
1749 if (self->fast_memo == NULL) {
1750 self->fast_memo = PyDict_New();
1751 if (self->fast_memo == NULL) {
1752 self->fast_nesting = -1;
1753 return 0;
1754 }
1755 }
1756 key = PyLong_FromVoidPtr(obj);
1757 if (key == NULL)
1758 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001759 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001760 Py_DECREF(key);
1761 PyErr_Format(PyExc_ValueError,
1762 "fast mode: can't pickle cyclic objects "
1763 "including object type %.200s at %p",
1764 obj->ob_type->tp_name, obj);
1765 self->fast_nesting = -1;
1766 return 0;
1767 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001768 if (PyErr_Occurred()) {
1769 return 0;
1770 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001771 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1772 Py_DECREF(key);
1773 self->fast_nesting = -1;
1774 return 0;
1775 }
1776 Py_DECREF(key);
1777 }
1778 return 1;
1779}
1780
1781static int
1782fast_save_leave(PicklerObject *self, PyObject *obj)
1783{
1784 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1785 PyObject *key = PyLong_FromVoidPtr(obj);
1786 if (key == NULL)
1787 return 0;
1788 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1789 Py_DECREF(key);
1790 return 0;
1791 }
1792 Py_DECREF(key);
1793 }
1794 return 1;
1795}
1796
1797static int
1798save_none(PicklerObject *self, PyObject *obj)
1799{
1800 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001801 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001802 return -1;
1803
1804 return 0;
1805}
1806
1807static int
1808save_bool(PicklerObject *self, PyObject *obj)
1809{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001810 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001811 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001812 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001813 return -1;
1814 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001815 else {
1816 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1817 * so that unpicklers written before bools were introduced unpickle them
1818 * as ints, but unpicklers after can recognize that bools were intended.
1819 * Note that protocol 2 added direct ways to pickle bools.
1820 */
1821 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1822 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1823 return -1;
1824 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001825 return 0;
1826}
1827
1828static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001829save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001830{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001831 PyObject *repr = NULL;
1832 Py_ssize_t size;
1833 long val;
1834 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001835
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001836 const char long_op = LONG;
1837
1838 val= PyLong_AsLong(obj);
1839 if (val == -1 && PyErr_Occurred()) {
1840 /* out of range for int pickling */
1841 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001842 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001843 else if (self->bin &&
1844 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001845 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001846 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001847
1848 Note: we can't use -0x80000000L in the above condition because some
1849 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1850 before applying the unary minus when sizeof(long) <= 4. The
1851 resulting value stays unsigned which is commonly not what we want,
1852 so MSVC happily warns us about it. However, that result would have
1853 been fine because we guard for sizeof(long) <= 4 which turns the
1854 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001855 char pdata[32];
1856 Py_ssize_t len = 0;
1857
1858 pdata[1] = (unsigned char)(val & 0xff);
1859 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1860 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1861 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001862
1863 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1864 if (pdata[2] == 0) {
1865 pdata[0] = BININT1;
1866 len = 2;
1867 }
1868 else {
1869 pdata[0] = BININT2;
1870 len = 3;
1871 }
1872 }
1873 else {
1874 pdata[0] = BININT;
1875 len = 5;
1876 }
1877
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001878 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001879 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001880
1881 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001882 }
1883
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001884 if (self->proto >= 2) {
1885 /* Linear-time pickling. */
1886 size_t nbits;
1887 size_t nbytes;
1888 unsigned char *pdata;
1889 char header[5];
1890 int i;
1891 int sign = _PyLong_Sign(obj);
1892
1893 if (sign == 0) {
1894 header[0] = LONG1;
1895 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001896 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001897 goto error;
1898 return 0;
1899 }
1900 nbits = _PyLong_NumBits(obj);
1901 if (nbits == (size_t)-1 && PyErr_Occurred())
1902 goto error;
1903 /* How many bytes do we need? There are nbits >> 3 full
1904 * bytes of data, and nbits & 7 leftover bits. If there
1905 * are any leftover bits, then we clearly need another
1906 * byte. Wnat's not so obvious is that we *probably*
1907 * need another byte even if there aren't any leftovers:
1908 * the most-significant bit of the most-significant byte
1909 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001910 * opposite of the one we need. The exception is ints
1911 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001912 * its own 256's-complement, so has the right sign bit
1913 * even without the extra byte. That's a pain to check
1914 * for in advance, though, so we always grab an extra
1915 * byte at the start, and cut it back later if possible.
1916 */
1917 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001918 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001919 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001920 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001921 goto error;
1922 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001923 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001924 if (repr == NULL)
1925 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001926 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001927 i = _PyLong_AsByteArray((PyLongObject *)obj,
1928 pdata, nbytes,
1929 1 /* little endian */ , 1 /* signed */ );
1930 if (i < 0)
1931 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001932 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001933 * needed. This is so iff the MSB is all redundant sign
1934 * bits.
1935 */
1936 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001937 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001938 pdata[nbytes - 1] == 0xff &&
1939 (pdata[nbytes - 2] & 0x80) != 0) {
1940 nbytes--;
1941 }
1942
1943 if (nbytes < 256) {
1944 header[0] = LONG1;
1945 header[1] = (unsigned char)nbytes;
1946 size = 2;
1947 }
1948 else {
1949 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001950 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001951 for (i = 1; i < 5; i++) {
1952 header[i] = (unsigned char)(size & 0xff);
1953 size >>= 8;
1954 }
1955 size = 5;
1956 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001957 if (_Pickler_Write(self, header, size) < 0 ||
1958 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001959 goto error;
1960 }
1961 else {
1962 char *string;
1963
Mark Dickinson8dd05142009-01-20 20:43:58 +00001964 /* proto < 2: write the repr and newline. This is quadratic-time (in
1965 the number of digits), in both directions. We add a trailing 'L'
1966 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001967
1968 repr = PyObject_Repr(obj);
1969 if (repr == NULL)
1970 goto error;
1971
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001972 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001973 if (string == NULL)
1974 goto error;
1975
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001976 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1977 _Pickler_Write(self, string, size) < 0 ||
1978 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001979 goto error;
1980 }
1981
1982 if (0) {
1983 error:
1984 status = -1;
1985 }
1986 Py_XDECREF(repr);
1987
1988 return status;
1989}
1990
1991static int
1992save_float(PicklerObject *self, PyObject *obj)
1993{
1994 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1995
1996 if (self->bin) {
1997 char pdata[9];
1998 pdata[0] = BINFLOAT;
1999 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2000 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002001 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002002 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002003 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002004 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002005 int result = -1;
2006 char *buf = NULL;
2007 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002008
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002009 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002010 goto done;
2011
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002012 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002013 if (!buf) {
2014 PyErr_NoMemory();
2015 goto done;
2016 }
2017
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002018 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002019 goto done;
2020
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002021 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002022 goto done;
2023
2024 result = 0;
2025done:
2026 PyMem_Free(buf);
2027 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002028 }
2029
2030 return 0;
2031}
2032
2033static int
2034save_bytes(PicklerObject *self, PyObject *obj)
2035{
2036 if (self->proto < 3) {
2037 /* Older pickle protocols do not have an opcode for pickling bytes
2038 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002039 the __reduce__ method) to permit bytes object unpickling.
2040
2041 Here we use a hack to be compatible with Python 2. Since in Python
2042 2 'bytes' is just an alias for 'str' (which has different
2043 parameters than the actual bytes object), we use codecs.encode
2044 to create the appropriate 'str' object when unpickled using
2045 Python 2 *and* the appropriate 'bytes' object when unpickled
2046 using Python 3. Again this is a hack and we don't need to do this
2047 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002048 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002049 int status;
2050
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002051 if (PyBytes_GET_SIZE(obj) == 0) {
2052 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2053 }
2054 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002055 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002056 PyObject *unicode_str =
2057 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2058 PyBytes_GET_SIZE(obj),
2059 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002060 _Py_IDENTIFIER(latin1);
2061
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002062 if (unicode_str == NULL)
2063 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002064 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002065 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002066 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002067 Py_DECREF(unicode_str);
2068 }
2069
2070 if (reduce_value == NULL)
2071 return -1;
2072
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073 /* save_reduce() will memoize the object automatically. */
2074 status = save_reduce(self, reduce_value, obj);
2075 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 return status;
2077 }
2078 else {
2079 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002080 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002081 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002082
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002083 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002084 if (size < 0)
2085 return -1;
2086
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002087 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002088 header[0] = SHORT_BINBYTES;
2089 header[1] = (unsigned char)size;
2090 len = 2;
2091 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002092 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002093 header[0] = BINBYTES;
2094 header[1] = (unsigned char)(size & 0xff);
2095 header[2] = (unsigned char)((size >> 8) & 0xff);
2096 header[3] = (unsigned char)((size >> 16) & 0xff);
2097 header[4] = (unsigned char)((size >> 24) & 0xff);
2098 len = 5;
2099 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002100 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002101 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002102 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002103 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002104 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002105 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002106 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002107 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002108 return -1; /* string too large */
2109 }
2110
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002111 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002112 return -1;
2113
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002114 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002115 return -1;
2116
2117 if (memo_put(self, obj) < 0)
2118 return -1;
2119
2120 return 0;
2121 }
2122}
2123
2124/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2125 backslash and newline characters to \uXXXX escapes. */
2126static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002127raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002128{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002129 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002130 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002131 void *data;
2132 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002133 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002134
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002135 if (PyUnicode_READY(obj))
2136 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002137
Victor Stinner358af132015-10-12 22:36:57 +02002138 _PyBytesWriter_Init(&writer);
2139
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002140 size = PyUnicode_GET_LENGTH(obj);
2141 data = PyUnicode_DATA(obj);
2142 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002143
Victor Stinner358af132015-10-12 22:36:57 +02002144 p = _PyBytesWriter_Alloc(&writer, size);
2145 if (p == NULL)
2146 goto error;
2147 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002148
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002149 for (i=0; i < size; i++) {
2150 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002151 /* Map 32-bit characters to '\Uxxxxxxxx' */
2152 if (ch >= 0x10000) {
Victor Stinner358af132015-10-12 22:36:57 +02002153 /* -1: substract 1 preallocated byte */
2154 p = _PyBytesWriter_Prepare(&writer, p, 10-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 >> 28) & 0xf];
2161 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2162 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2163 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2164 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2165 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2166 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2167 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002168 }
Victor Stinner358af132015-10-12 22:36:57 +02002169 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002170 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Victor Stinner358af132015-10-12 22:36:57 +02002171 /* -1: substract 1 preallocated byte */
2172 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2173 if (p == NULL)
2174 goto error;
2175
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002176 *p++ = '\\';
2177 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002178 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2179 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2180 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2181 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002182 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002183 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002184 else
2185 *p++ = (char) ch;
2186 }
Victor Stinner358af132015-10-12 22:36:57 +02002187
2188 return _PyBytesWriter_Finish(&writer, p);
2189
2190error:
2191 _PyBytesWriter_Dealloc(&writer);
2192 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002193}
2194
2195static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002196write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002197{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002198 char header[9];
2199 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002200
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002201 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002202 if (size <= 0xff && self->proto >= 4) {
2203 header[0] = SHORT_BINUNICODE;
2204 header[1] = (unsigned char)(size & 0xff);
2205 len = 2;
2206 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002207 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002208 header[0] = BINUNICODE;
2209 header[1] = (unsigned char)(size & 0xff);
2210 header[2] = (unsigned char)((size >> 8) & 0xff);
2211 header[3] = (unsigned char)((size >> 16) & 0xff);
2212 header[4] = (unsigned char)((size >> 24) & 0xff);
2213 len = 5;
2214 }
2215 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002216 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002217 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002218 len = 9;
2219 }
2220 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002221 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002222 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002223 return -1;
2224 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002225
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002226 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002227 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002228 if (_Pickler_Write(self, data, size) < 0)
2229 return -1;
2230
2231 return 0;
2232}
2233
2234static int
2235write_unicode_binary(PicklerObject *self, PyObject *obj)
2236{
2237 PyObject *encoded = NULL;
2238 Py_ssize_t size;
2239 char *data;
2240 int r;
2241
2242 if (PyUnicode_READY(obj))
2243 return -1;
2244
2245 data = PyUnicode_AsUTF8AndSize(obj, &size);
2246 if (data != NULL)
2247 return write_utf8(self, data, size);
2248
2249 /* Issue #8383: for strings with lone surrogates, fallback on the
2250 "surrogatepass" error handler. */
2251 PyErr_Clear();
2252 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2253 if (encoded == NULL)
2254 return -1;
2255
2256 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2257 PyBytes_GET_SIZE(encoded));
2258 Py_DECREF(encoded);
2259 return r;
2260}
2261
2262static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002263save_unicode(PicklerObject *self, PyObject *obj)
2264{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002265 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002266 if (write_unicode_binary(self, obj) < 0)
2267 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002268 }
2269 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002270 PyObject *encoded;
2271 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002272 const char unicode_op = UNICODE;
2273
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002274 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002275 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002276 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002277
Antoine Pitrou299978d2013-04-07 17:38:11 +02002278 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2279 Py_DECREF(encoded);
2280 return -1;
2281 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002282
2283 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002284 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2285 Py_DECREF(encoded);
2286 return -1;
2287 }
2288 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002290 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002291 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002292 }
2293 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002294 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002297}
2298
2299/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2300static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002301store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002302{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002303 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002304
2305 assert(PyTuple_Size(t) == len);
2306
2307 for (i = 0; i < len; i++) {
2308 PyObject *element = PyTuple_GET_ITEM(t, i);
2309
2310 if (element == NULL)
2311 return -1;
2312 if (save(self, element, 0) < 0)
2313 return -1;
2314 }
2315
2316 return 0;
2317}
2318
2319/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2320 * used across protocols to minimize the space needed to pickle them.
2321 * Tuples are also the only builtin immutable type that can be recursive
2322 * (a tuple can be reached from itself), and that requires some subtle
2323 * magic so that it works in all cases. IOW, this is a long routine.
2324 */
2325static int
2326save_tuple(PicklerObject *self, PyObject *obj)
2327{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002328 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002329
2330 const char mark_op = MARK;
2331 const char tuple_op = TUPLE;
2332 const char pop_op = POP;
2333 const char pop_mark_op = POP_MARK;
2334 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2335
2336 if ((len = PyTuple_Size(obj)) < 0)
2337 return -1;
2338
2339 if (len == 0) {
2340 char pdata[2];
2341
2342 if (self->proto) {
2343 pdata[0] = EMPTY_TUPLE;
2344 len = 1;
2345 }
2346 else {
2347 pdata[0] = MARK;
2348 pdata[1] = TUPLE;
2349 len = 2;
2350 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002351 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002352 return -1;
2353 return 0;
2354 }
2355
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002356 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002357 * saving the tuple elements, the tuple must be recursive, in
2358 * which case we'll pop everything we put on the stack, and fetch
2359 * its value from the memo.
2360 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002361 if (len <= 3 && self->proto >= 2) {
2362 /* Use TUPLE{1,2,3} opcodes. */
2363 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002364 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002365
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002366 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002367 /* pop the len elements */
2368 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002369 if (_Pickler_Write(self, &pop_op, 1) < 0)
2370 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002371 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002372 if (memo_get(self, obj) < 0)
2373 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002374
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002375 return 0;
2376 }
2377 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002378 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2379 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002380 }
2381 goto memoize;
2382 }
2383
2384 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2385 * Generate MARK e1 e2 ... TUPLE
2386 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002387 if (_Pickler_Write(self, &mark_op, 1) < 0)
2388 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002389
2390 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002391 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002392
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002393 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002394 /* pop the stack stuff we pushed */
2395 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002396 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2397 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002398 }
2399 else {
2400 /* Note that we pop one more than len, to remove
2401 * the MARK too.
2402 */
2403 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002404 if (_Pickler_Write(self, &pop_op, 1) < 0)
2405 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002406 }
2407 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002408 if (memo_get(self, obj) < 0)
2409 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002410
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002411 return 0;
2412 }
2413 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002414 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2415 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002416 }
2417
2418 memoize:
2419 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002420 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002421
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002422 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002423}
2424
2425/* iter is an iterator giving items, and we batch up chunks of
2426 * MARK item item ... item APPENDS
2427 * opcode sequences. Calling code should have arranged to first create an
2428 * empty list, or list-like object, for the APPENDS to operate on.
2429 * Returns 0 on success, <0 on error.
2430 */
2431static int
2432batch_list(PicklerObject *self, PyObject *iter)
2433{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002434 PyObject *obj = NULL;
2435 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002436 int i, n;
2437
2438 const char mark_op = MARK;
2439 const char append_op = APPEND;
2440 const char appends_op = APPENDS;
2441
2442 assert(iter != NULL);
2443
2444 /* XXX: I think this function could be made faster by avoiding the
2445 iterator interface and fetching objects directly from list using
2446 PyList_GET_ITEM.
2447 */
2448
2449 if (self->proto == 0) {
2450 /* APPENDS isn't available; do one at a time. */
2451 for (;;) {
2452 obj = PyIter_Next(iter);
2453 if (obj == NULL) {
2454 if (PyErr_Occurred())
2455 return -1;
2456 break;
2457 }
2458 i = save(self, obj, 0);
2459 Py_DECREF(obj);
2460 if (i < 0)
2461 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002462 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002463 return -1;
2464 }
2465 return 0;
2466 }
2467
2468 /* proto > 0: write in batches of BATCHSIZE. */
2469 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002470 /* Get first item */
2471 firstitem = PyIter_Next(iter);
2472 if (firstitem == NULL) {
2473 if (PyErr_Occurred())
2474 goto error;
2475
2476 /* nothing more to add */
2477 break;
2478 }
2479
2480 /* Try to get a second item */
2481 obj = PyIter_Next(iter);
2482 if (obj == NULL) {
2483 if (PyErr_Occurred())
2484 goto error;
2485
2486 /* Only one item to write */
2487 if (save(self, firstitem, 0) < 0)
2488 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002489 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002490 goto error;
2491 Py_CLEAR(firstitem);
2492 break;
2493 }
2494
2495 /* More than one item to write */
2496
2497 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002498 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002499 goto error;
2500
2501 if (save(self, firstitem, 0) < 0)
2502 goto error;
2503 Py_CLEAR(firstitem);
2504 n = 1;
2505
2506 /* Fetch and save up to BATCHSIZE items */
2507 while (obj) {
2508 if (save(self, obj, 0) < 0)
2509 goto error;
2510 Py_CLEAR(obj);
2511 n += 1;
2512
2513 if (n == BATCHSIZE)
2514 break;
2515
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002516 obj = PyIter_Next(iter);
2517 if (obj == NULL) {
2518 if (PyErr_Occurred())
2519 goto error;
2520 break;
2521 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002522 }
2523
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002524 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002525 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002526
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002527 } while (n == BATCHSIZE);
2528 return 0;
2529
2530 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002531 Py_XDECREF(firstitem);
2532 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002533 return -1;
2534}
2535
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002536/* This is a variant of batch_list() above, specialized for lists (with no
2537 * support for list subclasses). Like batch_list(), we batch up chunks of
2538 * MARK item item ... item APPENDS
2539 * opcode sequences. Calling code should have arranged to first create an
2540 * empty list, or list-like object, for the APPENDS to operate on.
2541 * Returns 0 on success, -1 on error.
2542 *
2543 * This version is considerably faster than batch_list(), if less general.
2544 *
2545 * Note that this only works for protocols > 0.
2546 */
2547static int
2548batch_list_exact(PicklerObject *self, PyObject *obj)
2549{
2550 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002551 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002552
2553 const char append_op = APPEND;
2554 const char appends_op = APPENDS;
2555 const char mark_op = MARK;
2556
2557 assert(obj != NULL);
2558 assert(self->proto > 0);
2559 assert(PyList_CheckExact(obj));
2560
2561 if (PyList_GET_SIZE(obj) == 1) {
2562 item = PyList_GET_ITEM(obj, 0);
2563 if (save(self, item, 0) < 0)
2564 return -1;
2565 if (_Pickler_Write(self, &append_op, 1) < 0)
2566 return -1;
2567 return 0;
2568 }
2569
2570 /* Write in batches of BATCHSIZE. */
2571 total = 0;
2572 do {
2573 this_batch = 0;
2574 if (_Pickler_Write(self, &mark_op, 1) < 0)
2575 return -1;
2576 while (total < PyList_GET_SIZE(obj)) {
2577 item = PyList_GET_ITEM(obj, total);
2578 if (save(self, item, 0) < 0)
2579 return -1;
2580 total++;
2581 if (++this_batch == BATCHSIZE)
2582 break;
2583 }
2584 if (_Pickler_Write(self, &appends_op, 1) < 0)
2585 return -1;
2586
2587 } while (total < PyList_GET_SIZE(obj));
2588
2589 return 0;
2590}
2591
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002592static int
2593save_list(PicklerObject *self, PyObject *obj)
2594{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002595 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002596 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002597 int status = 0;
2598
2599 if (self->fast && !fast_save_enter(self, obj))
2600 goto error;
2601
2602 /* Create an empty list. */
2603 if (self->bin) {
2604 header[0] = EMPTY_LIST;
2605 len = 1;
2606 }
2607 else {
2608 header[0] = MARK;
2609 header[1] = LIST;
2610 len = 2;
2611 }
2612
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002613 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002614 goto error;
2615
2616 /* Get list length, and bow out early if empty. */
2617 if ((len = PyList_Size(obj)) < 0)
2618 goto error;
2619
2620 if (memo_put(self, obj) < 0)
2621 goto error;
2622
2623 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002624 /* Materialize the list elements. */
2625 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002626 if (Py_EnterRecursiveCall(" while pickling an object"))
2627 goto error;
2628 status = batch_list_exact(self, obj);
2629 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002630 } else {
2631 PyObject *iter = PyObject_GetIter(obj);
2632 if (iter == NULL)
2633 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002634
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002635 if (Py_EnterRecursiveCall(" while pickling an object")) {
2636 Py_DECREF(iter);
2637 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002638 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002639 status = batch_list(self, iter);
2640 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002641 Py_DECREF(iter);
2642 }
2643 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002644 if (0) {
2645 error:
2646 status = -1;
2647 }
2648
2649 if (self->fast && !fast_save_leave(self, obj))
2650 status = -1;
2651
2652 return status;
2653}
2654
2655/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2656 * MARK key value ... key value SETITEMS
2657 * opcode sequences. Calling code should have arranged to first create an
2658 * empty dict, or dict-like object, for the SETITEMS to operate on.
2659 * Returns 0 on success, <0 on error.
2660 *
2661 * This is very much like batch_list(). The difference between saving
2662 * elements directly, and picking apart two-tuples, is so long-winded at
2663 * the C level, though, that attempts to combine these routines were too
2664 * ugly to bear.
2665 */
2666static int
2667batch_dict(PicklerObject *self, PyObject *iter)
2668{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002669 PyObject *obj = NULL;
2670 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002671 int i, n;
2672
2673 const char mark_op = MARK;
2674 const char setitem_op = SETITEM;
2675 const char setitems_op = SETITEMS;
2676
2677 assert(iter != NULL);
2678
2679 if (self->proto == 0) {
2680 /* SETITEMS isn't available; do one at a time. */
2681 for (;;) {
2682 obj = PyIter_Next(iter);
2683 if (obj == NULL) {
2684 if (PyErr_Occurred())
2685 return -1;
2686 break;
2687 }
2688 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2689 PyErr_SetString(PyExc_TypeError, "dict items "
2690 "iterator must return 2-tuples");
2691 return -1;
2692 }
2693 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2694 if (i >= 0)
2695 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2696 Py_DECREF(obj);
2697 if (i < 0)
2698 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002699 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002700 return -1;
2701 }
2702 return 0;
2703 }
2704
2705 /* proto > 0: write in batches of BATCHSIZE. */
2706 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002707 /* Get first item */
2708 firstitem = PyIter_Next(iter);
2709 if (firstitem == NULL) {
2710 if (PyErr_Occurred())
2711 goto error;
2712
2713 /* nothing more to add */
2714 break;
2715 }
2716 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2717 PyErr_SetString(PyExc_TypeError, "dict items "
2718 "iterator must return 2-tuples");
2719 goto error;
2720 }
2721
2722 /* Try to get a second item */
2723 obj = PyIter_Next(iter);
2724 if (obj == NULL) {
2725 if (PyErr_Occurred())
2726 goto error;
2727
2728 /* Only one item to write */
2729 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2730 goto error;
2731 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2732 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002733 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002734 goto error;
2735 Py_CLEAR(firstitem);
2736 break;
2737 }
2738
2739 /* More than one item to write */
2740
2741 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002742 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002743 goto error;
2744
2745 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2746 goto error;
2747 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2748 goto error;
2749 Py_CLEAR(firstitem);
2750 n = 1;
2751
2752 /* Fetch and save up to BATCHSIZE items */
2753 while (obj) {
2754 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2755 PyErr_SetString(PyExc_TypeError, "dict items "
2756 "iterator must return 2-tuples");
2757 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002758 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002759 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2760 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2761 goto error;
2762 Py_CLEAR(obj);
2763 n += 1;
2764
2765 if (n == BATCHSIZE)
2766 break;
2767
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002768 obj = PyIter_Next(iter);
2769 if (obj == NULL) {
2770 if (PyErr_Occurred())
2771 goto error;
2772 break;
2773 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002774 }
2775
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002776 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002777 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002778
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002779 } while (n == BATCHSIZE);
2780 return 0;
2781
2782 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002783 Py_XDECREF(firstitem);
2784 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002785 return -1;
2786}
2787
Collin Winter5c9b02d2009-05-25 05:43:30 +00002788/* This is a variant of batch_dict() above that specializes for dicts, with no
2789 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2790 * MARK key value ... key value SETITEMS
2791 * opcode sequences. Calling code should have arranged to first create an
2792 * empty dict, or dict-like object, for the SETITEMS to operate on.
2793 * Returns 0 on success, -1 on error.
2794 *
2795 * Note that this currently doesn't work for protocol 0.
2796 */
2797static int
2798batch_dict_exact(PicklerObject *self, PyObject *obj)
2799{
2800 PyObject *key = NULL, *value = NULL;
2801 int i;
2802 Py_ssize_t dict_size, ppos = 0;
2803
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002804 const char mark_op = MARK;
2805 const char setitem_op = SETITEM;
2806 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002807
2808 assert(obj != NULL);
2809 assert(self->proto > 0);
2810
2811 dict_size = PyDict_Size(obj);
2812
2813 /* Special-case len(d) == 1 to save space. */
2814 if (dict_size == 1) {
2815 PyDict_Next(obj, &ppos, &key, &value);
2816 if (save(self, key, 0) < 0)
2817 return -1;
2818 if (save(self, value, 0) < 0)
2819 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002820 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002821 return -1;
2822 return 0;
2823 }
2824
2825 /* Write in batches of BATCHSIZE. */
2826 do {
2827 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002828 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002829 return -1;
2830 while (PyDict_Next(obj, &ppos, &key, &value)) {
2831 if (save(self, key, 0) < 0)
2832 return -1;
2833 if (save(self, value, 0) < 0)
2834 return -1;
2835 if (++i == BATCHSIZE)
2836 break;
2837 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002838 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002839 return -1;
2840 if (PyDict_Size(obj) != dict_size) {
2841 PyErr_Format(
2842 PyExc_RuntimeError,
2843 "dictionary changed size during iteration");
2844 return -1;
2845 }
2846
2847 } while (i == BATCHSIZE);
2848 return 0;
2849}
2850
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002851static int
2852save_dict(PicklerObject *self, PyObject *obj)
2853{
2854 PyObject *items, *iter;
2855 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002856 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002857 int status = 0;
2858
2859 if (self->fast && !fast_save_enter(self, obj))
2860 goto error;
2861
2862 /* Create an empty dict. */
2863 if (self->bin) {
2864 header[0] = EMPTY_DICT;
2865 len = 1;
2866 }
2867 else {
2868 header[0] = MARK;
2869 header[1] = DICT;
2870 len = 2;
2871 }
2872
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002873 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002874 goto error;
2875
2876 /* Get dict size, and bow out early if empty. */
2877 if ((len = PyDict_Size(obj)) < 0)
2878 goto error;
2879
2880 if (memo_put(self, obj) < 0)
2881 goto error;
2882
2883 if (len != 0) {
2884 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002885 if (PyDict_CheckExact(obj) && self->proto > 0) {
2886 /* We can take certain shortcuts if we know this is a dict and
2887 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002888 if (Py_EnterRecursiveCall(" while pickling an object"))
2889 goto error;
2890 status = batch_dict_exact(self, obj);
2891 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002892 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002893 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002894
2895 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002896 if (items == NULL)
2897 goto error;
2898 iter = PyObject_GetIter(items);
2899 Py_DECREF(items);
2900 if (iter == NULL)
2901 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002902 if (Py_EnterRecursiveCall(" while pickling an object")) {
2903 Py_DECREF(iter);
2904 goto error;
2905 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002906 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002907 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002908 Py_DECREF(iter);
2909 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002910 }
2911
2912 if (0) {
2913 error:
2914 status = -1;
2915 }
2916
2917 if (self->fast && !fast_save_leave(self, obj))
2918 status = -1;
2919
2920 return status;
2921}
2922
2923static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002924save_set(PicklerObject *self, PyObject *obj)
2925{
2926 PyObject *item;
2927 int i;
2928 Py_ssize_t set_size, ppos = 0;
2929 Py_hash_t hash;
2930
2931 const char empty_set_op = EMPTY_SET;
2932 const char mark_op = MARK;
2933 const char additems_op = ADDITEMS;
2934
2935 if (self->proto < 4) {
2936 PyObject *items;
2937 PyObject *reduce_value;
2938 int status;
2939
2940 items = PySequence_List(obj);
2941 if (items == NULL) {
2942 return -1;
2943 }
2944 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2945 Py_DECREF(items);
2946 if (reduce_value == NULL) {
2947 return -1;
2948 }
2949 /* save_reduce() will memoize the object automatically. */
2950 status = save_reduce(self, reduce_value, obj);
2951 Py_DECREF(reduce_value);
2952 return status;
2953 }
2954
2955 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2956 return -1;
2957
2958 if (memo_put(self, obj) < 0)
2959 return -1;
2960
2961 set_size = PySet_GET_SIZE(obj);
2962 if (set_size == 0)
2963 return 0; /* nothing to do */
2964
2965 /* Write in batches of BATCHSIZE. */
2966 do {
2967 i = 0;
2968 if (_Pickler_Write(self, &mark_op, 1) < 0)
2969 return -1;
2970 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2971 if (save(self, item, 0) < 0)
2972 return -1;
2973 if (++i == BATCHSIZE)
2974 break;
2975 }
2976 if (_Pickler_Write(self, &additems_op, 1) < 0)
2977 return -1;
2978 if (PySet_GET_SIZE(obj) != set_size) {
2979 PyErr_Format(
2980 PyExc_RuntimeError,
2981 "set changed size during iteration");
2982 return -1;
2983 }
2984 } while (i == BATCHSIZE);
2985
2986 return 0;
2987}
2988
2989static int
2990save_frozenset(PicklerObject *self, PyObject *obj)
2991{
2992 PyObject *iter;
2993
2994 const char mark_op = MARK;
2995 const char frozenset_op = FROZENSET;
2996
2997 if (self->fast && !fast_save_enter(self, obj))
2998 return -1;
2999
3000 if (self->proto < 4) {
3001 PyObject *items;
3002 PyObject *reduce_value;
3003 int status;
3004
3005 items = PySequence_List(obj);
3006 if (items == NULL) {
3007 return -1;
3008 }
3009 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3010 items);
3011 Py_DECREF(items);
3012 if (reduce_value == NULL) {
3013 return -1;
3014 }
3015 /* save_reduce() will memoize the object automatically. */
3016 status = save_reduce(self, reduce_value, obj);
3017 Py_DECREF(reduce_value);
3018 return status;
3019 }
3020
3021 if (_Pickler_Write(self, &mark_op, 1) < 0)
3022 return -1;
3023
3024 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003025 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003026 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003027 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003028 for (;;) {
3029 PyObject *item;
3030
3031 item = PyIter_Next(iter);
3032 if (item == NULL) {
3033 if (PyErr_Occurred()) {
3034 Py_DECREF(iter);
3035 return -1;
3036 }
3037 break;
3038 }
3039 if (save(self, item, 0) < 0) {
3040 Py_DECREF(item);
3041 Py_DECREF(iter);
3042 return -1;
3043 }
3044 Py_DECREF(item);
3045 }
3046 Py_DECREF(iter);
3047
3048 /* If the object is already in the memo, this means it is
3049 recursive. In this case, throw away everything we put on the
3050 stack, and fetch the object back from the memo. */
3051 if (PyMemoTable_Get(self->memo, obj)) {
3052 const char pop_mark_op = POP_MARK;
3053
3054 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3055 return -1;
3056 if (memo_get(self, obj) < 0)
3057 return -1;
3058 return 0;
3059 }
3060
3061 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3062 return -1;
3063 if (memo_put(self, obj) < 0)
3064 return -1;
3065
3066 return 0;
3067}
3068
3069static int
3070fix_imports(PyObject **module_name, PyObject **global_name)
3071{
3072 PyObject *key;
3073 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003074 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003075
3076 key = PyTuple_Pack(2, *module_name, *global_name);
3077 if (key == NULL)
3078 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003079 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003080 Py_DECREF(key);
3081 if (item) {
3082 PyObject *fixed_module_name;
3083 PyObject *fixed_global_name;
3084
3085 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3086 PyErr_Format(PyExc_RuntimeError,
3087 "_compat_pickle.REVERSE_NAME_MAPPING values "
3088 "should be 2-tuples, not %.200s",
3089 Py_TYPE(item)->tp_name);
3090 return -1;
3091 }
3092 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3093 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3094 if (!PyUnicode_Check(fixed_module_name) ||
3095 !PyUnicode_Check(fixed_global_name)) {
3096 PyErr_Format(PyExc_RuntimeError,
3097 "_compat_pickle.REVERSE_NAME_MAPPING values "
3098 "should be pairs of str, not (%.200s, %.200s)",
3099 Py_TYPE(fixed_module_name)->tp_name,
3100 Py_TYPE(fixed_global_name)->tp_name);
3101 return -1;
3102 }
3103
3104 Py_CLEAR(*module_name);
3105 Py_CLEAR(*global_name);
3106 Py_INCREF(fixed_module_name);
3107 Py_INCREF(fixed_global_name);
3108 *module_name = fixed_module_name;
3109 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003110 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003111 }
3112 else if (PyErr_Occurred()) {
3113 return -1;
3114 }
3115
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003116 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003117 if (item) {
3118 if (!PyUnicode_Check(item)) {
3119 PyErr_Format(PyExc_RuntimeError,
3120 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3121 "should be strings, not %.200s",
3122 Py_TYPE(item)->tp_name);
3123 return -1;
3124 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003125 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003126 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003127 }
3128 else if (PyErr_Occurred()) {
3129 return -1;
3130 }
3131
3132 return 0;
3133}
3134
3135static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003136save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3137{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003138 PyObject *global_name = NULL;
3139 PyObject *module_name = NULL;
3140 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003141 PyObject *parent = NULL;
3142 PyObject *dotted_path = NULL;
3143 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003144 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003145 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003146 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003147 _Py_IDENTIFIER(__name__);
3148 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003149
3150 const char global_op = GLOBAL;
3151
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003152 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003153 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003154 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003155 }
3156 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003157 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3158 if (global_name == NULL) {
3159 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3160 goto error;
3161 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003162 }
3163 if (global_name == NULL) {
3164 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3165 if (global_name == NULL)
3166 goto error;
3167 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003168 }
3169
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003170 dotted_path = get_dotted_path(module, global_name);
3171 if (dotted_path == NULL)
3172 goto error;
3173 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003174 if (module_name == NULL)
3175 goto error;
3176
3177 /* XXX: Change to use the import C API directly with level=0 to disallow
3178 relative imports.
3179
3180 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3181 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3182 custom import functions (IMHO, this would be a nice security
3183 feature). The import C API would need to be extended to support the
3184 extra parameters of __import__ to fix that. */
3185 module = PyImport_Import(module_name);
3186 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003187 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003188 "Can't pickle %R: import of module %R failed",
3189 obj, module_name);
3190 goto error;
3191 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003192 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3193 Py_INCREF(lastname);
3194 cls = get_deep_attribute(module, dotted_path, &parent);
3195 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003196 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003197 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003198 "Can't pickle %R: attribute lookup %S on %S failed",
3199 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003200 goto error;
3201 }
3202 if (cls != obj) {
3203 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003204 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003205 "Can't pickle %R: it's not the same object as %S.%S",
3206 obj, module_name, global_name);
3207 goto error;
3208 }
3209 Py_DECREF(cls);
3210
3211 if (self->proto >= 2) {
3212 /* See whether this is in the extension registry, and if
3213 * so generate an EXT opcode.
3214 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003215 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003216 PyObject *code_obj; /* extension code as Python object */
3217 long code; /* extension code as C value */
3218 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003219 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003220
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003221 extension_key = PyTuple_Pack(2, module_name, global_name);
3222 if (extension_key == NULL) {
3223 goto error;
3224 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003225 code_obj = PyDict_GetItemWithError(st->extension_registry,
3226 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003227 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003228 /* The object is not registered in the extension registry.
3229 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003230 if (code_obj == NULL) {
3231 if (PyErr_Occurred()) {
3232 goto error;
3233 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003234 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003235 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003236
3237 /* XXX: pickle.py doesn't check neither the type, nor the range
3238 of the value returned by the extension_registry. It should for
3239 consistency. */
3240
3241 /* Verify code_obj has the right type and value. */
3242 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003243 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003244 "Can't pickle %R: extension code %R isn't an integer",
3245 obj, code_obj);
3246 goto error;
3247 }
3248 code = PyLong_AS_LONG(code_obj);
3249 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003250 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003251 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3252 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003253 goto error;
3254 }
3255
3256 /* Generate an EXT opcode. */
3257 if (code <= 0xff) {
3258 pdata[0] = EXT1;
3259 pdata[1] = (unsigned char)code;
3260 n = 2;
3261 }
3262 else if (code <= 0xffff) {
3263 pdata[0] = EXT2;
3264 pdata[1] = (unsigned char)(code & 0xff);
3265 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3266 n = 3;
3267 }
3268 else {
3269 pdata[0] = EXT4;
3270 pdata[1] = (unsigned char)(code & 0xff);
3271 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3272 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3273 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3274 n = 5;
3275 }
3276
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003277 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003278 goto error;
3279 }
3280 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003281 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003282 if (parent == module) {
3283 Py_INCREF(lastname);
3284 Py_DECREF(global_name);
3285 global_name = lastname;
3286 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003287 if (self->proto >= 4) {
3288 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003289
Christian Heimese8b1ba12013-11-23 21:13:39 +01003290 if (save(self, module_name, 0) < 0)
3291 goto error;
3292 if (save(self, global_name, 0) < 0)
3293 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003294
3295 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3296 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003297 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003298 else if (parent != module) {
3299 PickleState *st = _Pickle_GetGlobalState();
3300 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3301 st->getattr, parent, lastname);
3302 status = save_reduce(self, reduce_value, NULL);
3303 Py_DECREF(reduce_value);
3304 if (status < 0)
3305 goto error;
3306 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003307 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003308 /* Generate a normal global opcode if we are using a pickle
3309 protocol < 4, or if the object is not registered in the
3310 extension registry. */
3311 PyObject *encoded;
3312 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003313
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003314 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003315 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003316
3317 /* For protocol < 3 and if the user didn't request against doing
3318 so, we convert module names to the old 2.x module names. */
3319 if (self->proto < 3 && self->fix_imports) {
3320 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003321 goto error;
3322 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003323 }
3324
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003325 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3326 both the module name and the global name using UTF-8. We do so
3327 only when we are using the pickle protocol newer than version
3328 3. This is to ensure compatibility with older Unpickler running
3329 on Python 2.x. */
3330 if (self->proto == 3) {
3331 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003332 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003333 else {
3334 unicode_encoder = PyUnicode_AsASCIIString;
3335 }
3336 encoded = unicode_encoder(module_name);
3337 if (encoded == NULL) {
3338 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003339 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003340 "can't pickle module identifier '%S' using "
3341 "pickle protocol %i",
3342 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003343 goto error;
3344 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003345 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3346 PyBytes_GET_SIZE(encoded)) < 0) {
3347 Py_DECREF(encoded);
3348 goto error;
3349 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003350 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003351 if(_Pickler_Write(self, "\n", 1) < 0)
3352 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003353
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003354 /* Save the name of the module. */
3355 encoded = unicode_encoder(global_name);
3356 if (encoded == NULL) {
3357 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003358 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003359 "can't pickle global identifier '%S' using "
3360 "pickle protocol %i",
3361 global_name, self->proto);
3362 goto error;
3363 }
3364 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3365 PyBytes_GET_SIZE(encoded)) < 0) {
3366 Py_DECREF(encoded);
3367 goto error;
3368 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003369 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003370 if (_Pickler_Write(self, "\n", 1) < 0)
3371 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003372 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003373 /* Memoize the object. */
3374 if (memo_put(self, obj) < 0)
3375 goto error;
3376 }
3377
3378 if (0) {
3379 error:
3380 status = -1;
3381 }
3382 Py_XDECREF(module_name);
3383 Py_XDECREF(global_name);
3384 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003385 Py_XDECREF(parent);
3386 Py_XDECREF(dotted_path);
3387 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003388
3389 return status;
3390}
3391
3392static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003393save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3394{
3395 PyObject *reduce_value;
3396 int status;
3397
3398 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3399 if (reduce_value == NULL) {
3400 return -1;
3401 }
3402 status = save_reduce(self, reduce_value, obj);
3403 Py_DECREF(reduce_value);
3404 return status;
3405}
3406
3407static int
3408save_type(PicklerObject *self, PyObject *obj)
3409{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003410 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003411 return save_singleton_type(self, obj, Py_None);
3412 }
3413 else if (obj == (PyObject *)&PyEllipsis_Type) {
3414 return save_singleton_type(self, obj, Py_Ellipsis);
3415 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003416 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003417 return save_singleton_type(self, obj, Py_NotImplemented);
3418 }
3419 return save_global(self, obj, NULL);
3420}
3421
3422static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003423save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3424{
3425 PyObject *pid = NULL;
3426 int status = 0;
3427
3428 const char persid_op = PERSID;
3429 const char binpersid_op = BINPERSID;
3430
3431 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003432 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003433 if (pid == NULL)
3434 return -1;
3435
3436 if (pid != Py_None) {
3437 if (self->bin) {
3438 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003439 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003440 goto error;
3441 }
3442 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003443 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003444
3445 pid_str = PyObject_Str(pid);
3446 if (pid_str == NULL)
3447 goto error;
3448
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003449 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003450 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003451 if (!PyUnicode_IS_ASCII(pid_str)) {
3452 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3453 "persistent IDs in protocol 0 must be "
3454 "ASCII strings");
3455 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003456 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003457 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003458
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003459 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003460 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3461 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3462 _Pickler_Write(self, "\n", 1) < 0) {
3463 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003464 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003465 }
3466 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003467 }
3468 status = 1;
3469 }
3470
3471 if (0) {
3472 error:
3473 status = -1;
3474 }
3475 Py_XDECREF(pid);
3476
3477 return status;
3478}
3479
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003480static PyObject *
3481get_class(PyObject *obj)
3482{
3483 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003484 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003485
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003486 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003487 if (cls == NULL) {
3488 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3489 PyErr_Clear();
3490 cls = (PyObject *) Py_TYPE(obj);
3491 Py_INCREF(cls);
3492 }
3493 }
3494 return cls;
3495}
3496
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003497/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3498 * appropriate __reduce__ method for obj.
3499 */
3500static int
3501save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3502{
3503 PyObject *callable;
3504 PyObject *argtup;
3505 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003506 PyObject *listitems = Py_None;
3507 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003508 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003509 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003510 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003511
3512 const char reduce_op = REDUCE;
3513 const char build_op = BUILD;
3514 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003515 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003516
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003517 size = PyTuple_Size(args);
3518 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003519 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003520 "__reduce__ must contain 2 through 5 elements");
3521 return -1;
3522 }
3523
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3525 &callable, &argtup, &state, &listitems, &dictitems))
3526 return -1;
3527
3528 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003529 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003530 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003531 return -1;
3532 }
3533 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003534 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003535 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003536 return -1;
3537 }
3538
3539 if (state == Py_None)
3540 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003541
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003542 if (listitems == Py_None)
3543 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003544 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003545 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003546 "returned by __reduce__ must be an iterator, not %s",
3547 Py_TYPE(listitems)->tp_name);
3548 return -1;
3549 }
3550
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003551 if (dictitems == Py_None)
3552 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003553 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003554 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003555 "returned by __reduce__ must be an iterator, not %s",
3556 Py_TYPE(dictitems)->tp_name);
3557 return -1;
3558 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003559
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003560 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003561 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003562 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003563
Victor Stinner804e05e2013-11-14 01:26:17 +01003564 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003565 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003566 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003567 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003568 }
3569 PyErr_Clear();
3570 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003571 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003572 _Py_IDENTIFIER(__newobj_ex__);
3573 use_newobj_ex = PyUnicode_Compare(
3574 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003575 if (!use_newobj_ex) {
3576 _Py_IDENTIFIER(__newobj__);
3577 use_newobj = PyUnicode_Compare(
3578 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3579 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003580 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003581 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003582 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003583
3584 if (use_newobj_ex) {
3585 PyObject *cls;
3586 PyObject *args;
3587 PyObject *kwargs;
3588
3589 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003590 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003591 "length of the NEWOBJ_EX argument tuple must be "
3592 "exactly 3, not %zd", Py_SIZE(argtup));
3593 return -1;
3594 }
3595
3596 cls = PyTuple_GET_ITEM(argtup, 0);
3597 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003598 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003599 "first item from NEWOBJ_EX argument tuple must "
3600 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3601 return -1;
3602 }
3603 args = PyTuple_GET_ITEM(argtup, 1);
3604 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003605 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003606 "second item from NEWOBJ_EX argument tuple must "
3607 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3608 return -1;
3609 }
3610 kwargs = PyTuple_GET_ITEM(argtup, 2);
3611 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003612 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003613 "third item from NEWOBJ_EX argument tuple must "
3614 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3615 return -1;
3616 }
3617
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003618 if (self->proto >= 4) {
3619 if (save(self, cls, 0) < 0 ||
3620 save(self, args, 0) < 0 ||
3621 save(self, kwargs, 0) < 0 ||
3622 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3623 return -1;
3624 }
3625 }
3626 else {
3627 PyObject *newargs;
3628 PyObject *cls_new;
3629 Py_ssize_t i;
3630 _Py_IDENTIFIER(__new__);
3631
3632 newargs = PyTuple_New(Py_SIZE(args) + 2);
3633 if (newargs == NULL)
3634 return -1;
3635
3636 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3637 if (cls_new == NULL) {
3638 Py_DECREF(newargs);
3639 return -1;
3640 }
3641 PyTuple_SET_ITEM(newargs, 0, cls_new);
3642 Py_INCREF(cls);
3643 PyTuple_SET_ITEM(newargs, 1, cls);
3644 for (i = 0; i < Py_SIZE(args); i++) {
3645 PyObject *item = PyTuple_GET_ITEM(args, i);
3646 Py_INCREF(item);
3647 PyTuple_SET_ITEM(newargs, i + 2, item);
3648 }
3649
3650 callable = PyObject_Call(st->partial, newargs, kwargs);
3651 Py_DECREF(newargs);
3652 if (callable == NULL)
3653 return -1;
3654
3655 newargs = PyTuple_New(0);
3656 if (newargs == NULL) {
3657 Py_DECREF(callable);
3658 return -1;
3659 }
3660
3661 if (save(self, callable, 0) < 0 ||
3662 save(self, newargs, 0) < 0 ||
3663 _Pickler_Write(self, &reduce_op, 1) < 0) {
3664 Py_DECREF(newargs);
3665 Py_DECREF(callable);
3666 return -1;
3667 }
3668 Py_DECREF(newargs);
3669 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003670 }
3671 }
3672 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003673 PyObject *cls;
3674 PyObject *newargtup;
3675 PyObject *obj_class;
3676 int p;
3677
3678 /* Sanity checks. */
3679 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003680 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003681 return -1;
3682 }
3683
3684 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003685 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003686 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003687 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003688 return -1;
3689 }
3690
3691 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003692 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003693 p = obj_class != cls; /* true iff a problem */
3694 Py_DECREF(obj_class);
3695 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003696 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003697 "__newobj__ args has the wrong class");
3698 return -1;
3699 }
3700 }
3701 /* XXX: These calls save() are prone to infinite recursion. Imagine
3702 what happen if the value returned by the __reduce__() method of
3703 some extension type contains another object of the same type. Ouch!
3704
3705 Here is a quick example, that I ran into, to illustrate what I
3706 mean:
3707
3708 >>> import pickle, copyreg
3709 >>> copyreg.dispatch_table.pop(complex)
3710 >>> pickle.dumps(1+2j)
3711 Traceback (most recent call last):
3712 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003713 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003714
3715 Removing the complex class from copyreg.dispatch_table made the
3716 __reduce_ex__() method emit another complex object:
3717
3718 >>> (1+1j).__reduce_ex__(2)
3719 (<function __newobj__ at 0xb7b71c3c>,
3720 (<class 'complex'>, (1+1j)), None, None, None)
3721
3722 Thus when save() was called on newargstup (the 2nd item) recursion
3723 ensued. Of course, the bug was in the complex class which had a
3724 broken __getnewargs__() that emitted another complex object. But,
3725 the point, here, is it is quite easy to end up with a broken reduce
3726 function. */
3727
3728 /* Save the class and its __new__ arguments. */
3729 if (save(self, cls, 0) < 0)
3730 return -1;
3731
3732 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3733 if (newargtup == NULL)
3734 return -1;
3735
3736 p = save(self, newargtup, 0);
3737 Py_DECREF(newargtup);
3738 if (p < 0)
3739 return -1;
3740
3741 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003742 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003743 return -1;
3744 }
3745 else { /* Not using NEWOBJ. */
3746 if (save(self, callable, 0) < 0 ||
3747 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003748 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003749 return -1;
3750 }
3751
3752 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3753 the caller do not want to memoize the object. Not particularly useful,
3754 but that is to mimic the behavior save_reduce() in pickle.py when
3755 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003756 if (obj != NULL) {
3757 /* If the object is already in the memo, this means it is
3758 recursive. In this case, throw away everything we put on the
3759 stack, and fetch the object back from the memo. */
3760 if (PyMemoTable_Get(self->memo, obj)) {
3761 const char pop_op = POP;
3762
3763 if (_Pickler_Write(self, &pop_op, 1) < 0)
3764 return -1;
3765 if (memo_get(self, obj) < 0)
3766 return -1;
3767
3768 return 0;
3769 }
3770 else if (memo_put(self, obj) < 0)
3771 return -1;
3772 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003773
3774 if (listitems && batch_list(self, listitems) < 0)
3775 return -1;
3776
3777 if (dictitems && batch_dict(self, dictitems) < 0)
3778 return -1;
3779
3780 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003781 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003782 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003783 return -1;
3784 }
3785
3786 return 0;
3787}
3788
3789static int
3790save(PicklerObject *self, PyObject *obj, int pers_save)
3791{
3792 PyTypeObject *type;
3793 PyObject *reduce_func = NULL;
3794 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003795 int status = 0;
3796
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003797 if (_Pickler_OpcodeBoundary(self) < 0)
3798 return -1;
3799
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003800 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003801 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802
3803 /* The extra pers_save argument is necessary to avoid calling save_pers()
3804 on its returned object. */
3805 if (!pers_save && self->pers_func) {
3806 /* save_pers() returns:
3807 -1 to signal an error;
3808 0 if it did nothing successfully;
3809 1 if a persistent id was saved.
3810 */
3811 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3812 goto done;
3813 }
3814
3815 type = Py_TYPE(obj);
3816
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003817 /* The old cPickle had an optimization that used switch-case statement
3818 dispatching on the first letter of the type name. This has was removed
3819 since benchmarks shown that this optimization was actually slowing
3820 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003821
3822 /* Atom types; these aren't memoized, so don't check the memo. */
3823
3824 if (obj == Py_None) {
3825 status = save_none(self, obj);
3826 goto done;
3827 }
3828 else if (obj == Py_False || obj == Py_True) {
3829 status = save_bool(self, obj);
3830 goto done;
3831 }
3832 else if (type == &PyLong_Type) {
3833 status = save_long(self, obj);
3834 goto done;
3835 }
3836 else if (type == &PyFloat_Type) {
3837 status = save_float(self, obj);
3838 goto done;
3839 }
3840
3841 /* Check the memo to see if it has the object. If so, generate
3842 a GET (or BINGET) opcode, instead of pickling the object
3843 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003844 if (PyMemoTable_Get(self->memo, obj)) {
3845 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003846 goto error;
3847 goto done;
3848 }
3849
3850 if (type == &PyBytes_Type) {
3851 status = save_bytes(self, obj);
3852 goto done;
3853 }
3854 else if (type == &PyUnicode_Type) {
3855 status = save_unicode(self, obj);
3856 goto done;
3857 }
3858 else if (type == &PyDict_Type) {
3859 status = save_dict(self, obj);
3860 goto done;
3861 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003862 else if (type == &PySet_Type) {
3863 status = save_set(self, obj);
3864 goto done;
3865 }
3866 else if (type == &PyFrozenSet_Type) {
3867 status = save_frozenset(self, obj);
3868 goto done;
3869 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003870 else if (type == &PyList_Type) {
3871 status = save_list(self, obj);
3872 goto done;
3873 }
3874 else if (type == &PyTuple_Type) {
3875 status = save_tuple(self, obj);
3876 goto done;
3877 }
3878 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003879 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003880 goto done;
3881 }
3882 else if (type == &PyFunction_Type) {
3883 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003884 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003885 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003886
3887 /* XXX: This part needs some unit tests. */
3888
3889 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003890 * self.dispatch_table, copyreg.dispatch_table, the object's
3891 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003892 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003893 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003894 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003895 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3896 (PyObject *)type);
3897 if (reduce_func == NULL) {
3898 if (PyErr_Occurred()) {
3899 goto error;
3900 }
3901 } else {
3902 /* PyDict_GetItemWithError() returns a borrowed reference.
3903 Increase the reference count to be consistent with
3904 PyObject_GetItem and _PyObject_GetAttrId used below. */
3905 Py_INCREF(reduce_func);
3906 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003907 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003908 reduce_func = PyObject_GetItem(self->dispatch_table,
3909 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003910 if (reduce_func == NULL) {
3911 if (PyErr_ExceptionMatches(PyExc_KeyError))
3912 PyErr_Clear();
3913 else
3914 goto error;
3915 }
3916 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003917 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003918 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003919 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003920 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003921 else if (PyType_IsSubtype(type, &PyType_Type)) {
3922 status = save_global(self, obj, NULL);
3923 goto done;
3924 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003925 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003926 _Py_IDENTIFIER(__reduce__);
3927 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003928
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003929
3930 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3931 automatically defined as __reduce__. While this is convenient, this
3932 make it impossible to know which method was actually called. Of
3933 course, this is not a big deal. But still, it would be nice to let
3934 the user know which method was called when something go
3935 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3936 don't actually have to check for a __reduce__ method. */
3937
3938 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003939 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003940 if (reduce_func != NULL) {
3941 PyObject *proto;
3942 proto = PyLong_FromLong(self->proto);
3943 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003944 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003945 }
3946 }
3947 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003948 PickleState *st = _Pickle_GetGlobalState();
3949
3950 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003951 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003952 }
3953 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003954 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003955 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003956 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003957 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003958 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003959 PyObject *empty_tuple = PyTuple_New(0);
3960 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003961 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003962 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003963 }
3964 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003965 PyErr_Format(st->PicklingError,
3966 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003967 type->tp_name, obj);
3968 goto error;
3969 }
3970 }
3971 }
3972
3973 if (reduce_value == NULL)
3974 goto error;
3975
3976 if (PyUnicode_Check(reduce_value)) {
3977 status = save_global(self, obj, reduce_value);
3978 goto done;
3979 }
3980
3981 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003982 PickleState *st = _Pickle_GetGlobalState();
3983 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003984 "__reduce__ must return a string or tuple");
3985 goto error;
3986 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003987
3988 status = save_reduce(self, reduce_value, obj);
3989
3990 if (0) {
3991 error:
3992 status = -1;
3993 }
3994 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003995
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003996 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003997 Py_XDECREF(reduce_func);
3998 Py_XDECREF(reduce_value);
3999
4000 return status;
4001}
4002
4003static int
4004dump(PicklerObject *self, PyObject *obj)
4005{
4006 const char stop_op = STOP;
4007
4008 if (self->proto >= 2) {
4009 char header[2];
4010
4011 header[0] = PROTO;
4012 assert(self->proto >= 0 && self->proto < 256);
4013 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004014 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004015 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004016 if (self->proto >= 4)
4017 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004018 }
4019
4020 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004021 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004022 return -1;
4023
4024 return 0;
4025}
4026
Larry Hastings61272b72014-01-07 12:41:53 -08004027/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004028
4029_pickle.Pickler.clear_memo
4030
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004031Clears the pickler's "memo".
4032
4033The memo is the data structure that remembers which objects the
4034pickler has already seen, so that shared or recursive objects are
4035pickled by reference and not by value. This method is useful when
4036re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004037[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004038
Larry Hastings3cceb382014-01-04 11:09:09 -08004039static PyObject *
4040_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004041/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004042{
4043 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004044 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004045
4046 Py_RETURN_NONE;
4047}
4048
Larry Hastings61272b72014-01-07 12:41:53 -08004049/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004050
4051_pickle.Pickler.dump
4052
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004053 obj: object
4054 /
4055
4056Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004057[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004058
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004059static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004060_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004061/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004062{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004063 /* Check whether the Pickler was initialized correctly (issue3664).
4064 Developers often forget to call __init__() in their subclasses, which
4065 would trigger a segfault without this check. */
4066 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004067 PickleState *st = _Pickle_GetGlobalState();
4068 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004069 "Pickler.__init__() was not called by %s.__init__()",
4070 Py_TYPE(self)->tp_name);
4071 return NULL;
4072 }
4073
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004074 if (_Pickler_ClearBuffer(self) < 0)
4075 return NULL;
4076
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004077 if (dump(self, obj) < 0)
4078 return NULL;
4079
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004080 if (_Pickler_FlushToFile(self) < 0)
4081 return NULL;
4082
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004083 Py_RETURN_NONE;
4084}
4085
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004086/*[clinic input]
4087
4088_pickle.Pickler.__sizeof__ -> Py_ssize_t
4089
4090Returns size in memory, in bytes.
4091[clinic start generated code]*/
4092
4093static Py_ssize_t
4094_pickle_Pickler___sizeof___impl(PicklerObject *self)
4095/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4096{
4097 Py_ssize_t res, s;
4098
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004099 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004100 if (self->memo != NULL) {
4101 res += sizeof(PyMemoTable);
4102 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4103 }
4104 if (self->output_buffer != NULL) {
4105 s = _PySys_GetSizeOf(self->output_buffer);
4106 if (s == -1)
4107 return -1;
4108 res += s;
4109 }
4110 return res;
4111}
4112
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004113static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004114 _PICKLE_PICKLER_DUMP_METHODDEF
4115 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004116 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004117 {NULL, NULL} /* sentinel */
4118};
4119
4120static void
4121Pickler_dealloc(PicklerObject *self)
4122{
4123 PyObject_GC_UnTrack(self);
4124
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004125 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004126 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004127 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004128 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129 Py_XDECREF(self->fast_memo);
4130
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004131 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004132
4133 Py_TYPE(self)->tp_free((PyObject *)self);
4134}
4135
4136static int
4137Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4138{
4139 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004140 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004141 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004142 Py_VISIT(self->fast_memo);
4143 return 0;
4144}
4145
4146static int
4147Pickler_clear(PicklerObject *self)
4148{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004149 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004150 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004151 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004152 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004153 Py_CLEAR(self->fast_memo);
4154
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004155 if (self->memo != NULL) {
4156 PyMemoTable *memo = self->memo;
4157 self->memo = NULL;
4158 PyMemoTable_Del(memo);
4159 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004160 return 0;
4161}
4162
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004163
Larry Hastings61272b72014-01-07 12:41:53 -08004164/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004165
4166_pickle.Pickler.__init__
4167
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004168 file: object
4169 protocol: object = NULL
4170 fix_imports: bool = True
4171
4172This takes a binary file for writing a pickle data stream.
4173
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004174The optional *protocol* argument tells the pickler to use the given
4175protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4176protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004177
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004178Specifying a negative protocol version selects the highest protocol
4179version supported. The higher the protocol used, the more recent the
4180version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004181
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004182The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004183bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004184writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004185this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004186
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004187If *fix_imports* is True and protocol is less than 3, pickle will try
4188to map the new Python 3 names to the old module names used in Python
41892, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004190[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004191
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004192static int
Larry Hastings89964c42015-04-14 18:07:59 -04004193_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4194 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004195/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004196{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004197 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004198 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004199
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004200 /* In case of multiple __init__() calls, clear previous content. */
4201 if (self->write != NULL)
4202 (void)Pickler_clear(self);
4203
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004204 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004205 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004206
4207 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004208 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004209
4210 /* memo and output_buffer may have already been created in _Pickler_New */
4211 if (self->memo == NULL) {
4212 self->memo = PyMemoTable_New();
4213 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004214 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004215 }
4216 self->output_len = 0;
4217 if (self->output_buffer == NULL) {
4218 self->max_output_len = WRITE_BUF_SIZE;
4219 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4220 self->max_output_len);
4221 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004222 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004223 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004224
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004225 self->fast = 0;
4226 self->fast_nesting = 0;
4227 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004228 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004229 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4230 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4231 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004232 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004233 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004234 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004235 self->dispatch_table = NULL;
4236 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4237 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4238 &PyId_dispatch_table);
4239 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004240 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004241 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004242
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004243 return 0;
4244}
4245
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004246
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004247/* Define a proxy object for the Pickler's internal memo object. This is to
4248 * avoid breaking code like:
4249 * pickler.memo.clear()
4250 * and
4251 * pickler.memo = saved_memo
4252 * Is this a good idea? Not really, but we don't want to break code that uses
4253 * it. Note that we don't implement the entire mapping API here. This is
4254 * intentional, as these should be treated as black-box implementation details.
4255 */
4256
Larry Hastings61272b72014-01-07 12:41:53 -08004257/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004258_pickle.PicklerMemoProxy.clear
4259
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004260Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004261[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004262
Larry Hastings3cceb382014-01-04 11:09:09 -08004263static PyObject *
4264_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004265/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004266{
4267 if (self->pickler->memo)
4268 PyMemoTable_Clear(self->pickler->memo);
4269 Py_RETURN_NONE;
4270}
4271
Larry Hastings61272b72014-01-07 12:41:53 -08004272/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004273_pickle.PicklerMemoProxy.copy
4274
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004275Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004276[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004277
Larry Hastings3cceb382014-01-04 11:09:09 -08004278static PyObject *
4279_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004280/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004281{
4282 Py_ssize_t i;
4283 PyMemoTable *memo;
4284 PyObject *new_memo = PyDict_New();
4285 if (new_memo == NULL)
4286 return NULL;
4287
4288 memo = self->pickler->memo;
4289 for (i = 0; i < memo->mt_allocated; ++i) {
4290 PyMemoEntry entry = memo->mt_table[i];
4291 if (entry.me_key != NULL) {
4292 int status;
4293 PyObject *key, *value;
4294
4295 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004296 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004297
4298 if (key == NULL || value == NULL) {
4299 Py_XDECREF(key);
4300 Py_XDECREF(value);
4301 goto error;
4302 }
4303 status = PyDict_SetItem(new_memo, key, value);
4304 Py_DECREF(key);
4305 Py_DECREF(value);
4306 if (status < 0)
4307 goto error;
4308 }
4309 }
4310 return new_memo;
4311
4312 error:
4313 Py_XDECREF(new_memo);
4314 return NULL;
4315}
4316
Larry Hastings61272b72014-01-07 12:41:53 -08004317/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004318_pickle.PicklerMemoProxy.__reduce__
4319
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004320Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004321[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004322
Larry Hastings3cceb382014-01-04 11:09:09 -08004323static PyObject *
4324_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004325/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004326{
4327 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004328 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004329 if (contents == NULL)
4330 return NULL;
4331
4332 reduce_value = PyTuple_New(2);
4333 if (reduce_value == NULL) {
4334 Py_DECREF(contents);
4335 return NULL;
4336 }
4337 dict_args = PyTuple_New(1);
4338 if (dict_args == NULL) {
4339 Py_DECREF(contents);
4340 Py_DECREF(reduce_value);
4341 return NULL;
4342 }
4343 PyTuple_SET_ITEM(dict_args, 0, contents);
4344 Py_INCREF((PyObject *)&PyDict_Type);
4345 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4346 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4347 return reduce_value;
4348}
4349
4350static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004351 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4352 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4353 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004354 {NULL, NULL} /* sentinel */
4355};
4356
4357static void
4358PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4359{
4360 PyObject_GC_UnTrack(self);
4361 Py_XDECREF(self->pickler);
4362 PyObject_GC_Del((PyObject *)self);
4363}
4364
4365static int
4366PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4367 visitproc visit, void *arg)
4368{
4369 Py_VISIT(self->pickler);
4370 return 0;
4371}
4372
4373static int
4374PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4375{
4376 Py_CLEAR(self->pickler);
4377 return 0;
4378}
4379
4380static PyTypeObject PicklerMemoProxyType = {
4381 PyVarObject_HEAD_INIT(NULL, 0)
4382 "_pickle.PicklerMemoProxy", /*tp_name*/
4383 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4384 0,
4385 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4386 0, /* tp_print */
4387 0, /* tp_getattr */
4388 0, /* tp_setattr */
4389 0, /* tp_compare */
4390 0, /* tp_repr */
4391 0, /* tp_as_number */
4392 0, /* tp_as_sequence */
4393 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004394 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004395 0, /* tp_call */
4396 0, /* tp_str */
4397 PyObject_GenericGetAttr, /* tp_getattro */
4398 PyObject_GenericSetAttr, /* tp_setattro */
4399 0, /* tp_as_buffer */
4400 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4401 0, /* tp_doc */
4402 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4403 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4404 0, /* tp_richcompare */
4405 0, /* tp_weaklistoffset */
4406 0, /* tp_iter */
4407 0, /* tp_iternext */
4408 picklerproxy_methods, /* tp_methods */
4409};
4410
4411static PyObject *
4412PicklerMemoProxy_New(PicklerObject *pickler)
4413{
4414 PicklerMemoProxyObject *self;
4415
4416 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4417 if (self == NULL)
4418 return NULL;
4419 Py_INCREF(pickler);
4420 self->pickler = pickler;
4421 PyObject_GC_Track(self);
4422 return (PyObject *)self;
4423}
4424
4425/*****************************************************************************/
4426
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004427static PyObject *
4428Pickler_get_memo(PicklerObject *self)
4429{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004430 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004431}
4432
4433static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004434Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004435{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004436 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004437
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004438 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004439 PyErr_SetString(PyExc_TypeError,
4440 "attribute deletion is not supported");
4441 return -1;
4442 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004443
4444 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4445 PicklerObject *pickler =
4446 ((PicklerMemoProxyObject *)obj)->pickler;
4447
4448 new_memo = PyMemoTable_Copy(pickler->memo);
4449 if (new_memo == NULL)
4450 return -1;
4451 }
4452 else if (PyDict_Check(obj)) {
4453 Py_ssize_t i = 0;
4454 PyObject *key, *value;
4455
4456 new_memo = PyMemoTable_New();
4457 if (new_memo == NULL)
4458 return -1;
4459
4460 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004461 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004462 PyObject *memo_obj;
4463
4464 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4465 PyErr_SetString(PyExc_TypeError,
4466 "'memo' values must be 2-item tuples");
4467 goto error;
4468 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004469 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004470 if (memo_id == -1 && PyErr_Occurred())
4471 goto error;
4472 memo_obj = PyTuple_GET_ITEM(value, 1);
4473 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4474 goto error;
4475 }
4476 }
4477 else {
4478 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004479 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004480 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004481 return -1;
4482 }
4483
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004484 PyMemoTable_Del(self->memo);
4485 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004486
4487 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004488
4489 error:
4490 if (new_memo)
4491 PyMemoTable_Del(new_memo);
4492 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004493}
4494
4495static PyObject *
4496Pickler_get_persid(PicklerObject *self)
4497{
4498 if (self->pers_func == NULL)
4499 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4500 else
4501 Py_INCREF(self->pers_func);
4502 return self->pers_func;
4503}
4504
4505static int
4506Pickler_set_persid(PicklerObject *self, PyObject *value)
4507{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004508 if (value == NULL) {
4509 PyErr_SetString(PyExc_TypeError,
4510 "attribute deletion is not supported");
4511 return -1;
4512 }
4513 if (!PyCallable_Check(value)) {
4514 PyErr_SetString(PyExc_TypeError,
4515 "persistent_id must be a callable taking one argument");
4516 return -1;
4517 }
4518
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004519 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004520 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004521
4522 return 0;
4523}
4524
4525static PyMemberDef Pickler_members[] = {
4526 {"bin", T_INT, offsetof(PicklerObject, bin)},
4527 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004528 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004529 {NULL}
4530};
4531
4532static PyGetSetDef Pickler_getsets[] = {
4533 {"memo", (getter)Pickler_get_memo,
4534 (setter)Pickler_set_memo},
4535 {"persistent_id", (getter)Pickler_get_persid,
4536 (setter)Pickler_set_persid},
4537 {NULL}
4538};
4539
4540static PyTypeObject Pickler_Type = {
4541 PyVarObject_HEAD_INIT(NULL, 0)
4542 "_pickle.Pickler" , /*tp_name*/
4543 sizeof(PicklerObject), /*tp_basicsize*/
4544 0, /*tp_itemsize*/
4545 (destructor)Pickler_dealloc, /*tp_dealloc*/
4546 0, /*tp_print*/
4547 0, /*tp_getattr*/
4548 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004549 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004550 0, /*tp_repr*/
4551 0, /*tp_as_number*/
4552 0, /*tp_as_sequence*/
4553 0, /*tp_as_mapping*/
4554 0, /*tp_hash*/
4555 0, /*tp_call*/
4556 0, /*tp_str*/
4557 0, /*tp_getattro*/
4558 0, /*tp_setattro*/
4559 0, /*tp_as_buffer*/
4560 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004561 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004562 (traverseproc)Pickler_traverse, /*tp_traverse*/
4563 (inquiry)Pickler_clear, /*tp_clear*/
4564 0, /*tp_richcompare*/
4565 0, /*tp_weaklistoffset*/
4566 0, /*tp_iter*/
4567 0, /*tp_iternext*/
4568 Pickler_methods, /*tp_methods*/
4569 Pickler_members, /*tp_members*/
4570 Pickler_getsets, /*tp_getset*/
4571 0, /*tp_base*/
4572 0, /*tp_dict*/
4573 0, /*tp_descr_get*/
4574 0, /*tp_descr_set*/
4575 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004576 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004577 PyType_GenericAlloc, /*tp_alloc*/
4578 PyType_GenericNew, /*tp_new*/
4579 PyObject_GC_Del, /*tp_free*/
4580 0, /*tp_is_gc*/
4581};
4582
Victor Stinner121aab42011-09-29 23:40:53 +02004583/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004584
4585 XXX: It would be nice to able to avoid Python function call overhead, by
4586 using directly the C version of find_class(), when find_class() is not
4587 overridden by a subclass. Although, this could become rather hackish. A
4588 simpler optimization would be to call the C function when self is not a
4589 subclass instance. */
4590static PyObject *
4591find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4592{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004593 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004594
4595 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4596 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004597}
4598
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004599static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004600marker(UnpicklerObject *self)
4601{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004602 Py_ssize_t mark;
4603
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004604 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004605 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004606 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004607 return -1;
4608 }
4609
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004610 mark = self->marks[--self->num_marks];
4611 self->stack->mark_set = self->num_marks != 0;
4612 self->stack->fence = self->num_marks ?
4613 self->marks[self->num_marks - 1] : 0;
4614 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004615}
4616
4617static int
4618load_none(UnpicklerObject *self)
4619{
4620 PDATA_APPEND(self->stack, Py_None, -1);
4621 return 0;
4622}
4623
4624static int
4625bad_readline(void)
4626{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004627 PickleState *st = _Pickle_GetGlobalState();
4628 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004629 return -1;
4630}
4631
4632static int
4633load_int(UnpicklerObject *self)
4634{
4635 PyObject *value;
4636 char *endptr, *s;
4637 Py_ssize_t len;
4638 long x;
4639
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004640 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004641 return -1;
4642 if (len < 2)
4643 return bad_readline();
4644
4645 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004646 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004647 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004648 x = strtol(s, &endptr, 0);
4649
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004650 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004651 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004652 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004653 errno = 0;
4654 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004655 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004656 if (value == NULL) {
4657 PyErr_SetString(PyExc_ValueError,
4658 "could not convert string to int");
4659 return -1;
4660 }
4661 }
4662 else {
4663 if (len == 3 && (x == 0 || x == 1)) {
4664 if ((value = PyBool_FromLong(x)) == NULL)
4665 return -1;
4666 }
4667 else {
4668 if ((value = PyLong_FromLong(x)) == NULL)
4669 return -1;
4670 }
4671 }
4672
4673 PDATA_PUSH(self->stack, value, -1);
4674 return 0;
4675}
4676
4677static int
4678load_bool(UnpicklerObject *self, PyObject *boolean)
4679{
4680 assert(boolean == Py_True || boolean == Py_False);
4681 PDATA_APPEND(self->stack, boolean, -1);
4682 return 0;
4683}
4684
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004685/* s contains x bytes of an unsigned little-endian integer. Return its value
4686 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4687 */
4688static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004689calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004690{
4691 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004692 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004693 size_t x = 0;
4694
Serhiy Storchakae0606192015-09-29 22:10:07 +03004695 if (nbytes > (int)sizeof(size_t)) {
4696 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4697 * have 64-bit size that can't be represented on 32-bit platform.
4698 */
4699 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4700 if (s[i])
4701 return -1;
4702 }
4703 nbytes = (int)sizeof(size_t);
4704 }
4705 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004706 x |= (size_t) s[i] << (8 * i);
4707 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004708
4709 if (x > PY_SSIZE_T_MAX)
4710 return -1;
4711 else
4712 return (Py_ssize_t) x;
4713}
4714
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004715/* s contains x bytes of a little-endian integer. Return its value as a
4716 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004717 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004718 * of x-platform bugs.
4719 */
4720static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004721calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004722{
4723 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004724 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004725 long x = 0;
4726
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004727 for (i = 0; i < nbytes; i++) {
4728 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004729 }
4730
4731 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4732 * is signed, so on a box with longs bigger than 4 bytes we need
4733 * to extend a BININT's sign bit to the full width.
4734 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004735 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004736 x |= -(x & (1L << 31));
4737 }
4738
4739 return x;
4740}
4741
4742static int
4743load_binintx(UnpicklerObject *self, char *s, int size)
4744{
4745 PyObject *value;
4746 long x;
4747
4748 x = calc_binint(s, size);
4749
4750 if ((value = PyLong_FromLong(x)) == NULL)
4751 return -1;
4752
4753 PDATA_PUSH(self->stack, value, -1);
4754 return 0;
4755}
4756
4757static int
4758load_binint(UnpicklerObject *self)
4759{
4760 char *s;
4761
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004762 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004763 return -1;
4764
4765 return load_binintx(self, s, 4);
4766}
4767
4768static int
4769load_binint1(UnpicklerObject *self)
4770{
4771 char *s;
4772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004773 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004774 return -1;
4775
4776 return load_binintx(self, s, 1);
4777}
4778
4779static int
4780load_binint2(UnpicklerObject *self)
4781{
4782 char *s;
4783
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004784 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004785 return -1;
4786
4787 return load_binintx(self, s, 2);
4788}
4789
4790static int
4791load_long(UnpicklerObject *self)
4792{
4793 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004794 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004795 Py_ssize_t len;
4796
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004797 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004798 return -1;
4799 if (len < 2)
4800 return bad_readline();
4801
Mark Dickinson8dd05142009-01-20 20:43:58 +00004802 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4803 the 'L' before calling PyLong_FromString. In order to maintain
4804 compatibility with Python 3.0.0, we don't actually *require*
4805 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004806 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004807 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004808 /* XXX: Should the base argument explicitly set to 10? */
4809 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004810 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004811 return -1;
4812
4813 PDATA_PUSH(self->stack, value, -1);
4814 return 0;
4815}
4816
4817/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4818 * data following.
4819 */
4820static int
4821load_counted_long(UnpicklerObject *self, int size)
4822{
4823 PyObject *value;
4824 char *nbytes;
4825 char *pdata;
4826
4827 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004828 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004829 return -1;
4830
4831 size = calc_binint(nbytes, size);
4832 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004833 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004834 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004835 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004836 "LONG pickle has negative byte count");
4837 return -1;
4838 }
4839
4840 if (size == 0)
4841 value = PyLong_FromLong(0L);
4842 else {
4843 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004844 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004845 return -1;
4846 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4847 1 /* little endian */ , 1 /* signed */ );
4848 }
4849 if (value == NULL)
4850 return -1;
4851 PDATA_PUSH(self->stack, value, -1);
4852 return 0;
4853}
4854
4855static int
4856load_float(UnpicklerObject *self)
4857{
4858 PyObject *value;
4859 char *endptr, *s;
4860 Py_ssize_t len;
4861 double d;
4862
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004863 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004864 return -1;
4865 if (len < 2)
4866 return bad_readline();
4867
4868 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004869 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4870 if (d == -1.0 && PyErr_Occurred())
4871 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004872 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004873 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4874 return -1;
4875 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004876 value = PyFloat_FromDouble(d);
4877 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004878 return -1;
4879
4880 PDATA_PUSH(self->stack, value, -1);
4881 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004882}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004883
4884static int
4885load_binfloat(UnpicklerObject *self)
4886{
4887 PyObject *value;
4888 double x;
4889 char *s;
4890
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004891 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004892 return -1;
4893
4894 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4895 if (x == -1.0 && PyErr_Occurred())
4896 return -1;
4897
4898 if ((value = PyFloat_FromDouble(x)) == NULL)
4899 return -1;
4900
4901 PDATA_PUSH(self->stack, value, -1);
4902 return 0;
4903}
4904
4905static int
4906load_string(UnpicklerObject *self)
4907{
4908 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004909 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004910 Py_ssize_t len;
4911 char *s, *p;
4912
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004913 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004914 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004915 /* Strip the newline */
4916 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004917 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004918 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004919 p = s + 1;
4920 len -= 2;
4921 }
4922 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004923 PickleState *st = _Pickle_GetGlobalState();
4924 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004925 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004926 return -1;
4927 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004928 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004929
4930 /* Use the PyBytes API to decode the string, since that is what is used
4931 to encode, and then coerce the result to Unicode. */
4932 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004933 if (bytes == NULL)
4934 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004935
4936 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4937 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4938 if (strcmp(self->encoding, "bytes") == 0) {
4939 obj = bytes;
4940 }
4941 else {
4942 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4943 Py_DECREF(bytes);
4944 if (obj == NULL) {
4945 return -1;
4946 }
4947 }
4948
4949 PDATA_PUSH(self->stack, obj, -1);
4950 return 0;
4951}
4952
4953static int
4954load_counted_binstring(UnpicklerObject *self, int nbytes)
4955{
4956 PyObject *obj;
4957 Py_ssize_t size;
4958 char *s;
4959
4960 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004961 return -1;
4962
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004963 size = calc_binsize(s, nbytes);
4964 if (size < 0) {
4965 PickleState *st = _Pickle_GetGlobalState();
4966 PyErr_Format(st->UnpicklingError,
4967 "BINSTRING exceeds system's maximum size of %zd bytes",
4968 PY_SSIZE_T_MAX);
4969 return -1;
4970 }
4971
4972 if (_Unpickler_Read(self, &s, size) < 0)
4973 return -1;
4974
4975 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4976 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4977 if (strcmp(self->encoding, "bytes") == 0) {
4978 obj = PyBytes_FromStringAndSize(s, size);
4979 }
4980 else {
4981 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4982 }
4983 if (obj == NULL) {
4984 return -1;
4985 }
4986
4987 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004988 return 0;
4989}
4990
4991static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004992load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004993{
4994 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004995 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004996 char *s;
4997
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004998 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004999 return -1;
5000
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005001 size = calc_binsize(s, nbytes);
5002 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005003 PyErr_Format(PyExc_OverflowError,
5004 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005005 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005006 return -1;
5007 }
5008
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005009 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005010 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005011
5012 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005013 if (bytes == NULL)
5014 return -1;
5015
5016 PDATA_PUSH(self->stack, bytes, -1);
5017 return 0;
5018}
5019
5020static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005021load_unicode(UnpicklerObject *self)
5022{
5023 PyObject *str;
5024 Py_ssize_t len;
5025 char *s;
5026
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005027 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005028 return -1;
5029 if (len < 1)
5030 return bad_readline();
5031
5032 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5033 if (str == NULL)
5034 return -1;
5035
5036 PDATA_PUSH(self->stack, str, -1);
5037 return 0;
5038}
5039
5040static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005041load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042{
5043 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005044 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005045 char *s;
5046
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005047 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048 return -1;
5049
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005050 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005051 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005052 PyErr_Format(PyExc_OverflowError,
5053 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005054 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005055 return -1;
5056 }
5057
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005058 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005059 return -1;
5060
Victor Stinner485fb562010-04-13 11:07:24 +00005061 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005062 if (str == NULL)
5063 return -1;
5064
5065 PDATA_PUSH(self->stack, str, -1);
5066 return 0;
5067}
5068
5069static int
Victor Stinner21b47112016-03-14 18:09:39 +01005070load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005071{
5072 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005073
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005074 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005075 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005076
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005077 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005078 if (tuple == NULL)
5079 return -1;
5080 PDATA_PUSH(self->stack, tuple, -1);
5081 return 0;
5082}
5083
5084static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005085load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005086{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005087 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005088
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005089 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005090 return -1;
5091
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005092 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005093}
5094
5095static int
5096load_empty_list(UnpicklerObject *self)
5097{
5098 PyObject *list;
5099
5100 if ((list = PyList_New(0)) == NULL)
5101 return -1;
5102 PDATA_PUSH(self->stack, list, -1);
5103 return 0;
5104}
5105
5106static int
5107load_empty_dict(UnpicklerObject *self)
5108{
5109 PyObject *dict;
5110
5111 if ((dict = PyDict_New()) == NULL)
5112 return -1;
5113 PDATA_PUSH(self->stack, dict, -1);
5114 return 0;
5115}
5116
5117static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005118load_empty_set(UnpicklerObject *self)
5119{
5120 PyObject *set;
5121
5122 if ((set = PySet_New(NULL)) == NULL)
5123 return -1;
5124 PDATA_PUSH(self->stack, set, -1);
5125 return 0;
5126}
5127
5128static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005129load_list(UnpicklerObject *self)
5130{
5131 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005132 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005133
5134 if ((i = marker(self)) < 0)
5135 return -1;
5136
5137 list = Pdata_poplist(self->stack, i);
5138 if (list == NULL)
5139 return -1;
5140 PDATA_PUSH(self->stack, list, -1);
5141 return 0;
5142}
5143
5144static int
5145load_dict(UnpicklerObject *self)
5146{
5147 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005148 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005149
5150 if ((i = marker(self)) < 0)
5151 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005152 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005153
5154 if ((dict = PyDict_New()) == NULL)
5155 return -1;
5156
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005157 if ((j - i) % 2 != 0) {
5158 PickleState *st = _Pickle_GetGlobalState();
5159 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005160 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005161 return -1;
5162 }
5163
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005164 for (k = i + 1; k < j; k += 2) {
5165 key = self->stack->data[k - 1];
5166 value = self->stack->data[k];
5167 if (PyDict_SetItem(dict, key, value) < 0) {
5168 Py_DECREF(dict);
5169 return -1;
5170 }
5171 }
5172 Pdata_clear(self->stack, i);
5173 PDATA_PUSH(self->stack, dict, -1);
5174 return 0;
5175}
5176
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005177static int
5178load_frozenset(UnpicklerObject *self)
5179{
5180 PyObject *items;
5181 PyObject *frozenset;
5182 Py_ssize_t i;
5183
5184 if ((i = marker(self)) < 0)
5185 return -1;
5186
5187 items = Pdata_poptuple(self->stack, i);
5188 if (items == NULL)
5189 return -1;
5190
5191 frozenset = PyFrozenSet_New(items);
5192 Py_DECREF(items);
5193 if (frozenset == NULL)
5194 return -1;
5195
5196 PDATA_PUSH(self->stack, frozenset, -1);
5197 return 0;
5198}
5199
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005200static PyObject *
5201instantiate(PyObject *cls, PyObject *args)
5202{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005203 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005204 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005205 /* Caller must assure args are a tuple. Normally, args come from
5206 Pdata_poptuple which packs objects from the top of the stack
5207 into a newly created tuple. */
5208 assert(PyTuple_Check(args));
5209 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005210 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005211 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005212 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005213 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005214 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005215
5216 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005217 }
5218 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005219}
5220
5221static int
5222load_obj(UnpicklerObject *self)
5223{
5224 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005225 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005226
5227 if ((i = marker(self)) < 0)
5228 return -1;
5229
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005230 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005231 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005232
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005233 args = Pdata_poptuple(self->stack, i + 1);
5234 if (args == NULL)
5235 return -1;
5236
5237 PDATA_POP(self->stack, cls);
5238 if (cls) {
5239 obj = instantiate(cls, args);
5240 Py_DECREF(cls);
5241 }
5242 Py_DECREF(args);
5243 if (obj == NULL)
5244 return -1;
5245
5246 PDATA_PUSH(self->stack, obj, -1);
5247 return 0;
5248}
5249
5250static int
5251load_inst(UnpicklerObject *self)
5252{
5253 PyObject *cls = NULL;
5254 PyObject *args = NULL;
5255 PyObject *obj = NULL;
5256 PyObject *module_name;
5257 PyObject *class_name;
5258 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005259 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005260 char *s;
5261
5262 if ((i = marker(self)) < 0)
5263 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005264 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005265 return -1;
5266 if (len < 2)
5267 return bad_readline();
5268
5269 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5270 identifiers are permitted in Python 3.0, since the INST opcode is only
5271 supported by older protocols on Python 2.x. */
5272 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5273 if (module_name == NULL)
5274 return -1;
5275
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005276 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005277 if (len < 2) {
5278 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005279 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005280 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005281 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005282 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005283 cls = find_class(self, module_name, class_name);
5284 Py_DECREF(class_name);
5285 }
5286 }
5287 Py_DECREF(module_name);
5288
5289 if (cls == NULL)
5290 return -1;
5291
5292 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5293 obj = instantiate(cls, args);
5294 Py_DECREF(args);
5295 }
5296 Py_DECREF(cls);
5297
5298 if (obj == NULL)
5299 return -1;
5300
5301 PDATA_PUSH(self->stack, obj, -1);
5302 return 0;
5303}
5304
5305static int
5306load_newobj(UnpicklerObject *self)
5307{
5308 PyObject *args = NULL;
5309 PyObject *clsraw = NULL;
5310 PyTypeObject *cls; /* clsraw cast to its true type */
5311 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005312 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005313
5314 /* Stack is ... cls argtuple, and we want to call
5315 * cls.__new__(cls, *argtuple).
5316 */
5317 PDATA_POP(self->stack, args);
5318 if (args == NULL)
5319 goto error;
5320 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005321 PyErr_SetString(st->UnpicklingError,
5322 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005323 goto error;
5324 }
5325
5326 PDATA_POP(self->stack, clsraw);
5327 cls = (PyTypeObject *)clsraw;
5328 if (cls == NULL)
5329 goto error;
5330 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005331 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005332 "isn't a type object");
5333 goto error;
5334 }
5335 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005336 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005337 "has NULL tp_new");
5338 goto error;
5339 }
5340
5341 /* Call __new__. */
5342 obj = cls->tp_new(cls, args, NULL);
5343 if (obj == NULL)
5344 goto error;
5345
5346 Py_DECREF(args);
5347 Py_DECREF(clsraw);
5348 PDATA_PUSH(self->stack, obj, -1);
5349 return 0;
5350
5351 error:
5352 Py_XDECREF(args);
5353 Py_XDECREF(clsraw);
5354 return -1;
5355}
5356
5357static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005358load_newobj_ex(UnpicklerObject *self)
5359{
5360 PyObject *cls, *args, *kwargs;
5361 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005362 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005363
5364 PDATA_POP(self->stack, kwargs);
5365 if (kwargs == NULL) {
5366 return -1;
5367 }
5368 PDATA_POP(self->stack, args);
5369 if (args == NULL) {
5370 Py_DECREF(kwargs);
5371 return -1;
5372 }
5373 PDATA_POP(self->stack, cls);
5374 if (cls == NULL) {
5375 Py_DECREF(kwargs);
5376 Py_DECREF(args);
5377 return -1;
5378 }
Larry Hastings61272b72014-01-07 12:41:53 -08005379
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005380 if (!PyType_Check(cls)) {
5381 Py_DECREF(kwargs);
5382 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005383 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005384 "NEWOBJ_EX class argument must be a type, not %.200s",
5385 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005386 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005387 return -1;
5388 }
5389
5390 if (((PyTypeObject *)cls)->tp_new == NULL) {
5391 Py_DECREF(kwargs);
5392 Py_DECREF(args);
5393 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005394 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005395 "NEWOBJ_EX class argument doesn't have __new__");
5396 return -1;
5397 }
5398 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5399 Py_DECREF(kwargs);
5400 Py_DECREF(args);
5401 Py_DECREF(cls);
5402 if (obj == NULL) {
5403 return -1;
5404 }
5405 PDATA_PUSH(self->stack, obj, -1);
5406 return 0;
5407}
5408
5409static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005410load_global(UnpicklerObject *self)
5411{
5412 PyObject *global = NULL;
5413 PyObject *module_name;
5414 PyObject *global_name;
5415 Py_ssize_t len;
5416 char *s;
5417
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005418 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005419 return -1;
5420 if (len < 2)
5421 return bad_readline();
5422 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5423 if (!module_name)
5424 return -1;
5425
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005426 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005427 if (len < 2) {
5428 Py_DECREF(module_name);
5429 return bad_readline();
5430 }
5431 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5432 if (global_name) {
5433 global = find_class(self, module_name, global_name);
5434 Py_DECREF(global_name);
5435 }
5436 }
5437 Py_DECREF(module_name);
5438
5439 if (global == NULL)
5440 return -1;
5441 PDATA_PUSH(self->stack, global, -1);
5442 return 0;
5443}
5444
5445static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005446load_stack_global(UnpicklerObject *self)
5447{
5448 PyObject *global;
5449 PyObject *module_name;
5450 PyObject *global_name;
5451
5452 PDATA_POP(self->stack, global_name);
5453 PDATA_POP(self->stack, module_name);
5454 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5455 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005456 PickleState *st = _Pickle_GetGlobalState();
5457 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005458 Py_XDECREF(global_name);
5459 Py_XDECREF(module_name);
5460 return -1;
5461 }
5462 global = find_class(self, module_name, global_name);
5463 Py_DECREF(global_name);
5464 Py_DECREF(module_name);
5465 if (global == NULL)
5466 return -1;
5467 PDATA_PUSH(self->stack, global, -1);
5468 return 0;
5469}
5470
5471static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005472load_persid(UnpicklerObject *self)
5473{
5474 PyObject *pid;
5475 Py_ssize_t len;
5476 char *s;
5477
5478 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005479 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005480 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005481 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005482 return bad_readline();
5483
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005484 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5485 if (pid == NULL) {
5486 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5487 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5488 "persistent IDs in protocol 0 must be "
5489 "ASCII strings");
5490 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005491 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005492 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005493
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005494 /* This does not leak since _Pickle_FastCall() steals the reference
5495 to pid first. */
5496 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005497 if (pid == NULL)
5498 return -1;
5499
5500 PDATA_PUSH(self->stack, pid, -1);
5501 return 0;
5502 }
5503 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005504 PickleState *st = _Pickle_GetGlobalState();
5505 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005506 "A load persistent id instruction was encountered,\n"
5507 "but no persistent_load function was specified.");
5508 return -1;
5509 }
5510}
5511
5512static int
5513load_binpersid(UnpicklerObject *self)
5514{
5515 PyObject *pid;
5516
5517 if (self->pers_func) {
5518 PDATA_POP(self->stack, pid);
5519 if (pid == NULL)
5520 return -1;
5521
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005522 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005523 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005524 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005525 if (pid == NULL)
5526 return -1;
5527
5528 PDATA_PUSH(self->stack, pid, -1);
5529 return 0;
5530 }
5531 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005532 PickleState *st = _Pickle_GetGlobalState();
5533 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005534 "A load persistent id instruction was encountered,\n"
5535 "but no persistent_load function was specified.");
5536 return -1;
5537 }
5538}
5539
5540static int
5541load_pop(UnpicklerObject *self)
5542{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005543 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005544
5545 /* Note that we split the (pickle.py) stack into two stacks,
5546 * an object stack and a mark stack. We have to be clever and
5547 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005548 * mark stack first, and only signalling a stack underflow if
5549 * the object stack is empty and the mark stack doesn't match
5550 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005551 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005552 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005554 self->stack->mark_set = self->num_marks != 0;
5555 self->stack->fence = self->num_marks ?
5556 self->marks[self->num_marks - 1] : 0;
5557 } else if (len <= self->stack->fence)
5558 return Pdata_stack_underflow(self->stack);
5559 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560 len--;
5561 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005562 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005563 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005564 return 0;
5565}
5566
5567static int
5568load_pop_mark(UnpicklerObject *self)
5569{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005570 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005571
5572 if ((i = marker(self)) < 0)
5573 return -1;
5574
5575 Pdata_clear(self->stack, i);
5576
5577 return 0;
5578}
5579
5580static int
5581load_dup(UnpicklerObject *self)
5582{
5583 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005584 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005585
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005586 if (len <= self->stack->fence)
5587 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005588 last = self->stack->data[len - 1];
5589 PDATA_APPEND(self->stack, last, -1);
5590 return 0;
5591}
5592
5593static int
5594load_get(UnpicklerObject *self)
5595{
5596 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005597 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005598 Py_ssize_t len;
5599 char *s;
5600
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005601 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005602 return -1;
5603 if (len < 2)
5604 return bad_readline();
5605
5606 key = PyLong_FromString(s, NULL, 10);
5607 if (key == NULL)
5608 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005609 idx = PyLong_AsSsize_t(key);
5610 if (idx == -1 && PyErr_Occurred()) {
5611 Py_DECREF(key);
5612 return -1;
5613 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005614
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005615 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 if (value == NULL) {
5617 if (!PyErr_Occurred())
5618 PyErr_SetObject(PyExc_KeyError, key);
5619 Py_DECREF(key);
5620 return -1;
5621 }
5622 Py_DECREF(key);
5623
5624 PDATA_APPEND(self->stack, value, -1);
5625 return 0;
5626}
5627
5628static int
5629load_binget(UnpicklerObject *self)
5630{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005631 PyObject *value;
5632 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633 char *s;
5634
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005635 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005636 return -1;
5637
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005638 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005639
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005640 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005641 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005642 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005643 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005645 Py_DECREF(key);
5646 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005647 return -1;
5648 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649
5650 PDATA_APPEND(self->stack, value, -1);
5651 return 0;
5652}
5653
5654static int
5655load_long_binget(UnpicklerObject *self)
5656{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005657 PyObject *value;
5658 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005659 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005660
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005661 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662 return -1;
5663
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005664 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005665
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005666 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005667 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005668 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005669 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005670 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005671 Py_DECREF(key);
5672 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005673 return -1;
5674 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005675
5676 PDATA_APPEND(self->stack, value, -1);
5677 return 0;
5678}
5679
5680/* Push an object from the extension registry (EXT[124]). nbytes is
5681 * the number of bytes following the opcode, holding the index (code) value.
5682 */
5683static int
5684load_extension(UnpicklerObject *self, int nbytes)
5685{
5686 char *codebytes; /* the nbytes bytes after the opcode */
5687 long code; /* calc_binint returns long */
5688 PyObject *py_code; /* code as a Python int */
5689 PyObject *obj; /* the object to push */
5690 PyObject *pair; /* (module_name, class_name) */
5691 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005692 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005693
5694 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005695 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696 return -1;
5697 code = calc_binint(codebytes, nbytes);
5698 if (code <= 0) { /* note that 0 is forbidden */
5699 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005700 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005701 return -1;
5702 }
5703
5704 /* Look for the code in the cache. */
5705 py_code = PyLong_FromLong(code);
5706 if (py_code == NULL)
5707 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005708 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709 if (obj != NULL) {
5710 /* Bingo. */
5711 Py_DECREF(py_code);
5712 PDATA_APPEND(self->stack, obj, -1);
5713 return 0;
5714 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005715 if (PyErr_Occurred()) {
5716 Py_DECREF(py_code);
5717 return -1;
5718 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005719
5720 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005721 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005722 if (pair == NULL) {
5723 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005724 if (!PyErr_Occurred()) {
5725 PyErr_Format(PyExc_ValueError, "unregistered extension "
5726 "code %ld", code);
5727 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005728 return -1;
5729 }
5730 /* Since the extension registry is manipulable via Python code,
5731 * confirm that pair is really a 2-tuple of strings.
5732 */
5733 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5734 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5735 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5736 Py_DECREF(py_code);
5737 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5738 "isn't a 2-tuple of strings", code);
5739 return -1;
5740 }
5741 /* Load the object. */
5742 obj = find_class(self, module_name, class_name);
5743 if (obj == NULL) {
5744 Py_DECREF(py_code);
5745 return -1;
5746 }
5747 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005748 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005749 Py_DECREF(py_code);
5750 if (code < 0) {
5751 Py_DECREF(obj);
5752 return -1;
5753 }
5754 PDATA_PUSH(self->stack, obj, -1);
5755 return 0;
5756}
5757
5758static int
5759load_put(UnpicklerObject *self)
5760{
5761 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005762 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763 Py_ssize_t len;
5764 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767 return -1;
5768 if (len < 2)
5769 return bad_readline();
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
5774 key = PyLong_FromString(s, NULL, 10);
5775 if (key == NULL)
5776 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005777 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005778 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005779 if (idx < 0) {
5780 if (!PyErr_Occurred())
5781 PyErr_SetString(PyExc_ValueError,
5782 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005783 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005784 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785
5786 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005787}
5788
5789static int
5790load_binput(UnpicklerObject *self)
5791{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005792 PyObject *value;
5793 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005794 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005795
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005796 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005798
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005799 if (Py_SIZE(self->stack) <= self->stack->fence)
5800 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005801 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005802
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005803 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005804
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005805 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005806}
5807
5808static int
5809load_long_binput(UnpicklerObject *self)
5810{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005811 PyObject *value;
5812 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005813 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005814
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005815 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005816 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005817
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005818 if (Py_SIZE(self->stack) <= self->stack->fence)
5819 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005820 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005821
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005822 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005823 if (idx < 0) {
5824 PyErr_SetString(PyExc_ValueError,
5825 "negative LONG_BINPUT argument");
5826 return -1;
5827 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005828
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005829 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005830}
5831
5832static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005833load_memoize(UnpicklerObject *self)
5834{
5835 PyObject *value;
5836
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005837 if (Py_SIZE(self->stack) <= self->stack->fence)
5838 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005839 value = self->stack->data[Py_SIZE(self->stack) - 1];
5840
5841 return _Unpickler_MemoPut(self, self->memo_len, value);
5842}
5843
5844static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005845do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846{
5847 PyObject *value;
5848 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005849 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005850
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005851 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005852 if (x > len || x <= self->stack->fence)
5853 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005854 if (len == x) /* nothing to do */
5855 return 0;
5856
5857 list = self->stack->data[x - 1];
5858
5859 if (PyList_Check(list)) {
5860 PyObject *slice;
5861 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005862 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005863
5864 slice = Pdata_poplist(self->stack, x);
5865 if (!slice)
5866 return -1;
5867 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005868 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005869 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005870 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005871 }
5872 else {
5873 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005874 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005875
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005876 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005877 if (append_func == NULL)
5878 return -1;
5879 for (i = x; i < len; i++) {
5880 PyObject *result;
5881
5882 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005883 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005884 if (result == NULL) {
5885 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005886 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005887 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005888 return -1;
5889 }
5890 Py_DECREF(result);
5891 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005892 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005893 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005894 }
5895
5896 return 0;
5897}
5898
5899static int
5900load_append(UnpicklerObject *self)
5901{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005902 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5903 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005904 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005905}
5906
5907static int
5908load_appends(UnpicklerObject *self)
5909{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005910 Py_ssize_t i = marker(self);
5911 if (i < 0)
5912 return -1;
5913 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005914}
5915
5916static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005917do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005918{
5919 PyObject *value, *key;
5920 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005921 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005922 int status = 0;
5923
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005924 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005925 if (x > len || x <= self->stack->fence)
5926 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005927 if (len == x) /* nothing to do */
5928 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005929 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005930 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005931 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005932 PyErr_SetString(st->UnpicklingError,
5933 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005934 return -1;
5935 }
5936
5937 /* Here, dict does not actually need to be a PyDict; it could be anything
5938 that supports the __setitem__ attribute. */
5939 dict = self->stack->data[x - 1];
5940
5941 for (i = x + 1; i < len; i += 2) {
5942 key = self->stack->data[i - 1];
5943 value = self->stack->data[i];
5944 if (PyObject_SetItem(dict, key, value) < 0) {
5945 status = -1;
5946 break;
5947 }
5948 }
5949
5950 Pdata_clear(self->stack, x);
5951 return status;
5952}
5953
5954static int
5955load_setitem(UnpicklerObject *self)
5956{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005957 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005958}
5959
5960static int
5961load_setitems(UnpicklerObject *self)
5962{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005963 Py_ssize_t i = marker(self);
5964 if (i < 0)
5965 return -1;
5966 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005967}
5968
5969static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005970load_additems(UnpicklerObject *self)
5971{
5972 PyObject *set;
5973 Py_ssize_t mark, len, i;
5974
5975 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005976 if (mark < 0)
5977 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005978 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005979 if (mark > len || mark <= self->stack->fence)
5980 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005981 if (len == mark) /* nothing to do */
5982 return 0;
5983
5984 set = self->stack->data[mark - 1];
5985
5986 if (PySet_Check(set)) {
5987 PyObject *items;
5988 int status;
5989
5990 items = Pdata_poptuple(self->stack, mark);
5991 if (items == NULL)
5992 return -1;
5993
5994 status = _PySet_Update(set, items);
5995 Py_DECREF(items);
5996 return status;
5997 }
5998 else {
5999 PyObject *add_func;
6000 _Py_IDENTIFIER(add);
6001
6002 add_func = _PyObject_GetAttrId(set, &PyId_add);
6003 if (add_func == NULL)
6004 return -1;
6005 for (i = mark; i < len; i++) {
6006 PyObject *result;
6007 PyObject *item;
6008
6009 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006010 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006011 if (result == NULL) {
6012 Pdata_clear(self->stack, i + 1);
6013 Py_SIZE(self->stack) = mark;
6014 return -1;
6015 }
6016 Py_DECREF(result);
6017 }
6018 Py_SIZE(self->stack) = mark;
6019 }
6020
6021 return 0;
6022}
6023
6024static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006025load_build(UnpicklerObject *self)
6026{
6027 PyObject *state, *inst, *slotstate;
6028 PyObject *setstate;
6029 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006030 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006031
6032 /* Stack is ... instance, state. We want to leave instance at
6033 * the stack top, possibly mutated via instance.__setstate__(state).
6034 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006035 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6036 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006037
6038 PDATA_POP(self->stack, state);
6039 if (state == NULL)
6040 return -1;
6041
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006042 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006043
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006044 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006045 if (setstate == NULL) {
6046 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6047 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006048 else {
6049 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006050 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006051 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006052 }
6053 else {
6054 PyObject *result;
6055
6056 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006057 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006058 Py_DECREF(setstate);
6059 if (result == NULL)
6060 return -1;
6061 Py_DECREF(result);
6062 return 0;
6063 }
6064
6065 /* A default __setstate__. First see whether state embeds a
6066 * slot state dict too (a proto 2 addition).
6067 */
6068 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6069 PyObject *tmp = state;
6070
6071 state = PyTuple_GET_ITEM(tmp, 0);
6072 slotstate = PyTuple_GET_ITEM(tmp, 1);
6073 Py_INCREF(state);
6074 Py_INCREF(slotstate);
6075 Py_DECREF(tmp);
6076 }
6077 else
6078 slotstate = NULL;
6079
6080 /* Set inst.__dict__ from the state dict (if any). */
6081 if (state != Py_None) {
6082 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006083 PyObject *d_key, *d_value;
6084 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006085 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006086
6087 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006088 PickleState *st = _Pickle_GetGlobalState();
6089 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006090 goto error;
6091 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006092 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006093 if (dict == NULL)
6094 goto error;
6095
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006096 i = 0;
6097 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6098 /* normally the keys for instance attributes are
6099 interned. we should try to do that here. */
6100 Py_INCREF(d_key);
6101 if (PyUnicode_CheckExact(d_key))
6102 PyUnicode_InternInPlace(&d_key);
6103 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6104 Py_DECREF(d_key);
6105 goto error;
6106 }
6107 Py_DECREF(d_key);
6108 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006109 Py_DECREF(dict);
6110 }
6111
6112 /* Also set instance attributes from the slotstate dict (if any). */
6113 if (slotstate != NULL) {
6114 PyObject *d_key, *d_value;
6115 Py_ssize_t i;
6116
6117 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006118 PickleState *st = _Pickle_GetGlobalState();
6119 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006120 "slot state is not a dictionary");
6121 goto error;
6122 }
6123 i = 0;
6124 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6125 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6126 goto error;
6127 }
6128 }
6129
6130 if (0) {
6131 error:
6132 status = -1;
6133 }
6134
6135 Py_DECREF(state);
6136 Py_XDECREF(slotstate);
6137 return status;
6138}
6139
6140static int
6141load_mark(UnpicklerObject *self)
6142{
6143
6144 /* Note that we split the (pickle.py) stack into two stacks, an
6145 * object stack and a mark stack. Here we push a mark onto the
6146 * mark stack.
6147 */
6148
6149 if ((self->num_marks + 1) >= self->marks_size) {
6150 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006151
6152 /* Use the size_t type to check for overflow. */
6153 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006154 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006155 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006156 PyErr_NoMemory();
6157 return -1;
6158 }
6159
6160 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006161 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006162 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006163 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6164 if (self->marks == NULL) {
6165 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006166 PyErr_NoMemory();
6167 return -1;
6168 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006169 self->marks_size = (Py_ssize_t)alloc;
6170 }
6171
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006172 self->stack->mark_set = 1;
6173 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006174
6175 return 0;
6176}
6177
6178static int
6179load_reduce(UnpicklerObject *self)
6180{
6181 PyObject *callable = NULL;
6182 PyObject *argtup = NULL;
6183 PyObject *obj = NULL;
6184
6185 PDATA_POP(self->stack, argtup);
6186 if (argtup == NULL)
6187 return -1;
6188 PDATA_POP(self->stack, callable);
6189 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006190 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006191 Py_DECREF(callable);
6192 }
6193 Py_DECREF(argtup);
6194
6195 if (obj == NULL)
6196 return -1;
6197
6198 PDATA_PUSH(self->stack, obj, -1);
6199 return 0;
6200}
6201
6202/* Just raises an error if we don't know the protocol specified. PROTO
6203 * is the first opcode for protocols >= 2.
6204 */
6205static int
6206load_proto(UnpicklerObject *self)
6207{
6208 char *s;
6209 int i;
6210
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006211 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006212 return -1;
6213
6214 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006215 if (i <= HIGHEST_PROTOCOL) {
6216 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006217 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006218 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006219
6220 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6221 return -1;
6222}
6223
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006224static int
6225load_frame(UnpicklerObject *self)
6226{
6227 char *s;
6228 Py_ssize_t frame_len;
6229
6230 if (_Unpickler_Read(self, &s, 8) < 0)
6231 return -1;
6232
6233 frame_len = calc_binsize(s, 8);
6234 if (frame_len < 0) {
6235 PyErr_Format(PyExc_OverflowError,
6236 "FRAME length exceeds system's maximum of %zd bytes",
6237 PY_SSIZE_T_MAX);
6238 return -1;
6239 }
6240
6241 if (_Unpickler_Read(self, &s, frame_len) < 0)
6242 return -1;
6243
6244 /* Rewind to start of frame */
6245 self->next_read_idx -= frame_len;
6246 return 0;
6247}
6248
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006249static PyObject *
6250load(UnpicklerObject *self)
6251{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006252 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006253 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006254
6255 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006256 self->stack->mark_set = 0;
6257 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006258 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006259 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006260 Pdata_clear(self->stack, 0);
6261
6262 /* Convenient macros for the dispatch while-switch loop just below. */
6263#define OP(opcode, load_func) \
6264 case opcode: if (load_func(self) < 0) break; continue;
6265
6266#define OP_ARG(opcode, load_func, arg) \
6267 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6268
6269 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006270 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006271 break;
6272
6273 switch ((enum opcode)s[0]) {
6274 OP(NONE, load_none)
6275 OP(BININT, load_binint)
6276 OP(BININT1, load_binint1)
6277 OP(BININT2, load_binint2)
6278 OP(INT, load_int)
6279 OP(LONG, load_long)
6280 OP_ARG(LONG1, load_counted_long, 1)
6281 OP_ARG(LONG4, load_counted_long, 4)
6282 OP(FLOAT, load_float)
6283 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006284 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6285 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6286 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6287 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6288 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006289 OP(STRING, load_string)
6290 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006291 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6292 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6293 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006294 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6295 OP_ARG(TUPLE1, load_counted_tuple, 1)
6296 OP_ARG(TUPLE2, load_counted_tuple, 2)
6297 OP_ARG(TUPLE3, load_counted_tuple, 3)
6298 OP(TUPLE, load_tuple)
6299 OP(EMPTY_LIST, load_empty_list)
6300 OP(LIST, load_list)
6301 OP(EMPTY_DICT, load_empty_dict)
6302 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006303 OP(EMPTY_SET, load_empty_set)
6304 OP(ADDITEMS, load_additems)
6305 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006306 OP(OBJ, load_obj)
6307 OP(INST, load_inst)
6308 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006309 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006310 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006311 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006312 OP(APPEND, load_append)
6313 OP(APPENDS, load_appends)
6314 OP(BUILD, load_build)
6315 OP(DUP, load_dup)
6316 OP(BINGET, load_binget)
6317 OP(LONG_BINGET, load_long_binget)
6318 OP(GET, load_get)
6319 OP(MARK, load_mark)
6320 OP(BINPUT, load_binput)
6321 OP(LONG_BINPUT, load_long_binput)
6322 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006323 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006324 OP(POP, load_pop)
6325 OP(POP_MARK, load_pop_mark)
6326 OP(SETITEM, load_setitem)
6327 OP(SETITEMS, load_setitems)
6328 OP(PERSID, load_persid)
6329 OP(BINPERSID, load_binpersid)
6330 OP(REDUCE, load_reduce)
6331 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006332 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006333 OP_ARG(EXT1, load_extension, 1)
6334 OP_ARG(EXT2, load_extension, 2)
6335 OP_ARG(EXT4, load_extension, 4)
6336 OP_ARG(NEWTRUE, load_bool, Py_True)
6337 OP_ARG(NEWFALSE, load_bool, Py_False)
6338
6339 case STOP:
6340 break;
6341
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006342 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006343 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006344 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006345 }
6346 else {
6347 PickleState *st = _Pickle_GetGlobalState();
6348 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006349 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006350 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006351 return NULL;
6352 }
6353
6354 break; /* and we are done! */
6355 }
6356
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006357 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006358 return NULL;
6359 }
6360
Victor Stinner2ae57e32013-10-31 13:39:23 +01006361 if (_Unpickler_SkipConsumed(self) < 0)
6362 return NULL;
6363
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006364 PDATA_POP(self->stack, value);
6365 return value;
6366}
6367
Larry Hastings61272b72014-01-07 12:41:53 -08006368/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006369
6370_pickle.Unpickler.load
6371
6372Load a pickle.
6373
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006374Read a pickled object representation from the open file object given
6375in the constructor, and return the reconstituted object hierarchy
6376specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006377[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006378
Larry Hastings3cceb382014-01-04 11:09:09 -08006379static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006380_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006381/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006382{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006383 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006384
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006385 /* Check whether the Unpickler was initialized correctly. This prevents
6386 segfaulting if a subclass overridden __init__ with a function that does
6387 not call Unpickler.__init__(). Here, we simply ensure that self->read
6388 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006389 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006390 PickleState *st = _Pickle_GetGlobalState();
6391 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006392 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006393 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006394 return NULL;
6395 }
6396
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006397 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006398}
6399
6400/* The name of find_class() is misleading. In newer pickle protocols, this
6401 function is used for loading any global (i.e., functions), not just
6402 classes. The name is kept only for backward compatibility. */
6403
Larry Hastings61272b72014-01-07 12:41:53 -08006404/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006405
6406_pickle.Unpickler.find_class
6407
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006408 module_name: object
6409 global_name: object
6410 /
6411
6412Return an object from a specified module.
6413
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006414If necessary, the module will be imported. Subclasses may override
6415this method (e.g. to restrict unpickling of arbitrary classes and
6416functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006417
6418This method is called whenever a class or a function object is
6419needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006420[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006421
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006422static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006423_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6424 PyObject *module_name,
6425 PyObject *global_name)
6426/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006427{
6428 PyObject *global;
6429 PyObject *modules_dict;
6430 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006431 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006432
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006433 /* Try to map the old names used in Python 2.x to the new ones used in
6434 Python 3.x. We do this only with old pickle protocols and when the
6435 user has not disabled the feature. */
6436 if (self->proto < 3 && self->fix_imports) {
6437 PyObject *key;
6438 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006439 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006440
6441 /* Check if the global (i.e., a function or a class) was renamed
6442 or moved to another module. */
6443 key = PyTuple_Pack(2, module_name, global_name);
6444 if (key == NULL)
6445 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006446 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006447 Py_DECREF(key);
6448 if (item) {
6449 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6450 PyErr_Format(PyExc_RuntimeError,
6451 "_compat_pickle.NAME_MAPPING values should be "
6452 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6453 return NULL;
6454 }
6455 module_name = PyTuple_GET_ITEM(item, 0);
6456 global_name = PyTuple_GET_ITEM(item, 1);
6457 if (!PyUnicode_Check(module_name) ||
6458 !PyUnicode_Check(global_name)) {
6459 PyErr_Format(PyExc_RuntimeError,
6460 "_compat_pickle.NAME_MAPPING values should be "
6461 "pairs of str, not (%.200s, %.200s)",
6462 Py_TYPE(module_name)->tp_name,
6463 Py_TYPE(global_name)->tp_name);
6464 return NULL;
6465 }
6466 }
6467 else if (PyErr_Occurred()) {
6468 return NULL;
6469 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006470 else {
6471 /* Check if the module was renamed. */
6472 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6473 if (item) {
6474 if (!PyUnicode_Check(item)) {
6475 PyErr_Format(PyExc_RuntimeError,
6476 "_compat_pickle.IMPORT_MAPPING values should be "
6477 "strings, not %.200s", Py_TYPE(item)->tp_name);
6478 return NULL;
6479 }
6480 module_name = item;
6481 }
6482 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006483 return NULL;
6484 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006485 }
6486 }
6487
Victor Stinnerbb520202013-11-06 22:40:41 +01006488 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006489 if (modules_dict == NULL) {
6490 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006491 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006492 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006493
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006494 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006495 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006496 if (PyErr_Occurred())
6497 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006498 module = PyImport_Import(module_name);
6499 if (module == NULL)
6500 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006501 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006502 Py_DECREF(module);
6503 }
Victor Stinner121aab42011-09-29 23:40:53 +02006504 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006505 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006506 }
6507 return global;
6508}
6509
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006510/*[clinic input]
6511
6512_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6513
6514Returns size in memory, in bytes.
6515[clinic start generated code]*/
6516
6517static Py_ssize_t
6518_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6519/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6520{
6521 Py_ssize_t res;
6522
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006523 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006524 if (self->memo != NULL)
6525 res += self->memo_size * sizeof(PyObject *);
6526 if (self->marks != NULL)
6527 res += self->marks_size * sizeof(Py_ssize_t);
6528 if (self->input_line != NULL)
6529 res += strlen(self->input_line) + 1;
6530 if (self->encoding != NULL)
6531 res += strlen(self->encoding) + 1;
6532 if (self->errors != NULL)
6533 res += strlen(self->errors) + 1;
6534 return res;
6535}
6536
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006537static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006538 _PICKLE_UNPICKLER_LOAD_METHODDEF
6539 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006540 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006541 {NULL, NULL} /* sentinel */
6542};
6543
6544static void
6545Unpickler_dealloc(UnpicklerObject *self)
6546{
6547 PyObject_GC_UnTrack((PyObject *)self);
6548 Py_XDECREF(self->readline);
6549 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006550 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006551 Py_XDECREF(self->stack);
6552 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006553 if (self->buffer.buf != NULL) {
6554 PyBuffer_Release(&self->buffer);
6555 self->buffer.buf = NULL;
6556 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006557
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006558 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006559 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006560 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006561 PyMem_Free(self->encoding);
6562 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006563
6564 Py_TYPE(self)->tp_free((PyObject *)self);
6565}
6566
6567static int
6568Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6569{
6570 Py_VISIT(self->readline);
6571 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006572 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006573 Py_VISIT(self->stack);
6574 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006575 return 0;
6576}
6577
6578static int
6579Unpickler_clear(UnpicklerObject *self)
6580{
6581 Py_CLEAR(self->readline);
6582 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006583 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006584 Py_CLEAR(self->stack);
6585 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006586 if (self->buffer.buf != NULL) {
6587 PyBuffer_Release(&self->buffer);
6588 self->buffer.buf = NULL;
6589 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006590
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006591 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006592 PyMem_Free(self->marks);
6593 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006594 PyMem_Free(self->input_line);
6595 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006596 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006597 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006598 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006599 self->errors = NULL;
6600
6601 return 0;
6602}
6603
Larry Hastings61272b72014-01-07 12:41:53 -08006604/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006605
6606_pickle.Unpickler.__init__
6607
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006608 file: object
6609 *
6610 fix_imports: bool = True
6611 encoding: str = 'ASCII'
6612 errors: str = 'strict'
6613
6614This takes a binary file for reading a pickle data stream.
6615
6616The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006617protocol argument is needed. Bytes past the pickled object's
6618representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006619
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006620The argument *file* must have two methods, a read() method that takes
6621an integer argument, and a readline() method that requires no
6622arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006623binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006624other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006625
6626Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006627which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006628generated by Python 2. If *fix_imports* is True, pickle will try to
6629map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006630*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006631instances pickled by Python 2; these default to 'ASCII' and 'strict',
6632respectively. The *encoding* can be 'bytes' to read these 8-bit
6633string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006634[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006635
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006636static int
Larry Hastings89964c42015-04-14 18:07:59 -04006637_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6638 int fix_imports, const char *encoding,
6639 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006640/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006641{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006642 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006643
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006644 /* In case of multiple __init__() calls, clear previous content. */
6645 if (self->read != NULL)
6646 (void)Unpickler_clear(self);
6647
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006648 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006649 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006651 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006652 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006653
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006654 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006655 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006656 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006657
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006658 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006659 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6660 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006661 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006662 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006663 }
6664 else {
6665 self->pers_func = NULL;
6666 }
6667
6668 self->stack = (Pdata *)Pdata_New();
6669 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006670 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006671
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006672 self->memo_size = 32;
6673 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006674 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006675 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006676
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006677 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006678
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006679 return 0;
6680}
6681
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006682
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006683/* Define a proxy object for the Unpickler's internal memo object. This is to
6684 * avoid breaking code like:
6685 * unpickler.memo.clear()
6686 * and
6687 * unpickler.memo = saved_memo
6688 * Is this a good idea? Not really, but we don't want to break code that uses
6689 * it. Note that we don't implement the entire mapping API here. This is
6690 * intentional, as these should be treated as black-box implementation details.
6691 *
6692 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006693 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006694 */
6695
Larry Hastings61272b72014-01-07 12:41:53 -08006696/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006697_pickle.UnpicklerMemoProxy.clear
6698
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006699Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006700[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006701
Larry Hastings3cceb382014-01-04 11:09:09 -08006702static PyObject *
6703_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006704/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006705{
6706 _Unpickler_MemoCleanup(self->unpickler);
6707 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6708 if (self->unpickler->memo == NULL)
6709 return NULL;
6710 Py_RETURN_NONE;
6711}
6712
Larry Hastings61272b72014-01-07 12:41:53 -08006713/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006714_pickle.UnpicklerMemoProxy.copy
6715
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006716Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006717[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006718
Larry Hastings3cceb382014-01-04 11:09:09 -08006719static PyObject *
6720_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006721/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006722{
6723 Py_ssize_t i;
6724 PyObject *new_memo = PyDict_New();
6725 if (new_memo == NULL)
6726 return NULL;
6727
6728 for (i = 0; i < self->unpickler->memo_size; i++) {
6729 int status;
6730 PyObject *key, *value;
6731
6732 value = self->unpickler->memo[i];
6733 if (value == NULL)
6734 continue;
6735
6736 key = PyLong_FromSsize_t(i);
6737 if (key == NULL)
6738 goto error;
6739 status = PyDict_SetItem(new_memo, key, value);
6740 Py_DECREF(key);
6741 if (status < 0)
6742 goto error;
6743 }
6744 return new_memo;
6745
6746error:
6747 Py_DECREF(new_memo);
6748 return NULL;
6749}
6750
Larry Hastings61272b72014-01-07 12:41:53 -08006751/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006752_pickle.UnpicklerMemoProxy.__reduce__
6753
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006754Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006755[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006756
Larry Hastings3cceb382014-01-04 11:09:09 -08006757static PyObject *
6758_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006759/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006760{
6761 PyObject *reduce_value;
6762 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006763 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006764 if (contents == NULL)
6765 return NULL;
6766
6767 reduce_value = PyTuple_New(2);
6768 if (reduce_value == NULL) {
6769 Py_DECREF(contents);
6770 return NULL;
6771 }
6772 constructor_args = PyTuple_New(1);
6773 if (constructor_args == NULL) {
6774 Py_DECREF(contents);
6775 Py_DECREF(reduce_value);
6776 return NULL;
6777 }
6778 PyTuple_SET_ITEM(constructor_args, 0, contents);
6779 Py_INCREF((PyObject *)&PyDict_Type);
6780 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6781 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6782 return reduce_value;
6783}
6784
6785static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006786 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6787 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6788 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006789 {NULL, NULL} /* sentinel */
6790};
6791
6792static void
6793UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6794{
6795 PyObject_GC_UnTrack(self);
6796 Py_XDECREF(self->unpickler);
6797 PyObject_GC_Del((PyObject *)self);
6798}
6799
6800static int
6801UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6802 visitproc visit, void *arg)
6803{
6804 Py_VISIT(self->unpickler);
6805 return 0;
6806}
6807
6808static int
6809UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6810{
6811 Py_CLEAR(self->unpickler);
6812 return 0;
6813}
6814
6815static PyTypeObject UnpicklerMemoProxyType = {
6816 PyVarObject_HEAD_INIT(NULL, 0)
6817 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6818 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6819 0,
6820 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6821 0, /* tp_print */
6822 0, /* tp_getattr */
6823 0, /* tp_setattr */
6824 0, /* tp_compare */
6825 0, /* tp_repr */
6826 0, /* tp_as_number */
6827 0, /* tp_as_sequence */
6828 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006829 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006830 0, /* tp_call */
6831 0, /* tp_str */
6832 PyObject_GenericGetAttr, /* tp_getattro */
6833 PyObject_GenericSetAttr, /* tp_setattro */
6834 0, /* tp_as_buffer */
6835 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6836 0, /* tp_doc */
6837 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6838 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6839 0, /* tp_richcompare */
6840 0, /* tp_weaklistoffset */
6841 0, /* tp_iter */
6842 0, /* tp_iternext */
6843 unpicklerproxy_methods, /* tp_methods */
6844};
6845
6846static PyObject *
6847UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6848{
6849 UnpicklerMemoProxyObject *self;
6850
6851 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6852 &UnpicklerMemoProxyType);
6853 if (self == NULL)
6854 return NULL;
6855 Py_INCREF(unpickler);
6856 self->unpickler = unpickler;
6857 PyObject_GC_Track(self);
6858 return (PyObject *)self;
6859}
6860
6861/*****************************************************************************/
6862
6863
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006864static PyObject *
6865Unpickler_get_memo(UnpicklerObject *self)
6866{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006867 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006868}
6869
6870static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006871Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006872{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006873 PyObject **new_memo;
6874 Py_ssize_t new_memo_size = 0;
6875 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006876
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006877 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006878 PyErr_SetString(PyExc_TypeError,
6879 "attribute deletion is not supported");
6880 return -1;
6881 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006882
6883 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6884 UnpicklerObject *unpickler =
6885 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6886
6887 new_memo_size = unpickler->memo_size;
6888 new_memo = _Unpickler_NewMemo(new_memo_size);
6889 if (new_memo == NULL)
6890 return -1;
6891
6892 for (i = 0; i < new_memo_size; i++) {
6893 Py_XINCREF(unpickler->memo[i]);
6894 new_memo[i] = unpickler->memo[i];
6895 }
6896 }
6897 else if (PyDict_Check(obj)) {
6898 Py_ssize_t i = 0;
6899 PyObject *key, *value;
6900
6901 new_memo_size = PyDict_Size(obj);
6902 new_memo = _Unpickler_NewMemo(new_memo_size);
6903 if (new_memo == NULL)
6904 return -1;
6905
6906 while (PyDict_Next(obj, &i, &key, &value)) {
6907 Py_ssize_t idx;
6908 if (!PyLong_Check(key)) {
6909 PyErr_SetString(PyExc_TypeError,
6910 "memo key must be integers");
6911 goto error;
6912 }
6913 idx = PyLong_AsSsize_t(key);
6914 if (idx == -1 && PyErr_Occurred())
6915 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006916 if (idx < 0) {
6917 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006918 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006919 goto error;
6920 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006921 if (_Unpickler_MemoPut(self, idx, value) < 0)
6922 goto error;
6923 }
6924 }
6925 else {
6926 PyErr_Format(PyExc_TypeError,
6927 "'memo' attribute must be an UnpicklerMemoProxy object"
6928 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006929 return -1;
6930 }
6931
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006932 _Unpickler_MemoCleanup(self);
6933 self->memo_size = new_memo_size;
6934 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006935
6936 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006937
6938 error:
6939 if (new_memo_size) {
6940 i = new_memo_size;
6941 while (--i >= 0) {
6942 Py_XDECREF(new_memo[i]);
6943 }
6944 PyMem_FREE(new_memo);
6945 }
6946 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006947}
6948
6949static PyObject *
6950Unpickler_get_persload(UnpicklerObject *self)
6951{
6952 if (self->pers_func == NULL)
6953 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6954 else
6955 Py_INCREF(self->pers_func);
6956 return self->pers_func;
6957}
6958
6959static int
6960Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6961{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006962 if (value == NULL) {
6963 PyErr_SetString(PyExc_TypeError,
6964 "attribute deletion is not supported");
6965 return -1;
6966 }
6967 if (!PyCallable_Check(value)) {
6968 PyErr_SetString(PyExc_TypeError,
6969 "persistent_load must be a callable taking "
6970 "one argument");
6971 return -1;
6972 }
6973
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006974 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03006975 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006976
6977 return 0;
6978}
6979
6980static PyGetSetDef Unpickler_getsets[] = {
6981 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6982 {"persistent_load", (getter)Unpickler_get_persload,
6983 (setter)Unpickler_set_persload},
6984 {NULL}
6985};
6986
6987static PyTypeObject Unpickler_Type = {
6988 PyVarObject_HEAD_INIT(NULL, 0)
6989 "_pickle.Unpickler", /*tp_name*/
6990 sizeof(UnpicklerObject), /*tp_basicsize*/
6991 0, /*tp_itemsize*/
6992 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6993 0, /*tp_print*/
6994 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006995 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006996 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006997 0, /*tp_repr*/
6998 0, /*tp_as_number*/
6999 0, /*tp_as_sequence*/
7000 0, /*tp_as_mapping*/
7001 0, /*tp_hash*/
7002 0, /*tp_call*/
7003 0, /*tp_str*/
7004 0, /*tp_getattro*/
7005 0, /*tp_setattro*/
7006 0, /*tp_as_buffer*/
7007 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007008 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007009 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7010 (inquiry)Unpickler_clear, /*tp_clear*/
7011 0, /*tp_richcompare*/
7012 0, /*tp_weaklistoffset*/
7013 0, /*tp_iter*/
7014 0, /*tp_iternext*/
7015 Unpickler_methods, /*tp_methods*/
7016 0, /*tp_members*/
7017 Unpickler_getsets, /*tp_getset*/
7018 0, /*tp_base*/
7019 0, /*tp_dict*/
7020 0, /*tp_descr_get*/
7021 0, /*tp_descr_set*/
7022 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007023 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007024 PyType_GenericAlloc, /*tp_alloc*/
7025 PyType_GenericNew, /*tp_new*/
7026 PyObject_GC_Del, /*tp_free*/
7027 0, /*tp_is_gc*/
7028};
7029
Larry Hastings61272b72014-01-07 12:41:53 -08007030/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007031
7032_pickle.dump
7033
7034 obj: object
7035 file: object
7036 protocol: object = NULL
7037 *
7038 fix_imports: bool = True
7039
7040Write a pickled representation of obj to the open file object file.
7041
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007042This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7043be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007044
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007045The optional *protocol* argument tells the pickler to use the given
7046protocol supported protocols are 0, 1, 2, 3 and 4. The default
7047protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007048
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007049Specifying a negative protocol version selects the highest protocol
7050version supported. The higher the protocol used, the more recent the
7051version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007052
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007053The *file* argument must have a write() method that accepts a single
7054bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007055writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007056this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007057
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007058If *fix_imports* is True and protocol is less than 3, pickle will try
7059to map the new Python 3 names to the old module names used in Python
70602, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007061[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007062
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007063static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007064_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007065 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007066/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007067{
7068 PicklerObject *pickler = _Pickler_New();
7069
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007070 if (pickler == NULL)
7071 return NULL;
7072
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007073 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007074 goto error;
7075
7076 if (_Pickler_SetOutputStream(pickler, file) < 0)
7077 goto error;
7078
7079 if (dump(pickler, obj) < 0)
7080 goto error;
7081
7082 if (_Pickler_FlushToFile(pickler) < 0)
7083 goto error;
7084
7085 Py_DECREF(pickler);
7086 Py_RETURN_NONE;
7087
7088 error:
7089 Py_XDECREF(pickler);
7090 return NULL;
7091}
7092
Larry Hastings61272b72014-01-07 12:41:53 -08007093/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007094
7095_pickle.dumps
7096
7097 obj: object
7098 protocol: object = NULL
7099 *
7100 fix_imports: bool = True
7101
7102Return the pickled representation of the object as a bytes object.
7103
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007104The optional *protocol* argument tells the pickler to use the given
7105protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7106protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007107
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007108Specifying a negative protocol version selects the highest protocol
7109version supported. The higher the protocol used, the more recent the
7110version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007111
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007112If *fix_imports* is True and *protocol* is less than 3, pickle will
7113try to map the new Python 3 names to the old module names used in
7114Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007115[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007116
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007117static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007118_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007119 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007120/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007121{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007122 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007123 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007124
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007125 if (pickler == NULL)
7126 return NULL;
7127
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007128 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007129 goto error;
7130
7131 if (dump(pickler, obj) < 0)
7132 goto error;
7133
7134 result = _Pickler_GetString(pickler);
7135 Py_DECREF(pickler);
7136 return result;
7137
7138 error:
7139 Py_XDECREF(pickler);
7140 return NULL;
7141}
7142
Larry Hastings61272b72014-01-07 12:41:53 -08007143/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007144
7145_pickle.load
7146
7147 file: object
7148 *
7149 fix_imports: bool = True
7150 encoding: str = 'ASCII'
7151 errors: str = 'strict'
7152
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007153Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007154
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007155This is equivalent to ``Unpickler(file).load()``, but may be more
7156efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007157
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007158The protocol version of the pickle is detected automatically, so no
7159protocol argument is needed. Bytes past the pickled object's
7160representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007161
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007162The argument *file* must have two methods, a read() method that takes
7163an integer argument, and a readline() method that requires no
7164arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007165binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007166other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007167
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007168Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007169which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007170generated by Python 2. If *fix_imports* is True, pickle will try to
7171map the old Python 2 names to the new names used in Python 3. The
7172*encoding* and *errors* tell pickle how to decode 8-bit string
7173instances pickled by Python 2; these default to 'ASCII' and 'strict',
7174respectively. The *encoding* can be 'bytes' to read these 8-bit
7175string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007176[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007177
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007178static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007179_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007180 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007181/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007182{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007183 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007184 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007185
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007186 if (unpickler == NULL)
7187 return NULL;
7188
7189 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7190 goto error;
7191
7192 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7193 goto error;
7194
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007195 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007196
7197 result = load(unpickler);
7198 Py_DECREF(unpickler);
7199 return result;
7200
7201 error:
7202 Py_XDECREF(unpickler);
7203 return NULL;
7204}
7205
Larry Hastings61272b72014-01-07 12:41:53 -08007206/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007207
7208_pickle.loads
7209
7210 data: object
7211 *
7212 fix_imports: bool = True
7213 encoding: str = 'ASCII'
7214 errors: str = 'strict'
7215
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007216Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007217
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007218The protocol version of the pickle is detected automatically, so no
7219protocol argument is needed. Bytes past the pickled object's
7220representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007221
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007222Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007223which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007224generated by Python 2. If *fix_imports* is True, pickle will try to
7225map the old Python 2 names to the new names used in Python 3. The
7226*encoding* and *errors* tell pickle how to decode 8-bit string
7227instances pickled by Python 2; these default to 'ASCII' and 'strict',
7228respectively. The *encoding* can be 'bytes' to read these 8-bit
7229string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007230[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007231
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007232static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007233_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007234 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007235/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007236{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007237 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007238 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007239
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007240 if (unpickler == NULL)
7241 return NULL;
7242
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007243 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007244 goto error;
7245
7246 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7247 goto error;
7248
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007249 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007250
7251 result = load(unpickler);
7252 Py_DECREF(unpickler);
7253 return result;
7254
7255 error:
7256 Py_XDECREF(unpickler);
7257 return NULL;
7258}
7259
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007260static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007261 _PICKLE_DUMP_METHODDEF
7262 _PICKLE_DUMPS_METHODDEF
7263 _PICKLE_LOAD_METHODDEF
7264 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007265 {NULL, NULL} /* sentinel */
7266};
7267
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007268static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007269pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007270{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007271 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007272 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007273}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007274
Stefan Krahf483b0f2013-12-14 13:43:10 +01007275static void
7276pickle_free(PyObject *m)
7277{
7278 _Pickle_ClearState(_Pickle_GetState(m));
7279}
7280
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007281static int
7282pickle_traverse(PyObject *m, visitproc visit, void *arg)
7283{
7284 PickleState *st = _Pickle_GetState(m);
7285 Py_VISIT(st->PickleError);
7286 Py_VISIT(st->PicklingError);
7287 Py_VISIT(st->UnpicklingError);
7288 Py_VISIT(st->dispatch_table);
7289 Py_VISIT(st->extension_registry);
7290 Py_VISIT(st->extension_cache);
7291 Py_VISIT(st->inverted_registry);
7292 Py_VISIT(st->name_mapping_2to3);
7293 Py_VISIT(st->import_mapping_2to3);
7294 Py_VISIT(st->name_mapping_3to2);
7295 Py_VISIT(st->import_mapping_3to2);
7296 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007297 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007298 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007299}
7300
7301static struct PyModuleDef _picklemodule = {
7302 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007303 "_pickle", /* m_name */
7304 pickle_module_doc, /* m_doc */
7305 sizeof(PickleState), /* m_size */
7306 pickle_methods, /* m_methods */
7307 NULL, /* m_reload */
7308 pickle_traverse, /* m_traverse */
7309 pickle_clear, /* m_clear */
7310 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007311};
7312
7313PyMODINIT_FUNC
7314PyInit__pickle(void)
7315{
7316 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007317 PickleState *st;
7318
7319 m = PyState_FindModule(&_picklemodule);
7320 if (m) {
7321 Py_INCREF(m);
7322 return m;
7323 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007324
7325 if (PyType_Ready(&Unpickler_Type) < 0)
7326 return NULL;
7327 if (PyType_Ready(&Pickler_Type) < 0)
7328 return NULL;
7329 if (PyType_Ready(&Pdata_Type) < 0)
7330 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007331 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7332 return NULL;
7333 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7334 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007335
7336 /* Create the module and add the functions. */
7337 m = PyModule_Create(&_picklemodule);
7338 if (m == NULL)
7339 return NULL;
7340
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007341 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007342 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7343 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007344 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007345 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7346 return NULL;
7347
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007348 st = _Pickle_GetState(m);
7349
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007350 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007351 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7352 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007353 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007354 st->PicklingError = \
7355 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7356 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007357 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007358 st->UnpicklingError = \
7359 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7360 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007361 return NULL;
7362
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007363 Py_INCREF(st->PickleError);
7364 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007365 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007366 Py_INCREF(st->PicklingError);
7367 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007368 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007369 Py_INCREF(st->UnpicklingError);
7370 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007371 return NULL;
7372
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007373 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007374 return NULL;
7375
7376 return m;
7377}