blob: 6a684f25fefd386e6e60dcb2faeeac2979c9d95e [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 {
603 Py_ssize_t mt_mask;
604 Py_ssize_t mt_used;
605 Py_ssize_t mt_allocated;
606 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;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100651 Py_ssize_t memo_size; /* Capacity of the memo array */
652 Py_ssize_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{
738 Py_ssize_t i;
739 PyMemoTable *new = PyMemoTable_New();
740 if (new == NULL)
741 return NULL;
742
743 new->mt_used = self->mt_used;
744 new->mt_allocated = self->mt_allocated;
745 new->mt_mask = self->mt_mask;
746 /* The table we get from _New() is probably smaller than we wanted.
747 Free it and allocate one that's the right size. */
748 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500749 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000750 if (new->mt_table == NULL) {
751 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200752 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000753 return NULL;
754 }
755 for (i = 0; i < self->mt_allocated; i++) {
756 Py_XINCREF(self->mt_table[i].me_key);
757 }
758 memcpy(new->mt_table, self->mt_table,
759 sizeof(PyMemoEntry) * self->mt_allocated);
760
761 return new;
762}
763
764static Py_ssize_t
765PyMemoTable_Size(PyMemoTable *self)
766{
767 return self->mt_used;
768}
769
770static int
771PyMemoTable_Clear(PyMemoTable *self)
772{
773 Py_ssize_t i = self->mt_allocated;
774
775 while (--i >= 0) {
776 Py_XDECREF(self->mt_table[i].me_key);
777 }
778 self->mt_used = 0;
779 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
780 return 0;
781}
782
783static void
784PyMemoTable_Del(PyMemoTable *self)
785{
786 if (self == NULL)
787 return;
788 PyMemoTable_Clear(self);
789
790 PyMem_FREE(self->mt_table);
791 PyMem_FREE(self);
792}
793
794/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
795 can be considerably simpler than dictobject.c's lookdict(). */
796static PyMemoEntry *
797_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
798{
799 size_t i;
800 size_t perturb;
801 size_t mask = (size_t)self->mt_mask;
802 PyMemoEntry *table = self->mt_table;
803 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000804 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000805
806 i = hash & mask;
807 entry = &table[i];
808 if (entry->me_key == NULL || entry->me_key == key)
809 return entry;
810
811 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
812 i = (i << 2) + i + perturb + 1;
813 entry = &table[i & mask];
814 if (entry->me_key == NULL || entry->me_key == key)
815 return entry;
816 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700817 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000818}
819
820/* Returns -1 on failure, 0 on success. */
821static int
822_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
823{
824 PyMemoEntry *oldtable = NULL;
825 PyMemoEntry *oldentry, *newentry;
826 Py_ssize_t new_size = MT_MINSIZE;
827 Py_ssize_t to_process;
828
829 assert(min_size > 0);
830
831 /* Find the smallest valid table size >= min_size. */
832 while (new_size < min_size && new_size > 0)
833 new_size <<= 1;
834 if (new_size <= 0) {
835 PyErr_NoMemory();
836 return -1;
837 }
838 /* new_size needs to be a power of two. */
839 assert((new_size & (new_size - 1)) == 0);
840
841 /* Allocate new table. */
842 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500843 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000844 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200845 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000846 PyErr_NoMemory();
847 return -1;
848 }
849 self->mt_allocated = new_size;
850 self->mt_mask = new_size - 1;
851 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
852
853 /* Copy entries from the old table. */
854 to_process = self->mt_used;
855 for (oldentry = oldtable; to_process > 0; oldentry++) {
856 if (oldentry->me_key != NULL) {
857 to_process--;
858 /* newentry is a pointer to a chunk of the new
859 mt_table, so we're setting the key:value pair
860 in-place. */
861 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
862 newentry->me_key = oldentry->me_key;
863 newentry->me_value = oldentry->me_value;
864 }
865 }
866
867 /* Deallocate the old table. */
868 PyMem_FREE(oldtable);
869 return 0;
870}
871
872/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200873static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000874PyMemoTable_Get(PyMemoTable *self, PyObject *key)
875{
876 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
877 if (entry->me_key == NULL)
878 return NULL;
879 return &entry->me_value;
880}
881
882/* Returns -1 on failure, 0 on success. */
883static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200884PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000885{
886 PyMemoEntry *entry;
887
888 assert(key != NULL);
889
890 entry = _PyMemoTable_Lookup(self, key);
891 if (entry->me_key != NULL) {
892 entry->me_value = value;
893 return 0;
894 }
895 Py_INCREF(key);
896 entry->me_key = key;
897 entry->me_value = value;
898 self->mt_used++;
899
900 /* If we added a key, we can safely resize. Otherwise just return!
901 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
902 *
903 * Quadrupling the size improves average table sparseness
904 * (reducing collisions) at the cost of some memory. It also halves
905 * the number of expensive resize operations in a growing memo table.
906 *
907 * Very large memo tables (over 50K items) use doubling instead.
908 * This may help applications with severe memory constraints.
909 */
910 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
911 return 0;
912 return _PyMemoTable_ResizeTable(self,
913 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
914}
915
916#undef MT_MINSIZE
917#undef PERTURB_SHIFT
918
919/*************************************************************************/
920
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000921
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000922static int
923_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000924{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300925 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200926 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000927 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000928 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000929 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100930 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000931 return 0;
932}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000933
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100934static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100935_write_size64(char *out, size_t value)
936{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200937 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800938
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200939 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800940
941 for (i = 0; i < sizeof(size_t); i++) {
942 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
943 }
944 for (i = sizeof(size_t); i < 8; i++) {
945 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800946 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100947}
948
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100949static int
950_Pickler_CommitFrame(PicklerObject *self)
951{
952 size_t frame_len;
953 char *qdata;
954
955 if (!self->framing || self->frame_start == -1)
956 return 0;
957 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
958 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200959 if (frame_len >= FRAME_SIZE_MIN) {
960 qdata[0] = FRAME;
961 _write_size64(qdata + 1, frame_len);
962 }
963 else {
964 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
965 self->output_len -= FRAME_HEADER_SIZE;
966 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100967 self->frame_start = -1;
968 return 0;
969}
970
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000971static PyObject *
972_Pickler_GetString(PicklerObject *self)
973{
974 PyObject *output_buffer = self->output_buffer;
975
976 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100977
978 if (_Pickler_CommitFrame(self))
979 return NULL;
980
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000981 self->output_buffer = NULL;
982 /* Resize down to exact size */
983 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
984 return NULL;
985 return output_buffer;
986}
987
988static int
989_Pickler_FlushToFile(PicklerObject *self)
990{
991 PyObject *output, *result;
992
993 assert(self->write != NULL);
994
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100995 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000996 output = _Pickler_GetString(self);
997 if (output == NULL)
998 return -1;
999
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001000 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001001 Py_XDECREF(result);
1002 return (result == NULL) ? -1 : 0;
1003}
1004
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001005static int
1006_Pickler_OpcodeBoundary(PicklerObject *self)
1007{
1008 Py_ssize_t frame_len;
1009
1010 if (!self->framing || self->frame_start == -1) {
1011 return 0;
1012 }
1013 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1014 if (frame_len >= FRAME_SIZE_TARGET) {
1015 if(_Pickler_CommitFrame(self)) {
1016 return -1;
1017 }
Miss Islington (bot)e86db342018-02-03 17:41:43 -08001018 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001019 * file and reuse the pickler buffer for the next frame so as
1020 * to limit memory usage when dumping large complex objects to
1021 * a file.
1022 *
1023 * self->write is NULL when called via dumps.
1024 */
1025 if (self->write != NULL) {
1026 if (_Pickler_FlushToFile(self) < 0) {
1027 return -1;
1028 }
1029 if (_Pickler_ClearBuffer(self) < 0) {
1030 return -1;
1031 }
1032 }
1033 }
1034 return 0;
1035}
1036
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001037static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001038_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001039{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001040 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001042 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001043
1044 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001045 need_new_frame = (self->framing && self->frame_start == -1);
1046
1047 if (need_new_frame)
1048 n = data_len + FRAME_HEADER_SIZE;
1049 else
1050 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001051
1052 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001053 if (required > self->max_output_len) {
1054 /* Make place in buffer for the pickle chunk */
1055 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1056 PyErr_NoMemory();
1057 return -1;
1058 }
1059 self->max_output_len = (self->output_len + n) / 2 * 3;
1060 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1061 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001062 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001063 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001064 if (need_new_frame) {
1065 /* Setup new frame */
1066 Py_ssize_t frame_start = self->output_len;
1067 self->frame_start = frame_start;
1068 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1069 /* Write an invalid value, for debugging */
1070 buffer[frame_start + i] = 0xFE;
1071 }
1072 self->output_len += FRAME_HEADER_SIZE;
1073 }
1074 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001075 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001076 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001077 buffer[self->output_len + i] = s[i];
1078 }
1079 }
1080 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001081 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001082 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001083 self->output_len += data_len;
1084 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001085}
1086
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001087static PicklerObject *
1088_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001089{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001090 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001091
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001092 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1093 if (self == NULL)
1094 return NULL;
1095
1096 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001097 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001098 self->write = NULL;
1099 self->proto = 0;
1100 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001101 self->framing = 0;
1102 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001103 self->fast = 0;
1104 self->fast_nesting = 0;
1105 self->fix_imports = 0;
1106 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001107 self->max_output_len = WRITE_BUF_SIZE;
1108 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001109
1110 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001111 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1112 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001113
1114 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001115 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001116 return NULL;
1117 }
1118 return self;
1119}
1120
1121static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001122_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001123{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001124 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001125
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001126 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001127 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001128 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001129 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001130 proto = PyLong_AsLong(protocol);
1131 if (proto < 0) {
1132 if (proto == -1 && PyErr_Occurred())
1133 return -1;
1134 proto = HIGHEST_PROTOCOL;
1135 }
1136 else if (proto > HIGHEST_PROTOCOL) {
1137 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1138 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001139 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001140 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001141 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001142 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001143 self->bin = proto > 0;
1144 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001145 return 0;
1146}
1147
1148/* Returns -1 (with an exception set) on failure, 0 on success. This may
1149 be called once on a freshly created Pickler. */
1150static int
1151_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1152{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001153 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001154 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001155 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1156 return -1;
1157 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001158 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001159 PyErr_SetString(PyExc_TypeError,
1160 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001161 return -1;
1162 }
1163
1164 return 0;
1165}
1166
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001167/* Returns the size of the input on success, -1 on failure. This takes its
1168 own reference to `input`. */
1169static Py_ssize_t
1170_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1171{
1172 if (self->buffer.buf != NULL)
1173 PyBuffer_Release(&self->buffer);
1174 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1175 return -1;
1176 self->input_buffer = self->buffer.buf;
1177 self->input_len = self->buffer.len;
1178 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001179 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001180 return self->input_len;
1181}
1182
Antoine Pitrou04248a82010-10-12 20:51:21 +00001183static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001184bad_readline(void)
1185{
1186 PickleState *st = _Pickle_GetGlobalState();
1187 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1188 return -1;
1189}
1190
1191static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001192_Unpickler_SkipConsumed(UnpicklerObject *self)
1193{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001194 Py_ssize_t consumed;
1195 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001196
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001197 consumed = self->next_read_idx - self->prefetched_idx;
1198 if (consumed <= 0)
1199 return 0;
1200
1201 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001202 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001203 r = PyObject_CallFunction(self->read, "n", consumed);
1204 if (r == NULL)
1205 return -1;
1206 Py_DECREF(r);
1207
1208 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001209 return 0;
1210}
1211
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001212static const Py_ssize_t READ_WHOLE_LINE = -1;
1213
1214/* If reading from a file, we need to only pull the bytes we need, since there
1215 may be multiple pickle objects arranged contiguously in the same input
1216 buffer.
1217
1218 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1219 bytes from the input stream/buffer.
1220
1221 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1222 failure; on success, returns the number of bytes read from the file.
1223
1224 On success, self->input_len will be 0; this is intentional so that when
1225 unpickling from a file, the "we've run out of data" code paths will trigger,
1226 causing the Unpickler to go back to the file for more data. Use the returned
1227 size to tell you how much data you can process. */
1228static Py_ssize_t
1229_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1230{
1231 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001232 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001233
1234 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001235
Antoine Pitrou04248a82010-10-12 20:51:21 +00001236 if (_Unpickler_SkipConsumed(self) < 0)
1237 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001238
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001239 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001240 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001241 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001242 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001243 PyObject *len;
1244 /* Prefetch some data without advancing the file pointer, if possible */
1245 if (self->peek && n < PREFETCH) {
1246 len = PyLong_FromSsize_t(PREFETCH);
1247 if (len == NULL)
1248 return -1;
1249 data = _Pickle_FastCall(self->peek, len);
1250 if (data == NULL) {
1251 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1252 return -1;
1253 /* peek() is probably not supported by the given file object */
1254 PyErr_Clear();
1255 Py_CLEAR(self->peek);
1256 }
1257 else {
1258 read_size = _Unpickler_SetStringInput(self, data);
1259 Py_DECREF(data);
1260 self->prefetched_idx = 0;
1261 if (n <= read_size)
1262 return n;
1263 }
1264 }
1265 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001266 if (len == NULL)
1267 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001268 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001269 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001270 if (data == NULL)
1271 return -1;
1272
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001273 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001274 Py_DECREF(data);
1275 return read_size;
1276}
1277
Victor Stinner19ed27e2016-05-20 11:42:37 +02001278/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001279static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001280_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001281{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001282 Py_ssize_t num_read;
1283
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001284 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001285 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1286 PickleState *st = _Pickle_GetGlobalState();
1287 PyErr_SetString(st->UnpicklingError,
1288 "read would overflow (invalid bytecode)");
1289 return -1;
1290 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001291
1292 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1293 assert(self->next_read_idx + n > self->input_len);
1294
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001295 if (!self->read)
1296 return bad_readline();
1297
Antoine Pitrou04248a82010-10-12 20:51:21 +00001298 num_read = _Unpickler_ReadFromFile(self, n);
1299 if (num_read < 0)
1300 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001301 if (num_read < n)
1302 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001303 *s = self->input_buffer;
1304 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001305 return n;
1306}
1307
Victor Stinner19ed27e2016-05-20 11:42:37 +02001308/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1309
1310 This should be used for all data reads, rather than accessing the unpickler's
1311 input buffer directly. This method deals correctly with reading from input
1312 streams, which the input buffer doesn't deal with.
1313
1314 Note that when reading from a file-like object, self->next_read_idx won't
1315 be updated (it should remain at 0 for the entire unpickling process). You
1316 should use this function's return value to know how many bytes you can
1317 consume.
1318
1319 Returns -1 (with an exception set) on failure. On success, return the
1320 number of chars read. */
1321#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001322 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001323 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1324 (self)->next_read_idx += (n), \
1325 (n)) \
1326 : _Unpickler_ReadImpl(self, (s), (n)))
1327
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001328static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001329_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1330 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001331{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001332 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001333 if (input_line == NULL) {
1334 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001335 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001336 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001337
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001338 memcpy(input_line, line, len);
1339 input_line[len] = '\0';
1340 self->input_line = input_line;
1341 *result = self->input_line;
1342 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001343}
1344
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001345/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001346 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001347
1348 Returns the number of chars read, or -1 on failure. */
1349static Py_ssize_t
1350_Unpickler_Readline(UnpicklerObject *self, char **result)
1351{
1352 Py_ssize_t i, num_read;
1353
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001354 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001355 if (self->input_buffer[i] == '\n') {
1356 char *line_start = self->input_buffer + self->next_read_idx;
1357 num_read = i - self->next_read_idx + 1;
1358 self->next_read_idx = i + 1;
1359 return _Unpickler_CopyLine(self, line_start, num_read, result);
1360 }
1361 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001362 if (!self->read)
1363 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001364
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001365 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1366 if (num_read < 0)
1367 return -1;
1368 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1369 return bad_readline();
1370 self->next_read_idx = num_read;
1371 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001372}
1373
1374/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1375 will be modified in place. */
1376static int
1377_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1378{
1379 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001380
1381 assert(new_size > self->memo_size);
1382
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001383 PyMem_RESIZE(self->memo, PyObject *, new_size);
1384 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001385 PyErr_NoMemory();
1386 return -1;
1387 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001388 for (i = self->memo_size; i < new_size; i++)
1389 self->memo[i] = NULL;
1390 self->memo_size = new_size;
1391 return 0;
1392}
1393
1394/* Returns NULL if idx is out of bounds. */
1395static PyObject *
1396_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1397{
1398 if (idx < 0 || idx >= self->memo_size)
1399 return NULL;
1400
1401 return self->memo[idx];
1402}
1403
1404/* Returns -1 (with an exception set) on failure, 0 on success.
1405 This takes its own reference to `value`. */
1406static int
1407_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1408{
1409 PyObject *old_item;
1410
1411 if (idx >= self->memo_size) {
1412 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1413 return -1;
1414 assert(idx < self->memo_size);
1415 }
1416 Py_INCREF(value);
1417 old_item = self->memo[idx];
1418 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001419 if (old_item != NULL) {
1420 Py_DECREF(old_item);
1421 }
1422 else {
1423 self->memo_len++;
1424 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001425 return 0;
1426}
1427
1428static PyObject **
1429_Unpickler_NewMemo(Py_ssize_t new_size)
1430{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001431 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001432 if (memo == NULL) {
1433 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001434 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001435 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001436 memset(memo, 0, new_size * sizeof(PyObject *));
1437 return memo;
1438}
1439
1440/* Free the unpickler's memo, taking care to decref any items left in it. */
1441static void
1442_Unpickler_MemoCleanup(UnpicklerObject *self)
1443{
1444 Py_ssize_t i;
1445 PyObject **memo = self->memo;
1446
1447 if (self->memo == NULL)
1448 return;
1449 self->memo = NULL;
1450 i = self->memo_size;
1451 while (--i >= 0) {
1452 Py_XDECREF(memo[i]);
1453 }
1454 PyMem_FREE(memo);
1455}
1456
1457static UnpicklerObject *
1458_Unpickler_New(void)
1459{
1460 UnpicklerObject *self;
1461
1462 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1463 if (self == NULL)
1464 return NULL;
1465
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001466 self->pers_func = NULL;
1467 self->input_buffer = NULL;
1468 self->input_line = NULL;
1469 self->input_len = 0;
1470 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001471 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001472 self->read = NULL;
1473 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001474 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001475 self->encoding = NULL;
1476 self->errors = NULL;
1477 self->marks = NULL;
1478 self->num_marks = 0;
1479 self->marks_size = 0;
1480 self->proto = 0;
1481 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001482 memset(&self->buffer, 0, sizeof(Py_buffer));
1483 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001484 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001485 self->memo = _Unpickler_NewMemo(self->memo_size);
1486 self->stack = (Pdata *)Pdata_New();
1487
1488 if (self->memo == NULL || self->stack == NULL) {
1489 Py_DECREF(self);
1490 return NULL;
1491 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001492
1493 return self;
1494}
1495
1496/* Returns -1 (with an exception set) on failure, 0 on success. This may
1497 be called once on a freshly created Pickler. */
1498static int
1499_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1500{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001501 _Py_IDENTIFIER(peek);
1502 _Py_IDENTIFIER(read);
1503 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001504
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001505 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1506 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001507 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001508 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1509 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001510 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001511 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001512 PyErr_SetString(PyExc_TypeError,
1513 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001514 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001515 Py_CLEAR(self->read);
1516 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001517 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001518 return -1;
1519 }
1520 return 0;
1521}
1522
1523/* Returns -1 (with an exception set) on failure, 0 on success. This may
1524 be called once on a freshly created Pickler. */
1525static int
1526_Unpickler_SetInputEncoding(UnpicklerObject *self,
1527 const char *encoding,
1528 const char *errors)
1529{
1530 if (encoding == NULL)
1531 encoding = "ASCII";
1532 if (errors == NULL)
1533 errors = "strict";
1534
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001535 self->encoding = _PyMem_Strdup(encoding);
1536 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001537 if (self->encoding == NULL || self->errors == NULL) {
1538 PyErr_NoMemory();
1539 return -1;
1540 }
1541 return 0;
1542}
1543
1544/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001545static int
1546memo_get(PicklerObject *self, PyObject *key)
1547{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001548 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001549 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001550 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001551
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001552 value = PyMemoTable_Get(self->memo, key);
1553 if (value == NULL) {
1554 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001555 return -1;
1556 }
1557
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558 if (!self->bin) {
1559 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001560 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1561 "%" PY_FORMAT_SIZE_T "d\n", *value);
1562 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001563 }
1564 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001565 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001566 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001567 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001568 len = 2;
1569 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001570 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001571 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001572 pdata[1] = (unsigned char)(*value & 0xff);
1573 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1574 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1575 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001576 len = 5;
1577 }
1578 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001579 PickleState *st = _Pickle_GetGlobalState();
1580 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001581 "memo id too large for LONG_BINGET");
1582 return -1;
1583 }
1584 }
1585
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001586 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001587 return -1;
1588
1589 return 0;
1590}
1591
1592/* Store an object in the memo, assign it a new unique ID based on the number
1593 of objects currently stored in the memo and generate a PUT opcode. */
1594static int
1595memo_put(PicklerObject *self, PyObject *obj)
1596{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001597 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001598 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001599 Py_ssize_t idx;
1600
1601 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001602
1603 if (self->fast)
1604 return 0;
1605
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001606 idx = PyMemoTable_Size(self->memo);
1607 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1608 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001609
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001610 if (self->proto >= 4) {
1611 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1612 return -1;
1613 return 0;
1614 }
1615 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001616 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001617 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001618 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001619 len = strlen(pdata);
1620 }
1621 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001622 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001623 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001624 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001625 len = 2;
1626 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001627 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001628 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001629 pdata[1] = (unsigned char)(idx & 0xff);
1630 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1631 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1632 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001633 len = 5;
1634 }
1635 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001636 PickleState *st = _Pickle_GetGlobalState();
1637 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 "memo id too large for LONG_BINPUT");
1639 return -1;
1640 }
1641 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001642 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001643 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001644
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001645 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001646}
1647
1648static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001649get_dotted_path(PyObject *obj, PyObject *name)
1650{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001651 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001652 PyObject *dotted_path;
1653 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001654
1655 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001656 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001657 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001658 n = PyList_GET_SIZE(dotted_path);
1659 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001660 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001661 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001662 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001663 if (obj == NULL)
1664 PyErr_Format(PyExc_AttributeError,
1665 "Can't pickle local object %R", name);
1666 else
1667 PyErr_Format(PyExc_AttributeError,
1668 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001669 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001670 return NULL;
1671 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001672 }
1673 return dotted_path;
1674}
1675
1676static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001677get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001678{
1679 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001680 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001681
1682 assert(PyList_CheckExact(names));
1683 Py_INCREF(obj);
1684 n = PyList_GET_SIZE(names);
1685 for (i = 0; i < n; i++) {
1686 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001687 Py_XDECREF(parent);
1688 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001689 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001690 if (obj == NULL) {
1691 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001692 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001693 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001694 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001695 if (pparent != NULL)
1696 *pparent = parent;
1697 else
1698 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001699 return obj;
1700}
1701
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001702
1703static PyObject *
1704getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1705{
1706 PyObject *dotted_path, *attr;
1707
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001708 if (allow_qualname) {
1709 dotted_path = get_dotted_path(obj, name);
1710 if (dotted_path == NULL)
1711 return NULL;
1712 attr = get_deep_attribute(obj, dotted_path, NULL);
1713 Py_DECREF(dotted_path);
1714 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001715 else {
1716 (void)_PyObject_LookupAttr(obj, name, &attr);
1717 }
1718 if (attr == NULL && !PyErr_Occurred()) {
1719 PyErr_Format(PyExc_AttributeError,
1720 "Can't get attribute %R on %R", name, obj);
1721 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001722 return attr;
1723}
1724
Eric Snow3f9eee62017-09-15 16:35:20 -06001725static int
1726_checkmodule(PyObject *module_name, PyObject *module,
1727 PyObject *global, PyObject *dotted_path)
1728{
1729 if (module == Py_None) {
1730 return -1;
1731 }
1732 if (PyUnicode_Check(module_name) &&
1733 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1734 return -1;
1735 }
1736
1737 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1738 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001739 return -1;
1740 }
1741 if (candidate != global) {
1742 Py_DECREF(candidate);
1743 return -1;
1744 }
1745 Py_DECREF(candidate);
1746 return 0;
1747}
1748
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001749static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001750whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001751{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001752 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001753 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001754 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001755 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001756 _Py_IDENTIFIER(__module__);
1757 _Py_IDENTIFIER(modules);
1758 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001759
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001760 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1761 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001762 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001763 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001764 /* In some rare cases (e.g., bound methods of extension types),
1765 __module__ can be None. If it is so, then search sys.modules for
1766 the module of global. */
1767 if (module_name != Py_None)
1768 return module_name;
1769 Py_CLEAR(module_name);
1770 }
1771 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001772
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001773 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001774 modules = _PySys_GetObjectId(&PyId_modules);
1775 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001776 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001777 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001778 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001779 if (PyDict_CheckExact(modules)) {
1780 i = 0;
1781 while (PyDict_Next(modules, &i, &module_name, &module)) {
1782 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1783 Py_INCREF(module_name);
1784 return module_name;
1785 }
1786 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001787 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001788 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001789 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001790 }
1791 else {
1792 PyObject *iterator = PyObject_GetIter(modules);
1793 if (iterator == NULL) {
1794 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001795 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001796 while ((module_name = PyIter_Next(iterator))) {
1797 module = PyObject_GetItem(modules, module_name);
1798 if (module == NULL) {
1799 Py_DECREF(module_name);
1800 Py_DECREF(iterator);
1801 return NULL;
1802 }
1803 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1804 Py_DECREF(module);
1805 Py_DECREF(iterator);
1806 return module_name;
1807 }
1808 Py_DECREF(module);
1809 Py_DECREF(module_name);
1810 if (PyErr_Occurred()) {
1811 Py_DECREF(iterator);
1812 return NULL;
1813 }
1814 }
1815 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001816 }
1817
1818 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001819 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001820 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001821 return module_name;
1822}
1823
1824/* fast_save_enter() and fast_save_leave() are guards against recursive
1825 objects when Pickler is used with the "fast mode" (i.e., with object
1826 memoization disabled). If the nesting of a list or dict object exceed
1827 FAST_NESTING_LIMIT, these guards will start keeping an internal
1828 reference to the seen list or dict objects and check whether these objects
1829 are recursive. These are not strictly necessary, since save() has a
1830 hard-coded recursion limit, but they give a nicer error message than the
1831 typical RuntimeError. */
1832static int
1833fast_save_enter(PicklerObject *self, PyObject *obj)
1834{
1835 /* if fast_nesting < 0, we're doing an error exit. */
1836 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1837 PyObject *key = NULL;
1838 if (self->fast_memo == NULL) {
1839 self->fast_memo = PyDict_New();
1840 if (self->fast_memo == NULL) {
1841 self->fast_nesting = -1;
1842 return 0;
1843 }
1844 }
1845 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001846 if (key == NULL) {
1847 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001848 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001849 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001850 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001851 Py_DECREF(key);
1852 PyErr_Format(PyExc_ValueError,
1853 "fast mode: can't pickle cyclic objects "
1854 "including object type %.200s at %p",
1855 obj->ob_type->tp_name, obj);
1856 self->fast_nesting = -1;
1857 return 0;
1858 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001859 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001860 Py_DECREF(key);
1861 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001862 return 0;
1863 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001864 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1865 Py_DECREF(key);
1866 self->fast_nesting = -1;
1867 return 0;
1868 }
1869 Py_DECREF(key);
1870 }
1871 return 1;
1872}
1873
1874static int
1875fast_save_leave(PicklerObject *self, PyObject *obj)
1876{
1877 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1878 PyObject *key = PyLong_FromVoidPtr(obj);
1879 if (key == NULL)
1880 return 0;
1881 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1882 Py_DECREF(key);
1883 return 0;
1884 }
1885 Py_DECREF(key);
1886 }
1887 return 1;
1888}
1889
1890static int
1891save_none(PicklerObject *self, PyObject *obj)
1892{
1893 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001894 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001895 return -1;
1896
1897 return 0;
1898}
1899
1900static int
1901save_bool(PicklerObject *self, PyObject *obj)
1902{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001903 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001904 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001905 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 return -1;
1907 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001908 else {
1909 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1910 * so that unpicklers written before bools were introduced unpickle them
1911 * as ints, but unpicklers after can recognize that bools were intended.
1912 * Note that protocol 2 added direct ways to pickle bools.
1913 */
1914 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1915 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1916 return -1;
1917 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001918 return 0;
1919}
1920
1921static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001922save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001923{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001924 PyObject *repr = NULL;
1925 Py_ssize_t size;
1926 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001927 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001928 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001929
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001930 val= PyLong_AsLongAndOverflow(obj, &overflow);
1931 if (!overflow && (sizeof(long) <= 4 ||
1932 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1933 {
Larry Hastings61272b72014-01-07 12:41:53 -08001934 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001935
1936 Note: we can't use -0x80000000L in the above condition because some
1937 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1938 before applying the unary minus when sizeof(long) <= 4. The
1939 resulting value stays unsigned which is commonly not what we want,
1940 so MSVC happily warns us about it. However, that result would have
1941 been fine because we guard for sizeof(long) <= 4 which turns the
1942 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001943 char pdata[32];
1944 Py_ssize_t len = 0;
1945
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001946 if (self->bin) {
1947 pdata[1] = (unsigned char)(val & 0xff);
1948 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1949 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1950 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001951
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001952 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1953 pdata[0] = BININT;
1954 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001955 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001956 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001957 pdata[0] = BININT2;
1958 len = 3;
1959 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001960 else {
1961 pdata[0] = BININT1;
1962 len = 2;
1963 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001964 }
1965 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001966 sprintf(pdata, "%c%ld\n", INT, val);
1967 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001968 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001969 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001970 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001971
1972 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001973 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001974 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001975
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976 if (self->proto >= 2) {
1977 /* Linear-time pickling. */
1978 size_t nbits;
1979 size_t nbytes;
1980 unsigned char *pdata;
1981 char header[5];
1982 int i;
1983 int sign = _PyLong_Sign(obj);
1984
1985 if (sign == 0) {
1986 header[0] = LONG1;
1987 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001988 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001989 goto error;
1990 return 0;
1991 }
1992 nbits = _PyLong_NumBits(obj);
1993 if (nbits == (size_t)-1 && PyErr_Occurred())
1994 goto error;
1995 /* How many bytes do we need? There are nbits >> 3 full
1996 * bytes of data, and nbits & 7 leftover bits. If there
1997 * are any leftover bits, then we clearly need another
1998 * byte. Wnat's not so obvious is that we *probably*
1999 * need another byte even if there aren't any leftovers:
2000 * the most-significant bit of the most-significant byte
2001 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002002 * opposite of the one we need. The exception is ints
2003 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002004 * its own 256's-complement, so has the right sign bit
2005 * even without the extra byte. That's a pain to check
2006 * for in advance, though, so we always grab an extra
2007 * byte at the start, and cut it back later if possible.
2008 */
2009 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002010 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002011 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002012 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002013 goto error;
2014 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002015 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002016 if (repr == NULL)
2017 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002018 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002019 i = _PyLong_AsByteArray((PyLongObject *)obj,
2020 pdata, nbytes,
2021 1 /* little endian */ , 1 /* signed */ );
2022 if (i < 0)
2023 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002024 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002025 * needed. This is so iff the MSB is all redundant sign
2026 * bits.
2027 */
2028 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002029 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002030 pdata[nbytes - 1] == 0xff &&
2031 (pdata[nbytes - 2] & 0x80) != 0) {
2032 nbytes--;
2033 }
2034
2035 if (nbytes < 256) {
2036 header[0] = LONG1;
2037 header[1] = (unsigned char)nbytes;
2038 size = 2;
2039 }
2040 else {
2041 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002042 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002043 for (i = 1; i < 5; i++) {
2044 header[i] = (unsigned char)(size & 0xff);
2045 size >>= 8;
2046 }
2047 size = 5;
2048 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002049 if (_Pickler_Write(self, header, size) < 0 ||
2050 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002051 goto error;
2052 }
2053 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002054 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002055 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002056
Mark Dickinson8dd05142009-01-20 20:43:58 +00002057 /* proto < 2: write the repr and newline. This is quadratic-time (in
2058 the number of digits), in both directions. We add a trailing 'L'
2059 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002060
2061 repr = PyObject_Repr(obj);
2062 if (repr == NULL)
2063 goto error;
2064
Serhiy Storchaka06515832016-11-20 09:13:07 +02002065 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002066 if (string == NULL)
2067 goto error;
2068
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002069 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2070 _Pickler_Write(self, string, size) < 0 ||
2071 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002072 goto error;
2073 }
2074
2075 if (0) {
2076 error:
2077 status = -1;
2078 }
2079 Py_XDECREF(repr);
2080
2081 return status;
2082}
2083
2084static int
2085save_float(PicklerObject *self, PyObject *obj)
2086{
2087 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2088
2089 if (self->bin) {
2090 char pdata[9];
2091 pdata[0] = BINFLOAT;
2092 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2093 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002094 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002095 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002096 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002097 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002098 int result = -1;
2099 char *buf = NULL;
2100 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002101
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002102 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002103 goto done;
2104
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002105 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002106 if (!buf) {
2107 PyErr_NoMemory();
2108 goto done;
2109 }
2110
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002111 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002112 goto done;
2113
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002114 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002115 goto done;
2116
2117 result = 0;
2118done:
2119 PyMem_Free(buf);
2120 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002121 }
2122
2123 return 0;
2124}
2125
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002126/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002127
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002128 The large contiguous data is written directly into the underlying file
2129 object, bypassing the output_buffer of the Pickler. We intentionally
2130 do not insert a protocol 4 frame opcode to make it possible to optimize
2131 file.read calls in the loader.
2132 */
2133static int
2134_Pickler_write_bytes(PicklerObject *self,
2135 const char *header, Py_ssize_t header_size,
2136 const char *data, Py_ssize_t data_size,
2137 PyObject *payload)
2138{
2139 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2140 int framing = self->framing;
2141
2142 if (bypass_buffer) {
2143 assert(self->output_buffer != NULL);
2144 /* Commit the previous frame. */
2145 if (_Pickler_CommitFrame(self)) {
2146 return -1;
2147 }
2148 /* Disable framing temporarily */
2149 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002150 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002151
2152 if (_Pickler_Write(self, header, header_size) < 0) {
2153 return -1;
2154 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002155
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002156 if (bypass_buffer && self->write != NULL) {
2157 /* Bypass the in-memory buffer to directly stream large data
2158 into the underlying file object. */
2159 PyObject *result, *mem = NULL;
2160 /* Dump the output buffer to the file. */
2161 if (_Pickler_FlushToFile(self) < 0) {
2162 return -1;
2163 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002164
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002165 /* Stream write the payload into the file without going through the
2166 output buffer. */
2167 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002168 /* TODO: It would be better to use a memoryview with a linked
2169 original string if this is possible. */
2170 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002171 if (payload == NULL) {
2172 return -1;
2173 }
2174 }
2175 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2176 Py_XDECREF(mem);
2177 if (result == NULL) {
2178 return -1;
2179 }
2180 Py_DECREF(result);
2181
2182 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2183 if (_Pickler_ClearBuffer(self) < 0) {
2184 return -1;
2185 }
2186 }
2187 else {
2188 if (_Pickler_Write(self, data, data_size) < 0) {
2189 return -1;
2190 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002191 }
2192
2193 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002194 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002195
2196 return 0;
2197}
2198
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002199static int
2200save_bytes(PicklerObject *self, PyObject *obj)
2201{
2202 if (self->proto < 3) {
2203 /* Older pickle protocols do not have an opcode for pickling bytes
2204 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002205 the __reduce__ method) to permit bytes object unpickling.
2206
2207 Here we use a hack to be compatible with Python 2. Since in Python
2208 2 'bytes' is just an alias for 'str' (which has different
2209 parameters than the actual bytes object), we use codecs.encode
2210 to create the appropriate 'str' object when unpickled using
2211 Python 2 *and* the appropriate 'bytes' object when unpickled
2212 using Python 3. Again this is a hack and we don't need to do this
2213 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002214 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002215 int status;
2216
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002217 if (PyBytes_GET_SIZE(obj) == 0) {
2218 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2219 }
2220 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002221 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002222 PyObject *unicode_str =
2223 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2224 PyBytes_GET_SIZE(obj),
2225 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002226 _Py_IDENTIFIER(latin1);
2227
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002228 if (unicode_str == NULL)
2229 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002230 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002231 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002232 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002233 Py_DECREF(unicode_str);
2234 }
2235
2236 if (reduce_value == NULL)
2237 return -1;
2238
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002239 /* save_reduce() will memoize the object automatically. */
2240 status = save_reduce(self, reduce_value, obj);
2241 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002242 return status;
2243 }
2244 else {
2245 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002246 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002247 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002248
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002249 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002250 if (size < 0)
2251 return -1;
2252
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002253 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002254 header[0] = SHORT_BINBYTES;
2255 header[1] = (unsigned char)size;
2256 len = 2;
2257 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002258 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259 header[0] = BINBYTES;
2260 header[1] = (unsigned char)(size & 0xff);
2261 header[2] = (unsigned char)((size >> 8) & 0xff);
2262 header[3] = (unsigned char)((size >> 16) & 0xff);
2263 header[4] = (unsigned char)((size >> 24) & 0xff);
2264 len = 5;
2265 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002266 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002267 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002268 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002269 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002270 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002271 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002272 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002273 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002274 return -1; /* string too large */
2275 }
2276
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002277 if (_Pickler_write_bytes(self, header, len,
2278 PyBytes_AS_STRING(obj), size, obj) < 0)
2279 {
2280 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002281 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002282
2283 if (memo_put(self, obj) < 0)
2284 return -1;
2285
2286 return 0;
2287 }
2288}
2289
2290/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2291 backslash and newline characters to \uXXXX escapes. */
2292static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002293raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002294{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002296 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002297 void *data;
2298 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002299 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002300
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002301 if (PyUnicode_READY(obj))
2302 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002303
Victor Stinner358af132015-10-12 22:36:57 +02002304 _PyBytesWriter_Init(&writer);
2305
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002306 size = PyUnicode_GET_LENGTH(obj);
2307 data = PyUnicode_DATA(obj);
2308 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002309
Victor Stinner358af132015-10-12 22:36:57 +02002310 p = _PyBytesWriter_Alloc(&writer, size);
2311 if (p == NULL)
2312 goto error;
2313 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002314
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002315 for (i=0; i < size; i++) {
2316 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002317 /* Map 32-bit characters to '\Uxxxxxxxx' */
2318 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002319 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002320 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2321 if (p == NULL)
2322 goto error;
2323
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002324 *p++ = '\\';
2325 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002326 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2327 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2328 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2329 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2330 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2331 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2332 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2333 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002334 }
Victor Stinner358af132015-10-12 22:36:57 +02002335 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002336 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002337 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002338 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2339 if (p == NULL)
2340 goto error;
2341
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002342 *p++ = '\\';
2343 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002344 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2345 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2346 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2347 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002348 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002349 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002350 else
2351 *p++ = (char) ch;
2352 }
Victor Stinner358af132015-10-12 22:36:57 +02002353
2354 return _PyBytesWriter_Finish(&writer, p);
2355
2356error:
2357 _PyBytesWriter_Dealloc(&writer);
2358 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002359}
2360
2361static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002362write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002363{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002364 char header[9];
2365 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002366 PyObject *encoded = NULL;
2367 Py_ssize_t size;
2368 const char *data;
2369
2370 if (PyUnicode_READY(obj))
2371 return -1;
2372
2373 data = PyUnicode_AsUTF8AndSize(obj, &size);
2374 if (data == NULL) {
2375 /* Issue #8383: for strings with lone surrogates, fallback on the
2376 "surrogatepass" error handler. */
2377 PyErr_Clear();
2378 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2379 if (encoded == NULL)
2380 return -1;
2381
2382 data = PyBytes_AS_STRING(encoded);
2383 size = PyBytes_GET_SIZE(encoded);
2384 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002385
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002386 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002387 if (size <= 0xff && self->proto >= 4) {
2388 header[0] = SHORT_BINUNICODE;
2389 header[1] = (unsigned char)(size & 0xff);
2390 len = 2;
2391 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002392 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002393 header[0] = BINUNICODE;
2394 header[1] = (unsigned char)(size & 0xff);
2395 header[2] = (unsigned char)((size >> 8) & 0xff);
2396 header[3] = (unsigned char)((size >> 16) & 0xff);
2397 header[4] = (unsigned char)((size >> 24) & 0xff);
2398 len = 5;
2399 }
2400 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002401 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002402 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002403 len = 9;
2404 }
2405 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002406 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002407 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002408 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002409 return -1;
2410 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002411
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002412 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2413 Py_XDECREF(encoded);
2414 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002415 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002416 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002417 return 0;
2418}
2419
2420static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002421save_unicode(PicklerObject *self, PyObject *obj)
2422{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002423 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002424 if (write_unicode_binary(self, obj) < 0)
2425 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002426 }
2427 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002428 PyObject *encoded;
2429 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002430 const char unicode_op = UNICODE;
2431
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002432 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002433 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002434 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002435
Antoine Pitrou299978d2013-04-07 17:38:11 +02002436 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2437 Py_DECREF(encoded);
2438 return -1;
2439 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002440
2441 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002442 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2443 Py_DECREF(encoded);
2444 return -1;
2445 }
2446 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002447
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002448 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002449 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002450 }
2451 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002452 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002453
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002454 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455}
2456
2457/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2458static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002459store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002460{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002461 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002462
2463 assert(PyTuple_Size(t) == len);
2464
2465 for (i = 0; i < len; i++) {
2466 PyObject *element = PyTuple_GET_ITEM(t, i);
2467
2468 if (element == NULL)
2469 return -1;
2470 if (save(self, element, 0) < 0)
2471 return -1;
2472 }
2473
2474 return 0;
2475}
2476
2477/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2478 * used across protocols to minimize the space needed to pickle them.
2479 * Tuples are also the only builtin immutable type that can be recursive
2480 * (a tuple can be reached from itself), and that requires some subtle
2481 * magic so that it works in all cases. IOW, this is a long routine.
2482 */
2483static int
2484save_tuple(PicklerObject *self, PyObject *obj)
2485{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002486 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002487
2488 const char mark_op = MARK;
2489 const char tuple_op = TUPLE;
2490 const char pop_op = POP;
2491 const char pop_mark_op = POP_MARK;
2492 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2493
2494 if ((len = PyTuple_Size(obj)) < 0)
2495 return -1;
2496
2497 if (len == 0) {
2498 char pdata[2];
2499
2500 if (self->proto) {
2501 pdata[0] = EMPTY_TUPLE;
2502 len = 1;
2503 }
2504 else {
2505 pdata[0] = MARK;
2506 pdata[1] = TUPLE;
2507 len = 2;
2508 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002509 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002510 return -1;
2511 return 0;
2512 }
2513
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002514 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002515 * saving the tuple elements, the tuple must be recursive, in
2516 * which case we'll pop everything we put on the stack, and fetch
2517 * its value from the memo.
2518 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002519 if (len <= 3 && self->proto >= 2) {
2520 /* Use TUPLE{1,2,3} opcodes. */
2521 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002522 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002523
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002524 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002525 /* pop the len elements */
2526 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002527 if (_Pickler_Write(self, &pop_op, 1) < 0)
2528 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002529 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002530 if (memo_get(self, obj) < 0)
2531 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002532
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002533 return 0;
2534 }
2535 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002536 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2537 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002538 }
2539 goto memoize;
2540 }
2541
2542 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2543 * Generate MARK e1 e2 ... TUPLE
2544 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002545 if (_Pickler_Write(self, &mark_op, 1) < 0)
2546 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002547
2548 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002549 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002550
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002551 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002552 /* pop the stack stuff we pushed */
2553 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002554 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2555 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556 }
2557 else {
2558 /* Note that we pop one more than len, to remove
2559 * the MARK too.
2560 */
2561 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002562 if (_Pickler_Write(self, &pop_op, 1) < 0)
2563 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002564 }
2565 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002566 if (memo_get(self, obj) < 0)
2567 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002568
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002569 return 0;
2570 }
2571 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002572 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2573 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002574 }
2575
2576 memoize:
2577 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002578 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002579
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002580 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002581}
2582
2583/* iter is an iterator giving items, and we batch up chunks of
2584 * MARK item item ... item APPENDS
2585 * opcode sequences. Calling code should have arranged to first create an
2586 * empty list, or list-like object, for the APPENDS to operate on.
2587 * Returns 0 on success, <0 on error.
2588 */
2589static int
2590batch_list(PicklerObject *self, PyObject *iter)
2591{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002592 PyObject *obj = NULL;
2593 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002594 int i, n;
2595
2596 const char mark_op = MARK;
2597 const char append_op = APPEND;
2598 const char appends_op = APPENDS;
2599
2600 assert(iter != NULL);
2601
2602 /* XXX: I think this function could be made faster by avoiding the
2603 iterator interface and fetching objects directly from list using
2604 PyList_GET_ITEM.
2605 */
2606
2607 if (self->proto == 0) {
2608 /* APPENDS isn't available; do one at a time. */
2609 for (;;) {
2610 obj = PyIter_Next(iter);
2611 if (obj == NULL) {
2612 if (PyErr_Occurred())
2613 return -1;
2614 break;
2615 }
2616 i = save(self, obj, 0);
2617 Py_DECREF(obj);
2618 if (i < 0)
2619 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002620 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002621 return -1;
2622 }
2623 return 0;
2624 }
2625
2626 /* proto > 0: write in batches of BATCHSIZE. */
2627 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002628 /* Get first item */
2629 firstitem = PyIter_Next(iter);
2630 if (firstitem == NULL) {
2631 if (PyErr_Occurred())
2632 goto error;
2633
2634 /* nothing more to add */
2635 break;
2636 }
2637
2638 /* Try to get a second item */
2639 obj = PyIter_Next(iter);
2640 if (obj == NULL) {
2641 if (PyErr_Occurred())
2642 goto error;
2643
2644 /* Only one item to write */
2645 if (save(self, firstitem, 0) < 0)
2646 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002647 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002648 goto error;
2649 Py_CLEAR(firstitem);
2650 break;
2651 }
2652
2653 /* More than one item to write */
2654
2655 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002656 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002657 goto error;
2658
2659 if (save(self, firstitem, 0) < 0)
2660 goto error;
2661 Py_CLEAR(firstitem);
2662 n = 1;
2663
2664 /* Fetch and save up to BATCHSIZE items */
2665 while (obj) {
2666 if (save(self, obj, 0) < 0)
2667 goto error;
2668 Py_CLEAR(obj);
2669 n += 1;
2670
2671 if (n == BATCHSIZE)
2672 break;
2673
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002674 obj = PyIter_Next(iter);
2675 if (obj == NULL) {
2676 if (PyErr_Occurred())
2677 goto error;
2678 break;
2679 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002680 }
2681
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002682 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002683 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002684
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002685 } while (n == BATCHSIZE);
2686 return 0;
2687
2688 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002689 Py_XDECREF(firstitem);
2690 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002691 return -1;
2692}
2693
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002694/* This is a variant of batch_list() above, specialized for lists (with no
2695 * support for list subclasses). Like batch_list(), we batch up chunks of
2696 * MARK item item ... item APPENDS
2697 * opcode sequences. Calling code should have arranged to first create an
2698 * empty list, or list-like object, for the APPENDS to operate on.
2699 * Returns 0 on success, -1 on error.
2700 *
2701 * This version is considerably faster than batch_list(), if less general.
2702 *
2703 * Note that this only works for protocols > 0.
2704 */
2705static int
2706batch_list_exact(PicklerObject *self, PyObject *obj)
2707{
2708 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002709 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002710
2711 const char append_op = APPEND;
2712 const char appends_op = APPENDS;
2713 const char mark_op = MARK;
2714
2715 assert(obj != NULL);
2716 assert(self->proto > 0);
2717 assert(PyList_CheckExact(obj));
2718
2719 if (PyList_GET_SIZE(obj) == 1) {
2720 item = PyList_GET_ITEM(obj, 0);
2721 if (save(self, item, 0) < 0)
2722 return -1;
2723 if (_Pickler_Write(self, &append_op, 1) < 0)
2724 return -1;
2725 return 0;
2726 }
2727
2728 /* Write in batches of BATCHSIZE. */
2729 total = 0;
2730 do {
2731 this_batch = 0;
2732 if (_Pickler_Write(self, &mark_op, 1) < 0)
2733 return -1;
2734 while (total < PyList_GET_SIZE(obj)) {
2735 item = PyList_GET_ITEM(obj, total);
2736 if (save(self, item, 0) < 0)
2737 return -1;
2738 total++;
2739 if (++this_batch == BATCHSIZE)
2740 break;
2741 }
2742 if (_Pickler_Write(self, &appends_op, 1) < 0)
2743 return -1;
2744
2745 } while (total < PyList_GET_SIZE(obj));
2746
2747 return 0;
2748}
2749
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002750static int
2751save_list(PicklerObject *self, PyObject *obj)
2752{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002753 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002754 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002755 int status = 0;
2756
2757 if (self->fast && !fast_save_enter(self, obj))
2758 goto error;
2759
2760 /* Create an empty list. */
2761 if (self->bin) {
2762 header[0] = EMPTY_LIST;
2763 len = 1;
2764 }
2765 else {
2766 header[0] = MARK;
2767 header[1] = LIST;
2768 len = 2;
2769 }
2770
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002771 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002772 goto error;
2773
2774 /* Get list length, and bow out early if empty. */
2775 if ((len = PyList_Size(obj)) < 0)
2776 goto error;
2777
2778 if (memo_put(self, obj) < 0)
2779 goto error;
2780
2781 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002782 /* Materialize the list elements. */
2783 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002784 if (Py_EnterRecursiveCall(" while pickling an object"))
2785 goto error;
2786 status = batch_list_exact(self, obj);
2787 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002788 } else {
2789 PyObject *iter = PyObject_GetIter(obj);
2790 if (iter == NULL)
2791 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002792
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002793 if (Py_EnterRecursiveCall(" while pickling an object")) {
2794 Py_DECREF(iter);
2795 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002796 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002797 status = batch_list(self, iter);
2798 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002799 Py_DECREF(iter);
2800 }
2801 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002802 if (0) {
2803 error:
2804 status = -1;
2805 }
2806
2807 if (self->fast && !fast_save_leave(self, obj))
2808 status = -1;
2809
2810 return status;
2811}
2812
2813/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2814 * MARK key value ... key value SETITEMS
2815 * opcode sequences. Calling code should have arranged to first create an
2816 * empty dict, or dict-like object, for the SETITEMS to operate on.
2817 * Returns 0 on success, <0 on error.
2818 *
2819 * This is very much like batch_list(). The difference between saving
2820 * elements directly, and picking apart two-tuples, is so long-winded at
2821 * the C level, though, that attempts to combine these routines were too
2822 * ugly to bear.
2823 */
2824static int
2825batch_dict(PicklerObject *self, PyObject *iter)
2826{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002827 PyObject *obj = NULL;
2828 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002829 int i, n;
2830
2831 const char mark_op = MARK;
2832 const char setitem_op = SETITEM;
2833 const char setitems_op = SETITEMS;
2834
2835 assert(iter != NULL);
2836
2837 if (self->proto == 0) {
2838 /* SETITEMS isn't available; do one at a time. */
2839 for (;;) {
2840 obj = PyIter_Next(iter);
2841 if (obj == NULL) {
2842 if (PyErr_Occurred())
2843 return -1;
2844 break;
2845 }
2846 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2847 PyErr_SetString(PyExc_TypeError, "dict items "
2848 "iterator must return 2-tuples");
2849 return -1;
2850 }
2851 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2852 if (i >= 0)
2853 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2854 Py_DECREF(obj);
2855 if (i < 0)
2856 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002857 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002858 return -1;
2859 }
2860 return 0;
2861 }
2862
2863 /* proto > 0: write in batches of BATCHSIZE. */
2864 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002865 /* Get first item */
2866 firstitem = PyIter_Next(iter);
2867 if (firstitem == NULL) {
2868 if (PyErr_Occurred())
2869 goto error;
2870
2871 /* nothing more to add */
2872 break;
2873 }
2874 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2875 PyErr_SetString(PyExc_TypeError, "dict items "
2876 "iterator must return 2-tuples");
2877 goto error;
2878 }
2879
2880 /* Try to get a second item */
2881 obj = PyIter_Next(iter);
2882 if (obj == NULL) {
2883 if (PyErr_Occurred())
2884 goto error;
2885
2886 /* Only one item to write */
2887 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2888 goto error;
2889 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2890 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002891 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002892 goto error;
2893 Py_CLEAR(firstitem);
2894 break;
2895 }
2896
2897 /* More than one item to write */
2898
2899 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002900 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002901 goto error;
2902
2903 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2904 goto error;
2905 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2906 goto error;
2907 Py_CLEAR(firstitem);
2908 n = 1;
2909
2910 /* Fetch and save up to BATCHSIZE items */
2911 while (obj) {
2912 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2913 PyErr_SetString(PyExc_TypeError, "dict items "
2914 "iterator must return 2-tuples");
2915 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002916 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002917 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2918 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2919 goto error;
2920 Py_CLEAR(obj);
2921 n += 1;
2922
2923 if (n == BATCHSIZE)
2924 break;
2925
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002926 obj = PyIter_Next(iter);
2927 if (obj == NULL) {
2928 if (PyErr_Occurred())
2929 goto error;
2930 break;
2931 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002932 }
2933
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002934 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002935 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002936
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002937 } while (n == BATCHSIZE);
2938 return 0;
2939
2940 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002941 Py_XDECREF(firstitem);
2942 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002943 return -1;
2944}
2945
Collin Winter5c9b02d2009-05-25 05:43:30 +00002946/* This is a variant of batch_dict() above that specializes for dicts, with no
2947 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2948 * MARK key value ... key value SETITEMS
2949 * opcode sequences. Calling code should have arranged to first create an
2950 * empty dict, or dict-like object, for the SETITEMS to operate on.
2951 * Returns 0 on success, -1 on error.
2952 *
2953 * Note that this currently doesn't work for protocol 0.
2954 */
2955static int
2956batch_dict_exact(PicklerObject *self, PyObject *obj)
2957{
2958 PyObject *key = NULL, *value = NULL;
2959 int i;
2960 Py_ssize_t dict_size, ppos = 0;
2961
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002962 const char mark_op = MARK;
2963 const char setitem_op = SETITEM;
2964 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002965
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002966 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002967 assert(self->proto > 0);
2968
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002969 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002970
2971 /* Special-case len(d) == 1 to save space. */
2972 if (dict_size == 1) {
2973 PyDict_Next(obj, &ppos, &key, &value);
2974 if (save(self, key, 0) < 0)
2975 return -1;
2976 if (save(self, value, 0) < 0)
2977 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002978 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002979 return -1;
2980 return 0;
2981 }
2982
2983 /* Write in batches of BATCHSIZE. */
2984 do {
2985 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002986 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002987 return -1;
2988 while (PyDict_Next(obj, &ppos, &key, &value)) {
2989 if (save(self, key, 0) < 0)
2990 return -1;
2991 if (save(self, value, 0) < 0)
2992 return -1;
2993 if (++i == BATCHSIZE)
2994 break;
2995 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002996 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002997 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002998 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00002999 PyErr_Format(
3000 PyExc_RuntimeError,
3001 "dictionary changed size during iteration");
3002 return -1;
3003 }
3004
3005 } while (i == BATCHSIZE);
3006 return 0;
3007}
3008
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003009static int
3010save_dict(PicklerObject *self, PyObject *obj)
3011{
3012 PyObject *items, *iter;
3013 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003014 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003015 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003016 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003017
3018 if (self->fast && !fast_save_enter(self, obj))
3019 goto error;
3020
3021 /* Create an empty dict. */
3022 if (self->bin) {
3023 header[0] = EMPTY_DICT;
3024 len = 1;
3025 }
3026 else {
3027 header[0] = MARK;
3028 header[1] = DICT;
3029 len = 2;
3030 }
3031
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003032 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003033 goto error;
3034
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003035 if (memo_put(self, obj) < 0)
3036 goto error;
3037
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003038 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003039 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003040 if (PyDict_CheckExact(obj) && self->proto > 0) {
3041 /* We can take certain shortcuts if we know this is a dict and
3042 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003043 if (Py_EnterRecursiveCall(" while pickling an object"))
3044 goto error;
3045 status = batch_dict_exact(self, obj);
3046 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003047 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003048 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003049
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003050 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003051 if (items == NULL)
3052 goto error;
3053 iter = PyObject_GetIter(items);
3054 Py_DECREF(items);
3055 if (iter == NULL)
3056 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003057 if (Py_EnterRecursiveCall(" while pickling an object")) {
3058 Py_DECREF(iter);
3059 goto error;
3060 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003061 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003062 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003063 Py_DECREF(iter);
3064 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003065 }
3066
3067 if (0) {
3068 error:
3069 status = -1;
3070 }
3071
3072 if (self->fast && !fast_save_leave(self, obj))
3073 status = -1;
3074
3075 return status;
3076}
3077
3078static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003079save_set(PicklerObject *self, PyObject *obj)
3080{
3081 PyObject *item;
3082 int i;
3083 Py_ssize_t set_size, ppos = 0;
3084 Py_hash_t hash;
3085
3086 const char empty_set_op = EMPTY_SET;
3087 const char mark_op = MARK;
3088 const char additems_op = ADDITEMS;
3089
3090 if (self->proto < 4) {
3091 PyObject *items;
3092 PyObject *reduce_value;
3093 int status;
3094
3095 items = PySequence_List(obj);
3096 if (items == NULL) {
3097 return -1;
3098 }
3099 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3100 Py_DECREF(items);
3101 if (reduce_value == NULL) {
3102 return -1;
3103 }
3104 /* save_reduce() will memoize the object automatically. */
3105 status = save_reduce(self, reduce_value, obj);
3106 Py_DECREF(reduce_value);
3107 return status;
3108 }
3109
3110 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3111 return -1;
3112
3113 if (memo_put(self, obj) < 0)
3114 return -1;
3115
3116 set_size = PySet_GET_SIZE(obj);
3117 if (set_size == 0)
3118 return 0; /* nothing to do */
3119
3120 /* Write in batches of BATCHSIZE. */
3121 do {
3122 i = 0;
3123 if (_Pickler_Write(self, &mark_op, 1) < 0)
3124 return -1;
3125 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3126 if (save(self, item, 0) < 0)
3127 return -1;
3128 if (++i == BATCHSIZE)
3129 break;
3130 }
3131 if (_Pickler_Write(self, &additems_op, 1) < 0)
3132 return -1;
3133 if (PySet_GET_SIZE(obj) != set_size) {
3134 PyErr_Format(
3135 PyExc_RuntimeError,
3136 "set changed size during iteration");
3137 return -1;
3138 }
3139 } while (i == BATCHSIZE);
3140
3141 return 0;
3142}
3143
3144static int
3145save_frozenset(PicklerObject *self, PyObject *obj)
3146{
3147 PyObject *iter;
3148
3149 const char mark_op = MARK;
3150 const char frozenset_op = FROZENSET;
3151
3152 if (self->fast && !fast_save_enter(self, obj))
3153 return -1;
3154
3155 if (self->proto < 4) {
3156 PyObject *items;
3157 PyObject *reduce_value;
3158 int status;
3159
3160 items = PySequence_List(obj);
3161 if (items == NULL) {
3162 return -1;
3163 }
3164 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3165 items);
3166 Py_DECREF(items);
3167 if (reduce_value == NULL) {
3168 return -1;
3169 }
3170 /* save_reduce() will memoize the object automatically. */
3171 status = save_reduce(self, reduce_value, obj);
3172 Py_DECREF(reduce_value);
3173 return status;
3174 }
3175
3176 if (_Pickler_Write(self, &mark_op, 1) < 0)
3177 return -1;
3178
3179 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003180 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003181 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003182 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003183 for (;;) {
3184 PyObject *item;
3185
3186 item = PyIter_Next(iter);
3187 if (item == NULL) {
3188 if (PyErr_Occurred()) {
3189 Py_DECREF(iter);
3190 return -1;
3191 }
3192 break;
3193 }
3194 if (save(self, item, 0) < 0) {
3195 Py_DECREF(item);
3196 Py_DECREF(iter);
3197 return -1;
3198 }
3199 Py_DECREF(item);
3200 }
3201 Py_DECREF(iter);
3202
3203 /* If the object is already in the memo, this means it is
3204 recursive. In this case, throw away everything we put on the
3205 stack, and fetch the object back from the memo. */
3206 if (PyMemoTable_Get(self->memo, obj)) {
3207 const char pop_mark_op = POP_MARK;
3208
3209 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3210 return -1;
3211 if (memo_get(self, obj) < 0)
3212 return -1;
3213 return 0;
3214 }
3215
3216 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3217 return -1;
3218 if (memo_put(self, obj) < 0)
3219 return -1;
3220
3221 return 0;
3222}
3223
3224static int
3225fix_imports(PyObject **module_name, PyObject **global_name)
3226{
3227 PyObject *key;
3228 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003229 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003230
3231 key = PyTuple_Pack(2, *module_name, *global_name);
3232 if (key == NULL)
3233 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003234 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003235 Py_DECREF(key);
3236 if (item) {
3237 PyObject *fixed_module_name;
3238 PyObject *fixed_global_name;
3239
3240 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3241 PyErr_Format(PyExc_RuntimeError,
3242 "_compat_pickle.REVERSE_NAME_MAPPING values "
3243 "should be 2-tuples, not %.200s",
3244 Py_TYPE(item)->tp_name);
3245 return -1;
3246 }
3247 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3248 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3249 if (!PyUnicode_Check(fixed_module_name) ||
3250 !PyUnicode_Check(fixed_global_name)) {
3251 PyErr_Format(PyExc_RuntimeError,
3252 "_compat_pickle.REVERSE_NAME_MAPPING values "
3253 "should be pairs of str, not (%.200s, %.200s)",
3254 Py_TYPE(fixed_module_name)->tp_name,
3255 Py_TYPE(fixed_global_name)->tp_name);
3256 return -1;
3257 }
3258
3259 Py_CLEAR(*module_name);
3260 Py_CLEAR(*global_name);
3261 Py_INCREF(fixed_module_name);
3262 Py_INCREF(fixed_global_name);
3263 *module_name = fixed_module_name;
3264 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003265 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003266 }
3267 else if (PyErr_Occurred()) {
3268 return -1;
3269 }
3270
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003271 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003272 if (item) {
3273 if (!PyUnicode_Check(item)) {
3274 PyErr_Format(PyExc_RuntimeError,
3275 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3276 "should be strings, not %.200s",
3277 Py_TYPE(item)->tp_name);
3278 return -1;
3279 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003280 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003281 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003282 }
3283 else if (PyErr_Occurred()) {
3284 return -1;
3285 }
3286
3287 return 0;
3288}
3289
3290static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003291save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3292{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003293 PyObject *global_name = NULL;
3294 PyObject *module_name = NULL;
3295 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003296 PyObject *parent = NULL;
3297 PyObject *dotted_path = NULL;
3298 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003299 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003300 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003301 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003302 _Py_IDENTIFIER(__name__);
3303 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003304
3305 const char global_op = GLOBAL;
3306
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003307 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003308 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003309 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003310 }
3311 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003312 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3313 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003314 if (global_name == NULL) {
3315 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3316 if (global_name == NULL)
3317 goto error;
3318 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003319 }
3320
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003321 dotted_path = get_dotted_path(module, global_name);
3322 if (dotted_path == NULL)
3323 goto error;
3324 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003325 if (module_name == NULL)
3326 goto error;
3327
3328 /* XXX: Change to use the import C API directly with level=0 to disallow
3329 relative imports.
3330
3331 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3332 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3333 custom import functions (IMHO, this would be a nice security
3334 feature). The import C API would need to be extended to support the
3335 extra parameters of __import__ to fix that. */
3336 module = PyImport_Import(module_name);
3337 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003338 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003339 "Can't pickle %R: import of module %R failed",
3340 obj, module_name);
3341 goto error;
3342 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003343 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3344 Py_INCREF(lastname);
3345 cls = get_deep_attribute(module, dotted_path, &parent);
3346 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003347 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003348 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003349 "Can't pickle %R: attribute lookup %S on %S failed",
3350 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003351 goto error;
3352 }
3353 if (cls != obj) {
3354 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003355 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003356 "Can't pickle %R: it's not the same object as %S.%S",
3357 obj, module_name, global_name);
3358 goto error;
3359 }
3360 Py_DECREF(cls);
3361
3362 if (self->proto >= 2) {
3363 /* See whether this is in the extension registry, and if
3364 * so generate an EXT opcode.
3365 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003366 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003367 PyObject *code_obj; /* extension code as Python object */
3368 long code; /* extension code as C value */
3369 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003370 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003371
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003372 extension_key = PyTuple_Pack(2, module_name, global_name);
3373 if (extension_key == NULL) {
3374 goto error;
3375 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003376 code_obj = PyDict_GetItemWithError(st->extension_registry,
3377 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003378 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003379 /* The object is not registered in the extension registry.
3380 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003381 if (code_obj == NULL) {
3382 if (PyErr_Occurred()) {
3383 goto error;
3384 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003385 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003386 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003387
3388 /* XXX: pickle.py doesn't check neither the type, nor the range
3389 of the value returned by the extension_registry. It should for
3390 consistency. */
3391
3392 /* Verify code_obj has the right type and value. */
3393 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003394 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003395 "Can't pickle %R: extension code %R isn't an integer",
3396 obj, code_obj);
3397 goto error;
3398 }
3399 code = PyLong_AS_LONG(code_obj);
3400 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003401 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003402 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3403 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003404 goto error;
3405 }
3406
3407 /* Generate an EXT opcode. */
3408 if (code <= 0xff) {
3409 pdata[0] = EXT1;
3410 pdata[1] = (unsigned char)code;
3411 n = 2;
3412 }
3413 else if (code <= 0xffff) {
3414 pdata[0] = EXT2;
3415 pdata[1] = (unsigned char)(code & 0xff);
3416 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3417 n = 3;
3418 }
3419 else {
3420 pdata[0] = EXT4;
3421 pdata[1] = (unsigned char)(code & 0xff);
3422 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3423 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3424 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3425 n = 5;
3426 }
3427
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003428 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003429 goto error;
3430 }
3431 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003432 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003433 if (parent == module) {
3434 Py_INCREF(lastname);
3435 Py_DECREF(global_name);
3436 global_name = lastname;
3437 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003438 if (self->proto >= 4) {
3439 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003440
Christian Heimese8b1ba12013-11-23 21:13:39 +01003441 if (save(self, module_name, 0) < 0)
3442 goto error;
3443 if (save(self, global_name, 0) < 0)
3444 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003445
3446 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3447 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003448 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003449 else if (parent != module) {
3450 PickleState *st = _Pickle_GetGlobalState();
3451 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3452 st->getattr, parent, lastname);
3453 status = save_reduce(self, reduce_value, NULL);
3454 Py_DECREF(reduce_value);
3455 if (status < 0)
3456 goto error;
3457 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003458 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003459 /* Generate a normal global opcode if we are using a pickle
3460 protocol < 4, or if the object is not registered in the
3461 extension registry. */
3462 PyObject *encoded;
3463 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003464
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003465 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003466 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003467
3468 /* For protocol < 3 and if the user didn't request against doing
3469 so, we convert module names to the old 2.x module names. */
3470 if (self->proto < 3 && self->fix_imports) {
3471 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003472 goto error;
3473 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003474 }
3475
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003476 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3477 both the module name and the global name using UTF-8. We do so
3478 only when we are using the pickle protocol newer than version
3479 3. This is to ensure compatibility with older Unpickler running
3480 on Python 2.x. */
3481 if (self->proto == 3) {
3482 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003483 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003484 else {
3485 unicode_encoder = PyUnicode_AsASCIIString;
3486 }
3487 encoded = unicode_encoder(module_name);
3488 if (encoded == NULL) {
3489 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003490 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003491 "can't pickle module identifier '%S' using "
3492 "pickle protocol %i",
3493 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003494 goto error;
3495 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003496 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3497 PyBytes_GET_SIZE(encoded)) < 0) {
3498 Py_DECREF(encoded);
3499 goto error;
3500 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003501 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003502 if(_Pickler_Write(self, "\n", 1) < 0)
3503 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003504
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003505 /* Save the name of the module. */
3506 encoded = unicode_encoder(global_name);
3507 if (encoded == NULL) {
3508 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003509 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003510 "can't pickle global identifier '%S' using "
3511 "pickle protocol %i",
3512 global_name, self->proto);
3513 goto error;
3514 }
3515 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3516 PyBytes_GET_SIZE(encoded)) < 0) {
3517 Py_DECREF(encoded);
3518 goto error;
3519 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003520 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003521 if (_Pickler_Write(self, "\n", 1) < 0)
3522 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003523 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524 /* Memoize the object. */
3525 if (memo_put(self, obj) < 0)
3526 goto error;
3527 }
3528
3529 if (0) {
3530 error:
3531 status = -1;
3532 }
3533 Py_XDECREF(module_name);
3534 Py_XDECREF(global_name);
3535 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003536 Py_XDECREF(parent);
3537 Py_XDECREF(dotted_path);
3538 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003539
3540 return status;
3541}
3542
3543static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003544save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3545{
3546 PyObject *reduce_value;
3547 int status;
3548
3549 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3550 if (reduce_value == NULL) {
3551 return -1;
3552 }
3553 status = save_reduce(self, reduce_value, obj);
3554 Py_DECREF(reduce_value);
3555 return status;
3556}
3557
3558static int
3559save_type(PicklerObject *self, PyObject *obj)
3560{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003561 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003562 return save_singleton_type(self, obj, Py_None);
3563 }
3564 else if (obj == (PyObject *)&PyEllipsis_Type) {
3565 return save_singleton_type(self, obj, Py_Ellipsis);
3566 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003567 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003568 return save_singleton_type(self, obj, Py_NotImplemented);
3569 }
3570 return save_global(self, obj, NULL);
3571}
3572
3573static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003574save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003575{
3576 PyObject *pid = NULL;
3577 int status = 0;
3578
3579 const char persid_op = PERSID;
3580 const char binpersid_op = BINPERSID;
3581
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003582 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003583 if (pid == NULL)
3584 return -1;
3585
3586 if (pid != Py_None) {
3587 if (self->bin) {
3588 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003589 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003590 goto error;
3591 }
3592 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003593 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003594
3595 pid_str = PyObject_Str(pid);
3596 if (pid_str == NULL)
3597 goto error;
3598
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003599 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003600 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003601 if (!PyUnicode_IS_ASCII(pid_str)) {
3602 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3603 "persistent IDs in protocol 0 must be "
3604 "ASCII strings");
3605 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003606 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003607 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003608
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003609 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003610 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3611 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3612 _Pickler_Write(self, "\n", 1) < 0) {
3613 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003614 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003615 }
3616 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003617 }
3618 status = 1;
3619 }
3620
3621 if (0) {
3622 error:
3623 status = -1;
3624 }
3625 Py_XDECREF(pid);
3626
3627 return status;
3628}
3629
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003630static PyObject *
3631get_class(PyObject *obj)
3632{
3633 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003634 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003635
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003636 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3637 cls = (PyObject *) Py_TYPE(obj);
3638 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003639 }
3640 return cls;
3641}
3642
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003643/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3644 * appropriate __reduce__ method for obj.
3645 */
3646static int
3647save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3648{
3649 PyObject *callable;
3650 PyObject *argtup;
3651 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003652 PyObject *listitems = Py_None;
3653 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003654 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003655 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003656 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003657
3658 const char reduce_op = REDUCE;
3659 const char build_op = BUILD;
3660 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003661 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003662
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003663 size = PyTuple_Size(args);
3664 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003665 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003666 "__reduce__ must contain 2 through 5 elements");
3667 return -1;
3668 }
3669
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003670 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3671 &callable, &argtup, &state, &listitems, &dictitems))
3672 return -1;
3673
3674 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003675 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003676 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003677 return -1;
3678 }
3679 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003680 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003681 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003682 return -1;
3683 }
3684
3685 if (state == Py_None)
3686 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003687
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003688 if (listitems == Py_None)
3689 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003690 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003691 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003692 "returned by __reduce__ must be an iterator, not %s",
3693 Py_TYPE(listitems)->tp_name);
3694 return -1;
3695 }
3696
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003697 if (dictitems == Py_None)
3698 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003699 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003700 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003701 "returned by __reduce__ must be an iterator, not %s",
3702 Py_TYPE(dictitems)->tp_name);
3703 return -1;
3704 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003705
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003706 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003707 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003708 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003709
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003710 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3711 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003712 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003713 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003714 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003715 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3716 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003717 if (!use_newobj_ex) {
3718 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003719 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003720 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003721 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003722 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003723 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003724
3725 if (use_newobj_ex) {
3726 PyObject *cls;
3727 PyObject *args;
3728 PyObject *kwargs;
3729
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003730 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003731 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003732 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003733 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003734 return -1;
3735 }
3736
3737 cls = PyTuple_GET_ITEM(argtup, 0);
3738 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003739 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003740 "first item from NEWOBJ_EX argument tuple must "
3741 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3742 return -1;
3743 }
3744 args = PyTuple_GET_ITEM(argtup, 1);
3745 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003746 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003747 "second item from NEWOBJ_EX argument tuple must "
3748 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3749 return -1;
3750 }
3751 kwargs = PyTuple_GET_ITEM(argtup, 2);
3752 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003753 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003754 "third item from NEWOBJ_EX argument tuple must "
3755 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3756 return -1;
3757 }
3758
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003759 if (self->proto >= 4) {
3760 if (save(self, cls, 0) < 0 ||
3761 save(self, args, 0) < 0 ||
3762 save(self, kwargs, 0) < 0 ||
3763 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3764 return -1;
3765 }
3766 }
3767 else {
3768 PyObject *newargs;
3769 PyObject *cls_new;
3770 Py_ssize_t i;
3771 _Py_IDENTIFIER(__new__);
3772
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003773 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003774 if (newargs == NULL)
3775 return -1;
3776
3777 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3778 if (cls_new == NULL) {
3779 Py_DECREF(newargs);
3780 return -1;
3781 }
3782 PyTuple_SET_ITEM(newargs, 0, cls_new);
3783 Py_INCREF(cls);
3784 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003785 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003786 PyObject *item = PyTuple_GET_ITEM(args, i);
3787 Py_INCREF(item);
3788 PyTuple_SET_ITEM(newargs, i + 2, item);
3789 }
3790
3791 callable = PyObject_Call(st->partial, newargs, kwargs);
3792 Py_DECREF(newargs);
3793 if (callable == NULL)
3794 return -1;
3795
3796 newargs = PyTuple_New(0);
3797 if (newargs == NULL) {
3798 Py_DECREF(callable);
3799 return -1;
3800 }
3801
3802 if (save(self, callable, 0) < 0 ||
3803 save(self, newargs, 0) < 0 ||
3804 _Pickler_Write(self, &reduce_op, 1) < 0) {
3805 Py_DECREF(newargs);
3806 Py_DECREF(callable);
3807 return -1;
3808 }
3809 Py_DECREF(newargs);
3810 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003811 }
3812 }
3813 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003814 PyObject *cls;
3815 PyObject *newargtup;
3816 PyObject *obj_class;
3817 int p;
3818
3819 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003820 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003821 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003822 return -1;
3823 }
3824
3825 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003826 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003827 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003828 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003829 return -1;
3830 }
3831
3832 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003833 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003834 p = obj_class != cls; /* true iff a problem */
3835 Py_DECREF(obj_class);
3836 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003837 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003838 "__newobj__ args has the wrong class");
3839 return -1;
3840 }
3841 }
3842 /* XXX: These calls save() are prone to infinite recursion. Imagine
3843 what happen if the value returned by the __reduce__() method of
3844 some extension type contains another object of the same type. Ouch!
3845
3846 Here is a quick example, that I ran into, to illustrate what I
3847 mean:
3848
3849 >>> import pickle, copyreg
3850 >>> copyreg.dispatch_table.pop(complex)
3851 >>> pickle.dumps(1+2j)
3852 Traceback (most recent call last):
3853 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003854 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003855
3856 Removing the complex class from copyreg.dispatch_table made the
3857 __reduce_ex__() method emit another complex object:
3858
3859 >>> (1+1j).__reduce_ex__(2)
3860 (<function __newobj__ at 0xb7b71c3c>,
3861 (<class 'complex'>, (1+1j)), None, None, None)
3862
3863 Thus when save() was called on newargstup (the 2nd item) recursion
3864 ensued. Of course, the bug was in the complex class which had a
3865 broken __getnewargs__() that emitted another complex object. But,
3866 the point, here, is it is quite easy to end up with a broken reduce
3867 function. */
3868
3869 /* Save the class and its __new__ arguments. */
3870 if (save(self, cls, 0) < 0)
3871 return -1;
3872
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003873 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003874 if (newargtup == NULL)
3875 return -1;
3876
3877 p = save(self, newargtup, 0);
3878 Py_DECREF(newargtup);
3879 if (p < 0)
3880 return -1;
3881
3882 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003883 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003884 return -1;
3885 }
3886 else { /* Not using NEWOBJ. */
3887 if (save(self, callable, 0) < 0 ||
3888 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003889 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003890 return -1;
3891 }
3892
3893 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3894 the caller do not want to memoize the object. Not particularly useful,
3895 but that is to mimic the behavior save_reduce() in pickle.py when
3896 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003897 if (obj != NULL) {
3898 /* If the object is already in the memo, this means it is
3899 recursive. In this case, throw away everything we put on the
3900 stack, and fetch the object back from the memo. */
3901 if (PyMemoTable_Get(self->memo, obj)) {
3902 const char pop_op = POP;
3903
3904 if (_Pickler_Write(self, &pop_op, 1) < 0)
3905 return -1;
3906 if (memo_get(self, obj) < 0)
3907 return -1;
3908
3909 return 0;
3910 }
3911 else if (memo_put(self, obj) < 0)
3912 return -1;
3913 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003914
3915 if (listitems && batch_list(self, listitems) < 0)
3916 return -1;
3917
3918 if (dictitems && batch_dict(self, dictitems) < 0)
3919 return -1;
3920
3921 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003922 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003923 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003924 return -1;
3925 }
3926
3927 return 0;
3928}
3929
3930static int
3931save(PicklerObject *self, PyObject *obj, int pers_save)
3932{
3933 PyTypeObject *type;
3934 PyObject *reduce_func = NULL;
3935 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003936 int status = 0;
3937
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003938 if (_Pickler_OpcodeBoundary(self) < 0)
3939 return -1;
3940
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003941 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003942 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943
3944 /* The extra pers_save argument is necessary to avoid calling save_pers()
3945 on its returned object. */
3946 if (!pers_save && self->pers_func) {
3947 /* save_pers() returns:
3948 -1 to signal an error;
3949 0 if it did nothing successfully;
3950 1 if a persistent id was saved.
3951 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003952 if ((status = save_pers(self, obj)) != 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003953 goto done;
3954 }
3955
3956 type = Py_TYPE(obj);
3957
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003958 /* The old cPickle had an optimization that used switch-case statement
3959 dispatching on the first letter of the type name. This has was removed
3960 since benchmarks shown that this optimization was actually slowing
3961 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003962
3963 /* Atom types; these aren't memoized, so don't check the memo. */
3964
3965 if (obj == Py_None) {
3966 status = save_none(self, obj);
3967 goto done;
3968 }
3969 else if (obj == Py_False || obj == Py_True) {
3970 status = save_bool(self, obj);
3971 goto done;
3972 }
3973 else if (type == &PyLong_Type) {
3974 status = save_long(self, obj);
3975 goto done;
3976 }
3977 else if (type == &PyFloat_Type) {
3978 status = save_float(self, obj);
3979 goto done;
3980 }
3981
3982 /* Check the memo to see if it has the object. If so, generate
3983 a GET (or BINGET) opcode, instead of pickling the object
3984 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003985 if (PyMemoTable_Get(self->memo, obj)) {
3986 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003987 goto error;
3988 goto done;
3989 }
3990
3991 if (type == &PyBytes_Type) {
3992 status = save_bytes(self, obj);
3993 goto done;
3994 }
3995 else if (type == &PyUnicode_Type) {
3996 status = save_unicode(self, obj);
3997 goto done;
3998 }
3999 else if (type == &PyDict_Type) {
4000 status = save_dict(self, obj);
4001 goto done;
4002 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004003 else if (type == &PySet_Type) {
4004 status = save_set(self, obj);
4005 goto done;
4006 }
4007 else if (type == &PyFrozenSet_Type) {
4008 status = save_frozenset(self, obj);
4009 goto done;
4010 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004011 else if (type == &PyList_Type) {
4012 status = save_list(self, obj);
4013 goto done;
4014 }
4015 else if (type == &PyTuple_Type) {
4016 status = save_tuple(self, obj);
4017 goto done;
4018 }
4019 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004020 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004021 goto done;
4022 }
4023 else if (type == &PyFunction_Type) {
4024 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004025 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004026 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004027
4028 /* XXX: This part needs some unit tests. */
4029
4030 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004031 * self.dispatch_table, copyreg.dispatch_table, the object's
4032 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004033 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004034 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004035 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004036 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4037 (PyObject *)type);
4038 if (reduce_func == NULL) {
4039 if (PyErr_Occurred()) {
4040 goto error;
4041 }
4042 } else {
4043 /* PyDict_GetItemWithError() returns a borrowed reference.
4044 Increase the reference count to be consistent with
4045 PyObject_GetItem and _PyObject_GetAttrId used below. */
4046 Py_INCREF(reduce_func);
4047 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004048 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004049 reduce_func = PyObject_GetItem(self->dispatch_table,
4050 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004051 if (reduce_func == NULL) {
4052 if (PyErr_ExceptionMatches(PyExc_KeyError))
4053 PyErr_Clear();
4054 else
4055 goto error;
4056 }
4057 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004058 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004059 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004060 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004061 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004062 else if (PyType_IsSubtype(type, &PyType_Type)) {
4063 status = save_global(self, obj, NULL);
4064 goto done;
4065 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004066 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004067 _Py_IDENTIFIER(__reduce__);
4068 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004069
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004070
4071 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4072 automatically defined as __reduce__. While this is convenient, this
4073 make it impossible to know which method was actually called. Of
4074 course, this is not a big deal. But still, it would be nice to let
4075 the user know which method was called when something go
4076 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4077 don't actually have to check for a __reduce__ method. */
4078
4079 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004080 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4081 goto error;
4082 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004083 if (reduce_func != NULL) {
4084 PyObject *proto;
4085 proto = PyLong_FromLong(self->proto);
4086 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004087 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004088 }
4089 }
4090 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004091 PickleState *st = _Pickle_GetGlobalState();
4092
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004094 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004095 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004096 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004097 }
4098 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004099 PyErr_Format(st->PicklingError,
4100 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004101 type->tp_name, obj);
4102 goto error;
4103 }
4104 }
4105 }
4106
4107 if (reduce_value == NULL)
4108 goto error;
4109
4110 if (PyUnicode_Check(reduce_value)) {
4111 status = save_global(self, obj, reduce_value);
4112 goto done;
4113 }
4114
4115 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004116 PickleState *st = _Pickle_GetGlobalState();
4117 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004118 "__reduce__ must return a string or tuple");
4119 goto error;
4120 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004121
4122 status = save_reduce(self, reduce_value, obj);
4123
4124 if (0) {
4125 error:
4126 status = -1;
4127 }
4128 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004129
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004130 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004131 Py_XDECREF(reduce_func);
4132 Py_XDECREF(reduce_value);
4133
4134 return status;
4135}
4136
4137static int
4138dump(PicklerObject *self, PyObject *obj)
4139{
4140 const char stop_op = STOP;
4141
4142 if (self->proto >= 2) {
4143 char header[2];
4144
4145 header[0] = PROTO;
4146 assert(self->proto >= 0 && self->proto < 256);
4147 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004148 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004149 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004150 if (self->proto >= 4)
4151 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004152 }
4153
4154 if (save(self, obj, 0) < 0 ||
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004155 _Pickler_Write(self, &stop_op, 1) < 0 ||
4156 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004157 return -1;
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004158 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004159 return 0;
4160}
4161
Larry Hastings61272b72014-01-07 12:41:53 -08004162/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004163
4164_pickle.Pickler.clear_memo
4165
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004166Clears the pickler's "memo".
4167
4168The memo is the data structure that remembers which objects the
4169pickler has already seen, so that shared or recursive objects are
4170pickled by reference and not by value. This method is useful when
4171re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004172[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004173
Larry Hastings3cceb382014-01-04 11:09:09 -08004174static PyObject *
4175_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004176/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004177{
4178 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004179 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004180
4181 Py_RETURN_NONE;
4182}
4183
Larry Hastings61272b72014-01-07 12:41:53 -08004184/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004185
4186_pickle.Pickler.dump
4187
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004188 obj: object
4189 /
4190
4191Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004192[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004193
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004194static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004195_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004196/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004197{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004198 /* Check whether the Pickler was initialized correctly (issue3664).
4199 Developers often forget to call __init__() in their subclasses, which
4200 would trigger a segfault without this check. */
4201 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004202 PickleState *st = _Pickle_GetGlobalState();
4203 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004204 "Pickler.__init__() was not called by %s.__init__()",
4205 Py_TYPE(self)->tp_name);
4206 return NULL;
4207 }
4208
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004209 if (_Pickler_ClearBuffer(self) < 0)
4210 return NULL;
4211
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004212 if (dump(self, obj) < 0)
4213 return NULL;
4214
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004215 if (_Pickler_FlushToFile(self) < 0)
4216 return NULL;
4217
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004218 Py_RETURN_NONE;
4219}
4220
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004221/*[clinic input]
4222
4223_pickle.Pickler.__sizeof__ -> Py_ssize_t
4224
4225Returns size in memory, in bytes.
4226[clinic start generated code]*/
4227
4228static Py_ssize_t
4229_pickle_Pickler___sizeof___impl(PicklerObject *self)
4230/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4231{
4232 Py_ssize_t res, s;
4233
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004234 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004235 if (self->memo != NULL) {
4236 res += sizeof(PyMemoTable);
4237 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4238 }
4239 if (self->output_buffer != NULL) {
4240 s = _PySys_GetSizeOf(self->output_buffer);
4241 if (s == -1)
4242 return -1;
4243 res += s;
4244 }
4245 return res;
4246}
4247
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004248static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004249 _PICKLE_PICKLER_DUMP_METHODDEF
4250 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004251 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004252 {NULL, NULL} /* sentinel */
4253};
4254
4255static void
4256Pickler_dealloc(PicklerObject *self)
4257{
4258 PyObject_GC_UnTrack(self);
4259
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004260 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004261 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004262 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004263 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004264 Py_XDECREF(self->fast_memo);
4265
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004266 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004267
4268 Py_TYPE(self)->tp_free((PyObject *)self);
4269}
4270
4271static int
4272Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4273{
4274 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004275 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004276 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004277 Py_VISIT(self->fast_memo);
4278 return 0;
4279}
4280
4281static int
4282Pickler_clear(PicklerObject *self)
4283{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004284 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004285 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004286 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004287 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004288 Py_CLEAR(self->fast_memo);
4289
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004290 if (self->memo != NULL) {
4291 PyMemoTable *memo = self->memo;
4292 self->memo = NULL;
4293 PyMemoTable_Del(memo);
4294 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004295 return 0;
4296}
4297
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004298
Larry Hastings61272b72014-01-07 12:41:53 -08004299/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004300
4301_pickle.Pickler.__init__
4302
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004303 file: object
4304 protocol: object = NULL
4305 fix_imports: bool = True
4306
4307This takes a binary file for writing a pickle data stream.
4308
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004309The optional *protocol* argument tells the pickler to use the given
4310protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4311protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004312
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004313Specifying a negative protocol version selects the highest protocol
4314version supported. The higher the protocol used, the more recent the
4315version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004316
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004317The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004318bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004319writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004320this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004321
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004322If *fix_imports* is True and protocol is less than 3, pickle will try
4323to map the new Python 3 names to the old module names used in Python
43242, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004325[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004326
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004327static int
Larry Hastings89964c42015-04-14 18:07:59 -04004328_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4329 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004330/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004331{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004332 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004333 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004334
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004335 /* In case of multiple __init__() calls, clear previous content. */
4336 if (self->write != NULL)
4337 (void)Pickler_clear(self);
4338
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004339 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004340 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004341
4342 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004343 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004344
4345 /* memo and output_buffer may have already been created in _Pickler_New */
4346 if (self->memo == NULL) {
4347 self->memo = PyMemoTable_New();
4348 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004349 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004350 }
4351 self->output_len = 0;
4352 if (self->output_buffer == NULL) {
4353 self->max_output_len = WRITE_BUF_SIZE;
4354 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4355 self->max_output_len);
4356 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004357 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004358 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004359
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004360 self->fast = 0;
4361 self->fast_nesting = 0;
4362 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004363
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004364 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4365 &self->pers_func, &self->pers_func_self) < 0)
4366 {
4367 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004368 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004369
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004370 if (_PyObject_LookupAttrId((PyObject *)self,
4371 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4372 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004373 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004374
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004375 return 0;
4376}
4377
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004378
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004379/* Define a proxy object for the Pickler's internal memo object. This is to
4380 * avoid breaking code like:
4381 * pickler.memo.clear()
4382 * and
4383 * pickler.memo = saved_memo
4384 * Is this a good idea? Not really, but we don't want to break code that uses
4385 * it. Note that we don't implement the entire mapping API here. This is
4386 * intentional, as these should be treated as black-box implementation details.
4387 */
4388
Larry Hastings61272b72014-01-07 12:41:53 -08004389/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004390_pickle.PicklerMemoProxy.clear
4391
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004392Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004393[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004394
Larry Hastings3cceb382014-01-04 11:09:09 -08004395static PyObject *
4396_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004397/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004398{
4399 if (self->pickler->memo)
4400 PyMemoTable_Clear(self->pickler->memo);
4401 Py_RETURN_NONE;
4402}
4403
Larry Hastings61272b72014-01-07 12:41:53 -08004404/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004405_pickle.PicklerMemoProxy.copy
4406
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004407Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004408[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004409
Larry Hastings3cceb382014-01-04 11:09:09 -08004410static PyObject *
4411_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004412/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004413{
4414 Py_ssize_t i;
4415 PyMemoTable *memo;
4416 PyObject *new_memo = PyDict_New();
4417 if (new_memo == NULL)
4418 return NULL;
4419
4420 memo = self->pickler->memo;
4421 for (i = 0; i < memo->mt_allocated; ++i) {
4422 PyMemoEntry entry = memo->mt_table[i];
4423 if (entry.me_key != NULL) {
4424 int status;
4425 PyObject *key, *value;
4426
4427 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004428 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004429
4430 if (key == NULL || value == NULL) {
4431 Py_XDECREF(key);
4432 Py_XDECREF(value);
4433 goto error;
4434 }
4435 status = PyDict_SetItem(new_memo, key, value);
4436 Py_DECREF(key);
4437 Py_DECREF(value);
4438 if (status < 0)
4439 goto error;
4440 }
4441 }
4442 return new_memo;
4443
4444 error:
4445 Py_XDECREF(new_memo);
4446 return NULL;
4447}
4448
Larry Hastings61272b72014-01-07 12:41:53 -08004449/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004450_pickle.PicklerMemoProxy.__reduce__
4451
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004452Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004453[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004454
Larry Hastings3cceb382014-01-04 11:09:09 -08004455static PyObject *
4456_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004457/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004458{
4459 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004460 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004461 if (contents == NULL)
4462 return NULL;
4463
4464 reduce_value = PyTuple_New(2);
4465 if (reduce_value == NULL) {
4466 Py_DECREF(contents);
4467 return NULL;
4468 }
4469 dict_args = PyTuple_New(1);
4470 if (dict_args == NULL) {
4471 Py_DECREF(contents);
4472 Py_DECREF(reduce_value);
4473 return NULL;
4474 }
4475 PyTuple_SET_ITEM(dict_args, 0, contents);
4476 Py_INCREF((PyObject *)&PyDict_Type);
4477 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4478 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4479 return reduce_value;
4480}
4481
4482static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004483 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4484 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4485 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004486 {NULL, NULL} /* sentinel */
4487};
4488
4489static void
4490PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4491{
4492 PyObject_GC_UnTrack(self);
4493 Py_XDECREF(self->pickler);
4494 PyObject_GC_Del((PyObject *)self);
4495}
4496
4497static int
4498PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4499 visitproc visit, void *arg)
4500{
4501 Py_VISIT(self->pickler);
4502 return 0;
4503}
4504
4505static int
4506PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4507{
4508 Py_CLEAR(self->pickler);
4509 return 0;
4510}
4511
4512static PyTypeObject PicklerMemoProxyType = {
4513 PyVarObject_HEAD_INIT(NULL, 0)
4514 "_pickle.PicklerMemoProxy", /*tp_name*/
4515 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4516 0,
4517 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4518 0, /* tp_print */
4519 0, /* tp_getattr */
4520 0, /* tp_setattr */
4521 0, /* tp_compare */
4522 0, /* tp_repr */
4523 0, /* tp_as_number */
4524 0, /* tp_as_sequence */
4525 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004526 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004527 0, /* tp_call */
4528 0, /* tp_str */
4529 PyObject_GenericGetAttr, /* tp_getattro */
4530 PyObject_GenericSetAttr, /* tp_setattro */
4531 0, /* tp_as_buffer */
4532 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4533 0, /* tp_doc */
4534 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4535 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4536 0, /* tp_richcompare */
4537 0, /* tp_weaklistoffset */
4538 0, /* tp_iter */
4539 0, /* tp_iternext */
4540 picklerproxy_methods, /* tp_methods */
4541};
4542
4543static PyObject *
4544PicklerMemoProxy_New(PicklerObject *pickler)
4545{
4546 PicklerMemoProxyObject *self;
4547
4548 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4549 if (self == NULL)
4550 return NULL;
4551 Py_INCREF(pickler);
4552 self->pickler = pickler;
4553 PyObject_GC_Track(self);
4554 return (PyObject *)self;
4555}
4556
4557/*****************************************************************************/
4558
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004559static PyObject *
4560Pickler_get_memo(PicklerObject *self)
4561{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004562 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004563}
4564
4565static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004566Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004567{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004568 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004569
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004570 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004571 PyErr_SetString(PyExc_TypeError,
4572 "attribute deletion is not supported");
4573 return -1;
4574 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004575
4576 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4577 PicklerObject *pickler =
4578 ((PicklerMemoProxyObject *)obj)->pickler;
4579
4580 new_memo = PyMemoTable_Copy(pickler->memo);
4581 if (new_memo == NULL)
4582 return -1;
4583 }
4584 else if (PyDict_Check(obj)) {
4585 Py_ssize_t i = 0;
4586 PyObject *key, *value;
4587
4588 new_memo = PyMemoTable_New();
4589 if (new_memo == NULL)
4590 return -1;
4591
4592 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004593 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004594 PyObject *memo_obj;
4595
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004596 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004597 PyErr_SetString(PyExc_TypeError,
4598 "'memo' values must be 2-item tuples");
4599 goto error;
4600 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004601 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004602 if (memo_id == -1 && PyErr_Occurred())
4603 goto error;
4604 memo_obj = PyTuple_GET_ITEM(value, 1);
4605 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4606 goto error;
4607 }
4608 }
4609 else {
4610 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004611 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004612 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004613 return -1;
4614 }
4615
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004616 PyMemoTable_Del(self->memo);
4617 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004618
4619 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004620
4621 error:
4622 if (new_memo)
4623 PyMemoTable_Del(new_memo);
4624 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004625}
4626
4627static PyObject *
4628Pickler_get_persid(PicklerObject *self)
4629{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004630 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004631 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004632 return NULL;
4633 }
4634 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004635}
4636
4637static int
4638Pickler_set_persid(PicklerObject *self, PyObject *value)
4639{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004640 if (value == NULL) {
4641 PyErr_SetString(PyExc_TypeError,
4642 "attribute deletion is not supported");
4643 return -1;
4644 }
4645 if (!PyCallable_Check(value)) {
4646 PyErr_SetString(PyExc_TypeError,
4647 "persistent_id must be a callable taking one argument");
4648 return -1;
4649 }
4650
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004651 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004652 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004653 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004654
4655 return 0;
4656}
4657
4658static PyMemberDef Pickler_members[] = {
4659 {"bin", T_INT, offsetof(PicklerObject, bin)},
4660 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004661 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004662 {NULL}
4663};
4664
4665static PyGetSetDef Pickler_getsets[] = {
4666 {"memo", (getter)Pickler_get_memo,
4667 (setter)Pickler_set_memo},
4668 {"persistent_id", (getter)Pickler_get_persid,
4669 (setter)Pickler_set_persid},
4670 {NULL}
4671};
4672
4673static PyTypeObject Pickler_Type = {
4674 PyVarObject_HEAD_INIT(NULL, 0)
4675 "_pickle.Pickler" , /*tp_name*/
4676 sizeof(PicklerObject), /*tp_basicsize*/
4677 0, /*tp_itemsize*/
4678 (destructor)Pickler_dealloc, /*tp_dealloc*/
4679 0, /*tp_print*/
4680 0, /*tp_getattr*/
4681 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004682 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004683 0, /*tp_repr*/
4684 0, /*tp_as_number*/
4685 0, /*tp_as_sequence*/
4686 0, /*tp_as_mapping*/
4687 0, /*tp_hash*/
4688 0, /*tp_call*/
4689 0, /*tp_str*/
4690 0, /*tp_getattro*/
4691 0, /*tp_setattro*/
4692 0, /*tp_as_buffer*/
4693 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004694 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004695 (traverseproc)Pickler_traverse, /*tp_traverse*/
4696 (inquiry)Pickler_clear, /*tp_clear*/
4697 0, /*tp_richcompare*/
4698 0, /*tp_weaklistoffset*/
4699 0, /*tp_iter*/
4700 0, /*tp_iternext*/
4701 Pickler_methods, /*tp_methods*/
4702 Pickler_members, /*tp_members*/
4703 Pickler_getsets, /*tp_getset*/
4704 0, /*tp_base*/
4705 0, /*tp_dict*/
4706 0, /*tp_descr_get*/
4707 0, /*tp_descr_set*/
4708 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004709 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004710 PyType_GenericAlloc, /*tp_alloc*/
4711 PyType_GenericNew, /*tp_new*/
4712 PyObject_GC_Del, /*tp_free*/
4713 0, /*tp_is_gc*/
4714};
4715
Victor Stinner121aab42011-09-29 23:40:53 +02004716/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004717
4718 XXX: It would be nice to able to avoid Python function call overhead, by
4719 using directly the C version of find_class(), when find_class() is not
4720 overridden by a subclass. Although, this could become rather hackish. A
4721 simpler optimization would be to call the C function when self is not a
4722 subclass instance. */
4723static PyObject *
4724find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4725{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004726 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004727
Victor Stinner55ba38a2016-12-09 16:09:30 +01004728 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4729 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004730}
4731
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004732static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733marker(UnpicklerObject *self)
4734{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004735 Py_ssize_t mark;
4736
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004737 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004738 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004739 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004740 return -1;
4741 }
4742
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004743 mark = self->marks[--self->num_marks];
4744 self->stack->mark_set = self->num_marks != 0;
4745 self->stack->fence = self->num_marks ?
4746 self->marks[self->num_marks - 1] : 0;
4747 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004748}
4749
4750static int
4751load_none(UnpicklerObject *self)
4752{
4753 PDATA_APPEND(self->stack, Py_None, -1);
4754 return 0;
4755}
4756
4757static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004758load_int(UnpicklerObject *self)
4759{
4760 PyObject *value;
4761 char *endptr, *s;
4762 Py_ssize_t len;
4763 long x;
4764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004765 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004766 return -1;
4767 if (len < 2)
4768 return bad_readline();
4769
4770 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004771 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004772 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004773 x = strtol(s, &endptr, 0);
4774
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004775 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004776 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004777 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004778 errno = 0;
4779 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004780 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004781 if (value == NULL) {
4782 PyErr_SetString(PyExc_ValueError,
4783 "could not convert string to int");
4784 return -1;
4785 }
4786 }
4787 else {
4788 if (len == 3 && (x == 0 || x == 1)) {
4789 if ((value = PyBool_FromLong(x)) == NULL)
4790 return -1;
4791 }
4792 else {
4793 if ((value = PyLong_FromLong(x)) == NULL)
4794 return -1;
4795 }
4796 }
4797
4798 PDATA_PUSH(self->stack, value, -1);
4799 return 0;
4800}
4801
4802static int
4803load_bool(UnpicklerObject *self, PyObject *boolean)
4804{
4805 assert(boolean == Py_True || boolean == Py_False);
4806 PDATA_APPEND(self->stack, boolean, -1);
4807 return 0;
4808}
4809
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004810/* s contains x bytes of an unsigned little-endian integer. Return its value
4811 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4812 */
4813static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004814calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004815{
4816 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004817 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004818 size_t x = 0;
4819
Serhiy Storchakae0606192015-09-29 22:10:07 +03004820 if (nbytes > (int)sizeof(size_t)) {
4821 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4822 * have 64-bit size that can't be represented on 32-bit platform.
4823 */
4824 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4825 if (s[i])
4826 return -1;
4827 }
4828 nbytes = (int)sizeof(size_t);
4829 }
4830 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004831 x |= (size_t) s[i] << (8 * i);
4832 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004833
4834 if (x > PY_SSIZE_T_MAX)
4835 return -1;
4836 else
4837 return (Py_ssize_t) x;
4838}
4839
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004840/* s contains x bytes of a little-endian integer. Return its value as a
4841 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004842 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004843 * of x-platform bugs.
4844 */
4845static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004846calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004847{
4848 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004849 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004850 long x = 0;
4851
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004852 for (i = 0; i < nbytes; i++) {
4853 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004854 }
4855
4856 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4857 * is signed, so on a box with longs bigger than 4 bytes we need
4858 * to extend a BININT's sign bit to the full width.
4859 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004860 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004861 x |= -(x & (1L << 31));
4862 }
4863
4864 return x;
4865}
4866
4867static int
4868load_binintx(UnpicklerObject *self, char *s, int size)
4869{
4870 PyObject *value;
4871 long x;
4872
4873 x = calc_binint(s, size);
4874
4875 if ((value = PyLong_FromLong(x)) == NULL)
4876 return -1;
4877
4878 PDATA_PUSH(self->stack, value, -1);
4879 return 0;
4880}
4881
4882static int
4883load_binint(UnpicklerObject *self)
4884{
4885 char *s;
4886
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004887 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004888 return -1;
4889
4890 return load_binintx(self, s, 4);
4891}
4892
4893static int
4894load_binint1(UnpicklerObject *self)
4895{
4896 char *s;
4897
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004898 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899 return -1;
4900
4901 return load_binintx(self, s, 1);
4902}
4903
4904static int
4905load_binint2(UnpicklerObject *self)
4906{
4907 char *s;
4908
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004909 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004910 return -1;
4911
4912 return load_binintx(self, s, 2);
4913}
4914
4915static int
4916load_long(UnpicklerObject *self)
4917{
4918 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004919 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004920 Py_ssize_t len;
4921
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004922 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004923 return -1;
4924 if (len < 2)
4925 return bad_readline();
4926
Mark Dickinson8dd05142009-01-20 20:43:58 +00004927 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4928 the 'L' before calling PyLong_FromString. In order to maintain
4929 compatibility with Python 3.0.0, we don't actually *require*
4930 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004931 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004932 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004933 /* XXX: Should the base argument explicitly set to 10? */
4934 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004935 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004936 return -1;
4937
4938 PDATA_PUSH(self->stack, value, -1);
4939 return 0;
4940}
4941
4942/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4943 * data following.
4944 */
4945static int
4946load_counted_long(UnpicklerObject *self, int size)
4947{
4948 PyObject *value;
4949 char *nbytes;
4950 char *pdata;
4951
4952 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004953 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004954 return -1;
4955
4956 size = calc_binint(nbytes, size);
4957 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004958 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004959 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004960 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004961 "LONG pickle has negative byte count");
4962 return -1;
4963 }
4964
4965 if (size == 0)
4966 value = PyLong_FromLong(0L);
4967 else {
4968 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004969 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004970 return -1;
4971 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4972 1 /* little endian */ , 1 /* signed */ );
4973 }
4974 if (value == NULL)
4975 return -1;
4976 PDATA_PUSH(self->stack, value, -1);
4977 return 0;
4978}
4979
4980static int
4981load_float(UnpicklerObject *self)
4982{
4983 PyObject *value;
4984 char *endptr, *s;
4985 Py_ssize_t len;
4986 double d;
4987
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004988 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004989 return -1;
4990 if (len < 2)
4991 return bad_readline();
4992
4993 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004994 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4995 if (d == -1.0 && PyErr_Occurred())
4996 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004997 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004998 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4999 return -1;
5000 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005001 value = PyFloat_FromDouble(d);
5002 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005003 return -1;
5004
5005 PDATA_PUSH(self->stack, value, -1);
5006 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005007}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005008
5009static int
5010load_binfloat(UnpicklerObject *self)
5011{
5012 PyObject *value;
5013 double x;
5014 char *s;
5015
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005016 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005017 return -1;
5018
5019 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5020 if (x == -1.0 && PyErr_Occurred())
5021 return -1;
5022
5023 if ((value = PyFloat_FromDouble(x)) == NULL)
5024 return -1;
5025
5026 PDATA_PUSH(self->stack, value, -1);
5027 return 0;
5028}
5029
5030static int
5031load_string(UnpicklerObject *self)
5032{
5033 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005034 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005035 Py_ssize_t len;
5036 char *s, *p;
5037
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005038 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005039 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005040 /* Strip the newline */
5041 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005043 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044 p = s + 1;
5045 len -= 2;
5046 }
5047 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005048 PickleState *st = _Pickle_GetGlobalState();
5049 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005050 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005051 return -1;
5052 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005053 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005054
5055 /* Use the PyBytes API to decode the string, since that is what is used
5056 to encode, and then coerce the result to Unicode. */
5057 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005058 if (bytes == NULL)
5059 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005060
5061 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5062 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5063 if (strcmp(self->encoding, "bytes") == 0) {
5064 obj = bytes;
5065 }
5066 else {
5067 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5068 Py_DECREF(bytes);
5069 if (obj == NULL) {
5070 return -1;
5071 }
5072 }
5073
5074 PDATA_PUSH(self->stack, obj, -1);
5075 return 0;
5076}
5077
5078static int
5079load_counted_binstring(UnpicklerObject *self, int nbytes)
5080{
5081 PyObject *obj;
5082 Py_ssize_t size;
5083 char *s;
5084
5085 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005086 return -1;
5087
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005088 size = calc_binsize(s, nbytes);
5089 if (size < 0) {
5090 PickleState *st = _Pickle_GetGlobalState();
5091 PyErr_Format(st->UnpicklingError,
5092 "BINSTRING exceeds system's maximum size of %zd bytes",
5093 PY_SSIZE_T_MAX);
5094 return -1;
5095 }
5096
5097 if (_Unpickler_Read(self, &s, size) < 0)
5098 return -1;
5099
5100 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5101 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5102 if (strcmp(self->encoding, "bytes") == 0) {
5103 obj = PyBytes_FromStringAndSize(s, size);
5104 }
5105 else {
5106 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5107 }
5108 if (obj == NULL) {
5109 return -1;
5110 }
5111
5112 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005113 return 0;
5114}
5115
5116static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005117load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005118{
5119 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005120 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005121 char *s;
5122
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005123 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005124 return -1;
5125
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005126 size = calc_binsize(s, nbytes);
5127 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005128 PyErr_Format(PyExc_OverflowError,
5129 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005130 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005131 return -1;
5132 }
5133
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005134 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005135 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005136
5137 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005138 if (bytes == NULL)
5139 return -1;
5140
5141 PDATA_PUSH(self->stack, bytes, -1);
5142 return 0;
5143}
5144
5145static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005146load_unicode(UnpicklerObject *self)
5147{
5148 PyObject *str;
5149 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005150 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005151
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005152 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005153 return -1;
5154 if (len < 1)
5155 return bad_readline();
5156
5157 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5158 if (str == NULL)
5159 return -1;
5160
5161 PDATA_PUSH(self->stack, str, -1);
5162 return 0;
5163}
5164
5165static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005166load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005167{
5168 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005169 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005170 char *s;
5171
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005172 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005173 return -1;
5174
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005175 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005176 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005177 PyErr_Format(PyExc_OverflowError,
5178 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005179 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005180 return -1;
5181 }
5182
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005183 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005184 return -1;
5185
Victor Stinner485fb562010-04-13 11:07:24 +00005186 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005187 if (str == NULL)
5188 return -1;
5189
5190 PDATA_PUSH(self->stack, str, -1);
5191 return 0;
5192}
5193
5194static int
Victor Stinner21b47112016-03-14 18:09:39 +01005195load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005196{
5197 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005198
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005199 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005200 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005201
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005202 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005203 if (tuple == NULL)
5204 return -1;
5205 PDATA_PUSH(self->stack, tuple, -1);
5206 return 0;
5207}
5208
5209static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005210load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005211{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005212 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005213
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005214 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005215 return -1;
5216
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005217 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005218}
5219
5220static int
5221load_empty_list(UnpicklerObject *self)
5222{
5223 PyObject *list;
5224
5225 if ((list = PyList_New(0)) == NULL)
5226 return -1;
5227 PDATA_PUSH(self->stack, list, -1);
5228 return 0;
5229}
5230
5231static int
5232load_empty_dict(UnpicklerObject *self)
5233{
5234 PyObject *dict;
5235
5236 if ((dict = PyDict_New()) == NULL)
5237 return -1;
5238 PDATA_PUSH(self->stack, dict, -1);
5239 return 0;
5240}
5241
5242static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005243load_empty_set(UnpicklerObject *self)
5244{
5245 PyObject *set;
5246
5247 if ((set = PySet_New(NULL)) == NULL)
5248 return -1;
5249 PDATA_PUSH(self->stack, set, -1);
5250 return 0;
5251}
5252
5253static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005254load_list(UnpicklerObject *self)
5255{
5256 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005257 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005258
5259 if ((i = marker(self)) < 0)
5260 return -1;
5261
5262 list = Pdata_poplist(self->stack, i);
5263 if (list == NULL)
5264 return -1;
5265 PDATA_PUSH(self->stack, list, -1);
5266 return 0;
5267}
5268
5269static int
5270load_dict(UnpicklerObject *self)
5271{
5272 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005273 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005274
5275 if ((i = marker(self)) < 0)
5276 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005277 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005278
5279 if ((dict = PyDict_New()) == NULL)
5280 return -1;
5281
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005282 if ((j - i) % 2 != 0) {
5283 PickleState *st = _Pickle_GetGlobalState();
5284 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005285 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005286 return -1;
5287 }
5288
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005289 for (k = i + 1; k < j; k += 2) {
5290 key = self->stack->data[k - 1];
5291 value = self->stack->data[k];
5292 if (PyDict_SetItem(dict, key, value) < 0) {
5293 Py_DECREF(dict);
5294 return -1;
5295 }
5296 }
5297 Pdata_clear(self->stack, i);
5298 PDATA_PUSH(self->stack, dict, -1);
5299 return 0;
5300}
5301
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005302static int
5303load_frozenset(UnpicklerObject *self)
5304{
5305 PyObject *items;
5306 PyObject *frozenset;
5307 Py_ssize_t i;
5308
5309 if ((i = marker(self)) < 0)
5310 return -1;
5311
5312 items = Pdata_poptuple(self->stack, i);
5313 if (items == NULL)
5314 return -1;
5315
5316 frozenset = PyFrozenSet_New(items);
5317 Py_DECREF(items);
5318 if (frozenset == NULL)
5319 return -1;
5320
5321 PDATA_PUSH(self->stack, frozenset, -1);
5322 return 0;
5323}
5324
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005325static PyObject *
5326instantiate(PyObject *cls, PyObject *args)
5327{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005328 /* Caller must assure args are a tuple. Normally, args come from
5329 Pdata_poptuple which packs objects from the top of the stack
5330 into a newly created tuple. */
5331 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005332 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5333 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005334 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005335 PyObject *func;
5336 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5337 return NULL;
5338 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005339 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005340 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5341 }
5342 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005343 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005344 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005345}
5346
5347static int
5348load_obj(UnpicklerObject *self)
5349{
5350 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005351 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005352
5353 if ((i = marker(self)) < 0)
5354 return -1;
5355
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005356 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005357 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005358
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005359 args = Pdata_poptuple(self->stack, i + 1);
5360 if (args == NULL)
5361 return -1;
5362
5363 PDATA_POP(self->stack, cls);
5364 if (cls) {
5365 obj = instantiate(cls, args);
5366 Py_DECREF(cls);
5367 }
5368 Py_DECREF(args);
5369 if (obj == NULL)
5370 return -1;
5371
5372 PDATA_PUSH(self->stack, obj, -1);
5373 return 0;
5374}
5375
5376static int
5377load_inst(UnpicklerObject *self)
5378{
5379 PyObject *cls = NULL;
5380 PyObject *args = NULL;
5381 PyObject *obj = NULL;
5382 PyObject *module_name;
5383 PyObject *class_name;
5384 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005385 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005386 char *s;
5387
5388 if ((i = marker(self)) < 0)
5389 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005390 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005391 return -1;
5392 if (len < 2)
5393 return bad_readline();
5394
5395 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5396 identifiers are permitted in Python 3.0, since the INST opcode is only
5397 supported by older protocols on Python 2.x. */
5398 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5399 if (module_name == NULL)
5400 return -1;
5401
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005402 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005403 if (len < 2) {
5404 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005405 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005406 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005407 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005408 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005409 cls = find_class(self, module_name, class_name);
5410 Py_DECREF(class_name);
5411 }
5412 }
5413 Py_DECREF(module_name);
5414
5415 if (cls == NULL)
5416 return -1;
5417
5418 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5419 obj = instantiate(cls, args);
5420 Py_DECREF(args);
5421 }
5422 Py_DECREF(cls);
5423
5424 if (obj == NULL)
5425 return -1;
5426
5427 PDATA_PUSH(self->stack, obj, -1);
5428 return 0;
5429}
5430
5431static int
5432load_newobj(UnpicklerObject *self)
5433{
5434 PyObject *args = NULL;
5435 PyObject *clsraw = NULL;
5436 PyTypeObject *cls; /* clsraw cast to its true type */
5437 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005438 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005439
5440 /* Stack is ... cls argtuple, and we want to call
5441 * cls.__new__(cls, *argtuple).
5442 */
5443 PDATA_POP(self->stack, args);
5444 if (args == NULL)
5445 goto error;
5446 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005447 PyErr_SetString(st->UnpicklingError,
5448 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005449 goto error;
5450 }
5451
5452 PDATA_POP(self->stack, clsraw);
5453 cls = (PyTypeObject *)clsraw;
5454 if (cls == NULL)
5455 goto error;
5456 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005457 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005458 "isn't a type object");
5459 goto error;
5460 }
5461 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005462 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005463 "has NULL tp_new");
5464 goto error;
5465 }
5466
5467 /* Call __new__. */
5468 obj = cls->tp_new(cls, args, NULL);
5469 if (obj == NULL)
5470 goto error;
5471
5472 Py_DECREF(args);
5473 Py_DECREF(clsraw);
5474 PDATA_PUSH(self->stack, obj, -1);
5475 return 0;
5476
5477 error:
5478 Py_XDECREF(args);
5479 Py_XDECREF(clsraw);
5480 return -1;
5481}
5482
5483static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005484load_newobj_ex(UnpicklerObject *self)
5485{
5486 PyObject *cls, *args, *kwargs;
5487 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005488 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005489
5490 PDATA_POP(self->stack, kwargs);
5491 if (kwargs == NULL) {
5492 return -1;
5493 }
5494 PDATA_POP(self->stack, args);
5495 if (args == NULL) {
5496 Py_DECREF(kwargs);
5497 return -1;
5498 }
5499 PDATA_POP(self->stack, cls);
5500 if (cls == NULL) {
5501 Py_DECREF(kwargs);
5502 Py_DECREF(args);
5503 return -1;
5504 }
Larry Hastings61272b72014-01-07 12:41:53 -08005505
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005506 if (!PyType_Check(cls)) {
5507 Py_DECREF(kwargs);
5508 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005509 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005510 "NEWOBJ_EX class argument must be a type, not %.200s",
5511 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005512 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005513 return -1;
5514 }
5515
5516 if (((PyTypeObject *)cls)->tp_new == NULL) {
5517 Py_DECREF(kwargs);
5518 Py_DECREF(args);
5519 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005520 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005521 "NEWOBJ_EX class argument doesn't have __new__");
5522 return -1;
5523 }
5524 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5525 Py_DECREF(kwargs);
5526 Py_DECREF(args);
5527 Py_DECREF(cls);
5528 if (obj == NULL) {
5529 return -1;
5530 }
5531 PDATA_PUSH(self->stack, obj, -1);
5532 return 0;
5533}
5534
5535static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005536load_global(UnpicklerObject *self)
5537{
5538 PyObject *global = NULL;
5539 PyObject *module_name;
5540 PyObject *global_name;
5541 Py_ssize_t len;
5542 char *s;
5543
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005544 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005545 return -1;
5546 if (len < 2)
5547 return bad_readline();
5548 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5549 if (!module_name)
5550 return -1;
5551
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005552 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553 if (len < 2) {
5554 Py_DECREF(module_name);
5555 return bad_readline();
5556 }
5557 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5558 if (global_name) {
5559 global = find_class(self, module_name, global_name);
5560 Py_DECREF(global_name);
5561 }
5562 }
5563 Py_DECREF(module_name);
5564
5565 if (global == NULL)
5566 return -1;
5567 PDATA_PUSH(self->stack, global, -1);
5568 return 0;
5569}
5570
5571static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005572load_stack_global(UnpicklerObject *self)
5573{
5574 PyObject *global;
5575 PyObject *module_name;
5576 PyObject *global_name;
5577
5578 PDATA_POP(self->stack, global_name);
5579 PDATA_POP(self->stack, module_name);
5580 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5581 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005582 PickleState *st = _Pickle_GetGlobalState();
5583 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005584 Py_XDECREF(global_name);
5585 Py_XDECREF(module_name);
5586 return -1;
5587 }
5588 global = find_class(self, module_name, global_name);
5589 Py_DECREF(global_name);
5590 Py_DECREF(module_name);
5591 if (global == NULL)
5592 return -1;
5593 PDATA_PUSH(self->stack, global, -1);
5594 return 0;
5595}
5596
5597static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005598load_persid(UnpicklerObject *self)
5599{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005600 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601 Py_ssize_t len;
5602 char *s;
5603
5604 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005605 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005607 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005608 return bad_readline();
5609
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005610 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5611 if (pid == NULL) {
5612 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5613 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5614 "persistent IDs in protocol 0 must be "
5615 "ASCII strings");
5616 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005617 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005618 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005619
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005620 obj = call_method(self->pers_func, self->pers_func_self, pid);
5621 Py_DECREF(pid);
5622 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005623 return -1;
5624
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005625 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005626 return 0;
5627 }
5628 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005629 PickleState *st = _Pickle_GetGlobalState();
5630 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005631 "A load persistent id instruction was encountered,\n"
5632 "but no persistent_load function was specified.");
5633 return -1;
5634 }
5635}
5636
5637static int
5638load_binpersid(UnpicklerObject *self)
5639{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005640 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005641
5642 if (self->pers_func) {
5643 PDATA_POP(self->stack, pid);
5644 if (pid == NULL)
5645 return -1;
5646
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005647 obj = call_method(self->pers_func, self->pers_func_self, pid);
5648 Py_DECREF(pid);
5649 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005650 return -1;
5651
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005652 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005653 return 0;
5654 }
5655 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005656 PickleState *st = _Pickle_GetGlobalState();
5657 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005658 "A load persistent id instruction was encountered,\n"
5659 "but no persistent_load function was specified.");
5660 return -1;
5661 }
5662}
5663
5664static int
5665load_pop(UnpicklerObject *self)
5666{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005667 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005668
5669 /* Note that we split the (pickle.py) stack into two stacks,
5670 * an object stack and a mark stack. We have to be clever and
5671 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005672 * mark stack first, and only signalling a stack underflow if
5673 * the object stack is empty and the mark stack doesn't match
5674 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005675 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005676 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005677 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005678 self->stack->mark_set = self->num_marks != 0;
5679 self->stack->fence = self->num_marks ?
5680 self->marks[self->num_marks - 1] : 0;
5681 } else if (len <= self->stack->fence)
5682 return Pdata_stack_underflow(self->stack);
5683 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684 len--;
5685 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005686 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688 return 0;
5689}
5690
5691static int
5692load_pop_mark(UnpicklerObject *self)
5693{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005694 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005695
5696 if ((i = marker(self)) < 0)
5697 return -1;
5698
5699 Pdata_clear(self->stack, i);
5700
5701 return 0;
5702}
5703
5704static int
5705load_dup(UnpicklerObject *self)
5706{
5707 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005708 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005710 if (len <= self->stack->fence)
5711 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005712 last = self->stack->data[len - 1];
5713 PDATA_APPEND(self->stack, last, -1);
5714 return 0;
5715}
5716
5717static int
5718load_get(UnpicklerObject *self)
5719{
5720 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005721 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005722 Py_ssize_t len;
5723 char *s;
5724
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005725 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005726 return -1;
5727 if (len < 2)
5728 return bad_readline();
5729
5730 key = PyLong_FromString(s, NULL, 10);
5731 if (key == NULL)
5732 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005733 idx = PyLong_AsSsize_t(key);
5734 if (idx == -1 && PyErr_Occurred()) {
5735 Py_DECREF(key);
5736 return -1;
5737 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005738
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005739 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005740 if (value == NULL) {
5741 if (!PyErr_Occurred())
5742 PyErr_SetObject(PyExc_KeyError, key);
5743 Py_DECREF(key);
5744 return -1;
5745 }
5746 Py_DECREF(key);
5747
5748 PDATA_APPEND(self->stack, value, -1);
5749 return 0;
5750}
5751
5752static int
5753load_binget(UnpicklerObject *self)
5754{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005755 PyObject *value;
5756 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005757 char *s;
5758
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005759 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005760 return -1;
5761
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005762 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005764 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005767 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005768 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005769 Py_DECREF(key);
5770 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771 return -1;
5772 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773
5774 PDATA_APPEND(self->stack, value, -1);
5775 return 0;
5776}
5777
5778static int
5779load_long_binget(UnpicklerObject *self)
5780{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005781 PyObject *value;
5782 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005783 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005786 return -1;
5787
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005788 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005792 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005793 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005794 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005795 Py_DECREF(key);
5796 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797 return -1;
5798 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005799
5800 PDATA_APPEND(self->stack, value, -1);
5801 return 0;
5802}
5803
5804/* Push an object from the extension registry (EXT[124]). nbytes is
5805 * the number of bytes following the opcode, holding the index (code) value.
5806 */
5807static int
5808load_extension(UnpicklerObject *self, int nbytes)
5809{
5810 char *codebytes; /* the nbytes bytes after the opcode */
5811 long code; /* calc_binint returns long */
5812 PyObject *py_code; /* code as a Python int */
5813 PyObject *obj; /* the object to push */
5814 PyObject *pair; /* (module_name, class_name) */
5815 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005816 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817
5818 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005819 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005820 return -1;
5821 code = calc_binint(codebytes, nbytes);
5822 if (code <= 0) { /* note that 0 is forbidden */
5823 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005824 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005825 return -1;
5826 }
5827
5828 /* Look for the code in the cache. */
5829 py_code = PyLong_FromLong(code);
5830 if (py_code == NULL)
5831 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005832 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005833 if (obj != NULL) {
5834 /* Bingo. */
5835 Py_DECREF(py_code);
5836 PDATA_APPEND(self->stack, obj, -1);
5837 return 0;
5838 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005839 if (PyErr_Occurred()) {
5840 Py_DECREF(py_code);
5841 return -1;
5842 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005843
5844 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005845 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846 if (pair == NULL) {
5847 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005848 if (!PyErr_Occurred()) {
5849 PyErr_Format(PyExc_ValueError, "unregistered extension "
5850 "code %ld", code);
5851 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005852 return -1;
5853 }
5854 /* Since the extension registry is manipulable via Python code,
5855 * confirm that pair is really a 2-tuple of strings.
5856 */
5857 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5858 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5859 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5860 Py_DECREF(py_code);
5861 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5862 "isn't a 2-tuple of strings", code);
5863 return -1;
5864 }
5865 /* Load the object. */
5866 obj = find_class(self, module_name, class_name);
5867 if (obj == NULL) {
5868 Py_DECREF(py_code);
5869 return -1;
5870 }
5871 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005872 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005873 Py_DECREF(py_code);
5874 if (code < 0) {
5875 Py_DECREF(obj);
5876 return -1;
5877 }
5878 PDATA_PUSH(self->stack, obj, -1);
5879 return 0;
5880}
5881
5882static int
5883load_put(UnpicklerObject *self)
5884{
5885 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005886 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005887 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005888 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005889
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005890 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005891 return -1;
5892 if (len < 2)
5893 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005894 if (Py_SIZE(self->stack) <= self->stack->fence)
5895 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005896 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005897
5898 key = PyLong_FromString(s, NULL, 10);
5899 if (key == NULL)
5900 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005901 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005902 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005903 if (idx < 0) {
5904 if (!PyErr_Occurred())
5905 PyErr_SetString(PyExc_ValueError,
5906 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005907 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005908 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005909
5910 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005911}
5912
5913static int
5914load_binput(UnpicklerObject *self)
5915{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005916 PyObject *value;
5917 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005918 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005919
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005920 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005921 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005922
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005923 if (Py_SIZE(self->stack) <= self->stack->fence)
5924 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005925 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005926
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005927 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005928
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005929 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005930}
5931
5932static int
5933load_long_binput(UnpicklerObject *self)
5934{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005935 PyObject *value;
5936 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005937 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005938
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005939 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005940 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005941
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005942 if (Py_SIZE(self->stack) <= self->stack->fence)
5943 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005944 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005945
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005946 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005947 if (idx < 0) {
5948 PyErr_SetString(PyExc_ValueError,
5949 "negative LONG_BINPUT argument");
5950 return -1;
5951 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005952
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005953 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005954}
5955
5956static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005957load_memoize(UnpicklerObject *self)
5958{
5959 PyObject *value;
5960
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005961 if (Py_SIZE(self->stack) <= self->stack->fence)
5962 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005963 value = self->stack->data[Py_SIZE(self->stack) - 1];
5964
5965 return _Unpickler_MemoPut(self, self->memo_len, value);
5966}
5967
5968static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005969do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005970{
5971 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005972 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005973 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005974 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005975 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005976
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005977 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005978 if (x > len || x <= self->stack->fence)
5979 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005980 if (len == x) /* nothing to do */
5981 return 0;
5982
5983 list = self->stack->data[x - 1];
5984
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005985 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005986 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005987 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005988
5989 slice = Pdata_poplist(self->stack, x);
5990 if (!slice)
5991 return -1;
5992 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005993 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005994 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005995 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005996 }
5997 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005998 PyObject *extend_func;
5999 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006000
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006001 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6002 if (extend_func != NULL) {
6003 slice = Pdata_poplist(self->stack, x);
6004 if (!slice) {
6005 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006006 return -1;
6007 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006008 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006009 Py_DECREF(extend_func);
6010 if (result == NULL)
6011 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006012 Py_DECREF(result);
6013 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006014 else {
6015 PyObject *append_func;
6016 _Py_IDENTIFIER(append);
6017
6018 /* Even if the PEP 307 requires extend() and append() methods,
6019 fall back on append() if the object has no extend() method
6020 for backward compatibility. */
6021 PyErr_Clear();
6022 append_func = _PyObject_GetAttrId(list, &PyId_append);
6023 if (append_func == NULL)
6024 return -1;
6025 for (i = x; i < len; i++) {
6026 value = self->stack->data[i];
6027 result = _Pickle_FastCall(append_func, value);
6028 if (result == NULL) {
6029 Pdata_clear(self->stack, i + 1);
6030 Py_SIZE(self->stack) = x;
6031 Py_DECREF(append_func);
6032 return -1;
6033 }
6034 Py_DECREF(result);
6035 }
6036 Py_SIZE(self->stack) = x;
6037 Py_DECREF(append_func);
6038 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006039 }
6040
6041 return 0;
6042}
6043
6044static int
6045load_append(UnpicklerObject *self)
6046{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006047 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6048 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006049 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006050}
6051
6052static int
6053load_appends(UnpicklerObject *self)
6054{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006055 Py_ssize_t i = marker(self);
6056 if (i < 0)
6057 return -1;
6058 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006059}
6060
6061static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006062do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006063{
6064 PyObject *value, *key;
6065 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006066 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006067 int status = 0;
6068
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006069 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006070 if (x > len || x <= self->stack->fence)
6071 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006072 if (len == x) /* nothing to do */
6073 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006074 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006075 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006076 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006077 PyErr_SetString(st->UnpicklingError,
6078 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079 return -1;
6080 }
6081
6082 /* Here, dict does not actually need to be a PyDict; it could be anything
6083 that supports the __setitem__ attribute. */
6084 dict = self->stack->data[x - 1];
6085
6086 for (i = x + 1; i < len; i += 2) {
6087 key = self->stack->data[i - 1];
6088 value = self->stack->data[i];
6089 if (PyObject_SetItem(dict, key, value) < 0) {
6090 status = -1;
6091 break;
6092 }
6093 }
6094
6095 Pdata_clear(self->stack, x);
6096 return status;
6097}
6098
6099static int
6100load_setitem(UnpicklerObject *self)
6101{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006102 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006103}
6104
6105static int
6106load_setitems(UnpicklerObject *self)
6107{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006108 Py_ssize_t i = marker(self);
6109 if (i < 0)
6110 return -1;
6111 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006112}
6113
6114static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006115load_additems(UnpicklerObject *self)
6116{
6117 PyObject *set;
6118 Py_ssize_t mark, len, i;
6119
6120 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006121 if (mark < 0)
6122 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006123 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006124 if (mark > len || mark <= self->stack->fence)
6125 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006126 if (len == mark) /* nothing to do */
6127 return 0;
6128
6129 set = self->stack->data[mark - 1];
6130
6131 if (PySet_Check(set)) {
6132 PyObject *items;
6133 int status;
6134
6135 items = Pdata_poptuple(self->stack, mark);
6136 if (items == NULL)
6137 return -1;
6138
6139 status = _PySet_Update(set, items);
6140 Py_DECREF(items);
6141 return status;
6142 }
6143 else {
6144 PyObject *add_func;
6145 _Py_IDENTIFIER(add);
6146
6147 add_func = _PyObject_GetAttrId(set, &PyId_add);
6148 if (add_func == NULL)
6149 return -1;
6150 for (i = mark; i < len; i++) {
6151 PyObject *result;
6152 PyObject *item;
6153
6154 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006155 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006156 if (result == NULL) {
6157 Pdata_clear(self->stack, i + 1);
6158 Py_SIZE(self->stack) = mark;
6159 return -1;
6160 }
6161 Py_DECREF(result);
6162 }
6163 Py_SIZE(self->stack) = mark;
6164 }
6165
6166 return 0;
6167}
6168
6169static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006170load_build(UnpicklerObject *self)
6171{
6172 PyObject *state, *inst, *slotstate;
6173 PyObject *setstate;
6174 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006175 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006176
6177 /* Stack is ... instance, state. We want to leave instance at
6178 * the stack top, possibly mutated via instance.__setstate__(state).
6179 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006180 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6181 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006182
6183 PDATA_POP(self->stack, state);
6184 if (state == NULL)
6185 return -1;
6186
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006187 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006188
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006189 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6190 Py_DECREF(state);
6191 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006192 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006193 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006194 PyObject *result;
6195
6196 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006197 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006198 Py_DECREF(setstate);
6199 if (result == NULL)
6200 return -1;
6201 Py_DECREF(result);
6202 return 0;
6203 }
6204
6205 /* A default __setstate__. First see whether state embeds a
6206 * slot state dict too (a proto 2 addition).
6207 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006208 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006209 PyObject *tmp = state;
6210
6211 state = PyTuple_GET_ITEM(tmp, 0);
6212 slotstate = PyTuple_GET_ITEM(tmp, 1);
6213 Py_INCREF(state);
6214 Py_INCREF(slotstate);
6215 Py_DECREF(tmp);
6216 }
6217 else
6218 slotstate = NULL;
6219
6220 /* Set inst.__dict__ from the state dict (if any). */
6221 if (state != Py_None) {
6222 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006223 PyObject *d_key, *d_value;
6224 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006225 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006226
6227 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006228 PickleState *st = _Pickle_GetGlobalState();
6229 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006230 goto error;
6231 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006232 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006233 if (dict == NULL)
6234 goto error;
6235
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006236 i = 0;
6237 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6238 /* normally the keys for instance attributes are
6239 interned. we should try to do that here. */
6240 Py_INCREF(d_key);
6241 if (PyUnicode_CheckExact(d_key))
6242 PyUnicode_InternInPlace(&d_key);
6243 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6244 Py_DECREF(d_key);
6245 goto error;
6246 }
6247 Py_DECREF(d_key);
6248 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006249 Py_DECREF(dict);
6250 }
6251
6252 /* Also set instance attributes from the slotstate dict (if any). */
6253 if (slotstate != NULL) {
6254 PyObject *d_key, *d_value;
6255 Py_ssize_t i;
6256
6257 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006258 PickleState *st = _Pickle_GetGlobalState();
6259 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006260 "slot state is not a dictionary");
6261 goto error;
6262 }
6263 i = 0;
6264 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6265 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6266 goto error;
6267 }
6268 }
6269
6270 if (0) {
6271 error:
6272 status = -1;
6273 }
6274
6275 Py_DECREF(state);
6276 Py_XDECREF(slotstate);
6277 return status;
6278}
6279
6280static int
6281load_mark(UnpicklerObject *self)
6282{
6283
6284 /* Note that we split the (pickle.py) stack into two stacks, an
6285 * object stack and a mark stack. Here we push a mark onto the
6286 * mark stack.
6287 */
6288
6289 if ((self->num_marks + 1) >= self->marks_size) {
6290 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006291
6292 /* Use the size_t type to check for overflow. */
6293 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006294 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006295 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006296 PyErr_NoMemory();
6297 return -1;
6298 }
6299
6300 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006301 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006302 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006303 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6304 if (self->marks == NULL) {
6305 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006306 PyErr_NoMemory();
6307 return -1;
6308 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006309 self->marks_size = (Py_ssize_t)alloc;
6310 }
6311
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006312 self->stack->mark_set = 1;
6313 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006314
6315 return 0;
6316}
6317
6318static int
6319load_reduce(UnpicklerObject *self)
6320{
6321 PyObject *callable = NULL;
6322 PyObject *argtup = NULL;
6323 PyObject *obj = NULL;
6324
6325 PDATA_POP(self->stack, argtup);
6326 if (argtup == NULL)
6327 return -1;
6328 PDATA_POP(self->stack, callable);
6329 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006330 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006331 Py_DECREF(callable);
6332 }
6333 Py_DECREF(argtup);
6334
6335 if (obj == NULL)
6336 return -1;
6337
6338 PDATA_PUSH(self->stack, obj, -1);
6339 return 0;
6340}
6341
6342/* Just raises an error if we don't know the protocol specified. PROTO
6343 * is the first opcode for protocols >= 2.
6344 */
6345static int
6346load_proto(UnpicklerObject *self)
6347{
6348 char *s;
6349 int i;
6350
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006351 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006352 return -1;
6353
6354 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006355 if (i <= HIGHEST_PROTOCOL) {
6356 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006357 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006358 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006359
6360 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6361 return -1;
6362}
6363
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006364static int
6365load_frame(UnpicklerObject *self)
6366{
6367 char *s;
6368 Py_ssize_t frame_len;
6369
6370 if (_Unpickler_Read(self, &s, 8) < 0)
6371 return -1;
6372
6373 frame_len = calc_binsize(s, 8);
6374 if (frame_len < 0) {
6375 PyErr_Format(PyExc_OverflowError,
6376 "FRAME length exceeds system's maximum of %zd bytes",
6377 PY_SSIZE_T_MAX);
6378 return -1;
6379 }
6380
6381 if (_Unpickler_Read(self, &s, frame_len) < 0)
6382 return -1;
6383
6384 /* Rewind to start of frame */
6385 self->next_read_idx -= frame_len;
6386 return 0;
6387}
6388
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006389static PyObject *
6390load(UnpicklerObject *self)
6391{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006392 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006393 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006394
6395 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006396 self->stack->mark_set = 0;
6397 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006398 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006399 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006400 Pdata_clear(self->stack, 0);
6401
6402 /* Convenient macros for the dispatch while-switch loop just below. */
6403#define OP(opcode, load_func) \
6404 case opcode: if (load_func(self) < 0) break; continue;
6405
6406#define OP_ARG(opcode, load_func, arg) \
6407 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6408
6409 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006410 if (_Unpickler_Read(self, &s, 1) < 0) {
6411 PickleState *st = _Pickle_GetGlobalState();
6412 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6413 PyErr_Format(PyExc_EOFError, "Ran out of input");
6414 }
6415 return NULL;
6416 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006417
6418 switch ((enum opcode)s[0]) {
6419 OP(NONE, load_none)
6420 OP(BININT, load_binint)
6421 OP(BININT1, load_binint1)
6422 OP(BININT2, load_binint2)
6423 OP(INT, load_int)
6424 OP(LONG, load_long)
6425 OP_ARG(LONG1, load_counted_long, 1)
6426 OP_ARG(LONG4, load_counted_long, 4)
6427 OP(FLOAT, load_float)
6428 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006429 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6430 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6431 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6432 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6433 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006434 OP(STRING, load_string)
6435 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006436 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6437 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6438 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006439 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6440 OP_ARG(TUPLE1, load_counted_tuple, 1)
6441 OP_ARG(TUPLE2, load_counted_tuple, 2)
6442 OP_ARG(TUPLE3, load_counted_tuple, 3)
6443 OP(TUPLE, load_tuple)
6444 OP(EMPTY_LIST, load_empty_list)
6445 OP(LIST, load_list)
6446 OP(EMPTY_DICT, load_empty_dict)
6447 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006448 OP(EMPTY_SET, load_empty_set)
6449 OP(ADDITEMS, load_additems)
6450 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006451 OP(OBJ, load_obj)
6452 OP(INST, load_inst)
6453 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006454 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006455 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006456 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006457 OP(APPEND, load_append)
6458 OP(APPENDS, load_appends)
6459 OP(BUILD, load_build)
6460 OP(DUP, load_dup)
6461 OP(BINGET, load_binget)
6462 OP(LONG_BINGET, load_long_binget)
6463 OP(GET, load_get)
6464 OP(MARK, load_mark)
6465 OP(BINPUT, load_binput)
6466 OP(LONG_BINPUT, load_long_binput)
6467 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006468 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006469 OP(POP, load_pop)
6470 OP(POP_MARK, load_pop_mark)
6471 OP(SETITEM, load_setitem)
6472 OP(SETITEMS, load_setitems)
6473 OP(PERSID, load_persid)
6474 OP(BINPERSID, load_binpersid)
6475 OP(REDUCE, load_reduce)
6476 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006477 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006478 OP_ARG(EXT1, load_extension, 1)
6479 OP_ARG(EXT2, load_extension, 2)
6480 OP_ARG(EXT4, load_extension, 4)
6481 OP_ARG(NEWTRUE, load_bool, Py_True)
6482 OP_ARG(NEWFALSE, load_bool, Py_False)
6483
6484 case STOP:
6485 break;
6486
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006487 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006488 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006489 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006490 unsigned char c = (unsigned char) *s;
6491 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6492 PyErr_Format(st->UnpicklingError,
6493 "invalid load key, '%c'.", c);
6494 }
6495 else {
6496 PyErr_Format(st->UnpicklingError,
6497 "invalid load key, '\\x%02x'.", c);
6498 }
6499 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006500 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006501 }
6502
6503 break; /* and we are done! */
6504 }
6505
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006506 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006507 return NULL;
6508 }
6509
Victor Stinner2ae57e32013-10-31 13:39:23 +01006510 if (_Unpickler_SkipConsumed(self) < 0)
6511 return NULL;
6512
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006513 PDATA_POP(self->stack, value);
6514 return value;
6515}
6516
Larry Hastings61272b72014-01-07 12:41:53 -08006517/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006518
6519_pickle.Unpickler.load
6520
6521Load a pickle.
6522
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006523Read a pickled object representation from the open file object given
6524in the constructor, and return the reconstituted object hierarchy
6525specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006526[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006527
Larry Hastings3cceb382014-01-04 11:09:09 -08006528static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006529_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006530/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006531{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006532 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006533
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006534 /* Check whether the Unpickler was initialized correctly. This prevents
6535 segfaulting if a subclass overridden __init__ with a function that does
6536 not call Unpickler.__init__(). Here, we simply ensure that self->read
6537 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006538 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006539 PickleState *st = _Pickle_GetGlobalState();
6540 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006541 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006542 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006543 return NULL;
6544 }
6545
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006546 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006547}
6548
6549/* The name of find_class() is misleading. In newer pickle protocols, this
6550 function is used for loading any global (i.e., functions), not just
6551 classes. The name is kept only for backward compatibility. */
6552
Larry Hastings61272b72014-01-07 12:41:53 -08006553/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006554
6555_pickle.Unpickler.find_class
6556
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006557 module_name: object
6558 global_name: object
6559 /
6560
6561Return an object from a specified module.
6562
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006563If necessary, the module will be imported. Subclasses may override
6564this method (e.g. to restrict unpickling of arbitrary classes and
6565functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006566
6567This method is called whenever a class or a function object is
6568needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006569[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006570
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006571static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006572_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6573 PyObject *module_name,
6574 PyObject *global_name)
6575/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006576{
6577 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006578 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006579
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006580 /* Try to map the old names used in Python 2.x to the new ones used in
6581 Python 3.x. We do this only with old pickle protocols and when the
6582 user has not disabled the feature. */
6583 if (self->proto < 3 && self->fix_imports) {
6584 PyObject *key;
6585 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006586 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006587
6588 /* Check if the global (i.e., a function or a class) was renamed
6589 or moved to another module. */
6590 key = PyTuple_Pack(2, module_name, global_name);
6591 if (key == NULL)
6592 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006593 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006594 Py_DECREF(key);
6595 if (item) {
6596 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6597 PyErr_Format(PyExc_RuntimeError,
6598 "_compat_pickle.NAME_MAPPING values should be "
6599 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6600 return NULL;
6601 }
6602 module_name = PyTuple_GET_ITEM(item, 0);
6603 global_name = PyTuple_GET_ITEM(item, 1);
6604 if (!PyUnicode_Check(module_name) ||
6605 !PyUnicode_Check(global_name)) {
6606 PyErr_Format(PyExc_RuntimeError,
6607 "_compat_pickle.NAME_MAPPING values should be "
6608 "pairs of str, not (%.200s, %.200s)",
6609 Py_TYPE(module_name)->tp_name,
6610 Py_TYPE(global_name)->tp_name);
6611 return NULL;
6612 }
6613 }
6614 else if (PyErr_Occurred()) {
6615 return NULL;
6616 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006617 else {
6618 /* Check if the module was renamed. */
6619 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6620 if (item) {
6621 if (!PyUnicode_Check(item)) {
6622 PyErr_Format(PyExc_RuntimeError,
6623 "_compat_pickle.IMPORT_MAPPING values should be "
6624 "strings, not %.200s", Py_TYPE(item)->tp_name);
6625 return NULL;
6626 }
6627 module_name = item;
6628 }
6629 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006630 return NULL;
6631 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006632 }
6633 }
6634
Eric Snow3f9eee62017-09-15 16:35:20 -06006635 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006636 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006637 if (PyErr_Occurred())
6638 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006639 module = PyImport_Import(module_name);
6640 if (module == NULL)
6641 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006642 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006643 global = getattribute(module, global_name, self->proto >= 4);
6644 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006645 return global;
6646}
6647
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006648/*[clinic input]
6649
6650_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6651
6652Returns size in memory, in bytes.
6653[clinic start generated code]*/
6654
6655static Py_ssize_t
6656_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6657/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6658{
6659 Py_ssize_t res;
6660
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006661 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006662 if (self->memo != NULL)
6663 res += self->memo_size * sizeof(PyObject *);
6664 if (self->marks != NULL)
6665 res += self->marks_size * sizeof(Py_ssize_t);
6666 if (self->input_line != NULL)
6667 res += strlen(self->input_line) + 1;
6668 if (self->encoding != NULL)
6669 res += strlen(self->encoding) + 1;
6670 if (self->errors != NULL)
6671 res += strlen(self->errors) + 1;
6672 return res;
6673}
6674
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006675static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006676 _PICKLE_UNPICKLER_LOAD_METHODDEF
6677 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006678 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006679 {NULL, NULL} /* sentinel */
6680};
6681
6682static void
6683Unpickler_dealloc(UnpicklerObject *self)
6684{
6685 PyObject_GC_UnTrack((PyObject *)self);
6686 Py_XDECREF(self->readline);
6687 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006688 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006689 Py_XDECREF(self->stack);
6690 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006691 if (self->buffer.buf != NULL) {
6692 PyBuffer_Release(&self->buffer);
6693 self->buffer.buf = NULL;
6694 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006695
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006696 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006697 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006698 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006699 PyMem_Free(self->encoding);
6700 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006701
6702 Py_TYPE(self)->tp_free((PyObject *)self);
6703}
6704
6705static int
6706Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6707{
6708 Py_VISIT(self->readline);
6709 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006710 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006711 Py_VISIT(self->stack);
6712 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006713 return 0;
6714}
6715
6716static int
6717Unpickler_clear(UnpicklerObject *self)
6718{
6719 Py_CLEAR(self->readline);
6720 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006721 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006722 Py_CLEAR(self->stack);
6723 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006724 if (self->buffer.buf != NULL) {
6725 PyBuffer_Release(&self->buffer);
6726 self->buffer.buf = NULL;
6727 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006728
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006729 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006730 PyMem_Free(self->marks);
6731 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006732 PyMem_Free(self->input_line);
6733 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006734 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006735 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006736 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006737 self->errors = NULL;
6738
6739 return 0;
6740}
6741
Larry Hastings61272b72014-01-07 12:41:53 -08006742/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006743
6744_pickle.Unpickler.__init__
6745
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006746 file: object
6747 *
6748 fix_imports: bool = True
6749 encoding: str = 'ASCII'
6750 errors: str = 'strict'
6751
6752This takes a binary file for reading a pickle data stream.
6753
6754The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006755protocol argument is needed. Bytes past the pickled object's
6756representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006757
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006758The argument *file* must have two methods, a read() method that takes
6759an integer argument, and a readline() method that requires no
6760arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006761binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006762other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006763
6764Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006765which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006766generated by Python 2. If *fix_imports* is True, pickle will try to
6767map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006768*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006769instances pickled by Python 2; these default to 'ASCII' and 'strict',
6770respectively. The *encoding* can be 'bytes' to read these 8-bit
6771string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006772[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006773
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006774static int
Larry Hastings89964c42015-04-14 18:07:59 -04006775_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6776 int fix_imports, const char *encoding,
6777 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006778/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006779{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006780 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006781
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006782 /* In case of multiple __init__() calls, clear previous content. */
6783 if (self->read != NULL)
6784 (void)Unpickler_clear(self);
6785
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006786 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006787 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006788
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006789 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006790 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006791
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006792 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006793
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006794 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6795 &self->pers_func, &self->pers_func_self) < 0)
6796 {
6797 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006798 }
6799
6800 self->stack = (Pdata *)Pdata_New();
6801 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006802 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006803
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006804 self->memo_size = 32;
6805 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006806 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006807 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006808
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006809 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006810
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006811 return 0;
6812}
6813
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006814
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006815/* Define a proxy object for the Unpickler's internal memo object. This is to
6816 * avoid breaking code like:
6817 * unpickler.memo.clear()
6818 * and
6819 * unpickler.memo = saved_memo
6820 * Is this a good idea? Not really, but we don't want to break code that uses
6821 * it. Note that we don't implement the entire mapping API here. This is
6822 * intentional, as these should be treated as black-box implementation details.
6823 *
6824 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006825 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006826 */
6827
Larry Hastings61272b72014-01-07 12:41:53 -08006828/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006829_pickle.UnpicklerMemoProxy.clear
6830
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006831Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006832[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006833
Larry Hastings3cceb382014-01-04 11:09:09 -08006834static PyObject *
6835_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006836/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006837{
6838 _Unpickler_MemoCleanup(self->unpickler);
6839 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6840 if (self->unpickler->memo == NULL)
6841 return NULL;
6842 Py_RETURN_NONE;
6843}
6844
Larry Hastings61272b72014-01-07 12:41:53 -08006845/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006846_pickle.UnpicklerMemoProxy.copy
6847
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006848Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006849[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006850
Larry Hastings3cceb382014-01-04 11:09:09 -08006851static PyObject *
6852_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006853/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006854{
6855 Py_ssize_t i;
6856 PyObject *new_memo = PyDict_New();
6857 if (new_memo == NULL)
6858 return NULL;
6859
6860 for (i = 0; i < self->unpickler->memo_size; i++) {
6861 int status;
6862 PyObject *key, *value;
6863
6864 value = self->unpickler->memo[i];
6865 if (value == NULL)
6866 continue;
6867
6868 key = PyLong_FromSsize_t(i);
6869 if (key == NULL)
6870 goto error;
6871 status = PyDict_SetItem(new_memo, key, value);
6872 Py_DECREF(key);
6873 if (status < 0)
6874 goto error;
6875 }
6876 return new_memo;
6877
6878error:
6879 Py_DECREF(new_memo);
6880 return NULL;
6881}
6882
Larry Hastings61272b72014-01-07 12:41:53 -08006883/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006884_pickle.UnpicklerMemoProxy.__reduce__
6885
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006886Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006887[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006888
Larry Hastings3cceb382014-01-04 11:09:09 -08006889static PyObject *
6890_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006891/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006892{
6893 PyObject *reduce_value;
6894 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006895 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006896 if (contents == NULL)
6897 return NULL;
6898
6899 reduce_value = PyTuple_New(2);
6900 if (reduce_value == NULL) {
6901 Py_DECREF(contents);
6902 return NULL;
6903 }
6904 constructor_args = PyTuple_New(1);
6905 if (constructor_args == NULL) {
6906 Py_DECREF(contents);
6907 Py_DECREF(reduce_value);
6908 return NULL;
6909 }
6910 PyTuple_SET_ITEM(constructor_args, 0, contents);
6911 Py_INCREF((PyObject *)&PyDict_Type);
6912 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6913 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6914 return reduce_value;
6915}
6916
6917static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006918 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6919 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6920 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006921 {NULL, NULL} /* sentinel */
6922};
6923
6924static void
6925UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6926{
6927 PyObject_GC_UnTrack(self);
6928 Py_XDECREF(self->unpickler);
6929 PyObject_GC_Del((PyObject *)self);
6930}
6931
6932static int
6933UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6934 visitproc visit, void *arg)
6935{
6936 Py_VISIT(self->unpickler);
6937 return 0;
6938}
6939
6940static int
6941UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6942{
6943 Py_CLEAR(self->unpickler);
6944 return 0;
6945}
6946
6947static PyTypeObject UnpicklerMemoProxyType = {
6948 PyVarObject_HEAD_INIT(NULL, 0)
6949 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6950 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6951 0,
6952 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6953 0, /* tp_print */
6954 0, /* tp_getattr */
6955 0, /* tp_setattr */
6956 0, /* tp_compare */
6957 0, /* tp_repr */
6958 0, /* tp_as_number */
6959 0, /* tp_as_sequence */
6960 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006961 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006962 0, /* tp_call */
6963 0, /* tp_str */
6964 PyObject_GenericGetAttr, /* tp_getattro */
6965 PyObject_GenericSetAttr, /* tp_setattro */
6966 0, /* tp_as_buffer */
6967 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6968 0, /* tp_doc */
6969 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6970 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6971 0, /* tp_richcompare */
6972 0, /* tp_weaklistoffset */
6973 0, /* tp_iter */
6974 0, /* tp_iternext */
6975 unpicklerproxy_methods, /* tp_methods */
6976};
6977
6978static PyObject *
6979UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6980{
6981 UnpicklerMemoProxyObject *self;
6982
6983 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6984 &UnpicklerMemoProxyType);
6985 if (self == NULL)
6986 return NULL;
6987 Py_INCREF(unpickler);
6988 self->unpickler = unpickler;
6989 PyObject_GC_Track(self);
6990 return (PyObject *)self;
6991}
6992
6993/*****************************************************************************/
6994
6995
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006996static PyObject *
6997Unpickler_get_memo(UnpicklerObject *self)
6998{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006999 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007000}
7001
7002static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007003Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007004{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007005 PyObject **new_memo;
7006 Py_ssize_t new_memo_size = 0;
7007 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007008
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007009 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007010 PyErr_SetString(PyExc_TypeError,
7011 "attribute deletion is not supported");
7012 return -1;
7013 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007014
7015 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7016 UnpicklerObject *unpickler =
7017 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7018
7019 new_memo_size = unpickler->memo_size;
7020 new_memo = _Unpickler_NewMemo(new_memo_size);
7021 if (new_memo == NULL)
7022 return -1;
7023
7024 for (i = 0; i < new_memo_size; i++) {
7025 Py_XINCREF(unpickler->memo[i]);
7026 new_memo[i] = unpickler->memo[i];
7027 }
7028 }
7029 else if (PyDict_Check(obj)) {
7030 Py_ssize_t i = 0;
7031 PyObject *key, *value;
7032
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007033 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007034 new_memo = _Unpickler_NewMemo(new_memo_size);
7035 if (new_memo == NULL)
7036 return -1;
7037
7038 while (PyDict_Next(obj, &i, &key, &value)) {
7039 Py_ssize_t idx;
7040 if (!PyLong_Check(key)) {
7041 PyErr_SetString(PyExc_TypeError,
7042 "memo key must be integers");
7043 goto error;
7044 }
7045 idx = PyLong_AsSsize_t(key);
7046 if (idx == -1 && PyErr_Occurred())
7047 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007048 if (idx < 0) {
7049 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007050 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007051 goto error;
7052 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007053 if (_Unpickler_MemoPut(self, idx, value) < 0)
7054 goto error;
7055 }
7056 }
7057 else {
7058 PyErr_Format(PyExc_TypeError,
7059 "'memo' attribute must be an UnpicklerMemoProxy object"
7060 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007061 return -1;
7062 }
7063
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007064 _Unpickler_MemoCleanup(self);
7065 self->memo_size = new_memo_size;
7066 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007067
7068 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007069
7070 error:
7071 if (new_memo_size) {
7072 i = new_memo_size;
7073 while (--i >= 0) {
7074 Py_XDECREF(new_memo[i]);
7075 }
7076 PyMem_FREE(new_memo);
7077 }
7078 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007079}
7080
7081static PyObject *
7082Unpickler_get_persload(UnpicklerObject *self)
7083{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007084 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007085 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007086 return NULL;
7087 }
7088 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007089}
7090
7091static int
7092Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
7093{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007094 if (value == NULL) {
7095 PyErr_SetString(PyExc_TypeError,
7096 "attribute deletion is not supported");
7097 return -1;
7098 }
7099 if (!PyCallable_Check(value)) {
7100 PyErr_SetString(PyExc_TypeError,
7101 "persistent_load must be a callable taking "
7102 "one argument");
7103 return -1;
7104 }
7105
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007106 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007107 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007108 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007109
7110 return 0;
7111}
7112
7113static PyGetSetDef Unpickler_getsets[] = {
7114 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7115 {"persistent_load", (getter)Unpickler_get_persload,
7116 (setter)Unpickler_set_persload},
7117 {NULL}
7118};
7119
7120static PyTypeObject Unpickler_Type = {
7121 PyVarObject_HEAD_INIT(NULL, 0)
7122 "_pickle.Unpickler", /*tp_name*/
7123 sizeof(UnpicklerObject), /*tp_basicsize*/
7124 0, /*tp_itemsize*/
7125 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7126 0, /*tp_print*/
7127 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007128 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007129 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007130 0, /*tp_repr*/
7131 0, /*tp_as_number*/
7132 0, /*tp_as_sequence*/
7133 0, /*tp_as_mapping*/
7134 0, /*tp_hash*/
7135 0, /*tp_call*/
7136 0, /*tp_str*/
7137 0, /*tp_getattro*/
7138 0, /*tp_setattro*/
7139 0, /*tp_as_buffer*/
7140 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007141 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007142 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7143 (inquiry)Unpickler_clear, /*tp_clear*/
7144 0, /*tp_richcompare*/
7145 0, /*tp_weaklistoffset*/
7146 0, /*tp_iter*/
7147 0, /*tp_iternext*/
7148 Unpickler_methods, /*tp_methods*/
7149 0, /*tp_members*/
7150 Unpickler_getsets, /*tp_getset*/
7151 0, /*tp_base*/
7152 0, /*tp_dict*/
7153 0, /*tp_descr_get*/
7154 0, /*tp_descr_set*/
7155 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007156 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007157 PyType_GenericAlloc, /*tp_alloc*/
7158 PyType_GenericNew, /*tp_new*/
7159 PyObject_GC_Del, /*tp_free*/
7160 0, /*tp_is_gc*/
7161};
7162
Larry Hastings61272b72014-01-07 12:41:53 -08007163/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007164
7165_pickle.dump
7166
7167 obj: object
7168 file: object
7169 protocol: object = NULL
7170 *
7171 fix_imports: bool = True
7172
7173Write a pickled representation of obj to the open file object file.
7174
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007175This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7176be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007177
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007178The optional *protocol* argument tells the pickler to use the given
7179protocol supported protocols are 0, 1, 2, 3 and 4. The default
7180protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007181
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007182Specifying a negative protocol version selects the highest protocol
7183version supported. The higher the protocol used, the more recent the
7184version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007185
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007186The *file* argument must have a write() method that accepts a single
7187bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007188writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007189this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007190
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007191If *fix_imports* is True and protocol is less than 3, pickle will try
7192to map the new Python 3 names to the old module names used in Python
71932, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007194[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007195
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007196static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007197_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007198 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007199/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007200{
7201 PicklerObject *pickler = _Pickler_New();
7202
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007203 if (pickler == NULL)
7204 return NULL;
7205
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007206 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007207 goto error;
7208
7209 if (_Pickler_SetOutputStream(pickler, file) < 0)
7210 goto error;
7211
7212 if (dump(pickler, obj) < 0)
7213 goto error;
7214
7215 if (_Pickler_FlushToFile(pickler) < 0)
7216 goto error;
7217
7218 Py_DECREF(pickler);
7219 Py_RETURN_NONE;
7220
7221 error:
7222 Py_XDECREF(pickler);
7223 return NULL;
7224}
7225
Larry Hastings61272b72014-01-07 12:41:53 -08007226/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007227
7228_pickle.dumps
7229
7230 obj: object
7231 protocol: object = NULL
7232 *
7233 fix_imports: bool = True
7234
7235Return the pickled representation of the object as a bytes object.
7236
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007237The optional *protocol* argument tells the pickler to use the given
7238protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7239protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007240
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007241Specifying a negative protocol version selects the highest protocol
7242version supported. The higher the protocol used, the more recent the
7243version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007244
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007245If *fix_imports* is True and *protocol* is less than 3, pickle will
7246try to map the new Python 3 names to the old module names used in
7247Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007248[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007249
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007250static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007251_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007252 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007253/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007254{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007255 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007256 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007257
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007258 if (pickler == NULL)
7259 return NULL;
7260
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007261 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007262 goto error;
7263
7264 if (dump(pickler, obj) < 0)
7265 goto error;
7266
7267 result = _Pickler_GetString(pickler);
7268 Py_DECREF(pickler);
7269 return result;
7270
7271 error:
7272 Py_XDECREF(pickler);
7273 return NULL;
7274}
7275
Larry Hastings61272b72014-01-07 12:41:53 -08007276/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007277
7278_pickle.load
7279
7280 file: object
7281 *
7282 fix_imports: bool = True
7283 encoding: str = 'ASCII'
7284 errors: str = 'strict'
7285
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007286Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007287
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007288This is equivalent to ``Unpickler(file).load()``, but may be more
7289efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007290
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007291The protocol version of the pickle is detected automatically, so no
7292protocol argument is needed. Bytes past the pickled object's
7293representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007294
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007295The argument *file* must have two methods, a read() method that takes
7296an integer argument, and a readline() method that requires no
7297arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007298binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007299other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007300
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007301Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007302which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007303generated by Python 2. If *fix_imports* is True, pickle will try to
7304map the old Python 2 names to the new names used in Python 3. The
7305*encoding* and *errors* tell pickle how to decode 8-bit string
7306instances pickled by Python 2; these default to 'ASCII' and 'strict',
7307respectively. The *encoding* can be 'bytes' to read these 8-bit
7308string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007309[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007310
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007311static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007312_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007313 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007314/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007315{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007316 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007317 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007318
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007319 if (unpickler == NULL)
7320 return NULL;
7321
7322 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7323 goto error;
7324
7325 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7326 goto error;
7327
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007328 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007329
7330 result = load(unpickler);
7331 Py_DECREF(unpickler);
7332 return result;
7333
7334 error:
7335 Py_XDECREF(unpickler);
7336 return NULL;
7337}
7338
Larry Hastings61272b72014-01-07 12:41:53 -08007339/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007340
7341_pickle.loads
7342
7343 data: object
7344 *
7345 fix_imports: bool = True
7346 encoding: str = 'ASCII'
7347 errors: str = 'strict'
7348
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007349Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007350
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007351The protocol version of the pickle is detected automatically, so no
7352protocol argument is needed. Bytes past the pickled object's
7353representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007354
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007355Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007356which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007357generated by Python 2. If *fix_imports* is True, pickle will try to
7358map the old Python 2 names to the new names used in Python 3. The
7359*encoding* and *errors* tell pickle how to decode 8-bit string
7360instances pickled by Python 2; these default to 'ASCII' and 'strict',
7361respectively. The *encoding* can be 'bytes' to read these 8-bit
7362string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007363[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007364
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007365static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007366_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007367 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007368/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007369{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007370 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007371 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007372
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007373 if (unpickler == NULL)
7374 return NULL;
7375
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007376 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007377 goto error;
7378
7379 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7380 goto error;
7381
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007382 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007383
7384 result = load(unpickler);
7385 Py_DECREF(unpickler);
7386 return result;
7387
7388 error:
7389 Py_XDECREF(unpickler);
7390 return NULL;
7391}
7392
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007393static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007394 _PICKLE_DUMP_METHODDEF
7395 _PICKLE_DUMPS_METHODDEF
7396 _PICKLE_LOAD_METHODDEF
7397 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007398 {NULL, NULL} /* sentinel */
7399};
7400
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007401static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007402pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007403{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007404 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007405 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007406}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007407
Stefan Krahf483b0f2013-12-14 13:43:10 +01007408static void
7409pickle_free(PyObject *m)
7410{
7411 _Pickle_ClearState(_Pickle_GetState(m));
7412}
7413
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007414static int
7415pickle_traverse(PyObject *m, visitproc visit, void *arg)
7416{
7417 PickleState *st = _Pickle_GetState(m);
7418 Py_VISIT(st->PickleError);
7419 Py_VISIT(st->PicklingError);
7420 Py_VISIT(st->UnpicklingError);
7421 Py_VISIT(st->dispatch_table);
7422 Py_VISIT(st->extension_registry);
7423 Py_VISIT(st->extension_cache);
7424 Py_VISIT(st->inverted_registry);
7425 Py_VISIT(st->name_mapping_2to3);
7426 Py_VISIT(st->import_mapping_2to3);
7427 Py_VISIT(st->name_mapping_3to2);
7428 Py_VISIT(st->import_mapping_3to2);
7429 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007430 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007431 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007432}
7433
7434static struct PyModuleDef _picklemodule = {
7435 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007436 "_pickle", /* m_name */
7437 pickle_module_doc, /* m_doc */
7438 sizeof(PickleState), /* m_size */
7439 pickle_methods, /* m_methods */
7440 NULL, /* m_reload */
7441 pickle_traverse, /* m_traverse */
7442 pickle_clear, /* m_clear */
7443 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007444};
7445
7446PyMODINIT_FUNC
7447PyInit__pickle(void)
7448{
7449 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007450 PickleState *st;
7451
7452 m = PyState_FindModule(&_picklemodule);
7453 if (m) {
7454 Py_INCREF(m);
7455 return m;
7456 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007457
7458 if (PyType_Ready(&Unpickler_Type) < 0)
7459 return NULL;
7460 if (PyType_Ready(&Pickler_Type) < 0)
7461 return NULL;
7462 if (PyType_Ready(&Pdata_Type) < 0)
7463 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007464 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7465 return NULL;
7466 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7467 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007468
7469 /* Create the module and add the functions. */
7470 m = PyModule_Create(&_picklemodule);
7471 if (m == NULL)
7472 return NULL;
7473
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007474 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007475 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7476 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007477 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007478 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7479 return NULL;
7480
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007481 st = _Pickle_GetState(m);
7482
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007483 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007484 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7485 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007486 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007487 st->PicklingError = \
7488 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7489 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007490 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007491 st->UnpicklingError = \
7492 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7493 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007494 return NULL;
7495
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007496 Py_INCREF(st->PickleError);
7497 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007498 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007499 Py_INCREF(st->PicklingError);
7500 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007501 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007502 Py_INCREF(st->UnpicklingError);
7503 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007504 return NULL;
7505
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007506 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007507 return NULL;
7508
7509 return m;
7510}