blob: 57bb771addde904ede033e203a44551252b71c4d [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001
2/* Core extension modules are built-in on some platforms (e.g. Windows). */
3#ifdef Py_BUILD_CORE
Eric Snowfc1bf872017-09-11 18:30:43 -07004#define Py_BUILD_CORE_BUILTIN
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#undef Py_BUILD_CORE
6#endif
7
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008#include "Python.h"
9#include "structmember.h"
10
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -080011PyDoc_STRVAR(pickle_module_doc,
12"Optimized C implementation for the Python pickle module.");
13
Larry Hastings61272b72014-01-07 12:41:53 -080014/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080016class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
17class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
18class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
19class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080020[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080022
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000023/* Bump this when new opcodes are added to the pickle protocol. */
24enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010025 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000026 DEFAULT_PROTOCOL = 3
27};
28
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029/* Pickle opcodes. These must be kept updated with pickle.py.
30 Extensive docs are in pickletools.py. */
31enum opcode {
32 MARK = '(',
33 STOP = '.',
34 POP = '0',
35 POP_MARK = '1',
36 DUP = '2',
37 FLOAT = 'F',
38 INT = 'I',
39 BININT = 'J',
40 BININT1 = 'K',
41 LONG = 'L',
42 BININT2 = 'M',
43 NONE = 'N',
44 PERSID = 'P',
45 BINPERSID = 'Q',
46 REDUCE = 'R',
47 STRING = 'S',
48 BINSTRING = 'T',
49 SHORT_BINSTRING = 'U',
50 UNICODE = 'V',
51 BINUNICODE = 'X',
52 APPEND = 'a',
53 BUILD = 'b',
54 GLOBAL = 'c',
55 DICT = 'd',
56 EMPTY_DICT = '}',
57 APPENDS = 'e',
58 GET = 'g',
59 BINGET = 'h',
60 INST = 'i',
61 LONG_BINGET = 'j',
62 LIST = 'l',
63 EMPTY_LIST = ']',
64 OBJ = 'o',
65 PUT = 'p',
66 BINPUT = 'q',
67 LONG_BINPUT = 'r',
68 SETITEM = 's',
69 TUPLE = 't',
70 EMPTY_TUPLE = ')',
71 SETITEMS = 'u',
72 BINFLOAT = 'G',
73
74 /* Protocol 2. */
75 PROTO = '\x80',
76 NEWOBJ = '\x81',
77 EXT1 = '\x82',
78 EXT2 = '\x83',
79 EXT4 = '\x84',
80 TUPLE1 = '\x85',
81 TUPLE2 = '\x86',
82 TUPLE3 = '\x87',
83 NEWTRUE = '\x88',
84 NEWFALSE = '\x89',
85 LONG1 = '\x8a',
86 LONG4 = '\x8b',
87
88 /* Protocol 3 (Python 3.x) */
89 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010090 SHORT_BINBYTES = 'C',
91
92 /* Protocol 4 */
93 SHORT_BINUNICODE = '\x8c',
94 BINUNICODE8 = '\x8d',
95 BINBYTES8 = '\x8e',
96 EMPTY_SET = '\x8f',
97 ADDITEMS = '\x90',
98 FROZENSET = '\x91',
99 NEWOBJ_EX = '\x92',
100 STACK_GLOBAL = '\x93',
101 MEMOIZE = '\x94',
102 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000103};
104
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000105enum {
106 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
107 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
108 break if this gets out of synch with pickle.py, but it's unclear that would
109 help anything either. */
110 BATCHSIZE = 1000,
111
112 /* Nesting limit until Pickler, when running in "fast mode", starts
113 checking for self-referential data-structures. */
114 FAST_NESTING_LIMIT = 50,
115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000116 /* Initial size of the write buffer of Pickler. */
117 WRITE_BUF_SIZE = 4096,
118
Antoine Pitrou04248a82010-10-12 20:51:21 +0000119 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100120 PREFETCH = 8192 * 16,
121
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200122 FRAME_SIZE_MIN = 4,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100123 FRAME_SIZE_TARGET = 64 * 1024,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100124 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000125};
126
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800127/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129/* State of the pickle module, per PEP 3121. */
130typedef struct {
131 /* Exception classes for pickle. */
132 PyObject *PickleError;
133 PyObject *PicklingError;
134 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800136 /* copyreg.dispatch_table, {type_object: pickling_function} */
137 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800139 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* copyreg._extension_registry, {(module_name, function_name): code} */
142 PyObject *extension_registry;
143 /* copyreg._extension_cache, {code: object} */
144 PyObject *extension_cache;
145 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
146 PyObject *inverted_registry;
147
148 /* Import mappings for compatibility with Python 2.x */
149
150 /* _compat_pickle.NAME_MAPPING,
151 {(oldmodule, oldname): (newmodule, newname)} */
152 PyObject *name_mapping_2to3;
153 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
154 PyObject *import_mapping_2to3;
155 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
156 PyObject *name_mapping_3to2;
157 PyObject *import_mapping_3to2;
158
159 /* codecs.encode, used for saving bytes in older protocols */
160 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300161 /* builtins.getattr, used for saving nested names with protocol < 4 */
162 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300163 /* functools.partial, used for implementing __newobj_ex__ with protocols
164 2 and 3 */
165 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800166} PickleState;
167
168/* Forward declaration of the _pickle module definition. */
169static struct PyModuleDef _picklemodule;
170
171/* Given a module object, get its per-module state. */
172static PickleState *
173_Pickle_GetState(PyObject *module)
174{
175 return (PickleState *)PyModule_GetState(module);
176}
177
178/* Find the module instance imported in the currently running sub-interpreter
179 and get its state. */
180static PickleState *
181_Pickle_GetGlobalState(void)
182{
183 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
184}
185
186/* Clear the given pickle module state. */
187static void
188_Pickle_ClearState(PickleState *st)
189{
190 Py_CLEAR(st->PickleError);
191 Py_CLEAR(st->PicklingError);
192 Py_CLEAR(st->UnpicklingError);
193 Py_CLEAR(st->dispatch_table);
194 Py_CLEAR(st->extension_registry);
195 Py_CLEAR(st->extension_cache);
196 Py_CLEAR(st->inverted_registry);
197 Py_CLEAR(st->name_mapping_2to3);
198 Py_CLEAR(st->import_mapping_2to3);
199 Py_CLEAR(st->name_mapping_3to2);
200 Py_CLEAR(st->import_mapping_3to2);
201 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300202 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100203 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204}
205
206/* Initialize the given pickle module state. */
207static int
208_Pickle_InitState(PickleState *st)
209{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300210 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800211 PyObject *copyreg = NULL;
212 PyObject *compat_pickle = NULL;
213 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300214 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800215
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300216 builtins = PyEval_GetBuiltins();
217 if (builtins == NULL)
218 goto error;
219 st->getattr = PyDict_GetItemString(builtins, "getattr");
220 if (st->getattr == NULL)
221 goto error;
222 Py_INCREF(st->getattr);
223
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800224 copyreg = PyImport_ImportModule("copyreg");
225 if (!copyreg)
226 goto error;
227 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
228 if (!st->dispatch_table)
229 goto error;
230 if (!PyDict_CheckExact(st->dispatch_table)) {
231 PyErr_Format(PyExc_RuntimeError,
232 "copyreg.dispatch_table should be a dict, not %.200s",
233 Py_TYPE(st->dispatch_table)->tp_name);
234 goto error;
235 }
236 st->extension_registry = \
237 PyObject_GetAttrString(copyreg, "_extension_registry");
238 if (!st->extension_registry)
239 goto error;
240 if (!PyDict_CheckExact(st->extension_registry)) {
241 PyErr_Format(PyExc_RuntimeError,
242 "copyreg._extension_registry should be a dict, "
243 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
244 goto error;
245 }
246 st->inverted_registry = \
247 PyObject_GetAttrString(copyreg, "_inverted_registry");
248 if (!st->inverted_registry)
249 goto error;
250 if (!PyDict_CheckExact(st->inverted_registry)) {
251 PyErr_Format(PyExc_RuntimeError,
252 "copyreg._inverted_registry should be a dict, "
253 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
254 goto error;
255 }
256 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
257 if (!st->extension_cache)
258 goto error;
259 if (!PyDict_CheckExact(st->extension_cache)) {
260 PyErr_Format(PyExc_RuntimeError,
261 "copyreg._extension_cache should be a dict, "
262 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
263 goto error;
264 }
265 Py_CLEAR(copyreg);
266
267 /* Load the 2.x -> 3.x stdlib module mapping tables */
268 compat_pickle = PyImport_ImportModule("_compat_pickle");
269 if (!compat_pickle)
270 goto error;
271 st->name_mapping_2to3 = \
272 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
273 if (!st->name_mapping_2to3)
274 goto error;
275 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
276 PyErr_Format(PyExc_RuntimeError,
277 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
278 Py_TYPE(st->name_mapping_2to3)->tp_name);
279 goto error;
280 }
281 st->import_mapping_2to3 = \
282 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
283 if (!st->import_mapping_2to3)
284 goto error;
285 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
286 PyErr_Format(PyExc_RuntimeError,
287 "_compat_pickle.IMPORT_MAPPING should be a dict, "
288 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
289 goto error;
290 }
291 /* ... and the 3.x -> 2.x mapping tables */
292 st->name_mapping_3to2 = \
293 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
294 if (!st->name_mapping_3to2)
295 goto error;
296 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
297 PyErr_Format(PyExc_RuntimeError,
298 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
299 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
300 goto error;
301 }
302 st->import_mapping_3to2 = \
303 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
304 if (!st->import_mapping_3to2)
305 goto error;
306 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
307 PyErr_Format(PyExc_RuntimeError,
308 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
309 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
310 goto error;
311 }
312 Py_CLEAR(compat_pickle);
313
314 codecs = PyImport_ImportModule("codecs");
315 if (codecs == NULL)
316 goto error;
317 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
318 if (st->codecs_encode == NULL) {
319 goto error;
320 }
321 if (!PyCallable_Check(st->codecs_encode)) {
322 PyErr_Format(PyExc_RuntimeError,
323 "codecs.encode should be a callable, not %.200s",
324 Py_TYPE(st->codecs_encode)->tp_name);
325 goto error;
326 }
327 Py_CLEAR(codecs);
328
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300329 functools = PyImport_ImportModule("functools");
330 if (!functools)
331 goto error;
332 st->partial = PyObject_GetAttrString(functools, "partial");
333 if (!st->partial)
334 goto error;
335 Py_CLEAR(functools);
336
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 return 0;
338
339 error:
340 Py_CLEAR(copyreg);
341 Py_CLEAR(compat_pickle);
342 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300343 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800344 _Pickle_ClearState(st);
345 return -1;
346}
347
348/* Helper for calling a function with a single argument quickly.
349
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 This function steals the reference of the given argument. */
351static PyObject *
352_Pickle_FastCall(PyObject *func, PyObject *obj)
353{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 PyObject *result;
355
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100356 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200357 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800358 return result;
359}
360
361/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000362
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200363/* Retrieve and deconstruct a method for avoiding a reference cycle
364 (pickler -> bound method of pickler -> pickler) */
365static int
366init_method_ref(PyObject *self, _Py_Identifier *name,
367 PyObject **method_func, PyObject **method_self)
368{
369 PyObject *func, *func2;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200370 int ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200371
372 /* *method_func and *method_self should be consistent. All refcount decrements
373 should be occurred after setting *method_self and *method_func. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200374 ret = _PyObject_LookupAttrId(self, name, &func);
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200375 if (func == NULL) {
376 *method_self = NULL;
377 Py_CLEAR(*method_func);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200378 return ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200379 }
380
381 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
382 /* Deconstruct a bound Python method */
383 func2 = PyMethod_GET_FUNCTION(func);
384 Py_INCREF(func2);
385 *method_self = self; /* borrowed */
386 Py_XSETREF(*method_func, func2);
387 Py_DECREF(func);
388 return 0;
389 }
390 else {
391 *method_self = NULL;
392 Py_XSETREF(*method_func, func);
393 return 0;
394 }
395}
396
397/* Bind a method if it was deconstructed */
398static PyObject *
399reconstruct_method(PyObject *func, PyObject *self)
400{
401 if (self) {
402 return PyMethod_New(func, self);
403 }
404 else {
405 Py_INCREF(func);
406 return func;
407 }
408}
409
410static PyObject *
411call_method(PyObject *func, PyObject *self, PyObject *obj)
412{
413 if (self) {
414 return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
415 }
416 else {
417 return PyObject_CallFunctionObjArgs(func, obj, NULL);
418 }
419}
420
421/*************************************************************************/
422
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000423/* Internal data type used as the unpickling stack. */
424typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000425 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000426 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200427 int mark_set; /* is MARK set? */
428 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000429 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000430} Pdata;
431
432static void
433Pdata_dealloc(Pdata *self)
434{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200435 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436 while (--i >= 0) {
437 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000438 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000439 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000440 PyObject_Del(self);
441}
442
443static PyTypeObject Pdata_Type = {
444 PyVarObject_HEAD_INIT(NULL, 0)
445 "_pickle.Pdata", /*tp_name*/
446 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200447 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000448 (destructor)Pdata_dealloc, /*tp_dealloc*/
449};
450
451static PyObject *
452Pdata_New(void)
453{
454 Pdata *self;
455
456 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
457 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000458 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200459 self->mark_set = 0;
460 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000461 self->allocated = 8;
462 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000463 if (self->data)
464 return (PyObject *)self;
465 Py_DECREF(self);
466 return PyErr_NoMemory();
467}
468
469
470/* Retain only the initial clearto items. If clearto >= the current
471 * number of items, this is a (non-erroneous) NOP.
472 */
473static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200474Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000475{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200476 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200478 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000479 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 return 0;
481
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000482 while (--i >= clearto) {
483 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000484 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000485 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000486 return 0;
487}
488
489static int
490Pdata_grow(Pdata *self)
491{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000492 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200493 size_t allocated = (size_t)self->allocated;
494 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000495
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000496 new_allocated = (allocated >> 3) + 6;
497 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200498 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000499 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000500 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500501 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000502 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000503 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000504
505 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200506 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000507 return 0;
508
509 nomemory:
510 PyErr_NoMemory();
511 return -1;
512}
513
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200514static int
515Pdata_stack_underflow(Pdata *self)
516{
517 PickleState *st = _Pickle_GetGlobalState();
518 PyErr_SetString(st->UnpicklingError,
519 self->mark_set ?
520 "unexpected MARK found" :
521 "unpickling stack underflow");
522 return -1;
523}
524
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000525/* D is a Pdata*. Pop the topmost element and store it into V, which
526 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
527 * is raised and V is set to NULL.
528 */
529static PyObject *
530Pdata_pop(Pdata *self)
531{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200532 if (Py_SIZE(self) <= self->fence) {
533 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000534 return NULL;
535 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000536 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000537}
538#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
539
540static int
541Pdata_push(Pdata *self, PyObject *obj)
542{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000543 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000544 return -1;
545 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000546 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000547 return 0;
548}
549
550/* Push an object on stack, transferring its ownership to the stack. */
551#define PDATA_PUSH(D, O, ER) do { \
552 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
553
554/* Push an object on stack, adding a new reference to the object. */
555#define PDATA_APPEND(D, O, ER) do { \
556 Py_INCREF((O)); \
557 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
558
559static PyObject *
560Pdata_poptuple(Pdata *self, Py_ssize_t start)
561{
562 PyObject *tuple;
563 Py_ssize_t len, i, j;
564
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200565 if (start < self->fence) {
566 Pdata_stack_underflow(self);
567 return NULL;
568 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000569 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000570 tuple = PyTuple_New(len);
571 if (tuple == NULL)
572 return NULL;
573 for (i = start, j = 0; j < len; i++, j++)
574 PyTuple_SET_ITEM(tuple, j, self->data[i]);
575
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000576 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000577 return tuple;
578}
579
580static PyObject *
581Pdata_poplist(Pdata *self, Py_ssize_t start)
582{
583 PyObject *list;
584 Py_ssize_t len, i, j;
585
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000586 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000587 list = PyList_New(len);
588 if (list == NULL)
589 return NULL;
590 for (i = start, j = 0; j < len; i++, j++)
591 PyList_SET_ITEM(list, j, self->data[i]);
592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000594 return list;
595}
596
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000597typedef struct {
598 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200599 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000600} PyMemoEntry;
601
602typedef struct {
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700603 size_t mt_mask;
604 size_t mt_used;
605 size_t mt_allocated;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000606 PyMemoEntry *mt_table;
607} PyMemoTable;
608
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000609typedef struct PicklerObject {
610 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000611 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000612 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000613 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000614 PyObject *pers_func; /* persistent_id() method, can be NULL */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200615 PyObject *pers_func_self; /* borrowed reference to self if pers_func
616 is an unbound method, NULL otherwise */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100617 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000618
619 PyObject *write; /* write() method of the output stream. */
620 PyObject *output_buffer; /* Write into a local bytearray buffer before
621 flushing to the stream. */
622 Py_ssize_t output_len; /* Length of output_buffer. */
623 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000624 int proto; /* Pickle protocol number, >= 0 */
625 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100626 int framing; /* True when framing is enabled, proto >= 4 */
627 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000628 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100629 is no frame currently open. */
630
631 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000632 int fast; /* Enable fast mode if set to a true value.
633 The fast mode disable the usage of memo,
634 therefore speeding the pickling process by
635 not generating superfluous PUT opcodes. It
636 should not be used if with self-referential
637 objects. */
638 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000639 int fix_imports; /* Indicate whether Pickler should fix
640 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000641 PyObject *fast_memo;
642} PicklerObject;
643
644typedef struct UnpicklerObject {
645 PyObject_HEAD
646 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000647
648 /* The unpickler memo is just an array of PyObject *s. Using a dict
649 is unnecessary, since the keys are contiguous ints. */
650 PyObject **memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700651 size_t memo_size; /* Capacity of the memo array */
652 size_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000653
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000654 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200655 PyObject *pers_func_self; /* borrowed reference to self if pers_func
656 is an unbound method, NULL otherwise */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000657
658 Py_buffer buffer;
659 char *input_buffer;
660 char *input_line;
661 Py_ssize_t input_len;
662 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000663 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100664
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000665 PyObject *read; /* read() method of the input stream. */
666 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000667 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000668
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000669 char *encoding; /* Name of the encoding to be used for
670 decoding strings pickled using Python
671 2.x. The default value is "ASCII" */
672 char *errors; /* Name of errors handling scheme to used when
673 decoding strings. The default value is
674 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500675 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000676 objects. */
677 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
678 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000679 int proto; /* Protocol of the pickle loaded. */
680 int fix_imports; /* Indicate whether Unpickler should fix
681 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000682} UnpicklerObject;
683
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200684typedef struct {
685 PyObject_HEAD
686 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
687} PicklerMemoProxyObject;
688
689typedef struct {
690 PyObject_HEAD
691 UnpicklerObject *unpickler;
692} UnpicklerMemoProxyObject;
693
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000694/* Forward declarations */
695static int save(PicklerObject *, PyObject *, int);
696static int save_reduce(PicklerObject *, PyObject *, PyObject *);
697static PyTypeObject Pickler_Type;
698static PyTypeObject Unpickler_Type;
699
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200700#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000701
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000702/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300703 A custom hashtable mapping void* to Python ints. This is used by the pickler
704 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000705 a bunch of unnecessary object creation. This makes a huge performance
706 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000707
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000708#define MT_MINSIZE 8
709#define PERTURB_SHIFT 5
710
711
712static PyMemoTable *
713PyMemoTable_New(void)
714{
715 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
716 if (memo == NULL) {
717 PyErr_NoMemory();
718 return NULL;
719 }
720
721 memo->mt_used = 0;
722 memo->mt_allocated = MT_MINSIZE;
723 memo->mt_mask = MT_MINSIZE - 1;
724 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
725 if (memo->mt_table == NULL) {
726 PyMem_FREE(memo);
727 PyErr_NoMemory();
728 return NULL;
729 }
730 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
731
732 return memo;
733}
734
735static PyMemoTable *
736PyMemoTable_Copy(PyMemoTable *self)
737{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000738 PyMemoTable *new = PyMemoTable_New();
739 if (new == NULL)
740 return NULL;
741
742 new->mt_used = self->mt_used;
743 new->mt_allocated = self->mt_allocated;
744 new->mt_mask = self->mt_mask;
745 /* The table we get from _New() is probably smaller than we wanted.
746 Free it and allocate one that's the right size. */
747 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500748 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000749 if (new->mt_table == NULL) {
750 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200751 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000752 return NULL;
753 }
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700754 for (size_t i = 0; i < self->mt_allocated; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000755 Py_XINCREF(self->mt_table[i].me_key);
756 }
757 memcpy(new->mt_table, self->mt_table,
758 sizeof(PyMemoEntry) * self->mt_allocated);
759
760 return new;
761}
762
763static Py_ssize_t
764PyMemoTable_Size(PyMemoTable *self)
765{
766 return self->mt_used;
767}
768
769static int
770PyMemoTable_Clear(PyMemoTable *self)
771{
772 Py_ssize_t i = self->mt_allocated;
773
774 while (--i >= 0) {
775 Py_XDECREF(self->mt_table[i].me_key);
776 }
777 self->mt_used = 0;
778 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
779 return 0;
780}
781
782static void
783PyMemoTable_Del(PyMemoTable *self)
784{
785 if (self == NULL)
786 return;
787 PyMemoTable_Clear(self);
788
789 PyMem_FREE(self->mt_table);
790 PyMem_FREE(self);
791}
792
793/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
794 can be considerably simpler than dictobject.c's lookdict(). */
795static PyMemoEntry *
796_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
797{
798 size_t i;
799 size_t perturb;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700800 size_t mask = self->mt_mask;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000801 PyMemoEntry *table = self->mt_table;
802 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000803 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000804
805 i = hash & mask;
806 entry = &table[i];
807 if (entry->me_key == NULL || entry->me_key == key)
808 return entry;
809
810 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
811 i = (i << 2) + i + perturb + 1;
812 entry = &table[i & mask];
813 if (entry->me_key == NULL || entry->me_key == key)
814 return entry;
815 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700816 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000817}
818
819/* Returns -1 on failure, 0 on success. */
820static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700821_PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000822{
823 PyMemoEntry *oldtable = NULL;
824 PyMemoEntry *oldentry, *newentry;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700825 size_t new_size = MT_MINSIZE;
826 size_t to_process;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000827
828 assert(min_size > 0);
829
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700830 if (min_size > PY_SSIZE_T_MAX) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000831 PyErr_NoMemory();
832 return -1;
833 }
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700834
835 /* Find the smallest valid table size >= min_size. */
836 while (new_size < min_size) {
837 new_size <<= 1;
838 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000839 /* new_size needs to be a power of two. */
840 assert((new_size & (new_size - 1)) == 0);
841
842 /* Allocate new table. */
843 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500844 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000845 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200846 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000847 PyErr_NoMemory();
848 return -1;
849 }
850 self->mt_allocated = new_size;
851 self->mt_mask = new_size - 1;
852 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
853
854 /* Copy entries from the old table. */
855 to_process = self->mt_used;
856 for (oldentry = oldtable; to_process > 0; oldentry++) {
857 if (oldentry->me_key != NULL) {
858 to_process--;
859 /* newentry is a pointer to a chunk of the new
860 mt_table, so we're setting the key:value pair
861 in-place. */
862 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
863 newentry->me_key = oldentry->me_key;
864 newentry->me_value = oldentry->me_value;
865 }
866 }
867
868 /* Deallocate the old table. */
869 PyMem_FREE(oldtable);
870 return 0;
871}
872
873/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200874static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000875PyMemoTable_Get(PyMemoTable *self, PyObject *key)
876{
877 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
878 if (entry->me_key == NULL)
879 return NULL;
880 return &entry->me_value;
881}
882
883/* Returns -1 on failure, 0 on success. */
884static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200885PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000886{
887 PyMemoEntry *entry;
888
889 assert(key != NULL);
890
891 entry = _PyMemoTable_Lookup(self, key);
892 if (entry->me_key != NULL) {
893 entry->me_value = value;
894 return 0;
895 }
896 Py_INCREF(key);
897 entry->me_key = key;
898 entry->me_value = value;
899 self->mt_used++;
900
901 /* If we added a key, we can safely resize. Otherwise just return!
902 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
903 *
904 * Quadrupling the size improves average table sparseness
905 * (reducing collisions) at the cost of some memory. It also halves
906 * the number of expensive resize operations in a growing memo table.
907 *
908 * Very large memo tables (over 50K items) use doubling instead.
909 * This may help applications with severe memory constraints.
910 */
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700911 if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000912 return 0;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -0700913 }
914 // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow.
915 size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used;
916 return _PyMemoTable_ResizeTable(self, desired_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000917}
918
919#undef MT_MINSIZE
920#undef PERTURB_SHIFT
921
922/*************************************************************************/
923
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000925static int
926_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000927{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300928 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200929 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000930 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000931 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000932 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100933 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 return 0;
935}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000936
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100937static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100938_write_size64(char *out, size_t value)
939{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200940 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800941
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200942 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800943
944 for (i = 0; i < sizeof(size_t); i++) {
945 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
946 }
947 for (i = sizeof(size_t); i < 8; i++) {
948 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800949 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100950}
951
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100952static int
953_Pickler_CommitFrame(PicklerObject *self)
954{
955 size_t frame_len;
956 char *qdata;
957
958 if (!self->framing || self->frame_start == -1)
959 return 0;
960 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
961 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200962 if (frame_len >= FRAME_SIZE_MIN) {
963 qdata[0] = FRAME;
964 _write_size64(qdata + 1, frame_len);
965 }
966 else {
967 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
968 self->output_len -= FRAME_HEADER_SIZE;
969 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100970 self->frame_start = -1;
971 return 0;
972}
973
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000974static PyObject *
975_Pickler_GetString(PicklerObject *self)
976{
977 PyObject *output_buffer = self->output_buffer;
978
979 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100980
981 if (_Pickler_CommitFrame(self))
982 return NULL;
983
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000984 self->output_buffer = NULL;
985 /* Resize down to exact size */
986 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
987 return NULL;
988 return output_buffer;
989}
990
991static int
992_Pickler_FlushToFile(PicklerObject *self)
993{
994 PyObject *output, *result;
995
996 assert(self->write != NULL);
997
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100998 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 output = _Pickler_GetString(self);
1000 if (output == NULL)
1001 return -1;
1002
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001003 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001004 Py_XDECREF(result);
1005 return (result == NULL) ? -1 : 0;
1006}
1007
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001008static int
1009_Pickler_OpcodeBoundary(PicklerObject *self)
1010{
1011 Py_ssize_t frame_len;
1012
1013 if (!self->framing || self->frame_start == -1) {
1014 return 0;
1015 }
1016 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1017 if (frame_len >= FRAME_SIZE_TARGET) {
1018 if(_Pickler_CommitFrame(self)) {
1019 return -1;
1020 }
Miss Islington (bot)e86db342018-02-03 17:41:43 -08001021 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001022 * file and reuse the pickler buffer for the next frame so as
1023 * to limit memory usage when dumping large complex objects to
1024 * a file.
1025 *
1026 * self->write is NULL when called via dumps.
1027 */
1028 if (self->write != NULL) {
1029 if (_Pickler_FlushToFile(self) < 0) {
1030 return -1;
1031 }
1032 if (_Pickler_ClearBuffer(self) < 0) {
1033 return -1;
1034 }
1035 }
1036 }
1037 return 0;
1038}
1039
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001040static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001041_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001043 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001045 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046
1047 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001048 need_new_frame = (self->framing && self->frame_start == -1);
1049
1050 if (need_new_frame)
1051 n = data_len + FRAME_HEADER_SIZE;
1052 else
1053 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001054
1055 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001056 if (required > self->max_output_len) {
1057 /* Make place in buffer for the pickle chunk */
1058 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1059 PyErr_NoMemory();
1060 return -1;
1061 }
1062 self->max_output_len = (self->output_len + n) / 2 * 3;
1063 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1064 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001065 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001066 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001067 if (need_new_frame) {
1068 /* Setup new frame */
1069 Py_ssize_t frame_start = self->output_len;
1070 self->frame_start = frame_start;
1071 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1072 /* Write an invalid value, for debugging */
1073 buffer[frame_start + i] = 0xFE;
1074 }
1075 self->output_len += FRAME_HEADER_SIZE;
1076 }
1077 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001078 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001079 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001080 buffer[self->output_len + i] = s[i];
1081 }
1082 }
1083 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001084 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001085 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001086 self->output_len += data_len;
1087 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001088}
1089
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001090static PicklerObject *
1091_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001092{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001093 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001094
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001095 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1096 if (self == NULL)
1097 return NULL;
1098
1099 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001100 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001101 self->write = NULL;
1102 self->proto = 0;
1103 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001104 self->framing = 0;
1105 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001106 self->fast = 0;
1107 self->fast_nesting = 0;
1108 self->fix_imports = 0;
1109 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001110 self->max_output_len = WRITE_BUF_SIZE;
1111 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001112
1113 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001114 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1115 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001116
1117 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001118 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001119 return NULL;
1120 }
1121 return self;
1122}
1123
1124static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001125_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001126{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001127 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001129 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001131 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001132 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001133 proto = PyLong_AsLong(protocol);
1134 if (proto < 0) {
1135 if (proto == -1 && PyErr_Occurred())
1136 return -1;
1137 proto = HIGHEST_PROTOCOL;
1138 }
1139 else if (proto > HIGHEST_PROTOCOL) {
1140 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1141 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001142 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001143 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001145 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001146 self->bin = proto > 0;
1147 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001148 return 0;
1149}
1150
1151/* Returns -1 (with an exception set) on failure, 0 on success. This may
1152 be called once on a freshly created Pickler. */
1153static int
1154_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1155{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001156 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001157 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001158 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1159 return -1;
1160 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001161 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001162 PyErr_SetString(PyExc_TypeError,
1163 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001164 return -1;
1165 }
1166
1167 return 0;
1168}
1169
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001170/* Returns the size of the input on success, -1 on failure. This takes its
1171 own reference to `input`. */
1172static Py_ssize_t
1173_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1174{
1175 if (self->buffer.buf != NULL)
1176 PyBuffer_Release(&self->buffer);
1177 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1178 return -1;
1179 self->input_buffer = self->buffer.buf;
1180 self->input_len = self->buffer.len;
1181 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001182 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001183 return self->input_len;
1184}
1185
Antoine Pitrou04248a82010-10-12 20:51:21 +00001186static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001187bad_readline(void)
1188{
1189 PickleState *st = _Pickle_GetGlobalState();
1190 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1191 return -1;
1192}
1193
1194static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001195_Unpickler_SkipConsumed(UnpicklerObject *self)
1196{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001197 Py_ssize_t consumed;
1198 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001199
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001200 consumed = self->next_read_idx - self->prefetched_idx;
1201 if (consumed <= 0)
1202 return 0;
1203
1204 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001205 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001206 r = PyObject_CallFunction(self->read, "n", consumed);
1207 if (r == NULL)
1208 return -1;
1209 Py_DECREF(r);
1210
1211 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001212 return 0;
1213}
1214
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001215static const Py_ssize_t READ_WHOLE_LINE = -1;
1216
1217/* If reading from a file, we need to only pull the bytes we need, since there
1218 may be multiple pickle objects arranged contiguously in the same input
1219 buffer.
1220
1221 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1222 bytes from the input stream/buffer.
1223
1224 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1225 failure; on success, returns the number of bytes read from the file.
1226
1227 On success, self->input_len will be 0; this is intentional so that when
1228 unpickling from a file, the "we've run out of data" code paths will trigger,
1229 causing the Unpickler to go back to the file for more data. Use the returned
1230 size to tell you how much data you can process. */
1231static Py_ssize_t
1232_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1233{
1234 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001235 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001236
1237 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001238
Antoine Pitrou04248a82010-10-12 20:51:21 +00001239 if (_Unpickler_SkipConsumed(self) < 0)
1240 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001241
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001242 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001243 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001244 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001245 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001246 PyObject *len;
1247 /* Prefetch some data without advancing the file pointer, if possible */
1248 if (self->peek && n < PREFETCH) {
1249 len = PyLong_FromSsize_t(PREFETCH);
1250 if (len == NULL)
1251 return -1;
1252 data = _Pickle_FastCall(self->peek, len);
1253 if (data == NULL) {
1254 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1255 return -1;
1256 /* peek() is probably not supported by the given file object */
1257 PyErr_Clear();
1258 Py_CLEAR(self->peek);
1259 }
1260 else {
1261 read_size = _Unpickler_SetStringInput(self, data);
1262 Py_DECREF(data);
1263 self->prefetched_idx = 0;
1264 if (n <= read_size)
1265 return n;
1266 }
1267 }
1268 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001269 if (len == NULL)
1270 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001271 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001272 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001273 if (data == NULL)
1274 return -1;
1275
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001276 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001277 Py_DECREF(data);
1278 return read_size;
1279}
1280
Victor Stinner19ed27e2016-05-20 11:42:37 +02001281/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001282static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001283_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001284{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001285 Py_ssize_t num_read;
1286
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001287 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001288 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1289 PickleState *st = _Pickle_GetGlobalState();
1290 PyErr_SetString(st->UnpicklingError,
1291 "read would overflow (invalid bytecode)");
1292 return -1;
1293 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001294
1295 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1296 assert(self->next_read_idx + n > self->input_len);
1297
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001298 if (!self->read)
1299 return bad_readline();
1300
Antoine Pitrou04248a82010-10-12 20:51:21 +00001301 num_read = _Unpickler_ReadFromFile(self, n);
1302 if (num_read < 0)
1303 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001304 if (num_read < n)
1305 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001306 *s = self->input_buffer;
1307 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001308 return n;
1309}
1310
Victor Stinner19ed27e2016-05-20 11:42:37 +02001311/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1312
1313 This should be used for all data reads, rather than accessing the unpickler's
1314 input buffer directly. This method deals correctly with reading from input
1315 streams, which the input buffer doesn't deal with.
1316
1317 Note that when reading from a file-like object, self->next_read_idx won't
1318 be updated (it should remain at 0 for the entire unpickling process). You
1319 should use this function's return value to know how many bytes you can
1320 consume.
1321
1322 Returns -1 (with an exception set) on failure. On success, return the
1323 number of chars read. */
1324#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001325 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001326 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1327 (self)->next_read_idx += (n), \
1328 (n)) \
1329 : _Unpickler_ReadImpl(self, (s), (n)))
1330
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001331static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001332_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1333 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001334{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001335 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001336 if (input_line == NULL) {
1337 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001338 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001339 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001340
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001341 memcpy(input_line, line, len);
1342 input_line[len] = '\0';
1343 self->input_line = input_line;
1344 *result = self->input_line;
1345 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001346}
1347
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001348/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001349 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001350
1351 Returns the number of chars read, or -1 on failure. */
1352static Py_ssize_t
1353_Unpickler_Readline(UnpicklerObject *self, char **result)
1354{
1355 Py_ssize_t i, num_read;
1356
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001357 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001358 if (self->input_buffer[i] == '\n') {
1359 char *line_start = self->input_buffer + self->next_read_idx;
1360 num_read = i - self->next_read_idx + 1;
1361 self->next_read_idx = i + 1;
1362 return _Unpickler_CopyLine(self, line_start, num_read, result);
1363 }
1364 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001365 if (!self->read)
1366 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001367
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001368 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1369 if (num_read < 0)
1370 return -1;
1371 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1372 return bad_readline();
1373 self->next_read_idx = num_read;
1374 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001375}
1376
1377/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1378 will be modified in place. */
1379static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001380_Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001381{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001382 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001383
1384 assert(new_size > self->memo_size);
1385
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001386 PyObject **memo_new = self->memo;
1387 PyMem_RESIZE(memo_new, PyObject *, new_size);
1388 if (memo_new == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389 PyErr_NoMemory();
1390 return -1;
1391 }
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001392 self->memo = memo_new;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001393 for (i = self->memo_size; i < new_size; i++)
1394 self->memo[i] = NULL;
1395 self->memo_size = new_size;
1396 return 0;
1397}
1398
1399/* Returns NULL if idx is out of bounds. */
1400static PyObject *
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001401_Unpickler_MemoGet(UnpicklerObject *self, size_t idx)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001402{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001403 if (idx >= self->memo_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001404 return NULL;
1405
1406 return self->memo[idx];
1407}
1408
1409/* Returns -1 (with an exception set) on failure, 0 on success.
1410 This takes its own reference to `value`. */
1411static int
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07001412_Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001413{
1414 PyObject *old_item;
1415
1416 if (idx >= self->memo_size) {
1417 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1418 return -1;
1419 assert(idx < self->memo_size);
1420 }
1421 Py_INCREF(value);
1422 old_item = self->memo[idx];
1423 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001424 if (old_item != NULL) {
1425 Py_DECREF(old_item);
1426 }
1427 else {
1428 self->memo_len++;
1429 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001430 return 0;
1431}
1432
1433static PyObject **
1434_Unpickler_NewMemo(Py_ssize_t new_size)
1435{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001436 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001437 if (memo == NULL) {
1438 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001439 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001440 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001441 memset(memo, 0, new_size * sizeof(PyObject *));
1442 return memo;
1443}
1444
1445/* Free the unpickler's memo, taking care to decref any items left in it. */
1446static void
1447_Unpickler_MemoCleanup(UnpicklerObject *self)
1448{
1449 Py_ssize_t i;
1450 PyObject **memo = self->memo;
1451
1452 if (self->memo == NULL)
1453 return;
1454 self->memo = NULL;
1455 i = self->memo_size;
1456 while (--i >= 0) {
1457 Py_XDECREF(memo[i]);
1458 }
1459 PyMem_FREE(memo);
1460}
1461
1462static UnpicklerObject *
1463_Unpickler_New(void)
1464{
1465 UnpicklerObject *self;
1466
1467 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1468 if (self == NULL)
1469 return NULL;
1470
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001471 self->pers_func = NULL;
1472 self->input_buffer = NULL;
1473 self->input_line = NULL;
1474 self->input_len = 0;
1475 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001476 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001477 self->read = NULL;
1478 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001479 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001480 self->encoding = NULL;
1481 self->errors = NULL;
1482 self->marks = NULL;
1483 self->num_marks = 0;
1484 self->marks_size = 0;
1485 self->proto = 0;
1486 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001487 memset(&self->buffer, 0, sizeof(Py_buffer));
1488 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001489 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001490 self->memo = _Unpickler_NewMemo(self->memo_size);
1491 self->stack = (Pdata *)Pdata_New();
1492
1493 if (self->memo == NULL || self->stack == NULL) {
1494 Py_DECREF(self);
1495 return NULL;
1496 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001497
1498 return self;
1499}
1500
1501/* Returns -1 (with an exception set) on failure, 0 on success. This may
1502 be called once on a freshly created Pickler. */
1503static int
1504_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1505{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001506 _Py_IDENTIFIER(peek);
1507 _Py_IDENTIFIER(read);
1508 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001509
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001510 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1511 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001512 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001513 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1514 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001515 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001516 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001517 PyErr_SetString(PyExc_TypeError,
1518 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001519 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001520 Py_CLEAR(self->read);
1521 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001522 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001523 return -1;
1524 }
1525 return 0;
1526}
1527
1528/* Returns -1 (with an exception set) on failure, 0 on success. This may
1529 be called once on a freshly created Pickler. */
1530static int
1531_Unpickler_SetInputEncoding(UnpicklerObject *self,
1532 const char *encoding,
1533 const char *errors)
1534{
1535 if (encoding == NULL)
1536 encoding = "ASCII";
1537 if (errors == NULL)
1538 errors = "strict";
1539
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001540 self->encoding = _PyMem_Strdup(encoding);
1541 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001542 if (self->encoding == NULL || self->errors == NULL) {
1543 PyErr_NoMemory();
1544 return -1;
1545 }
1546 return 0;
1547}
1548
1549/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001550static int
1551memo_get(PicklerObject *self, PyObject *key)
1552{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001553 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001554 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001555 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001556
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001557 value = PyMemoTable_Get(self->memo, key);
1558 if (value == NULL) {
1559 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001560 return -1;
1561 }
1562
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001563 if (!self->bin) {
1564 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001565 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1566 "%" PY_FORMAT_SIZE_T "d\n", *value);
1567 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001568 }
1569 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001570 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001571 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001572 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001573 len = 2;
1574 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001575 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001576 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001577 pdata[1] = (unsigned char)(*value & 0xff);
1578 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1579 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1580 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001581 len = 5;
1582 }
1583 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001584 PickleState *st = _Pickle_GetGlobalState();
1585 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001586 "memo id too large for LONG_BINGET");
1587 return -1;
1588 }
1589 }
1590
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001591 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001592 return -1;
1593
1594 return 0;
1595}
1596
1597/* Store an object in the memo, assign it a new unique ID based on the number
1598 of objects currently stored in the memo and generate a PUT opcode. */
1599static int
1600memo_put(PicklerObject *self, PyObject *obj)
1601{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001602 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001603 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001604 Py_ssize_t idx;
1605
1606 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001607
1608 if (self->fast)
1609 return 0;
1610
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 idx = PyMemoTable_Size(self->memo);
1612 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1613 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001614
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001615 if (self->proto >= 4) {
1616 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1617 return -1;
1618 return 0;
1619 }
1620 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001621 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001622 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001623 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001624 len = strlen(pdata);
1625 }
1626 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001627 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001628 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001629 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001630 len = 2;
1631 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001632 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001633 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001634 pdata[1] = (unsigned char)(idx & 0xff);
1635 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1636 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1637 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 len = 5;
1639 }
1640 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001641 PickleState *st = _Pickle_GetGlobalState();
1642 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001643 "memo id too large for LONG_BINPUT");
1644 return -1;
1645 }
1646 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001647 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001651}
1652
1653static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001654get_dotted_path(PyObject *obj, PyObject *name)
1655{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001656 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001657 PyObject *dotted_path;
1658 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001659
1660 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001661 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001662 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001663 n = PyList_GET_SIZE(dotted_path);
1664 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001665 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001666 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001667 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001668 if (obj == NULL)
1669 PyErr_Format(PyExc_AttributeError,
1670 "Can't pickle local object %R", name);
1671 else
1672 PyErr_Format(PyExc_AttributeError,
1673 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001674 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001675 return NULL;
1676 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001677 }
1678 return dotted_path;
1679}
1680
1681static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001682get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001683{
1684 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001685 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001686
1687 assert(PyList_CheckExact(names));
1688 Py_INCREF(obj);
1689 n = PyList_GET_SIZE(names);
1690 for (i = 0; i < n; i++) {
1691 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001692 Py_XDECREF(parent);
1693 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001694 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001695 if (obj == NULL) {
1696 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001697 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001698 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001699 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001700 if (pparent != NULL)
1701 *pparent = parent;
1702 else
1703 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001704 return obj;
1705}
1706
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001707
1708static PyObject *
1709getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1710{
1711 PyObject *dotted_path, *attr;
1712
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001713 if (allow_qualname) {
1714 dotted_path = get_dotted_path(obj, name);
1715 if (dotted_path == NULL)
1716 return NULL;
1717 attr = get_deep_attribute(obj, dotted_path, NULL);
1718 Py_DECREF(dotted_path);
1719 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001720 else {
1721 (void)_PyObject_LookupAttr(obj, name, &attr);
1722 }
1723 if (attr == NULL && !PyErr_Occurred()) {
1724 PyErr_Format(PyExc_AttributeError,
1725 "Can't get attribute %R on %R", name, obj);
1726 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001727 return attr;
1728}
1729
Eric Snow3f9eee62017-09-15 16:35:20 -06001730static int
1731_checkmodule(PyObject *module_name, PyObject *module,
1732 PyObject *global, PyObject *dotted_path)
1733{
1734 if (module == Py_None) {
1735 return -1;
1736 }
1737 if (PyUnicode_Check(module_name) &&
1738 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1739 return -1;
1740 }
1741
1742 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1743 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001744 return -1;
1745 }
1746 if (candidate != global) {
1747 Py_DECREF(candidate);
1748 return -1;
1749 }
1750 Py_DECREF(candidate);
1751 return 0;
1752}
1753
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001754static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001755whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001756{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001757 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001758 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001759 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001760 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001761 _Py_IDENTIFIER(__module__);
1762 _Py_IDENTIFIER(modules);
1763 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001764
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001765 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1766 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001767 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001768 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001769 /* In some rare cases (e.g., bound methods of extension types),
1770 __module__ can be None. If it is so, then search sys.modules for
1771 the module of global. */
1772 if (module_name != Py_None)
1773 return module_name;
1774 Py_CLEAR(module_name);
1775 }
1776 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001777
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001778 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001779 modules = _PySys_GetObjectId(&PyId_modules);
1780 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001781 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001782 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001783 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001784 if (PyDict_CheckExact(modules)) {
1785 i = 0;
1786 while (PyDict_Next(modules, &i, &module_name, &module)) {
1787 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1788 Py_INCREF(module_name);
1789 return module_name;
1790 }
1791 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001792 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001793 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001794 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001795 }
1796 else {
1797 PyObject *iterator = PyObject_GetIter(modules);
1798 if (iterator == NULL) {
1799 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001800 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001801 while ((module_name = PyIter_Next(iterator))) {
1802 module = PyObject_GetItem(modules, module_name);
1803 if (module == NULL) {
1804 Py_DECREF(module_name);
1805 Py_DECREF(iterator);
1806 return NULL;
1807 }
1808 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1809 Py_DECREF(module);
1810 Py_DECREF(iterator);
1811 return module_name;
1812 }
1813 Py_DECREF(module);
1814 Py_DECREF(module_name);
1815 if (PyErr_Occurred()) {
1816 Py_DECREF(iterator);
1817 return NULL;
1818 }
1819 }
1820 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001821 }
1822
1823 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001824 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001825 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001826 return module_name;
1827}
1828
1829/* fast_save_enter() and fast_save_leave() are guards against recursive
1830 objects when Pickler is used with the "fast mode" (i.e., with object
1831 memoization disabled). If the nesting of a list or dict object exceed
1832 FAST_NESTING_LIMIT, these guards will start keeping an internal
1833 reference to the seen list or dict objects and check whether these objects
1834 are recursive. These are not strictly necessary, since save() has a
1835 hard-coded recursion limit, but they give a nicer error message than the
1836 typical RuntimeError. */
1837static int
1838fast_save_enter(PicklerObject *self, PyObject *obj)
1839{
1840 /* if fast_nesting < 0, we're doing an error exit. */
1841 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1842 PyObject *key = NULL;
1843 if (self->fast_memo == NULL) {
1844 self->fast_memo = PyDict_New();
1845 if (self->fast_memo == NULL) {
1846 self->fast_nesting = -1;
1847 return 0;
1848 }
1849 }
1850 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001851 if (key == NULL) {
1852 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001853 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001854 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001855 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001856 Py_DECREF(key);
1857 PyErr_Format(PyExc_ValueError,
1858 "fast mode: can't pickle cyclic objects "
1859 "including object type %.200s at %p",
1860 obj->ob_type->tp_name, obj);
1861 self->fast_nesting = -1;
1862 return 0;
1863 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001864 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001865 Py_DECREF(key);
1866 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001867 return 0;
1868 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001869 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1870 Py_DECREF(key);
1871 self->fast_nesting = -1;
1872 return 0;
1873 }
1874 Py_DECREF(key);
1875 }
1876 return 1;
1877}
1878
1879static int
1880fast_save_leave(PicklerObject *self, PyObject *obj)
1881{
1882 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1883 PyObject *key = PyLong_FromVoidPtr(obj);
1884 if (key == NULL)
1885 return 0;
1886 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1887 Py_DECREF(key);
1888 return 0;
1889 }
1890 Py_DECREF(key);
1891 }
1892 return 1;
1893}
1894
1895static int
1896save_none(PicklerObject *self, PyObject *obj)
1897{
1898 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001899 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001900 return -1;
1901
1902 return 0;
1903}
1904
1905static int
1906save_bool(PicklerObject *self, PyObject *obj)
1907{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001908 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001909 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001910 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001911 return -1;
1912 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001913 else {
1914 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1915 * so that unpicklers written before bools were introduced unpickle them
1916 * as ints, but unpicklers after can recognize that bools were intended.
1917 * Note that protocol 2 added direct ways to pickle bools.
1918 */
1919 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1920 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1921 return -1;
1922 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001923 return 0;
1924}
1925
1926static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001927save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001928{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001929 PyObject *repr = NULL;
1930 Py_ssize_t size;
1931 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001932 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001933 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001934
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001935 val= PyLong_AsLongAndOverflow(obj, &overflow);
1936 if (!overflow && (sizeof(long) <= 4 ||
1937 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1938 {
Larry Hastings61272b72014-01-07 12:41:53 -08001939 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001940
1941 Note: we can't use -0x80000000L in the above condition because some
1942 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1943 before applying the unary minus when sizeof(long) <= 4. The
1944 resulting value stays unsigned which is commonly not what we want,
1945 so MSVC happily warns us about it. However, that result would have
1946 been fine because we guard for sizeof(long) <= 4 which turns the
1947 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001948 char pdata[32];
1949 Py_ssize_t len = 0;
1950
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001951 if (self->bin) {
1952 pdata[1] = (unsigned char)(val & 0xff);
1953 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1954 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1955 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001956
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001957 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1958 pdata[0] = BININT;
1959 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001960 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001961 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001962 pdata[0] = BININT2;
1963 len = 3;
1964 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001965 else {
1966 pdata[0] = BININT1;
1967 len = 2;
1968 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001969 }
1970 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001971 sprintf(pdata, "%c%ld\n", INT, val);
1972 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001973 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001974 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001975 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001976
1977 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001978 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001979 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001980
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001981 if (self->proto >= 2) {
1982 /* Linear-time pickling. */
1983 size_t nbits;
1984 size_t nbytes;
1985 unsigned char *pdata;
1986 char header[5];
1987 int i;
1988 int sign = _PyLong_Sign(obj);
1989
1990 if (sign == 0) {
1991 header[0] = LONG1;
1992 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001993 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001994 goto error;
1995 return 0;
1996 }
1997 nbits = _PyLong_NumBits(obj);
1998 if (nbits == (size_t)-1 && PyErr_Occurred())
1999 goto error;
2000 /* How many bytes do we need? There are nbits >> 3 full
2001 * bytes of data, and nbits & 7 leftover bits. If there
2002 * are any leftover bits, then we clearly need another
2003 * byte. Wnat's not so obvious is that we *probably*
2004 * need another byte even if there aren't any leftovers:
2005 * the most-significant bit of the most-significant byte
2006 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002007 * opposite of the one we need. The exception is ints
2008 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002009 * its own 256's-complement, so has the right sign bit
2010 * even without the extra byte. That's a pain to check
2011 * for in advance, though, so we always grab an extra
2012 * byte at the start, and cut it back later if possible.
2013 */
2014 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002015 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002016 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002017 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002018 goto error;
2019 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002020 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002021 if (repr == NULL)
2022 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002023 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002024 i = _PyLong_AsByteArray((PyLongObject *)obj,
2025 pdata, nbytes,
2026 1 /* little endian */ , 1 /* signed */ );
2027 if (i < 0)
2028 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002029 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002030 * needed. This is so iff the MSB is all redundant sign
2031 * bits.
2032 */
2033 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002034 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002035 pdata[nbytes - 1] == 0xff &&
2036 (pdata[nbytes - 2] & 0x80) != 0) {
2037 nbytes--;
2038 }
2039
2040 if (nbytes < 256) {
2041 header[0] = LONG1;
2042 header[1] = (unsigned char)nbytes;
2043 size = 2;
2044 }
2045 else {
2046 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002047 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002048 for (i = 1; i < 5; i++) {
2049 header[i] = (unsigned char)(size & 0xff);
2050 size >>= 8;
2051 }
2052 size = 5;
2053 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002054 if (_Pickler_Write(self, header, size) < 0 ||
2055 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002056 goto error;
2057 }
2058 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002059 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002060 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061
Mark Dickinson8dd05142009-01-20 20:43:58 +00002062 /* proto < 2: write the repr and newline. This is quadratic-time (in
2063 the number of digits), in both directions. We add a trailing 'L'
2064 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002065
2066 repr = PyObject_Repr(obj);
2067 if (repr == NULL)
2068 goto error;
2069
Serhiy Storchaka06515832016-11-20 09:13:07 +02002070 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002071 if (string == NULL)
2072 goto error;
2073
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002074 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2075 _Pickler_Write(self, string, size) < 0 ||
2076 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002077 goto error;
2078 }
2079
2080 if (0) {
2081 error:
2082 status = -1;
2083 }
2084 Py_XDECREF(repr);
2085
2086 return status;
2087}
2088
2089static int
2090save_float(PicklerObject *self, PyObject *obj)
2091{
2092 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2093
2094 if (self->bin) {
2095 char pdata[9];
2096 pdata[0] = BINFLOAT;
2097 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2098 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002099 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002100 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002101 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002102 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002103 int result = -1;
2104 char *buf = NULL;
2105 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002106
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002107 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002108 goto done;
2109
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002110 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002111 if (!buf) {
2112 PyErr_NoMemory();
2113 goto done;
2114 }
2115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002116 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002117 goto done;
2118
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002119 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002120 goto done;
2121
2122 result = 0;
2123done:
2124 PyMem_Free(buf);
2125 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002126 }
2127
2128 return 0;
2129}
2130
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002131/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002132
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002133 The large contiguous data is written directly into the underlying file
2134 object, bypassing the output_buffer of the Pickler. We intentionally
2135 do not insert a protocol 4 frame opcode to make it possible to optimize
2136 file.read calls in the loader.
2137 */
2138static int
2139_Pickler_write_bytes(PicklerObject *self,
2140 const char *header, Py_ssize_t header_size,
2141 const char *data, Py_ssize_t data_size,
2142 PyObject *payload)
2143{
2144 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2145 int framing = self->framing;
2146
2147 if (bypass_buffer) {
2148 assert(self->output_buffer != NULL);
2149 /* Commit the previous frame. */
2150 if (_Pickler_CommitFrame(self)) {
2151 return -1;
2152 }
2153 /* Disable framing temporarily */
2154 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002155 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002156
2157 if (_Pickler_Write(self, header, header_size) < 0) {
2158 return -1;
2159 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002160
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002161 if (bypass_buffer && self->write != NULL) {
2162 /* Bypass the in-memory buffer to directly stream large data
2163 into the underlying file object. */
2164 PyObject *result, *mem = NULL;
2165 /* Dump the output buffer to the file. */
2166 if (_Pickler_FlushToFile(self) < 0) {
2167 return -1;
2168 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002169
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002170 /* Stream write the payload into the file without going through the
2171 output buffer. */
2172 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002173 /* TODO: It would be better to use a memoryview with a linked
2174 original string if this is possible. */
2175 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002176 if (payload == NULL) {
2177 return -1;
2178 }
2179 }
2180 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2181 Py_XDECREF(mem);
2182 if (result == NULL) {
2183 return -1;
2184 }
2185 Py_DECREF(result);
2186
2187 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2188 if (_Pickler_ClearBuffer(self) < 0) {
2189 return -1;
2190 }
2191 }
2192 else {
2193 if (_Pickler_Write(self, data, data_size) < 0) {
2194 return -1;
2195 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002196 }
2197
2198 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002199 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002200
2201 return 0;
2202}
2203
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002204static int
2205save_bytes(PicklerObject *self, PyObject *obj)
2206{
2207 if (self->proto < 3) {
2208 /* Older pickle protocols do not have an opcode for pickling bytes
2209 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002210 the __reduce__ method) to permit bytes object unpickling.
2211
2212 Here we use a hack to be compatible with Python 2. Since in Python
2213 2 'bytes' is just an alias for 'str' (which has different
2214 parameters than the actual bytes object), we use codecs.encode
2215 to create the appropriate 'str' object when unpickled using
2216 Python 2 *and* the appropriate 'bytes' object when unpickled
2217 using Python 3. Again this is a hack and we don't need to do this
2218 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002219 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002220 int status;
2221
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002222 if (PyBytes_GET_SIZE(obj) == 0) {
2223 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2224 }
2225 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002226 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002227 PyObject *unicode_str =
2228 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2229 PyBytes_GET_SIZE(obj),
2230 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002231 _Py_IDENTIFIER(latin1);
2232
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002233 if (unicode_str == NULL)
2234 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002235 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002236 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002237 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002238 Py_DECREF(unicode_str);
2239 }
2240
2241 if (reduce_value == NULL)
2242 return -1;
2243
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002244 /* save_reduce() will memoize the object automatically. */
2245 status = save_reduce(self, reduce_value, obj);
2246 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002247 return status;
2248 }
2249 else {
2250 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002251 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002252 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002253
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002254 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255 if (size < 0)
2256 return -1;
2257
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002258 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259 header[0] = SHORT_BINBYTES;
2260 header[1] = (unsigned char)size;
2261 len = 2;
2262 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002263 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002264 header[0] = BINBYTES;
2265 header[1] = (unsigned char)(size & 0xff);
2266 header[2] = (unsigned char)((size >> 8) & 0xff);
2267 header[3] = (unsigned char)((size >> 16) & 0xff);
2268 header[4] = (unsigned char)((size >> 24) & 0xff);
2269 len = 5;
2270 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002271 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002272 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002273 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002274 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002275 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002277 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002278 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279 return -1; /* string too large */
2280 }
2281
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002282 if (_Pickler_write_bytes(self, header, len,
2283 PyBytes_AS_STRING(obj), size, obj) < 0)
2284 {
2285 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002286 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287
2288 if (memo_put(self, obj) < 0)
2289 return -1;
2290
2291 return 0;
2292 }
2293}
2294
2295/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2296 backslash and newline characters to \uXXXX escapes. */
2297static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002298raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002299{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002300 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002301 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002302 void *data;
2303 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002304 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002305
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002306 if (PyUnicode_READY(obj))
2307 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002308
Victor Stinner358af132015-10-12 22:36:57 +02002309 _PyBytesWriter_Init(&writer);
2310
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002311 size = PyUnicode_GET_LENGTH(obj);
2312 data = PyUnicode_DATA(obj);
2313 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002314
Victor Stinner358af132015-10-12 22:36:57 +02002315 p = _PyBytesWriter_Alloc(&writer, size);
2316 if (p == NULL)
2317 goto error;
2318 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002319
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002320 for (i=0; i < size; i++) {
2321 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002322 /* Map 32-bit characters to '\Uxxxxxxxx' */
2323 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002324 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002325 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2326 if (p == NULL)
2327 goto error;
2328
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002329 *p++ = '\\';
2330 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002331 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2332 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2333 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2334 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2335 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2336 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2337 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2338 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002339 }
Victor Stinner358af132015-10-12 22:36:57 +02002340 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002341 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002342 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002343 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2344 if (p == NULL)
2345 goto error;
2346
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002347 *p++ = '\\';
2348 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002349 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2350 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2351 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2352 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002353 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002354 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002355 else
2356 *p++ = (char) ch;
2357 }
Victor Stinner358af132015-10-12 22:36:57 +02002358
2359 return _PyBytesWriter_Finish(&writer, p);
2360
2361error:
2362 _PyBytesWriter_Dealloc(&writer);
2363 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002364}
2365
2366static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002367write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002368{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002369 char header[9];
2370 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002371 PyObject *encoded = NULL;
2372 Py_ssize_t size;
2373 const char *data;
2374
2375 if (PyUnicode_READY(obj))
2376 return -1;
2377
2378 data = PyUnicode_AsUTF8AndSize(obj, &size);
2379 if (data == NULL) {
2380 /* Issue #8383: for strings with lone surrogates, fallback on the
2381 "surrogatepass" error handler. */
2382 PyErr_Clear();
2383 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2384 if (encoded == NULL)
2385 return -1;
2386
2387 data = PyBytes_AS_STRING(encoded);
2388 size = PyBytes_GET_SIZE(encoded);
2389 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002390
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002391 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002392 if (size <= 0xff && self->proto >= 4) {
2393 header[0] = SHORT_BINUNICODE;
2394 header[1] = (unsigned char)(size & 0xff);
2395 len = 2;
2396 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002397 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002398 header[0] = BINUNICODE;
2399 header[1] = (unsigned char)(size & 0xff);
2400 header[2] = (unsigned char)((size >> 8) & 0xff);
2401 header[3] = (unsigned char)((size >> 16) & 0xff);
2402 header[4] = (unsigned char)((size >> 24) & 0xff);
2403 len = 5;
2404 }
2405 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002406 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002407 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002408 len = 9;
2409 }
2410 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002411 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002412 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002413 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002414 return -1;
2415 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002416
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002417 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2418 Py_XDECREF(encoded);
2419 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002420 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002421 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002422 return 0;
2423}
2424
2425static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002426save_unicode(PicklerObject *self, PyObject *obj)
2427{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002428 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002429 if (write_unicode_binary(self, obj) < 0)
2430 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002431 }
2432 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002433 PyObject *encoded;
2434 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002435 const char unicode_op = UNICODE;
2436
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002437 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002438 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002439 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002440
Antoine Pitrou299978d2013-04-07 17:38:11 +02002441 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2442 Py_DECREF(encoded);
2443 return -1;
2444 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002445
2446 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002447 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2448 Py_DECREF(encoded);
2449 return -1;
2450 }
2451 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002452
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002453 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002454 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455 }
2456 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002457 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002458
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002459 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002460}
2461
2462/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2463static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002464store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002465{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002466 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002467
2468 assert(PyTuple_Size(t) == len);
2469
2470 for (i = 0; i < len; i++) {
2471 PyObject *element = PyTuple_GET_ITEM(t, i);
2472
2473 if (element == NULL)
2474 return -1;
2475 if (save(self, element, 0) < 0)
2476 return -1;
2477 }
2478
2479 return 0;
2480}
2481
2482/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2483 * used across protocols to minimize the space needed to pickle them.
2484 * Tuples are also the only builtin immutable type that can be recursive
2485 * (a tuple can be reached from itself), and that requires some subtle
2486 * magic so that it works in all cases. IOW, this is a long routine.
2487 */
2488static int
2489save_tuple(PicklerObject *self, PyObject *obj)
2490{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002491 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002492
2493 const char mark_op = MARK;
2494 const char tuple_op = TUPLE;
2495 const char pop_op = POP;
2496 const char pop_mark_op = POP_MARK;
2497 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2498
2499 if ((len = PyTuple_Size(obj)) < 0)
2500 return -1;
2501
2502 if (len == 0) {
2503 char pdata[2];
2504
2505 if (self->proto) {
2506 pdata[0] = EMPTY_TUPLE;
2507 len = 1;
2508 }
2509 else {
2510 pdata[0] = MARK;
2511 pdata[1] = TUPLE;
2512 len = 2;
2513 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002514 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002515 return -1;
2516 return 0;
2517 }
2518
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002519 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002520 * saving the tuple elements, the tuple must be recursive, in
2521 * which case we'll pop everything we put on the stack, and fetch
2522 * its value from the memo.
2523 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002524 if (len <= 3 && self->proto >= 2) {
2525 /* Use TUPLE{1,2,3} opcodes. */
2526 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002527 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002528
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002529 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002530 /* pop the len elements */
2531 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002532 if (_Pickler_Write(self, &pop_op, 1) < 0)
2533 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002534 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002535 if (memo_get(self, obj) < 0)
2536 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002537
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002538 return 0;
2539 }
2540 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002541 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2542 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002543 }
2544 goto memoize;
2545 }
2546
2547 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2548 * Generate MARK e1 e2 ... TUPLE
2549 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002550 if (_Pickler_Write(self, &mark_op, 1) < 0)
2551 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002552
2553 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002554 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002555
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002556 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002557 /* pop the stack stuff we pushed */
2558 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002559 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2560 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002561 }
2562 else {
2563 /* Note that we pop one more than len, to remove
2564 * the MARK too.
2565 */
2566 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002567 if (_Pickler_Write(self, &pop_op, 1) < 0)
2568 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002569 }
2570 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002571 if (memo_get(self, obj) < 0)
2572 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002573
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002574 return 0;
2575 }
2576 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002577 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2578 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002579 }
2580
2581 memoize:
2582 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002583 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002584
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002585 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002586}
2587
2588/* iter is an iterator giving items, and we batch up chunks of
2589 * MARK item item ... item APPENDS
2590 * opcode sequences. Calling code should have arranged to first create an
2591 * empty list, or list-like object, for the APPENDS to operate on.
2592 * Returns 0 on success, <0 on error.
2593 */
2594static int
2595batch_list(PicklerObject *self, PyObject *iter)
2596{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002597 PyObject *obj = NULL;
2598 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002599 int i, n;
2600
2601 const char mark_op = MARK;
2602 const char append_op = APPEND;
2603 const char appends_op = APPENDS;
2604
2605 assert(iter != NULL);
2606
2607 /* XXX: I think this function could be made faster by avoiding the
2608 iterator interface and fetching objects directly from list using
2609 PyList_GET_ITEM.
2610 */
2611
2612 if (self->proto == 0) {
2613 /* APPENDS isn't available; do one at a time. */
2614 for (;;) {
2615 obj = PyIter_Next(iter);
2616 if (obj == NULL) {
2617 if (PyErr_Occurred())
2618 return -1;
2619 break;
2620 }
2621 i = save(self, obj, 0);
2622 Py_DECREF(obj);
2623 if (i < 0)
2624 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002625 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002626 return -1;
2627 }
2628 return 0;
2629 }
2630
2631 /* proto > 0: write in batches of BATCHSIZE. */
2632 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002633 /* Get first item */
2634 firstitem = PyIter_Next(iter);
2635 if (firstitem == NULL) {
2636 if (PyErr_Occurred())
2637 goto error;
2638
2639 /* nothing more to add */
2640 break;
2641 }
2642
2643 /* Try to get a second item */
2644 obj = PyIter_Next(iter);
2645 if (obj == NULL) {
2646 if (PyErr_Occurred())
2647 goto error;
2648
2649 /* Only one item to write */
2650 if (save(self, firstitem, 0) < 0)
2651 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002652 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002653 goto error;
2654 Py_CLEAR(firstitem);
2655 break;
2656 }
2657
2658 /* More than one item to write */
2659
2660 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002661 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002662 goto error;
2663
2664 if (save(self, firstitem, 0) < 0)
2665 goto error;
2666 Py_CLEAR(firstitem);
2667 n = 1;
2668
2669 /* Fetch and save up to BATCHSIZE items */
2670 while (obj) {
2671 if (save(self, obj, 0) < 0)
2672 goto error;
2673 Py_CLEAR(obj);
2674 n += 1;
2675
2676 if (n == BATCHSIZE)
2677 break;
2678
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002679 obj = PyIter_Next(iter);
2680 if (obj == NULL) {
2681 if (PyErr_Occurred())
2682 goto error;
2683 break;
2684 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002685 }
2686
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002687 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002688 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002689
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002690 } while (n == BATCHSIZE);
2691 return 0;
2692
2693 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002694 Py_XDECREF(firstitem);
2695 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002696 return -1;
2697}
2698
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002699/* This is a variant of batch_list() above, specialized for lists (with no
2700 * support for list subclasses). Like batch_list(), we batch up chunks of
2701 * MARK item item ... item APPENDS
2702 * opcode sequences. Calling code should have arranged to first create an
2703 * empty list, or list-like object, for the APPENDS to operate on.
2704 * Returns 0 on success, -1 on error.
2705 *
2706 * This version is considerably faster than batch_list(), if less general.
2707 *
2708 * Note that this only works for protocols > 0.
2709 */
2710static int
2711batch_list_exact(PicklerObject *self, PyObject *obj)
2712{
2713 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002714 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002715
2716 const char append_op = APPEND;
2717 const char appends_op = APPENDS;
2718 const char mark_op = MARK;
2719
2720 assert(obj != NULL);
2721 assert(self->proto > 0);
2722 assert(PyList_CheckExact(obj));
2723
2724 if (PyList_GET_SIZE(obj) == 1) {
2725 item = PyList_GET_ITEM(obj, 0);
2726 if (save(self, item, 0) < 0)
2727 return -1;
2728 if (_Pickler_Write(self, &append_op, 1) < 0)
2729 return -1;
2730 return 0;
2731 }
2732
2733 /* Write in batches of BATCHSIZE. */
2734 total = 0;
2735 do {
2736 this_batch = 0;
2737 if (_Pickler_Write(self, &mark_op, 1) < 0)
2738 return -1;
2739 while (total < PyList_GET_SIZE(obj)) {
2740 item = PyList_GET_ITEM(obj, total);
2741 if (save(self, item, 0) < 0)
2742 return -1;
2743 total++;
2744 if (++this_batch == BATCHSIZE)
2745 break;
2746 }
2747 if (_Pickler_Write(self, &appends_op, 1) < 0)
2748 return -1;
2749
2750 } while (total < PyList_GET_SIZE(obj));
2751
2752 return 0;
2753}
2754
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002755static int
2756save_list(PicklerObject *self, PyObject *obj)
2757{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002758 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002759 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002760 int status = 0;
2761
2762 if (self->fast && !fast_save_enter(self, obj))
2763 goto error;
2764
2765 /* Create an empty list. */
2766 if (self->bin) {
2767 header[0] = EMPTY_LIST;
2768 len = 1;
2769 }
2770 else {
2771 header[0] = MARK;
2772 header[1] = LIST;
2773 len = 2;
2774 }
2775
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002776 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002777 goto error;
2778
2779 /* Get list length, and bow out early if empty. */
2780 if ((len = PyList_Size(obj)) < 0)
2781 goto error;
2782
2783 if (memo_put(self, obj) < 0)
2784 goto error;
2785
2786 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002787 /* Materialize the list elements. */
2788 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002789 if (Py_EnterRecursiveCall(" while pickling an object"))
2790 goto error;
2791 status = batch_list_exact(self, obj);
2792 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002793 } else {
2794 PyObject *iter = PyObject_GetIter(obj);
2795 if (iter == NULL)
2796 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002797
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002798 if (Py_EnterRecursiveCall(" while pickling an object")) {
2799 Py_DECREF(iter);
2800 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002801 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002802 status = batch_list(self, iter);
2803 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002804 Py_DECREF(iter);
2805 }
2806 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002807 if (0) {
2808 error:
2809 status = -1;
2810 }
2811
2812 if (self->fast && !fast_save_leave(self, obj))
2813 status = -1;
2814
2815 return status;
2816}
2817
2818/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2819 * MARK key value ... key value SETITEMS
2820 * opcode sequences. Calling code should have arranged to first create an
2821 * empty dict, or dict-like object, for the SETITEMS to operate on.
2822 * Returns 0 on success, <0 on error.
2823 *
2824 * This is very much like batch_list(). The difference between saving
2825 * elements directly, and picking apart two-tuples, is so long-winded at
2826 * the C level, though, that attempts to combine these routines were too
2827 * ugly to bear.
2828 */
2829static int
2830batch_dict(PicklerObject *self, PyObject *iter)
2831{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002832 PyObject *obj = NULL;
2833 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002834 int i, n;
2835
2836 const char mark_op = MARK;
2837 const char setitem_op = SETITEM;
2838 const char setitems_op = SETITEMS;
2839
2840 assert(iter != NULL);
2841
2842 if (self->proto == 0) {
2843 /* SETITEMS isn't available; do one at a time. */
2844 for (;;) {
2845 obj = PyIter_Next(iter);
2846 if (obj == NULL) {
2847 if (PyErr_Occurred())
2848 return -1;
2849 break;
2850 }
2851 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2852 PyErr_SetString(PyExc_TypeError, "dict items "
2853 "iterator must return 2-tuples");
2854 return -1;
2855 }
2856 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2857 if (i >= 0)
2858 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2859 Py_DECREF(obj);
2860 if (i < 0)
2861 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002862 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002863 return -1;
2864 }
2865 return 0;
2866 }
2867
2868 /* proto > 0: write in batches of BATCHSIZE. */
2869 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002870 /* Get first item */
2871 firstitem = PyIter_Next(iter);
2872 if (firstitem == NULL) {
2873 if (PyErr_Occurred())
2874 goto error;
2875
2876 /* nothing more to add */
2877 break;
2878 }
2879 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2880 PyErr_SetString(PyExc_TypeError, "dict items "
2881 "iterator must return 2-tuples");
2882 goto error;
2883 }
2884
2885 /* Try to get a second item */
2886 obj = PyIter_Next(iter);
2887 if (obj == NULL) {
2888 if (PyErr_Occurred())
2889 goto error;
2890
2891 /* Only one item to write */
2892 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2893 goto error;
2894 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2895 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002896 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002897 goto error;
2898 Py_CLEAR(firstitem);
2899 break;
2900 }
2901
2902 /* More than one item to write */
2903
2904 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002905 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002906 goto error;
2907
2908 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2909 goto error;
2910 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2911 goto error;
2912 Py_CLEAR(firstitem);
2913 n = 1;
2914
2915 /* Fetch and save up to BATCHSIZE items */
2916 while (obj) {
2917 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2918 PyErr_SetString(PyExc_TypeError, "dict items "
2919 "iterator must return 2-tuples");
2920 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002921 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002922 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2923 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2924 goto error;
2925 Py_CLEAR(obj);
2926 n += 1;
2927
2928 if (n == BATCHSIZE)
2929 break;
2930
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002931 obj = PyIter_Next(iter);
2932 if (obj == NULL) {
2933 if (PyErr_Occurred())
2934 goto error;
2935 break;
2936 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002937 }
2938
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002939 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002940 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002941
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002942 } while (n == BATCHSIZE);
2943 return 0;
2944
2945 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002946 Py_XDECREF(firstitem);
2947 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002948 return -1;
2949}
2950
Collin Winter5c9b02d2009-05-25 05:43:30 +00002951/* This is a variant of batch_dict() above that specializes for dicts, with no
2952 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2953 * MARK key value ... key value SETITEMS
2954 * opcode sequences. Calling code should have arranged to first create an
2955 * empty dict, or dict-like object, for the SETITEMS to operate on.
2956 * Returns 0 on success, -1 on error.
2957 *
2958 * Note that this currently doesn't work for protocol 0.
2959 */
2960static int
2961batch_dict_exact(PicklerObject *self, PyObject *obj)
2962{
2963 PyObject *key = NULL, *value = NULL;
2964 int i;
2965 Py_ssize_t dict_size, ppos = 0;
2966
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002967 const char mark_op = MARK;
2968 const char setitem_op = SETITEM;
2969 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002970
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002971 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002972 assert(self->proto > 0);
2973
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002974 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002975
2976 /* Special-case len(d) == 1 to save space. */
2977 if (dict_size == 1) {
2978 PyDict_Next(obj, &ppos, &key, &value);
2979 if (save(self, key, 0) < 0)
2980 return -1;
2981 if (save(self, value, 0) < 0)
2982 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002983 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002984 return -1;
2985 return 0;
2986 }
2987
2988 /* Write in batches of BATCHSIZE. */
2989 do {
2990 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002991 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002992 return -1;
2993 while (PyDict_Next(obj, &ppos, &key, &value)) {
2994 if (save(self, key, 0) < 0)
2995 return -1;
2996 if (save(self, value, 0) < 0)
2997 return -1;
2998 if (++i == BATCHSIZE)
2999 break;
3000 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003001 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003002 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003003 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003004 PyErr_Format(
3005 PyExc_RuntimeError,
3006 "dictionary changed size during iteration");
3007 return -1;
3008 }
3009
3010 } while (i == BATCHSIZE);
3011 return 0;
3012}
3013
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003014static int
3015save_dict(PicklerObject *self, PyObject *obj)
3016{
3017 PyObject *items, *iter;
3018 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003019 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003020 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003021 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003022
3023 if (self->fast && !fast_save_enter(self, obj))
3024 goto error;
3025
3026 /* Create an empty dict. */
3027 if (self->bin) {
3028 header[0] = EMPTY_DICT;
3029 len = 1;
3030 }
3031 else {
3032 header[0] = MARK;
3033 header[1] = DICT;
3034 len = 2;
3035 }
3036
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003037 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003038 goto error;
3039
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003040 if (memo_put(self, obj) < 0)
3041 goto error;
3042
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003043 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003044 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003045 if (PyDict_CheckExact(obj) && self->proto > 0) {
3046 /* We can take certain shortcuts if we know this is a dict and
3047 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003048 if (Py_EnterRecursiveCall(" while pickling an object"))
3049 goto error;
3050 status = batch_dict_exact(self, obj);
3051 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003052 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003053 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003054
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003055 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003056 if (items == NULL)
3057 goto error;
3058 iter = PyObject_GetIter(items);
3059 Py_DECREF(items);
3060 if (iter == NULL)
3061 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003062 if (Py_EnterRecursiveCall(" while pickling an object")) {
3063 Py_DECREF(iter);
3064 goto error;
3065 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003066 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003067 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003068 Py_DECREF(iter);
3069 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003070 }
3071
3072 if (0) {
3073 error:
3074 status = -1;
3075 }
3076
3077 if (self->fast && !fast_save_leave(self, obj))
3078 status = -1;
3079
3080 return status;
3081}
3082
3083static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003084save_set(PicklerObject *self, PyObject *obj)
3085{
3086 PyObject *item;
3087 int i;
3088 Py_ssize_t set_size, ppos = 0;
3089 Py_hash_t hash;
3090
3091 const char empty_set_op = EMPTY_SET;
3092 const char mark_op = MARK;
3093 const char additems_op = ADDITEMS;
3094
3095 if (self->proto < 4) {
3096 PyObject *items;
3097 PyObject *reduce_value;
3098 int status;
3099
3100 items = PySequence_List(obj);
3101 if (items == NULL) {
3102 return -1;
3103 }
3104 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3105 Py_DECREF(items);
3106 if (reduce_value == NULL) {
3107 return -1;
3108 }
3109 /* save_reduce() will memoize the object automatically. */
3110 status = save_reduce(self, reduce_value, obj);
3111 Py_DECREF(reduce_value);
3112 return status;
3113 }
3114
3115 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3116 return -1;
3117
3118 if (memo_put(self, obj) < 0)
3119 return -1;
3120
3121 set_size = PySet_GET_SIZE(obj);
3122 if (set_size == 0)
3123 return 0; /* nothing to do */
3124
3125 /* Write in batches of BATCHSIZE. */
3126 do {
3127 i = 0;
3128 if (_Pickler_Write(self, &mark_op, 1) < 0)
3129 return -1;
3130 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3131 if (save(self, item, 0) < 0)
3132 return -1;
3133 if (++i == BATCHSIZE)
3134 break;
3135 }
3136 if (_Pickler_Write(self, &additems_op, 1) < 0)
3137 return -1;
3138 if (PySet_GET_SIZE(obj) != set_size) {
3139 PyErr_Format(
3140 PyExc_RuntimeError,
3141 "set changed size during iteration");
3142 return -1;
3143 }
3144 } while (i == BATCHSIZE);
3145
3146 return 0;
3147}
3148
3149static int
3150save_frozenset(PicklerObject *self, PyObject *obj)
3151{
3152 PyObject *iter;
3153
3154 const char mark_op = MARK;
3155 const char frozenset_op = FROZENSET;
3156
3157 if (self->fast && !fast_save_enter(self, obj))
3158 return -1;
3159
3160 if (self->proto < 4) {
3161 PyObject *items;
3162 PyObject *reduce_value;
3163 int status;
3164
3165 items = PySequence_List(obj);
3166 if (items == NULL) {
3167 return -1;
3168 }
3169 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3170 items);
3171 Py_DECREF(items);
3172 if (reduce_value == NULL) {
3173 return -1;
3174 }
3175 /* save_reduce() will memoize the object automatically. */
3176 status = save_reduce(self, reduce_value, obj);
3177 Py_DECREF(reduce_value);
3178 return status;
3179 }
3180
3181 if (_Pickler_Write(self, &mark_op, 1) < 0)
3182 return -1;
3183
3184 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003185 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003186 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003187 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003188 for (;;) {
3189 PyObject *item;
3190
3191 item = PyIter_Next(iter);
3192 if (item == NULL) {
3193 if (PyErr_Occurred()) {
3194 Py_DECREF(iter);
3195 return -1;
3196 }
3197 break;
3198 }
3199 if (save(self, item, 0) < 0) {
3200 Py_DECREF(item);
3201 Py_DECREF(iter);
3202 return -1;
3203 }
3204 Py_DECREF(item);
3205 }
3206 Py_DECREF(iter);
3207
3208 /* If the object is already in the memo, this means it is
3209 recursive. In this case, throw away everything we put on the
3210 stack, and fetch the object back from the memo. */
3211 if (PyMemoTable_Get(self->memo, obj)) {
3212 const char pop_mark_op = POP_MARK;
3213
3214 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3215 return -1;
3216 if (memo_get(self, obj) < 0)
3217 return -1;
3218 return 0;
3219 }
3220
3221 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3222 return -1;
3223 if (memo_put(self, obj) < 0)
3224 return -1;
3225
3226 return 0;
3227}
3228
3229static int
3230fix_imports(PyObject **module_name, PyObject **global_name)
3231{
3232 PyObject *key;
3233 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003234 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003235
3236 key = PyTuple_Pack(2, *module_name, *global_name);
3237 if (key == NULL)
3238 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003239 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003240 Py_DECREF(key);
3241 if (item) {
3242 PyObject *fixed_module_name;
3243 PyObject *fixed_global_name;
3244
3245 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3246 PyErr_Format(PyExc_RuntimeError,
3247 "_compat_pickle.REVERSE_NAME_MAPPING values "
3248 "should be 2-tuples, not %.200s",
3249 Py_TYPE(item)->tp_name);
3250 return -1;
3251 }
3252 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3253 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3254 if (!PyUnicode_Check(fixed_module_name) ||
3255 !PyUnicode_Check(fixed_global_name)) {
3256 PyErr_Format(PyExc_RuntimeError,
3257 "_compat_pickle.REVERSE_NAME_MAPPING values "
3258 "should be pairs of str, not (%.200s, %.200s)",
3259 Py_TYPE(fixed_module_name)->tp_name,
3260 Py_TYPE(fixed_global_name)->tp_name);
3261 return -1;
3262 }
3263
3264 Py_CLEAR(*module_name);
3265 Py_CLEAR(*global_name);
3266 Py_INCREF(fixed_module_name);
3267 Py_INCREF(fixed_global_name);
3268 *module_name = fixed_module_name;
3269 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003270 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003271 }
3272 else if (PyErr_Occurred()) {
3273 return -1;
3274 }
3275
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003276 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003277 if (item) {
3278 if (!PyUnicode_Check(item)) {
3279 PyErr_Format(PyExc_RuntimeError,
3280 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3281 "should be strings, not %.200s",
3282 Py_TYPE(item)->tp_name);
3283 return -1;
3284 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003285 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003286 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003287 }
3288 else if (PyErr_Occurred()) {
3289 return -1;
3290 }
3291
3292 return 0;
3293}
3294
3295static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003296save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3297{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003298 PyObject *global_name = NULL;
3299 PyObject *module_name = NULL;
3300 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003301 PyObject *parent = NULL;
3302 PyObject *dotted_path = NULL;
3303 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003304 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003305 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003306 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003307 _Py_IDENTIFIER(__name__);
3308 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003309
3310 const char global_op = GLOBAL;
3311
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003312 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003313 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003314 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003315 }
3316 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003317 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3318 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003319 if (global_name == NULL) {
3320 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3321 if (global_name == NULL)
3322 goto error;
3323 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003324 }
3325
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003326 dotted_path = get_dotted_path(module, global_name);
3327 if (dotted_path == NULL)
3328 goto error;
3329 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003330 if (module_name == NULL)
3331 goto error;
3332
3333 /* XXX: Change to use the import C API directly with level=0 to disallow
3334 relative imports.
3335
3336 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3337 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3338 custom import functions (IMHO, this would be a nice security
3339 feature). The import C API would need to be extended to support the
3340 extra parameters of __import__ to fix that. */
3341 module = PyImport_Import(module_name);
3342 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003343 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003344 "Can't pickle %R: import of module %R failed",
3345 obj, module_name);
3346 goto error;
3347 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003348 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3349 Py_INCREF(lastname);
3350 cls = get_deep_attribute(module, dotted_path, &parent);
3351 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003352 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003353 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003354 "Can't pickle %R: attribute lookup %S on %S failed",
3355 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003356 goto error;
3357 }
3358 if (cls != obj) {
3359 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003360 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003361 "Can't pickle %R: it's not the same object as %S.%S",
3362 obj, module_name, global_name);
3363 goto error;
3364 }
3365 Py_DECREF(cls);
3366
3367 if (self->proto >= 2) {
3368 /* See whether this is in the extension registry, and if
3369 * so generate an EXT opcode.
3370 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003371 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003372 PyObject *code_obj; /* extension code as Python object */
3373 long code; /* extension code as C value */
3374 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003375 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003376
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003377 extension_key = PyTuple_Pack(2, module_name, global_name);
3378 if (extension_key == NULL) {
3379 goto error;
3380 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003381 code_obj = PyDict_GetItemWithError(st->extension_registry,
3382 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003383 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003384 /* The object is not registered in the extension registry.
3385 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003386 if (code_obj == NULL) {
3387 if (PyErr_Occurred()) {
3388 goto error;
3389 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003390 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003391 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003392
3393 /* XXX: pickle.py doesn't check neither the type, nor the range
3394 of the value returned by the extension_registry. It should for
3395 consistency. */
3396
3397 /* Verify code_obj has the right type and value. */
3398 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003399 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003400 "Can't pickle %R: extension code %R isn't an integer",
3401 obj, code_obj);
3402 goto error;
3403 }
3404 code = PyLong_AS_LONG(code_obj);
3405 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003406 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003407 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3408 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003409 goto error;
3410 }
3411
3412 /* Generate an EXT opcode. */
3413 if (code <= 0xff) {
3414 pdata[0] = EXT1;
3415 pdata[1] = (unsigned char)code;
3416 n = 2;
3417 }
3418 else if (code <= 0xffff) {
3419 pdata[0] = EXT2;
3420 pdata[1] = (unsigned char)(code & 0xff);
3421 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3422 n = 3;
3423 }
3424 else {
3425 pdata[0] = EXT4;
3426 pdata[1] = (unsigned char)(code & 0xff);
3427 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3428 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3429 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3430 n = 5;
3431 }
3432
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003433 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003434 goto error;
3435 }
3436 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003437 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003438 if (parent == module) {
3439 Py_INCREF(lastname);
3440 Py_DECREF(global_name);
3441 global_name = lastname;
3442 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003443 if (self->proto >= 4) {
3444 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003445
Christian Heimese8b1ba12013-11-23 21:13:39 +01003446 if (save(self, module_name, 0) < 0)
3447 goto error;
3448 if (save(self, global_name, 0) < 0)
3449 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003450
3451 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3452 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003453 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003454 else if (parent != module) {
3455 PickleState *st = _Pickle_GetGlobalState();
3456 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3457 st->getattr, parent, lastname);
Miss Islington (bot)3152bc32018-08-22 01:54:33 -04003458 if (reduce_value == NULL)
3459 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003460 status = save_reduce(self, reduce_value, NULL);
3461 Py_DECREF(reduce_value);
3462 if (status < 0)
3463 goto error;
3464 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003465 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003466 /* Generate a normal global opcode if we are using a pickle
3467 protocol < 4, or if the object is not registered in the
3468 extension registry. */
3469 PyObject *encoded;
3470 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003471
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003472 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003473 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003474
3475 /* For protocol < 3 and if the user didn't request against doing
3476 so, we convert module names to the old 2.x module names. */
3477 if (self->proto < 3 && self->fix_imports) {
3478 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003479 goto error;
3480 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003481 }
3482
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003483 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3484 both the module name and the global name using UTF-8. We do so
3485 only when we are using the pickle protocol newer than version
3486 3. This is to ensure compatibility with older Unpickler running
3487 on Python 2.x. */
3488 if (self->proto == 3) {
3489 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003490 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003491 else {
3492 unicode_encoder = PyUnicode_AsASCIIString;
3493 }
3494 encoded = unicode_encoder(module_name);
3495 if (encoded == NULL) {
3496 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003497 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003498 "can't pickle module identifier '%S' using "
3499 "pickle protocol %i",
3500 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003501 goto error;
3502 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003503 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3504 PyBytes_GET_SIZE(encoded)) < 0) {
3505 Py_DECREF(encoded);
3506 goto error;
3507 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003508 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003509 if(_Pickler_Write(self, "\n", 1) < 0)
3510 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003511
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003512 /* Save the name of the module. */
3513 encoded = unicode_encoder(global_name);
3514 if (encoded == NULL) {
3515 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003516 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003517 "can't pickle global identifier '%S' using "
3518 "pickle protocol %i",
3519 global_name, self->proto);
3520 goto error;
3521 }
3522 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3523 PyBytes_GET_SIZE(encoded)) < 0) {
3524 Py_DECREF(encoded);
3525 goto error;
3526 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003527 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003528 if (_Pickler_Write(self, "\n", 1) < 0)
3529 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003530 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003531 /* Memoize the object. */
3532 if (memo_put(self, obj) < 0)
3533 goto error;
3534 }
3535
3536 if (0) {
3537 error:
3538 status = -1;
3539 }
3540 Py_XDECREF(module_name);
3541 Py_XDECREF(global_name);
3542 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003543 Py_XDECREF(parent);
3544 Py_XDECREF(dotted_path);
3545 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003546
3547 return status;
3548}
3549
3550static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003551save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3552{
3553 PyObject *reduce_value;
3554 int status;
3555
3556 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3557 if (reduce_value == NULL) {
3558 return -1;
3559 }
3560 status = save_reduce(self, reduce_value, obj);
3561 Py_DECREF(reduce_value);
3562 return status;
3563}
3564
3565static int
3566save_type(PicklerObject *self, PyObject *obj)
3567{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003568 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003569 return save_singleton_type(self, obj, Py_None);
3570 }
3571 else if (obj == (PyObject *)&PyEllipsis_Type) {
3572 return save_singleton_type(self, obj, Py_Ellipsis);
3573 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003574 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003575 return save_singleton_type(self, obj, Py_NotImplemented);
3576 }
3577 return save_global(self, obj, NULL);
3578}
3579
3580static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003581save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003582{
3583 PyObject *pid = NULL;
3584 int status = 0;
3585
3586 const char persid_op = PERSID;
3587 const char binpersid_op = BINPERSID;
3588
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003589 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003590 if (pid == NULL)
3591 return -1;
3592
3593 if (pid != Py_None) {
3594 if (self->bin) {
3595 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003596 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003597 goto error;
3598 }
3599 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003600 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003601
3602 pid_str = PyObject_Str(pid);
3603 if (pid_str == NULL)
3604 goto error;
3605
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003606 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003607 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003608 if (!PyUnicode_IS_ASCII(pid_str)) {
3609 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3610 "persistent IDs in protocol 0 must be "
3611 "ASCII strings");
3612 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003613 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003614 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003615
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003616 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003617 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3618 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3619 _Pickler_Write(self, "\n", 1) < 0) {
3620 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003621 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003622 }
3623 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003624 }
3625 status = 1;
3626 }
3627
3628 if (0) {
3629 error:
3630 status = -1;
3631 }
3632 Py_XDECREF(pid);
3633
3634 return status;
3635}
3636
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003637static PyObject *
3638get_class(PyObject *obj)
3639{
3640 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003641 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003642
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003643 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3644 cls = (PyObject *) Py_TYPE(obj);
3645 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003646 }
3647 return cls;
3648}
3649
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003650/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3651 * appropriate __reduce__ method for obj.
3652 */
3653static int
3654save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3655{
3656 PyObject *callable;
3657 PyObject *argtup;
3658 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003659 PyObject *listitems = Py_None;
3660 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003661 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003662 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003663 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003664
3665 const char reduce_op = REDUCE;
3666 const char build_op = BUILD;
3667 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003668 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003669
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003670 size = PyTuple_Size(args);
3671 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003672 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003673 "__reduce__ must contain 2 through 5 elements");
3674 return -1;
3675 }
3676
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003677 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3678 &callable, &argtup, &state, &listitems, &dictitems))
3679 return -1;
3680
3681 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003682 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003683 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003684 return -1;
3685 }
3686 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003687 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003688 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003689 return -1;
3690 }
3691
3692 if (state == Py_None)
3693 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003694
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003695 if (listitems == Py_None)
3696 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003697 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003698 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003699 "returned by __reduce__ must be an iterator, not %s",
3700 Py_TYPE(listitems)->tp_name);
3701 return -1;
3702 }
3703
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003704 if (dictitems == Py_None)
3705 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003706 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003707 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003708 "returned by __reduce__ must be an iterator, not %s",
3709 Py_TYPE(dictitems)->tp_name);
3710 return -1;
3711 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003712
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003713 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003714 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003715 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003716
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003717 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3718 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003719 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003720 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003721 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003722 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3723 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003724 if (!use_newobj_ex) {
3725 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003726 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003727 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003728 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003729 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003730 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003731
3732 if (use_newobj_ex) {
3733 PyObject *cls;
3734 PyObject *args;
3735 PyObject *kwargs;
3736
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003737 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003738 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003739 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003740 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003741 return -1;
3742 }
3743
3744 cls = PyTuple_GET_ITEM(argtup, 0);
3745 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003746 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003747 "first item from NEWOBJ_EX argument tuple must "
3748 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3749 return -1;
3750 }
3751 args = PyTuple_GET_ITEM(argtup, 1);
3752 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003753 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003754 "second item from NEWOBJ_EX argument tuple must "
3755 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3756 return -1;
3757 }
3758 kwargs = PyTuple_GET_ITEM(argtup, 2);
3759 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003760 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003761 "third item from NEWOBJ_EX argument tuple must "
3762 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3763 return -1;
3764 }
3765
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003766 if (self->proto >= 4) {
3767 if (save(self, cls, 0) < 0 ||
3768 save(self, args, 0) < 0 ||
3769 save(self, kwargs, 0) < 0 ||
3770 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3771 return -1;
3772 }
3773 }
3774 else {
3775 PyObject *newargs;
3776 PyObject *cls_new;
3777 Py_ssize_t i;
3778 _Py_IDENTIFIER(__new__);
3779
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003780 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003781 if (newargs == NULL)
3782 return -1;
3783
3784 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3785 if (cls_new == NULL) {
3786 Py_DECREF(newargs);
3787 return -1;
3788 }
3789 PyTuple_SET_ITEM(newargs, 0, cls_new);
3790 Py_INCREF(cls);
3791 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003792 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003793 PyObject *item = PyTuple_GET_ITEM(args, i);
3794 Py_INCREF(item);
3795 PyTuple_SET_ITEM(newargs, i + 2, item);
3796 }
3797
3798 callable = PyObject_Call(st->partial, newargs, kwargs);
3799 Py_DECREF(newargs);
3800 if (callable == NULL)
3801 return -1;
3802
3803 newargs = PyTuple_New(0);
3804 if (newargs == NULL) {
3805 Py_DECREF(callable);
3806 return -1;
3807 }
3808
3809 if (save(self, callable, 0) < 0 ||
3810 save(self, newargs, 0) < 0 ||
3811 _Pickler_Write(self, &reduce_op, 1) < 0) {
3812 Py_DECREF(newargs);
3813 Py_DECREF(callable);
3814 return -1;
3815 }
3816 Py_DECREF(newargs);
3817 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003818 }
3819 }
3820 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003821 PyObject *cls;
3822 PyObject *newargtup;
3823 PyObject *obj_class;
3824 int p;
3825
3826 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003827 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003828 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003829 return -1;
3830 }
3831
3832 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003833 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003834 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003835 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003836 return -1;
3837 }
3838
3839 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003840 obj_class = get_class(obj);
Miss Islington (bot)e2f376f2018-12-05 11:35:41 -08003841 if (obj_class == NULL) {
3842 return -1;
3843 }
3844 p = obj_class != cls;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003845 Py_DECREF(obj_class);
3846 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003847 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003848 "__newobj__ args has the wrong class");
3849 return -1;
3850 }
3851 }
3852 /* XXX: These calls save() are prone to infinite recursion. Imagine
3853 what happen if the value returned by the __reduce__() method of
3854 some extension type contains another object of the same type. Ouch!
3855
3856 Here is a quick example, that I ran into, to illustrate what I
3857 mean:
3858
3859 >>> import pickle, copyreg
3860 >>> copyreg.dispatch_table.pop(complex)
3861 >>> pickle.dumps(1+2j)
3862 Traceback (most recent call last):
3863 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003864 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003865
3866 Removing the complex class from copyreg.dispatch_table made the
3867 __reduce_ex__() method emit another complex object:
3868
3869 >>> (1+1j).__reduce_ex__(2)
3870 (<function __newobj__ at 0xb7b71c3c>,
3871 (<class 'complex'>, (1+1j)), None, None, None)
3872
3873 Thus when save() was called on newargstup (the 2nd item) recursion
3874 ensued. Of course, the bug was in the complex class which had a
3875 broken __getnewargs__() that emitted another complex object. But,
3876 the point, here, is it is quite easy to end up with a broken reduce
3877 function. */
3878
3879 /* Save the class and its __new__ arguments. */
3880 if (save(self, cls, 0) < 0)
3881 return -1;
3882
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003883 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003884 if (newargtup == NULL)
3885 return -1;
3886
3887 p = save(self, newargtup, 0);
3888 Py_DECREF(newargtup);
3889 if (p < 0)
3890 return -1;
3891
3892 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003893 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003894 return -1;
3895 }
3896 else { /* Not using NEWOBJ. */
3897 if (save(self, callable, 0) < 0 ||
3898 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003899 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003900 return -1;
3901 }
3902
3903 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3904 the caller do not want to memoize the object. Not particularly useful,
3905 but that is to mimic the behavior save_reduce() in pickle.py when
3906 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003907 if (obj != NULL) {
3908 /* If the object is already in the memo, this means it is
3909 recursive. In this case, throw away everything we put on the
3910 stack, and fetch the object back from the memo. */
3911 if (PyMemoTable_Get(self->memo, obj)) {
3912 const char pop_op = POP;
3913
3914 if (_Pickler_Write(self, &pop_op, 1) < 0)
3915 return -1;
3916 if (memo_get(self, obj) < 0)
3917 return -1;
3918
3919 return 0;
3920 }
3921 else if (memo_put(self, obj) < 0)
3922 return -1;
3923 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003924
3925 if (listitems && batch_list(self, listitems) < 0)
3926 return -1;
3927
3928 if (dictitems && batch_dict(self, dictitems) < 0)
3929 return -1;
3930
3931 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003932 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003933 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003934 return -1;
3935 }
3936
3937 return 0;
3938}
3939
3940static int
3941save(PicklerObject *self, PyObject *obj, int pers_save)
3942{
3943 PyTypeObject *type;
3944 PyObject *reduce_func = NULL;
3945 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003946 int status = 0;
3947
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003948 if (_Pickler_OpcodeBoundary(self) < 0)
3949 return -1;
3950
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003951 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003952 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003953
3954 /* The extra pers_save argument is necessary to avoid calling save_pers()
3955 on its returned object. */
3956 if (!pers_save && self->pers_func) {
3957 /* save_pers() returns:
3958 -1 to signal an error;
3959 0 if it did nothing successfully;
3960 1 if a persistent id was saved.
3961 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003962 if ((status = save_pers(self, obj)) != 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003963 goto done;
3964 }
3965
3966 type = Py_TYPE(obj);
3967
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003968 /* The old cPickle had an optimization that used switch-case statement
3969 dispatching on the first letter of the type name. This has was removed
3970 since benchmarks shown that this optimization was actually slowing
3971 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003972
3973 /* Atom types; these aren't memoized, so don't check the memo. */
3974
3975 if (obj == Py_None) {
3976 status = save_none(self, obj);
3977 goto done;
3978 }
3979 else if (obj == Py_False || obj == Py_True) {
3980 status = save_bool(self, obj);
3981 goto done;
3982 }
3983 else if (type == &PyLong_Type) {
3984 status = save_long(self, obj);
3985 goto done;
3986 }
3987 else if (type == &PyFloat_Type) {
3988 status = save_float(self, obj);
3989 goto done;
3990 }
3991
3992 /* Check the memo to see if it has the object. If so, generate
3993 a GET (or BINGET) opcode, instead of pickling the object
3994 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003995 if (PyMemoTable_Get(self->memo, obj)) {
3996 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003997 goto error;
3998 goto done;
3999 }
4000
4001 if (type == &PyBytes_Type) {
4002 status = save_bytes(self, obj);
4003 goto done;
4004 }
4005 else if (type == &PyUnicode_Type) {
4006 status = save_unicode(self, obj);
4007 goto done;
4008 }
4009 else if (type == &PyDict_Type) {
4010 status = save_dict(self, obj);
4011 goto done;
4012 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004013 else if (type == &PySet_Type) {
4014 status = save_set(self, obj);
4015 goto done;
4016 }
4017 else if (type == &PyFrozenSet_Type) {
4018 status = save_frozenset(self, obj);
4019 goto done;
4020 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004021 else if (type == &PyList_Type) {
4022 status = save_list(self, obj);
4023 goto done;
4024 }
4025 else if (type == &PyTuple_Type) {
4026 status = save_tuple(self, obj);
4027 goto done;
4028 }
4029 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004030 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031 goto done;
4032 }
4033 else if (type == &PyFunction_Type) {
4034 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004035 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004036 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004037
4038 /* XXX: This part needs some unit tests. */
4039
4040 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004041 * self.dispatch_table, copyreg.dispatch_table, the object's
4042 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004043 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004044 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004045 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004046 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4047 (PyObject *)type);
4048 if (reduce_func == NULL) {
4049 if (PyErr_Occurred()) {
4050 goto error;
4051 }
4052 } else {
4053 /* PyDict_GetItemWithError() returns a borrowed reference.
4054 Increase the reference count to be consistent with
4055 PyObject_GetItem and _PyObject_GetAttrId used below. */
4056 Py_INCREF(reduce_func);
4057 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004058 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004059 reduce_func = PyObject_GetItem(self->dispatch_table,
4060 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004061 if (reduce_func == NULL) {
4062 if (PyErr_ExceptionMatches(PyExc_KeyError))
4063 PyErr_Clear();
4064 else
4065 goto error;
4066 }
4067 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004068 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004069 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004070 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004071 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004072 else if (PyType_IsSubtype(type, &PyType_Type)) {
4073 status = save_global(self, obj, NULL);
4074 goto done;
4075 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004076 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004077 _Py_IDENTIFIER(__reduce__);
4078 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004079
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004080
4081 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4082 automatically defined as __reduce__. While this is convenient, this
4083 make it impossible to know which method was actually called. Of
4084 course, this is not a big deal. But still, it would be nice to let
4085 the user know which method was called when something go
4086 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4087 don't actually have to check for a __reduce__ method. */
4088
4089 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004090 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4091 goto error;
4092 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 if (reduce_func != NULL) {
4094 PyObject *proto;
4095 proto = PyLong_FromLong(self->proto);
4096 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004097 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004098 }
4099 }
4100 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004101 PickleState *st = _Pickle_GetGlobalState();
4102
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004103 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004104 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004105 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004106 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004107 }
4108 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004109 PyErr_Format(st->PicklingError,
4110 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004111 type->tp_name, obj);
4112 goto error;
4113 }
4114 }
4115 }
4116
4117 if (reduce_value == NULL)
4118 goto error;
4119
4120 if (PyUnicode_Check(reduce_value)) {
4121 status = save_global(self, obj, reduce_value);
4122 goto done;
4123 }
4124
4125 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004126 PickleState *st = _Pickle_GetGlobalState();
4127 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004128 "__reduce__ must return a string or tuple");
4129 goto error;
4130 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004131
4132 status = save_reduce(self, reduce_value, obj);
4133
4134 if (0) {
4135 error:
4136 status = -1;
4137 }
4138 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004139
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004140 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004141 Py_XDECREF(reduce_func);
4142 Py_XDECREF(reduce_value);
4143
4144 return status;
4145}
4146
4147static int
4148dump(PicklerObject *self, PyObject *obj)
4149{
4150 const char stop_op = STOP;
4151
4152 if (self->proto >= 2) {
4153 char header[2];
4154
4155 header[0] = PROTO;
4156 assert(self->proto >= 0 && self->proto < 256);
4157 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004158 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004159 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004160 if (self->proto >= 4)
4161 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004162 }
4163
4164 if (save(self, obj, 0) < 0 ||
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004165 _Pickler_Write(self, &stop_op, 1) < 0 ||
4166 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004167 return -1;
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004168 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004169 return 0;
4170}
4171
Larry Hastings61272b72014-01-07 12:41:53 -08004172/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004173
4174_pickle.Pickler.clear_memo
4175
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004176Clears the pickler's "memo".
4177
4178The memo is the data structure that remembers which objects the
4179pickler has already seen, so that shared or recursive objects are
4180pickled by reference and not by value. This method is useful when
4181re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004182[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004183
Larry Hastings3cceb382014-01-04 11:09:09 -08004184static PyObject *
4185_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004186/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004187{
4188 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004189 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004190
4191 Py_RETURN_NONE;
4192}
4193
Larry Hastings61272b72014-01-07 12:41:53 -08004194/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004195
4196_pickle.Pickler.dump
4197
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004198 obj: object
4199 /
4200
4201Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004202[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004203
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004204static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004205_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004206/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004207{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004208 /* Check whether the Pickler was initialized correctly (issue3664).
4209 Developers often forget to call __init__() in their subclasses, which
4210 would trigger a segfault without this check. */
4211 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004212 PickleState *st = _Pickle_GetGlobalState();
4213 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004214 "Pickler.__init__() was not called by %s.__init__()",
4215 Py_TYPE(self)->tp_name);
4216 return NULL;
4217 }
4218
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004219 if (_Pickler_ClearBuffer(self) < 0)
4220 return NULL;
4221
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004222 if (dump(self, obj) < 0)
4223 return NULL;
4224
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004225 if (_Pickler_FlushToFile(self) < 0)
4226 return NULL;
4227
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004228 Py_RETURN_NONE;
4229}
4230
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004231/*[clinic input]
4232
4233_pickle.Pickler.__sizeof__ -> Py_ssize_t
4234
4235Returns size in memory, in bytes.
4236[clinic start generated code]*/
4237
4238static Py_ssize_t
4239_pickle_Pickler___sizeof___impl(PicklerObject *self)
4240/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4241{
4242 Py_ssize_t res, s;
4243
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004244 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004245 if (self->memo != NULL) {
4246 res += sizeof(PyMemoTable);
4247 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4248 }
4249 if (self->output_buffer != NULL) {
4250 s = _PySys_GetSizeOf(self->output_buffer);
4251 if (s == -1)
4252 return -1;
4253 res += s;
4254 }
4255 return res;
4256}
4257
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004258static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004259 _PICKLE_PICKLER_DUMP_METHODDEF
4260 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004261 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004262 {NULL, NULL} /* sentinel */
4263};
4264
4265static void
4266Pickler_dealloc(PicklerObject *self)
4267{
4268 PyObject_GC_UnTrack(self);
4269
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004270 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004271 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004272 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004273 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004274 Py_XDECREF(self->fast_memo);
4275
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004276 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004277
4278 Py_TYPE(self)->tp_free((PyObject *)self);
4279}
4280
4281static int
4282Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4283{
4284 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004285 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004286 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004287 Py_VISIT(self->fast_memo);
4288 return 0;
4289}
4290
4291static int
4292Pickler_clear(PicklerObject *self)
4293{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004294 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004295 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004296 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004297 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004298 Py_CLEAR(self->fast_memo);
4299
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004300 if (self->memo != NULL) {
4301 PyMemoTable *memo = self->memo;
4302 self->memo = NULL;
4303 PyMemoTable_Del(memo);
4304 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004305 return 0;
4306}
4307
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004308
Larry Hastings61272b72014-01-07 12:41:53 -08004309/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004310
4311_pickle.Pickler.__init__
4312
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004313 file: object
4314 protocol: object = NULL
4315 fix_imports: bool = True
4316
4317This takes a binary file for writing a pickle data stream.
4318
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004319The optional *protocol* argument tells the pickler to use the given
4320protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4321protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004322
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004323Specifying a negative protocol version selects the highest protocol
4324version supported. The higher the protocol used, the more recent the
4325version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004326
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004327The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004328bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004329writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004330this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004331
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004332If *fix_imports* is True and protocol is less than 3, pickle will try
4333to map the new Python 3 names to the old module names used in Python
43342, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004335[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004336
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004337static int
Larry Hastings89964c42015-04-14 18:07:59 -04004338_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4339 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004340/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004341{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004342 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004343 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004344
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004345 /* In case of multiple __init__() calls, clear previous content. */
4346 if (self->write != NULL)
4347 (void)Pickler_clear(self);
4348
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004349 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004350 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004351
4352 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004353 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004354
4355 /* memo and output_buffer may have already been created in _Pickler_New */
4356 if (self->memo == NULL) {
4357 self->memo = PyMemoTable_New();
4358 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004359 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004360 }
4361 self->output_len = 0;
4362 if (self->output_buffer == NULL) {
4363 self->max_output_len = WRITE_BUF_SIZE;
4364 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4365 self->max_output_len);
4366 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004367 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004368 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004369
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004370 self->fast = 0;
4371 self->fast_nesting = 0;
4372 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004373
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004374 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4375 &self->pers_func, &self->pers_func_self) < 0)
4376 {
4377 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004378 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004379
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004380 if (_PyObject_LookupAttrId((PyObject *)self,
4381 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4382 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004383 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004384
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004385 return 0;
4386}
4387
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004388
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004389/* Define a proxy object for the Pickler's internal memo object. This is to
4390 * avoid breaking code like:
4391 * pickler.memo.clear()
4392 * and
4393 * pickler.memo = saved_memo
4394 * Is this a good idea? Not really, but we don't want to break code that uses
4395 * it. Note that we don't implement the entire mapping API here. This is
4396 * intentional, as these should be treated as black-box implementation details.
4397 */
4398
Larry Hastings61272b72014-01-07 12:41:53 -08004399/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004400_pickle.PicklerMemoProxy.clear
4401
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004402Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004403[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004404
Larry Hastings3cceb382014-01-04 11:09:09 -08004405static PyObject *
4406_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004407/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004408{
4409 if (self->pickler->memo)
4410 PyMemoTable_Clear(self->pickler->memo);
4411 Py_RETURN_NONE;
4412}
4413
Larry Hastings61272b72014-01-07 12:41:53 -08004414/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004415_pickle.PicklerMemoProxy.copy
4416
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004417Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004418[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004419
Larry Hastings3cceb382014-01-04 11:09:09 -08004420static PyObject *
4421_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004422/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004423{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004424 PyMemoTable *memo;
4425 PyObject *new_memo = PyDict_New();
4426 if (new_memo == NULL)
4427 return NULL;
4428
4429 memo = self->pickler->memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07004430 for (size_t i = 0; i < memo->mt_allocated; ++i) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004431 PyMemoEntry entry = memo->mt_table[i];
4432 if (entry.me_key != NULL) {
4433 int status;
4434 PyObject *key, *value;
4435
4436 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004437 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004438
4439 if (key == NULL || value == NULL) {
4440 Py_XDECREF(key);
4441 Py_XDECREF(value);
4442 goto error;
4443 }
4444 status = PyDict_SetItem(new_memo, key, value);
4445 Py_DECREF(key);
4446 Py_DECREF(value);
4447 if (status < 0)
4448 goto error;
4449 }
4450 }
4451 return new_memo;
4452
4453 error:
4454 Py_XDECREF(new_memo);
4455 return NULL;
4456}
4457
Larry Hastings61272b72014-01-07 12:41:53 -08004458/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004459_pickle.PicklerMemoProxy.__reduce__
4460
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004461Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004462[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004463
Larry Hastings3cceb382014-01-04 11:09:09 -08004464static PyObject *
4465_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004466/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004467{
4468 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004469 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004470 if (contents == NULL)
4471 return NULL;
4472
4473 reduce_value = PyTuple_New(2);
4474 if (reduce_value == NULL) {
4475 Py_DECREF(contents);
4476 return NULL;
4477 }
4478 dict_args = PyTuple_New(1);
4479 if (dict_args == NULL) {
4480 Py_DECREF(contents);
4481 Py_DECREF(reduce_value);
4482 return NULL;
4483 }
4484 PyTuple_SET_ITEM(dict_args, 0, contents);
4485 Py_INCREF((PyObject *)&PyDict_Type);
4486 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4487 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4488 return reduce_value;
4489}
4490
4491static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004492 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4493 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4494 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004495 {NULL, NULL} /* sentinel */
4496};
4497
4498static void
4499PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4500{
4501 PyObject_GC_UnTrack(self);
4502 Py_XDECREF(self->pickler);
4503 PyObject_GC_Del((PyObject *)self);
4504}
4505
4506static int
4507PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4508 visitproc visit, void *arg)
4509{
4510 Py_VISIT(self->pickler);
4511 return 0;
4512}
4513
4514static int
4515PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4516{
4517 Py_CLEAR(self->pickler);
4518 return 0;
4519}
4520
4521static PyTypeObject PicklerMemoProxyType = {
4522 PyVarObject_HEAD_INIT(NULL, 0)
4523 "_pickle.PicklerMemoProxy", /*tp_name*/
4524 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4525 0,
4526 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4527 0, /* tp_print */
4528 0, /* tp_getattr */
4529 0, /* tp_setattr */
4530 0, /* tp_compare */
4531 0, /* tp_repr */
4532 0, /* tp_as_number */
4533 0, /* tp_as_sequence */
4534 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004535 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004536 0, /* tp_call */
4537 0, /* tp_str */
4538 PyObject_GenericGetAttr, /* tp_getattro */
4539 PyObject_GenericSetAttr, /* tp_setattro */
4540 0, /* tp_as_buffer */
4541 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4542 0, /* tp_doc */
4543 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4544 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4545 0, /* tp_richcompare */
4546 0, /* tp_weaklistoffset */
4547 0, /* tp_iter */
4548 0, /* tp_iternext */
4549 picklerproxy_methods, /* tp_methods */
4550};
4551
4552static PyObject *
4553PicklerMemoProxy_New(PicklerObject *pickler)
4554{
4555 PicklerMemoProxyObject *self;
4556
4557 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4558 if (self == NULL)
4559 return NULL;
4560 Py_INCREF(pickler);
4561 self->pickler = pickler;
4562 PyObject_GC_Track(self);
4563 return (PyObject *)self;
4564}
4565
4566/*****************************************************************************/
4567
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004568static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004569Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004571 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004572}
4573
4574static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004575Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004576{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004577 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004578
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004579 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004580 PyErr_SetString(PyExc_TypeError,
4581 "attribute deletion is not supported");
4582 return -1;
4583 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004584
4585 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4586 PicklerObject *pickler =
4587 ((PicklerMemoProxyObject *)obj)->pickler;
4588
4589 new_memo = PyMemoTable_Copy(pickler->memo);
4590 if (new_memo == NULL)
4591 return -1;
4592 }
4593 else if (PyDict_Check(obj)) {
4594 Py_ssize_t i = 0;
4595 PyObject *key, *value;
4596
4597 new_memo = PyMemoTable_New();
4598 if (new_memo == NULL)
4599 return -1;
4600
4601 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004602 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004603 PyObject *memo_obj;
4604
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004605 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004606 PyErr_SetString(PyExc_TypeError,
4607 "'memo' values must be 2-item tuples");
4608 goto error;
4609 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004610 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004611 if (memo_id == -1 && PyErr_Occurred())
4612 goto error;
4613 memo_obj = PyTuple_GET_ITEM(value, 1);
4614 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4615 goto error;
4616 }
4617 }
4618 else {
4619 PyErr_Format(PyExc_TypeError,
Miss Islington (bot)7beb8c52018-11-05 06:52:58 -08004620 "'memo' attribute must be a PicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004621 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004622 return -1;
4623 }
4624
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004625 PyMemoTable_Del(self->memo);
4626 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004627
4628 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004629
4630 error:
4631 if (new_memo)
4632 PyMemoTable_Del(new_memo);
4633 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004634}
4635
4636static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004637Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004638{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004639 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004640 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004641 return NULL;
4642 }
4643 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004644}
4645
4646static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08004647Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004648{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004649 if (value == NULL) {
4650 PyErr_SetString(PyExc_TypeError,
4651 "attribute deletion is not supported");
4652 return -1;
4653 }
4654 if (!PyCallable_Check(value)) {
4655 PyErr_SetString(PyExc_TypeError,
4656 "persistent_id must be a callable taking one argument");
4657 return -1;
4658 }
4659
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004660 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004661 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004662 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004663
4664 return 0;
4665}
4666
4667static PyMemberDef Pickler_members[] = {
4668 {"bin", T_INT, offsetof(PicklerObject, bin)},
4669 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004670 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004671 {NULL}
4672};
4673
4674static PyGetSetDef Pickler_getsets[] = {
4675 {"memo", (getter)Pickler_get_memo,
4676 (setter)Pickler_set_memo},
4677 {"persistent_id", (getter)Pickler_get_persid,
4678 (setter)Pickler_set_persid},
4679 {NULL}
4680};
4681
4682static PyTypeObject Pickler_Type = {
4683 PyVarObject_HEAD_INIT(NULL, 0)
4684 "_pickle.Pickler" , /*tp_name*/
4685 sizeof(PicklerObject), /*tp_basicsize*/
4686 0, /*tp_itemsize*/
4687 (destructor)Pickler_dealloc, /*tp_dealloc*/
4688 0, /*tp_print*/
4689 0, /*tp_getattr*/
4690 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004691 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004692 0, /*tp_repr*/
4693 0, /*tp_as_number*/
4694 0, /*tp_as_sequence*/
4695 0, /*tp_as_mapping*/
4696 0, /*tp_hash*/
4697 0, /*tp_call*/
4698 0, /*tp_str*/
4699 0, /*tp_getattro*/
4700 0, /*tp_setattro*/
4701 0, /*tp_as_buffer*/
4702 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004703 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004704 (traverseproc)Pickler_traverse, /*tp_traverse*/
4705 (inquiry)Pickler_clear, /*tp_clear*/
4706 0, /*tp_richcompare*/
4707 0, /*tp_weaklistoffset*/
4708 0, /*tp_iter*/
4709 0, /*tp_iternext*/
4710 Pickler_methods, /*tp_methods*/
4711 Pickler_members, /*tp_members*/
4712 Pickler_getsets, /*tp_getset*/
4713 0, /*tp_base*/
4714 0, /*tp_dict*/
4715 0, /*tp_descr_get*/
4716 0, /*tp_descr_set*/
4717 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004718 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004719 PyType_GenericAlloc, /*tp_alloc*/
4720 PyType_GenericNew, /*tp_new*/
4721 PyObject_GC_Del, /*tp_free*/
4722 0, /*tp_is_gc*/
4723};
4724
Victor Stinner121aab42011-09-29 23:40:53 +02004725/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004726
4727 XXX: It would be nice to able to avoid Python function call overhead, by
4728 using directly the C version of find_class(), when find_class() is not
4729 overridden by a subclass. Although, this could become rather hackish. A
4730 simpler optimization would be to call the C function when self is not a
4731 subclass instance. */
4732static PyObject *
4733find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4734{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004735 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004736
Victor Stinner55ba38a2016-12-09 16:09:30 +01004737 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4738 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004739}
4740
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004741static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004742marker(UnpicklerObject *self)
4743{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004744 Py_ssize_t mark;
4745
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004746 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004747 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004748 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004749 return -1;
4750 }
4751
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004752 mark = self->marks[--self->num_marks];
4753 self->stack->mark_set = self->num_marks != 0;
4754 self->stack->fence = self->num_marks ?
4755 self->marks[self->num_marks - 1] : 0;
4756 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004757}
4758
4759static int
4760load_none(UnpicklerObject *self)
4761{
4762 PDATA_APPEND(self->stack, Py_None, -1);
4763 return 0;
4764}
4765
4766static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004767load_int(UnpicklerObject *self)
4768{
4769 PyObject *value;
4770 char *endptr, *s;
4771 Py_ssize_t len;
4772 long x;
4773
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004774 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004775 return -1;
4776 if (len < 2)
4777 return bad_readline();
4778
4779 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004780 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004781 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004782 x = strtol(s, &endptr, 0);
4783
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004784 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004785 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004786 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004787 errno = 0;
4788 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004789 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004790 if (value == NULL) {
4791 PyErr_SetString(PyExc_ValueError,
4792 "could not convert string to int");
4793 return -1;
4794 }
4795 }
4796 else {
4797 if (len == 3 && (x == 0 || x == 1)) {
4798 if ((value = PyBool_FromLong(x)) == NULL)
4799 return -1;
4800 }
4801 else {
4802 if ((value = PyLong_FromLong(x)) == NULL)
4803 return -1;
4804 }
4805 }
4806
4807 PDATA_PUSH(self->stack, value, -1);
4808 return 0;
4809}
4810
4811static int
4812load_bool(UnpicklerObject *self, PyObject *boolean)
4813{
4814 assert(boolean == Py_True || boolean == Py_False);
4815 PDATA_APPEND(self->stack, boolean, -1);
4816 return 0;
4817}
4818
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004819/* s contains x bytes of an unsigned little-endian integer. Return its value
4820 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4821 */
4822static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004823calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004824{
4825 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004826 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004827 size_t x = 0;
4828
Serhiy Storchakae0606192015-09-29 22:10:07 +03004829 if (nbytes > (int)sizeof(size_t)) {
4830 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4831 * have 64-bit size that can't be represented on 32-bit platform.
4832 */
4833 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4834 if (s[i])
4835 return -1;
4836 }
4837 nbytes = (int)sizeof(size_t);
4838 }
4839 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004840 x |= (size_t) s[i] << (8 * i);
4841 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004842
4843 if (x > PY_SSIZE_T_MAX)
4844 return -1;
4845 else
4846 return (Py_ssize_t) x;
4847}
4848
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004849/* s contains x bytes of a little-endian integer. Return its value as a
4850 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004851 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004852 * of x-platform bugs.
4853 */
4854static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004855calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004856{
4857 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004858 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004859 long x = 0;
4860
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004861 for (i = 0; i < nbytes; i++) {
4862 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004863 }
4864
4865 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4866 * is signed, so on a box with longs bigger than 4 bytes we need
4867 * to extend a BININT's sign bit to the full width.
4868 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004869 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004870 x |= -(x & (1L << 31));
4871 }
4872
4873 return x;
4874}
4875
4876static int
4877load_binintx(UnpicklerObject *self, char *s, int size)
4878{
4879 PyObject *value;
4880 long x;
4881
4882 x = calc_binint(s, size);
4883
4884 if ((value = PyLong_FromLong(x)) == NULL)
4885 return -1;
4886
4887 PDATA_PUSH(self->stack, value, -1);
4888 return 0;
4889}
4890
4891static int
4892load_binint(UnpicklerObject *self)
4893{
4894 char *s;
4895
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004896 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004897 return -1;
4898
4899 return load_binintx(self, s, 4);
4900}
4901
4902static int
4903load_binint1(UnpicklerObject *self)
4904{
4905 char *s;
4906
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004907 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004908 return -1;
4909
4910 return load_binintx(self, s, 1);
4911}
4912
4913static int
4914load_binint2(UnpicklerObject *self)
4915{
4916 char *s;
4917
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004918 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004919 return -1;
4920
4921 return load_binintx(self, s, 2);
4922}
4923
4924static int
4925load_long(UnpicklerObject *self)
4926{
4927 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004928 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004929 Py_ssize_t len;
4930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004931 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004932 return -1;
4933 if (len < 2)
4934 return bad_readline();
4935
Mark Dickinson8dd05142009-01-20 20:43:58 +00004936 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4937 the 'L' before calling PyLong_FromString. In order to maintain
4938 compatibility with Python 3.0.0, we don't actually *require*
4939 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004940 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004941 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004942 /* XXX: Should the base argument explicitly set to 10? */
4943 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004944 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004945 return -1;
4946
4947 PDATA_PUSH(self->stack, value, -1);
4948 return 0;
4949}
4950
4951/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4952 * data following.
4953 */
4954static int
4955load_counted_long(UnpicklerObject *self, int size)
4956{
4957 PyObject *value;
4958 char *nbytes;
4959 char *pdata;
4960
4961 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004962 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004963 return -1;
4964
4965 size = calc_binint(nbytes, size);
4966 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004967 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004969 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004970 "LONG pickle has negative byte count");
4971 return -1;
4972 }
4973
4974 if (size == 0)
4975 value = PyLong_FromLong(0L);
4976 else {
4977 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004978 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004979 return -1;
4980 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4981 1 /* little endian */ , 1 /* signed */ );
4982 }
4983 if (value == NULL)
4984 return -1;
4985 PDATA_PUSH(self->stack, value, -1);
4986 return 0;
4987}
4988
4989static int
4990load_float(UnpicklerObject *self)
4991{
4992 PyObject *value;
4993 char *endptr, *s;
4994 Py_ssize_t len;
4995 double d;
4996
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004997 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004998 return -1;
4999 if (len < 2)
5000 return bad_readline();
5001
5002 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00005003 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
5004 if (d == -1.0 && PyErr_Occurred())
5005 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005006 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005007 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5008 return -1;
5009 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005010 value = PyFloat_FromDouble(d);
5011 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005012 return -1;
5013
5014 PDATA_PUSH(self->stack, value, -1);
5015 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005016}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005017
5018static int
5019load_binfloat(UnpicklerObject *self)
5020{
5021 PyObject *value;
5022 double x;
5023 char *s;
5024
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005025 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005026 return -1;
5027
5028 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5029 if (x == -1.0 && PyErr_Occurred())
5030 return -1;
5031
5032 if ((value = PyFloat_FromDouble(x)) == NULL)
5033 return -1;
5034
5035 PDATA_PUSH(self->stack, value, -1);
5036 return 0;
5037}
5038
5039static int
5040load_string(UnpicklerObject *self)
5041{
5042 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005043 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044 Py_ssize_t len;
5045 char *s, *p;
5046
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005047 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005049 /* Strip the newline */
5050 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005051 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005052 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005053 p = s + 1;
5054 len -= 2;
5055 }
5056 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005057 PickleState *st = _Pickle_GetGlobalState();
5058 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005059 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005060 return -1;
5061 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005062 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005063
5064 /* Use the PyBytes API to decode the string, since that is what is used
5065 to encode, and then coerce the result to Unicode. */
5066 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005067 if (bytes == NULL)
5068 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005069
5070 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5071 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5072 if (strcmp(self->encoding, "bytes") == 0) {
5073 obj = bytes;
5074 }
5075 else {
5076 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5077 Py_DECREF(bytes);
5078 if (obj == NULL) {
5079 return -1;
5080 }
5081 }
5082
5083 PDATA_PUSH(self->stack, obj, -1);
5084 return 0;
5085}
5086
5087static int
5088load_counted_binstring(UnpicklerObject *self, int nbytes)
5089{
5090 PyObject *obj;
5091 Py_ssize_t size;
5092 char *s;
5093
5094 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005095 return -1;
5096
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005097 size = calc_binsize(s, nbytes);
5098 if (size < 0) {
5099 PickleState *st = _Pickle_GetGlobalState();
5100 PyErr_Format(st->UnpicklingError,
5101 "BINSTRING exceeds system's maximum size of %zd bytes",
5102 PY_SSIZE_T_MAX);
5103 return -1;
5104 }
5105
5106 if (_Unpickler_Read(self, &s, size) < 0)
5107 return -1;
5108
5109 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5110 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5111 if (strcmp(self->encoding, "bytes") == 0) {
5112 obj = PyBytes_FromStringAndSize(s, size);
5113 }
5114 else {
5115 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5116 }
5117 if (obj == NULL) {
5118 return -1;
5119 }
5120
5121 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005122 return 0;
5123}
5124
5125static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005126load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005127{
5128 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005129 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005130 char *s;
5131
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005132 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005133 return -1;
5134
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005135 size = calc_binsize(s, nbytes);
5136 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005137 PyErr_Format(PyExc_OverflowError,
5138 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005139 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005140 return -1;
5141 }
5142
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005143 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005144 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005145
5146 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005147 if (bytes == NULL)
5148 return -1;
5149
5150 PDATA_PUSH(self->stack, bytes, -1);
5151 return 0;
5152}
5153
5154static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005155load_unicode(UnpicklerObject *self)
5156{
5157 PyObject *str;
5158 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005159 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005160
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005161 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005162 return -1;
5163 if (len < 1)
5164 return bad_readline();
5165
5166 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5167 if (str == NULL)
5168 return -1;
5169
5170 PDATA_PUSH(self->stack, str, -1);
5171 return 0;
5172}
5173
5174static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005175load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005176{
5177 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005178 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005179 char *s;
5180
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005181 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005182 return -1;
5183
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005184 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005185 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005186 PyErr_Format(PyExc_OverflowError,
5187 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005188 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005189 return -1;
5190 }
5191
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005192 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005193 return -1;
5194
Victor Stinner485fb562010-04-13 11:07:24 +00005195 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005196 if (str == NULL)
5197 return -1;
5198
5199 PDATA_PUSH(self->stack, str, -1);
5200 return 0;
5201}
5202
5203static int
Victor Stinner21b47112016-03-14 18:09:39 +01005204load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005205{
5206 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005207
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005208 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005209 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005210
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005211 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005212 if (tuple == NULL)
5213 return -1;
5214 PDATA_PUSH(self->stack, tuple, -1);
5215 return 0;
5216}
5217
5218static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005219load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005220{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005221 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005222
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005223 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005224 return -1;
5225
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005226 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005227}
5228
5229static int
5230load_empty_list(UnpicklerObject *self)
5231{
5232 PyObject *list;
5233
5234 if ((list = PyList_New(0)) == NULL)
5235 return -1;
5236 PDATA_PUSH(self->stack, list, -1);
5237 return 0;
5238}
5239
5240static int
5241load_empty_dict(UnpicklerObject *self)
5242{
5243 PyObject *dict;
5244
5245 if ((dict = PyDict_New()) == NULL)
5246 return -1;
5247 PDATA_PUSH(self->stack, dict, -1);
5248 return 0;
5249}
5250
5251static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005252load_empty_set(UnpicklerObject *self)
5253{
5254 PyObject *set;
5255
5256 if ((set = PySet_New(NULL)) == NULL)
5257 return -1;
5258 PDATA_PUSH(self->stack, set, -1);
5259 return 0;
5260}
5261
5262static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005263load_list(UnpicklerObject *self)
5264{
5265 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005266 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005267
5268 if ((i = marker(self)) < 0)
5269 return -1;
5270
5271 list = Pdata_poplist(self->stack, i);
5272 if (list == NULL)
5273 return -1;
5274 PDATA_PUSH(self->stack, list, -1);
5275 return 0;
5276}
5277
5278static int
5279load_dict(UnpicklerObject *self)
5280{
5281 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005282 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005283
5284 if ((i = marker(self)) < 0)
5285 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005286 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005287
5288 if ((dict = PyDict_New()) == NULL)
5289 return -1;
5290
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005291 if ((j - i) % 2 != 0) {
5292 PickleState *st = _Pickle_GetGlobalState();
5293 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005294 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005295 return -1;
5296 }
5297
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005298 for (k = i + 1; k < j; k += 2) {
5299 key = self->stack->data[k - 1];
5300 value = self->stack->data[k];
5301 if (PyDict_SetItem(dict, key, value) < 0) {
5302 Py_DECREF(dict);
5303 return -1;
5304 }
5305 }
5306 Pdata_clear(self->stack, i);
5307 PDATA_PUSH(self->stack, dict, -1);
5308 return 0;
5309}
5310
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005311static int
5312load_frozenset(UnpicklerObject *self)
5313{
5314 PyObject *items;
5315 PyObject *frozenset;
5316 Py_ssize_t i;
5317
5318 if ((i = marker(self)) < 0)
5319 return -1;
5320
5321 items = Pdata_poptuple(self->stack, i);
5322 if (items == NULL)
5323 return -1;
5324
5325 frozenset = PyFrozenSet_New(items);
5326 Py_DECREF(items);
5327 if (frozenset == NULL)
5328 return -1;
5329
5330 PDATA_PUSH(self->stack, frozenset, -1);
5331 return 0;
5332}
5333
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005334static PyObject *
5335instantiate(PyObject *cls, PyObject *args)
5336{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005337 /* Caller must assure args are a tuple. Normally, args come from
5338 Pdata_poptuple which packs objects from the top of the stack
5339 into a newly created tuple. */
5340 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005341 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5342 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005343 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005344 PyObject *func;
5345 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5346 return NULL;
5347 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005348 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005349 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5350 }
5351 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005352 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005353 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005354}
5355
5356static int
5357load_obj(UnpicklerObject *self)
5358{
5359 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005360 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005361
5362 if ((i = marker(self)) < 0)
5363 return -1;
5364
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005365 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005366 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005367
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005368 args = Pdata_poptuple(self->stack, i + 1);
5369 if (args == NULL)
5370 return -1;
5371
5372 PDATA_POP(self->stack, cls);
5373 if (cls) {
5374 obj = instantiate(cls, args);
5375 Py_DECREF(cls);
5376 }
5377 Py_DECREF(args);
5378 if (obj == NULL)
5379 return -1;
5380
5381 PDATA_PUSH(self->stack, obj, -1);
5382 return 0;
5383}
5384
5385static int
5386load_inst(UnpicklerObject *self)
5387{
5388 PyObject *cls = NULL;
5389 PyObject *args = NULL;
5390 PyObject *obj = NULL;
5391 PyObject *module_name;
5392 PyObject *class_name;
5393 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005394 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005395 char *s;
5396
5397 if ((i = marker(self)) < 0)
5398 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005399 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005400 return -1;
5401 if (len < 2)
5402 return bad_readline();
5403
5404 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5405 identifiers are permitted in Python 3.0, since the INST opcode is only
5406 supported by older protocols on Python 2.x. */
5407 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5408 if (module_name == NULL)
5409 return -1;
5410
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005411 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005412 if (len < 2) {
5413 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005414 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005415 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005416 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005417 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005418 cls = find_class(self, module_name, class_name);
5419 Py_DECREF(class_name);
5420 }
5421 }
5422 Py_DECREF(module_name);
5423
5424 if (cls == NULL)
5425 return -1;
5426
5427 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5428 obj = instantiate(cls, args);
5429 Py_DECREF(args);
5430 }
5431 Py_DECREF(cls);
5432
5433 if (obj == NULL)
5434 return -1;
5435
5436 PDATA_PUSH(self->stack, obj, -1);
5437 return 0;
5438}
5439
5440static int
5441load_newobj(UnpicklerObject *self)
5442{
5443 PyObject *args = NULL;
5444 PyObject *clsraw = NULL;
5445 PyTypeObject *cls; /* clsraw cast to its true type */
5446 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005447 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005448
5449 /* Stack is ... cls argtuple, and we want to call
5450 * cls.__new__(cls, *argtuple).
5451 */
5452 PDATA_POP(self->stack, args);
5453 if (args == NULL)
5454 goto error;
5455 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005456 PyErr_SetString(st->UnpicklingError,
5457 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005458 goto error;
5459 }
5460
5461 PDATA_POP(self->stack, clsraw);
5462 cls = (PyTypeObject *)clsraw;
5463 if (cls == NULL)
5464 goto error;
5465 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005466 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005467 "isn't a type object");
5468 goto error;
5469 }
5470 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005471 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005472 "has NULL tp_new");
5473 goto error;
5474 }
5475
5476 /* Call __new__. */
5477 obj = cls->tp_new(cls, args, NULL);
5478 if (obj == NULL)
5479 goto error;
5480
5481 Py_DECREF(args);
5482 Py_DECREF(clsraw);
5483 PDATA_PUSH(self->stack, obj, -1);
5484 return 0;
5485
5486 error:
5487 Py_XDECREF(args);
5488 Py_XDECREF(clsraw);
5489 return -1;
5490}
5491
5492static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005493load_newobj_ex(UnpicklerObject *self)
5494{
5495 PyObject *cls, *args, *kwargs;
5496 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005497 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005498
5499 PDATA_POP(self->stack, kwargs);
5500 if (kwargs == NULL) {
5501 return -1;
5502 }
5503 PDATA_POP(self->stack, args);
5504 if (args == NULL) {
5505 Py_DECREF(kwargs);
5506 return -1;
5507 }
5508 PDATA_POP(self->stack, cls);
5509 if (cls == NULL) {
5510 Py_DECREF(kwargs);
5511 Py_DECREF(args);
5512 return -1;
5513 }
Larry Hastings61272b72014-01-07 12:41:53 -08005514
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005515 if (!PyType_Check(cls)) {
5516 Py_DECREF(kwargs);
5517 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005518 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005519 "NEWOBJ_EX class argument must be a type, not %.200s",
5520 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005521 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005522 return -1;
5523 }
5524
5525 if (((PyTypeObject *)cls)->tp_new == NULL) {
5526 Py_DECREF(kwargs);
5527 Py_DECREF(args);
5528 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005529 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005530 "NEWOBJ_EX class argument doesn't have __new__");
5531 return -1;
5532 }
5533 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5534 Py_DECREF(kwargs);
5535 Py_DECREF(args);
5536 Py_DECREF(cls);
5537 if (obj == NULL) {
5538 return -1;
5539 }
5540 PDATA_PUSH(self->stack, obj, -1);
5541 return 0;
5542}
5543
5544static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005545load_global(UnpicklerObject *self)
5546{
5547 PyObject *global = NULL;
5548 PyObject *module_name;
5549 PyObject *global_name;
5550 Py_ssize_t len;
5551 char *s;
5552
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005553 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005554 return -1;
5555 if (len < 2)
5556 return bad_readline();
5557 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5558 if (!module_name)
5559 return -1;
5560
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005561 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005562 if (len < 2) {
5563 Py_DECREF(module_name);
5564 return bad_readline();
5565 }
5566 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5567 if (global_name) {
5568 global = find_class(self, module_name, global_name);
5569 Py_DECREF(global_name);
5570 }
5571 }
5572 Py_DECREF(module_name);
5573
5574 if (global == NULL)
5575 return -1;
5576 PDATA_PUSH(self->stack, global, -1);
5577 return 0;
5578}
5579
5580static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005581load_stack_global(UnpicklerObject *self)
5582{
5583 PyObject *global;
5584 PyObject *module_name;
5585 PyObject *global_name;
5586
5587 PDATA_POP(self->stack, global_name);
5588 PDATA_POP(self->stack, module_name);
5589 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5590 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005591 PickleState *st = _Pickle_GetGlobalState();
5592 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005593 Py_XDECREF(global_name);
5594 Py_XDECREF(module_name);
5595 return -1;
5596 }
5597 global = find_class(self, module_name, global_name);
5598 Py_DECREF(global_name);
5599 Py_DECREF(module_name);
5600 if (global == NULL)
5601 return -1;
5602 PDATA_PUSH(self->stack, global, -1);
5603 return 0;
5604}
5605
5606static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005607load_persid(UnpicklerObject *self)
5608{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005609 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005610 Py_ssize_t len;
5611 char *s;
5612
5613 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005614 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005616 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005617 return bad_readline();
5618
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005619 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5620 if (pid == NULL) {
5621 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5622 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5623 "persistent IDs in protocol 0 must be "
5624 "ASCII strings");
5625 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005626 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005627 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005629 obj = call_method(self->pers_func, self->pers_func_self, pid);
5630 Py_DECREF(pid);
5631 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632 return -1;
5633
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005634 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635 return 0;
5636 }
5637 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005638 PickleState *st = _Pickle_GetGlobalState();
5639 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005640 "A load persistent id instruction was encountered,\n"
5641 "but no persistent_load function was specified.");
5642 return -1;
5643 }
5644}
5645
5646static int
5647load_binpersid(UnpicklerObject *self)
5648{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005649 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005650
5651 if (self->pers_func) {
5652 PDATA_POP(self->stack, pid);
5653 if (pid == NULL)
5654 return -1;
5655
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005656 obj = call_method(self->pers_func, self->pers_func_self, pid);
5657 Py_DECREF(pid);
5658 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005659 return -1;
5660
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005661 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662 return 0;
5663 }
5664 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005665 PickleState *st = _Pickle_GetGlobalState();
5666 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005667 "A load persistent id instruction was encountered,\n"
5668 "but no persistent_load function was specified.");
5669 return -1;
5670 }
5671}
5672
5673static int
5674load_pop(UnpicklerObject *self)
5675{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005676 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005677
5678 /* Note that we split the (pickle.py) stack into two stacks,
5679 * an object stack and a mark stack. We have to be clever and
5680 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005681 * mark stack first, and only signalling a stack underflow if
5682 * the object stack is empty and the mark stack doesn't match
5683 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005685 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005686 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005687 self->stack->mark_set = self->num_marks != 0;
5688 self->stack->fence = self->num_marks ?
5689 self->marks[self->num_marks - 1] : 0;
5690 } else if (len <= self->stack->fence)
5691 return Pdata_stack_underflow(self->stack);
5692 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005693 len--;
5694 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005695 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005697 return 0;
5698}
5699
5700static int
5701load_pop_mark(UnpicklerObject *self)
5702{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005703 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005704
5705 if ((i = marker(self)) < 0)
5706 return -1;
5707
5708 Pdata_clear(self->stack, i);
5709
5710 return 0;
5711}
5712
5713static int
5714load_dup(UnpicklerObject *self)
5715{
5716 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005717 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005718
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005719 if (len <= self->stack->fence)
5720 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005721 last = self->stack->data[len - 1];
5722 PDATA_APPEND(self->stack, last, -1);
5723 return 0;
5724}
5725
5726static int
5727load_get(UnpicklerObject *self)
5728{
5729 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005730 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005731 Py_ssize_t len;
5732 char *s;
5733
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005734 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005735 return -1;
5736 if (len < 2)
5737 return bad_readline();
5738
5739 key = PyLong_FromString(s, NULL, 10);
5740 if (key == NULL)
5741 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005742 idx = PyLong_AsSsize_t(key);
5743 if (idx == -1 && PyErr_Occurred()) {
5744 Py_DECREF(key);
5745 return -1;
5746 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005747
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005748 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005749 if (value == NULL) {
5750 if (!PyErr_Occurred())
5751 PyErr_SetObject(PyExc_KeyError, key);
5752 Py_DECREF(key);
5753 return -1;
5754 }
5755 Py_DECREF(key);
5756
5757 PDATA_APPEND(self->stack, value, -1);
5758 return 0;
5759}
5760
5761static int
5762load_binget(UnpicklerObject *self)
5763{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005764 PyObject *value;
5765 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766 char *s;
5767
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005768 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005769 return -1;
5770
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005771 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005773 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005775 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005776 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005777 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005778 Py_DECREF(key);
5779 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005780 return -1;
5781 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005782
5783 PDATA_APPEND(self->stack, value, -1);
5784 return 0;
5785}
5786
5787static int
5788load_long_binget(UnpicklerObject *self)
5789{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 PyObject *value;
5791 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005792 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005794 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005795 return -1;
5796
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005797 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005798
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005799 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005800 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005801 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005802 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005804 Py_DECREF(key);
5805 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005806 return -1;
5807 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005808
5809 PDATA_APPEND(self->stack, value, -1);
5810 return 0;
5811}
5812
5813/* Push an object from the extension registry (EXT[124]). nbytes is
5814 * the number of bytes following the opcode, holding the index (code) value.
5815 */
5816static int
5817load_extension(UnpicklerObject *self, int nbytes)
5818{
5819 char *codebytes; /* the nbytes bytes after the opcode */
5820 long code; /* calc_binint returns long */
5821 PyObject *py_code; /* code as a Python int */
5822 PyObject *obj; /* the object to push */
5823 PyObject *pair; /* (module_name, class_name) */
5824 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005825 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005826
5827 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005828 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005829 return -1;
5830 code = calc_binint(codebytes, nbytes);
5831 if (code <= 0) { /* note that 0 is forbidden */
5832 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005833 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005834 return -1;
5835 }
5836
5837 /* Look for the code in the cache. */
5838 py_code = PyLong_FromLong(code);
5839 if (py_code == NULL)
5840 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005841 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005842 if (obj != NULL) {
5843 /* Bingo. */
5844 Py_DECREF(py_code);
5845 PDATA_APPEND(self->stack, obj, -1);
5846 return 0;
5847 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005848 if (PyErr_Occurred()) {
5849 Py_DECREF(py_code);
5850 return -1;
5851 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005852
5853 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005854 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005855 if (pair == NULL) {
5856 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005857 if (!PyErr_Occurred()) {
5858 PyErr_Format(PyExc_ValueError, "unregistered extension "
5859 "code %ld", code);
5860 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005861 return -1;
5862 }
5863 /* Since the extension registry is manipulable via Python code,
5864 * confirm that pair is really a 2-tuple of strings.
5865 */
5866 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5867 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5868 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5869 Py_DECREF(py_code);
5870 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5871 "isn't a 2-tuple of strings", code);
5872 return -1;
5873 }
5874 /* Load the object. */
5875 obj = find_class(self, module_name, class_name);
5876 if (obj == NULL) {
5877 Py_DECREF(py_code);
5878 return -1;
5879 }
5880 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005881 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005882 Py_DECREF(py_code);
5883 if (code < 0) {
5884 Py_DECREF(obj);
5885 return -1;
5886 }
5887 PDATA_PUSH(self->stack, obj, -1);
5888 return 0;
5889}
5890
5891static int
5892load_put(UnpicklerObject *self)
5893{
5894 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005895 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005896 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005897 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005898
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005899 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005900 return -1;
5901 if (len < 2)
5902 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005903 if (Py_SIZE(self->stack) <= self->stack->fence)
5904 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005905 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005906
5907 key = PyLong_FromString(s, NULL, 10);
5908 if (key == NULL)
5909 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005910 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005911 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005912 if (idx < 0) {
5913 if (!PyErr_Occurred())
5914 PyErr_SetString(PyExc_ValueError,
5915 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005916 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005917 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005918
5919 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005920}
5921
5922static int
5923load_binput(UnpicklerObject *self)
5924{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005925 PyObject *value;
5926 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005927 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005928
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005929 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005930 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005931
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005932 if (Py_SIZE(self->stack) <= self->stack->fence)
5933 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005934 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005935
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005936 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005937
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005938 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005939}
5940
5941static int
5942load_long_binput(UnpicklerObject *self)
5943{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005944 PyObject *value;
5945 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005946 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005947
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005948 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005949 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005950
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005951 if (Py_SIZE(self->stack) <= self->stack->fence)
5952 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005953 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005954
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005955 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005956 if (idx < 0) {
5957 PyErr_SetString(PyExc_ValueError,
5958 "negative LONG_BINPUT argument");
5959 return -1;
5960 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005961
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005962 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005963}
5964
5965static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005966load_memoize(UnpicklerObject *self)
5967{
5968 PyObject *value;
5969
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005970 if (Py_SIZE(self->stack) <= self->stack->fence)
5971 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005972 value = self->stack->data[Py_SIZE(self->stack) - 1];
5973
5974 return _Unpickler_MemoPut(self, self->memo_len, value);
5975}
5976
5977static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005978do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005979{
5980 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005981 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005982 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005983 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005984 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005985
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005986 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005987 if (x > len || x <= self->stack->fence)
5988 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005989 if (len == x) /* nothing to do */
5990 return 0;
5991
5992 list = self->stack->data[x - 1];
5993
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005994 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005995 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005996 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005997
5998 slice = Pdata_poplist(self->stack, x);
5999 if (!slice)
6000 return -1;
6001 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006002 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006003 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006004 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006005 }
6006 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006007 PyObject *extend_func;
6008 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006009
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006010 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6011 if (extend_func != NULL) {
6012 slice = Pdata_poplist(self->stack, x);
6013 if (!slice) {
6014 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006015 return -1;
6016 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006017 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006018 Py_DECREF(extend_func);
6019 if (result == NULL)
6020 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006021 Py_DECREF(result);
6022 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006023 else {
6024 PyObject *append_func;
6025 _Py_IDENTIFIER(append);
6026
6027 /* Even if the PEP 307 requires extend() and append() methods,
6028 fall back on append() if the object has no extend() method
6029 for backward compatibility. */
6030 PyErr_Clear();
6031 append_func = _PyObject_GetAttrId(list, &PyId_append);
6032 if (append_func == NULL)
6033 return -1;
6034 for (i = x; i < len; i++) {
6035 value = self->stack->data[i];
6036 result = _Pickle_FastCall(append_func, value);
6037 if (result == NULL) {
6038 Pdata_clear(self->stack, i + 1);
6039 Py_SIZE(self->stack) = x;
6040 Py_DECREF(append_func);
6041 return -1;
6042 }
6043 Py_DECREF(result);
6044 }
6045 Py_SIZE(self->stack) = x;
6046 Py_DECREF(append_func);
6047 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006048 }
6049
6050 return 0;
6051}
6052
6053static int
6054load_append(UnpicklerObject *self)
6055{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006056 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6057 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006058 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006059}
6060
6061static int
6062load_appends(UnpicklerObject *self)
6063{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006064 Py_ssize_t i = marker(self);
6065 if (i < 0)
6066 return -1;
6067 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006068}
6069
6070static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006071do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006072{
6073 PyObject *value, *key;
6074 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006075 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006076 int status = 0;
6077
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006078 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006079 if (x > len || x <= self->stack->fence)
6080 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006081 if (len == x) /* nothing to do */
6082 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006083 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006084 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006085 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006086 PyErr_SetString(st->UnpicklingError,
6087 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006088 return -1;
6089 }
6090
6091 /* Here, dict does not actually need to be a PyDict; it could be anything
6092 that supports the __setitem__ attribute. */
6093 dict = self->stack->data[x - 1];
6094
6095 for (i = x + 1; i < len; i += 2) {
6096 key = self->stack->data[i - 1];
6097 value = self->stack->data[i];
6098 if (PyObject_SetItem(dict, key, value) < 0) {
6099 status = -1;
6100 break;
6101 }
6102 }
6103
6104 Pdata_clear(self->stack, x);
6105 return status;
6106}
6107
6108static int
6109load_setitem(UnpicklerObject *self)
6110{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006111 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006112}
6113
6114static int
6115load_setitems(UnpicklerObject *self)
6116{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006117 Py_ssize_t i = marker(self);
6118 if (i < 0)
6119 return -1;
6120 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006121}
6122
6123static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006124load_additems(UnpicklerObject *self)
6125{
6126 PyObject *set;
6127 Py_ssize_t mark, len, i;
6128
6129 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006130 if (mark < 0)
6131 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006132 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006133 if (mark > len || mark <= self->stack->fence)
6134 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006135 if (len == mark) /* nothing to do */
6136 return 0;
6137
6138 set = self->stack->data[mark - 1];
6139
6140 if (PySet_Check(set)) {
6141 PyObject *items;
6142 int status;
6143
6144 items = Pdata_poptuple(self->stack, mark);
6145 if (items == NULL)
6146 return -1;
6147
6148 status = _PySet_Update(set, items);
6149 Py_DECREF(items);
6150 return status;
6151 }
6152 else {
6153 PyObject *add_func;
6154 _Py_IDENTIFIER(add);
6155
6156 add_func = _PyObject_GetAttrId(set, &PyId_add);
6157 if (add_func == NULL)
6158 return -1;
6159 for (i = mark; i < len; i++) {
6160 PyObject *result;
6161 PyObject *item;
6162
6163 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006164 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006165 if (result == NULL) {
6166 Pdata_clear(self->stack, i + 1);
6167 Py_SIZE(self->stack) = mark;
6168 return -1;
6169 }
6170 Py_DECREF(result);
6171 }
6172 Py_SIZE(self->stack) = mark;
6173 }
6174
6175 return 0;
6176}
6177
6178static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006179load_build(UnpicklerObject *self)
6180{
6181 PyObject *state, *inst, *slotstate;
6182 PyObject *setstate;
6183 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006184 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006185
6186 /* Stack is ... instance, state. We want to leave instance at
6187 * the stack top, possibly mutated via instance.__setstate__(state).
6188 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006189 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6190 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006191
6192 PDATA_POP(self->stack, state);
6193 if (state == NULL)
6194 return -1;
6195
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006196 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006197
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006198 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6199 Py_DECREF(state);
6200 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006201 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006202 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006203 PyObject *result;
6204
6205 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006206 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207 Py_DECREF(setstate);
6208 if (result == NULL)
6209 return -1;
6210 Py_DECREF(result);
6211 return 0;
6212 }
6213
6214 /* A default __setstate__. First see whether state embeds a
6215 * slot state dict too (a proto 2 addition).
6216 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006217 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006218 PyObject *tmp = state;
6219
6220 state = PyTuple_GET_ITEM(tmp, 0);
6221 slotstate = PyTuple_GET_ITEM(tmp, 1);
6222 Py_INCREF(state);
6223 Py_INCREF(slotstate);
6224 Py_DECREF(tmp);
6225 }
6226 else
6227 slotstate = NULL;
6228
6229 /* Set inst.__dict__ from the state dict (if any). */
6230 if (state != Py_None) {
6231 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006232 PyObject *d_key, *d_value;
6233 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006234 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006235
6236 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006237 PickleState *st = _Pickle_GetGlobalState();
6238 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006239 goto error;
6240 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006241 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006242 if (dict == NULL)
6243 goto error;
6244
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006245 i = 0;
6246 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6247 /* normally the keys for instance attributes are
6248 interned. we should try to do that here. */
6249 Py_INCREF(d_key);
6250 if (PyUnicode_CheckExact(d_key))
6251 PyUnicode_InternInPlace(&d_key);
6252 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6253 Py_DECREF(d_key);
6254 goto error;
6255 }
6256 Py_DECREF(d_key);
6257 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006258 Py_DECREF(dict);
6259 }
6260
6261 /* Also set instance attributes from the slotstate dict (if any). */
6262 if (slotstate != NULL) {
6263 PyObject *d_key, *d_value;
6264 Py_ssize_t i;
6265
6266 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006267 PickleState *st = _Pickle_GetGlobalState();
6268 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006269 "slot state is not a dictionary");
6270 goto error;
6271 }
6272 i = 0;
6273 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6274 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6275 goto error;
6276 }
6277 }
6278
6279 if (0) {
6280 error:
6281 status = -1;
6282 }
6283
6284 Py_DECREF(state);
6285 Py_XDECREF(slotstate);
6286 return status;
6287}
6288
6289static int
6290load_mark(UnpicklerObject *self)
6291{
6292
6293 /* Note that we split the (pickle.py) stack into two stacks, an
6294 * object stack and a mark stack. Here we push a mark onto the
6295 * mark stack.
6296 */
6297
6298 if ((self->num_marks + 1) >= self->marks_size) {
6299 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006300
6301 /* Use the size_t type to check for overflow. */
6302 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006303 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006304 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006305 PyErr_NoMemory();
6306 return -1;
6307 }
6308
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006309 Py_ssize_t *marks_old = self->marks;
6310 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006311 if (self->marks == NULL) {
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006312 PyMem_FREE(marks_old);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006313 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006314 PyErr_NoMemory();
6315 return -1;
6316 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006317 self->marks_size = (Py_ssize_t)alloc;
6318 }
6319
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006320 self->stack->mark_set = 1;
6321 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006322
6323 return 0;
6324}
6325
6326static int
6327load_reduce(UnpicklerObject *self)
6328{
6329 PyObject *callable = NULL;
6330 PyObject *argtup = NULL;
6331 PyObject *obj = NULL;
6332
6333 PDATA_POP(self->stack, argtup);
6334 if (argtup == NULL)
6335 return -1;
6336 PDATA_POP(self->stack, callable);
6337 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006338 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006339 Py_DECREF(callable);
6340 }
6341 Py_DECREF(argtup);
6342
6343 if (obj == NULL)
6344 return -1;
6345
6346 PDATA_PUSH(self->stack, obj, -1);
6347 return 0;
6348}
6349
6350/* Just raises an error if we don't know the protocol specified. PROTO
6351 * is the first opcode for protocols >= 2.
6352 */
6353static int
6354load_proto(UnpicklerObject *self)
6355{
6356 char *s;
6357 int i;
6358
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006359 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006360 return -1;
6361
6362 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006363 if (i <= HIGHEST_PROTOCOL) {
6364 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006365 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006366 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006367
6368 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6369 return -1;
6370}
6371
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006372static int
6373load_frame(UnpicklerObject *self)
6374{
6375 char *s;
6376 Py_ssize_t frame_len;
6377
6378 if (_Unpickler_Read(self, &s, 8) < 0)
6379 return -1;
6380
6381 frame_len = calc_binsize(s, 8);
6382 if (frame_len < 0) {
6383 PyErr_Format(PyExc_OverflowError,
6384 "FRAME length exceeds system's maximum of %zd bytes",
6385 PY_SSIZE_T_MAX);
6386 return -1;
6387 }
6388
6389 if (_Unpickler_Read(self, &s, frame_len) < 0)
6390 return -1;
6391
6392 /* Rewind to start of frame */
6393 self->next_read_idx -= frame_len;
6394 return 0;
6395}
6396
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006397static PyObject *
6398load(UnpicklerObject *self)
6399{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006400 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006401 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006402
6403 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006404 self->stack->mark_set = 0;
6405 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006406 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006407 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006408 Pdata_clear(self->stack, 0);
6409
6410 /* Convenient macros for the dispatch while-switch loop just below. */
6411#define OP(opcode, load_func) \
6412 case opcode: if (load_func(self) < 0) break; continue;
6413
6414#define OP_ARG(opcode, load_func, arg) \
6415 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6416
6417 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006418 if (_Unpickler_Read(self, &s, 1) < 0) {
6419 PickleState *st = _Pickle_GetGlobalState();
6420 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6421 PyErr_Format(PyExc_EOFError, "Ran out of input");
6422 }
6423 return NULL;
6424 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006425
6426 switch ((enum opcode)s[0]) {
6427 OP(NONE, load_none)
6428 OP(BININT, load_binint)
6429 OP(BININT1, load_binint1)
6430 OP(BININT2, load_binint2)
6431 OP(INT, load_int)
6432 OP(LONG, load_long)
6433 OP_ARG(LONG1, load_counted_long, 1)
6434 OP_ARG(LONG4, load_counted_long, 4)
6435 OP(FLOAT, load_float)
6436 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006437 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6438 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6439 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6440 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6441 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006442 OP(STRING, load_string)
6443 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006444 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6445 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6446 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006447 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6448 OP_ARG(TUPLE1, load_counted_tuple, 1)
6449 OP_ARG(TUPLE2, load_counted_tuple, 2)
6450 OP_ARG(TUPLE3, load_counted_tuple, 3)
6451 OP(TUPLE, load_tuple)
6452 OP(EMPTY_LIST, load_empty_list)
6453 OP(LIST, load_list)
6454 OP(EMPTY_DICT, load_empty_dict)
6455 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006456 OP(EMPTY_SET, load_empty_set)
6457 OP(ADDITEMS, load_additems)
6458 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006459 OP(OBJ, load_obj)
6460 OP(INST, load_inst)
6461 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006462 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006463 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006464 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006465 OP(APPEND, load_append)
6466 OP(APPENDS, load_appends)
6467 OP(BUILD, load_build)
6468 OP(DUP, load_dup)
6469 OP(BINGET, load_binget)
6470 OP(LONG_BINGET, load_long_binget)
6471 OP(GET, load_get)
6472 OP(MARK, load_mark)
6473 OP(BINPUT, load_binput)
6474 OP(LONG_BINPUT, load_long_binput)
6475 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006476 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006477 OP(POP, load_pop)
6478 OP(POP_MARK, load_pop_mark)
6479 OP(SETITEM, load_setitem)
6480 OP(SETITEMS, load_setitems)
6481 OP(PERSID, load_persid)
6482 OP(BINPERSID, load_binpersid)
6483 OP(REDUCE, load_reduce)
6484 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006485 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006486 OP_ARG(EXT1, load_extension, 1)
6487 OP_ARG(EXT2, load_extension, 2)
6488 OP_ARG(EXT4, load_extension, 4)
6489 OP_ARG(NEWTRUE, load_bool, Py_True)
6490 OP_ARG(NEWFALSE, load_bool, Py_False)
6491
6492 case STOP:
6493 break;
6494
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006495 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006496 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006497 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006498 unsigned char c = (unsigned char) *s;
6499 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6500 PyErr_Format(st->UnpicklingError,
6501 "invalid load key, '%c'.", c);
6502 }
6503 else {
6504 PyErr_Format(st->UnpicklingError,
6505 "invalid load key, '\\x%02x'.", c);
6506 }
6507 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006508 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006509 }
6510
6511 break; /* and we are done! */
6512 }
6513
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006514 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006515 return NULL;
6516 }
6517
Victor Stinner2ae57e32013-10-31 13:39:23 +01006518 if (_Unpickler_SkipConsumed(self) < 0)
6519 return NULL;
6520
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006521 PDATA_POP(self->stack, value);
6522 return value;
6523}
6524
Larry Hastings61272b72014-01-07 12:41:53 -08006525/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006526
6527_pickle.Unpickler.load
6528
6529Load a pickle.
6530
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006531Read a pickled object representation from the open file object given
6532in the constructor, and return the reconstituted object hierarchy
6533specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006534[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006535
Larry Hastings3cceb382014-01-04 11:09:09 -08006536static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006537_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006538/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006540 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006541
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006542 /* Check whether the Unpickler was initialized correctly. This prevents
6543 segfaulting if a subclass overridden __init__ with a function that does
6544 not call Unpickler.__init__(). Here, we simply ensure that self->read
6545 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006546 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006547 PickleState *st = _Pickle_GetGlobalState();
6548 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006549 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006550 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006551 return NULL;
6552 }
6553
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006554 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006555}
6556
6557/* The name of find_class() is misleading. In newer pickle protocols, this
6558 function is used for loading any global (i.e., functions), not just
6559 classes. The name is kept only for backward compatibility. */
6560
Larry Hastings61272b72014-01-07 12:41:53 -08006561/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006562
6563_pickle.Unpickler.find_class
6564
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006565 module_name: object
6566 global_name: object
6567 /
6568
6569Return an object from a specified module.
6570
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006571If necessary, the module will be imported. Subclasses may override
6572this method (e.g. to restrict unpickling of arbitrary classes and
6573functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006574
6575This method is called whenever a class or a function object is
6576needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006577[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006578
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006579static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006580_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6581 PyObject *module_name,
6582 PyObject *global_name)
6583/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006584{
6585 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006586 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006587
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006588 /* Try to map the old names used in Python 2.x to the new ones used in
6589 Python 3.x. We do this only with old pickle protocols and when the
6590 user has not disabled the feature. */
6591 if (self->proto < 3 && self->fix_imports) {
6592 PyObject *key;
6593 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006594 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006595
6596 /* Check if the global (i.e., a function or a class) was renamed
6597 or moved to another module. */
6598 key = PyTuple_Pack(2, module_name, global_name);
6599 if (key == NULL)
6600 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006601 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006602 Py_DECREF(key);
6603 if (item) {
6604 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6605 PyErr_Format(PyExc_RuntimeError,
6606 "_compat_pickle.NAME_MAPPING values should be "
6607 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6608 return NULL;
6609 }
6610 module_name = PyTuple_GET_ITEM(item, 0);
6611 global_name = PyTuple_GET_ITEM(item, 1);
6612 if (!PyUnicode_Check(module_name) ||
6613 !PyUnicode_Check(global_name)) {
6614 PyErr_Format(PyExc_RuntimeError,
6615 "_compat_pickle.NAME_MAPPING values should be "
6616 "pairs of str, not (%.200s, %.200s)",
6617 Py_TYPE(module_name)->tp_name,
6618 Py_TYPE(global_name)->tp_name);
6619 return NULL;
6620 }
6621 }
6622 else if (PyErr_Occurred()) {
6623 return NULL;
6624 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006625 else {
6626 /* Check if the module was renamed. */
6627 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6628 if (item) {
6629 if (!PyUnicode_Check(item)) {
6630 PyErr_Format(PyExc_RuntimeError,
6631 "_compat_pickle.IMPORT_MAPPING values should be "
6632 "strings, not %.200s", Py_TYPE(item)->tp_name);
6633 return NULL;
6634 }
6635 module_name = item;
6636 }
6637 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006638 return NULL;
6639 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006640 }
6641 }
6642
Eric Snow3f9eee62017-09-15 16:35:20 -06006643 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006644 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006645 if (PyErr_Occurred())
6646 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006647 module = PyImport_Import(module_name);
6648 if (module == NULL)
6649 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006651 global = getattribute(module, global_name, self->proto >= 4);
6652 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006653 return global;
6654}
6655
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006656/*[clinic input]
6657
6658_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6659
6660Returns size in memory, in bytes.
6661[clinic start generated code]*/
6662
6663static Py_ssize_t
6664_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6665/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6666{
6667 Py_ssize_t res;
6668
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006669 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006670 if (self->memo != NULL)
6671 res += self->memo_size * sizeof(PyObject *);
6672 if (self->marks != NULL)
6673 res += self->marks_size * sizeof(Py_ssize_t);
6674 if (self->input_line != NULL)
6675 res += strlen(self->input_line) + 1;
6676 if (self->encoding != NULL)
6677 res += strlen(self->encoding) + 1;
6678 if (self->errors != NULL)
6679 res += strlen(self->errors) + 1;
6680 return res;
6681}
6682
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006683static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006684 _PICKLE_UNPICKLER_LOAD_METHODDEF
6685 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006686 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006687 {NULL, NULL} /* sentinel */
6688};
6689
6690static void
6691Unpickler_dealloc(UnpicklerObject *self)
6692{
6693 PyObject_GC_UnTrack((PyObject *)self);
6694 Py_XDECREF(self->readline);
6695 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006696 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006697 Py_XDECREF(self->stack);
6698 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006699 if (self->buffer.buf != NULL) {
6700 PyBuffer_Release(&self->buffer);
6701 self->buffer.buf = NULL;
6702 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006703
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006704 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006705 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006706 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006707 PyMem_Free(self->encoding);
6708 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006709
6710 Py_TYPE(self)->tp_free((PyObject *)self);
6711}
6712
6713static int
6714Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6715{
6716 Py_VISIT(self->readline);
6717 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006718 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006719 Py_VISIT(self->stack);
6720 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006721 return 0;
6722}
6723
6724static int
6725Unpickler_clear(UnpicklerObject *self)
6726{
6727 Py_CLEAR(self->readline);
6728 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006729 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006730 Py_CLEAR(self->stack);
6731 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006732 if (self->buffer.buf != NULL) {
6733 PyBuffer_Release(&self->buffer);
6734 self->buffer.buf = NULL;
6735 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006736
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006737 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006738 PyMem_Free(self->marks);
6739 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006740 PyMem_Free(self->input_line);
6741 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006742 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006743 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006744 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006745 self->errors = NULL;
6746
6747 return 0;
6748}
6749
Larry Hastings61272b72014-01-07 12:41:53 -08006750/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006751
6752_pickle.Unpickler.__init__
6753
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006754 file: object
6755 *
6756 fix_imports: bool = True
6757 encoding: str = 'ASCII'
6758 errors: str = 'strict'
6759
6760This takes a binary file for reading a pickle data stream.
6761
6762The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006763protocol argument is needed. Bytes past the pickled object's
6764representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006765
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006766The argument *file* must have two methods, a read() method that takes
6767an integer argument, and a readline() method that requires no
6768arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006769binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006770other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006771
6772Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006773which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006774generated by Python 2. If *fix_imports* is True, pickle will try to
6775map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006776*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006777instances pickled by Python 2; these default to 'ASCII' and 'strict',
6778respectively. The *encoding* can be 'bytes' to read these 8-bit
6779string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006780[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006781
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006782static int
Larry Hastings89964c42015-04-14 18:07:59 -04006783_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6784 int fix_imports, const char *encoding,
6785 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006786/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006787{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006788 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006789
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006790 /* In case of multiple __init__() calls, clear previous content. */
6791 if (self->read != NULL)
6792 (void)Unpickler_clear(self);
6793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006794 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006795 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006796
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006797 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006798 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006799
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006800 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006801
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006802 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6803 &self->pers_func, &self->pers_func_self) < 0)
6804 {
6805 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006806 }
6807
6808 self->stack = (Pdata *)Pdata_New();
6809 if (self->stack == NULL)
Miss Islington (bot)758ad542018-09-28 23:01:48 -07006810 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006811
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006812 self->memo_size = 32;
6813 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006814 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006815 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006816
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006817 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006818
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006819 return 0;
6820}
6821
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006822
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006823/* Define a proxy object for the Unpickler's internal memo object. This is to
6824 * avoid breaking code like:
6825 * unpickler.memo.clear()
6826 * and
6827 * unpickler.memo = saved_memo
6828 * Is this a good idea? Not really, but we don't want to break code that uses
6829 * it. Note that we don't implement the entire mapping API here. This is
6830 * intentional, as these should be treated as black-box implementation details.
6831 *
6832 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006833 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006834 */
6835
Larry Hastings61272b72014-01-07 12:41:53 -08006836/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006837_pickle.UnpicklerMemoProxy.clear
6838
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006839Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006840[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006841
Larry Hastings3cceb382014-01-04 11:09:09 -08006842static PyObject *
6843_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006844/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006845{
6846 _Unpickler_MemoCleanup(self->unpickler);
6847 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6848 if (self->unpickler->memo == NULL)
6849 return NULL;
6850 Py_RETURN_NONE;
6851}
6852
Larry Hastings61272b72014-01-07 12:41:53 -08006853/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006854_pickle.UnpicklerMemoProxy.copy
6855
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006856Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006857[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006858
Larry Hastings3cceb382014-01-04 11:09:09 -08006859static PyObject *
6860_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006861/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006862{
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07006863 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006864 PyObject *new_memo = PyDict_New();
6865 if (new_memo == NULL)
6866 return NULL;
6867
6868 for (i = 0; i < self->unpickler->memo_size; i++) {
6869 int status;
6870 PyObject *key, *value;
6871
6872 value = self->unpickler->memo[i];
6873 if (value == NULL)
6874 continue;
6875
6876 key = PyLong_FromSsize_t(i);
6877 if (key == NULL)
6878 goto error;
6879 status = PyDict_SetItem(new_memo, key, value);
6880 Py_DECREF(key);
6881 if (status < 0)
6882 goto error;
6883 }
6884 return new_memo;
6885
6886error:
6887 Py_DECREF(new_memo);
6888 return NULL;
6889}
6890
Larry Hastings61272b72014-01-07 12:41:53 -08006891/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006892_pickle.UnpicklerMemoProxy.__reduce__
6893
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006894Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006895[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006896
Larry Hastings3cceb382014-01-04 11:09:09 -08006897static PyObject *
6898_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006899/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006900{
6901 PyObject *reduce_value;
6902 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006903 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006904 if (contents == NULL)
6905 return NULL;
6906
6907 reduce_value = PyTuple_New(2);
6908 if (reduce_value == NULL) {
6909 Py_DECREF(contents);
6910 return NULL;
6911 }
6912 constructor_args = PyTuple_New(1);
6913 if (constructor_args == NULL) {
6914 Py_DECREF(contents);
6915 Py_DECREF(reduce_value);
6916 return NULL;
6917 }
6918 PyTuple_SET_ITEM(constructor_args, 0, contents);
6919 Py_INCREF((PyObject *)&PyDict_Type);
6920 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6921 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6922 return reduce_value;
6923}
6924
6925static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006926 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6927 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6928 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006929 {NULL, NULL} /* sentinel */
6930};
6931
6932static void
6933UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6934{
6935 PyObject_GC_UnTrack(self);
6936 Py_XDECREF(self->unpickler);
6937 PyObject_GC_Del((PyObject *)self);
6938}
6939
6940static int
6941UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6942 visitproc visit, void *arg)
6943{
6944 Py_VISIT(self->unpickler);
6945 return 0;
6946}
6947
6948static int
6949UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6950{
6951 Py_CLEAR(self->unpickler);
6952 return 0;
6953}
6954
6955static PyTypeObject UnpicklerMemoProxyType = {
6956 PyVarObject_HEAD_INIT(NULL, 0)
6957 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6958 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6959 0,
6960 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6961 0, /* tp_print */
6962 0, /* tp_getattr */
6963 0, /* tp_setattr */
6964 0, /* tp_compare */
6965 0, /* tp_repr */
6966 0, /* tp_as_number */
6967 0, /* tp_as_sequence */
6968 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006969 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006970 0, /* tp_call */
6971 0, /* tp_str */
6972 PyObject_GenericGetAttr, /* tp_getattro */
6973 PyObject_GenericSetAttr, /* tp_setattro */
6974 0, /* tp_as_buffer */
6975 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6976 0, /* tp_doc */
6977 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6978 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6979 0, /* tp_richcompare */
6980 0, /* tp_weaklistoffset */
6981 0, /* tp_iter */
6982 0, /* tp_iternext */
6983 unpicklerproxy_methods, /* tp_methods */
6984};
6985
6986static PyObject *
6987UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6988{
6989 UnpicklerMemoProxyObject *self;
6990
6991 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6992 &UnpicklerMemoProxyType);
6993 if (self == NULL)
6994 return NULL;
6995 Py_INCREF(unpickler);
6996 self->unpickler = unpickler;
6997 PyObject_GC_Track(self);
6998 return (PyObject *)self;
6999}
7000
7001/*****************************************************************************/
7002
7003
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007004static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007005Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007006{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007007 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007008}
7009
7010static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007011Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007012{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007013 PyObject **new_memo;
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007014 size_t new_memo_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007015
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007016 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007017 PyErr_SetString(PyExc_TypeError,
7018 "attribute deletion is not supported");
7019 return -1;
7020 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007021
7022 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7023 UnpicklerObject *unpickler =
7024 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7025
7026 new_memo_size = unpickler->memo_size;
7027 new_memo = _Unpickler_NewMemo(new_memo_size);
7028 if (new_memo == NULL)
7029 return -1;
7030
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007031 for (size_t i = 0; i < new_memo_size; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007032 Py_XINCREF(unpickler->memo[i]);
7033 new_memo[i] = unpickler->memo[i];
7034 }
7035 }
7036 else if (PyDict_Check(obj)) {
7037 Py_ssize_t i = 0;
7038 PyObject *key, *value;
7039
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007040 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007041 new_memo = _Unpickler_NewMemo(new_memo_size);
7042 if (new_memo == NULL)
7043 return -1;
7044
7045 while (PyDict_Next(obj, &i, &key, &value)) {
7046 Py_ssize_t idx;
7047 if (!PyLong_Check(key)) {
7048 PyErr_SetString(PyExc_TypeError,
7049 "memo key must be integers");
7050 goto error;
7051 }
7052 idx = PyLong_AsSsize_t(key);
7053 if (idx == -1 && PyErr_Occurred())
7054 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007055 if (idx < 0) {
7056 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007057 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007058 goto error;
7059 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007060 if (_Unpickler_MemoPut(self, idx, value) < 0)
7061 goto error;
7062 }
7063 }
7064 else {
7065 PyErr_Format(PyExc_TypeError,
Miss Islington (bot)7beb8c52018-11-05 06:52:58 -08007066 "'memo' attribute must be an UnpicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007067 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007068 return -1;
7069 }
7070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007071 _Unpickler_MemoCleanup(self);
7072 self->memo_size = new_memo_size;
7073 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007074
7075 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007076
7077 error:
7078 if (new_memo_size) {
Miss Islington (bot)ef4306b2018-09-20 18:52:36 -07007079 for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007080 Py_XDECREF(new_memo[i]);
7081 }
7082 PyMem_FREE(new_memo);
7083 }
7084 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007085}
7086
7087static PyObject *
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007088Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007089{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007090 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007091 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007092 return NULL;
7093 }
7094 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007095}
7096
7097static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08007098Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007099{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007100 if (value == NULL) {
7101 PyErr_SetString(PyExc_TypeError,
7102 "attribute deletion is not supported");
7103 return -1;
7104 }
7105 if (!PyCallable_Check(value)) {
7106 PyErr_SetString(PyExc_TypeError,
7107 "persistent_load must be a callable taking "
7108 "one argument");
7109 return -1;
7110 }
7111
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007112 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007113 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007114 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007115
7116 return 0;
7117}
7118
7119static PyGetSetDef Unpickler_getsets[] = {
7120 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7121 {"persistent_load", (getter)Unpickler_get_persload,
7122 (setter)Unpickler_set_persload},
7123 {NULL}
7124};
7125
7126static PyTypeObject Unpickler_Type = {
7127 PyVarObject_HEAD_INIT(NULL, 0)
7128 "_pickle.Unpickler", /*tp_name*/
7129 sizeof(UnpicklerObject), /*tp_basicsize*/
7130 0, /*tp_itemsize*/
7131 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7132 0, /*tp_print*/
7133 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007134 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007135 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007136 0, /*tp_repr*/
7137 0, /*tp_as_number*/
7138 0, /*tp_as_sequence*/
7139 0, /*tp_as_mapping*/
7140 0, /*tp_hash*/
7141 0, /*tp_call*/
7142 0, /*tp_str*/
7143 0, /*tp_getattro*/
7144 0, /*tp_setattro*/
7145 0, /*tp_as_buffer*/
7146 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007147 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007148 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7149 (inquiry)Unpickler_clear, /*tp_clear*/
7150 0, /*tp_richcompare*/
7151 0, /*tp_weaklistoffset*/
7152 0, /*tp_iter*/
7153 0, /*tp_iternext*/
7154 Unpickler_methods, /*tp_methods*/
7155 0, /*tp_members*/
7156 Unpickler_getsets, /*tp_getset*/
7157 0, /*tp_base*/
7158 0, /*tp_dict*/
7159 0, /*tp_descr_get*/
7160 0, /*tp_descr_set*/
7161 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007162 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007163 PyType_GenericAlloc, /*tp_alloc*/
7164 PyType_GenericNew, /*tp_new*/
7165 PyObject_GC_Del, /*tp_free*/
7166 0, /*tp_is_gc*/
7167};
7168
Larry Hastings61272b72014-01-07 12:41:53 -08007169/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007170
7171_pickle.dump
7172
7173 obj: object
7174 file: object
7175 protocol: object = NULL
7176 *
7177 fix_imports: bool = True
7178
7179Write a pickled representation of obj to the open file object file.
7180
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007181This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7182be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007183
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007184The optional *protocol* argument tells the pickler to use the given
7185protocol supported protocols are 0, 1, 2, 3 and 4. The default
7186protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007187
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007188Specifying a negative protocol version selects the highest protocol
7189version supported. The higher the protocol used, the more recent the
7190version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007191
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007192The *file* argument must have a write() method that accepts a single
7193bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007194writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007195this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007196
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007197If *fix_imports* is True and protocol is less than 3, pickle will try
7198to map the new Python 3 names to the old module names used in Python
71992, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007200[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007201
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007202static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007203_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007204 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007205/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007206{
7207 PicklerObject *pickler = _Pickler_New();
7208
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007209 if (pickler == NULL)
7210 return NULL;
7211
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007212 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007213 goto error;
7214
7215 if (_Pickler_SetOutputStream(pickler, file) < 0)
7216 goto error;
7217
7218 if (dump(pickler, obj) < 0)
7219 goto error;
7220
7221 if (_Pickler_FlushToFile(pickler) < 0)
7222 goto error;
7223
7224 Py_DECREF(pickler);
7225 Py_RETURN_NONE;
7226
7227 error:
7228 Py_XDECREF(pickler);
7229 return NULL;
7230}
7231
Larry Hastings61272b72014-01-07 12:41:53 -08007232/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007233
7234_pickle.dumps
7235
7236 obj: object
7237 protocol: object = NULL
7238 *
7239 fix_imports: bool = True
7240
7241Return the pickled representation of the object as a bytes object.
7242
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007243The optional *protocol* argument tells the pickler to use the given
7244protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7245protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007246
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007247Specifying a negative protocol version selects the highest protocol
7248version supported. The higher the protocol used, the more recent the
7249version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007250
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007251If *fix_imports* is True and *protocol* is less than 3, pickle will
7252try to map the new Python 3 names to the old module names used in
7253Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007254[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007255
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007256static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007257_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007258 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007259/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007260{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007261 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007262 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007263
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007264 if (pickler == NULL)
7265 return NULL;
7266
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007267 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007268 goto error;
7269
7270 if (dump(pickler, obj) < 0)
7271 goto error;
7272
7273 result = _Pickler_GetString(pickler);
7274 Py_DECREF(pickler);
7275 return result;
7276
7277 error:
7278 Py_XDECREF(pickler);
7279 return NULL;
7280}
7281
Larry Hastings61272b72014-01-07 12:41:53 -08007282/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007283
7284_pickle.load
7285
7286 file: object
7287 *
7288 fix_imports: bool = True
7289 encoding: str = 'ASCII'
7290 errors: str = 'strict'
7291
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007292Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007293
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007294This is equivalent to ``Unpickler(file).load()``, but may be more
7295efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007296
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007297The protocol version of the pickle is detected automatically, so no
7298protocol argument is needed. Bytes past the pickled object's
7299representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007300
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007301The argument *file* must have two methods, a read() method that takes
7302an integer argument, and a readline() method that requires no
7303arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007304binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007305other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007306
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007307Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007308which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007309generated by Python 2. If *fix_imports* is True, pickle will try to
7310map the old Python 2 names to the new names used in Python 3. The
7311*encoding* and *errors* tell pickle how to decode 8-bit string
7312instances pickled by Python 2; these default to 'ASCII' and 'strict',
7313respectively. The *encoding* can be 'bytes' to read these 8-bit
7314string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007315[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007316
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007317static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007318_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007319 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007320/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007321{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007322 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007323 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007324
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007325 if (unpickler == NULL)
7326 return NULL;
7327
7328 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7329 goto error;
7330
7331 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7332 goto error;
7333
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007334 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007335
7336 result = load(unpickler);
7337 Py_DECREF(unpickler);
7338 return result;
7339
7340 error:
7341 Py_XDECREF(unpickler);
7342 return NULL;
7343}
7344
Larry Hastings61272b72014-01-07 12:41:53 -08007345/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007346
7347_pickle.loads
7348
7349 data: object
7350 *
7351 fix_imports: bool = True
7352 encoding: str = 'ASCII'
7353 errors: str = 'strict'
7354
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007355Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007356
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007357The protocol version of the pickle is detected automatically, so no
7358protocol argument is needed. Bytes past the pickled object's
7359representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007360
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007361Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007362which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007363generated by Python 2. If *fix_imports* is True, pickle will try to
7364map the old Python 2 names to the new names used in Python 3. The
7365*encoding* and *errors* tell pickle how to decode 8-bit string
7366instances pickled by Python 2; these default to 'ASCII' and 'strict',
7367respectively. The *encoding* can be 'bytes' to read these 8-bit
7368string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007369[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007370
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007371static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007372_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007373 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007374/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007375{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007376 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007377 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007378
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007379 if (unpickler == NULL)
7380 return NULL;
7381
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007382 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007383 goto error;
7384
7385 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7386 goto error;
7387
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007388 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007389
7390 result = load(unpickler);
7391 Py_DECREF(unpickler);
7392 return result;
7393
7394 error:
7395 Py_XDECREF(unpickler);
7396 return NULL;
7397}
7398
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007399static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007400 _PICKLE_DUMP_METHODDEF
7401 _PICKLE_DUMPS_METHODDEF
7402 _PICKLE_LOAD_METHODDEF
7403 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007404 {NULL, NULL} /* sentinel */
7405};
7406
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007407static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007408pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007409{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007410 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007411 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007412}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007413
Stefan Krahf483b0f2013-12-14 13:43:10 +01007414static void
7415pickle_free(PyObject *m)
7416{
7417 _Pickle_ClearState(_Pickle_GetState(m));
7418}
7419
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007420static int
7421pickle_traverse(PyObject *m, visitproc visit, void *arg)
7422{
7423 PickleState *st = _Pickle_GetState(m);
7424 Py_VISIT(st->PickleError);
7425 Py_VISIT(st->PicklingError);
7426 Py_VISIT(st->UnpicklingError);
7427 Py_VISIT(st->dispatch_table);
7428 Py_VISIT(st->extension_registry);
7429 Py_VISIT(st->extension_cache);
7430 Py_VISIT(st->inverted_registry);
7431 Py_VISIT(st->name_mapping_2to3);
7432 Py_VISIT(st->import_mapping_2to3);
7433 Py_VISIT(st->name_mapping_3to2);
7434 Py_VISIT(st->import_mapping_3to2);
7435 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007436 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007437 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007438}
7439
7440static struct PyModuleDef _picklemodule = {
7441 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007442 "_pickle", /* m_name */
7443 pickle_module_doc, /* m_doc */
7444 sizeof(PickleState), /* m_size */
7445 pickle_methods, /* m_methods */
7446 NULL, /* m_reload */
7447 pickle_traverse, /* m_traverse */
7448 pickle_clear, /* m_clear */
7449 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007450};
7451
7452PyMODINIT_FUNC
7453PyInit__pickle(void)
7454{
7455 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007456 PickleState *st;
7457
7458 m = PyState_FindModule(&_picklemodule);
7459 if (m) {
7460 Py_INCREF(m);
7461 return m;
7462 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007463
7464 if (PyType_Ready(&Unpickler_Type) < 0)
7465 return NULL;
7466 if (PyType_Ready(&Pickler_Type) < 0)
7467 return NULL;
7468 if (PyType_Ready(&Pdata_Type) < 0)
7469 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007470 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7471 return NULL;
7472 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7473 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007474
7475 /* Create the module and add the functions. */
7476 m = PyModule_Create(&_picklemodule);
7477 if (m == NULL)
7478 return NULL;
7479
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007480 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007481 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7482 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007483 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007484 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7485 return NULL;
7486
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007487 st = _Pickle_GetState(m);
7488
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007489 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007490 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7491 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007492 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007493 st->PicklingError = \
7494 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7495 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007496 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007497 st->UnpicklingError = \
7498 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7499 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007500 return NULL;
7501
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007502 Py_INCREF(st->PickleError);
7503 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007504 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007505 Py_INCREF(st->PicklingError);
7506 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007507 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007508 Py_INCREF(st->UnpicklingError);
7509 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007510 return NULL;
7511
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007512 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007513 return NULL;
7514
7515 return m;
7516}