blob: 60ef921521efbc53d3ab75dfc0d8d203629dedd6 [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001
2/* Core extension modules are built-in on some platforms (e.g. Windows). */
3#ifdef Py_BUILD_CORE
Eric Snowfc1bf872017-09-11 18:30:43 -07004#define Py_BUILD_CORE_BUILTIN
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#undef Py_BUILD_CORE
6#endif
7
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008#include "Python.h"
9#include "structmember.h"
10
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -080011PyDoc_STRVAR(pickle_module_doc,
12"Optimized C implementation for the Python pickle module.");
13
Larry Hastings61272b72014-01-07 12:41:53 -080014/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080016class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
17class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
18class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
19class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080020[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080022
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000023/* Bump this when new opcodes are added to the pickle protocol. */
24enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010025 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000026 DEFAULT_PROTOCOL = 3
27};
28
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029/* Pickle opcodes. These must be kept updated with pickle.py.
30 Extensive docs are in pickletools.py. */
31enum opcode {
32 MARK = '(',
33 STOP = '.',
34 POP = '0',
35 POP_MARK = '1',
36 DUP = '2',
37 FLOAT = 'F',
38 INT = 'I',
39 BININT = 'J',
40 BININT1 = 'K',
41 LONG = 'L',
42 BININT2 = 'M',
43 NONE = 'N',
44 PERSID = 'P',
45 BINPERSID = 'Q',
46 REDUCE = 'R',
47 STRING = 'S',
48 BINSTRING = 'T',
49 SHORT_BINSTRING = 'U',
50 UNICODE = 'V',
51 BINUNICODE = 'X',
52 APPEND = 'a',
53 BUILD = 'b',
54 GLOBAL = 'c',
55 DICT = 'd',
56 EMPTY_DICT = '}',
57 APPENDS = 'e',
58 GET = 'g',
59 BINGET = 'h',
60 INST = 'i',
61 LONG_BINGET = 'j',
62 LIST = 'l',
63 EMPTY_LIST = ']',
64 OBJ = 'o',
65 PUT = 'p',
66 BINPUT = 'q',
67 LONG_BINPUT = 'r',
68 SETITEM = 's',
69 TUPLE = 't',
70 EMPTY_TUPLE = ')',
71 SETITEMS = 'u',
72 BINFLOAT = 'G',
73
74 /* Protocol 2. */
75 PROTO = '\x80',
76 NEWOBJ = '\x81',
77 EXT1 = '\x82',
78 EXT2 = '\x83',
79 EXT4 = '\x84',
80 TUPLE1 = '\x85',
81 TUPLE2 = '\x86',
82 TUPLE3 = '\x87',
83 NEWTRUE = '\x88',
84 NEWFALSE = '\x89',
85 LONG1 = '\x8a',
86 LONG4 = '\x8b',
87
88 /* Protocol 3 (Python 3.x) */
89 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010090 SHORT_BINBYTES = 'C',
91
92 /* Protocol 4 */
93 SHORT_BINUNICODE = '\x8c',
94 BINUNICODE8 = '\x8d',
95 BINBYTES8 = '\x8e',
96 EMPTY_SET = '\x8f',
97 ADDITEMS = '\x90',
98 FROZENSET = '\x91',
99 NEWOBJ_EX = '\x92',
100 STACK_GLOBAL = '\x93',
101 MEMOIZE = '\x94',
102 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000103};
104
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000105enum {
106 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
107 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
108 break if this gets out of synch with pickle.py, but it's unclear that would
109 help anything either. */
110 BATCHSIZE = 1000,
111
112 /* Nesting limit until Pickler, when running in "fast mode", starts
113 checking for self-referential data-structures. */
114 FAST_NESTING_LIMIT = 50,
115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000116 /* Initial size of the write buffer of Pickler. */
117 WRITE_BUF_SIZE = 4096,
118
Antoine Pitrou04248a82010-10-12 20:51:21 +0000119 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100120 PREFETCH = 8192 * 16,
121
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200122 FRAME_SIZE_MIN = 4,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100123 FRAME_SIZE_TARGET = 64 * 1024,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100124 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000125};
126
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800127/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129/* State of the pickle module, per PEP 3121. */
130typedef struct {
131 /* Exception classes for pickle. */
132 PyObject *PickleError;
133 PyObject *PicklingError;
134 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800136 /* copyreg.dispatch_table, {type_object: pickling_function} */
137 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800139 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* copyreg._extension_registry, {(module_name, function_name): code} */
142 PyObject *extension_registry;
143 /* copyreg._extension_cache, {code: object} */
144 PyObject *extension_cache;
145 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
146 PyObject *inverted_registry;
147
148 /* Import mappings for compatibility with Python 2.x */
149
150 /* _compat_pickle.NAME_MAPPING,
151 {(oldmodule, oldname): (newmodule, newname)} */
152 PyObject *name_mapping_2to3;
153 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
154 PyObject *import_mapping_2to3;
155 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
156 PyObject *name_mapping_3to2;
157 PyObject *import_mapping_3to2;
158
159 /* codecs.encode, used for saving bytes in older protocols */
160 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300161 /* builtins.getattr, used for saving nested names with protocol < 4 */
162 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300163 /* functools.partial, used for implementing __newobj_ex__ with protocols
164 2 and 3 */
165 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800166} PickleState;
167
168/* Forward declaration of the _pickle module definition. */
169static struct PyModuleDef _picklemodule;
170
171/* Given a module object, get its per-module state. */
172static PickleState *
173_Pickle_GetState(PyObject *module)
174{
175 return (PickleState *)PyModule_GetState(module);
176}
177
178/* Find the module instance imported in the currently running sub-interpreter
179 and get its state. */
180static PickleState *
181_Pickle_GetGlobalState(void)
182{
183 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
184}
185
186/* Clear the given pickle module state. */
187static void
188_Pickle_ClearState(PickleState *st)
189{
190 Py_CLEAR(st->PickleError);
191 Py_CLEAR(st->PicklingError);
192 Py_CLEAR(st->UnpicklingError);
193 Py_CLEAR(st->dispatch_table);
194 Py_CLEAR(st->extension_registry);
195 Py_CLEAR(st->extension_cache);
196 Py_CLEAR(st->inverted_registry);
197 Py_CLEAR(st->name_mapping_2to3);
198 Py_CLEAR(st->import_mapping_2to3);
199 Py_CLEAR(st->name_mapping_3to2);
200 Py_CLEAR(st->import_mapping_3to2);
201 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300202 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100203 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204}
205
206/* Initialize the given pickle module state. */
207static int
208_Pickle_InitState(PickleState *st)
209{
210 PyObject *copyreg = NULL;
211 PyObject *compat_pickle = NULL;
212 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300213 PyObject *functools = NULL;
Serhiy Storchaka3cae16d2018-12-11 10:51:27 +0200214 _Py_IDENTIFIER(getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800215
Serhiy Storchaka3cae16d2018-12-11 10:51:27 +0200216 st->getattr = _PyEval_GetBuiltinId(&PyId_getattr);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300217 if (st->getattr == NULL)
218 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300219
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800220 copyreg = PyImport_ImportModule("copyreg");
221 if (!copyreg)
222 goto error;
223 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
224 if (!st->dispatch_table)
225 goto error;
226 if (!PyDict_CheckExact(st->dispatch_table)) {
227 PyErr_Format(PyExc_RuntimeError,
228 "copyreg.dispatch_table should be a dict, not %.200s",
229 Py_TYPE(st->dispatch_table)->tp_name);
230 goto error;
231 }
232 st->extension_registry = \
233 PyObject_GetAttrString(copyreg, "_extension_registry");
234 if (!st->extension_registry)
235 goto error;
236 if (!PyDict_CheckExact(st->extension_registry)) {
237 PyErr_Format(PyExc_RuntimeError,
238 "copyreg._extension_registry should be a dict, "
239 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
240 goto error;
241 }
242 st->inverted_registry = \
243 PyObject_GetAttrString(copyreg, "_inverted_registry");
244 if (!st->inverted_registry)
245 goto error;
246 if (!PyDict_CheckExact(st->inverted_registry)) {
247 PyErr_Format(PyExc_RuntimeError,
248 "copyreg._inverted_registry should be a dict, "
249 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
250 goto error;
251 }
252 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
253 if (!st->extension_cache)
254 goto error;
255 if (!PyDict_CheckExact(st->extension_cache)) {
256 PyErr_Format(PyExc_RuntimeError,
257 "copyreg._extension_cache should be a dict, "
258 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
259 goto error;
260 }
261 Py_CLEAR(copyreg);
262
263 /* Load the 2.x -> 3.x stdlib module mapping tables */
264 compat_pickle = PyImport_ImportModule("_compat_pickle");
265 if (!compat_pickle)
266 goto error;
267 st->name_mapping_2to3 = \
268 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
269 if (!st->name_mapping_2to3)
270 goto error;
271 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
272 PyErr_Format(PyExc_RuntimeError,
273 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
274 Py_TYPE(st->name_mapping_2to3)->tp_name);
275 goto error;
276 }
277 st->import_mapping_2to3 = \
278 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
279 if (!st->import_mapping_2to3)
280 goto error;
281 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
282 PyErr_Format(PyExc_RuntimeError,
283 "_compat_pickle.IMPORT_MAPPING should be a dict, "
284 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
285 goto error;
286 }
287 /* ... and the 3.x -> 2.x mapping tables */
288 st->name_mapping_3to2 = \
289 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
290 if (!st->name_mapping_3to2)
291 goto error;
292 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
293 PyErr_Format(PyExc_RuntimeError,
294 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
295 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
296 goto error;
297 }
298 st->import_mapping_3to2 = \
299 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
300 if (!st->import_mapping_3to2)
301 goto error;
302 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
303 PyErr_Format(PyExc_RuntimeError,
304 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
305 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
306 goto error;
307 }
308 Py_CLEAR(compat_pickle);
309
310 codecs = PyImport_ImportModule("codecs");
311 if (codecs == NULL)
312 goto error;
313 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
314 if (st->codecs_encode == NULL) {
315 goto error;
316 }
317 if (!PyCallable_Check(st->codecs_encode)) {
318 PyErr_Format(PyExc_RuntimeError,
319 "codecs.encode should be a callable, not %.200s",
320 Py_TYPE(st->codecs_encode)->tp_name);
321 goto error;
322 }
323 Py_CLEAR(codecs);
324
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300325 functools = PyImport_ImportModule("functools");
326 if (!functools)
327 goto error;
328 st->partial = PyObject_GetAttrString(functools, "partial");
329 if (!st->partial)
330 goto error;
331 Py_CLEAR(functools);
332
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800333 return 0;
334
335 error:
336 Py_CLEAR(copyreg);
337 Py_CLEAR(compat_pickle);
338 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300339 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800340 _Pickle_ClearState(st);
341 return -1;
342}
343
344/* Helper for calling a function with a single argument quickly.
345
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800346 This function steals the reference of the given argument. */
347static PyObject *
348_Pickle_FastCall(PyObject *func, PyObject *obj)
349{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 PyObject *result;
351
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100352 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200353 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 return result;
355}
356
357/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000358
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200359/* Retrieve and deconstruct a method for avoiding a reference cycle
360 (pickler -> bound method of pickler -> pickler) */
361static int
362init_method_ref(PyObject *self, _Py_Identifier *name,
363 PyObject **method_func, PyObject **method_self)
364{
365 PyObject *func, *func2;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200366 int ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200367
368 /* *method_func and *method_self should be consistent. All refcount decrements
369 should be occurred after setting *method_self and *method_func. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200370 ret = _PyObject_LookupAttrId(self, name, &func);
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200371 if (func == NULL) {
372 *method_self = NULL;
373 Py_CLEAR(*method_func);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200374 return ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200375 }
376
377 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
378 /* Deconstruct a bound Python method */
379 func2 = PyMethod_GET_FUNCTION(func);
380 Py_INCREF(func2);
381 *method_self = self; /* borrowed */
382 Py_XSETREF(*method_func, func2);
383 Py_DECREF(func);
384 return 0;
385 }
386 else {
387 *method_self = NULL;
388 Py_XSETREF(*method_func, func);
389 return 0;
390 }
391}
392
393/* Bind a method if it was deconstructed */
394static PyObject *
395reconstruct_method(PyObject *func, PyObject *self)
396{
397 if (self) {
398 return PyMethod_New(func, self);
399 }
400 else {
401 Py_INCREF(func);
402 return func;
403 }
404}
405
406static PyObject *
407call_method(PyObject *func, PyObject *self, PyObject *obj)
408{
409 if (self) {
410 return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
411 }
412 else {
413 return PyObject_CallFunctionObjArgs(func, obj, NULL);
414 }
415}
416
417/*************************************************************************/
418
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000419/* Internal data type used as the unpickling stack. */
420typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000421 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000422 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200423 int mark_set; /* is MARK set? */
424 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000425 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000426} Pdata;
427
428static void
429Pdata_dealloc(Pdata *self)
430{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200431 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 while (--i >= 0) {
433 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000434 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000435 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436 PyObject_Del(self);
437}
438
439static PyTypeObject Pdata_Type = {
440 PyVarObject_HEAD_INIT(NULL, 0)
441 "_pickle.Pdata", /*tp_name*/
442 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200443 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000444 (destructor)Pdata_dealloc, /*tp_dealloc*/
445};
446
447static PyObject *
448Pdata_New(void)
449{
450 Pdata *self;
451
452 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
453 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000454 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200455 self->mark_set = 0;
456 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000457 self->allocated = 8;
458 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000459 if (self->data)
460 return (PyObject *)self;
461 Py_DECREF(self);
462 return PyErr_NoMemory();
463}
464
465
466/* Retain only the initial clearto items. If clearto >= the current
467 * number of items, this is a (non-erroneous) NOP.
468 */
469static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200470Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000471{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200472 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000473
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200474 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000475 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000476 return 0;
477
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000478 while (--i >= clearto) {
479 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000481 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000482 return 0;
483}
484
485static int
486Pdata_grow(Pdata *self)
487{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000488 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200489 size_t allocated = (size_t)self->allocated;
490 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000491
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000492 new_allocated = (allocated >> 3) + 6;
493 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200494 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000495 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000496 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500497 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000498 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000499 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000500
501 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200502 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000503 return 0;
504
505 nomemory:
506 PyErr_NoMemory();
507 return -1;
508}
509
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200510static int
511Pdata_stack_underflow(Pdata *self)
512{
513 PickleState *st = _Pickle_GetGlobalState();
514 PyErr_SetString(st->UnpicklingError,
515 self->mark_set ?
516 "unexpected MARK found" :
517 "unpickling stack underflow");
518 return -1;
519}
520
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000521/* D is a Pdata*. Pop the topmost element and store it into V, which
522 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
523 * is raised and V is set to NULL.
524 */
525static PyObject *
526Pdata_pop(Pdata *self)
527{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200528 if (Py_SIZE(self) <= self->fence) {
529 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000530 return NULL;
531 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000532 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000533}
534#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
535
536static int
537Pdata_push(Pdata *self, PyObject *obj)
538{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000539 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000540 return -1;
541 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000542 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000543 return 0;
544}
545
546/* Push an object on stack, transferring its ownership to the stack. */
547#define PDATA_PUSH(D, O, ER) do { \
548 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
549
550/* Push an object on stack, adding a new reference to the object. */
551#define PDATA_APPEND(D, O, ER) do { \
552 Py_INCREF((O)); \
553 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
554
555static PyObject *
556Pdata_poptuple(Pdata *self, Py_ssize_t start)
557{
558 PyObject *tuple;
559 Py_ssize_t len, i, j;
560
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200561 if (start < self->fence) {
562 Pdata_stack_underflow(self);
563 return NULL;
564 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000565 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000566 tuple = PyTuple_New(len);
567 if (tuple == NULL)
568 return NULL;
569 for (i = start, j = 0; j < len; i++, j++)
570 PyTuple_SET_ITEM(tuple, j, self->data[i]);
571
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000572 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000573 return tuple;
574}
575
576static PyObject *
577Pdata_poplist(Pdata *self, Py_ssize_t start)
578{
579 PyObject *list;
580 Py_ssize_t len, i, j;
581
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000582 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000583 list = PyList_New(len);
584 if (list == NULL)
585 return NULL;
586 for (i = start, j = 0; j < len; i++, j++)
587 PyList_SET_ITEM(list, j, self->data[i]);
588
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000589 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000590 return list;
591}
592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593typedef struct {
594 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200595 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000596} PyMemoEntry;
597
598typedef struct {
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700599 size_t mt_mask;
600 size_t mt_used;
601 size_t mt_allocated;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000602 PyMemoEntry *mt_table;
603} PyMemoTable;
604
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000605typedef struct PicklerObject {
606 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000607 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000608 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000609 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000610 PyObject *pers_func; /* persistent_id() method, can be NULL */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200611 PyObject *pers_func_self; /* borrowed reference to self if pers_func
612 is an unbound method, NULL otherwise */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100613 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000614
615 PyObject *write; /* write() method of the output stream. */
616 PyObject *output_buffer; /* Write into a local bytearray buffer before
617 flushing to the stream. */
618 Py_ssize_t output_len; /* Length of output_buffer. */
619 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000620 int proto; /* Pickle protocol number, >= 0 */
621 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100622 int framing; /* True when framing is enabled, proto >= 4 */
623 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000624 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100625 is no frame currently open. */
626
627 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000628 int fast; /* Enable fast mode if set to a true value.
629 The fast mode disable the usage of memo,
630 therefore speeding the pickling process by
631 not generating superfluous PUT opcodes. It
632 should not be used if with self-referential
633 objects. */
634 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000635 int fix_imports; /* Indicate whether Pickler should fix
636 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000637 PyObject *fast_memo;
638} PicklerObject;
639
640typedef struct UnpicklerObject {
641 PyObject_HEAD
642 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000643
644 /* The unpickler memo is just an array of PyObject *s. Using a dict
645 is unnecessary, since the keys are contiguous ints. */
646 PyObject **memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700647 size_t memo_size; /* Capacity of the memo array */
648 size_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000649
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000650 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200651 PyObject *pers_func_self; /* borrowed reference to self if pers_func
652 is an unbound method, NULL otherwise */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000653
654 Py_buffer buffer;
655 char *input_buffer;
656 char *input_line;
657 Py_ssize_t input_len;
658 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000659 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100660
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000661 PyObject *read; /* read() method of the input stream. */
662 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000663 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000664
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000665 char *encoding; /* Name of the encoding to be used for
666 decoding strings pickled using Python
667 2.x. The default value is "ASCII" */
668 char *errors; /* Name of errors handling scheme to used when
669 decoding strings. The default value is
670 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500671 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000672 objects. */
673 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
674 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000675 int proto; /* Protocol of the pickle loaded. */
676 int fix_imports; /* Indicate whether Unpickler should fix
677 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000678} UnpicklerObject;
679
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200680typedef struct {
681 PyObject_HEAD
682 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
683} PicklerMemoProxyObject;
684
685typedef struct {
686 PyObject_HEAD
687 UnpicklerObject *unpickler;
688} UnpicklerMemoProxyObject;
689
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000690/* Forward declarations */
691static int save(PicklerObject *, PyObject *, int);
692static int save_reduce(PicklerObject *, PyObject *, PyObject *);
693static PyTypeObject Pickler_Type;
694static PyTypeObject Unpickler_Type;
695
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200696#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000697
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000698/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300699 A custom hashtable mapping void* to Python ints. This is used by the pickler
700 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000701 a bunch of unnecessary object creation. This makes a huge performance
702 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000703
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000704#define MT_MINSIZE 8
705#define PERTURB_SHIFT 5
706
707
708static PyMemoTable *
709PyMemoTable_New(void)
710{
711 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
712 if (memo == NULL) {
713 PyErr_NoMemory();
714 return NULL;
715 }
716
717 memo->mt_used = 0;
718 memo->mt_allocated = MT_MINSIZE;
719 memo->mt_mask = MT_MINSIZE - 1;
720 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
721 if (memo->mt_table == NULL) {
722 PyMem_FREE(memo);
723 PyErr_NoMemory();
724 return NULL;
725 }
726 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
727
728 return memo;
729}
730
731static PyMemoTable *
732PyMemoTable_Copy(PyMemoTable *self)
733{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000734 PyMemoTable *new = PyMemoTable_New();
735 if (new == NULL)
736 return NULL;
737
738 new->mt_used = self->mt_used;
739 new->mt_allocated = self->mt_allocated;
740 new->mt_mask = self->mt_mask;
741 /* The table we get from _New() is probably smaller than we wanted.
742 Free it and allocate one that's the right size. */
743 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500744 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000745 if (new->mt_table == NULL) {
746 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200747 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000748 return NULL;
749 }
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700750 for (size_t i = 0; i < self->mt_allocated; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000751 Py_XINCREF(self->mt_table[i].me_key);
752 }
753 memcpy(new->mt_table, self->mt_table,
754 sizeof(PyMemoEntry) * self->mt_allocated);
755
756 return new;
757}
758
759static Py_ssize_t
760PyMemoTable_Size(PyMemoTable *self)
761{
762 return self->mt_used;
763}
764
765static int
766PyMemoTable_Clear(PyMemoTable *self)
767{
768 Py_ssize_t i = self->mt_allocated;
769
770 while (--i >= 0) {
771 Py_XDECREF(self->mt_table[i].me_key);
772 }
773 self->mt_used = 0;
774 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
775 return 0;
776}
777
778static void
779PyMemoTable_Del(PyMemoTable *self)
780{
781 if (self == NULL)
782 return;
783 PyMemoTable_Clear(self);
784
785 PyMem_FREE(self->mt_table);
786 PyMem_FREE(self);
787}
788
789/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
790 can be considerably simpler than dictobject.c's lookdict(). */
791static PyMemoEntry *
792_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
793{
794 size_t i;
795 size_t perturb;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700796 size_t mask = self->mt_mask;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000797 PyMemoEntry *table = self->mt_table;
798 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000799 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000800
801 i = hash & mask;
802 entry = &table[i];
803 if (entry->me_key == NULL || entry->me_key == key)
804 return entry;
805
806 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
807 i = (i << 2) + i + perturb + 1;
808 entry = &table[i & mask];
809 if (entry->me_key == NULL || entry->me_key == key)
810 return entry;
811 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700812 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000813}
814
815/* Returns -1 on failure, 0 on success. */
816static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700817_PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000818{
819 PyMemoEntry *oldtable = NULL;
820 PyMemoEntry *oldentry, *newentry;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700821 size_t new_size = MT_MINSIZE;
822 size_t to_process;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000823
824 assert(min_size > 0);
825
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700826 if (min_size > PY_SSIZE_T_MAX) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000827 PyErr_NoMemory();
828 return -1;
829 }
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700830
831 /* Find the smallest valid table size >= min_size. */
832 while (new_size < min_size) {
833 new_size <<= 1;
834 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000835 /* new_size needs to be a power of two. */
836 assert((new_size & (new_size - 1)) == 0);
837
838 /* Allocate new table. */
839 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500840 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000841 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200842 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000843 PyErr_NoMemory();
844 return -1;
845 }
846 self->mt_allocated = new_size;
847 self->mt_mask = new_size - 1;
848 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
849
850 /* Copy entries from the old table. */
851 to_process = self->mt_used;
852 for (oldentry = oldtable; to_process > 0; oldentry++) {
853 if (oldentry->me_key != NULL) {
854 to_process--;
855 /* newentry is a pointer to a chunk of the new
856 mt_table, so we're setting the key:value pair
857 in-place. */
858 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
859 newentry->me_key = oldentry->me_key;
860 newentry->me_value = oldentry->me_value;
861 }
862 }
863
864 /* Deallocate the old table. */
865 PyMem_FREE(oldtable);
866 return 0;
867}
868
869/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200870static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000871PyMemoTable_Get(PyMemoTable *self, PyObject *key)
872{
873 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
874 if (entry->me_key == NULL)
875 return NULL;
876 return &entry->me_value;
877}
878
879/* Returns -1 on failure, 0 on success. */
880static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200881PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000882{
883 PyMemoEntry *entry;
884
885 assert(key != NULL);
886
887 entry = _PyMemoTable_Lookup(self, key);
888 if (entry->me_key != NULL) {
889 entry->me_value = value;
890 return 0;
891 }
892 Py_INCREF(key);
893 entry->me_key = key;
894 entry->me_value = value;
895 self->mt_used++;
896
897 /* If we added a key, we can safely resize. Otherwise just return!
898 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
899 *
900 * Quadrupling the size improves average table sparseness
901 * (reducing collisions) at the cost of some memory. It also halves
902 * the number of expensive resize operations in a growing memo table.
903 *
904 * Very large memo tables (over 50K items) use doubling instead.
905 * This may help applications with severe memory constraints.
906 */
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700907 if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000908 return 0;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700909 }
910 // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow.
911 size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used;
912 return _PyMemoTable_ResizeTable(self, desired_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000913}
914
915#undef MT_MINSIZE
916#undef PERTURB_SHIFT
917
918/*************************************************************************/
919
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000920
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000921static int
922_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000923{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300924 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200925 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000926 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000927 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000928 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100929 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000930 return 0;
931}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000932
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100933static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100934_write_size64(char *out, size_t value)
935{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200936 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800937
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200938 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800939
940 for (i = 0; i < sizeof(size_t); i++) {
941 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
942 }
943 for (i = sizeof(size_t); i < 8; i++) {
944 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800945 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100946}
947
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100948static int
949_Pickler_CommitFrame(PicklerObject *self)
950{
951 size_t frame_len;
952 char *qdata;
953
954 if (!self->framing || self->frame_start == -1)
955 return 0;
956 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
957 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200958 if (frame_len >= FRAME_SIZE_MIN) {
959 qdata[0] = FRAME;
960 _write_size64(qdata + 1, frame_len);
961 }
962 else {
963 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
964 self->output_len -= FRAME_HEADER_SIZE;
965 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100966 self->frame_start = -1;
967 return 0;
968}
969
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000970static PyObject *
971_Pickler_GetString(PicklerObject *self)
972{
973 PyObject *output_buffer = self->output_buffer;
974
975 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100976
977 if (_Pickler_CommitFrame(self))
978 return NULL;
979
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000980 self->output_buffer = NULL;
981 /* Resize down to exact size */
982 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
983 return NULL;
984 return output_buffer;
985}
986
987static int
988_Pickler_FlushToFile(PicklerObject *self)
989{
990 PyObject *output, *result;
991
992 assert(self->write != NULL);
993
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100994 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000995 output = _Pickler_GetString(self);
996 if (output == NULL)
997 return -1;
998
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800999 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001000 Py_XDECREF(result);
1001 return (result == NULL) ? -1 : 0;
1002}
1003
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001004static int
1005_Pickler_OpcodeBoundary(PicklerObject *self)
1006{
1007 Py_ssize_t frame_len;
1008
1009 if (!self->framing || self->frame_start == -1) {
1010 return 0;
1011 }
1012 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1013 if (frame_len >= FRAME_SIZE_TARGET) {
1014 if(_Pickler_CommitFrame(self)) {
1015 return -1;
1016 }
Miss Islington (bot)e86db342018-02-03 17:41:43 -08001017 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001018 * file and reuse the pickler buffer for the next frame so as
1019 * to limit memory usage when dumping large complex objects to
1020 * a file.
1021 *
1022 * self->write is NULL when called via dumps.
1023 */
1024 if (self->write != NULL) {
1025 if (_Pickler_FlushToFile(self) < 0) {
1026 return -1;
1027 }
1028 if (_Pickler_ClearBuffer(self) < 0) {
1029 return -1;
1030 }
1031 }
1032 }
1033 return 0;
1034}
1035
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001036static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001037_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001038{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001039 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001040 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001041 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042
1043 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001044 need_new_frame = (self->framing && self->frame_start == -1);
1045
1046 if (need_new_frame)
1047 n = data_len + FRAME_HEADER_SIZE;
1048 else
1049 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050
1051 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001052 if (required > self->max_output_len) {
1053 /* Make place in buffer for the pickle chunk */
1054 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1055 PyErr_NoMemory();
1056 return -1;
1057 }
1058 self->max_output_len = (self->output_len + n) / 2 * 3;
1059 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1060 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001061 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001062 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001063 if (need_new_frame) {
1064 /* Setup new frame */
1065 Py_ssize_t frame_start = self->output_len;
1066 self->frame_start = frame_start;
1067 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1068 /* Write an invalid value, for debugging */
1069 buffer[frame_start + i] = 0xFE;
1070 }
1071 self->output_len += FRAME_HEADER_SIZE;
1072 }
1073 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001074 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001075 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001076 buffer[self->output_len + i] = s[i];
1077 }
1078 }
1079 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001080 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001081 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001082 self->output_len += data_len;
1083 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001084}
1085
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001086static PicklerObject *
1087_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001088{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001089 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001090
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001091 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1092 if (self == NULL)
1093 return NULL;
1094
1095 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001096 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001097 self->write = NULL;
1098 self->proto = 0;
1099 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001100 self->framing = 0;
1101 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001102 self->fast = 0;
1103 self->fast_nesting = 0;
1104 self->fix_imports = 0;
1105 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001106 self->max_output_len = WRITE_BUF_SIZE;
1107 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001108
1109 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001110 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1111 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001112
1113 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001114 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001115 return NULL;
1116 }
1117 return self;
1118}
1119
1120static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001121_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001122{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001123 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001124
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001125 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001126 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001127 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001129 proto = PyLong_AsLong(protocol);
1130 if (proto < 0) {
1131 if (proto == -1 && PyErr_Occurred())
1132 return -1;
1133 proto = HIGHEST_PROTOCOL;
1134 }
1135 else if (proto > HIGHEST_PROTOCOL) {
1136 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1137 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001138 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001139 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001140 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001141 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001142 self->bin = proto > 0;
1143 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144 return 0;
1145}
1146
1147/* Returns -1 (with an exception set) on failure, 0 on success. This may
1148 be called once on a freshly created Pickler. */
1149static int
1150_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1151{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001152 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001153 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001154 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1155 return -1;
1156 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001157 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001158 PyErr_SetString(PyExc_TypeError,
1159 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001160 return -1;
1161 }
1162
1163 return 0;
1164}
1165
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001166/* Returns the size of the input on success, -1 on failure. This takes its
1167 own reference to `input`. */
1168static Py_ssize_t
1169_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1170{
1171 if (self->buffer.buf != NULL)
1172 PyBuffer_Release(&self->buffer);
1173 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1174 return -1;
1175 self->input_buffer = self->buffer.buf;
1176 self->input_len = self->buffer.len;
1177 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001178 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001179 return self->input_len;
1180}
1181
Antoine Pitrou04248a82010-10-12 20:51:21 +00001182static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001183bad_readline(void)
1184{
1185 PickleState *st = _Pickle_GetGlobalState();
1186 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1187 return -1;
1188}
1189
1190static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001191_Unpickler_SkipConsumed(UnpicklerObject *self)
1192{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001193 Py_ssize_t consumed;
1194 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001195
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001196 consumed = self->next_read_idx - self->prefetched_idx;
1197 if (consumed <= 0)
1198 return 0;
1199
1200 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001201 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001202 r = PyObject_CallFunction(self->read, "n", consumed);
1203 if (r == NULL)
1204 return -1;
1205 Py_DECREF(r);
1206
1207 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001208 return 0;
1209}
1210
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001211static const Py_ssize_t READ_WHOLE_LINE = -1;
1212
1213/* If reading from a file, we need to only pull the bytes we need, since there
1214 may be multiple pickle objects arranged contiguously in the same input
1215 buffer.
1216
1217 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1218 bytes from the input stream/buffer.
1219
1220 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1221 failure; on success, returns the number of bytes read from the file.
1222
1223 On success, self->input_len will be 0; this is intentional so that when
1224 unpickling from a file, the "we've run out of data" code paths will trigger,
1225 causing the Unpickler to go back to the file for more data. Use the returned
1226 size to tell you how much data you can process. */
1227static Py_ssize_t
1228_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1229{
1230 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001231 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001232
1233 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001234
Antoine Pitrou04248a82010-10-12 20:51:21 +00001235 if (_Unpickler_SkipConsumed(self) < 0)
1236 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001237
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001238 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001239 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001240 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001241 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001242 PyObject *len;
1243 /* Prefetch some data without advancing the file pointer, if possible */
1244 if (self->peek && n < PREFETCH) {
1245 len = PyLong_FromSsize_t(PREFETCH);
1246 if (len == NULL)
1247 return -1;
1248 data = _Pickle_FastCall(self->peek, len);
1249 if (data == NULL) {
1250 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1251 return -1;
1252 /* peek() is probably not supported by the given file object */
1253 PyErr_Clear();
1254 Py_CLEAR(self->peek);
1255 }
1256 else {
1257 read_size = _Unpickler_SetStringInput(self, data);
1258 Py_DECREF(data);
1259 self->prefetched_idx = 0;
1260 if (n <= read_size)
1261 return n;
1262 }
1263 }
1264 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001265 if (len == NULL)
1266 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001267 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001268 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001269 if (data == NULL)
1270 return -1;
1271
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001272 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001273 Py_DECREF(data);
1274 return read_size;
1275}
1276
Victor Stinner19ed27e2016-05-20 11:42:37 +02001277/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001278static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001279_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001280{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001281 Py_ssize_t num_read;
1282
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001283 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001284 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1285 PickleState *st = _Pickle_GetGlobalState();
1286 PyErr_SetString(st->UnpicklingError,
1287 "read would overflow (invalid bytecode)");
1288 return -1;
1289 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001290
1291 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1292 assert(self->next_read_idx + n > self->input_len);
1293
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001294 if (!self->read)
1295 return bad_readline();
1296
Antoine Pitrou04248a82010-10-12 20:51:21 +00001297 num_read = _Unpickler_ReadFromFile(self, n);
1298 if (num_read < 0)
1299 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001300 if (num_read < n)
1301 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001302 *s = self->input_buffer;
1303 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001304 return n;
1305}
1306
Victor Stinner19ed27e2016-05-20 11:42:37 +02001307/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1308
1309 This should be used for all data reads, rather than accessing the unpickler's
1310 input buffer directly. This method deals correctly with reading from input
1311 streams, which the input buffer doesn't deal with.
1312
1313 Note that when reading from a file-like object, self->next_read_idx won't
1314 be updated (it should remain at 0 for the entire unpickling process). You
1315 should use this function's return value to know how many bytes you can
1316 consume.
1317
1318 Returns -1 (with an exception set) on failure. On success, return the
1319 number of chars read. */
1320#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001321 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001322 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1323 (self)->next_read_idx += (n), \
1324 (n)) \
1325 : _Unpickler_ReadImpl(self, (s), (n)))
1326
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001327static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001328_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1329 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001330{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001331 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001332 if (input_line == NULL) {
1333 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001334 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001335 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001336
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001337 memcpy(input_line, line, len);
1338 input_line[len] = '\0';
1339 self->input_line = input_line;
1340 *result = self->input_line;
1341 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001342}
1343
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001344/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001345 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001346
1347 Returns the number of chars read, or -1 on failure. */
1348static Py_ssize_t
1349_Unpickler_Readline(UnpicklerObject *self, char **result)
1350{
1351 Py_ssize_t i, num_read;
1352
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001353 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001354 if (self->input_buffer[i] == '\n') {
1355 char *line_start = self->input_buffer + self->next_read_idx;
1356 num_read = i - self->next_read_idx + 1;
1357 self->next_read_idx = i + 1;
1358 return _Unpickler_CopyLine(self, line_start, num_read, result);
1359 }
1360 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001361 if (!self->read)
1362 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001363
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001364 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1365 if (num_read < 0)
1366 return -1;
1367 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1368 return bad_readline();
1369 self->next_read_idx = num_read;
1370 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001371}
1372
1373/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1374 will be modified in place. */
1375static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001376_Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001377{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001378 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001379
1380 assert(new_size > self->memo_size);
1381
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001382 PyObject **memo_new = self->memo;
1383 PyMem_RESIZE(memo_new, PyObject *, new_size);
1384 if (memo_new == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001385 PyErr_NoMemory();
1386 return -1;
1387 }
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001388 self->memo = memo_new;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389 for (i = self->memo_size; i < new_size; i++)
1390 self->memo[i] = NULL;
1391 self->memo_size = new_size;
1392 return 0;
1393}
1394
1395/* Returns NULL if idx is out of bounds. */
1396static PyObject *
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001397_Unpickler_MemoGet(UnpicklerObject *self, size_t idx)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001398{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001399 if (idx >= self->memo_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001400 return NULL;
1401
1402 return self->memo[idx];
1403}
1404
1405/* Returns -1 (with an exception set) on failure, 0 on success.
1406 This takes its own reference to `value`. */
1407static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001408_Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001409{
1410 PyObject *old_item;
1411
1412 if (idx >= self->memo_size) {
1413 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1414 return -1;
1415 assert(idx < self->memo_size);
1416 }
1417 Py_INCREF(value);
1418 old_item = self->memo[idx];
1419 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001420 if (old_item != NULL) {
1421 Py_DECREF(old_item);
1422 }
1423 else {
1424 self->memo_len++;
1425 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001426 return 0;
1427}
1428
1429static PyObject **
1430_Unpickler_NewMemo(Py_ssize_t new_size)
1431{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001432 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001433 if (memo == NULL) {
1434 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001435 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001436 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001437 memset(memo, 0, new_size * sizeof(PyObject *));
1438 return memo;
1439}
1440
1441/* Free the unpickler's memo, taking care to decref any items left in it. */
1442static void
1443_Unpickler_MemoCleanup(UnpicklerObject *self)
1444{
1445 Py_ssize_t i;
1446 PyObject **memo = self->memo;
1447
1448 if (self->memo == NULL)
1449 return;
1450 self->memo = NULL;
1451 i = self->memo_size;
1452 while (--i >= 0) {
1453 Py_XDECREF(memo[i]);
1454 }
1455 PyMem_FREE(memo);
1456}
1457
1458static UnpicklerObject *
1459_Unpickler_New(void)
1460{
1461 UnpicklerObject *self;
1462
1463 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1464 if (self == NULL)
1465 return NULL;
1466
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001467 self->pers_func = NULL;
1468 self->input_buffer = NULL;
1469 self->input_line = NULL;
1470 self->input_len = 0;
1471 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001472 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001473 self->read = NULL;
1474 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001475 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001476 self->encoding = NULL;
1477 self->errors = NULL;
1478 self->marks = NULL;
1479 self->num_marks = 0;
1480 self->marks_size = 0;
1481 self->proto = 0;
1482 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001483 memset(&self->buffer, 0, sizeof(Py_buffer));
1484 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001485 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001486 self->memo = _Unpickler_NewMemo(self->memo_size);
1487 self->stack = (Pdata *)Pdata_New();
1488
1489 if (self->memo == NULL || self->stack == NULL) {
1490 Py_DECREF(self);
1491 return NULL;
1492 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001493
1494 return self;
1495}
1496
1497/* Returns -1 (with an exception set) on failure, 0 on success. This may
1498 be called once on a freshly created Pickler. */
1499static int
1500_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1501{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001502 _Py_IDENTIFIER(peek);
1503 _Py_IDENTIFIER(read);
1504 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001505
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001506 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1507 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001508 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001509 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1510 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001511 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001512 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001513 PyErr_SetString(PyExc_TypeError,
1514 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001515 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001516 Py_CLEAR(self->read);
1517 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001518 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001519 return -1;
1520 }
1521 return 0;
1522}
1523
1524/* Returns -1 (with an exception set) on failure, 0 on success. This may
1525 be called once on a freshly created Pickler. */
1526static int
1527_Unpickler_SetInputEncoding(UnpicklerObject *self,
1528 const char *encoding,
1529 const char *errors)
1530{
1531 if (encoding == NULL)
1532 encoding = "ASCII";
1533 if (errors == NULL)
1534 errors = "strict";
1535
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001536 self->encoding = _PyMem_Strdup(encoding);
1537 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001538 if (self->encoding == NULL || self->errors == NULL) {
1539 PyErr_NoMemory();
1540 return -1;
1541 }
1542 return 0;
1543}
1544
1545/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001546static int
1547memo_get(PicklerObject *self, PyObject *key)
1548{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001549 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001550 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001551 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001552
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001553 value = PyMemoTable_Get(self->memo, key);
1554 if (value == NULL) {
1555 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001556 return -1;
1557 }
1558
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001559 if (!self->bin) {
1560 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001561 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1562 "%" PY_FORMAT_SIZE_T "d\n", *value);
1563 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001564 }
1565 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001566 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001567 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001568 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001569 len = 2;
1570 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001571 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001572 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001573 pdata[1] = (unsigned char)(*value & 0xff);
1574 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1575 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1576 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001577 len = 5;
1578 }
1579 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001580 PickleState *st = _Pickle_GetGlobalState();
1581 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001582 "memo id too large for LONG_BINGET");
1583 return -1;
1584 }
1585 }
1586
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001587 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001588 return -1;
1589
1590 return 0;
1591}
1592
1593/* Store an object in the memo, assign it a new unique ID based on the number
1594 of objects currently stored in the memo and generate a PUT opcode. */
1595static int
1596memo_put(PicklerObject *self, PyObject *obj)
1597{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001598 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001599 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001600 Py_ssize_t idx;
1601
1602 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001603
1604 if (self->fast)
1605 return 0;
1606
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001607 idx = PyMemoTable_Size(self->memo);
1608 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1609 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001610
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 if (self->proto >= 4) {
1612 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1613 return -1;
1614 return 0;
1615 }
1616 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001617 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001618 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001619 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001620 len = strlen(pdata);
1621 }
1622 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001623 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001624 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001625 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001626 len = 2;
1627 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001628 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001629 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001630 pdata[1] = (unsigned char)(idx & 0xff);
1631 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1632 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1633 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001634 len = 5;
1635 }
1636 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001637 PickleState *st = _Pickle_GetGlobalState();
1638 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001639 "memo id too large for LONG_BINPUT");
1640 return -1;
1641 }
1642 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001643 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001644 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001645
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001646 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001647}
1648
1649static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001650get_dotted_path(PyObject *obj, PyObject *name)
1651{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001652 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001653 PyObject *dotted_path;
1654 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001655
1656 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001657 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001658 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001659 n = PyList_GET_SIZE(dotted_path);
1660 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001661 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001662 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001663 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001664 if (obj == NULL)
1665 PyErr_Format(PyExc_AttributeError,
1666 "Can't pickle local object %R", name);
1667 else
1668 PyErr_Format(PyExc_AttributeError,
1669 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001670 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001671 return NULL;
1672 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001673 }
1674 return dotted_path;
1675}
1676
1677static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001678get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001679{
1680 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001681 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001682
1683 assert(PyList_CheckExact(names));
1684 Py_INCREF(obj);
1685 n = PyList_GET_SIZE(names);
1686 for (i = 0; i < n; i++) {
1687 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001688 Py_XDECREF(parent);
1689 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001690 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001691 if (obj == NULL) {
1692 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001693 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001694 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001695 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001696 if (pparent != NULL)
1697 *pparent = parent;
1698 else
1699 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001700 return obj;
1701}
1702
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001703
1704static PyObject *
1705getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1706{
1707 PyObject *dotted_path, *attr;
1708
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001709 if (allow_qualname) {
1710 dotted_path = get_dotted_path(obj, name);
1711 if (dotted_path == NULL)
1712 return NULL;
1713 attr = get_deep_attribute(obj, dotted_path, NULL);
1714 Py_DECREF(dotted_path);
1715 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001716 else {
1717 (void)_PyObject_LookupAttr(obj, name, &attr);
1718 }
1719 if (attr == NULL && !PyErr_Occurred()) {
1720 PyErr_Format(PyExc_AttributeError,
1721 "Can't get attribute %R on %R", name, obj);
1722 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001723 return attr;
1724}
1725
Eric Snow3f9eee62017-09-15 16:35:20 -06001726static int
1727_checkmodule(PyObject *module_name, PyObject *module,
1728 PyObject *global, PyObject *dotted_path)
1729{
1730 if (module == Py_None) {
1731 return -1;
1732 }
1733 if (PyUnicode_Check(module_name) &&
1734 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1735 return -1;
1736 }
1737
1738 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1739 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001740 return -1;
1741 }
1742 if (candidate != global) {
1743 Py_DECREF(candidate);
1744 return -1;
1745 }
1746 Py_DECREF(candidate);
1747 return 0;
1748}
1749
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001750static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001751whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001752{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001753 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001754 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001755 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001756 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001757 _Py_IDENTIFIER(__module__);
1758 _Py_IDENTIFIER(modules);
1759 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001760
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001761 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1762 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001763 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001764 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001765 /* In some rare cases (e.g., bound methods of extension types),
1766 __module__ can be None. If it is so, then search sys.modules for
1767 the module of global. */
1768 if (module_name != Py_None)
1769 return module_name;
1770 Py_CLEAR(module_name);
1771 }
1772 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001773
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001774 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001775 modules = _PySys_GetObjectId(&PyId_modules);
1776 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001777 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001778 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001779 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001780 if (PyDict_CheckExact(modules)) {
1781 i = 0;
1782 while (PyDict_Next(modules, &i, &module_name, &module)) {
1783 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1784 Py_INCREF(module_name);
1785 return module_name;
1786 }
1787 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001788 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001789 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001790 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001791 }
1792 else {
1793 PyObject *iterator = PyObject_GetIter(modules);
1794 if (iterator == NULL) {
1795 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001796 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001797 while ((module_name = PyIter_Next(iterator))) {
1798 module = PyObject_GetItem(modules, module_name);
1799 if (module == NULL) {
1800 Py_DECREF(module_name);
1801 Py_DECREF(iterator);
1802 return NULL;
1803 }
1804 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1805 Py_DECREF(module);
1806 Py_DECREF(iterator);
1807 return module_name;
1808 }
1809 Py_DECREF(module);
1810 Py_DECREF(module_name);
1811 if (PyErr_Occurred()) {
1812 Py_DECREF(iterator);
1813 return NULL;
1814 }
1815 }
1816 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001817 }
1818
1819 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001820 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001821 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001822 return module_name;
1823}
1824
1825/* fast_save_enter() and fast_save_leave() are guards against recursive
1826 objects when Pickler is used with the "fast mode" (i.e., with object
1827 memoization disabled). If the nesting of a list or dict object exceed
1828 FAST_NESTING_LIMIT, these guards will start keeping an internal
1829 reference to the seen list or dict objects and check whether these objects
1830 are recursive. These are not strictly necessary, since save() has a
1831 hard-coded recursion limit, but they give a nicer error message than the
1832 typical RuntimeError. */
1833static int
1834fast_save_enter(PicklerObject *self, PyObject *obj)
1835{
1836 /* if fast_nesting < 0, we're doing an error exit. */
1837 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1838 PyObject *key = NULL;
1839 if (self->fast_memo == NULL) {
1840 self->fast_memo = PyDict_New();
1841 if (self->fast_memo == NULL) {
1842 self->fast_nesting = -1;
1843 return 0;
1844 }
1845 }
1846 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001847 if (key == NULL) {
1848 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001849 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001850 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001851 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001852 Py_DECREF(key);
1853 PyErr_Format(PyExc_ValueError,
1854 "fast mode: can't pickle cyclic objects "
1855 "including object type %.200s at %p",
1856 obj->ob_type->tp_name, obj);
1857 self->fast_nesting = -1;
1858 return 0;
1859 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001860 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001861 Py_DECREF(key);
1862 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001863 return 0;
1864 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1866 Py_DECREF(key);
1867 self->fast_nesting = -1;
1868 return 0;
1869 }
1870 Py_DECREF(key);
1871 }
1872 return 1;
1873}
1874
1875static int
1876fast_save_leave(PicklerObject *self, PyObject *obj)
1877{
1878 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1879 PyObject *key = PyLong_FromVoidPtr(obj);
1880 if (key == NULL)
1881 return 0;
1882 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1883 Py_DECREF(key);
1884 return 0;
1885 }
1886 Py_DECREF(key);
1887 }
1888 return 1;
1889}
1890
1891static int
1892save_none(PicklerObject *self, PyObject *obj)
1893{
1894 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001895 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001896 return -1;
1897
1898 return 0;
1899}
1900
1901static int
1902save_bool(PicklerObject *self, PyObject *obj)
1903{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001904 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001905 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001906 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001907 return -1;
1908 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001909 else {
1910 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1911 * so that unpicklers written before bools were introduced unpickle them
1912 * as ints, but unpicklers after can recognize that bools were intended.
1913 * Note that protocol 2 added direct ways to pickle bools.
1914 */
1915 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1916 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1917 return -1;
1918 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001919 return 0;
1920}
1921
1922static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001923save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001924{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001925 PyObject *repr = NULL;
1926 Py_ssize_t size;
1927 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001928 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001929 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001930
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001931 val= PyLong_AsLongAndOverflow(obj, &overflow);
1932 if (!overflow && (sizeof(long) <= 4 ||
1933 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1934 {
Larry Hastings61272b72014-01-07 12:41:53 -08001935 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001936
1937 Note: we can't use -0x80000000L in the above condition because some
1938 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1939 before applying the unary minus when sizeof(long) <= 4. The
1940 resulting value stays unsigned which is commonly not what we want,
1941 so MSVC happily warns us about it. However, that result would have
1942 been fine because we guard for sizeof(long) <= 4 which turns the
1943 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001944 char pdata[32];
1945 Py_ssize_t len = 0;
1946
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001947 if (self->bin) {
1948 pdata[1] = (unsigned char)(val & 0xff);
1949 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1950 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1951 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001952
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001953 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1954 pdata[0] = BININT;
1955 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001956 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001957 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001958 pdata[0] = BININT2;
1959 len = 3;
1960 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001961 else {
1962 pdata[0] = BININT1;
1963 len = 2;
1964 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001965 }
1966 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001967 sprintf(pdata, "%c%ld\n", INT, val);
1968 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001969 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001970 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001971 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001972
1973 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001974 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001975 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001977 if (self->proto >= 2) {
1978 /* Linear-time pickling. */
1979 size_t nbits;
1980 size_t nbytes;
1981 unsigned char *pdata;
1982 char header[5];
1983 int i;
1984 int sign = _PyLong_Sign(obj);
1985
1986 if (sign == 0) {
1987 header[0] = LONG1;
1988 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001989 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001990 goto error;
1991 return 0;
1992 }
1993 nbits = _PyLong_NumBits(obj);
1994 if (nbits == (size_t)-1 && PyErr_Occurred())
1995 goto error;
1996 /* How many bytes do we need? There are nbits >> 3 full
1997 * bytes of data, and nbits & 7 leftover bits. If there
1998 * are any leftover bits, then we clearly need another
1999 * byte. Wnat's not so obvious is that we *probably*
2000 * need another byte even if there aren't any leftovers:
2001 * the most-significant bit of the most-significant byte
2002 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002003 * opposite of the one we need. The exception is ints
2004 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002005 * its own 256's-complement, so has the right sign bit
2006 * even without the extra byte. That's a pain to check
2007 * for in advance, though, so we always grab an extra
2008 * byte at the start, and cut it back later if possible.
2009 */
2010 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002011 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002012 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002013 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002014 goto error;
2015 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002016 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002017 if (repr == NULL)
2018 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002019 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002020 i = _PyLong_AsByteArray((PyLongObject *)obj,
2021 pdata, nbytes,
2022 1 /* little endian */ , 1 /* signed */ );
2023 if (i < 0)
2024 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002025 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002026 * needed. This is so iff the MSB is all redundant sign
2027 * bits.
2028 */
2029 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002030 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002031 pdata[nbytes - 1] == 0xff &&
2032 (pdata[nbytes - 2] & 0x80) != 0) {
2033 nbytes--;
2034 }
2035
2036 if (nbytes < 256) {
2037 header[0] = LONG1;
2038 header[1] = (unsigned char)nbytes;
2039 size = 2;
2040 }
2041 else {
2042 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002043 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002044 for (i = 1; i < 5; i++) {
2045 header[i] = (unsigned char)(size & 0xff);
2046 size >>= 8;
2047 }
2048 size = 5;
2049 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002050 if (_Pickler_Write(self, header, size) < 0 ||
2051 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002052 goto error;
2053 }
2054 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002055 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002056 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002057
Mark Dickinson8dd05142009-01-20 20:43:58 +00002058 /* proto < 2: write the repr and newline. This is quadratic-time (in
2059 the number of digits), in both directions. We add a trailing 'L'
2060 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061
2062 repr = PyObject_Repr(obj);
2063 if (repr == NULL)
2064 goto error;
2065
Serhiy Storchaka06515832016-11-20 09:13:07 +02002066 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002067 if (string == NULL)
2068 goto error;
2069
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002070 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2071 _Pickler_Write(self, string, size) < 0 ||
2072 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073 goto error;
2074 }
2075
2076 if (0) {
2077 error:
2078 status = -1;
2079 }
2080 Py_XDECREF(repr);
2081
2082 return status;
2083}
2084
2085static int
2086save_float(PicklerObject *self, PyObject *obj)
2087{
2088 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2089
2090 if (self->bin) {
2091 char pdata[9];
2092 pdata[0] = BINFLOAT;
2093 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2094 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002095 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002097 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002099 int result = -1;
2100 char *buf = NULL;
2101 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002102
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002103 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002104 goto done;
2105
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002106 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002107 if (!buf) {
2108 PyErr_NoMemory();
2109 goto done;
2110 }
2111
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002112 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002113 goto done;
2114
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002115 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002116 goto done;
2117
2118 result = 0;
2119done:
2120 PyMem_Free(buf);
2121 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002122 }
2123
2124 return 0;
2125}
2126
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002127/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002128
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002129 The large contiguous data is written directly into the underlying file
2130 object, bypassing the output_buffer of the Pickler. We intentionally
2131 do not insert a protocol 4 frame opcode to make it possible to optimize
2132 file.read calls in the loader.
2133 */
2134static int
2135_Pickler_write_bytes(PicklerObject *self,
2136 const char *header, Py_ssize_t header_size,
2137 const char *data, Py_ssize_t data_size,
2138 PyObject *payload)
2139{
2140 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2141 int framing = self->framing;
2142
2143 if (bypass_buffer) {
2144 assert(self->output_buffer != NULL);
2145 /* Commit the previous frame. */
2146 if (_Pickler_CommitFrame(self)) {
2147 return -1;
2148 }
2149 /* Disable framing temporarily */
2150 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002151 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002152
2153 if (_Pickler_Write(self, header, header_size) < 0) {
2154 return -1;
2155 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002156
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002157 if (bypass_buffer && self->write != NULL) {
2158 /* Bypass the in-memory buffer to directly stream large data
2159 into the underlying file object. */
2160 PyObject *result, *mem = NULL;
2161 /* Dump the output buffer to the file. */
2162 if (_Pickler_FlushToFile(self) < 0) {
2163 return -1;
2164 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002165
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002166 /* Stream write the payload into the file without going through the
2167 output buffer. */
2168 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002169 /* TODO: It would be better to use a memoryview with a linked
2170 original string if this is possible. */
2171 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002172 if (payload == NULL) {
2173 return -1;
2174 }
2175 }
2176 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2177 Py_XDECREF(mem);
2178 if (result == NULL) {
2179 return -1;
2180 }
2181 Py_DECREF(result);
2182
2183 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2184 if (_Pickler_ClearBuffer(self) < 0) {
2185 return -1;
2186 }
2187 }
2188 else {
2189 if (_Pickler_Write(self, data, data_size) < 0) {
2190 return -1;
2191 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002192 }
2193
2194 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002195 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002196
2197 return 0;
2198}
2199
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002200static int
2201save_bytes(PicklerObject *self, PyObject *obj)
2202{
2203 if (self->proto < 3) {
2204 /* Older pickle protocols do not have an opcode for pickling bytes
2205 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002206 the __reduce__ method) to permit bytes object unpickling.
2207
2208 Here we use a hack to be compatible with Python 2. Since in Python
2209 2 'bytes' is just an alias for 'str' (which has different
2210 parameters than the actual bytes object), we use codecs.encode
2211 to create the appropriate 'str' object when unpickled using
2212 Python 2 *and* the appropriate 'bytes' object when unpickled
2213 using Python 3. Again this is a hack and we don't need to do this
2214 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002215 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002216 int status;
2217
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002218 if (PyBytes_GET_SIZE(obj) == 0) {
2219 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2220 }
2221 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002222 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002223 PyObject *unicode_str =
2224 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2225 PyBytes_GET_SIZE(obj),
2226 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002227 _Py_IDENTIFIER(latin1);
2228
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002229 if (unicode_str == NULL)
2230 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002231 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002232 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002233 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002234 Py_DECREF(unicode_str);
2235 }
2236
2237 if (reduce_value == NULL)
2238 return -1;
2239
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002240 /* save_reduce() will memoize the object automatically. */
2241 status = save_reduce(self, reduce_value, obj);
2242 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002243 return status;
2244 }
2245 else {
2246 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002247 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002248 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002249
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002250 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002251 if (size < 0)
2252 return -1;
2253
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002254 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255 header[0] = SHORT_BINBYTES;
2256 header[1] = (unsigned char)size;
2257 len = 2;
2258 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002259 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002260 header[0] = BINBYTES;
2261 header[1] = (unsigned char)(size & 0xff);
2262 header[2] = (unsigned char)((size >> 8) & 0xff);
2263 header[3] = (unsigned char)((size >> 16) & 0xff);
2264 header[4] = (unsigned char)((size >> 24) & 0xff);
2265 len = 5;
2266 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002267 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002268 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002269 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002270 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002271 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002272 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002273 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002274 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002275 return -1; /* string too large */
2276 }
2277
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002278 if (_Pickler_write_bytes(self, header, len,
2279 PyBytes_AS_STRING(obj), size, obj) < 0)
2280 {
2281 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002282 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002283
2284 if (memo_put(self, obj) < 0)
2285 return -1;
2286
2287 return 0;
2288 }
2289}
2290
2291/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2292 backslash and newline characters to \uXXXX escapes. */
2293static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002294raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002297 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002298 void *data;
2299 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002300 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002301
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002302 if (PyUnicode_READY(obj))
2303 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002304
Victor Stinner358af132015-10-12 22:36:57 +02002305 _PyBytesWriter_Init(&writer);
2306
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002307 size = PyUnicode_GET_LENGTH(obj);
2308 data = PyUnicode_DATA(obj);
2309 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002310
Victor Stinner358af132015-10-12 22:36:57 +02002311 p = _PyBytesWriter_Alloc(&writer, size);
2312 if (p == NULL)
2313 goto error;
2314 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002315
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002316 for (i=0; i < size; i++) {
2317 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002318 /* Map 32-bit characters to '\Uxxxxxxxx' */
2319 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002320 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002321 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2322 if (p == NULL)
2323 goto error;
2324
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002325 *p++ = '\\';
2326 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002327 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2328 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2329 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2330 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2331 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2332 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2333 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2334 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002335 }
Victor Stinner358af132015-10-12 22:36:57 +02002336 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002337 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002338 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002339 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2340 if (p == NULL)
2341 goto error;
2342
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002343 *p++ = '\\';
2344 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002345 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2346 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2347 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2348 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002349 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002350 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351 else
2352 *p++ = (char) ch;
2353 }
Victor Stinner358af132015-10-12 22:36:57 +02002354
2355 return _PyBytesWriter_Finish(&writer, p);
2356
2357error:
2358 _PyBytesWriter_Dealloc(&writer);
2359 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002360}
2361
2362static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002363write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002364{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002365 char header[9];
2366 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002367 PyObject *encoded = NULL;
2368 Py_ssize_t size;
2369 const char *data;
2370
2371 if (PyUnicode_READY(obj))
2372 return -1;
2373
2374 data = PyUnicode_AsUTF8AndSize(obj, &size);
2375 if (data == NULL) {
2376 /* Issue #8383: for strings with lone surrogates, fallback on the
2377 "surrogatepass" error handler. */
2378 PyErr_Clear();
2379 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2380 if (encoded == NULL)
2381 return -1;
2382
2383 data = PyBytes_AS_STRING(encoded);
2384 size = PyBytes_GET_SIZE(encoded);
2385 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002386
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002387 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002388 if (size <= 0xff && self->proto >= 4) {
2389 header[0] = SHORT_BINUNICODE;
2390 header[1] = (unsigned char)(size & 0xff);
2391 len = 2;
2392 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002393 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002394 header[0] = BINUNICODE;
2395 header[1] = (unsigned char)(size & 0xff);
2396 header[2] = (unsigned char)((size >> 8) & 0xff);
2397 header[3] = (unsigned char)((size >> 16) & 0xff);
2398 header[4] = (unsigned char)((size >> 24) & 0xff);
2399 len = 5;
2400 }
2401 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002402 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002403 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002404 len = 9;
2405 }
2406 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002407 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002408 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002409 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002410 return -1;
2411 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002412
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002413 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2414 Py_XDECREF(encoded);
2415 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002416 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002417 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002418 return 0;
2419}
2420
2421static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002422save_unicode(PicklerObject *self, PyObject *obj)
2423{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002424 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002425 if (write_unicode_binary(self, obj) < 0)
2426 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002427 }
2428 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002429 PyObject *encoded;
2430 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002431 const char unicode_op = UNICODE;
2432
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002433 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002434 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002435 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002436
Antoine Pitrou299978d2013-04-07 17:38:11 +02002437 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2438 Py_DECREF(encoded);
2439 return -1;
2440 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002441
2442 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002443 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2444 Py_DECREF(encoded);
2445 return -1;
2446 }
2447 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002448
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002449 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002450 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002451 }
2452 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002453 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002454
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002456}
2457
2458/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2459static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002460store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002461{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002462 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002463
2464 assert(PyTuple_Size(t) == len);
2465
2466 for (i = 0; i < len; i++) {
2467 PyObject *element = PyTuple_GET_ITEM(t, i);
2468
2469 if (element == NULL)
2470 return -1;
2471 if (save(self, element, 0) < 0)
2472 return -1;
2473 }
2474
2475 return 0;
2476}
2477
2478/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2479 * used across protocols to minimize the space needed to pickle them.
2480 * Tuples are also the only builtin immutable type that can be recursive
2481 * (a tuple can be reached from itself), and that requires some subtle
2482 * magic so that it works in all cases. IOW, this is a long routine.
2483 */
2484static int
2485save_tuple(PicklerObject *self, PyObject *obj)
2486{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002487 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002488
2489 const char mark_op = MARK;
2490 const char tuple_op = TUPLE;
2491 const char pop_op = POP;
2492 const char pop_mark_op = POP_MARK;
2493 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2494
2495 if ((len = PyTuple_Size(obj)) < 0)
2496 return -1;
2497
2498 if (len == 0) {
2499 char pdata[2];
2500
2501 if (self->proto) {
2502 pdata[0] = EMPTY_TUPLE;
2503 len = 1;
2504 }
2505 else {
2506 pdata[0] = MARK;
2507 pdata[1] = TUPLE;
2508 len = 2;
2509 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002510 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002511 return -1;
2512 return 0;
2513 }
2514
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002515 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002516 * saving the tuple elements, the tuple must be recursive, in
2517 * which case we'll pop everything we put on the stack, and fetch
2518 * its value from the memo.
2519 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002520 if (len <= 3 && self->proto >= 2) {
2521 /* Use TUPLE{1,2,3} opcodes. */
2522 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002523 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002524
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002525 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002526 /* pop the len elements */
2527 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002528 if (_Pickler_Write(self, &pop_op, 1) < 0)
2529 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002530 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002531 if (memo_get(self, obj) < 0)
2532 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002533
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002534 return 0;
2535 }
2536 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002537 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2538 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002539 }
2540 goto memoize;
2541 }
2542
2543 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2544 * Generate MARK e1 e2 ... TUPLE
2545 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002546 if (_Pickler_Write(self, &mark_op, 1) < 0)
2547 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002548
2549 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002550 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002551
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002552 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002553 /* pop the stack stuff we pushed */
2554 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002555 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2556 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002557 }
2558 else {
2559 /* Note that we pop one more than len, to remove
2560 * the MARK too.
2561 */
2562 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002563 if (_Pickler_Write(self, &pop_op, 1) < 0)
2564 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002565 }
2566 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002567 if (memo_get(self, obj) < 0)
2568 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002569
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002570 return 0;
2571 }
2572 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002573 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2574 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002575 }
2576
2577 memoize:
2578 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002579 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002580
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002581 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002582}
2583
2584/* iter is an iterator giving items, and we batch up chunks of
2585 * MARK item item ... item APPENDS
2586 * opcode sequences. Calling code should have arranged to first create an
2587 * empty list, or list-like object, for the APPENDS to operate on.
2588 * Returns 0 on success, <0 on error.
2589 */
2590static int
2591batch_list(PicklerObject *self, PyObject *iter)
2592{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002593 PyObject *obj = NULL;
2594 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002595 int i, n;
2596
2597 const char mark_op = MARK;
2598 const char append_op = APPEND;
2599 const char appends_op = APPENDS;
2600
2601 assert(iter != NULL);
2602
2603 /* XXX: I think this function could be made faster by avoiding the
2604 iterator interface and fetching objects directly from list using
2605 PyList_GET_ITEM.
2606 */
2607
2608 if (self->proto == 0) {
2609 /* APPENDS isn't available; do one at a time. */
2610 for (;;) {
2611 obj = PyIter_Next(iter);
2612 if (obj == NULL) {
2613 if (PyErr_Occurred())
2614 return -1;
2615 break;
2616 }
2617 i = save(self, obj, 0);
2618 Py_DECREF(obj);
2619 if (i < 0)
2620 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002621 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002622 return -1;
2623 }
2624 return 0;
2625 }
2626
2627 /* proto > 0: write in batches of BATCHSIZE. */
2628 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002629 /* Get first item */
2630 firstitem = PyIter_Next(iter);
2631 if (firstitem == NULL) {
2632 if (PyErr_Occurred())
2633 goto error;
2634
2635 /* nothing more to add */
2636 break;
2637 }
2638
2639 /* Try to get a second item */
2640 obj = PyIter_Next(iter);
2641 if (obj == NULL) {
2642 if (PyErr_Occurred())
2643 goto error;
2644
2645 /* Only one item to write */
2646 if (save(self, firstitem, 0) < 0)
2647 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002648 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002649 goto error;
2650 Py_CLEAR(firstitem);
2651 break;
2652 }
2653
2654 /* More than one item to write */
2655
2656 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002657 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002658 goto error;
2659
2660 if (save(self, firstitem, 0) < 0)
2661 goto error;
2662 Py_CLEAR(firstitem);
2663 n = 1;
2664
2665 /* Fetch and save up to BATCHSIZE items */
2666 while (obj) {
2667 if (save(self, obj, 0) < 0)
2668 goto error;
2669 Py_CLEAR(obj);
2670 n += 1;
2671
2672 if (n == BATCHSIZE)
2673 break;
2674
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002675 obj = PyIter_Next(iter);
2676 if (obj == NULL) {
2677 if (PyErr_Occurred())
2678 goto error;
2679 break;
2680 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002681 }
2682
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002683 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002684 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002685
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002686 } while (n == BATCHSIZE);
2687 return 0;
2688
2689 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002690 Py_XDECREF(firstitem);
2691 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002692 return -1;
2693}
2694
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002695/* This is a variant of batch_list() above, specialized for lists (with no
2696 * support for list subclasses). Like batch_list(), we batch up chunks of
2697 * MARK item item ... item APPENDS
2698 * opcode sequences. Calling code should have arranged to first create an
2699 * empty list, or list-like object, for the APPENDS to operate on.
2700 * Returns 0 on success, -1 on error.
2701 *
2702 * This version is considerably faster than batch_list(), if less general.
2703 *
2704 * Note that this only works for protocols > 0.
2705 */
2706static int
2707batch_list_exact(PicklerObject *self, PyObject *obj)
2708{
2709 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002710 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002711
2712 const char append_op = APPEND;
2713 const char appends_op = APPENDS;
2714 const char mark_op = MARK;
2715
2716 assert(obj != NULL);
2717 assert(self->proto > 0);
2718 assert(PyList_CheckExact(obj));
2719
2720 if (PyList_GET_SIZE(obj) == 1) {
2721 item = PyList_GET_ITEM(obj, 0);
2722 if (save(self, item, 0) < 0)
2723 return -1;
2724 if (_Pickler_Write(self, &append_op, 1) < 0)
2725 return -1;
2726 return 0;
2727 }
2728
2729 /* Write in batches of BATCHSIZE. */
2730 total = 0;
2731 do {
2732 this_batch = 0;
2733 if (_Pickler_Write(self, &mark_op, 1) < 0)
2734 return -1;
2735 while (total < PyList_GET_SIZE(obj)) {
2736 item = PyList_GET_ITEM(obj, total);
2737 if (save(self, item, 0) < 0)
2738 return -1;
2739 total++;
2740 if (++this_batch == BATCHSIZE)
2741 break;
2742 }
2743 if (_Pickler_Write(self, &appends_op, 1) < 0)
2744 return -1;
2745
2746 } while (total < PyList_GET_SIZE(obj));
2747
2748 return 0;
2749}
2750
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002751static int
2752save_list(PicklerObject *self, PyObject *obj)
2753{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002754 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002755 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002756 int status = 0;
2757
2758 if (self->fast && !fast_save_enter(self, obj))
2759 goto error;
2760
2761 /* Create an empty list. */
2762 if (self->bin) {
2763 header[0] = EMPTY_LIST;
2764 len = 1;
2765 }
2766 else {
2767 header[0] = MARK;
2768 header[1] = LIST;
2769 len = 2;
2770 }
2771
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002772 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002773 goto error;
2774
2775 /* Get list length, and bow out early if empty. */
2776 if ((len = PyList_Size(obj)) < 0)
2777 goto error;
2778
2779 if (memo_put(self, obj) < 0)
2780 goto error;
2781
2782 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002783 /* Materialize the list elements. */
2784 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002785 if (Py_EnterRecursiveCall(" while pickling an object"))
2786 goto error;
2787 status = batch_list_exact(self, obj);
2788 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002789 } else {
2790 PyObject *iter = PyObject_GetIter(obj);
2791 if (iter == NULL)
2792 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002793
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002794 if (Py_EnterRecursiveCall(" while pickling an object")) {
2795 Py_DECREF(iter);
2796 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002797 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002798 status = batch_list(self, iter);
2799 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002800 Py_DECREF(iter);
2801 }
2802 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002803 if (0) {
2804 error:
2805 status = -1;
2806 }
2807
2808 if (self->fast && !fast_save_leave(self, obj))
2809 status = -1;
2810
2811 return status;
2812}
2813
2814/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2815 * MARK key value ... key value SETITEMS
2816 * opcode sequences. Calling code should have arranged to first create an
2817 * empty dict, or dict-like object, for the SETITEMS to operate on.
2818 * Returns 0 on success, <0 on error.
2819 *
2820 * This is very much like batch_list(). The difference between saving
2821 * elements directly, and picking apart two-tuples, is so long-winded at
2822 * the C level, though, that attempts to combine these routines were too
2823 * ugly to bear.
2824 */
2825static int
2826batch_dict(PicklerObject *self, PyObject *iter)
2827{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002828 PyObject *obj = NULL;
2829 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002830 int i, n;
2831
2832 const char mark_op = MARK;
2833 const char setitem_op = SETITEM;
2834 const char setitems_op = SETITEMS;
2835
2836 assert(iter != NULL);
2837
2838 if (self->proto == 0) {
2839 /* SETITEMS isn't available; do one at a time. */
2840 for (;;) {
2841 obj = PyIter_Next(iter);
2842 if (obj == NULL) {
2843 if (PyErr_Occurred())
2844 return -1;
2845 break;
2846 }
2847 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2848 PyErr_SetString(PyExc_TypeError, "dict items "
2849 "iterator must return 2-tuples");
2850 return -1;
2851 }
2852 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2853 if (i >= 0)
2854 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2855 Py_DECREF(obj);
2856 if (i < 0)
2857 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002858 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002859 return -1;
2860 }
2861 return 0;
2862 }
2863
2864 /* proto > 0: write in batches of BATCHSIZE. */
2865 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002866 /* Get first item */
2867 firstitem = PyIter_Next(iter);
2868 if (firstitem == NULL) {
2869 if (PyErr_Occurred())
2870 goto error;
2871
2872 /* nothing more to add */
2873 break;
2874 }
2875 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2876 PyErr_SetString(PyExc_TypeError, "dict items "
2877 "iterator must return 2-tuples");
2878 goto error;
2879 }
2880
2881 /* Try to get a second item */
2882 obj = PyIter_Next(iter);
2883 if (obj == NULL) {
2884 if (PyErr_Occurred())
2885 goto error;
2886
2887 /* Only one item to write */
2888 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2889 goto error;
2890 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2891 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002892 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002893 goto error;
2894 Py_CLEAR(firstitem);
2895 break;
2896 }
2897
2898 /* More than one item to write */
2899
2900 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002901 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002902 goto error;
2903
2904 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2905 goto error;
2906 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2907 goto error;
2908 Py_CLEAR(firstitem);
2909 n = 1;
2910
2911 /* Fetch and save up to BATCHSIZE items */
2912 while (obj) {
2913 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2914 PyErr_SetString(PyExc_TypeError, "dict items "
2915 "iterator must return 2-tuples");
2916 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002917 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002918 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2919 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2920 goto error;
2921 Py_CLEAR(obj);
2922 n += 1;
2923
2924 if (n == BATCHSIZE)
2925 break;
2926
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002927 obj = PyIter_Next(iter);
2928 if (obj == NULL) {
2929 if (PyErr_Occurred())
2930 goto error;
2931 break;
2932 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002933 }
2934
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002935 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002936 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002937
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002938 } while (n == BATCHSIZE);
2939 return 0;
2940
2941 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002942 Py_XDECREF(firstitem);
2943 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002944 return -1;
2945}
2946
Collin Winter5c9b02d2009-05-25 05:43:30 +00002947/* This is a variant of batch_dict() above that specializes for dicts, with no
2948 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2949 * MARK key value ... key value SETITEMS
2950 * opcode sequences. Calling code should have arranged to first create an
2951 * empty dict, or dict-like object, for the SETITEMS to operate on.
2952 * Returns 0 on success, -1 on error.
2953 *
2954 * Note that this currently doesn't work for protocol 0.
2955 */
2956static int
2957batch_dict_exact(PicklerObject *self, PyObject *obj)
2958{
2959 PyObject *key = NULL, *value = NULL;
2960 int i;
2961 Py_ssize_t dict_size, ppos = 0;
2962
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002963 const char mark_op = MARK;
2964 const char setitem_op = SETITEM;
2965 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002966
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002967 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002968 assert(self->proto > 0);
2969
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002970 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002971
2972 /* Special-case len(d) == 1 to save space. */
2973 if (dict_size == 1) {
2974 PyDict_Next(obj, &ppos, &key, &value);
2975 if (save(self, key, 0) < 0)
2976 return -1;
2977 if (save(self, value, 0) < 0)
2978 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002979 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002980 return -1;
2981 return 0;
2982 }
2983
2984 /* Write in batches of BATCHSIZE. */
2985 do {
2986 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002987 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002988 return -1;
2989 while (PyDict_Next(obj, &ppos, &key, &value)) {
2990 if (save(self, key, 0) < 0)
2991 return -1;
2992 if (save(self, value, 0) < 0)
2993 return -1;
2994 if (++i == BATCHSIZE)
2995 break;
2996 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002997 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002998 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002999 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003000 PyErr_Format(
3001 PyExc_RuntimeError,
3002 "dictionary changed size during iteration");
3003 return -1;
3004 }
3005
3006 } while (i == BATCHSIZE);
3007 return 0;
3008}
3009
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003010static int
3011save_dict(PicklerObject *self, PyObject *obj)
3012{
3013 PyObject *items, *iter;
3014 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003015 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003016 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003017 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003018
3019 if (self->fast && !fast_save_enter(self, obj))
3020 goto error;
3021
3022 /* Create an empty dict. */
3023 if (self->bin) {
3024 header[0] = EMPTY_DICT;
3025 len = 1;
3026 }
3027 else {
3028 header[0] = MARK;
3029 header[1] = DICT;
3030 len = 2;
3031 }
3032
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003033 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003034 goto error;
3035
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003036 if (memo_put(self, obj) < 0)
3037 goto error;
3038
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003039 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003040 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003041 if (PyDict_CheckExact(obj) && self->proto > 0) {
3042 /* We can take certain shortcuts if we know this is a dict and
3043 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003044 if (Py_EnterRecursiveCall(" while pickling an object"))
3045 goto error;
3046 status = batch_dict_exact(self, obj);
3047 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003048 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003049 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003050
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003051 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003052 if (items == NULL)
3053 goto error;
3054 iter = PyObject_GetIter(items);
3055 Py_DECREF(items);
3056 if (iter == NULL)
3057 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003058 if (Py_EnterRecursiveCall(" while pickling an object")) {
3059 Py_DECREF(iter);
3060 goto error;
3061 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003062 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003063 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003064 Py_DECREF(iter);
3065 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003066 }
3067
3068 if (0) {
3069 error:
3070 status = -1;
3071 }
3072
3073 if (self->fast && !fast_save_leave(self, obj))
3074 status = -1;
3075
3076 return status;
3077}
3078
3079static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003080save_set(PicklerObject *self, PyObject *obj)
3081{
3082 PyObject *item;
3083 int i;
3084 Py_ssize_t set_size, ppos = 0;
3085 Py_hash_t hash;
3086
3087 const char empty_set_op = EMPTY_SET;
3088 const char mark_op = MARK;
3089 const char additems_op = ADDITEMS;
3090
3091 if (self->proto < 4) {
3092 PyObject *items;
3093 PyObject *reduce_value;
3094 int status;
3095
3096 items = PySequence_List(obj);
3097 if (items == NULL) {
3098 return -1;
3099 }
3100 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3101 Py_DECREF(items);
3102 if (reduce_value == NULL) {
3103 return -1;
3104 }
3105 /* save_reduce() will memoize the object automatically. */
3106 status = save_reduce(self, reduce_value, obj);
3107 Py_DECREF(reduce_value);
3108 return status;
3109 }
3110
3111 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3112 return -1;
3113
3114 if (memo_put(self, obj) < 0)
3115 return -1;
3116
3117 set_size = PySet_GET_SIZE(obj);
3118 if (set_size == 0)
3119 return 0; /* nothing to do */
3120
3121 /* Write in batches of BATCHSIZE. */
3122 do {
3123 i = 0;
3124 if (_Pickler_Write(self, &mark_op, 1) < 0)
3125 return -1;
3126 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3127 if (save(self, item, 0) < 0)
3128 return -1;
3129 if (++i == BATCHSIZE)
3130 break;
3131 }
3132 if (_Pickler_Write(self, &additems_op, 1) < 0)
3133 return -1;
3134 if (PySet_GET_SIZE(obj) != set_size) {
3135 PyErr_Format(
3136 PyExc_RuntimeError,
3137 "set changed size during iteration");
3138 return -1;
3139 }
3140 } while (i == BATCHSIZE);
3141
3142 return 0;
3143}
3144
3145static int
3146save_frozenset(PicklerObject *self, PyObject *obj)
3147{
3148 PyObject *iter;
3149
3150 const char mark_op = MARK;
3151 const char frozenset_op = FROZENSET;
3152
3153 if (self->fast && !fast_save_enter(self, obj))
3154 return -1;
3155
3156 if (self->proto < 4) {
3157 PyObject *items;
3158 PyObject *reduce_value;
3159 int status;
3160
3161 items = PySequence_List(obj);
3162 if (items == NULL) {
3163 return -1;
3164 }
3165 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3166 items);
3167 Py_DECREF(items);
3168 if (reduce_value == NULL) {
3169 return -1;
3170 }
3171 /* save_reduce() will memoize the object automatically. */
3172 status = save_reduce(self, reduce_value, obj);
3173 Py_DECREF(reduce_value);
3174 return status;
3175 }
3176
3177 if (_Pickler_Write(self, &mark_op, 1) < 0)
3178 return -1;
3179
3180 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003181 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003182 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003183 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003184 for (;;) {
3185 PyObject *item;
3186
3187 item = PyIter_Next(iter);
3188 if (item == NULL) {
3189 if (PyErr_Occurred()) {
3190 Py_DECREF(iter);
3191 return -1;
3192 }
3193 break;
3194 }
3195 if (save(self, item, 0) < 0) {
3196 Py_DECREF(item);
3197 Py_DECREF(iter);
3198 return -1;
3199 }
3200 Py_DECREF(item);
3201 }
3202 Py_DECREF(iter);
3203
3204 /* If the object is already in the memo, this means it is
3205 recursive. In this case, throw away everything we put on the
3206 stack, and fetch the object back from the memo. */
3207 if (PyMemoTable_Get(self->memo, obj)) {
3208 const char pop_mark_op = POP_MARK;
3209
3210 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3211 return -1;
3212 if (memo_get(self, obj) < 0)
3213 return -1;
3214 return 0;
3215 }
3216
3217 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3218 return -1;
3219 if (memo_put(self, obj) < 0)
3220 return -1;
3221
3222 return 0;
3223}
3224
3225static int
3226fix_imports(PyObject **module_name, PyObject **global_name)
3227{
3228 PyObject *key;
3229 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003230 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003231
3232 key = PyTuple_Pack(2, *module_name, *global_name);
3233 if (key == NULL)
3234 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003235 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003236 Py_DECREF(key);
3237 if (item) {
3238 PyObject *fixed_module_name;
3239 PyObject *fixed_global_name;
3240
3241 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3242 PyErr_Format(PyExc_RuntimeError,
3243 "_compat_pickle.REVERSE_NAME_MAPPING values "
3244 "should be 2-tuples, not %.200s",
3245 Py_TYPE(item)->tp_name);
3246 return -1;
3247 }
3248 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3249 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3250 if (!PyUnicode_Check(fixed_module_name) ||
3251 !PyUnicode_Check(fixed_global_name)) {
3252 PyErr_Format(PyExc_RuntimeError,
3253 "_compat_pickle.REVERSE_NAME_MAPPING values "
3254 "should be pairs of str, not (%.200s, %.200s)",
3255 Py_TYPE(fixed_module_name)->tp_name,
3256 Py_TYPE(fixed_global_name)->tp_name);
3257 return -1;
3258 }
3259
3260 Py_CLEAR(*module_name);
3261 Py_CLEAR(*global_name);
3262 Py_INCREF(fixed_module_name);
3263 Py_INCREF(fixed_global_name);
3264 *module_name = fixed_module_name;
3265 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003266 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003267 }
3268 else if (PyErr_Occurred()) {
3269 return -1;
3270 }
3271
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003272 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003273 if (item) {
3274 if (!PyUnicode_Check(item)) {
3275 PyErr_Format(PyExc_RuntimeError,
3276 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3277 "should be strings, not %.200s",
3278 Py_TYPE(item)->tp_name);
3279 return -1;
3280 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003281 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003282 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003283 }
3284 else if (PyErr_Occurred()) {
3285 return -1;
3286 }
3287
3288 return 0;
3289}
3290
3291static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003292save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3293{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003294 PyObject *global_name = NULL;
3295 PyObject *module_name = NULL;
3296 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003297 PyObject *parent = NULL;
3298 PyObject *dotted_path = NULL;
3299 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003300 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003301 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003302 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003303 _Py_IDENTIFIER(__name__);
3304 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003305
3306 const char global_op = GLOBAL;
3307
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003308 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003309 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003310 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003311 }
3312 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003313 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3314 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003315 if (global_name == NULL) {
3316 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3317 if (global_name == NULL)
3318 goto error;
3319 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003320 }
3321
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003322 dotted_path = get_dotted_path(module, global_name);
3323 if (dotted_path == NULL)
3324 goto error;
3325 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003326 if (module_name == NULL)
3327 goto error;
3328
3329 /* XXX: Change to use the import C API directly with level=0 to disallow
3330 relative imports.
3331
3332 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3333 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3334 custom import functions (IMHO, this would be a nice security
3335 feature). The import C API would need to be extended to support the
3336 extra parameters of __import__ to fix that. */
3337 module = PyImport_Import(module_name);
3338 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003339 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003340 "Can't pickle %R: import of module %R failed",
3341 obj, module_name);
3342 goto error;
3343 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003344 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3345 Py_INCREF(lastname);
3346 cls = get_deep_attribute(module, dotted_path, &parent);
3347 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003348 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003349 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003350 "Can't pickle %R: attribute lookup %S on %S failed",
3351 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003352 goto error;
3353 }
3354 if (cls != obj) {
3355 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003356 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003357 "Can't pickle %R: it's not the same object as %S.%S",
3358 obj, module_name, global_name);
3359 goto error;
3360 }
3361 Py_DECREF(cls);
3362
3363 if (self->proto >= 2) {
3364 /* See whether this is in the extension registry, and if
3365 * so generate an EXT opcode.
3366 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003367 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003368 PyObject *code_obj; /* extension code as Python object */
3369 long code; /* extension code as C value */
3370 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003371 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003372
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003373 extension_key = PyTuple_Pack(2, module_name, global_name);
3374 if (extension_key == NULL) {
3375 goto error;
3376 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003377 code_obj = PyDict_GetItemWithError(st->extension_registry,
3378 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003379 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003380 /* The object is not registered in the extension registry.
3381 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003382 if (code_obj == NULL) {
3383 if (PyErr_Occurred()) {
3384 goto error;
3385 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003386 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003387 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003388
3389 /* XXX: pickle.py doesn't check neither the type, nor the range
3390 of the value returned by the extension_registry. It should for
3391 consistency. */
3392
3393 /* Verify code_obj has the right type and value. */
3394 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003395 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003396 "Can't pickle %R: extension code %R isn't an integer",
3397 obj, code_obj);
3398 goto error;
3399 }
3400 code = PyLong_AS_LONG(code_obj);
3401 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003402 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003403 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3404 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003405 goto error;
3406 }
3407
3408 /* Generate an EXT opcode. */
3409 if (code <= 0xff) {
3410 pdata[0] = EXT1;
3411 pdata[1] = (unsigned char)code;
3412 n = 2;
3413 }
3414 else if (code <= 0xffff) {
3415 pdata[0] = EXT2;
3416 pdata[1] = (unsigned char)(code & 0xff);
3417 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3418 n = 3;
3419 }
3420 else {
3421 pdata[0] = EXT4;
3422 pdata[1] = (unsigned char)(code & 0xff);
3423 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3424 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3425 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3426 n = 5;
3427 }
3428
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003429 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003430 goto error;
3431 }
3432 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003433 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003434 if (parent == module) {
3435 Py_INCREF(lastname);
3436 Py_DECREF(global_name);
3437 global_name = lastname;
3438 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003439 if (self->proto >= 4) {
3440 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003441
Christian Heimese8b1ba12013-11-23 21:13:39 +01003442 if (save(self, module_name, 0) < 0)
3443 goto error;
3444 if (save(self, global_name, 0) < 0)
3445 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003446
3447 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3448 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003449 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003450 else if (parent != module) {
3451 PickleState *st = _Pickle_GetGlobalState();
3452 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3453 st->getattr, parent, lastname);
Miss Islington (bot)3152bc32018-08-22 01:54:33 -04003454 if (reduce_value == NULL)
3455 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003456 status = save_reduce(self, reduce_value, NULL);
3457 Py_DECREF(reduce_value);
3458 if (status < 0)
3459 goto error;
3460 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003461 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003462 /* Generate a normal global opcode if we are using a pickle
3463 protocol < 4, or if the object is not registered in the
3464 extension registry. */
3465 PyObject *encoded;
3466 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003467
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003468 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003469 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003470
3471 /* For protocol < 3 and if the user didn't request against doing
3472 so, we convert module names to the old 2.x module names. */
3473 if (self->proto < 3 && self->fix_imports) {
3474 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003475 goto error;
3476 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003477 }
3478
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003479 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3480 both the module name and the global name using UTF-8. We do so
3481 only when we are using the pickle protocol newer than version
3482 3. This is to ensure compatibility with older Unpickler running
3483 on Python 2.x. */
3484 if (self->proto == 3) {
3485 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003486 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003487 else {
3488 unicode_encoder = PyUnicode_AsASCIIString;
3489 }
3490 encoded = unicode_encoder(module_name);
3491 if (encoded == NULL) {
3492 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003493 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003494 "can't pickle module identifier '%S' using "
3495 "pickle protocol %i",
3496 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003497 goto error;
3498 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003499 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3500 PyBytes_GET_SIZE(encoded)) < 0) {
3501 Py_DECREF(encoded);
3502 goto error;
3503 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003504 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003505 if(_Pickler_Write(self, "\n", 1) < 0)
3506 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003507
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003508 /* Save the name of the module. */
3509 encoded = unicode_encoder(global_name);
3510 if (encoded == NULL) {
3511 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003512 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003513 "can't pickle global identifier '%S' using "
3514 "pickle protocol %i",
3515 global_name, self->proto);
3516 goto error;
3517 }
3518 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3519 PyBytes_GET_SIZE(encoded)) < 0) {
3520 Py_DECREF(encoded);
3521 goto error;
3522 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003523 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003524 if (_Pickler_Write(self, "\n", 1) < 0)
3525 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003526 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003527 /* Memoize the object. */
3528 if (memo_put(self, obj) < 0)
3529 goto error;
3530 }
3531
3532 if (0) {
3533 error:
3534 status = -1;
3535 }
3536 Py_XDECREF(module_name);
3537 Py_XDECREF(global_name);
3538 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003539 Py_XDECREF(parent);
3540 Py_XDECREF(dotted_path);
3541 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003542
3543 return status;
3544}
3545
3546static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003547save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3548{
3549 PyObject *reduce_value;
3550 int status;
3551
3552 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3553 if (reduce_value == NULL) {
3554 return -1;
3555 }
3556 status = save_reduce(self, reduce_value, obj);
3557 Py_DECREF(reduce_value);
3558 return status;
3559}
3560
3561static int
3562save_type(PicklerObject *self, PyObject *obj)
3563{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003564 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003565 return save_singleton_type(self, obj, Py_None);
3566 }
3567 else if (obj == (PyObject *)&PyEllipsis_Type) {
3568 return save_singleton_type(self, obj, Py_Ellipsis);
3569 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003570 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003571 return save_singleton_type(self, obj, Py_NotImplemented);
3572 }
3573 return save_global(self, obj, NULL);
3574}
3575
3576static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003577save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003578{
3579 PyObject *pid = NULL;
3580 int status = 0;
3581
3582 const char persid_op = PERSID;
3583 const char binpersid_op = BINPERSID;
3584
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003585 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003586 if (pid == NULL)
3587 return -1;
3588
3589 if (pid != Py_None) {
3590 if (self->bin) {
3591 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003592 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003593 goto error;
3594 }
3595 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003596 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003597
3598 pid_str = PyObject_Str(pid);
3599 if (pid_str == NULL)
3600 goto error;
3601
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003602 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003603 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003604 if (!PyUnicode_IS_ASCII(pid_str)) {
3605 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3606 "persistent IDs in protocol 0 must be "
3607 "ASCII strings");
3608 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003609 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003610 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003611
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003612 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003613 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3614 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3615 _Pickler_Write(self, "\n", 1) < 0) {
3616 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003617 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003618 }
3619 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003620 }
3621 status = 1;
3622 }
3623
3624 if (0) {
3625 error:
3626 status = -1;
3627 }
3628 Py_XDECREF(pid);
3629
3630 return status;
3631}
3632
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003633static PyObject *
3634get_class(PyObject *obj)
3635{
3636 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003637 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003638
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003639 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3640 cls = (PyObject *) Py_TYPE(obj);
3641 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003642 }
3643 return cls;
3644}
3645
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003646/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3647 * appropriate __reduce__ method for obj.
3648 */
3649static int
3650save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3651{
3652 PyObject *callable;
3653 PyObject *argtup;
3654 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003655 PyObject *listitems = Py_None;
3656 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003657 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003658 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003659 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003660
3661 const char reduce_op = REDUCE;
3662 const char build_op = BUILD;
3663 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003664 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003665
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003666 size = PyTuple_Size(args);
3667 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003668 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003669 "__reduce__ must contain 2 through 5 elements");
3670 return -1;
3671 }
3672
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003673 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3674 &callable, &argtup, &state, &listitems, &dictitems))
3675 return -1;
3676
3677 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003678 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003679 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003680 return -1;
3681 }
3682 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003683 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003684 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003685 return -1;
3686 }
3687
3688 if (state == Py_None)
3689 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003690
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003691 if (listitems == Py_None)
3692 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003693 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003694 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003695 "returned by __reduce__ must be an iterator, not %s",
3696 Py_TYPE(listitems)->tp_name);
3697 return -1;
3698 }
3699
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003700 if (dictitems == Py_None)
3701 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003702 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003703 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003704 "returned by __reduce__ must be an iterator, not %s",
3705 Py_TYPE(dictitems)->tp_name);
3706 return -1;
3707 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003708
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003709 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003710 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003711 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003712
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003713 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3714 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003715 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003716 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003717 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003718 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3719 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003720 if (!use_newobj_ex) {
3721 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003722 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003723 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003724 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003725 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003726 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003727
3728 if (use_newobj_ex) {
3729 PyObject *cls;
3730 PyObject *args;
3731 PyObject *kwargs;
3732
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003733 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003734 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003735 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003736 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003737 return -1;
3738 }
3739
3740 cls = PyTuple_GET_ITEM(argtup, 0);
3741 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003742 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003743 "first item from NEWOBJ_EX argument tuple must "
3744 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3745 return -1;
3746 }
3747 args = PyTuple_GET_ITEM(argtup, 1);
3748 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003749 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003750 "second item from NEWOBJ_EX argument tuple must "
3751 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3752 return -1;
3753 }
3754 kwargs = PyTuple_GET_ITEM(argtup, 2);
3755 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003756 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003757 "third item from NEWOBJ_EX argument tuple must "
3758 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3759 return -1;
3760 }
3761
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003762 if (self->proto >= 4) {
3763 if (save(self, cls, 0) < 0 ||
3764 save(self, args, 0) < 0 ||
3765 save(self, kwargs, 0) < 0 ||
3766 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3767 return -1;
3768 }
3769 }
3770 else {
3771 PyObject *newargs;
3772 PyObject *cls_new;
3773 Py_ssize_t i;
3774 _Py_IDENTIFIER(__new__);
3775
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003776 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003777 if (newargs == NULL)
3778 return -1;
3779
3780 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3781 if (cls_new == NULL) {
3782 Py_DECREF(newargs);
3783 return -1;
3784 }
3785 PyTuple_SET_ITEM(newargs, 0, cls_new);
3786 Py_INCREF(cls);
3787 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003788 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003789 PyObject *item = PyTuple_GET_ITEM(args, i);
3790 Py_INCREF(item);
3791 PyTuple_SET_ITEM(newargs, i + 2, item);
3792 }
3793
3794 callable = PyObject_Call(st->partial, newargs, kwargs);
3795 Py_DECREF(newargs);
3796 if (callable == NULL)
3797 return -1;
3798
3799 newargs = PyTuple_New(0);
3800 if (newargs == NULL) {
3801 Py_DECREF(callable);
3802 return -1;
3803 }
3804
3805 if (save(self, callable, 0) < 0 ||
3806 save(self, newargs, 0) < 0 ||
3807 _Pickler_Write(self, &reduce_op, 1) < 0) {
3808 Py_DECREF(newargs);
3809 Py_DECREF(callable);
3810 return -1;
3811 }
3812 Py_DECREF(newargs);
3813 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003814 }
3815 }
3816 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003817 PyObject *cls;
3818 PyObject *newargtup;
3819 PyObject *obj_class;
3820 int p;
3821
3822 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003823 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003824 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003825 return -1;
3826 }
3827
3828 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003829 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003830 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003831 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003832 return -1;
3833 }
3834
3835 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003836 obj_class = get_class(obj);
Miss Islington (bot)e2f376f2018-12-05 11:35:41 -08003837 if (obj_class == NULL) {
3838 return -1;
3839 }
3840 p = obj_class != cls;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003841 Py_DECREF(obj_class);
3842 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003843 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003844 "__newobj__ args has the wrong class");
3845 return -1;
3846 }
3847 }
3848 /* XXX: These calls save() are prone to infinite recursion. Imagine
3849 what happen if the value returned by the __reduce__() method of
3850 some extension type contains another object of the same type. Ouch!
3851
3852 Here is a quick example, that I ran into, to illustrate what I
3853 mean:
3854
3855 >>> import pickle, copyreg
3856 >>> copyreg.dispatch_table.pop(complex)
3857 >>> pickle.dumps(1+2j)
3858 Traceback (most recent call last):
3859 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003860 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003861
3862 Removing the complex class from copyreg.dispatch_table made the
3863 __reduce_ex__() method emit another complex object:
3864
3865 >>> (1+1j).__reduce_ex__(2)
3866 (<function __newobj__ at 0xb7b71c3c>,
3867 (<class 'complex'>, (1+1j)), None, None, None)
3868
3869 Thus when save() was called on newargstup (the 2nd item) recursion
3870 ensued. Of course, the bug was in the complex class which had a
3871 broken __getnewargs__() that emitted another complex object. But,
3872 the point, here, is it is quite easy to end up with a broken reduce
3873 function. */
3874
3875 /* Save the class and its __new__ arguments. */
3876 if (save(self, cls, 0) < 0)
3877 return -1;
3878
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003879 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003880 if (newargtup == NULL)
3881 return -1;
3882
3883 p = save(self, newargtup, 0);
3884 Py_DECREF(newargtup);
3885 if (p < 0)
3886 return -1;
3887
3888 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003889 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003890 return -1;
3891 }
3892 else { /* Not using NEWOBJ. */
3893 if (save(self, callable, 0) < 0 ||
3894 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003895 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003896 return -1;
3897 }
3898
3899 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3900 the caller do not want to memoize the object. Not particularly useful,
3901 but that is to mimic the behavior save_reduce() in pickle.py when
3902 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003903 if (obj != NULL) {
3904 /* If the object is already in the memo, this means it is
3905 recursive. In this case, throw away everything we put on the
3906 stack, and fetch the object back from the memo. */
3907 if (PyMemoTable_Get(self->memo, obj)) {
3908 const char pop_op = POP;
3909
3910 if (_Pickler_Write(self, &pop_op, 1) < 0)
3911 return -1;
3912 if (memo_get(self, obj) < 0)
3913 return -1;
3914
3915 return 0;
3916 }
3917 else if (memo_put(self, obj) < 0)
3918 return -1;
3919 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003920
3921 if (listitems && batch_list(self, listitems) < 0)
3922 return -1;
3923
3924 if (dictitems && batch_dict(self, dictitems) < 0)
3925 return -1;
3926
3927 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003928 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003929 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003930 return -1;
3931 }
3932
3933 return 0;
3934}
3935
3936static int
3937save(PicklerObject *self, PyObject *obj, int pers_save)
3938{
3939 PyTypeObject *type;
3940 PyObject *reduce_func = NULL;
3941 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003942 int status = 0;
3943
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003944 if (_Pickler_OpcodeBoundary(self) < 0)
3945 return -1;
3946
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003947 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003948 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003949
3950 /* The extra pers_save argument is necessary to avoid calling save_pers()
3951 on its returned object. */
3952 if (!pers_save && self->pers_func) {
3953 /* save_pers() returns:
3954 -1 to signal an error;
3955 0 if it did nothing successfully;
3956 1 if a persistent id was saved.
3957 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003958 if ((status = save_pers(self, obj)) != 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003959 goto done;
3960 }
3961
3962 type = Py_TYPE(obj);
3963
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003964 /* The old cPickle had an optimization that used switch-case statement
3965 dispatching on the first letter of the type name. This has was removed
3966 since benchmarks shown that this optimization was actually slowing
3967 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003968
3969 /* Atom types; these aren't memoized, so don't check the memo. */
3970
3971 if (obj == Py_None) {
3972 status = save_none(self, obj);
3973 goto done;
3974 }
3975 else if (obj == Py_False || obj == Py_True) {
3976 status = save_bool(self, obj);
3977 goto done;
3978 }
3979 else if (type == &PyLong_Type) {
3980 status = save_long(self, obj);
3981 goto done;
3982 }
3983 else if (type == &PyFloat_Type) {
3984 status = save_float(self, obj);
3985 goto done;
3986 }
3987
3988 /* Check the memo to see if it has the object. If so, generate
3989 a GET (or BINGET) opcode, instead of pickling the object
3990 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003991 if (PyMemoTable_Get(self->memo, obj)) {
3992 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003993 goto error;
3994 goto done;
3995 }
3996
3997 if (type == &PyBytes_Type) {
3998 status = save_bytes(self, obj);
3999 goto done;
4000 }
4001 else if (type == &PyUnicode_Type) {
4002 status = save_unicode(self, obj);
4003 goto done;
4004 }
4005 else if (type == &PyDict_Type) {
4006 status = save_dict(self, obj);
4007 goto done;
4008 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004009 else if (type == &PySet_Type) {
4010 status = save_set(self, obj);
4011 goto done;
4012 }
4013 else if (type == &PyFrozenSet_Type) {
4014 status = save_frozenset(self, obj);
4015 goto done;
4016 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004017 else if (type == &PyList_Type) {
4018 status = save_list(self, obj);
4019 goto done;
4020 }
4021 else if (type == &PyTuple_Type) {
4022 status = save_tuple(self, obj);
4023 goto done;
4024 }
4025 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004026 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004027 goto done;
4028 }
4029 else if (type == &PyFunction_Type) {
4030 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004031 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004032 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004033
4034 /* XXX: This part needs some unit tests. */
4035
4036 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004037 * self.dispatch_table, copyreg.dispatch_table, the object's
4038 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004039 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004040 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004041 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004042 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4043 (PyObject *)type);
4044 if (reduce_func == NULL) {
4045 if (PyErr_Occurred()) {
4046 goto error;
4047 }
4048 } else {
4049 /* PyDict_GetItemWithError() returns a borrowed reference.
4050 Increase the reference count to be consistent with
4051 PyObject_GetItem and _PyObject_GetAttrId used below. */
4052 Py_INCREF(reduce_func);
4053 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004054 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004055 reduce_func = PyObject_GetItem(self->dispatch_table,
4056 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004057 if (reduce_func == NULL) {
4058 if (PyErr_ExceptionMatches(PyExc_KeyError))
4059 PyErr_Clear();
4060 else
4061 goto error;
4062 }
4063 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004064 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004065 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004066 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004067 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004068 else if (PyType_IsSubtype(type, &PyType_Type)) {
4069 status = save_global(self, obj, NULL);
4070 goto done;
4071 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004072 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004073 _Py_IDENTIFIER(__reduce__);
4074 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004075
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004076
4077 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4078 automatically defined as __reduce__. While this is convenient, this
4079 make it impossible to know which method was actually called. Of
4080 course, this is not a big deal. But still, it would be nice to let
4081 the user know which method was called when something go
4082 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4083 don't actually have to check for a __reduce__ method. */
4084
4085 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004086 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4087 goto error;
4088 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004089 if (reduce_func != NULL) {
4090 PyObject *proto;
4091 proto = PyLong_FromLong(self->proto);
4092 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004093 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004094 }
4095 }
4096 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004097 PickleState *st = _Pickle_GetGlobalState();
4098
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004100 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004101 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004102 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004103 }
4104 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004105 PyErr_Format(st->PicklingError,
4106 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004107 type->tp_name, obj);
4108 goto error;
4109 }
4110 }
4111 }
4112
4113 if (reduce_value == NULL)
4114 goto error;
4115
4116 if (PyUnicode_Check(reduce_value)) {
4117 status = save_global(self, obj, reduce_value);
4118 goto done;
4119 }
4120
4121 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004122 PickleState *st = _Pickle_GetGlobalState();
4123 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004124 "__reduce__ must return a string or tuple");
4125 goto error;
4126 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004127
4128 status = save_reduce(self, reduce_value, obj);
4129
4130 if (0) {
4131 error:
4132 status = -1;
4133 }
4134 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004135
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004136 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004137 Py_XDECREF(reduce_func);
4138 Py_XDECREF(reduce_value);
4139
4140 return status;
4141}
4142
4143static int
4144dump(PicklerObject *self, PyObject *obj)
4145{
4146 const char stop_op = STOP;
4147
4148 if (self->proto >= 2) {
4149 char header[2];
4150
4151 header[0] = PROTO;
4152 assert(self->proto >= 0 && self->proto < 256);
4153 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004154 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004155 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004156 if (self->proto >= 4)
4157 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004158 }
4159
4160 if (save(self, obj, 0) < 0 ||
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004161 _Pickler_Write(self, &stop_op, 1) < 0 ||
4162 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004163 return -1;
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004164 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004165 return 0;
4166}
4167
Larry Hastings61272b72014-01-07 12:41:53 -08004168/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004169
4170_pickle.Pickler.clear_memo
4171
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172Clears the pickler's "memo".
4173
4174The memo is the data structure that remembers which objects the
4175pickler has already seen, so that shared or recursive objects are
4176pickled by reference and not by value. This method is useful when
4177re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004178[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004179
Larry Hastings3cceb382014-01-04 11:09:09 -08004180static PyObject *
4181_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004182/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004183{
4184 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004185 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004186
4187 Py_RETURN_NONE;
4188}
4189
Larry Hastings61272b72014-01-07 12:41:53 -08004190/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004191
4192_pickle.Pickler.dump
4193
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004194 obj: object
4195 /
4196
4197Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004198[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004199
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004200static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004201_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004202/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004203{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004204 /* Check whether the Pickler was initialized correctly (issue3664).
4205 Developers often forget to call __init__() in their subclasses, which
4206 would trigger a segfault without this check. */
4207 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004208 PickleState *st = _Pickle_GetGlobalState();
4209 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004210 "Pickler.__init__() was not called by %s.__init__()",
4211 Py_TYPE(self)->tp_name);
4212 return NULL;
4213 }
4214
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004215 if (_Pickler_ClearBuffer(self) < 0)
4216 return NULL;
4217
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004218 if (dump(self, obj) < 0)
4219 return NULL;
4220
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004221 if (_Pickler_FlushToFile(self) < 0)
4222 return NULL;
4223
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004224 Py_RETURN_NONE;
4225}
4226
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004227/*[clinic input]
4228
4229_pickle.Pickler.__sizeof__ -> Py_ssize_t
4230
4231Returns size in memory, in bytes.
4232[clinic start generated code]*/
4233
4234static Py_ssize_t
4235_pickle_Pickler___sizeof___impl(PicklerObject *self)
4236/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4237{
4238 Py_ssize_t res, s;
4239
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004240 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004241 if (self->memo != NULL) {
4242 res += sizeof(PyMemoTable);
4243 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4244 }
4245 if (self->output_buffer != NULL) {
4246 s = _PySys_GetSizeOf(self->output_buffer);
4247 if (s == -1)
4248 return -1;
4249 res += s;
4250 }
4251 return res;
4252}
4253
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004254static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004255 _PICKLE_PICKLER_DUMP_METHODDEF
4256 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004257 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004258 {NULL, NULL} /* sentinel */
4259};
4260
4261static void
4262Pickler_dealloc(PicklerObject *self)
4263{
4264 PyObject_GC_UnTrack(self);
4265
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004266 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004267 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004268 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004269 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004270 Py_XDECREF(self->fast_memo);
4271
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004272 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004273
4274 Py_TYPE(self)->tp_free((PyObject *)self);
4275}
4276
4277static int
4278Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4279{
4280 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004281 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004282 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004283 Py_VISIT(self->fast_memo);
4284 return 0;
4285}
4286
4287static int
4288Pickler_clear(PicklerObject *self)
4289{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004290 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004291 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004292 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004293 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004294 Py_CLEAR(self->fast_memo);
4295
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004296 if (self->memo != NULL) {
4297 PyMemoTable *memo = self->memo;
4298 self->memo = NULL;
4299 PyMemoTable_Del(memo);
4300 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004301 return 0;
4302}
4303
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004304
Larry Hastings61272b72014-01-07 12:41:53 -08004305/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004306
4307_pickle.Pickler.__init__
4308
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004309 file: object
4310 protocol: object = NULL
4311 fix_imports: bool = True
4312
4313This takes a binary file for writing a pickle data stream.
4314
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004315The optional *protocol* argument tells the pickler to use the given
4316protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4317protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004318
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004319Specifying a negative protocol version selects the highest protocol
4320version supported. The higher the protocol used, the more recent the
4321version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004322
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004323The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004324bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004325writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004326this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004327
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004328If *fix_imports* is True and protocol is less than 3, pickle will try
4329to map the new Python 3 names to the old module names used in Python
43302, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004331[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004332
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004333static int
Larry Hastings89964c42015-04-14 18:07:59 -04004334_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4335 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004336/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004337{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004338 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004339 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004340
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004341 /* In case of multiple __init__() calls, clear previous content. */
4342 if (self->write != NULL)
4343 (void)Pickler_clear(self);
4344
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004345 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004346 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004347
4348 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004349 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004350
4351 /* memo and output_buffer may have already been created in _Pickler_New */
4352 if (self->memo == NULL) {
4353 self->memo = PyMemoTable_New();
4354 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004355 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004356 }
4357 self->output_len = 0;
4358 if (self->output_buffer == NULL) {
4359 self->max_output_len = WRITE_BUF_SIZE;
4360 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4361 self->max_output_len);
4362 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004363 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004364 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004365
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004366 self->fast = 0;
4367 self->fast_nesting = 0;
4368 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004369
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004370 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4371 &self->pers_func, &self->pers_func_self) < 0)
4372 {
4373 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004374 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004375
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004376 if (_PyObject_LookupAttrId((PyObject *)self,
4377 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4378 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004379 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004380
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004381 return 0;
4382}
4383
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004384
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004385/* Define a proxy object for the Pickler's internal memo object. This is to
4386 * avoid breaking code like:
4387 * pickler.memo.clear()
4388 * and
4389 * pickler.memo = saved_memo
4390 * Is this a good idea? Not really, but we don't want to break code that uses
4391 * it. Note that we don't implement the entire mapping API here. This is
4392 * intentional, as these should be treated as black-box implementation details.
4393 */
4394
Larry Hastings61272b72014-01-07 12:41:53 -08004395/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004396_pickle.PicklerMemoProxy.clear
4397
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004398Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004399[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004400
Larry Hastings3cceb382014-01-04 11:09:09 -08004401static PyObject *
4402_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004403/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004404{
4405 if (self->pickler->memo)
4406 PyMemoTable_Clear(self->pickler->memo);
4407 Py_RETURN_NONE;
4408}
4409
Larry Hastings61272b72014-01-07 12:41:53 -08004410/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004411_pickle.PicklerMemoProxy.copy
4412
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004413Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004414[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004415
Larry Hastings3cceb382014-01-04 11:09:09 -08004416static PyObject *
4417_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004418/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004419{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004420 PyMemoTable *memo;
4421 PyObject *new_memo = PyDict_New();
4422 if (new_memo == NULL)
4423 return NULL;
4424
4425 memo = self->pickler->memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07004426 for (size_t i = 0; i < memo->mt_allocated; ++i) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004427 PyMemoEntry entry = memo->mt_table[i];
4428 if (entry.me_key != NULL) {
4429 int status;
4430 PyObject *key, *value;
4431
4432 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004433 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004434
4435 if (key == NULL || value == NULL) {
4436 Py_XDECREF(key);
4437 Py_XDECREF(value);
4438 goto error;
4439 }
4440 status = PyDict_SetItem(new_memo, key, value);
4441 Py_DECREF(key);
4442 Py_DECREF(value);
4443 if (status < 0)
4444 goto error;
4445 }
4446 }
4447 return new_memo;
4448
4449 error:
4450 Py_XDECREF(new_memo);
4451 return NULL;
4452}
4453
Larry Hastings61272b72014-01-07 12:41:53 -08004454/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004455_pickle.PicklerMemoProxy.__reduce__
4456
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004457Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004458[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004459
Larry Hastings3cceb382014-01-04 11:09:09 -08004460static PyObject *
4461_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004462/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004463{
4464 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004465 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004466 if (contents == NULL)
4467 return NULL;
4468
4469 reduce_value = PyTuple_New(2);
4470 if (reduce_value == NULL) {
4471 Py_DECREF(contents);
4472 return NULL;
4473 }
4474 dict_args = PyTuple_New(1);
4475 if (dict_args == NULL) {
4476 Py_DECREF(contents);
4477 Py_DECREF(reduce_value);
4478 return NULL;
4479 }
4480 PyTuple_SET_ITEM(dict_args, 0, contents);
4481 Py_INCREF((PyObject *)&PyDict_Type);
4482 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4483 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4484 return reduce_value;
4485}
4486
4487static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004488 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4489 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4490 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004491 {NULL, NULL} /* sentinel */
4492};
4493
4494static void
4495PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4496{
4497 PyObject_GC_UnTrack(self);
4498 Py_XDECREF(self->pickler);
4499 PyObject_GC_Del((PyObject *)self);
4500}
4501
4502static int
4503PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4504 visitproc visit, void *arg)
4505{
4506 Py_VISIT(self->pickler);
4507 return 0;
4508}
4509
4510static int
4511PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4512{
4513 Py_CLEAR(self->pickler);
4514 return 0;
4515}
4516
4517static PyTypeObject PicklerMemoProxyType = {
4518 PyVarObject_HEAD_INIT(NULL, 0)
4519 "_pickle.PicklerMemoProxy", /*tp_name*/
4520 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4521 0,
4522 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4523 0, /* tp_print */
4524 0, /* tp_getattr */
4525 0, /* tp_setattr */
4526 0, /* tp_compare */
4527 0, /* tp_repr */
4528 0, /* tp_as_number */
4529 0, /* tp_as_sequence */
4530 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004531 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004532 0, /* tp_call */
4533 0, /* tp_str */
4534 PyObject_GenericGetAttr, /* tp_getattro */
4535 PyObject_GenericSetAttr, /* tp_setattro */
4536 0, /* tp_as_buffer */
4537 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4538 0, /* tp_doc */
4539 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4540 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4541 0, /* tp_richcompare */
4542 0, /* tp_weaklistoffset */
4543 0, /* tp_iter */
4544 0, /* tp_iternext */
4545 picklerproxy_methods, /* tp_methods */
4546};
4547
4548static PyObject *
4549PicklerMemoProxy_New(PicklerObject *pickler)
4550{
4551 PicklerMemoProxyObject *self;
4552
4553 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4554 if (self == NULL)
4555 return NULL;
4556 Py_INCREF(pickler);
4557 self->pickler = pickler;
4558 PyObject_GC_Track(self);
4559 return (PyObject *)self;
4560}
4561
4562/*****************************************************************************/
4563
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004564static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004565Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004566{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004567 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004568}
4569
4570static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004571Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004572{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004573 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004574
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004575 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004576 PyErr_SetString(PyExc_TypeError,
4577 "attribute deletion is not supported");
4578 return -1;
4579 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004580
4581 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4582 PicklerObject *pickler =
4583 ((PicklerMemoProxyObject *)obj)->pickler;
4584
4585 new_memo = PyMemoTable_Copy(pickler->memo);
4586 if (new_memo == NULL)
4587 return -1;
4588 }
4589 else if (PyDict_Check(obj)) {
4590 Py_ssize_t i = 0;
4591 PyObject *key, *value;
4592
4593 new_memo = PyMemoTable_New();
4594 if (new_memo == NULL)
4595 return -1;
4596
4597 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004598 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004599 PyObject *memo_obj;
4600
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004601 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004602 PyErr_SetString(PyExc_TypeError,
4603 "'memo' values must be 2-item tuples");
4604 goto error;
4605 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004606 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004607 if (memo_id == -1 && PyErr_Occurred())
4608 goto error;
4609 memo_obj = PyTuple_GET_ITEM(value, 1);
4610 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4611 goto error;
4612 }
4613 }
4614 else {
4615 PyErr_Format(PyExc_TypeError,
Miss Islington (bot)7beb8c52018-11-05 06:52:58 -08004616 "'memo' attribute must be a PicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004617 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004618 return -1;
4619 }
4620
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004621 PyMemoTable_Del(self->memo);
4622 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004623
4624 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004625
4626 error:
4627 if (new_memo)
4628 PyMemoTable_Del(new_memo);
4629 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004630}
4631
4632static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004633Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004634{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004635 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004636 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004637 return NULL;
4638 }
4639 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004640}
4641
4642static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004643Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004644{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004645 if (value == NULL) {
4646 PyErr_SetString(PyExc_TypeError,
4647 "attribute deletion is not supported");
4648 return -1;
4649 }
4650 if (!PyCallable_Check(value)) {
4651 PyErr_SetString(PyExc_TypeError,
4652 "persistent_id must be a callable taking one argument");
4653 return -1;
4654 }
4655
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004656 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004657 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004658 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004659
4660 return 0;
4661}
4662
4663static PyMemberDef Pickler_members[] = {
4664 {"bin", T_INT, offsetof(PicklerObject, bin)},
4665 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004666 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004667 {NULL}
4668};
4669
4670static PyGetSetDef Pickler_getsets[] = {
4671 {"memo", (getter)Pickler_get_memo,
4672 (setter)Pickler_set_memo},
4673 {"persistent_id", (getter)Pickler_get_persid,
4674 (setter)Pickler_set_persid},
4675 {NULL}
4676};
4677
4678static PyTypeObject Pickler_Type = {
4679 PyVarObject_HEAD_INIT(NULL, 0)
4680 "_pickle.Pickler" , /*tp_name*/
4681 sizeof(PicklerObject), /*tp_basicsize*/
4682 0, /*tp_itemsize*/
4683 (destructor)Pickler_dealloc, /*tp_dealloc*/
4684 0, /*tp_print*/
4685 0, /*tp_getattr*/
4686 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004687 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004688 0, /*tp_repr*/
4689 0, /*tp_as_number*/
4690 0, /*tp_as_sequence*/
4691 0, /*tp_as_mapping*/
4692 0, /*tp_hash*/
4693 0, /*tp_call*/
4694 0, /*tp_str*/
4695 0, /*tp_getattro*/
4696 0, /*tp_setattro*/
4697 0, /*tp_as_buffer*/
4698 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004699 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004700 (traverseproc)Pickler_traverse, /*tp_traverse*/
4701 (inquiry)Pickler_clear, /*tp_clear*/
4702 0, /*tp_richcompare*/
4703 0, /*tp_weaklistoffset*/
4704 0, /*tp_iter*/
4705 0, /*tp_iternext*/
4706 Pickler_methods, /*tp_methods*/
4707 Pickler_members, /*tp_members*/
4708 Pickler_getsets, /*tp_getset*/
4709 0, /*tp_base*/
4710 0, /*tp_dict*/
4711 0, /*tp_descr_get*/
4712 0, /*tp_descr_set*/
4713 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004714 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004715 PyType_GenericAlloc, /*tp_alloc*/
4716 PyType_GenericNew, /*tp_new*/
4717 PyObject_GC_Del, /*tp_free*/
4718 0, /*tp_is_gc*/
4719};
4720
Victor Stinner121aab42011-09-29 23:40:53 +02004721/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004722
4723 XXX: It would be nice to able to avoid Python function call overhead, by
4724 using directly the C version of find_class(), when find_class() is not
4725 overridden by a subclass. Although, this could become rather hackish. A
4726 simpler optimization would be to call the C function when self is not a
4727 subclass instance. */
4728static PyObject *
4729find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4730{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004731 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004732
Victor Stinner55ba38a2016-12-09 16:09:30 +01004733 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4734 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004735}
4736
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004737static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004738marker(UnpicklerObject *self)
4739{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004740 Py_ssize_t mark;
4741
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004742 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004743 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004744 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004745 return -1;
4746 }
4747
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004748 mark = self->marks[--self->num_marks];
4749 self->stack->mark_set = self->num_marks != 0;
4750 self->stack->fence = self->num_marks ?
4751 self->marks[self->num_marks - 1] : 0;
4752 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004753}
4754
4755static int
4756load_none(UnpicklerObject *self)
4757{
4758 PDATA_APPEND(self->stack, Py_None, -1);
4759 return 0;
4760}
4761
4762static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004763load_int(UnpicklerObject *self)
4764{
4765 PyObject *value;
4766 char *endptr, *s;
4767 Py_ssize_t len;
4768 long x;
4769
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004770 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004771 return -1;
4772 if (len < 2)
4773 return bad_readline();
4774
4775 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004776 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004777 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004778 x = strtol(s, &endptr, 0);
4779
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004780 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004781 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004782 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004783 errno = 0;
4784 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004785 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004786 if (value == NULL) {
4787 PyErr_SetString(PyExc_ValueError,
4788 "could not convert string to int");
4789 return -1;
4790 }
4791 }
4792 else {
4793 if (len == 3 && (x == 0 || x == 1)) {
4794 if ((value = PyBool_FromLong(x)) == NULL)
4795 return -1;
4796 }
4797 else {
4798 if ((value = PyLong_FromLong(x)) == NULL)
4799 return -1;
4800 }
4801 }
4802
4803 PDATA_PUSH(self->stack, value, -1);
4804 return 0;
4805}
4806
4807static int
4808load_bool(UnpicklerObject *self, PyObject *boolean)
4809{
4810 assert(boolean == Py_True || boolean == Py_False);
4811 PDATA_APPEND(self->stack, boolean, -1);
4812 return 0;
4813}
4814
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004815/* s contains x bytes of an unsigned little-endian integer. Return its value
4816 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4817 */
4818static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004819calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004820{
4821 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004822 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004823 size_t x = 0;
4824
Serhiy Storchakae0606192015-09-29 22:10:07 +03004825 if (nbytes > (int)sizeof(size_t)) {
4826 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4827 * have 64-bit size that can't be represented on 32-bit platform.
4828 */
4829 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4830 if (s[i])
4831 return -1;
4832 }
4833 nbytes = (int)sizeof(size_t);
4834 }
4835 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004836 x |= (size_t) s[i] << (8 * i);
4837 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004838
4839 if (x > PY_SSIZE_T_MAX)
4840 return -1;
4841 else
4842 return (Py_ssize_t) x;
4843}
4844
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004845/* s contains x bytes of a little-endian integer. Return its value as a
4846 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004847 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004848 * of x-platform bugs.
4849 */
4850static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004851calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004852{
4853 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004854 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004855 long x = 0;
4856
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004857 for (i = 0; i < nbytes; i++) {
4858 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004859 }
4860
4861 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4862 * is signed, so on a box with longs bigger than 4 bytes we need
4863 * to extend a BININT's sign bit to the full width.
4864 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004865 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004866 x |= -(x & (1L << 31));
4867 }
4868
4869 return x;
4870}
4871
4872static int
4873load_binintx(UnpicklerObject *self, char *s, int size)
4874{
4875 PyObject *value;
4876 long x;
4877
4878 x = calc_binint(s, size);
4879
4880 if ((value = PyLong_FromLong(x)) == NULL)
4881 return -1;
4882
4883 PDATA_PUSH(self->stack, value, -1);
4884 return 0;
4885}
4886
4887static int
4888load_binint(UnpicklerObject *self)
4889{
4890 char *s;
4891
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004892 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004893 return -1;
4894
4895 return load_binintx(self, s, 4);
4896}
4897
4898static int
4899load_binint1(UnpicklerObject *self)
4900{
4901 char *s;
4902
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004903 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004904 return -1;
4905
4906 return load_binintx(self, s, 1);
4907}
4908
4909static int
4910load_binint2(UnpicklerObject *self)
4911{
4912 char *s;
4913
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004914 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004915 return -1;
4916
4917 return load_binintx(self, s, 2);
4918}
4919
4920static int
4921load_long(UnpicklerObject *self)
4922{
4923 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004924 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004925 Py_ssize_t len;
4926
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004927 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004928 return -1;
4929 if (len < 2)
4930 return bad_readline();
4931
Mark Dickinson8dd05142009-01-20 20:43:58 +00004932 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4933 the 'L' before calling PyLong_FromString. In order to maintain
4934 compatibility with Python 3.0.0, we don't actually *require*
4935 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004936 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004937 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004938 /* XXX: Should the base argument explicitly set to 10? */
4939 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004940 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004941 return -1;
4942
4943 PDATA_PUSH(self->stack, value, -1);
4944 return 0;
4945}
4946
4947/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4948 * data following.
4949 */
4950static int
4951load_counted_long(UnpicklerObject *self, int size)
4952{
4953 PyObject *value;
4954 char *nbytes;
4955 char *pdata;
4956
4957 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004958 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004959 return -1;
4960
4961 size = calc_binint(nbytes, size);
4962 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004963 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004964 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004965 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004966 "LONG pickle has negative byte count");
4967 return -1;
4968 }
4969
4970 if (size == 0)
4971 value = PyLong_FromLong(0L);
4972 else {
4973 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004974 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004975 return -1;
4976 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4977 1 /* little endian */ , 1 /* signed */ );
4978 }
4979 if (value == NULL)
4980 return -1;
4981 PDATA_PUSH(self->stack, value, -1);
4982 return 0;
4983}
4984
4985static int
4986load_float(UnpicklerObject *self)
4987{
4988 PyObject *value;
4989 char *endptr, *s;
4990 Py_ssize_t len;
4991 double d;
4992
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004993 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004994 return -1;
4995 if (len < 2)
4996 return bad_readline();
4997
4998 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004999 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
5000 if (d == -1.0 && PyErr_Occurred())
5001 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005002 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005003 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5004 return -1;
5005 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005006 value = PyFloat_FromDouble(d);
5007 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005008 return -1;
5009
5010 PDATA_PUSH(self->stack, value, -1);
5011 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005012}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005013
5014static int
5015load_binfloat(UnpicklerObject *self)
5016{
5017 PyObject *value;
5018 double x;
5019 char *s;
5020
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005021 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005022 return -1;
5023
5024 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5025 if (x == -1.0 && PyErr_Occurred())
5026 return -1;
5027
5028 if ((value = PyFloat_FromDouble(x)) == NULL)
5029 return -1;
5030
5031 PDATA_PUSH(self->stack, value, -1);
5032 return 0;
5033}
5034
5035static int
5036load_string(UnpicklerObject *self)
5037{
5038 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005039 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040 Py_ssize_t len;
5041 char *s, *p;
5042
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005043 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005045 /* Strip the newline */
5046 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005047 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005048 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005049 p = s + 1;
5050 len -= 2;
5051 }
5052 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005053 PickleState *st = _Pickle_GetGlobalState();
5054 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005055 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005056 return -1;
5057 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005058 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005059
5060 /* Use the PyBytes API to decode the string, since that is what is used
5061 to encode, and then coerce the result to Unicode. */
5062 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005063 if (bytes == NULL)
5064 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005065
5066 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5067 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5068 if (strcmp(self->encoding, "bytes") == 0) {
5069 obj = bytes;
5070 }
5071 else {
5072 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5073 Py_DECREF(bytes);
5074 if (obj == NULL) {
5075 return -1;
5076 }
5077 }
5078
5079 PDATA_PUSH(self->stack, obj, -1);
5080 return 0;
5081}
5082
5083static int
5084load_counted_binstring(UnpicklerObject *self, int nbytes)
5085{
5086 PyObject *obj;
5087 Py_ssize_t size;
5088 char *s;
5089
5090 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005091 return -1;
5092
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005093 size = calc_binsize(s, nbytes);
5094 if (size < 0) {
5095 PickleState *st = _Pickle_GetGlobalState();
5096 PyErr_Format(st->UnpicklingError,
5097 "BINSTRING exceeds system's maximum size of %zd bytes",
5098 PY_SSIZE_T_MAX);
5099 return -1;
5100 }
5101
5102 if (_Unpickler_Read(self, &s, size) < 0)
5103 return -1;
5104
5105 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5106 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5107 if (strcmp(self->encoding, "bytes") == 0) {
5108 obj = PyBytes_FromStringAndSize(s, size);
5109 }
5110 else {
5111 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5112 }
5113 if (obj == NULL) {
5114 return -1;
5115 }
5116
5117 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005118 return 0;
5119}
5120
5121static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005122load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005123{
5124 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005125 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005126 char *s;
5127
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005128 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005129 return -1;
5130
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005131 size = calc_binsize(s, nbytes);
5132 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005133 PyErr_Format(PyExc_OverflowError,
5134 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005135 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005136 return -1;
5137 }
5138
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005139 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005140 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005141
5142 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005143 if (bytes == NULL)
5144 return -1;
5145
5146 PDATA_PUSH(self->stack, bytes, -1);
5147 return 0;
5148}
5149
5150static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005151load_unicode(UnpicklerObject *self)
5152{
5153 PyObject *str;
5154 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005155 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005156
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005157 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005158 return -1;
5159 if (len < 1)
5160 return bad_readline();
5161
5162 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5163 if (str == NULL)
5164 return -1;
5165
5166 PDATA_PUSH(self->stack, str, -1);
5167 return 0;
5168}
5169
5170static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005171load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005172{
5173 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005174 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005175 char *s;
5176
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005177 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005178 return -1;
5179
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005180 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005181 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005182 PyErr_Format(PyExc_OverflowError,
5183 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005184 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005185 return -1;
5186 }
5187
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005188 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005189 return -1;
5190
Victor Stinner485fb562010-04-13 11:07:24 +00005191 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005192 if (str == NULL)
5193 return -1;
5194
5195 PDATA_PUSH(self->stack, str, -1);
5196 return 0;
5197}
5198
5199static int
Victor Stinner21b47112016-03-14 18:09:39 +01005200load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005201{
5202 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005203
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005204 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005205 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005206
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005207 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005208 if (tuple == NULL)
5209 return -1;
5210 PDATA_PUSH(self->stack, tuple, -1);
5211 return 0;
5212}
5213
5214static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005215load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005216{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005217 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005218
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005219 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005220 return -1;
5221
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005222 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005223}
5224
5225static int
5226load_empty_list(UnpicklerObject *self)
5227{
5228 PyObject *list;
5229
5230 if ((list = PyList_New(0)) == NULL)
5231 return -1;
5232 PDATA_PUSH(self->stack, list, -1);
5233 return 0;
5234}
5235
5236static int
5237load_empty_dict(UnpicklerObject *self)
5238{
5239 PyObject *dict;
5240
5241 if ((dict = PyDict_New()) == NULL)
5242 return -1;
5243 PDATA_PUSH(self->stack, dict, -1);
5244 return 0;
5245}
5246
5247static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005248load_empty_set(UnpicklerObject *self)
5249{
5250 PyObject *set;
5251
5252 if ((set = PySet_New(NULL)) == NULL)
5253 return -1;
5254 PDATA_PUSH(self->stack, set, -1);
5255 return 0;
5256}
5257
5258static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005259load_list(UnpicklerObject *self)
5260{
5261 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005262 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005263
5264 if ((i = marker(self)) < 0)
5265 return -1;
5266
5267 list = Pdata_poplist(self->stack, i);
5268 if (list == NULL)
5269 return -1;
5270 PDATA_PUSH(self->stack, list, -1);
5271 return 0;
5272}
5273
5274static int
5275load_dict(UnpicklerObject *self)
5276{
5277 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005278 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005279
5280 if ((i = marker(self)) < 0)
5281 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005282 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005283
5284 if ((dict = PyDict_New()) == NULL)
5285 return -1;
5286
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005287 if ((j - i) % 2 != 0) {
5288 PickleState *st = _Pickle_GetGlobalState();
5289 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005290 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005291 return -1;
5292 }
5293
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005294 for (k = i + 1; k < j; k += 2) {
5295 key = self->stack->data[k - 1];
5296 value = self->stack->data[k];
5297 if (PyDict_SetItem(dict, key, value) < 0) {
5298 Py_DECREF(dict);
5299 return -1;
5300 }
5301 }
5302 Pdata_clear(self->stack, i);
5303 PDATA_PUSH(self->stack, dict, -1);
5304 return 0;
5305}
5306
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005307static int
5308load_frozenset(UnpicklerObject *self)
5309{
5310 PyObject *items;
5311 PyObject *frozenset;
5312 Py_ssize_t i;
5313
5314 if ((i = marker(self)) < 0)
5315 return -1;
5316
5317 items = Pdata_poptuple(self->stack, i);
5318 if (items == NULL)
5319 return -1;
5320
5321 frozenset = PyFrozenSet_New(items);
5322 Py_DECREF(items);
5323 if (frozenset == NULL)
5324 return -1;
5325
5326 PDATA_PUSH(self->stack, frozenset, -1);
5327 return 0;
5328}
5329
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005330static PyObject *
5331instantiate(PyObject *cls, PyObject *args)
5332{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005333 /* Caller must assure args are a tuple. Normally, args come from
5334 Pdata_poptuple which packs objects from the top of the stack
5335 into a newly created tuple. */
5336 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005337 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5338 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005339 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005340 PyObject *func;
5341 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5342 return NULL;
5343 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005344 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005345 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5346 }
5347 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005348 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005349 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005350}
5351
5352static int
5353load_obj(UnpicklerObject *self)
5354{
5355 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005356 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005357
5358 if ((i = marker(self)) < 0)
5359 return -1;
5360
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005361 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005362 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005363
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005364 args = Pdata_poptuple(self->stack, i + 1);
5365 if (args == NULL)
5366 return -1;
5367
5368 PDATA_POP(self->stack, cls);
5369 if (cls) {
5370 obj = instantiate(cls, args);
5371 Py_DECREF(cls);
5372 }
5373 Py_DECREF(args);
5374 if (obj == NULL)
5375 return -1;
5376
5377 PDATA_PUSH(self->stack, obj, -1);
5378 return 0;
5379}
5380
5381static int
5382load_inst(UnpicklerObject *self)
5383{
5384 PyObject *cls = NULL;
5385 PyObject *args = NULL;
5386 PyObject *obj = NULL;
5387 PyObject *module_name;
5388 PyObject *class_name;
5389 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005390 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005391 char *s;
5392
5393 if ((i = marker(self)) < 0)
5394 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005395 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005396 return -1;
5397 if (len < 2)
5398 return bad_readline();
5399
5400 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5401 identifiers are permitted in Python 3.0, since the INST opcode is only
5402 supported by older protocols on Python 2.x. */
5403 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5404 if (module_name == NULL)
5405 return -1;
5406
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005407 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005408 if (len < 2) {
5409 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005410 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005411 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005412 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005413 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005414 cls = find_class(self, module_name, class_name);
5415 Py_DECREF(class_name);
5416 }
5417 }
5418 Py_DECREF(module_name);
5419
5420 if (cls == NULL)
5421 return -1;
5422
5423 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5424 obj = instantiate(cls, args);
5425 Py_DECREF(args);
5426 }
5427 Py_DECREF(cls);
5428
5429 if (obj == NULL)
5430 return -1;
5431
5432 PDATA_PUSH(self->stack, obj, -1);
5433 return 0;
5434}
5435
5436static int
5437load_newobj(UnpicklerObject *self)
5438{
5439 PyObject *args = NULL;
5440 PyObject *clsraw = NULL;
5441 PyTypeObject *cls; /* clsraw cast to its true type */
5442 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005443 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005444
5445 /* Stack is ... cls argtuple, and we want to call
5446 * cls.__new__(cls, *argtuple).
5447 */
5448 PDATA_POP(self->stack, args);
5449 if (args == NULL)
5450 goto error;
5451 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005452 PyErr_SetString(st->UnpicklingError,
5453 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005454 goto error;
5455 }
5456
5457 PDATA_POP(self->stack, clsraw);
5458 cls = (PyTypeObject *)clsraw;
5459 if (cls == NULL)
5460 goto error;
5461 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005462 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005463 "isn't a type object");
5464 goto error;
5465 }
5466 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005467 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005468 "has NULL tp_new");
5469 goto error;
5470 }
5471
5472 /* Call __new__. */
5473 obj = cls->tp_new(cls, args, NULL);
5474 if (obj == NULL)
5475 goto error;
5476
5477 Py_DECREF(args);
5478 Py_DECREF(clsraw);
5479 PDATA_PUSH(self->stack, obj, -1);
5480 return 0;
5481
5482 error:
5483 Py_XDECREF(args);
5484 Py_XDECREF(clsraw);
5485 return -1;
5486}
5487
5488static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005489load_newobj_ex(UnpicklerObject *self)
5490{
5491 PyObject *cls, *args, *kwargs;
5492 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005493 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005494
5495 PDATA_POP(self->stack, kwargs);
5496 if (kwargs == NULL) {
5497 return -1;
5498 }
5499 PDATA_POP(self->stack, args);
5500 if (args == NULL) {
5501 Py_DECREF(kwargs);
5502 return -1;
5503 }
5504 PDATA_POP(self->stack, cls);
5505 if (cls == NULL) {
5506 Py_DECREF(kwargs);
5507 Py_DECREF(args);
5508 return -1;
5509 }
Larry Hastings61272b72014-01-07 12:41:53 -08005510
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005511 if (!PyType_Check(cls)) {
5512 Py_DECREF(kwargs);
5513 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005514 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005515 "NEWOBJ_EX class argument must be a type, not %.200s",
5516 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005517 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005518 return -1;
5519 }
5520
5521 if (((PyTypeObject *)cls)->tp_new == NULL) {
5522 Py_DECREF(kwargs);
5523 Py_DECREF(args);
5524 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005525 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005526 "NEWOBJ_EX class argument doesn't have __new__");
5527 return -1;
5528 }
5529 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5530 Py_DECREF(kwargs);
5531 Py_DECREF(args);
5532 Py_DECREF(cls);
5533 if (obj == NULL) {
5534 return -1;
5535 }
5536 PDATA_PUSH(self->stack, obj, -1);
5537 return 0;
5538}
5539
5540static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005541load_global(UnpicklerObject *self)
5542{
5543 PyObject *global = NULL;
5544 PyObject *module_name;
5545 PyObject *global_name;
5546 Py_ssize_t len;
5547 char *s;
5548
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005549 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005550 return -1;
5551 if (len < 2)
5552 return bad_readline();
5553 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5554 if (!module_name)
5555 return -1;
5556
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005557 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005558 if (len < 2) {
5559 Py_DECREF(module_name);
5560 return bad_readline();
5561 }
5562 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5563 if (global_name) {
5564 global = find_class(self, module_name, global_name);
5565 Py_DECREF(global_name);
5566 }
5567 }
5568 Py_DECREF(module_name);
5569
5570 if (global == NULL)
5571 return -1;
5572 PDATA_PUSH(self->stack, global, -1);
5573 return 0;
5574}
5575
5576static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005577load_stack_global(UnpicklerObject *self)
5578{
5579 PyObject *global;
5580 PyObject *module_name;
5581 PyObject *global_name;
5582
5583 PDATA_POP(self->stack, global_name);
5584 PDATA_POP(self->stack, module_name);
5585 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5586 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005587 PickleState *st = _Pickle_GetGlobalState();
5588 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005589 Py_XDECREF(global_name);
5590 Py_XDECREF(module_name);
5591 return -1;
5592 }
5593 global = find_class(self, module_name, global_name);
5594 Py_DECREF(global_name);
5595 Py_DECREF(module_name);
5596 if (global == NULL)
5597 return -1;
5598 PDATA_PUSH(self->stack, global, -1);
5599 return 0;
5600}
5601
5602static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603load_persid(UnpicklerObject *self)
5604{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005605 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 Py_ssize_t len;
5607 char *s;
5608
5609 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005610 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005611 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005612 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005613 return bad_readline();
5614
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005615 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5616 if (pid == NULL) {
5617 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5618 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5619 "persistent IDs in protocol 0 must be "
5620 "ASCII strings");
5621 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005623 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005624
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005625 obj = call_method(self->pers_func, self->pers_func_self, pid);
5626 Py_DECREF(pid);
5627 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628 return -1;
5629
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005630 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005631 return 0;
5632 }
5633 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005634 PickleState *st = _Pickle_GetGlobalState();
5635 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005636 "A load persistent id instruction was encountered,\n"
5637 "but no persistent_load function was specified.");
5638 return -1;
5639 }
5640}
5641
5642static int
5643load_binpersid(UnpicklerObject *self)
5644{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005645 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005646
5647 if (self->pers_func) {
5648 PDATA_POP(self->stack, pid);
5649 if (pid == NULL)
5650 return -1;
5651
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005652 obj = call_method(self->pers_func, self->pers_func_self, pid);
5653 Py_DECREF(pid);
5654 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005655 return -1;
5656
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005657 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005658 return 0;
5659 }
5660 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005661 PickleState *st = _Pickle_GetGlobalState();
5662 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005663 "A load persistent id instruction was encountered,\n"
5664 "but no persistent_load function was specified.");
5665 return -1;
5666 }
5667}
5668
5669static int
5670load_pop(UnpicklerObject *self)
5671{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005672 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005673
5674 /* Note that we split the (pickle.py) stack into two stacks,
5675 * an object stack and a mark stack. We have to be clever and
5676 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005677 * mark stack first, and only signalling a stack underflow if
5678 * the object stack is empty and the mark stack doesn't match
5679 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005680 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005681 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005683 self->stack->mark_set = self->num_marks != 0;
5684 self->stack->fence = self->num_marks ?
5685 self->marks[self->num_marks - 1] : 0;
5686 } else if (len <= self->stack->fence)
5687 return Pdata_stack_underflow(self->stack);
5688 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005689 len--;
5690 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005691 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005692 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005693 return 0;
5694}
5695
5696static int
5697load_pop_mark(UnpicklerObject *self)
5698{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005699 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005700
5701 if ((i = marker(self)) < 0)
5702 return -1;
5703
5704 Pdata_clear(self->stack, i);
5705
5706 return 0;
5707}
5708
5709static int
5710load_dup(UnpicklerObject *self)
5711{
5712 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005713 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005714
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005715 if (len <= self->stack->fence)
5716 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005717 last = self->stack->data[len - 1];
5718 PDATA_APPEND(self->stack, last, -1);
5719 return 0;
5720}
5721
5722static int
5723load_get(UnpicklerObject *self)
5724{
5725 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005726 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005727 Py_ssize_t len;
5728 char *s;
5729
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005730 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005731 return -1;
5732 if (len < 2)
5733 return bad_readline();
5734
5735 key = PyLong_FromString(s, NULL, 10);
5736 if (key == NULL)
5737 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005738 idx = PyLong_AsSsize_t(key);
5739 if (idx == -1 && PyErr_Occurred()) {
5740 Py_DECREF(key);
5741 return -1;
5742 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005743
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005744 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005745 if (value == NULL) {
5746 if (!PyErr_Occurred())
5747 PyErr_SetObject(PyExc_KeyError, key);
5748 Py_DECREF(key);
5749 return -1;
5750 }
5751 Py_DECREF(key);
5752
5753 PDATA_APPEND(self->stack, value, -1);
5754 return 0;
5755}
5756
5757static int
5758load_binget(UnpicklerObject *self)
5759{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005760 PyObject *value;
5761 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005762 char *s;
5763
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005764 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 return -1;
5766
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005767 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005768
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005769 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005770 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005771 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005772 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005774 Py_DECREF(key);
5775 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776 return -1;
5777 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005778
5779 PDATA_APPEND(self->stack, value, -1);
5780 return 0;
5781}
5782
5783static int
5784load_long_binget(UnpicklerObject *self)
5785{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005786 PyObject *value;
5787 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005788 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791 return -1;
5792
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005793 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005794
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005795 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005796 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005797 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005798 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005799 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005800 Py_DECREF(key);
5801 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005802 return -1;
5803 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005804
5805 PDATA_APPEND(self->stack, value, -1);
5806 return 0;
5807}
5808
5809/* Push an object from the extension registry (EXT[124]). nbytes is
5810 * the number of bytes following the opcode, holding the index (code) value.
5811 */
5812static int
5813load_extension(UnpicklerObject *self, int nbytes)
5814{
5815 char *codebytes; /* the nbytes bytes after the opcode */
5816 long code; /* calc_binint returns long */
5817 PyObject *py_code; /* code as a Python int */
5818 PyObject *obj; /* the object to push */
5819 PyObject *pair; /* (module_name, class_name) */
5820 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005821 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005822
5823 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005824 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005825 return -1;
5826 code = calc_binint(codebytes, nbytes);
5827 if (code <= 0) { /* note that 0 is forbidden */
5828 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005829 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005830 return -1;
5831 }
5832
5833 /* Look for the code in the cache. */
5834 py_code = PyLong_FromLong(code);
5835 if (py_code == NULL)
5836 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005837 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005838 if (obj != NULL) {
5839 /* Bingo. */
5840 Py_DECREF(py_code);
5841 PDATA_APPEND(self->stack, obj, -1);
5842 return 0;
5843 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005844 if (PyErr_Occurred()) {
5845 Py_DECREF(py_code);
5846 return -1;
5847 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005848
5849 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005850 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005851 if (pair == NULL) {
5852 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005853 if (!PyErr_Occurred()) {
5854 PyErr_Format(PyExc_ValueError, "unregistered extension "
5855 "code %ld", code);
5856 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005857 return -1;
5858 }
5859 /* Since the extension registry is manipulable via Python code,
5860 * confirm that pair is really a 2-tuple of strings.
5861 */
5862 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5863 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5864 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5865 Py_DECREF(py_code);
5866 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5867 "isn't a 2-tuple of strings", code);
5868 return -1;
5869 }
5870 /* Load the object. */
5871 obj = find_class(self, module_name, class_name);
5872 if (obj == NULL) {
5873 Py_DECREF(py_code);
5874 return -1;
5875 }
5876 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005877 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005878 Py_DECREF(py_code);
5879 if (code < 0) {
5880 Py_DECREF(obj);
5881 return -1;
5882 }
5883 PDATA_PUSH(self->stack, obj, -1);
5884 return 0;
5885}
5886
5887static int
5888load_put(UnpicklerObject *self)
5889{
5890 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005891 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005892 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005893 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005894
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005895 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005896 return -1;
5897 if (len < 2)
5898 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005899 if (Py_SIZE(self->stack) <= self->stack->fence)
5900 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005901 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005902
5903 key = PyLong_FromString(s, NULL, 10);
5904 if (key == NULL)
5905 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005906 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005907 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005908 if (idx < 0) {
5909 if (!PyErr_Occurred())
5910 PyErr_SetString(PyExc_ValueError,
5911 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005912 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005913 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005914
5915 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005916}
5917
5918static int
5919load_binput(UnpicklerObject *self)
5920{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005921 PyObject *value;
5922 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005925 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005926 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005927
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005928 if (Py_SIZE(self->stack) <= self->stack->fence)
5929 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005930 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005931
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005932 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005933
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005934 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005935}
5936
5937static int
5938load_long_binput(UnpicklerObject *self)
5939{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005940 PyObject *value;
5941 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005942 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005943
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005944 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005945 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005946
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005947 if (Py_SIZE(self->stack) <= self->stack->fence)
5948 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005949 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005950
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005951 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005952 if (idx < 0) {
5953 PyErr_SetString(PyExc_ValueError,
5954 "negative LONG_BINPUT argument");
5955 return -1;
5956 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005957
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005958 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005959}
5960
5961static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005962load_memoize(UnpicklerObject *self)
5963{
5964 PyObject *value;
5965
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005966 if (Py_SIZE(self->stack) <= self->stack->fence)
5967 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005968 value = self->stack->data[Py_SIZE(self->stack) - 1];
5969
5970 return _Unpickler_MemoPut(self, self->memo_len, value);
5971}
5972
5973static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005974do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005975{
5976 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005977 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005978 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005979 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005980 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005981
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005982 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005983 if (x > len || x <= self->stack->fence)
5984 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005985 if (len == x) /* nothing to do */
5986 return 0;
5987
5988 list = self->stack->data[x - 1];
5989
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005990 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005991 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005992 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005993
5994 slice = Pdata_poplist(self->stack, x);
5995 if (!slice)
5996 return -1;
5997 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005998 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005999 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006000 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006001 }
6002 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006003 PyObject *extend_func;
6004 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006005
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006006 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6007 if (extend_func != NULL) {
6008 slice = Pdata_poplist(self->stack, x);
6009 if (!slice) {
6010 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006011 return -1;
6012 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006013 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006014 Py_DECREF(extend_func);
6015 if (result == NULL)
6016 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006017 Py_DECREF(result);
6018 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006019 else {
6020 PyObject *append_func;
6021 _Py_IDENTIFIER(append);
6022
6023 /* Even if the PEP 307 requires extend() and append() methods,
6024 fall back on append() if the object has no extend() method
6025 for backward compatibility. */
6026 PyErr_Clear();
6027 append_func = _PyObject_GetAttrId(list, &PyId_append);
6028 if (append_func == NULL)
6029 return -1;
6030 for (i = x; i < len; i++) {
6031 value = self->stack->data[i];
6032 result = _Pickle_FastCall(append_func, value);
6033 if (result == NULL) {
6034 Pdata_clear(self->stack, i + 1);
6035 Py_SIZE(self->stack) = x;
6036 Py_DECREF(append_func);
6037 return -1;
6038 }
6039 Py_DECREF(result);
6040 }
6041 Py_SIZE(self->stack) = x;
6042 Py_DECREF(append_func);
6043 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006044 }
6045
6046 return 0;
6047}
6048
6049static int
6050load_append(UnpicklerObject *self)
6051{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006052 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6053 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006054 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006055}
6056
6057static int
6058load_appends(UnpicklerObject *self)
6059{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006060 Py_ssize_t i = marker(self);
6061 if (i < 0)
6062 return -1;
6063 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006064}
6065
6066static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006067do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006068{
6069 PyObject *value, *key;
6070 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006071 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006072 int status = 0;
6073
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006074 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006075 if (x > len || x <= self->stack->fence)
6076 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006077 if (len == x) /* nothing to do */
6078 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006079 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006080 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006081 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006082 PyErr_SetString(st->UnpicklingError,
6083 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006084 return -1;
6085 }
6086
6087 /* Here, dict does not actually need to be a PyDict; it could be anything
6088 that supports the __setitem__ attribute. */
6089 dict = self->stack->data[x - 1];
6090
6091 for (i = x + 1; i < len; i += 2) {
6092 key = self->stack->data[i - 1];
6093 value = self->stack->data[i];
6094 if (PyObject_SetItem(dict, key, value) < 0) {
6095 status = -1;
6096 break;
6097 }
6098 }
6099
6100 Pdata_clear(self->stack, x);
6101 return status;
6102}
6103
6104static int
6105load_setitem(UnpicklerObject *self)
6106{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006107 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006108}
6109
6110static int
6111load_setitems(UnpicklerObject *self)
6112{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006113 Py_ssize_t i = marker(self);
6114 if (i < 0)
6115 return -1;
6116 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006117}
6118
6119static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006120load_additems(UnpicklerObject *self)
6121{
6122 PyObject *set;
6123 Py_ssize_t mark, len, i;
6124
6125 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006126 if (mark < 0)
6127 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006128 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006129 if (mark > len || mark <= self->stack->fence)
6130 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006131 if (len == mark) /* nothing to do */
6132 return 0;
6133
6134 set = self->stack->data[mark - 1];
6135
6136 if (PySet_Check(set)) {
6137 PyObject *items;
6138 int status;
6139
6140 items = Pdata_poptuple(self->stack, mark);
6141 if (items == NULL)
6142 return -1;
6143
6144 status = _PySet_Update(set, items);
6145 Py_DECREF(items);
6146 return status;
6147 }
6148 else {
6149 PyObject *add_func;
6150 _Py_IDENTIFIER(add);
6151
6152 add_func = _PyObject_GetAttrId(set, &PyId_add);
6153 if (add_func == NULL)
6154 return -1;
6155 for (i = mark; i < len; i++) {
6156 PyObject *result;
6157 PyObject *item;
6158
6159 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006160 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006161 if (result == NULL) {
6162 Pdata_clear(self->stack, i + 1);
6163 Py_SIZE(self->stack) = mark;
6164 return -1;
6165 }
6166 Py_DECREF(result);
6167 }
6168 Py_SIZE(self->stack) = mark;
6169 }
6170
6171 return 0;
6172}
6173
6174static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006175load_build(UnpicklerObject *self)
6176{
6177 PyObject *state, *inst, *slotstate;
6178 PyObject *setstate;
6179 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006180 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006181
6182 /* Stack is ... instance, state. We want to leave instance at
6183 * the stack top, possibly mutated via instance.__setstate__(state).
6184 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006185 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6186 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006187
6188 PDATA_POP(self->stack, state);
6189 if (state == NULL)
6190 return -1;
6191
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006192 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006193
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006194 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6195 Py_DECREF(state);
6196 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006197 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006198 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006199 PyObject *result;
6200
6201 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006202 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006203 Py_DECREF(setstate);
6204 if (result == NULL)
6205 return -1;
6206 Py_DECREF(result);
6207 return 0;
6208 }
6209
6210 /* A default __setstate__. First see whether state embeds a
6211 * slot state dict too (a proto 2 addition).
6212 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006213 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006214 PyObject *tmp = state;
6215
6216 state = PyTuple_GET_ITEM(tmp, 0);
6217 slotstate = PyTuple_GET_ITEM(tmp, 1);
6218 Py_INCREF(state);
6219 Py_INCREF(slotstate);
6220 Py_DECREF(tmp);
6221 }
6222 else
6223 slotstate = NULL;
6224
6225 /* Set inst.__dict__ from the state dict (if any). */
6226 if (state != Py_None) {
6227 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006228 PyObject *d_key, *d_value;
6229 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006230 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006231
6232 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006233 PickleState *st = _Pickle_GetGlobalState();
6234 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006235 goto error;
6236 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006237 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006238 if (dict == NULL)
6239 goto error;
6240
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006241 i = 0;
6242 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6243 /* normally the keys for instance attributes are
6244 interned. we should try to do that here. */
6245 Py_INCREF(d_key);
6246 if (PyUnicode_CheckExact(d_key))
6247 PyUnicode_InternInPlace(&d_key);
6248 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6249 Py_DECREF(d_key);
6250 goto error;
6251 }
6252 Py_DECREF(d_key);
6253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006254 Py_DECREF(dict);
6255 }
6256
6257 /* Also set instance attributes from the slotstate dict (if any). */
6258 if (slotstate != NULL) {
6259 PyObject *d_key, *d_value;
6260 Py_ssize_t i;
6261
6262 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006263 PickleState *st = _Pickle_GetGlobalState();
6264 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006265 "slot state is not a dictionary");
6266 goto error;
6267 }
6268 i = 0;
6269 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6270 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6271 goto error;
6272 }
6273 }
6274
6275 if (0) {
6276 error:
6277 status = -1;
6278 }
6279
6280 Py_DECREF(state);
6281 Py_XDECREF(slotstate);
6282 return status;
6283}
6284
6285static int
6286load_mark(UnpicklerObject *self)
6287{
6288
6289 /* Note that we split the (pickle.py) stack into two stacks, an
6290 * object stack and a mark stack. Here we push a mark onto the
6291 * mark stack.
6292 */
6293
6294 if ((self->num_marks + 1) >= self->marks_size) {
6295 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006296
6297 /* Use the size_t type to check for overflow. */
6298 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006299 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006300 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006301 PyErr_NoMemory();
6302 return -1;
6303 }
6304
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006305 Py_ssize_t *marks_old = self->marks;
6306 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006307 if (self->marks == NULL) {
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006308 PyMem_FREE(marks_old);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006309 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006310 PyErr_NoMemory();
6311 return -1;
6312 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006313 self->marks_size = (Py_ssize_t)alloc;
6314 }
6315
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006316 self->stack->mark_set = 1;
6317 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006318
6319 return 0;
6320}
6321
6322static int
6323load_reduce(UnpicklerObject *self)
6324{
6325 PyObject *callable = NULL;
6326 PyObject *argtup = NULL;
6327 PyObject *obj = NULL;
6328
6329 PDATA_POP(self->stack, argtup);
6330 if (argtup == NULL)
6331 return -1;
6332 PDATA_POP(self->stack, callable);
6333 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006334 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006335 Py_DECREF(callable);
6336 }
6337 Py_DECREF(argtup);
6338
6339 if (obj == NULL)
6340 return -1;
6341
6342 PDATA_PUSH(self->stack, obj, -1);
6343 return 0;
6344}
6345
6346/* Just raises an error if we don't know the protocol specified. PROTO
6347 * is the first opcode for protocols >= 2.
6348 */
6349static int
6350load_proto(UnpicklerObject *self)
6351{
6352 char *s;
6353 int i;
6354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006355 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006356 return -1;
6357
6358 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006359 if (i <= HIGHEST_PROTOCOL) {
6360 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006361 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006362 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006363
6364 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6365 return -1;
6366}
6367
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006368static int
6369load_frame(UnpicklerObject *self)
6370{
6371 char *s;
6372 Py_ssize_t frame_len;
6373
6374 if (_Unpickler_Read(self, &s, 8) < 0)
6375 return -1;
6376
6377 frame_len = calc_binsize(s, 8);
6378 if (frame_len < 0) {
6379 PyErr_Format(PyExc_OverflowError,
6380 "FRAME length exceeds system's maximum of %zd bytes",
6381 PY_SSIZE_T_MAX);
6382 return -1;
6383 }
6384
6385 if (_Unpickler_Read(self, &s, frame_len) < 0)
6386 return -1;
6387
6388 /* Rewind to start of frame */
6389 self->next_read_idx -= frame_len;
6390 return 0;
6391}
6392
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006393static PyObject *
6394load(UnpicklerObject *self)
6395{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006397 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006398
6399 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006400 self->stack->mark_set = 0;
6401 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006402 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006403 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006404 Pdata_clear(self->stack, 0);
6405
6406 /* Convenient macros for the dispatch while-switch loop just below. */
6407#define OP(opcode, load_func) \
6408 case opcode: if (load_func(self) < 0) break; continue;
6409
6410#define OP_ARG(opcode, load_func, arg) \
6411 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6412
6413 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006414 if (_Unpickler_Read(self, &s, 1) < 0) {
6415 PickleState *st = _Pickle_GetGlobalState();
6416 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6417 PyErr_Format(PyExc_EOFError, "Ran out of input");
6418 }
6419 return NULL;
6420 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006421
6422 switch ((enum opcode)s[0]) {
6423 OP(NONE, load_none)
6424 OP(BININT, load_binint)
6425 OP(BININT1, load_binint1)
6426 OP(BININT2, load_binint2)
6427 OP(INT, load_int)
6428 OP(LONG, load_long)
6429 OP_ARG(LONG1, load_counted_long, 1)
6430 OP_ARG(LONG4, load_counted_long, 4)
6431 OP(FLOAT, load_float)
6432 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006433 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6434 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6435 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6436 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6437 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006438 OP(STRING, load_string)
6439 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006440 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6441 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6442 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006443 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6444 OP_ARG(TUPLE1, load_counted_tuple, 1)
6445 OP_ARG(TUPLE2, load_counted_tuple, 2)
6446 OP_ARG(TUPLE3, load_counted_tuple, 3)
6447 OP(TUPLE, load_tuple)
6448 OP(EMPTY_LIST, load_empty_list)
6449 OP(LIST, load_list)
6450 OP(EMPTY_DICT, load_empty_dict)
6451 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006452 OP(EMPTY_SET, load_empty_set)
6453 OP(ADDITEMS, load_additems)
6454 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006455 OP(OBJ, load_obj)
6456 OP(INST, load_inst)
6457 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006458 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006459 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006460 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006461 OP(APPEND, load_append)
6462 OP(APPENDS, load_appends)
6463 OP(BUILD, load_build)
6464 OP(DUP, load_dup)
6465 OP(BINGET, load_binget)
6466 OP(LONG_BINGET, load_long_binget)
6467 OP(GET, load_get)
6468 OP(MARK, load_mark)
6469 OP(BINPUT, load_binput)
6470 OP(LONG_BINPUT, load_long_binput)
6471 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006472 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006473 OP(POP, load_pop)
6474 OP(POP_MARK, load_pop_mark)
6475 OP(SETITEM, load_setitem)
6476 OP(SETITEMS, load_setitems)
6477 OP(PERSID, load_persid)
6478 OP(BINPERSID, load_binpersid)
6479 OP(REDUCE, load_reduce)
6480 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006481 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482 OP_ARG(EXT1, load_extension, 1)
6483 OP_ARG(EXT2, load_extension, 2)
6484 OP_ARG(EXT4, load_extension, 4)
6485 OP_ARG(NEWTRUE, load_bool, Py_True)
6486 OP_ARG(NEWFALSE, load_bool, Py_False)
6487
6488 case STOP:
6489 break;
6490
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006491 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006492 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006493 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006494 unsigned char c = (unsigned char) *s;
6495 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6496 PyErr_Format(st->UnpicklingError,
6497 "invalid load key, '%c'.", c);
6498 }
6499 else {
6500 PyErr_Format(st->UnpicklingError,
6501 "invalid load key, '\\x%02x'.", c);
6502 }
6503 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006504 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006505 }
6506
6507 break; /* and we are done! */
6508 }
6509
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006510 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006511 return NULL;
6512 }
6513
Victor Stinner2ae57e32013-10-31 13:39:23 +01006514 if (_Unpickler_SkipConsumed(self) < 0)
6515 return NULL;
6516
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006517 PDATA_POP(self->stack, value);
6518 return value;
6519}
6520
Larry Hastings61272b72014-01-07 12:41:53 -08006521/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006522
6523_pickle.Unpickler.load
6524
6525Load a pickle.
6526
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006527Read a pickled object representation from the open file object given
6528in the constructor, and return the reconstituted object hierarchy
6529specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006530[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006531
Larry Hastings3cceb382014-01-04 11:09:09 -08006532static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006533_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006534/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006535{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006536 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006537
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006538 /* Check whether the Unpickler was initialized correctly. This prevents
6539 segfaulting if a subclass overridden __init__ with a function that does
6540 not call Unpickler.__init__(). Here, we simply ensure that self->read
6541 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006542 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006543 PickleState *st = _Pickle_GetGlobalState();
6544 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006545 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006546 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006547 return NULL;
6548 }
6549
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006550 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006551}
6552
6553/* The name of find_class() is misleading. In newer pickle protocols, this
6554 function is used for loading any global (i.e., functions), not just
6555 classes. The name is kept only for backward compatibility. */
6556
Larry Hastings61272b72014-01-07 12:41:53 -08006557/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006558
6559_pickle.Unpickler.find_class
6560
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006561 module_name: object
6562 global_name: object
6563 /
6564
6565Return an object from a specified module.
6566
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006567If necessary, the module will be imported. Subclasses may override
6568this method (e.g. to restrict unpickling of arbitrary classes and
6569functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006570
6571This method is called whenever a class or a function object is
6572needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006573[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006574
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006575static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006576_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6577 PyObject *module_name,
6578 PyObject *global_name)
6579/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006580{
6581 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006582 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006583
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006584 /* Try to map the old names used in Python 2.x to the new ones used in
6585 Python 3.x. We do this only with old pickle protocols and when the
6586 user has not disabled the feature. */
6587 if (self->proto < 3 && self->fix_imports) {
6588 PyObject *key;
6589 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006590 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006591
6592 /* Check if the global (i.e., a function or a class) was renamed
6593 or moved to another module. */
6594 key = PyTuple_Pack(2, module_name, global_name);
6595 if (key == NULL)
6596 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006597 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006598 Py_DECREF(key);
6599 if (item) {
6600 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6601 PyErr_Format(PyExc_RuntimeError,
6602 "_compat_pickle.NAME_MAPPING values should be "
6603 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6604 return NULL;
6605 }
6606 module_name = PyTuple_GET_ITEM(item, 0);
6607 global_name = PyTuple_GET_ITEM(item, 1);
6608 if (!PyUnicode_Check(module_name) ||
6609 !PyUnicode_Check(global_name)) {
6610 PyErr_Format(PyExc_RuntimeError,
6611 "_compat_pickle.NAME_MAPPING values should be "
6612 "pairs of str, not (%.200s, %.200s)",
6613 Py_TYPE(module_name)->tp_name,
6614 Py_TYPE(global_name)->tp_name);
6615 return NULL;
6616 }
6617 }
6618 else if (PyErr_Occurred()) {
6619 return NULL;
6620 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006621 else {
6622 /* Check if the module was renamed. */
6623 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6624 if (item) {
6625 if (!PyUnicode_Check(item)) {
6626 PyErr_Format(PyExc_RuntimeError,
6627 "_compat_pickle.IMPORT_MAPPING values should be "
6628 "strings, not %.200s", Py_TYPE(item)->tp_name);
6629 return NULL;
6630 }
6631 module_name = item;
6632 }
6633 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006634 return NULL;
6635 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006636 }
6637 }
6638
Eric Snow3f9eee62017-09-15 16:35:20 -06006639 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006640 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006641 if (PyErr_Occurred())
6642 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006643 module = PyImport_Import(module_name);
6644 if (module == NULL)
6645 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006646 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006647 global = getattribute(module, global_name, self->proto >= 4);
6648 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006649 return global;
6650}
6651
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006652/*[clinic input]
6653
6654_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6655
6656Returns size in memory, in bytes.
6657[clinic start generated code]*/
6658
6659static Py_ssize_t
6660_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6661/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6662{
6663 Py_ssize_t res;
6664
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006665 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006666 if (self->memo != NULL)
6667 res += self->memo_size * sizeof(PyObject *);
6668 if (self->marks != NULL)
6669 res += self->marks_size * sizeof(Py_ssize_t);
6670 if (self->input_line != NULL)
6671 res += strlen(self->input_line) + 1;
6672 if (self->encoding != NULL)
6673 res += strlen(self->encoding) + 1;
6674 if (self->errors != NULL)
6675 res += strlen(self->errors) + 1;
6676 return res;
6677}
6678
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006679static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006680 _PICKLE_UNPICKLER_LOAD_METHODDEF
6681 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006682 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006683 {NULL, NULL} /* sentinel */
6684};
6685
6686static void
6687Unpickler_dealloc(UnpicklerObject *self)
6688{
6689 PyObject_GC_UnTrack((PyObject *)self);
6690 Py_XDECREF(self->readline);
6691 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006692 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006693 Py_XDECREF(self->stack);
6694 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006695 if (self->buffer.buf != NULL) {
6696 PyBuffer_Release(&self->buffer);
6697 self->buffer.buf = NULL;
6698 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006699
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006700 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006701 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006702 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006703 PyMem_Free(self->encoding);
6704 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006705
6706 Py_TYPE(self)->tp_free((PyObject *)self);
6707}
6708
6709static int
6710Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6711{
6712 Py_VISIT(self->readline);
6713 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006714 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006715 Py_VISIT(self->stack);
6716 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006717 return 0;
6718}
6719
6720static int
6721Unpickler_clear(UnpicklerObject *self)
6722{
6723 Py_CLEAR(self->readline);
6724 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006725 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006726 Py_CLEAR(self->stack);
6727 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006728 if (self->buffer.buf != NULL) {
6729 PyBuffer_Release(&self->buffer);
6730 self->buffer.buf = NULL;
6731 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006732
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006733 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006734 PyMem_Free(self->marks);
6735 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006736 PyMem_Free(self->input_line);
6737 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006738 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006739 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006740 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006741 self->errors = NULL;
6742
6743 return 0;
6744}
6745
Larry Hastings61272b72014-01-07 12:41:53 -08006746/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006747
6748_pickle.Unpickler.__init__
6749
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006750 file: object
6751 *
6752 fix_imports: bool = True
6753 encoding: str = 'ASCII'
6754 errors: str = 'strict'
6755
6756This takes a binary file for reading a pickle data stream.
6757
6758The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006759protocol argument is needed. Bytes past the pickled object's
6760representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006761
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006762The argument *file* must have two methods, a read() method that takes
6763an integer argument, and a readline() method that requires no
6764arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006765binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006766other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006767
6768Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006769which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006770generated by Python 2. If *fix_imports* is True, pickle will try to
6771map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006772*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006773instances pickled by Python 2; these default to 'ASCII' and 'strict',
6774respectively. The *encoding* can be 'bytes' to read these 8-bit
6775string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006776[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006777
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006778static int
Larry Hastings89964c42015-04-14 18:07:59 -04006779_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6780 int fix_imports, const char *encoding,
6781 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006782/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006783{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006784 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006785
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006786 /* In case of multiple __init__() calls, clear previous content. */
6787 if (self->read != NULL)
6788 (void)Unpickler_clear(self);
6789
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006790 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006791 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006792
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006793 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006794 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006795
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006796 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006797
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006798 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6799 &self->pers_func, &self->pers_func_self) < 0)
6800 {
6801 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006802 }
6803
6804 self->stack = (Pdata *)Pdata_New();
6805 if (self->stack == NULL)
Miss Islington (bot)758ad542018-09-28 23:01:48 -07006806 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006807
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006808 self->memo_size = 32;
6809 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006810 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006811 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006812
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006813 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006814
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006815 return 0;
6816}
6817
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006818
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006819/* Define a proxy object for the Unpickler's internal memo object. This is to
6820 * avoid breaking code like:
6821 * unpickler.memo.clear()
6822 * and
6823 * unpickler.memo = saved_memo
6824 * Is this a good idea? Not really, but we don't want to break code that uses
6825 * it. Note that we don't implement the entire mapping API here. This is
6826 * intentional, as these should be treated as black-box implementation details.
6827 *
6828 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006829 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006830 */
6831
Larry Hastings61272b72014-01-07 12:41:53 -08006832/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006833_pickle.UnpicklerMemoProxy.clear
6834
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006835Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006836[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006837
Larry Hastings3cceb382014-01-04 11:09:09 -08006838static PyObject *
6839_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006840/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006841{
6842 _Unpickler_MemoCleanup(self->unpickler);
6843 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6844 if (self->unpickler->memo == NULL)
6845 return NULL;
6846 Py_RETURN_NONE;
6847}
6848
Larry Hastings61272b72014-01-07 12:41:53 -08006849/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006850_pickle.UnpicklerMemoProxy.copy
6851
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006852Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006853[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006854
Larry Hastings3cceb382014-01-04 11:09:09 -08006855static PyObject *
6856_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006857/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006858{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07006859 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006860 PyObject *new_memo = PyDict_New();
6861 if (new_memo == NULL)
6862 return NULL;
6863
6864 for (i = 0; i < self->unpickler->memo_size; i++) {
6865 int status;
6866 PyObject *key, *value;
6867
6868 value = self->unpickler->memo[i];
6869 if (value == NULL)
6870 continue;
6871
6872 key = PyLong_FromSsize_t(i);
6873 if (key == NULL)
6874 goto error;
6875 status = PyDict_SetItem(new_memo, key, value);
6876 Py_DECREF(key);
6877 if (status < 0)
6878 goto error;
6879 }
6880 return new_memo;
6881
6882error:
6883 Py_DECREF(new_memo);
6884 return NULL;
6885}
6886
Larry Hastings61272b72014-01-07 12:41:53 -08006887/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006888_pickle.UnpicklerMemoProxy.__reduce__
6889
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006890Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006891[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006892
Larry Hastings3cceb382014-01-04 11:09:09 -08006893static PyObject *
6894_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006895/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006896{
6897 PyObject *reduce_value;
6898 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006899 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006900 if (contents == NULL)
6901 return NULL;
6902
6903 reduce_value = PyTuple_New(2);
6904 if (reduce_value == NULL) {
6905 Py_DECREF(contents);
6906 return NULL;
6907 }
6908 constructor_args = PyTuple_New(1);
6909 if (constructor_args == NULL) {
6910 Py_DECREF(contents);
6911 Py_DECREF(reduce_value);
6912 return NULL;
6913 }
6914 PyTuple_SET_ITEM(constructor_args, 0, contents);
6915 Py_INCREF((PyObject *)&PyDict_Type);
6916 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6917 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6918 return reduce_value;
6919}
6920
6921static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006922 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6923 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6924 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006925 {NULL, NULL} /* sentinel */
6926};
6927
6928static void
6929UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6930{
6931 PyObject_GC_UnTrack(self);
6932 Py_XDECREF(self->unpickler);
6933 PyObject_GC_Del((PyObject *)self);
6934}
6935
6936static int
6937UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6938 visitproc visit, void *arg)
6939{
6940 Py_VISIT(self->unpickler);
6941 return 0;
6942}
6943
6944static int
6945UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6946{
6947 Py_CLEAR(self->unpickler);
6948 return 0;
6949}
6950
6951static PyTypeObject UnpicklerMemoProxyType = {
6952 PyVarObject_HEAD_INIT(NULL, 0)
6953 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6954 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6955 0,
6956 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6957 0, /* tp_print */
6958 0, /* tp_getattr */
6959 0, /* tp_setattr */
6960 0, /* tp_compare */
6961 0, /* tp_repr */
6962 0, /* tp_as_number */
6963 0, /* tp_as_sequence */
6964 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006965 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006966 0, /* tp_call */
6967 0, /* tp_str */
6968 PyObject_GenericGetAttr, /* tp_getattro */
6969 PyObject_GenericSetAttr, /* tp_setattro */
6970 0, /* tp_as_buffer */
6971 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6972 0, /* tp_doc */
6973 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6974 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6975 0, /* tp_richcompare */
6976 0, /* tp_weaklistoffset */
6977 0, /* tp_iter */
6978 0, /* tp_iternext */
6979 unpicklerproxy_methods, /* tp_methods */
6980};
6981
6982static PyObject *
6983UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6984{
6985 UnpicklerMemoProxyObject *self;
6986
6987 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6988 &UnpicklerMemoProxyType);
6989 if (self == NULL)
6990 return NULL;
6991 Py_INCREF(unpickler);
6992 self->unpickler = unpickler;
6993 PyObject_GC_Track(self);
6994 return (PyObject *)self;
6995}
6996
6997/*****************************************************************************/
6998
6999
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007000static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007001Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007002{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007003 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007004}
7005
7006static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007007Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007008{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007009 PyObject **new_memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007010 size_t new_memo_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007011
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007012 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007013 PyErr_SetString(PyExc_TypeError,
7014 "attribute deletion is not supported");
7015 return -1;
7016 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007017
7018 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7019 UnpicklerObject *unpickler =
7020 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7021
7022 new_memo_size = unpickler->memo_size;
7023 new_memo = _Unpickler_NewMemo(new_memo_size);
7024 if (new_memo == NULL)
7025 return -1;
7026
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007027 for (size_t i = 0; i < new_memo_size; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007028 Py_XINCREF(unpickler->memo[i]);
7029 new_memo[i] = unpickler->memo[i];
7030 }
7031 }
7032 else if (PyDict_Check(obj)) {
7033 Py_ssize_t i = 0;
7034 PyObject *key, *value;
7035
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007036 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007037 new_memo = _Unpickler_NewMemo(new_memo_size);
7038 if (new_memo == NULL)
7039 return -1;
7040
7041 while (PyDict_Next(obj, &i, &key, &value)) {
7042 Py_ssize_t idx;
7043 if (!PyLong_Check(key)) {
7044 PyErr_SetString(PyExc_TypeError,
7045 "memo key must be integers");
7046 goto error;
7047 }
7048 idx = PyLong_AsSsize_t(key);
7049 if (idx == -1 && PyErr_Occurred())
7050 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007051 if (idx < 0) {
7052 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007053 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007054 goto error;
7055 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007056 if (_Unpickler_MemoPut(self, idx, value) < 0)
7057 goto error;
7058 }
7059 }
7060 else {
7061 PyErr_Format(PyExc_TypeError,
Miss Islington (bot)7beb8c52018-11-05 06:52:58 -08007062 "'memo' attribute must be an UnpicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007063 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007064 return -1;
7065 }
7066
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007067 _Unpickler_MemoCleanup(self);
7068 self->memo_size = new_memo_size;
7069 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007070
7071 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007072
7073 error:
7074 if (new_memo_size) {
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007075 for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007076 Py_XDECREF(new_memo[i]);
7077 }
7078 PyMem_FREE(new_memo);
7079 }
7080 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007081}
7082
7083static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007084Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007085{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007086 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007087 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007088 return NULL;
7089 }
7090 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007091}
7092
7093static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007094Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007095{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007096 if (value == NULL) {
7097 PyErr_SetString(PyExc_TypeError,
7098 "attribute deletion is not supported");
7099 return -1;
7100 }
7101 if (!PyCallable_Check(value)) {
7102 PyErr_SetString(PyExc_TypeError,
7103 "persistent_load must be a callable taking "
7104 "one argument");
7105 return -1;
7106 }
7107
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007108 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007109 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007110 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007111
7112 return 0;
7113}
7114
7115static PyGetSetDef Unpickler_getsets[] = {
7116 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7117 {"persistent_load", (getter)Unpickler_get_persload,
7118 (setter)Unpickler_set_persload},
7119 {NULL}
7120};
7121
7122static PyTypeObject Unpickler_Type = {
7123 PyVarObject_HEAD_INIT(NULL, 0)
7124 "_pickle.Unpickler", /*tp_name*/
7125 sizeof(UnpicklerObject), /*tp_basicsize*/
7126 0, /*tp_itemsize*/
7127 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7128 0, /*tp_print*/
7129 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007130 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007131 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007132 0, /*tp_repr*/
7133 0, /*tp_as_number*/
7134 0, /*tp_as_sequence*/
7135 0, /*tp_as_mapping*/
7136 0, /*tp_hash*/
7137 0, /*tp_call*/
7138 0, /*tp_str*/
7139 0, /*tp_getattro*/
7140 0, /*tp_setattro*/
7141 0, /*tp_as_buffer*/
7142 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007143 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007144 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7145 (inquiry)Unpickler_clear, /*tp_clear*/
7146 0, /*tp_richcompare*/
7147 0, /*tp_weaklistoffset*/
7148 0, /*tp_iter*/
7149 0, /*tp_iternext*/
7150 Unpickler_methods, /*tp_methods*/
7151 0, /*tp_members*/
7152 Unpickler_getsets, /*tp_getset*/
7153 0, /*tp_base*/
7154 0, /*tp_dict*/
7155 0, /*tp_descr_get*/
7156 0, /*tp_descr_set*/
7157 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007158 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007159 PyType_GenericAlloc, /*tp_alloc*/
7160 PyType_GenericNew, /*tp_new*/
7161 PyObject_GC_Del, /*tp_free*/
7162 0, /*tp_is_gc*/
7163};
7164
Larry Hastings61272b72014-01-07 12:41:53 -08007165/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007166
7167_pickle.dump
7168
7169 obj: object
7170 file: object
7171 protocol: object = NULL
7172 *
7173 fix_imports: bool = True
7174
7175Write a pickled representation of obj to the open file object file.
7176
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007177This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7178be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007179
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007180The optional *protocol* argument tells the pickler to use the given
7181protocol supported protocols are 0, 1, 2, 3 and 4. The default
7182protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007183
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007184Specifying a negative protocol version selects the highest protocol
7185version supported. The higher the protocol used, the more recent the
7186version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007187
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007188The *file* argument must have a write() method that accepts a single
7189bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007190writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007191this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007192
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007193If *fix_imports* is True and protocol is less than 3, pickle will try
7194to map the new Python 3 names to the old module names used in Python
71952, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007196[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007197
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007198static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007199_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007200 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007201/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007202{
7203 PicklerObject *pickler = _Pickler_New();
7204
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007205 if (pickler == NULL)
7206 return NULL;
7207
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007208 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007209 goto error;
7210
7211 if (_Pickler_SetOutputStream(pickler, file) < 0)
7212 goto error;
7213
7214 if (dump(pickler, obj) < 0)
7215 goto error;
7216
7217 if (_Pickler_FlushToFile(pickler) < 0)
7218 goto error;
7219
7220 Py_DECREF(pickler);
7221 Py_RETURN_NONE;
7222
7223 error:
7224 Py_XDECREF(pickler);
7225 return NULL;
7226}
7227
Larry Hastings61272b72014-01-07 12:41:53 -08007228/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007229
7230_pickle.dumps
7231
7232 obj: object
7233 protocol: object = NULL
7234 *
7235 fix_imports: bool = True
7236
7237Return the pickled representation of the object as a bytes object.
7238
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007239The optional *protocol* argument tells the pickler to use the given
7240protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7241protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007242
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007243Specifying a negative protocol version selects the highest protocol
7244version supported. The higher the protocol used, the more recent the
7245version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007246
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007247If *fix_imports* is True and *protocol* is less than 3, pickle will
7248try to map the new Python 3 names to the old module names used in
7249Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007250[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007251
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007252static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007253_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007254 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007255/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007256{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007257 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007258 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007259
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007260 if (pickler == NULL)
7261 return NULL;
7262
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007263 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007264 goto error;
7265
7266 if (dump(pickler, obj) < 0)
7267 goto error;
7268
7269 result = _Pickler_GetString(pickler);
7270 Py_DECREF(pickler);
7271 return result;
7272
7273 error:
7274 Py_XDECREF(pickler);
7275 return NULL;
7276}
7277
Larry Hastings61272b72014-01-07 12:41:53 -08007278/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007279
7280_pickle.load
7281
7282 file: object
7283 *
7284 fix_imports: bool = True
7285 encoding: str = 'ASCII'
7286 errors: str = 'strict'
7287
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007288Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007289
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007290This is equivalent to ``Unpickler(file).load()``, but may be more
7291efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007292
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007293The protocol version of the pickle is detected automatically, so no
7294protocol argument is needed. Bytes past the pickled object's
7295representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007296
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007297The argument *file* must have two methods, a read() method that takes
7298an integer argument, and a readline() method that requires no
7299arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007300binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007301other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007302
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007303Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007304which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007305generated by Python 2. If *fix_imports* is True, pickle will try to
7306map the old Python 2 names to the new names used in Python 3. The
7307*encoding* and *errors* tell pickle how to decode 8-bit string
7308instances pickled by Python 2; these default to 'ASCII' and 'strict',
7309respectively. The *encoding* can be 'bytes' to read these 8-bit
7310string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007311[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007312
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007313static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007314_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007315 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007316/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007317{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007318 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007319 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007320
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007321 if (unpickler == NULL)
7322 return NULL;
7323
7324 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7325 goto error;
7326
7327 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7328 goto error;
7329
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007330 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007331
7332 result = load(unpickler);
7333 Py_DECREF(unpickler);
7334 return result;
7335
7336 error:
7337 Py_XDECREF(unpickler);
7338 return NULL;
7339}
7340
Larry Hastings61272b72014-01-07 12:41:53 -08007341/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007342
7343_pickle.loads
7344
7345 data: object
7346 *
7347 fix_imports: bool = True
7348 encoding: str = 'ASCII'
7349 errors: str = 'strict'
7350
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007351Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007352
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007353The protocol version of the pickle is detected automatically, so no
7354protocol argument is needed. Bytes past the pickled object's
7355representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007356
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007357Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007358which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007359generated by Python 2. If *fix_imports* is True, pickle will try to
7360map the old Python 2 names to the new names used in Python 3. The
7361*encoding* and *errors* tell pickle how to decode 8-bit string
7362instances pickled by Python 2; these default to 'ASCII' and 'strict',
7363respectively. The *encoding* can be 'bytes' to read these 8-bit
7364string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007365[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007366
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007367static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007368_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007369 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007370/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007371{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007372 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007373 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007374
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007375 if (unpickler == NULL)
7376 return NULL;
7377
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007378 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007379 goto error;
7380
7381 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7382 goto error;
7383
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007384 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007385
7386 result = load(unpickler);
7387 Py_DECREF(unpickler);
7388 return result;
7389
7390 error:
7391 Py_XDECREF(unpickler);
7392 return NULL;
7393}
7394
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007395static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007396 _PICKLE_DUMP_METHODDEF
7397 _PICKLE_DUMPS_METHODDEF
7398 _PICKLE_LOAD_METHODDEF
7399 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007400 {NULL, NULL} /* sentinel */
7401};
7402
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007403static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007404pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007405{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007406 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007407 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007408}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007409
Stefan Krahf483b0f2013-12-14 13:43:10 +01007410static void
7411pickle_free(PyObject *m)
7412{
7413 _Pickle_ClearState(_Pickle_GetState(m));
7414}
7415
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007416static int
7417pickle_traverse(PyObject *m, visitproc visit, void *arg)
7418{
7419 PickleState *st = _Pickle_GetState(m);
7420 Py_VISIT(st->PickleError);
7421 Py_VISIT(st->PicklingError);
7422 Py_VISIT(st->UnpicklingError);
7423 Py_VISIT(st->dispatch_table);
7424 Py_VISIT(st->extension_registry);
7425 Py_VISIT(st->extension_cache);
7426 Py_VISIT(st->inverted_registry);
7427 Py_VISIT(st->name_mapping_2to3);
7428 Py_VISIT(st->import_mapping_2to3);
7429 Py_VISIT(st->name_mapping_3to2);
7430 Py_VISIT(st->import_mapping_3to2);
7431 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007432 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007433 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007434}
7435
7436static struct PyModuleDef _picklemodule = {
7437 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007438 "_pickle", /* m_name */
7439 pickle_module_doc, /* m_doc */
7440 sizeof(PickleState), /* m_size */
7441 pickle_methods, /* m_methods */
7442 NULL, /* m_reload */
7443 pickle_traverse, /* m_traverse */
7444 pickle_clear, /* m_clear */
7445 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007446};
7447
7448PyMODINIT_FUNC
7449PyInit__pickle(void)
7450{
7451 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007452 PickleState *st;
7453
7454 m = PyState_FindModule(&_picklemodule);
7455 if (m) {
7456 Py_INCREF(m);
7457 return m;
7458 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007459
7460 if (PyType_Ready(&Unpickler_Type) < 0)
7461 return NULL;
7462 if (PyType_Ready(&Pickler_Type) < 0)
7463 return NULL;
7464 if (PyType_Ready(&Pdata_Type) < 0)
7465 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007466 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7467 return NULL;
7468 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7469 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007470
7471 /* Create the module and add the functions. */
7472 m = PyModule_Create(&_picklemodule);
7473 if (m == NULL)
7474 return NULL;
7475
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007476 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007477 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7478 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007479 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007480 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7481 return NULL;
7482
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007483 st = _Pickle_GetState(m);
7484
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007485 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007486 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7487 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007488 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007489 st->PicklingError = \
7490 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7491 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007492 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007493 st->UnpicklingError = \
7494 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7495 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007496 return NULL;
7497
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007498 Py_INCREF(st->PickleError);
7499 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007500 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007501 Py_INCREF(st->PicklingError);
7502 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007503 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007504 Py_INCREF(st->UnpicklingError);
7505 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007506 return NULL;
7507
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007508 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007509 return NULL;
7510
7511 return m;
7512}