blob: c8b3ef70f52150006fdc91c4e3378d7f43911d65 [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{
210 PyObject *copyreg = NULL;
211 PyObject *compat_pickle = NULL;
212 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300213 PyObject *functools = NULL;
Serhiy Storchaka3cae16d2018-12-11 10:51:27 +0200214 _Py_IDENTIFIER(getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800215
Serhiy Storchaka3cae16d2018-12-11 10:51:27 +0200216 st->getattr = _PyEval_GetBuiltinId(&PyId_getattr);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300217 if (st->getattr == NULL)
218 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300219
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800220 copyreg = PyImport_ImportModule("copyreg");
221 if (!copyreg)
222 goto error;
223 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
224 if (!st->dispatch_table)
225 goto error;
226 if (!PyDict_CheckExact(st->dispatch_table)) {
227 PyErr_Format(PyExc_RuntimeError,
228 "copyreg.dispatch_table should be a dict, not %.200s",
229 Py_TYPE(st->dispatch_table)->tp_name);
230 goto error;
231 }
232 st->extension_registry = \
233 PyObject_GetAttrString(copyreg, "_extension_registry");
234 if (!st->extension_registry)
235 goto error;
236 if (!PyDict_CheckExact(st->extension_registry)) {
237 PyErr_Format(PyExc_RuntimeError,
238 "copyreg._extension_registry should be a dict, "
239 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
240 goto error;
241 }
242 st->inverted_registry = \
243 PyObject_GetAttrString(copyreg, "_inverted_registry");
244 if (!st->inverted_registry)
245 goto error;
246 if (!PyDict_CheckExact(st->inverted_registry)) {
247 PyErr_Format(PyExc_RuntimeError,
248 "copyreg._inverted_registry should be a dict, "
249 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
250 goto error;
251 }
252 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
253 if (!st->extension_cache)
254 goto error;
255 if (!PyDict_CheckExact(st->extension_cache)) {
256 PyErr_Format(PyExc_RuntimeError,
257 "copyreg._extension_cache should be a dict, "
258 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
259 goto error;
260 }
261 Py_CLEAR(copyreg);
262
263 /* Load the 2.x -> 3.x stdlib module mapping tables */
264 compat_pickle = PyImport_ImportModule("_compat_pickle");
265 if (!compat_pickle)
266 goto error;
267 st->name_mapping_2to3 = \
268 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
269 if (!st->name_mapping_2to3)
270 goto error;
271 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
272 PyErr_Format(PyExc_RuntimeError,
273 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
274 Py_TYPE(st->name_mapping_2to3)->tp_name);
275 goto error;
276 }
277 st->import_mapping_2to3 = \
278 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
279 if (!st->import_mapping_2to3)
280 goto error;
281 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
282 PyErr_Format(PyExc_RuntimeError,
283 "_compat_pickle.IMPORT_MAPPING should be a dict, "
284 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
285 goto error;
286 }
287 /* ... and the 3.x -> 2.x mapping tables */
288 st->name_mapping_3to2 = \
289 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
290 if (!st->name_mapping_3to2)
291 goto error;
292 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
293 PyErr_Format(PyExc_RuntimeError,
294 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
295 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
296 goto error;
297 }
298 st->import_mapping_3to2 = \
299 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
300 if (!st->import_mapping_3to2)
301 goto error;
302 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
303 PyErr_Format(PyExc_RuntimeError,
304 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
305 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
306 goto error;
307 }
308 Py_CLEAR(compat_pickle);
309
310 codecs = PyImport_ImportModule("codecs");
311 if (codecs == NULL)
312 goto error;
313 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
314 if (st->codecs_encode == NULL) {
315 goto error;
316 }
317 if (!PyCallable_Check(st->codecs_encode)) {
318 PyErr_Format(PyExc_RuntimeError,
319 "codecs.encode should be a callable, not %.200s",
320 Py_TYPE(st->codecs_encode)->tp_name);
321 goto error;
322 }
323 Py_CLEAR(codecs);
324
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300325 functools = PyImport_ImportModule("functools");
326 if (!functools)
327 goto error;
328 st->partial = PyObject_GetAttrString(functools, "partial");
329 if (!st->partial)
330 goto error;
331 Py_CLEAR(functools);
332
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800333 return 0;
334
335 error:
336 Py_CLEAR(copyreg);
337 Py_CLEAR(compat_pickle);
338 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300339 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800340 _Pickle_ClearState(st);
341 return -1;
342}
343
344/* Helper for calling a function with a single argument quickly.
345
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800346 This function steals the reference of the given argument. */
347static PyObject *
348_Pickle_FastCall(PyObject *func, PyObject *obj)
349{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 PyObject *result;
351
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100352 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200353 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 return result;
355}
356
357/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000358
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200359/* Retrieve and deconstruct a method for avoiding a reference cycle
360 (pickler -> bound method of pickler -> pickler) */
361static int
362init_method_ref(PyObject *self, _Py_Identifier *name,
363 PyObject **method_func, PyObject **method_self)
364{
365 PyObject *func, *func2;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200366 int ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200367
368 /* *method_func and *method_self should be consistent. All refcount decrements
369 should be occurred after setting *method_self and *method_func. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200370 ret = _PyObject_LookupAttrId(self, name, &func);
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200371 if (func == NULL) {
372 *method_self = NULL;
373 Py_CLEAR(*method_func);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200374 return ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200375 }
376
377 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
378 /* Deconstruct a bound Python method */
379 func2 = PyMethod_GET_FUNCTION(func);
380 Py_INCREF(func2);
381 *method_self = self; /* borrowed */
382 Py_XSETREF(*method_func, func2);
383 Py_DECREF(func);
384 return 0;
385 }
386 else {
387 *method_self = NULL;
388 Py_XSETREF(*method_func, func);
389 return 0;
390 }
391}
392
393/* Bind a method if it was deconstructed */
394static PyObject *
395reconstruct_method(PyObject *func, PyObject *self)
396{
397 if (self) {
398 return PyMethod_New(func, self);
399 }
400 else {
401 Py_INCREF(func);
402 return func;
403 }
404}
405
406static PyObject *
407call_method(PyObject *func, PyObject *self, PyObject *obj)
408{
409 if (self) {
410 return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
411 }
412 else {
413 return PyObject_CallFunctionObjArgs(func, obj, NULL);
414 }
415}
416
417/*************************************************************************/
418
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000419/* Internal data type used as the unpickling stack. */
420typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000421 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000422 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200423 int mark_set; /* is MARK set? */
424 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000425 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000426} Pdata;
427
428static void
429Pdata_dealloc(Pdata *self)
430{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200431 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 while (--i >= 0) {
433 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000434 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000435 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436 PyObject_Del(self);
437}
438
439static PyTypeObject Pdata_Type = {
440 PyVarObject_HEAD_INIT(NULL, 0)
441 "_pickle.Pdata", /*tp_name*/
442 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200443 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000444 (destructor)Pdata_dealloc, /*tp_dealloc*/
445};
446
447static PyObject *
448Pdata_New(void)
449{
450 Pdata *self;
451
452 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
453 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000454 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200455 self->mark_set = 0;
456 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000457 self->allocated = 8;
458 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000459 if (self->data)
460 return (PyObject *)self;
461 Py_DECREF(self);
462 return PyErr_NoMemory();
463}
464
465
466/* Retain only the initial clearto items. If clearto >= the current
467 * number of items, this is a (non-erroneous) NOP.
468 */
469static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200470Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000471{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200472 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000473
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200474 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000475 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000476 return 0;
477
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000478 while (--i >= clearto) {
479 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000481 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000482 return 0;
483}
484
485static int
486Pdata_grow(Pdata *self)
487{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000488 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200489 size_t allocated = (size_t)self->allocated;
490 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000491
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000492 new_allocated = (allocated >> 3) + 6;
493 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200494 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000495 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000496 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500497 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000498 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000499 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000500
501 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200502 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000503 return 0;
504
505 nomemory:
506 PyErr_NoMemory();
507 return -1;
508}
509
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200510static int
511Pdata_stack_underflow(Pdata *self)
512{
513 PickleState *st = _Pickle_GetGlobalState();
514 PyErr_SetString(st->UnpicklingError,
515 self->mark_set ?
516 "unexpected MARK found" :
517 "unpickling stack underflow");
518 return -1;
519}
520
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000521/* D is a Pdata*. Pop the topmost element and store it into V, which
522 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
523 * is raised and V is set to NULL.
524 */
525static PyObject *
526Pdata_pop(Pdata *self)
527{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200528 if (Py_SIZE(self) <= self->fence) {
529 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000530 return NULL;
531 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000532 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000533}
534#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
535
536static int
537Pdata_push(Pdata *self, PyObject *obj)
538{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000539 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000540 return -1;
541 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000542 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000543 return 0;
544}
545
546/* Push an object on stack, transferring its ownership to the stack. */
547#define PDATA_PUSH(D, O, ER) do { \
548 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
549
550/* Push an object on stack, adding a new reference to the object. */
551#define PDATA_APPEND(D, O, ER) do { \
552 Py_INCREF((O)); \
553 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
554
555static PyObject *
556Pdata_poptuple(Pdata *self, Py_ssize_t start)
557{
558 PyObject *tuple;
559 Py_ssize_t len, i, j;
560
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200561 if (start < self->fence) {
562 Pdata_stack_underflow(self);
563 return NULL;
564 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000565 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000566 tuple = PyTuple_New(len);
567 if (tuple == NULL)
568 return NULL;
569 for (i = start, j = 0; j < len; i++, j++)
570 PyTuple_SET_ITEM(tuple, j, self->data[i]);
571
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000572 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000573 return tuple;
574}
575
576static PyObject *
577Pdata_poplist(Pdata *self, Py_ssize_t start)
578{
579 PyObject *list;
580 Py_ssize_t len, i, j;
581
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000582 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000583 list = PyList_New(len);
584 if (list == NULL)
585 return NULL;
586 for (i = start, j = 0; j < len; i++, j++)
587 PyList_SET_ITEM(list, j, self->data[i]);
588
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000589 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000590 return list;
591}
592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593typedef struct {
594 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200595 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000596} PyMemoEntry;
597
598typedef struct {
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700599 size_t mt_mask;
600 size_t mt_used;
601 size_t mt_allocated;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000602 PyMemoEntry *mt_table;
603} PyMemoTable;
604
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000605typedef struct PicklerObject {
606 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000607 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000608 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000609 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000610 PyObject *pers_func; /* persistent_id() method, can be NULL */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200611 PyObject *pers_func_self; /* borrowed reference to self if pers_func
612 is an unbound method, NULL otherwise */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100613 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000614
615 PyObject *write; /* write() method of the output stream. */
616 PyObject *output_buffer; /* Write into a local bytearray buffer before
617 flushing to the stream. */
618 Py_ssize_t output_len; /* Length of output_buffer. */
619 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000620 int proto; /* Pickle protocol number, >= 0 */
621 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100622 int framing; /* True when framing is enabled, proto >= 4 */
623 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000624 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100625 is no frame currently open. */
626
627 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000628 int fast; /* Enable fast mode if set to a true value.
629 The fast mode disable the usage of memo,
630 therefore speeding the pickling process by
631 not generating superfluous PUT opcodes. It
632 should not be used if with self-referential
633 objects. */
634 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000635 int fix_imports; /* Indicate whether Pickler should fix
636 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000637 PyObject *fast_memo;
638} PicklerObject;
639
640typedef struct UnpicklerObject {
641 PyObject_HEAD
642 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000643
644 /* The unpickler memo is just an array of PyObject *s. Using a dict
645 is unnecessary, since the keys are contiguous ints. */
646 PyObject **memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700647 size_t memo_size; /* Capacity of the memo array */
648 size_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000649
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000650 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200651 PyObject *pers_func_self; /* borrowed reference to self if pers_func
652 is an unbound method, NULL otherwise */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000653
654 Py_buffer buffer;
655 char *input_buffer;
656 char *input_line;
657 Py_ssize_t input_len;
658 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000659 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100660
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000661 PyObject *read; /* read() method of the input stream. */
662 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000663 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000664
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000665 char *encoding; /* Name of the encoding to be used for
666 decoding strings pickled using Python
667 2.x. The default value is "ASCII" */
668 char *errors; /* Name of errors handling scheme to used when
669 decoding strings. The default value is
670 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500671 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000672 objects. */
673 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
674 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000675 int proto; /* Protocol of the pickle loaded. */
676 int fix_imports; /* Indicate whether Unpickler should fix
677 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000678} UnpicklerObject;
679
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200680typedef struct {
681 PyObject_HEAD
682 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
683} PicklerMemoProxyObject;
684
685typedef struct {
686 PyObject_HEAD
687 UnpicklerObject *unpickler;
688} UnpicklerMemoProxyObject;
689
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000690/* Forward declarations */
691static int save(PicklerObject *, PyObject *, int);
692static int save_reduce(PicklerObject *, PyObject *, PyObject *);
693static PyTypeObject Pickler_Type;
694static PyTypeObject Unpickler_Type;
695
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200696#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000697
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000698/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300699 A custom hashtable mapping void* to Python ints. This is used by the pickler
700 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000701 a bunch of unnecessary object creation. This makes a huge performance
702 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000703
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000704#define MT_MINSIZE 8
705#define PERTURB_SHIFT 5
706
707
708static PyMemoTable *
709PyMemoTable_New(void)
710{
711 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
712 if (memo == NULL) {
713 PyErr_NoMemory();
714 return NULL;
715 }
716
717 memo->mt_used = 0;
718 memo->mt_allocated = MT_MINSIZE;
719 memo->mt_mask = MT_MINSIZE - 1;
720 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
721 if (memo->mt_table == NULL) {
722 PyMem_FREE(memo);
723 PyErr_NoMemory();
724 return NULL;
725 }
726 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
727
728 return memo;
729}
730
731static PyMemoTable *
732PyMemoTable_Copy(PyMemoTable *self)
733{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000734 PyMemoTable *new = PyMemoTable_New();
735 if (new == NULL)
736 return NULL;
737
738 new->mt_used = self->mt_used;
739 new->mt_allocated = self->mt_allocated;
740 new->mt_mask = self->mt_mask;
741 /* The table we get from _New() is probably smaller than we wanted.
742 Free it and allocate one that's the right size. */
743 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500744 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000745 if (new->mt_table == NULL) {
746 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200747 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000748 return NULL;
749 }
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700750 for (size_t i = 0; i < self->mt_allocated; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000751 Py_XINCREF(self->mt_table[i].me_key);
752 }
753 memcpy(new->mt_table, self->mt_table,
754 sizeof(PyMemoEntry) * self->mt_allocated);
755
756 return new;
757}
758
759static Py_ssize_t
760PyMemoTable_Size(PyMemoTable *self)
761{
762 return self->mt_used;
763}
764
765static int
766PyMemoTable_Clear(PyMemoTable *self)
767{
768 Py_ssize_t i = self->mt_allocated;
769
770 while (--i >= 0) {
771 Py_XDECREF(self->mt_table[i].me_key);
772 }
773 self->mt_used = 0;
774 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
775 return 0;
776}
777
778static void
779PyMemoTable_Del(PyMemoTable *self)
780{
781 if (self == NULL)
782 return;
783 PyMemoTable_Clear(self);
784
785 PyMem_FREE(self->mt_table);
786 PyMem_FREE(self);
787}
788
789/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
790 can be considerably simpler than dictobject.c's lookdict(). */
791static PyMemoEntry *
792_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
793{
794 size_t i;
795 size_t perturb;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700796 size_t mask = self->mt_mask;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000797 PyMemoEntry *table = self->mt_table;
798 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000799 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000800
801 i = hash & mask;
802 entry = &table[i];
803 if (entry->me_key == NULL || entry->me_key == key)
804 return entry;
805
806 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
807 i = (i << 2) + i + perturb + 1;
808 entry = &table[i & mask];
809 if (entry->me_key == NULL || entry->me_key == key)
810 return entry;
811 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700812 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000813}
814
815/* Returns -1 on failure, 0 on success. */
816static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700817_PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000818{
819 PyMemoEntry *oldtable = NULL;
820 PyMemoEntry *oldentry, *newentry;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700821 size_t new_size = MT_MINSIZE;
822 size_t to_process;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000823
824 assert(min_size > 0);
825
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700826 if (min_size > PY_SSIZE_T_MAX) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000827 PyErr_NoMemory();
828 return -1;
829 }
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700830
831 /* Find the smallest valid table size >= min_size. */
832 while (new_size < min_size) {
833 new_size <<= 1;
834 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000835 /* new_size needs to be a power of two. */
836 assert((new_size & (new_size - 1)) == 0);
837
838 /* Allocate new table. */
839 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500840 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000841 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200842 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000843 PyErr_NoMemory();
844 return -1;
845 }
846 self->mt_allocated = new_size;
847 self->mt_mask = new_size - 1;
848 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
849
850 /* Copy entries from the old table. */
851 to_process = self->mt_used;
852 for (oldentry = oldtable; to_process > 0; oldentry++) {
853 if (oldentry->me_key != NULL) {
854 to_process--;
855 /* newentry is a pointer to a chunk of the new
856 mt_table, so we're setting the key:value pair
857 in-place. */
858 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
859 newentry->me_key = oldentry->me_key;
860 newentry->me_value = oldentry->me_value;
861 }
862 }
863
864 /* Deallocate the old table. */
865 PyMem_FREE(oldtable);
866 return 0;
867}
868
869/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200870static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000871PyMemoTable_Get(PyMemoTable *self, PyObject *key)
872{
873 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
874 if (entry->me_key == NULL)
875 return NULL;
876 return &entry->me_value;
877}
878
879/* Returns -1 on failure, 0 on success. */
880static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200881PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000882{
883 PyMemoEntry *entry;
884
885 assert(key != NULL);
886
887 entry = _PyMemoTable_Lookup(self, key);
888 if (entry->me_key != NULL) {
889 entry->me_value = value;
890 return 0;
891 }
892 Py_INCREF(key);
893 entry->me_key = key;
894 entry->me_value = value;
895 self->mt_used++;
896
897 /* If we added a key, we can safely resize. Otherwise just return!
898 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
899 *
900 * Quadrupling the size improves average table sparseness
901 * (reducing collisions) at the cost of some memory. It also halves
902 * the number of expensive resize operations in a growing memo table.
903 *
904 * Very large memo tables (over 50K items) use doubling instead.
905 * This may help applications with severe memory constraints.
906 */
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700907 if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000908 return 0;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700909 }
910 // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow.
911 size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used;
912 return _PyMemoTable_ResizeTable(self, desired_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000913}
914
915#undef MT_MINSIZE
916#undef PERTURB_SHIFT
917
918/*************************************************************************/
919
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000920
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000921static int
922_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000923{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300924 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200925 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000926 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000927 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000928 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100929 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000930 return 0;
931}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000932
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100933static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100934_write_size64(char *out, size_t value)
935{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200936 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800937
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200938 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800939
940 for (i = 0; i < sizeof(size_t); i++) {
941 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
942 }
943 for (i = sizeof(size_t); i < 8; i++) {
944 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800945 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100946}
947
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100948static int
949_Pickler_CommitFrame(PicklerObject *self)
950{
951 size_t frame_len;
952 char *qdata;
953
954 if (!self->framing || self->frame_start == -1)
955 return 0;
956 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
957 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200958 if (frame_len >= FRAME_SIZE_MIN) {
959 qdata[0] = FRAME;
960 _write_size64(qdata + 1, frame_len);
961 }
962 else {
963 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
964 self->output_len -= FRAME_HEADER_SIZE;
965 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100966 self->frame_start = -1;
967 return 0;
968}
969
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000970static PyObject *
971_Pickler_GetString(PicklerObject *self)
972{
973 PyObject *output_buffer = self->output_buffer;
974
975 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100976
977 if (_Pickler_CommitFrame(self))
978 return NULL;
979
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000980 self->output_buffer = NULL;
981 /* Resize down to exact size */
982 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
983 return NULL;
984 return output_buffer;
985}
986
987static int
988_Pickler_FlushToFile(PicklerObject *self)
989{
990 PyObject *output, *result;
991
992 assert(self->write != NULL);
993
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100994 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000995 output = _Pickler_GetString(self);
996 if (output == NULL)
997 return -1;
998
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800999 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001000 Py_XDECREF(result);
1001 return (result == NULL) ? -1 : 0;
1002}
1003
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001004static int
1005_Pickler_OpcodeBoundary(PicklerObject *self)
1006{
1007 Py_ssize_t frame_len;
1008
1009 if (!self->framing || self->frame_start == -1) {
1010 return 0;
1011 }
1012 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1013 if (frame_len >= FRAME_SIZE_TARGET) {
1014 if(_Pickler_CommitFrame(self)) {
1015 return -1;
1016 }
Miss Islington (bot)e86db342018-02-03 17:41:43 -08001017 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001018 * file and reuse the pickler buffer for the next frame so as
1019 * to limit memory usage when dumping large complex objects to
1020 * a file.
1021 *
1022 * self->write is NULL when called via dumps.
1023 */
1024 if (self->write != NULL) {
1025 if (_Pickler_FlushToFile(self) < 0) {
1026 return -1;
1027 }
1028 if (_Pickler_ClearBuffer(self) < 0) {
1029 return -1;
1030 }
1031 }
1032 }
1033 return 0;
1034}
1035
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001036static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001037_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001038{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001039 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001040 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001041 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042
1043 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001044 need_new_frame = (self->framing && self->frame_start == -1);
1045
1046 if (need_new_frame)
1047 n = data_len + FRAME_HEADER_SIZE;
1048 else
1049 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050
1051 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001052 if (required > self->max_output_len) {
1053 /* Make place in buffer for the pickle chunk */
1054 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1055 PyErr_NoMemory();
1056 return -1;
1057 }
1058 self->max_output_len = (self->output_len + n) / 2 * 3;
1059 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1060 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001061 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001062 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001063 if (need_new_frame) {
1064 /* Setup new frame */
1065 Py_ssize_t frame_start = self->output_len;
1066 self->frame_start = frame_start;
1067 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1068 /* Write an invalid value, for debugging */
1069 buffer[frame_start + i] = 0xFE;
1070 }
1071 self->output_len += FRAME_HEADER_SIZE;
1072 }
1073 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001074 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001075 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001076 buffer[self->output_len + i] = s[i];
1077 }
1078 }
1079 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001080 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001081 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001082 self->output_len += data_len;
1083 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001084}
1085
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001086static PicklerObject *
1087_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001088{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001089 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001090
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001091 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1092 if (self == NULL)
1093 return NULL;
1094
1095 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001096 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001097 self->write = NULL;
1098 self->proto = 0;
1099 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001100 self->framing = 0;
1101 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001102 self->fast = 0;
1103 self->fast_nesting = 0;
1104 self->fix_imports = 0;
1105 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001106 self->max_output_len = WRITE_BUF_SIZE;
1107 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001108
1109 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001110 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1111 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001112
1113 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001114 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001115 return NULL;
1116 }
Miss Islington (bot)c0f6f532019-04-23 05:18:15 -07001117
1118 PyObject_GC_Track(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001119 return self;
1120}
1121
1122static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001123_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001124{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001125 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001126
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001127 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001129 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001131 proto = PyLong_AsLong(protocol);
1132 if (proto < 0) {
1133 if (proto == -1 && PyErr_Occurred())
1134 return -1;
1135 proto = HIGHEST_PROTOCOL;
1136 }
1137 else if (proto > HIGHEST_PROTOCOL) {
1138 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1139 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001140 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001141 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001142 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001143 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144 self->bin = proto > 0;
1145 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001146 return 0;
1147}
1148
1149/* Returns -1 (with an exception set) on failure, 0 on success. This may
1150 be called once on a freshly created Pickler. */
1151static int
1152_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1153{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001154 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001155 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001156 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1157 return -1;
1158 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001159 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001160 PyErr_SetString(PyExc_TypeError,
1161 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001162 return -1;
1163 }
1164
1165 return 0;
1166}
1167
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001168/* Returns the size of the input on success, -1 on failure. This takes its
1169 own reference to `input`. */
1170static Py_ssize_t
1171_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1172{
1173 if (self->buffer.buf != NULL)
1174 PyBuffer_Release(&self->buffer);
1175 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1176 return -1;
1177 self->input_buffer = self->buffer.buf;
1178 self->input_len = self->buffer.len;
1179 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001180 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001181 return self->input_len;
1182}
1183
Antoine Pitrou04248a82010-10-12 20:51:21 +00001184static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001185bad_readline(void)
1186{
1187 PickleState *st = _Pickle_GetGlobalState();
1188 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1189 return -1;
1190}
1191
1192static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001193_Unpickler_SkipConsumed(UnpicklerObject *self)
1194{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001195 Py_ssize_t consumed;
1196 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001197
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001198 consumed = self->next_read_idx - self->prefetched_idx;
1199 if (consumed <= 0)
1200 return 0;
1201
1202 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001203 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001204 r = PyObject_CallFunction(self->read, "n", consumed);
1205 if (r == NULL)
1206 return -1;
1207 Py_DECREF(r);
1208
1209 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001210 return 0;
1211}
1212
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001213static const Py_ssize_t READ_WHOLE_LINE = -1;
1214
1215/* If reading from a file, we need to only pull the bytes we need, since there
1216 may be multiple pickle objects arranged contiguously in the same input
1217 buffer.
1218
1219 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1220 bytes from the input stream/buffer.
1221
1222 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1223 failure; on success, returns the number of bytes read from the file.
1224
1225 On success, self->input_len will be 0; this is intentional so that when
1226 unpickling from a file, the "we've run out of data" code paths will trigger,
1227 causing the Unpickler to go back to the file for more data. Use the returned
1228 size to tell you how much data you can process. */
1229static Py_ssize_t
1230_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1231{
1232 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001233 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001234
1235 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001236
Antoine Pitrou04248a82010-10-12 20:51:21 +00001237 if (_Unpickler_SkipConsumed(self) < 0)
1238 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001239
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001240 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001241 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001242 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001243 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001244 PyObject *len;
1245 /* Prefetch some data without advancing the file pointer, if possible */
1246 if (self->peek && n < PREFETCH) {
1247 len = PyLong_FromSsize_t(PREFETCH);
1248 if (len == NULL)
1249 return -1;
1250 data = _Pickle_FastCall(self->peek, len);
1251 if (data == NULL) {
1252 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1253 return -1;
1254 /* peek() is probably not supported by the given file object */
1255 PyErr_Clear();
1256 Py_CLEAR(self->peek);
1257 }
1258 else {
1259 read_size = _Unpickler_SetStringInput(self, data);
1260 Py_DECREF(data);
1261 self->prefetched_idx = 0;
1262 if (n <= read_size)
1263 return n;
1264 }
1265 }
1266 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001267 if (len == NULL)
1268 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001269 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001270 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001271 if (data == NULL)
1272 return -1;
1273
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001274 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001275 Py_DECREF(data);
1276 return read_size;
1277}
1278
Victor Stinner19ed27e2016-05-20 11:42:37 +02001279/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001280static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001281_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001282{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001283 Py_ssize_t num_read;
1284
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001285 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001286 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1287 PickleState *st = _Pickle_GetGlobalState();
1288 PyErr_SetString(st->UnpicklingError,
1289 "read would overflow (invalid bytecode)");
1290 return -1;
1291 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001292
1293 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1294 assert(self->next_read_idx + n > self->input_len);
1295
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001296 if (!self->read)
1297 return bad_readline();
1298
Antoine Pitrou04248a82010-10-12 20:51:21 +00001299 num_read = _Unpickler_ReadFromFile(self, n);
1300 if (num_read < 0)
1301 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001302 if (num_read < n)
1303 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001304 *s = self->input_buffer;
1305 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001306 return n;
1307}
1308
Victor Stinner19ed27e2016-05-20 11:42:37 +02001309/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1310
1311 This should be used for all data reads, rather than accessing the unpickler's
1312 input buffer directly. This method deals correctly with reading from input
1313 streams, which the input buffer doesn't deal with.
1314
1315 Note that when reading from a file-like object, self->next_read_idx won't
1316 be updated (it should remain at 0 for the entire unpickling process). You
1317 should use this function's return value to know how many bytes you can
1318 consume.
1319
1320 Returns -1 (with an exception set) on failure. On success, return the
1321 number of chars read. */
1322#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001323 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001324 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1325 (self)->next_read_idx += (n), \
1326 (n)) \
1327 : _Unpickler_ReadImpl(self, (s), (n)))
1328
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001329static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001330_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1331 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001332{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001333 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001334 if (input_line == NULL) {
1335 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001336 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001337 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001338
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001339 memcpy(input_line, line, len);
1340 input_line[len] = '\0';
1341 self->input_line = input_line;
1342 *result = self->input_line;
1343 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001344}
1345
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001346/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001347 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001348
1349 Returns the number of chars read, or -1 on failure. */
1350static Py_ssize_t
1351_Unpickler_Readline(UnpicklerObject *self, char **result)
1352{
1353 Py_ssize_t i, num_read;
1354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001355 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001356 if (self->input_buffer[i] == '\n') {
1357 char *line_start = self->input_buffer + self->next_read_idx;
1358 num_read = i - self->next_read_idx + 1;
1359 self->next_read_idx = i + 1;
1360 return _Unpickler_CopyLine(self, line_start, num_read, result);
1361 }
1362 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001363 if (!self->read)
1364 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001365
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001366 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1367 if (num_read < 0)
1368 return -1;
1369 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1370 return bad_readline();
1371 self->next_read_idx = num_read;
1372 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001373}
1374
1375/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1376 will be modified in place. */
1377static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001378_Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001379{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001380 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001381
1382 assert(new_size > self->memo_size);
1383
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001384 PyObject **memo_new = self->memo;
1385 PyMem_RESIZE(memo_new, PyObject *, new_size);
1386 if (memo_new == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001387 PyErr_NoMemory();
1388 return -1;
1389 }
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001390 self->memo = memo_new;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001391 for (i = self->memo_size; i < new_size; i++)
1392 self->memo[i] = NULL;
1393 self->memo_size = new_size;
1394 return 0;
1395}
1396
1397/* Returns NULL if idx is out of bounds. */
1398static PyObject *
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001399_Unpickler_MemoGet(UnpicklerObject *self, size_t idx)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001400{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001401 if (idx >= self->memo_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001402 return NULL;
1403
1404 return self->memo[idx];
1405}
1406
1407/* Returns -1 (with an exception set) on failure, 0 on success.
1408 This takes its own reference to `value`. */
1409static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001410_Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001411{
1412 PyObject *old_item;
1413
1414 if (idx >= self->memo_size) {
1415 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1416 return -1;
1417 assert(idx < self->memo_size);
1418 }
1419 Py_INCREF(value);
1420 old_item = self->memo[idx];
1421 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001422 if (old_item != NULL) {
1423 Py_DECREF(old_item);
1424 }
1425 else {
1426 self->memo_len++;
1427 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001428 return 0;
1429}
1430
1431static PyObject **
1432_Unpickler_NewMemo(Py_ssize_t new_size)
1433{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001434 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001435 if (memo == NULL) {
1436 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001437 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001438 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001439 memset(memo, 0, new_size * sizeof(PyObject *));
1440 return memo;
1441}
1442
1443/* Free the unpickler's memo, taking care to decref any items left in it. */
1444static void
1445_Unpickler_MemoCleanup(UnpicklerObject *self)
1446{
1447 Py_ssize_t i;
1448 PyObject **memo = self->memo;
1449
1450 if (self->memo == NULL)
1451 return;
1452 self->memo = NULL;
1453 i = self->memo_size;
1454 while (--i >= 0) {
1455 Py_XDECREF(memo[i]);
1456 }
1457 PyMem_FREE(memo);
1458}
1459
1460static UnpicklerObject *
1461_Unpickler_New(void)
1462{
1463 UnpicklerObject *self;
1464
1465 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1466 if (self == NULL)
1467 return NULL;
1468
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001469 self->pers_func = NULL;
1470 self->input_buffer = NULL;
1471 self->input_line = NULL;
1472 self->input_len = 0;
1473 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001474 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001475 self->read = NULL;
1476 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001477 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001478 self->encoding = NULL;
1479 self->errors = NULL;
1480 self->marks = NULL;
1481 self->num_marks = 0;
1482 self->marks_size = 0;
1483 self->proto = 0;
1484 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001485 memset(&self->buffer, 0, sizeof(Py_buffer));
1486 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001487 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001488 self->memo = _Unpickler_NewMemo(self->memo_size);
1489 self->stack = (Pdata *)Pdata_New();
1490
1491 if (self->memo == NULL || self->stack == NULL) {
1492 Py_DECREF(self);
1493 return NULL;
1494 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001495
Miss Islington (bot)c0f6f532019-04-23 05:18:15 -07001496 PyObject_GC_Track(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001497 return self;
1498}
1499
1500/* Returns -1 (with an exception set) on failure, 0 on success. This may
1501 be called once on a freshly created Pickler. */
1502static int
1503_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1504{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001505 _Py_IDENTIFIER(peek);
1506 _Py_IDENTIFIER(read);
1507 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001508
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001509 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1510 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001511 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001512 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1513 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001514 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001515 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001516 PyErr_SetString(PyExc_TypeError,
1517 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001518 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001519 Py_CLEAR(self->read);
1520 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001521 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001522 return -1;
1523 }
1524 return 0;
1525}
1526
1527/* Returns -1 (with an exception set) on failure, 0 on success. This may
1528 be called once on a freshly created Pickler. */
1529static int
1530_Unpickler_SetInputEncoding(UnpicklerObject *self,
1531 const char *encoding,
1532 const char *errors)
1533{
1534 if (encoding == NULL)
1535 encoding = "ASCII";
1536 if (errors == NULL)
1537 errors = "strict";
1538
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001539 self->encoding = _PyMem_Strdup(encoding);
1540 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001541 if (self->encoding == NULL || self->errors == NULL) {
1542 PyErr_NoMemory();
1543 return -1;
1544 }
1545 return 0;
1546}
1547
1548/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001549static int
1550memo_get(PicklerObject *self, PyObject *key)
1551{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001552 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001553 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001554 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001555
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001556 value = PyMemoTable_Get(self->memo, key);
1557 if (value == NULL) {
1558 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001559 return -1;
1560 }
1561
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001562 if (!self->bin) {
1563 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001564 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1565 "%" PY_FORMAT_SIZE_T "d\n", *value);
1566 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001567 }
1568 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001569 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001570 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001571 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001572 len = 2;
1573 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001574 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001575 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001576 pdata[1] = (unsigned char)(*value & 0xff);
1577 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1578 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1579 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001580 len = 5;
1581 }
1582 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001583 PickleState *st = _Pickle_GetGlobalState();
1584 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001585 "memo id too large for LONG_BINGET");
1586 return -1;
1587 }
1588 }
1589
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001590 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001591 return -1;
1592
1593 return 0;
1594}
1595
1596/* Store an object in the memo, assign it a new unique ID based on the number
1597 of objects currently stored in the memo and generate a PUT opcode. */
1598static int
1599memo_put(PicklerObject *self, PyObject *obj)
1600{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001601 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001602 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001603 Py_ssize_t idx;
1604
1605 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001606
1607 if (self->fast)
1608 return 0;
1609
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001610 idx = PyMemoTable_Size(self->memo);
1611 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1612 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001613
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001614 if (self->proto >= 4) {
1615 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1616 return -1;
1617 return 0;
1618 }
1619 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001620 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001621 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001622 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001623 len = strlen(pdata);
1624 }
1625 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001626 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001627 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001628 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001629 len = 2;
1630 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001631 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001632 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001633 pdata[1] = (unsigned char)(idx & 0xff);
1634 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1635 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1636 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001637 len = 5;
1638 }
1639 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001640 PickleState *st = _Pickle_GetGlobalState();
1641 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001642 "memo id too large for LONG_BINPUT");
1643 return -1;
1644 }
1645 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001646 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001647 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001648
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001649 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001650}
1651
1652static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001653get_dotted_path(PyObject *obj, PyObject *name)
1654{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001655 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001656 PyObject *dotted_path;
1657 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001658
1659 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001660 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001661 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001662 n = PyList_GET_SIZE(dotted_path);
1663 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001664 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001665 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001666 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001667 if (obj == NULL)
1668 PyErr_Format(PyExc_AttributeError,
1669 "Can't pickle local object %R", name);
1670 else
1671 PyErr_Format(PyExc_AttributeError,
1672 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001673 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001674 return NULL;
1675 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001676 }
1677 return dotted_path;
1678}
1679
1680static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001681get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001682{
1683 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001684 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001685
1686 assert(PyList_CheckExact(names));
1687 Py_INCREF(obj);
1688 n = PyList_GET_SIZE(names);
1689 for (i = 0; i < n; i++) {
1690 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001691 Py_XDECREF(parent);
1692 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001693 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001694 if (obj == NULL) {
1695 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001696 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001697 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001698 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001699 if (pparent != NULL)
1700 *pparent = parent;
1701 else
1702 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001703 return obj;
1704}
1705
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001706
1707static PyObject *
1708getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1709{
1710 PyObject *dotted_path, *attr;
1711
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001712 if (allow_qualname) {
1713 dotted_path = get_dotted_path(obj, name);
1714 if (dotted_path == NULL)
1715 return NULL;
1716 attr = get_deep_attribute(obj, dotted_path, NULL);
1717 Py_DECREF(dotted_path);
1718 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001719 else {
1720 (void)_PyObject_LookupAttr(obj, name, &attr);
1721 }
1722 if (attr == NULL && !PyErr_Occurred()) {
1723 PyErr_Format(PyExc_AttributeError,
1724 "Can't get attribute %R on %R", name, obj);
1725 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001726 return attr;
1727}
1728
Eric Snow3f9eee62017-09-15 16:35:20 -06001729static int
1730_checkmodule(PyObject *module_name, PyObject *module,
1731 PyObject *global, PyObject *dotted_path)
1732{
1733 if (module == Py_None) {
1734 return -1;
1735 }
1736 if (PyUnicode_Check(module_name) &&
1737 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1738 return -1;
1739 }
1740
1741 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1742 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001743 return -1;
1744 }
1745 if (candidate != global) {
1746 Py_DECREF(candidate);
1747 return -1;
1748 }
1749 Py_DECREF(candidate);
1750 return 0;
1751}
1752
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001753static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001754whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001755{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001756 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001757 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001758 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001759 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001760 _Py_IDENTIFIER(__module__);
1761 _Py_IDENTIFIER(modules);
1762 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001763
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001764 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1765 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001766 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001767 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001768 /* In some rare cases (e.g., bound methods of extension types),
1769 __module__ can be None. If it is so, then search sys.modules for
1770 the module of global. */
1771 if (module_name != Py_None)
1772 return module_name;
1773 Py_CLEAR(module_name);
1774 }
1775 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001776
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001777 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001778 modules = _PySys_GetObjectId(&PyId_modules);
1779 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001780 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001781 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001782 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001783 if (PyDict_CheckExact(modules)) {
1784 i = 0;
1785 while (PyDict_Next(modules, &i, &module_name, &module)) {
1786 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1787 Py_INCREF(module_name);
1788 return module_name;
1789 }
1790 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001791 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001792 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001793 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001794 }
1795 else {
1796 PyObject *iterator = PyObject_GetIter(modules);
1797 if (iterator == NULL) {
1798 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001799 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001800 while ((module_name = PyIter_Next(iterator))) {
1801 module = PyObject_GetItem(modules, module_name);
1802 if (module == NULL) {
1803 Py_DECREF(module_name);
1804 Py_DECREF(iterator);
1805 return NULL;
1806 }
1807 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1808 Py_DECREF(module);
1809 Py_DECREF(iterator);
1810 return module_name;
1811 }
1812 Py_DECREF(module);
1813 Py_DECREF(module_name);
1814 if (PyErr_Occurred()) {
1815 Py_DECREF(iterator);
1816 return NULL;
1817 }
1818 }
1819 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001820 }
1821
1822 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001823 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001824 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001825 return module_name;
1826}
1827
1828/* fast_save_enter() and fast_save_leave() are guards against recursive
1829 objects when Pickler is used with the "fast mode" (i.e., with object
1830 memoization disabled). If the nesting of a list or dict object exceed
1831 FAST_NESTING_LIMIT, these guards will start keeping an internal
1832 reference to the seen list or dict objects and check whether these objects
1833 are recursive. These are not strictly necessary, since save() has a
1834 hard-coded recursion limit, but they give a nicer error message than the
1835 typical RuntimeError. */
1836static int
1837fast_save_enter(PicklerObject *self, PyObject *obj)
1838{
1839 /* if fast_nesting < 0, we're doing an error exit. */
1840 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1841 PyObject *key = NULL;
1842 if (self->fast_memo == NULL) {
1843 self->fast_memo = PyDict_New();
1844 if (self->fast_memo == NULL) {
1845 self->fast_nesting = -1;
1846 return 0;
1847 }
1848 }
1849 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001850 if (key == NULL) {
1851 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001852 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001853 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001854 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001855 Py_DECREF(key);
1856 PyErr_Format(PyExc_ValueError,
1857 "fast mode: can't pickle cyclic objects "
1858 "including object type %.200s at %p",
1859 obj->ob_type->tp_name, obj);
1860 self->fast_nesting = -1;
1861 return 0;
1862 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001863 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001864 Py_DECREF(key);
1865 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001866 return 0;
1867 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001868 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1869 Py_DECREF(key);
1870 self->fast_nesting = -1;
1871 return 0;
1872 }
1873 Py_DECREF(key);
1874 }
1875 return 1;
1876}
1877
1878static int
1879fast_save_leave(PicklerObject *self, PyObject *obj)
1880{
1881 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1882 PyObject *key = PyLong_FromVoidPtr(obj);
1883 if (key == NULL)
1884 return 0;
1885 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1886 Py_DECREF(key);
1887 return 0;
1888 }
1889 Py_DECREF(key);
1890 }
1891 return 1;
1892}
1893
1894static int
1895save_none(PicklerObject *self, PyObject *obj)
1896{
1897 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001898 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001899 return -1;
1900
1901 return 0;
1902}
1903
1904static int
1905save_bool(PicklerObject *self, PyObject *obj)
1906{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001907 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001908 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001909 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001910 return -1;
1911 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001912 else {
1913 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1914 * so that unpicklers written before bools were introduced unpickle them
1915 * as ints, but unpicklers after can recognize that bools were intended.
1916 * Note that protocol 2 added direct ways to pickle bools.
1917 */
1918 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1919 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1920 return -1;
1921 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001922 return 0;
1923}
1924
1925static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001926save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001927{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001928 PyObject *repr = NULL;
1929 Py_ssize_t size;
1930 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001931 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001932 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001933
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001934 val= PyLong_AsLongAndOverflow(obj, &overflow);
1935 if (!overflow && (sizeof(long) <= 4 ||
1936 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1937 {
Larry Hastings61272b72014-01-07 12:41:53 -08001938 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001939
1940 Note: we can't use -0x80000000L in the above condition because some
1941 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1942 before applying the unary minus when sizeof(long) <= 4. The
1943 resulting value stays unsigned which is commonly not what we want,
1944 so MSVC happily warns us about it. However, that result would have
1945 been fine because we guard for sizeof(long) <= 4 which turns the
1946 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001947 char pdata[32];
1948 Py_ssize_t len = 0;
1949
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001950 if (self->bin) {
1951 pdata[1] = (unsigned char)(val & 0xff);
1952 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1953 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1954 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001955
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001956 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1957 pdata[0] = BININT;
1958 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001959 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001960 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001961 pdata[0] = BININT2;
1962 len = 3;
1963 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001964 else {
1965 pdata[0] = BININT1;
1966 len = 2;
1967 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001968 }
1969 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001970 sprintf(pdata, "%c%ld\n", INT, val);
1971 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001972 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001973 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001974 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001975
1976 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001977 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001978 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001979
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001980 if (self->proto >= 2) {
1981 /* Linear-time pickling. */
1982 size_t nbits;
1983 size_t nbytes;
1984 unsigned char *pdata;
1985 char header[5];
1986 int i;
1987 int sign = _PyLong_Sign(obj);
1988
1989 if (sign == 0) {
1990 header[0] = LONG1;
1991 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001992 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001993 goto error;
1994 return 0;
1995 }
1996 nbits = _PyLong_NumBits(obj);
1997 if (nbits == (size_t)-1 && PyErr_Occurred())
1998 goto error;
1999 /* How many bytes do we need? There are nbits >> 3 full
2000 * bytes of data, and nbits & 7 leftover bits. If there
2001 * are any leftover bits, then we clearly need another
2002 * byte. Wnat's not so obvious is that we *probably*
2003 * need another byte even if there aren't any leftovers:
2004 * the most-significant bit of the most-significant byte
2005 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002006 * opposite of the one we need. The exception is ints
2007 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002008 * its own 256's-complement, so has the right sign bit
2009 * even without the extra byte. That's a pain to check
2010 * for in advance, though, so we always grab an extra
2011 * byte at the start, and cut it back later if possible.
2012 */
2013 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002014 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002015 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002016 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002017 goto error;
2018 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002019 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002020 if (repr == NULL)
2021 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002022 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002023 i = _PyLong_AsByteArray((PyLongObject *)obj,
2024 pdata, nbytes,
2025 1 /* little endian */ , 1 /* signed */ );
2026 if (i < 0)
2027 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002028 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002029 * needed. This is so iff the MSB is all redundant sign
2030 * bits.
2031 */
2032 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002033 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002034 pdata[nbytes - 1] == 0xff &&
2035 (pdata[nbytes - 2] & 0x80) != 0) {
2036 nbytes--;
2037 }
2038
2039 if (nbytes < 256) {
2040 header[0] = LONG1;
2041 header[1] = (unsigned char)nbytes;
2042 size = 2;
2043 }
2044 else {
2045 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002046 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002047 for (i = 1; i < 5; i++) {
2048 header[i] = (unsigned char)(size & 0xff);
2049 size >>= 8;
2050 }
2051 size = 5;
2052 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002053 if (_Pickler_Write(self, header, size) < 0 ||
2054 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002055 goto error;
2056 }
2057 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002058 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002059 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002060
Mark Dickinson8dd05142009-01-20 20:43:58 +00002061 /* proto < 2: write the repr and newline. This is quadratic-time (in
2062 the number of digits), in both directions. We add a trailing 'L'
2063 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002064
2065 repr = PyObject_Repr(obj);
2066 if (repr == NULL)
2067 goto error;
2068
Serhiy Storchaka06515832016-11-20 09:13:07 +02002069 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002070 if (string == NULL)
2071 goto error;
2072
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002073 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2074 _Pickler_Write(self, string, size) < 0 ||
2075 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 goto error;
2077 }
2078
2079 if (0) {
2080 error:
2081 status = -1;
2082 }
2083 Py_XDECREF(repr);
2084
2085 return status;
2086}
2087
2088static int
2089save_float(PicklerObject *self, PyObject *obj)
2090{
2091 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2092
2093 if (self->bin) {
2094 char pdata[9];
2095 pdata[0] = BINFLOAT;
2096 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2097 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002098 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002100 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002101 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002102 int result = -1;
2103 char *buf = NULL;
2104 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002105
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002106 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002107 goto done;
2108
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002109 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002110 if (!buf) {
2111 PyErr_NoMemory();
2112 goto done;
2113 }
2114
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002115 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002116 goto done;
2117
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002118 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002119 goto done;
2120
2121 result = 0;
2122done:
2123 PyMem_Free(buf);
2124 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002125 }
2126
2127 return 0;
2128}
2129
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002130/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002131
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002132 The large contiguous data is written directly into the underlying file
2133 object, bypassing the output_buffer of the Pickler. We intentionally
2134 do not insert a protocol 4 frame opcode to make it possible to optimize
2135 file.read calls in the loader.
2136 */
2137static int
2138_Pickler_write_bytes(PicklerObject *self,
2139 const char *header, Py_ssize_t header_size,
2140 const char *data, Py_ssize_t data_size,
2141 PyObject *payload)
2142{
2143 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2144 int framing = self->framing;
2145
2146 if (bypass_buffer) {
2147 assert(self->output_buffer != NULL);
2148 /* Commit the previous frame. */
2149 if (_Pickler_CommitFrame(self)) {
2150 return -1;
2151 }
2152 /* Disable framing temporarily */
2153 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002154 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002155
2156 if (_Pickler_Write(self, header, header_size) < 0) {
2157 return -1;
2158 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002159
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002160 if (bypass_buffer && self->write != NULL) {
2161 /* Bypass the in-memory buffer to directly stream large data
2162 into the underlying file object. */
2163 PyObject *result, *mem = NULL;
2164 /* Dump the output buffer to the file. */
2165 if (_Pickler_FlushToFile(self) < 0) {
2166 return -1;
2167 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002168
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002169 /* Stream write the payload into the file without going through the
2170 output buffer. */
2171 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002172 /* TODO: It would be better to use a memoryview with a linked
2173 original string if this is possible. */
2174 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002175 if (payload == NULL) {
2176 return -1;
2177 }
2178 }
2179 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2180 Py_XDECREF(mem);
2181 if (result == NULL) {
2182 return -1;
2183 }
2184 Py_DECREF(result);
2185
2186 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2187 if (_Pickler_ClearBuffer(self) < 0) {
2188 return -1;
2189 }
2190 }
2191 else {
2192 if (_Pickler_Write(self, data, data_size) < 0) {
2193 return -1;
2194 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002195 }
2196
2197 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002198 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002199
2200 return 0;
2201}
2202
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002203static int
2204save_bytes(PicklerObject *self, PyObject *obj)
2205{
2206 if (self->proto < 3) {
2207 /* Older pickle protocols do not have an opcode for pickling bytes
2208 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002209 the __reduce__ method) to permit bytes object unpickling.
2210
2211 Here we use a hack to be compatible with Python 2. Since in Python
2212 2 'bytes' is just an alias for 'str' (which has different
2213 parameters than the actual bytes object), we use codecs.encode
2214 to create the appropriate 'str' object when unpickled using
2215 Python 2 *and* the appropriate 'bytes' object when unpickled
2216 using Python 3. Again this is a hack and we don't need to do this
2217 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002218 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002219 int status;
2220
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002221 if (PyBytes_GET_SIZE(obj) == 0) {
2222 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2223 }
2224 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002225 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002226 PyObject *unicode_str =
2227 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2228 PyBytes_GET_SIZE(obj),
2229 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002230 _Py_IDENTIFIER(latin1);
2231
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002232 if (unicode_str == NULL)
2233 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002234 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002235 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002236 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002237 Py_DECREF(unicode_str);
2238 }
2239
2240 if (reduce_value == NULL)
2241 return -1;
2242
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002243 /* save_reduce() will memoize the object automatically. */
2244 status = save_reduce(self, reduce_value, obj);
2245 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002246 return status;
2247 }
2248 else {
2249 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002250 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002251 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002253 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002254 if (size < 0)
2255 return -1;
2256
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002257 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002258 header[0] = SHORT_BINBYTES;
2259 header[1] = (unsigned char)size;
2260 len = 2;
2261 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002262 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002263 header[0] = BINBYTES;
2264 header[1] = (unsigned char)(size & 0xff);
2265 header[2] = (unsigned char)((size >> 8) & 0xff);
2266 header[3] = (unsigned char)((size >> 16) & 0xff);
2267 header[4] = (unsigned char)((size >> 24) & 0xff);
2268 len = 5;
2269 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002270 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002271 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002272 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002273 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002274 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002275 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002276 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002277 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002278 return -1; /* string too large */
2279 }
2280
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002281 if (_Pickler_write_bytes(self, header, len,
2282 PyBytes_AS_STRING(obj), size, obj) < 0)
2283 {
2284 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002285 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002286
2287 if (memo_put(self, obj) < 0)
2288 return -1;
2289
2290 return 0;
2291 }
2292}
2293
2294/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2295 backslash and newline characters to \uXXXX escapes. */
2296static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002297raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002298{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002299 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002300 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002301 void *data;
2302 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002303 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002304
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002305 if (PyUnicode_READY(obj))
2306 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002307
Victor Stinner358af132015-10-12 22:36:57 +02002308 _PyBytesWriter_Init(&writer);
2309
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002310 size = PyUnicode_GET_LENGTH(obj);
2311 data = PyUnicode_DATA(obj);
2312 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002313
Victor Stinner358af132015-10-12 22:36:57 +02002314 p = _PyBytesWriter_Alloc(&writer, size);
2315 if (p == NULL)
2316 goto error;
2317 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002318
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002319 for (i=0; i < size; i++) {
2320 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002321 /* Map 32-bit characters to '\Uxxxxxxxx' */
2322 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002323 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002324 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2325 if (p == NULL)
2326 goto error;
2327
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002328 *p++ = '\\';
2329 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002330 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2331 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2332 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2333 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2334 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2335 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2336 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2337 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002338 }
Victor Stinner358af132015-10-12 22:36:57 +02002339 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002340 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002341 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002342 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2343 if (p == NULL)
2344 goto error;
2345
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002346 *p++ = '\\';
2347 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002348 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2349 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2350 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2351 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002352 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002353 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002354 else
2355 *p++ = (char) ch;
2356 }
Victor Stinner358af132015-10-12 22:36:57 +02002357
2358 return _PyBytesWriter_Finish(&writer, p);
2359
2360error:
2361 _PyBytesWriter_Dealloc(&writer);
2362 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002363}
2364
2365static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002366write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002367{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002368 char header[9];
2369 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002370 PyObject *encoded = NULL;
2371 Py_ssize_t size;
2372 const char *data;
2373
2374 if (PyUnicode_READY(obj))
2375 return -1;
2376
2377 data = PyUnicode_AsUTF8AndSize(obj, &size);
2378 if (data == NULL) {
2379 /* Issue #8383: for strings with lone surrogates, fallback on the
2380 "surrogatepass" error handler. */
2381 PyErr_Clear();
2382 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2383 if (encoded == NULL)
2384 return -1;
2385
2386 data = PyBytes_AS_STRING(encoded);
2387 size = PyBytes_GET_SIZE(encoded);
2388 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002389
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002390 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002391 if (size <= 0xff && self->proto >= 4) {
2392 header[0] = SHORT_BINUNICODE;
2393 header[1] = (unsigned char)(size & 0xff);
2394 len = 2;
2395 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002396 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002397 header[0] = BINUNICODE;
2398 header[1] = (unsigned char)(size & 0xff);
2399 header[2] = (unsigned char)((size >> 8) & 0xff);
2400 header[3] = (unsigned char)((size >> 16) & 0xff);
2401 header[4] = (unsigned char)((size >> 24) & 0xff);
2402 len = 5;
2403 }
2404 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002405 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002406 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002407 len = 9;
2408 }
2409 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002410 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002411 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002412 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002413 return -1;
2414 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002415
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002416 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2417 Py_XDECREF(encoded);
2418 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002419 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002420 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002421 return 0;
2422}
2423
2424static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002425save_unicode(PicklerObject *self, PyObject *obj)
2426{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002427 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002428 if (write_unicode_binary(self, obj) < 0)
2429 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002430 }
2431 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002432 PyObject *encoded;
2433 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002434 const char unicode_op = UNICODE;
2435
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002436 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002437 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002438 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002439
Antoine Pitrou299978d2013-04-07 17:38:11 +02002440 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2441 Py_DECREF(encoded);
2442 return -1;
2443 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002444
2445 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002446 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2447 Py_DECREF(encoded);
2448 return -1;
2449 }
2450 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002451
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002452 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002453 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002454 }
2455 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002456 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002457
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002458 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002459}
2460
2461/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2462static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002463store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002464{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002465 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002466
2467 assert(PyTuple_Size(t) == len);
2468
2469 for (i = 0; i < len; i++) {
2470 PyObject *element = PyTuple_GET_ITEM(t, i);
2471
2472 if (element == NULL)
2473 return -1;
2474 if (save(self, element, 0) < 0)
2475 return -1;
2476 }
2477
2478 return 0;
2479}
2480
2481/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2482 * used across protocols to minimize the space needed to pickle them.
2483 * Tuples are also the only builtin immutable type that can be recursive
2484 * (a tuple can be reached from itself), and that requires some subtle
2485 * magic so that it works in all cases. IOW, this is a long routine.
2486 */
2487static int
2488save_tuple(PicklerObject *self, PyObject *obj)
2489{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002490 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002491
2492 const char mark_op = MARK;
2493 const char tuple_op = TUPLE;
2494 const char pop_op = POP;
2495 const char pop_mark_op = POP_MARK;
2496 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2497
2498 if ((len = PyTuple_Size(obj)) < 0)
2499 return -1;
2500
2501 if (len == 0) {
2502 char pdata[2];
2503
2504 if (self->proto) {
2505 pdata[0] = EMPTY_TUPLE;
2506 len = 1;
2507 }
2508 else {
2509 pdata[0] = MARK;
2510 pdata[1] = TUPLE;
2511 len = 2;
2512 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002513 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002514 return -1;
2515 return 0;
2516 }
2517
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002518 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002519 * saving the tuple elements, the tuple must be recursive, in
2520 * which case we'll pop everything we put on the stack, and fetch
2521 * its value from the memo.
2522 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002523 if (len <= 3 && self->proto >= 2) {
2524 /* Use TUPLE{1,2,3} opcodes. */
2525 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002526 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002527
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002528 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002529 /* pop the len elements */
2530 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002531 if (_Pickler_Write(self, &pop_op, 1) < 0)
2532 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002533 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002534 if (memo_get(self, obj) < 0)
2535 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002536
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002537 return 0;
2538 }
2539 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002540 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2541 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002542 }
2543 goto memoize;
2544 }
2545
2546 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2547 * Generate MARK e1 e2 ... TUPLE
2548 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002549 if (_Pickler_Write(self, &mark_op, 1) < 0)
2550 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002551
2552 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002553 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002554
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002555 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556 /* pop the stack stuff we pushed */
2557 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002558 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2559 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002560 }
2561 else {
2562 /* Note that we pop one more than len, to remove
2563 * the MARK too.
2564 */
2565 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002566 if (_Pickler_Write(self, &pop_op, 1) < 0)
2567 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002568 }
2569 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002570 if (memo_get(self, obj) < 0)
2571 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002572
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002573 return 0;
2574 }
2575 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002576 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2577 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002578 }
2579
2580 memoize:
2581 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002582 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002583
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002584 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002585}
2586
2587/* iter is an iterator giving items, and we batch up chunks of
2588 * MARK item item ... item APPENDS
2589 * opcode sequences. Calling code should have arranged to first create an
2590 * empty list, or list-like object, for the APPENDS to operate on.
2591 * Returns 0 on success, <0 on error.
2592 */
2593static int
2594batch_list(PicklerObject *self, PyObject *iter)
2595{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002596 PyObject *obj = NULL;
2597 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002598 int i, n;
2599
2600 const char mark_op = MARK;
2601 const char append_op = APPEND;
2602 const char appends_op = APPENDS;
2603
2604 assert(iter != NULL);
2605
2606 /* XXX: I think this function could be made faster by avoiding the
2607 iterator interface and fetching objects directly from list using
2608 PyList_GET_ITEM.
2609 */
2610
2611 if (self->proto == 0) {
2612 /* APPENDS isn't available; do one at a time. */
2613 for (;;) {
2614 obj = PyIter_Next(iter);
2615 if (obj == NULL) {
2616 if (PyErr_Occurred())
2617 return -1;
2618 break;
2619 }
2620 i = save(self, obj, 0);
2621 Py_DECREF(obj);
2622 if (i < 0)
2623 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002624 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002625 return -1;
2626 }
2627 return 0;
2628 }
2629
2630 /* proto > 0: write in batches of BATCHSIZE. */
2631 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002632 /* Get first item */
2633 firstitem = PyIter_Next(iter);
2634 if (firstitem == NULL) {
2635 if (PyErr_Occurred())
2636 goto error;
2637
2638 /* nothing more to add */
2639 break;
2640 }
2641
2642 /* Try to get a second item */
2643 obj = PyIter_Next(iter);
2644 if (obj == NULL) {
2645 if (PyErr_Occurred())
2646 goto error;
2647
2648 /* Only one item to write */
2649 if (save(self, firstitem, 0) < 0)
2650 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002651 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002652 goto error;
2653 Py_CLEAR(firstitem);
2654 break;
2655 }
2656
2657 /* More than one item to write */
2658
2659 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002660 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002661 goto error;
2662
2663 if (save(self, firstitem, 0) < 0)
2664 goto error;
2665 Py_CLEAR(firstitem);
2666 n = 1;
2667
2668 /* Fetch and save up to BATCHSIZE items */
2669 while (obj) {
2670 if (save(self, obj, 0) < 0)
2671 goto error;
2672 Py_CLEAR(obj);
2673 n += 1;
2674
2675 if (n == BATCHSIZE)
2676 break;
2677
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002678 obj = PyIter_Next(iter);
2679 if (obj == NULL) {
2680 if (PyErr_Occurred())
2681 goto error;
2682 break;
2683 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002684 }
2685
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002686 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002687 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002688
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002689 } while (n == BATCHSIZE);
2690 return 0;
2691
2692 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002693 Py_XDECREF(firstitem);
2694 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002695 return -1;
2696}
2697
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002698/* This is a variant of batch_list() above, specialized for lists (with no
2699 * support for list subclasses). Like batch_list(), we batch up chunks of
2700 * MARK item item ... item APPENDS
2701 * opcode sequences. Calling code should have arranged to first create an
2702 * empty list, or list-like object, for the APPENDS to operate on.
2703 * Returns 0 on success, -1 on error.
2704 *
2705 * This version is considerably faster than batch_list(), if less general.
2706 *
2707 * Note that this only works for protocols > 0.
2708 */
2709static int
2710batch_list_exact(PicklerObject *self, PyObject *obj)
2711{
2712 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002713 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002714
2715 const char append_op = APPEND;
2716 const char appends_op = APPENDS;
2717 const char mark_op = MARK;
2718
2719 assert(obj != NULL);
2720 assert(self->proto > 0);
2721 assert(PyList_CheckExact(obj));
2722
2723 if (PyList_GET_SIZE(obj) == 1) {
2724 item = PyList_GET_ITEM(obj, 0);
2725 if (save(self, item, 0) < 0)
2726 return -1;
2727 if (_Pickler_Write(self, &append_op, 1) < 0)
2728 return -1;
2729 return 0;
2730 }
2731
2732 /* Write in batches of BATCHSIZE. */
2733 total = 0;
2734 do {
2735 this_batch = 0;
2736 if (_Pickler_Write(self, &mark_op, 1) < 0)
2737 return -1;
2738 while (total < PyList_GET_SIZE(obj)) {
2739 item = PyList_GET_ITEM(obj, total);
2740 if (save(self, item, 0) < 0)
2741 return -1;
2742 total++;
2743 if (++this_batch == BATCHSIZE)
2744 break;
2745 }
2746 if (_Pickler_Write(self, &appends_op, 1) < 0)
2747 return -1;
2748
2749 } while (total < PyList_GET_SIZE(obj));
2750
2751 return 0;
2752}
2753
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002754static int
2755save_list(PicklerObject *self, PyObject *obj)
2756{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002757 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002758 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002759 int status = 0;
2760
2761 if (self->fast && !fast_save_enter(self, obj))
2762 goto error;
2763
2764 /* Create an empty list. */
2765 if (self->bin) {
2766 header[0] = EMPTY_LIST;
2767 len = 1;
2768 }
2769 else {
2770 header[0] = MARK;
2771 header[1] = LIST;
2772 len = 2;
2773 }
2774
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002775 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002776 goto error;
2777
2778 /* Get list length, and bow out early if empty. */
2779 if ((len = PyList_Size(obj)) < 0)
2780 goto error;
2781
2782 if (memo_put(self, obj) < 0)
2783 goto error;
2784
2785 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002786 /* Materialize the list elements. */
2787 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002788 if (Py_EnterRecursiveCall(" while pickling an object"))
2789 goto error;
2790 status = batch_list_exact(self, obj);
2791 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002792 } else {
2793 PyObject *iter = PyObject_GetIter(obj);
2794 if (iter == NULL)
2795 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002796
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002797 if (Py_EnterRecursiveCall(" while pickling an object")) {
2798 Py_DECREF(iter);
2799 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002800 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002801 status = batch_list(self, iter);
2802 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002803 Py_DECREF(iter);
2804 }
2805 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002806 if (0) {
2807 error:
2808 status = -1;
2809 }
2810
2811 if (self->fast && !fast_save_leave(self, obj))
2812 status = -1;
2813
2814 return status;
2815}
2816
2817/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2818 * MARK key value ... key value SETITEMS
2819 * opcode sequences. Calling code should have arranged to first create an
2820 * empty dict, or dict-like object, for the SETITEMS to operate on.
2821 * Returns 0 on success, <0 on error.
2822 *
2823 * This is very much like batch_list(). The difference between saving
2824 * elements directly, and picking apart two-tuples, is so long-winded at
2825 * the C level, though, that attempts to combine these routines were too
2826 * ugly to bear.
2827 */
2828static int
2829batch_dict(PicklerObject *self, PyObject *iter)
2830{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002831 PyObject *obj = NULL;
2832 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002833 int i, n;
2834
2835 const char mark_op = MARK;
2836 const char setitem_op = SETITEM;
2837 const char setitems_op = SETITEMS;
2838
2839 assert(iter != NULL);
2840
2841 if (self->proto == 0) {
2842 /* SETITEMS isn't available; do one at a time. */
2843 for (;;) {
2844 obj = PyIter_Next(iter);
2845 if (obj == NULL) {
2846 if (PyErr_Occurred())
2847 return -1;
2848 break;
2849 }
2850 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2851 PyErr_SetString(PyExc_TypeError, "dict items "
2852 "iterator must return 2-tuples");
2853 return -1;
2854 }
2855 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2856 if (i >= 0)
2857 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2858 Py_DECREF(obj);
2859 if (i < 0)
2860 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002861 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002862 return -1;
2863 }
2864 return 0;
2865 }
2866
2867 /* proto > 0: write in batches of BATCHSIZE. */
2868 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002869 /* Get first item */
2870 firstitem = PyIter_Next(iter);
2871 if (firstitem == NULL) {
2872 if (PyErr_Occurred())
2873 goto error;
2874
2875 /* nothing more to add */
2876 break;
2877 }
2878 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2879 PyErr_SetString(PyExc_TypeError, "dict items "
2880 "iterator must return 2-tuples");
2881 goto error;
2882 }
2883
2884 /* Try to get a second item */
2885 obj = PyIter_Next(iter);
2886 if (obj == NULL) {
2887 if (PyErr_Occurred())
2888 goto error;
2889
2890 /* Only one item to write */
2891 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2892 goto error;
2893 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2894 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002895 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002896 goto error;
2897 Py_CLEAR(firstitem);
2898 break;
2899 }
2900
2901 /* More than one item to write */
2902
2903 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002904 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002905 goto error;
2906
2907 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2908 goto error;
2909 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2910 goto error;
2911 Py_CLEAR(firstitem);
2912 n = 1;
2913
2914 /* Fetch and save up to BATCHSIZE items */
2915 while (obj) {
2916 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2917 PyErr_SetString(PyExc_TypeError, "dict items "
2918 "iterator must return 2-tuples");
2919 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002920 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002921 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2922 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2923 goto error;
2924 Py_CLEAR(obj);
2925 n += 1;
2926
2927 if (n == BATCHSIZE)
2928 break;
2929
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002930 obj = PyIter_Next(iter);
2931 if (obj == NULL) {
2932 if (PyErr_Occurred())
2933 goto error;
2934 break;
2935 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002936 }
2937
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002938 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002939 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002940
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002941 } while (n == BATCHSIZE);
2942 return 0;
2943
2944 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002945 Py_XDECREF(firstitem);
2946 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002947 return -1;
2948}
2949
Collin Winter5c9b02d2009-05-25 05:43:30 +00002950/* This is a variant of batch_dict() above that specializes for dicts, with no
2951 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2952 * MARK key value ... key value SETITEMS
2953 * opcode sequences. Calling code should have arranged to first create an
2954 * empty dict, or dict-like object, for the SETITEMS to operate on.
2955 * Returns 0 on success, -1 on error.
2956 *
2957 * Note that this currently doesn't work for protocol 0.
2958 */
2959static int
2960batch_dict_exact(PicklerObject *self, PyObject *obj)
2961{
2962 PyObject *key = NULL, *value = NULL;
2963 int i;
2964 Py_ssize_t dict_size, ppos = 0;
2965
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002966 const char mark_op = MARK;
2967 const char setitem_op = SETITEM;
2968 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002969
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002970 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002971 assert(self->proto > 0);
2972
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002973 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002974
2975 /* Special-case len(d) == 1 to save space. */
2976 if (dict_size == 1) {
2977 PyDict_Next(obj, &ppos, &key, &value);
2978 if (save(self, key, 0) < 0)
2979 return -1;
2980 if (save(self, value, 0) < 0)
2981 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002982 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002983 return -1;
2984 return 0;
2985 }
2986
2987 /* Write in batches of BATCHSIZE. */
2988 do {
2989 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002990 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002991 return -1;
2992 while (PyDict_Next(obj, &ppos, &key, &value)) {
2993 if (save(self, key, 0) < 0)
2994 return -1;
2995 if (save(self, value, 0) < 0)
2996 return -1;
2997 if (++i == BATCHSIZE)
2998 break;
2999 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003000 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003001 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003002 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003003 PyErr_Format(
3004 PyExc_RuntimeError,
3005 "dictionary changed size during iteration");
3006 return -1;
3007 }
3008
3009 } while (i == BATCHSIZE);
3010 return 0;
3011}
3012
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003013static int
3014save_dict(PicklerObject *self, PyObject *obj)
3015{
3016 PyObject *items, *iter;
3017 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003018 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003019 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003020 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003021
3022 if (self->fast && !fast_save_enter(self, obj))
3023 goto error;
3024
3025 /* Create an empty dict. */
3026 if (self->bin) {
3027 header[0] = EMPTY_DICT;
3028 len = 1;
3029 }
3030 else {
3031 header[0] = MARK;
3032 header[1] = DICT;
3033 len = 2;
3034 }
3035
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003036 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003037 goto error;
3038
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003039 if (memo_put(self, obj) < 0)
3040 goto error;
3041
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003042 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003043 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003044 if (PyDict_CheckExact(obj) && self->proto > 0) {
3045 /* We can take certain shortcuts if we know this is a dict and
3046 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003047 if (Py_EnterRecursiveCall(" while pickling an object"))
3048 goto error;
3049 status = batch_dict_exact(self, obj);
3050 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003051 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003052 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003053
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003054 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003055 if (items == NULL)
3056 goto error;
3057 iter = PyObject_GetIter(items);
3058 Py_DECREF(items);
3059 if (iter == NULL)
3060 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003061 if (Py_EnterRecursiveCall(" while pickling an object")) {
3062 Py_DECREF(iter);
3063 goto error;
3064 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003065 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003066 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003067 Py_DECREF(iter);
3068 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003069 }
3070
3071 if (0) {
3072 error:
3073 status = -1;
3074 }
3075
3076 if (self->fast && !fast_save_leave(self, obj))
3077 status = -1;
3078
3079 return status;
3080}
3081
3082static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003083save_set(PicklerObject *self, PyObject *obj)
3084{
3085 PyObject *item;
3086 int i;
3087 Py_ssize_t set_size, ppos = 0;
3088 Py_hash_t hash;
3089
3090 const char empty_set_op = EMPTY_SET;
3091 const char mark_op = MARK;
3092 const char additems_op = ADDITEMS;
3093
3094 if (self->proto < 4) {
3095 PyObject *items;
3096 PyObject *reduce_value;
3097 int status;
3098
3099 items = PySequence_List(obj);
3100 if (items == NULL) {
3101 return -1;
3102 }
3103 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3104 Py_DECREF(items);
3105 if (reduce_value == NULL) {
3106 return -1;
3107 }
3108 /* save_reduce() will memoize the object automatically. */
3109 status = save_reduce(self, reduce_value, obj);
3110 Py_DECREF(reduce_value);
3111 return status;
3112 }
3113
3114 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3115 return -1;
3116
3117 if (memo_put(self, obj) < 0)
3118 return -1;
3119
3120 set_size = PySet_GET_SIZE(obj);
3121 if (set_size == 0)
3122 return 0; /* nothing to do */
3123
3124 /* Write in batches of BATCHSIZE. */
3125 do {
3126 i = 0;
3127 if (_Pickler_Write(self, &mark_op, 1) < 0)
3128 return -1;
3129 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3130 if (save(self, item, 0) < 0)
3131 return -1;
3132 if (++i == BATCHSIZE)
3133 break;
3134 }
3135 if (_Pickler_Write(self, &additems_op, 1) < 0)
3136 return -1;
3137 if (PySet_GET_SIZE(obj) != set_size) {
3138 PyErr_Format(
3139 PyExc_RuntimeError,
3140 "set changed size during iteration");
3141 return -1;
3142 }
3143 } while (i == BATCHSIZE);
3144
3145 return 0;
3146}
3147
3148static int
3149save_frozenset(PicklerObject *self, PyObject *obj)
3150{
3151 PyObject *iter;
3152
3153 const char mark_op = MARK;
3154 const char frozenset_op = FROZENSET;
3155
3156 if (self->fast && !fast_save_enter(self, obj))
3157 return -1;
3158
3159 if (self->proto < 4) {
3160 PyObject *items;
3161 PyObject *reduce_value;
3162 int status;
3163
3164 items = PySequence_List(obj);
3165 if (items == NULL) {
3166 return -1;
3167 }
3168 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3169 items);
3170 Py_DECREF(items);
3171 if (reduce_value == NULL) {
3172 return -1;
3173 }
3174 /* save_reduce() will memoize the object automatically. */
3175 status = save_reduce(self, reduce_value, obj);
3176 Py_DECREF(reduce_value);
3177 return status;
3178 }
3179
3180 if (_Pickler_Write(self, &mark_op, 1) < 0)
3181 return -1;
3182
3183 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003184 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003185 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003186 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003187 for (;;) {
3188 PyObject *item;
3189
3190 item = PyIter_Next(iter);
3191 if (item == NULL) {
3192 if (PyErr_Occurred()) {
3193 Py_DECREF(iter);
3194 return -1;
3195 }
3196 break;
3197 }
3198 if (save(self, item, 0) < 0) {
3199 Py_DECREF(item);
3200 Py_DECREF(iter);
3201 return -1;
3202 }
3203 Py_DECREF(item);
3204 }
3205 Py_DECREF(iter);
3206
3207 /* If the object is already in the memo, this means it is
3208 recursive. In this case, throw away everything we put on the
3209 stack, and fetch the object back from the memo. */
3210 if (PyMemoTable_Get(self->memo, obj)) {
3211 const char pop_mark_op = POP_MARK;
3212
3213 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3214 return -1;
3215 if (memo_get(self, obj) < 0)
3216 return -1;
3217 return 0;
3218 }
3219
3220 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3221 return -1;
3222 if (memo_put(self, obj) < 0)
3223 return -1;
3224
3225 return 0;
3226}
3227
3228static int
3229fix_imports(PyObject **module_name, PyObject **global_name)
3230{
3231 PyObject *key;
3232 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003233 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003234
3235 key = PyTuple_Pack(2, *module_name, *global_name);
3236 if (key == NULL)
3237 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003238 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003239 Py_DECREF(key);
3240 if (item) {
3241 PyObject *fixed_module_name;
3242 PyObject *fixed_global_name;
3243
3244 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3245 PyErr_Format(PyExc_RuntimeError,
3246 "_compat_pickle.REVERSE_NAME_MAPPING values "
3247 "should be 2-tuples, not %.200s",
3248 Py_TYPE(item)->tp_name);
3249 return -1;
3250 }
3251 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3252 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3253 if (!PyUnicode_Check(fixed_module_name) ||
3254 !PyUnicode_Check(fixed_global_name)) {
3255 PyErr_Format(PyExc_RuntimeError,
3256 "_compat_pickle.REVERSE_NAME_MAPPING values "
3257 "should be pairs of str, not (%.200s, %.200s)",
3258 Py_TYPE(fixed_module_name)->tp_name,
3259 Py_TYPE(fixed_global_name)->tp_name);
3260 return -1;
3261 }
3262
3263 Py_CLEAR(*module_name);
3264 Py_CLEAR(*global_name);
3265 Py_INCREF(fixed_module_name);
3266 Py_INCREF(fixed_global_name);
3267 *module_name = fixed_module_name;
3268 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003269 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003270 }
3271 else if (PyErr_Occurred()) {
3272 return -1;
3273 }
3274
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003275 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003276 if (item) {
3277 if (!PyUnicode_Check(item)) {
3278 PyErr_Format(PyExc_RuntimeError,
3279 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3280 "should be strings, not %.200s",
3281 Py_TYPE(item)->tp_name);
3282 return -1;
3283 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003284 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003285 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003286 }
3287 else if (PyErr_Occurred()) {
3288 return -1;
3289 }
3290
3291 return 0;
3292}
3293
3294static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003295save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3296{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003297 PyObject *global_name = NULL;
3298 PyObject *module_name = NULL;
3299 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003300 PyObject *parent = NULL;
3301 PyObject *dotted_path = NULL;
3302 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003303 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003304 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003305 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003306 _Py_IDENTIFIER(__name__);
3307 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003308
3309 const char global_op = GLOBAL;
3310
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003311 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003312 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003313 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003314 }
3315 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003316 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3317 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003318 if (global_name == NULL) {
3319 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3320 if (global_name == NULL)
3321 goto error;
3322 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003323 }
3324
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003325 dotted_path = get_dotted_path(module, global_name);
3326 if (dotted_path == NULL)
3327 goto error;
3328 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003329 if (module_name == NULL)
3330 goto error;
3331
3332 /* XXX: Change to use the import C API directly with level=0 to disallow
3333 relative imports.
3334
3335 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3336 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3337 custom import functions (IMHO, this would be a nice security
3338 feature). The import C API would need to be extended to support the
3339 extra parameters of __import__ to fix that. */
3340 module = PyImport_Import(module_name);
3341 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003342 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003343 "Can't pickle %R: import of module %R failed",
3344 obj, module_name);
3345 goto error;
3346 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003347 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3348 Py_INCREF(lastname);
3349 cls = get_deep_attribute(module, dotted_path, &parent);
3350 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003351 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003352 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003353 "Can't pickle %R: attribute lookup %S on %S failed",
3354 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003355 goto error;
3356 }
3357 if (cls != obj) {
3358 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003359 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003360 "Can't pickle %R: it's not the same object as %S.%S",
3361 obj, module_name, global_name);
3362 goto error;
3363 }
3364 Py_DECREF(cls);
3365
3366 if (self->proto >= 2) {
3367 /* See whether this is in the extension registry, and if
3368 * so generate an EXT opcode.
3369 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003370 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003371 PyObject *code_obj; /* extension code as Python object */
3372 long code; /* extension code as C value */
3373 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003374 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003375
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003376 extension_key = PyTuple_Pack(2, module_name, global_name);
3377 if (extension_key == NULL) {
3378 goto error;
3379 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003380 code_obj = PyDict_GetItemWithError(st->extension_registry,
3381 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003382 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003383 /* The object is not registered in the extension registry.
3384 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003385 if (code_obj == NULL) {
3386 if (PyErr_Occurred()) {
3387 goto error;
3388 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003389 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003390 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003391
3392 /* XXX: pickle.py doesn't check neither the type, nor the range
3393 of the value returned by the extension_registry. It should for
3394 consistency. */
3395
3396 /* Verify code_obj has the right type and value. */
3397 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003398 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003399 "Can't pickle %R: extension code %R isn't an integer",
3400 obj, code_obj);
3401 goto error;
3402 }
3403 code = PyLong_AS_LONG(code_obj);
3404 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003405 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003406 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3407 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003408 goto error;
3409 }
3410
3411 /* Generate an EXT opcode. */
3412 if (code <= 0xff) {
3413 pdata[0] = EXT1;
3414 pdata[1] = (unsigned char)code;
3415 n = 2;
3416 }
3417 else if (code <= 0xffff) {
3418 pdata[0] = EXT2;
3419 pdata[1] = (unsigned char)(code & 0xff);
3420 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3421 n = 3;
3422 }
3423 else {
3424 pdata[0] = EXT4;
3425 pdata[1] = (unsigned char)(code & 0xff);
3426 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3427 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3428 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3429 n = 5;
3430 }
3431
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003432 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003433 goto error;
3434 }
3435 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003436 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003437 if (parent == module) {
3438 Py_INCREF(lastname);
3439 Py_DECREF(global_name);
3440 global_name = lastname;
3441 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003442 if (self->proto >= 4) {
3443 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003444
Christian Heimese8b1ba12013-11-23 21:13:39 +01003445 if (save(self, module_name, 0) < 0)
3446 goto error;
3447 if (save(self, global_name, 0) < 0)
3448 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003449
3450 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3451 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003452 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003453 else if (parent != module) {
3454 PickleState *st = _Pickle_GetGlobalState();
3455 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3456 st->getattr, parent, lastname);
Miss Islington (bot)3152bc32018-08-22 01:54:33 -04003457 if (reduce_value == NULL)
3458 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003459 status = save_reduce(self, reduce_value, NULL);
3460 Py_DECREF(reduce_value);
3461 if (status < 0)
3462 goto error;
3463 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003464 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003465 /* Generate a normal global opcode if we are using a pickle
3466 protocol < 4, or if the object is not registered in the
3467 extension registry. */
3468 PyObject *encoded;
3469 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003470
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003471 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003472 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003473
3474 /* For protocol < 3 and if the user didn't request against doing
3475 so, we convert module names to the old 2.x module names. */
3476 if (self->proto < 3 && self->fix_imports) {
3477 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003478 goto error;
3479 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003480 }
3481
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003482 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3483 both the module name and the global name using UTF-8. We do so
3484 only when we are using the pickle protocol newer than version
3485 3. This is to ensure compatibility with older Unpickler running
3486 on Python 2.x. */
3487 if (self->proto == 3) {
3488 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003489 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003490 else {
3491 unicode_encoder = PyUnicode_AsASCIIString;
3492 }
3493 encoded = unicode_encoder(module_name);
3494 if (encoded == NULL) {
3495 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003496 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003497 "can't pickle module identifier '%S' using "
3498 "pickle protocol %i",
3499 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003500 goto error;
3501 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003502 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3503 PyBytes_GET_SIZE(encoded)) < 0) {
3504 Py_DECREF(encoded);
3505 goto error;
3506 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003507 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003508 if(_Pickler_Write(self, "\n", 1) < 0)
3509 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003510
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003511 /* Save the name of the module. */
3512 encoded = unicode_encoder(global_name);
3513 if (encoded == NULL) {
3514 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003515 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003516 "can't pickle global identifier '%S' using "
3517 "pickle protocol %i",
3518 global_name, self->proto);
3519 goto error;
3520 }
3521 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3522 PyBytes_GET_SIZE(encoded)) < 0) {
3523 Py_DECREF(encoded);
3524 goto error;
3525 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003526 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003527 if (_Pickler_Write(self, "\n", 1) < 0)
3528 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003529 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003530 /* Memoize the object. */
3531 if (memo_put(self, obj) < 0)
3532 goto error;
3533 }
3534
3535 if (0) {
3536 error:
3537 status = -1;
3538 }
3539 Py_XDECREF(module_name);
3540 Py_XDECREF(global_name);
3541 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003542 Py_XDECREF(parent);
3543 Py_XDECREF(dotted_path);
3544 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003545
3546 return status;
3547}
3548
3549static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003550save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3551{
3552 PyObject *reduce_value;
3553 int status;
3554
3555 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3556 if (reduce_value == NULL) {
3557 return -1;
3558 }
3559 status = save_reduce(self, reduce_value, obj);
3560 Py_DECREF(reduce_value);
3561 return status;
3562}
3563
3564static int
3565save_type(PicklerObject *self, PyObject *obj)
3566{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003567 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003568 return save_singleton_type(self, obj, Py_None);
3569 }
3570 else if (obj == (PyObject *)&PyEllipsis_Type) {
3571 return save_singleton_type(self, obj, Py_Ellipsis);
3572 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003573 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003574 return save_singleton_type(self, obj, Py_NotImplemented);
3575 }
3576 return save_global(self, obj, NULL);
3577}
3578
3579static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003580save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003581{
3582 PyObject *pid = NULL;
3583 int status = 0;
3584
3585 const char persid_op = PERSID;
3586 const char binpersid_op = BINPERSID;
3587
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003588 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003589 if (pid == NULL)
3590 return -1;
3591
3592 if (pid != Py_None) {
3593 if (self->bin) {
3594 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003595 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003596 goto error;
3597 }
3598 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003599 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003600
3601 pid_str = PyObject_Str(pid);
3602 if (pid_str == NULL)
3603 goto error;
3604
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003605 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003606 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003607 if (!PyUnicode_IS_ASCII(pid_str)) {
3608 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3609 "persistent IDs in protocol 0 must be "
3610 "ASCII strings");
3611 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003612 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003613 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003614
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003615 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003616 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3617 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3618 _Pickler_Write(self, "\n", 1) < 0) {
3619 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003620 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003621 }
3622 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003623 }
3624 status = 1;
3625 }
3626
3627 if (0) {
3628 error:
3629 status = -1;
3630 }
3631 Py_XDECREF(pid);
3632
3633 return status;
3634}
3635
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003636static PyObject *
3637get_class(PyObject *obj)
3638{
3639 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003640 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003641
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003642 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3643 cls = (PyObject *) Py_TYPE(obj);
3644 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003645 }
3646 return cls;
3647}
3648
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003649/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3650 * appropriate __reduce__ method for obj.
3651 */
3652static int
3653save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3654{
3655 PyObject *callable;
3656 PyObject *argtup;
3657 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003658 PyObject *listitems = Py_None;
3659 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003660 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003661 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003662 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003663
3664 const char reduce_op = REDUCE;
3665 const char build_op = BUILD;
3666 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003667 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003668
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003669 size = PyTuple_Size(args);
3670 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003671 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003672 "__reduce__ must contain 2 through 5 elements");
3673 return -1;
3674 }
3675
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003676 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3677 &callable, &argtup, &state, &listitems, &dictitems))
3678 return -1;
3679
3680 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003681 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003682 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003683 return -1;
3684 }
3685 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003686 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003687 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003688 return -1;
3689 }
3690
3691 if (state == Py_None)
3692 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003693
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003694 if (listitems == Py_None)
3695 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003696 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003697 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003698 "returned by __reduce__ must be an iterator, not %s",
3699 Py_TYPE(listitems)->tp_name);
3700 return -1;
3701 }
3702
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003703 if (dictitems == Py_None)
3704 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003705 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003706 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003707 "returned by __reduce__ must be an iterator, not %s",
3708 Py_TYPE(dictitems)->tp_name);
3709 return -1;
3710 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003711
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003712 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003713 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003714 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003715
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003716 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3717 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003718 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003719 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003720 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003721 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3722 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003723 if (!use_newobj_ex) {
3724 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003725 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003726 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003727 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003728 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003729 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003730
3731 if (use_newobj_ex) {
3732 PyObject *cls;
3733 PyObject *args;
3734 PyObject *kwargs;
3735
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003736 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003737 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003738 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003739 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003740 return -1;
3741 }
3742
3743 cls = PyTuple_GET_ITEM(argtup, 0);
3744 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003745 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003746 "first item from NEWOBJ_EX argument tuple must "
3747 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3748 return -1;
3749 }
3750 args = PyTuple_GET_ITEM(argtup, 1);
3751 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003752 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003753 "second item from NEWOBJ_EX argument tuple must "
3754 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3755 return -1;
3756 }
3757 kwargs = PyTuple_GET_ITEM(argtup, 2);
3758 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003759 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003760 "third item from NEWOBJ_EX argument tuple must "
3761 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3762 return -1;
3763 }
3764
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003765 if (self->proto >= 4) {
3766 if (save(self, cls, 0) < 0 ||
3767 save(self, args, 0) < 0 ||
3768 save(self, kwargs, 0) < 0 ||
3769 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3770 return -1;
3771 }
3772 }
3773 else {
3774 PyObject *newargs;
3775 PyObject *cls_new;
3776 Py_ssize_t i;
3777 _Py_IDENTIFIER(__new__);
3778
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003779 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003780 if (newargs == NULL)
3781 return -1;
3782
3783 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3784 if (cls_new == NULL) {
3785 Py_DECREF(newargs);
3786 return -1;
3787 }
3788 PyTuple_SET_ITEM(newargs, 0, cls_new);
3789 Py_INCREF(cls);
3790 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003791 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003792 PyObject *item = PyTuple_GET_ITEM(args, i);
3793 Py_INCREF(item);
3794 PyTuple_SET_ITEM(newargs, i + 2, item);
3795 }
3796
3797 callable = PyObject_Call(st->partial, newargs, kwargs);
3798 Py_DECREF(newargs);
3799 if (callable == NULL)
3800 return -1;
3801
3802 newargs = PyTuple_New(0);
3803 if (newargs == NULL) {
3804 Py_DECREF(callable);
3805 return -1;
3806 }
3807
3808 if (save(self, callable, 0) < 0 ||
3809 save(self, newargs, 0) < 0 ||
3810 _Pickler_Write(self, &reduce_op, 1) < 0) {
3811 Py_DECREF(newargs);
3812 Py_DECREF(callable);
3813 return -1;
3814 }
3815 Py_DECREF(newargs);
3816 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003817 }
3818 }
3819 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003820 PyObject *cls;
3821 PyObject *newargtup;
3822 PyObject *obj_class;
3823 int p;
3824
3825 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003826 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003827 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003828 return -1;
3829 }
3830
3831 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003832 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003833 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003834 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003835 return -1;
3836 }
3837
3838 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003839 obj_class = get_class(obj);
Miss Islington (bot)e2f376f2018-12-05 11:35:41 -08003840 if (obj_class == NULL) {
3841 return -1;
3842 }
3843 p = obj_class != cls;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003844 Py_DECREF(obj_class);
3845 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003846 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003847 "__newobj__ args has the wrong class");
3848 return -1;
3849 }
3850 }
3851 /* XXX: These calls save() are prone to infinite recursion. Imagine
3852 what happen if the value returned by the __reduce__() method of
3853 some extension type contains another object of the same type. Ouch!
3854
3855 Here is a quick example, that I ran into, to illustrate what I
3856 mean:
3857
3858 >>> import pickle, copyreg
3859 >>> copyreg.dispatch_table.pop(complex)
3860 >>> pickle.dumps(1+2j)
3861 Traceback (most recent call last):
3862 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003863 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003864
3865 Removing the complex class from copyreg.dispatch_table made the
3866 __reduce_ex__() method emit another complex object:
3867
3868 >>> (1+1j).__reduce_ex__(2)
3869 (<function __newobj__ at 0xb7b71c3c>,
3870 (<class 'complex'>, (1+1j)), None, None, None)
3871
3872 Thus when save() was called on newargstup (the 2nd item) recursion
3873 ensued. Of course, the bug was in the complex class which had a
3874 broken __getnewargs__() that emitted another complex object. But,
3875 the point, here, is it is quite easy to end up with a broken reduce
3876 function. */
3877
3878 /* Save the class and its __new__ arguments. */
3879 if (save(self, cls, 0) < 0)
3880 return -1;
3881
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003882 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003883 if (newargtup == NULL)
3884 return -1;
3885
3886 p = save(self, newargtup, 0);
3887 Py_DECREF(newargtup);
3888 if (p < 0)
3889 return -1;
3890
3891 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003892 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003893 return -1;
3894 }
3895 else { /* Not using NEWOBJ. */
3896 if (save(self, callable, 0) < 0 ||
3897 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003898 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 return -1;
3900 }
3901
3902 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3903 the caller do not want to memoize the object. Not particularly useful,
3904 but that is to mimic the behavior save_reduce() in pickle.py when
3905 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003906 if (obj != NULL) {
3907 /* If the object is already in the memo, this means it is
3908 recursive. In this case, throw away everything we put on the
3909 stack, and fetch the object back from the memo. */
3910 if (PyMemoTable_Get(self->memo, obj)) {
3911 const char pop_op = POP;
3912
3913 if (_Pickler_Write(self, &pop_op, 1) < 0)
3914 return -1;
3915 if (memo_get(self, obj) < 0)
3916 return -1;
3917
3918 return 0;
3919 }
3920 else if (memo_put(self, obj) < 0)
3921 return -1;
3922 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003923
3924 if (listitems && batch_list(self, listitems) < 0)
3925 return -1;
3926
3927 if (dictitems && batch_dict(self, dictitems) < 0)
3928 return -1;
3929
3930 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003931 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003932 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 return -1;
3934 }
3935
3936 return 0;
3937}
3938
3939static int
3940save(PicklerObject *self, PyObject *obj, int pers_save)
3941{
3942 PyTypeObject *type;
3943 PyObject *reduce_func = NULL;
3944 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003945 int status = 0;
3946
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003947 if (_Pickler_OpcodeBoundary(self) < 0)
3948 return -1;
3949
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003950 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003951 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003952
3953 /* The extra pers_save argument is necessary to avoid calling save_pers()
3954 on its returned object. */
3955 if (!pers_save && self->pers_func) {
3956 /* save_pers() returns:
3957 -1 to signal an error;
3958 0 if it did nothing successfully;
3959 1 if a persistent id was saved.
3960 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003961 if ((status = save_pers(self, obj)) != 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003962 goto done;
3963 }
3964
3965 type = Py_TYPE(obj);
3966
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003967 /* The old cPickle had an optimization that used switch-case statement
3968 dispatching on the first letter of the type name. This has was removed
3969 since benchmarks shown that this optimization was actually slowing
3970 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003971
3972 /* Atom types; these aren't memoized, so don't check the memo. */
3973
3974 if (obj == Py_None) {
3975 status = save_none(self, obj);
3976 goto done;
3977 }
3978 else if (obj == Py_False || obj == Py_True) {
3979 status = save_bool(self, obj);
3980 goto done;
3981 }
3982 else if (type == &PyLong_Type) {
3983 status = save_long(self, obj);
3984 goto done;
3985 }
3986 else if (type == &PyFloat_Type) {
3987 status = save_float(self, obj);
3988 goto done;
3989 }
3990
3991 /* Check the memo to see if it has the object. If so, generate
3992 a GET (or BINGET) opcode, instead of pickling the object
3993 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003994 if (PyMemoTable_Get(self->memo, obj)) {
3995 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003996 goto error;
3997 goto done;
3998 }
3999
4000 if (type == &PyBytes_Type) {
4001 status = save_bytes(self, obj);
4002 goto done;
4003 }
4004 else if (type == &PyUnicode_Type) {
4005 status = save_unicode(self, obj);
4006 goto done;
4007 }
4008 else if (type == &PyDict_Type) {
4009 status = save_dict(self, obj);
4010 goto done;
4011 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004012 else if (type == &PySet_Type) {
4013 status = save_set(self, obj);
4014 goto done;
4015 }
4016 else if (type == &PyFrozenSet_Type) {
4017 status = save_frozenset(self, obj);
4018 goto done;
4019 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004020 else if (type == &PyList_Type) {
4021 status = save_list(self, obj);
4022 goto done;
4023 }
4024 else if (type == &PyTuple_Type) {
4025 status = save_tuple(self, obj);
4026 goto done;
4027 }
4028 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004029 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004030 goto done;
4031 }
4032 else if (type == &PyFunction_Type) {
4033 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004034 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004035 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004036
4037 /* XXX: This part needs some unit tests. */
4038
4039 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004040 * self.dispatch_table, copyreg.dispatch_table, the object's
4041 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004042 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004043 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004044 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004045 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4046 (PyObject *)type);
4047 if (reduce_func == NULL) {
4048 if (PyErr_Occurred()) {
4049 goto error;
4050 }
4051 } else {
4052 /* PyDict_GetItemWithError() returns a borrowed reference.
4053 Increase the reference count to be consistent with
4054 PyObject_GetItem and _PyObject_GetAttrId used below. */
4055 Py_INCREF(reduce_func);
4056 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004057 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004058 reduce_func = PyObject_GetItem(self->dispatch_table,
4059 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004060 if (reduce_func == NULL) {
4061 if (PyErr_ExceptionMatches(PyExc_KeyError))
4062 PyErr_Clear();
4063 else
4064 goto error;
4065 }
4066 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004067 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004068 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004069 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004070 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004071 else if (PyType_IsSubtype(type, &PyType_Type)) {
4072 status = save_global(self, obj, NULL);
4073 goto done;
4074 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004075 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004076 _Py_IDENTIFIER(__reduce__);
4077 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004078
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004079
4080 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4081 automatically defined as __reduce__. While this is convenient, this
4082 make it impossible to know which method was actually called. Of
4083 course, this is not a big deal. But still, it would be nice to let
4084 the user know which method was called when something go
4085 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4086 don't actually have to check for a __reduce__ method. */
4087
4088 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004089 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4090 goto error;
4091 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004092 if (reduce_func != NULL) {
4093 PyObject *proto;
4094 proto = PyLong_FromLong(self->proto);
4095 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004096 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004097 }
4098 }
4099 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004100 PickleState *st = _Pickle_GetGlobalState();
4101
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004102 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004103 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004104 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004105 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004106 }
4107 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004108 PyErr_Format(st->PicklingError,
4109 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004110 type->tp_name, obj);
4111 goto error;
4112 }
4113 }
4114 }
4115
4116 if (reduce_value == NULL)
4117 goto error;
4118
4119 if (PyUnicode_Check(reduce_value)) {
4120 status = save_global(self, obj, reduce_value);
4121 goto done;
4122 }
4123
4124 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004125 PickleState *st = _Pickle_GetGlobalState();
4126 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004127 "__reduce__ must return a string or tuple");
4128 goto error;
4129 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004130
4131 status = save_reduce(self, reduce_value, obj);
4132
4133 if (0) {
4134 error:
4135 status = -1;
4136 }
4137 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004138
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004139 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004140 Py_XDECREF(reduce_func);
4141 Py_XDECREF(reduce_value);
4142
4143 return status;
4144}
4145
4146static int
4147dump(PicklerObject *self, PyObject *obj)
4148{
4149 const char stop_op = STOP;
4150
4151 if (self->proto >= 2) {
4152 char header[2];
4153
4154 header[0] = PROTO;
4155 assert(self->proto >= 0 && self->proto < 256);
4156 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004157 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004158 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004159 if (self->proto >= 4)
4160 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004161 }
4162
4163 if (save(self, obj, 0) < 0 ||
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004164 _Pickler_Write(self, &stop_op, 1) < 0 ||
4165 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004166 return -1;
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004167 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004168 return 0;
4169}
4170
Larry Hastings61272b72014-01-07 12:41:53 -08004171/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172
4173_pickle.Pickler.clear_memo
4174
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004175Clears the pickler's "memo".
4176
4177The memo is the data structure that remembers which objects the
4178pickler has already seen, so that shared or recursive objects are
4179pickled by reference and not by value. This method is useful when
4180re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004181[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004182
Larry Hastings3cceb382014-01-04 11:09:09 -08004183static PyObject *
4184_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004185/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004186{
4187 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004188 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004189
4190 Py_RETURN_NONE;
4191}
4192
Larry Hastings61272b72014-01-07 12:41:53 -08004193/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004194
4195_pickle.Pickler.dump
4196
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004197 obj: object
4198 /
4199
4200Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004201[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004202
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004203static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004204_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004205/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004206{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004207 /* Check whether the Pickler was initialized correctly (issue3664).
4208 Developers often forget to call __init__() in their subclasses, which
4209 would trigger a segfault without this check. */
4210 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004211 PickleState *st = _Pickle_GetGlobalState();
4212 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004213 "Pickler.__init__() was not called by %s.__init__()",
4214 Py_TYPE(self)->tp_name);
4215 return NULL;
4216 }
4217
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004218 if (_Pickler_ClearBuffer(self) < 0)
4219 return NULL;
4220
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004221 if (dump(self, obj) < 0)
4222 return NULL;
4223
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004224 if (_Pickler_FlushToFile(self) < 0)
4225 return NULL;
4226
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004227 Py_RETURN_NONE;
4228}
4229
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004230/*[clinic input]
4231
4232_pickle.Pickler.__sizeof__ -> Py_ssize_t
4233
4234Returns size in memory, in bytes.
4235[clinic start generated code]*/
4236
4237static Py_ssize_t
4238_pickle_Pickler___sizeof___impl(PicklerObject *self)
4239/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4240{
4241 Py_ssize_t res, s;
4242
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004243 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004244 if (self->memo != NULL) {
4245 res += sizeof(PyMemoTable);
4246 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4247 }
4248 if (self->output_buffer != NULL) {
4249 s = _PySys_GetSizeOf(self->output_buffer);
4250 if (s == -1)
4251 return -1;
4252 res += s;
4253 }
4254 return res;
4255}
4256
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004257static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004258 _PICKLE_PICKLER_DUMP_METHODDEF
4259 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004260 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004261 {NULL, NULL} /* sentinel */
4262};
4263
4264static void
4265Pickler_dealloc(PicklerObject *self)
4266{
4267 PyObject_GC_UnTrack(self);
4268
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004269 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004270 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004271 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004272 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004273 Py_XDECREF(self->fast_memo);
4274
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004275 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004276
4277 Py_TYPE(self)->tp_free((PyObject *)self);
4278}
4279
4280static int
4281Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4282{
4283 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004284 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004285 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004286 Py_VISIT(self->fast_memo);
4287 return 0;
4288}
4289
4290static int
4291Pickler_clear(PicklerObject *self)
4292{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004293 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004294 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004295 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004296 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004297 Py_CLEAR(self->fast_memo);
4298
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004299 if (self->memo != NULL) {
4300 PyMemoTable *memo = self->memo;
4301 self->memo = NULL;
4302 PyMemoTable_Del(memo);
4303 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004304 return 0;
4305}
4306
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004307
Larry Hastings61272b72014-01-07 12:41:53 -08004308/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004309
4310_pickle.Pickler.__init__
4311
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004312 file: object
4313 protocol: object = NULL
4314 fix_imports: bool = True
4315
4316This takes a binary file for writing a pickle data stream.
4317
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004318The optional *protocol* argument tells the pickler to use the given
4319protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4320protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004321
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004322Specifying a negative protocol version selects the highest protocol
4323version supported. The higher the protocol used, the more recent the
4324version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004325
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004326The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004327bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004328writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004329this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004330
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004331If *fix_imports* is True and protocol is less than 3, pickle will try
4332to map the new Python 3 names to the old module names used in Python
43332, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004334[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004335
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004336static int
Larry Hastings89964c42015-04-14 18:07:59 -04004337_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4338 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004339/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004340{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004341 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004342 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004343
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004344 /* In case of multiple __init__() calls, clear previous content. */
4345 if (self->write != NULL)
4346 (void)Pickler_clear(self);
4347
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004348 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004349 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004350
4351 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004352 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004353
4354 /* memo and output_buffer may have already been created in _Pickler_New */
4355 if (self->memo == NULL) {
4356 self->memo = PyMemoTable_New();
4357 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004358 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004359 }
4360 self->output_len = 0;
4361 if (self->output_buffer == NULL) {
4362 self->max_output_len = WRITE_BUF_SIZE;
4363 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4364 self->max_output_len);
4365 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004366 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004367 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004368
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004369 self->fast = 0;
4370 self->fast_nesting = 0;
4371 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004372
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004373 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4374 &self->pers_func, &self->pers_func_self) < 0)
4375 {
4376 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004377 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004378
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004379 if (_PyObject_LookupAttrId((PyObject *)self,
4380 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4381 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004382 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004383
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004384 return 0;
4385}
4386
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004387
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004388/* Define a proxy object for the Pickler's internal memo object. This is to
4389 * avoid breaking code like:
4390 * pickler.memo.clear()
4391 * and
4392 * pickler.memo = saved_memo
4393 * Is this a good idea? Not really, but we don't want to break code that uses
4394 * it. Note that we don't implement the entire mapping API here. This is
4395 * intentional, as these should be treated as black-box implementation details.
4396 */
4397
Larry Hastings61272b72014-01-07 12:41:53 -08004398/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004399_pickle.PicklerMemoProxy.clear
4400
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004401Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004402[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004403
Larry Hastings3cceb382014-01-04 11:09:09 -08004404static PyObject *
4405_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004406/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004407{
4408 if (self->pickler->memo)
4409 PyMemoTable_Clear(self->pickler->memo);
4410 Py_RETURN_NONE;
4411}
4412
Larry Hastings61272b72014-01-07 12:41:53 -08004413/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004414_pickle.PicklerMemoProxy.copy
4415
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004416Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004417[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004418
Larry Hastings3cceb382014-01-04 11:09:09 -08004419static PyObject *
4420_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004421/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004422{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004423 PyMemoTable *memo;
4424 PyObject *new_memo = PyDict_New();
4425 if (new_memo == NULL)
4426 return NULL;
4427
4428 memo = self->pickler->memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07004429 for (size_t i = 0; i < memo->mt_allocated; ++i) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004430 PyMemoEntry entry = memo->mt_table[i];
4431 if (entry.me_key != NULL) {
4432 int status;
4433 PyObject *key, *value;
4434
4435 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004436 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004437
4438 if (key == NULL || value == NULL) {
4439 Py_XDECREF(key);
4440 Py_XDECREF(value);
4441 goto error;
4442 }
4443 status = PyDict_SetItem(new_memo, key, value);
4444 Py_DECREF(key);
4445 Py_DECREF(value);
4446 if (status < 0)
4447 goto error;
4448 }
4449 }
4450 return new_memo;
4451
4452 error:
4453 Py_XDECREF(new_memo);
4454 return NULL;
4455}
4456
Larry Hastings61272b72014-01-07 12:41:53 -08004457/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004458_pickle.PicklerMemoProxy.__reduce__
4459
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004460Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004461[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004462
Larry Hastings3cceb382014-01-04 11:09:09 -08004463static PyObject *
4464_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004465/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004466{
4467 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004468 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004469 if (contents == NULL)
4470 return NULL;
4471
4472 reduce_value = PyTuple_New(2);
4473 if (reduce_value == NULL) {
4474 Py_DECREF(contents);
4475 return NULL;
4476 }
4477 dict_args = PyTuple_New(1);
4478 if (dict_args == NULL) {
4479 Py_DECREF(contents);
4480 Py_DECREF(reduce_value);
4481 return NULL;
4482 }
4483 PyTuple_SET_ITEM(dict_args, 0, contents);
4484 Py_INCREF((PyObject *)&PyDict_Type);
4485 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4486 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4487 return reduce_value;
4488}
4489
4490static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004491 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4492 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4493 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004494 {NULL, NULL} /* sentinel */
4495};
4496
4497static void
4498PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4499{
4500 PyObject_GC_UnTrack(self);
4501 Py_XDECREF(self->pickler);
4502 PyObject_GC_Del((PyObject *)self);
4503}
4504
4505static int
4506PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4507 visitproc visit, void *arg)
4508{
4509 Py_VISIT(self->pickler);
4510 return 0;
4511}
4512
4513static int
4514PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4515{
4516 Py_CLEAR(self->pickler);
4517 return 0;
4518}
4519
4520static PyTypeObject PicklerMemoProxyType = {
4521 PyVarObject_HEAD_INIT(NULL, 0)
4522 "_pickle.PicklerMemoProxy", /*tp_name*/
4523 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4524 0,
4525 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4526 0, /* tp_print */
4527 0, /* tp_getattr */
4528 0, /* tp_setattr */
4529 0, /* tp_compare */
4530 0, /* tp_repr */
4531 0, /* tp_as_number */
4532 0, /* tp_as_sequence */
4533 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004534 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004535 0, /* tp_call */
4536 0, /* tp_str */
4537 PyObject_GenericGetAttr, /* tp_getattro */
4538 PyObject_GenericSetAttr, /* tp_setattro */
4539 0, /* tp_as_buffer */
4540 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4541 0, /* tp_doc */
4542 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4543 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4544 0, /* tp_richcompare */
4545 0, /* tp_weaklistoffset */
4546 0, /* tp_iter */
4547 0, /* tp_iternext */
4548 picklerproxy_methods, /* tp_methods */
4549};
4550
4551static PyObject *
4552PicklerMemoProxy_New(PicklerObject *pickler)
4553{
4554 PicklerMemoProxyObject *self;
4555
4556 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4557 if (self == NULL)
4558 return NULL;
4559 Py_INCREF(pickler);
4560 self->pickler = pickler;
4561 PyObject_GC_Track(self);
4562 return (PyObject *)self;
4563}
4564
4565/*****************************************************************************/
4566
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004567static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004568Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004569{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004570 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004571}
4572
4573static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004574Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004575{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004576 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004577
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004578 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004579 PyErr_SetString(PyExc_TypeError,
4580 "attribute deletion is not supported");
4581 return -1;
4582 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004583
4584 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4585 PicklerObject *pickler =
4586 ((PicklerMemoProxyObject *)obj)->pickler;
4587
4588 new_memo = PyMemoTable_Copy(pickler->memo);
4589 if (new_memo == NULL)
4590 return -1;
4591 }
4592 else if (PyDict_Check(obj)) {
4593 Py_ssize_t i = 0;
4594 PyObject *key, *value;
4595
4596 new_memo = PyMemoTable_New();
4597 if (new_memo == NULL)
4598 return -1;
4599
4600 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004601 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004602 PyObject *memo_obj;
4603
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004604 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004605 PyErr_SetString(PyExc_TypeError,
4606 "'memo' values must be 2-item tuples");
4607 goto error;
4608 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004609 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004610 if (memo_id == -1 && PyErr_Occurred())
4611 goto error;
4612 memo_obj = PyTuple_GET_ITEM(value, 1);
4613 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4614 goto error;
4615 }
4616 }
4617 else {
4618 PyErr_Format(PyExc_TypeError,
Miss Islington (bot)7beb8c52018-11-05 06:52:58 -08004619 "'memo' attribute must be a PicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004620 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004621 return -1;
4622 }
4623
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004624 PyMemoTable_Del(self->memo);
4625 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004626
4627 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004628
4629 error:
4630 if (new_memo)
4631 PyMemoTable_Del(new_memo);
4632 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004633}
4634
4635static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004636Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004637{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004638 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004639 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004640 return NULL;
4641 }
4642 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004643}
4644
4645static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004646Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004647{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004648 if (value == NULL) {
4649 PyErr_SetString(PyExc_TypeError,
4650 "attribute deletion is not supported");
4651 return -1;
4652 }
4653 if (!PyCallable_Check(value)) {
4654 PyErr_SetString(PyExc_TypeError,
4655 "persistent_id must be a callable taking one argument");
4656 return -1;
4657 }
4658
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004659 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004660 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004661 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004662
4663 return 0;
4664}
4665
4666static PyMemberDef Pickler_members[] = {
4667 {"bin", T_INT, offsetof(PicklerObject, bin)},
4668 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004669 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004670 {NULL}
4671};
4672
4673static PyGetSetDef Pickler_getsets[] = {
4674 {"memo", (getter)Pickler_get_memo,
4675 (setter)Pickler_set_memo},
4676 {"persistent_id", (getter)Pickler_get_persid,
4677 (setter)Pickler_set_persid},
4678 {NULL}
4679};
4680
4681static PyTypeObject Pickler_Type = {
4682 PyVarObject_HEAD_INIT(NULL, 0)
4683 "_pickle.Pickler" , /*tp_name*/
4684 sizeof(PicklerObject), /*tp_basicsize*/
4685 0, /*tp_itemsize*/
4686 (destructor)Pickler_dealloc, /*tp_dealloc*/
4687 0, /*tp_print*/
4688 0, /*tp_getattr*/
4689 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004690 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004691 0, /*tp_repr*/
4692 0, /*tp_as_number*/
4693 0, /*tp_as_sequence*/
4694 0, /*tp_as_mapping*/
4695 0, /*tp_hash*/
4696 0, /*tp_call*/
4697 0, /*tp_str*/
4698 0, /*tp_getattro*/
4699 0, /*tp_setattro*/
4700 0, /*tp_as_buffer*/
4701 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004702 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004703 (traverseproc)Pickler_traverse, /*tp_traverse*/
4704 (inquiry)Pickler_clear, /*tp_clear*/
4705 0, /*tp_richcompare*/
4706 0, /*tp_weaklistoffset*/
4707 0, /*tp_iter*/
4708 0, /*tp_iternext*/
4709 Pickler_methods, /*tp_methods*/
4710 Pickler_members, /*tp_members*/
4711 Pickler_getsets, /*tp_getset*/
4712 0, /*tp_base*/
4713 0, /*tp_dict*/
4714 0, /*tp_descr_get*/
4715 0, /*tp_descr_set*/
4716 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004717 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004718 PyType_GenericAlloc, /*tp_alloc*/
4719 PyType_GenericNew, /*tp_new*/
4720 PyObject_GC_Del, /*tp_free*/
4721 0, /*tp_is_gc*/
4722};
4723
Victor Stinner121aab42011-09-29 23:40:53 +02004724/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004725
4726 XXX: It would be nice to able to avoid Python function call overhead, by
4727 using directly the C version of find_class(), when find_class() is not
4728 overridden by a subclass. Although, this could become rather hackish. A
4729 simpler optimization would be to call the C function when self is not a
4730 subclass instance. */
4731static PyObject *
4732find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4733{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004734 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004735
Victor Stinner55ba38a2016-12-09 16:09:30 +01004736 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4737 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004738}
4739
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004740static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004741marker(UnpicklerObject *self)
4742{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004743 Py_ssize_t mark;
4744
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004745 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004746 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004747 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004748 return -1;
4749 }
4750
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004751 mark = self->marks[--self->num_marks];
4752 self->stack->mark_set = self->num_marks != 0;
4753 self->stack->fence = self->num_marks ?
4754 self->marks[self->num_marks - 1] : 0;
4755 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004756}
4757
4758static int
4759load_none(UnpicklerObject *self)
4760{
4761 PDATA_APPEND(self->stack, Py_None, -1);
4762 return 0;
4763}
4764
4765static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004766load_int(UnpicklerObject *self)
4767{
4768 PyObject *value;
4769 char *endptr, *s;
4770 Py_ssize_t len;
4771 long x;
4772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004773 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004774 return -1;
4775 if (len < 2)
4776 return bad_readline();
4777
4778 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004779 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004780 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004781 x = strtol(s, &endptr, 0);
4782
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004783 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004784 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004785 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004786 errno = 0;
4787 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004788 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004789 if (value == NULL) {
4790 PyErr_SetString(PyExc_ValueError,
4791 "could not convert string to int");
4792 return -1;
4793 }
4794 }
4795 else {
4796 if (len == 3 && (x == 0 || x == 1)) {
4797 if ((value = PyBool_FromLong(x)) == NULL)
4798 return -1;
4799 }
4800 else {
4801 if ((value = PyLong_FromLong(x)) == NULL)
4802 return -1;
4803 }
4804 }
4805
4806 PDATA_PUSH(self->stack, value, -1);
4807 return 0;
4808}
4809
4810static int
4811load_bool(UnpicklerObject *self, PyObject *boolean)
4812{
4813 assert(boolean == Py_True || boolean == Py_False);
4814 PDATA_APPEND(self->stack, boolean, -1);
4815 return 0;
4816}
4817
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004818/* s contains x bytes of an unsigned little-endian integer. Return its value
4819 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4820 */
4821static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004822calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004823{
4824 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004825 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004826 size_t x = 0;
4827
Serhiy Storchakae0606192015-09-29 22:10:07 +03004828 if (nbytes > (int)sizeof(size_t)) {
4829 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4830 * have 64-bit size that can't be represented on 32-bit platform.
4831 */
4832 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4833 if (s[i])
4834 return -1;
4835 }
4836 nbytes = (int)sizeof(size_t);
4837 }
4838 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004839 x |= (size_t) s[i] << (8 * i);
4840 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004841
4842 if (x > PY_SSIZE_T_MAX)
4843 return -1;
4844 else
4845 return (Py_ssize_t) x;
4846}
4847
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004848/* s contains x bytes of a little-endian integer. Return its value as a
4849 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004850 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004851 * of x-platform bugs.
4852 */
4853static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004854calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004855{
4856 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004857 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004858 long x = 0;
4859
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004860 for (i = 0; i < nbytes; i++) {
4861 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004862 }
4863
4864 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4865 * is signed, so on a box with longs bigger than 4 bytes we need
4866 * to extend a BININT's sign bit to the full width.
4867 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004868 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004869 x |= -(x & (1L << 31));
4870 }
4871
4872 return x;
4873}
4874
4875static int
4876load_binintx(UnpicklerObject *self, char *s, int size)
4877{
4878 PyObject *value;
4879 long x;
4880
4881 x = calc_binint(s, size);
4882
4883 if ((value = PyLong_FromLong(x)) == NULL)
4884 return -1;
4885
4886 PDATA_PUSH(self->stack, value, -1);
4887 return 0;
4888}
4889
4890static int
4891load_binint(UnpicklerObject *self)
4892{
4893 char *s;
4894
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004895 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004896 return -1;
4897
4898 return load_binintx(self, s, 4);
4899}
4900
4901static int
4902load_binint1(UnpicklerObject *self)
4903{
4904 char *s;
4905
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004906 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004907 return -1;
4908
4909 return load_binintx(self, s, 1);
4910}
4911
4912static int
4913load_binint2(UnpicklerObject *self)
4914{
4915 char *s;
4916
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004917 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004918 return -1;
4919
4920 return load_binintx(self, s, 2);
4921}
4922
4923static int
4924load_long(UnpicklerObject *self)
4925{
4926 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004927 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004928 Py_ssize_t len;
4929
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004930 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004931 return -1;
4932 if (len < 2)
4933 return bad_readline();
4934
Mark Dickinson8dd05142009-01-20 20:43:58 +00004935 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4936 the 'L' before calling PyLong_FromString. In order to maintain
4937 compatibility with Python 3.0.0, we don't actually *require*
4938 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004939 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004940 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004941 /* XXX: Should the base argument explicitly set to 10? */
4942 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004943 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004944 return -1;
4945
4946 PDATA_PUSH(self->stack, value, -1);
4947 return 0;
4948}
4949
4950/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4951 * data following.
4952 */
4953static int
4954load_counted_long(UnpicklerObject *self, int size)
4955{
4956 PyObject *value;
4957 char *nbytes;
4958 char *pdata;
4959
4960 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004961 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004962 return -1;
4963
4964 size = calc_binint(nbytes, size);
4965 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004966 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004967 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004968 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004969 "LONG pickle has negative byte count");
4970 return -1;
4971 }
4972
4973 if (size == 0)
4974 value = PyLong_FromLong(0L);
4975 else {
4976 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004977 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004978 return -1;
4979 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4980 1 /* little endian */ , 1 /* signed */ );
4981 }
4982 if (value == NULL)
4983 return -1;
4984 PDATA_PUSH(self->stack, value, -1);
4985 return 0;
4986}
4987
4988static int
4989load_float(UnpicklerObject *self)
4990{
4991 PyObject *value;
4992 char *endptr, *s;
4993 Py_ssize_t len;
4994 double d;
4995
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004996 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004997 return -1;
4998 if (len < 2)
4999 return bad_readline();
5000
5001 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00005002 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
5003 if (d == -1.0 && PyErr_Occurred())
5004 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005005 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005006 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5007 return -1;
5008 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005009 value = PyFloat_FromDouble(d);
5010 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005011 return -1;
5012
5013 PDATA_PUSH(self->stack, value, -1);
5014 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005015}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005016
5017static int
5018load_binfloat(UnpicklerObject *self)
5019{
5020 PyObject *value;
5021 double x;
5022 char *s;
5023
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005024 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005025 return -1;
5026
5027 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5028 if (x == -1.0 && PyErr_Occurred())
5029 return -1;
5030
5031 if ((value = PyFloat_FromDouble(x)) == NULL)
5032 return -1;
5033
5034 PDATA_PUSH(self->stack, value, -1);
5035 return 0;
5036}
5037
5038static int
5039load_string(UnpicklerObject *self)
5040{
5041 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005042 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005043 Py_ssize_t len;
5044 char *s, *p;
5045
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005046 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005047 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005048 /* Strip the newline */
5049 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005050 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005051 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005052 p = s + 1;
5053 len -= 2;
5054 }
5055 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005056 PickleState *st = _Pickle_GetGlobalState();
5057 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005058 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005059 return -1;
5060 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005061 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005062
5063 /* Use the PyBytes API to decode the string, since that is what is used
5064 to encode, and then coerce the result to Unicode. */
5065 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005066 if (bytes == NULL)
5067 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005068
5069 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5070 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5071 if (strcmp(self->encoding, "bytes") == 0) {
5072 obj = bytes;
5073 }
5074 else {
5075 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5076 Py_DECREF(bytes);
5077 if (obj == NULL) {
5078 return -1;
5079 }
5080 }
5081
5082 PDATA_PUSH(self->stack, obj, -1);
5083 return 0;
5084}
5085
5086static int
5087load_counted_binstring(UnpicklerObject *self, int nbytes)
5088{
5089 PyObject *obj;
5090 Py_ssize_t size;
5091 char *s;
5092
5093 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005094 return -1;
5095
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005096 size = calc_binsize(s, nbytes);
5097 if (size < 0) {
5098 PickleState *st = _Pickle_GetGlobalState();
5099 PyErr_Format(st->UnpicklingError,
5100 "BINSTRING exceeds system's maximum size of %zd bytes",
5101 PY_SSIZE_T_MAX);
5102 return -1;
5103 }
5104
5105 if (_Unpickler_Read(self, &s, size) < 0)
5106 return -1;
5107
5108 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5109 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5110 if (strcmp(self->encoding, "bytes") == 0) {
5111 obj = PyBytes_FromStringAndSize(s, size);
5112 }
5113 else {
5114 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5115 }
5116 if (obj == NULL) {
5117 return -1;
5118 }
5119
5120 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005121 return 0;
5122}
5123
5124static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005125load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005126{
5127 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005128 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005129 char *s;
5130
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005131 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005132 return -1;
5133
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005134 size = calc_binsize(s, nbytes);
5135 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005136 PyErr_Format(PyExc_OverflowError,
5137 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005138 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005139 return -1;
5140 }
5141
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005142 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005143 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005144
5145 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005146 if (bytes == NULL)
5147 return -1;
5148
5149 PDATA_PUSH(self->stack, bytes, -1);
5150 return 0;
5151}
5152
5153static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005154load_unicode(UnpicklerObject *self)
5155{
5156 PyObject *str;
5157 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005158 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005159
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005160 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005161 return -1;
5162 if (len < 1)
5163 return bad_readline();
5164
5165 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5166 if (str == NULL)
5167 return -1;
5168
5169 PDATA_PUSH(self->stack, str, -1);
5170 return 0;
5171}
5172
5173static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005174load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005175{
5176 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005177 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005178 char *s;
5179
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005180 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005181 return -1;
5182
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005183 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005184 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005185 PyErr_Format(PyExc_OverflowError,
5186 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005187 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005188 return -1;
5189 }
5190
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005191 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005192 return -1;
5193
Victor Stinner485fb562010-04-13 11:07:24 +00005194 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005195 if (str == NULL)
5196 return -1;
5197
5198 PDATA_PUSH(self->stack, str, -1);
5199 return 0;
5200}
5201
5202static int
Victor Stinner21b47112016-03-14 18:09:39 +01005203load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005204{
5205 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005206
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005207 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005208 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005209
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005210 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005211 if (tuple == NULL)
5212 return -1;
5213 PDATA_PUSH(self->stack, tuple, -1);
5214 return 0;
5215}
5216
5217static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005218load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005219{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005220 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005221
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005222 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005223 return -1;
5224
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005225 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005226}
5227
5228static int
5229load_empty_list(UnpicklerObject *self)
5230{
5231 PyObject *list;
5232
5233 if ((list = PyList_New(0)) == NULL)
5234 return -1;
5235 PDATA_PUSH(self->stack, list, -1);
5236 return 0;
5237}
5238
5239static int
5240load_empty_dict(UnpicklerObject *self)
5241{
5242 PyObject *dict;
5243
5244 if ((dict = PyDict_New()) == NULL)
5245 return -1;
5246 PDATA_PUSH(self->stack, dict, -1);
5247 return 0;
5248}
5249
5250static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005251load_empty_set(UnpicklerObject *self)
5252{
5253 PyObject *set;
5254
5255 if ((set = PySet_New(NULL)) == NULL)
5256 return -1;
5257 PDATA_PUSH(self->stack, set, -1);
5258 return 0;
5259}
5260
5261static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005262load_list(UnpicklerObject *self)
5263{
5264 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005265 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005266
5267 if ((i = marker(self)) < 0)
5268 return -1;
5269
5270 list = Pdata_poplist(self->stack, i);
5271 if (list == NULL)
5272 return -1;
5273 PDATA_PUSH(self->stack, list, -1);
5274 return 0;
5275}
5276
5277static int
5278load_dict(UnpicklerObject *self)
5279{
5280 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005281 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005282
5283 if ((i = marker(self)) < 0)
5284 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005285 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005286
5287 if ((dict = PyDict_New()) == NULL)
5288 return -1;
5289
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005290 if ((j - i) % 2 != 0) {
5291 PickleState *st = _Pickle_GetGlobalState();
5292 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005293 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005294 return -1;
5295 }
5296
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005297 for (k = i + 1; k < j; k += 2) {
5298 key = self->stack->data[k - 1];
5299 value = self->stack->data[k];
5300 if (PyDict_SetItem(dict, key, value) < 0) {
5301 Py_DECREF(dict);
5302 return -1;
5303 }
5304 }
5305 Pdata_clear(self->stack, i);
5306 PDATA_PUSH(self->stack, dict, -1);
5307 return 0;
5308}
5309
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005310static int
5311load_frozenset(UnpicklerObject *self)
5312{
5313 PyObject *items;
5314 PyObject *frozenset;
5315 Py_ssize_t i;
5316
5317 if ((i = marker(self)) < 0)
5318 return -1;
5319
5320 items = Pdata_poptuple(self->stack, i);
5321 if (items == NULL)
5322 return -1;
5323
5324 frozenset = PyFrozenSet_New(items);
5325 Py_DECREF(items);
5326 if (frozenset == NULL)
5327 return -1;
5328
5329 PDATA_PUSH(self->stack, frozenset, -1);
5330 return 0;
5331}
5332
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005333static PyObject *
5334instantiate(PyObject *cls, PyObject *args)
5335{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005336 /* Caller must assure args are a tuple. Normally, args come from
5337 Pdata_poptuple which packs objects from the top of the stack
5338 into a newly created tuple. */
5339 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005340 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5341 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005342 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005343 PyObject *func;
5344 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5345 return NULL;
5346 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005347 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005348 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5349 }
5350 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005351 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005352 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005353}
5354
5355static int
5356load_obj(UnpicklerObject *self)
5357{
5358 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005359 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005360
5361 if ((i = marker(self)) < 0)
5362 return -1;
5363
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005364 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005365 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005366
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005367 args = Pdata_poptuple(self->stack, i + 1);
5368 if (args == NULL)
5369 return -1;
5370
5371 PDATA_POP(self->stack, cls);
5372 if (cls) {
5373 obj = instantiate(cls, args);
5374 Py_DECREF(cls);
5375 }
5376 Py_DECREF(args);
5377 if (obj == NULL)
5378 return -1;
5379
5380 PDATA_PUSH(self->stack, obj, -1);
5381 return 0;
5382}
5383
5384static int
5385load_inst(UnpicklerObject *self)
5386{
5387 PyObject *cls = NULL;
5388 PyObject *args = NULL;
5389 PyObject *obj = NULL;
5390 PyObject *module_name;
5391 PyObject *class_name;
5392 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005393 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005394 char *s;
5395
5396 if ((i = marker(self)) < 0)
5397 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005398 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005399 return -1;
5400 if (len < 2)
5401 return bad_readline();
5402
5403 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5404 identifiers are permitted in Python 3.0, since the INST opcode is only
5405 supported by older protocols on Python 2.x. */
5406 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5407 if (module_name == NULL)
5408 return -1;
5409
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005410 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005411 if (len < 2) {
5412 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005413 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005414 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005415 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005416 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005417 cls = find_class(self, module_name, class_name);
5418 Py_DECREF(class_name);
5419 }
5420 }
5421 Py_DECREF(module_name);
5422
5423 if (cls == NULL)
5424 return -1;
5425
5426 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5427 obj = instantiate(cls, args);
5428 Py_DECREF(args);
5429 }
5430 Py_DECREF(cls);
5431
5432 if (obj == NULL)
5433 return -1;
5434
5435 PDATA_PUSH(self->stack, obj, -1);
5436 return 0;
5437}
5438
5439static int
5440load_newobj(UnpicklerObject *self)
5441{
5442 PyObject *args = NULL;
5443 PyObject *clsraw = NULL;
5444 PyTypeObject *cls; /* clsraw cast to its true type */
5445 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005446 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005447
5448 /* Stack is ... cls argtuple, and we want to call
5449 * cls.__new__(cls, *argtuple).
5450 */
5451 PDATA_POP(self->stack, args);
5452 if (args == NULL)
5453 goto error;
5454 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005455 PyErr_SetString(st->UnpicklingError,
5456 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005457 goto error;
5458 }
5459
5460 PDATA_POP(self->stack, clsraw);
5461 cls = (PyTypeObject *)clsraw;
5462 if (cls == NULL)
5463 goto error;
5464 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005465 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005466 "isn't a type object");
5467 goto error;
5468 }
5469 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005470 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005471 "has NULL tp_new");
5472 goto error;
5473 }
5474
5475 /* Call __new__. */
5476 obj = cls->tp_new(cls, args, NULL);
5477 if (obj == NULL)
5478 goto error;
5479
5480 Py_DECREF(args);
5481 Py_DECREF(clsraw);
5482 PDATA_PUSH(self->stack, obj, -1);
5483 return 0;
5484
5485 error:
5486 Py_XDECREF(args);
5487 Py_XDECREF(clsraw);
5488 return -1;
5489}
5490
5491static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005492load_newobj_ex(UnpicklerObject *self)
5493{
5494 PyObject *cls, *args, *kwargs;
5495 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005496 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005497
5498 PDATA_POP(self->stack, kwargs);
5499 if (kwargs == NULL) {
5500 return -1;
5501 }
5502 PDATA_POP(self->stack, args);
5503 if (args == NULL) {
5504 Py_DECREF(kwargs);
5505 return -1;
5506 }
5507 PDATA_POP(self->stack, cls);
5508 if (cls == NULL) {
5509 Py_DECREF(kwargs);
5510 Py_DECREF(args);
5511 return -1;
5512 }
Larry Hastings61272b72014-01-07 12:41:53 -08005513
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005514 if (!PyType_Check(cls)) {
5515 Py_DECREF(kwargs);
5516 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005517 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005518 "NEWOBJ_EX class argument must be a type, not %.200s",
5519 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005520 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005521 return -1;
5522 }
5523
5524 if (((PyTypeObject *)cls)->tp_new == NULL) {
5525 Py_DECREF(kwargs);
5526 Py_DECREF(args);
5527 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005528 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005529 "NEWOBJ_EX class argument doesn't have __new__");
5530 return -1;
5531 }
5532 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5533 Py_DECREF(kwargs);
5534 Py_DECREF(args);
5535 Py_DECREF(cls);
5536 if (obj == NULL) {
5537 return -1;
5538 }
5539 PDATA_PUSH(self->stack, obj, -1);
5540 return 0;
5541}
5542
5543static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005544load_global(UnpicklerObject *self)
5545{
5546 PyObject *global = NULL;
5547 PyObject *module_name;
5548 PyObject *global_name;
5549 Py_ssize_t len;
5550 char *s;
5551
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005552 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553 return -1;
5554 if (len < 2)
5555 return bad_readline();
5556 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5557 if (!module_name)
5558 return -1;
5559
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005560 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005561 if (len < 2) {
5562 Py_DECREF(module_name);
5563 return bad_readline();
5564 }
5565 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5566 if (global_name) {
5567 global = find_class(self, module_name, global_name);
5568 Py_DECREF(global_name);
5569 }
5570 }
5571 Py_DECREF(module_name);
5572
5573 if (global == NULL)
5574 return -1;
5575 PDATA_PUSH(self->stack, global, -1);
5576 return 0;
5577}
5578
5579static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005580load_stack_global(UnpicklerObject *self)
5581{
5582 PyObject *global;
5583 PyObject *module_name;
5584 PyObject *global_name;
5585
5586 PDATA_POP(self->stack, global_name);
5587 PDATA_POP(self->stack, module_name);
5588 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5589 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005590 PickleState *st = _Pickle_GetGlobalState();
5591 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005592 Py_XDECREF(global_name);
5593 Py_XDECREF(module_name);
5594 return -1;
5595 }
5596 global = find_class(self, module_name, global_name);
5597 Py_DECREF(global_name);
5598 Py_DECREF(module_name);
5599 if (global == NULL)
5600 return -1;
5601 PDATA_PUSH(self->stack, global, -1);
5602 return 0;
5603}
5604
5605static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606load_persid(UnpicklerObject *self)
5607{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005608 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609 Py_ssize_t len;
5610 char *s;
5611
5612 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005613 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005614 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005615 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 return bad_readline();
5617
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005618 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5619 if (pid == NULL) {
5620 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5621 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5622 "persistent IDs in protocol 0 must be "
5623 "ASCII strings");
5624 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005626 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005628 obj = call_method(self->pers_func, self->pers_func_self, pid);
5629 Py_DECREF(pid);
5630 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005631 return -1;
5632
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005633 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634 return 0;
5635 }
5636 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005637 PickleState *st = _Pickle_GetGlobalState();
5638 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005639 "A load persistent id instruction was encountered,\n"
5640 "but no persistent_load function was specified.");
5641 return -1;
5642 }
5643}
5644
5645static int
5646load_binpersid(UnpicklerObject *self)
5647{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005648 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649
5650 if (self->pers_func) {
5651 PDATA_POP(self->stack, pid);
5652 if (pid == NULL)
5653 return -1;
5654
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005655 obj = call_method(self->pers_func, self->pers_func_self, pid);
5656 Py_DECREF(pid);
5657 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005658 return -1;
5659
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005660 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005661 return 0;
5662 }
5663 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005664 PickleState *st = _Pickle_GetGlobalState();
5665 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005666 "A load persistent id instruction was encountered,\n"
5667 "but no persistent_load function was specified.");
5668 return -1;
5669 }
5670}
5671
5672static int
5673load_pop(UnpicklerObject *self)
5674{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005675 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005676
5677 /* Note that we split the (pickle.py) stack into two stacks,
5678 * an object stack and a mark stack. We have to be clever and
5679 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005680 * mark stack first, and only signalling a stack underflow if
5681 * the object stack is empty and the mark stack doesn't match
5682 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005683 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005684 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005685 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005686 self->stack->mark_set = self->num_marks != 0;
5687 self->stack->fence = self->num_marks ?
5688 self->marks[self->num_marks - 1] : 0;
5689 } else if (len <= self->stack->fence)
5690 return Pdata_stack_underflow(self->stack);
5691 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005692 len--;
5693 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005694 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005695 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696 return 0;
5697}
5698
5699static int
5700load_pop_mark(UnpicklerObject *self)
5701{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005702 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005703
5704 if ((i = marker(self)) < 0)
5705 return -1;
5706
5707 Pdata_clear(self->stack, i);
5708
5709 return 0;
5710}
5711
5712static int
5713load_dup(UnpicklerObject *self)
5714{
5715 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005716 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005717
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005718 if (len <= self->stack->fence)
5719 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005720 last = self->stack->data[len - 1];
5721 PDATA_APPEND(self->stack, last, -1);
5722 return 0;
5723}
5724
5725static int
5726load_get(UnpicklerObject *self)
5727{
5728 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005729 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005730 Py_ssize_t len;
5731 char *s;
5732
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005733 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005734 return -1;
5735 if (len < 2)
5736 return bad_readline();
5737
5738 key = PyLong_FromString(s, NULL, 10);
5739 if (key == NULL)
5740 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005741 idx = PyLong_AsSsize_t(key);
5742 if (idx == -1 && PyErr_Occurred()) {
5743 Py_DECREF(key);
5744 return -1;
5745 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005746
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005747 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005748 if (value == NULL) {
5749 if (!PyErr_Occurred())
5750 PyErr_SetObject(PyExc_KeyError, key);
5751 Py_DECREF(key);
5752 return -1;
5753 }
5754 Py_DECREF(key);
5755
5756 PDATA_APPEND(self->stack, value, -1);
5757 return 0;
5758}
5759
5760static int
5761load_binget(UnpicklerObject *self)
5762{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005763 PyObject *value;
5764 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 char *s;
5766
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005767 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005768 return -1;
5769
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005770 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005772 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005774 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005775 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005777 Py_DECREF(key);
5778 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005779 return -1;
5780 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781
5782 PDATA_APPEND(self->stack, value, -1);
5783 return 0;
5784}
5785
5786static int
5787load_long_binget(UnpicklerObject *self)
5788{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005789 PyObject *value;
5790 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005792
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005793 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005794 return -1;
5795
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005796 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005798 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005799 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005800 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005801 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005802 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005803 Py_DECREF(key);
5804 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005805 return -1;
5806 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005807
5808 PDATA_APPEND(self->stack, value, -1);
5809 return 0;
5810}
5811
5812/* Push an object from the extension registry (EXT[124]). nbytes is
5813 * the number of bytes following the opcode, holding the index (code) value.
5814 */
5815static int
5816load_extension(UnpicklerObject *self, int nbytes)
5817{
5818 char *codebytes; /* the nbytes bytes after the opcode */
5819 long code; /* calc_binint returns long */
5820 PyObject *py_code; /* code as a Python int */
5821 PyObject *obj; /* the object to push */
5822 PyObject *pair; /* (module_name, class_name) */
5823 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005824 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005825
5826 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005827 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005828 return -1;
5829 code = calc_binint(codebytes, nbytes);
5830 if (code <= 0) { /* note that 0 is forbidden */
5831 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005832 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005833 return -1;
5834 }
5835
5836 /* Look for the code in the cache. */
5837 py_code = PyLong_FromLong(code);
5838 if (py_code == NULL)
5839 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005840 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005841 if (obj != NULL) {
5842 /* Bingo. */
5843 Py_DECREF(py_code);
5844 PDATA_APPEND(self->stack, obj, -1);
5845 return 0;
5846 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005847 if (PyErr_Occurred()) {
5848 Py_DECREF(py_code);
5849 return -1;
5850 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005851
5852 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005853 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005854 if (pair == NULL) {
5855 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005856 if (!PyErr_Occurred()) {
5857 PyErr_Format(PyExc_ValueError, "unregistered extension "
5858 "code %ld", code);
5859 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005860 return -1;
5861 }
5862 /* Since the extension registry is manipulable via Python code,
5863 * confirm that pair is really a 2-tuple of strings.
5864 */
5865 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5866 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5867 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5868 Py_DECREF(py_code);
5869 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5870 "isn't a 2-tuple of strings", code);
5871 return -1;
5872 }
5873 /* Load the object. */
5874 obj = find_class(self, module_name, class_name);
5875 if (obj == NULL) {
5876 Py_DECREF(py_code);
5877 return -1;
5878 }
5879 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005880 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005881 Py_DECREF(py_code);
5882 if (code < 0) {
5883 Py_DECREF(obj);
5884 return -1;
5885 }
5886 PDATA_PUSH(self->stack, obj, -1);
5887 return 0;
5888}
5889
5890static int
5891load_put(UnpicklerObject *self)
5892{
5893 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005894 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005895 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005896 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005897
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005898 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005899 return -1;
5900 if (len < 2)
5901 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005902 if (Py_SIZE(self->stack) <= self->stack->fence)
5903 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005904 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005905
5906 key = PyLong_FromString(s, NULL, 10);
5907 if (key == NULL)
5908 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005909 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005910 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005911 if (idx < 0) {
5912 if (!PyErr_Occurred())
5913 PyErr_SetString(PyExc_ValueError,
5914 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005915 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005916 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005917
5918 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005919}
5920
5921static int
5922load_binput(UnpicklerObject *self)
5923{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005924 PyObject *value;
5925 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005926 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005927
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005928 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005929 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005930
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005931 if (Py_SIZE(self->stack) <= self->stack->fence)
5932 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005933 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005934
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005935 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005936
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005937 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005938}
5939
5940static int
5941load_long_binput(UnpicklerObject *self)
5942{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005943 PyObject *value;
5944 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005945 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005946
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005947 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005948 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005949
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005950 if (Py_SIZE(self->stack) <= self->stack->fence)
5951 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005952 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005953
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005954 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005955 if (idx < 0) {
5956 PyErr_SetString(PyExc_ValueError,
5957 "negative LONG_BINPUT argument");
5958 return -1;
5959 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005960
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005961 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005962}
5963
5964static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005965load_memoize(UnpicklerObject *self)
5966{
5967 PyObject *value;
5968
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005969 if (Py_SIZE(self->stack) <= self->stack->fence)
5970 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005971 value = self->stack->data[Py_SIZE(self->stack) - 1];
5972
5973 return _Unpickler_MemoPut(self, self->memo_len, value);
5974}
5975
5976static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005977do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005978{
5979 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005980 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005981 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005982 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005983 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005984
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005985 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005986 if (x > len || x <= self->stack->fence)
5987 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005988 if (len == x) /* nothing to do */
5989 return 0;
5990
5991 list = self->stack->data[x - 1];
5992
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005993 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005994 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005995 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005996
5997 slice = Pdata_poplist(self->stack, x);
5998 if (!slice)
5999 return -1;
6000 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006001 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006002 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006003 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006004 }
6005 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006006 PyObject *extend_func;
6007 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006008
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006009 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6010 if (extend_func != NULL) {
6011 slice = Pdata_poplist(self->stack, x);
6012 if (!slice) {
6013 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006014 return -1;
6015 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006016 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006017 Py_DECREF(extend_func);
6018 if (result == NULL)
6019 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006020 Py_DECREF(result);
6021 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006022 else {
6023 PyObject *append_func;
6024 _Py_IDENTIFIER(append);
6025
6026 /* Even if the PEP 307 requires extend() and append() methods,
6027 fall back on append() if the object has no extend() method
6028 for backward compatibility. */
6029 PyErr_Clear();
6030 append_func = _PyObject_GetAttrId(list, &PyId_append);
6031 if (append_func == NULL)
6032 return -1;
6033 for (i = x; i < len; i++) {
6034 value = self->stack->data[i];
6035 result = _Pickle_FastCall(append_func, value);
6036 if (result == NULL) {
6037 Pdata_clear(self->stack, i + 1);
6038 Py_SIZE(self->stack) = x;
6039 Py_DECREF(append_func);
6040 return -1;
6041 }
6042 Py_DECREF(result);
6043 }
6044 Py_SIZE(self->stack) = x;
6045 Py_DECREF(append_func);
6046 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006047 }
6048
6049 return 0;
6050}
6051
6052static int
6053load_append(UnpicklerObject *self)
6054{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006055 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6056 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006057 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006058}
6059
6060static int
6061load_appends(UnpicklerObject *self)
6062{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006063 Py_ssize_t i = marker(self);
6064 if (i < 0)
6065 return -1;
6066 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006067}
6068
6069static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006070do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006071{
6072 PyObject *value, *key;
6073 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006074 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075 int status = 0;
6076
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006077 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006078 if (x > len || x <= self->stack->fence)
6079 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006080 if (len == x) /* nothing to do */
6081 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006082 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006083 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006084 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006085 PyErr_SetString(st->UnpicklingError,
6086 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006087 return -1;
6088 }
6089
6090 /* Here, dict does not actually need to be a PyDict; it could be anything
6091 that supports the __setitem__ attribute. */
6092 dict = self->stack->data[x - 1];
6093
6094 for (i = x + 1; i < len; i += 2) {
6095 key = self->stack->data[i - 1];
6096 value = self->stack->data[i];
6097 if (PyObject_SetItem(dict, key, value) < 0) {
6098 status = -1;
6099 break;
6100 }
6101 }
6102
6103 Pdata_clear(self->stack, x);
6104 return status;
6105}
6106
6107static int
6108load_setitem(UnpicklerObject *self)
6109{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006110 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006111}
6112
6113static int
6114load_setitems(UnpicklerObject *self)
6115{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006116 Py_ssize_t i = marker(self);
6117 if (i < 0)
6118 return -1;
6119 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006120}
6121
6122static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006123load_additems(UnpicklerObject *self)
6124{
6125 PyObject *set;
6126 Py_ssize_t mark, len, i;
6127
6128 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006129 if (mark < 0)
6130 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006131 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006132 if (mark > len || mark <= self->stack->fence)
6133 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006134 if (len == mark) /* nothing to do */
6135 return 0;
6136
6137 set = self->stack->data[mark - 1];
6138
6139 if (PySet_Check(set)) {
6140 PyObject *items;
6141 int status;
6142
6143 items = Pdata_poptuple(self->stack, mark);
6144 if (items == NULL)
6145 return -1;
6146
6147 status = _PySet_Update(set, items);
6148 Py_DECREF(items);
6149 return status;
6150 }
6151 else {
6152 PyObject *add_func;
6153 _Py_IDENTIFIER(add);
6154
6155 add_func = _PyObject_GetAttrId(set, &PyId_add);
6156 if (add_func == NULL)
6157 return -1;
6158 for (i = mark; i < len; i++) {
6159 PyObject *result;
6160 PyObject *item;
6161
6162 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006163 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006164 if (result == NULL) {
6165 Pdata_clear(self->stack, i + 1);
6166 Py_SIZE(self->stack) = mark;
6167 return -1;
6168 }
6169 Py_DECREF(result);
6170 }
6171 Py_SIZE(self->stack) = mark;
6172 }
6173
6174 return 0;
6175}
6176
6177static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006178load_build(UnpicklerObject *self)
6179{
6180 PyObject *state, *inst, *slotstate;
6181 PyObject *setstate;
6182 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006183 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006184
6185 /* Stack is ... instance, state. We want to leave instance at
6186 * the stack top, possibly mutated via instance.__setstate__(state).
6187 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006188 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6189 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006190
6191 PDATA_POP(self->stack, state);
6192 if (state == NULL)
6193 return -1;
6194
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006195 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006196
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006197 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6198 Py_DECREF(state);
6199 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006200 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006201 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006202 PyObject *result;
6203
6204 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006205 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206 Py_DECREF(setstate);
6207 if (result == NULL)
6208 return -1;
6209 Py_DECREF(result);
6210 return 0;
6211 }
6212
6213 /* A default __setstate__. First see whether state embeds a
6214 * slot state dict too (a proto 2 addition).
6215 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006216 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006217 PyObject *tmp = state;
6218
6219 state = PyTuple_GET_ITEM(tmp, 0);
6220 slotstate = PyTuple_GET_ITEM(tmp, 1);
6221 Py_INCREF(state);
6222 Py_INCREF(slotstate);
6223 Py_DECREF(tmp);
6224 }
6225 else
6226 slotstate = NULL;
6227
6228 /* Set inst.__dict__ from the state dict (if any). */
6229 if (state != Py_None) {
6230 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006231 PyObject *d_key, *d_value;
6232 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006233 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006234
6235 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006236 PickleState *st = _Pickle_GetGlobalState();
6237 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006238 goto error;
6239 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006240 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241 if (dict == NULL)
6242 goto error;
6243
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006244 i = 0;
6245 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6246 /* normally the keys for instance attributes are
6247 interned. we should try to do that here. */
6248 Py_INCREF(d_key);
6249 if (PyUnicode_CheckExact(d_key))
6250 PyUnicode_InternInPlace(&d_key);
6251 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6252 Py_DECREF(d_key);
6253 goto error;
6254 }
6255 Py_DECREF(d_key);
6256 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006257 Py_DECREF(dict);
6258 }
6259
6260 /* Also set instance attributes from the slotstate dict (if any). */
6261 if (slotstate != NULL) {
6262 PyObject *d_key, *d_value;
6263 Py_ssize_t i;
6264
6265 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006266 PickleState *st = _Pickle_GetGlobalState();
6267 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006268 "slot state is not a dictionary");
6269 goto error;
6270 }
6271 i = 0;
6272 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6273 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6274 goto error;
6275 }
6276 }
6277
6278 if (0) {
6279 error:
6280 status = -1;
6281 }
6282
6283 Py_DECREF(state);
6284 Py_XDECREF(slotstate);
6285 return status;
6286}
6287
6288static int
6289load_mark(UnpicklerObject *self)
6290{
6291
6292 /* Note that we split the (pickle.py) stack into two stacks, an
6293 * object stack and a mark stack. Here we push a mark onto the
6294 * mark stack.
6295 */
6296
6297 if ((self->num_marks + 1) >= self->marks_size) {
6298 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006299
6300 /* Use the size_t type to check for overflow. */
6301 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006302 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006303 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006304 PyErr_NoMemory();
6305 return -1;
6306 }
6307
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006308 Py_ssize_t *marks_old = self->marks;
6309 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006310 if (self->marks == NULL) {
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006311 PyMem_FREE(marks_old);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006312 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006313 PyErr_NoMemory();
6314 return -1;
6315 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006316 self->marks_size = (Py_ssize_t)alloc;
6317 }
6318
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006319 self->stack->mark_set = 1;
6320 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006321
6322 return 0;
6323}
6324
6325static int
6326load_reduce(UnpicklerObject *self)
6327{
6328 PyObject *callable = NULL;
6329 PyObject *argtup = NULL;
6330 PyObject *obj = NULL;
6331
6332 PDATA_POP(self->stack, argtup);
6333 if (argtup == NULL)
6334 return -1;
6335 PDATA_POP(self->stack, callable);
6336 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006337 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006338 Py_DECREF(callable);
6339 }
6340 Py_DECREF(argtup);
6341
6342 if (obj == NULL)
6343 return -1;
6344
6345 PDATA_PUSH(self->stack, obj, -1);
6346 return 0;
6347}
6348
6349/* Just raises an error if we don't know the protocol specified. PROTO
6350 * is the first opcode for protocols >= 2.
6351 */
6352static int
6353load_proto(UnpicklerObject *self)
6354{
6355 char *s;
6356 int i;
6357
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006358 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006359 return -1;
6360
6361 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006362 if (i <= HIGHEST_PROTOCOL) {
6363 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006364 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006365 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006366
6367 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6368 return -1;
6369}
6370
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006371static int
6372load_frame(UnpicklerObject *self)
6373{
6374 char *s;
6375 Py_ssize_t frame_len;
6376
6377 if (_Unpickler_Read(self, &s, 8) < 0)
6378 return -1;
6379
6380 frame_len = calc_binsize(s, 8);
6381 if (frame_len < 0) {
6382 PyErr_Format(PyExc_OverflowError,
6383 "FRAME length exceeds system's maximum of %zd bytes",
6384 PY_SSIZE_T_MAX);
6385 return -1;
6386 }
6387
6388 if (_Unpickler_Read(self, &s, frame_len) < 0)
6389 return -1;
6390
6391 /* Rewind to start of frame */
6392 self->next_read_idx -= frame_len;
6393 return 0;
6394}
6395
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396static PyObject *
6397load(UnpicklerObject *self)
6398{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006399 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006400 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006401
6402 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006403 self->stack->mark_set = 0;
6404 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006405 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006406 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006407 Pdata_clear(self->stack, 0);
6408
6409 /* Convenient macros for the dispatch while-switch loop just below. */
6410#define OP(opcode, load_func) \
6411 case opcode: if (load_func(self) < 0) break; continue;
6412
6413#define OP_ARG(opcode, load_func, arg) \
6414 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6415
6416 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006417 if (_Unpickler_Read(self, &s, 1) < 0) {
6418 PickleState *st = _Pickle_GetGlobalState();
6419 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6420 PyErr_Format(PyExc_EOFError, "Ran out of input");
6421 }
6422 return NULL;
6423 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006424
6425 switch ((enum opcode)s[0]) {
6426 OP(NONE, load_none)
6427 OP(BININT, load_binint)
6428 OP(BININT1, load_binint1)
6429 OP(BININT2, load_binint2)
6430 OP(INT, load_int)
6431 OP(LONG, load_long)
6432 OP_ARG(LONG1, load_counted_long, 1)
6433 OP_ARG(LONG4, load_counted_long, 4)
6434 OP(FLOAT, load_float)
6435 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006436 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6437 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6438 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6439 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6440 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006441 OP(STRING, load_string)
6442 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006443 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6444 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6445 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006446 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6447 OP_ARG(TUPLE1, load_counted_tuple, 1)
6448 OP_ARG(TUPLE2, load_counted_tuple, 2)
6449 OP_ARG(TUPLE3, load_counted_tuple, 3)
6450 OP(TUPLE, load_tuple)
6451 OP(EMPTY_LIST, load_empty_list)
6452 OP(LIST, load_list)
6453 OP(EMPTY_DICT, load_empty_dict)
6454 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006455 OP(EMPTY_SET, load_empty_set)
6456 OP(ADDITEMS, load_additems)
6457 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006458 OP(OBJ, load_obj)
6459 OP(INST, load_inst)
6460 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006461 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006462 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006463 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006464 OP(APPEND, load_append)
6465 OP(APPENDS, load_appends)
6466 OP(BUILD, load_build)
6467 OP(DUP, load_dup)
6468 OP(BINGET, load_binget)
6469 OP(LONG_BINGET, load_long_binget)
6470 OP(GET, load_get)
6471 OP(MARK, load_mark)
6472 OP(BINPUT, load_binput)
6473 OP(LONG_BINPUT, load_long_binput)
6474 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006475 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006476 OP(POP, load_pop)
6477 OP(POP_MARK, load_pop_mark)
6478 OP(SETITEM, load_setitem)
6479 OP(SETITEMS, load_setitems)
6480 OP(PERSID, load_persid)
6481 OP(BINPERSID, load_binpersid)
6482 OP(REDUCE, load_reduce)
6483 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006484 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006485 OP_ARG(EXT1, load_extension, 1)
6486 OP_ARG(EXT2, load_extension, 2)
6487 OP_ARG(EXT4, load_extension, 4)
6488 OP_ARG(NEWTRUE, load_bool, Py_True)
6489 OP_ARG(NEWFALSE, load_bool, Py_False)
6490
6491 case STOP:
6492 break;
6493
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006494 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006495 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006496 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006497 unsigned char c = (unsigned char) *s;
6498 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6499 PyErr_Format(st->UnpicklingError,
6500 "invalid load key, '%c'.", c);
6501 }
6502 else {
6503 PyErr_Format(st->UnpicklingError,
6504 "invalid load key, '\\x%02x'.", c);
6505 }
6506 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006507 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006508 }
6509
6510 break; /* and we are done! */
6511 }
6512
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006513 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006514 return NULL;
6515 }
6516
Victor Stinner2ae57e32013-10-31 13:39:23 +01006517 if (_Unpickler_SkipConsumed(self) < 0)
6518 return NULL;
6519
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006520 PDATA_POP(self->stack, value);
6521 return value;
6522}
6523
Larry Hastings61272b72014-01-07 12:41:53 -08006524/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006525
6526_pickle.Unpickler.load
6527
6528Load a pickle.
6529
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006530Read a pickled object representation from the open file object given
6531in the constructor, and return the reconstituted object hierarchy
6532specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006533[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006534
Larry Hastings3cceb382014-01-04 11:09:09 -08006535static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006536_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006537/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006538{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006539 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006540
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006541 /* Check whether the Unpickler was initialized correctly. This prevents
6542 segfaulting if a subclass overridden __init__ with a function that does
6543 not call Unpickler.__init__(). Here, we simply ensure that self->read
6544 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006545 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006546 PickleState *st = _Pickle_GetGlobalState();
6547 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006548 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006549 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006550 return NULL;
6551 }
6552
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006553 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006554}
6555
6556/* The name of find_class() is misleading. In newer pickle protocols, this
6557 function is used for loading any global (i.e., functions), not just
6558 classes. The name is kept only for backward compatibility. */
6559
Larry Hastings61272b72014-01-07 12:41:53 -08006560/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006561
6562_pickle.Unpickler.find_class
6563
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006564 module_name: object
6565 global_name: object
6566 /
6567
6568Return an object from a specified module.
6569
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006570If necessary, the module will be imported. Subclasses may override
6571this method (e.g. to restrict unpickling of arbitrary classes and
6572functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006573
6574This method is called whenever a class or a function object is
6575needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006576[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006577
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006578static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006579_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6580 PyObject *module_name,
6581 PyObject *global_name)
6582/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006583{
6584 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006585 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006586
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006587 /* Try to map the old names used in Python 2.x to the new ones used in
6588 Python 3.x. We do this only with old pickle protocols and when the
6589 user has not disabled the feature. */
6590 if (self->proto < 3 && self->fix_imports) {
6591 PyObject *key;
6592 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006593 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006594
6595 /* Check if the global (i.e., a function or a class) was renamed
6596 or moved to another module. */
6597 key = PyTuple_Pack(2, module_name, global_name);
6598 if (key == NULL)
6599 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006600 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006601 Py_DECREF(key);
6602 if (item) {
6603 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6604 PyErr_Format(PyExc_RuntimeError,
6605 "_compat_pickle.NAME_MAPPING values should be "
6606 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6607 return NULL;
6608 }
6609 module_name = PyTuple_GET_ITEM(item, 0);
6610 global_name = PyTuple_GET_ITEM(item, 1);
6611 if (!PyUnicode_Check(module_name) ||
6612 !PyUnicode_Check(global_name)) {
6613 PyErr_Format(PyExc_RuntimeError,
6614 "_compat_pickle.NAME_MAPPING values should be "
6615 "pairs of str, not (%.200s, %.200s)",
6616 Py_TYPE(module_name)->tp_name,
6617 Py_TYPE(global_name)->tp_name);
6618 return NULL;
6619 }
6620 }
6621 else if (PyErr_Occurred()) {
6622 return NULL;
6623 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006624 else {
6625 /* Check if the module was renamed. */
6626 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6627 if (item) {
6628 if (!PyUnicode_Check(item)) {
6629 PyErr_Format(PyExc_RuntimeError,
6630 "_compat_pickle.IMPORT_MAPPING values should be "
6631 "strings, not %.200s", Py_TYPE(item)->tp_name);
6632 return NULL;
6633 }
6634 module_name = item;
6635 }
6636 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006637 return NULL;
6638 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006639 }
6640 }
6641
Miss Islington (bot)31294322019-02-18 07:52:32 -08006642 /*
6643 * we don't use PyImport_GetModule here, because it can return partially-
6644 * initialised modules, which then cause the getattribute to fail.
6645 */
6646 module = PyImport_Import(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006647 if (module == NULL) {
Miss Islington (bot)31294322019-02-18 07:52:32 -08006648 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006649 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006650 global = getattribute(module, global_name, self->proto >= 4);
6651 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006652 return global;
6653}
6654
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006655/*[clinic input]
6656
6657_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6658
6659Returns size in memory, in bytes.
6660[clinic start generated code]*/
6661
6662static Py_ssize_t
6663_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6664/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6665{
6666 Py_ssize_t res;
6667
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006668 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006669 if (self->memo != NULL)
6670 res += self->memo_size * sizeof(PyObject *);
6671 if (self->marks != NULL)
6672 res += self->marks_size * sizeof(Py_ssize_t);
6673 if (self->input_line != NULL)
6674 res += strlen(self->input_line) + 1;
6675 if (self->encoding != NULL)
6676 res += strlen(self->encoding) + 1;
6677 if (self->errors != NULL)
6678 res += strlen(self->errors) + 1;
6679 return res;
6680}
6681
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006682static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006683 _PICKLE_UNPICKLER_LOAD_METHODDEF
6684 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006685 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006686 {NULL, NULL} /* sentinel */
6687};
6688
6689static void
6690Unpickler_dealloc(UnpicklerObject *self)
6691{
6692 PyObject_GC_UnTrack((PyObject *)self);
6693 Py_XDECREF(self->readline);
6694 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006695 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006696 Py_XDECREF(self->stack);
6697 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006698 if (self->buffer.buf != NULL) {
6699 PyBuffer_Release(&self->buffer);
6700 self->buffer.buf = NULL;
6701 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006702
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006703 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006704 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006705 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006706 PyMem_Free(self->encoding);
6707 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006708
6709 Py_TYPE(self)->tp_free((PyObject *)self);
6710}
6711
6712static int
6713Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6714{
6715 Py_VISIT(self->readline);
6716 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006717 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006718 Py_VISIT(self->stack);
6719 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006720 return 0;
6721}
6722
6723static int
6724Unpickler_clear(UnpicklerObject *self)
6725{
6726 Py_CLEAR(self->readline);
6727 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006728 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006729 Py_CLEAR(self->stack);
6730 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006731 if (self->buffer.buf != NULL) {
6732 PyBuffer_Release(&self->buffer);
6733 self->buffer.buf = NULL;
6734 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006735
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006736 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006737 PyMem_Free(self->marks);
6738 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006739 PyMem_Free(self->input_line);
6740 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006741 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006742 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006743 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006744 self->errors = NULL;
6745
6746 return 0;
6747}
6748
Larry Hastings61272b72014-01-07 12:41:53 -08006749/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006750
6751_pickle.Unpickler.__init__
6752
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006753 file: object
6754 *
6755 fix_imports: bool = True
6756 encoding: str = 'ASCII'
6757 errors: str = 'strict'
6758
6759This takes a binary file for reading a pickle data stream.
6760
6761The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006762protocol argument is needed. Bytes past the pickled object's
6763representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006764
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006765The argument *file* must have two methods, a read() method that takes
6766an integer argument, and a readline() method that requires no
6767arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006768binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006769other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006770
6771Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006772which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006773generated by Python 2. If *fix_imports* is True, pickle will try to
6774map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006775*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006776instances pickled by Python 2; these default to 'ASCII' and 'strict',
6777respectively. The *encoding* can be 'bytes' to read these 8-bit
6778string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006779[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006780
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006781static int
Larry Hastings89964c42015-04-14 18:07:59 -04006782_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6783 int fix_imports, const char *encoding,
6784 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006785/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006786{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006787 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006788
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006789 /* In case of multiple __init__() calls, clear previous content. */
6790 if (self->read != NULL)
6791 (void)Unpickler_clear(self);
6792
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006793 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006794 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006795
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006796 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006797 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006798
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006799 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006800
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006801 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6802 &self->pers_func, &self->pers_func_self) < 0)
6803 {
6804 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006805 }
6806
6807 self->stack = (Pdata *)Pdata_New();
6808 if (self->stack == NULL)
Miss Islington (bot)758ad542018-09-28 23:01:48 -07006809 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006810
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006811 self->memo_size = 32;
6812 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006813 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006814 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006815
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006816 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006817
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006818 return 0;
6819}
6820
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006821
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006822/* Define a proxy object for the Unpickler's internal memo object. This is to
6823 * avoid breaking code like:
6824 * unpickler.memo.clear()
6825 * and
6826 * unpickler.memo = saved_memo
6827 * Is this a good idea? Not really, but we don't want to break code that uses
6828 * it. Note that we don't implement the entire mapping API here. This is
6829 * intentional, as these should be treated as black-box implementation details.
6830 *
6831 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006832 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006833 */
6834
Larry Hastings61272b72014-01-07 12:41:53 -08006835/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006836_pickle.UnpicklerMemoProxy.clear
6837
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006838Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006839[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006840
Larry Hastings3cceb382014-01-04 11:09:09 -08006841static PyObject *
6842_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006843/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006844{
6845 _Unpickler_MemoCleanup(self->unpickler);
6846 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6847 if (self->unpickler->memo == NULL)
6848 return NULL;
6849 Py_RETURN_NONE;
6850}
6851
Larry Hastings61272b72014-01-07 12:41:53 -08006852/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006853_pickle.UnpicklerMemoProxy.copy
6854
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006855Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006856[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006857
Larry Hastings3cceb382014-01-04 11:09:09 -08006858static PyObject *
6859_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006860/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006861{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07006862 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006863 PyObject *new_memo = PyDict_New();
6864 if (new_memo == NULL)
6865 return NULL;
6866
6867 for (i = 0; i < self->unpickler->memo_size; i++) {
6868 int status;
6869 PyObject *key, *value;
6870
6871 value = self->unpickler->memo[i];
6872 if (value == NULL)
6873 continue;
6874
6875 key = PyLong_FromSsize_t(i);
6876 if (key == NULL)
6877 goto error;
6878 status = PyDict_SetItem(new_memo, key, value);
6879 Py_DECREF(key);
6880 if (status < 0)
6881 goto error;
6882 }
6883 return new_memo;
6884
6885error:
6886 Py_DECREF(new_memo);
6887 return NULL;
6888}
6889
Larry Hastings61272b72014-01-07 12:41:53 -08006890/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006891_pickle.UnpicklerMemoProxy.__reduce__
6892
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006893Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006894[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006895
Larry Hastings3cceb382014-01-04 11:09:09 -08006896static PyObject *
6897_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006898/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006899{
6900 PyObject *reduce_value;
6901 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006902 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006903 if (contents == NULL)
6904 return NULL;
6905
6906 reduce_value = PyTuple_New(2);
6907 if (reduce_value == NULL) {
6908 Py_DECREF(contents);
6909 return NULL;
6910 }
6911 constructor_args = PyTuple_New(1);
6912 if (constructor_args == NULL) {
6913 Py_DECREF(contents);
6914 Py_DECREF(reduce_value);
6915 return NULL;
6916 }
6917 PyTuple_SET_ITEM(constructor_args, 0, contents);
6918 Py_INCREF((PyObject *)&PyDict_Type);
6919 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6920 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6921 return reduce_value;
6922}
6923
6924static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006925 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6926 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6927 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006928 {NULL, NULL} /* sentinel */
6929};
6930
6931static void
6932UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6933{
6934 PyObject_GC_UnTrack(self);
6935 Py_XDECREF(self->unpickler);
6936 PyObject_GC_Del((PyObject *)self);
6937}
6938
6939static int
6940UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6941 visitproc visit, void *arg)
6942{
6943 Py_VISIT(self->unpickler);
6944 return 0;
6945}
6946
6947static int
6948UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6949{
6950 Py_CLEAR(self->unpickler);
6951 return 0;
6952}
6953
6954static PyTypeObject UnpicklerMemoProxyType = {
6955 PyVarObject_HEAD_INIT(NULL, 0)
6956 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6957 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6958 0,
6959 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6960 0, /* tp_print */
6961 0, /* tp_getattr */
6962 0, /* tp_setattr */
6963 0, /* tp_compare */
6964 0, /* tp_repr */
6965 0, /* tp_as_number */
6966 0, /* tp_as_sequence */
6967 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006968 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006969 0, /* tp_call */
6970 0, /* tp_str */
6971 PyObject_GenericGetAttr, /* tp_getattro */
6972 PyObject_GenericSetAttr, /* tp_setattro */
6973 0, /* tp_as_buffer */
6974 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6975 0, /* tp_doc */
6976 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6977 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6978 0, /* tp_richcompare */
6979 0, /* tp_weaklistoffset */
6980 0, /* tp_iter */
6981 0, /* tp_iternext */
6982 unpicklerproxy_methods, /* tp_methods */
6983};
6984
6985static PyObject *
6986UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6987{
6988 UnpicklerMemoProxyObject *self;
6989
6990 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6991 &UnpicklerMemoProxyType);
6992 if (self == NULL)
6993 return NULL;
6994 Py_INCREF(unpickler);
6995 self->unpickler = unpickler;
6996 PyObject_GC_Track(self);
6997 return (PyObject *)self;
6998}
6999
7000/*****************************************************************************/
7001
7002
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007003static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007004Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007005{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007006 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007007}
7008
7009static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007010Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007011{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007012 PyObject **new_memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007013 size_t new_memo_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007014
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007015 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007016 PyErr_SetString(PyExc_TypeError,
7017 "attribute deletion is not supported");
7018 return -1;
7019 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007020
7021 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7022 UnpicklerObject *unpickler =
7023 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7024
7025 new_memo_size = unpickler->memo_size;
7026 new_memo = _Unpickler_NewMemo(new_memo_size);
7027 if (new_memo == NULL)
7028 return -1;
7029
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007030 for (size_t i = 0; i < new_memo_size; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007031 Py_XINCREF(unpickler->memo[i]);
7032 new_memo[i] = unpickler->memo[i];
7033 }
7034 }
7035 else if (PyDict_Check(obj)) {
7036 Py_ssize_t i = 0;
7037 PyObject *key, *value;
7038
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007039 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007040 new_memo = _Unpickler_NewMemo(new_memo_size);
7041 if (new_memo == NULL)
7042 return -1;
7043
7044 while (PyDict_Next(obj, &i, &key, &value)) {
7045 Py_ssize_t idx;
7046 if (!PyLong_Check(key)) {
7047 PyErr_SetString(PyExc_TypeError,
7048 "memo key must be integers");
7049 goto error;
7050 }
7051 idx = PyLong_AsSsize_t(key);
7052 if (idx == -1 && PyErr_Occurred())
7053 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007054 if (idx < 0) {
7055 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007056 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007057 goto error;
7058 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007059 if (_Unpickler_MemoPut(self, idx, value) < 0)
7060 goto error;
7061 }
7062 }
7063 else {
7064 PyErr_Format(PyExc_TypeError,
Miss Islington (bot)7beb8c52018-11-05 06:52:58 -08007065 "'memo' attribute must be an UnpicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007066 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007067 return -1;
7068 }
7069
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007070 _Unpickler_MemoCleanup(self);
7071 self->memo_size = new_memo_size;
7072 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007073
7074 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007075
7076 error:
7077 if (new_memo_size) {
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007078 for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007079 Py_XDECREF(new_memo[i]);
7080 }
7081 PyMem_FREE(new_memo);
7082 }
7083 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007084}
7085
7086static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007087Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007088{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007089 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007090 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007091 return NULL;
7092 }
7093 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007094}
7095
7096static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007097Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007098{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007099 if (value == NULL) {
7100 PyErr_SetString(PyExc_TypeError,
7101 "attribute deletion is not supported");
7102 return -1;
7103 }
7104 if (!PyCallable_Check(value)) {
7105 PyErr_SetString(PyExc_TypeError,
7106 "persistent_load must be a callable taking "
7107 "one argument");
7108 return -1;
7109 }
7110
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007111 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007112 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007113 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007114
7115 return 0;
7116}
7117
7118static PyGetSetDef Unpickler_getsets[] = {
7119 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7120 {"persistent_load", (getter)Unpickler_get_persload,
7121 (setter)Unpickler_set_persload},
7122 {NULL}
7123};
7124
7125static PyTypeObject Unpickler_Type = {
7126 PyVarObject_HEAD_INIT(NULL, 0)
7127 "_pickle.Unpickler", /*tp_name*/
7128 sizeof(UnpicklerObject), /*tp_basicsize*/
7129 0, /*tp_itemsize*/
7130 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7131 0, /*tp_print*/
7132 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007133 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007134 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007135 0, /*tp_repr*/
7136 0, /*tp_as_number*/
7137 0, /*tp_as_sequence*/
7138 0, /*tp_as_mapping*/
7139 0, /*tp_hash*/
7140 0, /*tp_call*/
7141 0, /*tp_str*/
7142 0, /*tp_getattro*/
7143 0, /*tp_setattro*/
7144 0, /*tp_as_buffer*/
7145 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007146 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007147 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7148 (inquiry)Unpickler_clear, /*tp_clear*/
7149 0, /*tp_richcompare*/
7150 0, /*tp_weaklistoffset*/
7151 0, /*tp_iter*/
7152 0, /*tp_iternext*/
7153 Unpickler_methods, /*tp_methods*/
7154 0, /*tp_members*/
7155 Unpickler_getsets, /*tp_getset*/
7156 0, /*tp_base*/
7157 0, /*tp_dict*/
7158 0, /*tp_descr_get*/
7159 0, /*tp_descr_set*/
7160 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007161 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007162 PyType_GenericAlloc, /*tp_alloc*/
7163 PyType_GenericNew, /*tp_new*/
7164 PyObject_GC_Del, /*tp_free*/
7165 0, /*tp_is_gc*/
7166};
7167
Larry Hastings61272b72014-01-07 12:41:53 -08007168/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007169
7170_pickle.dump
7171
7172 obj: object
7173 file: object
7174 protocol: object = NULL
7175 *
7176 fix_imports: bool = True
7177
7178Write a pickled representation of obj to the open file object file.
7179
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007180This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7181be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007182
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007183The optional *protocol* argument tells the pickler to use the given
7184protocol supported protocols are 0, 1, 2, 3 and 4. The default
7185protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007186
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007187Specifying a negative protocol version selects the highest protocol
7188version supported. The higher the protocol used, the more recent the
7189version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007190
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007191The *file* argument must have a write() method that accepts a single
7192bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007193writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007194this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007195
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007196If *fix_imports* is True and protocol is less than 3, pickle will try
7197to map the new Python 3 names to the old module names used in Python
71982, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007199[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007200
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007201static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007202_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007203 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007204/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007205{
7206 PicklerObject *pickler = _Pickler_New();
7207
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007208 if (pickler == NULL)
7209 return NULL;
7210
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007211 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007212 goto error;
7213
7214 if (_Pickler_SetOutputStream(pickler, file) < 0)
7215 goto error;
7216
7217 if (dump(pickler, obj) < 0)
7218 goto error;
7219
7220 if (_Pickler_FlushToFile(pickler) < 0)
7221 goto error;
7222
7223 Py_DECREF(pickler);
7224 Py_RETURN_NONE;
7225
7226 error:
7227 Py_XDECREF(pickler);
7228 return NULL;
7229}
7230
Larry Hastings61272b72014-01-07 12:41:53 -08007231/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007232
7233_pickle.dumps
7234
7235 obj: object
7236 protocol: object = NULL
7237 *
7238 fix_imports: bool = True
7239
7240Return the pickled representation of the object as a bytes object.
7241
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007242The optional *protocol* argument tells the pickler to use the given
7243protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7244protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007245
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007246Specifying a negative protocol version selects the highest protocol
7247version supported. The higher the protocol used, the more recent the
7248version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007249
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007250If *fix_imports* is True and *protocol* is less than 3, pickle will
7251try to map the new Python 3 names to the old module names used in
7252Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007253[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007254
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007255static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007256_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007257 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007258/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007259{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007260 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007261 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007262
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007263 if (pickler == NULL)
7264 return NULL;
7265
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007266 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007267 goto error;
7268
7269 if (dump(pickler, obj) < 0)
7270 goto error;
7271
7272 result = _Pickler_GetString(pickler);
7273 Py_DECREF(pickler);
7274 return result;
7275
7276 error:
7277 Py_XDECREF(pickler);
7278 return NULL;
7279}
7280
Larry Hastings61272b72014-01-07 12:41:53 -08007281/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007282
7283_pickle.load
7284
7285 file: object
7286 *
7287 fix_imports: bool = True
7288 encoding: str = 'ASCII'
7289 errors: str = 'strict'
7290
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007291Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007292
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007293This is equivalent to ``Unpickler(file).load()``, but may be more
7294efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007295
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007296The protocol version of the pickle is detected automatically, so no
7297protocol argument is needed. Bytes past the pickled object's
7298representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007299
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007300The argument *file* must have two methods, a read() method that takes
7301an integer argument, and a readline() method that requires no
7302arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007303binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007304other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007305
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007306Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007307which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007308generated by Python 2. If *fix_imports* is True, pickle will try to
7309map the old Python 2 names to the new names used in Python 3. The
7310*encoding* and *errors* tell pickle how to decode 8-bit string
7311instances pickled by Python 2; these default to 'ASCII' and 'strict',
7312respectively. The *encoding* can be 'bytes' to read these 8-bit
7313string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007314[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007315
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007316static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007317_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007318 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007319/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007320{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007321 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007322 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007323
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007324 if (unpickler == NULL)
7325 return NULL;
7326
7327 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7328 goto error;
7329
7330 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7331 goto error;
7332
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007333 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007334
7335 result = load(unpickler);
7336 Py_DECREF(unpickler);
7337 return result;
7338
7339 error:
7340 Py_XDECREF(unpickler);
7341 return NULL;
7342}
7343
Larry Hastings61272b72014-01-07 12:41:53 -08007344/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007345
7346_pickle.loads
7347
7348 data: object
7349 *
7350 fix_imports: bool = True
7351 encoding: str = 'ASCII'
7352 errors: str = 'strict'
7353
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007354Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007355
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007356The protocol version of the pickle is detected automatically, so no
7357protocol argument is needed. Bytes past the pickled object's
7358representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007359
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007360Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007361which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007362generated by Python 2. If *fix_imports* is True, pickle will try to
7363map the old Python 2 names to the new names used in Python 3. The
7364*encoding* and *errors* tell pickle how to decode 8-bit string
7365instances pickled by Python 2; these default to 'ASCII' and 'strict',
7366respectively. The *encoding* can be 'bytes' to read these 8-bit
7367string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007368[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007369
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007370static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007371_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007372 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007373/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007374{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007375 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007376 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007377
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007378 if (unpickler == NULL)
7379 return NULL;
7380
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007381 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007382 goto error;
7383
7384 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7385 goto error;
7386
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007387 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007388
7389 result = load(unpickler);
7390 Py_DECREF(unpickler);
7391 return result;
7392
7393 error:
7394 Py_XDECREF(unpickler);
7395 return NULL;
7396}
7397
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007398static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007399 _PICKLE_DUMP_METHODDEF
7400 _PICKLE_DUMPS_METHODDEF
7401 _PICKLE_LOAD_METHODDEF
7402 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007403 {NULL, NULL} /* sentinel */
7404};
7405
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007406static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007407pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007408{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007409 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007410 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007411}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007412
Stefan Krahf483b0f2013-12-14 13:43:10 +01007413static void
7414pickle_free(PyObject *m)
7415{
7416 _Pickle_ClearState(_Pickle_GetState(m));
7417}
7418
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007419static int
7420pickle_traverse(PyObject *m, visitproc visit, void *arg)
7421{
7422 PickleState *st = _Pickle_GetState(m);
7423 Py_VISIT(st->PickleError);
7424 Py_VISIT(st->PicklingError);
7425 Py_VISIT(st->UnpicklingError);
7426 Py_VISIT(st->dispatch_table);
7427 Py_VISIT(st->extension_registry);
7428 Py_VISIT(st->extension_cache);
7429 Py_VISIT(st->inverted_registry);
7430 Py_VISIT(st->name_mapping_2to3);
7431 Py_VISIT(st->import_mapping_2to3);
7432 Py_VISIT(st->name_mapping_3to2);
7433 Py_VISIT(st->import_mapping_3to2);
7434 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007435 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007436 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007437}
7438
7439static struct PyModuleDef _picklemodule = {
7440 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007441 "_pickle", /* m_name */
7442 pickle_module_doc, /* m_doc */
7443 sizeof(PickleState), /* m_size */
7444 pickle_methods, /* m_methods */
7445 NULL, /* m_reload */
7446 pickle_traverse, /* m_traverse */
7447 pickle_clear, /* m_clear */
7448 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007449};
7450
7451PyMODINIT_FUNC
7452PyInit__pickle(void)
7453{
7454 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007455 PickleState *st;
7456
7457 m = PyState_FindModule(&_picklemodule);
7458 if (m) {
7459 Py_INCREF(m);
7460 return m;
7461 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007462
7463 if (PyType_Ready(&Unpickler_Type) < 0)
7464 return NULL;
7465 if (PyType_Ready(&Pickler_Type) < 0)
7466 return NULL;
7467 if (PyType_Ready(&Pdata_Type) < 0)
7468 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007469 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7470 return NULL;
7471 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7472 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007473
7474 /* Create the module and add the functions. */
7475 m = PyModule_Create(&_picklemodule);
7476 if (m == NULL)
7477 return NULL;
7478
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007479 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007480 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7481 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007482 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007483 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7484 return NULL;
7485
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007486 st = _Pickle_GetState(m);
7487
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007488 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007489 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7490 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007491 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007492 st->PicklingError = \
7493 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7494 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007495 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007496 st->UnpicklingError = \
7497 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7498 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007499 return NULL;
7500
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007501 Py_INCREF(st->PickleError);
7502 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007503 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007504 Py_INCREF(st->PicklingError);
7505 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007506 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007507 Py_INCREF(st->UnpicklingError);
7508 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007509 return NULL;
7510
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007511 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007512 return NULL;
7513
7514 return m;
7515}