blob: 34ce38c5d3b2c40b4f81a367b3c1a5fbbb56a938 [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001
2/* Core extension modules are built-in on some platforms (e.g. Windows). */
3#ifdef Py_BUILD_CORE
Eric Snowfc1bf872017-09-11 18:30:43 -07004#define Py_BUILD_CORE_BUILTIN
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#undef Py_BUILD_CORE
6#endif
7
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008#include "Python.h"
9#include "structmember.h"
10
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -080011PyDoc_STRVAR(pickle_module_doc,
12"Optimized C implementation for the Python pickle module.");
13
Larry Hastings61272b72014-01-07 12:41:53 -080014/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080016class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
17class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
18class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
19class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080020[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080022
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000023/* Bump this when new opcodes are added to the pickle protocol. */
24enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010025 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000026 DEFAULT_PROTOCOL = 3
27};
28
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029/* Pickle opcodes. These must be kept updated with pickle.py.
30 Extensive docs are in pickletools.py. */
31enum opcode {
32 MARK = '(',
33 STOP = '.',
34 POP = '0',
35 POP_MARK = '1',
36 DUP = '2',
37 FLOAT = 'F',
38 INT = 'I',
39 BININT = 'J',
40 BININT1 = 'K',
41 LONG = 'L',
42 BININT2 = 'M',
43 NONE = 'N',
44 PERSID = 'P',
45 BINPERSID = 'Q',
46 REDUCE = 'R',
47 STRING = 'S',
48 BINSTRING = 'T',
49 SHORT_BINSTRING = 'U',
50 UNICODE = 'V',
51 BINUNICODE = 'X',
52 APPEND = 'a',
53 BUILD = 'b',
54 GLOBAL = 'c',
55 DICT = 'd',
56 EMPTY_DICT = '}',
57 APPENDS = 'e',
58 GET = 'g',
59 BINGET = 'h',
60 INST = 'i',
61 LONG_BINGET = 'j',
62 LIST = 'l',
63 EMPTY_LIST = ']',
64 OBJ = 'o',
65 PUT = 'p',
66 BINPUT = 'q',
67 LONG_BINPUT = 'r',
68 SETITEM = 's',
69 TUPLE = 't',
70 EMPTY_TUPLE = ')',
71 SETITEMS = 'u',
72 BINFLOAT = 'G',
73
74 /* Protocol 2. */
75 PROTO = '\x80',
76 NEWOBJ = '\x81',
77 EXT1 = '\x82',
78 EXT2 = '\x83',
79 EXT4 = '\x84',
80 TUPLE1 = '\x85',
81 TUPLE2 = '\x86',
82 TUPLE3 = '\x87',
83 NEWTRUE = '\x88',
84 NEWFALSE = '\x89',
85 LONG1 = '\x8a',
86 LONG4 = '\x8b',
87
88 /* Protocol 3 (Python 3.x) */
89 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010090 SHORT_BINBYTES = 'C',
91
92 /* Protocol 4 */
93 SHORT_BINUNICODE = '\x8c',
94 BINUNICODE8 = '\x8d',
95 BINBYTES8 = '\x8e',
96 EMPTY_SET = '\x8f',
97 ADDITEMS = '\x90',
98 FROZENSET = '\x91',
99 NEWOBJ_EX = '\x92',
100 STACK_GLOBAL = '\x93',
101 MEMOIZE = '\x94',
102 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000103};
104
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000105enum {
106 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
107 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
108 break if this gets out of synch with pickle.py, but it's unclear that would
109 help anything either. */
110 BATCHSIZE = 1000,
111
112 /* Nesting limit until Pickler, when running in "fast mode", starts
113 checking for self-referential data-structures. */
114 FAST_NESTING_LIMIT = 50,
115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000116 /* Initial size of the write buffer of Pickler. */
117 WRITE_BUF_SIZE = 4096,
118
Antoine Pitrou04248a82010-10-12 20:51:21 +0000119 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100120 PREFETCH = 8192 * 16,
121
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200122 FRAME_SIZE_MIN = 4,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100123 FRAME_SIZE_TARGET = 64 * 1024,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100124 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000125};
126
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800127/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129/* State of the pickle module, per PEP 3121. */
130typedef struct {
131 /* Exception classes for pickle. */
132 PyObject *PickleError;
133 PyObject *PicklingError;
134 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800136 /* copyreg.dispatch_table, {type_object: pickling_function} */
137 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800139 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* copyreg._extension_registry, {(module_name, function_name): code} */
142 PyObject *extension_registry;
143 /* copyreg._extension_cache, {code: object} */
144 PyObject *extension_cache;
145 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
146 PyObject *inverted_registry;
147
148 /* Import mappings for compatibility with Python 2.x */
149
150 /* _compat_pickle.NAME_MAPPING,
151 {(oldmodule, oldname): (newmodule, newname)} */
152 PyObject *name_mapping_2to3;
153 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
154 PyObject *import_mapping_2to3;
155 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
156 PyObject *name_mapping_3to2;
157 PyObject *import_mapping_3to2;
158
159 /* codecs.encode, used for saving bytes in older protocols */
160 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300161 /* builtins.getattr, used for saving nested names with protocol < 4 */
162 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300163 /* functools.partial, used for implementing __newobj_ex__ with protocols
164 2 and 3 */
165 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800166} PickleState;
167
168/* Forward declaration of the _pickle module definition. */
169static struct PyModuleDef _picklemodule;
170
171/* Given a module object, get its per-module state. */
172static PickleState *
173_Pickle_GetState(PyObject *module)
174{
175 return (PickleState *)PyModule_GetState(module);
176}
177
178/* Find the module instance imported in the currently running sub-interpreter
179 and get its state. */
180static PickleState *
181_Pickle_GetGlobalState(void)
182{
183 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
184}
185
186/* Clear the given pickle module state. */
187static void
188_Pickle_ClearState(PickleState *st)
189{
190 Py_CLEAR(st->PickleError);
191 Py_CLEAR(st->PicklingError);
192 Py_CLEAR(st->UnpicklingError);
193 Py_CLEAR(st->dispatch_table);
194 Py_CLEAR(st->extension_registry);
195 Py_CLEAR(st->extension_cache);
196 Py_CLEAR(st->inverted_registry);
197 Py_CLEAR(st->name_mapping_2to3);
198 Py_CLEAR(st->import_mapping_2to3);
199 Py_CLEAR(st->name_mapping_3to2);
200 Py_CLEAR(st->import_mapping_3to2);
201 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300202 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100203 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204}
205
206/* Initialize the given pickle module state. */
207static int
208_Pickle_InitState(PickleState *st)
209{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300210 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800211 PyObject *copyreg = NULL;
212 PyObject *compat_pickle = NULL;
213 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300214 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800215
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300216 builtins = PyEval_GetBuiltins();
217 if (builtins == NULL)
218 goto error;
219 st->getattr = PyDict_GetItemString(builtins, "getattr");
220 if (st->getattr == NULL)
221 goto error;
222 Py_INCREF(st->getattr);
223
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800224 copyreg = PyImport_ImportModule("copyreg");
225 if (!copyreg)
226 goto error;
227 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
228 if (!st->dispatch_table)
229 goto error;
230 if (!PyDict_CheckExact(st->dispatch_table)) {
231 PyErr_Format(PyExc_RuntimeError,
232 "copyreg.dispatch_table should be a dict, not %.200s",
233 Py_TYPE(st->dispatch_table)->tp_name);
234 goto error;
235 }
236 st->extension_registry = \
237 PyObject_GetAttrString(copyreg, "_extension_registry");
238 if (!st->extension_registry)
239 goto error;
240 if (!PyDict_CheckExact(st->extension_registry)) {
241 PyErr_Format(PyExc_RuntimeError,
242 "copyreg._extension_registry should be a dict, "
243 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
244 goto error;
245 }
246 st->inverted_registry = \
247 PyObject_GetAttrString(copyreg, "_inverted_registry");
248 if (!st->inverted_registry)
249 goto error;
250 if (!PyDict_CheckExact(st->inverted_registry)) {
251 PyErr_Format(PyExc_RuntimeError,
252 "copyreg._inverted_registry should be a dict, "
253 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
254 goto error;
255 }
256 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
257 if (!st->extension_cache)
258 goto error;
259 if (!PyDict_CheckExact(st->extension_cache)) {
260 PyErr_Format(PyExc_RuntimeError,
261 "copyreg._extension_cache should be a dict, "
262 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
263 goto error;
264 }
265 Py_CLEAR(copyreg);
266
267 /* Load the 2.x -> 3.x stdlib module mapping tables */
268 compat_pickle = PyImport_ImportModule("_compat_pickle");
269 if (!compat_pickle)
270 goto error;
271 st->name_mapping_2to3 = \
272 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
273 if (!st->name_mapping_2to3)
274 goto error;
275 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
276 PyErr_Format(PyExc_RuntimeError,
277 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
278 Py_TYPE(st->name_mapping_2to3)->tp_name);
279 goto error;
280 }
281 st->import_mapping_2to3 = \
282 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
283 if (!st->import_mapping_2to3)
284 goto error;
285 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
286 PyErr_Format(PyExc_RuntimeError,
287 "_compat_pickle.IMPORT_MAPPING should be a dict, "
288 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
289 goto error;
290 }
291 /* ... and the 3.x -> 2.x mapping tables */
292 st->name_mapping_3to2 = \
293 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
294 if (!st->name_mapping_3to2)
295 goto error;
296 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
297 PyErr_Format(PyExc_RuntimeError,
298 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
299 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
300 goto error;
301 }
302 st->import_mapping_3to2 = \
303 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
304 if (!st->import_mapping_3to2)
305 goto error;
306 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
307 PyErr_Format(PyExc_RuntimeError,
308 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
309 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
310 goto error;
311 }
312 Py_CLEAR(compat_pickle);
313
314 codecs = PyImport_ImportModule("codecs");
315 if (codecs == NULL)
316 goto error;
317 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
318 if (st->codecs_encode == NULL) {
319 goto error;
320 }
321 if (!PyCallable_Check(st->codecs_encode)) {
322 PyErr_Format(PyExc_RuntimeError,
323 "codecs.encode should be a callable, not %.200s",
324 Py_TYPE(st->codecs_encode)->tp_name);
325 goto error;
326 }
327 Py_CLEAR(codecs);
328
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300329 functools = PyImport_ImportModule("functools");
330 if (!functools)
331 goto error;
332 st->partial = PyObject_GetAttrString(functools, "partial");
333 if (!st->partial)
334 goto error;
335 Py_CLEAR(functools);
336
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 return 0;
338
339 error:
340 Py_CLEAR(copyreg);
341 Py_CLEAR(compat_pickle);
342 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300343 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800344 _Pickle_ClearState(st);
345 return -1;
346}
347
348/* Helper for calling a function with a single argument quickly.
349
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 This function steals the reference of the given argument. */
351static PyObject *
352_Pickle_FastCall(PyObject *func, PyObject *obj)
353{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 PyObject *result;
355
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100356 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200357 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800358 return result;
359}
360
361/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000362
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200363/* Retrieve and deconstruct a method for avoiding a reference cycle
364 (pickler -> bound method of pickler -> pickler) */
365static int
366init_method_ref(PyObject *self, _Py_Identifier *name,
367 PyObject **method_func, PyObject **method_self)
368{
369 PyObject *func, *func2;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200370 int ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200371
372 /* *method_func and *method_self should be consistent. All refcount decrements
373 should be occurred after setting *method_self and *method_func. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200374 ret = _PyObject_LookupAttrId(self, name, &func);
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200375 if (func == NULL) {
376 *method_self = NULL;
377 Py_CLEAR(*method_func);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200378 return ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200379 }
380
381 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
382 /* Deconstruct a bound Python method */
383 func2 = PyMethod_GET_FUNCTION(func);
384 Py_INCREF(func2);
385 *method_self = self; /* borrowed */
386 Py_XSETREF(*method_func, func2);
387 Py_DECREF(func);
388 return 0;
389 }
390 else {
391 *method_self = NULL;
392 Py_XSETREF(*method_func, func);
393 return 0;
394 }
395}
396
397/* Bind a method if it was deconstructed */
398static PyObject *
399reconstruct_method(PyObject *func, PyObject *self)
400{
401 if (self) {
402 return PyMethod_New(func, self);
403 }
404 else {
405 Py_INCREF(func);
406 return func;
407 }
408}
409
410static PyObject *
411call_method(PyObject *func, PyObject *self, PyObject *obj)
412{
413 if (self) {
414 return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
415 }
416 else {
417 return PyObject_CallFunctionObjArgs(func, obj, NULL);
418 }
419}
420
421/*************************************************************************/
422
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000423/* Internal data type used as the unpickling stack. */
424typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000425 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000426 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200427 int mark_set; /* is MARK set? */
428 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000429 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000430} Pdata;
431
432static void
433Pdata_dealloc(Pdata *self)
434{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200435 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436 while (--i >= 0) {
437 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000438 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000439 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000440 PyObject_Del(self);
441}
442
443static PyTypeObject Pdata_Type = {
444 PyVarObject_HEAD_INIT(NULL, 0)
445 "_pickle.Pdata", /*tp_name*/
446 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200447 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000448 (destructor)Pdata_dealloc, /*tp_dealloc*/
449};
450
451static PyObject *
452Pdata_New(void)
453{
454 Pdata *self;
455
456 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
457 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000458 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200459 self->mark_set = 0;
460 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000461 self->allocated = 8;
462 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000463 if (self->data)
464 return (PyObject *)self;
465 Py_DECREF(self);
466 return PyErr_NoMemory();
467}
468
469
470/* Retain only the initial clearto items. If clearto >= the current
471 * number of items, this is a (non-erroneous) NOP.
472 */
473static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200474Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000475{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200476 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200478 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000479 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 return 0;
481
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000482 while (--i >= clearto) {
483 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000484 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000485 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000486 return 0;
487}
488
489static int
490Pdata_grow(Pdata *self)
491{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000492 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200493 size_t allocated = (size_t)self->allocated;
494 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000495
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000496 new_allocated = (allocated >> 3) + 6;
497 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200498 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000499 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000500 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500501 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000502 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000503 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000504
505 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200506 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000507 return 0;
508
509 nomemory:
510 PyErr_NoMemory();
511 return -1;
512}
513
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200514static int
515Pdata_stack_underflow(Pdata *self)
516{
517 PickleState *st = _Pickle_GetGlobalState();
518 PyErr_SetString(st->UnpicklingError,
519 self->mark_set ?
520 "unexpected MARK found" :
521 "unpickling stack underflow");
522 return -1;
523}
524
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000525/* D is a Pdata*. Pop the topmost element and store it into V, which
526 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
527 * is raised and V is set to NULL.
528 */
529static PyObject *
530Pdata_pop(Pdata *self)
531{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200532 if (Py_SIZE(self) <= self->fence) {
533 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000534 return NULL;
535 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000536 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000537}
538#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
539
540static int
541Pdata_push(Pdata *self, PyObject *obj)
542{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000543 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000544 return -1;
545 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000546 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000547 return 0;
548}
549
550/* Push an object on stack, transferring its ownership to the stack. */
551#define PDATA_PUSH(D, O, ER) do { \
552 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
553
554/* Push an object on stack, adding a new reference to the object. */
555#define PDATA_APPEND(D, O, ER) do { \
556 Py_INCREF((O)); \
557 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
558
559static PyObject *
560Pdata_poptuple(Pdata *self, Py_ssize_t start)
561{
562 PyObject *tuple;
563 Py_ssize_t len, i, j;
564
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200565 if (start < self->fence) {
566 Pdata_stack_underflow(self);
567 return NULL;
568 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000569 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000570 tuple = PyTuple_New(len);
571 if (tuple == NULL)
572 return NULL;
573 for (i = start, j = 0; j < len; i++, j++)
574 PyTuple_SET_ITEM(tuple, j, self->data[i]);
575
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000576 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000577 return tuple;
578}
579
580static PyObject *
581Pdata_poplist(Pdata *self, Py_ssize_t start)
582{
583 PyObject *list;
584 Py_ssize_t len, i, j;
585
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000586 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000587 list = PyList_New(len);
588 if (list == NULL)
589 return NULL;
590 for (i = start, j = 0; j < len; i++, j++)
591 PyList_SET_ITEM(list, j, self->data[i]);
592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000594 return list;
595}
596
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000597typedef struct {
598 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200599 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000600} PyMemoEntry;
601
602typedef struct {
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700603 size_t mt_mask;
604 size_t mt_used;
605 size_t mt_allocated;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000606 PyMemoEntry *mt_table;
607} PyMemoTable;
608
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000609typedef struct PicklerObject {
610 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000611 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000612 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000613 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000614 PyObject *pers_func; /* persistent_id() method, can be NULL */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200615 PyObject *pers_func_self; /* borrowed reference to self if pers_func
616 is an unbound method, NULL otherwise */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100617 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000618
619 PyObject *write; /* write() method of the output stream. */
620 PyObject *output_buffer; /* Write into a local bytearray buffer before
621 flushing to the stream. */
622 Py_ssize_t output_len; /* Length of output_buffer. */
623 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000624 int proto; /* Pickle protocol number, >= 0 */
625 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100626 int framing; /* True when framing is enabled, proto >= 4 */
627 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000628 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100629 is no frame currently open. */
630
631 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000632 int fast; /* Enable fast mode if set to a true value.
633 The fast mode disable the usage of memo,
634 therefore speeding the pickling process by
635 not generating superfluous PUT opcodes. It
636 should not be used if with self-referential
637 objects. */
638 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000639 int fix_imports; /* Indicate whether Pickler should fix
640 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000641 PyObject *fast_memo;
642} PicklerObject;
643
644typedef struct UnpicklerObject {
645 PyObject_HEAD
646 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000647
648 /* The unpickler memo is just an array of PyObject *s. Using a dict
649 is unnecessary, since the keys are contiguous ints. */
650 PyObject **memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700651 size_t memo_size; /* Capacity of the memo array */
652 size_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000653
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000654 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200655 PyObject *pers_func_self; /* borrowed reference to self if pers_func
656 is an unbound method, NULL otherwise */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000657
658 Py_buffer buffer;
659 char *input_buffer;
660 char *input_line;
661 Py_ssize_t input_len;
662 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000663 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100664
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000665 PyObject *read; /* read() method of the input stream. */
666 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000667 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000668
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000669 char *encoding; /* Name of the encoding to be used for
670 decoding strings pickled using Python
671 2.x. The default value is "ASCII" */
672 char *errors; /* Name of errors handling scheme to used when
673 decoding strings. The default value is
674 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500675 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000676 objects. */
677 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
678 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000679 int proto; /* Protocol of the pickle loaded. */
680 int fix_imports; /* Indicate whether Unpickler should fix
681 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000682} UnpicklerObject;
683
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200684typedef struct {
685 PyObject_HEAD
686 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
687} PicklerMemoProxyObject;
688
689typedef struct {
690 PyObject_HEAD
691 UnpicklerObject *unpickler;
692} UnpicklerMemoProxyObject;
693
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000694/* Forward declarations */
695static int save(PicklerObject *, PyObject *, int);
696static int save_reduce(PicklerObject *, PyObject *, PyObject *);
697static PyTypeObject Pickler_Type;
698static PyTypeObject Unpickler_Type;
699
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200700#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000701
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000702/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300703 A custom hashtable mapping void* to Python ints. This is used by the pickler
704 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000705 a bunch of unnecessary object creation. This makes a huge performance
706 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000707
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000708#define MT_MINSIZE 8
709#define PERTURB_SHIFT 5
710
711
712static PyMemoTable *
713PyMemoTable_New(void)
714{
715 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
716 if (memo == NULL) {
717 PyErr_NoMemory();
718 return NULL;
719 }
720
721 memo->mt_used = 0;
722 memo->mt_allocated = MT_MINSIZE;
723 memo->mt_mask = MT_MINSIZE - 1;
724 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
725 if (memo->mt_table == NULL) {
726 PyMem_FREE(memo);
727 PyErr_NoMemory();
728 return NULL;
729 }
730 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
731
732 return memo;
733}
734
735static PyMemoTable *
736PyMemoTable_Copy(PyMemoTable *self)
737{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000738 PyMemoTable *new = PyMemoTable_New();
739 if (new == NULL)
740 return NULL;
741
742 new->mt_used = self->mt_used;
743 new->mt_allocated = self->mt_allocated;
744 new->mt_mask = self->mt_mask;
745 /* The table we get from _New() is probably smaller than we wanted.
746 Free it and allocate one that's the right size. */
747 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500748 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000749 if (new->mt_table == NULL) {
750 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200751 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000752 return NULL;
753 }
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700754 for (size_t i = 0; i < self->mt_allocated; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000755 Py_XINCREF(self->mt_table[i].me_key);
756 }
757 memcpy(new->mt_table, self->mt_table,
758 sizeof(PyMemoEntry) * self->mt_allocated);
759
760 return new;
761}
762
763static Py_ssize_t
764PyMemoTable_Size(PyMemoTable *self)
765{
766 return self->mt_used;
767}
768
769static int
770PyMemoTable_Clear(PyMemoTable *self)
771{
772 Py_ssize_t i = self->mt_allocated;
773
774 while (--i >= 0) {
775 Py_XDECREF(self->mt_table[i].me_key);
776 }
777 self->mt_used = 0;
778 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
779 return 0;
780}
781
782static void
783PyMemoTable_Del(PyMemoTable *self)
784{
785 if (self == NULL)
786 return;
787 PyMemoTable_Clear(self);
788
789 PyMem_FREE(self->mt_table);
790 PyMem_FREE(self);
791}
792
793/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
794 can be considerably simpler than dictobject.c's lookdict(). */
795static PyMemoEntry *
796_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
797{
798 size_t i;
799 size_t perturb;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700800 size_t mask = self->mt_mask;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000801 PyMemoEntry *table = self->mt_table;
802 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000803 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000804
805 i = hash & mask;
806 entry = &table[i];
807 if (entry->me_key == NULL || entry->me_key == key)
808 return entry;
809
810 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
811 i = (i << 2) + i + perturb + 1;
812 entry = &table[i & mask];
813 if (entry->me_key == NULL || entry->me_key == key)
814 return entry;
815 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700816 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000817}
818
819/* Returns -1 on failure, 0 on success. */
820static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700821_PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000822{
823 PyMemoEntry *oldtable = NULL;
824 PyMemoEntry *oldentry, *newentry;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700825 size_t new_size = MT_MINSIZE;
826 size_t to_process;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000827
828 assert(min_size > 0);
829
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700830 if (min_size > PY_SSIZE_T_MAX) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000831 PyErr_NoMemory();
832 return -1;
833 }
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700834
835 /* Find the smallest valid table size >= min_size. */
836 while (new_size < min_size) {
837 new_size <<= 1;
838 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000839 /* new_size needs to be a power of two. */
840 assert((new_size & (new_size - 1)) == 0);
841
842 /* Allocate new table. */
843 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500844 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000845 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200846 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000847 PyErr_NoMemory();
848 return -1;
849 }
850 self->mt_allocated = new_size;
851 self->mt_mask = new_size - 1;
852 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
853
854 /* Copy entries from the old table. */
855 to_process = self->mt_used;
856 for (oldentry = oldtable; to_process > 0; oldentry++) {
857 if (oldentry->me_key != NULL) {
858 to_process--;
859 /* newentry is a pointer to a chunk of the new
860 mt_table, so we're setting the key:value pair
861 in-place. */
862 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
863 newentry->me_key = oldentry->me_key;
864 newentry->me_value = oldentry->me_value;
865 }
866 }
867
868 /* Deallocate the old table. */
869 PyMem_FREE(oldtable);
870 return 0;
871}
872
873/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200874static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000875PyMemoTable_Get(PyMemoTable *self, PyObject *key)
876{
877 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
878 if (entry->me_key == NULL)
879 return NULL;
880 return &entry->me_value;
881}
882
883/* Returns -1 on failure, 0 on success. */
884static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200885PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000886{
887 PyMemoEntry *entry;
888
889 assert(key != NULL);
890
891 entry = _PyMemoTable_Lookup(self, key);
892 if (entry->me_key != NULL) {
893 entry->me_value = value;
894 return 0;
895 }
896 Py_INCREF(key);
897 entry->me_key = key;
898 entry->me_value = value;
899 self->mt_used++;
900
901 /* If we added a key, we can safely resize. Otherwise just return!
902 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
903 *
904 * Quadrupling the size improves average table sparseness
905 * (reducing collisions) at the cost of some memory. It also halves
906 * the number of expensive resize operations in a growing memo table.
907 *
908 * Very large memo tables (over 50K items) use doubling instead.
909 * This may help applications with severe memory constraints.
910 */
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700911 if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000912 return 0;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700913 }
914 // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow.
915 size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used;
916 return _PyMemoTable_ResizeTable(self, desired_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000917}
918
919#undef MT_MINSIZE
920#undef PERTURB_SHIFT
921
922/*************************************************************************/
923
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000925static int
926_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000927{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300928 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200929 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000930 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000931 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000932 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100933 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 return 0;
935}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000936
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100937static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100938_write_size64(char *out, size_t value)
939{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200940 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800941
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200942 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800943
944 for (i = 0; i < sizeof(size_t); i++) {
945 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
946 }
947 for (i = sizeof(size_t); i < 8; i++) {
948 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800949 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100950}
951
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100952static int
953_Pickler_CommitFrame(PicklerObject *self)
954{
955 size_t frame_len;
956 char *qdata;
957
958 if (!self->framing || self->frame_start == -1)
959 return 0;
960 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
961 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200962 if (frame_len >= FRAME_SIZE_MIN) {
963 qdata[0] = FRAME;
964 _write_size64(qdata + 1, frame_len);
965 }
966 else {
967 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
968 self->output_len -= FRAME_HEADER_SIZE;
969 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100970 self->frame_start = -1;
971 return 0;
972}
973
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000974static PyObject *
975_Pickler_GetString(PicklerObject *self)
976{
977 PyObject *output_buffer = self->output_buffer;
978
979 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100980
981 if (_Pickler_CommitFrame(self))
982 return NULL;
983
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000984 self->output_buffer = NULL;
985 /* Resize down to exact size */
986 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
987 return NULL;
988 return output_buffer;
989}
990
991static int
992_Pickler_FlushToFile(PicklerObject *self)
993{
994 PyObject *output, *result;
995
996 assert(self->write != NULL);
997
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100998 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 output = _Pickler_GetString(self);
1000 if (output == NULL)
1001 return -1;
1002
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001003 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001004 Py_XDECREF(result);
1005 return (result == NULL) ? -1 : 0;
1006}
1007
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001008static int
1009_Pickler_OpcodeBoundary(PicklerObject *self)
1010{
1011 Py_ssize_t frame_len;
1012
1013 if (!self->framing || self->frame_start == -1) {
1014 return 0;
1015 }
1016 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1017 if (frame_len >= FRAME_SIZE_TARGET) {
1018 if(_Pickler_CommitFrame(self)) {
1019 return -1;
1020 }
Miss Islington (bot)e86db342018-02-03 17:41:43 -08001021 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001022 * file and reuse the pickler buffer for the next frame so as
1023 * to limit memory usage when dumping large complex objects to
1024 * a file.
1025 *
1026 * self->write is NULL when called via dumps.
1027 */
1028 if (self->write != NULL) {
1029 if (_Pickler_FlushToFile(self) < 0) {
1030 return -1;
1031 }
1032 if (_Pickler_ClearBuffer(self) < 0) {
1033 return -1;
1034 }
1035 }
1036 }
1037 return 0;
1038}
1039
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001040static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001041_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001043 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001045 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046
1047 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001048 need_new_frame = (self->framing && self->frame_start == -1);
1049
1050 if (need_new_frame)
1051 n = data_len + FRAME_HEADER_SIZE;
1052 else
1053 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001054
1055 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001056 if (required > self->max_output_len) {
1057 /* Make place in buffer for the pickle chunk */
1058 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1059 PyErr_NoMemory();
1060 return -1;
1061 }
1062 self->max_output_len = (self->output_len + n) / 2 * 3;
1063 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1064 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001065 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001066 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001067 if (need_new_frame) {
1068 /* Setup new frame */
1069 Py_ssize_t frame_start = self->output_len;
1070 self->frame_start = frame_start;
1071 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1072 /* Write an invalid value, for debugging */
1073 buffer[frame_start + i] = 0xFE;
1074 }
1075 self->output_len += FRAME_HEADER_SIZE;
1076 }
1077 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001078 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001079 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001080 buffer[self->output_len + i] = s[i];
1081 }
1082 }
1083 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001084 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001085 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001086 self->output_len += data_len;
1087 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001088}
1089
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001090static PicklerObject *
1091_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001092{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001093 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001094
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001095 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1096 if (self == NULL)
1097 return NULL;
1098
1099 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001100 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001101 self->write = NULL;
1102 self->proto = 0;
1103 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001104 self->framing = 0;
1105 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001106 self->fast = 0;
1107 self->fast_nesting = 0;
1108 self->fix_imports = 0;
1109 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001110 self->max_output_len = WRITE_BUF_SIZE;
1111 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001112
1113 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001114 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1115 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001116
1117 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001118 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001119 return NULL;
1120 }
1121 return self;
1122}
1123
1124static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001125_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001126{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001127 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001129 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001131 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001132 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001133 proto = PyLong_AsLong(protocol);
1134 if (proto < 0) {
1135 if (proto == -1 && PyErr_Occurred())
1136 return -1;
1137 proto = HIGHEST_PROTOCOL;
1138 }
1139 else if (proto > HIGHEST_PROTOCOL) {
1140 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1141 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001142 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001143 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001145 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001146 self->bin = proto > 0;
1147 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001148 return 0;
1149}
1150
1151/* Returns -1 (with an exception set) on failure, 0 on success. This may
1152 be called once on a freshly created Pickler. */
1153static int
1154_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1155{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001156 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001157 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001158 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1159 return -1;
1160 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001161 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001162 PyErr_SetString(PyExc_TypeError,
1163 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001164 return -1;
1165 }
1166
1167 return 0;
1168}
1169
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001170/* Returns the size of the input on success, -1 on failure. This takes its
1171 own reference to `input`. */
1172static Py_ssize_t
1173_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1174{
1175 if (self->buffer.buf != NULL)
1176 PyBuffer_Release(&self->buffer);
1177 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1178 return -1;
1179 self->input_buffer = self->buffer.buf;
1180 self->input_len = self->buffer.len;
1181 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001182 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001183 return self->input_len;
1184}
1185
Antoine Pitrou04248a82010-10-12 20:51:21 +00001186static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001187bad_readline(void)
1188{
1189 PickleState *st = _Pickle_GetGlobalState();
1190 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1191 return -1;
1192}
1193
1194static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001195_Unpickler_SkipConsumed(UnpicklerObject *self)
1196{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001197 Py_ssize_t consumed;
1198 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001199
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001200 consumed = self->next_read_idx - self->prefetched_idx;
1201 if (consumed <= 0)
1202 return 0;
1203
1204 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001205 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001206 r = PyObject_CallFunction(self->read, "n", consumed);
1207 if (r == NULL)
1208 return -1;
1209 Py_DECREF(r);
1210
1211 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001212 return 0;
1213}
1214
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001215static const Py_ssize_t READ_WHOLE_LINE = -1;
1216
1217/* If reading from a file, we need to only pull the bytes we need, since there
1218 may be multiple pickle objects arranged contiguously in the same input
1219 buffer.
1220
1221 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1222 bytes from the input stream/buffer.
1223
1224 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1225 failure; on success, returns the number of bytes read from the file.
1226
1227 On success, self->input_len will be 0; this is intentional so that when
1228 unpickling from a file, the "we've run out of data" code paths will trigger,
1229 causing the Unpickler to go back to the file for more data. Use the returned
1230 size to tell you how much data you can process. */
1231static Py_ssize_t
1232_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1233{
1234 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001235 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001236
1237 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001238
Antoine Pitrou04248a82010-10-12 20:51:21 +00001239 if (_Unpickler_SkipConsumed(self) < 0)
1240 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001241
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001242 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001243 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001244 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001245 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001246 PyObject *len;
1247 /* Prefetch some data without advancing the file pointer, if possible */
1248 if (self->peek && n < PREFETCH) {
1249 len = PyLong_FromSsize_t(PREFETCH);
1250 if (len == NULL)
1251 return -1;
1252 data = _Pickle_FastCall(self->peek, len);
1253 if (data == NULL) {
1254 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1255 return -1;
1256 /* peek() is probably not supported by the given file object */
1257 PyErr_Clear();
1258 Py_CLEAR(self->peek);
1259 }
1260 else {
1261 read_size = _Unpickler_SetStringInput(self, data);
1262 Py_DECREF(data);
1263 self->prefetched_idx = 0;
1264 if (n <= read_size)
1265 return n;
1266 }
1267 }
1268 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001269 if (len == NULL)
1270 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001271 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001272 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001273 if (data == NULL)
1274 return -1;
1275
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001276 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001277 Py_DECREF(data);
1278 return read_size;
1279}
1280
Victor Stinner19ed27e2016-05-20 11:42:37 +02001281/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001282static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001283_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001284{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001285 Py_ssize_t num_read;
1286
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001287 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001288 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1289 PickleState *st = _Pickle_GetGlobalState();
1290 PyErr_SetString(st->UnpicklingError,
1291 "read would overflow (invalid bytecode)");
1292 return -1;
1293 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001294
1295 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1296 assert(self->next_read_idx + n > self->input_len);
1297
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001298 if (!self->read)
1299 return bad_readline();
1300
Antoine Pitrou04248a82010-10-12 20:51:21 +00001301 num_read = _Unpickler_ReadFromFile(self, n);
1302 if (num_read < 0)
1303 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001304 if (num_read < n)
1305 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001306 *s = self->input_buffer;
1307 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001308 return n;
1309}
1310
Victor Stinner19ed27e2016-05-20 11:42:37 +02001311/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1312
1313 This should be used for all data reads, rather than accessing the unpickler's
1314 input buffer directly. This method deals correctly with reading from input
1315 streams, which the input buffer doesn't deal with.
1316
1317 Note that when reading from a file-like object, self->next_read_idx won't
1318 be updated (it should remain at 0 for the entire unpickling process). You
1319 should use this function's return value to know how many bytes you can
1320 consume.
1321
1322 Returns -1 (with an exception set) on failure. On success, return the
1323 number of chars read. */
1324#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001325 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001326 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1327 (self)->next_read_idx += (n), \
1328 (n)) \
1329 : _Unpickler_ReadImpl(self, (s), (n)))
1330
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001331static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001332_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1333 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001334{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001335 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001336 if (input_line == NULL) {
1337 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001338 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001339 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001340
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001341 memcpy(input_line, line, len);
1342 input_line[len] = '\0';
1343 self->input_line = input_line;
1344 *result = self->input_line;
1345 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001346}
1347
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001348/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001349 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001350
1351 Returns the number of chars read, or -1 on failure. */
1352static Py_ssize_t
1353_Unpickler_Readline(UnpicklerObject *self, char **result)
1354{
1355 Py_ssize_t i, num_read;
1356
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001357 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001358 if (self->input_buffer[i] == '\n') {
1359 char *line_start = self->input_buffer + self->next_read_idx;
1360 num_read = i - self->next_read_idx + 1;
1361 self->next_read_idx = i + 1;
1362 return _Unpickler_CopyLine(self, line_start, num_read, result);
1363 }
1364 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001365 if (!self->read)
1366 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001367
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001368 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1369 if (num_read < 0)
1370 return -1;
1371 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1372 return bad_readline();
1373 self->next_read_idx = num_read;
1374 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001375}
1376
1377/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1378 will be modified in place. */
1379static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001380_Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001381{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001382 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001383
1384 assert(new_size > self->memo_size);
1385
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001386 PyObject **memo_new = self->memo;
1387 PyMem_RESIZE(memo_new, PyObject *, new_size);
1388 if (memo_new == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389 PyErr_NoMemory();
1390 return -1;
1391 }
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001392 self->memo = memo_new;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001393 for (i = self->memo_size; i < new_size; i++)
1394 self->memo[i] = NULL;
1395 self->memo_size = new_size;
1396 return 0;
1397}
1398
1399/* Returns NULL if idx is out of bounds. */
1400static PyObject *
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001401_Unpickler_MemoGet(UnpicklerObject *self, size_t idx)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001402{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001403 if (idx >= self->memo_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001404 return NULL;
1405
1406 return self->memo[idx];
1407}
1408
1409/* Returns -1 (with an exception set) on failure, 0 on success.
1410 This takes its own reference to `value`. */
1411static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001412_Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001413{
1414 PyObject *old_item;
1415
1416 if (idx >= self->memo_size) {
1417 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1418 return -1;
1419 assert(idx < self->memo_size);
1420 }
1421 Py_INCREF(value);
1422 old_item = self->memo[idx];
1423 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001424 if (old_item != NULL) {
1425 Py_DECREF(old_item);
1426 }
1427 else {
1428 self->memo_len++;
1429 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001430 return 0;
1431}
1432
1433static PyObject **
1434_Unpickler_NewMemo(Py_ssize_t new_size)
1435{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001436 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001437 if (memo == NULL) {
1438 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001439 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001440 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001441 memset(memo, 0, new_size * sizeof(PyObject *));
1442 return memo;
1443}
1444
1445/* Free the unpickler's memo, taking care to decref any items left in it. */
1446static void
1447_Unpickler_MemoCleanup(UnpicklerObject *self)
1448{
1449 Py_ssize_t i;
1450 PyObject **memo = self->memo;
1451
1452 if (self->memo == NULL)
1453 return;
1454 self->memo = NULL;
1455 i = self->memo_size;
1456 while (--i >= 0) {
1457 Py_XDECREF(memo[i]);
1458 }
1459 PyMem_FREE(memo);
1460}
1461
1462static UnpicklerObject *
1463_Unpickler_New(void)
1464{
1465 UnpicklerObject *self;
1466
1467 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1468 if (self == NULL)
1469 return NULL;
1470
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001471 self->pers_func = NULL;
1472 self->input_buffer = NULL;
1473 self->input_line = NULL;
1474 self->input_len = 0;
1475 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001476 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001477 self->read = NULL;
1478 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001479 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001480 self->encoding = NULL;
1481 self->errors = NULL;
1482 self->marks = NULL;
1483 self->num_marks = 0;
1484 self->marks_size = 0;
1485 self->proto = 0;
1486 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001487 memset(&self->buffer, 0, sizeof(Py_buffer));
1488 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001489 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001490 self->memo = _Unpickler_NewMemo(self->memo_size);
1491 self->stack = (Pdata *)Pdata_New();
1492
1493 if (self->memo == NULL || self->stack == NULL) {
1494 Py_DECREF(self);
1495 return NULL;
1496 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001497
1498 return self;
1499}
1500
1501/* Returns -1 (with an exception set) on failure, 0 on success. This may
1502 be called once on a freshly created Pickler. */
1503static int
1504_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1505{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001506 _Py_IDENTIFIER(peek);
1507 _Py_IDENTIFIER(read);
1508 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001509
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001510 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1511 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001512 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001513 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1514 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001515 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001516 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001517 PyErr_SetString(PyExc_TypeError,
1518 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001519 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001520 Py_CLEAR(self->read);
1521 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001522 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001523 return -1;
1524 }
1525 return 0;
1526}
1527
1528/* Returns -1 (with an exception set) on failure, 0 on success. This may
1529 be called once on a freshly created Pickler. */
1530static int
1531_Unpickler_SetInputEncoding(UnpicklerObject *self,
1532 const char *encoding,
1533 const char *errors)
1534{
1535 if (encoding == NULL)
1536 encoding = "ASCII";
1537 if (errors == NULL)
1538 errors = "strict";
1539
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001540 self->encoding = _PyMem_Strdup(encoding);
1541 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001542 if (self->encoding == NULL || self->errors == NULL) {
1543 PyErr_NoMemory();
1544 return -1;
1545 }
1546 return 0;
1547}
1548
1549/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001550static int
1551memo_get(PicklerObject *self, PyObject *key)
1552{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001553 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001554 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001555 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001556
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001557 value = PyMemoTable_Get(self->memo, key);
1558 if (value == NULL) {
1559 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001560 return -1;
1561 }
1562
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001563 if (!self->bin) {
1564 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001565 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1566 "%" PY_FORMAT_SIZE_T "d\n", *value);
1567 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001568 }
1569 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001570 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001571 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001572 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001573 len = 2;
1574 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001575 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001576 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001577 pdata[1] = (unsigned char)(*value & 0xff);
1578 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1579 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1580 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001581 len = 5;
1582 }
1583 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001584 PickleState *st = _Pickle_GetGlobalState();
1585 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001586 "memo id too large for LONG_BINGET");
1587 return -1;
1588 }
1589 }
1590
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001591 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001592 return -1;
1593
1594 return 0;
1595}
1596
1597/* Store an object in the memo, assign it a new unique ID based on the number
1598 of objects currently stored in the memo and generate a PUT opcode. */
1599static int
1600memo_put(PicklerObject *self, PyObject *obj)
1601{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001602 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001603 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001604 Py_ssize_t idx;
1605
1606 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001607
1608 if (self->fast)
1609 return 0;
1610
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 idx = PyMemoTable_Size(self->memo);
1612 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1613 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001614
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001615 if (self->proto >= 4) {
1616 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1617 return -1;
1618 return 0;
1619 }
1620 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001621 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001622 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001623 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001624 len = strlen(pdata);
1625 }
1626 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001627 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001628 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001629 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001630 len = 2;
1631 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001632 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001633 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001634 pdata[1] = (unsigned char)(idx & 0xff);
1635 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1636 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1637 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 len = 5;
1639 }
1640 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001641 PickleState *st = _Pickle_GetGlobalState();
1642 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001643 "memo id too large for LONG_BINPUT");
1644 return -1;
1645 }
1646 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001647 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001651}
1652
1653static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001654get_dotted_path(PyObject *obj, PyObject *name)
1655{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001656 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001657 PyObject *dotted_path;
1658 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001659
1660 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001661 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001662 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001663 n = PyList_GET_SIZE(dotted_path);
1664 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001665 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001666 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001667 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001668 if (obj == NULL)
1669 PyErr_Format(PyExc_AttributeError,
1670 "Can't pickle local object %R", name);
1671 else
1672 PyErr_Format(PyExc_AttributeError,
1673 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001674 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001675 return NULL;
1676 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001677 }
1678 return dotted_path;
1679}
1680
1681static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001682get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001683{
1684 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001685 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001686
1687 assert(PyList_CheckExact(names));
1688 Py_INCREF(obj);
1689 n = PyList_GET_SIZE(names);
1690 for (i = 0; i < n; i++) {
1691 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001692 Py_XDECREF(parent);
1693 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001694 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001695 if (obj == NULL) {
1696 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001697 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001698 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001699 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001700 if (pparent != NULL)
1701 *pparent = parent;
1702 else
1703 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001704 return obj;
1705}
1706
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001707
1708static PyObject *
1709getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1710{
1711 PyObject *dotted_path, *attr;
1712
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001713 if (allow_qualname) {
1714 dotted_path = get_dotted_path(obj, name);
1715 if (dotted_path == NULL)
1716 return NULL;
1717 attr = get_deep_attribute(obj, dotted_path, NULL);
1718 Py_DECREF(dotted_path);
1719 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001720 else {
1721 (void)_PyObject_LookupAttr(obj, name, &attr);
1722 }
1723 if (attr == NULL && !PyErr_Occurred()) {
1724 PyErr_Format(PyExc_AttributeError,
1725 "Can't get attribute %R on %R", name, obj);
1726 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001727 return attr;
1728}
1729
Eric Snow3f9eee62017-09-15 16:35:20 -06001730static int
1731_checkmodule(PyObject *module_name, PyObject *module,
1732 PyObject *global, PyObject *dotted_path)
1733{
1734 if (module == Py_None) {
1735 return -1;
1736 }
1737 if (PyUnicode_Check(module_name) &&
1738 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1739 return -1;
1740 }
1741
1742 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1743 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001744 return -1;
1745 }
1746 if (candidate != global) {
1747 Py_DECREF(candidate);
1748 return -1;
1749 }
1750 Py_DECREF(candidate);
1751 return 0;
1752}
1753
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001754static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001755whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001756{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001757 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001758 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001759 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001760 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001761 _Py_IDENTIFIER(__module__);
1762 _Py_IDENTIFIER(modules);
1763 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001764
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001765 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1766 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001767 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001768 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001769 /* In some rare cases (e.g., bound methods of extension types),
1770 __module__ can be None. If it is so, then search sys.modules for
1771 the module of global. */
1772 if (module_name != Py_None)
1773 return module_name;
1774 Py_CLEAR(module_name);
1775 }
1776 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001777
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001778 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001779 modules = _PySys_GetObjectId(&PyId_modules);
1780 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001781 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001782 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001783 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001784 if (PyDict_CheckExact(modules)) {
1785 i = 0;
1786 while (PyDict_Next(modules, &i, &module_name, &module)) {
1787 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1788 Py_INCREF(module_name);
1789 return module_name;
1790 }
1791 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001792 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001793 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001794 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001795 }
1796 else {
1797 PyObject *iterator = PyObject_GetIter(modules);
1798 if (iterator == NULL) {
1799 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001800 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001801 while ((module_name = PyIter_Next(iterator))) {
1802 module = PyObject_GetItem(modules, module_name);
1803 if (module == NULL) {
1804 Py_DECREF(module_name);
1805 Py_DECREF(iterator);
1806 return NULL;
1807 }
1808 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1809 Py_DECREF(module);
1810 Py_DECREF(iterator);
1811 return module_name;
1812 }
1813 Py_DECREF(module);
1814 Py_DECREF(module_name);
1815 if (PyErr_Occurred()) {
1816 Py_DECREF(iterator);
1817 return NULL;
1818 }
1819 }
1820 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001821 }
1822
1823 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001824 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001825 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001826 return module_name;
1827}
1828
1829/* fast_save_enter() and fast_save_leave() are guards against recursive
1830 objects when Pickler is used with the "fast mode" (i.e., with object
1831 memoization disabled). If the nesting of a list or dict object exceed
1832 FAST_NESTING_LIMIT, these guards will start keeping an internal
1833 reference to the seen list or dict objects and check whether these objects
1834 are recursive. These are not strictly necessary, since save() has a
1835 hard-coded recursion limit, but they give a nicer error message than the
1836 typical RuntimeError. */
1837static int
1838fast_save_enter(PicklerObject *self, PyObject *obj)
1839{
1840 /* if fast_nesting < 0, we're doing an error exit. */
1841 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1842 PyObject *key = NULL;
1843 if (self->fast_memo == NULL) {
1844 self->fast_memo = PyDict_New();
1845 if (self->fast_memo == NULL) {
1846 self->fast_nesting = -1;
1847 return 0;
1848 }
1849 }
1850 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001851 if (key == NULL) {
1852 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001853 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001854 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001855 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001856 Py_DECREF(key);
1857 PyErr_Format(PyExc_ValueError,
1858 "fast mode: can't pickle cyclic objects "
1859 "including object type %.200s at %p",
1860 obj->ob_type->tp_name, obj);
1861 self->fast_nesting = -1;
1862 return 0;
1863 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001864 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001865 Py_DECREF(key);
1866 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001867 return 0;
1868 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001869 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1870 Py_DECREF(key);
1871 self->fast_nesting = -1;
1872 return 0;
1873 }
1874 Py_DECREF(key);
1875 }
1876 return 1;
1877}
1878
1879static int
1880fast_save_leave(PicklerObject *self, PyObject *obj)
1881{
1882 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1883 PyObject *key = PyLong_FromVoidPtr(obj);
1884 if (key == NULL)
1885 return 0;
1886 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1887 Py_DECREF(key);
1888 return 0;
1889 }
1890 Py_DECREF(key);
1891 }
1892 return 1;
1893}
1894
1895static int
1896save_none(PicklerObject *self, PyObject *obj)
1897{
1898 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001899 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001900 return -1;
1901
1902 return 0;
1903}
1904
1905static int
1906save_bool(PicklerObject *self, PyObject *obj)
1907{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001908 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001909 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001910 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001911 return -1;
1912 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001913 else {
1914 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1915 * so that unpicklers written before bools were introduced unpickle them
1916 * as ints, but unpicklers after can recognize that bools were intended.
1917 * Note that protocol 2 added direct ways to pickle bools.
1918 */
1919 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1920 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1921 return -1;
1922 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001923 return 0;
1924}
1925
1926static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001927save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001928{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001929 PyObject *repr = NULL;
1930 Py_ssize_t size;
1931 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001932 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001933 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001934
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001935 val= PyLong_AsLongAndOverflow(obj, &overflow);
1936 if (!overflow && (sizeof(long) <= 4 ||
1937 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1938 {
Larry Hastings61272b72014-01-07 12:41:53 -08001939 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001940
1941 Note: we can't use -0x80000000L in the above condition because some
1942 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1943 before applying the unary minus when sizeof(long) <= 4. The
1944 resulting value stays unsigned which is commonly not what we want,
1945 so MSVC happily warns us about it. However, that result would have
1946 been fine because we guard for sizeof(long) <= 4 which turns the
1947 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001948 char pdata[32];
1949 Py_ssize_t len = 0;
1950
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001951 if (self->bin) {
1952 pdata[1] = (unsigned char)(val & 0xff);
1953 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1954 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1955 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001956
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001957 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1958 pdata[0] = BININT;
1959 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001960 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001961 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001962 pdata[0] = BININT2;
1963 len = 3;
1964 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001965 else {
1966 pdata[0] = BININT1;
1967 len = 2;
1968 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001969 }
1970 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001971 sprintf(pdata, "%c%ld\n", INT, val);
1972 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001973 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001974 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001975 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001976
1977 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001978 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001979 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001980
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001981 if (self->proto >= 2) {
1982 /* Linear-time pickling. */
1983 size_t nbits;
1984 size_t nbytes;
1985 unsigned char *pdata;
1986 char header[5];
1987 int i;
1988 int sign = _PyLong_Sign(obj);
1989
1990 if (sign == 0) {
1991 header[0] = LONG1;
1992 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001993 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001994 goto error;
1995 return 0;
1996 }
1997 nbits = _PyLong_NumBits(obj);
1998 if (nbits == (size_t)-1 && PyErr_Occurred())
1999 goto error;
2000 /* How many bytes do we need? There are nbits >> 3 full
2001 * bytes of data, and nbits & 7 leftover bits. If there
2002 * are any leftover bits, then we clearly need another
2003 * byte. Wnat's not so obvious is that we *probably*
2004 * need another byte even if there aren't any leftovers:
2005 * the most-significant bit of the most-significant byte
2006 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002007 * opposite of the one we need. The exception is ints
2008 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002009 * its own 256's-complement, so has the right sign bit
2010 * even without the extra byte. That's a pain to check
2011 * for in advance, though, so we always grab an extra
2012 * byte at the start, and cut it back later if possible.
2013 */
2014 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002015 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002016 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002017 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002018 goto error;
2019 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002020 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002021 if (repr == NULL)
2022 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002023 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002024 i = _PyLong_AsByteArray((PyLongObject *)obj,
2025 pdata, nbytes,
2026 1 /* little endian */ , 1 /* signed */ );
2027 if (i < 0)
2028 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002029 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002030 * needed. This is so iff the MSB is all redundant sign
2031 * bits.
2032 */
2033 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002034 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002035 pdata[nbytes - 1] == 0xff &&
2036 (pdata[nbytes - 2] & 0x80) != 0) {
2037 nbytes--;
2038 }
2039
2040 if (nbytes < 256) {
2041 header[0] = LONG1;
2042 header[1] = (unsigned char)nbytes;
2043 size = 2;
2044 }
2045 else {
2046 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002047 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002048 for (i = 1; i < 5; i++) {
2049 header[i] = (unsigned char)(size & 0xff);
2050 size >>= 8;
2051 }
2052 size = 5;
2053 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002054 if (_Pickler_Write(self, header, size) < 0 ||
2055 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002056 goto error;
2057 }
2058 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002059 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002060 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061
Mark Dickinson8dd05142009-01-20 20:43:58 +00002062 /* proto < 2: write the repr and newline. This is quadratic-time (in
2063 the number of digits), in both directions. We add a trailing 'L'
2064 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002065
2066 repr = PyObject_Repr(obj);
2067 if (repr == NULL)
2068 goto error;
2069
Serhiy Storchaka06515832016-11-20 09:13:07 +02002070 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002071 if (string == NULL)
2072 goto error;
2073
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002074 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2075 _Pickler_Write(self, string, size) < 0 ||
2076 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002077 goto error;
2078 }
2079
2080 if (0) {
2081 error:
2082 status = -1;
2083 }
2084 Py_XDECREF(repr);
2085
2086 return status;
2087}
2088
2089static int
2090save_float(PicklerObject *self, PyObject *obj)
2091{
2092 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2093
2094 if (self->bin) {
2095 char pdata[9];
2096 pdata[0] = BINFLOAT;
2097 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2098 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002099 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002100 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002101 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002102 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002103 int result = -1;
2104 char *buf = NULL;
2105 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002106
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002107 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002108 goto done;
2109
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002110 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002111 if (!buf) {
2112 PyErr_NoMemory();
2113 goto done;
2114 }
2115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002116 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002117 goto done;
2118
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002119 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002120 goto done;
2121
2122 result = 0;
2123done:
2124 PyMem_Free(buf);
2125 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002126 }
2127
2128 return 0;
2129}
2130
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002131/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002132
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002133 The large contiguous data is written directly into the underlying file
2134 object, bypassing the output_buffer of the Pickler. We intentionally
2135 do not insert a protocol 4 frame opcode to make it possible to optimize
2136 file.read calls in the loader.
2137 */
2138static int
2139_Pickler_write_bytes(PicklerObject *self,
2140 const char *header, Py_ssize_t header_size,
2141 const char *data, Py_ssize_t data_size,
2142 PyObject *payload)
2143{
2144 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2145 int framing = self->framing;
2146
2147 if (bypass_buffer) {
2148 assert(self->output_buffer != NULL);
2149 /* Commit the previous frame. */
2150 if (_Pickler_CommitFrame(self)) {
2151 return -1;
2152 }
2153 /* Disable framing temporarily */
2154 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002155 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002156
2157 if (_Pickler_Write(self, header, header_size) < 0) {
2158 return -1;
2159 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002160
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002161 if (bypass_buffer && self->write != NULL) {
2162 /* Bypass the in-memory buffer to directly stream large data
2163 into the underlying file object. */
2164 PyObject *result, *mem = NULL;
2165 /* Dump the output buffer to the file. */
2166 if (_Pickler_FlushToFile(self) < 0) {
2167 return -1;
2168 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002169
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002170 /* Stream write the payload into the file without going through the
2171 output buffer. */
2172 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002173 /* TODO: It would be better to use a memoryview with a linked
2174 original string if this is possible. */
2175 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002176 if (payload == NULL) {
2177 return -1;
2178 }
2179 }
2180 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2181 Py_XDECREF(mem);
2182 if (result == NULL) {
2183 return -1;
2184 }
2185 Py_DECREF(result);
2186
2187 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2188 if (_Pickler_ClearBuffer(self) < 0) {
2189 return -1;
2190 }
2191 }
2192 else {
2193 if (_Pickler_Write(self, data, data_size) < 0) {
2194 return -1;
2195 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002196 }
2197
2198 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002199 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002200
2201 return 0;
2202}
2203
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002204static int
2205save_bytes(PicklerObject *self, PyObject *obj)
2206{
2207 if (self->proto < 3) {
2208 /* Older pickle protocols do not have an opcode for pickling bytes
2209 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002210 the __reduce__ method) to permit bytes object unpickling.
2211
2212 Here we use a hack to be compatible with Python 2. Since in Python
2213 2 'bytes' is just an alias for 'str' (which has different
2214 parameters than the actual bytes object), we use codecs.encode
2215 to create the appropriate 'str' object when unpickled using
2216 Python 2 *and* the appropriate 'bytes' object when unpickled
2217 using Python 3. Again this is a hack and we don't need to do this
2218 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002219 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002220 int status;
2221
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002222 if (PyBytes_GET_SIZE(obj) == 0) {
2223 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2224 }
2225 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002226 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002227 PyObject *unicode_str =
2228 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2229 PyBytes_GET_SIZE(obj),
2230 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002231 _Py_IDENTIFIER(latin1);
2232
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002233 if (unicode_str == NULL)
2234 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002235 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002236 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002237 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002238 Py_DECREF(unicode_str);
2239 }
2240
2241 if (reduce_value == NULL)
2242 return -1;
2243
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002244 /* save_reduce() will memoize the object automatically. */
2245 status = save_reduce(self, reduce_value, obj);
2246 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002247 return status;
2248 }
2249 else {
2250 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002251 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002252 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002253
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002254 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255 if (size < 0)
2256 return -1;
2257
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002258 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259 header[0] = SHORT_BINBYTES;
2260 header[1] = (unsigned char)size;
2261 len = 2;
2262 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002263 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002264 header[0] = BINBYTES;
2265 header[1] = (unsigned char)(size & 0xff);
2266 header[2] = (unsigned char)((size >> 8) & 0xff);
2267 header[3] = (unsigned char)((size >> 16) & 0xff);
2268 header[4] = (unsigned char)((size >> 24) & 0xff);
2269 len = 5;
2270 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002271 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002272 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002273 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002274 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002275 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002277 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002278 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279 return -1; /* string too large */
2280 }
2281
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002282 if (_Pickler_write_bytes(self, header, len,
2283 PyBytes_AS_STRING(obj), size, obj) < 0)
2284 {
2285 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002286 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287
2288 if (memo_put(self, obj) < 0)
2289 return -1;
2290
2291 return 0;
2292 }
2293}
2294
2295/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2296 backslash and newline characters to \uXXXX escapes. */
2297static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002298raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002299{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002300 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002301 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002302 void *data;
2303 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002304 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002305
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002306 if (PyUnicode_READY(obj))
2307 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002308
Victor Stinner358af132015-10-12 22:36:57 +02002309 _PyBytesWriter_Init(&writer);
2310
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002311 size = PyUnicode_GET_LENGTH(obj);
2312 data = PyUnicode_DATA(obj);
2313 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002314
Victor Stinner358af132015-10-12 22:36:57 +02002315 p = _PyBytesWriter_Alloc(&writer, size);
2316 if (p == NULL)
2317 goto error;
2318 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002319
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002320 for (i=0; i < size; i++) {
2321 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002322 /* Map 32-bit characters to '\Uxxxxxxxx' */
2323 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002324 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002325 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2326 if (p == NULL)
2327 goto error;
2328
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002329 *p++ = '\\';
2330 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002331 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2332 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2333 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2334 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2335 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2336 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2337 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2338 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002339 }
Victor Stinner358af132015-10-12 22:36:57 +02002340 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002341 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002342 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002343 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2344 if (p == NULL)
2345 goto error;
2346
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002347 *p++ = '\\';
2348 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002349 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2350 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2351 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2352 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002353 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002354 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002355 else
2356 *p++ = (char) ch;
2357 }
Victor Stinner358af132015-10-12 22:36:57 +02002358
2359 return _PyBytesWriter_Finish(&writer, p);
2360
2361error:
2362 _PyBytesWriter_Dealloc(&writer);
2363 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002364}
2365
2366static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002367write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002368{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002369 char header[9];
2370 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002371 PyObject *encoded = NULL;
2372 Py_ssize_t size;
2373 const char *data;
2374
2375 if (PyUnicode_READY(obj))
2376 return -1;
2377
2378 data = PyUnicode_AsUTF8AndSize(obj, &size);
2379 if (data == NULL) {
2380 /* Issue #8383: for strings with lone surrogates, fallback on the
2381 "surrogatepass" error handler. */
2382 PyErr_Clear();
2383 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2384 if (encoded == NULL)
2385 return -1;
2386
2387 data = PyBytes_AS_STRING(encoded);
2388 size = PyBytes_GET_SIZE(encoded);
2389 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002390
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002391 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002392 if (size <= 0xff && self->proto >= 4) {
2393 header[0] = SHORT_BINUNICODE;
2394 header[1] = (unsigned char)(size & 0xff);
2395 len = 2;
2396 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002397 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002398 header[0] = BINUNICODE;
2399 header[1] = (unsigned char)(size & 0xff);
2400 header[2] = (unsigned char)((size >> 8) & 0xff);
2401 header[3] = (unsigned char)((size >> 16) & 0xff);
2402 header[4] = (unsigned char)((size >> 24) & 0xff);
2403 len = 5;
2404 }
2405 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002406 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002407 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002408 len = 9;
2409 }
2410 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002411 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002412 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002413 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002414 return -1;
2415 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002416
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002417 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2418 Py_XDECREF(encoded);
2419 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002420 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002421 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002422 return 0;
2423}
2424
2425static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002426save_unicode(PicklerObject *self, PyObject *obj)
2427{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002428 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002429 if (write_unicode_binary(self, obj) < 0)
2430 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002431 }
2432 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002433 PyObject *encoded;
2434 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002435 const char unicode_op = UNICODE;
2436
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002437 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002438 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002439 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002440
Antoine Pitrou299978d2013-04-07 17:38:11 +02002441 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2442 Py_DECREF(encoded);
2443 return -1;
2444 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002445
2446 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002447 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2448 Py_DECREF(encoded);
2449 return -1;
2450 }
2451 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002452
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002453 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002454 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455 }
2456 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002457 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002458
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002459 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002460}
2461
2462/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2463static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002464store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002465{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002466 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002467
2468 assert(PyTuple_Size(t) == len);
2469
2470 for (i = 0; i < len; i++) {
2471 PyObject *element = PyTuple_GET_ITEM(t, i);
2472
2473 if (element == NULL)
2474 return -1;
2475 if (save(self, element, 0) < 0)
2476 return -1;
2477 }
2478
2479 return 0;
2480}
2481
2482/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2483 * used across protocols to minimize the space needed to pickle them.
2484 * Tuples are also the only builtin immutable type that can be recursive
2485 * (a tuple can be reached from itself), and that requires some subtle
2486 * magic so that it works in all cases. IOW, this is a long routine.
2487 */
2488static int
2489save_tuple(PicklerObject *self, PyObject *obj)
2490{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002491 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002492
2493 const char mark_op = MARK;
2494 const char tuple_op = TUPLE;
2495 const char pop_op = POP;
2496 const char pop_mark_op = POP_MARK;
2497 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2498
2499 if ((len = PyTuple_Size(obj)) < 0)
2500 return -1;
2501
2502 if (len == 0) {
2503 char pdata[2];
2504
2505 if (self->proto) {
2506 pdata[0] = EMPTY_TUPLE;
2507 len = 1;
2508 }
2509 else {
2510 pdata[0] = MARK;
2511 pdata[1] = TUPLE;
2512 len = 2;
2513 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002514 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002515 return -1;
2516 return 0;
2517 }
2518
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002519 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002520 * saving the tuple elements, the tuple must be recursive, in
2521 * which case we'll pop everything we put on the stack, and fetch
2522 * its value from the memo.
2523 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002524 if (len <= 3 && self->proto >= 2) {
2525 /* Use TUPLE{1,2,3} opcodes. */
2526 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002527 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002528
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002529 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002530 /* pop the len elements */
2531 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002532 if (_Pickler_Write(self, &pop_op, 1) < 0)
2533 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002534 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002535 if (memo_get(self, obj) < 0)
2536 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002537
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002538 return 0;
2539 }
2540 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002541 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2542 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002543 }
2544 goto memoize;
2545 }
2546
2547 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2548 * Generate MARK e1 e2 ... TUPLE
2549 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002550 if (_Pickler_Write(self, &mark_op, 1) < 0)
2551 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002552
2553 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002554 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002555
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002556 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002557 /* pop the stack stuff we pushed */
2558 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002559 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2560 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002561 }
2562 else {
2563 /* Note that we pop one more than len, to remove
2564 * the MARK too.
2565 */
2566 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002567 if (_Pickler_Write(self, &pop_op, 1) < 0)
2568 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002569 }
2570 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002571 if (memo_get(self, obj) < 0)
2572 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002573
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002574 return 0;
2575 }
2576 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002577 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2578 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002579 }
2580
2581 memoize:
2582 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002583 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002584
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002585 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002586}
2587
2588/* iter is an iterator giving items, and we batch up chunks of
2589 * MARK item item ... item APPENDS
2590 * opcode sequences. Calling code should have arranged to first create an
2591 * empty list, or list-like object, for the APPENDS to operate on.
2592 * Returns 0 on success, <0 on error.
2593 */
2594static int
2595batch_list(PicklerObject *self, PyObject *iter)
2596{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002597 PyObject *obj = NULL;
2598 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002599 int i, n;
2600
2601 const char mark_op = MARK;
2602 const char append_op = APPEND;
2603 const char appends_op = APPENDS;
2604
2605 assert(iter != NULL);
2606
2607 /* XXX: I think this function could be made faster by avoiding the
2608 iterator interface and fetching objects directly from list using
2609 PyList_GET_ITEM.
2610 */
2611
2612 if (self->proto == 0) {
2613 /* APPENDS isn't available; do one at a time. */
2614 for (;;) {
2615 obj = PyIter_Next(iter);
2616 if (obj == NULL) {
2617 if (PyErr_Occurred())
2618 return -1;
2619 break;
2620 }
2621 i = save(self, obj, 0);
2622 Py_DECREF(obj);
2623 if (i < 0)
2624 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002625 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002626 return -1;
2627 }
2628 return 0;
2629 }
2630
2631 /* proto > 0: write in batches of BATCHSIZE. */
2632 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002633 /* Get first item */
2634 firstitem = PyIter_Next(iter);
2635 if (firstitem == NULL) {
2636 if (PyErr_Occurred())
2637 goto error;
2638
2639 /* nothing more to add */
2640 break;
2641 }
2642
2643 /* Try to get a second item */
2644 obj = PyIter_Next(iter);
2645 if (obj == NULL) {
2646 if (PyErr_Occurred())
2647 goto error;
2648
2649 /* Only one item to write */
2650 if (save(self, firstitem, 0) < 0)
2651 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002652 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002653 goto error;
2654 Py_CLEAR(firstitem);
2655 break;
2656 }
2657
2658 /* More than one item to write */
2659
2660 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002661 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002662 goto error;
2663
2664 if (save(self, firstitem, 0) < 0)
2665 goto error;
2666 Py_CLEAR(firstitem);
2667 n = 1;
2668
2669 /* Fetch and save up to BATCHSIZE items */
2670 while (obj) {
2671 if (save(self, obj, 0) < 0)
2672 goto error;
2673 Py_CLEAR(obj);
2674 n += 1;
2675
2676 if (n == BATCHSIZE)
2677 break;
2678
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002679 obj = PyIter_Next(iter);
2680 if (obj == NULL) {
2681 if (PyErr_Occurred())
2682 goto error;
2683 break;
2684 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002685 }
2686
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002687 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002688 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002689
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002690 } while (n == BATCHSIZE);
2691 return 0;
2692
2693 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002694 Py_XDECREF(firstitem);
2695 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002696 return -1;
2697}
2698
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002699/* This is a variant of batch_list() above, specialized for lists (with no
2700 * support for list subclasses). Like batch_list(), we batch up chunks of
2701 * MARK item item ... item APPENDS
2702 * opcode sequences. Calling code should have arranged to first create an
2703 * empty list, or list-like object, for the APPENDS to operate on.
2704 * Returns 0 on success, -1 on error.
2705 *
2706 * This version is considerably faster than batch_list(), if less general.
2707 *
2708 * Note that this only works for protocols > 0.
2709 */
2710static int
2711batch_list_exact(PicklerObject *self, PyObject *obj)
2712{
2713 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002714 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002715
2716 const char append_op = APPEND;
2717 const char appends_op = APPENDS;
2718 const char mark_op = MARK;
2719
2720 assert(obj != NULL);
2721 assert(self->proto > 0);
2722 assert(PyList_CheckExact(obj));
2723
2724 if (PyList_GET_SIZE(obj) == 1) {
2725 item = PyList_GET_ITEM(obj, 0);
2726 if (save(self, item, 0) < 0)
2727 return -1;
2728 if (_Pickler_Write(self, &append_op, 1) < 0)
2729 return -1;
2730 return 0;
2731 }
2732
2733 /* Write in batches of BATCHSIZE. */
2734 total = 0;
2735 do {
2736 this_batch = 0;
2737 if (_Pickler_Write(self, &mark_op, 1) < 0)
2738 return -1;
2739 while (total < PyList_GET_SIZE(obj)) {
2740 item = PyList_GET_ITEM(obj, total);
2741 if (save(self, item, 0) < 0)
2742 return -1;
2743 total++;
2744 if (++this_batch == BATCHSIZE)
2745 break;
2746 }
2747 if (_Pickler_Write(self, &appends_op, 1) < 0)
2748 return -1;
2749
2750 } while (total < PyList_GET_SIZE(obj));
2751
2752 return 0;
2753}
2754
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002755static int
2756save_list(PicklerObject *self, PyObject *obj)
2757{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002758 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002759 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002760 int status = 0;
2761
2762 if (self->fast && !fast_save_enter(self, obj))
2763 goto error;
2764
2765 /* Create an empty list. */
2766 if (self->bin) {
2767 header[0] = EMPTY_LIST;
2768 len = 1;
2769 }
2770 else {
2771 header[0] = MARK;
2772 header[1] = LIST;
2773 len = 2;
2774 }
2775
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002776 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002777 goto error;
2778
2779 /* Get list length, and bow out early if empty. */
2780 if ((len = PyList_Size(obj)) < 0)
2781 goto error;
2782
2783 if (memo_put(self, obj) < 0)
2784 goto error;
2785
2786 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002787 /* Materialize the list elements. */
2788 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002789 if (Py_EnterRecursiveCall(" while pickling an object"))
2790 goto error;
2791 status = batch_list_exact(self, obj);
2792 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002793 } else {
2794 PyObject *iter = PyObject_GetIter(obj);
2795 if (iter == NULL)
2796 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002797
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002798 if (Py_EnterRecursiveCall(" while pickling an object")) {
2799 Py_DECREF(iter);
2800 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002801 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002802 status = batch_list(self, iter);
2803 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002804 Py_DECREF(iter);
2805 }
2806 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002807 if (0) {
2808 error:
2809 status = -1;
2810 }
2811
2812 if (self->fast && !fast_save_leave(self, obj))
2813 status = -1;
2814
2815 return status;
2816}
2817
2818/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2819 * MARK key value ... key value SETITEMS
2820 * opcode sequences. Calling code should have arranged to first create an
2821 * empty dict, or dict-like object, for the SETITEMS to operate on.
2822 * Returns 0 on success, <0 on error.
2823 *
2824 * This is very much like batch_list(). The difference between saving
2825 * elements directly, and picking apart two-tuples, is so long-winded at
2826 * the C level, though, that attempts to combine these routines were too
2827 * ugly to bear.
2828 */
2829static int
2830batch_dict(PicklerObject *self, PyObject *iter)
2831{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002832 PyObject *obj = NULL;
2833 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002834 int i, n;
2835
2836 const char mark_op = MARK;
2837 const char setitem_op = SETITEM;
2838 const char setitems_op = SETITEMS;
2839
2840 assert(iter != NULL);
2841
2842 if (self->proto == 0) {
2843 /* SETITEMS isn't available; do one at a time. */
2844 for (;;) {
2845 obj = PyIter_Next(iter);
2846 if (obj == NULL) {
2847 if (PyErr_Occurred())
2848 return -1;
2849 break;
2850 }
2851 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2852 PyErr_SetString(PyExc_TypeError, "dict items "
2853 "iterator must return 2-tuples");
2854 return -1;
2855 }
2856 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2857 if (i >= 0)
2858 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2859 Py_DECREF(obj);
2860 if (i < 0)
2861 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002862 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002863 return -1;
2864 }
2865 return 0;
2866 }
2867
2868 /* proto > 0: write in batches of BATCHSIZE. */
2869 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002870 /* Get first item */
2871 firstitem = PyIter_Next(iter);
2872 if (firstitem == NULL) {
2873 if (PyErr_Occurred())
2874 goto error;
2875
2876 /* nothing more to add */
2877 break;
2878 }
2879 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2880 PyErr_SetString(PyExc_TypeError, "dict items "
2881 "iterator must return 2-tuples");
2882 goto error;
2883 }
2884
2885 /* Try to get a second item */
2886 obj = PyIter_Next(iter);
2887 if (obj == NULL) {
2888 if (PyErr_Occurred())
2889 goto error;
2890
2891 /* Only one item to write */
2892 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2893 goto error;
2894 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2895 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002896 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002897 goto error;
2898 Py_CLEAR(firstitem);
2899 break;
2900 }
2901
2902 /* More than one item to write */
2903
2904 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002905 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002906 goto error;
2907
2908 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2909 goto error;
2910 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2911 goto error;
2912 Py_CLEAR(firstitem);
2913 n = 1;
2914
2915 /* Fetch and save up to BATCHSIZE items */
2916 while (obj) {
2917 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2918 PyErr_SetString(PyExc_TypeError, "dict items "
2919 "iterator must return 2-tuples");
2920 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002921 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002922 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2923 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2924 goto error;
2925 Py_CLEAR(obj);
2926 n += 1;
2927
2928 if (n == BATCHSIZE)
2929 break;
2930
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002931 obj = PyIter_Next(iter);
2932 if (obj == NULL) {
2933 if (PyErr_Occurred())
2934 goto error;
2935 break;
2936 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002937 }
2938
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002939 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002940 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002941
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002942 } while (n == BATCHSIZE);
2943 return 0;
2944
2945 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002946 Py_XDECREF(firstitem);
2947 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002948 return -1;
2949}
2950
Collin Winter5c9b02d2009-05-25 05:43:30 +00002951/* This is a variant of batch_dict() above that specializes for dicts, with no
2952 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2953 * MARK key value ... key value SETITEMS
2954 * opcode sequences. Calling code should have arranged to first create an
2955 * empty dict, or dict-like object, for the SETITEMS to operate on.
2956 * Returns 0 on success, -1 on error.
2957 *
2958 * Note that this currently doesn't work for protocol 0.
2959 */
2960static int
2961batch_dict_exact(PicklerObject *self, PyObject *obj)
2962{
2963 PyObject *key = NULL, *value = NULL;
2964 int i;
2965 Py_ssize_t dict_size, ppos = 0;
2966
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002967 const char mark_op = MARK;
2968 const char setitem_op = SETITEM;
2969 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002970
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002971 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002972 assert(self->proto > 0);
2973
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002974 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002975
2976 /* Special-case len(d) == 1 to save space. */
2977 if (dict_size == 1) {
2978 PyDict_Next(obj, &ppos, &key, &value);
2979 if (save(self, key, 0) < 0)
2980 return -1;
2981 if (save(self, value, 0) < 0)
2982 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002983 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002984 return -1;
2985 return 0;
2986 }
2987
2988 /* Write in batches of BATCHSIZE. */
2989 do {
2990 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002991 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002992 return -1;
2993 while (PyDict_Next(obj, &ppos, &key, &value)) {
2994 if (save(self, key, 0) < 0)
2995 return -1;
2996 if (save(self, value, 0) < 0)
2997 return -1;
2998 if (++i == BATCHSIZE)
2999 break;
3000 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003001 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003002 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003003 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003004 PyErr_Format(
3005 PyExc_RuntimeError,
3006 "dictionary changed size during iteration");
3007 return -1;
3008 }
3009
3010 } while (i == BATCHSIZE);
3011 return 0;
3012}
3013
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003014static int
3015save_dict(PicklerObject *self, PyObject *obj)
3016{
3017 PyObject *items, *iter;
3018 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003019 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003020 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003021 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003022
3023 if (self->fast && !fast_save_enter(self, obj))
3024 goto error;
3025
3026 /* Create an empty dict. */
3027 if (self->bin) {
3028 header[0] = EMPTY_DICT;
3029 len = 1;
3030 }
3031 else {
3032 header[0] = MARK;
3033 header[1] = DICT;
3034 len = 2;
3035 }
3036
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003037 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003038 goto error;
3039
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003040 if (memo_put(self, obj) < 0)
3041 goto error;
3042
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003043 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003044 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003045 if (PyDict_CheckExact(obj) && self->proto > 0) {
3046 /* We can take certain shortcuts if we know this is a dict and
3047 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003048 if (Py_EnterRecursiveCall(" while pickling an object"))
3049 goto error;
3050 status = batch_dict_exact(self, obj);
3051 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003052 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003053 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003054
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003055 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003056 if (items == NULL)
3057 goto error;
3058 iter = PyObject_GetIter(items);
3059 Py_DECREF(items);
3060 if (iter == NULL)
3061 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003062 if (Py_EnterRecursiveCall(" while pickling an object")) {
3063 Py_DECREF(iter);
3064 goto error;
3065 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003066 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003067 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003068 Py_DECREF(iter);
3069 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003070 }
3071
3072 if (0) {
3073 error:
3074 status = -1;
3075 }
3076
3077 if (self->fast && !fast_save_leave(self, obj))
3078 status = -1;
3079
3080 return status;
3081}
3082
3083static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003084save_set(PicklerObject *self, PyObject *obj)
3085{
3086 PyObject *item;
3087 int i;
3088 Py_ssize_t set_size, ppos = 0;
3089 Py_hash_t hash;
3090
3091 const char empty_set_op = EMPTY_SET;
3092 const char mark_op = MARK;
3093 const char additems_op = ADDITEMS;
3094
3095 if (self->proto < 4) {
3096 PyObject *items;
3097 PyObject *reduce_value;
3098 int status;
3099
3100 items = PySequence_List(obj);
3101 if (items == NULL) {
3102 return -1;
3103 }
3104 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3105 Py_DECREF(items);
3106 if (reduce_value == NULL) {
3107 return -1;
3108 }
3109 /* save_reduce() will memoize the object automatically. */
3110 status = save_reduce(self, reduce_value, obj);
3111 Py_DECREF(reduce_value);
3112 return status;
3113 }
3114
3115 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3116 return -1;
3117
3118 if (memo_put(self, obj) < 0)
3119 return -1;
3120
3121 set_size = PySet_GET_SIZE(obj);
3122 if (set_size == 0)
3123 return 0; /* nothing to do */
3124
3125 /* Write in batches of BATCHSIZE. */
3126 do {
3127 i = 0;
3128 if (_Pickler_Write(self, &mark_op, 1) < 0)
3129 return -1;
3130 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3131 if (save(self, item, 0) < 0)
3132 return -1;
3133 if (++i == BATCHSIZE)
3134 break;
3135 }
3136 if (_Pickler_Write(self, &additems_op, 1) < 0)
3137 return -1;
3138 if (PySet_GET_SIZE(obj) != set_size) {
3139 PyErr_Format(
3140 PyExc_RuntimeError,
3141 "set changed size during iteration");
3142 return -1;
3143 }
3144 } while (i == BATCHSIZE);
3145
3146 return 0;
3147}
3148
3149static int
3150save_frozenset(PicklerObject *self, PyObject *obj)
3151{
3152 PyObject *iter;
3153
3154 const char mark_op = MARK;
3155 const char frozenset_op = FROZENSET;
3156
3157 if (self->fast && !fast_save_enter(self, obj))
3158 return -1;
3159
3160 if (self->proto < 4) {
3161 PyObject *items;
3162 PyObject *reduce_value;
3163 int status;
3164
3165 items = PySequence_List(obj);
3166 if (items == NULL) {
3167 return -1;
3168 }
3169 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3170 items);
3171 Py_DECREF(items);
3172 if (reduce_value == NULL) {
3173 return -1;
3174 }
3175 /* save_reduce() will memoize the object automatically. */
3176 status = save_reduce(self, reduce_value, obj);
3177 Py_DECREF(reduce_value);
3178 return status;
3179 }
3180
3181 if (_Pickler_Write(self, &mark_op, 1) < 0)
3182 return -1;
3183
3184 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003185 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003186 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003187 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003188 for (;;) {
3189 PyObject *item;
3190
3191 item = PyIter_Next(iter);
3192 if (item == NULL) {
3193 if (PyErr_Occurred()) {
3194 Py_DECREF(iter);
3195 return -1;
3196 }
3197 break;
3198 }
3199 if (save(self, item, 0) < 0) {
3200 Py_DECREF(item);
3201 Py_DECREF(iter);
3202 return -1;
3203 }
3204 Py_DECREF(item);
3205 }
3206 Py_DECREF(iter);
3207
3208 /* If the object is already in the memo, this means it is
3209 recursive. In this case, throw away everything we put on the
3210 stack, and fetch the object back from the memo. */
3211 if (PyMemoTable_Get(self->memo, obj)) {
3212 const char pop_mark_op = POP_MARK;
3213
3214 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3215 return -1;
3216 if (memo_get(self, obj) < 0)
3217 return -1;
3218 return 0;
3219 }
3220
3221 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3222 return -1;
3223 if (memo_put(self, obj) < 0)
3224 return -1;
3225
3226 return 0;
3227}
3228
3229static int
3230fix_imports(PyObject **module_name, PyObject **global_name)
3231{
3232 PyObject *key;
3233 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003234 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003235
3236 key = PyTuple_Pack(2, *module_name, *global_name);
3237 if (key == NULL)
3238 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003239 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003240 Py_DECREF(key);
3241 if (item) {
3242 PyObject *fixed_module_name;
3243 PyObject *fixed_global_name;
3244
3245 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3246 PyErr_Format(PyExc_RuntimeError,
3247 "_compat_pickle.REVERSE_NAME_MAPPING values "
3248 "should be 2-tuples, not %.200s",
3249 Py_TYPE(item)->tp_name);
3250 return -1;
3251 }
3252 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3253 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3254 if (!PyUnicode_Check(fixed_module_name) ||
3255 !PyUnicode_Check(fixed_global_name)) {
3256 PyErr_Format(PyExc_RuntimeError,
3257 "_compat_pickle.REVERSE_NAME_MAPPING values "
3258 "should be pairs of str, not (%.200s, %.200s)",
3259 Py_TYPE(fixed_module_name)->tp_name,
3260 Py_TYPE(fixed_global_name)->tp_name);
3261 return -1;
3262 }
3263
3264 Py_CLEAR(*module_name);
3265 Py_CLEAR(*global_name);
3266 Py_INCREF(fixed_module_name);
3267 Py_INCREF(fixed_global_name);
3268 *module_name = fixed_module_name;
3269 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003270 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003271 }
3272 else if (PyErr_Occurred()) {
3273 return -1;
3274 }
3275
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003276 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003277 if (item) {
3278 if (!PyUnicode_Check(item)) {
3279 PyErr_Format(PyExc_RuntimeError,
3280 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3281 "should be strings, not %.200s",
3282 Py_TYPE(item)->tp_name);
3283 return -1;
3284 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003285 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003286 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003287 }
3288 else if (PyErr_Occurred()) {
3289 return -1;
3290 }
3291
3292 return 0;
3293}
3294
3295static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003296save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3297{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003298 PyObject *global_name = NULL;
3299 PyObject *module_name = NULL;
3300 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003301 PyObject *parent = NULL;
3302 PyObject *dotted_path = NULL;
3303 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003304 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003305 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003306 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003307 _Py_IDENTIFIER(__name__);
3308 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003309
3310 const char global_op = GLOBAL;
3311
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003312 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003313 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003314 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003315 }
3316 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003317 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3318 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003319 if (global_name == NULL) {
3320 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3321 if (global_name == NULL)
3322 goto error;
3323 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003324 }
3325
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003326 dotted_path = get_dotted_path(module, global_name);
3327 if (dotted_path == NULL)
3328 goto error;
3329 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003330 if (module_name == NULL)
3331 goto error;
3332
3333 /* XXX: Change to use the import C API directly with level=0 to disallow
3334 relative imports.
3335
3336 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3337 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3338 custom import functions (IMHO, this would be a nice security
3339 feature). The import C API would need to be extended to support the
3340 extra parameters of __import__ to fix that. */
3341 module = PyImport_Import(module_name);
3342 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003343 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003344 "Can't pickle %R: import of module %R failed",
3345 obj, module_name);
3346 goto error;
3347 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003348 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3349 Py_INCREF(lastname);
3350 cls = get_deep_attribute(module, dotted_path, &parent);
3351 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003352 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003353 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003354 "Can't pickle %R: attribute lookup %S on %S failed",
3355 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003356 goto error;
3357 }
3358 if (cls != obj) {
3359 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003360 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003361 "Can't pickle %R: it's not the same object as %S.%S",
3362 obj, module_name, global_name);
3363 goto error;
3364 }
3365 Py_DECREF(cls);
3366
3367 if (self->proto >= 2) {
3368 /* See whether this is in the extension registry, and if
3369 * so generate an EXT opcode.
3370 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003371 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003372 PyObject *code_obj; /* extension code as Python object */
3373 long code; /* extension code as C value */
3374 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003375 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003376
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003377 extension_key = PyTuple_Pack(2, module_name, global_name);
3378 if (extension_key == NULL) {
3379 goto error;
3380 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003381 code_obj = PyDict_GetItemWithError(st->extension_registry,
3382 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003383 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003384 /* The object is not registered in the extension registry.
3385 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003386 if (code_obj == NULL) {
3387 if (PyErr_Occurred()) {
3388 goto error;
3389 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003390 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003391 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003392
3393 /* XXX: pickle.py doesn't check neither the type, nor the range
3394 of the value returned by the extension_registry. It should for
3395 consistency. */
3396
3397 /* Verify code_obj has the right type and value. */
3398 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003399 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003400 "Can't pickle %R: extension code %R isn't an integer",
3401 obj, code_obj);
3402 goto error;
3403 }
3404 code = PyLong_AS_LONG(code_obj);
3405 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003406 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003407 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3408 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003409 goto error;
3410 }
3411
3412 /* Generate an EXT opcode. */
3413 if (code <= 0xff) {
3414 pdata[0] = EXT1;
3415 pdata[1] = (unsigned char)code;
3416 n = 2;
3417 }
3418 else if (code <= 0xffff) {
3419 pdata[0] = EXT2;
3420 pdata[1] = (unsigned char)(code & 0xff);
3421 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3422 n = 3;
3423 }
3424 else {
3425 pdata[0] = EXT4;
3426 pdata[1] = (unsigned char)(code & 0xff);
3427 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3428 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3429 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3430 n = 5;
3431 }
3432
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003433 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003434 goto error;
3435 }
3436 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003437 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003438 if (parent == module) {
3439 Py_INCREF(lastname);
3440 Py_DECREF(global_name);
3441 global_name = lastname;
3442 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003443 if (self->proto >= 4) {
3444 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003445
Christian Heimese8b1ba12013-11-23 21:13:39 +01003446 if (save(self, module_name, 0) < 0)
3447 goto error;
3448 if (save(self, global_name, 0) < 0)
3449 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003450
3451 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3452 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003453 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003454 else if (parent != module) {
3455 PickleState *st = _Pickle_GetGlobalState();
3456 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3457 st->getattr, parent, lastname);
Miss Islington (bot)3152bc32018-08-22 01:54:33 -04003458 if (reduce_value == NULL)
3459 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003460 status = save_reduce(self, reduce_value, NULL);
3461 Py_DECREF(reduce_value);
3462 if (status < 0)
3463 goto error;
3464 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003465 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003466 /* Generate a normal global opcode if we are using a pickle
3467 protocol < 4, or if the object is not registered in the
3468 extension registry. */
3469 PyObject *encoded;
3470 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003471
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003472 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003473 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003474
3475 /* For protocol < 3 and if the user didn't request against doing
3476 so, we convert module names to the old 2.x module names. */
3477 if (self->proto < 3 && self->fix_imports) {
3478 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003479 goto error;
3480 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003481 }
3482
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003483 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3484 both the module name and the global name using UTF-8. We do so
3485 only when we are using the pickle protocol newer than version
3486 3. This is to ensure compatibility with older Unpickler running
3487 on Python 2.x. */
3488 if (self->proto == 3) {
3489 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003490 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003491 else {
3492 unicode_encoder = PyUnicode_AsASCIIString;
3493 }
3494 encoded = unicode_encoder(module_name);
3495 if (encoded == NULL) {
3496 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003497 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003498 "can't pickle module identifier '%S' using "
3499 "pickle protocol %i",
3500 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003501 goto error;
3502 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003503 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3504 PyBytes_GET_SIZE(encoded)) < 0) {
3505 Py_DECREF(encoded);
3506 goto error;
3507 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003508 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003509 if(_Pickler_Write(self, "\n", 1) < 0)
3510 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003511
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003512 /* Save the name of the module. */
3513 encoded = unicode_encoder(global_name);
3514 if (encoded == NULL) {
3515 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003516 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003517 "can't pickle global identifier '%S' using "
3518 "pickle protocol %i",
3519 global_name, self->proto);
3520 goto error;
3521 }
3522 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3523 PyBytes_GET_SIZE(encoded)) < 0) {
3524 Py_DECREF(encoded);
3525 goto error;
3526 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003527 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003528 if (_Pickler_Write(self, "\n", 1) < 0)
3529 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003530 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003531 /* Memoize the object. */
3532 if (memo_put(self, obj) < 0)
3533 goto error;
3534 }
3535
3536 if (0) {
3537 error:
3538 status = -1;
3539 }
3540 Py_XDECREF(module_name);
3541 Py_XDECREF(global_name);
3542 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003543 Py_XDECREF(parent);
3544 Py_XDECREF(dotted_path);
3545 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003546
3547 return status;
3548}
3549
3550static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003551save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3552{
3553 PyObject *reduce_value;
3554 int status;
3555
3556 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3557 if (reduce_value == NULL) {
3558 return -1;
3559 }
3560 status = save_reduce(self, reduce_value, obj);
3561 Py_DECREF(reduce_value);
3562 return status;
3563}
3564
3565static int
3566save_type(PicklerObject *self, PyObject *obj)
3567{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003568 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003569 return save_singleton_type(self, obj, Py_None);
3570 }
3571 else if (obj == (PyObject *)&PyEllipsis_Type) {
3572 return save_singleton_type(self, obj, Py_Ellipsis);
3573 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003574 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003575 return save_singleton_type(self, obj, Py_NotImplemented);
3576 }
3577 return save_global(self, obj, NULL);
3578}
3579
3580static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003581save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003582{
3583 PyObject *pid = NULL;
3584 int status = 0;
3585
3586 const char persid_op = PERSID;
3587 const char binpersid_op = BINPERSID;
3588
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003589 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003590 if (pid == NULL)
3591 return -1;
3592
3593 if (pid != Py_None) {
3594 if (self->bin) {
3595 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003596 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003597 goto error;
3598 }
3599 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003600 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003601
3602 pid_str = PyObject_Str(pid);
3603 if (pid_str == NULL)
3604 goto error;
3605
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003606 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003607 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003608 if (!PyUnicode_IS_ASCII(pid_str)) {
3609 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3610 "persistent IDs in protocol 0 must be "
3611 "ASCII strings");
3612 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003613 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003614 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003615
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003616 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003617 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3618 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3619 _Pickler_Write(self, "\n", 1) < 0) {
3620 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003621 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003622 }
3623 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003624 }
3625 status = 1;
3626 }
3627
3628 if (0) {
3629 error:
3630 status = -1;
3631 }
3632 Py_XDECREF(pid);
3633
3634 return status;
3635}
3636
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003637static PyObject *
3638get_class(PyObject *obj)
3639{
3640 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003641 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003642
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003643 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3644 cls = (PyObject *) Py_TYPE(obj);
3645 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003646 }
3647 return cls;
3648}
3649
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003650/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3651 * appropriate __reduce__ method for obj.
3652 */
3653static int
3654save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3655{
3656 PyObject *callable;
3657 PyObject *argtup;
3658 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003659 PyObject *listitems = Py_None;
3660 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003661 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003662 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003663 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003664
3665 const char reduce_op = REDUCE;
3666 const char build_op = BUILD;
3667 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003668 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003669
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003670 size = PyTuple_Size(args);
3671 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003672 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003673 "__reduce__ must contain 2 through 5 elements");
3674 return -1;
3675 }
3676
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003677 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3678 &callable, &argtup, &state, &listitems, &dictitems))
3679 return -1;
3680
3681 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003682 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003683 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003684 return -1;
3685 }
3686 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003687 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003688 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003689 return -1;
3690 }
3691
3692 if (state == Py_None)
3693 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003694
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003695 if (listitems == Py_None)
3696 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003697 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003698 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003699 "returned by __reduce__ must be an iterator, not %s",
3700 Py_TYPE(listitems)->tp_name);
3701 return -1;
3702 }
3703
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003704 if (dictitems == Py_None)
3705 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003706 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003707 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003708 "returned by __reduce__ must be an iterator, not %s",
3709 Py_TYPE(dictitems)->tp_name);
3710 return -1;
3711 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003712
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003713 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003714 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003715 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003716
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003717 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3718 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003719 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003720 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003721 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003722 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3723 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003724 if (!use_newobj_ex) {
3725 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003726 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003727 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003728 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003729 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003730 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003731
3732 if (use_newobj_ex) {
3733 PyObject *cls;
3734 PyObject *args;
3735 PyObject *kwargs;
3736
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003737 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003738 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003739 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003740 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003741 return -1;
3742 }
3743
3744 cls = PyTuple_GET_ITEM(argtup, 0);
3745 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003746 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003747 "first item from NEWOBJ_EX argument tuple must "
3748 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3749 return -1;
3750 }
3751 args = PyTuple_GET_ITEM(argtup, 1);
3752 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003753 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003754 "second item from NEWOBJ_EX argument tuple must "
3755 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3756 return -1;
3757 }
3758 kwargs = PyTuple_GET_ITEM(argtup, 2);
3759 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003760 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003761 "third item from NEWOBJ_EX argument tuple must "
3762 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3763 return -1;
3764 }
3765
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003766 if (self->proto >= 4) {
3767 if (save(self, cls, 0) < 0 ||
3768 save(self, args, 0) < 0 ||
3769 save(self, kwargs, 0) < 0 ||
3770 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3771 return -1;
3772 }
3773 }
3774 else {
3775 PyObject *newargs;
3776 PyObject *cls_new;
3777 Py_ssize_t i;
3778 _Py_IDENTIFIER(__new__);
3779
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003780 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003781 if (newargs == NULL)
3782 return -1;
3783
3784 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3785 if (cls_new == NULL) {
3786 Py_DECREF(newargs);
3787 return -1;
3788 }
3789 PyTuple_SET_ITEM(newargs, 0, cls_new);
3790 Py_INCREF(cls);
3791 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003792 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003793 PyObject *item = PyTuple_GET_ITEM(args, i);
3794 Py_INCREF(item);
3795 PyTuple_SET_ITEM(newargs, i + 2, item);
3796 }
3797
3798 callable = PyObject_Call(st->partial, newargs, kwargs);
3799 Py_DECREF(newargs);
3800 if (callable == NULL)
3801 return -1;
3802
3803 newargs = PyTuple_New(0);
3804 if (newargs == NULL) {
3805 Py_DECREF(callable);
3806 return -1;
3807 }
3808
3809 if (save(self, callable, 0) < 0 ||
3810 save(self, newargs, 0) < 0 ||
3811 _Pickler_Write(self, &reduce_op, 1) < 0) {
3812 Py_DECREF(newargs);
3813 Py_DECREF(callable);
3814 return -1;
3815 }
3816 Py_DECREF(newargs);
3817 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003818 }
3819 }
3820 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003821 PyObject *cls;
3822 PyObject *newargtup;
3823 PyObject *obj_class;
3824 int p;
3825
3826 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003827 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003828 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003829 return -1;
3830 }
3831
3832 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003833 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003834 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003835 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003836 return -1;
3837 }
3838
3839 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003840 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003841 p = obj_class != cls; /* true iff a problem */
3842 Py_DECREF(obj_class);
3843 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003844 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003845 "__newobj__ args has the wrong class");
3846 return -1;
3847 }
3848 }
3849 /* XXX: These calls save() are prone to infinite recursion. Imagine
3850 what happen if the value returned by the __reduce__() method of
3851 some extension type contains another object of the same type. Ouch!
3852
3853 Here is a quick example, that I ran into, to illustrate what I
3854 mean:
3855
3856 >>> import pickle, copyreg
3857 >>> copyreg.dispatch_table.pop(complex)
3858 >>> pickle.dumps(1+2j)
3859 Traceback (most recent call last):
3860 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003861 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003862
3863 Removing the complex class from copyreg.dispatch_table made the
3864 __reduce_ex__() method emit another complex object:
3865
3866 >>> (1+1j).__reduce_ex__(2)
3867 (<function __newobj__ at 0xb7b71c3c>,
3868 (<class 'complex'>, (1+1j)), None, None, None)
3869
3870 Thus when save() was called on newargstup (the 2nd item) recursion
3871 ensued. Of course, the bug was in the complex class which had a
3872 broken __getnewargs__() that emitted another complex object. But,
3873 the point, here, is it is quite easy to end up with a broken reduce
3874 function. */
3875
3876 /* Save the class and its __new__ arguments. */
3877 if (save(self, cls, 0) < 0)
3878 return -1;
3879
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003880 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003881 if (newargtup == NULL)
3882 return -1;
3883
3884 p = save(self, newargtup, 0);
3885 Py_DECREF(newargtup);
3886 if (p < 0)
3887 return -1;
3888
3889 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003890 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003891 return -1;
3892 }
3893 else { /* Not using NEWOBJ. */
3894 if (save(self, callable, 0) < 0 ||
3895 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003896 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003897 return -1;
3898 }
3899
3900 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3901 the caller do not want to memoize the object. Not particularly useful,
3902 but that is to mimic the behavior save_reduce() in pickle.py when
3903 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003904 if (obj != NULL) {
3905 /* If the object is already in the memo, this means it is
3906 recursive. In this case, throw away everything we put on the
3907 stack, and fetch the object back from the memo. */
3908 if (PyMemoTable_Get(self->memo, obj)) {
3909 const char pop_op = POP;
3910
3911 if (_Pickler_Write(self, &pop_op, 1) < 0)
3912 return -1;
3913 if (memo_get(self, obj) < 0)
3914 return -1;
3915
3916 return 0;
3917 }
3918 else if (memo_put(self, obj) < 0)
3919 return -1;
3920 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003921
3922 if (listitems && batch_list(self, listitems) < 0)
3923 return -1;
3924
3925 if (dictitems && batch_dict(self, dictitems) < 0)
3926 return -1;
3927
3928 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003929 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003930 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003931 return -1;
3932 }
3933
3934 return 0;
3935}
3936
3937static int
3938save(PicklerObject *self, PyObject *obj, int pers_save)
3939{
3940 PyTypeObject *type;
3941 PyObject *reduce_func = NULL;
3942 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943 int status = 0;
3944
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003945 if (_Pickler_OpcodeBoundary(self) < 0)
3946 return -1;
3947
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003948 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003949 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003950
3951 /* The extra pers_save argument is necessary to avoid calling save_pers()
3952 on its returned object. */
3953 if (!pers_save && self->pers_func) {
3954 /* save_pers() returns:
3955 -1 to signal an error;
3956 0 if it did nothing successfully;
3957 1 if a persistent id was saved.
3958 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003959 if ((status = save_pers(self, obj)) != 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003960 goto done;
3961 }
3962
3963 type = Py_TYPE(obj);
3964
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003965 /* The old cPickle had an optimization that used switch-case statement
3966 dispatching on the first letter of the type name. This has was removed
3967 since benchmarks shown that this optimization was actually slowing
3968 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003969
3970 /* Atom types; these aren't memoized, so don't check the memo. */
3971
3972 if (obj == Py_None) {
3973 status = save_none(self, obj);
3974 goto done;
3975 }
3976 else if (obj == Py_False || obj == Py_True) {
3977 status = save_bool(self, obj);
3978 goto done;
3979 }
3980 else if (type == &PyLong_Type) {
3981 status = save_long(self, obj);
3982 goto done;
3983 }
3984 else if (type == &PyFloat_Type) {
3985 status = save_float(self, obj);
3986 goto done;
3987 }
3988
3989 /* Check the memo to see if it has the object. If so, generate
3990 a GET (or BINGET) opcode, instead of pickling the object
3991 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003992 if (PyMemoTable_Get(self->memo, obj)) {
3993 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003994 goto error;
3995 goto done;
3996 }
3997
3998 if (type == &PyBytes_Type) {
3999 status = save_bytes(self, obj);
4000 goto done;
4001 }
4002 else if (type == &PyUnicode_Type) {
4003 status = save_unicode(self, obj);
4004 goto done;
4005 }
4006 else if (type == &PyDict_Type) {
4007 status = save_dict(self, obj);
4008 goto done;
4009 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004010 else if (type == &PySet_Type) {
4011 status = save_set(self, obj);
4012 goto done;
4013 }
4014 else if (type == &PyFrozenSet_Type) {
4015 status = save_frozenset(self, obj);
4016 goto done;
4017 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004018 else if (type == &PyList_Type) {
4019 status = save_list(self, obj);
4020 goto done;
4021 }
4022 else if (type == &PyTuple_Type) {
4023 status = save_tuple(self, obj);
4024 goto done;
4025 }
4026 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004027 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004028 goto done;
4029 }
4030 else if (type == &PyFunction_Type) {
4031 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004032 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004033 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004034
4035 /* XXX: This part needs some unit tests. */
4036
4037 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004038 * self.dispatch_table, copyreg.dispatch_table, the object's
4039 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004040 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004041 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004042 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004043 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4044 (PyObject *)type);
4045 if (reduce_func == NULL) {
4046 if (PyErr_Occurred()) {
4047 goto error;
4048 }
4049 } else {
4050 /* PyDict_GetItemWithError() returns a borrowed reference.
4051 Increase the reference count to be consistent with
4052 PyObject_GetItem and _PyObject_GetAttrId used below. */
4053 Py_INCREF(reduce_func);
4054 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004055 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004056 reduce_func = PyObject_GetItem(self->dispatch_table,
4057 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004058 if (reduce_func == NULL) {
4059 if (PyErr_ExceptionMatches(PyExc_KeyError))
4060 PyErr_Clear();
4061 else
4062 goto error;
4063 }
4064 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004065 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004066 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004067 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004068 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004069 else if (PyType_IsSubtype(type, &PyType_Type)) {
4070 status = save_global(self, obj, NULL);
4071 goto done;
4072 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004073 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004074 _Py_IDENTIFIER(__reduce__);
4075 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004076
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004077
4078 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4079 automatically defined as __reduce__. While this is convenient, this
4080 make it impossible to know which method was actually called. Of
4081 course, this is not a big deal. But still, it would be nice to let
4082 the user know which method was called when something go
4083 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4084 don't actually have to check for a __reduce__ method. */
4085
4086 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004087 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4088 goto error;
4089 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004090 if (reduce_func != NULL) {
4091 PyObject *proto;
4092 proto = PyLong_FromLong(self->proto);
4093 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004094 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004095 }
4096 }
4097 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004098 PickleState *st = _Pickle_GetGlobalState();
4099
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004100 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004101 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004102 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004103 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004104 }
4105 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004106 PyErr_Format(st->PicklingError,
4107 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004108 type->tp_name, obj);
4109 goto error;
4110 }
4111 }
4112 }
4113
4114 if (reduce_value == NULL)
4115 goto error;
4116
4117 if (PyUnicode_Check(reduce_value)) {
4118 status = save_global(self, obj, reduce_value);
4119 goto done;
4120 }
4121
4122 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004123 PickleState *st = _Pickle_GetGlobalState();
4124 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004125 "__reduce__ must return a string or tuple");
4126 goto error;
4127 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004128
4129 status = save_reduce(self, reduce_value, obj);
4130
4131 if (0) {
4132 error:
4133 status = -1;
4134 }
4135 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004136
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004137 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004138 Py_XDECREF(reduce_func);
4139 Py_XDECREF(reduce_value);
4140
4141 return status;
4142}
4143
4144static int
4145dump(PicklerObject *self, PyObject *obj)
4146{
4147 const char stop_op = STOP;
4148
4149 if (self->proto >= 2) {
4150 char header[2];
4151
4152 header[0] = PROTO;
4153 assert(self->proto >= 0 && self->proto < 256);
4154 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004155 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004156 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004157 if (self->proto >= 4)
4158 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004159 }
4160
4161 if (save(self, obj, 0) < 0 ||
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004162 _Pickler_Write(self, &stop_op, 1) < 0 ||
4163 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004164 return -1;
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004165 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004166 return 0;
4167}
4168
Larry Hastings61272b72014-01-07 12:41:53 -08004169/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170
4171_pickle.Pickler.clear_memo
4172
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004173Clears the pickler's "memo".
4174
4175The memo is the data structure that remembers which objects the
4176pickler has already seen, so that shared or recursive objects are
4177pickled by reference and not by value. This method is useful when
4178re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004179[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004180
Larry Hastings3cceb382014-01-04 11:09:09 -08004181static PyObject *
4182_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004183/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004184{
4185 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004186 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004187
4188 Py_RETURN_NONE;
4189}
4190
Larry Hastings61272b72014-01-07 12:41:53 -08004191/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004192
4193_pickle.Pickler.dump
4194
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004195 obj: object
4196 /
4197
4198Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004199[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004200
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004201static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004202_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004203/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004204{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004205 /* Check whether the Pickler was initialized correctly (issue3664).
4206 Developers often forget to call __init__() in their subclasses, which
4207 would trigger a segfault without this check. */
4208 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004209 PickleState *st = _Pickle_GetGlobalState();
4210 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004211 "Pickler.__init__() was not called by %s.__init__()",
4212 Py_TYPE(self)->tp_name);
4213 return NULL;
4214 }
4215
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004216 if (_Pickler_ClearBuffer(self) < 0)
4217 return NULL;
4218
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004219 if (dump(self, obj) < 0)
4220 return NULL;
4221
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004222 if (_Pickler_FlushToFile(self) < 0)
4223 return NULL;
4224
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004225 Py_RETURN_NONE;
4226}
4227
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004228/*[clinic input]
4229
4230_pickle.Pickler.__sizeof__ -> Py_ssize_t
4231
4232Returns size in memory, in bytes.
4233[clinic start generated code]*/
4234
4235static Py_ssize_t
4236_pickle_Pickler___sizeof___impl(PicklerObject *self)
4237/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4238{
4239 Py_ssize_t res, s;
4240
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004241 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004242 if (self->memo != NULL) {
4243 res += sizeof(PyMemoTable);
4244 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4245 }
4246 if (self->output_buffer != NULL) {
4247 s = _PySys_GetSizeOf(self->output_buffer);
4248 if (s == -1)
4249 return -1;
4250 res += s;
4251 }
4252 return res;
4253}
4254
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004255static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004256 _PICKLE_PICKLER_DUMP_METHODDEF
4257 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004258 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004259 {NULL, NULL} /* sentinel */
4260};
4261
4262static void
4263Pickler_dealloc(PicklerObject *self)
4264{
4265 PyObject_GC_UnTrack(self);
4266
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004267 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004268 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004269 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004270 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004271 Py_XDECREF(self->fast_memo);
4272
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004273 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004274
4275 Py_TYPE(self)->tp_free((PyObject *)self);
4276}
4277
4278static int
4279Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4280{
4281 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004282 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004283 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004284 Py_VISIT(self->fast_memo);
4285 return 0;
4286}
4287
4288static int
4289Pickler_clear(PicklerObject *self)
4290{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004291 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004292 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004293 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004294 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004295 Py_CLEAR(self->fast_memo);
4296
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004297 if (self->memo != NULL) {
4298 PyMemoTable *memo = self->memo;
4299 self->memo = NULL;
4300 PyMemoTable_Del(memo);
4301 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004302 return 0;
4303}
4304
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004305
Larry Hastings61272b72014-01-07 12:41:53 -08004306/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004307
4308_pickle.Pickler.__init__
4309
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004310 file: object
4311 protocol: object = NULL
4312 fix_imports: bool = True
4313
4314This takes a binary file for writing a pickle data stream.
4315
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004316The optional *protocol* argument tells the pickler to use the given
4317protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4318protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004319
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004320Specifying a negative protocol version selects the highest protocol
4321version supported. The higher the protocol used, the more recent the
4322version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004323
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004324The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004325bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004326writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004327this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004328
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004329If *fix_imports* is True and protocol is less than 3, pickle will try
4330to map the new Python 3 names to the old module names used in Python
43312, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004332[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004333
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004334static int
Larry Hastings89964c42015-04-14 18:07:59 -04004335_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4336 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004337/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004338{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004339 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004340 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004341
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004342 /* In case of multiple __init__() calls, clear previous content. */
4343 if (self->write != NULL)
4344 (void)Pickler_clear(self);
4345
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004346 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004347 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004348
4349 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004350 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004351
4352 /* memo and output_buffer may have already been created in _Pickler_New */
4353 if (self->memo == NULL) {
4354 self->memo = PyMemoTable_New();
4355 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004356 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004357 }
4358 self->output_len = 0;
4359 if (self->output_buffer == NULL) {
4360 self->max_output_len = WRITE_BUF_SIZE;
4361 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4362 self->max_output_len);
4363 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004364 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004365 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004366
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004367 self->fast = 0;
4368 self->fast_nesting = 0;
4369 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004370
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004371 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4372 &self->pers_func, &self->pers_func_self) < 0)
4373 {
4374 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004375 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004376
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004377 if (_PyObject_LookupAttrId((PyObject *)self,
4378 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4379 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004380 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004381
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004382 return 0;
4383}
4384
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004385
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004386/* Define a proxy object for the Pickler's internal memo object. This is to
4387 * avoid breaking code like:
4388 * pickler.memo.clear()
4389 * and
4390 * pickler.memo = saved_memo
4391 * Is this a good idea? Not really, but we don't want to break code that uses
4392 * it. Note that we don't implement the entire mapping API here. This is
4393 * intentional, as these should be treated as black-box implementation details.
4394 */
4395
Larry Hastings61272b72014-01-07 12:41:53 -08004396/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004397_pickle.PicklerMemoProxy.clear
4398
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004399Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004400[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004401
Larry Hastings3cceb382014-01-04 11:09:09 -08004402static PyObject *
4403_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004404/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004405{
4406 if (self->pickler->memo)
4407 PyMemoTable_Clear(self->pickler->memo);
4408 Py_RETURN_NONE;
4409}
4410
Larry Hastings61272b72014-01-07 12:41:53 -08004411/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004412_pickle.PicklerMemoProxy.copy
4413
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004414Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004415[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004416
Larry Hastings3cceb382014-01-04 11:09:09 -08004417static PyObject *
4418_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004419/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004420{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004421 PyMemoTable *memo;
4422 PyObject *new_memo = PyDict_New();
4423 if (new_memo == NULL)
4424 return NULL;
4425
4426 memo = self->pickler->memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07004427 for (size_t i = 0; i < memo->mt_allocated; ++i) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004428 PyMemoEntry entry = memo->mt_table[i];
4429 if (entry.me_key != NULL) {
4430 int status;
4431 PyObject *key, *value;
4432
4433 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004434 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004435
4436 if (key == NULL || value == NULL) {
4437 Py_XDECREF(key);
4438 Py_XDECREF(value);
4439 goto error;
4440 }
4441 status = PyDict_SetItem(new_memo, key, value);
4442 Py_DECREF(key);
4443 Py_DECREF(value);
4444 if (status < 0)
4445 goto error;
4446 }
4447 }
4448 return new_memo;
4449
4450 error:
4451 Py_XDECREF(new_memo);
4452 return NULL;
4453}
4454
Larry Hastings61272b72014-01-07 12:41:53 -08004455/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004456_pickle.PicklerMemoProxy.__reduce__
4457
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004458Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004459[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004460
Larry Hastings3cceb382014-01-04 11:09:09 -08004461static PyObject *
4462_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004463/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004464{
4465 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004466 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004467 if (contents == NULL)
4468 return NULL;
4469
4470 reduce_value = PyTuple_New(2);
4471 if (reduce_value == NULL) {
4472 Py_DECREF(contents);
4473 return NULL;
4474 }
4475 dict_args = PyTuple_New(1);
4476 if (dict_args == NULL) {
4477 Py_DECREF(contents);
4478 Py_DECREF(reduce_value);
4479 return NULL;
4480 }
4481 PyTuple_SET_ITEM(dict_args, 0, contents);
4482 Py_INCREF((PyObject *)&PyDict_Type);
4483 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4484 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4485 return reduce_value;
4486}
4487
4488static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004489 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4490 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4491 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004492 {NULL, NULL} /* sentinel */
4493};
4494
4495static void
4496PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4497{
4498 PyObject_GC_UnTrack(self);
4499 Py_XDECREF(self->pickler);
4500 PyObject_GC_Del((PyObject *)self);
4501}
4502
4503static int
4504PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4505 visitproc visit, void *arg)
4506{
4507 Py_VISIT(self->pickler);
4508 return 0;
4509}
4510
4511static int
4512PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4513{
4514 Py_CLEAR(self->pickler);
4515 return 0;
4516}
4517
4518static PyTypeObject PicklerMemoProxyType = {
4519 PyVarObject_HEAD_INIT(NULL, 0)
4520 "_pickle.PicklerMemoProxy", /*tp_name*/
4521 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4522 0,
4523 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4524 0, /* tp_print */
4525 0, /* tp_getattr */
4526 0, /* tp_setattr */
4527 0, /* tp_compare */
4528 0, /* tp_repr */
4529 0, /* tp_as_number */
4530 0, /* tp_as_sequence */
4531 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004532 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004533 0, /* tp_call */
4534 0, /* tp_str */
4535 PyObject_GenericGetAttr, /* tp_getattro */
4536 PyObject_GenericSetAttr, /* tp_setattro */
4537 0, /* tp_as_buffer */
4538 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4539 0, /* tp_doc */
4540 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4541 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4542 0, /* tp_richcompare */
4543 0, /* tp_weaklistoffset */
4544 0, /* tp_iter */
4545 0, /* tp_iternext */
4546 picklerproxy_methods, /* tp_methods */
4547};
4548
4549static PyObject *
4550PicklerMemoProxy_New(PicklerObject *pickler)
4551{
4552 PicklerMemoProxyObject *self;
4553
4554 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4555 if (self == NULL)
4556 return NULL;
4557 Py_INCREF(pickler);
4558 self->pickler = pickler;
4559 PyObject_GC_Track(self);
4560 return (PyObject *)self;
4561}
4562
4563/*****************************************************************************/
4564
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004565static PyObject *
4566Pickler_get_memo(PicklerObject *self)
4567{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004568 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004569}
4570
4571static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004572Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004573{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004574 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004575
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004576 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004577 PyErr_SetString(PyExc_TypeError,
4578 "attribute deletion is not supported");
4579 return -1;
4580 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004581
4582 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4583 PicklerObject *pickler =
4584 ((PicklerMemoProxyObject *)obj)->pickler;
4585
4586 new_memo = PyMemoTable_Copy(pickler->memo);
4587 if (new_memo == NULL)
4588 return -1;
4589 }
4590 else if (PyDict_Check(obj)) {
4591 Py_ssize_t i = 0;
4592 PyObject *key, *value;
4593
4594 new_memo = PyMemoTable_New();
4595 if (new_memo == NULL)
4596 return -1;
4597
4598 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004599 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004600 PyObject *memo_obj;
4601
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004602 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004603 PyErr_SetString(PyExc_TypeError,
4604 "'memo' values must be 2-item tuples");
4605 goto error;
4606 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004607 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004608 if (memo_id == -1 && PyErr_Occurred())
4609 goto error;
4610 memo_obj = PyTuple_GET_ITEM(value, 1);
4611 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4612 goto error;
4613 }
4614 }
4615 else {
4616 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004617 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004618 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004619 return -1;
4620 }
4621
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004622 PyMemoTable_Del(self->memo);
4623 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004624
4625 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004626
4627 error:
4628 if (new_memo)
4629 PyMemoTable_Del(new_memo);
4630 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004631}
4632
4633static PyObject *
4634Pickler_get_persid(PicklerObject *self)
4635{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004636 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004637 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004638 return NULL;
4639 }
4640 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004641}
4642
4643static int
4644Pickler_set_persid(PicklerObject *self, PyObject *value)
4645{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004646 if (value == NULL) {
4647 PyErr_SetString(PyExc_TypeError,
4648 "attribute deletion is not supported");
4649 return -1;
4650 }
4651 if (!PyCallable_Check(value)) {
4652 PyErr_SetString(PyExc_TypeError,
4653 "persistent_id must be a callable taking one argument");
4654 return -1;
4655 }
4656
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004657 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004658 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004659 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004660
4661 return 0;
4662}
4663
4664static PyMemberDef Pickler_members[] = {
4665 {"bin", T_INT, offsetof(PicklerObject, bin)},
4666 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004667 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004668 {NULL}
4669};
4670
4671static PyGetSetDef Pickler_getsets[] = {
4672 {"memo", (getter)Pickler_get_memo,
4673 (setter)Pickler_set_memo},
4674 {"persistent_id", (getter)Pickler_get_persid,
4675 (setter)Pickler_set_persid},
4676 {NULL}
4677};
4678
4679static PyTypeObject Pickler_Type = {
4680 PyVarObject_HEAD_INIT(NULL, 0)
4681 "_pickle.Pickler" , /*tp_name*/
4682 sizeof(PicklerObject), /*tp_basicsize*/
4683 0, /*tp_itemsize*/
4684 (destructor)Pickler_dealloc, /*tp_dealloc*/
4685 0, /*tp_print*/
4686 0, /*tp_getattr*/
4687 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004688 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004689 0, /*tp_repr*/
4690 0, /*tp_as_number*/
4691 0, /*tp_as_sequence*/
4692 0, /*tp_as_mapping*/
4693 0, /*tp_hash*/
4694 0, /*tp_call*/
4695 0, /*tp_str*/
4696 0, /*tp_getattro*/
4697 0, /*tp_setattro*/
4698 0, /*tp_as_buffer*/
4699 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004700 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004701 (traverseproc)Pickler_traverse, /*tp_traverse*/
4702 (inquiry)Pickler_clear, /*tp_clear*/
4703 0, /*tp_richcompare*/
4704 0, /*tp_weaklistoffset*/
4705 0, /*tp_iter*/
4706 0, /*tp_iternext*/
4707 Pickler_methods, /*tp_methods*/
4708 Pickler_members, /*tp_members*/
4709 Pickler_getsets, /*tp_getset*/
4710 0, /*tp_base*/
4711 0, /*tp_dict*/
4712 0, /*tp_descr_get*/
4713 0, /*tp_descr_set*/
4714 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004715 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004716 PyType_GenericAlloc, /*tp_alloc*/
4717 PyType_GenericNew, /*tp_new*/
4718 PyObject_GC_Del, /*tp_free*/
4719 0, /*tp_is_gc*/
4720};
4721
Victor Stinner121aab42011-09-29 23:40:53 +02004722/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004723
4724 XXX: It would be nice to able to avoid Python function call overhead, by
4725 using directly the C version of find_class(), when find_class() is not
4726 overridden by a subclass. Although, this could become rather hackish. A
4727 simpler optimization would be to call the C function when self is not a
4728 subclass instance. */
4729static PyObject *
4730find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4731{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004732 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004733
Victor Stinner55ba38a2016-12-09 16:09:30 +01004734 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4735 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004736}
4737
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004738static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004739marker(UnpicklerObject *self)
4740{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004741 Py_ssize_t mark;
4742
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004743 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004744 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004745 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004746 return -1;
4747 }
4748
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004749 mark = self->marks[--self->num_marks];
4750 self->stack->mark_set = self->num_marks != 0;
4751 self->stack->fence = self->num_marks ?
4752 self->marks[self->num_marks - 1] : 0;
4753 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004754}
4755
4756static int
4757load_none(UnpicklerObject *self)
4758{
4759 PDATA_APPEND(self->stack, Py_None, -1);
4760 return 0;
4761}
4762
4763static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004764load_int(UnpicklerObject *self)
4765{
4766 PyObject *value;
4767 char *endptr, *s;
4768 Py_ssize_t len;
4769 long x;
4770
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004771 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004772 return -1;
4773 if (len < 2)
4774 return bad_readline();
4775
4776 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004777 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004778 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004779 x = strtol(s, &endptr, 0);
4780
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004781 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004782 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004783 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004784 errno = 0;
4785 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004786 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004787 if (value == NULL) {
4788 PyErr_SetString(PyExc_ValueError,
4789 "could not convert string to int");
4790 return -1;
4791 }
4792 }
4793 else {
4794 if (len == 3 && (x == 0 || x == 1)) {
4795 if ((value = PyBool_FromLong(x)) == NULL)
4796 return -1;
4797 }
4798 else {
4799 if ((value = PyLong_FromLong(x)) == NULL)
4800 return -1;
4801 }
4802 }
4803
4804 PDATA_PUSH(self->stack, value, -1);
4805 return 0;
4806}
4807
4808static int
4809load_bool(UnpicklerObject *self, PyObject *boolean)
4810{
4811 assert(boolean == Py_True || boolean == Py_False);
4812 PDATA_APPEND(self->stack, boolean, -1);
4813 return 0;
4814}
4815
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004816/* s contains x bytes of an unsigned little-endian integer. Return its value
4817 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4818 */
4819static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004820calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004821{
4822 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004823 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004824 size_t x = 0;
4825
Serhiy Storchakae0606192015-09-29 22:10:07 +03004826 if (nbytes > (int)sizeof(size_t)) {
4827 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4828 * have 64-bit size that can't be represented on 32-bit platform.
4829 */
4830 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4831 if (s[i])
4832 return -1;
4833 }
4834 nbytes = (int)sizeof(size_t);
4835 }
4836 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004837 x |= (size_t) s[i] << (8 * i);
4838 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004839
4840 if (x > PY_SSIZE_T_MAX)
4841 return -1;
4842 else
4843 return (Py_ssize_t) x;
4844}
4845
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004846/* s contains x bytes of a little-endian integer. Return its value as a
4847 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004848 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004849 * of x-platform bugs.
4850 */
4851static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004852calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004853{
4854 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004855 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004856 long x = 0;
4857
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004858 for (i = 0; i < nbytes; i++) {
4859 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004860 }
4861
4862 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4863 * is signed, so on a box with longs bigger than 4 bytes we need
4864 * to extend a BININT's sign bit to the full width.
4865 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004866 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004867 x |= -(x & (1L << 31));
4868 }
4869
4870 return x;
4871}
4872
4873static int
4874load_binintx(UnpicklerObject *self, char *s, int size)
4875{
4876 PyObject *value;
4877 long x;
4878
4879 x = calc_binint(s, size);
4880
4881 if ((value = PyLong_FromLong(x)) == NULL)
4882 return -1;
4883
4884 PDATA_PUSH(self->stack, value, -1);
4885 return 0;
4886}
4887
4888static int
4889load_binint(UnpicklerObject *self)
4890{
4891 char *s;
4892
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004893 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004894 return -1;
4895
4896 return load_binintx(self, s, 4);
4897}
4898
4899static int
4900load_binint1(UnpicklerObject *self)
4901{
4902 char *s;
4903
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004904 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004905 return -1;
4906
4907 return load_binintx(self, s, 1);
4908}
4909
4910static int
4911load_binint2(UnpicklerObject *self)
4912{
4913 char *s;
4914
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004915 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004916 return -1;
4917
4918 return load_binintx(self, s, 2);
4919}
4920
4921static int
4922load_long(UnpicklerObject *self)
4923{
4924 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004925 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004926 Py_ssize_t len;
4927
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004928 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004929 return -1;
4930 if (len < 2)
4931 return bad_readline();
4932
Mark Dickinson8dd05142009-01-20 20:43:58 +00004933 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4934 the 'L' before calling PyLong_FromString. In order to maintain
4935 compatibility with Python 3.0.0, we don't actually *require*
4936 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004937 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004938 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004939 /* XXX: Should the base argument explicitly set to 10? */
4940 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004941 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004942 return -1;
4943
4944 PDATA_PUSH(self->stack, value, -1);
4945 return 0;
4946}
4947
4948/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4949 * data following.
4950 */
4951static int
4952load_counted_long(UnpicklerObject *self, int size)
4953{
4954 PyObject *value;
4955 char *nbytes;
4956 char *pdata;
4957
4958 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004959 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004960 return -1;
4961
4962 size = calc_binint(nbytes, size);
4963 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004964 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004965 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004966 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004967 "LONG pickle has negative byte count");
4968 return -1;
4969 }
4970
4971 if (size == 0)
4972 value = PyLong_FromLong(0L);
4973 else {
4974 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004975 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004976 return -1;
4977 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4978 1 /* little endian */ , 1 /* signed */ );
4979 }
4980 if (value == NULL)
4981 return -1;
4982 PDATA_PUSH(self->stack, value, -1);
4983 return 0;
4984}
4985
4986static int
4987load_float(UnpicklerObject *self)
4988{
4989 PyObject *value;
4990 char *endptr, *s;
4991 Py_ssize_t len;
4992 double d;
4993
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004994 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004995 return -1;
4996 if (len < 2)
4997 return bad_readline();
4998
4999 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00005000 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
5001 if (d == -1.0 && PyErr_Occurred())
5002 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005003 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005004 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5005 return -1;
5006 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005007 value = PyFloat_FromDouble(d);
5008 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005009 return -1;
5010
5011 PDATA_PUSH(self->stack, value, -1);
5012 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005013}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005014
5015static int
5016load_binfloat(UnpicklerObject *self)
5017{
5018 PyObject *value;
5019 double x;
5020 char *s;
5021
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005022 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005023 return -1;
5024
5025 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5026 if (x == -1.0 && PyErr_Occurred())
5027 return -1;
5028
5029 if ((value = PyFloat_FromDouble(x)) == NULL)
5030 return -1;
5031
5032 PDATA_PUSH(self->stack, value, -1);
5033 return 0;
5034}
5035
5036static int
5037load_string(UnpicklerObject *self)
5038{
5039 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005040 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005041 Py_ssize_t len;
5042 char *s, *p;
5043
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005044 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005045 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005046 /* Strip the newline */
5047 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005049 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005050 p = s + 1;
5051 len -= 2;
5052 }
5053 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005054 PickleState *st = _Pickle_GetGlobalState();
5055 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005056 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005057 return -1;
5058 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005059 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005060
5061 /* Use the PyBytes API to decode the string, since that is what is used
5062 to encode, and then coerce the result to Unicode. */
5063 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005064 if (bytes == NULL)
5065 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005066
5067 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5068 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5069 if (strcmp(self->encoding, "bytes") == 0) {
5070 obj = bytes;
5071 }
5072 else {
5073 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5074 Py_DECREF(bytes);
5075 if (obj == NULL) {
5076 return -1;
5077 }
5078 }
5079
5080 PDATA_PUSH(self->stack, obj, -1);
5081 return 0;
5082}
5083
5084static int
5085load_counted_binstring(UnpicklerObject *self, int nbytes)
5086{
5087 PyObject *obj;
5088 Py_ssize_t size;
5089 char *s;
5090
5091 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005092 return -1;
5093
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005094 size = calc_binsize(s, nbytes);
5095 if (size < 0) {
5096 PickleState *st = _Pickle_GetGlobalState();
5097 PyErr_Format(st->UnpicklingError,
5098 "BINSTRING exceeds system's maximum size of %zd bytes",
5099 PY_SSIZE_T_MAX);
5100 return -1;
5101 }
5102
5103 if (_Unpickler_Read(self, &s, size) < 0)
5104 return -1;
5105
5106 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5107 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5108 if (strcmp(self->encoding, "bytes") == 0) {
5109 obj = PyBytes_FromStringAndSize(s, size);
5110 }
5111 else {
5112 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5113 }
5114 if (obj == NULL) {
5115 return -1;
5116 }
5117
5118 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005119 return 0;
5120}
5121
5122static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005123load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005124{
5125 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005126 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005127 char *s;
5128
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005129 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005130 return -1;
5131
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005132 size = calc_binsize(s, nbytes);
5133 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005134 PyErr_Format(PyExc_OverflowError,
5135 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005136 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005137 return -1;
5138 }
5139
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005140 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005141 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005142
5143 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005144 if (bytes == NULL)
5145 return -1;
5146
5147 PDATA_PUSH(self->stack, bytes, -1);
5148 return 0;
5149}
5150
5151static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005152load_unicode(UnpicklerObject *self)
5153{
5154 PyObject *str;
5155 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005156 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005157
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005158 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005159 return -1;
5160 if (len < 1)
5161 return bad_readline();
5162
5163 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5164 if (str == NULL)
5165 return -1;
5166
5167 PDATA_PUSH(self->stack, str, -1);
5168 return 0;
5169}
5170
5171static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005172load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005173{
5174 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005175 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005176 char *s;
5177
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005178 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005179 return -1;
5180
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005181 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005182 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005183 PyErr_Format(PyExc_OverflowError,
5184 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005185 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005186 return -1;
5187 }
5188
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005189 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005190 return -1;
5191
Victor Stinner485fb562010-04-13 11:07:24 +00005192 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005193 if (str == NULL)
5194 return -1;
5195
5196 PDATA_PUSH(self->stack, str, -1);
5197 return 0;
5198}
5199
5200static int
Victor Stinner21b47112016-03-14 18:09:39 +01005201load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005202{
5203 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005204
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005205 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005206 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005207
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005208 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005209 if (tuple == NULL)
5210 return -1;
5211 PDATA_PUSH(self->stack, tuple, -1);
5212 return 0;
5213}
5214
5215static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005216load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005217{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005218 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005219
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005220 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005221 return -1;
5222
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005223 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005224}
5225
5226static int
5227load_empty_list(UnpicklerObject *self)
5228{
5229 PyObject *list;
5230
5231 if ((list = PyList_New(0)) == NULL)
5232 return -1;
5233 PDATA_PUSH(self->stack, list, -1);
5234 return 0;
5235}
5236
5237static int
5238load_empty_dict(UnpicklerObject *self)
5239{
5240 PyObject *dict;
5241
5242 if ((dict = PyDict_New()) == NULL)
5243 return -1;
5244 PDATA_PUSH(self->stack, dict, -1);
5245 return 0;
5246}
5247
5248static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005249load_empty_set(UnpicklerObject *self)
5250{
5251 PyObject *set;
5252
5253 if ((set = PySet_New(NULL)) == NULL)
5254 return -1;
5255 PDATA_PUSH(self->stack, set, -1);
5256 return 0;
5257}
5258
5259static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005260load_list(UnpicklerObject *self)
5261{
5262 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005263 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005264
5265 if ((i = marker(self)) < 0)
5266 return -1;
5267
5268 list = Pdata_poplist(self->stack, i);
5269 if (list == NULL)
5270 return -1;
5271 PDATA_PUSH(self->stack, list, -1);
5272 return 0;
5273}
5274
5275static int
5276load_dict(UnpicklerObject *self)
5277{
5278 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005279 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005280
5281 if ((i = marker(self)) < 0)
5282 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005283 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005284
5285 if ((dict = PyDict_New()) == NULL)
5286 return -1;
5287
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005288 if ((j - i) % 2 != 0) {
5289 PickleState *st = _Pickle_GetGlobalState();
5290 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005291 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005292 return -1;
5293 }
5294
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005295 for (k = i + 1; k < j; k += 2) {
5296 key = self->stack->data[k - 1];
5297 value = self->stack->data[k];
5298 if (PyDict_SetItem(dict, key, value) < 0) {
5299 Py_DECREF(dict);
5300 return -1;
5301 }
5302 }
5303 Pdata_clear(self->stack, i);
5304 PDATA_PUSH(self->stack, dict, -1);
5305 return 0;
5306}
5307
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005308static int
5309load_frozenset(UnpicklerObject *self)
5310{
5311 PyObject *items;
5312 PyObject *frozenset;
5313 Py_ssize_t i;
5314
5315 if ((i = marker(self)) < 0)
5316 return -1;
5317
5318 items = Pdata_poptuple(self->stack, i);
5319 if (items == NULL)
5320 return -1;
5321
5322 frozenset = PyFrozenSet_New(items);
5323 Py_DECREF(items);
5324 if (frozenset == NULL)
5325 return -1;
5326
5327 PDATA_PUSH(self->stack, frozenset, -1);
5328 return 0;
5329}
5330
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005331static PyObject *
5332instantiate(PyObject *cls, PyObject *args)
5333{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005334 /* Caller must assure args are a tuple. Normally, args come from
5335 Pdata_poptuple which packs objects from the top of the stack
5336 into a newly created tuple. */
5337 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005338 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5339 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005340 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005341 PyObject *func;
5342 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5343 return NULL;
5344 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005345 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005346 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5347 }
5348 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005349 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005350 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005351}
5352
5353static int
5354load_obj(UnpicklerObject *self)
5355{
5356 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005357 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005358
5359 if ((i = marker(self)) < 0)
5360 return -1;
5361
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005362 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005363 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005364
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005365 args = Pdata_poptuple(self->stack, i + 1);
5366 if (args == NULL)
5367 return -1;
5368
5369 PDATA_POP(self->stack, cls);
5370 if (cls) {
5371 obj = instantiate(cls, args);
5372 Py_DECREF(cls);
5373 }
5374 Py_DECREF(args);
5375 if (obj == NULL)
5376 return -1;
5377
5378 PDATA_PUSH(self->stack, obj, -1);
5379 return 0;
5380}
5381
5382static int
5383load_inst(UnpicklerObject *self)
5384{
5385 PyObject *cls = NULL;
5386 PyObject *args = NULL;
5387 PyObject *obj = NULL;
5388 PyObject *module_name;
5389 PyObject *class_name;
5390 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005391 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005392 char *s;
5393
5394 if ((i = marker(self)) < 0)
5395 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005396 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005397 return -1;
5398 if (len < 2)
5399 return bad_readline();
5400
5401 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5402 identifiers are permitted in Python 3.0, since the INST opcode is only
5403 supported by older protocols on Python 2.x. */
5404 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5405 if (module_name == NULL)
5406 return -1;
5407
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005408 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005409 if (len < 2) {
5410 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005411 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005412 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005413 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005414 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005415 cls = find_class(self, module_name, class_name);
5416 Py_DECREF(class_name);
5417 }
5418 }
5419 Py_DECREF(module_name);
5420
5421 if (cls == NULL)
5422 return -1;
5423
5424 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5425 obj = instantiate(cls, args);
5426 Py_DECREF(args);
5427 }
5428 Py_DECREF(cls);
5429
5430 if (obj == NULL)
5431 return -1;
5432
5433 PDATA_PUSH(self->stack, obj, -1);
5434 return 0;
5435}
5436
5437static int
5438load_newobj(UnpicklerObject *self)
5439{
5440 PyObject *args = NULL;
5441 PyObject *clsraw = NULL;
5442 PyTypeObject *cls; /* clsraw cast to its true type */
5443 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005444 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005445
5446 /* Stack is ... cls argtuple, and we want to call
5447 * cls.__new__(cls, *argtuple).
5448 */
5449 PDATA_POP(self->stack, args);
5450 if (args == NULL)
5451 goto error;
5452 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005453 PyErr_SetString(st->UnpicklingError,
5454 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005455 goto error;
5456 }
5457
5458 PDATA_POP(self->stack, clsraw);
5459 cls = (PyTypeObject *)clsraw;
5460 if (cls == NULL)
5461 goto error;
5462 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005463 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005464 "isn't a type object");
5465 goto error;
5466 }
5467 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005468 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005469 "has NULL tp_new");
5470 goto error;
5471 }
5472
5473 /* Call __new__. */
5474 obj = cls->tp_new(cls, args, NULL);
5475 if (obj == NULL)
5476 goto error;
5477
5478 Py_DECREF(args);
5479 Py_DECREF(clsraw);
5480 PDATA_PUSH(self->stack, obj, -1);
5481 return 0;
5482
5483 error:
5484 Py_XDECREF(args);
5485 Py_XDECREF(clsraw);
5486 return -1;
5487}
5488
5489static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005490load_newobj_ex(UnpicklerObject *self)
5491{
5492 PyObject *cls, *args, *kwargs;
5493 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005494 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005495
5496 PDATA_POP(self->stack, kwargs);
5497 if (kwargs == NULL) {
5498 return -1;
5499 }
5500 PDATA_POP(self->stack, args);
5501 if (args == NULL) {
5502 Py_DECREF(kwargs);
5503 return -1;
5504 }
5505 PDATA_POP(self->stack, cls);
5506 if (cls == NULL) {
5507 Py_DECREF(kwargs);
5508 Py_DECREF(args);
5509 return -1;
5510 }
Larry Hastings61272b72014-01-07 12:41:53 -08005511
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005512 if (!PyType_Check(cls)) {
5513 Py_DECREF(kwargs);
5514 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005515 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005516 "NEWOBJ_EX class argument must be a type, not %.200s",
5517 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005518 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005519 return -1;
5520 }
5521
5522 if (((PyTypeObject *)cls)->tp_new == NULL) {
5523 Py_DECREF(kwargs);
5524 Py_DECREF(args);
5525 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005526 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005527 "NEWOBJ_EX class argument doesn't have __new__");
5528 return -1;
5529 }
5530 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5531 Py_DECREF(kwargs);
5532 Py_DECREF(args);
5533 Py_DECREF(cls);
5534 if (obj == NULL) {
5535 return -1;
5536 }
5537 PDATA_PUSH(self->stack, obj, -1);
5538 return 0;
5539}
5540
5541static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005542load_global(UnpicklerObject *self)
5543{
5544 PyObject *global = NULL;
5545 PyObject *module_name;
5546 PyObject *global_name;
5547 Py_ssize_t len;
5548 char *s;
5549
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005550 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005551 return -1;
5552 if (len < 2)
5553 return bad_readline();
5554 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5555 if (!module_name)
5556 return -1;
5557
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005558 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005559 if (len < 2) {
5560 Py_DECREF(module_name);
5561 return bad_readline();
5562 }
5563 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5564 if (global_name) {
5565 global = find_class(self, module_name, global_name);
5566 Py_DECREF(global_name);
5567 }
5568 }
5569 Py_DECREF(module_name);
5570
5571 if (global == NULL)
5572 return -1;
5573 PDATA_PUSH(self->stack, global, -1);
5574 return 0;
5575}
5576
5577static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005578load_stack_global(UnpicklerObject *self)
5579{
5580 PyObject *global;
5581 PyObject *module_name;
5582 PyObject *global_name;
5583
5584 PDATA_POP(self->stack, global_name);
5585 PDATA_POP(self->stack, module_name);
5586 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5587 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005588 PickleState *st = _Pickle_GetGlobalState();
5589 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005590 Py_XDECREF(global_name);
5591 Py_XDECREF(module_name);
5592 return -1;
5593 }
5594 global = find_class(self, module_name, global_name);
5595 Py_DECREF(global_name);
5596 Py_DECREF(module_name);
5597 if (global == NULL)
5598 return -1;
5599 PDATA_PUSH(self->stack, global, -1);
5600 return 0;
5601}
5602
5603static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604load_persid(UnpicklerObject *self)
5605{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005606 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005607 Py_ssize_t len;
5608 char *s;
5609
5610 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005611 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005613 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005614 return bad_readline();
5615
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005616 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5617 if (pid == NULL) {
5618 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5619 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5620 "persistent IDs in protocol 0 must be "
5621 "ASCII strings");
5622 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005623 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005624 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005626 obj = call_method(self->pers_func, self->pers_func_self, pid);
5627 Py_DECREF(pid);
5628 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005629 return -1;
5630
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005631 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632 return 0;
5633 }
5634 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005635 PickleState *st = _Pickle_GetGlobalState();
5636 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005637 "A load persistent id instruction was encountered,\n"
5638 "but no persistent_load function was specified.");
5639 return -1;
5640 }
5641}
5642
5643static int
5644load_binpersid(UnpicklerObject *self)
5645{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005646 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005647
5648 if (self->pers_func) {
5649 PDATA_POP(self->stack, pid);
5650 if (pid == NULL)
5651 return -1;
5652
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005653 obj = call_method(self->pers_func, self->pers_func_self, pid);
5654 Py_DECREF(pid);
5655 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005656 return -1;
5657
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005658 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005659 return 0;
5660 }
5661 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005662 PickleState *st = _Pickle_GetGlobalState();
5663 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005664 "A load persistent id instruction was encountered,\n"
5665 "but no persistent_load function was specified.");
5666 return -1;
5667 }
5668}
5669
5670static int
5671load_pop(UnpicklerObject *self)
5672{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005673 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005674
5675 /* Note that we split the (pickle.py) stack into two stacks,
5676 * an object stack and a mark stack. We have to be clever and
5677 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005678 * mark stack first, and only signalling a stack underflow if
5679 * the object stack is empty and the mark stack doesn't match
5680 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005681 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005682 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005683 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005684 self->stack->mark_set = self->num_marks != 0;
5685 self->stack->fence = self->num_marks ?
5686 self->marks[self->num_marks - 1] : 0;
5687 } else if (len <= self->stack->fence)
5688 return Pdata_stack_underflow(self->stack);
5689 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 len--;
5691 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005692 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005693 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005694 return 0;
5695}
5696
5697static int
5698load_pop_mark(UnpicklerObject *self)
5699{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005700 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005701
5702 if ((i = marker(self)) < 0)
5703 return -1;
5704
5705 Pdata_clear(self->stack, i);
5706
5707 return 0;
5708}
5709
5710static int
5711load_dup(UnpicklerObject *self)
5712{
5713 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005714 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005715
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005716 if (len <= self->stack->fence)
5717 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005718 last = self->stack->data[len - 1];
5719 PDATA_APPEND(self->stack, last, -1);
5720 return 0;
5721}
5722
5723static int
5724load_get(UnpicklerObject *self)
5725{
5726 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005727 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005728 Py_ssize_t len;
5729 char *s;
5730
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005731 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005732 return -1;
5733 if (len < 2)
5734 return bad_readline();
5735
5736 key = PyLong_FromString(s, NULL, 10);
5737 if (key == NULL)
5738 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005739 idx = PyLong_AsSsize_t(key);
5740 if (idx == -1 && PyErr_Occurred()) {
5741 Py_DECREF(key);
5742 return -1;
5743 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005744
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005745 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005746 if (value == NULL) {
5747 if (!PyErr_Occurred())
5748 PyErr_SetObject(PyExc_KeyError, key);
5749 Py_DECREF(key);
5750 return -1;
5751 }
5752 Py_DECREF(key);
5753
5754 PDATA_APPEND(self->stack, value, -1);
5755 return 0;
5756}
5757
5758static int
5759load_binget(UnpicklerObject *self)
5760{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005761 PyObject *value;
5762 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763 char *s;
5764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005765 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766 return -1;
5767
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005768 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005769
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005770 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005772 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005773 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005775 Py_DECREF(key);
5776 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005777 return -1;
5778 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005779
5780 PDATA_APPEND(self->stack, value, -1);
5781 return 0;
5782}
5783
5784static int
5785load_long_binget(UnpicklerObject *self)
5786{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005787 PyObject *value;
5788 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005790
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005791 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005792 return -1;
5793
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005794 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005795
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005796 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005798 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005799 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005800 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005801 Py_DECREF(key);
5802 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803 return -1;
5804 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005805
5806 PDATA_APPEND(self->stack, value, -1);
5807 return 0;
5808}
5809
5810/* Push an object from the extension registry (EXT[124]). nbytes is
5811 * the number of bytes following the opcode, holding the index (code) value.
5812 */
5813static int
5814load_extension(UnpicklerObject *self, int nbytes)
5815{
5816 char *codebytes; /* the nbytes bytes after the opcode */
5817 long code; /* calc_binint returns long */
5818 PyObject *py_code; /* code as a Python int */
5819 PyObject *obj; /* the object to push */
5820 PyObject *pair; /* (module_name, class_name) */
5821 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005822 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005823
5824 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005825 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005826 return -1;
5827 code = calc_binint(codebytes, nbytes);
5828 if (code <= 0) { /* note that 0 is forbidden */
5829 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005830 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005831 return -1;
5832 }
5833
5834 /* Look for the code in the cache. */
5835 py_code = PyLong_FromLong(code);
5836 if (py_code == NULL)
5837 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005838 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005839 if (obj != NULL) {
5840 /* Bingo. */
5841 Py_DECREF(py_code);
5842 PDATA_APPEND(self->stack, obj, -1);
5843 return 0;
5844 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005845 if (PyErr_Occurred()) {
5846 Py_DECREF(py_code);
5847 return -1;
5848 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005849
5850 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005851 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005852 if (pair == NULL) {
5853 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005854 if (!PyErr_Occurred()) {
5855 PyErr_Format(PyExc_ValueError, "unregistered extension "
5856 "code %ld", code);
5857 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005858 return -1;
5859 }
5860 /* Since the extension registry is manipulable via Python code,
5861 * confirm that pair is really a 2-tuple of strings.
5862 */
5863 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5864 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5865 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5866 Py_DECREF(py_code);
5867 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5868 "isn't a 2-tuple of strings", code);
5869 return -1;
5870 }
5871 /* Load the object. */
5872 obj = find_class(self, module_name, class_name);
5873 if (obj == NULL) {
5874 Py_DECREF(py_code);
5875 return -1;
5876 }
5877 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005878 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005879 Py_DECREF(py_code);
5880 if (code < 0) {
5881 Py_DECREF(obj);
5882 return -1;
5883 }
5884 PDATA_PUSH(self->stack, obj, -1);
5885 return 0;
5886}
5887
5888static int
5889load_put(UnpicklerObject *self)
5890{
5891 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005892 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005893 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005894 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005895
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005896 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005897 return -1;
5898 if (len < 2)
5899 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005900 if (Py_SIZE(self->stack) <= self->stack->fence)
5901 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005902 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005903
5904 key = PyLong_FromString(s, NULL, 10);
5905 if (key == NULL)
5906 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005907 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005908 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005909 if (idx < 0) {
5910 if (!PyErr_Occurred())
5911 PyErr_SetString(PyExc_ValueError,
5912 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005913 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005914 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005915
5916 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005917}
5918
5919static int
5920load_binput(UnpicklerObject *self)
5921{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005922 PyObject *value;
5923 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005924 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005925
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005926 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005927 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005928
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005929 if (Py_SIZE(self->stack) <= self->stack->fence)
5930 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005931 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005932
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005933 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005934
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005935 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005936}
5937
5938static int
5939load_long_binput(UnpicklerObject *self)
5940{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005941 PyObject *value;
5942 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005943 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005944
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005945 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005946 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005947
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005948 if (Py_SIZE(self->stack) <= self->stack->fence)
5949 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005950 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005951
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005952 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005953 if (idx < 0) {
5954 PyErr_SetString(PyExc_ValueError,
5955 "negative LONG_BINPUT argument");
5956 return -1;
5957 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005958
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005959 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005960}
5961
5962static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005963load_memoize(UnpicklerObject *self)
5964{
5965 PyObject *value;
5966
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005967 if (Py_SIZE(self->stack) <= self->stack->fence)
5968 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005969 value = self->stack->data[Py_SIZE(self->stack) - 1];
5970
5971 return _Unpickler_MemoPut(self, self->memo_len, value);
5972}
5973
5974static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005975do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005976{
5977 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005978 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005979 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005980 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005981 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005982
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005983 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005984 if (x > len || x <= self->stack->fence)
5985 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005986 if (len == x) /* nothing to do */
5987 return 0;
5988
5989 list = self->stack->data[x - 1];
5990
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005991 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005992 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005993 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005994
5995 slice = Pdata_poplist(self->stack, x);
5996 if (!slice)
5997 return -1;
5998 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005999 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006000 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006001 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006002 }
6003 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006004 PyObject *extend_func;
6005 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006006
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006007 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6008 if (extend_func != NULL) {
6009 slice = Pdata_poplist(self->stack, x);
6010 if (!slice) {
6011 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006012 return -1;
6013 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006014 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006015 Py_DECREF(extend_func);
6016 if (result == NULL)
6017 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006018 Py_DECREF(result);
6019 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006020 else {
6021 PyObject *append_func;
6022 _Py_IDENTIFIER(append);
6023
6024 /* Even if the PEP 307 requires extend() and append() methods,
6025 fall back on append() if the object has no extend() method
6026 for backward compatibility. */
6027 PyErr_Clear();
6028 append_func = _PyObject_GetAttrId(list, &PyId_append);
6029 if (append_func == NULL)
6030 return -1;
6031 for (i = x; i < len; i++) {
6032 value = self->stack->data[i];
6033 result = _Pickle_FastCall(append_func, value);
6034 if (result == NULL) {
6035 Pdata_clear(self->stack, i + 1);
6036 Py_SIZE(self->stack) = x;
6037 Py_DECREF(append_func);
6038 return -1;
6039 }
6040 Py_DECREF(result);
6041 }
6042 Py_SIZE(self->stack) = x;
6043 Py_DECREF(append_func);
6044 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006045 }
6046
6047 return 0;
6048}
6049
6050static int
6051load_append(UnpicklerObject *self)
6052{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006053 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6054 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006055 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006056}
6057
6058static int
6059load_appends(UnpicklerObject *self)
6060{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006061 Py_ssize_t i = marker(self);
6062 if (i < 0)
6063 return -1;
6064 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006065}
6066
6067static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006068do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006069{
6070 PyObject *value, *key;
6071 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006072 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006073 int status = 0;
6074
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006075 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006076 if (x > len || x <= self->stack->fence)
6077 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006078 if (len == x) /* nothing to do */
6079 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006080 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006081 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006082 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006083 PyErr_SetString(st->UnpicklingError,
6084 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006085 return -1;
6086 }
6087
6088 /* Here, dict does not actually need to be a PyDict; it could be anything
6089 that supports the __setitem__ attribute. */
6090 dict = self->stack->data[x - 1];
6091
6092 for (i = x + 1; i < len; i += 2) {
6093 key = self->stack->data[i - 1];
6094 value = self->stack->data[i];
6095 if (PyObject_SetItem(dict, key, value) < 0) {
6096 status = -1;
6097 break;
6098 }
6099 }
6100
6101 Pdata_clear(self->stack, x);
6102 return status;
6103}
6104
6105static int
6106load_setitem(UnpicklerObject *self)
6107{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006108 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006109}
6110
6111static int
6112load_setitems(UnpicklerObject *self)
6113{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006114 Py_ssize_t i = marker(self);
6115 if (i < 0)
6116 return -1;
6117 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006118}
6119
6120static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006121load_additems(UnpicklerObject *self)
6122{
6123 PyObject *set;
6124 Py_ssize_t mark, len, i;
6125
6126 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006127 if (mark < 0)
6128 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006129 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006130 if (mark > len || mark <= self->stack->fence)
6131 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006132 if (len == mark) /* nothing to do */
6133 return 0;
6134
6135 set = self->stack->data[mark - 1];
6136
6137 if (PySet_Check(set)) {
6138 PyObject *items;
6139 int status;
6140
6141 items = Pdata_poptuple(self->stack, mark);
6142 if (items == NULL)
6143 return -1;
6144
6145 status = _PySet_Update(set, items);
6146 Py_DECREF(items);
6147 return status;
6148 }
6149 else {
6150 PyObject *add_func;
6151 _Py_IDENTIFIER(add);
6152
6153 add_func = _PyObject_GetAttrId(set, &PyId_add);
6154 if (add_func == NULL)
6155 return -1;
6156 for (i = mark; i < len; i++) {
6157 PyObject *result;
6158 PyObject *item;
6159
6160 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006161 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006162 if (result == NULL) {
6163 Pdata_clear(self->stack, i + 1);
6164 Py_SIZE(self->stack) = mark;
6165 return -1;
6166 }
6167 Py_DECREF(result);
6168 }
6169 Py_SIZE(self->stack) = mark;
6170 }
6171
6172 return 0;
6173}
6174
6175static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006176load_build(UnpicklerObject *self)
6177{
6178 PyObject *state, *inst, *slotstate;
6179 PyObject *setstate;
6180 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006181 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006182
6183 /* Stack is ... instance, state. We want to leave instance at
6184 * the stack top, possibly mutated via instance.__setstate__(state).
6185 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006186 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6187 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006188
6189 PDATA_POP(self->stack, state);
6190 if (state == NULL)
6191 return -1;
6192
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006193 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006194
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006195 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6196 Py_DECREF(state);
6197 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006198 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006199 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006200 PyObject *result;
6201
6202 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006203 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006204 Py_DECREF(setstate);
6205 if (result == NULL)
6206 return -1;
6207 Py_DECREF(result);
6208 return 0;
6209 }
6210
6211 /* A default __setstate__. First see whether state embeds a
6212 * slot state dict too (a proto 2 addition).
6213 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006214 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006215 PyObject *tmp = state;
6216
6217 state = PyTuple_GET_ITEM(tmp, 0);
6218 slotstate = PyTuple_GET_ITEM(tmp, 1);
6219 Py_INCREF(state);
6220 Py_INCREF(slotstate);
6221 Py_DECREF(tmp);
6222 }
6223 else
6224 slotstate = NULL;
6225
6226 /* Set inst.__dict__ from the state dict (if any). */
6227 if (state != Py_None) {
6228 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006229 PyObject *d_key, *d_value;
6230 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006231 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006232
6233 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006234 PickleState *st = _Pickle_GetGlobalState();
6235 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006236 goto error;
6237 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006238 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006239 if (dict == NULL)
6240 goto error;
6241
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006242 i = 0;
6243 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6244 /* normally the keys for instance attributes are
6245 interned. we should try to do that here. */
6246 Py_INCREF(d_key);
6247 if (PyUnicode_CheckExact(d_key))
6248 PyUnicode_InternInPlace(&d_key);
6249 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6250 Py_DECREF(d_key);
6251 goto error;
6252 }
6253 Py_DECREF(d_key);
6254 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006255 Py_DECREF(dict);
6256 }
6257
6258 /* Also set instance attributes from the slotstate dict (if any). */
6259 if (slotstate != NULL) {
6260 PyObject *d_key, *d_value;
6261 Py_ssize_t i;
6262
6263 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006264 PickleState *st = _Pickle_GetGlobalState();
6265 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006266 "slot state is not a dictionary");
6267 goto error;
6268 }
6269 i = 0;
6270 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6271 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6272 goto error;
6273 }
6274 }
6275
6276 if (0) {
6277 error:
6278 status = -1;
6279 }
6280
6281 Py_DECREF(state);
6282 Py_XDECREF(slotstate);
6283 return status;
6284}
6285
6286static int
6287load_mark(UnpicklerObject *self)
6288{
6289
6290 /* Note that we split the (pickle.py) stack into two stacks, an
6291 * object stack and a mark stack. Here we push a mark onto the
6292 * mark stack.
6293 */
6294
6295 if ((self->num_marks + 1) >= self->marks_size) {
6296 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006297
6298 /* Use the size_t type to check for overflow. */
6299 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006300 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006301 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006302 PyErr_NoMemory();
6303 return -1;
6304 }
6305
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006306 Py_ssize_t *marks_old = self->marks;
6307 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006308 if (self->marks == NULL) {
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006309 PyMem_FREE(marks_old);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006310 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006311 PyErr_NoMemory();
6312 return -1;
6313 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006314 self->marks_size = (Py_ssize_t)alloc;
6315 }
6316
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006317 self->stack->mark_set = 1;
6318 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006319
6320 return 0;
6321}
6322
6323static int
6324load_reduce(UnpicklerObject *self)
6325{
6326 PyObject *callable = NULL;
6327 PyObject *argtup = NULL;
6328 PyObject *obj = NULL;
6329
6330 PDATA_POP(self->stack, argtup);
6331 if (argtup == NULL)
6332 return -1;
6333 PDATA_POP(self->stack, callable);
6334 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006335 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006336 Py_DECREF(callable);
6337 }
6338 Py_DECREF(argtup);
6339
6340 if (obj == NULL)
6341 return -1;
6342
6343 PDATA_PUSH(self->stack, obj, -1);
6344 return 0;
6345}
6346
6347/* Just raises an error if we don't know the protocol specified. PROTO
6348 * is the first opcode for protocols >= 2.
6349 */
6350static int
6351load_proto(UnpicklerObject *self)
6352{
6353 char *s;
6354 int i;
6355
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006356 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006357 return -1;
6358
6359 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006360 if (i <= HIGHEST_PROTOCOL) {
6361 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006362 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006363 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006364
6365 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6366 return -1;
6367}
6368
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006369static int
6370load_frame(UnpicklerObject *self)
6371{
6372 char *s;
6373 Py_ssize_t frame_len;
6374
6375 if (_Unpickler_Read(self, &s, 8) < 0)
6376 return -1;
6377
6378 frame_len = calc_binsize(s, 8);
6379 if (frame_len < 0) {
6380 PyErr_Format(PyExc_OverflowError,
6381 "FRAME length exceeds system's maximum of %zd bytes",
6382 PY_SSIZE_T_MAX);
6383 return -1;
6384 }
6385
6386 if (_Unpickler_Read(self, &s, frame_len) < 0)
6387 return -1;
6388
6389 /* Rewind to start of frame */
6390 self->next_read_idx -= frame_len;
6391 return 0;
6392}
6393
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006394static PyObject *
6395load(UnpicklerObject *self)
6396{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006397 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006398 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006399
6400 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006401 self->stack->mark_set = 0;
6402 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006403 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006404 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006405 Pdata_clear(self->stack, 0);
6406
6407 /* Convenient macros for the dispatch while-switch loop just below. */
6408#define OP(opcode, load_func) \
6409 case opcode: if (load_func(self) < 0) break; continue;
6410
6411#define OP_ARG(opcode, load_func, arg) \
6412 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6413
6414 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006415 if (_Unpickler_Read(self, &s, 1) < 0) {
6416 PickleState *st = _Pickle_GetGlobalState();
6417 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6418 PyErr_Format(PyExc_EOFError, "Ran out of input");
6419 }
6420 return NULL;
6421 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006422
6423 switch ((enum opcode)s[0]) {
6424 OP(NONE, load_none)
6425 OP(BININT, load_binint)
6426 OP(BININT1, load_binint1)
6427 OP(BININT2, load_binint2)
6428 OP(INT, load_int)
6429 OP(LONG, load_long)
6430 OP_ARG(LONG1, load_counted_long, 1)
6431 OP_ARG(LONG4, load_counted_long, 4)
6432 OP(FLOAT, load_float)
6433 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006434 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6435 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6436 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6437 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6438 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006439 OP(STRING, load_string)
6440 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006441 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6442 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6443 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006444 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6445 OP_ARG(TUPLE1, load_counted_tuple, 1)
6446 OP_ARG(TUPLE2, load_counted_tuple, 2)
6447 OP_ARG(TUPLE3, load_counted_tuple, 3)
6448 OP(TUPLE, load_tuple)
6449 OP(EMPTY_LIST, load_empty_list)
6450 OP(LIST, load_list)
6451 OP(EMPTY_DICT, load_empty_dict)
6452 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006453 OP(EMPTY_SET, load_empty_set)
6454 OP(ADDITEMS, load_additems)
6455 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006456 OP(OBJ, load_obj)
6457 OP(INST, load_inst)
6458 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006459 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006460 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006461 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006462 OP(APPEND, load_append)
6463 OP(APPENDS, load_appends)
6464 OP(BUILD, load_build)
6465 OP(DUP, load_dup)
6466 OP(BINGET, load_binget)
6467 OP(LONG_BINGET, load_long_binget)
6468 OP(GET, load_get)
6469 OP(MARK, load_mark)
6470 OP(BINPUT, load_binput)
6471 OP(LONG_BINPUT, load_long_binput)
6472 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006473 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006474 OP(POP, load_pop)
6475 OP(POP_MARK, load_pop_mark)
6476 OP(SETITEM, load_setitem)
6477 OP(SETITEMS, load_setitems)
6478 OP(PERSID, load_persid)
6479 OP(BINPERSID, load_binpersid)
6480 OP(REDUCE, load_reduce)
6481 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006482 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006483 OP_ARG(EXT1, load_extension, 1)
6484 OP_ARG(EXT2, load_extension, 2)
6485 OP_ARG(EXT4, load_extension, 4)
6486 OP_ARG(NEWTRUE, load_bool, Py_True)
6487 OP_ARG(NEWFALSE, load_bool, Py_False)
6488
6489 case STOP:
6490 break;
6491
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006492 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006493 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006494 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006495 unsigned char c = (unsigned char) *s;
6496 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6497 PyErr_Format(st->UnpicklingError,
6498 "invalid load key, '%c'.", c);
6499 }
6500 else {
6501 PyErr_Format(st->UnpicklingError,
6502 "invalid load key, '\\x%02x'.", c);
6503 }
6504 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006505 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006506 }
6507
6508 break; /* and we are done! */
6509 }
6510
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006511 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006512 return NULL;
6513 }
6514
Victor Stinner2ae57e32013-10-31 13:39:23 +01006515 if (_Unpickler_SkipConsumed(self) < 0)
6516 return NULL;
6517
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006518 PDATA_POP(self->stack, value);
6519 return value;
6520}
6521
Larry Hastings61272b72014-01-07 12:41:53 -08006522/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006523
6524_pickle.Unpickler.load
6525
6526Load a pickle.
6527
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006528Read a pickled object representation from the open file object given
6529in the constructor, and return the reconstituted object hierarchy
6530specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006531[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006532
Larry Hastings3cceb382014-01-04 11:09:09 -08006533static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006534_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006535/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006536{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006537 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006538
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539 /* Check whether the Unpickler was initialized correctly. This prevents
6540 segfaulting if a subclass overridden __init__ with a function that does
6541 not call Unpickler.__init__(). Here, we simply ensure that self->read
6542 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006543 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006544 PickleState *st = _Pickle_GetGlobalState();
6545 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006547 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006548 return NULL;
6549 }
6550
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006551 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006552}
6553
6554/* The name of find_class() is misleading. In newer pickle protocols, this
6555 function is used for loading any global (i.e., functions), not just
6556 classes. The name is kept only for backward compatibility. */
6557
Larry Hastings61272b72014-01-07 12:41:53 -08006558/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006559
6560_pickle.Unpickler.find_class
6561
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006562 module_name: object
6563 global_name: object
6564 /
6565
6566Return an object from a specified module.
6567
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006568If necessary, the module will be imported. Subclasses may override
6569this method (e.g. to restrict unpickling of arbitrary classes and
6570functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006571
6572This method is called whenever a class or a function object is
6573needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006574[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006575
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006576static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006577_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6578 PyObject *module_name,
6579 PyObject *global_name)
6580/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006581{
6582 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006583 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006584
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006585 /* Try to map the old names used in Python 2.x to the new ones used in
6586 Python 3.x. We do this only with old pickle protocols and when the
6587 user has not disabled the feature. */
6588 if (self->proto < 3 && self->fix_imports) {
6589 PyObject *key;
6590 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006591 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006592
6593 /* Check if the global (i.e., a function or a class) was renamed
6594 or moved to another module. */
6595 key = PyTuple_Pack(2, module_name, global_name);
6596 if (key == NULL)
6597 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006598 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006599 Py_DECREF(key);
6600 if (item) {
6601 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6602 PyErr_Format(PyExc_RuntimeError,
6603 "_compat_pickle.NAME_MAPPING values should be "
6604 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6605 return NULL;
6606 }
6607 module_name = PyTuple_GET_ITEM(item, 0);
6608 global_name = PyTuple_GET_ITEM(item, 1);
6609 if (!PyUnicode_Check(module_name) ||
6610 !PyUnicode_Check(global_name)) {
6611 PyErr_Format(PyExc_RuntimeError,
6612 "_compat_pickle.NAME_MAPPING values should be "
6613 "pairs of str, not (%.200s, %.200s)",
6614 Py_TYPE(module_name)->tp_name,
6615 Py_TYPE(global_name)->tp_name);
6616 return NULL;
6617 }
6618 }
6619 else if (PyErr_Occurred()) {
6620 return NULL;
6621 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006622 else {
6623 /* Check if the module was renamed. */
6624 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6625 if (item) {
6626 if (!PyUnicode_Check(item)) {
6627 PyErr_Format(PyExc_RuntimeError,
6628 "_compat_pickle.IMPORT_MAPPING values should be "
6629 "strings, not %.200s", Py_TYPE(item)->tp_name);
6630 return NULL;
6631 }
6632 module_name = item;
6633 }
6634 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006635 return NULL;
6636 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006637 }
6638 }
6639
Eric Snow3f9eee62017-09-15 16:35:20 -06006640 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006641 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006642 if (PyErr_Occurred())
6643 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006644 module = PyImport_Import(module_name);
6645 if (module == NULL)
6646 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006647 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006648 global = getattribute(module, global_name, self->proto >= 4);
6649 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650 return global;
6651}
6652
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006653/*[clinic input]
6654
6655_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6656
6657Returns size in memory, in bytes.
6658[clinic start generated code]*/
6659
6660static Py_ssize_t
6661_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6662/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6663{
6664 Py_ssize_t res;
6665
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006666 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006667 if (self->memo != NULL)
6668 res += self->memo_size * sizeof(PyObject *);
6669 if (self->marks != NULL)
6670 res += self->marks_size * sizeof(Py_ssize_t);
6671 if (self->input_line != NULL)
6672 res += strlen(self->input_line) + 1;
6673 if (self->encoding != NULL)
6674 res += strlen(self->encoding) + 1;
6675 if (self->errors != NULL)
6676 res += strlen(self->errors) + 1;
6677 return res;
6678}
6679
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006680static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006681 _PICKLE_UNPICKLER_LOAD_METHODDEF
6682 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006683 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006684 {NULL, NULL} /* sentinel */
6685};
6686
6687static void
6688Unpickler_dealloc(UnpicklerObject *self)
6689{
6690 PyObject_GC_UnTrack((PyObject *)self);
6691 Py_XDECREF(self->readline);
6692 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006693 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006694 Py_XDECREF(self->stack);
6695 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006696 if (self->buffer.buf != NULL) {
6697 PyBuffer_Release(&self->buffer);
6698 self->buffer.buf = NULL;
6699 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006700
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006701 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006702 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006703 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006704 PyMem_Free(self->encoding);
6705 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006706
6707 Py_TYPE(self)->tp_free((PyObject *)self);
6708}
6709
6710static int
6711Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6712{
6713 Py_VISIT(self->readline);
6714 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006715 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006716 Py_VISIT(self->stack);
6717 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006718 return 0;
6719}
6720
6721static int
6722Unpickler_clear(UnpicklerObject *self)
6723{
6724 Py_CLEAR(self->readline);
6725 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006726 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006727 Py_CLEAR(self->stack);
6728 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006729 if (self->buffer.buf != NULL) {
6730 PyBuffer_Release(&self->buffer);
6731 self->buffer.buf = NULL;
6732 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006733
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006734 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006735 PyMem_Free(self->marks);
6736 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006737 PyMem_Free(self->input_line);
6738 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006739 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006740 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006741 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006742 self->errors = NULL;
6743
6744 return 0;
6745}
6746
Larry Hastings61272b72014-01-07 12:41:53 -08006747/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006748
6749_pickle.Unpickler.__init__
6750
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006751 file: object
6752 *
6753 fix_imports: bool = True
6754 encoding: str = 'ASCII'
6755 errors: str = 'strict'
6756
6757This takes a binary file for reading a pickle data stream.
6758
6759The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006760protocol argument is needed. Bytes past the pickled object's
6761representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006762
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006763The argument *file* must have two methods, a read() method that takes
6764an integer argument, and a readline() method that requires no
6765arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006766binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006767other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006768
6769Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006770which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006771generated by Python 2. If *fix_imports* is True, pickle will try to
6772map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006773*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006774instances pickled by Python 2; these default to 'ASCII' and 'strict',
6775respectively. The *encoding* can be 'bytes' to read these 8-bit
6776string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006777[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006778
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006779static int
Larry Hastings89964c42015-04-14 18:07:59 -04006780_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6781 int fix_imports, const char *encoding,
6782 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006783/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006784{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006785 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006786
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006787 /* In case of multiple __init__() calls, clear previous content. */
6788 if (self->read != NULL)
6789 (void)Unpickler_clear(self);
6790
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006791 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006792 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006794 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006795 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006796
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006797 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006798
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006799 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6800 &self->pers_func, &self->pers_func_self) < 0)
6801 {
6802 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006803 }
6804
6805 self->stack = (Pdata *)Pdata_New();
6806 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006807 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006808
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006809 self->memo_size = 32;
6810 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006811 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006812 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006813
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006814 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006815
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006816 return 0;
6817}
6818
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006819
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006820/* Define a proxy object for the Unpickler's internal memo object. This is to
6821 * avoid breaking code like:
6822 * unpickler.memo.clear()
6823 * and
6824 * unpickler.memo = saved_memo
6825 * Is this a good idea? Not really, but we don't want to break code that uses
6826 * it. Note that we don't implement the entire mapping API here. This is
6827 * intentional, as these should be treated as black-box implementation details.
6828 *
6829 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006830 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006831 */
6832
Larry Hastings61272b72014-01-07 12:41:53 -08006833/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006834_pickle.UnpicklerMemoProxy.clear
6835
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006836Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006837[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006838
Larry Hastings3cceb382014-01-04 11:09:09 -08006839static PyObject *
6840_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006841/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006842{
6843 _Unpickler_MemoCleanup(self->unpickler);
6844 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6845 if (self->unpickler->memo == NULL)
6846 return NULL;
6847 Py_RETURN_NONE;
6848}
6849
Larry Hastings61272b72014-01-07 12:41:53 -08006850/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006851_pickle.UnpicklerMemoProxy.copy
6852
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006853Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006854[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006855
Larry Hastings3cceb382014-01-04 11:09:09 -08006856static PyObject *
6857_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006858/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006859{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07006860 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006861 PyObject *new_memo = PyDict_New();
6862 if (new_memo == NULL)
6863 return NULL;
6864
6865 for (i = 0; i < self->unpickler->memo_size; i++) {
6866 int status;
6867 PyObject *key, *value;
6868
6869 value = self->unpickler->memo[i];
6870 if (value == NULL)
6871 continue;
6872
6873 key = PyLong_FromSsize_t(i);
6874 if (key == NULL)
6875 goto error;
6876 status = PyDict_SetItem(new_memo, key, value);
6877 Py_DECREF(key);
6878 if (status < 0)
6879 goto error;
6880 }
6881 return new_memo;
6882
6883error:
6884 Py_DECREF(new_memo);
6885 return NULL;
6886}
6887
Larry Hastings61272b72014-01-07 12:41:53 -08006888/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006889_pickle.UnpicklerMemoProxy.__reduce__
6890
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006891Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006892[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006893
Larry Hastings3cceb382014-01-04 11:09:09 -08006894static PyObject *
6895_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006896/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006897{
6898 PyObject *reduce_value;
6899 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006900 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006901 if (contents == NULL)
6902 return NULL;
6903
6904 reduce_value = PyTuple_New(2);
6905 if (reduce_value == NULL) {
6906 Py_DECREF(contents);
6907 return NULL;
6908 }
6909 constructor_args = PyTuple_New(1);
6910 if (constructor_args == NULL) {
6911 Py_DECREF(contents);
6912 Py_DECREF(reduce_value);
6913 return NULL;
6914 }
6915 PyTuple_SET_ITEM(constructor_args, 0, contents);
6916 Py_INCREF((PyObject *)&PyDict_Type);
6917 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6918 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6919 return reduce_value;
6920}
6921
6922static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006923 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6924 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6925 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006926 {NULL, NULL} /* sentinel */
6927};
6928
6929static void
6930UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6931{
6932 PyObject_GC_UnTrack(self);
6933 Py_XDECREF(self->unpickler);
6934 PyObject_GC_Del((PyObject *)self);
6935}
6936
6937static int
6938UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6939 visitproc visit, void *arg)
6940{
6941 Py_VISIT(self->unpickler);
6942 return 0;
6943}
6944
6945static int
6946UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6947{
6948 Py_CLEAR(self->unpickler);
6949 return 0;
6950}
6951
6952static PyTypeObject UnpicklerMemoProxyType = {
6953 PyVarObject_HEAD_INIT(NULL, 0)
6954 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6955 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6956 0,
6957 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6958 0, /* tp_print */
6959 0, /* tp_getattr */
6960 0, /* tp_setattr */
6961 0, /* tp_compare */
6962 0, /* tp_repr */
6963 0, /* tp_as_number */
6964 0, /* tp_as_sequence */
6965 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006966 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006967 0, /* tp_call */
6968 0, /* tp_str */
6969 PyObject_GenericGetAttr, /* tp_getattro */
6970 PyObject_GenericSetAttr, /* tp_setattro */
6971 0, /* tp_as_buffer */
6972 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6973 0, /* tp_doc */
6974 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6975 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6976 0, /* tp_richcompare */
6977 0, /* tp_weaklistoffset */
6978 0, /* tp_iter */
6979 0, /* tp_iternext */
6980 unpicklerproxy_methods, /* tp_methods */
6981};
6982
6983static PyObject *
6984UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6985{
6986 UnpicklerMemoProxyObject *self;
6987
6988 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6989 &UnpicklerMemoProxyType);
6990 if (self == NULL)
6991 return NULL;
6992 Py_INCREF(unpickler);
6993 self->unpickler = unpickler;
6994 PyObject_GC_Track(self);
6995 return (PyObject *)self;
6996}
6997
6998/*****************************************************************************/
6999
7000
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007001static PyObject *
7002Unpickler_get_memo(UnpicklerObject *self)
7003{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007004 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007005}
7006
7007static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007008Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007009{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007010 PyObject **new_memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007011 size_t new_memo_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007012
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007013 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007014 PyErr_SetString(PyExc_TypeError,
7015 "attribute deletion is not supported");
7016 return -1;
7017 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007018
7019 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7020 UnpicklerObject *unpickler =
7021 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7022
7023 new_memo_size = unpickler->memo_size;
7024 new_memo = _Unpickler_NewMemo(new_memo_size);
7025 if (new_memo == NULL)
7026 return -1;
7027
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007028 for (size_t i = 0; i < new_memo_size; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007029 Py_XINCREF(unpickler->memo[i]);
7030 new_memo[i] = unpickler->memo[i];
7031 }
7032 }
7033 else if (PyDict_Check(obj)) {
7034 Py_ssize_t i = 0;
7035 PyObject *key, *value;
7036
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007037 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007038 new_memo = _Unpickler_NewMemo(new_memo_size);
7039 if (new_memo == NULL)
7040 return -1;
7041
7042 while (PyDict_Next(obj, &i, &key, &value)) {
7043 Py_ssize_t idx;
7044 if (!PyLong_Check(key)) {
7045 PyErr_SetString(PyExc_TypeError,
7046 "memo key must be integers");
7047 goto error;
7048 }
7049 idx = PyLong_AsSsize_t(key);
7050 if (idx == -1 && PyErr_Occurred())
7051 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007052 if (idx < 0) {
7053 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007054 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007055 goto error;
7056 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007057 if (_Unpickler_MemoPut(self, idx, value) < 0)
7058 goto error;
7059 }
7060 }
7061 else {
7062 PyErr_Format(PyExc_TypeError,
7063 "'memo' attribute must be an UnpicklerMemoProxy object"
7064 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007065 return -1;
7066 }
7067
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007068 _Unpickler_MemoCleanup(self);
7069 self->memo_size = new_memo_size;
7070 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007071
7072 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007073
7074 error:
7075 if (new_memo_size) {
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007076 for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007077 Py_XDECREF(new_memo[i]);
7078 }
7079 PyMem_FREE(new_memo);
7080 }
7081 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007082}
7083
7084static PyObject *
7085Unpickler_get_persload(UnpicklerObject *self)
7086{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007087 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007088 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007089 return NULL;
7090 }
7091 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007092}
7093
7094static int
7095Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
7096{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007097 if (value == NULL) {
7098 PyErr_SetString(PyExc_TypeError,
7099 "attribute deletion is not supported");
7100 return -1;
7101 }
7102 if (!PyCallable_Check(value)) {
7103 PyErr_SetString(PyExc_TypeError,
7104 "persistent_load must be a callable taking "
7105 "one argument");
7106 return -1;
7107 }
7108
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007109 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007110 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007111 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007112
7113 return 0;
7114}
7115
7116static PyGetSetDef Unpickler_getsets[] = {
7117 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7118 {"persistent_load", (getter)Unpickler_get_persload,
7119 (setter)Unpickler_set_persload},
7120 {NULL}
7121};
7122
7123static PyTypeObject Unpickler_Type = {
7124 PyVarObject_HEAD_INIT(NULL, 0)
7125 "_pickle.Unpickler", /*tp_name*/
7126 sizeof(UnpicklerObject), /*tp_basicsize*/
7127 0, /*tp_itemsize*/
7128 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7129 0, /*tp_print*/
7130 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007131 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007132 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007133 0, /*tp_repr*/
7134 0, /*tp_as_number*/
7135 0, /*tp_as_sequence*/
7136 0, /*tp_as_mapping*/
7137 0, /*tp_hash*/
7138 0, /*tp_call*/
7139 0, /*tp_str*/
7140 0, /*tp_getattro*/
7141 0, /*tp_setattro*/
7142 0, /*tp_as_buffer*/
7143 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007144 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007145 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7146 (inquiry)Unpickler_clear, /*tp_clear*/
7147 0, /*tp_richcompare*/
7148 0, /*tp_weaklistoffset*/
7149 0, /*tp_iter*/
7150 0, /*tp_iternext*/
7151 Unpickler_methods, /*tp_methods*/
7152 0, /*tp_members*/
7153 Unpickler_getsets, /*tp_getset*/
7154 0, /*tp_base*/
7155 0, /*tp_dict*/
7156 0, /*tp_descr_get*/
7157 0, /*tp_descr_set*/
7158 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007159 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007160 PyType_GenericAlloc, /*tp_alloc*/
7161 PyType_GenericNew, /*tp_new*/
7162 PyObject_GC_Del, /*tp_free*/
7163 0, /*tp_is_gc*/
7164};
7165
Larry Hastings61272b72014-01-07 12:41:53 -08007166/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007167
7168_pickle.dump
7169
7170 obj: object
7171 file: object
7172 protocol: object = NULL
7173 *
7174 fix_imports: bool = True
7175
7176Write a pickled representation of obj to the open file object file.
7177
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007178This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7179be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007180
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007181The optional *protocol* argument tells the pickler to use the given
7182protocol supported protocols are 0, 1, 2, 3 and 4. The default
7183protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007184
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007185Specifying a negative protocol version selects the highest protocol
7186version supported. The higher the protocol used, the more recent the
7187version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007189The *file* argument must have a write() method that accepts a single
7190bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007191writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007192this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007193
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007194If *fix_imports* is True and protocol is less than 3, pickle will try
7195to map the new Python 3 names to the old module names used in Python
71962, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007197[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007198
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007199static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007200_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007201 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007202/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007203{
7204 PicklerObject *pickler = _Pickler_New();
7205
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007206 if (pickler == NULL)
7207 return NULL;
7208
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007209 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007210 goto error;
7211
7212 if (_Pickler_SetOutputStream(pickler, file) < 0)
7213 goto error;
7214
7215 if (dump(pickler, obj) < 0)
7216 goto error;
7217
7218 if (_Pickler_FlushToFile(pickler) < 0)
7219 goto error;
7220
7221 Py_DECREF(pickler);
7222 Py_RETURN_NONE;
7223
7224 error:
7225 Py_XDECREF(pickler);
7226 return NULL;
7227}
7228
Larry Hastings61272b72014-01-07 12:41:53 -08007229/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007230
7231_pickle.dumps
7232
7233 obj: object
7234 protocol: object = NULL
7235 *
7236 fix_imports: bool = True
7237
7238Return the pickled representation of the object as a bytes object.
7239
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007240The optional *protocol* argument tells the pickler to use the given
7241protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7242protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007243
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007244Specifying a negative protocol version selects the highest protocol
7245version supported. The higher the protocol used, the more recent the
7246version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007247
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007248If *fix_imports* is True and *protocol* is less than 3, pickle will
7249try to map the new Python 3 names to the old module names used in
7250Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007251[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007252
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007253static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007254_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007255 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007256/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007257{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007258 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007259 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007260
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007261 if (pickler == NULL)
7262 return NULL;
7263
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007264 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007265 goto error;
7266
7267 if (dump(pickler, obj) < 0)
7268 goto error;
7269
7270 result = _Pickler_GetString(pickler);
7271 Py_DECREF(pickler);
7272 return result;
7273
7274 error:
7275 Py_XDECREF(pickler);
7276 return NULL;
7277}
7278
Larry Hastings61272b72014-01-07 12:41:53 -08007279/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007280
7281_pickle.load
7282
7283 file: object
7284 *
7285 fix_imports: bool = True
7286 encoding: str = 'ASCII'
7287 errors: str = 'strict'
7288
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007289Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007290
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007291This is equivalent to ``Unpickler(file).load()``, but may be more
7292efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007293
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007294The protocol version of the pickle is detected automatically, so no
7295protocol argument is needed. Bytes past the pickled object's
7296representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007297
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007298The argument *file* must have two methods, a read() method that takes
7299an integer argument, and a readline() method that requires no
7300arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007301binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007302other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007303
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007304Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007305which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007306generated by Python 2. If *fix_imports* is True, pickle will try to
7307map the old Python 2 names to the new names used in Python 3. The
7308*encoding* and *errors* tell pickle how to decode 8-bit string
7309instances pickled by Python 2; these default to 'ASCII' and 'strict',
7310respectively. The *encoding* can be 'bytes' to read these 8-bit
7311string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007312[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007313
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007314static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007315_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007316 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007317/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007318{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007319 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007320 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007321
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007322 if (unpickler == NULL)
7323 return NULL;
7324
7325 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7326 goto error;
7327
7328 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7329 goto error;
7330
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007331 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007332
7333 result = load(unpickler);
7334 Py_DECREF(unpickler);
7335 return result;
7336
7337 error:
7338 Py_XDECREF(unpickler);
7339 return NULL;
7340}
7341
Larry Hastings61272b72014-01-07 12:41:53 -08007342/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007343
7344_pickle.loads
7345
7346 data: object
7347 *
7348 fix_imports: bool = True
7349 encoding: str = 'ASCII'
7350 errors: str = 'strict'
7351
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007352Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007353
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007354The protocol version of the pickle is detected automatically, so no
7355protocol argument is needed. Bytes past the pickled object's
7356representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007357
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007358Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007359which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007360generated by Python 2. If *fix_imports* is True, pickle will try to
7361map the old Python 2 names to the new names used in Python 3. The
7362*encoding* and *errors* tell pickle how to decode 8-bit string
7363instances pickled by Python 2; these default to 'ASCII' and 'strict',
7364respectively. The *encoding* can be 'bytes' to read these 8-bit
7365string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007366[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007367
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007368static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007369_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007370 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007371/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007372{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007373 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007374 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007375
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007376 if (unpickler == NULL)
7377 return NULL;
7378
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007379 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007380 goto error;
7381
7382 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7383 goto error;
7384
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007385 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007386
7387 result = load(unpickler);
7388 Py_DECREF(unpickler);
7389 return result;
7390
7391 error:
7392 Py_XDECREF(unpickler);
7393 return NULL;
7394}
7395
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007396static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007397 _PICKLE_DUMP_METHODDEF
7398 _PICKLE_DUMPS_METHODDEF
7399 _PICKLE_LOAD_METHODDEF
7400 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007401 {NULL, NULL} /* sentinel */
7402};
7403
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007404static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007405pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007406{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007407 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007408 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007409}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007410
Stefan Krahf483b0f2013-12-14 13:43:10 +01007411static void
7412pickle_free(PyObject *m)
7413{
7414 _Pickle_ClearState(_Pickle_GetState(m));
7415}
7416
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007417static int
7418pickle_traverse(PyObject *m, visitproc visit, void *arg)
7419{
7420 PickleState *st = _Pickle_GetState(m);
7421 Py_VISIT(st->PickleError);
7422 Py_VISIT(st->PicklingError);
7423 Py_VISIT(st->UnpicklingError);
7424 Py_VISIT(st->dispatch_table);
7425 Py_VISIT(st->extension_registry);
7426 Py_VISIT(st->extension_cache);
7427 Py_VISIT(st->inverted_registry);
7428 Py_VISIT(st->name_mapping_2to3);
7429 Py_VISIT(st->import_mapping_2to3);
7430 Py_VISIT(st->name_mapping_3to2);
7431 Py_VISIT(st->import_mapping_3to2);
7432 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007433 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007434 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007435}
7436
7437static struct PyModuleDef _picklemodule = {
7438 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007439 "_pickle", /* m_name */
7440 pickle_module_doc, /* m_doc */
7441 sizeof(PickleState), /* m_size */
7442 pickle_methods, /* m_methods */
7443 NULL, /* m_reload */
7444 pickle_traverse, /* m_traverse */
7445 pickle_clear, /* m_clear */
7446 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007447};
7448
7449PyMODINIT_FUNC
7450PyInit__pickle(void)
7451{
7452 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007453 PickleState *st;
7454
7455 m = PyState_FindModule(&_picklemodule);
7456 if (m) {
7457 Py_INCREF(m);
7458 return m;
7459 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007460
7461 if (PyType_Ready(&Unpickler_Type) < 0)
7462 return NULL;
7463 if (PyType_Ready(&Pickler_Type) < 0)
7464 return NULL;
7465 if (PyType_Ready(&Pdata_Type) < 0)
7466 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007467 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7468 return NULL;
7469 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7470 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007471
7472 /* Create the module and add the functions. */
7473 m = PyModule_Create(&_picklemodule);
7474 if (m == NULL)
7475 return NULL;
7476
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007477 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007478 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7479 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007480 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007481 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7482 return NULL;
7483
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007484 st = _Pickle_GetState(m);
7485
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007486 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007487 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7488 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007489 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007490 st->PicklingError = \
7491 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7492 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007493 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007494 st->UnpicklingError = \
7495 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7496 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007497 return NULL;
7498
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007499 Py_INCREF(st->PickleError);
7500 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007501 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007502 Py_INCREF(st->PicklingError);
7503 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007504 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007505 Py_INCREF(st->UnpicklingError);
7506 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007507 return NULL;
7508
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007509 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007510 return NULL;
7511
7512 return m;
7513}