blob: 87f3cf7b614aabac89caf6aeafc0decc23546043 [file] [log] [blame]
Victor Stinner5c75f372019-04-17 23:02:26 +02001/* pickle accelerator C extensor: _pickle module.
2 *
3 * It is built as a built-in module (Py_BUILD_CORE_BUILTIN define) on Windows
4 * and as an extension module (Py_BUILD_CORE_MODULE define) on other
5 * platforms. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06006
Victor Stinner5c75f372019-04-17 23:02:26 +02007#if !defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE_MODULE)
8# error "Py_BUILD_CORE_BUILTIN or Py_BUILD_CORE_MODULE must be defined"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06009#endif
10
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000011#include "Python.h"
12#include "structmember.h"
13
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -080014PyDoc_STRVAR(pickle_module_doc,
15"Optimized C implementation for the Python pickle module.");
16
Larry Hastings61272b72014-01-07 12:41:53 -080017/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080018module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080019class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
20class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
21class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
22class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080023[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030024/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080025
Łukasz Langac51d8c92018-04-03 23:06:53 -070026/* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol.
27 Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python
28 already includes it. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010030 HIGHEST_PROTOCOL = 4,
Łukasz Langac51d8c92018-04-03 23:06:53 -070031 DEFAULT_PROTOCOL = 4
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000032};
33
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000034/* Pickle opcodes. These must be kept updated with pickle.py.
35 Extensive docs are in pickletools.py. */
36enum opcode {
37 MARK = '(',
38 STOP = '.',
39 POP = '0',
40 POP_MARK = '1',
41 DUP = '2',
42 FLOAT = 'F',
43 INT = 'I',
44 BININT = 'J',
45 BININT1 = 'K',
46 LONG = 'L',
47 BININT2 = 'M',
48 NONE = 'N',
49 PERSID = 'P',
50 BINPERSID = 'Q',
51 REDUCE = 'R',
52 STRING = 'S',
53 BINSTRING = 'T',
54 SHORT_BINSTRING = 'U',
55 UNICODE = 'V',
56 BINUNICODE = 'X',
57 APPEND = 'a',
58 BUILD = 'b',
59 GLOBAL = 'c',
60 DICT = 'd',
61 EMPTY_DICT = '}',
62 APPENDS = 'e',
63 GET = 'g',
64 BINGET = 'h',
65 INST = 'i',
66 LONG_BINGET = 'j',
67 LIST = 'l',
68 EMPTY_LIST = ']',
69 OBJ = 'o',
70 PUT = 'p',
71 BINPUT = 'q',
72 LONG_BINPUT = 'r',
73 SETITEM = 's',
74 TUPLE = 't',
75 EMPTY_TUPLE = ')',
76 SETITEMS = 'u',
77 BINFLOAT = 'G',
78
79 /* Protocol 2. */
80 PROTO = '\x80',
81 NEWOBJ = '\x81',
82 EXT1 = '\x82',
83 EXT2 = '\x83',
84 EXT4 = '\x84',
85 TUPLE1 = '\x85',
86 TUPLE2 = '\x86',
87 TUPLE3 = '\x87',
88 NEWTRUE = '\x88',
89 NEWFALSE = '\x89',
90 LONG1 = '\x8a',
91 LONG4 = '\x8b',
92
93 /* Protocol 3 (Python 3.x) */
94 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010095 SHORT_BINBYTES = 'C',
96
97 /* Protocol 4 */
98 SHORT_BINUNICODE = '\x8c',
99 BINUNICODE8 = '\x8d',
100 BINBYTES8 = '\x8e',
101 EMPTY_SET = '\x8f',
102 ADDITEMS = '\x90',
103 FROZENSET = '\x91',
104 NEWOBJ_EX = '\x92',
105 STACK_GLOBAL = '\x93',
106 MEMOIZE = '\x94',
107 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000108};
109
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000110enum {
111 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
112 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
113 break if this gets out of synch with pickle.py, but it's unclear that would
114 help anything either. */
115 BATCHSIZE = 1000,
116
117 /* Nesting limit until Pickler, when running in "fast mode", starts
118 checking for self-referential data-structures. */
119 FAST_NESTING_LIMIT = 50,
120
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000121 /* Initial size of the write buffer of Pickler. */
122 WRITE_BUF_SIZE = 4096,
123
Antoine Pitrou04248a82010-10-12 20:51:21 +0000124 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100125 PREFETCH = 8192 * 16,
126
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200127 FRAME_SIZE_MIN = 4,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100128 FRAME_SIZE_TARGET = 64 * 1024,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100129 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000130};
131
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800132/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800134/* State of the pickle module, per PEP 3121. */
135typedef struct {
136 /* Exception classes for pickle. */
137 PyObject *PickleError;
138 PyObject *PicklingError;
139 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* copyreg.dispatch_table, {type_object: pickling_function} */
142 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000143
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800144 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000145
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800146 /* copyreg._extension_registry, {(module_name, function_name): code} */
147 PyObject *extension_registry;
148 /* copyreg._extension_cache, {code: object} */
149 PyObject *extension_cache;
150 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
151 PyObject *inverted_registry;
152
153 /* Import mappings for compatibility with Python 2.x */
154
155 /* _compat_pickle.NAME_MAPPING,
156 {(oldmodule, oldname): (newmodule, newname)} */
157 PyObject *name_mapping_2to3;
158 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
159 PyObject *import_mapping_2to3;
160 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
161 PyObject *name_mapping_3to2;
162 PyObject *import_mapping_3to2;
163
164 /* codecs.encode, used for saving bytes in older protocols */
165 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300166 /* builtins.getattr, used for saving nested names with protocol < 4 */
167 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300168 /* functools.partial, used for implementing __newobj_ex__ with protocols
169 2 and 3 */
170 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800171} PickleState;
172
173/* Forward declaration of the _pickle module definition. */
174static struct PyModuleDef _picklemodule;
175
176/* Given a module object, get its per-module state. */
177static PickleState *
178_Pickle_GetState(PyObject *module)
179{
180 return (PickleState *)PyModule_GetState(module);
181}
182
183/* Find the module instance imported in the currently running sub-interpreter
184 and get its state. */
185static PickleState *
186_Pickle_GetGlobalState(void)
187{
188 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
189}
190
191/* Clear the given pickle module state. */
192static void
193_Pickle_ClearState(PickleState *st)
194{
195 Py_CLEAR(st->PickleError);
196 Py_CLEAR(st->PicklingError);
197 Py_CLEAR(st->UnpicklingError);
198 Py_CLEAR(st->dispatch_table);
199 Py_CLEAR(st->extension_registry);
200 Py_CLEAR(st->extension_cache);
201 Py_CLEAR(st->inverted_registry);
202 Py_CLEAR(st->name_mapping_2to3);
203 Py_CLEAR(st->import_mapping_2to3);
204 Py_CLEAR(st->name_mapping_3to2);
205 Py_CLEAR(st->import_mapping_3to2);
206 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300207 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100208 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800209}
210
211/* Initialize the given pickle module state. */
212static int
213_Pickle_InitState(PickleState *st)
214{
215 PyObject *copyreg = NULL;
216 PyObject *compat_pickle = NULL;
217 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300218 PyObject *functools = NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200219 _Py_IDENTIFIER(getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800220
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200221 st->getattr = _PyEval_GetBuiltinId(&PyId_getattr);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300222 if (st->getattr == NULL)
223 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300224
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800225 copyreg = PyImport_ImportModule("copyreg");
226 if (!copyreg)
227 goto error;
228 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
229 if (!st->dispatch_table)
230 goto error;
231 if (!PyDict_CheckExact(st->dispatch_table)) {
232 PyErr_Format(PyExc_RuntimeError,
233 "copyreg.dispatch_table should be a dict, not %.200s",
234 Py_TYPE(st->dispatch_table)->tp_name);
235 goto error;
236 }
237 st->extension_registry = \
238 PyObject_GetAttrString(copyreg, "_extension_registry");
239 if (!st->extension_registry)
240 goto error;
241 if (!PyDict_CheckExact(st->extension_registry)) {
242 PyErr_Format(PyExc_RuntimeError,
243 "copyreg._extension_registry should be a dict, "
244 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
245 goto error;
246 }
247 st->inverted_registry = \
248 PyObject_GetAttrString(copyreg, "_inverted_registry");
249 if (!st->inverted_registry)
250 goto error;
251 if (!PyDict_CheckExact(st->inverted_registry)) {
252 PyErr_Format(PyExc_RuntimeError,
253 "copyreg._inverted_registry should be a dict, "
254 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
255 goto error;
256 }
257 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
258 if (!st->extension_cache)
259 goto error;
260 if (!PyDict_CheckExact(st->extension_cache)) {
261 PyErr_Format(PyExc_RuntimeError,
262 "copyreg._extension_cache should be a dict, "
263 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
264 goto error;
265 }
266 Py_CLEAR(copyreg);
267
268 /* Load the 2.x -> 3.x stdlib module mapping tables */
269 compat_pickle = PyImport_ImportModule("_compat_pickle");
270 if (!compat_pickle)
271 goto error;
272 st->name_mapping_2to3 = \
273 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
274 if (!st->name_mapping_2to3)
275 goto error;
276 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
277 PyErr_Format(PyExc_RuntimeError,
278 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
279 Py_TYPE(st->name_mapping_2to3)->tp_name);
280 goto error;
281 }
282 st->import_mapping_2to3 = \
283 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
284 if (!st->import_mapping_2to3)
285 goto error;
286 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
287 PyErr_Format(PyExc_RuntimeError,
288 "_compat_pickle.IMPORT_MAPPING should be a dict, "
289 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
290 goto error;
291 }
292 /* ... and the 3.x -> 2.x mapping tables */
293 st->name_mapping_3to2 = \
294 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
295 if (!st->name_mapping_3to2)
296 goto error;
297 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
298 PyErr_Format(PyExc_RuntimeError,
299 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
300 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
301 goto error;
302 }
303 st->import_mapping_3to2 = \
304 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
305 if (!st->import_mapping_3to2)
306 goto error;
307 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
308 PyErr_Format(PyExc_RuntimeError,
309 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
310 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
311 goto error;
312 }
313 Py_CLEAR(compat_pickle);
314
315 codecs = PyImport_ImportModule("codecs");
316 if (codecs == NULL)
317 goto error;
318 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
319 if (st->codecs_encode == NULL) {
320 goto error;
321 }
322 if (!PyCallable_Check(st->codecs_encode)) {
323 PyErr_Format(PyExc_RuntimeError,
324 "codecs.encode should be a callable, not %.200s",
325 Py_TYPE(st->codecs_encode)->tp_name);
326 goto error;
327 }
328 Py_CLEAR(codecs);
329
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300330 functools = PyImport_ImportModule("functools");
331 if (!functools)
332 goto error;
333 st->partial = PyObject_GetAttrString(functools, "partial");
334 if (!st->partial)
335 goto error;
336 Py_CLEAR(functools);
337
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800338 return 0;
339
340 error:
341 Py_CLEAR(copyreg);
342 Py_CLEAR(compat_pickle);
343 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300344 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800345 _Pickle_ClearState(st);
346 return -1;
347}
348
349/* Helper for calling a function with a single argument quickly.
350
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800351 This function steals the reference of the given argument. */
352static PyObject *
353_Pickle_FastCall(PyObject *func, PyObject *obj)
354{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800355 PyObject *result;
356
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100357 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200358 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800359 return result;
360}
361
362/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000363
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200364/* Retrieve and deconstruct a method for avoiding a reference cycle
365 (pickler -> bound method of pickler -> pickler) */
366static int
367init_method_ref(PyObject *self, _Py_Identifier *name,
368 PyObject **method_func, PyObject **method_self)
369{
370 PyObject *func, *func2;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200371 int ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200372
373 /* *method_func and *method_self should be consistent. All refcount decrements
374 should be occurred after setting *method_self and *method_func. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200375 ret = _PyObject_LookupAttrId(self, name, &func);
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200376 if (func == NULL) {
377 *method_self = NULL;
378 Py_CLEAR(*method_func);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200379 return ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200380 }
381
382 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
383 /* Deconstruct a bound Python method */
384 func2 = PyMethod_GET_FUNCTION(func);
385 Py_INCREF(func2);
386 *method_self = self; /* borrowed */
387 Py_XSETREF(*method_func, func2);
388 Py_DECREF(func);
389 return 0;
390 }
391 else {
392 *method_self = NULL;
393 Py_XSETREF(*method_func, func);
394 return 0;
395 }
396}
397
398/* Bind a method if it was deconstructed */
399static PyObject *
400reconstruct_method(PyObject *func, PyObject *self)
401{
402 if (self) {
403 return PyMethod_New(func, self);
404 }
405 else {
406 Py_INCREF(func);
407 return func;
408 }
409}
410
411static PyObject *
412call_method(PyObject *func, PyObject *self, PyObject *obj)
413{
414 if (self) {
415 return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
416 }
417 else {
418 return PyObject_CallFunctionObjArgs(func, obj, NULL);
419 }
420}
421
422/*************************************************************************/
423
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000424/* Internal data type used as the unpickling stack. */
425typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000426 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000427 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200428 int mark_set; /* is MARK set? */
429 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000430 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000431} Pdata;
432
433static void
434Pdata_dealloc(Pdata *self)
435{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200436 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000437 while (--i >= 0) {
438 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000439 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000440 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000441 PyObject_Del(self);
442}
443
444static PyTypeObject Pdata_Type = {
445 PyVarObject_HEAD_INIT(NULL, 0)
446 "_pickle.Pdata", /*tp_name*/
447 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200448 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000449 (destructor)Pdata_dealloc, /*tp_dealloc*/
450};
451
452static PyObject *
453Pdata_New(void)
454{
455 Pdata *self;
456
457 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
458 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000459 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200460 self->mark_set = 0;
461 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000462 self->allocated = 8;
463 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000464 if (self->data)
465 return (PyObject *)self;
466 Py_DECREF(self);
467 return PyErr_NoMemory();
468}
469
470
471/* Retain only the initial clearto items. If clearto >= the current
472 * number of items, this is a (non-erroneous) NOP.
473 */
474static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200475Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000476{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200477 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000478
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200479 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000480 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000481 return 0;
482
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000483 while (--i >= clearto) {
484 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000485 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000486 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000487 return 0;
488}
489
490static int
491Pdata_grow(Pdata *self)
492{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000493 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200494 size_t allocated = (size_t)self->allocated;
495 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000496
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000497 new_allocated = (allocated >> 3) + 6;
498 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200499 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000500 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000501 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500502 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000503 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000504 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000505
506 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200507 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000508 return 0;
509
510 nomemory:
511 PyErr_NoMemory();
512 return -1;
513}
514
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200515static int
516Pdata_stack_underflow(Pdata *self)
517{
518 PickleState *st = _Pickle_GetGlobalState();
519 PyErr_SetString(st->UnpicklingError,
520 self->mark_set ?
521 "unexpected MARK found" :
522 "unpickling stack underflow");
523 return -1;
524}
525
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000526/* D is a Pdata*. Pop the topmost element and store it into V, which
527 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
528 * is raised and V is set to NULL.
529 */
530static PyObject *
531Pdata_pop(Pdata *self)
532{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200533 if (Py_SIZE(self) <= self->fence) {
534 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000535 return NULL;
536 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000537 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000538}
539#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
540
541static int
542Pdata_push(Pdata *self, PyObject *obj)
543{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000544 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000545 return -1;
546 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000547 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000548 return 0;
549}
550
551/* Push an object on stack, transferring its ownership to the stack. */
552#define PDATA_PUSH(D, O, ER) do { \
553 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
554
555/* Push an object on stack, adding a new reference to the object. */
556#define PDATA_APPEND(D, O, ER) do { \
557 Py_INCREF((O)); \
558 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
559
560static PyObject *
561Pdata_poptuple(Pdata *self, Py_ssize_t start)
562{
563 PyObject *tuple;
564 Py_ssize_t len, i, j;
565
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200566 if (start < self->fence) {
567 Pdata_stack_underflow(self);
568 return NULL;
569 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000570 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000571 tuple = PyTuple_New(len);
572 if (tuple == NULL)
573 return NULL;
574 for (i = start, j = 0; j < len; i++, j++)
575 PyTuple_SET_ITEM(tuple, j, self->data[i]);
576
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000577 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000578 return tuple;
579}
580
581static PyObject *
582Pdata_poplist(Pdata *self, Py_ssize_t start)
583{
584 PyObject *list;
585 Py_ssize_t len, i, j;
586
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000587 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000588 list = PyList_New(len);
589 if (list == NULL)
590 return NULL;
591 for (i = start, j = 0; j < len; i++, j++)
592 PyList_SET_ITEM(list, j, self->data[i]);
593
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000594 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000595 return list;
596}
597
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000598typedef struct {
599 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200600 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000601} PyMemoEntry;
602
603typedef struct {
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700604 size_t mt_mask;
605 size_t mt_used;
606 size_t mt_allocated;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000607 PyMemoEntry *mt_table;
608} PyMemoTable;
609
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000610typedef struct PicklerObject {
611 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000612 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000613 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000614 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000615 PyObject *pers_func; /* persistent_id() method, can be NULL */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200616 PyObject *pers_func_self; /* borrowed reference to self if pers_func
617 is an unbound method, NULL otherwise */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100618 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Pierre Glaser289f1f82019-05-08 23:08:25 +0200619 PyObject *reducer_override; /* hook for invoking user-defined callbacks
620 instead of save_global when pickling
621 functions and classes*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000622
623 PyObject *write; /* write() method of the output stream. */
624 PyObject *output_buffer; /* Write into a local bytearray buffer before
625 flushing to the stream. */
626 Py_ssize_t output_len; /* Length of output_buffer. */
627 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000628 int proto; /* Pickle protocol number, >= 0 */
629 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100630 int framing; /* True when framing is enabled, proto >= 4 */
631 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000632 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100633 is no frame currently open. */
634
635 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000636 int fast; /* Enable fast mode if set to a true value.
637 The fast mode disable the usage of memo,
638 therefore speeding the pickling process by
639 not generating superfluous PUT opcodes. It
640 should not be used if with self-referential
641 objects. */
642 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000643 int fix_imports; /* Indicate whether Pickler should fix
644 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000645 PyObject *fast_memo;
646} PicklerObject;
647
648typedef struct UnpicklerObject {
649 PyObject_HEAD
650 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000651
652 /* The unpickler memo is just an array of PyObject *s. Using a dict
653 is unnecessary, since the keys are contiguous ints. */
654 PyObject **memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700655 size_t memo_size; /* Capacity of the memo array */
656 size_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000657
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000658 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200659 PyObject *pers_func_self; /* borrowed reference to self if pers_func
660 is an unbound method, NULL otherwise */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000661
662 Py_buffer buffer;
663 char *input_buffer;
664 char *input_line;
665 Py_ssize_t input_len;
666 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000667 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100668
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000669 PyObject *read; /* read() method of the input stream. */
670 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000671 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000672
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000673 char *encoding; /* Name of the encoding to be used for
674 decoding strings pickled using Python
675 2.x. The default value is "ASCII" */
676 char *errors; /* Name of errors handling scheme to used when
677 decoding strings. The default value is
678 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500679 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000680 objects. */
681 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
682 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000683 int proto; /* Protocol of the pickle loaded. */
684 int fix_imports; /* Indicate whether Unpickler should fix
685 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000686} UnpicklerObject;
687
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200688typedef struct {
689 PyObject_HEAD
690 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
691} PicklerMemoProxyObject;
692
693typedef struct {
694 PyObject_HEAD
695 UnpicklerObject *unpickler;
696} UnpicklerMemoProxyObject;
697
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000698/* Forward declarations */
699static int save(PicklerObject *, PyObject *, int);
700static int save_reduce(PicklerObject *, PyObject *, PyObject *);
701static PyTypeObject Pickler_Type;
702static PyTypeObject Unpickler_Type;
703
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200704#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000705
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000706/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300707 A custom hashtable mapping void* to Python ints. This is used by the pickler
708 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000709 a bunch of unnecessary object creation. This makes a huge performance
710 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000711
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000712#define MT_MINSIZE 8
713#define PERTURB_SHIFT 5
714
715
716static PyMemoTable *
717PyMemoTable_New(void)
718{
719 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
720 if (memo == NULL) {
721 PyErr_NoMemory();
722 return NULL;
723 }
724
725 memo->mt_used = 0;
726 memo->mt_allocated = MT_MINSIZE;
727 memo->mt_mask = MT_MINSIZE - 1;
728 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
729 if (memo->mt_table == NULL) {
730 PyMem_FREE(memo);
731 PyErr_NoMemory();
732 return NULL;
733 }
734 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
735
736 return memo;
737}
738
739static PyMemoTable *
740PyMemoTable_Copy(PyMemoTable *self)
741{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000742 PyMemoTable *new = PyMemoTable_New();
743 if (new == NULL)
744 return NULL;
745
746 new->mt_used = self->mt_used;
747 new->mt_allocated = self->mt_allocated;
748 new->mt_mask = self->mt_mask;
749 /* The table we get from _New() is probably smaller than we wanted.
750 Free it and allocate one that's the right size. */
751 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500752 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000753 if (new->mt_table == NULL) {
754 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200755 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000756 return NULL;
757 }
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700758 for (size_t i = 0; i < self->mt_allocated; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000759 Py_XINCREF(self->mt_table[i].me_key);
760 }
761 memcpy(new->mt_table, self->mt_table,
762 sizeof(PyMemoEntry) * self->mt_allocated);
763
764 return new;
765}
766
767static Py_ssize_t
768PyMemoTable_Size(PyMemoTable *self)
769{
770 return self->mt_used;
771}
772
773static int
774PyMemoTable_Clear(PyMemoTable *self)
775{
776 Py_ssize_t i = self->mt_allocated;
777
778 while (--i >= 0) {
779 Py_XDECREF(self->mt_table[i].me_key);
780 }
781 self->mt_used = 0;
782 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
783 return 0;
784}
785
786static void
787PyMemoTable_Del(PyMemoTable *self)
788{
789 if (self == NULL)
790 return;
791 PyMemoTable_Clear(self);
792
793 PyMem_FREE(self->mt_table);
794 PyMem_FREE(self);
795}
796
797/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
798 can be considerably simpler than dictobject.c's lookdict(). */
799static PyMemoEntry *
800_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
801{
802 size_t i;
803 size_t perturb;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700804 size_t mask = self->mt_mask;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000805 PyMemoEntry *table = self->mt_table;
806 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000807 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000808
809 i = hash & mask;
810 entry = &table[i];
811 if (entry->me_key == NULL || entry->me_key == key)
812 return entry;
813
814 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
815 i = (i << 2) + i + perturb + 1;
816 entry = &table[i & mask];
817 if (entry->me_key == NULL || entry->me_key == key)
818 return entry;
819 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700820 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000821}
822
823/* Returns -1 on failure, 0 on success. */
824static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700825_PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000826{
827 PyMemoEntry *oldtable = NULL;
828 PyMemoEntry *oldentry, *newentry;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700829 size_t new_size = MT_MINSIZE;
830 size_t to_process;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000831
832 assert(min_size > 0);
833
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700834 if (min_size > PY_SSIZE_T_MAX) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000835 PyErr_NoMemory();
836 return -1;
837 }
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700838
839 /* Find the smallest valid table size >= min_size. */
840 while (new_size < min_size) {
841 new_size <<= 1;
842 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000843 /* new_size needs to be a power of two. */
844 assert((new_size & (new_size - 1)) == 0);
845
846 /* Allocate new table. */
847 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500848 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000849 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200850 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000851 PyErr_NoMemory();
852 return -1;
853 }
854 self->mt_allocated = new_size;
855 self->mt_mask = new_size - 1;
856 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
857
858 /* Copy entries from the old table. */
859 to_process = self->mt_used;
860 for (oldentry = oldtable; to_process > 0; oldentry++) {
861 if (oldentry->me_key != NULL) {
862 to_process--;
863 /* newentry is a pointer to a chunk of the new
864 mt_table, so we're setting the key:value pair
865 in-place. */
866 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
867 newentry->me_key = oldentry->me_key;
868 newentry->me_value = oldentry->me_value;
869 }
870 }
871
872 /* Deallocate the old table. */
873 PyMem_FREE(oldtable);
874 return 0;
875}
876
877/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200878static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000879PyMemoTable_Get(PyMemoTable *self, PyObject *key)
880{
881 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
882 if (entry->me_key == NULL)
883 return NULL;
884 return &entry->me_value;
885}
886
887/* Returns -1 on failure, 0 on success. */
888static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200889PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000890{
891 PyMemoEntry *entry;
892
893 assert(key != NULL);
894
895 entry = _PyMemoTable_Lookup(self, key);
896 if (entry->me_key != NULL) {
897 entry->me_value = value;
898 return 0;
899 }
900 Py_INCREF(key);
901 entry->me_key = key;
902 entry->me_value = value;
903 self->mt_used++;
904
905 /* If we added a key, we can safely resize. Otherwise just return!
906 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
907 *
908 * Quadrupling the size improves average table sparseness
909 * (reducing collisions) at the cost of some memory. It also halves
910 * the number of expensive resize operations in a growing memo table.
911 *
912 * Very large memo tables (over 50K items) use doubling instead.
913 * This may help applications with severe memory constraints.
914 */
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700915 if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000916 return 0;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700917 }
918 // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow.
919 size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used;
920 return _PyMemoTable_ResizeTable(self, desired_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000921}
922
923#undef MT_MINSIZE
924#undef PERTURB_SHIFT
925
926/*************************************************************************/
927
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000928
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000929static int
930_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000931{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300932 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200933 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000935 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000936 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100937 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000938 return 0;
939}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000940
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100941static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100942_write_size64(char *out, size_t value)
943{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200944 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800945
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200946 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800947
948 for (i = 0; i < sizeof(size_t); i++) {
949 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
950 }
951 for (i = sizeof(size_t); i < 8; i++) {
952 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800953 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100954}
955
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100956static int
957_Pickler_CommitFrame(PicklerObject *self)
958{
959 size_t frame_len;
960 char *qdata;
961
962 if (!self->framing || self->frame_start == -1)
963 return 0;
964 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
965 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200966 if (frame_len >= FRAME_SIZE_MIN) {
967 qdata[0] = FRAME;
968 _write_size64(qdata + 1, frame_len);
969 }
970 else {
971 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
972 self->output_len -= FRAME_HEADER_SIZE;
973 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100974 self->frame_start = -1;
975 return 0;
976}
977
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000978static PyObject *
979_Pickler_GetString(PicklerObject *self)
980{
981 PyObject *output_buffer = self->output_buffer;
982
983 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100984
985 if (_Pickler_CommitFrame(self))
986 return NULL;
987
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000988 self->output_buffer = NULL;
989 /* Resize down to exact size */
990 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
991 return NULL;
992 return output_buffer;
993}
994
995static int
996_Pickler_FlushToFile(PicklerObject *self)
997{
998 PyObject *output, *result;
999
1000 assert(self->write != NULL);
1001
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001002 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001003 output = _Pickler_GetString(self);
1004 if (output == NULL)
1005 return -1;
1006
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001007 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001008 Py_XDECREF(result);
1009 return (result == NULL) ? -1 : 0;
1010}
1011
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001012static int
1013_Pickler_OpcodeBoundary(PicklerObject *self)
1014{
1015 Py_ssize_t frame_len;
1016
1017 if (!self->framing || self->frame_start == -1) {
1018 return 0;
1019 }
1020 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1021 if (frame_len >= FRAME_SIZE_TARGET) {
1022 if(_Pickler_CommitFrame(self)) {
1023 return -1;
1024 }
Leo Ariasc3d95082018-02-03 18:36:10 -06001025 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001026 * file and reuse the pickler buffer for the next frame so as
1027 * to limit memory usage when dumping large complex objects to
1028 * a file.
1029 *
1030 * self->write is NULL when called via dumps.
1031 */
1032 if (self->write != NULL) {
1033 if (_Pickler_FlushToFile(self) < 0) {
1034 return -1;
1035 }
1036 if (_Pickler_ClearBuffer(self) < 0) {
1037 return -1;
1038 }
1039 }
1040 }
1041 return 0;
1042}
1043
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001044static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001045_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001047 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001048 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001049 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050
1051 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001052 need_new_frame = (self->framing && self->frame_start == -1);
1053
1054 if (need_new_frame)
1055 n = data_len + FRAME_HEADER_SIZE;
1056 else
1057 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001058
1059 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001060 if (required > self->max_output_len) {
1061 /* Make place in buffer for the pickle chunk */
1062 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1063 PyErr_NoMemory();
1064 return -1;
1065 }
1066 self->max_output_len = (self->output_len + n) / 2 * 3;
1067 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1068 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001069 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001070 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001071 if (need_new_frame) {
1072 /* Setup new frame */
1073 Py_ssize_t frame_start = self->output_len;
1074 self->frame_start = frame_start;
1075 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1076 /* Write an invalid value, for debugging */
1077 buffer[frame_start + i] = 0xFE;
1078 }
1079 self->output_len += FRAME_HEADER_SIZE;
1080 }
1081 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001082 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001083 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001084 buffer[self->output_len + i] = s[i];
1085 }
1086 }
1087 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001088 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001089 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001090 self->output_len += data_len;
1091 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001092}
1093
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001094static PicklerObject *
1095_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001096{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001097 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001098
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001099 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1100 if (self == NULL)
1101 return NULL;
1102
1103 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001104 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001105 self->write = NULL;
1106 self->proto = 0;
1107 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001108 self->framing = 0;
1109 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001110 self->fast = 0;
1111 self->fast_nesting = 0;
1112 self->fix_imports = 0;
1113 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001114 self->max_output_len = WRITE_BUF_SIZE;
1115 self->output_len = 0;
Pierre Glaser289f1f82019-05-08 23:08:25 +02001116 self->reducer_override = NULL;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001117
1118 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001119 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1120 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001121
1122 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001123 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001124 return NULL;
1125 }
Zackery Spytz359bd4f2019-04-23 05:56:08 -06001126
1127 PyObject_GC_Track(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128 return self;
1129}
1130
1131static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001132_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001133{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001134 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001135
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001136 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001137 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001138 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001139 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001140 proto = PyLong_AsLong(protocol);
1141 if (proto < 0) {
1142 if (proto == -1 && PyErr_Occurred())
1143 return -1;
1144 proto = HIGHEST_PROTOCOL;
1145 }
1146 else if (proto > HIGHEST_PROTOCOL) {
1147 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1148 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001149 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001150 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001151 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001152 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001153 self->bin = proto > 0;
1154 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001155 return 0;
1156}
1157
1158/* Returns -1 (with an exception set) on failure, 0 on success. This may
1159 be called once on a freshly created Pickler. */
1160static int
1161_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1162{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001163 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001164 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001165 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1166 return -1;
1167 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001168 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001169 PyErr_SetString(PyExc_TypeError,
1170 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001171 return -1;
1172 }
1173
1174 return 0;
1175}
1176
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001177/* Returns the size of the input on success, -1 on failure. This takes its
1178 own reference to `input`. */
1179static Py_ssize_t
1180_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1181{
1182 if (self->buffer.buf != NULL)
1183 PyBuffer_Release(&self->buffer);
1184 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1185 return -1;
1186 self->input_buffer = self->buffer.buf;
1187 self->input_len = self->buffer.len;
1188 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001189 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001190 return self->input_len;
1191}
1192
Antoine Pitrou04248a82010-10-12 20:51:21 +00001193static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001194bad_readline(void)
1195{
1196 PickleState *st = _Pickle_GetGlobalState();
1197 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1198 return -1;
1199}
1200
1201static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001202_Unpickler_SkipConsumed(UnpicklerObject *self)
1203{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001204 Py_ssize_t consumed;
1205 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001206
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001207 consumed = self->next_read_idx - self->prefetched_idx;
1208 if (consumed <= 0)
1209 return 0;
1210
1211 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001212 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001213 r = PyObject_CallFunction(self->read, "n", consumed);
1214 if (r == NULL)
1215 return -1;
1216 Py_DECREF(r);
1217
1218 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001219 return 0;
1220}
1221
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001222static const Py_ssize_t READ_WHOLE_LINE = -1;
1223
1224/* If reading from a file, we need to only pull the bytes we need, since there
1225 may be multiple pickle objects arranged contiguously in the same input
1226 buffer.
1227
1228 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1229 bytes from the input stream/buffer.
1230
1231 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1232 failure; on success, returns the number of bytes read from the file.
1233
1234 On success, self->input_len will be 0; this is intentional so that when
1235 unpickling from a file, the "we've run out of data" code paths will trigger,
1236 causing the Unpickler to go back to the file for more data. Use the returned
1237 size to tell you how much data you can process. */
1238static Py_ssize_t
1239_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1240{
1241 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001242 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001243
1244 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001245
Antoine Pitrou04248a82010-10-12 20:51:21 +00001246 if (_Unpickler_SkipConsumed(self) < 0)
1247 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001248
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001249 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001250 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001251 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001252 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001253 PyObject *len;
1254 /* Prefetch some data without advancing the file pointer, if possible */
1255 if (self->peek && n < PREFETCH) {
1256 len = PyLong_FromSsize_t(PREFETCH);
1257 if (len == NULL)
1258 return -1;
1259 data = _Pickle_FastCall(self->peek, len);
1260 if (data == NULL) {
1261 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1262 return -1;
1263 /* peek() is probably not supported by the given file object */
1264 PyErr_Clear();
1265 Py_CLEAR(self->peek);
1266 }
1267 else {
1268 read_size = _Unpickler_SetStringInput(self, data);
1269 Py_DECREF(data);
1270 self->prefetched_idx = 0;
1271 if (n <= read_size)
1272 return n;
1273 }
1274 }
1275 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001276 if (len == NULL)
1277 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001278 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001279 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001280 if (data == NULL)
1281 return -1;
1282
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001283 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001284 Py_DECREF(data);
1285 return read_size;
1286}
1287
Victor Stinner19ed27e2016-05-20 11:42:37 +02001288/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001289static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001290_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001291{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001292 Py_ssize_t num_read;
1293
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001294 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001295 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1296 PickleState *st = _Pickle_GetGlobalState();
1297 PyErr_SetString(st->UnpicklingError,
1298 "read would overflow (invalid bytecode)");
1299 return -1;
1300 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001301
1302 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1303 assert(self->next_read_idx + n > self->input_len);
1304
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001305 if (!self->read)
1306 return bad_readline();
1307
Antoine Pitrou04248a82010-10-12 20:51:21 +00001308 num_read = _Unpickler_ReadFromFile(self, n);
1309 if (num_read < 0)
1310 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001311 if (num_read < n)
1312 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001313 *s = self->input_buffer;
1314 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001315 return n;
1316}
1317
Victor Stinner19ed27e2016-05-20 11:42:37 +02001318/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1319
1320 This should be used for all data reads, rather than accessing the unpickler's
1321 input buffer directly. This method deals correctly with reading from input
1322 streams, which the input buffer doesn't deal with.
1323
1324 Note that when reading from a file-like object, self->next_read_idx won't
1325 be updated (it should remain at 0 for the entire unpickling process). You
1326 should use this function's return value to know how many bytes you can
1327 consume.
1328
1329 Returns -1 (with an exception set) on failure. On success, return the
1330 number of chars read. */
1331#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001332 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001333 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1334 (self)->next_read_idx += (n), \
1335 (n)) \
1336 : _Unpickler_ReadImpl(self, (s), (n)))
1337
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001338static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001339_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1340 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001341{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001342 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001343 if (input_line == NULL) {
1344 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001345 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001346 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001347
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001348 memcpy(input_line, line, len);
1349 input_line[len] = '\0';
1350 self->input_line = input_line;
1351 *result = self->input_line;
1352 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001353}
1354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001355/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001356 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001357
1358 Returns the number of chars read, or -1 on failure. */
1359static Py_ssize_t
1360_Unpickler_Readline(UnpicklerObject *self, char **result)
1361{
1362 Py_ssize_t i, num_read;
1363
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001364 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001365 if (self->input_buffer[i] == '\n') {
1366 char *line_start = self->input_buffer + self->next_read_idx;
1367 num_read = i - self->next_read_idx + 1;
1368 self->next_read_idx = i + 1;
1369 return _Unpickler_CopyLine(self, line_start, num_read, result);
1370 }
1371 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001372 if (!self->read)
1373 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001374
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001375 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1376 if (num_read < 0)
1377 return -1;
1378 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1379 return bad_readline();
1380 self->next_read_idx = num_read;
1381 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001382}
1383
1384/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1385 will be modified in place. */
1386static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001387_Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001388{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001389 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001390
1391 assert(new_size > self->memo_size);
1392
Sergey Fedoseev67b9cc82018-08-16 09:27:50 +05001393 PyObject **memo_new = self->memo;
1394 PyMem_RESIZE(memo_new, PyObject *, new_size);
1395 if (memo_new == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001396 PyErr_NoMemory();
1397 return -1;
1398 }
Sergey Fedoseev67b9cc82018-08-16 09:27:50 +05001399 self->memo = memo_new;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001400 for (i = self->memo_size; i < new_size; i++)
1401 self->memo[i] = NULL;
1402 self->memo_size = new_size;
1403 return 0;
1404}
1405
1406/* Returns NULL if idx is out of bounds. */
1407static PyObject *
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001408_Unpickler_MemoGet(UnpicklerObject *self, size_t idx)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001409{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001410 if (idx >= self->memo_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001411 return NULL;
1412
1413 return self->memo[idx];
1414}
1415
1416/* Returns -1 (with an exception set) on failure, 0 on success.
1417 This takes its own reference to `value`. */
1418static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001419_Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001420{
1421 PyObject *old_item;
1422
1423 if (idx >= self->memo_size) {
1424 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1425 return -1;
1426 assert(idx < self->memo_size);
1427 }
1428 Py_INCREF(value);
1429 old_item = self->memo[idx];
1430 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001431 if (old_item != NULL) {
1432 Py_DECREF(old_item);
1433 }
1434 else {
1435 self->memo_len++;
1436 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001437 return 0;
1438}
1439
1440static PyObject **
1441_Unpickler_NewMemo(Py_ssize_t new_size)
1442{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001443 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001444 if (memo == NULL) {
1445 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001446 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001447 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001448 memset(memo, 0, new_size * sizeof(PyObject *));
1449 return memo;
1450}
1451
1452/* Free the unpickler's memo, taking care to decref any items left in it. */
1453static void
1454_Unpickler_MemoCleanup(UnpicklerObject *self)
1455{
1456 Py_ssize_t i;
1457 PyObject **memo = self->memo;
1458
1459 if (self->memo == NULL)
1460 return;
1461 self->memo = NULL;
1462 i = self->memo_size;
1463 while (--i >= 0) {
1464 Py_XDECREF(memo[i]);
1465 }
1466 PyMem_FREE(memo);
1467}
1468
1469static UnpicklerObject *
1470_Unpickler_New(void)
1471{
1472 UnpicklerObject *self;
1473
1474 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1475 if (self == NULL)
1476 return NULL;
1477
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001478 self->pers_func = NULL;
1479 self->input_buffer = NULL;
1480 self->input_line = NULL;
1481 self->input_len = 0;
1482 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001483 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001484 self->read = NULL;
1485 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001486 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001487 self->encoding = NULL;
1488 self->errors = NULL;
1489 self->marks = NULL;
1490 self->num_marks = 0;
1491 self->marks_size = 0;
1492 self->proto = 0;
1493 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001494 memset(&self->buffer, 0, sizeof(Py_buffer));
1495 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001496 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001497 self->memo = _Unpickler_NewMemo(self->memo_size);
1498 self->stack = (Pdata *)Pdata_New();
1499
1500 if (self->memo == NULL || self->stack == NULL) {
1501 Py_DECREF(self);
1502 return NULL;
1503 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001504
Zackery Spytz359bd4f2019-04-23 05:56:08 -06001505 PyObject_GC_Track(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001506 return self;
1507}
1508
1509/* Returns -1 (with an exception set) on failure, 0 on success. This may
1510 be called once on a freshly created Pickler. */
1511static int
1512_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1513{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001514 _Py_IDENTIFIER(peek);
1515 _Py_IDENTIFIER(read);
1516 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001517
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001518 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1519 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001520 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001521 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1522 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001523 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001524 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001525 PyErr_SetString(PyExc_TypeError,
1526 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001527 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001528 Py_CLEAR(self->read);
1529 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001530 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001531 return -1;
1532 }
1533 return 0;
1534}
1535
1536/* Returns -1 (with an exception set) on failure, 0 on success. This may
1537 be called once on a freshly created Pickler. */
1538static int
1539_Unpickler_SetInputEncoding(UnpicklerObject *self,
1540 const char *encoding,
1541 const char *errors)
1542{
1543 if (encoding == NULL)
1544 encoding = "ASCII";
1545 if (errors == NULL)
1546 errors = "strict";
1547
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001548 self->encoding = _PyMem_Strdup(encoding);
1549 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001550 if (self->encoding == NULL || self->errors == NULL) {
1551 PyErr_NoMemory();
1552 return -1;
1553 }
1554 return 0;
1555}
1556
1557/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558static int
1559memo_get(PicklerObject *self, PyObject *key)
1560{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001561 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001562 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001563 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001564
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001565 value = PyMemoTable_Get(self->memo, key);
1566 if (value == NULL) {
1567 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001568 return -1;
1569 }
1570
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001571 if (!self->bin) {
1572 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001573 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1574 "%" PY_FORMAT_SIZE_T "d\n", *value);
1575 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001576 }
1577 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001578 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001579 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001580 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001581 len = 2;
1582 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001583 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001584 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001585 pdata[1] = (unsigned char)(*value & 0xff);
1586 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1587 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1588 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001589 len = 5;
1590 }
1591 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001592 PickleState *st = _Pickle_GetGlobalState();
1593 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001594 "memo id too large for LONG_BINGET");
1595 return -1;
1596 }
1597 }
1598
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001599 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001600 return -1;
1601
1602 return 0;
1603}
1604
1605/* Store an object in the memo, assign it a new unique ID based on the number
1606 of objects currently stored in the memo and generate a PUT opcode. */
1607static int
1608memo_put(PicklerObject *self, PyObject *obj)
1609{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001610 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001611 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001612 Py_ssize_t idx;
1613
1614 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001615
1616 if (self->fast)
1617 return 0;
1618
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001619 idx = PyMemoTable_Size(self->memo);
1620 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1621 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001622
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001623 if (self->proto >= 4) {
1624 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1625 return -1;
1626 return 0;
1627 }
1628 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001629 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001630 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001631 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001632 len = strlen(pdata);
1633 }
1634 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001635 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001636 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001637 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 len = 2;
1639 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001640 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001641 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001642 pdata[1] = (unsigned char)(idx & 0xff);
1643 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1644 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1645 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001646 len = 5;
1647 }
1648 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001649 PickleState *st = _Pickle_GetGlobalState();
1650 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001651 "memo id too large for LONG_BINPUT");
1652 return -1;
1653 }
1654 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001655 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001656 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001657
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001658 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001659}
1660
1661static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001662get_dotted_path(PyObject *obj, PyObject *name)
1663{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001664 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001665 PyObject *dotted_path;
1666 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001667
1668 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001669 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001670 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001671 n = PyList_GET_SIZE(dotted_path);
1672 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001673 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001674 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001675 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001676 if (obj == NULL)
1677 PyErr_Format(PyExc_AttributeError,
1678 "Can't pickle local object %R", name);
1679 else
1680 PyErr_Format(PyExc_AttributeError,
1681 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001682 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001683 return NULL;
1684 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001685 }
1686 return dotted_path;
1687}
1688
1689static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001690get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001691{
1692 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001693 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001694
1695 assert(PyList_CheckExact(names));
1696 Py_INCREF(obj);
1697 n = PyList_GET_SIZE(names);
1698 for (i = 0; i < n; i++) {
1699 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001700 Py_XDECREF(parent);
1701 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001702 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001703 if (obj == NULL) {
1704 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001705 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001706 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001707 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001708 if (pparent != NULL)
1709 *pparent = parent;
1710 else
1711 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001712 return obj;
1713}
1714
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001715
1716static PyObject *
1717getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1718{
1719 PyObject *dotted_path, *attr;
1720
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001721 if (allow_qualname) {
1722 dotted_path = get_dotted_path(obj, name);
1723 if (dotted_path == NULL)
1724 return NULL;
1725 attr = get_deep_attribute(obj, dotted_path, NULL);
1726 Py_DECREF(dotted_path);
1727 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001728 else {
1729 (void)_PyObject_LookupAttr(obj, name, &attr);
1730 }
1731 if (attr == NULL && !PyErr_Occurred()) {
1732 PyErr_Format(PyExc_AttributeError,
1733 "Can't get attribute %R on %R", name, obj);
1734 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001735 return attr;
1736}
1737
Eric Snow3f9eee62017-09-15 16:35:20 -06001738static int
1739_checkmodule(PyObject *module_name, PyObject *module,
1740 PyObject *global, PyObject *dotted_path)
1741{
1742 if (module == Py_None) {
1743 return -1;
1744 }
1745 if (PyUnicode_Check(module_name) &&
1746 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1747 return -1;
1748 }
1749
1750 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1751 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001752 return -1;
1753 }
1754 if (candidate != global) {
1755 Py_DECREF(candidate);
1756 return -1;
1757 }
1758 Py_DECREF(candidate);
1759 return 0;
1760}
1761
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001762static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001763whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001764{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001765 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001766 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001767 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001768 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001769 _Py_IDENTIFIER(__module__);
1770 _Py_IDENTIFIER(modules);
1771 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001772
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001773 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1774 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001775 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001776 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001777 /* In some rare cases (e.g., bound methods of extension types),
1778 __module__ can be None. If it is so, then search sys.modules for
1779 the module of global. */
1780 if (module_name != Py_None)
1781 return module_name;
1782 Py_CLEAR(module_name);
1783 }
1784 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001785
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001786 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001787 modules = _PySys_GetObjectId(&PyId_modules);
1788 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001789 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001790 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001791 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001792 if (PyDict_CheckExact(modules)) {
1793 i = 0;
1794 while (PyDict_Next(modules, &i, &module_name, &module)) {
1795 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1796 Py_INCREF(module_name);
1797 return module_name;
1798 }
1799 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001800 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001801 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001802 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001803 }
1804 else {
1805 PyObject *iterator = PyObject_GetIter(modules);
1806 if (iterator == NULL) {
1807 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001808 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001809 while ((module_name = PyIter_Next(iterator))) {
1810 module = PyObject_GetItem(modules, module_name);
1811 if (module == NULL) {
1812 Py_DECREF(module_name);
1813 Py_DECREF(iterator);
1814 return NULL;
1815 }
1816 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1817 Py_DECREF(module);
1818 Py_DECREF(iterator);
1819 return module_name;
1820 }
1821 Py_DECREF(module);
1822 Py_DECREF(module_name);
1823 if (PyErr_Occurred()) {
1824 Py_DECREF(iterator);
1825 return NULL;
1826 }
1827 }
1828 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001829 }
1830
1831 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001832 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001833 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001834 return module_name;
1835}
1836
1837/* fast_save_enter() and fast_save_leave() are guards against recursive
1838 objects when Pickler is used with the "fast mode" (i.e., with object
1839 memoization disabled). If the nesting of a list or dict object exceed
1840 FAST_NESTING_LIMIT, these guards will start keeping an internal
1841 reference to the seen list or dict objects and check whether these objects
1842 are recursive. These are not strictly necessary, since save() has a
1843 hard-coded recursion limit, but they give a nicer error message than the
1844 typical RuntimeError. */
1845static int
1846fast_save_enter(PicklerObject *self, PyObject *obj)
1847{
1848 /* if fast_nesting < 0, we're doing an error exit. */
1849 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1850 PyObject *key = NULL;
1851 if (self->fast_memo == NULL) {
1852 self->fast_memo = PyDict_New();
1853 if (self->fast_memo == NULL) {
1854 self->fast_nesting = -1;
1855 return 0;
1856 }
1857 }
1858 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001859 if (key == NULL) {
1860 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001861 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001862 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001863 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001864 Py_DECREF(key);
1865 PyErr_Format(PyExc_ValueError,
1866 "fast mode: can't pickle cyclic objects "
1867 "including object type %.200s at %p",
1868 obj->ob_type->tp_name, obj);
1869 self->fast_nesting = -1;
1870 return 0;
1871 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001872 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001873 Py_DECREF(key);
1874 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001875 return 0;
1876 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001877 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1878 Py_DECREF(key);
1879 self->fast_nesting = -1;
1880 return 0;
1881 }
1882 Py_DECREF(key);
1883 }
1884 return 1;
1885}
1886
1887static int
1888fast_save_leave(PicklerObject *self, PyObject *obj)
1889{
1890 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1891 PyObject *key = PyLong_FromVoidPtr(obj);
1892 if (key == NULL)
1893 return 0;
1894 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1895 Py_DECREF(key);
1896 return 0;
1897 }
1898 Py_DECREF(key);
1899 }
1900 return 1;
1901}
1902
1903static int
1904save_none(PicklerObject *self, PyObject *obj)
1905{
1906 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001907 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001908 return -1;
1909
1910 return 0;
1911}
1912
1913static int
1914save_bool(PicklerObject *self, PyObject *obj)
1915{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001916 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001917 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001918 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001919 return -1;
1920 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001921 else {
1922 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1923 * so that unpicklers written before bools were introduced unpickle them
1924 * as ints, but unpicklers after can recognize that bools were intended.
1925 * Note that protocol 2 added direct ways to pickle bools.
1926 */
1927 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1928 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1929 return -1;
1930 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001931 return 0;
1932}
1933
1934static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001935save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001936{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001937 PyObject *repr = NULL;
1938 Py_ssize_t size;
1939 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001940 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001941 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001942
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001943 val= PyLong_AsLongAndOverflow(obj, &overflow);
1944 if (!overflow && (sizeof(long) <= 4 ||
1945 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1946 {
Larry Hastings61272b72014-01-07 12:41:53 -08001947 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001948
1949 Note: we can't use -0x80000000L in the above condition because some
1950 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1951 before applying the unary minus when sizeof(long) <= 4. The
1952 resulting value stays unsigned which is commonly not what we want,
1953 so MSVC happily warns us about it. However, that result would have
1954 been fine because we guard for sizeof(long) <= 4 which turns the
1955 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001956 char pdata[32];
1957 Py_ssize_t len = 0;
1958
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001959 if (self->bin) {
1960 pdata[1] = (unsigned char)(val & 0xff);
1961 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1962 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1963 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001964
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001965 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1966 pdata[0] = BININT;
1967 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001968 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001969 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001970 pdata[0] = BININT2;
1971 len = 3;
1972 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001973 else {
1974 pdata[0] = BININT1;
1975 len = 2;
1976 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001977 }
1978 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001979 sprintf(pdata, "%c%ld\n", INT, val);
1980 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001981 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001982 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001983 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001984
1985 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001986 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001987 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001988
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001989 if (self->proto >= 2) {
1990 /* Linear-time pickling. */
1991 size_t nbits;
1992 size_t nbytes;
1993 unsigned char *pdata;
1994 char header[5];
1995 int i;
1996 int sign = _PyLong_Sign(obj);
1997
1998 if (sign == 0) {
1999 header[0] = LONG1;
2000 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002001 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002002 goto error;
2003 return 0;
2004 }
2005 nbits = _PyLong_NumBits(obj);
2006 if (nbits == (size_t)-1 && PyErr_Occurred())
2007 goto error;
2008 /* How many bytes do we need? There are nbits >> 3 full
2009 * bytes of data, and nbits & 7 leftover bits. If there
2010 * are any leftover bits, then we clearly need another
2011 * byte. Wnat's not so obvious is that we *probably*
2012 * need another byte even if there aren't any leftovers:
2013 * the most-significant bit of the most-significant byte
2014 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002015 * opposite of the one we need. The exception is ints
2016 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002017 * its own 256's-complement, so has the right sign bit
2018 * even without the extra byte. That's a pain to check
2019 * for in advance, though, so we always grab an extra
2020 * byte at the start, and cut it back later if possible.
2021 */
2022 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002023 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002024 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002025 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002026 goto error;
2027 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002028 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002029 if (repr == NULL)
2030 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002031 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 i = _PyLong_AsByteArray((PyLongObject *)obj,
2033 pdata, nbytes,
2034 1 /* little endian */ , 1 /* signed */ );
2035 if (i < 0)
2036 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002037 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002038 * needed. This is so iff the MSB is all redundant sign
2039 * bits.
2040 */
2041 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002042 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002043 pdata[nbytes - 1] == 0xff &&
2044 (pdata[nbytes - 2] & 0x80) != 0) {
2045 nbytes--;
2046 }
2047
2048 if (nbytes < 256) {
2049 header[0] = LONG1;
2050 header[1] = (unsigned char)nbytes;
2051 size = 2;
2052 }
2053 else {
2054 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002055 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002056 for (i = 1; i < 5; i++) {
2057 header[i] = (unsigned char)(size & 0xff);
2058 size >>= 8;
2059 }
2060 size = 5;
2061 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002062 if (_Pickler_Write(self, header, size) < 0 ||
2063 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002064 goto error;
2065 }
2066 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002067 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002068 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002069
Mark Dickinson8dd05142009-01-20 20:43:58 +00002070 /* proto < 2: write the repr and newline. This is quadratic-time (in
2071 the number of digits), in both directions. We add a trailing 'L'
2072 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073
2074 repr = PyObject_Repr(obj);
2075 if (repr == NULL)
2076 goto error;
2077
Serhiy Storchaka06515832016-11-20 09:13:07 +02002078 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002079 if (string == NULL)
2080 goto error;
2081
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002082 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2083 _Pickler_Write(self, string, size) < 0 ||
2084 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002085 goto error;
2086 }
2087
2088 if (0) {
2089 error:
2090 status = -1;
2091 }
2092 Py_XDECREF(repr);
2093
2094 return status;
2095}
2096
2097static int
2098save_float(PicklerObject *self, PyObject *obj)
2099{
2100 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2101
2102 if (self->bin) {
2103 char pdata[9];
2104 pdata[0] = BINFLOAT;
2105 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2106 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002107 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002108 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002109 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002110 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002111 int result = -1;
2112 char *buf = NULL;
2113 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002114
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002115 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002116 goto done;
2117
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002118 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002119 if (!buf) {
2120 PyErr_NoMemory();
2121 goto done;
2122 }
2123
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002124 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002125 goto done;
2126
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002127 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002128 goto done;
2129
2130 result = 0;
2131done:
2132 PyMem_Free(buf);
2133 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002134 }
2135
2136 return 0;
2137}
2138
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002139/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002140
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002141 The large contiguous data is written directly into the underlying file
2142 object, bypassing the output_buffer of the Pickler. We intentionally
2143 do not insert a protocol 4 frame opcode to make it possible to optimize
2144 file.read calls in the loader.
2145 */
2146static int
2147_Pickler_write_bytes(PicklerObject *self,
2148 const char *header, Py_ssize_t header_size,
2149 const char *data, Py_ssize_t data_size,
2150 PyObject *payload)
2151{
2152 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2153 int framing = self->framing;
2154
2155 if (bypass_buffer) {
2156 assert(self->output_buffer != NULL);
2157 /* Commit the previous frame. */
2158 if (_Pickler_CommitFrame(self)) {
2159 return -1;
2160 }
2161 /* Disable framing temporarily */
2162 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002163 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002164
2165 if (_Pickler_Write(self, header, header_size) < 0) {
2166 return -1;
2167 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002168
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002169 if (bypass_buffer && self->write != NULL) {
2170 /* Bypass the in-memory buffer to directly stream large data
2171 into the underlying file object. */
2172 PyObject *result, *mem = NULL;
2173 /* Dump the output buffer to the file. */
2174 if (_Pickler_FlushToFile(self) < 0) {
2175 return -1;
2176 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002177
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002178 /* Stream write the payload into the file without going through the
2179 output buffer. */
2180 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002181 /* TODO: It would be better to use a memoryview with a linked
2182 original string if this is possible. */
2183 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002184 if (payload == NULL) {
2185 return -1;
2186 }
2187 }
2188 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2189 Py_XDECREF(mem);
2190 if (result == NULL) {
2191 return -1;
2192 }
2193 Py_DECREF(result);
2194
2195 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2196 if (_Pickler_ClearBuffer(self) < 0) {
2197 return -1;
2198 }
2199 }
2200 else {
2201 if (_Pickler_Write(self, data, data_size) < 0) {
2202 return -1;
2203 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002204 }
2205
2206 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002207 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002208
2209 return 0;
2210}
2211
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002212static int
2213save_bytes(PicklerObject *self, PyObject *obj)
2214{
2215 if (self->proto < 3) {
2216 /* Older pickle protocols do not have an opcode for pickling bytes
2217 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002218 the __reduce__ method) to permit bytes object unpickling.
2219
2220 Here we use a hack to be compatible with Python 2. Since in Python
2221 2 'bytes' is just an alias for 'str' (which has different
2222 parameters than the actual bytes object), we use codecs.encode
2223 to create the appropriate 'str' object when unpickled using
2224 Python 2 *and* the appropriate 'bytes' object when unpickled
2225 using Python 3. Again this is a hack and we don't need to do this
2226 with newer protocols. */
Pierre Glaser289f1f82019-05-08 23:08:25 +02002227 PyObject *reduce_value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002228 int status;
2229
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002230 if (PyBytes_GET_SIZE(obj) == 0) {
2231 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2232 }
2233 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002234 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002235 PyObject *unicode_str =
2236 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2237 PyBytes_GET_SIZE(obj),
2238 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002239 _Py_IDENTIFIER(latin1);
2240
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002241 if (unicode_str == NULL)
2242 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002243 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002244 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002245 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002246 Py_DECREF(unicode_str);
2247 }
2248
2249 if (reduce_value == NULL)
2250 return -1;
2251
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 /* save_reduce() will memoize the object automatically. */
2253 status = save_reduce(self, reduce_value, obj);
2254 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255 return status;
2256 }
2257 else {
2258 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002259 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002260 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002261
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002262 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002263 if (size < 0)
2264 return -1;
2265
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002266 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002267 header[0] = SHORT_BINBYTES;
2268 header[1] = (unsigned char)size;
2269 len = 2;
2270 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002271 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002272 header[0] = BINBYTES;
2273 header[1] = (unsigned char)(size & 0xff);
2274 header[2] = (unsigned char)((size >> 8) & 0xff);
2275 header[3] = (unsigned char)((size >> 16) & 0xff);
2276 header[4] = (unsigned char)((size >> 24) & 0xff);
2277 len = 5;
2278 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002279 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002280 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002281 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002282 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002283 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002284 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002285 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002286 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287 return -1; /* string too large */
2288 }
2289
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002290 if (_Pickler_write_bytes(self, header, len,
2291 PyBytes_AS_STRING(obj), size, obj) < 0)
2292 {
2293 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002294 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295
2296 if (memo_put(self, obj) < 0)
2297 return -1;
2298
2299 return 0;
2300 }
2301}
2302
2303/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2304 backslash and newline characters to \uXXXX escapes. */
2305static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002306raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002307{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002308 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002309 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002310 void *data;
2311 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002312 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002313
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002314 if (PyUnicode_READY(obj))
2315 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002316
Victor Stinner358af132015-10-12 22:36:57 +02002317 _PyBytesWriter_Init(&writer);
2318
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002319 size = PyUnicode_GET_LENGTH(obj);
2320 data = PyUnicode_DATA(obj);
2321 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002322
Victor Stinner358af132015-10-12 22:36:57 +02002323 p = _PyBytesWriter_Alloc(&writer, size);
2324 if (p == NULL)
2325 goto error;
2326 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002327
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002328 for (i=0; i < size; i++) {
2329 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002330 /* Map 32-bit characters to '\Uxxxxxxxx' */
2331 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002332 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002333 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2334 if (p == NULL)
2335 goto error;
2336
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002337 *p++ = '\\';
2338 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002339 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2340 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2341 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2342 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2343 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2344 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2345 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2346 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002347 }
Victor Stinner358af132015-10-12 22:36:57 +02002348 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002349 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002350 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002351 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2352 if (p == NULL)
2353 goto error;
2354
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002355 *p++ = '\\';
2356 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002357 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2358 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2359 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2360 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002361 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002362 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002363 else
2364 *p++ = (char) ch;
2365 }
Victor Stinner358af132015-10-12 22:36:57 +02002366
2367 return _PyBytesWriter_Finish(&writer, p);
2368
2369error:
2370 _PyBytesWriter_Dealloc(&writer);
2371 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002372}
2373
2374static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002375write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002376{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002377 char header[9];
2378 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002379 PyObject *encoded = NULL;
2380 Py_ssize_t size;
2381 const char *data;
2382
2383 if (PyUnicode_READY(obj))
2384 return -1;
2385
2386 data = PyUnicode_AsUTF8AndSize(obj, &size);
2387 if (data == NULL) {
2388 /* Issue #8383: for strings with lone surrogates, fallback on the
2389 "surrogatepass" error handler. */
2390 PyErr_Clear();
2391 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2392 if (encoded == NULL)
2393 return -1;
2394
2395 data = PyBytes_AS_STRING(encoded);
2396 size = PyBytes_GET_SIZE(encoded);
2397 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002398
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002399 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002400 if (size <= 0xff && self->proto >= 4) {
2401 header[0] = SHORT_BINUNICODE;
2402 header[1] = (unsigned char)(size & 0xff);
2403 len = 2;
2404 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002405 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002406 header[0] = BINUNICODE;
2407 header[1] = (unsigned char)(size & 0xff);
2408 header[2] = (unsigned char)((size >> 8) & 0xff);
2409 header[3] = (unsigned char)((size >> 16) & 0xff);
2410 header[4] = (unsigned char)((size >> 24) & 0xff);
2411 len = 5;
2412 }
2413 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002414 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002415 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002416 len = 9;
2417 }
2418 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002419 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002420 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002421 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002422 return -1;
2423 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002424
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002425 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2426 Py_XDECREF(encoded);
2427 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002428 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002429 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002430 return 0;
2431}
2432
2433static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002434save_unicode(PicklerObject *self, PyObject *obj)
2435{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002436 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002437 if (write_unicode_binary(self, obj) < 0)
2438 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002439 }
2440 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002441 PyObject *encoded;
2442 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002443 const char unicode_op = UNICODE;
2444
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002445 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002446 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002447 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002448
Antoine Pitrou299978d2013-04-07 17:38:11 +02002449 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2450 Py_DECREF(encoded);
2451 return -1;
2452 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002453
2454 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002455 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2456 Py_DECREF(encoded);
2457 return -1;
2458 }
2459 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002460
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002461 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002462 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002463 }
2464 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002465 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002466
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002467 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002468}
2469
2470/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2471static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002472store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002473{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002474 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002475
2476 assert(PyTuple_Size(t) == len);
2477
2478 for (i = 0; i < len; i++) {
2479 PyObject *element = PyTuple_GET_ITEM(t, i);
2480
2481 if (element == NULL)
2482 return -1;
2483 if (save(self, element, 0) < 0)
2484 return -1;
2485 }
2486
2487 return 0;
2488}
2489
2490/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2491 * used across protocols to minimize the space needed to pickle them.
2492 * Tuples are also the only builtin immutable type that can be recursive
2493 * (a tuple can be reached from itself), and that requires some subtle
2494 * magic so that it works in all cases. IOW, this is a long routine.
2495 */
2496static int
2497save_tuple(PicklerObject *self, PyObject *obj)
2498{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002499 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002500
2501 const char mark_op = MARK;
2502 const char tuple_op = TUPLE;
2503 const char pop_op = POP;
2504 const char pop_mark_op = POP_MARK;
2505 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2506
2507 if ((len = PyTuple_Size(obj)) < 0)
2508 return -1;
2509
2510 if (len == 0) {
2511 char pdata[2];
2512
2513 if (self->proto) {
2514 pdata[0] = EMPTY_TUPLE;
2515 len = 1;
2516 }
2517 else {
2518 pdata[0] = MARK;
2519 pdata[1] = TUPLE;
2520 len = 2;
2521 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002522 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002523 return -1;
2524 return 0;
2525 }
2526
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002527 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002528 * saving the tuple elements, the tuple must be recursive, in
2529 * which case we'll pop everything we put on the stack, and fetch
2530 * its value from the memo.
2531 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002532 if (len <= 3 && self->proto >= 2) {
2533 /* Use TUPLE{1,2,3} opcodes. */
2534 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002535 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002536
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002537 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002538 /* pop the len elements */
2539 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002540 if (_Pickler_Write(self, &pop_op, 1) < 0)
2541 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002542 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002543 if (memo_get(self, obj) < 0)
2544 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002545
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002546 return 0;
2547 }
2548 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002549 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2550 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002551 }
2552 goto memoize;
2553 }
2554
2555 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2556 * Generate MARK e1 e2 ... TUPLE
2557 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002558 if (_Pickler_Write(self, &mark_op, 1) < 0)
2559 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002560
2561 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002562 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002563
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002564 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002565 /* pop the stack stuff we pushed */
2566 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002567 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2568 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002569 }
2570 else {
2571 /* Note that we pop one more than len, to remove
2572 * the MARK too.
2573 */
2574 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002575 if (_Pickler_Write(self, &pop_op, 1) < 0)
2576 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002577 }
2578 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002579 if (memo_get(self, obj) < 0)
2580 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002581
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002582 return 0;
2583 }
2584 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002585 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2586 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002587 }
2588
2589 memoize:
2590 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002591 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002593 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002594}
2595
2596/* iter is an iterator giving items, and we batch up chunks of
2597 * MARK item item ... item APPENDS
2598 * opcode sequences. Calling code should have arranged to first create an
2599 * empty list, or list-like object, for the APPENDS to operate on.
2600 * Returns 0 on success, <0 on error.
2601 */
2602static int
2603batch_list(PicklerObject *self, PyObject *iter)
2604{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002605 PyObject *obj = NULL;
2606 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002607 int i, n;
2608
2609 const char mark_op = MARK;
2610 const char append_op = APPEND;
2611 const char appends_op = APPENDS;
2612
2613 assert(iter != NULL);
2614
2615 /* XXX: I think this function could be made faster by avoiding the
2616 iterator interface and fetching objects directly from list using
2617 PyList_GET_ITEM.
2618 */
2619
2620 if (self->proto == 0) {
2621 /* APPENDS isn't available; do one at a time. */
2622 for (;;) {
2623 obj = PyIter_Next(iter);
2624 if (obj == NULL) {
2625 if (PyErr_Occurred())
2626 return -1;
2627 break;
2628 }
2629 i = save(self, obj, 0);
2630 Py_DECREF(obj);
2631 if (i < 0)
2632 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002633 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002634 return -1;
2635 }
2636 return 0;
2637 }
2638
2639 /* proto > 0: write in batches of BATCHSIZE. */
2640 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002641 /* Get first item */
2642 firstitem = PyIter_Next(iter);
2643 if (firstitem == NULL) {
2644 if (PyErr_Occurred())
2645 goto error;
2646
2647 /* nothing more to add */
2648 break;
2649 }
2650
2651 /* Try to get a second item */
2652 obj = PyIter_Next(iter);
2653 if (obj == NULL) {
2654 if (PyErr_Occurred())
2655 goto error;
2656
2657 /* Only one item to write */
2658 if (save(self, firstitem, 0) < 0)
2659 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002660 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002661 goto error;
2662 Py_CLEAR(firstitem);
2663 break;
2664 }
2665
2666 /* More than one item to write */
2667
2668 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002669 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002670 goto error;
2671
2672 if (save(self, firstitem, 0) < 0)
2673 goto error;
2674 Py_CLEAR(firstitem);
2675 n = 1;
2676
2677 /* Fetch and save up to BATCHSIZE items */
2678 while (obj) {
2679 if (save(self, obj, 0) < 0)
2680 goto error;
2681 Py_CLEAR(obj);
2682 n += 1;
2683
2684 if (n == BATCHSIZE)
2685 break;
2686
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002687 obj = PyIter_Next(iter);
2688 if (obj == NULL) {
2689 if (PyErr_Occurred())
2690 goto error;
2691 break;
2692 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002693 }
2694
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002695 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002696 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002697
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002698 } while (n == BATCHSIZE);
2699 return 0;
2700
2701 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002702 Py_XDECREF(firstitem);
2703 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002704 return -1;
2705}
2706
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002707/* This is a variant of batch_list() above, specialized for lists (with no
2708 * support for list subclasses). Like batch_list(), we batch up chunks of
2709 * MARK item item ... item APPENDS
2710 * opcode sequences. Calling code should have arranged to first create an
2711 * empty list, or list-like object, for the APPENDS to operate on.
2712 * Returns 0 on success, -1 on error.
2713 *
2714 * This version is considerably faster than batch_list(), if less general.
2715 *
2716 * Note that this only works for protocols > 0.
2717 */
2718static int
2719batch_list_exact(PicklerObject *self, PyObject *obj)
2720{
2721 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002722 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002723
2724 const char append_op = APPEND;
2725 const char appends_op = APPENDS;
2726 const char mark_op = MARK;
2727
2728 assert(obj != NULL);
2729 assert(self->proto > 0);
2730 assert(PyList_CheckExact(obj));
2731
2732 if (PyList_GET_SIZE(obj) == 1) {
2733 item = PyList_GET_ITEM(obj, 0);
2734 if (save(self, item, 0) < 0)
2735 return -1;
2736 if (_Pickler_Write(self, &append_op, 1) < 0)
2737 return -1;
2738 return 0;
2739 }
2740
2741 /* Write in batches of BATCHSIZE. */
2742 total = 0;
2743 do {
2744 this_batch = 0;
2745 if (_Pickler_Write(self, &mark_op, 1) < 0)
2746 return -1;
2747 while (total < PyList_GET_SIZE(obj)) {
2748 item = PyList_GET_ITEM(obj, total);
2749 if (save(self, item, 0) < 0)
2750 return -1;
2751 total++;
2752 if (++this_batch == BATCHSIZE)
2753 break;
2754 }
2755 if (_Pickler_Write(self, &appends_op, 1) < 0)
2756 return -1;
2757
2758 } while (total < PyList_GET_SIZE(obj));
2759
2760 return 0;
2761}
2762
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002763static int
2764save_list(PicklerObject *self, PyObject *obj)
2765{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002766 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002767 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002768 int status = 0;
2769
2770 if (self->fast && !fast_save_enter(self, obj))
2771 goto error;
2772
2773 /* Create an empty list. */
2774 if (self->bin) {
2775 header[0] = EMPTY_LIST;
2776 len = 1;
2777 }
2778 else {
2779 header[0] = MARK;
2780 header[1] = LIST;
2781 len = 2;
2782 }
2783
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002784 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002785 goto error;
2786
2787 /* Get list length, and bow out early if empty. */
2788 if ((len = PyList_Size(obj)) < 0)
2789 goto error;
2790
2791 if (memo_put(self, obj) < 0)
2792 goto error;
2793
2794 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002795 /* Materialize the list elements. */
2796 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002797 if (Py_EnterRecursiveCall(" while pickling an object"))
2798 goto error;
2799 status = batch_list_exact(self, obj);
2800 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002801 } else {
2802 PyObject *iter = PyObject_GetIter(obj);
2803 if (iter == NULL)
2804 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002805
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002806 if (Py_EnterRecursiveCall(" while pickling an object")) {
2807 Py_DECREF(iter);
2808 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002809 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002810 status = batch_list(self, iter);
2811 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002812 Py_DECREF(iter);
2813 }
2814 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002815 if (0) {
2816 error:
2817 status = -1;
2818 }
2819
2820 if (self->fast && !fast_save_leave(self, obj))
2821 status = -1;
2822
2823 return status;
2824}
2825
2826/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2827 * MARK key value ... key value SETITEMS
2828 * opcode sequences. Calling code should have arranged to first create an
2829 * empty dict, or dict-like object, for the SETITEMS to operate on.
2830 * Returns 0 on success, <0 on error.
2831 *
2832 * This is very much like batch_list(). The difference between saving
2833 * elements directly, and picking apart two-tuples, is so long-winded at
2834 * the C level, though, that attempts to combine these routines were too
2835 * ugly to bear.
2836 */
2837static int
2838batch_dict(PicklerObject *self, PyObject *iter)
2839{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002840 PyObject *obj = NULL;
2841 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002842 int i, n;
2843
2844 const char mark_op = MARK;
2845 const char setitem_op = SETITEM;
2846 const char setitems_op = SETITEMS;
2847
2848 assert(iter != NULL);
2849
2850 if (self->proto == 0) {
2851 /* SETITEMS isn't available; do one at a time. */
2852 for (;;) {
2853 obj = PyIter_Next(iter);
2854 if (obj == NULL) {
2855 if (PyErr_Occurred())
2856 return -1;
2857 break;
2858 }
2859 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2860 PyErr_SetString(PyExc_TypeError, "dict items "
2861 "iterator must return 2-tuples");
2862 return -1;
2863 }
2864 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2865 if (i >= 0)
2866 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2867 Py_DECREF(obj);
2868 if (i < 0)
2869 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002870 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002871 return -1;
2872 }
2873 return 0;
2874 }
2875
2876 /* proto > 0: write in batches of BATCHSIZE. */
2877 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002878 /* Get first item */
2879 firstitem = PyIter_Next(iter);
2880 if (firstitem == NULL) {
2881 if (PyErr_Occurred())
2882 goto error;
2883
2884 /* nothing more to add */
2885 break;
2886 }
2887 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2888 PyErr_SetString(PyExc_TypeError, "dict items "
2889 "iterator must return 2-tuples");
2890 goto error;
2891 }
2892
2893 /* Try to get a second item */
2894 obj = PyIter_Next(iter);
2895 if (obj == NULL) {
2896 if (PyErr_Occurred())
2897 goto error;
2898
2899 /* Only one item to write */
2900 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2901 goto error;
2902 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2903 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002904 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002905 goto error;
2906 Py_CLEAR(firstitem);
2907 break;
2908 }
2909
2910 /* More than one item to write */
2911
2912 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002913 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002914 goto error;
2915
2916 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2917 goto error;
2918 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2919 goto error;
2920 Py_CLEAR(firstitem);
2921 n = 1;
2922
2923 /* Fetch and save up to BATCHSIZE items */
2924 while (obj) {
2925 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2926 PyErr_SetString(PyExc_TypeError, "dict items "
2927 "iterator must return 2-tuples");
2928 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002929 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002930 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2931 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2932 goto error;
2933 Py_CLEAR(obj);
2934 n += 1;
2935
2936 if (n == BATCHSIZE)
2937 break;
2938
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002939 obj = PyIter_Next(iter);
2940 if (obj == NULL) {
2941 if (PyErr_Occurred())
2942 goto error;
2943 break;
2944 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002945 }
2946
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002947 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002948 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002949
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002950 } while (n == BATCHSIZE);
2951 return 0;
2952
2953 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002954 Py_XDECREF(firstitem);
2955 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002956 return -1;
2957}
2958
Collin Winter5c9b02d2009-05-25 05:43:30 +00002959/* This is a variant of batch_dict() above that specializes for dicts, with no
2960 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2961 * MARK key value ... key value SETITEMS
2962 * opcode sequences. Calling code should have arranged to first create an
2963 * empty dict, or dict-like object, for the SETITEMS to operate on.
2964 * Returns 0 on success, -1 on error.
2965 *
2966 * Note that this currently doesn't work for protocol 0.
2967 */
2968static int
2969batch_dict_exact(PicklerObject *self, PyObject *obj)
2970{
2971 PyObject *key = NULL, *value = NULL;
2972 int i;
2973 Py_ssize_t dict_size, ppos = 0;
2974
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002975 const char mark_op = MARK;
2976 const char setitem_op = SETITEM;
2977 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002978
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002979 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002980 assert(self->proto > 0);
2981
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002982 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002983
2984 /* Special-case len(d) == 1 to save space. */
2985 if (dict_size == 1) {
2986 PyDict_Next(obj, &ppos, &key, &value);
2987 if (save(self, key, 0) < 0)
2988 return -1;
2989 if (save(self, value, 0) < 0)
2990 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002991 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002992 return -1;
2993 return 0;
2994 }
2995
2996 /* Write in batches of BATCHSIZE. */
2997 do {
2998 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002999 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003000 return -1;
3001 while (PyDict_Next(obj, &ppos, &key, &value)) {
3002 if (save(self, key, 0) < 0)
3003 return -1;
3004 if (save(self, value, 0) < 0)
3005 return -1;
3006 if (++i == BATCHSIZE)
3007 break;
3008 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003009 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003010 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003011 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003012 PyErr_Format(
3013 PyExc_RuntimeError,
3014 "dictionary changed size during iteration");
3015 return -1;
3016 }
3017
3018 } while (i == BATCHSIZE);
3019 return 0;
3020}
3021
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003022static int
3023save_dict(PicklerObject *self, PyObject *obj)
3024{
3025 PyObject *items, *iter;
3026 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003027 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003028 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003029 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003030
3031 if (self->fast && !fast_save_enter(self, obj))
3032 goto error;
3033
3034 /* Create an empty dict. */
3035 if (self->bin) {
3036 header[0] = EMPTY_DICT;
3037 len = 1;
3038 }
3039 else {
3040 header[0] = MARK;
3041 header[1] = DICT;
3042 len = 2;
3043 }
3044
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003045 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003046 goto error;
3047
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003048 if (memo_put(self, obj) < 0)
3049 goto error;
3050
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003051 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003052 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003053 if (PyDict_CheckExact(obj) && self->proto > 0) {
3054 /* We can take certain shortcuts if we know this is a dict and
3055 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003056 if (Py_EnterRecursiveCall(" while pickling an object"))
3057 goto error;
3058 status = batch_dict_exact(self, obj);
3059 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003060 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003061 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003062
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003063 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003064 if (items == NULL)
3065 goto error;
3066 iter = PyObject_GetIter(items);
3067 Py_DECREF(items);
3068 if (iter == NULL)
3069 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003070 if (Py_EnterRecursiveCall(" while pickling an object")) {
3071 Py_DECREF(iter);
3072 goto error;
3073 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003074 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003075 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003076 Py_DECREF(iter);
3077 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003078 }
3079
3080 if (0) {
3081 error:
3082 status = -1;
3083 }
3084
3085 if (self->fast && !fast_save_leave(self, obj))
3086 status = -1;
3087
3088 return status;
3089}
3090
3091static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003092save_set(PicklerObject *self, PyObject *obj)
3093{
3094 PyObject *item;
3095 int i;
3096 Py_ssize_t set_size, ppos = 0;
3097 Py_hash_t hash;
3098
3099 const char empty_set_op = EMPTY_SET;
3100 const char mark_op = MARK;
3101 const char additems_op = ADDITEMS;
3102
3103 if (self->proto < 4) {
3104 PyObject *items;
3105 PyObject *reduce_value;
3106 int status;
3107
3108 items = PySequence_List(obj);
3109 if (items == NULL) {
3110 return -1;
3111 }
3112 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3113 Py_DECREF(items);
3114 if (reduce_value == NULL) {
3115 return -1;
3116 }
3117 /* save_reduce() will memoize the object automatically. */
3118 status = save_reduce(self, reduce_value, obj);
3119 Py_DECREF(reduce_value);
3120 return status;
3121 }
3122
3123 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3124 return -1;
3125
3126 if (memo_put(self, obj) < 0)
3127 return -1;
3128
3129 set_size = PySet_GET_SIZE(obj);
3130 if (set_size == 0)
3131 return 0; /* nothing to do */
3132
3133 /* Write in batches of BATCHSIZE. */
3134 do {
3135 i = 0;
3136 if (_Pickler_Write(self, &mark_op, 1) < 0)
3137 return -1;
3138 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3139 if (save(self, item, 0) < 0)
3140 return -1;
3141 if (++i == BATCHSIZE)
3142 break;
3143 }
3144 if (_Pickler_Write(self, &additems_op, 1) < 0)
3145 return -1;
3146 if (PySet_GET_SIZE(obj) != set_size) {
3147 PyErr_Format(
3148 PyExc_RuntimeError,
3149 "set changed size during iteration");
3150 return -1;
3151 }
3152 } while (i == BATCHSIZE);
3153
3154 return 0;
3155}
3156
3157static int
3158save_frozenset(PicklerObject *self, PyObject *obj)
3159{
3160 PyObject *iter;
3161
3162 const char mark_op = MARK;
3163 const char frozenset_op = FROZENSET;
3164
3165 if (self->fast && !fast_save_enter(self, obj))
3166 return -1;
3167
3168 if (self->proto < 4) {
3169 PyObject *items;
3170 PyObject *reduce_value;
3171 int status;
3172
3173 items = PySequence_List(obj);
3174 if (items == NULL) {
3175 return -1;
3176 }
3177 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3178 items);
3179 Py_DECREF(items);
3180 if (reduce_value == NULL) {
3181 return -1;
3182 }
3183 /* save_reduce() will memoize the object automatically. */
3184 status = save_reduce(self, reduce_value, obj);
3185 Py_DECREF(reduce_value);
3186 return status;
3187 }
3188
3189 if (_Pickler_Write(self, &mark_op, 1) < 0)
3190 return -1;
3191
3192 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003193 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003194 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003195 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003196 for (;;) {
3197 PyObject *item;
3198
3199 item = PyIter_Next(iter);
3200 if (item == NULL) {
3201 if (PyErr_Occurred()) {
3202 Py_DECREF(iter);
3203 return -1;
3204 }
3205 break;
3206 }
3207 if (save(self, item, 0) < 0) {
3208 Py_DECREF(item);
3209 Py_DECREF(iter);
3210 return -1;
3211 }
3212 Py_DECREF(item);
3213 }
3214 Py_DECREF(iter);
3215
3216 /* If the object is already in the memo, this means it is
3217 recursive. In this case, throw away everything we put on the
3218 stack, and fetch the object back from the memo. */
3219 if (PyMemoTable_Get(self->memo, obj)) {
3220 const char pop_mark_op = POP_MARK;
3221
3222 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3223 return -1;
3224 if (memo_get(self, obj) < 0)
3225 return -1;
3226 return 0;
3227 }
3228
3229 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3230 return -1;
3231 if (memo_put(self, obj) < 0)
3232 return -1;
3233
3234 return 0;
3235}
3236
3237static int
3238fix_imports(PyObject **module_name, PyObject **global_name)
3239{
3240 PyObject *key;
3241 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003242 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003243
3244 key = PyTuple_Pack(2, *module_name, *global_name);
3245 if (key == NULL)
3246 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003247 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003248 Py_DECREF(key);
3249 if (item) {
3250 PyObject *fixed_module_name;
3251 PyObject *fixed_global_name;
3252
3253 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3254 PyErr_Format(PyExc_RuntimeError,
3255 "_compat_pickle.REVERSE_NAME_MAPPING values "
3256 "should be 2-tuples, not %.200s",
3257 Py_TYPE(item)->tp_name);
3258 return -1;
3259 }
3260 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3261 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3262 if (!PyUnicode_Check(fixed_module_name) ||
3263 !PyUnicode_Check(fixed_global_name)) {
3264 PyErr_Format(PyExc_RuntimeError,
3265 "_compat_pickle.REVERSE_NAME_MAPPING values "
3266 "should be pairs of str, not (%.200s, %.200s)",
3267 Py_TYPE(fixed_module_name)->tp_name,
3268 Py_TYPE(fixed_global_name)->tp_name);
3269 return -1;
3270 }
3271
3272 Py_CLEAR(*module_name);
3273 Py_CLEAR(*global_name);
3274 Py_INCREF(fixed_module_name);
3275 Py_INCREF(fixed_global_name);
3276 *module_name = fixed_module_name;
3277 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003278 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003279 }
3280 else if (PyErr_Occurred()) {
3281 return -1;
3282 }
3283
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003284 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003285 if (item) {
3286 if (!PyUnicode_Check(item)) {
3287 PyErr_Format(PyExc_RuntimeError,
3288 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3289 "should be strings, not %.200s",
3290 Py_TYPE(item)->tp_name);
3291 return -1;
3292 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003293 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003294 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003295 }
3296 else if (PyErr_Occurred()) {
3297 return -1;
3298 }
3299
3300 return 0;
3301}
3302
3303static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003304save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3305{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003306 PyObject *global_name = NULL;
3307 PyObject *module_name = NULL;
3308 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003309 PyObject *parent = NULL;
3310 PyObject *dotted_path = NULL;
3311 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003312 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003313 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003314 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003315 _Py_IDENTIFIER(__name__);
3316 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003317
3318 const char global_op = GLOBAL;
3319
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003320 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003321 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003322 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003323 }
3324 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003325 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3326 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003327 if (global_name == NULL) {
3328 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3329 if (global_name == NULL)
3330 goto error;
3331 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003332 }
3333
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003334 dotted_path = get_dotted_path(module, global_name);
3335 if (dotted_path == NULL)
3336 goto error;
3337 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003338 if (module_name == NULL)
3339 goto error;
3340
3341 /* XXX: Change to use the import C API directly with level=0 to disallow
3342 relative imports.
3343
3344 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3345 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3346 custom import functions (IMHO, this would be a nice security
3347 feature). The import C API would need to be extended to support the
3348 extra parameters of __import__ to fix that. */
3349 module = PyImport_Import(module_name);
3350 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003351 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003352 "Can't pickle %R: import of module %R failed",
3353 obj, module_name);
3354 goto error;
3355 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003356 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3357 Py_INCREF(lastname);
3358 cls = get_deep_attribute(module, dotted_path, &parent);
3359 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003360 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003361 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003362 "Can't pickle %R: attribute lookup %S on %S failed",
3363 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003364 goto error;
3365 }
3366 if (cls != obj) {
3367 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003368 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003369 "Can't pickle %R: it's not the same object as %S.%S",
3370 obj, module_name, global_name);
3371 goto error;
3372 }
3373 Py_DECREF(cls);
3374
3375 if (self->proto >= 2) {
3376 /* See whether this is in the extension registry, and if
3377 * so generate an EXT opcode.
3378 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003379 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003380 PyObject *code_obj; /* extension code as Python object */
3381 long code; /* extension code as C value */
3382 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003383 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003384
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003385 extension_key = PyTuple_Pack(2, module_name, global_name);
3386 if (extension_key == NULL) {
3387 goto error;
3388 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003389 code_obj = PyDict_GetItemWithError(st->extension_registry,
3390 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003391 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003392 /* The object is not registered in the extension registry.
3393 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003394 if (code_obj == NULL) {
3395 if (PyErr_Occurred()) {
3396 goto error;
3397 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003398 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003399 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003400
3401 /* XXX: pickle.py doesn't check neither the type, nor the range
3402 of the value returned by the extension_registry. It should for
3403 consistency. */
3404
3405 /* Verify code_obj has the right type and value. */
3406 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003407 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003408 "Can't pickle %R: extension code %R isn't an integer",
3409 obj, code_obj);
3410 goto error;
3411 }
3412 code = PyLong_AS_LONG(code_obj);
3413 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003414 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003415 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3416 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003417 goto error;
3418 }
3419
3420 /* Generate an EXT opcode. */
3421 if (code <= 0xff) {
3422 pdata[0] = EXT1;
3423 pdata[1] = (unsigned char)code;
3424 n = 2;
3425 }
3426 else if (code <= 0xffff) {
3427 pdata[0] = EXT2;
3428 pdata[1] = (unsigned char)(code & 0xff);
3429 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3430 n = 3;
3431 }
3432 else {
3433 pdata[0] = EXT4;
3434 pdata[1] = (unsigned char)(code & 0xff);
3435 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3436 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3437 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3438 n = 5;
3439 }
3440
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003441 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003442 goto error;
3443 }
3444 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003445 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003446 if (parent == module) {
3447 Py_INCREF(lastname);
3448 Py_DECREF(global_name);
3449 global_name = lastname;
3450 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003451 if (self->proto >= 4) {
3452 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003453
Christian Heimese8b1ba12013-11-23 21:13:39 +01003454 if (save(self, module_name, 0) < 0)
3455 goto error;
3456 if (save(self, global_name, 0) < 0)
3457 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003458
3459 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3460 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003461 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003462 else if (parent != module) {
3463 PickleState *st = _Pickle_GetGlobalState();
3464 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3465 st->getattr, parent, lastname);
Alexey Izbyshevf8c06b02018-08-22 07:51:25 +03003466 if (reduce_value == NULL)
3467 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003468 status = save_reduce(self, reduce_value, NULL);
3469 Py_DECREF(reduce_value);
3470 if (status < 0)
3471 goto error;
3472 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003473 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003474 /* Generate a normal global opcode if we are using a pickle
3475 protocol < 4, or if the object is not registered in the
3476 extension registry. */
3477 PyObject *encoded;
3478 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003479
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003480 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003481 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003482
3483 /* For protocol < 3 and if the user didn't request against doing
3484 so, we convert module names to the old 2.x module names. */
3485 if (self->proto < 3 && self->fix_imports) {
3486 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003487 goto error;
3488 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003489 }
3490
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003491 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3492 both the module name and the global name using UTF-8. We do so
3493 only when we are using the pickle protocol newer than version
3494 3. This is to ensure compatibility with older Unpickler running
3495 on Python 2.x. */
3496 if (self->proto == 3) {
3497 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003498 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003499 else {
3500 unicode_encoder = PyUnicode_AsASCIIString;
3501 }
3502 encoded = unicode_encoder(module_name);
3503 if (encoded == NULL) {
3504 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003505 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003506 "can't pickle module identifier '%S' using "
3507 "pickle protocol %i",
3508 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003509 goto error;
3510 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003511 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3512 PyBytes_GET_SIZE(encoded)) < 0) {
3513 Py_DECREF(encoded);
3514 goto error;
3515 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003516 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003517 if(_Pickler_Write(self, "\n", 1) < 0)
3518 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003519
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003520 /* Save the name of the module. */
3521 encoded = unicode_encoder(global_name);
3522 if (encoded == NULL) {
3523 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003524 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003525 "can't pickle global identifier '%S' using "
3526 "pickle protocol %i",
3527 global_name, self->proto);
3528 goto error;
3529 }
3530 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3531 PyBytes_GET_SIZE(encoded)) < 0) {
3532 Py_DECREF(encoded);
3533 goto error;
3534 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003535 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003536 if (_Pickler_Write(self, "\n", 1) < 0)
3537 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003538 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003539 /* Memoize the object. */
3540 if (memo_put(self, obj) < 0)
3541 goto error;
3542 }
3543
3544 if (0) {
3545 error:
3546 status = -1;
3547 }
3548 Py_XDECREF(module_name);
3549 Py_XDECREF(global_name);
3550 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003551 Py_XDECREF(parent);
3552 Py_XDECREF(dotted_path);
3553 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003554
3555 return status;
3556}
3557
3558static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003559save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3560{
3561 PyObject *reduce_value;
3562 int status;
3563
3564 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3565 if (reduce_value == NULL) {
3566 return -1;
3567 }
3568 status = save_reduce(self, reduce_value, obj);
3569 Py_DECREF(reduce_value);
3570 return status;
3571}
3572
3573static int
3574save_type(PicklerObject *self, PyObject *obj)
3575{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003576 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003577 return save_singleton_type(self, obj, Py_None);
3578 }
3579 else if (obj == (PyObject *)&PyEllipsis_Type) {
3580 return save_singleton_type(self, obj, Py_Ellipsis);
3581 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003582 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003583 return save_singleton_type(self, obj, Py_NotImplemented);
3584 }
3585 return save_global(self, obj, NULL);
3586}
3587
3588static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003589save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003590{
3591 PyObject *pid = NULL;
3592 int status = 0;
3593
3594 const char persid_op = PERSID;
3595 const char binpersid_op = BINPERSID;
3596
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003597 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003598 if (pid == NULL)
3599 return -1;
3600
3601 if (pid != Py_None) {
3602 if (self->bin) {
3603 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003604 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003605 goto error;
3606 }
3607 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003608 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003609
3610 pid_str = PyObject_Str(pid);
3611 if (pid_str == NULL)
3612 goto error;
3613
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003614 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003615 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003616 if (!PyUnicode_IS_ASCII(pid_str)) {
3617 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3618 "persistent IDs in protocol 0 must be "
3619 "ASCII strings");
3620 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003621 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003622 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003623
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003624 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003625 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3626 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3627 _Pickler_Write(self, "\n", 1) < 0) {
3628 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003629 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003630 }
3631 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003632 }
3633 status = 1;
3634 }
3635
3636 if (0) {
3637 error:
3638 status = -1;
3639 }
3640 Py_XDECREF(pid);
3641
3642 return status;
3643}
3644
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003645static PyObject *
3646get_class(PyObject *obj)
3647{
3648 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003649 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003650
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003651 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3652 cls = (PyObject *) Py_TYPE(obj);
3653 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003654 }
3655 return cls;
3656}
3657
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003658/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3659 * appropriate __reduce__ method for obj.
3660 */
3661static int
3662save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3663{
3664 PyObject *callable;
3665 PyObject *argtup;
3666 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003667 PyObject *listitems = Py_None;
3668 PyObject *dictitems = Py_None;
Pierre Glaser65d98d02019-05-08 21:40:25 +02003669 PyObject *state_setter = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003670 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003671 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003672 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003673
3674 const char reduce_op = REDUCE;
3675 const char build_op = BUILD;
3676 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003677 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003678
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003679 size = PyTuple_Size(args);
Pierre Glaser65d98d02019-05-08 21:40:25 +02003680 if (size < 2 || size > 6) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003681 PyErr_SetString(st->PicklingError, "tuple returned by "
Pierre Glaser65d98d02019-05-08 21:40:25 +02003682 "__reduce__ must contain 2 through 6 elements");
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003683 return -1;
3684 }
3685
Pierre Glaser65d98d02019-05-08 21:40:25 +02003686 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 6,
3687 &callable, &argtup, &state, &listitems, &dictitems,
3688 &state_setter))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003689 return -1;
3690
3691 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003692 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003693 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003694 return -1;
3695 }
3696 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003697 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003698 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003699 return -1;
3700 }
3701
3702 if (state == Py_None)
3703 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003704
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003705 if (listitems == Py_None)
3706 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003707 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003708 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003709 "returned by __reduce__ must be an iterator, not %s",
3710 Py_TYPE(listitems)->tp_name);
3711 return -1;
3712 }
3713
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003714 if (dictitems == Py_None)
3715 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003716 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003717 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003718 "returned by __reduce__ must be an iterator, not %s",
3719 Py_TYPE(dictitems)->tp_name);
3720 return -1;
3721 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003722
Pierre Glaser65d98d02019-05-08 21:40:25 +02003723 if (state_setter == Py_None)
3724 state_setter = NULL;
3725 else if (!PyCallable_Check(state_setter)) {
3726 PyErr_Format(st->PicklingError, "sixth element of the tuple "
3727 "returned by __reduce__ must be a function, not %s",
3728 Py_TYPE(state_setter)->tp_name);
3729 return -1;
3730 }
3731
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003732 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003733 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003734 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003735
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003736 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3737 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003738 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003739 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003740 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003741 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3742 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003743 if (!use_newobj_ex) {
3744 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003745 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003746 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003747 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003748 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003749 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003750
3751 if (use_newobj_ex) {
3752 PyObject *cls;
3753 PyObject *args;
3754 PyObject *kwargs;
3755
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003756 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003757 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003758 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003759 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003760 return -1;
3761 }
3762
3763 cls = PyTuple_GET_ITEM(argtup, 0);
3764 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003765 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003766 "first item from NEWOBJ_EX argument tuple must "
3767 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3768 return -1;
3769 }
3770 args = PyTuple_GET_ITEM(argtup, 1);
3771 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003772 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003773 "second item from NEWOBJ_EX argument tuple must "
3774 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3775 return -1;
3776 }
3777 kwargs = PyTuple_GET_ITEM(argtup, 2);
3778 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003779 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003780 "third item from NEWOBJ_EX argument tuple must "
3781 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3782 return -1;
3783 }
3784
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003785 if (self->proto >= 4) {
3786 if (save(self, cls, 0) < 0 ||
3787 save(self, args, 0) < 0 ||
3788 save(self, kwargs, 0) < 0 ||
3789 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3790 return -1;
3791 }
3792 }
3793 else {
3794 PyObject *newargs;
3795 PyObject *cls_new;
3796 Py_ssize_t i;
3797 _Py_IDENTIFIER(__new__);
3798
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003799 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003800 if (newargs == NULL)
3801 return -1;
3802
3803 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3804 if (cls_new == NULL) {
3805 Py_DECREF(newargs);
3806 return -1;
3807 }
3808 PyTuple_SET_ITEM(newargs, 0, cls_new);
3809 Py_INCREF(cls);
3810 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003811 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003812 PyObject *item = PyTuple_GET_ITEM(args, i);
3813 Py_INCREF(item);
3814 PyTuple_SET_ITEM(newargs, i + 2, item);
3815 }
3816
3817 callable = PyObject_Call(st->partial, newargs, kwargs);
3818 Py_DECREF(newargs);
3819 if (callable == NULL)
3820 return -1;
3821
3822 newargs = PyTuple_New(0);
3823 if (newargs == NULL) {
3824 Py_DECREF(callable);
3825 return -1;
3826 }
3827
3828 if (save(self, callable, 0) < 0 ||
3829 save(self, newargs, 0) < 0 ||
3830 _Pickler_Write(self, &reduce_op, 1) < 0) {
3831 Py_DECREF(newargs);
3832 Py_DECREF(callable);
3833 return -1;
3834 }
3835 Py_DECREF(newargs);
3836 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003837 }
3838 }
3839 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003840 PyObject *cls;
3841 PyObject *newargtup;
3842 PyObject *obj_class;
3843 int p;
3844
3845 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003846 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003847 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003848 return -1;
3849 }
3850
3851 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003852 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003853 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003854 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003855 return -1;
3856 }
3857
3858 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003859 obj_class = get_class(obj);
Zackery Spytz25d38972018-12-05 11:29:20 -07003860 if (obj_class == NULL) {
3861 return -1;
3862 }
3863 p = obj_class != cls;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003864 Py_DECREF(obj_class);
3865 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003866 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003867 "__newobj__ args has the wrong class");
3868 return -1;
3869 }
3870 }
3871 /* XXX: These calls save() are prone to infinite recursion. Imagine
3872 what happen if the value returned by the __reduce__() method of
3873 some extension type contains another object of the same type. Ouch!
3874
3875 Here is a quick example, that I ran into, to illustrate what I
3876 mean:
3877
3878 >>> import pickle, copyreg
3879 >>> copyreg.dispatch_table.pop(complex)
3880 >>> pickle.dumps(1+2j)
3881 Traceback (most recent call last):
3882 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003883 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003884
3885 Removing the complex class from copyreg.dispatch_table made the
3886 __reduce_ex__() method emit another complex object:
3887
3888 >>> (1+1j).__reduce_ex__(2)
3889 (<function __newobj__ at 0xb7b71c3c>,
3890 (<class 'complex'>, (1+1j)), None, None, None)
3891
3892 Thus when save() was called on newargstup (the 2nd item) recursion
3893 ensued. Of course, the bug was in the complex class which had a
3894 broken __getnewargs__() that emitted another complex object. But,
3895 the point, here, is it is quite easy to end up with a broken reduce
3896 function. */
3897
3898 /* Save the class and its __new__ arguments. */
3899 if (save(self, cls, 0) < 0)
3900 return -1;
3901
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003902 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003903 if (newargtup == NULL)
3904 return -1;
3905
3906 p = save(self, newargtup, 0);
3907 Py_DECREF(newargtup);
3908 if (p < 0)
3909 return -1;
3910
3911 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003912 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003913 return -1;
3914 }
3915 else { /* Not using NEWOBJ. */
3916 if (save(self, callable, 0) < 0 ||
3917 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003918 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003919 return -1;
3920 }
3921
3922 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3923 the caller do not want to memoize the object. Not particularly useful,
3924 but that is to mimic the behavior save_reduce() in pickle.py when
3925 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003926 if (obj != NULL) {
3927 /* If the object is already in the memo, this means it is
3928 recursive. In this case, throw away everything we put on the
3929 stack, and fetch the object back from the memo. */
3930 if (PyMemoTable_Get(self->memo, obj)) {
3931 const char pop_op = POP;
3932
3933 if (_Pickler_Write(self, &pop_op, 1) < 0)
3934 return -1;
3935 if (memo_get(self, obj) < 0)
3936 return -1;
3937
3938 return 0;
3939 }
3940 else if (memo_put(self, obj) < 0)
3941 return -1;
3942 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943
3944 if (listitems && batch_list(self, listitems) < 0)
3945 return -1;
3946
3947 if (dictitems && batch_dict(self, dictitems) < 0)
3948 return -1;
3949
3950 if (state) {
Pierre Glaser65d98d02019-05-08 21:40:25 +02003951 if (state_setter == NULL) {
3952 if (save(self, state, 0) < 0 ||
3953 _Pickler_Write(self, &build_op, 1) < 0)
3954 return -1;
3955 }
3956 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003957
Pierre Glaser65d98d02019-05-08 21:40:25 +02003958 /* If a state_setter is specified, call it instead of load_build to
3959 * update obj's with its previous state.
3960 * The first 4 save/write instructions push state_setter and its
3961 * tuple of expected arguments (obj, state) onto the stack. The
3962 * REDUCE opcode triggers the state_setter(obj, state) function
3963 * call. Finally, because state-updating routines only do in-place
3964 * modification, the whole operation has to be stack-transparent.
3965 * Thus, we finally pop the call's output from the stack.*/
3966
3967 const char tupletwo_op = TUPLE2;
3968 const char pop_op = POP;
3969 if (save(self, state_setter, 0) < 0 ||
3970 save(self, obj, 0) < 0 || save(self, state, 0) < 0 ||
3971 _Pickler_Write(self, &tupletwo_op, 1) < 0 ||
3972 _Pickler_Write(self, &reduce_op, 1) < 0 ||
3973 _Pickler_Write(self, &pop_op, 1) < 0)
3974 return -1;
3975 }
3976 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003977 return 0;
3978}
3979
3980static int
3981save(PicklerObject *self, PyObject *obj, int pers_save)
3982{
3983 PyTypeObject *type;
3984 PyObject *reduce_func = NULL;
3985 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003986 int status = 0;
3987
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003988 if (_Pickler_OpcodeBoundary(self) < 0)
3989 return -1;
3990
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003991 /* The extra pers_save argument is necessary to avoid calling save_pers()
3992 on its returned object. */
3993 if (!pers_save && self->pers_func) {
3994 /* save_pers() returns:
3995 -1 to signal an error;
3996 0 if it did nothing successfully;
3997 1 if a persistent id was saved.
3998 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003999 if ((status = save_pers(self, obj)) != 0)
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004000 return status;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004001 }
4002
4003 type = Py_TYPE(obj);
4004
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004005 /* The old cPickle had an optimization that used switch-case statement
4006 dispatching on the first letter of the type name. This has was removed
4007 since benchmarks shown that this optimization was actually slowing
4008 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004009
4010 /* Atom types; these aren't memoized, so don't check the memo. */
4011
4012 if (obj == Py_None) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004013 return save_none(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004014 }
4015 else if (obj == Py_False || obj == Py_True) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004016 return save_bool(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004017 }
4018 else if (type == &PyLong_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004019 return save_long(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004020 }
4021 else if (type == &PyFloat_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004022 return save_float(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004023 }
4024
4025 /* Check the memo to see if it has the object. If so, generate
4026 a GET (or BINGET) opcode, instead of pickling the object
4027 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004028 if (PyMemoTable_Get(self->memo, obj)) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004029 return memo_get(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004030 }
4031
4032 if (type == &PyBytes_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004033 return save_bytes(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004034 }
4035 else if (type == &PyUnicode_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004036 return save_unicode(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004037 }
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004038
4039 /* We're only calling Py_EnterRecursiveCall here so that atomic
4040 types above are pickled faster. */
4041 if (Py_EnterRecursiveCall(" while pickling an object")) {
4042 return -1;
4043 }
4044
4045 if (type == &PyDict_Type) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004046 status = save_dict(self, obj);
4047 goto done;
4048 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004049 else if (type == &PySet_Type) {
4050 status = save_set(self, obj);
4051 goto done;
4052 }
4053 else if (type == &PyFrozenSet_Type) {
4054 status = save_frozenset(self, obj);
4055 goto done;
4056 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004057 else if (type == &PyList_Type) {
4058 status = save_list(self, obj);
4059 goto done;
4060 }
4061 else if (type == &PyTuple_Type) {
4062 status = save_tuple(self, obj);
4063 goto done;
4064 }
Pierre Glaser289f1f82019-05-08 23:08:25 +02004065
4066 /* Now, check reducer_override. If it returns NotImplemented,
4067 * fallback to save_type or save_global, and then perhaps to the
4068 * regular reduction mechanism.
4069 */
4070 if (self->reducer_override != NULL) {
4071 reduce_value = PyObject_CallFunctionObjArgs(self->reducer_override,
4072 obj, NULL);
4073 if (reduce_value == NULL) {
4074 goto error;
4075 }
4076 if (reduce_value != Py_NotImplemented) {
4077 goto reduce;
4078 }
4079 Py_DECREF(reduce_value);
4080 reduce_value = NULL;
4081 }
4082
4083 if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004084 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004085 goto done;
4086 }
4087 else if (type == &PyFunction_Type) {
4088 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004089 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004090 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004091
4092 /* XXX: This part needs some unit tests. */
4093
4094 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004095 * self.dispatch_table, copyreg.dispatch_table, the object's
4096 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004097 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004098 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004099 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004100 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4101 (PyObject *)type);
4102 if (reduce_func == NULL) {
4103 if (PyErr_Occurred()) {
4104 goto error;
4105 }
4106 } else {
4107 /* PyDict_GetItemWithError() returns a borrowed reference.
4108 Increase the reference count to be consistent with
4109 PyObject_GetItem and _PyObject_GetAttrId used below. */
4110 Py_INCREF(reduce_func);
4111 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004112 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004113 reduce_func = PyObject_GetItem(self->dispatch_table,
4114 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004115 if (reduce_func == NULL) {
4116 if (PyErr_ExceptionMatches(PyExc_KeyError))
4117 PyErr_Clear();
4118 else
4119 goto error;
4120 }
4121 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004122 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004123 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004124 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004125 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004126 else if (PyType_IsSubtype(type, &PyType_Type)) {
4127 status = save_global(self, obj, NULL);
4128 goto done;
4129 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004130 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004131 _Py_IDENTIFIER(__reduce__);
4132 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004133
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004134
4135 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4136 automatically defined as __reduce__. While this is convenient, this
4137 make it impossible to know which method was actually called. Of
4138 course, this is not a big deal. But still, it would be nice to let
4139 the user know which method was called when something go
4140 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4141 don't actually have to check for a __reduce__ method. */
4142
4143 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004144 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4145 goto error;
4146 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004147 if (reduce_func != NULL) {
4148 PyObject *proto;
4149 proto = PyLong_FromLong(self->proto);
4150 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004151 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004152 }
4153 }
4154 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004155 PickleState *st = _Pickle_GetGlobalState();
4156
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004157 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004158 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004159 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004160 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004161 }
4162 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004163 PyErr_Format(st->PicklingError,
4164 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004165 type->tp_name, obj);
4166 goto error;
4167 }
4168 }
4169 }
4170
4171 if (reduce_value == NULL)
4172 goto error;
4173
Pierre Glaser289f1f82019-05-08 23:08:25 +02004174 reduce:
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004175 if (PyUnicode_Check(reduce_value)) {
4176 status = save_global(self, obj, reduce_value);
4177 goto done;
4178 }
4179
4180 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004181 PickleState *st = _Pickle_GetGlobalState();
4182 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004183 "__reduce__ must return a string or tuple");
4184 goto error;
4185 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004186
4187 status = save_reduce(self, reduce_value, obj);
4188
4189 if (0) {
4190 error:
4191 status = -1;
4192 }
4193 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004194
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004195 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004196 Py_XDECREF(reduce_func);
4197 Py_XDECREF(reduce_value);
4198
4199 return status;
4200}
4201
4202static int
4203dump(PicklerObject *self, PyObject *obj)
4204{
4205 const char stop_op = STOP;
Pierre Glaser289f1f82019-05-08 23:08:25 +02004206 PyObject *tmp;
4207 _Py_IDENTIFIER(reducer_override);
4208
4209 if (_PyObject_LookupAttrId((PyObject *)self, &PyId_reducer_override,
4210 &tmp) < 0) {
4211 return -1;
4212 }
4213 /* Cache the reducer_override method, if it exists. */
4214 if (tmp != NULL) {
4215 Py_XSETREF(self->reducer_override, tmp);
4216 }
4217 else {
4218 Py_CLEAR(self->reducer_override);
4219 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004220
4221 if (self->proto >= 2) {
4222 char header[2];
4223
4224 header[0] = PROTO;
4225 assert(self->proto >= 0 && self->proto < 256);
4226 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004227 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004228 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004229 if (self->proto >= 4)
4230 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004231 }
4232
4233 if (save(self, obj, 0) < 0 ||
Serhiy Storchakac8695292018-04-04 00:11:27 +03004234 _Pickler_Write(self, &stop_op, 1) < 0 ||
4235 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004236 return -1;
Serhiy Storchakac8695292018-04-04 00:11:27 +03004237 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004238 return 0;
4239}
4240
Larry Hastings61272b72014-01-07 12:41:53 -08004241/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004242
4243_pickle.Pickler.clear_memo
4244
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004245Clears the pickler's "memo".
4246
4247The memo is the data structure that remembers which objects the
4248pickler has already seen, so that shared or recursive objects are
4249pickled by reference and not by value. This method is useful when
4250re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004251[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004252
Larry Hastings3cceb382014-01-04 11:09:09 -08004253static PyObject *
4254_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004255/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004256{
4257 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004258 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004259
4260 Py_RETURN_NONE;
4261}
4262
Larry Hastings61272b72014-01-07 12:41:53 -08004263/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004264
4265_pickle.Pickler.dump
4266
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004267 obj: object
4268 /
4269
4270Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004271[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004272
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004273static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004274_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004275/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004276{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004277 /* Check whether the Pickler was initialized correctly (issue3664).
4278 Developers often forget to call __init__() in their subclasses, which
4279 would trigger a segfault without this check. */
4280 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004281 PickleState *st = _Pickle_GetGlobalState();
4282 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004283 "Pickler.__init__() was not called by %s.__init__()",
4284 Py_TYPE(self)->tp_name);
4285 return NULL;
4286 }
4287
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004288 if (_Pickler_ClearBuffer(self) < 0)
4289 return NULL;
4290
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004291 if (dump(self, obj) < 0)
4292 return NULL;
4293
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004294 if (_Pickler_FlushToFile(self) < 0)
4295 return NULL;
4296
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004297 Py_RETURN_NONE;
4298}
4299
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004300/*[clinic input]
4301
4302_pickle.Pickler.__sizeof__ -> Py_ssize_t
4303
4304Returns size in memory, in bytes.
4305[clinic start generated code]*/
4306
4307static Py_ssize_t
4308_pickle_Pickler___sizeof___impl(PicklerObject *self)
4309/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4310{
4311 Py_ssize_t res, s;
4312
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004313 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004314 if (self->memo != NULL) {
4315 res += sizeof(PyMemoTable);
4316 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4317 }
4318 if (self->output_buffer != NULL) {
4319 s = _PySys_GetSizeOf(self->output_buffer);
4320 if (s == -1)
4321 return -1;
4322 res += s;
4323 }
4324 return res;
4325}
4326
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004327static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004328 _PICKLE_PICKLER_DUMP_METHODDEF
4329 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004330 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004331 {NULL, NULL} /* sentinel */
4332};
4333
4334static void
4335Pickler_dealloc(PicklerObject *self)
4336{
4337 PyObject_GC_UnTrack(self);
4338
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004339 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004340 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004341 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004342 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004343 Py_XDECREF(self->fast_memo);
Pierre Glaser289f1f82019-05-08 23:08:25 +02004344 Py_XDECREF(self->reducer_override);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004345
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004346 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004347
4348 Py_TYPE(self)->tp_free((PyObject *)self);
4349}
4350
4351static int
4352Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4353{
4354 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004355 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004356 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004357 Py_VISIT(self->fast_memo);
Pierre Glaser289f1f82019-05-08 23:08:25 +02004358 Py_VISIT(self->reducer_override);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004359 return 0;
4360}
4361
4362static int
4363Pickler_clear(PicklerObject *self)
4364{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004365 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004366 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004367 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004368 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004369 Py_CLEAR(self->fast_memo);
Pierre Glaser289f1f82019-05-08 23:08:25 +02004370 Py_CLEAR(self->reducer_override);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004371
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004372 if (self->memo != NULL) {
4373 PyMemoTable *memo = self->memo;
4374 self->memo = NULL;
4375 PyMemoTable_Del(memo);
4376 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004377 return 0;
4378}
4379
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004380
Larry Hastings61272b72014-01-07 12:41:53 -08004381/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004382
4383_pickle.Pickler.__init__
4384
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004385 file: object
4386 protocol: object = NULL
4387 fix_imports: bool = True
4388
4389This takes a binary file for writing a pickle data stream.
4390
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004391The optional *protocol* argument tells the pickler to use the given
4392protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4393protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004394
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004395Specifying a negative protocol version selects the highest protocol
4396version supported. The higher the protocol used, the more recent the
4397version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004398
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004399The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004400bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004401writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004402this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004403
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004404If *fix_imports* is True and protocol is less than 3, pickle will try
4405to map the new Python 3 names to the old module names used in Python
44062, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004407[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004408
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004409static int
Larry Hastings89964c42015-04-14 18:07:59 -04004410_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4411 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004412/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004413{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004414 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004415 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004416
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004417 /* In case of multiple __init__() calls, clear previous content. */
4418 if (self->write != NULL)
4419 (void)Pickler_clear(self);
4420
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004421 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004422 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004423
4424 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004425 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004426
4427 /* memo and output_buffer may have already been created in _Pickler_New */
4428 if (self->memo == NULL) {
4429 self->memo = PyMemoTable_New();
4430 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004431 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004432 }
4433 self->output_len = 0;
4434 if (self->output_buffer == NULL) {
4435 self->max_output_len = WRITE_BUF_SIZE;
4436 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4437 self->max_output_len);
4438 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004439 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004440 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004441
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004442 self->fast = 0;
4443 self->fast_nesting = 0;
4444 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004445
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004446 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4447 &self->pers_func, &self->pers_func_self) < 0)
4448 {
4449 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004450 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004451
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004452 if (_PyObject_LookupAttrId((PyObject *)self,
4453 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4454 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004455 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004456
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004457 return 0;
4458}
4459
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004460
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004461/* Define a proxy object for the Pickler's internal memo object. This is to
4462 * avoid breaking code like:
4463 * pickler.memo.clear()
4464 * and
4465 * pickler.memo = saved_memo
4466 * Is this a good idea? Not really, but we don't want to break code that uses
4467 * it. Note that we don't implement the entire mapping API here. This is
4468 * intentional, as these should be treated as black-box implementation details.
4469 */
4470
Larry Hastings61272b72014-01-07 12:41:53 -08004471/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004472_pickle.PicklerMemoProxy.clear
4473
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004474Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004475[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004476
Larry Hastings3cceb382014-01-04 11:09:09 -08004477static PyObject *
4478_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004479/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004480{
4481 if (self->pickler->memo)
4482 PyMemoTable_Clear(self->pickler->memo);
4483 Py_RETURN_NONE;
4484}
4485
Larry Hastings61272b72014-01-07 12:41:53 -08004486/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004487_pickle.PicklerMemoProxy.copy
4488
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004489Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004490[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004491
Larry Hastings3cceb382014-01-04 11:09:09 -08004492static PyObject *
4493_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004494/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004495{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004496 PyMemoTable *memo;
4497 PyObject *new_memo = PyDict_New();
4498 if (new_memo == NULL)
4499 return NULL;
4500
4501 memo = self->pickler->memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07004502 for (size_t i = 0; i < memo->mt_allocated; ++i) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004503 PyMemoEntry entry = memo->mt_table[i];
4504 if (entry.me_key != NULL) {
4505 int status;
4506 PyObject *key, *value;
4507
4508 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004509 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004510
4511 if (key == NULL || value == NULL) {
4512 Py_XDECREF(key);
4513 Py_XDECREF(value);
4514 goto error;
4515 }
4516 status = PyDict_SetItem(new_memo, key, value);
4517 Py_DECREF(key);
4518 Py_DECREF(value);
4519 if (status < 0)
4520 goto error;
4521 }
4522 }
4523 return new_memo;
4524
4525 error:
4526 Py_XDECREF(new_memo);
4527 return NULL;
4528}
4529
Larry Hastings61272b72014-01-07 12:41:53 -08004530/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004531_pickle.PicklerMemoProxy.__reduce__
4532
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004533Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004534[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004535
Larry Hastings3cceb382014-01-04 11:09:09 -08004536static PyObject *
4537_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004538/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004539{
4540 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004541 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004542 if (contents == NULL)
4543 return NULL;
4544
4545 reduce_value = PyTuple_New(2);
4546 if (reduce_value == NULL) {
4547 Py_DECREF(contents);
4548 return NULL;
4549 }
4550 dict_args = PyTuple_New(1);
4551 if (dict_args == NULL) {
4552 Py_DECREF(contents);
4553 Py_DECREF(reduce_value);
4554 return NULL;
4555 }
4556 PyTuple_SET_ITEM(dict_args, 0, contents);
4557 Py_INCREF((PyObject *)&PyDict_Type);
4558 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4559 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4560 return reduce_value;
4561}
4562
4563static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004564 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4565 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4566 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004567 {NULL, NULL} /* sentinel */
4568};
4569
4570static void
4571PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4572{
4573 PyObject_GC_UnTrack(self);
4574 Py_XDECREF(self->pickler);
4575 PyObject_GC_Del((PyObject *)self);
4576}
4577
4578static int
4579PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4580 visitproc visit, void *arg)
4581{
4582 Py_VISIT(self->pickler);
4583 return 0;
4584}
4585
4586static int
4587PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4588{
4589 Py_CLEAR(self->pickler);
4590 return 0;
4591}
4592
4593static PyTypeObject PicklerMemoProxyType = {
4594 PyVarObject_HEAD_INIT(NULL, 0)
4595 "_pickle.PicklerMemoProxy", /*tp_name*/
4596 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4597 0,
4598 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4599 0, /* tp_print */
4600 0, /* tp_getattr */
4601 0, /* tp_setattr */
4602 0, /* tp_compare */
4603 0, /* tp_repr */
4604 0, /* tp_as_number */
4605 0, /* tp_as_sequence */
4606 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004607 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004608 0, /* tp_call */
4609 0, /* tp_str */
4610 PyObject_GenericGetAttr, /* tp_getattro */
4611 PyObject_GenericSetAttr, /* tp_setattro */
4612 0, /* tp_as_buffer */
4613 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4614 0, /* tp_doc */
4615 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4616 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4617 0, /* tp_richcompare */
4618 0, /* tp_weaklistoffset */
4619 0, /* tp_iter */
4620 0, /* tp_iternext */
4621 picklerproxy_methods, /* tp_methods */
4622};
4623
4624static PyObject *
4625PicklerMemoProxy_New(PicklerObject *pickler)
4626{
4627 PicklerMemoProxyObject *self;
4628
4629 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4630 if (self == NULL)
4631 return NULL;
4632 Py_INCREF(pickler);
4633 self->pickler = pickler;
4634 PyObject_GC_Track(self);
4635 return (PyObject *)self;
4636}
4637
4638/*****************************************************************************/
4639
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004640static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004641Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004642{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004643 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004644}
4645
4646static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004647Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004648{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004649 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004650
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004651 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004652 PyErr_SetString(PyExc_TypeError,
4653 "attribute deletion is not supported");
4654 return -1;
4655 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004656
4657 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4658 PicklerObject *pickler =
4659 ((PicklerMemoProxyObject *)obj)->pickler;
4660
4661 new_memo = PyMemoTable_Copy(pickler->memo);
4662 if (new_memo == NULL)
4663 return -1;
4664 }
4665 else if (PyDict_Check(obj)) {
4666 Py_ssize_t i = 0;
4667 PyObject *key, *value;
4668
4669 new_memo = PyMemoTable_New();
4670 if (new_memo == NULL)
4671 return -1;
4672
4673 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004674 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004675 PyObject *memo_obj;
4676
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004677 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004678 PyErr_SetString(PyExc_TypeError,
4679 "'memo' values must be 2-item tuples");
4680 goto error;
4681 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004682 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004683 if (memo_id == -1 && PyErr_Occurred())
4684 goto error;
4685 memo_obj = PyTuple_GET_ITEM(value, 1);
4686 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4687 goto error;
4688 }
4689 }
4690 else {
4691 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +02004692 "'memo' attribute must be a PicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004693 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004694 return -1;
4695 }
4696
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004697 PyMemoTable_Del(self->memo);
4698 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004699
4700 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004701
4702 error:
4703 if (new_memo)
4704 PyMemoTable_Del(new_memo);
4705 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004706}
4707
4708static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004709Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004710{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004711 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004712 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004713 return NULL;
4714 }
4715 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004716}
4717
4718static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004719Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004720{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004721 if (value == NULL) {
4722 PyErr_SetString(PyExc_TypeError,
4723 "attribute deletion is not supported");
4724 return -1;
4725 }
4726 if (!PyCallable_Check(value)) {
4727 PyErr_SetString(PyExc_TypeError,
4728 "persistent_id must be a callable taking one argument");
4729 return -1;
4730 }
4731
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004732 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004734 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004735
4736 return 0;
4737}
4738
4739static PyMemberDef Pickler_members[] = {
4740 {"bin", T_INT, offsetof(PicklerObject, bin)},
4741 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004742 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004743 {NULL}
4744};
4745
4746static PyGetSetDef Pickler_getsets[] = {
4747 {"memo", (getter)Pickler_get_memo,
4748 (setter)Pickler_set_memo},
4749 {"persistent_id", (getter)Pickler_get_persid,
4750 (setter)Pickler_set_persid},
4751 {NULL}
4752};
4753
4754static PyTypeObject Pickler_Type = {
4755 PyVarObject_HEAD_INIT(NULL, 0)
4756 "_pickle.Pickler" , /*tp_name*/
4757 sizeof(PicklerObject), /*tp_basicsize*/
4758 0, /*tp_itemsize*/
4759 (destructor)Pickler_dealloc, /*tp_dealloc*/
4760 0, /*tp_print*/
4761 0, /*tp_getattr*/
4762 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004763 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004764 0, /*tp_repr*/
4765 0, /*tp_as_number*/
4766 0, /*tp_as_sequence*/
4767 0, /*tp_as_mapping*/
4768 0, /*tp_hash*/
4769 0, /*tp_call*/
4770 0, /*tp_str*/
4771 0, /*tp_getattro*/
4772 0, /*tp_setattro*/
4773 0, /*tp_as_buffer*/
4774 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004775 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004776 (traverseproc)Pickler_traverse, /*tp_traverse*/
4777 (inquiry)Pickler_clear, /*tp_clear*/
4778 0, /*tp_richcompare*/
4779 0, /*tp_weaklistoffset*/
4780 0, /*tp_iter*/
4781 0, /*tp_iternext*/
4782 Pickler_methods, /*tp_methods*/
4783 Pickler_members, /*tp_members*/
4784 Pickler_getsets, /*tp_getset*/
4785 0, /*tp_base*/
4786 0, /*tp_dict*/
4787 0, /*tp_descr_get*/
4788 0, /*tp_descr_set*/
4789 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004790 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004791 PyType_GenericAlloc, /*tp_alloc*/
4792 PyType_GenericNew, /*tp_new*/
4793 PyObject_GC_Del, /*tp_free*/
4794 0, /*tp_is_gc*/
4795};
4796
Victor Stinner121aab42011-09-29 23:40:53 +02004797/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004798
4799 XXX: It would be nice to able to avoid Python function call overhead, by
4800 using directly the C version of find_class(), when find_class() is not
4801 overridden by a subclass. Although, this could become rather hackish. A
4802 simpler optimization would be to call the C function when self is not a
4803 subclass instance. */
4804static PyObject *
4805find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4806{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004807 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004808
Victor Stinner55ba38a2016-12-09 16:09:30 +01004809 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4810 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004811}
4812
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004813static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004814marker(UnpicklerObject *self)
4815{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004816 Py_ssize_t mark;
4817
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004818 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004819 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004820 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004821 return -1;
4822 }
4823
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004824 mark = self->marks[--self->num_marks];
4825 self->stack->mark_set = self->num_marks != 0;
4826 self->stack->fence = self->num_marks ?
4827 self->marks[self->num_marks - 1] : 0;
4828 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004829}
4830
4831static int
4832load_none(UnpicklerObject *self)
4833{
4834 PDATA_APPEND(self->stack, Py_None, -1);
4835 return 0;
4836}
4837
4838static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004839load_int(UnpicklerObject *self)
4840{
4841 PyObject *value;
4842 char *endptr, *s;
4843 Py_ssize_t len;
4844 long x;
4845
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004846 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004847 return -1;
4848 if (len < 2)
4849 return bad_readline();
4850
4851 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004852 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004853 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004854 x = strtol(s, &endptr, 0);
4855
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004856 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004857 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004858 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004859 errno = 0;
4860 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004861 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004862 if (value == NULL) {
4863 PyErr_SetString(PyExc_ValueError,
4864 "could not convert string to int");
4865 return -1;
4866 }
4867 }
4868 else {
4869 if (len == 3 && (x == 0 || x == 1)) {
4870 if ((value = PyBool_FromLong(x)) == NULL)
4871 return -1;
4872 }
4873 else {
4874 if ((value = PyLong_FromLong(x)) == NULL)
4875 return -1;
4876 }
4877 }
4878
4879 PDATA_PUSH(self->stack, value, -1);
4880 return 0;
4881}
4882
4883static int
4884load_bool(UnpicklerObject *self, PyObject *boolean)
4885{
4886 assert(boolean == Py_True || boolean == Py_False);
4887 PDATA_APPEND(self->stack, boolean, -1);
4888 return 0;
4889}
4890
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004891/* s contains x bytes of an unsigned little-endian integer. Return its value
4892 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4893 */
4894static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004895calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004896{
4897 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004898 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004899 size_t x = 0;
4900
Serhiy Storchakae0606192015-09-29 22:10:07 +03004901 if (nbytes > (int)sizeof(size_t)) {
4902 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4903 * have 64-bit size that can't be represented on 32-bit platform.
4904 */
4905 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4906 if (s[i])
4907 return -1;
4908 }
4909 nbytes = (int)sizeof(size_t);
4910 }
4911 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004912 x |= (size_t) s[i] << (8 * i);
4913 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004914
4915 if (x > PY_SSIZE_T_MAX)
4916 return -1;
4917 else
4918 return (Py_ssize_t) x;
4919}
4920
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004921/* s contains x bytes of a little-endian integer. Return its value as a
4922 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004923 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004924 * of x-platform bugs.
4925 */
4926static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004927calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004928{
4929 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004930 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004931 long x = 0;
4932
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004933 for (i = 0; i < nbytes; i++) {
4934 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004935 }
4936
4937 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4938 * is signed, so on a box with longs bigger than 4 bytes we need
4939 * to extend a BININT's sign bit to the full width.
4940 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004941 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004942 x |= -(x & (1L << 31));
4943 }
4944
4945 return x;
4946}
4947
4948static int
4949load_binintx(UnpicklerObject *self, char *s, int size)
4950{
4951 PyObject *value;
4952 long x;
4953
4954 x = calc_binint(s, size);
4955
4956 if ((value = PyLong_FromLong(x)) == NULL)
4957 return -1;
4958
4959 PDATA_PUSH(self->stack, value, -1);
4960 return 0;
4961}
4962
4963static int
4964load_binint(UnpicklerObject *self)
4965{
4966 char *s;
4967
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004968 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004969 return -1;
4970
4971 return load_binintx(self, s, 4);
4972}
4973
4974static int
4975load_binint1(UnpicklerObject *self)
4976{
4977 char *s;
4978
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004979 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004980 return -1;
4981
4982 return load_binintx(self, s, 1);
4983}
4984
4985static int
4986load_binint2(UnpicklerObject *self)
4987{
4988 char *s;
4989
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004990 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004991 return -1;
4992
4993 return load_binintx(self, s, 2);
4994}
4995
4996static int
4997load_long(UnpicklerObject *self)
4998{
4999 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005000 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005001 Py_ssize_t len;
5002
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005003 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005004 return -1;
5005 if (len < 2)
5006 return bad_readline();
5007
Mark Dickinson8dd05142009-01-20 20:43:58 +00005008 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
5009 the 'L' before calling PyLong_FromString. In order to maintain
5010 compatibility with Python 3.0.0, we don't actually *require*
5011 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005012 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00005013 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00005014 /* XXX: Should the base argument explicitly set to 10? */
5015 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00005016 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005017 return -1;
5018
5019 PDATA_PUSH(self->stack, value, -1);
5020 return 0;
5021}
5022
5023/* 'size' bytes contain the # of bytes of little-endian 256's-complement
5024 * data following.
5025 */
5026static int
5027load_counted_long(UnpicklerObject *self, int size)
5028{
5029 PyObject *value;
5030 char *nbytes;
5031 char *pdata;
5032
5033 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005034 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005035 return -1;
5036
5037 size = calc_binint(nbytes, size);
5038 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005039 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005041 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042 "LONG pickle has negative byte count");
5043 return -1;
5044 }
5045
5046 if (size == 0)
5047 value = PyLong_FromLong(0L);
5048 else {
5049 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005050 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005051 return -1;
5052 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
5053 1 /* little endian */ , 1 /* signed */ );
5054 }
5055 if (value == NULL)
5056 return -1;
5057 PDATA_PUSH(self->stack, value, -1);
5058 return 0;
5059}
5060
5061static int
5062load_float(UnpicklerObject *self)
5063{
5064 PyObject *value;
5065 char *endptr, *s;
5066 Py_ssize_t len;
5067 double d;
5068
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005069 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005070 return -1;
5071 if (len < 2)
5072 return bad_readline();
5073
5074 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00005075 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
5076 if (d == -1.0 && PyErr_Occurred())
5077 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005078 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005079 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5080 return -1;
5081 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005082 value = PyFloat_FromDouble(d);
5083 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005084 return -1;
5085
5086 PDATA_PUSH(self->stack, value, -1);
5087 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005088}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005089
5090static int
5091load_binfloat(UnpicklerObject *self)
5092{
5093 PyObject *value;
5094 double x;
5095 char *s;
5096
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005097 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005098 return -1;
5099
5100 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5101 if (x == -1.0 && PyErr_Occurred())
5102 return -1;
5103
5104 if ((value = PyFloat_FromDouble(x)) == NULL)
5105 return -1;
5106
5107 PDATA_PUSH(self->stack, value, -1);
5108 return 0;
5109}
5110
5111static int
5112load_string(UnpicklerObject *self)
5113{
5114 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005115 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005116 Py_ssize_t len;
5117 char *s, *p;
5118
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005119 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005120 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005121 /* Strip the newline */
5122 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005123 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005124 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005125 p = s + 1;
5126 len -= 2;
5127 }
5128 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005129 PickleState *st = _Pickle_GetGlobalState();
5130 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005131 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005132 return -1;
5133 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005134 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005135
5136 /* Use the PyBytes API to decode the string, since that is what is used
5137 to encode, and then coerce the result to Unicode. */
5138 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005139 if (bytes == NULL)
5140 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005141
5142 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5143 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5144 if (strcmp(self->encoding, "bytes") == 0) {
5145 obj = bytes;
5146 }
5147 else {
5148 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5149 Py_DECREF(bytes);
5150 if (obj == NULL) {
5151 return -1;
5152 }
5153 }
5154
5155 PDATA_PUSH(self->stack, obj, -1);
5156 return 0;
5157}
5158
5159static int
5160load_counted_binstring(UnpicklerObject *self, int nbytes)
5161{
5162 PyObject *obj;
5163 Py_ssize_t size;
5164 char *s;
5165
5166 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005167 return -1;
5168
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005169 size = calc_binsize(s, nbytes);
5170 if (size < 0) {
5171 PickleState *st = _Pickle_GetGlobalState();
5172 PyErr_Format(st->UnpicklingError,
5173 "BINSTRING exceeds system's maximum size of %zd bytes",
5174 PY_SSIZE_T_MAX);
5175 return -1;
5176 }
5177
5178 if (_Unpickler_Read(self, &s, size) < 0)
5179 return -1;
5180
5181 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5182 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5183 if (strcmp(self->encoding, "bytes") == 0) {
5184 obj = PyBytes_FromStringAndSize(s, size);
5185 }
5186 else {
5187 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5188 }
5189 if (obj == NULL) {
5190 return -1;
5191 }
5192
5193 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005194 return 0;
5195}
5196
5197static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005198load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005199{
5200 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005201 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005202 char *s;
5203
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005204 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005205 return -1;
5206
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005207 size = calc_binsize(s, nbytes);
5208 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005209 PyErr_Format(PyExc_OverflowError,
5210 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005211 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005212 return -1;
5213 }
5214
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005215 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005216 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005217
5218 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005219 if (bytes == NULL)
5220 return -1;
5221
5222 PDATA_PUSH(self->stack, bytes, -1);
5223 return 0;
5224}
5225
5226static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005227load_unicode(UnpicklerObject *self)
5228{
5229 PyObject *str;
5230 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005231 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005232
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005233 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005234 return -1;
5235 if (len < 1)
5236 return bad_readline();
5237
5238 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5239 if (str == NULL)
5240 return -1;
5241
5242 PDATA_PUSH(self->stack, str, -1);
5243 return 0;
5244}
5245
5246static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005247load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005248{
5249 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005250 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005251 char *s;
5252
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005253 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005254 return -1;
5255
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005256 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005257 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005258 PyErr_Format(PyExc_OverflowError,
5259 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005260 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005261 return -1;
5262 }
5263
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005264 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005265 return -1;
5266
Victor Stinner485fb562010-04-13 11:07:24 +00005267 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005268 if (str == NULL)
5269 return -1;
5270
5271 PDATA_PUSH(self->stack, str, -1);
5272 return 0;
5273}
5274
5275static int
Victor Stinner21b47112016-03-14 18:09:39 +01005276load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005277{
5278 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005279
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005280 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005281 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005282
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005283 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005284 if (tuple == NULL)
5285 return -1;
5286 PDATA_PUSH(self->stack, tuple, -1);
5287 return 0;
5288}
5289
5290static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005291load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005292{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005293 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005294
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005295 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005296 return -1;
5297
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005298 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005299}
5300
5301static int
5302load_empty_list(UnpicklerObject *self)
5303{
5304 PyObject *list;
5305
5306 if ((list = PyList_New(0)) == NULL)
5307 return -1;
5308 PDATA_PUSH(self->stack, list, -1);
5309 return 0;
5310}
5311
5312static int
5313load_empty_dict(UnpicklerObject *self)
5314{
5315 PyObject *dict;
5316
5317 if ((dict = PyDict_New()) == NULL)
5318 return -1;
5319 PDATA_PUSH(self->stack, dict, -1);
5320 return 0;
5321}
5322
5323static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005324load_empty_set(UnpicklerObject *self)
5325{
5326 PyObject *set;
5327
5328 if ((set = PySet_New(NULL)) == NULL)
5329 return -1;
5330 PDATA_PUSH(self->stack, set, -1);
5331 return 0;
5332}
5333
5334static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005335load_list(UnpicklerObject *self)
5336{
5337 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005338 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005339
5340 if ((i = marker(self)) < 0)
5341 return -1;
5342
5343 list = Pdata_poplist(self->stack, i);
5344 if (list == NULL)
5345 return -1;
5346 PDATA_PUSH(self->stack, list, -1);
5347 return 0;
5348}
5349
5350static int
5351load_dict(UnpicklerObject *self)
5352{
5353 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005354 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005355
5356 if ((i = marker(self)) < 0)
5357 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005358 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005359
5360 if ((dict = PyDict_New()) == NULL)
5361 return -1;
5362
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005363 if ((j - i) % 2 != 0) {
5364 PickleState *st = _Pickle_GetGlobalState();
5365 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005366 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005367 return -1;
5368 }
5369
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005370 for (k = i + 1; k < j; k += 2) {
5371 key = self->stack->data[k - 1];
5372 value = self->stack->data[k];
5373 if (PyDict_SetItem(dict, key, value) < 0) {
5374 Py_DECREF(dict);
5375 return -1;
5376 }
5377 }
5378 Pdata_clear(self->stack, i);
5379 PDATA_PUSH(self->stack, dict, -1);
5380 return 0;
5381}
5382
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005383static int
5384load_frozenset(UnpicklerObject *self)
5385{
5386 PyObject *items;
5387 PyObject *frozenset;
5388 Py_ssize_t i;
5389
5390 if ((i = marker(self)) < 0)
5391 return -1;
5392
5393 items = Pdata_poptuple(self->stack, i);
5394 if (items == NULL)
5395 return -1;
5396
5397 frozenset = PyFrozenSet_New(items);
5398 Py_DECREF(items);
5399 if (frozenset == NULL)
5400 return -1;
5401
5402 PDATA_PUSH(self->stack, frozenset, -1);
5403 return 0;
5404}
5405
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005406static PyObject *
5407instantiate(PyObject *cls, PyObject *args)
5408{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005409 /* Caller must assure args are a tuple. Normally, args come from
5410 Pdata_poptuple which packs objects from the top of the stack
5411 into a newly created tuple. */
5412 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005413 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5414 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005415 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005416 PyObject *func;
5417 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5418 return NULL;
5419 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005420 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005421 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5422 }
5423 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005424 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005425 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005426}
5427
5428static int
5429load_obj(UnpicklerObject *self)
5430{
5431 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005432 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005433
5434 if ((i = marker(self)) < 0)
5435 return -1;
5436
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005437 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005438 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005439
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005440 args = Pdata_poptuple(self->stack, i + 1);
5441 if (args == NULL)
5442 return -1;
5443
5444 PDATA_POP(self->stack, cls);
5445 if (cls) {
5446 obj = instantiate(cls, args);
5447 Py_DECREF(cls);
5448 }
5449 Py_DECREF(args);
5450 if (obj == NULL)
5451 return -1;
5452
5453 PDATA_PUSH(self->stack, obj, -1);
5454 return 0;
5455}
5456
5457static int
5458load_inst(UnpicklerObject *self)
5459{
5460 PyObject *cls = NULL;
5461 PyObject *args = NULL;
5462 PyObject *obj = NULL;
5463 PyObject *module_name;
5464 PyObject *class_name;
5465 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005466 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005467 char *s;
5468
5469 if ((i = marker(self)) < 0)
5470 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005471 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005472 return -1;
5473 if (len < 2)
5474 return bad_readline();
5475
5476 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5477 identifiers are permitted in Python 3.0, since the INST opcode is only
5478 supported by older protocols on Python 2.x. */
5479 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5480 if (module_name == NULL)
5481 return -1;
5482
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005483 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005484 if (len < 2) {
5485 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005486 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005487 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005488 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005489 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005490 cls = find_class(self, module_name, class_name);
5491 Py_DECREF(class_name);
5492 }
5493 }
5494 Py_DECREF(module_name);
5495
5496 if (cls == NULL)
5497 return -1;
5498
5499 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5500 obj = instantiate(cls, args);
5501 Py_DECREF(args);
5502 }
5503 Py_DECREF(cls);
5504
5505 if (obj == NULL)
5506 return -1;
5507
5508 PDATA_PUSH(self->stack, obj, -1);
5509 return 0;
5510}
5511
5512static int
5513load_newobj(UnpicklerObject *self)
5514{
5515 PyObject *args = NULL;
5516 PyObject *clsraw = NULL;
5517 PyTypeObject *cls; /* clsraw cast to its true type */
5518 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005519 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005520
5521 /* Stack is ... cls argtuple, and we want to call
5522 * cls.__new__(cls, *argtuple).
5523 */
5524 PDATA_POP(self->stack, args);
5525 if (args == NULL)
5526 goto error;
5527 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005528 PyErr_SetString(st->UnpicklingError,
5529 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005530 goto error;
5531 }
5532
5533 PDATA_POP(self->stack, clsraw);
5534 cls = (PyTypeObject *)clsraw;
5535 if (cls == NULL)
5536 goto error;
5537 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005538 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005539 "isn't a type object");
5540 goto error;
5541 }
5542 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005543 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005544 "has NULL tp_new");
5545 goto error;
5546 }
5547
5548 /* Call __new__. */
5549 obj = cls->tp_new(cls, args, NULL);
5550 if (obj == NULL)
5551 goto error;
5552
5553 Py_DECREF(args);
5554 Py_DECREF(clsraw);
5555 PDATA_PUSH(self->stack, obj, -1);
5556 return 0;
5557
5558 error:
5559 Py_XDECREF(args);
5560 Py_XDECREF(clsraw);
5561 return -1;
5562}
5563
5564static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005565load_newobj_ex(UnpicklerObject *self)
5566{
5567 PyObject *cls, *args, *kwargs;
5568 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005569 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005570
5571 PDATA_POP(self->stack, kwargs);
5572 if (kwargs == NULL) {
5573 return -1;
5574 }
5575 PDATA_POP(self->stack, args);
5576 if (args == NULL) {
5577 Py_DECREF(kwargs);
5578 return -1;
5579 }
5580 PDATA_POP(self->stack, cls);
5581 if (cls == NULL) {
5582 Py_DECREF(kwargs);
5583 Py_DECREF(args);
5584 return -1;
5585 }
Larry Hastings61272b72014-01-07 12:41:53 -08005586
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005587 if (!PyType_Check(cls)) {
5588 Py_DECREF(kwargs);
5589 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005590 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005591 "NEWOBJ_EX class argument must be a type, not %.200s",
5592 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005593 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005594 return -1;
5595 }
5596
5597 if (((PyTypeObject *)cls)->tp_new == NULL) {
5598 Py_DECREF(kwargs);
5599 Py_DECREF(args);
5600 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005601 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005602 "NEWOBJ_EX class argument doesn't have __new__");
5603 return -1;
5604 }
5605 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5606 Py_DECREF(kwargs);
5607 Py_DECREF(args);
5608 Py_DECREF(cls);
5609 if (obj == NULL) {
5610 return -1;
5611 }
5612 PDATA_PUSH(self->stack, obj, -1);
5613 return 0;
5614}
5615
5616static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005617load_global(UnpicklerObject *self)
5618{
5619 PyObject *global = NULL;
5620 PyObject *module_name;
5621 PyObject *global_name;
5622 Py_ssize_t len;
5623 char *s;
5624
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005625 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005626 return -1;
5627 if (len < 2)
5628 return bad_readline();
5629 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5630 if (!module_name)
5631 return -1;
5632
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005633 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634 if (len < 2) {
5635 Py_DECREF(module_name);
5636 return bad_readline();
5637 }
5638 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5639 if (global_name) {
5640 global = find_class(self, module_name, global_name);
5641 Py_DECREF(global_name);
5642 }
5643 }
5644 Py_DECREF(module_name);
5645
5646 if (global == NULL)
5647 return -1;
5648 PDATA_PUSH(self->stack, global, -1);
5649 return 0;
5650}
5651
5652static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005653load_stack_global(UnpicklerObject *self)
5654{
5655 PyObject *global;
5656 PyObject *module_name;
5657 PyObject *global_name;
5658
5659 PDATA_POP(self->stack, global_name);
5660 PDATA_POP(self->stack, module_name);
5661 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5662 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005663 PickleState *st = _Pickle_GetGlobalState();
5664 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005665 Py_XDECREF(global_name);
5666 Py_XDECREF(module_name);
5667 return -1;
5668 }
5669 global = find_class(self, module_name, global_name);
5670 Py_DECREF(global_name);
5671 Py_DECREF(module_name);
5672 if (global == NULL)
5673 return -1;
5674 PDATA_PUSH(self->stack, global, -1);
5675 return 0;
5676}
5677
5678static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005679load_persid(UnpicklerObject *self)
5680{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005681 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682 Py_ssize_t len;
5683 char *s;
5684
5685 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005686 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005688 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005689 return bad_readline();
5690
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005691 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5692 if (pid == NULL) {
5693 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5694 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5695 "persistent IDs in protocol 0 must be "
5696 "ASCII strings");
5697 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005698 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005699 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005700
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005701 obj = call_method(self->pers_func, self->pers_func_self, pid);
5702 Py_DECREF(pid);
5703 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005704 return -1;
5705
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005706 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005707 return 0;
5708 }
5709 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005710 PickleState *st = _Pickle_GetGlobalState();
5711 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005712 "A load persistent id instruction was encountered,\n"
5713 "but no persistent_load function was specified.");
5714 return -1;
5715 }
5716}
5717
5718static int
5719load_binpersid(UnpicklerObject *self)
5720{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005721 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005722
5723 if (self->pers_func) {
5724 PDATA_POP(self->stack, pid);
5725 if (pid == NULL)
5726 return -1;
5727
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005728 obj = call_method(self->pers_func, self->pers_func_self, pid);
5729 Py_DECREF(pid);
5730 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005731 return -1;
5732
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005733 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005734 return 0;
5735 }
5736 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005737 PickleState *st = _Pickle_GetGlobalState();
5738 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005739 "A load persistent id instruction was encountered,\n"
5740 "but no persistent_load function was specified.");
5741 return -1;
5742 }
5743}
5744
5745static int
5746load_pop(UnpicklerObject *self)
5747{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005748 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005749
5750 /* Note that we split the (pickle.py) stack into two stacks,
5751 * an object stack and a mark stack. We have to be clever and
5752 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005753 * mark stack first, and only signalling a stack underflow if
5754 * the object stack is empty and the mark stack doesn't match
5755 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005756 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005757 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005758 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005759 self->stack->mark_set = self->num_marks != 0;
5760 self->stack->fence = self->num_marks ?
5761 self->marks[self->num_marks - 1] : 0;
5762 } else if (len <= self->stack->fence)
5763 return Pdata_stack_underflow(self->stack);
5764 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 len--;
5766 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005767 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005768 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005769 return 0;
5770}
5771
5772static int
5773load_pop_mark(UnpicklerObject *self)
5774{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005775 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776
5777 if ((i = marker(self)) < 0)
5778 return -1;
5779
5780 Pdata_clear(self->stack, i);
5781
5782 return 0;
5783}
5784
5785static int
5786load_dup(UnpicklerObject *self)
5787{
5788 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005789 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005790
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005791 if (len <= self->stack->fence)
5792 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793 last = self->stack->data[len - 1];
5794 PDATA_APPEND(self->stack, last, -1);
5795 return 0;
5796}
5797
5798static int
5799load_get(UnpicklerObject *self)
5800{
5801 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005802 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803 Py_ssize_t len;
5804 char *s;
5805
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005806 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005807 return -1;
5808 if (len < 2)
5809 return bad_readline();
5810
5811 key = PyLong_FromString(s, NULL, 10);
5812 if (key == NULL)
5813 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005814 idx = PyLong_AsSsize_t(key);
5815 if (idx == -1 && PyErr_Occurred()) {
5816 Py_DECREF(key);
5817 return -1;
5818 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005819
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005820 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005821 if (value == NULL) {
5822 if (!PyErr_Occurred())
5823 PyErr_SetObject(PyExc_KeyError, key);
5824 Py_DECREF(key);
5825 return -1;
5826 }
5827 Py_DECREF(key);
5828
5829 PDATA_APPEND(self->stack, value, -1);
5830 return 0;
5831}
5832
5833static int
5834load_binget(UnpicklerObject *self)
5835{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005836 PyObject *value;
5837 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005838 char *s;
5839
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005840 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005841 return -1;
5842
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005843 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005844
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005845 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005847 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005848 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005849 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005850 Py_DECREF(key);
5851 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005852 return -1;
5853 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005854
5855 PDATA_APPEND(self->stack, value, -1);
5856 return 0;
5857}
5858
5859static int
5860load_long_binget(UnpicklerObject *self)
5861{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005862 PyObject *value;
5863 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005864 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005865
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005866 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005867 return -1;
5868
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005869 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005870
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005871 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005872 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005873 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005874 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005875 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005876 Py_DECREF(key);
5877 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005878 return -1;
5879 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005880
5881 PDATA_APPEND(self->stack, value, -1);
5882 return 0;
5883}
5884
5885/* Push an object from the extension registry (EXT[124]). nbytes is
5886 * the number of bytes following the opcode, holding the index (code) value.
5887 */
5888static int
5889load_extension(UnpicklerObject *self, int nbytes)
5890{
5891 char *codebytes; /* the nbytes bytes after the opcode */
5892 long code; /* calc_binint returns long */
5893 PyObject *py_code; /* code as a Python int */
5894 PyObject *obj; /* the object to push */
5895 PyObject *pair; /* (module_name, class_name) */
5896 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005897 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005898
5899 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005900 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005901 return -1;
5902 code = calc_binint(codebytes, nbytes);
5903 if (code <= 0) { /* note that 0 is forbidden */
5904 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005905 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005906 return -1;
5907 }
5908
5909 /* Look for the code in the cache. */
5910 py_code = PyLong_FromLong(code);
5911 if (py_code == NULL)
5912 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005913 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005914 if (obj != NULL) {
5915 /* Bingo. */
5916 Py_DECREF(py_code);
5917 PDATA_APPEND(self->stack, obj, -1);
5918 return 0;
5919 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005920 if (PyErr_Occurred()) {
5921 Py_DECREF(py_code);
5922 return -1;
5923 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005924
5925 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005926 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005927 if (pair == NULL) {
5928 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005929 if (!PyErr_Occurred()) {
5930 PyErr_Format(PyExc_ValueError, "unregistered extension "
5931 "code %ld", code);
5932 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005933 return -1;
5934 }
5935 /* Since the extension registry is manipulable via Python code,
5936 * confirm that pair is really a 2-tuple of strings.
5937 */
Victor Stinnerb37672d2018-11-22 03:37:50 +01005938 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
5939 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005940 }
Victor Stinnerb37672d2018-11-22 03:37:50 +01005941
5942 module_name = PyTuple_GET_ITEM(pair, 0);
5943 if (!PyUnicode_Check(module_name)) {
5944 goto error;
5945 }
5946
5947 class_name = PyTuple_GET_ITEM(pair, 1);
5948 if (!PyUnicode_Check(class_name)) {
5949 goto error;
5950 }
5951
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005952 /* Load the object. */
5953 obj = find_class(self, module_name, class_name);
5954 if (obj == NULL) {
5955 Py_DECREF(py_code);
5956 return -1;
5957 }
5958 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005959 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005960 Py_DECREF(py_code);
5961 if (code < 0) {
5962 Py_DECREF(obj);
5963 return -1;
5964 }
5965 PDATA_PUSH(self->stack, obj, -1);
5966 return 0;
Victor Stinnerb37672d2018-11-22 03:37:50 +01005967
5968error:
5969 Py_DECREF(py_code);
5970 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5971 "isn't a 2-tuple of strings", code);
5972 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005973}
5974
5975static int
5976load_put(UnpicklerObject *self)
5977{
5978 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005979 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005980 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005981 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005982
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005983 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005984 return -1;
5985 if (len < 2)
5986 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005987 if (Py_SIZE(self->stack) <= self->stack->fence)
5988 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005989 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005990
5991 key = PyLong_FromString(s, NULL, 10);
5992 if (key == NULL)
5993 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005994 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005995 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005996 if (idx < 0) {
5997 if (!PyErr_Occurred())
5998 PyErr_SetString(PyExc_ValueError,
5999 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006000 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02006001 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006002
6003 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006004}
6005
6006static int
6007load_binput(UnpicklerObject *self)
6008{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006009 PyObject *value;
6010 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006011 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006012
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006013 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006014 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006015
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006016 if (Py_SIZE(self->stack) <= self->stack->fence)
6017 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006018 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006019
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006020 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006021
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006022 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006023}
6024
6025static int
6026load_long_binput(UnpicklerObject *self)
6027{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006028 PyObject *value;
6029 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006030 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006031
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006032 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006033 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006034
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006035 if (Py_SIZE(self->stack) <= self->stack->fence)
6036 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006037 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006038
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006039 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02006040 if (idx < 0) {
6041 PyErr_SetString(PyExc_ValueError,
6042 "negative LONG_BINPUT argument");
6043 return -1;
6044 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006045
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006046 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006047}
6048
6049static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006050load_memoize(UnpicklerObject *self)
6051{
6052 PyObject *value;
6053
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006054 if (Py_SIZE(self->stack) <= self->stack->fence)
6055 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006056 value = self->stack->data[Py_SIZE(self->stack) - 1];
6057
6058 return _Unpickler_MemoPut(self, self->memo_len, value);
6059}
6060
6061static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006062do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006063{
6064 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006065 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006066 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006067 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006068 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006069
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006070 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006071 if (x > len || x <= self->stack->fence)
6072 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006073 if (len == x) /* nothing to do */
6074 return 0;
6075
6076 list = self->stack->data[x - 1];
6077
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006078 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006080 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006081
6082 slice = Pdata_poplist(self->stack, x);
6083 if (!slice)
6084 return -1;
6085 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006086 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006087 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006088 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006089 }
6090 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006091 PyObject *extend_func;
6092 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006093
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006094 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6095 if (extend_func != NULL) {
6096 slice = Pdata_poplist(self->stack, x);
6097 if (!slice) {
6098 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006099 return -1;
6100 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006101 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006102 Py_DECREF(extend_func);
6103 if (result == NULL)
6104 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006105 Py_DECREF(result);
6106 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006107 else {
6108 PyObject *append_func;
6109 _Py_IDENTIFIER(append);
6110
6111 /* Even if the PEP 307 requires extend() and append() methods,
6112 fall back on append() if the object has no extend() method
6113 for backward compatibility. */
6114 PyErr_Clear();
6115 append_func = _PyObject_GetAttrId(list, &PyId_append);
6116 if (append_func == NULL)
6117 return -1;
6118 for (i = x; i < len; i++) {
6119 value = self->stack->data[i];
6120 result = _Pickle_FastCall(append_func, value);
6121 if (result == NULL) {
6122 Pdata_clear(self->stack, i + 1);
6123 Py_SIZE(self->stack) = x;
6124 Py_DECREF(append_func);
6125 return -1;
6126 }
6127 Py_DECREF(result);
6128 }
6129 Py_SIZE(self->stack) = x;
6130 Py_DECREF(append_func);
6131 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006132 }
6133
6134 return 0;
6135}
6136
6137static int
6138load_append(UnpicklerObject *self)
6139{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006140 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6141 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006142 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006143}
6144
6145static int
6146load_appends(UnpicklerObject *self)
6147{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006148 Py_ssize_t i = marker(self);
6149 if (i < 0)
6150 return -1;
6151 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006152}
6153
6154static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006155do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006156{
6157 PyObject *value, *key;
6158 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006159 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006160 int status = 0;
6161
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006162 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006163 if (x > len || x <= self->stack->fence)
6164 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006165 if (len == x) /* nothing to do */
6166 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006167 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006168 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006169 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006170 PyErr_SetString(st->UnpicklingError,
6171 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006172 return -1;
6173 }
6174
6175 /* Here, dict does not actually need to be a PyDict; it could be anything
6176 that supports the __setitem__ attribute. */
6177 dict = self->stack->data[x - 1];
6178
6179 for (i = x + 1; i < len; i += 2) {
6180 key = self->stack->data[i - 1];
6181 value = self->stack->data[i];
6182 if (PyObject_SetItem(dict, key, value) < 0) {
6183 status = -1;
6184 break;
6185 }
6186 }
6187
6188 Pdata_clear(self->stack, x);
6189 return status;
6190}
6191
6192static int
6193load_setitem(UnpicklerObject *self)
6194{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006195 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006196}
6197
6198static int
6199load_setitems(UnpicklerObject *self)
6200{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006201 Py_ssize_t i = marker(self);
6202 if (i < 0)
6203 return -1;
6204 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006205}
6206
6207static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006208load_additems(UnpicklerObject *self)
6209{
6210 PyObject *set;
6211 Py_ssize_t mark, len, i;
6212
6213 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006214 if (mark < 0)
6215 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006216 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006217 if (mark > len || mark <= self->stack->fence)
6218 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006219 if (len == mark) /* nothing to do */
6220 return 0;
6221
6222 set = self->stack->data[mark - 1];
6223
6224 if (PySet_Check(set)) {
6225 PyObject *items;
6226 int status;
6227
6228 items = Pdata_poptuple(self->stack, mark);
6229 if (items == NULL)
6230 return -1;
6231
6232 status = _PySet_Update(set, items);
6233 Py_DECREF(items);
6234 return status;
6235 }
6236 else {
6237 PyObject *add_func;
6238 _Py_IDENTIFIER(add);
6239
6240 add_func = _PyObject_GetAttrId(set, &PyId_add);
6241 if (add_func == NULL)
6242 return -1;
6243 for (i = mark; i < len; i++) {
6244 PyObject *result;
6245 PyObject *item;
6246
6247 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006248 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006249 if (result == NULL) {
6250 Pdata_clear(self->stack, i + 1);
6251 Py_SIZE(self->stack) = mark;
6252 return -1;
6253 }
6254 Py_DECREF(result);
6255 }
6256 Py_SIZE(self->stack) = mark;
6257 }
6258
6259 return 0;
6260}
6261
6262static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006263load_build(UnpicklerObject *self)
6264{
6265 PyObject *state, *inst, *slotstate;
6266 PyObject *setstate;
6267 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006268 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006269
6270 /* Stack is ... instance, state. We want to leave instance at
6271 * the stack top, possibly mutated via instance.__setstate__(state).
6272 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006273 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6274 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006275
6276 PDATA_POP(self->stack, state);
6277 if (state == NULL)
6278 return -1;
6279
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006280 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006281
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006282 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6283 Py_DECREF(state);
6284 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006285 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006286 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006287 PyObject *result;
6288
6289 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006290 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006291 Py_DECREF(setstate);
6292 if (result == NULL)
6293 return -1;
6294 Py_DECREF(result);
6295 return 0;
6296 }
6297
6298 /* A default __setstate__. First see whether state embeds a
6299 * slot state dict too (a proto 2 addition).
6300 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006301 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006302 PyObject *tmp = state;
6303
6304 state = PyTuple_GET_ITEM(tmp, 0);
6305 slotstate = PyTuple_GET_ITEM(tmp, 1);
6306 Py_INCREF(state);
6307 Py_INCREF(slotstate);
6308 Py_DECREF(tmp);
6309 }
6310 else
6311 slotstate = NULL;
6312
6313 /* Set inst.__dict__ from the state dict (if any). */
6314 if (state != Py_None) {
6315 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006316 PyObject *d_key, *d_value;
6317 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006318 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006319
6320 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006321 PickleState *st = _Pickle_GetGlobalState();
6322 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006323 goto error;
6324 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006325 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006326 if (dict == NULL)
6327 goto error;
6328
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006329 i = 0;
6330 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6331 /* normally the keys for instance attributes are
6332 interned. we should try to do that here. */
6333 Py_INCREF(d_key);
6334 if (PyUnicode_CheckExact(d_key))
6335 PyUnicode_InternInPlace(&d_key);
6336 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6337 Py_DECREF(d_key);
6338 goto error;
6339 }
6340 Py_DECREF(d_key);
6341 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006342 Py_DECREF(dict);
6343 }
6344
6345 /* Also set instance attributes from the slotstate dict (if any). */
6346 if (slotstate != NULL) {
6347 PyObject *d_key, *d_value;
6348 Py_ssize_t i;
6349
6350 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006351 PickleState *st = _Pickle_GetGlobalState();
6352 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006353 "slot state is not a dictionary");
6354 goto error;
6355 }
6356 i = 0;
6357 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6358 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6359 goto error;
6360 }
6361 }
6362
6363 if (0) {
6364 error:
6365 status = -1;
6366 }
6367
6368 Py_DECREF(state);
6369 Py_XDECREF(slotstate);
6370 return status;
6371}
6372
6373static int
6374load_mark(UnpicklerObject *self)
6375{
6376
6377 /* Note that we split the (pickle.py) stack into two stacks, an
6378 * object stack and a mark stack. Here we push a mark onto the
6379 * mark stack.
6380 */
6381
Sergey Fedoseev86b89912018-08-25 12:54:40 +05006382 if (self->num_marks >= self->marks_size) {
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006383 size_t alloc = ((size_t)self->num_marks << 1) + 20;
6384 Py_ssize_t *marks_new = self->marks;
6385 PyMem_RESIZE(marks_new, Py_ssize_t, alloc);
6386 if (marks_new == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006387 PyErr_NoMemory();
6388 return -1;
6389 }
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006390 self->marks = marks_new;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006391 self->marks_size = (Py_ssize_t)alloc;
6392 }
6393
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006394 self->stack->mark_set = 1;
6395 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396
6397 return 0;
6398}
6399
6400static int
6401load_reduce(UnpicklerObject *self)
6402{
6403 PyObject *callable = NULL;
6404 PyObject *argtup = NULL;
6405 PyObject *obj = NULL;
6406
6407 PDATA_POP(self->stack, argtup);
6408 if (argtup == NULL)
6409 return -1;
6410 PDATA_POP(self->stack, callable);
6411 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006412 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006413 Py_DECREF(callable);
6414 }
6415 Py_DECREF(argtup);
6416
6417 if (obj == NULL)
6418 return -1;
6419
6420 PDATA_PUSH(self->stack, obj, -1);
6421 return 0;
6422}
6423
6424/* Just raises an error if we don't know the protocol specified. PROTO
6425 * is the first opcode for protocols >= 2.
6426 */
6427static int
6428load_proto(UnpicklerObject *self)
6429{
6430 char *s;
6431 int i;
6432
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006433 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006434 return -1;
6435
6436 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006437 if (i <= HIGHEST_PROTOCOL) {
6438 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006439 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006440 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006441
6442 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6443 return -1;
6444}
6445
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006446static int
6447load_frame(UnpicklerObject *self)
6448{
6449 char *s;
6450 Py_ssize_t frame_len;
6451
6452 if (_Unpickler_Read(self, &s, 8) < 0)
6453 return -1;
6454
6455 frame_len = calc_binsize(s, 8);
6456 if (frame_len < 0) {
6457 PyErr_Format(PyExc_OverflowError,
6458 "FRAME length exceeds system's maximum of %zd bytes",
6459 PY_SSIZE_T_MAX);
6460 return -1;
6461 }
6462
6463 if (_Unpickler_Read(self, &s, frame_len) < 0)
6464 return -1;
6465
6466 /* Rewind to start of frame */
6467 self->next_read_idx -= frame_len;
6468 return 0;
6469}
6470
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006471static PyObject *
6472load(UnpicklerObject *self)
6473{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006474 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006475 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006476
6477 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006478 self->stack->mark_set = 0;
6479 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006480 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006481 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482 Pdata_clear(self->stack, 0);
6483
6484 /* Convenient macros for the dispatch while-switch loop just below. */
6485#define OP(opcode, load_func) \
6486 case opcode: if (load_func(self) < 0) break; continue;
6487
6488#define OP_ARG(opcode, load_func, arg) \
6489 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6490
6491 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006492 if (_Unpickler_Read(self, &s, 1) < 0) {
6493 PickleState *st = _Pickle_GetGlobalState();
6494 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6495 PyErr_Format(PyExc_EOFError, "Ran out of input");
6496 }
6497 return NULL;
6498 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006499
6500 switch ((enum opcode)s[0]) {
6501 OP(NONE, load_none)
6502 OP(BININT, load_binint)
6503 OP(BININT1, load_binint1)
6504 OP(BININT2, load_binint2)
6505 OP(INT, load_int)
6506 OP(LONG, load_long)
6507 OP_ARG(LONG1, load_counted_long, 1)
6508 OP_ARG(LONG4, load_counted_long, 4)
6509 OP(FLOAT, load_float)
6510 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006511 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6512 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6513 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6514 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6515 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006516 OP(STRING, load_string)
6517 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006518 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6519 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6520 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006521 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6522 OP_ARG(TUPLE1, load_counted_tuple, 1)
6523 OP_ARG(TUPLE2, load_counted_tuple, 2)
6524 OP_ARG(TUPLE3, load_counted_tuple, 3)
6525 OP(TUPLE, load_tuple)
6526 OP(EMPTY_LIST, load_empty_list)
6527 OP(LIST, load_list)
6528 OP(EMPTY_DICT, load_empty_dict)
6529 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006530 OP(EMPTY_SET, load_empty_set)
6531 OP(ADDITEMS, load_additems)
6532 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006533 OP(OBJ, load_obj)
6534 OP(INST, load_inst)
6535 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006536 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006537 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006538 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539 OP(APPEND, load_append)
6540 OP(APPENDS, load_appends)
6541 OP(BUILD, load_build)
6542 OP(DUP, load_dup)
6543 OP(BINGET, load_binget)
6544 OP(LONG_BINGET, load_long_binget)
6545 OP(GET, load_get)
6546 OP(MARK, load_mark)
6547 OP(BINPUT, load_binput)
6548 OP(LONG_BINPUT, load_long_binput)
6549 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006550 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006551 OP(POP, load_pop)
6552 OP(POP_MARK, load_pop_mark)
6553 OP(SETITEM, load_setitem)
6554 OP(SETITEMS, load_setitems)
6555 OP(PERSID, load_persid)
6556 OP(BINPERSID, load_binpersid)
6557 OP(REDUCE, load_reduce)
6558 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006559 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006560 OP_ARG(EXT1, load_extension, 1)
6561 OP_ARG(EXT2, load_extension, 2)
6562 OP_ARG(EXT4, load_extension, 4)
6563 OP_ARG(NEWTRUE, load_bool, Py_True)
6564 OP_ARG(NEWFALSE, load_bool, Py_False)
6565
6566 case STOP:
6567 break;
6568
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006569 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006570 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006571 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006572 unsigned char c = (unsigned char) *s;
6573 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6574 PyErr_Format(st->UnpicklingError,
6575 "invalid load key, '%c'.", c);
6576 }
6577 else {
6578 PyErr_Format(st->UnpicklingError,
6579 "invalid load key, '\\x%02x'.", c);
6580 }
6581 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006582 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006583 }
6584
6585 break; /* and we are done! */
6586 }
6587
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006588 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006589 return NULL;
6590 }
6591
Victor Stinner2ae57e32013-10-31 13:39:23 +01006592 if (_Unpickler_SkipConsumed(self) < 0)
6593 return NULL;
6594
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006595 PDATA_POP(self->stack, value);
6596 return value;
6597}
6598
Larry Hastings61272b72014-01-07 12:41:53 -08006599/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006600
6601_pickle.Unpickler.load
6602
6603Load a pickle.
6604
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006605Read a pickled object representation from the open file object given
6606in the constructor, and return the reconstituted object hierarchy
6607specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006608[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006609
Larry Hastings3cceb382014-01-04 11:09:09 -08006610static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006611_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006612/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006613{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006614 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006615
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006616 /* Check whether the Unpickler was initialized correctly. This prevents
6617 segfaulting if a subclass overridden __init__ with a function that does
6618 not call Unpickler.__init__(). Here, we simply ensure that self->read
6619 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006620 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006621 PickleState *st = _Pickle_GetGlobalState();
6622 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006623 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006624 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006625 return NULL;
6626 }
6627
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006628 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006629}
6630
6631/* The name of find_class() is misleading. In newer pickle protocols, this
6632 function is used for loading any global (i.e., functions), not just
6633 classes. The name is kept only for backward compatibility. */
6634
Larry Hastings61272b72014-01-07 12:41:53 -08006635/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006636
6637_pickle.Unpickler.find_class
6638
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006639 module_name: object
6640 global_name: object
6641 /
6642
6643Return an object from a specified module.
6644
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006645If necessary, the module will be imported. Subclasses may override
6646this method (e.g. to restrict unpickling of arbitrary classes and
6647functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006648
6649This method is called whenever a class or a function object is
6650needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006651[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006652
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006653static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006654_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6655 PyObject *module_name,
6656 PyObject *global_name)
6657/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006658{
6659 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006660 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006661
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006662 /* Try to map the old names used in Python 2.x to the new ones used in
6663 Python 3.x. We do this only with old pickle protocols and when the
6664 user has not disabled the feature. */
6665 if (self->proto < 3 && self->fix_imports) {
6666 PyObject *key;
6667 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006668 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006669
6670 /* Check if the global (i.e., a function or a class) was renamed
6671 or moved to another module. */
6672 key = PyTuple_Pack(2, module_name, global_name);
6673 if (key == NULL)
6674 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006675 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006676 Py_DECREF(key);
6677 if (item) {
6678 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6679 PyErr_Format(PyExc_RuntimeError,
6680 "_compat_pickle.NAME_MAPPING values should be "
6681 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6682 return NULL;
6683 }
6684 module_name = PyTuple_GET_ITEM(item, 0);
6685 global_name = PyTuple_GET_ITEM(item, 1);
6686 if (!PyUnicode_Check(module_name) ||
6687 !PyUnicode_Check(global_name)) {
6688 PyErr_Format(PyExc_RuntimeError,
6689 "_compat_pickle.NAME_MAPPING values should be "
6690 "pairs of str, not (%.200s, %.200s)",
6691 Py_TYPE(module_name)->tp_name,
6692 Py_TYPE(global_name)->tp_name);
6693 return NULL;
6694 }
6695 }
6696 else if (PyErr_Occurred()) {
6697 return NULL;
6698 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006699 else {
6700 /* Check if the module was renamed. */
6701 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6702 if (item) {
6703 if (!PyUnicode_Check(item)) {
6704 PyErr_Format(PyExc_RuntimeError,
6705 "_compat_pickle.IMPORT_MAPPING values should be "
6706 "strings, not %.200s", Py_TYPE(item)->tp_name);
6707 return NULL;
6708 }
6709 module_name = item;
6710 }
6711 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006712 return NULL;
6713 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006714 }
6715 }
6716
tjb9004371c0a2019-02-18 23:30:51 +08006717 /*
6718 * we don't use PyImport_GetModule here, because it can return partially-
6719 * initialised modules, which then cause the getattribute to fail.
6720 */
6721 module = PyImport_Import(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006722 if (module == NULL) {
tjb9004371c0a2019-02-18 23:30:51 +08006723 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006724 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006725 global = getattribute(module, global_name, self->proto >= 4);
6726 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006727 return global;
6728}
6729
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006730/*[clinic input]
6731
6732_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6733
6734Returns size in memory, in bytes.
6735[clinic start generated code]*/
6736
6737static Py_ssize_t
6738_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6739/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6740{
6741 Py_ssize_t res;
6742
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006743 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006744 if (self->memo != NULL)
6745 res += self->memo_size * sizeof(PyObject *);
6746 if (self->marks != NULL)
6747 res += self->marks_size * sizeof(Py_ssize_t);
6748 if (self->input_line != NULL)
6749 res += strlen(self->input_line) + 1;
6750 if (self->encoding != NULL)
6751 res += strlen(self->encoding) + 1;
6752 if (self->errors != NULL)
6753 res += strlen(self->errors) + 1;
6754 return res;
6755}
6756
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006757static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006758 _PICKLE_UNPICKLER_LOAD_METHODDEF
6759 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006760 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006761 {NULL, NULL} /* sentinel */
6762};
6763
6764static void
6765Unpickler_dealloc(UnpicklerObject *self)
6766{
6767 PyObject_GC_UnTrack((PyObject *)self);
6768 Py_XDECREF(self->readline);
6769 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006770 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006771 Py_XDECREF(self->stack);
6772 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006773 if (self->buffer.buf != NULL) {
6774 PyBuffer_Release(&self->buffer);
6775 self->buffer.buf = NULL;
6776 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006777
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006778 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006779 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006780 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006781 PyMem_Free(self->encoding);
6782 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006783
6784 Py_TYPE(self)->tp_free((PyObject *)self);
6785}
6786
6787static int
6788Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6789{
6790 Py_VISIT(self->readline);
6791 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006792 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006793 Py_VISIT(self->stack);
6794 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006795 return 0;
6796}
6797
6798static int
6799Unpickler_clear(UnpicklerObject *self)
6800{
6801 Py_CLEAR(self->readline);
6802 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006803 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006804 Py_CLEAR(self->stack);
6805 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006806 if (self->buffer.buf != NULL) {
6807 PyBuffer_Release(&self->buffer);
6808 self->buffer.buf = NULL;
6809 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006810
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006811 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006812 PyMem_Free(self->marks);
6813 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006814 PyMem_Free(self->input_line);
6815 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006816 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006817 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006818 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006819 self->errors = NULL;
6820
6821 return 0;
6822}
6823
Larry Hastings61272b72014-01-07 12:41:53 -08006824/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006825
6826_pickle.Unpickler.__init__
6827
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006828 file: object
6829 *
6830 fix_imports: bool = True
6831 encoding: str = 'ASCII'
6832 errors: str = 'strict'
6833
6834This takes a binary file for reading a pickle data stream.
6835
6836The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006837protocol argument is needed. Bytes past the pickled object's
6838representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006839
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006840The argument *file* must have two methods, a read() method that takes
6841an integer argument, and a readline() method that requires no
6842arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006843binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006844other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006845
6846Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006847which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006848generated by Python 2. If *fix_imports* is True, pickle will try to
6849map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006850*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006851instances pickled by Python 2; these default to 'ASCII' and 'strict',
6852respectively. The *encoding* can be 'bytes' to read these 8-bit
6853string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006854[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006855
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006856static int
Larry Hastings89964c42015-04-14 18:07:59 -04006857_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6858 int fix_imports, const char *encoding,
6859 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006860/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006861{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006862 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006863
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006864 /* In case of multiple __init__() calls, clear previous content. */
6865 if (self->read != NULL)
6866 (void)Unpickler_clear(self);
6867
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006868 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006869 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006870
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006871 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006872 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006873
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006874 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006875
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006876 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6877 &self->pers_func, &self->pers_func_self) < 0)
6878 {
6879 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006880 }
6881
6882 self->stack = (Pdata *)Pdata_New();
6883 if (self->stack == NULL)
Zackery Spytz4b430e52018-09-28 23:48:46 -06006884 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006885
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006886 self->memo_size = 32;
6887 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006888 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006889 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006890
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006891 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006892
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006893 return 0;
6894}
6895
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006896
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006897/* Define a proxy object for the Unpickler's internal memo object. This is to
6898 * avoid breaking code like:
6899 * unpickler.memo.clear()
6900 * and
6901 * unpickler.memo = saved_memo
6902 * Is this a good idea? Not really, but we don't want to break code that uses
6903 * it. Note that we don't implement the entire mapping API here. This is
6904 * intentional, as these should be treated as black-box implementation details.
6905 *
6906 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006907 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006908 */
6909
Larry Hastings61272b72014-01-07 12:41:53 -08006910/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006911_pickle.UnpicklerMemoProxy.clear
6912
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006913Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006914[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006915
Larry Hastings3cceb382014-01-04 11:09:09 -08006916static PyObject *
6917_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006918/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006919{
6920 _Unpickler_MemoCleanup(self->unpickler);
6921 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6922 if (self->unpickler->memo == NULL)
6923 return NULL;
6924 Py_RETURN_NONE;
6925}
6926
Larry Hastings61272b72014-01-07 12:41:53 -08006927/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006928_pickle.UnpicklerMemoProxy.copy
6929
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006930Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006931[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006932
Larry Hastings3cceb382014-01-04 11:09:09 -08006933static PyObject *
6934_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006935/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006936{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07006937 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006938 PyObject *new_memo = PyDict_New();
6939 if (new_memo == NULL)
6940 return NULL;
6941
6942 for (i = 0; i < self->unpickler->memo_size; i++) {
6943 int status;
6944 PyObject *key, *value;
6945
6946 value = self->unpickler->memo[i];
6947 if (value == NULL)
6948 continue;
6949
6950 key = PyLong_FromSsize_t(i);
6951 if (key == NULL)
6952 goto error;
6953 status = PyDict_SetItem(new_memo, key, value);
6954 Py_DECREF(key);
6955 if (status < 0)
6956 goto error;
6957 }
6958 return new_memo;
6959
6960error:
6961 Py_DECREF(new_memo);
6962 return NULL;
6963}
6964
Larry Hastings61272b72014-01-07 12:41:53 -08006965/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006966_pickle.UnpicklerMemoProxy.__reduce__
6967
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006968Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006969[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006970
Larry Hastings3cceb382014-01-04 11:09:09 -08006971static PyObject *
6972_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006973/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006974{
6975 PyObject *reduce_value;
6976 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006977 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006978 if (contents == NULL)
6979 return NULL;
6980
6981 reduce_value = PyTuple_New(2);
6982 if (reduce_value == NULL) {
6983 Py_DECREF(contents);
6984 return NULL;
6985 }
6986 constructor_args = PyTuple_New(1);
6987 if (constructor_args == NULL) {
6988 Py_DECREF(contents);
6989 Py_DECREF(reduce_value);
6990 return NULL;
6991 }
6992 PyTuple_SET_ITEM(constructor_args, 0, contents);
6993 Py_INCREF((PyObject *)&PyDict_Type);
6994 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6995 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6996 return reduce_value;
6997}
6998
6999static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007000 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
7001 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
7002 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007003 {NULL, NULL} /* sentinel */
7004};
7005
7006static void
7007UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
7008{
7009 PyObject_GC_UnTrack(self);
7010 Py_XDECREF(self->unpickler);
7011 PyObject_GC_Del((PyObject *)self);
7012}
7013
7014static int
7015UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
7016 visitproc visit, void *arg)
7017{
7018 Py_VISIT(self->unpickler);
7019 return 0;
7020}
7021
7022static int
7023UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
7024{
7025 Py_CLEAR(self->unpickler);
7026 return 0;
7027}
7028
7029static PyTypeObject UnpicklerMemoProxyType = {
7030 PyVarObject_HEAD_INIT(NULL, 0)
7031 "_pickle.UnpicklerMemoProxy", /*tp_name*/
7032 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
7033 0,
7034 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
7035 0, /* tp_print */
7036 0, /* tp_getattr */
7037 0, /* tp_setattr */
7038 0, /* tp_compare */
7039 0, /* tp_repr */
7040 0, /* tp_as_number */
7041 0, /* tp_as_sequence */
7042 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00007043 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007044 0, /* tp_call */
7045 0, /* tp_str */
7046 PyObject_GenericGetAttr, /* tp_getattro */
7047 PyObject_GenericSetAttr, /* tp_setattro */
7048 0, /* tp_as_buffer */
7049 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
7050 0, /* tp_doc */
7051 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
7052 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
7053 0, /* tp_richcompare */
7054 0, /* tp_weaklistoffset */
7055 0, /* tp_iter */
7056 0, /* tp_iternext */
7057 unpicklerproxy_methods, /* tp_methods */
7058};
7059
7060static PyObject *
7061UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
7062{
7063 UnpicklerMemoProxyObject *self;
7064
7065 self = PyObject_GC_New(UnpicklerMemoProxyObject,
7066 &UnpicklerMemoProxyType);
7067 if (self == NULL)
7068 return NULL;
7069 Py_INCREF(unpickler);
7070 self->unpickler = unpickler;
7071 PyObject_GC_Track(self);
7072 return (PyObject *)self;
7073}
7074
7075/*****************************************************************************/
7076
7077
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007078static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007079Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007080{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007081 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007082}
7083
7084static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007085Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007086{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007087 PyObject **new_memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007088 size_t new_memo_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007089
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007090 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007091 PyErr_SetString(PyExc_TypeError,
7092 "attribute deletion is not supported");
7093 return -1;
7094 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007095
7096 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7097 UnpicklerObject *unpickler =
7098 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7099
7100 new_memo_size = unpickler->memo_size;
7101 new_memo = _Unpickler_NewMemo(new_memo_size);
7102 if (new_memo == NULL)
7103 return -1;
7104
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007105 for (size_t i = 0; i < new_memo_size; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007106 Py_XINCREF(unpickler->memo[i]);
7107 new_memo[i] = unpickler->memo[i];
7108 }
7109 }
7110 else if (PyDict_Check(obj)) {
7111 Py_ssize_t i = 0;
7112 PyObject *key, *value;
7113
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007114 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007115 new_memo = _Unpickler_NewMemo(new_memo_size);
7116 if (new_memo == NULL)
7117 return -1;
7118
7119 while (PyDict_Next(obj, &i, &key, &value)) {
7120 Py_ssize_t idx;
7121 if (!PyLong_Check(key)) {
7122 PyErr_SetString(PyExc_TypeError,
7123 "memo key must be integers");
7124 goto error;
7125 }
7126 idx = PyLong_AsSsize_t(key);
7127 if (idx == -1 && PyErr_Occurred())
7128 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007129 if (idx < 0) {
7130 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007131 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007132 goto error;
7133 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007134 if (_Unpickler_MemoPut(self, idx, value) < 0)
7135 goto error;
7136 }
7137 }
7138 else {
7139 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +02007140 "'memo' attribute must be an UnpicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007141 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007142 return -1;
7143 }
7144
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007145 _Unpickler_MemoCleanup(self);
7146 self->memo_size = new_memo_size;
7147 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007148
7149 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007150
7151 error:
7152 if (new_memo_size) {
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007153 for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007154 Py_XDECREF(new_memo[i]);
7155 }
7156 PyMem_FREE(new_memo);
7157 }
7158 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007159}
7160
7161static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007162Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007163{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007164 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007165 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007166 return NULL;
7167 }
7168 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007169}
7170
7171static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007172Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007173{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007174 if (value == NULL) {
7175 PyErr_SetString(PyExc_TypeError,
7176 "attribute deletion is not supported");
7177 return -1;
7178 }
7179 if (!PyCallable_Check(value)) {
7180 PyErr_SetString(PyExc_TypeError,
7181 "persistent_load must be a callable taking "
7182 "one argument");
7183 return -1;
7184 }
7185
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007186 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007187 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007188 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007189
7190 return 0;
7191}
7192
7193static PyGetSetDef Unpickler_getsets[] = {
7194 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7195 {"persistent_load", (getter)Unpickler_get_persload,
7196 (setter)Unpickler_set_persload},
7197 {NULL}
7198};
7199
7200static PyTypeObject Unpickler_Type = {
7201 PyVarObject_HEAD_INIT(NULL, 0)
7202 "_pickle.Unpickler", /*tp_name*/
7203 sizeof(UnpicklerObject), /*tp_basicsize*/
7204 0, /*tp_itemsize*/
7205 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7206 0, /*tp_print*/
7207 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007208 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007209 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007210 0, /*tp_repr*/
7211 0, /*tp_as_number*/
7212 0, /*tp_as_sequence*/
7213 0, /*tp_as_mapping*/
7214 0, /*tp_hash*/
7215 0, /*tp_call*/
7216 0, /*tp_str*/
7217 0, /*tp_getattro*/
7218 0, /*tp_setattro*/
7219 0, /*tp_as_buffer*/
7220 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007221 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007222 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7223 (inquiry)Unpickler_clear, /*tp_clear*/
7224 0, /*tp_richcompare*/
7225 0, /*tp_weaklistoffset*/
7226 0, /*tp_iter*/
7227 0, /*tp_iternext*/
7228 Unpickler_methods, /*tp_methods*/
7229 0, /*tp_members*/
7230 Unpickler_getsets, /*tp_getset*/
7231 0, /*tp_base*/
7232 0, /*tp_dict*/
7233 0, /*tp_descr_get*/
7234 0, /*tp_descr_set*/
7235 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007236 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007237 PyType_GenericAlloc, /*tp_alloc*/
7238 PyType_GenericNew, /*tp_new*/
7239 PyObject_GC_Del, /*tp_free*/
7240 0, /*tp_is_gc*/
7241};
7242
Larry Hastings61272b72014-01-07 12:41:53 -08007243/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007244
7245_pickle.dump
7246
7247 obj: object
7248 file: object
7249 protocol: object = NULL
7250 *
7251 fix_imports: bool = True
7252
7253Write a pickled representation of obj to the open file object file.
7254
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007255This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7256be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007257
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007258The optional *protocol* argument tells the pickler to use the given
Łukasz Langac51d8c92018-04-03 23:06:53 -07007259protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7260protocol is 4. It was introduced in Python 3.4, it is incompatible
7261with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007262
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007263Specifying a negative protocol version selects the highest protocol
7264version supported. The higher the protocol used, the more recent the
7265version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007266
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007267The *file* argument must have a write() method that accepts a single
7268bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007269writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007270this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007271
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007272If *fix_imports* is True and protocol is less than 3, pickle will try
7273to map the new Python 3 names to the old module names used in Python
72742, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007275[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007276
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007277static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007278_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007279 PyObject *protocol, int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007280/*[clinic end generated code: output=a4774d5fde7d34de input=93f1408489a87472]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007281{
7282 PicklerObject *pickler = _Pickler_New();
7283
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007284 if (pickler == NULL)
7285 return NULL;
7286
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007287 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007288 goto error;
7289
7290 if (_Pickler_SetOutputStream(pickler, file) < 0)
7291 goto error;
7292
7293 if (dump(pickler, obj) < 0)
7294 goto error;
7295
7296 if (_Pickler_FlushToFile(pickler) < 0)
7297 goto error;
7298
7299 Py_DECREF(pickler);
7300 Py_RETURN_NONE;
7301
7302 error:
7303 Py_XDECREF(pickler);
7304 return NULL;
7305}
7306
Larry Hastings61272b72014-01-07 12:41:53 -08007307/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007308
7309_pickle.dumps
7310
7311 obj: object
7312 protocol: object = NULL
7313 *
7314 fix_imports: bool = True
7315
7316Return the pickled representation of the object as a bytes object.
7317
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007318The optional *protocol* argument tells the pickler to use the given
7319protocol; supported protocols are 0, 1, 2, 3 and 4. The default
Łukasz Langac51d8c92018-04-03 23:06:53 -07007320protocol is 4. It was introduced in Python 3.4, it is incompatible
7321with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007322
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007323Specifying a negative protocol version selects the highest protocol
7324version supported. The higher the protocol used, the more recent the
7325version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007326
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007327If *fix_imports* is True and *protocol* is less than 3, pickle will
7328try to map the new Python 3 names to the old module names used in
7329Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007330[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007331
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007332static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007333_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007334 int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007335/*[clinic end generated code: output=d75d5cda456fd261 input=b6efb45a7d19b5ab]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007336{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007337 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007338 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007339
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007340 if (pickler == NULL)
7341 return NULL;
7342
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007343 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007344 goto error;
7345
7346 if (dump(pickler, obj) < 0)
7347 goto error;
7348
7349 result = _Pickler_GetString(pickler);
7350 Py_DECREF(pickler);
7351 return result;
7352
7353 error:
7354 Py_XDECREF(pickler);
7355 return NULL;
7356}
7357
Larry Hastings61272b72014-01-07 12:41:53 -08007358/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007359
7360_pickle.load
7361
7362 file: object
7363 *
7364 fix_imports: bool = True
7365 encoding: str = 'ASCII'
7366 errors: str = 'strict'
7367
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007368Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007369
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007370This is equivalent to ``Unpickler(file).load()``, but may be more
7371efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007372
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007373The protocol version of the pickle is detected automatically, so no
7374protocol argument is needed. Bytes past the pickled object's
7375representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007376
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007377The argument *file* must have two methods, a read() method that takes
7378an integer argument, and a readline() method that requires no
7379arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007380binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007381other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007382
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007383Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007384which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007385generated by Python 2. If *fix_imports* is True, pickle will try to
7386map the old Python 2 names to the new names used in Python 3. The
7387*encoding* and *errors* tell pickle how to decode 8-bit string
7388instances pickled by Python 2; these default to 'ASCII' and 'strict',
7389respectively. The *encoding* can be 'bytes' to read these 8-bit
7390string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007391[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007392
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007393static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007394_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007395 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007396/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007397{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007398 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007399 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007400
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007401 if (unpickler == NULL)
7402 return NULL;
7403
7404 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7405 goto error;
7406
7407 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7408 goto error;
7409
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007410 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007411
7412 result = load(unpickler);
7413 Py_DECREF(unpickler);
7414 return result;
7415
7416 error:
7417 Py_XDECREF(unpickler);
7418 return NULL;
7419}
7420
Larry Hastings61272b72014-01-07 12:41:53 -08007421/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007422
7423_pickle.loads
7424
7425 data: object
7426 *
7427 fix_imports: bool = True
7428 encoding: str = 'ASCII'
7429 errors: str = 'strict'
7430
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007431Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007432
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007433The protocol version of the pickle is detected automatically, so no
7434protocol argument is needed. Bytes past the pickled object's
7435representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007436
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007437Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007438which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007439generated by Python 2. If *fix_imports* is True, pickle will try to
7440map the old Python 2 names to the new names used in Python 3. The
7441*encoding* and *errors* tell pickle how to decode 8-bit string
7442instances pickled by Python 2; these default to 'ASCII' and 'strict',
7443respectively. The *encoding* can be 'bytes' to read these 8-bit
7444string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007445[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007446
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007447static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007448_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007449 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007450/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007451{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007452 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007453 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007454
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007455 if (unpickler == NULL)
7456 return NULL;
7457
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007458 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007459 goto error;
7460
7461 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7462 goto error;
7463
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007464 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007465
7466 result = load(unpickler);
7467 Py_DECREF(unpickler);
7468 return result;
7469
7470 error:
7471 Py_XDECREF(unpickler);
7472 return NULL;
7473}
7474
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007475static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007476 _PICKLE_DUMP_METHODDEF
7477 _PICKLE_DUMPS_METHODDEF
7478 _PICKLE_LOAD_METHODDEF
7479 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007480 {NULL, NULL} /* sentinel */
7481};
7482
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007483static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007484pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007485{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007486 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007487 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007488}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007489
Stefan Krahf483b0f2013-12-14 13:43:10 +01007490static void
7491pickle_free(PyObject *m)
7492{
7493 _Pickle_ClearState(_Pickle_GetState(m));
7494}
7495
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007496static int
7497pickle_traverse(PyObject *m, visitproc visit, void *arg)
7498{
7499 PickleState *st = _Pickle_GetState(m);
7500 Py_VISIT(st->PickleError);
7501 Py_VISIT(st->PicklingError);
7502 Py_VISIT(st->UnpicklingError);
7503 Py_VISIT(st->dispatch_table);
7504 Py_VISIT(st->extension_registry);
7505 Py_VISIT(st->extension_cache);
7506 Py_VISIT(st->inverted_registry);
7507 Py_VISIT(st->name_mapping_2to3);
7508 Py_VISIT(st->import_mapping_2to3);
7509 Py_VISIT(st->name_mapping_3to2);
7510 Py_VISIT(st->import_mapping_3to2);
7511 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007512 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007513 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007514}
7515
7516static struct PyModuleDef _picklemodule = {
7517 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007518 "_pickle", /* m_name */
7519 pickle_module_doc, /* m_doc */
7520 sizeof(PickleState), /* m_size */
7521 pickle_methods, /* m_methods */
7522 NULL, /* m_reload */
7523 pickle_traverse, /* m_traverse */
7524 pickle_clear, /* m_clear */
7525 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007526};
7527
7528PyMODINIT_FUNC
7529PyInit__pickle(void)
7530{
7531 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007532 PickleState *st;
7533
7534 m = PyState_FindModule(&_picklemodule);
7535 if (m) {
7536 Py_INCREF(m);
7537 return m;
7538 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007539
7540 if (PyType_Ready(&Unpickler_Type) < 0)
7541 return NULL;
7542 if (PyType_Ready(&Pickler_Type) < 0)
7543 return NULL;
7544 if (PyType_Ready(&Pdata_Type) < 0)
7545 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007546 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7547 return NULL;
7548 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7549 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007550
7551 /* Create the module and add the functions. */
7552 m = PyModule_Create(&_picklemodule);
7553 if (m == NULL)
7554 return NULL;
7555
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007556 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007557 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7558 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007559 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007560 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7561 return NULL;
7562
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007563 st = _Pickle_GetState(m);
7564
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007565 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007566 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7567 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007568 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007569 st->PicklingError = \
7570 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7571 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007572 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007573 st->UnpicklingError = \
7574 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7575 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007576 return NULL;
7577
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007578 Py_INCREF(st->PickleError);
7579 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007580 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007581 Py_INCREF(st->PicklingError);
7582 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007583 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007584 Py_INCREF(st->UnpicklingError);
7585 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007586 return NULL;
7587
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007588 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007589 return NULL;
7590
7591 return m;
7592}