blob: fed3fa221e7df6de1adca5caac1e8b77ebab89f6 [file] [log] [blame]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001#include "Python.h"
2#include "structmember.h"
3
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004PyDoc_STRVAR(pickle_module_doc,
5"Optimized C implementation for the Python pickle module.");
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08008module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -08009class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
10class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
11class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
12class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080013[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030014/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000016/* Bump this when new opcodes are added to the pickle protocol. */
17enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010018 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000019 DEFAULT_PROTOCOL = 3
20};
21
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000022/* Pickle opcodes. These must be kept updated with pickle.py.
23 Extensive docs are in pickletools.py. */
24enum opcode {
25 MARK = '(',
26 STOP = '.',
27 POP = '0',
28 POP_MARK = '1',
29 DUP = '2',
30 FLOAT = 'F',
31 INT = 'I',
32 BININT = 'J',
33 BININT1 = 'K',
34 LONG = 'L',
35 BININT2 = 'M',
36 NONE = 'N',
37 PERSID = 'P',
38 BINPERSID = 'Q',
39 REDUCE = 'R',
40 STRING = 'S',
41 BINSTRING = 'T',
42 SHORT_BINSTRING = 'U',
43 UNICODE = 'V',
44 BINUNICODE = 'X',
45 APPEND = 'a',
46 BUILD = 'b',
47 GLOBAL = 'c',
48 DICT = 'd',
49 EMPTY_DICT = '}',
50 APPENDS = 'e',
51 GET = 'g',
52 BINGET = 'h',
53 INST = 'i',
54 LONG_BINGET = 'j',
55 LIST = 'l',
56 EMPTY_LIST = ']',
57 OBJ = 'o',
58 PUT = 'p',
59 BINPUT = 'q',
60 LONG_BINPUT = 'r',
61 SETITEM = 's',
62 TUPLE = 't',
63 EMPTY_TUPLE = ')',
64 SETITEMS = 'u',
65 BINFLOAT = 'G',
66
67 /* Protocol 2. */
68 PROTO = '\x80',
69 NEWOBJ = '\x81',
70 EXT1 = '\x82',
71 EXT2 = '\x83',
72 EXT4 = '\x84',
73 TUPLE1 = '\x85',
74 TUPLE2 = '\x86',
75 TUPLE3 = '\x87',
76 NEWTRUE = '\x88',
77 NEWFALSE = '\x89',
78 LONG1 = '\x8a',
79 LONG4 = '\x8b',
80
81 /* Protocol 3 (Python 3.x) */
82 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010083 SHORT_BINBYTES = 'C',
84
85 /* Protocol 4 */
86 SHORT_BINUNICODE = '\x8c',
87 BINUNICODE8 = '\x8d',
88 BINBYTES8 = '\x8e',
89 EMPTY_SET = '\x8f',
90 ADDITEMS = '\x90',
91 FROZENSET = '\x91',
92 NEWOBJ_EX = '\x92',
93 STACK_GLOBAL = '\x93',
94 MEMOIZE = '\x94',
95 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000096};
97
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000098enum {
99 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
100 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
101 break if this gets out of synch with pickle.py, but it's unclear that would
102 help anything either. */
103 BATCHSIZE = 1000,
104
105 /* Nesting limit until Pickler, when running in "fast mode", starts
106 checking for self-referential data-structures. */
107 FAST_NESTING_LIMIT = 50,
108
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000109 /* Initial size of the write buffer of Pickler. */
110 WRITE_BUF_SIZE = 4096,
111
Antoine Pitrou04248a82010-10-12 20:51:21 +0000112 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100113 PREFETCH = 8192 * 16,
114
115 FRAME_SIZE_TARGET = 64 * 1024,
116
117 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000118};
119
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800120/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000121
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800122/* State of the pickle module, per PEP 3121. */
123typedef struct {
124 /* Exception classes for pickle. */
125 PyObject *PickleError;
126 PyObject *PicklingError;
127 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129 /* copyreg.dispatch_table, {type_object: pickling_function} */
130 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000131
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800132 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800134 /* copyreg._extension_registry, {(module_name, function_name): code} */
135 PyObject *extension_registry;
136 /* copyreg._extension_cache, {code: object} */
137 PyObject *extension_cache;
138 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
139 PyObject *inverted_registry;
140
141 /* Import mappings for compatibility with Python 2.x */
142
143 /* _compat_pickle.NAME_MAPPING,
144 {(oldmodule, oldname): (newmodule, newname)} */
145 PyObject *name_mapping_2to3;
146 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
147 PyObject *import_mapping_2to3;
148 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
149 PyObject *name_mapping_3to2;
150 PyObject *import_mapping_3to2;
151
152 /* codecs.encode, used for saving bytes in older protocols */
153 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300154 /* builtins.getattr, used for saving nested names with protocol < 4 */
155 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300156 /* functools.partial, used for implementing __newobj_ex__ with protocols
157 2 and 3 */
158 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800159} PickleState;
160
161/* Forward declaration of the _pickle module definition. */
162static struct PyModuleDef _picklemodule;
163
164/* Given a module object, get its per-module state. */
165static PickleState *
166_Pickle_GetState(PyObject *module)
167{
168 return (PickleState *)PyModule_GetState(module);
169}
170
171/* Find the module instance imported in the currently running sub-interpreter
172 and get its state. */
173static PickleState *
174_Pickle_GetGlobalState(void)
175{
176 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
177}
178
179/* Clear the given pickle module state. */
180static void
181_Pickle_ClearState(PickleState *st)
182{
183 Py_CLEAR(st->PickleError);
184 Py_CLEAR(st->PicklingError);
185 Py_CLEAR(st->UnpicklingError);
186 Py_CLEAR(st->dispatch_table);
187 Py_CLEAR(st->extension_registry);
188 Py_CLEAR(st->extension_cache);
189 Py_CLEAR(st->inverted_registry);
190 Py_CLEAR(st->name_mapping_2to3);
191 Py_CLEAR(st->import_mapping_2to3);
192 Py_CLEAR(st->name_mapping_3to2);
193 Py_CLEAR(st->import_mapping_3to2);
194 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300195 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100196 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800197}
198
199/* Initialize the given pickle module state. */
200static int
201_Pickle_InitState(PickleState *st)
202{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300203 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204 PyObject *copyreg = NULL;
205 PyObject *compat_pickle = NULL;
206 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300207 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800208
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300209 builtins = PyEval_GetBuiltins();
210 if (builtins == NULL)
211 goto error;
212 st->getattr = PyDict_GetItemString(builtins, "getattr");
213 if (st->getattr == NULL)
214 goto error;
215 Py_INCREF(st->getattr);
216
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800217 copyreg = PyImport_ImportModule("copyreg");
218 if (!copyreg)
219 goto error;
220 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
221 if (!st->dispatch_table)
222 goto error;
223 if (!PyDict_CheckExact(st->dispatch_table)) {
224 PyErr_Format(PyExc_RuntimeError,
225 "copyreg.dispatch_table should be a dict, not %.200s",
226 Py_TYPE(st->dispatch_table)->tp_name);
227 goto error;
228 }
229 st->extension_registry = \
230 PyObject_GetAttrString(copyreg, "_extension_registry");
231 if (!st->extension_registry)
232 goto error;
233 if (!PyDict_CheckExact(st->extension_registry)) {
234 PyErr_Format(PyExc_RuntimeError,
235 "copyreg._extension_registry should be a dict, "
236 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
237 goto error;
238 }
239 st->inverted_registry = \
240 PyObject_GetAttrString(copyreg, "_inverted_registry");
241 if (!st->inverted_registry)
242 goto error;
243 if (!PyDict_CheckExact(st->inverted_registry)) {
244 PyErr_Format(PyExc_RuntimeError,
245 "copyreg._inverted_registry should be a dict, "
246 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
247 goto error;
248 }
249 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
250 if (!st->extension_cache)
251 goto error;
252 if (!PyDict_CheckExact(st->extension_cache)) {
253 PyErr_Format(PyExc_RuntimeError,
254 "copyreg._extension_cache should be a dict, "
255 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
256 goto error;
257 }
258 Py_CLEAR(copyreg);
259
260 /* Load the 2.x -> 3.x stdlib module mapping tables */
261 compat_pickle = PyImport_ImportModule("_compat_pickle");
262 if (!compat_pickle)
263 goto error;
264 st->name_mapping_2to3 = \
265 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
266 if (!st->name_mapping_2to3)
267 goto error;
268 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
269 PyErr_Format(PyExc_RuntimeError,
270 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
271 Py_TYPE(st->name_mapping_2to3)->tp_name);
272 goto error;
273 }
274 st->import_mapping_2to3 = \
275 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
276 if (!st->import_mapping_2to3)
277 goto error;
278 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
279 PyErr_Format(PyExc_RuntimeError,
280 "_compat_pickle.IMPORT_MAPPING should be a dict, "
281 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
282 goto error;
283 }
284 /* ... and the 3.x -> 2.x mapping tables */
285 st->name_mapping_3to2 = \
286 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
287 if (!st->name_mapping_3to2)
288 goto error;
289 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
290 PyErr_Format(PyExc_RuntimeError,
291 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
292 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
293 goto error;
294 }
295 st->import_mapping_3to2 = \
296 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
297 if (!st->import_mapping_3to2)
298 goto error;
299 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
300 PyErr_Format(PyExc_RuntimeError,
301 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
302 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
303 goto error;
304 }
305 Py_CLEAR(compat_pickle);
306
307 codecs = PyImport_ImportModule("codecs");
308 if (codecs == NULL)
309 goto error;
310 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
311 if (st->codecs_encode == NULL) {
312 goto error;
313 }
314 if (!PyCallable_Check(st->codecs_encode)) {
315 PyErr_Format(PyExc_RuntimeError,
316 "codecs.encode should be a callable, not %.200s",
317 Py_TYPE(st->codecs_encode)->tp_name);
318 goto error;
319 }
320 Py_CLEAR(codecs);
321
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300322 functools = PyImport_ImportModule("functools");
323 if (!functools)
324 goto error;
325 st->partial = PyObject_GetAttrString(functools, "partial");
326 if (!st->partial)
327 goto error;
328 Py_CLEAR(functools);
329
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800330 return 0;
331
332 error:
333 Py_CLEAR(copyreg);
334 Py_CLEAR(compat_pickle);
335 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300336 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 _Pickle_ClearState(st);
338 return -1;
339}
340
341/* Helper for calling a function with a single argument quickly.
342
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800343 This function steals the reference of the given argument. */
344static PyObject *
345_Pickle_FastCall(PyObject *func, PyObject *obj)
346{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800347 PyObject *result;
348
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800349 /* Note: this function used to reuse the argument tuple. This used to give
350 a slight performance boost with older pickle implementations where many
351 unbuffered reads occurred (thus needing many function calls).
352
353 However, this optimization was removed because it was too complicated
354 to get right. It abused the C API for tuples to mutate them which led
355 to subtle reference counting and concurrency bugs. Furthermore, the
356 introduction of protocol 4 and the prefetching optimization via peek()
357 significantly reduced the number of function calls we do. Thus, the
358 benefits became marginal at best. */
359
Victor Stinner559bb6a2016-08-22 22:48:54 +0200360 result = _PyObject_CallArg1(func, obj);
Victor Stinner75210692016-08-19 18:59:15 +0200361 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800362 return result;
363}
364
365/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000366
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000367/* Internal data type used as the unpickling stack. */
368typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000369 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000370 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200371 int mark_set; /* is MARK set? */
372 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000373 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000374} Pdata;
375
376static void
377Pdata_dealloc(Pdata *self)
378{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200379 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000380 while (--i >= 0) {
381 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000382 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000383 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000384 PyObject_Del(self);
385}
386
387static PyTypeObject Pdata_Type = {
388 PyVarObject_HEAD_INIT(NULL, 0)
389 "_pickle.Pdata", /*tp_name*/
390 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200391 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000392 (destructor)Pdata_dealloc, /*tp_dealloc*/
393};
394
395static PyObject *
396Pdata_New(void)
397{
398 Pdata *self;
399
400 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
401 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000402 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200403 self->mark_set = 0;
404 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000405 self->allocated = 8;
406 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000407 if (self->data)
408 return (PyObject *)self;
409 Py_DECREF(self);
410 return PyErr_NoMemory();
411}
412
413
414/* Retain only the initial clearto items. If clearto >= the current
415 * number of items, this is a (non-erroneous) NOP.
416 */
417static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200418Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000419{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200420 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000421
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200422 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000423 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000424 return 0;
425
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000426 while (--i >= clearto) {
427 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000428 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000429 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000430 return 0;
431}
432
433static int
434Pdata_grow(Pdata *self)
435{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200437 size_t allocated = (size_t)self->allocated;
438 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000439
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000440 new_allocated = (allocated >> 3) + 6;
441 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200442 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000443 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000444 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500445 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000446 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000447 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000448
449 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200450 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000451 return 0;
452
453 nomemory:
454 PyErr_NoMemory();
455 return -1;
456}
457
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200458static int
459Pdata_stack_underflow(Pdata *self)
460{
461 PickleState *st = _Pickle_GetGlobalState();
462 PyErr_SetString(st->UnpicklingError,
463 self->mark_set ?
464 "unexpected MARK found" :
465 "unpickling stack underflow");
466 return -1;
467}
468
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000469/* D is a Pdata*. Pop the topmost element and store it into V, which
470 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
471 * is raised and V is set to NULL.
472 */
473static PyObject *
474Pdata_pop(Pdata *self)
475{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200476 if (Py_SIZE(self) <= self->fence) {
477 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000478 return NULL;
479 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000480 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000481}
482#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
483
484static int
485Pdata_push(Pdata *self, PyObject *obj)
486{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000487 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000488 return -1;
489 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000490 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000491 return 0;
492}
493
494/* Push an object on stack, transferring its ownership to the stack. */
495#define PDATA_PUSH(D, O, ER) do { \
496 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
497
498/* Push an object on stack, adding a new reference to the object. */
499#define PDATA_APPEND(D, O, ER) do { \
500 Py_INCREF((O)); \
501 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
502
503static PyObject *
504Pdata_poptuple(Pdata *self, Py_ssize_t start)
505{
506 PyObject *tuple;
507 Py_ssize_t len, i, j;
508
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200509 if (start < self->fence) {
510 Pdata_stack_underflow(self);
511 return NULL;
512 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000513 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000514 tuple = PyTuple_New(len);
515 if (tuple == NULL)
516 return NULL;
517 for (i = start, j = 0; j < len; i++, j++)
518 PyTuple_SET_ITEM(tuple, j, self->data[i]);
519
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000520 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000521 return tuple;
522}
523
524static PyObject *
525Pdata_poplist(Pdata *self, Py_ssize_t start)
526{
527 PyObject *list;
528 Py_ssize_t len, i, j;
529
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000530 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000531 list = PyList_New(len);
532 if (list == NULL)
533 return NULL;
534 for (i = start, j = 0; j < len; i++, j++)
535 PyList_SET_ITEM(list, j, self->data[i]);
536
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000537 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000538 return list;
539}
540
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000541typedef struct {
542 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200543 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000544} PyMemoEntry;
545
546typedef struct {
547 Py_ssize_t mt_mask;
548 Py_ssize_t mt_used;
549 Py_ssize_t mt_allocated;
550 PyMemoEntry *mt_table;
551} PyMemoTable;
552
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000553typedef struct PicklerObject {
554 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000555 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000556 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000557 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000558 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100559 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000560
561 PyObject *write; /* write() method of the output stream. */
562 PyObject *output_buffer; /* Write into a local bytearray buffer before
563 flushing to the stream. */
564 Py_ssize_t output_len; /* Length of output_buffer. */
565 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000566 int proto; /* Pickle protocol number, >= 0 */
567 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100568 int framing; /* True when framing is enabled, proto >= 4 */
569 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000570 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100571 is no frame currently open. */
572
573 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000574 int fast; /* Enable fast mode if set to a true value.
575 The fast mode disable the usage of memo,
576 therefore speeding the pickling process by
577 not generating superfluous PUT opcodes. It
578 should not be used if with self-referential
579 objects. */
580 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000581 int fix_imports; /* Indicate whether Pickler should fix
582 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000583 PyObject *fast_memo;
584} PicklerObject;
585
586typedef struct UnpicklerObject {
587 PyObject_HEAD
588 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000589
590 /* The unpickler memo is just an array of PyObject *s. Using a dict
591 is unnecessary, since the keys are contiguous ints. */
592 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100593 Py_ssize_t memo_size; /* Capacity of the memo array */
594 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000595
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000596 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000597
598 Py_buffer buffer;
599 char *input_buffer;
600 char *input_line;
601 Py_ssize_t input_len;
602 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000603 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100604
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000605 PyObject *read; /* read() method of the input stream. */
606 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000607 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000608
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000609 char *encoding; /* Name of the encoding to be used for
610 decoding strings pickled using Python
611 2.x. The default value is "ASCII" */
612 char *errors; /* Name of errors handling scheme to used when
613 decoding strings. The default value is
614 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500615 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000616 objects. */
617 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
618 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000619 int proto; /* Protocol of the pickle loaded. */
620 int fix_imports; /* Indicate whether Unpickler should fix
621 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000622} UnpicklerObject;
623
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200624typedef struct {
625 PyObject_HEAD
626 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
627} PicklerMemoProxyObject;
628
629typedef struct {
630 PyObject_HEAD
631 UnpicklerObject *unpickler;
632} UnpicklerMemoProxyObject;
633
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000634/* Forward declarations */
635static int save(PicklerObject *, PyObject *, int);
636static int save_reduce(PicklerObject *, PyObject *, PyObject *);
637static PyTypeObject Pickler_Type;
638static PyTypeObject Unpickler_Type;
639
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200640#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000641
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000642/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300643 A custom hashtable mapping void* to Python ints. This is used by the pickler
644 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000645 a bunch of unnecessary object creation. This makes a huge performance
646 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000647
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000648#define MT_MINSIZE 8
649#define PERTURB_SHIFT 5
650
651
652static PyMemoTable *
653PyMemoTable_New(void)
654{
655 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
656 if (memo == NULL) {
657 PyErr_NoMemory();
658 return NULL;
659 }
660
661 memo->mt_used = 0;
662 memo->mt_allocated = MT_MINSIZE;
663 memo->mt_mask = MT_MINSIZE - 1;
664 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
665 if (memo->mt_table == NULL) {
666 PyMem_FREE(memo);
667 PyErr_NoMemory();
668 return NULL;
669 }
670 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
671
672 return memo;
673}
674
675static PyMemoTable *
676PyMemoTable_Copy(PyMemoTable *self)
677{
678 Py_ssize_t i;
679 PyMemoTable *new = PyMemoTable_New();
680 if (new == NULL)
681 return NULL;
682
683 new->mt_used = self->mt_used;
684 new->mt_allocated = self->mt_allocated;
685 new->mt_mask = self->mt_mask;
686 /* The table we get from _New() is probably smaller than we wanted.
687 Free it and allocate one that's the right size. */
688 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500689 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000690 if (new->mt_table == NULL) {
691 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200692 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000693 return NULL;
694 }
695 for (i = 0; i < self->mt_allocated; i++) {
696 Py_XINCREF(self->mt_table[i].me_key);
697 }
698 memcpy(new->mt_table, self->mt_table,
699 sizeof(PyMemoEntry) * self->mt_allocated);
700
701 return new;
702}
703
704static Py_ssize_t
705PyMemoTable_Size(PyMemoTable *self)
706{
707 return self->mt_used;
708}
709
710static int
711PyMemoTable_Clear(PyMemoTable *self)
712{
713 Py_ssize_t i = self->mt_allocated;
714
715 while (--i >= 0) {
716 Py_XDECREF(self->mt_table[i].me_key);
717 }
718 self->mt_used = 0;
719 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
720 return 0;
721}
722
723static void
724PyMemoTable_Del(PyMemoTable *self)
725{
726 if (self == NULL)
727 return;
728 PyMemoTable_Clear(self);
729
730 PyMem_FREE(self->mt_table);
731 PyMem_FREE(self);
732}
733
734/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
735 can be considerably simpler than dictobject.c's lookdict(). */
736static PyMemoEntry *
737_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
738{
739 size_t i;
740 size_t perturb;
741 size_t mask = (size_t)self->mt_mask;
742 PyMemoEntry *table = self->mt_table;
743 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000744 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000745
746 i = hash & mask;
747 entry = &table[i];
748 if (entry->me_key == NULL || entry->me_key == key)
749 return entry;
750
751 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
752 i = (i << 2) + i + perturb + 1;
753 entry = &table[i & mask];
754 if (entry->me_key == NULL || entry->me_key == key)
755 return entry;
756 }
757 assert(0); /* Never reached */
758 return NULL;
759}
760
761/* Returns -1 on failure, 0 on success. */
762static int
763_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
764{
765 PyMemoEntry *oldtable = NULL;
766 PyMemoEntry *oldentry, *newentry;
767 Py_ssize_t new_size = MT_MINSIZE;
768 Py_ssize_t to_process;
769
770 assert(min_size > 0);
771
772 /* Find the smallest valid table size >= min_size. */
773 while (new_size < min_size && new_size > 0)
774 new_size <<= 1;
775 if (new_size <= 0) {
776 PyErr_NoMemory();
777 return -1;
778 }
779 /* new_size needs to be a power of two. */
780 assert((new_size & (new_size - 1)) == 0);
781
782 /* Allocate new table. */
783 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500784 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000785 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200786 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000787 PyErr_NoMemory();
788 return -1;
789 }
790 self->mt_allocated = new_size;
791 self->mt_mask = new_size - 1;
792 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
793
794 /* Copy entries from the old table. */
795 to_process = self->mt_used;
796 for (oldentry = oldtable; to_process > 0; oldentry++) {
797 if (oldentry->me_key != NULL) {
798 to_process--;
799 /* newentry is a pointer to a chunk of the new
800 mt_table, so we're setting the key:value pair
801 in-place. */
802 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
803 newentry->me_key = oldentry->me_key;
804 newentry->me_value = oldentry->me_value;
805 }
806 }
807
808 /* Deallocate the old table. */
809 PyMem_FREE(oldtable);
810 return 0;
811}
812
813/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200814static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000815PyMemoTable_Get(PyMemoTable *self, PyObject *key)
816{
817 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
818 if (entry->me_key == NULL)
819 return NULL;
820 return &entry->me_value;
821}
822
823/* Returns -1 on failure, 0 on success. */
824static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200825PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000826{
827 PyMemoEntry *entry;
828
829 assert(key != NULL);
830
831 entry = _PyMemoTable_Lookup(self, key);
832 if (entry->me_key != NULL) {
833 entry->me_value = value;
834 return 0;
835 }
836 Py_INCREF(key);
837 entry->me_key = key;
838 entry->me_value = value;
839 self->mt_used++;
840
841 /* If we added a key, we can safely resize. Otherwise just return!
842 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
843 *
844 * Quadrupling the size improves average table sparseness
845 * (reducing collisions) at the cost of some memory. It also halves
846 * the number of expensive resize operations in a growing memo table.
847 *
848 * Very large memo tables (over 50K items) use doubling instead.
849 * This may help applications with severe memory constraints.
850 */
851 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
852 return 0;
853 return _PyMemoTable_ResizeTable(self,
854 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
855}
856
857#undef MT_MINSIZE
858#undef PERTURB_SHIFT
859
860/*************************************************************************/
861
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000862
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000863static int
864_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000865{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300866 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200867 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000868 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000869 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000870 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100871 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000872 return 0;
873}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000874
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100875static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100876_write_size64(char *out, size_t value)
877{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200878 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800879
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200880 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800881
882 for (i = 0; i < sizeof(size_t); i++) {
883 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
884 }
885 for (i = sizeof(size_t); i < 8; i++) {
886 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800887 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100888}
889
890static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100891_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
892{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100893 qdata[0] = FRAME;
894 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100895}
896
897static int
898_Pickler_CommitFrame(PicklerObject *self)
899{
900 size_t frame_len;
901 char *qdata;
902
903 if (!self->framing || self->frame_start == -1)
904 return 0;
905 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
906 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
907 _Pickler_WriteFrameHeader(self, qdata, frame_len);
908 self->frame_start = -1;
909 return 0;
910}
911
912static int
913_Pickler_OpcodeBoundary(PicklerObject *self)
914{
915 Py_ssize_t frame_len;
916
917 if (!self->framing || self->frame_start == -1)
918 return 0;
919 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
920 if (frame_len >= FRAME_SIZE_TARGET)
921 return _Pickler_CommitFrame(self);
922 else
923 return 0;
924}
925
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000926static PyObject *
927_Pickler_GetString(PicklerObject *self)
928{
929 PyObject *output_buffer = self->output_buffer;
930
931 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100932
933 if (_Pickler_CommitFrame(self))
934 return NULL;
935
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000936 self->output_buffer = NULL;
937 /* Resize down to exact size */
938 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
939 return NULL;
940 return output_buffer;
941}
942
943static int
944_Pickler_FlushToFile(PicklerObject *self)
945{
946 PyObject *output, *result;
947
948 assert(self->write != NULL);
949
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100950 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000951 output = _Pickler_GetString(self);
952 if (output == NULL)
953 return -1;
954
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800955 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000956 Py_XDECREF(result);
957 return (result == NULL) ? -1 : 0;
958}
959
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200960static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100961_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000962{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100963 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000964 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100965 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000966
967 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100968 need_new_frame = (self->framing && self->frame_start == -1);
969
970 if (need_new_frame)
971 n = data_len + FRAME_HEADER_SIZE;
972 else
973 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000974
975 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100976 if (required > self->max_output_len) {
977 /* Make place in buffer for the pickle chunk */
978 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
979 PyErr_NoMemory();
980 return -1;
981 }
982 self->max_output_len = (self->output_len + n) / 2 * 3;
983 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
984 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000985 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000986 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100987 if (need_new_frame) {
988 /* Setup new frame */
989 Py_ssize_t frame_start = self->output_len;
990 self->frame_start = frame_start;
991 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
992 /* Write an invalid value, for debugging */
993 buffer[frame_start + i] = 0xFE;
994 }
995 self->output_len += FRAME_HEADER_SIZE;
996 }
997 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000998 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100999 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001000 buffer[self->output_len + i] = s[i];
1001 }
1002 }
1003 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001004 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001005 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001006 self->output_len += data_len;
1007 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001008}
1009
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001010static PicklerObject *
1011_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001012{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001013 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001014
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001015 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1016 if (self == NULL)
1017 return NULL;
1018
1019 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001020 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021 self->write = NULL;
1022 self->proto = 0;
1023 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001024 self->framing = 0;
1025 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001026 self->fast = 0;
1027 self->fast_nesting = 0;
1028 self->fix_imports = 0;
1029 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001030 self->max_output_len = WRITE_BUF_SIZE;
1031 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001032
1033 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001034 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1035 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001036
1037 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001038 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001039 return NULL;
1040 }
1041 return self;
1042}
1043
1044static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001045_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001047 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001048
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001049 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001051 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001053 proto = PyLong_AsLong(protocol);
1054 if (proto < 0) {
1055 if (proto == -1 && PyErr_Occurred())
1056 return -1;
1057 proto = HIGHEST_PROTOCOL;
1058 }
1059 else if (proto > HIGHEST_PROTOCOL) {
1060 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1061 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001062 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001063 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001064 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001065 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001066 self->bin = proto > 0;
1067 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001068 return 0;
1069}
1070
1071/* Returns -1 (with an exception set) on failure, 0 on success. This may
1072 be called once on a freshly created Pickler. */
1073static int
1074_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1075{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001076 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001077 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001078 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001079 if (self->write == NULL) {
1080 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1081 PyErr_SetString(PyExc_TypeError,
1082 "file must have a 'write' attribute");
1083 return -1;
1084 }
1085
1086 return 0;
1087}
1088
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001089/* Returns the size of the input on success, -1 on failure. This takes its
1090 own reference to `input`. */
1091static Py_ssize_t
1092_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1093{
1094 if (self->buffer.buf != NULL)
1095 PyBuffer_Release(&self->buffer);
1096 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1097 return -1;
1098 self->input_buffer = self->buffer.buf;
1099 self->input_len = self->buffer.len;
1100 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001101 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001102 return self->input_len;
1103}
1104
Antoine Pitrou04248a82010-10-12 20:51:21 +00001105static int
1106_Unpickler_SkipConsumed(UnpicklerObject *self)
1107{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001108 Py_ssize_t consumed;
1109 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001110
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001111 consumed = self->next_read_idx - self->prefetched_idx;
1112 if (consumed <= 0)
1113 return 0;
1114
1115 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001116 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001117 r = PyObject_CallFunction(self->read, "n", consumed);
1118 if (r == NULL)
1119 return -1;
1120 Py_DECREF(r);
1121
1122 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001123 return 0;
1124}
1125
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001126static const Py_ssize_t READ_WHOLE_LINE = -1;
1127
1128/* If reading from a file, we need to only pull the bytes we need, since there
1129 may be multiple pickle objects arranged contiguously in the same input
1130 buffer.
1131
1132 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1133 bytes from the input stream/buffer.
1134
1135 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1136 failure; on success, returns the number of bytes read from the file.
1137
1138 On success, self->input_len will be 0; this is intentional so that when
1139 unpickling from a file, the "we've run out of data" code paths will trigger,
1140 causing the Unpickler to go back to the file for more data. Use the returned
1141 size to tell you how much data you can process. */
1142static Py_ssize_t
1143_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1144{
1145 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001146 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001147
1148 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001149
Antoine Pitrou04248a82010-10-12 20:51:21 +00001150 if (_Unpickler_SkipConsumed(self) < 0)
1151 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001152
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001153 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001154 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001155 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001156 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001157 PyObject *len;
1158 /* Prefetch some data without advancing the file pointer, if possible */
1159 if (self->peek && n < PREFETCH) {
1160 len = PyLong_FromSsize_t(PREFETCH);
1161 if (len == NULL)
1162 return -1;
1163 data = _Pickle_FastCall(self->peek, len);
1164 if (data == NULL) {
1165 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1166 return -1;
1167 /* peek() is probably not supported by the given file object */
1168 PyErr_Clear();
1169 Py_CLEAR(self->peek);
1170 }
1171 else {
1172 read_size = _Unpickler_SetStringInput(self, data);
1173 Py_DECREF(data);
1174 self->prefetched_idx = 0;
1175 if (n <= read_size)
1176 return n;
1177 }
1178 }
1179 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001180 if (len == NULL)
1181 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001182 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001183 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001184 if (data == NULL)
1185 return -1;
1186
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001187 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001188 Py_DECREF(data);
1189 return read_size;
1190}
1191
Victor Stinner19ed27e2016-05-20 11:42:37 +02001192/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001193static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001194_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001195{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001196 Py_ssize_t num_read;
1197
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001198 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001199 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1200 PickleState *st = _Pickle_GetGlobalState();
1201 PyErr_SetString(st->UnpicklingError,
1202 "read would overflow (invalid bytecode)");
1203 return -1;
1204 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001205
1206 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1207 assert(self->next_read_idx + n > self->input_len);
1208
Antoine Pitrou04248a82010-10-12 20:51:21 +00001209 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001210 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001211 return -1;
1212 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001213 num_read = _Unpickler_ReadFromFile(self, n);
1214 if (num_read < 0)
1215 return -1;
1216 if (num_read < n) {
1217 PyErr_Format(PyExc_EOFError, "Ran out of input");
1218 return -1;
1219 }
1220 *s = self->input_buffer;
1221 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001222 return n;
1223}
1224
Victor Stinner19ed27e2016-05-20 11:42:37 +02001225/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1226
1227 This should be used for all data reads, rather than accessing the unpickler's
1228 input buffer directly. This method deals correctly with reading from input
1229 streams, which the input buffer doesn't deal with.
1230
1231 Note that when reading from a file-like object, self->next_read_idx won't
1232 be updated (it should remain at 0 for the entire unpickling process). You
1233 should use this function's return value to know how many bytes you can
1234 consume.
1235
1236 Returns -1 (with an exception set) on failure. On success, return the
1237 number of chars read. */
1238#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001239 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001240 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1241 (self)->next_read_idx += (n), \
1242 (n)) \
1243 : _Unpickler_ReadImpl(self, (s), (n)))
1244
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001245static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001246_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1247 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001248{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001249 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001250 if (input_line == NULL) {
1251 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001252 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001254
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001255 memcpy(input_line, line, len);
1256 input_line[len] = '\0';
1257 self->input_line = input_line;
1258 *result = self->input_line;
1259 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001260}
1261
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001262/* Read a line from the input stream/buffer. If we run off the end of the input
1263 before hitting \n, return the data we found.
1264
1265 Returns the number of chars read, or -1 on failure. */
1266static Py_ssize_t
1267_Unpickler_Readline(UnpicklerObject *self, char **result)
1268{
1269 Py_ssize_t i, num_read;
1270
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001271 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001272 if (self->input_buffer[i] == '\n') {
1273 char *line_start = self->input_buffer + self->next_read_idx;
1274 num_read = i - self->next_read_idx + 1;
1275 self->next_read_idx = i + 1;
1276 return _Unpickler_CopyLine(self, line_start, num_read, result);
1277 }
1278 }
1279 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001280 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1281 if (num_read < 0)
1282 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001283 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001284 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001285 }
Victor Stinner121aab42011-09-29 23:40:53 +02001286
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001287 /* If we get here, we've run off the end of the input string. Return the
1288 remaining string and let the caller figure it out. */
1289 *result = self->input_buffer + self->next_read_idx;
1290 num_read = i - self->next_read_idx;
1291 self->next_read_idx = i;
1292 return num_read;
1293}
1294
1295/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1296 will be modified in place. */
1297static int
1298_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1299{
1300 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001301
1302 assert(new_size > self->memo_size);
1303
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001304 PyMem_RESIZE(self->memo, PyObject *, new_size);
1305 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001306 PyErr_NoMemory();
1307 return -1;
1308 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001309 for (i = self->memo_size; i < new_size; i++)
1310 self->memo[i] = NULL;
1311 self->memo_size = new_size;
1312 return 0;
1313}
1314
1315/* Returns NULL if idx is out of bounds. */
1316static PyObject *
1317_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1318{
1319 if (idx < 0 || idx >= self->memo_size)
1320 return NULL;
1321
1322 return self->memo[idx];
1323}
1324
1325/* Returns -1 (with an exception set) on failure, 0 on success.
1326 This takes its own reference to `value`. */
1327static int
1328_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1329{
1330 PyObject *old_item;
1331
1332 if (idx >= self->memo_size) {
1333 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1334 return -1;
1335 assert(idx < self->memo_size);
1336 }
1337 Py_INCREF(value);
1338 old_item = self->memo[idx];
1339 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001340 if (old_item != NULL) {
1341 Py_DECREF(old_item);
1342 }
1343 else {
1344 self->memo_len++;
1345 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001346 return 0;
1347}
1348
1349static PyObject **
1350_Unpickler_NewMemo(Py_ssize_t new_size)
1351{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001352 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001353 if (memo == NULL) {
1354 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001355 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001356 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001357 memset(memo, 0, new_size * sizeof(PyObject *));
1358 return memo;
1359}
1360
1361/* Free the unpickler's memo, taking care to decref any items left in it. */
1362static void
1363_Unpickler_MemoCleanup(UnpicklerObject *self)
1364{
1365 Py_ssize_t i;
1366 PyObject **memo = self->memo;
1367
1368 if (self->memo == NULL)
1369 return;
1370 self->memo = NULL;
1371 i = self->memo_size;
1372 while (--i >= 0) {
1373 Py_XDECREF(memo[i]);
1374 }
1375 PyMem_FREE(memo);
1376}
1377
1378static UnpicklerObject *
1379_Unpickler_New(void)
1380{
1381 UnpicklerObject *self;
1382
1383 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1384 if (self == NULL)
1385 return NULL;
1386
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001387 self->pers_func = NULL;
1388 self->input_buffer = NULL;
1389 self->input_line = NULL;
1390 self->input_len = 0;
1391 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001392 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001393 self->read = NULL;
1394 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001395 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001396 self->encoding = NULL;
1397 self->errors = NULL;
1398 self->marks = NULL;
1399 self->num_marks = 0;
1400 self->marks_size = 0;
1401 self->proto = 0;
1402 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001403 memset(&self->buffer, 0, sizeof(Py_buffer));
1404 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001405 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001406 self->memo = _Unpickler_NewMemo(self->memo_size);
1407 self->stack = (Pdata *)Pdata_New();
1408
1409 if (self->memo == NULL || self->stack == NULL) {
1410 Py_DECREF(self);
1411 return NULL;
1412 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001413
1414 return self;
1415}
1416
1417/* Returns -1 (with an exception set) on failure, 0 on success. This may
1418 be called once on a freshly created Pickler. */
1419static int
1420_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1421{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001422 _Py_IDENTIFIER(peek);
1423 _Py_IDENTIFIER(read);
1424 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001425
1426 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001427 if (self->peek == NULL) {
1428 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1429 PyErr_Clear();
1430 else
1431 return -1;
1432 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001433 self->read = _PyObject_GetAttrId(file, &PyId_read);
1434 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001435 if (self->readline == NULL || self->read == NULL) {
1436 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1437 PyErr_SetString(PyExc_TypeError,
1438 "file must have 'read' and 'readline' attributes");
1439 Py_CLEAR(self->read);
1440 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001441 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001442 return -1;
1443 }
1444 return 0;
1445}
1446
1447/* Returns -1 (with an exception set) on failure, 0 on success. This may
1448 be called once on a freshly created Pickler. */
1449static int
1450_Unpickler_SetInputEncoding(UnpicklerObject *self,
1451 const char *encoding,
1452 const char *errors)
1453{
1454 if (encoding == NULL)
1455 encoding = "ASCII";
1456 if (errors == NULL)
1457 errors = "strict";
1458
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001459 self->encoding = _PyMem_Strdup(encoding);
1460 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001461 if (self->encoding == NULL || self->errors == NULL) {
1462 PyErr_NoMemory();
1463 return -1;
1464 }
1465 return 0;
1466}
1467
1468/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001469static int
1470memo_get(PicklerObject *self, PyObject *key)
1471{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001472 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001473 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001474 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001475
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001476 value = PyMemoTable_Get(self->memo, key);
1477 if (value == NULL) {
1478 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001479 return -1;
1480 }
1481
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001482 if (!self->bin) {
1483 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001484 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1485 "%" PY_FORMAT_SIZE_T "d\n", *value);
1486 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001487 }
1488 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001489 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001490 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001491 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001492 len = 2;
1493 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001494 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001495 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001496 pdata[1] = (unsigned char)(*value & 0xff);
1497 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1498 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1499 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001500 len = 5;
1501 }
1502 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001503 PickleState *st = _Pickle_GetGlobalState();
1504 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001505 "memo id too large for LONG_BINGET");
1506 return -1;
1507 }
1508 }
1509
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001510 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001511 return -1;
1512
1513 return 0;
1514}
1515
1516/* Store an object in the memo, assign it a new unique ID based on the number
1517 of objects currently stored in the memo and generate a PUT opcode. */
1518static int
1519memo_put(PicklerObject *self, PyObject *obj)
1520{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001521 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001522 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001523 Py_ssize_t idx;
1524
1525 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001526
1527 if (self->fast)
1528 return 0;
1529
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001530 idx = PyMemoTable_Size(self->memo);
1531 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1532 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001533
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001534 if (self->proto >= 4) {
1535 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1536 return -1;
1537 return 0;
1538 }
1539 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001540 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001541 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001542 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001543 len = strlen(pdata);
1544 }
1545 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001546 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001547 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001548 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001549 len = 2;
1550 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001551 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001552 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001553 pdata[1] = (unsigned char)(idx & 0xff);
1554 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1555 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1556 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001557 len = 5;
1558 }
1559 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001560 PickleState *st = _Pickle_GetGlobalState();
1561 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001562 "memo id too large for LONG_BINPUT");
1563 return -1;
1564 }
1565 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001566 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001567 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001568
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001569 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001570}
1571
1572static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001573get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001574 _Py_static_string(PyId_dot, ".");
1575 _Py_static_string(PyId_locals, "<locals>");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001576 PyObject *dotted_path;
1577 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001578
1579 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001580 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001581 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001582 n = PyList_GET_SIZE(dotted_path);
1583 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001584 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001585 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001586 PyObject *result = PyUnicode_RichCompare(
1587 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1588 int is_equal = (result == Py_True);
1589 assert(PyBool_Check(result));
1590 Py_DECREF(result);
1591 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001592 if (obj == NULL)
1593 PyErr_Format(PyExc_AttributeError,
1594 "Can't pickle local object %R", name);
1595 else
1596 PyErr_Format(PyExc_AttributeError,
1597 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001598 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001599 return NULL;
1600 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001601 }
1602 return dotted_path;
1603}
1604
1605static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001606get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001607{
1608 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001609 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001610
1611 assert(PyList_CheckExact(names));
1612 Py_INCREF(obj);
1613 n = PyList_GET_SIZE(names);
1614 for (i = 0; i < n; i++) {
1615 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001616 Py_XDECREF(parent);
1617 parent = obj;
1618 obj = PyObject_GetAttr(parent, name);
1619 if (obj == NULL) {
1620 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001621 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001622 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001623 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001624 if (pparent != NULL)
1625 *pparent = parent;
1626 else
1627 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001628 return obj;
1629}
1630
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001631static void
1632reformat_attribute_error(PyObject *obj, PyObject *name)
1633{
1634 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1635 PyErr_Clear();
1636 PyErr_Format(PyExc_AttributeError,
1637 "Can't get attribute %R on %R", name, obj);
1638 }
1639}
1640
1641
1642static PyObject *
1643getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1644{
1645 PyObject *dotted_path, *attr;
1646
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001647 if (allow_qualname) {
1648 dotted_path = get_dotted_path(obj, name);
1649 if (dotted_path == NULL)
1650 return NULL;
1651 attr = get_deep_attribute(obj, dotted_path, NULL);
1652 Py_DECREF(dotted_path);
1653 }
1654 else
1655 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001656 if (attr == NULL)
1657 reformat_attribute_error(obj, name);
1658 return attr;
1659}
1660
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001661static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001662whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001663{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001664 PyObject *module_name;
1665 PyObject *modules_dict;
1666 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001667 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001668 _Py_IDENTIFIER(__module__);
1669 _Py_IDENTIFIER(modules);
1670 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001671
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001672 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1673
1674 if (module_name == NULL) {
1675 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001676 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001677 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001678 }
1679 else {
1680 /* In some rare cases (e.g., bound methods of extension types),
1681 __module__ can be None. If it is so, then search sys.modules for
1682 the module of global. */
1683 if (module_name != Py_None)
1684 return module_name;
1685 Py_CLEAR(module_name);
1686 }
1687 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001688
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001689 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001690 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001691 if (modules_dict == NULL) {
1692 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001693 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001694 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695
1696 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001697 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1698 PyObject *candidate;
1699 if (PyUnicode_Check(module_name) &&
1700 !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001701 continue;
1702 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001703 continue;
1704
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001705 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001706 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001707 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001708 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001709 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001710 continue;
1711 }
1712
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001713 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001714 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001715 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001716 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001717 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001718 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001719 }
1720
1721 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001722 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001723 Py_INCREF(module_name);
1724 return module_name;
1725}
1726
1727/* fast_save_enter() and fast_save_leave() are guards against recursive
1728 objects when Pickler is used with the "fast mode" (i.e., with object
1729 memoization disabled). If the nesting of a list or dict object exceed
1730 FAST_NESTING_LIMIT, these guards will start keeping an internal
1731 reference to the seen list or dict objects and check whether these objects
1732 are recursive. These are not strictly necessary, since save() has a
1733 hard-coded recursion limit, but they give a nicer error message than the
1734 typical RuntimeError. */
1735static int
1736fast_save_enter(PicklerObject *self, PyObject *obj)
1737{
1738 /* if fast_nesting < 0, we're doing an error exit. */
1739 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1740 PyObject *key = NULL;
1741 if (self->fast_memo == NULL) {
1742 self->fast_memo = PyDict_New();
1743 if (self->fast_memo == NULL) {
1744 self->fast_nesting = -1;
1745 return 0;
1746 }
1747 }
1748 key = PyLong_FromVoidPtr(obj);
1749 if (key == NULL)
1750 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001751 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001752 Py_DECREF(key);
1753 PyErr_Format(PyExc_ValueError,
1754 "fast mode: can't pickle cyclic objects "
1755 "including object type %.200s at %p",
1756 obj->ob_type->tp_name, obj);
1757 self->fast_nesting = -1;
1758 return 0;
1759 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001760 if (PyErr_Occurred()) {
1761 return 0;
1762 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001763 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1764 Py_DECREF(key);
1765 self->fast_nesting = -1;
1766 return 0;
1767 }
1768 Py_DECREF(key);
1769 }
1770 return 1;
1771}
1772
1773static int
1774fast_save_leave(PicklerObject *self, PyObject *obj)
1775{
1776 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1777 PyObject *key = PyLong_FromVoidPtr(obj);
1778 if (key == NULL)
1779 return 0;
1780 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1781 Py_DECREF(key);
1782 return 0;
1783 }
1784 Py_DECREF(key);
1785 }
1786 return 1;
1787}
1788
1789static int
1790save_none(PicklerObject *self, PyObject *obj)
1791{
1792 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001793 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001794 return -1;
1795
1796 return 0;
1797}
1798
1799static int
1800save_bool(PicklerObject *self, PyObject *obj)
1801{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001802 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001803 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001804 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001805 return -1;
1806 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001807 else {
1808 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1809 * so that unpicklers written before bools were introduced unpickle them
1810 * as ints, but unpicklers after can recognize that bools were intended.
1811 * Note that protocol 2 added direct ways to pickle bools.
1812 */
1813 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1814 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1815 return -1;
1816 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001817 return 0;
1818}
1819
1820static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001821save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001822{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001823 PyObject *repr = NULL;
1824 Py_ssize_t size;
1825 long val;
1826 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001827
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001828 const char long_op = LONG;
1829
1830 val= PyLong_AsLong(obj);
1831 if (val == -1 && PyErr_Occurred()) {
1832 /* out of range for int pickling */
1833 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001834 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001835 else if (self->bin &&
1836 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001837 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001838 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001839
1840 Note: we can't use -0x80000000L in the above condition because some
1841 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1842 before applying the unary minus when sizeof(long) <= 4. The
1843 resulting value stays unsigned which is commonly not what we want,
1844 so MSVC happily warns us about it. However, that result would have
1845 been fine because we guard for sizeof(long) <= 4 which turns the
1846 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001847 char pdata[32];
1848 Py_ssize_t len = 0;
1849
1850 pdata[1] = (unsigned char)(val & 0xff);
1851 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1852 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1853 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001854
1855 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1856 if (pdata[2] == 0) {
1857 pdata[0] = BININT1;
1858 len = 2;
1859 }
1860 else {
1861 pdata[0] = BININT2;
1862 len = 3;
1863 }
1864 }
1865 else {
1866 pdata[0] = BININT;
1867 len = 5;
1868 }
1869
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001870 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001871 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001872
1873 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001874 }
1875
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001876 if (self->proto >= 2) {
1877 /* Linear-time pickling. */
1878 size_t nbits;
1879 size_t nbytes;
1880 unsigned char *pdata;
1881 char header[5];
1882 int i;
1883 int sign = _PyLong_Sign(obj);
1884
1885 if (sign == 0) {
1886 header[0] = LONG1;
1887 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001888 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001889 goto error;
1890 return 0;
1891 }
1892 nbits = _PyLong_NumBits(obj);
1893 if (nbits == (size_t)-1 && PyErr_Occurred())
1894 goto error;
1895 /* How many bytes do we need? There are nbits >> 3 full
1896 * bytes of data, and nbits & 7 leftover bits. If there
1897 * are any leftover bits, then we clearly need another
1898 * byte. Wnat's not so obvious is that we *probably*
1899 * need another byte even if there aren't any leftovers:
1900 * the most-significant bit of the most-significant byte
1901 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001902 * opposite of the one we need. The exception is ints
1903 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001904 * its own 256's-complement, so has the right sign bit
1905 * even without the extra byte. That's a pain to check
1906 * for in advance, though, so we always grab an extra
1907 * byte at the start, and cut it back later if possible.
1908 */
1909 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001910 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001911 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001912 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001913 goto error;
1914 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001915 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001916 if (repr == NULL)
1917 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001918 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001919 i = _PyLong_AsByteArray((PyLongObject *)obj,
1920 pdata, nbytes,
1921 1 /* little endian */ , 1 /* signed */ );
1922 if (i < 0)
1923 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001924 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001925 * needed. This is so iff the MSB is all redundant sign
1926 * bits.
1927 */
1928 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001929 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001930 pdata[nbytes - 1] == 0xff &&
1931 (pdata[nbytes - 2] & 0x80) != 0) {
1932 nbytes--;
1933 }
1934
1935 if (nbytes < 256) {
1936 header[0] = LONG1;
1937 header[1] = (unsigned char)nbytes;
1938 size = 2;
1939 }
1940 else {
1941 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001942 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001943 for (i = 1; i < 5; i++) {
1944 header[i] = (unsigned char)(size & 0xff);
1945 size >>= 8;
1946 }
1947 size = 5;
1948 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001949 if (_Pickler_Write(self, header, size) < 0 ||
1950 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001951 goto error;
1952 }
1953 else {
1954 char *string;
1955
Mark Dickinson8dd05142009-01-20 20:43:58 +00001956 /* proto < 2: write the repr and newline. This is quadratic-time (in
1957 the number of digits), in both directions. We add a trailing 'L'
1958 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001959
1960 repr = PyObject_Repr(obj);
1961 if (repr == NULL)
1962 goto error;
1963
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001964 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001965 if (string == NULL)
1966 goto error;
1967
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001968 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1969 _Pickler_Write(self, string, size) < 0 ||
1970 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001971 goto error;
1972 }
1973
1974 if (0) {
1975 error:
1976 status = -1;
1977 }
1978 Py_XDECREF(repr);
1979
1980 return status;
1981}
1982
1983static int
1984save_float(PicklerObject *self, PyObject *obj)
1985{
1986 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1987
1988 if (self->bin) {
1989 char pdata[9];
1990 pdata[0] = BINFLOAT;
1991 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1992 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001993 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001994 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001995 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001996 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001997 int result = -1;
1998 char *buf = NULL;
1999 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002000
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002001 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002002 goto done;
2003
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002004 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002005 if (!buf) {
2006 PyErr_NoMemory();
2007 goto done;
2008 }
2009
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002010 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002011 goto done;
2012
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002013 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002014 goto done;
2015
2016 result = 0;
2017done:
2018 PyMem_Free(buf);
2019 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002020 }
2021
2022 return 0;
2023}
2024
2025static int
2026save_bytes(PicklerObject *self, PyObject *obj)
2027{
2028 if (self->proto < 3) {
2029 /* Older pickle protocols do not have an opcode for pickling bytes
2030 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002031 the __reduce__ method) to permit bytes object unpickling.
2032
2033 Here we use a hack to be compatible with Python 2. Since in Python
2034 2 'bytes' is just an alias for 'str' (which has different
2035 parameters than the actual bytes object), we use codecs.encode
2036 to create the appropriate 'str' object when unpickled using
2037 Python 2 *and* the appropriate 'bytes' object when unpickled
2038 using Python 3. Again this is a hack and we don't need to do this
2039 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002040 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002041 int status;
2042
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002043 if (PyBytes_GET_SIZE(obj) == 0) {
2044 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2045 }
2046 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002047 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002048 PyObject *unicode_str =
2049 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2050 PyBytes_GET_SIZE(obj),
2051 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002052 _Py_IDENTIFIER(latin1);
2053
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002054 if (unicode_str == NULL)
2055 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002056 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002057 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002058 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002059 Py_DECREF(unicode_str);
2060 }
2061
2062 if (reduce_value == NULL)
2063 return -1;
2064
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002065 /* save_reduce() will memoize the object automatically. */
2066 status = save_reduce(self, reduce_value, obj);
2067 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002068 return status;
2069 }
2070 else {
2071 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002072 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002073 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002074
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002075 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 if (size < 0)
2077 return -1;
2078
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002079 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002080 header[0] = SHORT_BINBYTES;
2081 header[1] = (unsigned char)size;
2082 len = 2;
2083 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002084 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002085 header[0] = BINBYTES;
2086 header[1] = (unsigned char)(size & 0xff);
2087 header[2] = (unsigned char)((size >> 8) & 0xff);
2088 header[3] = (unsigned char)((size >> 16) & 0xff);
2089 header[4] = (unsigned char)((size >> 24) & 0xff);
2090 len = 5;
2091 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002092 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002093 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002094 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002095 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002096 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002097 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002098 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002099 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002100 return -1; /* string too large */
2101 }
2102
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002103 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002104 return -1;
2105
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002106 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002107 return -1;
2108
2109 if (memo_put(self, obj) < 0)
2110 return -1;
2111
2112 return 0;
2113 }
2114}
2115
2116/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2117 backslash and newline characters to \uXXXX escapes. */
2118static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002119raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002120{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002121 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002122 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002123 void *data;
2124 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002125 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002126
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002127 if (PyUnicode_READY(obj))
2128 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002129
Victor Stinner358af132015-10-12 22:36:57 +02002130 _PyBytesWriter_Init(&writer);
2131
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002132 size = PyUnicode_GET_LENGTH(obj);
2133 data = PyUnicode_DATA(obj);
2134 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002135
Victor Stinner358af132015-10-12 22:36:57 +02002136 p = _PyBytesWriter_Alloc(&writer, size);
2137 if (p == NULL)
2138 goto error;
2139 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002140
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002141 for (i=0; i < size; i++) {
2142 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002143 /* Map 32-bit characters to '\Uxxxxxxxx' */
2144 if (ch >= 0x10000) {
Victor Stinner358af132015-10-12 22:36:57 +02002145 /* -1: substract 1 preallocated byte */
2146 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2147 if (p == NULL)
2148 goto error;
2149
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002150 *p++ = '\\';
2151 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002152 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2153 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2154 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2155 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2156 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2157 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2158 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2159 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002160 }
Victor Stinner358af132015-10-12 22:36:57 +02002161 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002162 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Victor Stinner358af132015-10-12 22:36:57 +02002163 /* -1: substract 1 preallocated byte */
2164 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2165 if (p == NULL)
2166 goto error;
2167
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002168 *p++ = '\\';
2169 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002170 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2171 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2172 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2173 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002174 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002175 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002176 else
2177 *p++ = (char) ch;
2178 }
Victor Stinner358af132015-10-12 22:36:57 +02002179
2180 return _PyBytesWriter_Finish(&writer, p);
2181
2182error:
2183 _PyBytesWriter_Dealloc(&writer);
2184 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002185}
2186
2187static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002188write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002189{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002190 char header[9];
2191 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002192
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002193 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002194 if (size <= 0xff && self->proto >= 4) {
2195 header[0] = SHORT_BINUNICODE;
2196 header[1] = (unsigned char)(size & 0xff);
2197 len = 2;
2198 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002199 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002200 header[0] = BINUNICODE;
2201 header[1] = (unsigned char)(size & 0xff);
2202 header[2] = (unsigned char)((size >> 8) & 0xff);
2203 header[3] = (unsigned char)((size >> 16) & 0xff);
2204 header[4] = (unsigned char)((size >> 24) & 0xff);
2205 len = 5;
2206 }
2207 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002208 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002209 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002210 len = 9;
2211 }
2212 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002213 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002214 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002215 return -1;
2216 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002217
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002218 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002219 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002220 if (_Pickler_Write(self, data, size) < 0)
2221 return -1;
2222
2223 return 0;
2224}
2225
2226static int
2227write_unicode_binary(PicklerObject *self, PyObject *obj)
2228{
2229 PyObject *encoded = NULL;
2230 Py_ssize_t size;
2231 char *data;
2232 int r;
2233
2234 if (PyUnicode_READY(obj))
2235 return -1;
2236
2237 data = PyUnicode_AsUTF8AndSize(obj, &size);
2238 if (data != NULL)
2239 return write_utf8(self, data, size);
2240
2241 /* Issue #8383: for strings with lone surrogates, fallback on the
2242 "surrogatepass" error handler. */
2243 PyErr_Clear();
2244 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2245 if (encoded == NULL)
2246 return -1;
2247
2248 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2249 PyBytes_GET_SIZE(encoded));
2250 Py_DECREF(encoded);
2251 return r;
2252}
2253
2254static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255save_unicode(PicklerObject *self, PyObject *obj)
2256{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002258 if (write_unicode_binary(self, obj) < 0)
2259 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002260 }
2261 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002262 PyObject *encoded;
2263 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002264 const char unicode_op = UNICODE;
2265
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002266 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002267 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002268 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002269
Antoine Pitrou299978d2013-04-07 17:38:11 +02002270 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2271 Py_DECREF(encoded);
2272 return -1;
2273 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002274
2275 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002276 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2277 Py_DECREF(encoded);
2278 return -1;
2279 }
2280 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002281
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002282 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002283 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002284 }
2285 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002286 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002288 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289}
2290
2291/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2292static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002293store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002294{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002295 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296
2297 assert(PyTuple_Size(t) == len);
2298
2299 for (i = 0; i < len; i++) {
2300 PyObject *element = PyTuple_GET_ITEM(t, i);
2301
2302 if (element == NULL)
2303 return -1;
2304 if (save(self, element, 0) < 0)
2305 return -1;
2306 }
2307
2308 return 0;
2309}
2310
2311/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2312 * used across protocols to minimize the space needed to pickle them.
2313 * Tuples are also the only builtin immutable type that can be recursive
2314 * (a tuple can be reached from itself), and that requires some subtle
2315 * magic so that it works in all cases. IOW, this is a long routine.
2316 */
2317static int
2318save_tuple(PicklerObject *self, PyObject *obj)
2319{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002320 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002321
2322 const char mark_op = MARK;
2323 const char tuple_op = TUPLE;
2324 const char pop_op = POP;
2325 const char pop_mark_op = POP_MARK;
2326 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2327
2328 if ((len = PyTuple_Size(obj)) < 0)
2329 return -1;
2330
2331 if (len == 0) {
2332 char pdata[2];
2333
2334 if (self->proto) {
2335 pdata[0] = EMPTY_TUPLE;
2336 len = 1;
2337 }
2338 else {
2339 pdata[0] = MARK;
2340 pdata[1] = TUPLE;
2341 len = 2;
2342 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002343 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002344 return -1;
2345 return 0;
2346 }
2347
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002348 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002349 * saving the tuple elements, the tuple must be recursive, in
2350 * which case we'll pop everything we put on the stack, and fetch
2351 * its value from the memo.
2352 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002353 if (len <= 3 && self->proto >= 2) {
2354 /* Use TUPLE{1,2,3} opcodes. */
2355 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002356 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002357
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002358 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002359 /* pop the len elements */
2360 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002361 if (_Pickler_Write(self, &pop_op, 1) < 0)
2362 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002363 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002364 if (memo_get(self, obj) < 0)
2365 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002366
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002367 return 0;
2368 }
2369 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002370 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2371 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002372 }
2373 goto memoize;
2374 }
2375
2376 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2377 * Generate MARK e1 e2 ... TUPLE
2378 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002379 if (_Pickler_Write(self, &mark_op, 1) < 0)
2380 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002381
2382 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002383 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002384
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002385 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002386 /* pop the stack stuff we pushed */
2387 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002388 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2389 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002390 }
2391 else {
2392 /* Note that we pop one more than len, to remove
2393 * the MARK too.
2394 */
2395 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002396 if (_Pickler_Write(self, &pop_op, 1) < 0)
2397 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002398 }
2399 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002400 if (memo_get(self, obj) < 0)
2401 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002402
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002403 return 0;
2404 }
2405 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002406 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2407 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002408 }
2409
2410 memoize:
2411 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002412 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002413
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002414 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002415}
2416
2417/* iter is an iterator giving items, and we batch up chunks of
2418 * MARK item item ... item APPENDS
2419 * opcode sequences. Calling code should have arranged to first create an
2420 * empty list, or list-like object, for the APPENDS to operate on.
2421 * Returns 0 on success, <0 on error.
2422 */
2423static int
2424batch_list(PicklerObject *self, PyObject *iter)
2425{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002426 PyObject *obj = NULL;
2427 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002428 int i, n;
2429
2430 const char mark_op = MARK;
2431 const char append_op = APPEND;
2432 const char appends_op = APPENDS;
2433
2434 assert(iter != NULL);
2435
2436 /* XXX: I think this function could be made faster by avoiding the
2437 iterator interface and fetching objects directly from list using
2438 PyList_GET_ITEM.
2439 */
2440
2441 if (self->proto == 0) {
2442 /* APPENDS isn't available; do one at a time. */
2443 for (;;) {
2444 obj = PyIter_Next(iter);
2445 if (obj == NULL) {
2446 if (PyErr_Occurred())
2447 return -1;
2448 break;
2449 }
2450 i = save(self, obj, 0);
2451 Py_DECREF(obj);
2452 if (i < 0)
2453 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002454 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455 return -1;
2456 }
2457 return 0;
2458 }
2459
2460 /* proto > 0: write in batches of BATCHSIZE. */
2461 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002462 /* Get first item */
2463 firstitem = PyIter_Next(iter);
2464 if (firstitem == NULL) {
2465 if (PyErr_Occurred())
2466 goto error;
2467
2468 /* nothing more to add */
2469 break;
2470 }
2471
2472 /* Try to get a second item */
2473 obj = PyIter_Next(iter);
2474 if (obj == NULL) {
2475 if (PyErr_Occurred())
2476 goto error;
2477
2478 /* Only one item to write */
2479 if (save(self, firstitem, 0) < 0)
2480 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002481 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002482 goto error;
2483 Py_CLEAR(firstitem);
2484 break;
2485 }
2486
2487 /* More than one item to write */
2488
2489 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002490 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002491 goto error;
2492
2493 if (save(self, firstitem, 0) < 0)
2494 goto error;
2495 Py_CLEAR(firstitem);
2496 n = 1;
2497
2498 /* Fetch and save up to BATCHSIZE items */
2499 while (obj) {
2500 if (save(self, obj, 0) < 0)
2501 goto error;
2502 Py_CLEAR(obj);
2503 n += 1;
2504
2505 if (n == BATCHSIZE)
2506 break;
2507
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002508 obj = PyIter_Next(iter);
2509 if (obj == NULL) {
2510 if (PyErr_Occurred())
2511 goto error;
2512 break;
2513 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002514 }
2515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002516 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002517 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002518
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002519 } while (n == BATCHSIZE);
2520 return 0;
2521
2522 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002523 Py_XDECREF(firstitem);
2524 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002525 return -1;
2526}
2527
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002528/* This is a variant of batch_list() above, specialized for lists (with no
2529 * support for list subclasses). Like batch_list(), we batch up chunks of
2530 * MARK item item ... item APPENDS
2531 * opcode sequences. Calling code should have arranged to first create an
2532 * empty list, or list-like object, for the APPENDS to operate on.
2533 * Returns 0 on success, -1 on error.
2534 *
2535 * This version is considerably faster than batch_list(), if less general.
2536 *
2537 * Note that this only works for protocols > 0.
2538 */
2539static int
2540batch_list_exact(PicklerObject *self, PyObject *obj)
2541{
2542 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002543 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002544
2545 const char append_op = APPEND;
2546 const char appends_op = APPENDS;
2547 const char mark_op = MARK;
2548
2549 assert(obj != NULL);
2550 assert(self->proto > 0);
2551 assert(PyList_CheckExact(obj));
2552
2553 if (PyList_GET_SIZE(obj) == 1) {
2554 item = PyList_GET_ITEM(obj, 0);
2555 if (save(self, item, 0) < 0)
2556 return -1;
2557 if (_Pickler_Write(self, &append_op, 1) < 0)
2558 return -1;
2559 return 0;
2560 }
2561
2562 /* Write in batches of BATCHSIZE. */
2563 total = 0;
2564 do {
2565 this_batch = 0;
2566 if (_Pickler_Write(self, &mark_op, 1) < 0)
2567 return -1;
2568 while (total < PyList_GET_SIZE(obj)) {
2569 item = PyList_GET_ITEM(obj, total);
2570 if (save(self, item, 0) < 0)
2571 return -1;
2572 total++;
2573 if (++this_batch == BATCHSIZE)
2574 break;
2575 }
2576 if (_Pickler_Write(self, &appends_op, 1) < 0)
2577 return -1;
2578
2579 } while (total < PyList_GET_SIZE(obj));
2580
2581 return 0;
2582}
2583
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002584static int
2585save_list(PicklerObject *self, PyObject *obj)
2586{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002587 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002588 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002589 int status = 0;
2590
2591 if (self->fast && !fast_save_enter(self, obj))
2592 goto error;
2593
2594 /* Create an empty list. */
2595 if (self->bin) {
2596 header[0] = EMPTY_LIST;
2597 len = 1;
2598 }
2599 else {
2600 header[0] = MARK;
2601 header[1] = LIST;
2602 len = 2;
2603 }
2604
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002605 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002606 goto error;
2607
2608 /* Get list length, and bow out early if empty. */
2609 if ((len = PyList_Size(obj)) < 0)
2610 goto error;
2611
2612 if (memo_put(self, obj) < 0)
2613 goto error;
2614
2615 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002616 /* Materialize the list elements. */
2617 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002618 if (Py_EnterRecursiveCall(" while pickling an object"))
2619 goto error;
2620 status = batch_list_exact(self, obj);
2621 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002622 } else {
2623 PyObject *iter = PyObject_GetIter(obj);
2624 if (iter == NULL)
2625 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002626
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002627 if (Py_EnterRecursiveCall(" while pickling an object")) {
2628 Py_DECREF(iter);
2629 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002630 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002631 status = batch_list(self, iter);
2632 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002633 Py_DECREF(iter);
2634 }
2635 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002636 if (0) {
2637 error:
2638 status = -1;
2639 }
2640
2641 if (self->fast && !fast_save_leave(self, obj))
2642 status = -1;
2643
2644 return status;
2645}
2646
2647/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2648 * MARK key value ... key value SETITEMS
2649 * opcode sequences. Calling code should have arranged to first create an
2650 * empty dict, or dict-like object, for the SETITEMS to operate on.
2651 * Returns 0 on success, <0 on error.
2652 *
2653 * This is very much like batch_list(). The difference between saving
2654 * elements directly, and picking apart two-tuples, is so long-winded at
2655 * the C level, though, that attempts to combine these routines were too
2656 * ugly to bear.
2657 */
2658static int
2659batch_dict(PicklerObject *self, PyObject *iter)
2660{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002661 PyObject *obj = NULL;
2662 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002663 int i, n;
2664
2665 const char mark_op = MARK;
2666 const char setitem_op = SETITEM;
2667 const char setitems_op = SETITEMS;
2668
2669 assert(iter != NULL);
2670
2671 if (self->proto == 0) {
2672 /* SETITEMS isn't available; do one at a time. */
2673 for (;;) {
2674 obj = PyIter_Next(iter);
2675 if (obj == NULL) {
2676 if (PyErr_Occurred())
2677 return -1;
2678 break;
2679 }
2680 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2681 PyErr_SetString(PyExc_TypeError, "dict items "
2682 "iterator must return 2-tuples");
2683 return -1;
2684 }
2685 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2686 if (i >= 0)
2687 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2688 Py_DECREF(obj);
2689 if (i < 0)
2690 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002691 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002692 return -1;
2693 }
2694 return 0;
2695 }
2696
2697 /* proto > 0: write in batches of BATCHSIZE. */
2698 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002699 /* Get first item */
2700 firstitem = PyIter_Next(iter);
2701 if (firstitem == NULL) {
2702 if (PyErr_Occurred())
2703 goto error;
2704
2705 /* nothing more to add */
2706 break;
2707 }
2708 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2709 PyErr_SetString(PyExc_TypeError, "dict items "
2710 "iterator must return 2-tuples");
2711 goto error;
2712 }
2713
2714 /* Try to get a second item */
2715 obj = PyIter_Next(iter);
2716 if (obj == NULL) {
2717 if (PyErr_Occurred())
2718 goto error;
2719
2720 /* Only one item to write */
2721 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2722 goto error;
2723 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2724 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002725 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002726 goto error;
2727 Py_CLEAR(firstitem);
2728 break;
2729 }
2730
2731 /* More than one item to write */
2732
2733 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002734 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002735 goto error;
2736
2737 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2738 goto error;
2739 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2740 goto error;
2741 Py_CLEAR(firstitem);
2742 n = 1;
2743
2744 /* Fetch and save up to BATCHSIZE items */
2745 while (obj) {
2746 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2747 PyErr_SetString(PyExc_TypeError, "dict items "
2748 "iterator must return 2-tuples");
2749 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002750 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002751 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2752 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2753 goto error;
2754 Py_CLEAR(obj);
2755 n += 1;
2756
2757 if (n == BATCHSIZE)
2758 break;
2759
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002760 obj = PyIter_Next(iter);
2761 if (obj == NULL) {
2762 if (PyErr_Occurred())
2763 goto error;
2764 break;
2765 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002766 }
2767
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002768 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002769 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002770
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002771 } while (n == BATCHSIZE);
2772 return 0;
2773
2774 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002775 Py_XDECREF(firstitem);
2776 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002777 return -1;
2778}
2779
Collin Winter5c9b02d2009-05-25 05:43:30 +00002780/* This is a variant of batch_dict() above that specializes for dicts, with no
2781 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2782 * MARK key value ... key value SETITEMS
2783 * opcode sequences. Calling code should have arranged to first create an
2784 * empty dict, or dict-like object, for the SETITEMS to operate on.
2785 * Returns 0 on success, -1 on error.
2786 *
2787 * Note that this currently doesn't work for protocol 0.
2788 */
2789static int
2790batch_dict_exact(PicklerObject *self, PyObject *obj)
2791{
2792 PyObject *key = NULL, *value = NULL;
2793 int i;
2794 Py_ssize_t dict_size, ppos = 0;
2795
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002796 const char mark_op = MARK;
2797 const char setitem_op = SETITEM;
2798 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002799
2800 assert(obj != NULL);
2801 assert(self->proto > 0);
2802
2803 dict_size = PyDict_Size(obj);
2804
2805 /* Special-case len(d) == 1 to save space. */
2806 if (dict_size == 1) {
2807 PyDict_Next(obj, &ppos, &key, &value);
2808 if (save(self, key, 0) < 0)
2809 return -1;
2810 if (save(self, value, 0) < 0)
2811 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002812 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002813 return -1;
2814 return 0;
2815 }
2816
2817 /* Write in batches of BATCHSIZE. */
2818 do {
2819 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002820 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002821 return -1;
2822 while (PyDict_Next(obj, &ppos, &key, &value)) {
2823 if (save(self, key, 0) < 0)
2824 return -1;
2825 if (save(self, value, 0) < 0)
2826 return -1;
2827 if (++i == BATCHSIZE)
2828 break;
2829 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002830 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002831 return -1;
2832 if (PyDict_Size(obj) != dict_size) {
2833 PyErr_Format(
2834 PyExc_RuntimeError,
2835 "dictionary changed size during iteration");
2836 return -1;
2837 }
2838
2839 } while (i == BATCHSIZE);
2840 return 0;
2841}
2842
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002843static int
2844save_dict(PicklerObject *self, PyObject *obj)
2845{
2846 PyObject *items, *iter;
2847 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002848 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002849 int status = 0;
2850
2851 if (self->fast && !fast_save_enter(self, obj))
2852 goto error;
2853
2854 /* Create an empty dict. */
2855 if (self->bin) {
2856 header[0] = EMPTY_DICT;
2857 len = 1;
2858 }
2859 else {
2860 header[0] = MARK;
2861 header[1] = DICT;
2862 len = 2;
2863 }
2864
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002865 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002866 goto error;
2867
2868 /* Get dict size, and bow out early if empty. */
2869 if ((len = PyDict_Size(obj)) < 0)
2870 goto error;
2871
2872 if (memo_put(self, obj) < 0)
2873 goto error;
2874
2875 if (len != 0) {
2876 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002877 if (PyDict_CheckExact(obj) && self->proto > 0) {
2878 /* We can take certain shortcuts if we know this is a dict and
2879 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002880 if (Py_EnterRecursiveCall(" while pickling an object"))
2881 goto error;
2882 status = batch_dict_exact(self, obj);
2883 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002884 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002885 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002886
2887 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002888 if (items == NULL)
2889 goto error;
2890 iter = PyObject_GetIter(items);
2891 Py_DECREF(items);
2892 if (iter == NULL)
2893 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002894 if (Py_EnterRecursiveCall(" while pickling an object")) {
2895 Py_DECREF(iter);
2896 goto error;
2897 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002898 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002899 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002900 Py_DECREF(iter);
2901 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002902 }
2903
2904 if (0) {
2905 error:
2906 status = -1;
2907 }
2908
2909 if (self->fast && !fast_save_leave(self, obj))
2910 status = -1;
2911
2912 return status;
2913}
2914
2915static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002916save_set(PicklerObject *self, PyObject *obj)
2917{
2918 PyObject *item;
2919 int i;
2920 Py_ssize_t set_size, ppos = 0;
2921 Py_hash_t hash;
2922
2923 const char empty_set_op = EMPTY_SET;
2924 const char mark_op = MARK;
2925 const char additems_op = ADDITEMS;
2926
2927 if (self->proto < 4) {
2928 PyObject *items;
2929 PyObject *reduce_value;
2930 int status;
2931
2932 items = PySequence_List(obj);
2933 if (items == NULL) {
2934 return -1;
2935 }
2936 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2937 Py_DECREF(items);
2938 if (reduce_value == NULL) {
2939 return -1;
2940 }
2941 /* save_reduce() will memoize the object automatically. */
2942 status = save_reduce(self, reduce_value, obj);
2943 Py_DECREF(reduce_value);
2944 return status;
2945 }
2946
2947 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2948 return -1;
2949
2950 if (memo_put(self, obj) < 0)
2951 return -1;
2952
2953 set_size = PySet_GET_SIZE(obj);
2954 if (set_size == 0)
2955 return 0; /* nothing to do */
2956
2957 /* Write in batches of BATCHSIZE. */
2958 do {
2959 i = 0;
2960 if (_Pickler_Write(self, &mark_op, 1) < 0)
2961 return -1;
2962 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2963 if (save(self, item, 0) < 0)
2964 return -1;
2965 if (++i == BATCHSIZE)
2966 break;
2967 }
2968 if (_Pickler_Write(self, &additems_op, 1) < 0)
2969 return -1;
2970 if (PySet_GET_SIZE(obj) != set_size) {
2971 PyErr_Format(
2972 PyExc_RuntimeError,
2973 "set changed size during iteration");
2974 return -1;
2975 }
2976 } while (i == BATCHSIZE);
2977
2978 return 0;
2979}
2980
2981static int
2982save_frozenset(PicklerObject *self, PyObject *obj)
2983{
2984 PyObject *iter;
2985
2986 const char mark_op = MARK;
2987 const char frozenset_op = FROZENSET;
2988
2989 if (self->fast && !fast_save_enter(self, obj))
2990 return -1;
2991
2992 if (self->proto < 4) {
2993 PyObject *items;
2994 PyObject *reduce_value;
2995 int status;
2996
2997 items = PySequence_List(obj);
2998 if (items == NULL) {
2999 return -1;
3000 }
3001 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3002 items);
3003 Py_DECREF(items);
3004 if (reduce_value == NULL) {
3005 return -1;
3006 }
3007 /* save_reduce() will memoize the object automatically. */
3008 status = save_reduce(self, reduce_value, obj);
3009 Py_DECREF(reduce_value);
3010 return status;
3011 }
3012
3013 if (_Pickler_Write(self, &mark_op, 1) < 0)
3014 return -1;
3015
3016 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003017 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003018 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003019 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003020 for (;;) {
3021 PyObject *item;
3022
3023 item = PyIter_Next(iter);
3024 if (item == NULL) {
3025 if (PyErr_Occurred()) {
3026 Py_DECREF(iter);
3027 return -1;
3028 }
3029 break;
3030 }
3031 if (save(self, item, 0) < 0) {
3032 Py_DECREF(item);
3033 Py_DECREF(iter);
3034 return -1;
3035 }
3036 Py_DECREF(item);
3037 }
3038 Py_DECREF(iter);
3039
3040 /* If the object is already in the memo, this means it is
3041 recursive. In this case, throw away everything we put on the
3042 stack, and fetch the object back from the memo. */
3043 if (PyMemoTable_Get(self->memo, obj)) {
3044 const char pop_mark_op = POP_MARK;
3045
3046 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3047 return -1;
3048 if (memo_get(self, obj) < 0)
3049 return -1;
3050 return 0;
3051 }
3052
3053 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3054 return -1;
3055 if (memo_put(self, obj) < 0)
3056 return -1;
3057
3058 return 0;
3059}
3060
3061static int
3062fix_imports(PyObject **module_name, PyObject **global_name)
3063{
3064 PyObject *key;
3065 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003066 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003067
3068 key = PyTuple_Pack(2, *module_name, *global_name);
3069 if (key == NULL)
3070 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003071 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003072 Py_DECREF(key);
3073 if (item) {
3074 PyObject *fixed_module_name;
3075 PyObject *fixed_global_name;
3076
3077 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3078 PyErr_Format(PyExc_RuntimeError,
3079 "_compat_pickle.REVERSE_NAME_MAPPING values "
3080 "should be 2-tuples, not %.200s",
3081 Py_TYPE(item)->tp_name);
3082 return -1;
3083 }
3084 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3085 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3086 if (!PyUnicode_Check(fixed_module_name) ||
3087 !PyUnicode_Check(fixed_global_name)) {
3088 PyErr_Format(PyExc_RuntimeError,
3089 "_compat_pickle.REVERSE_NAME_MAPPING values "
3090 "should be pairs of str, not (%.200s, %.200s)",
3091 Py_TYPE(fixed_module_name)->tp_name,
3092 Py_TYPE(fixed_global_name)->tp_name);
3093 return -1;
3094 }
3095
3096 Py_CLEAR(*module_name);
3097 Py_CLEAR(*global_name);
3098 Py_INCREF(fixed_module_name);
3099 Py_INCREF(fixed_global_name);
3100 *module_name = fixed_module_name;
3101 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003102 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003103 }
3104 else if (PyErr_Occurred()) {
3105 return -1;
3106 }
3107
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003108 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003109 if (item) {
3110 if (!PyUnicode_Check(item)) {
3111 PyErr_Format(PyExc_RuntimeError,
3112 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3113 "should be strings, not %.200s",
3114 Py_TYPE(item)->tp_name);
3115 return -1;
3116 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003117 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003118 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003119 }
3120 else if (PyErr_Occurred()) {
3121 return -1;
3122 }
3123
3124 return 0;
3125}
3126
3127static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003128save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3129{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003130 PyObject *global_name = NULL;
3131 PyObject *module_name = NULL;
3132 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003133 PyObject *parent = NULL;
3134 PyObject *dotted_path = NULL;
3135 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003136 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003137 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003138 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003139 _Py_IDENTIFIER(__name__);
3140 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003141
3142 const char global_op = GLOBAL;
3143
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003144 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003145 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003146 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003147 }
3148 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003149 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3150 if (global_name == NULL) {
3151 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3152 goto error;
3153 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003154 }
3155 if (global_name == NULL) {
3156 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3157 if (global_name == NULL)
3158 goto error;
3159 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003160 }
3161
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003162 dotted_path = get_dotted_path(module, global_name);
3163 if (dotted_path == NULL)
3164 goto error;
3165 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003166 if (module_name == NULL)
3167 goto error;
3168
3169 /* XXX: Change to use the import C API directly with level=0 to disallow
3170 relative imports.
3171
3172 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3173 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3174 custom import functions (IMHO, this would be a nice security
3175 feature). The import C API would need to be extended to support the
3176 extra parameters of __import__ to fix that. */
3177 module = PyImport_Import(module_name);
3178 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003179 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003180 "Can't pickle %R: import of module %R failed",
3181 obj, module_name);
3182 goto error;
3183 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003184 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3185 Py_INCREF(lastname);
3186 cls = get_deep_attribute(module, dotted_path, &parent);
3187 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003188 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003189 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003190 "Can't pickle %R: attribute lookup %S on %S failed",
3191 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003192 goto error;
3193 }
3194 if (cls != obj) {
3195 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003196 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003197 "Can't pickle %R: it's not the same object as %S.%S",
3198 obj, module_name, global_name);
3199 goto error;
3200 }
3201 Py_DECREF(cls);
3202
3203 if (self->proto >= 2) {
3204 /* See whether this is in the extension registry, and if
3205 * so generate an EXT opcode.
3206 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003207 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003208 PyObject *code_obj; /* extension code as Python object */
3209 long code; /* extension code as C value */
3210 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003211 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003212
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003213 extension_key = PyTuple_Pack(2, module_name, global_name);
3214 if (extension_key == NULL) {
3215 goto error;
3216 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003217 code_obj = PyDict_GetItemWithError(st->extension_registry,
3218 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003219 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003220 /* The object is not registered in the extension registry.
3221 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003222 if (code_obj == NULL) {
3223 if (PyErr_Occurred()) {
3224 goto error;
3225 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003226 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003227 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003228
3229 /* XXX: pickle.py doesn't check neither the type, nor the range
3230 of the value returned by the extension_registry. It should for
3231 consistency. */
3232
3233 /* Verify code_obj has the right type and value. */
3234 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003235 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003236 "Can't pickle %R: extension code %R isn't an integer",
3237 obj, code_obj);
3238 goto error;
3239 }
3240 code = PyLong_AS_LONG(code_obj);
3241 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003242 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003243 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3244 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003245 goto error;
3246 }
3247
3248 /* Generate an EXT opcode. */
3249 if (code <= 0xff) {
3250 pdata[0] = EXT1;
3251 pdata[1] = (unsigned char)code;
3252 n = 2;
3253 }
3254 else if (code <= 0xffff) {
3255 pdata[0] = EXT2;
3256 pdata[1] = (unsigned char)(code & 0xff);
3257 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3258 n = 3;
3259 }
3260 else {
3261 pdata[0] = EXT4;
3262 pdata[1] = (unsigned char)(code & 0xff);
3263 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3264 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3265 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3266 n = 5;
3267 }
3268
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003269 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003270 goto error;
3271 }
3272 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003273 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003274 if (parent == module) {
3275 Py_INCREF(lastname);
3276 Py_DECREF(global_name);
3277 global_name = lastname;
3278 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003279 if (self->proto >= 4) {
3280 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003281
Christian Heimese8b1ba12013-11-23 21:13:39 +01003282 if (save(self, module_name, 0) < 0)
3283 goto error;
3284 if (save(self, global_name, 0) < 0)
3285 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003286
3287 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3288 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003289 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003290 else if (parent != module) {
3291 PickleState *st = _Pickle_GetGlobalState();
3292 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3293 st->getattr, parent, lastname);
3294 status = save_reduce(self, reduce_value, NULL);
3295 Py_DECREF(reduce_value);
3296 if (status < 0)
3297 goto error;
3298 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003299 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003300 /* Generate a normal global opcode if we are using a pickle
3301 protocol < 4, or if the object is not registered in the
3302 extension registry. */
3303 PyObject *encoded;
3304 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003305
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003306 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003307 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003308
3309 /* For protocol < 3 and if the user didn't request against doing
3310 so, we convert module names to the old 2.x module names. */
3311 if (self->proto < 3 && self->fix_imports) {
3312 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003313 goto error;
3314 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003315 }
3316
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003317 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3318 both the module name and the global name using UTF-8. We do so
3319 only when we are using the pickle protocol newer than version
3320 3. This is to ensure compatibility with older Unpickler running
3321 on Python 2.x. */
3322 if (self->proto == 3) {
3323 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003324 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003325 else {
3326 unicode_encoder = PyUnicode_AsASCIIString;
3327 }
3328 encoded = unicode_encoder(module_name);
3329 if (encoded == NULL) {
3330 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003331 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003332 "can't pickle module identifier '%S' using "
3333 "pickle protocol %i",
3334 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003335 goto error;
3336 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003337 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3338 PyBytes_GET_SIZE(encoded)) < 0) {
3339 Py_DECREF(encoded);
3340 goto error;
3341 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003342 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003343 if(_Pickler_Write(self, "\n", 1) < 0)
3344 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003345
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003346 /* Save the name of the module. */
3347 encoded = unicode_encoder(global_name);
3348 if (encoded == NULL) {
3349 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003350 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003351 "can't pickle global identifier '%S' using "
3352 "pickle protocol %i",
3353 global_name, self->proto);
3354 goto error;
3355 }
3356 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3357 PyBytes_GET_SIZE(encoded)) < 0) {
3358 Py_DECREF(encoded);
3359 goto error;
3360 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003361 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003362 if (_Pickler_Write(self, "\n", 1) < 0)
3363 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003364 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003365 /* Memoize the object. */
3366 if (memo_put(self, obj) < 0)
3367 goto error;
3368 }
3369
3370 if (0) {
3371 error:
3372 status = -1;
3373 }
3374 Py_XDECREF(module_name);
3375 Py_XDECREF(global_name);
3376 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003377 Py_XDECREF(parent);
3378 Py_XDECREF(dotted_path);
3379 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003380
3381 return status;
3382}
3383
3384static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003385save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3386{
3387 PyObject *reduce_value;
3388 int status;
3389
3390 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3391 if (reduce_value == NULL) {
3392 return -1;
3393 }
3394 status = save_reduce(self, reduce_value, obj);
3395 Py_DECREF(reduce_value);
3396 return status;
3397}
3398
3399static int
3400save_type(PicklerObject *self, PyObject *obj)
3401{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003402 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003403 return save_singleton_type(self, obj, Py_None);
3404 }
3405 else if (obj == (PyObject *)&PyEllipsis_Type) {
3406 return save_singleton_type(self, obj, Py_Ellipsis);
3407 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003408 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003409 return save_singleton_type(self, obj, Py_NotImplemented);
3410 }
3411 return save_global(self, obj, NULL);
3412}
3413
3414static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003415save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3416{
3417 PyObject *pid = NULL;
3418 int status = 0;
3419
3420 const char persid_op = PERSID;
3421 const char binpersid_op = BINPERSID;
3422
3423 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003424 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003425 if (pid == NULL)
3426 return -1;
3427
3428 if (pid != Py_None) {
3429 if (self->bin) {
3430 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003431 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003432 goto error;
3433 }
3434 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003435 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003436
3437 pid_str = PyObject_Str(pid);
3438 if (pid_str == NULL)
3439 goto error;
3440
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003441 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003442 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003443 if (!PyUnicode_IS_ASCII(pid_str)) {
3444 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3445 "persistent IDs in protocol 0 must be "
3446 "ASCII strings");
3447 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003448 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003449 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003450
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003451 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003452 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3453 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3454 _Pickler_Write(self, "\n", 1) < 0) {
3455 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003456 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003457 }
3458 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003459 }
3460 status = 1;
3461 }
3462
3463 if (0) {
3464 error:
3465 status = -1;
3466 }
3467 Py_XDECREF(pid);
3468
3469 return status;
3470}
3471
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003472static PyObject *
3473get_class(PyObject *obj)
3474{
3475 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003476 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003477
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003478 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003479 if (cls == NULL) {
3480 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3481 PyErr_Clear();
3482 cls = (PyObject *) Py_TYPE(obj);
3483 Py_INCREF(cls);
3484 }
3485 }
3486 return cls;
3487}
3488
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003489/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3490 * appropriate __reduce__ method for obj.
3491 */
3492static int
3493save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3494{
3495 PyObject *callable;
3496 PyObject *argtup;
3497 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003498 PyObject *listitems = Py_None;
3499 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003500 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003501 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003502 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003503
3504 const char reduce_op = REDUCE;
3505 const char build_op = BUILD;
3506 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003507 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003508
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003509 size = PyTuple_Size(args);
3510 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003511 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003512 "__reduce__ must contain 2 through 5 elements");
3513 return -1;
3514 }
3515
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003516 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3517 &callable, &argtup, &state, &listitems, &dictitems))
3518 return -1;
3519
3520 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003521 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003522 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003523 return -1;
3524 }
3525 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003526 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003527 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003528 return -1;
3529 }
3530
3531 if (state == Py_None)
3532 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003533
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003534 if (listitems == Py_None)
3535 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003536 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003537 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003538 "returned by __reduce__ must be an iterator, not %s",
3539 Py_TYPE(listitems)->tp_name);
3540 return -1;
3541 }
3542
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003543 if (dictitems == Py_None)
3544 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003545 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003546 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003547 "returned by __reduce__ must be an iterator, not %s",
3548 Py_TYPE(dictitems)->tp_name);
3549 return -1;
3550 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003551
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003552 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003553 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003554 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003555
Victor Stinner804e05e2013-11-14 01:26:17 +01003556 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003557 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003558 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003559 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003560 }
3561 PyErr_Clear();
3562 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003563 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003564 _Py_IDENTIFIER(__newobj_ex__);
3565 use_newobj_ex = PyUnicode_Compare(
3566 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003567 if (!use_newobj_ex) {
3568 _Py_IDENTIFIER(__newobj__);
3569 use_newobj = PyUnicode_Compare(
3570 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3571 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003572 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003573 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003574 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003575
3576 if (use_newobj_ex) {
3577 PyObject *cls;
3578 PyObject *args;
3579 PyObject *kwargs;
3580
3581 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003582 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003583 "length of the NEWOBJ_EX argument tuple must be "
3584 "exactly 3, not %zd", Py_SIZE(argtup));
3585 return -1;
3586 }
3587
3588 cls = PyTuple_GET_ITEM(argtup, 0);
3589 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003590 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003591 "first item from NEWOBJ_EX argument tuple must "
3592 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3593 return -1;
3594 }
3595 args = PyTuple_GET_ITEM(argtup, 1);
3596 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003597 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003598 "second item from NEWOBJ_EX argument tuple must "
3599 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3600 return -1;
3601 }
3602 kwargs = PyTuple_GET_ITEM(argtup, 2);
3603 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003604 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003605 "third item from NEWOBJ_EX argument tuple must "
3606 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3607 return -1;
3608 }
3609
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003610 if (self->proto >= 4) {
3611 if (save(self, cls, 0) < 0 ||
3612 save(self, args, 0) < 0 ||
3613 save(self, kwargs, 0) < 0 ||
3614 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3615 return -1;
3616 }
3617 }
3618 else {
3619 PyObject *newargs;
3620 PyObject *cls_new;
3621 Py_ssize_t i;
3622 _Py_IDENTIFIER(__new__);
3623
3624 newargs = PyTuple_New(Py_SIZE(args) + 2);
3625 if (newargs == NULL)
3626 return -1;
3627
3628 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3629 if (cls_new == NULL) {
3630 Py_DECREF(newargs);
3631 return -1;
3632 }
3633 PyTuple_SET_ITEM(newargs, 0, cls_new);
3634 Py_INCREF(cls);
3635 PyTuple_SET_ITEM(newargs, 1, cls);
3636 for (i = 0; i < Py_SIZE(args); i++) {
3637 PyObject *item = PyTuple_GET_ITEM(args, i);
3638 Py_INCREF(item);
3639 PyTuple_SET_ITEM(newargs, i + 2, item);
3640 }
3641
3642 callable = PyObject_Call(st->partial, newargs, kwargs);
3643 Py_DECREF(newargs);
3644 if (callable == NULL)
3645 return -1;
3646
3647 newargs = PyTuple_New(0);
3648 if (newargs == NULL) {
3649 Py_DECREF(callable);
3650 return -1;
3651 }
3652
3653 if (save(self, callable, 0) < 0 ||
3654 save(self, newargs, 0) < 0 ||
3655 _Pickler_Write(self, &reduce_op, 1) < 0) {
3656 Py_DECREF(newargs);
3657 Py_DECREF(callable);
3658 return -1;
3659 }
3660 Py_DECREF(newargs);
3661 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003662 }
3663 }
3664 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003665 PyObject *cls;
3666 PyObject *newargtup;
3667 PyObject *obj_class;
3668 int p;
3669
3670 /* Sanity checks. */
3671 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003672 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003673 return -1;
3674 }
3675
3676 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003677 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003678 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003679 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003680 return -1;
3681 }
3682
3683 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003684 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003685 p = obj_class != cls; /* true iff a problem */
3686 Py_DECREF(obj_class);
3687 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003688 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003689 "__newobj__ args has the wrong class");
3690 return -1;
3691 }
3692 }
3693 /* XXX: These calls save() are prone to infinite recursion. Imagine
3694 what happen if the value returned by the __reduce__() method of
3695 some extension type contains another object of the same type. Ouch!
3696
3697 Here is a quick example, that I ran into, to illustrate what I
3698 mean:
3699
3700 >>> import pickle, copyreg
3701 >>> copyreg.dispatch_table.pop(complex)
3702 >>> pickle.dumps(1+2j)
3703 Traceback (most recent call last):
3704 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003705 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003706
3707 Removing the complex class from copyreg.dispatch_table made the
3708 __reduce_ex__() method emit another complex object:
3709
3710 >>> (1+1j).__reduce_ex__(2)
3711 (<function __newobj__ at 0xb7b71c3c>,
3712 (<class 'complex'>, (1+1j)), None, None, None)
3713
3714 Thus when save() was called on newargstup (the 2nd item) recursion
3715 ensued. Of course, the bug was in the complex class which had a
3716 broken __getnewargs__() that emitted another complex object. But,
3717 the point, here, is it is quite easy to end up with a broken reduce
3718 function. */
3719
3720 /* Save the class and its __new__ arguments. */
3721 if (save(self, cls, 0) < 0)
3722 return -1;
3723
3724 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3725 if (newargtup == NULL)
3726 return -1;
3727
3728 p = save(self, newargtup, 0);
3729 Py_DECREF(newargtup);
3730 if (p < 0)
3731 return -1;
3732
3733 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003734 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003735 return -1;
3736 }
3737 else { /* Not using NEWOBJ. */
3738 if (save(self, callable, 0) < 0 ||
3739 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003740 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003741 return -1;
3742 }
3743
3744 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3745 the caller do not want to memoize the object. Not particularly useful,
3746 but that is to mimic the behavior save_reduce() in pickle.py when
3747 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003748 if (obj != NULL) {
3749 /* If the object is already in the memo, this means it is
3750 recursive. In this case, throw away everything we put on the
3751 stack, and fetch the object back from the memo. */
3752 if (PyMemoTable_Get(self->memo, obj)) {
3753 const char pop_op = POP;
3754
3755 if (_Pickler_Write(self, &pop_op, 1) < 0)
3756 return -1;
3757 if (memo_get(self, obj) < 0)
3758 return -1;
3759
3760 return 0;
3761 }
3762 else if (memo_put(self, obj) < 0)
3763 return -1;
3764 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003765
3766 if (listitems && batch_list(self, listitems) < 0)
3767 return -1;
3768
3769 if (dictitems && batch_dict(self, dictitems) < 0)
3770 return -1;
3771
3772 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003773 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003774 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003775 return -1;
3776 }
3777
3778 return 0;
3779}
3780
3781static int
3782save(PicklerObject *self, PyObject *obj, int pers_save)
3783{
3784 PyTypeObject *type;
3785 PyObject *reduce_func = NULL;
3786 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003787 int status = 0;
3788
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003789 if (_Pickler_OpcodeBoundary(self) < 0)
3790 return -1;
3791
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003792 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003793 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003794
3795 /* The extra pers_save argument is necessary to avoid calling save_pers()
3796 on its returned object. */
3797 if (!pers_save && self->pers_func) {
3798 /* save_pers() returns:
3799 -1 to signal an error;
3800 0 if it did nothing successfully;
3801 1 if a persistent id was saved.
3802 */
3803 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3804 goto done;
3805 }
3806
3807 type = Py_TYPE(obj);
3808
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003809 /* The old cPickle had an optimization that used switch-case statement
3810 dispatching on the first letter of the type name. This has was removed
3811 since benchmarks shown that this optimization was actually slowing
3812 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003813
3814 /* Atom types; these aren't memoized, so don't check the memo. */
3815
3816 if (obj == Py_None) {
3817 status = save_none(self, obj);
3818 goto done;
3819 }
3820 else if (obj == Py_False || obj == Py_True) {
3821 status = save_bool(self, obj);
3822 goto done;
3823 }
3824 else if (type == &PyLong_Type) {
3825 status = save_long(self, obj);
3826 goto done;
3827 }
3828 else if (type == &PyFloat_Type) {
3829 status = save_float(self, obj);
3830 goto done;
3831 }
3832
3833 /* Check the memo to see if it has the object. If so, generate
3834 a GET (or BINGET) opcode, instead of pickling the object
3835 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003836 if (PyMemoTable_Get(self->memo, obj)) {
3837 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003838 goto error;
3839 goto done;
3840 }
3841
3842 if (type == &PyBytes_Type) {
3843 status = save_bytes(self, obj);
3844 goto done;
3845 }
3846 else if (type == &PyUnicode_Type) {
3847 status = save_unicode(self, obj);
3848 goto done;
3849 }
3850 else if (type == &PyDict_Type) {
3851 status = save_dict(self, obj);
3852 goto done;
3853 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003854 else if (type == &PySet_Type) {
3855 status = save_set(self, obj);
3856 goto done;
3857 }
3858 else if (type == &PyFrozenSet_Type) {
3859 status = save_frozenset(self, obj);
3860 goto done;
3861 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003862 else if (type == &PyList_Type) {
3863 status = save_list(self, obj);
3864 goto done;
3865 }
3866 else if (type == &PyTuple_Type) {
3867 status = save_tuple(self, obj);
3868 goto done;
3869 }
3870 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003871 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003872 goto done;
3873 }
3874 else if (type == &PyFunction_Type) {
3875 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003876 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003877 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003878
3879 /* XXX: This part needs some unit tests. */
3880
3881 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003882 * self.dispatch_table, copyreg.dispatch_table, the object's
3883 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003884 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003885 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003886 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003887 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3888 (PyObject *)type);
3889 if (reduce_func == NULL) {
3890 if (PyErr_Occurred()) {
3891 goto error;
3892 }
3893 } else {
3894 /* PyDict_GetItemWithError() returns a borrowed reference.
3895 Increase the reference count to be consistent with
3896 PyObject_GetItem and _PyObject_GetAttrId used below. */
3897 Py_INCREF(reduce_func);
3898 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003899 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003900 reduce_func = PyObject_GetItem(self->dispatch_table,
3901 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003902 if (reduce_func == NULL) {
3903 if (PyErr_ExceptionMatches(PyExc_KeyError))
3904 PyErr_Clear();
3905 else
3906 goto error;
3907 }
3908 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003909 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003910 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003911 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003912 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003913 else if (PyType_IsSubtype(type, &PyType_Type)) {
3914 status = save_global(self, obj, NULL);
3915 goto done;
3916 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003917 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003918 _Py_IDENTIFIER(__reduce__);
3919 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003920
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003921
3922 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3923 automatically defined as __reduce__. While this is convenient, this
3924 make it impossible to know which method was actually called. Of
3925 course, this is not a big deal. But still, it would be nice to let
3926 the user know which method was called when something go
3927 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3928 don't actually have to check for a __reduce__ method. */
3929
3930 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003931 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003932 if (reduce_func != NULL) {
3933 PyObject *proto;
3934 proto = PyLong_FromLong(self->proto);
3935 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003936 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003937 }
3938 }
3939 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003940 PickleState *st = _Pickle_GetGlobalState();
3941
3942 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003944 }
3945 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003946 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003947 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003948 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003949 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003950 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02003951 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003952 }
3953 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003954 PyErr_Format(st->PicklingError,
3955 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003956 type->tp_name, obj);
3957 goto error;
3958 }
3959 }
3960 }
3961
3962 if (reduce_value == NULL)
3963 goto error;
3964
3965 if (PyUnicode_Check(reduce_value)) {
3966 status = save_global(self, obj, reduce_value);
3967 goto done;
3968 }
3969
3970 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003971 PickleState *st = _Pickle_GetGlobalState();
3972 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 "__reduce__ must return a string or tuple");
3974 goto error;
3975 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003976
3977 status = save_reduce(self, reduce_value, obj);
3978
3979 if (0) {
3980 error:
3981 status = -1;
3982 }
3983 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003984
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003985 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003986 Py_XDECREF(reduce_func);
3987 Py_XDECREF(reduce_value);
3988
3989 return status;
3990}
3991
3992static int
3993dump(PicklerObject *self, PyObject *obj)
3994{
3995 const char stop_op = STOP;
3996
3997 if (self->proto >= 2) {
3998 char header[2];
3999
4000 header[0] = PROTO;
4001 assert(self->proto >= 0 && self->proto < 256);
4002 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004003 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004004 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004005 if (self->proto >= 4)
4006 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004007 }
4008
4009 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004010 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004011 return -1;
4012
4013 return 0;
4014}
4015
Larry Hastings61272b72014-01-07 12:41:53 -08004016/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004017
4018_pickle.Pickler.clear_memo
4019
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004020Clears the pickler's "memo".
4021
4022The memo is the data structure that remembers which objects the
4023pickler has already seen, so that shared or recursive objects are
4024pickled by reference and not by value. This method is useful when
4025re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004026[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004027
Larry Hastings3cceb382014-01-04 11:09:09 -08004028static PyObject *
4029_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004030/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031{
4032 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004033 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004034
4035 Py_RETURN_NONE;
4036}
4037
Larry Hastings61272b72014-01-07 12:41:53 -08004038/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004039
4040_pickle.Pickler.dump
4041
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004042 obj: object
4043 /
4044
4045Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004046[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004047
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004048static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004049_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004050/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004051{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004052 /* Check whether the Pickler was initialized correctly (issue3664).
4053 Developers often forget to call __init__() in their subclasses, which
4054 would trigger a segfault without this check. */
4055 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004056 PickleState *st = _Pickle_GetGlobalState();
4057 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004058 "Pickler.__init__() was not called by %s.__init__()",
4059 Py_TYPE(self)->tp_name);
4060 return NULL;
4061 }
4062
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004063 if (_Pickler_ClearBuffer(self) < 0)
4064 return NULL;
4065
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004066 if (dump(self, obj) < 0)
4067 return NULL;
4068
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004069 if (_Pickler_FlushToFile(self) < 0)
4070 return NULL;
4071
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004072 Py_RETURN_NONE;
4073}
4074
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004075/*[clinic input]
4076
4077_pickle.Pickler.__sizeof__ -> Py_ssize_t
4078
4079Returns size in memory, in bytes.
4080[clinic start generated code]*/
4081
4082static Py_ssize_t
4083_pickle_Pickler___sizeof___impl(PicklerObject *self)
4084/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4085{
4086 Py_ssize_t res, s;
4087
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004088 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004089 if (self->memo != NULL) {
4090 res += sizeof(PyMemoTable);
4091 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4092 }
4093 if (self->output_buffer != NULL) {
4094 s = _PySys_GetSizeOf(self->output_buffer);
4095 if (s == -1)
4096 return -1;
4097 res += s;
4098 }
4099 return res;
4100}
4101
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004102static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004103 _PICKLE_PICKLER_DUMP_METHODDEF
4104 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004105 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004106 {NULL, NULL} /* sentinel */
4107};
4108
4109static void
4110Pickler_dealloc(PicklerObject *self)
4111{
4112 PyObject_GC_UnTrack(self);
4113
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004114 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004115 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004116 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004117 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004118 Py_XDECREF(self->fast_memo);
4119
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004120 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004121
4122 Py_TYPE(self)->tp_free((PyObject *)self);
4123}
4124
4125static int
4126Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4127{
4128 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004130 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004131 Py_VISIT(self->fast_memo);
4132 return 0;
4133}
4134
4135static int
4136Pickler_clear(PicklerObject *self)
4137{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004138 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004139 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004140 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004141 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004142 Py_CLEAR(self->fast_memo);
4143
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004144 if (self->memo != NULL) {
4145 PyMemoTable *memo = self->memo;
4146 self->memo = NULL;
4147 PyMemoTable_Del(memo);
4148 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004149 return 0;
4150}
4151
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004152
Larry Hastings61272b72014-01-07 12:41:53 -08004153/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004154
4155_pickle.Pickler.__init__
4156
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004157 file: object
4158 protocol: object = NULL
4159 fix_imports: bool = True
4160
4161This takes a binary file for writing a pickle data stream.
4162
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004163The optional *protocol* argument tells the pickler to use the given
4164protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4165protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004166
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004167Specifying a negative protocol version selects the highest protocol
4168version supported. The higher the protocol used, the more recent the
4169version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004171The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004173writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004174this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004175
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004176If *fix_imports* is True and protocol is less than 3, pickle will try
4177to map the new Python 3 names to the old module names used in Python
41782, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004179[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004180
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004181static int
Larry Hastings89964c42015-04-14 18:07:59 -04004182_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4183 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004184/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004185{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004186 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004187 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004188
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004189 /* In case of multiple __init__() calls, clear previous content. */
4190 if (self->write != NULL)
4191 (void)Pickler_clear(self);
4192
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004193 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004194 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004195
4196 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004197 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004198
4199 /* memo and output_buffer may have already been created in _Pickler_New */
4200 if (self->memo == NULL) {
4201 self->memo = PyMemoTable_New();
4202 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004203 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004204 }
4205 self->output_len = 0;
4206 if (self->output_buffer == NULL) {
4207 self->max_output_len = WRITE_BUF_SIZE;
4208 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4209 self->max_output_len);
4210 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004211 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004212 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004213
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004214 self->fast = 0;
4215 self->fast_nesting = 0;
4216 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004217 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004218 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4219 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4220 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004221 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004222 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004223 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004224 self->dispatch_table = NULL;
4225 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4226 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4227 &PyId_dispatch_table);
4228 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004229 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004230 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004231
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004232 return 0;
4233}
4234
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004235
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004236/* Define a proxy object for the Pickler's internal memo object. This is to
4237 * avoid breaking code like:
4238 * pickler.memo.clear()
4239 * and
4240 * pickler.memo = saved_memo
4241 * Is this a good idea? Not really, but we don't want to break code that uses
4242 * it. Note that we don't implement the entire mapping API here. This is
4243 * intentional, as these should be treated as black-box implementation details.
4244 */
4245
Larry Hastings61272b72014-01-07 12:41:53 -08004246/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004247_pickle.PicklerMemoProxy.clear
4248
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004249Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004250[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004251
Larry Hastings3cceb382014-01-04 11:09:09 -08004252static PyObject *
4253_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004254/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004255{
4256 if (self->pickler->memo)
4257 PyMemoTable_Clear(self->pickler->memo);
4258 Py_RETURN_NONE;
4259}
4260
Larry Hastings61272b72014-01-07 12:41:53 -08004261/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004262_pickle.PicklerMemoProxy.copy
4263
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004264Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004265[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004266
Larry Hastings3cceb382014-01-04 11:09:09 -08004267static PyObject *
4268_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004269/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004270{
4271 Py_ssize_t i;
4272 PyMemoTable *memo;
4273 PyObject *new_memo = PyDict_New();
4274 if (new_memo == NULL)
4275 return NULL;
4276
4277 memo = self->pickler->memo;
4278 for (i = 0; i < memo->mt_allocated; ++i) {
4279 PyMemoEntry entry = memo->mt_table[i];
4280 if (entry.me_key != NULL) {
4281 int status;
4282 PyObject *key, *value;
4283
4284 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004285 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004286
4287 if (key == NULL || value == NULL) {
4288 Py_XDECREF(key);
4289 Py_XDECREF(value);
4290 goto error;
4291 }
4292 status = PyDict_SetItem(new_memo, key, value);
4293 Py_DECREF(key);
4294 Py_DECREF(value);
4295 if (status < 0)
4296 goto error;
4297 }
4298 }
4299 return new_memo;
4300
4301 error:
4302 Py_XDECREF(new_memo);
4303 return NULL;
4304}
4305
Larry Hastings61272b72014-01-07 12:41:53 -08004306/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004307_pickle.PicklerMemoProxy.__reduce__
4308
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004309Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004310[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004311
Larry Hastings3cceb382014-01-04 11:09:09 -08004312static PyObject *
4313_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004314/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004315{
4316 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004317 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004318 if (contents == NULL)
4319 return NULL;
4320
4321 reduce_value = PyTuple_New(2);
4322 if (reduce_value == NULL) {
4323 Py_DECREF(contents);
4324 return NULL;
4325 }
4326 dict_args = PyTuple_New(1);
4327 if (dict_args == NULL) {
4328 Py_DECREF(contents);
4329 Py_DECREF(reduce_value);
4330 return NULL;
4331 }
4332 PyTuple_SET_ITEM(dict_args, 0, contents);
4333 Py_INCREF((PyObject *)&PyDict_Type);
4334 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4335 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4336 return reduce_value;
4337}
4338
4339static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004340 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4341 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4342 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004343 {NULL, NULL} /* sentinel */
4344};
4345
4346static void
4347PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4348{
4349 PyObject_GC_UnTrack(self);
4350 Py_XDECREF(self->pickler);
4351 PyObject_GC_Del((PyObject *)self);
4352}
4353
4354static int
4355PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4356 visitproc visit, void *arg)
4357{
4358 Py_VISIT(self->pickler);
4359 return 0;
4360}
4361
4362static int
4363PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4364{
4365 Py_CLEAR(self->pickler);
4366 return 0;
4367}
4368
4369static PyTypeObject PicklerMemoProxyType = {
4370 PyVarObject_HEAD_INIT(NULL, 0)
4371 "_pickle.PicklerMemoProxy", /*tp_name*/
4372 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4373 0,
4374 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4375 0, /* tp_print */
4376 0, /* tp_getattr */
4377 0, /* tp_setattr */
4378 0, /* tp_compare */
4379 0, /* tp_repr */
4380 0, /* tp_as_number */
4381 0, /* tp_as_sequence */
4382 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004383 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004384 0, /* tp_call */
4385 0, /* tp_str */
4386 PyObject_GenericGetAttr, /* tp_getattro */
4387 PyObject_GenericSetAttr, /* tp_setattro */
4388 0, /* tp_as_buffer */
4389 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4390 0, /* tp_doc */
4391 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4392 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4393 0, /* tp_richcompare */
4394 0, /* tp_weaklistoffset */
4395 0, /* tp_iter */
4396 0, /* tp_iternext */
4397 picklerproxy_methods, /* tp_methods */
4398};
4399
4400static PyObject *
4401PicklerMemoProxy_New(PicklerObject *pickler)
4402{
4403 PicklerMemoProxyObject *self;
4404
4405 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4406 if (self == NULL)
4407 return NULL;
4408 Py_INCREF(pickler);
4409 self->pickler = pickler;
4410 PyObject_GC_Track(self);
4411 return (PyObject *)self;
4412}
4413
4414/*****************************************************************************/
4415
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004416static PyObject *
4417Pickler_get_memo(PicklerObject *self)
4418{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004419 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004420}
4421
4422static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004423Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004424{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004425 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004426
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004427 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004428 PyErr_SetString(PyExc_TypeError,
4429 "attribute deletion is not supported");
4430 return -1;
4431 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004432
4433 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4434 PicklerObject *pickler =
4435 ((PicklerMemoProxyObject *)obj)->pickler;
4436
4437 new_memo = PyMemoTable_Copy(pickler->memo);
4438 if (new_memo == NULL)
4439 return -1;
4440 }
4441 else if (PyDict_Check(obj)) {
4442 Py_ssize_t i = 0;
4443 PyObject *key, *value;
4444
4445 new_memo = PyMemoTable_New();
4446 if (new_memo == NULL)
4447 return -1;
4448
4449 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004450 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004451 PyObject *memo_obj;
4452
4453 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4454 PyErr_SetString(PyExc_TypeError,
4455 "'memo' values must be 2-item tuples");
4456 goto error;
4457 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004458 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004459 if (memo_id == -1 && PyErr_Occurred())
4460 goto error;
4461 memo_obj = PyTuple_GET_ITEM(value, 1);
4462 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4463 goto error;
4464 }
4465 }
4466 else {
4467 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004468 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004469 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004470 return -1;
4471 }
4472
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004473 PyMemoTable_Del(self->memo);
4474 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004475
4476 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004477
4478 error:
4479 if (new_memo)
4480 PyMemoTable_Del(new_memo);
4481 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004482}
4483
4484static PyObject *
4485Pickler_get_persid(PicklerObject *self)
4486{
4487 if (self->pers_func == NULL)
4488 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4489 else
4490 Py_INCREF(self->pers_func);
4491 return self->pers_func;
4492}
4493
4494static int
4495Pickler_set_persid(PicklerObject *self, PyObject *value)
4496{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004497 if (value == NULL) {
4498 PyErr_SetString(PyExc_TypeError,
4499 "attribute deletion is not supported");
4500 return -1;
4501 }
4502 if (!PyCallable_Check(value)) {
4503 PyErr_SetString(PyExc_TypeError,
4504 "persistent_id must be a callable taking one argument");
4505 return -1;
4506 }
4507
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004508 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004509 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004510
4511 return 0;
4512}
4513
4514static PyMemberDef Pickler_members[] = {
4515 {"bin", T_INT, offsetof(PicklerObject, bin)},
4516 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004517 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004518 {NULL}
4519};
4520
4521static PyGetSetDef Pickler_getsets[] = {
4522 {"memo", (getter)Pickler_get_memo,
4523 (setter)Pickler_set_memo},
4524 {"persistent_id", (getter)Pickler_get_persid,
4525 (setter)Pickler_set_persid},
4526 {NULL}
4527};
4528
4529static PyTypeObject Pickler_Type = {
4530 PyVarObject_HEAD_INIT(NULL, 0)
4531 "_pickle.Pickler" , /*tp_name*/
4532 sizeof(PicklerObject), /*tp_basicsize*/
4533 0, /*tp_itemsize*/
4534 (destructor)Pickler_dealloc, /*tp_dealloc*/
4535 0, /*tp_print*/
4536 0, /*tp_getattr*/
4537 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004538 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004539 0, /*tp_repr*/
4540 0, /*tp_as_number*/
4541 0, /*tp_as_sequence*/
4542 0, /*tp_as_mapping*/
4543 0, /*tp_hash*/
4544 0, /*tp_call*/
4545 0, /*tp_str*/
4546 0, /*tp_getattro*/
4547 0, /*tp_setattro*/
4548 0, /*tp_as_buffer*/
4549 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004550 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004551 (traverseproc)Pickler_traverse, /*tp_traverse*/
4552 (inquiry)Pickler_clear, /*tp_clear*/
4553 0, /*tp_richcompare*/
4554 0, /*tp_weaklistoffset*/
4555 0, /*tp_iter*/
4556 0, /*tp_iternext*/
4557 Pickler_methods, /*tp_methods*/
4558 Pickler_members, /*tp_members*/
4559 Pickler_getsets, /*tp_getset*/
4560 0, /*tp_base*/
4561 0, /*tp_dict*/
4562 0, /*tp_descr_get*/
4563 0, /*tp_descr_set*/
4564 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004565 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004566 PyType_GenericAlloc, /*tp_alloc*/
4567 PyType_GenericNew, /*tp_new*/
4568 PyObject_GC_Del, /*tp_free*/
4569 0, /*tp_is_gc*/
4570};
4571
Victor Stinner121aab42011-09-29 23:40:53 +02004572/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004573
4574 XXX: It would be nice to able to avoid Python function call overhead, by
4575 using directly the C version of find_class(), when find_class() is not
4576 overridden by a subclass. Although, this could become rather hackish. A
4577 simpler optimization would be to call the C function when self is not a
4578 subclass instance. */
4579static PyObject *
4580find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4581{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004582 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004583
4584 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4585 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586}
4587
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004588static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004589marker(UnpicklerObject *self)
4590{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004591 Py_ssize_t mark;
4592
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004593 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004594 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004595 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004596 return -1;
4597 }
4598
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004599 mark = self->marks[--self->num_marks];
4600 self->stack->mark_set = self->num_marks != 0;
4601 self->stack->fence = self->num_marks ?
4602 self->marks[self->num_marks - 1] : 0;
4603 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004604}
4605
4606static int
4607load_none(UnpicklerObject *self)
4608{
4609 PDATA_APPEND(self->stack, Py_None, -1);
4610 return 0;
4611}
4612
4613static int
4614bad_readline(void)
4615{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004616 PickleState *st = _Pickle_GetGlobalState();
4617 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004618 return -1;
4619}
4620
4621static int
4622load_int(UnpicklerObject *self)
4623{
4624 PyObject *value;
4625 char *endptr, *s;
4626 Py_ssize_t len;
4627 long x;
4628
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004629 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004630 return -1;
4631 if (len < 2)
4632 return bad_readline();
4633
4634 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004635 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004636 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004637 x = strtol(s, &endptr, 0);
4638
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004639 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004640 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004641 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004642 errno = 0;
4643 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004644 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004645 if (value == NULL) {
4646 PyErr_SetString(PyExc_ValueError,
4647 "could not convert string to int");
4648 return -1;
4649 }
4650 }
4651 else {
4652 if (len == 3 && (x == 0 || x == 1)) {
4653 if ((value = PyBool_FromLong(x)) == NULL)
4654 return -1;
4655 }
4656 else {
4657 if ((value = PyLong_FromLong(x)) == NULL)
4658 return -1;
4659 }
4660 }
4661
4662 PDATA_PUSH(self->stack, value, -1);
4663 return 0;
4664}
4665
4666static int
4667load_bool(UnpicklerObject *self, PyObject *boolean)
4668{
4669 assert(boolean == Py_True || boolean == Py_False);
4670 PDATA_APPEND(self->stack, boolean, -1);
4671 return 0;
4672}
4673
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004674/* s contains x bytes of an unsigned little-endian integer. Return its value
4675 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4676 */
4677static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004678calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004679{
4680 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004681 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004682 size_t x = 0;
4683
Serhiy Storchakae0606192015-09-29 22:10:07 +03004684 if (nbytes > (int)sizeof(size_t)) {
4685 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4686 * have 64-bit size that can't be represented on 32-bit platform.
4687 */
4688 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4689 if (s[i])
4690 return -1;
4691 }
4692 nbytes = (int)sizeof(size_t);
4693 }
4694 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004695 x |= (size_t) s[i] << (8 * i);
4696 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004697
4698 if (x > PY_SSIZE_T_MAX)
4699 return -1;
4700 else
4701 return (Py_ssize_t) x;
4702}
4703
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004704/* s contains x bytes of a little-endian integer. Return its value as a
4705 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004706 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004707 * of x-platform bugs.
4708 */
4709static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004710calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004711{
4712 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004713 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004714 long x = 0;
4715
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004716 for (i = 0; i < nbytes; i++) {
4717 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004718 }
4719
4720 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4721 * is signed, so on a box with longs bigger than 4 bytes we need
4722 * to extend a BININT's sign bit to the full width.
4723 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004724 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004725 x |= -(x & (1L << 31));
4726 }
4727
4728 return x;
4729}
4730
4731static int
4732load_binintx(UnpicklerObject *self, char *s, int size)
4733{
4734 PyObject *value;
4735 long x;
4736
4737 x = calc_binint(s, size);
4738
4739 if ((value = PyLong_FromLong(x)) == NULL)
4740 return -1;
4741
4742 PDATA_PUSH(self->stack, value, -1);
4743 return 0;
4744}
4745
4746static int
4747load_binint(UnpicklerObject *self)
4748{
4749 char *s;
4750
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004751 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004752 return -1;
4753
4754 return load_binintx(self, s, 4);
4755}
4756
4757static int
4758load_binint1(UnpicklerObject *self)
4759{
4760 char *s;
4761
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004762 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004763 return -1;
4764
4765 return load_binintx(self, s, 1);
4766}
4767
4768static int
4769load_binint2(UnpicklerObject *self)
4770{
4771 char *s;
4772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004773 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004774 return -1;
4775
4776 return load_binintx(self, s, 2);
4777}
4778
4779static int
4780load_long(UnpicklerObject *self)
4781{
4782 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004783 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004784 Py_ssize_t len;
4785
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004786 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004787 return -1;
4788 if (len < 2)
4789 return bad_readline();
4790
Mark Dickinson8dd05142009-01-20 20:43:58 +00004791 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4792 the 'L' before calling PyLong_FromString. In order to maintain
4793 compatibility with Python 3.0.0, we don't actually *require*
4794 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004795 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004796 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004797 /* XXX: Should the base argument explicitly set to 10? */
4798 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004799 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004800 return -1;
4801
4802 PDATA_PUSH(self->stack, value, -1);
4803 return 0;
4804}
4805
4806/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4807 * data following.
4808 */
4809static int
4810load_counted_long(UnpicklerObject *self, int size)
4811{
4812 PyObject *value;
4813 char *nbytes;
4814 char *pdata;
4815
4816 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004817 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004818 return -1;
4819
4820 size = calc_binint(nbytes, size);
4821 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004822 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004823 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004824 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004825 "LONG pickle has negative byte count");
4826 return -1;
4827 }
4828
4829 if (size == 0)
4830 value = PyLong_FromLong(0L);
4831 else {
4832 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004833 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004834 return -1;
4835 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4836 1 /* little endian */ , 1 /* signed */ );
4837 }
4838 if (value == NULL)
4839 return -1;
4840 PDATA_PUSH(self->stack, value, -1);
4841 return 0;
4842}
4843
4844static int
4845load_float(UnpicklerObject *self)
4846{
4847 PyObject *value;
4848 char *endptr, *s;
4849 Py_ssize_t len;
4850 double d;
4851
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004852 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004853 return -1;
4854 if (len < 2)
4855 return bad_readline();
4856
4857 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004858 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4859 if (d == -1.0 && PyErr_Occurred())
4860 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004861 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004862 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4863 return -1;
4864 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004865 value = PyFloat_FromDouble(d);
4866 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004867 return -1;
4868
4869 PDATA_PUSH(self->stack, value, -1);
4870 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004871}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004872
4873static int
4874load_binfloat(UnpicklerObject *self)
4875{
4876 PyObject *value;
4877 double x;
4878 char *s;
4879
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004880 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004881 return -1;
4882
4883 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4884 if (x == -1.0 && PyErr_Occurred())
4885 return -1;
4886
4887 if ((value = PyFloat_FromDouble(x)) == NULL)
4888 return -1;
4889
4890 PDATA_PUSH(self->stack, value, -1);
4891 return 0;
4892}
4893
4894static int
4895load_string(UnpicklerObject *self)
4896{
4897 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004898 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899 Py_ssize_t len;
4900 char *s, *p;
4901
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004902 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004903 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004904 /* Strip the newline */
4905 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004906 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004907 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004908 p = s + 1;
4909 len -= 2;
4910 }
4911 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004912 PickleState *st = _Pickle_GetGlobalState();
4913 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004914 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004915 return -1;
4916 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004917 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004918
4919 /* Use the PyBytes API to decode the string, since that is what is used
4920 to encode, and then coerce the result to Unicode. */
4921 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004922 if (bytes == NULL)
4923 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004924
4925 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4926 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4927 if (strcmp(self->encoding, "bytes") == 0) {
4928 obj = bytes;
4929 }
4930 else {
4931 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4932 Py_DECREF(bytes);
4933 if (obj == NULL) {
4934 return -1;
4935 }
4936 }
4937
4938 PDATA_PUSH(self->stack, obj, -1);
4939 return 0;
4940}
4941
4942static int
4943load_counted_binstring(UnpicklerObject *self, int nbytes)
4944{
4945 PyObject *obj;
4946 Py_ssize_t size;
4947 char *s;
4948
4949 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004950 return -1;
4951
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004952 size = calc_binsize(s, nbytes);
4953 if (size < 0) {
4954 PickleState *st = _Pickle_GetGlobalState();
4955 PyErr_Format(st->UnpicklingError,
4956 "BINSTRING exceeds system's maximum size of %zd bytes",
4957 PY_SSIZE_T_MAX);
4958 return -1;
4959 }
4960
4961 if (_Unpickler_Read(self, &s, size) < 0)
4962 return -1;
4963
4964 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4965 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4966 if (strcmp(self->encoding, "bytes") == 0) {
4967 obj = PyBytes_FromStringAndSize(s, size);
4968 }
4969 else {
4970 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4971 }
4972 if (obj == NULL) {
4973 return -1;
4974 }
4975
4976 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004977 return 0;
4978}
4979
4980static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004981load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004982{
4983 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004984 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004985 char *s;
4986
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004987 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004988 return -1;
4989
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004990 size = calc_binsize(s, nbytes);
4991 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004992 PyErr_Format(PyExc_OverflowError,
4993 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004994 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004995 return -1;
4996 }
4997
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004998 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004999 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005000
5001 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005002 if (bytes == NULL)
5003 return -1;
5004
5005 PDATA_PUSH(self->stack, bytes, -1);
5006 return 0;
5007}
5008
5009static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005010load_unicode(UnpicklerObject *self)
5011{
5012 PyObject *str;
5013 Py_ssize_t len;
5014 char *s;
5015
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005016 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005017 return -1;
5018 if (len < 1)
5019 return bad_readline();
5020
5021 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5022 if (str == NULL)
5023 return -1;
5024
5025 PDATA_PUSH(self->stack, str, -1);
5026 return 0;
5027}
5028
5029static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005030load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005031{
5032 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005033 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005034 char *s;
5035
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005036 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005037 return -1;
5038
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005039 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005041 PyErr_Format(PyExc_OverflowError,
5042 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005043 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044 return -1;
5045 }
5046
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005047 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048 return -1;
5049
Victor Stinner485fb562010-04-13 11:07:24 +00005050 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005051 if (str == NULL)
5052 return -1;
5053
5054 PDATA_PUSH(self->stack, str, -1);
5055 return 0;
5056}
5057
5058static int
Victor Stinner21b47112016-03-14 18:09:39 +01005059load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005060{
5061 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005062
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005063 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005064 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005065
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005066 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005067 if (tuple == NULL)
5068 return -1;
5069 PDATA_PUSH(self->stack, tuple, -1);
5070 return 0;
5071}
5072
5073static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005074load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005075{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005076 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005077
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005078 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005079 return -1;
5080
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005081 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005082}
5083
5084static int
5085load_empty_list(UnpicklerObject *self)
5086{
5087 PyObject *list;
5088
5089 if ((list = PyList_New(0)) == NULL)
5090 return -1;
5091 PDATA_PUSH(self->stack, list, -1);
5092 return 0;
5093}
5094
5095static int
5096load_empty_dict(UnpicklerObject *self)
5097{
5098 PyObject *dict;
5099
5100 if ((dict = PyDict_New()) == NULL)
5101 return -1;
5102 PDATA_PUSH(self->stack, dict, -1);
5103 return 0;
5104}
5105
5106static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005107load_empty_set(UnpicklerObject *self)
5108{
5109 PyObject *set;
5110
5111 if ((set = PySet_New(NULL)) == NULL)
5112 return -1;
5113 PDATA_PUSH(self->stack, set, -1);
5114 return 0;
5115}
5116
5117static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005118load_list(UnpicklerObject *self)
5119{
5120 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005121 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005122
5123 if ((i = marker(self)) < 0)
5124 return -1;
5125
5126 list = Pdata_poplist(self->stack, i);
5127 if (list == NULL)
5128 return -1;
5129 PDATA_PUSH(self->stack, list, -1);
5130 return 0;
5131}
5132
5133static int
5134load_dict(UnpicklerObject *self)
5135{
5136 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005137 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005138
5139 if ((i = marker(self)) < 0)
5140 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005141 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005142
5143 if ((dict = PyDict_New()) == NULL)
5144 return -1;
5145
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005146 if ((j - i) % 2 != 0) {
5147 PickleState *st = _Pickle_GetGlobalState();
5148 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005149 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005150 return -1;
5151 }
5152
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005153 for (k = i + 1; k < j; k += 2) {
5154 key = self->stack->data[k - 1];
5155 value = self->stack->data[k];
5156 if (PyDict_SetItem(dict, key, value) < 0) {
5157 Py_DECREF(dict);
5158 return -1;
5159 }
5160 }
5161 Pdata_clear(self->stack, i);
5162 PDATA_PUSH(self->stack, dict, -1);
5163 return 0;
5164}
5165
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005166static int
5167load_frozenset(UnpicklerObject *self)
5168{
5169 PyObject *items;
5170 PyObject *frozenset;
5171 Py_ssize_t i;
5172
5173 if ((i = marker(self)) < 0)
5174 return -1;
5175
5176 items = Pdata_poptuple(self->stack, i);
5177 if (items == NULL)
5178 return -1;
5179
5180 frozenset = PyFrozenSet_New(items);
5181 Py_DECREF(items);
5182 if (frozenset == NULL)
5183 return -1;
5184
5185 PDATA_PUSH(self->stack, frozenset, -1);
5186 return 0;
5187}
5188
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005189static PyObject *
5190instantiate(PyObject *cls, PyObject *args)
5191{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005192 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005193 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005194 /* Caller must assure args are a tuple. Normally, args come from
5195 Pdata_poptuple which packs objects from the top of the stack
5196 into a newly created tuple. */
5197 assert(PyTuple_Check(args));
5198 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005199 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005200 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005201 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005202 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005203 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005204
5205 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005206 }
5207 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005208}
5209
5210static int
5211load_obj(UnpicklerObject *self)
5212{
5213 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005214 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005215
5216 if ((i = marker(self)) < 0)
5217 return -1;
5218
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005219 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005220 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005221
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005222 args = Pdata_poptuple(self->stack, i + 1);
5223 if (args == NULL)
5224 return -1;
5225
5226 PDATA_POP(self->stack, cls);
5227 if (cls) {
5228 obj = instantiate(cls, args);
5229 Py_DECREF(cls);
5230 }
5231 Py_DECREF(args);
5232 if (obj == NULL)
5233 return -1;
5234
5235 PDATA_PUSH(self->stack, obj, -1);
5236 return 0;
5237}
5238
5239static int
5240load_inst(UnpicklerObject *self)
5241{
5242 PyObject *cls = NULL;
5243 PyObject *args = NULL;
5244 PyObject *obj = NULL;
5245 PyObject *module_name;
5246 PyObject *class_name;
5247 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005248 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005249 char *s;
5250
5251 if ((i = marker(self)) < 0)
5252 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005253 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005254 return -1;
5255 if (len < 2)
5256 return bad_readline();
5257
5258 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5259 identifiers are permitted in Python 3.0, since the INST opcode is only
5260 supported by older protocols on Python 2.x. */
5261 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5262 if (module_name == NULL)
5263 return -1;
5264
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005265 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005266 if (len < 2) {
5267 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005268 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005269 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005270 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005271 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005272 cls = find_class(self, module_name, class_name);
5273 Py_DECREF(class_name);
5274 }
5275 }
5276 Py_DECREF(module_name);
5277
5278 if (cls == NULL)
5279 return -1;
5280
5281 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5282 obj = instantiate(cls, args);
5283 Py_DECREF(args);
5284 }
5285 Py_DECREF(cls);
5286
5287 if (obj == NULL)
5288 return -1;
5289
5290 PDATA_PUSH(self->stack, obj, -1);
5291 return 0;
5292}
5293
5294static int
5295load_newobj(UnpicklerObject *self)
5296{
5297 PyObject *args = NULL;
5298 PyObject *clsraw = NULL;
5299 PyTypeObject *cls; /* clsraw cast to its true type */
5300 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005301 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005302
5303 /* Stack is ... cls argtuple, and we want to call
5304 * cls.__new__(cls, *argtuple).
5305 */
5306 PDATA_POP(self->stack, args);
5307 if (args == NULL)
5308 goto error;
5309 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005310 PyErr_SetString(st->UnpicklingError,
5311 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005312 goto error;
5313 }
5314
5315 PDATA_POP(self->stack, clsraw);
5316 cls = (PyTypeObject *)clsraw;
5317 if (cls == NULL)
5318 goto error;
5319 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005320 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005321 "isn't a type object");
5322 goto error;
5323 }
5324 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005325 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005326 "has NULL tp_new");
5327 goto error;
5328 }
5329
5330 /* Call __new__. */
5331 obj = cls->tp_new(cls, args, NULL);
5332 if (obj == NULL)
5333 goto error;
5334
5335 Py_DECREF(args);
5336 Py_DECREF(clsraw);
5337 PDATA_PUSH(self->stack, obj, -1);
5338 return 0;
5339
5340 error:
5341 Py_XDECREF(args);
5342 Py_XDECREF(clsraw);
5343 return -1;
5344}
5345
5346static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005347load_newobj_ex(UnpicklerObject *self)
5348{
5349 PyObject *cls, *args, *kwargs;
5350 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005351 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005352
5353 PDATA_POP(self->stack, kwargs);
5354 if (kwargs == NULL) {
5355 return -1;
5356 }
5357 PDATA_POP(self->stack, args);
5358 if (args == NULL) {
5359 Py_DECREF(kwargs);
5360 return -1;
5361 }
5362 PDATA_POP(self->stack, cls);
5363 if (cls == NULL) {
5364 Py_DECREF(kwargs);
5365 Py_DECREF(args);
5366 return -1;
5367 }
Larry Hastings61272b72014-01-07 12:41:53 -08005368
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005369 if (!PyType_Check(cls)) {
5370 Py_DECREF(kwargs);
5371 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005372 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005373 "NEWOBJ_EX class argument must be a type, not %.200s",
5374 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005375 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005376 return -1;
5377 }
5378
5379 if (((PyTypeObject *)cls)->tp_new == NULL) {
5380 Py_DECREF(kwargs);
5381 Py_DECREF(args);
5382 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005383 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005384 "NEWOBJ_EX class argument doesn't have __new__");
5385 return -1;
5386 }
5387 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5388 Py_DECREF(kwargs);
5389 Py_DECREF(args);
5390 Py_DECREF(cls);
5391 if (obj == NULL) {
5392 return -1;
5393 }
5394 PDATA_PUSH(self->stack, obj, -1);
5395 return 0;
5396}
5397
5398static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005399load_global(UnpicklerObject *self)
5400{
5401 PyObject *global = NULL;
5402 PyObject *module_name;
5403 PyObject *global_name;
5404 Py_ssize_t len;
5405 char *s;
5406
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005407 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005408 return -1;
5409 if (len < 2)
5410 return bad_readline();
5411 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5412 if (!module_name)
5413 return -1;
5414
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005415 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005416 if (len < 2) {
5417 Py_DECREF(module_name);
5418 return bad_readline();
5419 }
5420 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5421 if (global_name) {
5422 global = find_class(self, module_name, global_name);
5423 Py_DECREF(global_name);
5424 }
5425 }
5426 Py_DECREF(module_name);
5427
5428 if (global == NULL)
5429 return -1;
5430 PDATA_PUSH(self->stack, global, -1);
5431 return 0;
5432}
5433
5434static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005435load_stack_global(UnpicklerObject *self)
5436{
5437 PyObject *global;
5438 PyObject *module_name;
5439 PyObject *global_name;
5440
5441 PDATA_POP(self->stack, global_name);
5442 PDATA_POP(self->stack, module_name);
5443 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5444 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005445 PickleState *st = _Pickle_GetGlobalState();
5446 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005447 Py_XDECREF(global_name);
5448 Py_XDECREF(module_name);
5449 return -1;
5450 }
5451 global = find_class(self, module_name, global_name);
5452 Py_DECREF(global_name);
5453 Py_DECREF(module_name);
5454 if (global == NULL)
5455 return -1;
5456 PDATA_PUSH(self->stack, global, -1);
5457 return 0;
5458}
5459
5460static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461load_persid(UnpicklerObject *self)
5462{
5463 PyObject *pid;
5464 Py_ssize_t len;
5465 char *s;
5466
5467 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005468 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005469 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005470 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005471 return bad_readline();
5472
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005473 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5474 if (pid == NULL) {
5475 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5476 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5477 "persistent IDs in protocol 0 must be "
5478 "ASCII strings");
5479 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005480 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005481 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005482
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005483 /* This does not leak since _Pickle_FastCall() steals the reference
5484 to pid first. */
5485 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005486 if (pid == NULL)
5487 return -1;
5488
5489 PDATA_PUSH(self->stack, pid, -1);
5490 return 0;
5491 }
5492 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005493 PickleState *st = _Pickle_GetGlobalState();
5494 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005495 "A load persistent id instruction was encountered,\n"
5496 "but no persistent_load function was specified.");
5497 return -1;
5498 }
5499}
5500
5501static int
5502load_binpersid(UnpicklerObject *self)
5503{
5504 PyObject *pid;
5505
5506 if (self->pers_func) {
5507 PDATA_POP(self->stack, pid);
5508 if (pid == NULL)
5509 return -1;
5510
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005511 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005512 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005513 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005514 if (pid == NULL)
5515 return -1;
5516
5517 PDATA_PUSH(self->stack, pid, -1);
5518 return 0;
5519 }
5520 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005521 PickleState *st = _Pickle_GetGlobalState();
5522 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005523 "A load persistent id instruction was encountered,\n"
5524 "but no persistent_load function was specified.");
5525 return -1;
5526 }
5527}
5528
5529static int
5530load_pop(UnpicklerObject *self)
5531{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005532 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005533
5534 /* Note that we split the (pickle.py) stack into two stacks,
5535 * an object stack and a mark stack. We have to be clever and
5536 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005537 * mark stack first, and only signalling a stack underflow if
5538 * the object stack is empty and the mark stack doesn't match
5539 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005540 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005541 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005542 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005543 self->stack->mark_set = self->num_marks != 0;
5544 self->stack->fence = self->num_marks ?
5545 self->marks[self->num_marks - 1] : 0;
5546 } else if (len <= self->stack->fence)
5547 return Pdata_stack_underflow(self->stack);
5548 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005549 len--;
5550 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005551 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005552 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553 return 0;
5554}
5555
5556static int
5557load_pop_mark(UnpicklerObject *self)
5558{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005559 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560
5561 if ((i = marker(self)) < 0)
5562 return -1;
5563
5564 Pdata_clear(self->stack, i);
5565
5566 return 0;
5567}
5568
5569static int
5570load_dup(UnpicklerObject *self)
5571{
5572 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005573 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005574
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005575 if (len <= self->stack->fence)
5576 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005577 last = self->stack->data[len - 1];
5578 PDATA_APPEND(self->stack, last, -1);
5579 return 0;
5580}
5581
5582static int
5583load_get(UnpicklerObject *self)
5584{
5585 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005586 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005587 Py_ssize_t len;
5588 char *s;
5589
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005590 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005591 return -1;
5592 if (len < 2)
5593 return bad_readline();
5594
5595 key = PyLong_FromString(s, NULL, 10);
5596 if (key == NULL)
5597 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 idx = PyLong_AsSsize_t(key);
5599 if (idx == -1 && PyErr_Occurred()) {
5600 Py_DECREF(key);
5601 return -1;
5602 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005604 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005605 if (value == NULL) {
5606 if (!PyErr_Occurred())
5607 PyErr_SetObject(PyExc_KeyError, key);
5608 Py_DECREF(key);
5609 return -1;
5610 }
5611 Py_DECREF(key);
5612
5613 PDATA_APPEND(self->stack, value, -1);
5614 return 0;
5615}
5616
5617static int
5618load_binget(UnpicklerObject *self)
5619{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005620 PyObject *value;
5621 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622 char *s;
5623
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005624 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625 return -1;
5626
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005627 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005629 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005631 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005632 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005634 Py_DECREF(key);
5635 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005636 return -1;
5637 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638
5639 PDATA_APPEND(self->stack, value, -1);
5640 return 0;
5641}
5642
5643static int
5644load_long_binget(UnpicklerObject *self)
5645{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005646 PyObject *value;
5647 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005648 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005650 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005651 return -1;
5652
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005653 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005654
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005655 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005656 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005657 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005658 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005659 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005660 Py_DECREF(key);
5661 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662 return -1;
5663 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005664
5665 PDATA_APPEND(self->stack, value, -1);
5666 return 0;
5667}
5668
5669/* Push an object from the extension registry (EXT[124]). nbytes is
5670 * the number of bytes following the opcode, holding the index (code) value.
5671 */
5672static int
5673load_extension(UnpicklerObject *self, int nbytes)
5674{
5675 char *codebytes; /* the nbytes bytes after the opcode */
5676 long code; /* calc_binint returns long */
5677 PyObject *py_code; /* code as a Python int */
5678 PyObject *obj; /* the object to push */
5679 PyObject *pair; /* (module_name, class_name) */
5680 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005681 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682
5683 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005684 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005685 return -1;
5686 code = calc_binint(codebytes, nbytes);
5687 if (code <= 0) { /* note that 0 is forbidden */
5688 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005689 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 return -1;
5691 }
5692
5693 /* Look for the code in the cache. */
5694 py_code = PyLong_FromLong(code);
5695 if (py_code == NULL)
5696 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005697 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005698 if (obj != NULL) {
5699 /* Bingo. */
5700 Py_DECREF(py_code);
5701 PDATA_APPEND(self->stack, obj, -1);
5702 return 0;
5703 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005704 if (PyErr_Occurred()) {
5705 Py_DECREF(py_code);
5706 return -1;
5707 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005708
5709 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005710 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005711 if (pair == NULL) {
5712 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005713 if (!PyErr_Occurred()) {
5714 PyErr_Format(PyExc_ValueError, "unregistered extension "
5715 "code %ld", code);
5716 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005717 return -1;
5718 }
5719 /* Since the extension registry is manipulable via Python code,
5720 * confirm that pair is really a 2-tuple of strings.
5721 */
5722 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5723 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5724 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5725 Py_DECREF(py_code);
5726 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5727 "isn't a 2-tuple of strings", code);
5728 return -1;
5729 }
5730 /* Load the object. */
5731 obj = find_class(self, module_name, class_name);
5732 if (obj == NULL) {
5733 Py_DECREF(py_code);
5734 return -1;
5735 }
5736 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005737 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005738 Py_DECREF(py_code);
5739 if (code < 0) {
5740 Py_DECREF(obj);
5741 return -1;
5742 }
5743 PDATA_PUSH(self->stack, obj, -1);
5744 return 0;
5745}
5746
5747static int
5748load_put(UnpicklerObject *self)
5749{
5750 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005751 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005752 Py_ssize_t len;
5753 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005754
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005755 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005756 return -1;
5757 if (len < 2)
5758 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005759 if (Py_SIZE(self->stack) <= self->stack->fence)
5760 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005761 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005762
5763 key = PyLong_FromString(s, NULL, 10);
5764 if (key == NULL)
5765 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005768 if (idx < 0) {
5769 if (!PyErr_Occurred())
5770 PyErr_SetString(PyExc_ValueError,
5771 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005772 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005773 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005774
5775 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776}
5777
5778static int
5779load_binput(UnpicklerObject *self)
5780{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005781 PyObject *value;
5782 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005783 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005786 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005787
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005788 if (Py_SIZE(self->stack) <= self->stack->fence)
5789 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005792 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005794 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005795}
5796
5797static int
5798load_long_binput(UnpicklerObject *self)
5799{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005800 PyObject *value;
5801 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005802 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005804 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005805 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005806
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005807 if (Py_SIZE(self->stack) <= self->stack->fence)
5808 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005809 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005810
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005811 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005812 if (idx < 0) {
5813 PyErr_SetString(PyExc_ValueError,
5814 "negative LONG_BINPUT argument");
5815 return -1;
5816 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005818 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005819}
5820
5821static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005822load_memoize(UnpicklerObject *self)
5823{
5824 PyObject *value;
5825
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005826 if (Py_SIZE(self->stack) <= self->stack->fence)
5827 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005828 value = self->stack->data[Py_SIZE(self->stack) - 1];
5829
5830 return _Unpickler_MemoPut(self, self->memo_len, value);
5831}
5832
5833static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005834do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005835{
5836 PyObject *value;
5837 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005838 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005839
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005840 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005841 if (x > len || x <= self->stack->fence)
5842 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005843 if (len == x) /* nothing to do */
5844 return 0;
5845
5846 list = self->stack->data[x - 1];
5847
5848 if (PyList_Check(list)) {
5849 PyObject *slice;
5850 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005851 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005852
5853 slice = Pdata_poplist(self->stack, x);
5854 if (!slice)
5855 return -1;
5856 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005857 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005858 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005859 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005860 }
5861 else {
5862 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005863 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005864
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005865 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005866 if (append_func == NULL)
5867 return -1;
5868 for (i = x; i < len; i++) {
5869 PyObject *result;
5870
5871 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005872 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005873 if (result == NULL) {
5874 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005875 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005876 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005877 return -1;
5878 }
5879 Py_DECREF(result);
5880 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005881 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005882 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005883 }
5884
5885 return 0;
5886}
5887
5888static int
5889load_append(UnpicklerObject *self)
5890{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005891 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5892 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005893 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005894}
5895
5896static int
5897load_appends(UnpicklerObject *self)
5898{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005899 Py_ssize_t i = marker(self);
5900 if (i < 0)
5901 return -1;
5902 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005903}
5904
5905static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005906do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005907{
5908 PyObject *value, *key;
5909 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005910 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005911 int status = 0;
5912
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005913 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005914 if (x > len || x <= self->stack->fence)
5915 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005916 if (len == x) /* nothing to do */
5917 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005918 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005919 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005920 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005921 PyErr_SetString(st->UnpicklingError,
5922 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923 return -1;
5924 }
5925
5926 /* Here, dict does not actually need to be a PyDict; it could be anything
5927 that supports the __setitem__ attribute. */
5928 dict = self->stack->data[x - 1];
5929
5930 for (i = x + 1; i < len; i += 2) {
5931 key = self->stack->data[i - 1];
5932 value = self->stack->data[i];
5933 if (PyObject_SetItem(dict, key, value) < 0) {
5934 status = -1;
5935 break;
5936 }
5937 }
5938
5939 Pdata_clear(self->stack, x);
5940 return status;
5941}
5942
5943static int
5944load_setitem(UnpicklerObject *self)
5945{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005946 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005947}
5948
5949static int
5950load_setitems(UnpicklerObject *self)
5951{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005952 Py_ssize_t i = marker(self);
5953 if (i < 0)
5954 return -1;
5955 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005956}
5957
5958static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005959load_additems(UnpicklerObject *self)
5960{
5961 PyObject *set;
5962 Py_ssize_t mark, len, i;
5963
5964 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005965 if (mark < 0)
5966 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005967 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005968 if (mark > len || mark <= self->stack->fence)
5969 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005970 if (len == mark) /* nothing to do */
5971 return 0;
5972
5973 set = self->stack->data[mark - 1];
5974
5975 if (PySet_Check(set)) {
5976 PyObject *items;
5977 int status;
5978
5979 items = Pdata_poptuple(self->stack, mark);
5980 if (items == NULL)
5981 return -1;
5982
5983 status = _PySet_Update(set, items);
5984 Py_DECREF(items);
5985 return status;
5986 }
5987 else {
5988 PyObject *add_func;
5989 _Py_IDENTIFIER(add);
5990
5991 add_func = _PyObject_GetAttrId(set, &PyId_add);
5992 if (add_func == NULL)
5993 return -1;
5994 for (i = mark; i < len; i++) {
5995 PyObject *result;
5996 PyObject *item;
5997
5998 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005999 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006000 if (result == NULL) {
6001 Pdata_clear(self->stack, i + 1);
6002 Py_SIZE(self->stack) = mark;
6003 return -1;
6004 }
6005 Py_DECREF(result);
6006 }
6007 Py_SIZE(self->stack) = mark;
6008 }
6009
6010 return 0;
6011}
6012
6013static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006014load_build(UnpicklerObject *self)
6015{
6016 PyObject *state, *inst, *slotstate;
6017 PyObject *setstate;
6018 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006019 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006020
6021 /* Stack is ... instance, state. We want to leave instance at
6022 * the stack top, possibly mutated via instance.__setstate__(state).
6023 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006024 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6025 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006026
6027 PDATA_POP(self->stack, state);
6028 if (state == NULL)
6029 return -1;
6030
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006031 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006032
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006033 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006034 if (setstate == NULL) {
6035 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6036 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006037 else {
6038 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006039 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006040 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006041 }
6042 else {
6043 PyObject *result;
6044
6045 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006046 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006047 Py_DECREF(setstate);
6048 if (result == NULL)
6049 return -1;
6050 Py_DECREF(result);
6051 return 0;
6052 }
6053
6054 /* A default __setstate__. First see whether state embeds a
6055 * slot state dict too (a proto 2 addition).
6056 */
6057 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6058 PyObject *tmp = state;
6059
6060 state = PyTuple_GET_ITEM(tmp, 0);
6061 slotstate = PyTuple_GET_ITEM(tmp, 1);
6062 Py_INCREF(state);
6063 Py_INCREF(slotstate);
6064 Py_DECREF(tmp);
6065 }
6066 else
6067 slotstate = NULL;
6068
6069 /* Set inst.__dict__ from the state dict (if any). */
6070 if (state != Py_None) {
6071 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006072 PyObject *d_key, *d_value;
6073 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006074 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075
6076 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006077 PickleState *st = _Pickle_GetGlobalState();
6078 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079 goto error;
6080 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006081 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006082 if (dict == NULL)
6083 goto error;
6084
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006085 i = 0;
6086 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6087 /* normally the keys for instance attributes are
6088 interned. we should try to do that here. */
6089 Py_INCREF(d_key);
6090 if (PyUnicode_CheckExact(d_key))
6091 PyUnicode_InternInPlace(&d_key);
6092 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6093 Py_DECREF(d_key);
6094 goto error;
6095 }
6096 Py_DECREF(d_key);
6097 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006098 Py_DECREF(dict);
6099 }
6100
6101 /* Also set instance attributes from the slotstate dict (if any). */
6102 if (slotstate != NULL) {
6103 PyObject *d_key, *d_value;
6104 Py_ssize_t i;
6105
6106 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006107 PickleState *st = _Pickle_GetGlobalState();
6108 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006109 "slot state is not a dictionary");
6110 goto error;
6111 }
6112 i = 0;
6113 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6114 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6115 goto error;
6116 }
6117 }
6118
6119 if (0) {
6120 error:
6121 status = -1;
6122 }
6123
6124 Py_DECREF(state);
6125 Py_XDECREF(slotstate);
6126 return status;
6127}
6128
6129static int
6130load_mark(UnpicklerObject *self)
6131{
6132
6133 /* Note that we split the (pickle.py) stack into two stacks, an
6134 * object stack and a mark stack. Here we push a mark onto the
6135 * mark stack.
6136 */
6137
6138 if ((self->num_marks + 1) >= self->marks_size) {
6139 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006140
6141 /* Use the size_t type to check for overflow. */
6142 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006143 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006144 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006145 PyErr_NoMemory();
6146 return -1;
6147 }
6148
6149 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006150 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006151 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006152 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6153 if (self->marks == NULL) {
6154 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006155 PyErr_NoMemory();
6156 return -1;
6157 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006158 self->marks_size = (Py_ssize_t)alloc;
6159 }
6160
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006161 self->stack->mark_set = 1;
6162 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006163
6164 return 0;
6165}
6166
6167static int
6168load_reduce(UnpicklerObject *self)
6169{
6170 PyObject *callable = NULL;
6171 PyObject *argtup = NULL;
6172 PyObject *obj = NULL;
6173
6174 PDATA_POP(self->stack, argtup);
6175 if (argtup == NULL)
6176 return -1;
6177 PDATA_POP(self->stack, callable);
6178 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006179 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006180 Py_DECREF(callable);
6181 }
6182 Py_DECREF(argtup);
6183
6184 if (obj == NULL)
6185 return -1;
6186
6187 PDATA_PUSH(self->stack, obj, -1);
6188 return 0;
6189}
6190
6191/* Just raises an error if we don't know the protocol specified. PROTO
6192 * is the first opcode for protocols >= 2.
6193 */
6194static int
6195load_proto(UnpicklerObject *self)
6196{
6197 char *s;
6198 int i;
6199
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006200 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006201 return -1;
6202
6203 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006204 if (i <= HIGHEST_PROTOCOL) {
6205 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006207 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006208
6209 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6210 return -1;
6211}
6212
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006213static int
6214load_frame(UnpicklerObject *self)
6215{
6216 char *s;
6217 Py_ssize_t frame_len;
6218
6219 if (_Unpickler_Read(self, &s, 8) < 0)
6220 return -1;
6221
6222 frame_len = calc_binsize(s, 8);
6223 if (frame_len < 0) {
6224 PyErr_Format(PyExc_OverflowError,
6225 "FRAME length exceeds system's maximum of %zd bytes",
6226 PY_SSIZE_T_MAX);
6227 return -1;
6228 }
6229
6230 if (_Unpickler_Read(self, &s, frame_len) < 0)
6231 return -1;
6232
6233 /* Rewind to start of frame */
6234 self->next_read_idx -= frame_len;
6235 return 0;
6236}
6237
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006238static PyObject *
6239load(UnpicklerObject *self)
6240{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006242 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006243
6244 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006245 self->stack->mark_set = 0;
6246 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006247 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006248 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006249 Pdata_clear(self->stack, 0);
6250
6251 /* Convenient macros for the dispatch while-switch loop just below. */
6252#define OP(opcode, load_func) \
6253 case opcode: if (load_func(self) < 0) break; continue;
6254
6255#define OP_ARG(opcode, load_func, arg) \
6256 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6257
6258 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006259 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006260 break;
6261
6262 switch ((enum opcode)s[0]) {
6263 OP(NONE, load_none)
6264 OP(BININT, load_binint)
6265 OP(BININT1, load_binint1)
6266 OP(BININT2, load_binint2)
6267 OP(INT, load_int)
6268 OP(LONG, load_long)
6269 OP_ARG(LONG1, load_counted_long, 1)
6270 OP_ARG(LONG4, load_counted_long, 4)
6271 OP(FLOAT, load_float)
6272 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006273 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6274 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6275 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6276 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6277 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006278 OP(STRING, load_string)
6279 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006280 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6281 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6282 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006283 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6284 OP_ARG(TUPLE1, load_counted_tuple, 1)
6285 OP_ARG(TUPLE2, load_counted_tuple, 2)
6286 OP_ARG(TUPLE3, load_counted_tuple, 3)
6287 OP(TUPLE, load_tuple)
6288 OP(EMPTY_LIST, load_empty_list)
6289 OP(LIST, load_list)
6290 OP(EMPTY_DICT, load_empty_dict)
6291 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006292 OP(EMPTY_SET, load_empty_set)
6293 OP(ADDITEMS, load_additems)
6294 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006295 OP(OBJ, load_obj)
6296 OP(INST, load_inst)
6297 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006298 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006299 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006300 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006301 OP(APPEND, load_append)
6302 OP(APPENDS, load_appends)
6303 OP(BUILD, load_build)
6304 OP(DUP, load_dup)
6305 OP(BINGET, load_binget)
6306 OP(LONG_BINGET, load_long_binget)
6307 OP(GET, load_get)
6308 OP(MARK, load_mark)
6309 OP(BINPUT, load_binput)
6310 OP(LONG_BINPUT, load_long_binput)
6311 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006312 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006313 OP(POP, load_pop)
6314 OP(POP_MARK, load_pop_mark)
6315 OP(SETITEM, load_setitem)
6316 OP(SETITEMS, load_setitems)
6317 OP(PERSID, load_persid)
6318 OP(BINPERSID, load_binpersid)
6319 OP(REDUCE, load_reduce)
6320 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006321 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006322 OP_ARG(EXT1, load_extension, 1)
6323 OP_ARG(EXT2, load_extension, 2)
6324 OP_ARG(EXT4, load_extension, 4)
6325 OP_ARG(NEWTRUE, load_bool, Py_True)
6326 OP_ARG(NEWFALSE, load_bool, Py_False)
6327
6328 case STOP:
6329 break;
6330
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006331 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006332 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006333 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006334 }
6335 else {
6336 PickleState *st = _Pickle_GetGlobalState();
6337 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006338 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006339 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006340 return NULL;
6341 }
6342
6343 break; /* and we are done! */
6344 }
6345
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006346 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006347 return NULL;
6348 }
6349
Victor Stinner2ae57e32013-10-31 13:39:23 +01006350 if (_Unpickler_SkipConsumed(self) < 0)
6351 return NULL;
6352
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006353 PDATA_POP(self->stack, value);
6354 return value;
6355}
6356
Larry Hastings61272b72014-01-07 12:41:53 -08006357/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006358
6359_pickle.Unpickler.load
6360
6361Load a pickle.
6362
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006363Read a pickled object representation from the open file object given
6364in the constructor, and return the reconstituted object hierarchy
6365specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006366[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006367
Larry Hastings3cceb382014-01-04 11:09:09 -08006368static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006369_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006370/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006371{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006372 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006373
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006374 /* Check whether the Unpickler was initialized correctly. This prevents
6375 segfaulting if a subclass overridden __init__ with a function that does
6376 not call Unpickler.__init__(). Here, we simply ensure that self->read
6377 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006378 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006379 PickleState *st = _Pickle_GetGlobalState();
6380 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006381 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006382 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006383 return NULL;
6384 }
6385
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006386 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006387}
6388
6389/* The name of find_class() is misleading. In newer pickle protocols, this
6390 function is used for loading any global (i.e., functions), not just
6391 classes. The name is kept only for backward compatibility. */
6392
Larry Hastings61272b72014-01-07 12:41:53 -08006393/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006394
6395_pickle.Unpickler.find_class
6396
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006397 module_name: object
6398 global_name: object
6399 /
6400
6401Return an object from a specified module.
6402
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006403If necessary, the module will be imported. Subclasses may override
6404this method (e.g. to restrict unpickling of arbitrary classes and
6405functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006406
6407This method is called whenever a class or a function object is
6408needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006409[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006410
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006411static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006412_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6413 PyObject *module_name,
6414 PyObject *global_name)
6415/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006416{
6417 PyObject *global;
6418 PyObject *modules_dict;
6419 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006420 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006421
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006422 /* Try to map the old names used in Python 2.x to the new ones used in
6423 Python 3.x. We do this only with old pickle protocols and when the
6424 user has not disabled the feature. */
6425 if (self->proto < 3 && self->fix_imports) {
6426 PyObject *key;
6427 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006428 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006429
6430 /* Check if the global (i.e., a function or a class) was renamed
6431 or moved to another module. */
6432 key = PyTuple_Pack(2, module_name, global_name);
6433 if (key == NULL)
6434 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006435 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006436 Py_DECREF(key);
6437 if (item) {
6438 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6439 PyErr_Format(PyExc_RuntimeError,
6440 "_compat_pickle.NAME_MAPPING values should be "
6441 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6442 return NULL;
6443 }
6444 module_name = PyTuple_GET_ITEM(item, 0);
6445 global_name = PyTuple_GET_ITEM(item, 1);
6446 if (!PyUnicode_Check(module_name) ||
6447 !PyUnicode_Check(global_name)) {
6448 PyErr_Format(PyExc_RuntimeError,
6449 "_compat_pickle.NAME_MAPPING values should be "
6450 "pairs of str, not (%.200s, %.200s)",
6451 Py_TYPE(module_name)->tp_name,
6452 Py_TYPE(global_name)->tp_name);
6453 return NULL;
6454 }
6455 }
6456 else if (PyErr_Occurred()) {
6457 return NULL;
6458 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006459 else {
6460 /* Check if the module was renamed. */
6461 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6462 if (item) {
6463 if (!PyUnicode_Check(item)) {
6464 PyErr_Format(PyExc_RuntimeError,
6465 "_compat_pickle.IMPORT_MAPPING values should be "
6466 "strings, not %.200s", Py_TYPE(item)->tp_name);
6467 return NULL;
6468 }
6469 module_name = item;
6470 }
6471 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006472 return NULL;
6473 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006474 }
6475 }
6476
Victor Stinnerbb520202013-11-06 22:40:41 +01006477 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006478 if (modules_dict == NULL) {
6479 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006480 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006481 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006483 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006484 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006485 if (PyErr_Occurred())
6486 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006487 module = PyImport_Import(module_name);
6488 if (module == NULL)
6489 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006490 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006491 Py_DECREF(module);
6492 }
Victor Stinner121aab42011-09-29 23:40:53 +02006493 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006494 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006495 }
6496 return global;
6497}
6498
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006499/*[clinic input]
6500
6501_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6502
6503Returns size in memory, in bytes.
6504[clinic start generated code]*/
6505
6506static Py_ssize_t
6507_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6508/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6509{
6510 Py_ssize_t res;
6511
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006512 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006513 if (self->memo != NULL)
6514 res += self->memo_size * sizeof(PyObject *);
6515 if (self->marks != NULL)
6516 res += self->marks_size * sizeof(Py_ssize_t);
6517 if (self->input_line != NULL)
6518 res += strlen(self->input_line) + 1;
6519 if (self->encoding != NULL)
6520 res += strlen(self->encoding) + 1;
6521 if (self->errors != NULL)
6522 res += strlen(self->errors) + 1;
6523 return res;
6524}
6525
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006526static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006527 _PICKLE_UNPICKLER_LOAD_METHODDEF
6528 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006529 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006530 {NULL, NULL} /* sentinel */
6531};
6532
6533static void
6534Unpickler_dealloc(UnpicklerObject *self)
6535{
6536 PyObject_GC_UnTrack((PyObject *)self);
6537 Py_XDECREF(self->readline);
6538 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006539 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540 Py_XDECREF(self->stack);
6541 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006542 if (self->buffer.buf != NULL) {
6543 PyBuffer_Release(&self->buffer);
6544 self->buffer.buf = NULL;
6545 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006547 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006548 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006549 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006550 PyMem_Free(self->encoding);
6551 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006552
6553 Py_TYPE(self)->tp_free((PyObject *)self);
6554}
6555
6556static int
6557Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6558{
6559 Py_VISIT(self->readline);
6560 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006561 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006562 Py_VISIT(self->stack);
6563 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006564 return 0;
6565}
6566
6567static int
6568Unpickler_clear(UnpicklerObject *self)
6569{
6570 Py_CLEAR(self->readline);
6571 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006572 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006573 Py_CLEAR(self->stack);
6574 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006575 if (self->buffer.buf != NULL) {
6576 PyBuffer_Release(&self->buffer);
6577 self->buffer.buf = NULL;
6578 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006579
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006580 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006581 PyMem_Free(self->marks);
6582 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006583 PyMem_Free(self->input_line);
6584 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006585 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006586 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006587 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006588 self->errors = NULL;
6589
6590 return 0;
6591}
6592
Larry Hastings61272b72014-01-07 12:41:53 -08006593/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006594
6595_pickle.Unpickler.__init__
6596
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006597 file: object
6598 *
6599 fix_imports: bool = True
6600 encoding: str = 'ASCII'
6601 errors: str = 'strict'
6602
6603This takes a binary file for reading a pickle data stream.
6604
6605The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006606protocol argument is needed. Bytes past the pickled object's
6607representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006608
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006609The argument *file* must have two methods, a read() method that takes
6610an integer argument, and a readline() method that requires no
6611arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006612binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006613other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006614
6615Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006616which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006617generated by Python 2. If *fix_imports* is True, pickle will try to
6618map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006619*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006620instances pickled by Python 2; these default to 'ASCII' and 'strict',
6621respectively. The *encoding* can be 'bytes' to read these 8-bit
6622string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006623[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006624
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006625static int
Larry Hastings89964c42015-04-14 18:07:59 -04006626_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6627 int fix_imports, const char *encoding,
6628 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006629/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006630{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006631 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006632
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006633 /* In case of multiple __init__() calls, clear previous content. */
6634 if (self->read != NULL)
6635 (void)Unpickler_clear(self);
6636
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006637 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006638 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006639
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006640 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006641 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006642
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006643 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006644 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006645 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006646
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006647 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006648 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6649 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006651 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006652 }
6653 else {
6654 self->pers_func = NULL;
6655 }
6656
6657 self->stack = (Pdata *)Pdata_New();
6658 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006659 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006660
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006661 self->memo_size = 32;
6662 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006663 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006664 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006665
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006666 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006667
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006668 return 0;
6669}
6670
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006671
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006672/* Define a proxy object for the Unpickler's internal memo object. This is to
6673 * avoid breaking code like:
6674 * unpickler.memo.clear()
6675 * and
6676 * unpickler.memo = saved_memo
6677 * Is this a good idea? Not really, but we don't want to break code that uses
6678 * it. Note that we don't implement the entire mapping API here. This is
6679 * intentional, as these should be treated as black-box implementation details.
6680 *
6681 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006682 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006683 */
6684
Larry Hastings61272b72014-01-07 12:41:53 -08006685/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006686_pickle.UnpicklerMemoProxy.clear
6687
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006688Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006689[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006690
Larry Hastings3cceb382014-01-04 11:09:09 -08006691static PyObject *
6692_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006693/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006694{
6695 _Unpickler_MemoCleanup(self->unpickler);
6696 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6697 if (self->unpickler->memo == NULL)
6698 return NULL;
6699 Py_RETURN_NONE;
6700}
6701
Larry Hastings61272b72014-01-07 12:41:53 -08006702/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006703_pickle.UnpicklerMemoProxy.copy
6704
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006705Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006706[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006707
Larry Hastings3cceb382014-01-04 11:09:09 -08006708static PyObject *
6709_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006710/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006711{
6712 Py_ssize_t i;
6713 PyObject *new_memo = PyDict_New();
6714 if (new_memo == NULL)
6715 return NULL;
6716
6717 for (i = 0; i < self->unpickler->memo_size; i++) {
6718 int status;
6719 PyObject *key, *value;
6720
6721 value = self->unpickler->memo[i];
6722 if (value == NULL)
6723 continue;
6724
6725 key = PyLong_FromSsize_t(i);
6726 if (key == NULL)
6727 goto error;
6728 status = PyDict_SetItem(new_memo, key, value);
6729 Py_DECREF(key);
6730 if (status < 0)
6731 goto error;
6732 }
6733 return new_memo;
6734
6735error:
6736 Py_DECREF(new_memo);
6737 return NULL;
6738}
6739
Larry Hastings61272b72014-01-07 12:41:53 -08006740/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006741_pickle.UnpicklerMemoProxy.__reduce__
6742
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006743Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006744[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006745
Larry Hastings3cceb382014-01-04 11:09:09 -08006746static PyObject *
6747_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006748/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006749{
6750 PyObject *reduce_value;
6751 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006752 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006753 if (contents == NULL)
6754 return NULL;
6755
6756 reduce_value = PyTuple_New(2);
6757 if (reduce_value == NULL) {
6758 Py_DECREF(contents);
6759 return NULL;
6760 }
6761 constructor_args = PyTuple_New(1);
6762 if (constructor_args == NULL) {
6763 Py_DECREF(contents);
6764 Py_DECREF(reduce_value);
6765 return NULL;
6766 }
6767 PyTuple_SET_ITEM(constructor_args, 0, contents);
6768 Py_INCREF((PyObject *)&PyDict_Type);
6769 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6770 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6771 return reduce_value;
6772}
6773
6774static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006775 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6776 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6777 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006778 {NULL, NULL} /* sentinel */
6779};
6780
6781static void
6782UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6783{
6784 PyObject_GC_UnTrack(self);
6785 Py_XDECREF(self->unpickler);
6786 PyObject_GC_Del((PyObject *)self);
6787}
6788
6789static int
6790UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6791 visitproc visit, void *arg)
6792{
6793 Py_VISIT(self->unpickler);
6794 return 0;
6795}
6796
6797static int
6798UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6799{
6800 Py_CLEAR(self->unpickler);
6801 return 0;
6802}
6803
6804static PyTypeObject UnpicklerMemoProxyType = {
6805 PyVarObject_HEAD_INIT(NULL, 0)
6806 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6807 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6808 0,
6809 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6810 0, /* tp_print */
6811 0, /* tp_getattr */
6812 0, /* tp_setattr */
6813 0, /* tp_compare */
6814 0, /* tp_repr */
6815 0, /* tp_as_number */
6816 0, /* tp_as_sequence */
6817 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006818 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006819 0, /* tp_call */
6820 0, /* tp_str */
6821 PyObject_GenericGetAttr, /* tp_getattro */
6822 PyObject_GenericSetAttr, /* tp_setattro */
6823 0, /* tp_as_buffer */
6824 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6825 0, /* tp_doc */
6826 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6827 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6828 0, /* tp_richcompare */
6829 0, /* tp_weaklistoffset */
6830 0, /* tp_iter */
6831 0, /* tp_iternext */
6832 unpicklerproxy_methods, /* tp_methods */
6833};
6834
6835static PyObject *
6836UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6837{
6838 UnpicklerMemoProxyObject *self;
6839
6840 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6841 &UnpicklerMemoProxyType);
6842 if (self == NULL)
6843 return NULL;
6844 Py_INCREF(unpickler);
6845 self->unpickler = unpickler;
6846 PyObject_GC_Track(self);
6847 return (PyObject *)self;
6848}
6849
6850/*****************************************************************************/
6851
6852
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006853static PyObject *
6854Unpickler_get_memo(UnpicklerObject *self)
6855{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006856 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006857}
6858
6859static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006860Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006861{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006862 PyObject **new_memo;
6863 Py_ssize_t new_memo_size = 0;
6864 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006865
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006866 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006867 PyErr_SetString(PyExc_TypeError,
6868 "attribute deletion is not supported");
6869 return -1;
6870 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006871
6872 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6873 UnpicklerObject *unpickler =
6874 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6875
6876 new_memo_size = unpickler->memo_size;
6877 new_memo = _Unpickler_NewMemo(new_memo_size);
6878 if (new_memo == NULL)
6879 return -1;
6880
6881 for (i = 0; i < new_memo_size; i++) {
6882 Py_XINCREF(unpickler->memo[i]);
6883 new_memo[i] = unpickler->memo[i];
6884 }
6885 }
6886 else if (PyDict_Check(obj)) {
6887 Py_ssize_t i = 0;
6888 PyObject *key, *value;
6889
6890 new_memo_size = PyDict_Size(obj);
6891 new_memo = _Unpickler_NewMemo(new_memo_size);
6892 if (new_memo == NULL)
6893 return -1;
6894
6895 while (PyDict_Next(obj, &i, &key, &value)) {
6896 Py_ssize_t idx;
6897 if (!PyLong_Check(key)) {
6898 PyErr_SetString(PyExc_TypeError,
6899 "memo key must be integers");
6900 goto error;
6901 }
6902 idx = PyLong_AsSsize_t(key);
6903 if (idx == -1 && PyErr_Occurred())
6904 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006905 if (idx < 0) {
6906 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006907 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006908 goto error;
6909 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006910 if (_Unpickler_MemoPut(self, idx, value) < 0)
6911 goto error;
6912 }
6913 }
6914 else {
6915 PyErr_Format(PyExc_TypeError,
6916 "'memo' attribute must be an UnpicklerMemoProxy object"
6917 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006918 return -1;
6919 }
6920
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006921 _Unpickler_MemoCleanup(self);
6922 self->memo_size = new_memo_size;
6923 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006924
6925 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006926
6927 error:
6928 if (new_memo_size) {
6929 i = new_memo_size;
6930 while (--i >= 0) {
6931 Py_XDECREF(new_memo[i]);
6932 }
6933 PyMem_FREE(new_memo);
6934 }
6935 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006936}
6937
6938static PyObject *
6939Unpickler_get_persload(UnpicklerObject *self)
6940{
6941 if (self->pers_func == NULL)
6942 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6943 else
6944 Py_INCREF(self->pers_func);
6945 return self->pers_func;
6946}
6947
6948static int
6949Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6950{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006951 if (value == NULL) {
6952 PyErr_SetString(PyExc_TypeError,
6953 "attribute deletion is not supported");
6954 return -1;
6955 }
6956 if (!PyCallable_Check(value)) {
6957 PyErr_SetString(PyExc_TypeError,
6958 "persistent_load must be a callable taking "
6959 "one argument");
6960 return -1;
6961 }
6962
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006963 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03006964 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006965
6966 return 0;
6967}
6968
6969static PyGetSetDef Unpickler_getsets[] = {
6970 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6971 {"persistent_load", (getter)Unpickler_get_persload,
6972 (setter)Unpickler_set_persload},
6973 {NULL}
6974};
6975
6976static PyTypeObject Unpickler_Type = {
6977 PyVarObject_HEAD_INIT(NULL, 0)
6978 "_pickle.Unpickler", /*tp_name*/
6979 sizeof(UnpicklerObject), /*tp_basicsize*/
6980 0, /*tp_itemsize*/
6981 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6982 0, /*tp_print*/
6983 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006984 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006985 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006986 0, /*tp_repr*/
6987 0, /*tp_as_number*/
6988 0, /*tp_as_sequence*/
6989 0, /*tp_as_mapping*/
6990 0, /*tp_hash*/
6991 0, /*tp_call*/
6992 0, /*tp_str*/
6993 0, /*tp_getattro*/
6994 0, /*tp_setattro*/
6995 0, /*tp_as_buffer*/
6996 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006997 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006998 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6999 (inquiry)Unpickler_clear, /*tp_clear*/
7000 0, /*tp_richcompare*/
7001 0, /*tp_weaklistoffset*/
7002 0, /*tp_iter*/
7003 0, /*tp_iternext*/
7004 Unpickler_methods, /*tp_methods*/
7005 0, /*tp_members*/
7006 Unpickler_getsets, /*tp_getset*/
7007 0, /*tp_base*/
7008 0, /*tp_dict*/
7009 0, /*tp_descr_get*/
7010 0, /*tp_descr_set*/
7011 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007012 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007013 PyType_GenericAlloc, /*tp_alloc*/
7014 PyType_GenericNew, /*tp_new*/
7015 PyObject_GC_Del, /*tp_free*/
7016 0, /*tp_is_gc*/
7017};
7018
Larry Hastings61272b72014-01-07 12:41:53 -08007019/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007020
7021_pickle.dump
7022
7023 obj: object
7024 file: object
7025 protocol: object = NULL
7026 *
7027 fix_imports: bool = True
7028
7029Write a pickled representation of obj to the open file object file.
7030
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007031This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7032be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007033
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007034The optional *protocol* argument tells the pickler to use the given
7035protocol supported protocols are 0, 1, 2, 3 and 4. The default
7036protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007037
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007038Specifying a negative protocol version selects the highest protocol
7039version supported. The higher the protocol used, the more recent the
7040version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007041
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007042The *file* argument must have a write() method that accepts a single
7043bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007044writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007045this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007046
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007047If *fix_imports* is True and protocol is less than 3, pickle will try
7048to map the new Python 3 names to the old module names used in Python
70492, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007050[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007051
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007052static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007053_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007054 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007055/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007056{
7057 PicklerObject *pickler = _Pickler_New();
7058
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007059 if (pickler == NULL)
7060 return NULL;
7061
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007062 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007063 goto error;
7064
7065 if (_Pickler_SetOutputStream(pickler, file) < 0)
7066 goto error;
7067
7068 if (dump(pickler, obj) < 0)
7069 goto error;
7070
7071 if (_Pickler_FlushToFile(pickler) < 0)
7072 goto error;
7073
7074 Py_DECREF(pickler);
7075 Py_RETURN_NONE;
7076
7077 error:
7078 Py_XDECREF(pickler);
7079 return NULL;
7080}
7081
Larry Hastings61272b72014-01-07 12:41:53 -08007082/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007083
7084_pickle.dumps
7085
7086 obj: object
7087 protocol: object = NULL
7088 *
7089 fix_imports: bool = True
7090
7091Return the pickled representation of the object as a bytes object.
7092
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007093The optional *protocol* argument tells the pickler to use the given
7094protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7095protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007096
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007097Specifying a negative protocol version selects the highest protocol
7098version supported. The higher the protocol used, the more recent the
7099version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007100
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007101If *fix_imports* is True and *protocol* is less than 3, pickle will
7102try to map the new Python 3 names to the old module names used in
7103Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007104[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007105
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007106static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007107_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007108 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007109/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007110{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007111 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007112 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007113
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007114 if (pickler == NULL)
7115 return NULL;
7116
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007117 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007118 goto error;
7119
7120 if (dump(pickler, obj) < 0)
7121 goto error;
7122
7123 result = _Pickler_GetString(pickler);
7124 Py_DECREF(pickler);
7125 return result;
7126
7127 error:
7128 Py_XDECREF(pickler);
7129 return NULL;
7130}
7131
Larry Hastings61272b72014-01-07 12:41:53 -08007132/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007133
7134_pickle.load
7135
7136 file: object
7137 *
7138 fix_imports: bool = True
7139 encoding: str = 'ASCII'
7140 errors: str = 'strict'
7141
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007142Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007143
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007144This is equivalent to ``Unpickler(file).load()``, but may be more
7145efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007146
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007147The protocol version of the pickle is detected automatically, so no
7148protocol argument is needed. Bytes past the pickled object's
7149representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007150
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007151The argument *file* must have two methods, a read() method that takes
7152an integer argument, and a readline() method that requires no
7153arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007154binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007155other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007156
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007157Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007158which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007159generated by Python 2. If *fix_imports* is True, pickle will try to
7160map the old Python 2 names to the new names used in Python 3. The
7161*encoding* and *errors* tell pickle how to decode 8-bit string
7162instances pickled by Python 2; these default to 'ASCII' and 'strict',
7163respectively. The *encoding* can be 'bytes' to read these 8-bit
7164string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007165[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007166
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007167static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007168_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007169 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007170/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007171{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007172 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007173 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007174
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007175 if (unpickler == NULL)
7176 return NULL;
7177
7178 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7179 goto error;
7180
7181 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7182 goto error;
7183
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007184 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007185
7186 result = load(unpickler);
7187 Py_DECREF(unpickler);
7188 return result;
7189
7190 error:
7191 Py_XDECREF(unpickler);
7192 return NULL;
7193}
7194
Larry Hastings61272b72014-01-07 12:41:53 -08007195/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007196
7197_pickle.loads
7198
7199 data: object
7200 *
7201 fix_imports: bool = True
7202 encoding: str = 'ASCII'
7203 errors: str = 'strict'
7204
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007205Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007206
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007207The protocol version of the pickle is detected automatically, so no
7208protocol argument is needed. Bytes past the pickled object's
7209representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007210
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007211Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007212which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007213generated by Python 2. If *fix_imports* is True, pickle will try to
7214map the old Python 2 names to the new names used in Python 3. The
7215*encoding* and *errors* tell pickle how to decode 8-bit string
7216instances pickled by Python 2; these default to 'ASCII' and 'strict',
7217respectively. The *encoding* can be 'bytes' to read these 8-bit
7218string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007219[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007220
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007221static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007222_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007223 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007224/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007225{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007226 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007227 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007228
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007229 if (unpickler == NULL)
7230 return NULL;
7231
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007232 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007233 goto error;
7234
7235 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7236 goto error;
7237
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007238 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007239
7240 result = load(unpickler);
7241 Py_DECREF(unpickler);
7242 return result;
7243
7244 error:
7245 Py_XDECREF(unpickler);
7246 return NULL;
7247}
7248
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007249static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007250 _PICKLE_DUMP_METHODDEF
7251 _PICKLE_DUMPS_METHODDEF
7252 _PICKLE_LOAD_METHODDEF
7253 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007254 {NULL, NULL} /* sentinel */
7255};
7256
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007257static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007258pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007259{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007260 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007261 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007262}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007263
Stefan Krahf483b0f2013-12-14 13:43:10 +01007264static void
7265pickle_free(PyObject *m)
7266{
7267 _Pickle_ClearState(_Pickle_GetState(m));
7268}
7269
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007270static int
7271pickle_traverse(PyObject *m, visitproc visit, void *arg)
7272{
7273 PickleState *st = _Pickle_GetState(m);
7274 Py_VISIT(st->PickleError);
7275 Py_VISIT(st->PicklingError);
7276 Py_VISIT(st->UnpicklingError);
7277 Py_VISIT(st->dispatch_table);
7278 Py_VISIT(st->extension_registry);
7279 Py_VISIT(st->extension_cache);
7280 Py_VISIT(st->inverted_registry);
7281 Py_VISIT(st->name_mapping_2to3);
7282 Py_VISIT(st->import_mapping_2to3);
7283 Py_VISIT(st->name_mapping_3to2);
7284 Py_VISIT(st->import_mapping_3to2);
7285 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007286 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007287 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007288}
7289
7290static struct PyModuleDef _picklemodule = {
7291 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007292 "_pickle", /* m_name */
7293 pickle_module_doc, /* m_doc */
7294 sizeof(PickleState), /* m_size */
7295 pickle_methods, /* m_methods */
7296 NULL, /* m_reload */
7297 pickle_traverse, /* m_traverse */
7298 pickle_clear, /* m_clear */
7299 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007300};
7301
7302PyMODINIT_FUNC
7303PyInit__pickle(void)
7304{
7305 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007306 PickleState *st;
7307
7308 m = PyState_FindModule(&_picklemodule);
7309 if (m) {
7310 Py_INCREF(m);
7311 return m;
7312 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007313
7314 if (PyType_Ready(&Unpickler_Type) < 0)
7315 return NULL;
7316 if (PyType_Ready(&Pickler_Type) < 0)
7317 return NULL;
7318 if (PyType_Ready(&Pdata_Type) < 0)
7319 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007320 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7321 return NULL;
7322 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7323 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007324
7325 /* Create the module and add the functions. */
7326 m = PyModule_Create(&_picklemodule);
7327 if (m == NULL)
7328 return NULL;
7329
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007330 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007331 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7332 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007333 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007334 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7335 return NULL;
7336
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007337 st = _Pickle_GetState(m);
7338
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007339 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007340 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7341 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007342 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007343 st->PicklingError = \
7344 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7345 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007346 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007347 st->UnpicklingError = \
7348 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7349 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007350 return NULL;
7351
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007352 Py_INCREF(st->PickleError);
7353 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007354 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007355 Py_INCREF(st->PicklingError);
7356 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007357 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007358 Py_INCREF(st->UnpicklingError);
7359 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007360 return NULL;
7361
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007362 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007363 return NULL;
7364
7365 return m;
7366}