blob: fc119c0caa4975ac533a515a41735c295be1a304 [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
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001383 PyObject **memo_new = self->memo;
1384 PyMem_RESIZE(memo_new, PyObject *, new_size);
1385 if (memo_new == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001386 PyErr_NoMemory();
1387 return -1;
1388 }
Miss Islington (bot)962051e2018-08-16 00:53:00 -04001389 self->memo = memo_new;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001390 for (i = self->memo_size; i < new_size; i++)
1391 self->memo[i] = NULL;
1392 self->memo_size = new_size;
1393 return 0;
1394}
1395
1396/* Returns NULL if idx is out of bounds. */
1397static PyObject *
1398_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1399{
1400 if (idx < 0 || idx >= self->memo_size)
1401 return NULL;
1402
1403 return self->memo[idx];
1404}
1405
1406/* Returns -1 (with an exception set) on failure, 0 on success.
1407 This takes its own reference to `value`. */
1408static int
1409_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1410{
1411 PyObject *old_item;
1412
1413 if (idx >= self->memo_size) {
1414 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1415 return -1;
1416 assert(idx < self->memo_size);
1417 }
1418 Py_INCREF(value);
1419 old_item = self->memo[idx];
1420 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001421 if (old_item != NULL) {
1422 Py_DECREF(old_item);
1423 }
1424 else {
1425 self->memo_len++;
1426 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001427 return 0;
1428}
1429
1430static PyObject **
1431_Unpickler_NewMemo(Py_ssize_t new_size)
1432{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001433 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001434 if (memo == NULL) {
1435 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001436 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001437 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001438 memset(memo, 0, new_size * sizeof(PyObject *));
1439 return memo;
1440}
1441
1442/* Free the unpickler's memo, taking care to decref any items left in it. */
1443static void
1444_Unpickler_MemoCleanup(UnpicklerObject *self)
1445{
1446 Py_ssize_t i;
1447 PyObject **memo = self->memo;
1448
1449 if (self->memo == NULL)
1450 return;
1451 self->memo = NULL;
1452 i = self->memo_size;
1453 while (--i >= 0) {
1454 Py_XDECREF(memo[i]);
1455 }
1456 PyMem_FREE(memo);
1457}
1458
1459static UnpicklerObject *
1460_Unpickler_New(void)
1461{
1462 UnpicklerObject *self;
1463
1464 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1465 if (self == NULL)
1466 return NULL;
1467
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001468 self->pers_func = NULL;
1469 self->input_buffer = NULL;
1470 self->input_line = NULL;
1471 self->input_len = 0;
1472 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001473 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001474 self->read = NULL;
1475 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001476 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001477 self->encoding = NULL;
1478 self->errors = NULL;
1479 self->marks = NULL;
1480 self->num_marks = 0;
1481 self->marks_size = 0;
1482 self->proto = 0;
1483 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001484 memset(&self->buffer, 0, sizeof(Py_buffer));
1485 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001486 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001487 self->memo = _Unpickler_NewMemo(self->memo_size);
1488 self->stack = (Pdata *)Pdata_New();
1489
1490 if (self->memo == NULL || self->stack == NULL) {
1491 Py_DECREF(self);
1492 return NULL;
1493 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001494
1495 return self;
1496}
1497
1498/* Returns -1 (with an exception set) on failure, 0 on success. This may
1499 be called once on a freshly created Pickler. */
1500static int
1501_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1502{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001503 _Py_IDENTIFIER(peek);
1504 _Py_IDENTIFIER(read);
1505 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001506
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001507 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1508 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001509 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001510 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1511 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001512 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001513 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001514 PyErr_SetString(PyExc_TypeError,
1515 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001516 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001517 Py_CLEAR(self->read);
1518 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001519 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001520 return -1;
1521 }
1522 return 0;
1523}
1524
1525/* Returns -1 (with an exception set) on failure, 0 on success. This may
1526 be called once on a freshly created Pickler. */
1527static int
1528_Unpickler_SetInputEncoding(UnpicklerObject *self,
1529 const char *encoding,
1530 const char *errors)
1531{
1532 if (encoding == NULL)
1533 encoding = "ASCII";
1534 if (errors == NULL)
1535 errors = "strict";
1536
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001537 self->encoding = _PyMem_Strdup(encoding);
1538 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001539 if (self->encoding == NULL || self->errors == NULL) {
1540 PyErr_NoMemory();
1541 return -1;
1542 }
1543 return 0;
1544}
1545
1546/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001547static int
1548memo_get(PicklerObject *self, PyObject *key)
1549{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001550 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001551 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001552 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001553
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001554 value = PyMemoTable_Get(self->memo, key);
1555 if (value == NULL) {
1556 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001557 return -1;
1558 }
1559
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001560 if (!self->bin) {
1561 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001562 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1563 "%" PY_FORMAT_SIZE_T "d\n", *value);
1564 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001565 }
1566 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001567 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001568 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001569 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001570 len = 2;
1571 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001572 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001573 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001574 pdata[1] = (unsigned char)(*value & 0xff);
1575 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1576 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1577 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001578 len = 5;
1579 }
1580 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001581 PickleState *st = _Pickle_GetGlobalState();
1582 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001583 "memo id too large for LONG_BINGET");
1584 return -1;
1585 }
1586 }
1587
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001588 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001589 return -1;
1590
1591 return 0;
1592}
1593
1594/* Store an object in the memo, assign it a new unique ID based on the number
1595 of objects currently stored in the memo and generate a PUT opcode. */
1596static int
1597memo_put(PicklerObject *self, PyObject *obj)
1598{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001599 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001600 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001601 Py_ssize_t idx;
1602
1603 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001604
1605 if (self->fast)
1606 return 0;
1607
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001608 idx = PyMemoTable_Size(self->memo);
1609 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1610 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001611
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001612 if (self->proto >= 4) {
1613 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1614 return -1;
1615 return 0;
1616 }
1617 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001618 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001619 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001620 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001621 len = strlen(pdata);
1622 }
1623 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001624 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001625 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001626 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001627 len = 2;
1628 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001629 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001630 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001631 pdata[1] = (unsigned char)(idx & 0xff);
1632 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1633 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1634 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001635 len = 5;
1636 }
1637 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001638 PickleState *st = _Pickle_GetGlobalState();
1639 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001640 "memo id too large for LONG_BINPUT");
1641 return -1;
1642 }
1643 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001644 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001645 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001646
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001647 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001648}
1649
1650static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001651get_dotted_path(PyObject *obj, PyObject *name)
1652{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001653 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001654 PyObject *dotted_path;
1655 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001656
1657 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001658 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001659 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001660 n = PyList_GET_SIZE(dotted_path);
1661 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001662 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001663 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001664 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001665 if (obj == NULL)
1666 PyErr_Format(PyExc_AttributeError,
1667 "Can't pickle local object %R", name);
1668 else
1669 PyErr_Format(PyExc_AttributeError,
1670 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001671 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001672 return NULL;
1673 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001674 }
1675 return dotted_path;
1676}
1677
1678static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001679get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001680{
1681 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001682 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001683
1684 assert(PyList_CheckExact(names));
1685 Py_INCREF(obj);
1686 n = PyList_GET_SIZE(names);
1687 for (i = 0; i < n; i++) {
1688 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001689 Py_XDECREF(parent);
1690 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001691 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001692 if (obj == NULL) {
1693 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001694 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001695 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001696 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001697 if (pparent != NULL)
1698 *pparent = parent;
1699 else
1700 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001701 return obj;
1702}
1703
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001704
1705static PyObject *
1706getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1707{
1708 PyObject *dotted_path, *attr;
1709
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001710 if (allow_qualname) {
1711 dotted_path = get_dotted_path(obj, name);
1712 if (dotted_path == NULL)
1713 return NULL;
1714 attr = get_deep_attribute(obj, dotted_path, NULL);
1715 Py_DECREF(dotted_path);
1716 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001717 else {
1718 (void)_PyObject_LookupAttr(obj, name, &attr);
1719 }
1720 if (attr == NULL && !PyErr_Occurred()) {
1721 PyErr_Format(PyExc_AttributeError,
1722 "Can't get attribute %R on %R", name, obj);
1723 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001724 return attr;
1725}
1726
Eric Snow3f9eee62017-09-15 16:35:20 -06001727static int
1728_checkmodule(PyObject *module_name, PyObject *module,
1729 PyObject *global, PyObject *dotted_path)
1730{
1731 if (module == Py_None) {
1732 return -1;
1733 }
1734 if (PyUnicode_Check(module_name) &&
1735 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1736 return -1;
1737 }
1738
1739 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1740 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001741 return -1;
1742 }
1743 if (candidate != global) {
1744 Py_DECREF(candidate);
1745 return -1;
1746 }
1747 Py_DECREF(candidate);
1748 return 0;
1749}
1750
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001751static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001752whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001753{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001754 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001755 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001756 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001757 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001758 _Py_IDENTIFIER(__module__);
1759 _Py_IDENTIFIER(modules);
1760 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001761
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001762 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1763 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001764 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001765 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001766 /* In some rare cases (e.g., bound methods of extension types),
1767 __module__ can be None. If it is so, then search sys.modules for
1768 the module of global. */
1769 if (module_name != Py_None)
1770 return module_name;
1771 Py_CLEAR(module_name);
1772 }
1773 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001774
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001775 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001776 modules = _PySys_GetObjectId(&PyId_modules);
1777 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001778 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001779 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001780 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001781 if (PyDict_CheckExact(modules)) {
1782 i = 0;
1783 while (PyDict_Next(modules, &i, &module_name, &module)) {
1784 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1785 Py_INCREF(module_name);
1786 return module_name;
1787 }
1788 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001789 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001790 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001791 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001792 }
1793 else {
1794 PyObject *iterator = PyObject_GetIter(modules);
1795 if (iterator == NULL) {
1796 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001797 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001798 while ((module_name = PyIter_Next(iterator))) {
1799 module = PyObject_GetItem(modules, module_name);
1800 if (module == NULL) {
1801 Py_DECREF(module_name);
1802 Py_DECREF(iterator);
1803 return NULL;
1804 }
1805 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1806 Py_DECREF(module);
1807 Py_DECREF(iterator);
1808 return module_name;
1809 }
1810 Py_DECREF(module);
1811 Py_DECREF(module_name);
1812 if (PyErr_Occurred()) {
1813 Py_DECREF(iterator);
1814 return NULL;
1815 }
1816 }
1817 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001818 }
1819
1820 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001821 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001822 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001823 return module_name;
1824}
1825
1826/* fast_save_enter() and fast_save_leave() are guards against recursive
1827 objects when Pickler is used with the "fast mode" (i.e., with object
1828 memoization disabled). If the nesting of a list or dict object exceed
1829 FAST_NESTING_LIMIT, these guards will start keeping an internal
1830 reference to the seen list or dict objects and check whether these objects
1831 are recursive. These are not strictly necessary, since save() has a
1832 hard-coded recursion limit, but they give a nicer error message than the
1833 typical RuntimeError. */
1834static int
1835fast_save_enter(PicklerObject *self, PyObject *obj)
1836{
1837 /* if fast_nesting < 0, we're doing an error exit. */
1838 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1839 PyObject *key = NULL;
1840 if (self->fast_memo == NULL) {
1841 self->fast_memo = PyDict_New();
1842 if (self->fast_memo == NULL) {
1843 self->fast_nesting = -1;
1844 return 0;
1845 }
1846 }
1847 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001848 if (key == NULL) {
1849 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001850 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001851 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001852 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001853 Py_DECREF(key);
1854 PyErr_Format(PyExc_ValueError,
1855 "fast mode: can't pickle cyclic objects "
1856 "including object type %.200s at %p",
1857 obj->ob_type->tp_name, obj);
1858 self->fast_nesting = -1;
1859 return 0;
1860 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001861 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001862 Py_DECREF(key);
1863 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001864 return 0;
1865 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001866 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1867 Py_DECREF(key);
1868 self->fast_nesting = -1;
1869 return 0;
1870 }
1871 Py_DECREF(key);
1872 }
1873 return 1;
1874}
1875
1876static int
1877fast_save_leave(PicklerObject *self, PyObject *obj)
1878{
1879 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1880 PyObject *key = PyLong_FromVoidPtr(obj);
1881 if (key == NULL)
1882 return 0;
1883 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1884 Py_DECREF(key);
1885 return 0;
1886 }
1887 Py_DECREF(key);
1888 }
1889 return 1;
1890}
1891
1892static int
1893save_none(PicklerObject *self, PyObject *obj)
1894{
1895 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001896 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001897 return -1;
1898
1899 return 0;
1900}
1901
1902static int
1903save_bool(PicklerObject *self, PyObject *obj)
1904{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001905 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001906 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001907 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001908 return -1;
1909 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001910 else {
1911 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1912 * so that unpicklers written before bools were introduced unpickle them
1913 * as ints, but unpicklers after can recognize that bools were intended.
1914 * Note that protocol 2 added direct ways to pickle bools.
1915 */
1916 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1917 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1918 return -1;
1919 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001920 return 0;
1921}
1922
1923static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001924save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001925{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001926 PyObject *repr = NULL;
1927 Py_ssize_t size;
1928 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001929 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001930 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001931
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001932 val= PyLong_AsLongAndOverflow(obj, &overflow);
1933 if (!overflow && (sizeof(long) <= 4 ||
1934 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1935 {
Larry Hastings61272b72014-01-07 12:41:53 -08001936 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001937
1938 Note: we can't use -0x80000000L in the above condition because some
1939 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1940 before applying the unary minus when sizeof(long) <= 4. The
1941 resulting value stays unsigned which is commonly not what we want,
1942 so MSVC happily warns us about it. However, that result would have
1943 been fine because we guard for sizeof(long) <= 4 which turns the
1944 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001945 char pdata[32];
1946 Py_ssize_t len = 0;
1947
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001948 if (self->bin) {
1949 pdata[1] = (unsigned char)(val & 0xff);
1950 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1951 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1952 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001953
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001954 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1955 pdata[0] = BININT;
1956 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001957 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001958 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001959 pdata[0] = BININT2;
1960 len = 3;
1961 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001962 else {
1963 pdata[0] = BININT1;
1964 len = 2;
1965 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001966 }
1967 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001968 sprintf(pdata, "%c%ld\n", INT, val);
1969 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001970 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001971 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001972 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001973
1974 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001975 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001976 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001977
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001978 if (self->proto >= 2) {
1979 /* Linear-time pickling. */
1980 size_t nbits;
1981 size_t nbytes;
1982 unsigned char *pdata;
1983 char header[5];
1984 int i;
1985 int sign = _PyLong_Sign(obj);
1986
1987 if (sign == 0) {
1988 header[0] = LONG1;
1989 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001990 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001991 goto error;
1992 return 0;
1993 }
1994 nbits = _PyLong_NumBits(obj);
1995 if (nbits == (size_t)-1 && PyErr_Occurred())
1996 goto error;
1997 /* How many bytes do we need? There are nbits >> 3 full
1998 * bytes of data, and nbits & 7 leftover bits. If there
1999 * are any leftover bits, then we clearly need another
2000 * byte. Wnat's not so obvious is that we *probably*
2001 * need another byte even if there aren't any leftovers:
2002 * the most-significant bit of the most-significant byte
2003 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002004 * opposite of the one we need. The exception is ints
2005 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002006 * its own 256's-complement, so has the right sign bit
2007 * even without the extra byte. That's a pain to check
2008 * for in advance, though, so we always grab an extra
2009 * byte at the start, and cut it back later if possible.
2010 */
2011 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002012 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002013 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002014 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002015 goto error;
2016 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002017 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002018 if (repr == NULL)
2019 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002020 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002021 i = _PyLong_AsByteArray((PyLongObject *)obj,
2022 pdata, nbytes,
2023 1 /* little endian */ , 1 /* signed */ );
2024 if (i < 0)
2025 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002026 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002027 * needed. This is so iff the MSB is all redundant sign
2028 * bits.
2029 */
2030 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002031 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 pdata[nbytes - 1] == 0xff &&
2033 (pdata[nbytes - 2] & 0x80) != 0) {
2034 nbytes--;
2035 }
2036
2037 if (nbytes < 256) {
2038 header[0] = LONG1;
2039 header[1] = (unsigned char)nbytes;
2040 size = 2;
2041 }
2042 else {
2043 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002044 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002045 for (i = 1; i < 5; i++) {
2046 header[i] = (unsigned char)(size & 0xff);
2047 size >>= 8;
2048 }
2049 size = 5;
2050 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002051 if (_Pickler_Write(self, header, size) < 0 ||
2052 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002053 goto error;
2054 }
2055 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002056 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002057 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002058
Mark Dickinson8dd05142009-01-20 20:43:58 +00002059 /* proto < 2: write the repr and newline. This is quadratic-time (in
2060 the number of digits), in both directions. We add a trailing 'L'
2061 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002062
2063 repr = PyObject_Repr(obj);
2064 if (repr == NULL)
2065 goto error;
2066
Serhiy Storchaka06515832016-11-20 09:13:07 +02002067 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002068 if (string == NULL)
2069 goto error;
2070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002071 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2072 _Pickler_Write(self, string, size) < 0 ||
2073 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002074 goto error;
2075 }
2076
2077 if (0) {
2078 error:
2079 status = -1;
2080 }
2081 Py_XDECREF(repr);
2082
2083 return status;
2084}
2085
2086static int
2087save_float(PicklerObject *self, PyObject *obj)
2088{
2089 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2090
2091 if (self->bin) {
2092 char pdata[9];
2093 pdata[0] = BINFLOAT;
2094 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2095 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002096 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002097 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002098 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002100 int result = -1;
2101 char *buf = NULL;
2102 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002103
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002104 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002105 goto done;
2106
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002107 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002108 if (!buf) {
2109 PyErr_NoMemory();
2110 goto done;
2111 }
2112
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002113 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002114 goto done;
2115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002116 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002117 goto done;
2118
2119 result = 0;
2120done:
2121 PyMem_Free(buf);
2122 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002123 }
2124
2125 return 0;
2126}
2127
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002128/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002129
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002130 The large contiguous data is written directly into the underlying file
2131 object, bypassing the output_buffer of the Pickler. We intentionally
2132 do not insert a protocol 4 frame opcode to make it possible to optimize
2133 file.read calls in the loader.
2134 */
2135static int
2136_Pickler_write_bytes(PicklerObject *self,
2137 const char *header, Py_ssize_t header_size,
2138 const char *data, Py_ssize_t data_size,
2139 PyObject *payload)
2140{
2141 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2142 int framing = self->framing;
2143
2144 if (bypass_buffer) {
2145 assert(self->output_buffer != NULL);
2146 /* Commit the previous frame. */
2147 if (_Pickler_CommitFrame(self)) {
2148 return -1;
2149 }
2150 /* Disable framing temporarily */
2151 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002152 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002153
2154 if (_Pickler_Write(self, header, header_size) < 0) {
2155 return -1;
2156 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002157
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002158 if (bypass_buffer && self->write != NULL) {
2159 /* Bypass the in-memory buffer to directly stream large data
2160 into the underlying file object. */
2161 PyObject *result, *mem = NULL;
2162 /* Dump the output buffer to the file. */
2163 if (_Pickler_FlushToFile(self) < 0) {
2164 return -1;
2165 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002166
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002167 /* Stream write the payload into the file without going through the
2168 output buffer. */
2169 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002170 /* TODO: It would be better to use a memoryview with a linked
2171 original string if this is possible. */
2172 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002173 if (payload == NULL) {
2174 return -1;
2175 }
2176 }
2177 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2178 Py_XDECREF(mem);
2179 if (result == NULL) {
2180 return -1;
2181 }
2182 Py_DECREF(result);
2183
2184 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2185 if (_Pickler_ClearBuffer(self) < 0) {
2186 return -1;
2187 }
2188 }
2189 else {
2190 if (_Pickler_Write(self, data, data_size) < 0) {
2191 return -1;
2192 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002193 }
2194
2195 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002196 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002197
2198 return 0;
2199}
2200
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002201static int
2202save_bytes(PicklerObject *self, PyObject *obj)
2203{
2204 if (self->proto < 3) {
2205 /* Older pickle protocols do not have an opcode for pickling bytes
2206 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002207 the __reduce__ method) to permit bytes object unpickling.
2208
2209 Here we use a hack to be compatible with Python 2. Since in Python
2210 2 'bytes' is just an alias for 'str' (which has different
2211 parameters than the actual bytes object), we use codecs.encode
2212 to create the appropriate 'str' object when unpickled using
2213 Python 2 *and* the appropriate 'bytes' object when unpickled
2214 using Python 3. Again this is a hack and we don't need to do this
2215 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002216 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002217 int status;
2218
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002219 if (PyBytes_GET_SIZE(obj) == 0) {
2220 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2221 }
2222 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002223 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002224 PyObject *unicode_str =
2225 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2226 PyBytes_GET_SIZE(obj),
2227 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002228 _Py_IDENTIFIER(latin1);
2229
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002230 if (unicode_str == NULL)
2231 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002232 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002233 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002234 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002235 Py_DECREF(unicode_str);
2236 }
2237
2238 if (reduce_value == NULL)
2239 return -1;
2240
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002241 /* save_reduce() will memoize the object automatically. */
2242 status = save_reduce(self, reduce_value, obj);
2243 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002244 return status;
2245 }
2246 else {
2247 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002248 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002249 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002250
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002251 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 if (size < 0)
2253 return -1;
2254
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002255 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256 header[0] = SHORT_BINBYTES;
2257 header[1] = (unsigned char)size;
2258 len = 2;
2259 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002260 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002261 header[0] = BINBYTES;
2262 header[1] = (unsigned char)(size & 0xff);
2263 header[2] = (unsigned char)((size >> 8) & 0xff);
2264 header[3] = (unsigned char)((size >> 16) & 0xff);
2265 header[4] = (unsigned char)((size >> 24) & 0xff);
2266 len = 5;
2267 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002268 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002269 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002270 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002271 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002272 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002273 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002274 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002275 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 return -1; /* string too large */
2277 }
2278
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002279 if (_Pickler_write_bytes(self, header, len,
2280 PyBytes_AS_STRING(obj), size, obj) < 0)
2281 {
2282 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002283 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002284
2285 if (memo_put(self, obj) < 0)
2286 return -1;
2287
2288 return 0;
2289 }
2290}
2291
2292/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2293 backslash and newline characters to \uXXXX escapes. */
2294static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002295raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002297 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002298 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002299 void *data;
2300 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002301 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002302
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002303 if (PyUnicode_READY(obj))
2304 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002305
Victor Stinner358af132015-10-12 22:36:57 +02002306 _PyBytesWriter_Init(&writer);
2307
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002308 size = PyUnicode_GET_LENGTH(obj);
2309 data = PyUnicode_DATA(obj);
2310 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002311
Victor Stinner358af132015-10-12 22:36:57 +02002312 p = _PyBytesWriter_Alloc(&writer, size);
2313 if (p == NULL)
2314 goto error;
2315 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002316
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002317 for (i=0; i < size; i++) {
2318 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002319 /* Map 32-bit characters to '\Uxxxxxxxx' */
2320 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002321 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002322 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2323 if (p == NULL)
2324 goto error;
2325
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002326 *p++ = '\\';
2327 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002328 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2329 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2330 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2331 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2332 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2333 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2334 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2335 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002336 }
Victor Stinner358af132015-10-12 22:36:57 +02002337 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002338 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002339 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002340 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2341 if (p == NULL)
2342 goto error;
2343
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002344 *p++ = '\\';
2345 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002346 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2347 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2348 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2349 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002350 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002351 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002352 else
2353 *p++ = (char) ch;
2354 }
Victor Stinner358af132015-10-12 22:36:57 +02002355
2356 return _PyBytesWriter_Finish(&writer, p);
2357
2358error:
2359 _PyBytesWriter_Dealloc(&writer);
2360 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002361}
2362
2363static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002364write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002365{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002366 char header[9];
2367 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002368 PyObject *encoded = NULL;
2369 Py_ssize_t size;
2370 const char *data;
2371
2372 if (PyUnicode_READY(obj))
2373 return -1;
2374
2375 data = PyUnicode_AsUTF8AndSize(obj, &size);
2376 if (data == NULL) {
2377 /* Issue #8383: for strings with lone surrogates, fallback on the
2378 "surrogatepass" error handler. */
2379 PyErr_Clear();
2380 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2381 if (encoded == NULL)
2382 return -1;
2383
2384 data = PyBytes_AS_STRING(encoded);
2385 size = PyBytes_GET_SIZE(encoded);
2386 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002387
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002388 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002389 if (size <= 0xff && self->proto >= 4) {
2390 header[0] = SHORT_BINUNICODE;
2391 header[1] = (unsigned char)(size & 0xff);
2392 len = 2;
2393 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002394 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002395 header[0] = BINUNICODE;
2396 header[1] = (unsigned char)(size & 0xff);
2397 header[2] = (unsigned char)((size >> 8) & 0xff);
2398 header[3] = (unsigned char)((size >> 16) & 0xff);
2399 header[4] = (unsigned char)((size >> 24) & 0xff);
2400 len = 5;
2401 }
2402 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002403 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002404 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002405 len = 9;
2406 }
2407 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002408 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002409 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002410 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002411 return -1;
2412 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002413
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002414 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2415 Py_XDECREF(encoded);
2416 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002417 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002418 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002419 return 0;
2420}
2421
2422static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002423save_unicode(PicklerObject *self, PyObject *obj)
2424{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002425 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002426 if (write_unicode_binary(self, obj) < 0)
2427 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002428 }
2429 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002430 PyObject *encoded;
2431 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002432 const char unicode_op = UNICODE;
2433
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002434 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002435 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002436 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002437
Antoine Pitrou299978d2013-04-07 17:38:11 +02002438 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2439 Py_DECREF(encoded);
2440 return -1;
2441 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002442
2443 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002444 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2445 Py_DECREF(encoded);
2446 return -1;
2447 }
2448 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002449
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002450 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002451 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002452 }
2453 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002454 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002456 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002457}
2458
2459/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2460static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002461store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002462{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002463 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002464
2465 assert(PyTuple_Size(t) == len);
2466
2467 for (i = 0; i < len; i++) {
2468 PyObject *element = PyTuple_GET_ITEM(t, i);
2469
2470 if (element == NULL)
2471 return -1;
2472 if (save(self, element, 0) < 0)
2473 return -1;
2474 }
2475
2476 return 0;
2477}
2478
2479/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2480 * used across protocols to minimize the space needed to pickle them.
2481 * Tuples are also the only builtin immutable type that can be recursive
2482 * (a tuple can be reached from itself), and that requires some subtle
2483 * magic so that it works in all cases. IOW, this is a long routine.
2484 */
2485static int
2486save_tuple(PicklerObject *self, PyObject *obj)
2487{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002488 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002489
2490 const char mark_op = MARK;
2491 const char tuple_op = TUPLE;
2492 const char pop_op = POP;
2493 const char pop_mark_op = POP_MARK;
2494 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2495
2496 if ((len = PyTuple_Size(obj)) < 0)
2497 return -1;
2498
2499 if (len == 0) {
2500 char pdata[2];
2501
2502 if (self->proto) {
2503 pdata[0] = EMPTY_TUPLE;
2504 len = 1;
2505 }
2506 else {
2507 pdata[0] = MARK;
2508 pdata[1] = TUPLE;
2509 len = 2;
2510 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002511 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002512 return -1;
2513 return 0;
2514 }
2515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002516 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517 * saving the tuple elements, the tuple must be recursive, in
2518 * which case we'll pop everything we put on the stack, and fetch
2519 * its value from the memo.
2520 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002521 if (len <= 3 && self->proto >= 2) {
2522 /* Use TUPLE{1,2,3} opcodes. */
2523 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002524 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002526 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002527 /* pop the len elements */
2528 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002529 if (_Pickler_Write(self, &pop_op, 1) < 0)
2530 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002531 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002532 if (memo_get(self, obj) < 0)
2533 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002534
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002535 return 0;
2536 }
2537 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002538 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2539 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002540 }
2541 goto memoize;
2542 }
2543
2544 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2545 * Generate MARK e1 e2 ... TUPLE
2546 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002547 if (_Pickler_Write(self, &mark_op, 1) < 0)
2548 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002549
2550 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002551 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002552
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002553 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002554 /* pop the stack stuff we pushed */
2555 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002556 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2557 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002558 }
2559 else {
2560 /* Note that we pop one more than len, to remove
2561 * the MARK too.
2562 */
2563 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002564 if (_Pickler_Write(self, &pop_op, 1) < 0)
2565 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002566 }
2567 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002568 if (memo_get(self, obj) < 0)
2569 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002570
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002571 return 0;
2572 }
2573 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002574 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2575 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002576 }
2577
2578 memoize:
2579 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002580 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002581
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002582 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002583}
2584
2585/* iter is an iterator giving items, and we batch up chunks of
2586 * MARK item item ... item APPENDS
2587 * opcode sequences. Calling code should have arranged to first create an
2588 * empty list, or list-like object, for the APPENDS to operate on.
2589 * Returns 0 on success, <0 on error.
2590 */
2591static int
2592batch_list(PicklerObject *self, PyObject *iter)
2593{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002594 PyObject *obj = NULL;
2595 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002596 int i, n;
2597
2598 const char mark_op = MARK;
2599 const char append_op = APPEND;
2600 const char appends_op = APPENDS;
2601
2602 assert(iter != NULL);
2603
2604 /* XXX: I think this function could be made faster by avoiding the
2605 iterator interface and fetching objects directly from list using
2606 PyList_GET_ITEM.
2607 */
2608
2609 if (self->proto == 0) {
2610 /* APPENDS isn't available; do one at a time. */
2611 for (;;) {
2612 obj = PyIter_Next(iter);
2613 if (obj == NULL) {
2614 if (PyErr_Occurred())
2615 return -1;
2616 break;
2617 }
2618 i = save(self, obj, 0);
2619 Py_DECREF(obj);
2620 if (i < 0)
2621 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002622 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002623 return -1;
2624 }
2625 return 0;
2626 }
2627
2628 /* proto > 0: write in batches of BATCHSIZE. */
2629 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002630 /* Get first item */
2631 firstitem = PyIter_Next(iter);
2632 if (firstitem == NULL) {
2633 if (PyErr_Occurred())
2634 goto error;
2635
2636 /* nothing more to add */
2637 break;
2638 }
2639
2640 /* Try to get a second item */
2641 obj = PyIter_Next(iter);
2642 if (obj == NULL) {
2643 if (PyErr_Occurred())
2644 goto error;
2645
2646 /* Only one item to write */
2647 if (save(self, firstitem, 0) < 0)
2648 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002649 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002650 goto error;
2651 Py_CLEAR(firstitem);
2652 break;
2653 }
2654
2655 /* More than one item to write */
2656
2657 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002658 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002659 goto error;
2660
2661 if (save(self, firstitem, 0) < 0)
2662 goto error;
2663 Py_CLEAR(firstitem);
2664 n = 1;
2665
2666 /* Fetch and save up to BATCHSIZE items */
2667 while (obj) {
2668 if (save(self, obj, 0) < 0)
2669 goto error;
2670 Py_CLEAR(obj);
2671 n += 1;
2672
2673 if (n == BATCHSIZE)
2674 break;
2675
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002676 obj = PyIter_Next(iter);
2677 if (obj == NULL) {
2678 if (PyErr_Occurred())
2679 goto error;
2680 break;
2681 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002682 }
2683
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002684 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002685 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002686
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002687 } while (n == BATCHSIZE);
2688 return 0;
2689
2690 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002691 Py_XDECREF(firstitem);
2692 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002693 return -1;
2694}
2695
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002696/* This is a variant of batch_list() above, specialized for lists (with no
2697 * support for list subclasses). Like batch_list(), we batch up chunks of
2698 * MARK item item ... item APPENDS
2699 * opcode sequences. Calling code should have arranged to first create an
2700 * empty list, or list-like object, for the APPENDS to operate on.
2701 * Returns 0 on success, -1 on error.
2702 *
2703 * This version is considerably faster than batch_list(), if less general.
2704 *
2705 * Note that this only works for protocols > 0.
2706 */
2707static int
2708batch_list_exact(PicklerObject *self, PyObject *obj)
2709{
2710 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002711 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002712
2713 const char append_op = APPEND;
2714 const char appends_op = APPENDS;
2715 const char mark_op = MARK;
2716
2717 assert(obj != NULL);
2718 assert(self->proto > 0);
2719 assert(PyList_CheckExact(obj));
2720
2721 if (PyList_GET_SIZE(obj) == 1) {
2722 item = PyList_GET_ITEM(obj, 0);
2723 if (save(self, item, 0) < 0)
2724 return -1;
2725 if (_Pickler_Write(self, &append_op, 1) < 0)
2726 return -1;
2727 return 0;
2728 }
2729
2730 /* Write in batches of BATCHSIZE. */
2731 total = 0;
2732 do {
2733 this_batch = 0;
2734 if (_Pickler_Write(self, &mark_op, 1) < 0)
2735 return -1;
2736 while (total < PyList_GET_SIZE(obj)) {
2737 item = PyList_GET_ITEM(obj, total);
2738 if (save(self, item, 0) < 0)
2739 return -1;
2740 total++;
2741 if (++this_batch == BATCHSIZE)
2742 break;
2743 }
2744 if (_Pickler_Write(self, &appends_op, 1) < 0)
2745 return -1;
2746
2747 } while (total < PyList_GET_SIZE(obj));
2748
2749 return 0;
2750}
2751
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002752static int
2753save_list(PicklerObject *self, PyObject *obj)
2754{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002755 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002756 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002757 int status = 0;
2758
2759 if (self->fast && !fast_save_enter(self, obj))
2760 goto error;
2761
2762 /* Create an empty list. */
2763 if (self->bin) {
2764 header[0] = EMPTY_LIST;
2765 len = 1;
2766 }
2767 else {
2768 header[0] = MARK;
2769 header[1] = LIST;
2770 len = 2;
2771 }
2772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002773 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002774 goto error;
2775
2776 /* Get list length, and bow out early if empty. */
2777 if ((len = PyList_Size(obj)) < 0)
2778 goto error;
2779
2780 if (memo_put(self, obj) < 0)
2781 goto error;
2782
2783 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002784 /* Materialize the list elements. */
2785 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002786 if (Py_EnterRecursiveCall(" while pickling an object"))
2787 goto error;
2788 status = batch_list_exact(self, obj);
2789 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002790 } else {
2791 PyObject *iter = PyObject_GetIter(obj);
2792 if (iter == NULL)
2793 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002794
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002795 if (Py_EnterRecursiveCall(" while pickling an object")) {
2796 Py_DECREF(iter);
2797 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002798 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002799 status = batch_list(self, iter);
2800 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002801 Py_DECREF(iter);
2802 }
2803 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002804 if (0) {
2805 error:
2806 status = -1;
2807 }
2808
2809 if (self->fast && !fast_save_leave(self, obj))
2810 status = -1;
2811
2812 return status;
2813}
2814
2815/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2816 * MARK key value ... key value SETITEMS
2817 * opcode sequences. Calling code should have arranged to first create an
2818 * empty dict, or dict-like object, for the SETITEMS to operate on.
2819 * Returns 0 on success, <0 on error.
2820 *
2821 * This is very much like batch_list(). The difference between saving
2822 * elements directly, and picking apart two-tuples, is so long-winded at
2823 * the C level, though, that attempts to combine these routines were too
2824 * ugly to bear.
2825 */
2826static int
2827batch_dict(PicklerObject *self, PyObject *iter)
2828{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002829 PyObject *obj = NULL;
2830 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002831 int i, n;
2832
2833 const char mark_op = MARK;
2834 const char setitem_op = SETITEM;
2835 const char setitems_op = SETITEMS;
2836
2837 assert(iter != NULL);
2838
2839 if (self->proto == 0) {
2840 /* SETITEMS isn't available; do one at a time. */
2841 for (;;) {
2842 obj = PyIter_Next(iter);
2843 if (obj == NULL) {
2844 if (PyErr_Occurred())
2845 return -1;
2846 break;
2847 }
2848 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2849 PyErr_SetString(PyExc_TypeError, "dict items "
2850 "iterator must return 2-tuples");
2851 return -1;
2852 }
2853 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2854 if (i >= 0)
2855 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2856 Py_DECREF(obj);
2857 if (i < 0)
2858 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002859 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002860 return -1;
2861 }
2862 return 0;
2863 }
2864
2865 /* proto > 0: write in batches of BATCHSIZE. */
2866 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002867 /* Get first item */
2868 firstitem = PyIter_Next(iter);
2869 if (firstitem == NULL) {
2870 if (PyErr_Occurred())
2871 goto error;
2872
2873 /* nothing more to add */
2874 break;
2875 }
2876 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2877 PyErr_SetString(PyExc_TypeError, "dict items "
2878 "iterator must return 2-tuples");
2879 goto error;
2880 }
2881
2882 /* Try to get a second item */
2883 obj = PyIter_Next(iter);
2884 if (obj == NULL) {
2885 if (PyErr_Occurred())
2886 goto error;
2887
2888 /* Only one item to write */
2889 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2890 goto error;
2891 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2892 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002893 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002894 goto error;
2895 Py_CLEAR(firstitem);
2896 break;
2897 }
2898
2899 /* More than one item to write */
2900
2901 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002902 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002903 goto error;
2904
2905 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2906 goto error;
2907 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2908 goto error;
2909 Py_CLEAR(firstitem);
2910 n = 1;
2911
2912 /* Fetch and save up to BATCHSIZE items */
2913 while (obj) {
2914 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2915 PyErr_SetString(PyExc_TypeError, "dict items "
2916 "iterator must return 2-tuples");
2917 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002918 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002919 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2920 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2921 goto error;
2922 Py_CLEAR(obj);
2923 n += 1;
2924
2925 if (n == BATCHSIZE)
2926 break;
2927
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002928 obj = PyIter_Next(iter);
2929 if (obj == NULL) {
2930 if (PyErr_Occurred())
2931 goto error;
2932 break;
2933 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002934 }
2935
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002936 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002937 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002938
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002939 } while (n == BATCHSIZE);
2940 return 0;
2941
2942 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002943 Py_XDECREF(firstitem);
2944 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002945 return -1;
2946}
2947
Collin Winter5c9b02d2009-05-25 05:43:30 +00002948/* This is a variant of batch_dict() above that specializes for dicts, with no
2949 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2950 * MARK key value ... key value SETITEMS
2951 * opcode sequences. Calling code should have arranged to first create an
2952 * empty dict, or dict-like object, for the SETITEMS to operate on.
2953 * Returns 0 on success, -1 on error.
2954 *
2955 * Note that this currently doesn't work for protocol 0.
2956 */
2957static int
2958batch_dict_exact(PicklerObject *self, PyObject *obj)
2959{
2960 PyObject *key = NULL, *value = NULL;
2961 int i;
2962 Py_ssize_t dict_size, ppos = 0;
2963
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002964 const char mark_op = MARK;
2965 const char setitem_op = SETITEM;
2966 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002967
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002968 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002969 assert(self->proto > 0);
2970
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002971 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002972
2973 /* Special-case len(d) == 1 to save space. */
2974 if (dict_size == 1) {
2975 PyDict_Next(obj, &ppos, &key, &value);
2976 if (save(self, key, 0) < 0)
2977 return -1;
2978 if (save(self, value, 0) < 0)
2979 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002980 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002981 return -1;
2982 return 0;
2983 }
2984
2985 /* Write in batches of BATCHSIZE. */
2986 do {
2987 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002988 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002989 return -1;
2990 while (PyDict_Next(obj, &ppos, &key, &value)) {
2991 if (save(self, key, 0) < 0)
2992 return -1;
2993 if (save(self, value, 0) < 0)
2994 return -1;
2995 if (++i == BATCHSIZE)
2996 break;
2997 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002998 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002999 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003000 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003001 PyErr_Format(
3002 PyExc_RuntimeError,
3003 "dictionary changed size during iteration");
3004 return -1;
3005 }
3006
3007 } while (i == BATCHSIZE);
3008 return 0;
3009}
3010
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003011static int
3012save_dict(PicklerObject *self, PyObject *obj)
3013{
3014 PyObject *items, *iter;
3015 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003016 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003017 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003018 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003019
3020 if (self->fast && !fast_save_enter(self, obj))
3021 goto error;
3022
3023 /* Create an empty dict. */
3024 if (self->bin) {
3025 header[0] = EMPTY_DICT;
3026 len = 1;
3027 }
3028 else {
3029 header[0] = MARK;
3030 header[1] = DICT;
3031 len = 2;
3032 }
3033
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003034 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003035 goto error;
3036
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003037 if (memo_put(self, obj) < 0)
3038 goto error;
3039
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003040 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003041 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003042 if (PyDict_CheckExact(obj) && self->proto > 0) {
3043 /* We can take certain shortcuts if we know this is a dict and
3044 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003045 if (Py_EnterRecursiveCall(" while pickling an object"))
3046 goto error;
3047 status = batch_dict_exact(self, obj);
3048 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003049 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003050 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003051
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003052 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003053 if (items == NULL)
3054 goto error;
3055 iter = PyObject_GetIter(items);
3056 Py_DECREF(items);
3057 if (iter == NULL)
3058 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003059 if (Py_EnterRecursiveCall(" while pickling an object")) {
3060 Py_DECREF(iter);
3061 goto error;
3062 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003063 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003064 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003065 Py_DECREF(iter);
3066 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003067 }
3068
3069 if (0) {
3070 error:
3071 status = -1;
3072 }
3073
3074 if (self->fast && !fast_save_leave(self, obj))
3075 status = -1;
3076
3077 return status;
3078}
3079
3080static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003081save_set(PicklerObject *self, PyObject *obj)
3082{
3083 PyObject *item;
3084 int i;
3085 Py_ssize_t set_size, ppos = 0;
3086 Py_hash_t hash;
3087
3088 const char empty_set_op = EMPTY_SET;
3089 const char mark_op = MARK;
3090 const char additems_op = ADDITEMS;
3091
3092 if (self->proto < 4) {
3093 PyObject *items;
3094 PyObject *reduce_value;
3095 int status;
3096
3097 items = PySequence_List(obj);
3098 if (items == NULL) {
3099 return -1;
3100 }
3101 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3102 Py_DECREF(items);
3103 if (reduce_value == NULL) {
3104 return -1;
3105 }
3106 /* save_reduce() will memoize the object automatically. */
3107 status = save_reduce(self, reduce_value, obj);
3108 Py_DECREF(reduce_value);
3109 return status;
3110 }
3111
3112 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3113 return -1;
3114
3115 if (memo_put(self, obj) < 0)
3116 return -1;
3117
3118 set_size = PySet_GET_SIZE(obj);
3119 if (set_size == 0)
3120 return 0; /* nothing to do */
3121
3122 /* Write in batches of BATCHSIZE. */
3123 do {
3124 i = 0;
3125 if (_Pickler_Write(self, &mark_op, 1) < 0)
3126 return -1;
3127 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3128 if (save(self, item, 0) < 0)
3129 return -1;
3130 if (++i == BATCHSIZE)
3131 break;
3132 }
3133 if (_Pickler_Write(self, &additems_op, 1) < 0)
3134 return -1;
3135 if (PySet_GET_SIZE(obj) != set_size) {
3136 PyErr_Format(
3137 PyExc_RuntimeError,
3138 "set changed size during iteration");
3139 return -1;
3140 }
3141 } while (i == BATCHSIZE);
3142
3143 return 0;
3144}
3145
3146static int
3147save_frozenset(PicklerObject *self, PyObject *obj)
3148{
3149 PyObject *iter;
3150
3151 const char mark_op = MARK;
3152 const char frozenset_op = FROZENSET;
3153
3154 if (self->fast && !fast_save_enter(self, obj))
3155 return -1;
3156
3157 if (self->proto < 4) {
3158 PyObject *items;
3159 PyObject *reduce_value;
3160 int status;
3161
3162 items = PySequence_List(obj);
3163 if (items == NULL) {
3164 return -1;
3165 }
3166 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3167 items);
3168 Py_DECREF(items);
3169 if (reduce_value == NULL) {
3170 return -1;
3171 }
3172 /* save_reduce() will memoize the object automatically. */
3173 status = save_reduce(self, reduce_value, obj);
3174 Py_DECREF(reduce_value);
3175 return status;
3176 }
3177
3178 if (_Pickler_Write(self, &mark_op, 1) < 0)
3179 return -1;
3180
3181 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003182 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003183 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003184 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003185 for (;;) {
3186 PyObject *item;
3187
3188 item = PyIter_Next(iter);
3189 if (item == NULL) {
3190 if (PyErr_Occurred()) {
3191 Py_DECREF(iter);
3192 return -1;
3193 }
3194 break;
3195 }
3196 if (save(self, item, 0) < 0) {
3197 Py_DECREF(item);
3198 Py_DECREF(iter);
3199 return -1;
3200 }
3201 Py_DECREF(item);
3202 }
3203 Py_DECREF(iter);
3204
3205 /* If the object is already in the memo, this means it is
3206 recursive. In this case, throw away everything we put on the
3207 stack, and fetch the object back from the memo. */
3208 if (PyMemoTable_Get(self->memo, obj)) {
3209 const char pop_mark_op = POP_MARK;
3210
3211 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3212 return -1;
3213 if (memo_get(self, obj) < 0)
3214 return -1;
3215 return 0;
3216 }
3217
3218 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3219 return -1;
3220 if (memo_put(self, obj) < 0)
3221 return -1;
3222
3223 return 0;
3224}
3225
3226static int
3227fix_imports(PyObject **module_name, PyObject **global_name)
3228{
3229 PyObject *key;
3230 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003231 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003232
3233 key = PyTuple_Pack(2, *module_name, *global_name);
3234 if (key == NULL)
3235 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003236 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003237 Py_DECREF(key);
3238 if (item) {
3239 PyObject *fixed_module_name;
3240 PyObject *fixed_global_name;
3241
3242 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3243 PyErr_Format(PyExc_RuntimeError,
3244 "_compat_pickle.REVERSE_NAME_MAPPING values "
3245 "should be 2-tuples, not %.200s",
3246 Py_TYPE(item)->tp_name);
3247 return -1;
3248 }
3249 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3250 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3251 if (!PyUnicode_Check(fixed_module_name) ||
3252 !PyUnicode_Check(fixed_global_name)) {
3253 PyErr_Format(PyExc_RuntimeError,
3254 "_compat_pickle.REVERSE_NAME_MAPPING values "
3255 "should be pairs of str, not (%.200s, %.200s)",
3256 Py_TYPE(fixed_module_name)->tp_name,
3257 Py_TYPE(fixed_global_name)->tp_name);
3258 return -1;
3259 }
3260
3261 Py_CLEAR(*module_name);
3262 Py_CLEAR(*global_name);
3263 Py_INCREF(fixed_module_name);
3264 Py_INCREF(fixed_global_name);
3265 *module_name = fixed_module_name;
3266 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003267 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003268 }
3269 else if (PyErr_Occurred()) {
3270 return -1;
3271 }
3272
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003273 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003274 if (item) {
3275 if (!PyUnicode_Check(item)) {
3276 PyErr_Format(PyExc_RuntimeError,
3277 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3278 "should be strings, not %.200s",
3279 Py_TYPE(item)->tp_name);
3280 return -1;
3281 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003282 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003283 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003284 }
3285 else if (PyErr_Occurred()) {
3286 return -1;
3287 }
3288
3289 return 0;
3290}
3291
3292static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003293save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3294{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003295 PyObject *global_name = NULL;
3296 PyObject *module_name = NULL;
3297 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003298 PyObject *parent = NULL;
3299 PyObject *dotted_path = NULL;
3300 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003301 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003302 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003303 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003304 _Py_IDENTIFIER(__name__);
3305 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003306
3307 const char global_op = GLOBAL;
3308
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003309 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003310 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003311 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003312 }
3313 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003314 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3315 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003316 if (global_name == NULL) {
3317 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3318 if (global_name == NULL)
3319 goto error;
3320 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003321 }
3322
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003323 dotted_path = get_dotted_path(module, global_name);
3324 if (dotted_path == NULL)
3325 goto error;
3326 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003327 if (module_name == NULL)
3328 goto error;
3329
3330 /* XXX: Change to use the import C API directly with level=0 to disallow
3331 relative imports.
3332
3333 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3334 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3335 custom import functions (IMHO, this would be a nice security
3336 feature). The import C API would need to be extended to support the
3337 extra parameters of __import__ to fix that. */
3338 module = PyImport_Import(module_name);
3339 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003340 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003341 "Can't pickle %R: import of module %R failed",
3342 obj, module_name);
3343 goto error;
3344 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003345 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3346 Py_INCREF(lastname);
3347 cls = get_deep_attribute(module, dotted_path, &parent);
3348 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003349 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003350 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003351 "Can't pickle %R: attribute lookup %S on %S failed",
3352 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003353 goto error;
3354 }
3355 if (cls != obj) {
3356 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003357 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003358 "Can't pickle %R: it's not the same object as %S.%S",
3359 obj, module_name, global_name);
3360 goto error;
3361 }
3362 Py_DECREF(cls);
3363
3364 if (self->proto >= 2) {
3365 /* See whether this is in the extension registry, and if
3366 * so generate an EXT opcode.
3367 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003368 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003369 PyObject *code_obj; /* extension code as Python object */
3370 long code; /* extension code as C value */
3371 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003372 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003373
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003374 extension_key = PyTuple_Pack(2, module_name, global_name);
3375 if (extension_key == NULL) {
3376 goto error;
3377 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003378 code_obj = PyDict_GetItemWithError(st->extension_registry,
3379 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003380 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003381 /* The object is not registered in the extension registry.
3382 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003383 if (code_obj == NULL) {
3384 if (PyErr_Occurred()) {
3385 goto error;
3386 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003387 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003388 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003389
3390 /* XXX: pickle.py doesn't check neither the type, nor the range
3391 of the value returned by the extension_registry. It should for
3392 consistency. */
3393
3394 /* Verify code_obj has the right type and value. */
3395 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003396 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003397 "Can't pickle %R: extension code %R isn't an integer",
3398 obj, code_obj);
3399 goto error;
3400 }
3401 code = PyLong_AS_LONG(code_obj);
3402 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003403 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003404 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3405 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003406 goto error;
3407 }
3408
3409 /* Generate an EXT opcode. */
3410 if (code <= 0xff) {
3411 pdata[0] = EXT1;
3412 pdata[1] = (unsigned char)code;
3413 n = 2;
3414 }
3415 else if (code <= 0xffff) {
3416 pdata[0] = EXT2;
3417 pdata[1] = (unsigned char)(code & 0xff);
3418 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3419 n = 3;
3420 }
3421 else {
3422 pdata[0] = EXT4;
3423 pdata[1] = (unsigned char)(code & 0xff);
3424 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3425 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3426 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3427 n = 5;
3428 }
3429
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003430 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003431 goto error;
3432 }
3433 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003434 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003435 if (parent == module) {
3436 Py_INCREF(lastname);
3437 Py_DECREF(global_name);
3438 global_name = lastname;
3439 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003440 if (self->proto >= 4) {
3441 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003442
Christian Heimese8b1ba12013-11-23 21:13:39 +01003443 if (save(self, module_name, 0) < 0)
3444 goto error;
3445 if (save(self, global_name, 0) < 0)
3446 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003447
3448 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3449 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003450 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003451 else if (parent != module) {
3452 PickleState *st = _Pickle_GetGlobalState();
3453 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3454 st->getattr, parent, lastname);
Miss Islington (bot)3152bc32018-08-22 01:54:33 -04003455 if (reduce_value == NULL)
3456 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003457 status = save_reduce(self, reduce_value, NULL);
3458 Py_DECREF(reduce_value);
3459 if (status < 0)
3460 goto error;
3461 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003462 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003463 /* Generate a normal global opcode if we are using a pickle
3464 protocol < 4, or if the object is not registered in the
3465 extension registry. */
3466 PyObject *encoded;
3467 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003468
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003469 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003470 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003471
3472 /* For protocol < 3 and if the user didn't request against doing
3473 so, we convert module names to the old 2.x module names. */
3474 if (self->proto < 3 && self->fix_imports) {
3475 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003476 goto error;
3477 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003478 }
3479
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003480 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3481 both the module name and the global name using UTF-8. We do so
3482 only when we are using the pickle protocol newer than version
3483 3. This is to ensure compatibility with older Unpickler running
3484 on Python 2.x. */
3485 if (self->proto == 3) {
3486 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003487 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003488 else {
3489 unicode_encoder = PyUnicode_AsASCIIString;
3490 }
3491 encoded = unicode_encoder(module_name);
3492 if (encoded == NULL) {
3493 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003494 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003495 "can't pickle module identifier '%S' using "
3496 "pickle protocol %i",
3497 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003498 goto error;
3499 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003500 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3501 PyBytes_GET_SIZE(encoded)) < 0) {
3502 Py_DECREF(encoded);
3503 goto error;
3504 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003505 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003506 if(_Pickler_Write(self, "\n", 1) < 0)
3507 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003508
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003509 /* Save the name of the module. */
3510 encoded = unicode_encoder(global_name);
3511 if (encoded == NULL) {
3512 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003513 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003514 "can't pickle global identifier '%S' using "
3515 "pickle protocol %i",
3516 global_name, self->proto);
3517 goto error;
3518 }
3519 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3520 PyBytes_GET_SIZE(encoded)) < 0) {
3521 Py_DECREF(encoded);
3522 goto error;
3523 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003525 if (_Pickler_Write(self, "\n", 1) < 0)
3526 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003527 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003528 /* Memoize the object. */
3529 if (memo_put(self, obj) < 0)
3530 goto error;
3531 }
3532
3533 if (0) {
3534 error:
3535 status = -1;
3536 }
3537 Py_XDECREF(module_name);
3538 Py_XDECREF(global_name);
3539 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003540 Py_XDECREF(parent);
3541 Py_XDECREF(dotted_path);
3542 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003543
3544 return status;
3545}
3546
3547static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003548save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3549{
3550 PyObject *reduce_value;
3551 int status;
3552
3553 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3554 if (reduce_value == NULL) {
3555 return -1;
3556 }
3557 status = save_reduce(self, reduce_value, obj);
3558 Py_DECREF(reduce_value);
3559 return status;
3560}
3561
3562static int
3563save_type(PicklerObject *self, PyObject *obj)
3564{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003565 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003566 return save_singleton_type(self, obj, Py_None);
3567 }
3568 else if (obj == (PyObject *)&PyEllipsis_Type) {
3569 return save_singleton_type(self, obj, Py_Ellipsis);
3570 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003571 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003572 return save_singleton_type(self, obj, Py_NotImplemented);
3573 }
3574 return save_global(self, obj, NULL);
3575}
3576
3577static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003578save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003579{
3580 PyObject *pid = NULL;
3581 int status = 0;
3582
3583 const char persid_op = PERSID;
3584 const char binpersid_op = BINPERSID;
3585
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003586 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003587 if (pid == NULL)
3588 return -1;
3589
3590 if (pid != Py_None) {
3591 if (self->bin) {
3592 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003593 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003594 goto error;
3595 }
3596 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003597 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003598
3599 pid_str = PyObject_Str(pid);
3600 if (pid_str == NULL)
3601 goto error;
3602
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003603 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003604 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003605 if (!PyUnicode_IS_ASCII(pid_str)) {
3606 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3607 "persistent IDs in protocol 0 must be "
3608 "ASCII strings");
3609 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003610 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003611 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003612
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003613 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003614 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3615 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3616 _Pickler_Write(self, "\n", 1) < 0) {
3617 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003618 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003619 }
3620 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003621 }
3622 status = 1;
3623 }
3624
3625 if (0) {
3626 error:
3627 status = -1;
3628 }
3629 Py_XDECREF(pid);
3630
3631 return status;
3632}
3633
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003634static PyObject *
3635get_class(PyObject *obj)
3636{
3637 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003638 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003639
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003640 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3641 cls = (PyObject *) Py_TYPE(obj);
3642 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003643 }
3644 return cls;
3645}
3646
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003647/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3648 * appropriate __reduce__ method for obj.
3649 */
3650static int
3651save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3652{
3653 PyObject *callable;
3654 PyObject *argtup;
3655 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003656 PyObject *listitems = Py_None;
3657 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003658 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003659 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003660 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003661
3662 const char reduce_op = REDUCE;
3663 const char build_op = BUILD;
3664 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003665 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003666
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003667 size = PyTuple_Size(args);
3668 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003669 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003670 "__reduce__ must contain 2 through 5 elements");
3671 return -1;
3672 }
3673
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003674 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3675 &callable, &argtup, &state, &listitems, &dictitems))
3676 return -1;
3677
3678 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003679 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003680 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003681 return -1;
3682 }
3683 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003684 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003685 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003686 return -1;
3687 }
3688
3689 if (state == Py_None)
3690 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003691
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003692 if (listitems == Py_None)
3693 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003694 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003695 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003696 "returned by __reduce__ must be an iterator, not %s",
3697 Py_TYPE(listitems)->tp_name);
3698 return -1;
3699 }
3700
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003701 if (dictitems == Py_None)
3702 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003703 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003704 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003705 "returned by __reduce__ must be an iterator, not %s",
3706 Py_TYPE(dictitems)->tp_name);
3707 return -1;
3708 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003709
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003710 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003711 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003712 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003713
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003714 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3715 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003716 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003717 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003718 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003719 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3720 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003721 if (!use_newobj_ex) {
3722 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003723 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003724 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003725 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003726 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003727 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003728
3729 if (use_newobj_ex) {
3730 PyObject *cls;
3731 PyObject *args;
3732 PyObject *kwargs;
3733
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003734 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003735 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003736 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003737 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003738 return -1;
3739 }
3740
3741 cls = PyTuple_GET_ITEM(argtup, 0);
3742 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003743 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003744 "first item from NEWOBJ_EX argument tuple must "
3745 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3746 return -1;
3747 }
3748 args = PyTuple_GET_ITEM(argtup, 1);
3749 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003750 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003751 "second item from NEWOBJ_EX argument tuple must "
3752 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3753 return -1;
3754 }
3755 kwargs = PyTuple_GET_ITEM(argtup, 2);
3756 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003757 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003758 "third item from NEWOBJ_EX argument tuple must "
3759 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3760 return -1;
3761 }
3762
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003763 if (self->proto >= 4) {
3764 if (save(self, cls, 0) < 0 ||
3765 save(self, args, 0) < 0 ||
3766 save(self, kwargs, 0) < 0 ||
3767 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3768 return -1;
3769 }
3770 }
3771 else {
3772 PyObject *newargs;
3773 PyObject *cls_new;
3774 Py_ssize_t i;
3775 _Py_IDENTIFIER(__new__);
3776
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003777 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003778 if (newargs == NULL)
3779 return -1;
3780
3781 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3782 if (cls_new == NULL) {
3783 Py_DECREF(newargs);
3784 return -1;
3785 }
3786 PyTuple_SET_ITEM(newargs, 0, cls_new);
3787 Py_INCREF(cls);
3788 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003789 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003790 PyObject *item = PyTuple_GET_ITEM(args, i);
3791 Py_INCREF(item);
3792 PyTuple_SET_ITEM(newargs, i + 2, item);
3793 }
3794
3795 callable = PyObject_Call(st->partial, newargs, kwargs);
3796 Py_DECREF(newargs);
3797 if (callable == NULL)
3798 return -1;
3799
3800 newargs = PyTuple_New(0);
3801 if (newargs == NULL) {
3802 Py_DECREF(callable);
3803 return -1;
3804 }
3805
3806 if (save(self, callable, 0) < 0 ||
3807 save(self, newargs, 0) < 0 ||
3808 _Pickler_Write(self, &reduce_op, 1) < 0) {
3809 Py_DECREF(newargs);
3810 Py_DECREF(callable);
3811 return -1;
3812 }
3813 Py_DECREF(newargs);
3814 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003815 }
3816 }
3817 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003818 PyObject *cls;
3819 PyObject *newargtup;
3820 PyObject *obj_class;
3821 int p;
3822
3823 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003824 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003825 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003826 return -1;
3827 }
3828
3829 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003830 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003831 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003832 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003833 return -1;
3834 }
3835
3836 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003837 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003838 p = obj_class != cls; /* true iff a problem */
3839 Py_DECREF(obj_class);
3840 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003841 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003842 "__newobj__ args has the wrong class");
3843 return -1;
3844 }
3845 }
3846 /* XXX: These calls save() are prone to infinite recursion. Imagine
3847 what happen if the value returned by the __reduce__() method of
3848 some extension type contains another object of the same type. Ouch!
3849
3850 Here is a quick example, that I ran into, to illustrate what I
3851 mean:
3852
3853 >>> import pickle, copyreg
3854 >>> copyreg.dispatch_table.pop(complex)
3855 >>> pickle.dumps(1+2j)
3856 Traceback (most recent call last):
3857 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003858 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003859
3860 Removing the complex class from copyreg.dispatch_table made the
3861 __reduce_ex__() method emit another complex object:
3862
3863 >>> (1+1j).__reduce_ex__(2)
3864 (<function __newobj__ at 0xb7b71c3c>,
3865 (<class 'complex'>, (1+1j)), None, None, None)
3866
3867 Thus when save() was called on newargstup (the 2nd item) recursion
3868 ensued. Of course, the bug was in the complex class which had a
3869 broken __getnewargs__() that emitted another complex object. But,
3870 the point, here, is it is quite easy to end up with a broken reduce
3871 function. */
3872
3873 /* Save the class and its __new__ arguments. */
3874 if (save(self, cls, 0) < 0)
3875 return -1;
3876
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003877 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003878 if (newargtup == NULL)
3879 return -1;
3880
3881 p = save(self, newargtup, 0);
3882 Py_DECREF(newargtup);
3883 if (p < 0)
3884 return -1;
3885
3886 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003887 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003888 return -1;
3889 }
3890 else { /* Not using NEWOBJ. */
3891 if (save(self, callable, 0) < 0 ||
3892 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003893 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003894 return -1;
3895 }
3896
3897 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3898 the caller do not want to memoize the object. Not particularly useful,
3899 but that is to mimic the behavior save_reduce() in pickle.py when
3900 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003901 if (obj != NULL) {
3902 /* If the object is already in the memo, this means it is
3903 recursive. In this case, throw away everything we put on the
3904 stack, and fetch the object back from the memo. */
3905 if (PyMemoTable_Get(self->memo, obj)) {
3906 const char pop_op = POP;
3907
3908 if (_Pickler_Write(self, &pop_op, 1) < 0)
3909 return -1;
3910 if (memo_get(self, obj) < 0)
3911 return -1;
3912
3913 return 0;
3914 }
3915 else if (memo_put(self, obj) < 0)
3916 return -1;
3917 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003918
3919 if (listitems && batch_list(self, listitems) < 0)
3920 return -1;
3921
3922 if (dictitems && batch_dict(self, dictitems) < 0)
3923 return -1;
3924
3925 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003926 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003927 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003928 return -1;
3929 }
3930
3931 return 0;
3932}
3933
3934static int
3935save(PicklerObject *self, PyObject *obj, int pers_save)
3936{
3937 PyTypeObject *type;
3938 PyObject *reduce_func = NULL;
3939 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003940 int status = 0;
3941
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003942 if (_Pickler_OpcodeBoundary(self) < 0)
3943 return -1;
3944
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003945 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003946 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003947
3948 /* The extra pers_save argument is necessary to avoid calling save_pers()
3949 on its returned object. */
3950 if (!pers_save && self->pers_func) {
3951 /* save_pers() returns:
3952 -1 to signal an error;
3953 0 if it did nothing successfully;
3954 1 if a persistent id was saved.
3955 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003956 if ((status = save_pers(self, obj)) != 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003957 goto done;
3958 }
3959
3960 type = Py_TYPE(obj);
3961
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003962 /* The old cPickle had an optimization that used switch-case statement
3963 dispatching on the first letter of the type name. This has was removed
3964 since benchmarks shown that this optimization was actually slowing
3965 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003966
3967 /* Atom types; these aren't memoized, so don't check the memo. */
3968
3969 if (obj == Py_None) {
3970 status = save_none(self, obj);
3971 goto done;
3972 }
3973 else if (obj == Py_False || obj == Py_True) {
3974 status = save_bool(self, obj);
3975 goto done;
3976 }
3977 else if (type == &PyLong_Type) {
3978 status = save_long(self, obj);
3979 goto done;
3980 }
3981 else if (type == &PyFloat_Type) {
3982 status = save_float(self, obj);
3983 goto done;
3984 }
3985
3986 /* Check the memo to see if it has the object. If so, generate
3987 a GET (or BINGET) opcode, instead of pickling the object
3988 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003989 if (PyMemoTable_Get(self->memo, obj)) {
3990 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003991 goto error;
3992 goto done;
3993 }
3994
3995 if (type == &PyBytes_Type) {
3996 status = save_bytes(self, obj);
3997 goto done;
3998 }
3999 else if (type == &PyUnicode_Type) {
4000 status = save_unicode(self, obj);
4001 goto done;
4002 }
4003 else if (type == &PyDict_Type) {
4004 status = save_dict(self, obj);
4005 goto done;
4006 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004007 else if (type == &PySet_Type) {
4008 status = save_set(self, obj);
4009 goto done;
4010 }
4011 else if (type == &PyFrozenSet_Type) {
4012 status = save_frozenset(self, obj);
4013 goto done;
4014 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004015 else if (type == &PyList_Type) {
4016 status = save_list(self, obj);
4017 goto done;
4018 }
4019 else if (type == &PyTuple_Type) {
4020 status = save_tuple(self, obj);
4021 goto done;
4022 }
4023 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004024 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004025 goto done;
4026 }
4027 else if (type == &PyFunction_Type) {
4028 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004029 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004030 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031
4032 /* XXX: This part needs some unit tests. */
4033
4034 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004035 * self.dispatch_table, copyreg.dispatch_table, the object's
4036 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004037 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004038 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004039 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004040 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4041 (PyObject *)type);
4042 if (reduce_func == NULL) {
4043 if (PyErr_Occurred()) {
4044 goto error;
4045 }
4046 } else {
4047 /* PyDict_GetItemWithError() returns a borrowed reference.
4048 Increase the reference count to be consistent with
4049 PyObject_GetItem and _PyObject_GetAttrId used below. */
4050 Py_INCREF(reduce_func);
4051 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004052 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004053 reduce_func = PyObject_GetItem(self->dispatch_table,
4054 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004055 if (reduce_func == NULL) {
4056 if (PyErr_ExceptionMatches(PyExc_KeyError))
4057 PyErr_Clear();
4058 else
4059 goto error;
4060 }
4061 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004062 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004063 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004064 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004065 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004066 else if (PyType_IsSubtype(type, &PyType_Type)) {
4067 status = save_global(self, obj, NULL);
4068 goto done;
4069 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004070 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004071 _Py_IDENTIFIER(__reduce__);
4072 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004073
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004074
4075 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4076 automatically defined as __reduce__. While this is convenient, this
4077 make it impossible to know which method was actually called. Of
4078 course, this is not a big deal. But still, it would be nice to let
4079 the user know which method was called when something go
4080 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4081 don't actually have to check for a __reduce__ method. */
4082
4083 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004084 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4085 goto error;
4086 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004087 if (reduce_func != NULL) {
4088 PyObject *proto;
4089 proto = PyLong_FromLong(self->proto);
4090 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004091 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004092 }
4093 }
4094 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004095 PickleState *st = _Pickle_GetGlobalState();
4096
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004097 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004098 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004100 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004101 }
4102 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004103 PyErr_Format(st->PicklingError,
4104 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004105 type->tp_name, obj);
4106 goto error;
4107 }
4108 }
4109 }
4110
4111 if (reduce_value == NULL)
4112 goto error;
4113
4114 if (PyUnicode_Check(reduce_value)) {
4115 status = save_global(self, obj, reduce_value);
4116 goto done;
4117 }
4118
4119 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004120 PickleState *st = _Pickle_GetGlobalState();
4121 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004122 "__reduce__ must return a string or tuple");
4123 goto error;
4124 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004125
4126 status = save_reduce(self, reduce_value, obj);
4127
4128 if (0) {
4129 error:
4130 status = -1;
4131 }
4132 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004133
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004134 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004135 Py_XDECREF(reduce_func);
4136 Py_XDECREF(reduce_value);
4137
4138 return status;
4139}
4140
4141static int
4142dump(PicklerObject *self, PyObject *obj)
4143{
4144 const char stop_op = STOP;
4145
4146 if (self->proto >= 2) {
4147 char header[2];
4148
4149 header[0] = PROTO;
4150 assert(self->proto >= 0 && self->proto < 256);
4151 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004152 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004153 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004154 if (self->proto >= 4)
4155 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004156 }
4157
4158 if (save(self, obj, 0) < 0 ||
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004159 _Pickler_Write(self, &stop_op, 1) < 0 ||
4160 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004161 return -1;
Miss Islington (bot)74735e22018-04-03 14:35:11 -07004162 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004163 return 0;
4164}
4165
Larry Hastings61272b72014-01-07 12:41:53 -08004166/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004167
4168_pickle.Pickler.clear_memo
4169
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170Clears the pickler's "memo".
4171
4172The memo is the data structure that remembers which objects the
4173pickler has already seen, so that shared or recursive objects are
4174pickled by reference and not by value. This method is useful when
4175re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004176[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004177
Larry Hastings3cceb382014-01-04 11:09:09 -08004178static PyObject *
4179_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004180/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004181{
4182 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004183 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004184
4185 Py_RETURN_NONE;
4186}
4187
Larry Hastings61272b72014-01-07 12:41:53 -08004188/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004189
4190_pickle.Pickler.dump
4191
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004192 obj: object
4193 /
4194
4195Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004196[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004197
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004198static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004199_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004200/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004201{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004202 /* Check whether the Pickler was initialized correctly (issue3664).
4203 Developers often forget to call __init__() in their subclasses, which
4204 would trigger a segfault without this check. */
4205 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004206 PickleState *st = _Pickle_GetGlobalState();
4207 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004208 "Pickler.__init__() was not called by %s.__init__()",
4209 Py_TYPE(self)->tp_name);
4210 return NULL;
4211 }
4212
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004213 if (_Pickler_ClearBuffer(self) < 0)
4214 return NULL;
4215
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004216 if (dump(self, obj) < 0)
4217 return NULL;
4218
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004219 if (_Pickler_FlushToFile(self) < 0)
4220 return NULL;
4221
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004222 Py_RETURN_NONE;
4223}
4224
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004225/*[clinic input]
4226
4227_pickle.Pickler.__sizeof__ -> Py_ssize_t
4228
4229Returns size in memory, in bytes.
4230[clinic start generated code]*/
4231
4232static Py_ssize_t
4233_pickle_Pickler___sizeof___impl(PicklerObject *self)
4234/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4235{
4236 Py_ssize_t res, s;
4237
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004238 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004239 if (self->memo != NULL) {
4240 res += sizeof(PyMemoTable);
4241 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4242 }
4243 if (self->output_buffer != NULL) {
4244 s = _PySys_GetSizeOf(self->output_buffer);
4245 if (s == -1)
4246 return -1;
4247 res += s;
4248 }
4249 return res;
4250}
4251
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004252static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004253 _PICKLE_PICKLER_DUMP_METHODDEF
4254 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004255 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004256 {NULL, NULL} /* sentinel */
4257};
4258
4259static void
4260Pickler_dealloc(PicklerObject *self)
4261{
4262 PyObject_GC_UnTrack(self);
4263
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004264 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004265 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004266 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004267 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004268 Py_XDECREF(self->fast_memo);
4269
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004270 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004271
4272 Py_TYPE(self)->tp_free((PyObject *)self);
4273}
4274
4275static int
4276Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4277{
4278 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004279 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004280 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004281 Py_VISIT(self->fast_memo);
4282 return 0;
4283}
4284
4285static int
4286Pickler_clear(PicklerObject *self)
4287{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004288 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004289 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004290 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004291 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004292 Py_CLEAR(self->fast_memo);
4293
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004294 if (self->memo != NULL) {
4295 PyMemoTable *memo = self->memo;
4296 self->memo = NULL;
4297 PyMemoTable_Del(memo);
4298 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004299 return 0;
4300}
4301
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004302
Larry Hastings61272b72014-01-07 12:41:53 -08004303/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004304
4305_pickle.Pickler.__init__
4306
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004307 file: object
4308 protocol: object = NULL
4309 fix_imports: bool = True
4310
4311This takes a binary file for writing a pickle data stream.
4312
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004313The optional *protocol* argument tells the pickler to use the given
4314protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4315protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004316
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004317Specifying a negative protocol version selects the highest protocol
4318version supported. The higher the protocol used, the more recent the
4319version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004320
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004321The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004322bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004323writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004324this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004325
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004326If *fix_imports* is True and protocol is less than 3, pickle will try
4327to map the new Python 3 names to the old module names used in Python
43282, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004329[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004330
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004331static int
Larry Hastings89964c42015-04-14 18:07:59 -04004332_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4333 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004334/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004335{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004336 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004337 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004338
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004339 /* In case of multiple __init__() calls, clear previous content. */
4340 if (self->write != NULL)
4341 (void)Pickler_clear(self);
4342
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004343 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004344 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004345
4346 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004347 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004348
4349 /* memo and output_buffer may have already been created in _Pickler_New */
4350 if (self->memo == NULL) {
4351 self->memo = PyMemoTable_New();
4352 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004353 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004354 }
4355 self->output_len = 0;
4356 if (self->output_buffer == NULL) {
4357 self->max_output_len = WRITE_BUF_SIZE;
4358 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4359 self->max_output_len);
4360 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004361 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004362 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004363
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004364 self->fast = 0;
4365 self->fast_nesting = 0;
4366 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004367
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004368 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4369 &self->pers_func, &self->pers_func_self) < 0)
4370 {
4371 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004372 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004373
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004374 if (_PyObject_LookupAttrId((PyObject *)self,
4375 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4376 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004377 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004378
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004379 return 0;
4380}
4381
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004382
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004383/* Define a proxy object for the Pickler's internal memo object. This is to
4384 * avoid breaking code like:
4385 * pickler.memo.clear()
4386 * and
4387 * pickler.memo = saved_memo
4388 * Is this a good idea? Not really, but we don't want to break code that uses
4389 * it. Note that we don't implement the entire mapping API here. This is
4390 * intentional, as these should be treated as black-box implementation details.
4391 */
4392
Larry Hastings61272b72014-01-07 12:41:53 -08004393/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004394_pickle.PicklerMemoProxy.clear
4395
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004396Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004397[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004398
Larry Hastings3cceb382014-01-04 11:09:09 -08004399static PyObject *
4400_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004401/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004402{
4403 if (self->pickler->memo)
4404 PyMemoTable_Clear(self->pickler->memo);
4405 Py_RETURN_NONE;
4406}
4407
Larry Hastings61272b72014-01-07 12:41:53 -08004408/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004409_pickle.PicklerMemoProxy.copy
4410
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004411Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004412[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004413
Larry Hastings3cceb382014-01-04 11:09:09 -08004414static PyObject *
4415_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004416/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004417{
4418 Py_ssize_t i;
4419 PyMemoTable *memo;
4420 PyObject *new_memo = PyDict_New();
4421 if (new_memo == NULL)
4422 return NULL;
4423
4424 memo = self->pickler->memo;
4425 for (i = 0; i < memo->mt_allocated; ++i) {
4426 PyMemoEntry entry = memo->mt_table[i];
4427 if (entry.me_key != NULL) {
4428 int status;
4429 PyObject *key, *value;
4430
4431 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004432 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004433
4434 if (key == NULL || value == NULL) {
4435 Py_XDECREF(key);
4436 Py_XDECREF(value);
4437 goto error;
4438 }
4439 status = PyDict_SetItem(new_memo, key, value);
4440 Py_DECREF(key);
4441 Py_DECREF(value);
4442 if (status < 0)
4443 goto error;
4444 }
4445 }
4446 return new_memo;
4447
4448 error:
4449 Py_XDECREF(new_memo);
4450 return NULL;
4451}
4452
Larry Hastings61272b72014-01-07 12:41:53 -08004453/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004454_pickle.PicklerMemoProxy.__reduce__
4455
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004456Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004457[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004458
Larry Hastings3cceb382014-01-04 11:09:09 -08004459static PyObject *
4460_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004461/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004462{
4463 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004464 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004465 if (contents == NULL)
4466 return NULL;
4467
4468 reduce_value = PyTuple_New(2);
4469 if (reduce_value == NULL) {
4470 Py_DECREF(contents);
4471 return NULL;
4472 }
4473 dict_args = PyTuple_New(1);
4474 if (dict_args == NULL) {
4475 Py_DECREF(contents);
4476 Py_DECREF(reduce_value);
4477 return NULL;
4478 }
4479 PyTuple_SET_ITEM(dict_args, 0, contents);
4480 Py_INCREF((PyObject *)&PyDict_Type);
4481 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4482 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4483 return reduce_value;
4484}
4485
4486static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004487 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4488 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4489 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004490 {NULL, NULL} /* sentinel */
4491};
4492
4493static void
4494PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4495{
4496 PyObject_GC_UnTrack(self);
4497 Py_XDECREF(self->pickler);
4498 PyObject_GC_Del((PyObject *)self);
4499}
4500
4501static int
4502PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4503 visitproc visit, void *arg)
4504{
4505 Py_VISIT(self->pickler);
4506 return 0;
4507}
4508
4509static int
4510PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4511{
4512 Py_CLEAR(self->pickler);
4513 return 0;
4514}
4515
4516static PyTypeObject PicklerMemoProxyType = {
4517 PyVarObject_HEAD_INIT(NULL, 0)
4518 "_pickle.PicklerMemoProxy", /*tp_name*/
4519 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4520 0,
4521 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4522 0, /* tp_print */
4523 0, /* tp_getattr */
4524 0, /* tp_setattr */
4525 0, /* tp_compare */
4526 0, /* tp_repr */
4527 0, /* tp_as_number */
4528 0, /* tp_as_sequence */
4529 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004530 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004531 0, /* tp_call */
4532 0, /* tp_str */
4533 PyObject_GenericGetAttr, /* tp_getattro */
4534 PyObject_GenericSetAttr, /* tp_setattro */
4535 0, /* tp_as_buffer */
4536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4537 0, /* tp_doc */
4538 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4539 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4540 0, /* tp_richcompare */
4541 0, /* tp_weaklistoffset */
4542 0, /* tp_iter */
4543 0, /* tp_iternext */
4544 picklerproxy_methods, /* tp_methods */
4545};
4546
4547static PyObject *
4548PicklerMemoProxy_New(PicklerObject *pickler)
4549{
4550 PicklerMemoProxyObject *self;
4551
4552 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4553 if (self == NULL)
4554 return NULL;
4555 Py_INCREF(pickler);
4556 self->pickler = pickler;
4557 PyObject_GC_Track(self);
4558 return (PyObject *)self;
4559}
4560
4561/*****************************************************************************/
4562
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004563static PyObject *
4564Pickler_get_memo(PicklerObject *self)
4565{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004566 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004567}
4568
4569static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004570Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004571{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004572 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004573
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004574 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004575 PyErr_SetString(PyExc_TypeError,
4576 "attribute deletion is not supported");
4577 return -1;
4578 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004579
4580 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4581 PicklerObject *pickler =
4582 ((PicklerMemoProxyObject *)obj)->pickler;
4583
4584 new_memo = PyMemoTable_Copy(pickler->memo);
4585 if (new_memo == NULL)
4586 return -1;
4587 }
4588 else if (PyDict_Check(obj)) {
4589 Py_ssize_t i = 0;
4590 PyObject *key, *value;
4591
4592 new_memo = PyMemoTable_New();
4593 if (new_memo == NULL)
4594 return -1;
4595
4596 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004597 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004598 PyObject *memo_obj;
4599
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004600 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004601 PyErr_SetString(PyExc_TypeError,
4602 "'memo' values must be 2-item tuples");
4603 goto error;
4604 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004605 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004606 if (memo_id == -1 && PyErr_Occurred())
4607 goto error;
4608 memo_obj = PyTuple_GET_ITEM(value, 1);
4609 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4610 goto error;
4611 }
4612 }
4613 else {
4614 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004615 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004616 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004617 return -1;
4618 }
4619
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004620 PyMemoTable_Del(self->memo);
4621 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004622
4623 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004624
4625 error:
4626 if (new_memo)
4627 PyMemoTable_Del(new_memo);
4628 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004629}
4630
4631static PyObject *
4632Pickler_get_persid(PicklerObject *self)
4633{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004634 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004635 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004636 return NULL;
4637 }
4638 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004639}
4640
4641static int
4642Pickler_set_persid(PicklerObject *self, PyObject *value)
4643{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004644 if (value == NULL) {
4645 PyErr_SetString(PyExc_TypeError,
4646 "attribute deletion is not supported");
4647 return -1;
4648 }
4649 if (!PyCallable_Check(value)) {
4650 PyErr_SetString(PyExc_TypeError,
4651 "persistent_id must be a callable taking one argument");
4652 return -1;
4653 }
4654
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004655 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004656 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004657 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004658
4659 return 0;
4660}
4661
4662static PyMemberDef Pickler_members[] = {
4663 {"bin", T_INT, offsetof(PicklerObject, bin)},
4664 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004665 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004666 {NULL}
4667};
4668
4669static PyGetSetDef Pickler_getsets[] = {
4670 {"memo", (getter)Pickler_get_memo,
4671 (setter)Pickler_set_memo},
4672 {"persistent_id", (getter)Pickler_get_persid,
4673 (setter)Pickler_set_persid},
4674 {NULL}
4675};
4676
4677static PyTypeObject Pickler_Type = {
4678 PyVarObject_HEAD_INIT(NULL, 0)
4679 "_pickle.Pickler" , /*tp_name*/
4680 sizeof(PicklerObject), /*tp_basicsize*/
4681 0, /*tp_itemsize*/
4682 (destructor)Pickler_dealloc, /*tp_dealloc*/
4683 0, /*tp_print*/
4684 0, /*tp_getattr*/
4685 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004686 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004687 0, /*tp_repr*/
4688 0, /*tp_as_number*/
4689 0, /*tp_as_sequence*/
4690 0, /*tp_as_mapping*/
4691 0, /*tp_hash*/
4692 0, /*tp_call*/
4693 0, /*tp_str*/
4694 0, /*tp_getattro*/
4695 0, /*tp_setattro*/
4696 0, /*tp_as_buffer*/
4697 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004698 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004699 (traverseproc)Pickler_traverse, /*tp_traverse*/
4700 (inquiry)Pickler_clear, /*tp_clear*/
4701 0, /*tp_richcompare*/
4702 0, /*tp_weaklistoffset*/
4703 0, /*tp_iter*/
4704 0, /*tp_iternext*/
4705 Pickler_methods, /*tp_methods*/
4706 Pickler_members, /*tp_members*/
4707 Pickler_getsets, /*tp_getset*/
4708 0, /*tp_base*/
4709 0, /*tp_dict*/
4710 0, /*tp_descr_get*/
4711 0, /*tp_descr_set*/
4712 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004713 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004714 PyType_GenericAlloc, /*tp_alloc*/
4715 PyType_GenericNew, /*tp_new*/
4716 PyObject_GC_Del, /*tp_free*/
4717 0, /*tp_is_gc*/
4718};
4719
Victor Stinner121aab42011-09-29 23:40:53 +02004720/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004721
4722 XXX: It would be nice to able to avoid Python function call overhead, by
4723 using directly the C version of find_class(), when find_class() is not
4724 overridden by a subclass. Although, this could become rather hackish. A
4725 simpler optimization would be to call the C function when self is not a
4726 subclass instance. */
4727static PyObject *
4728find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4729{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004730 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004731
Victor Stinner55ba38a2016-12-09 16:09:30 +01004732 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4733 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004734}
4735
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004736static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004737marker(UnpicklerObject *self)
4738{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004739 Py_ssize_t mark;
4740
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004741 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004742 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004743 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004744 return -1;
4745 }
4746
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004747 mark = self->marks[--self->num_marks];
4748 self->stack->mark_set = self->num_marks != 0;
4749 self->stack->fence = self->num_marks ?
4750 self->marks[self->num_marks - 1] : 0;
4751 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004752}
4753
4754static int
4755load_none(UnpicklerObject *self)
4756{
4757 PDATA_APPEND(self->stack, Py_None, -1);
4758 return 0;
4759}
4760
4761static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004762load_int(UnpicklerObject *self)
4763{
4764 PyObject *value;
4765 char *endptr, *s;
4766 Py_ssize_t len;
4767 long x;
4768
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004769 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004770 return -1;
4771 if (len < 2)
4772 return bad_readline();
4773
4774 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004775 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004776 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004777 x = strtol(s, &endptr, 0);
4778
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004779 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004780 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004781 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004782 errno = 0;
4783 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004784 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004785 if (value == NULL) {
4786 PyErr_SetString(PyExc_ValueError,
4787 "could not convert string to int");
4788 return -1;
4789 }
4790 }
4791 else {
4792 if (len == 3 && (x == 0 || x == 1)) {
4793 if ((value = PyBool_FromLong(x)) == NULL)
4794 return -1;
4795 }
4796 else {
4797 if ((value = PyLong_FromLong(x)) == NULL)
4798 return -1;
4799 }
4800 }
4801
4802 PDATA_PUSH(self->stack, value, -1);
4803 return 0;
4804}
4805
4806static int
4807load_bool(UnpicklerObject *self, PyObject *boolean)
4808{
4809 assert(boolean == Py_True || boolean == Py_False);
4810 PDATA_APPEND(self->stack, boolean, -1);
4811 return 0;
4812}
4813
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004814/* s contains x bytes of an unsigned little-endian integer. Return its value
4815 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4816 */
4817static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004818calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004819{
4820 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004821 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004822 size_t x = 0;
4823
Serhiy Storchakae0606192015-09-29 22:10:07 +03004824 if (nbytes > (int)sizeof(size_t)) {
4825 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4826 * have 64-bit size that can't be represented on 32-bit platform.
4827 */
4828 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4829 if (s[i])
4830 return -1;
4831 }
4832 nbytes = (int)sizeof(size_t);
4833 }
4834 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004835 x |= (size_t) s[i] << (8 * i);
4836 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004837
4838 if (x > PY_SSIZE_T_MAX)
4839 return -1;
4840 else
4841 return (Py_ssize_t) x;
4842}
4843
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004844/* s contains x bytes of a little-endian integer. Return its value as a
4845 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004846 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004847 * of x-platform bugs.
4848 */
4849static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004850calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004851{
4852 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004853 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004854 long x = 0;
4855
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004856 for (i = 0; i < nbytes; i++) {
4857 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004858 }
4859
4860 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4861 * is signed, so on a box with longs bigger than 4 bytes we need
4862 * to extend a BININT's sign bit to the full width.
4863 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004864 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004865 x |= -(x & (1L << 31));
4866 }
4867
4868 return x;
4869}
4870
4871static int
4872load_binintx(UnpicklerObject *self, char *s, int size)
4873{
4874 PyObject *value;
4875 long x;
4876
4877 x = calc_binint(s, size);
4878
4879 if ((value = PyLong_FromLong(x)) == NULL)
4880 return -1;
4881
4882 PDATA_PUSH(self->stack, value, -1);
4883 return 0;
4884}
4885
4886static int
4887load_binint(UnpicklerObject *self)
4888{
4889 char *s;
4890
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004891 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004892 return -1;
4893
4894 return load_binintx(self, s, 4);
4895}
4896
4897static int
4898load_binint1(UnpicklerObject *self)
4899{
4900 char *s;
4901
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004902 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004903 return -1;
4904
4905 return load_binintx(self, s, 1);
4906}
4907
4908static int
4909load_binint2(UnpicklerObject *self)
4910{
4911 char *s;
4912
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004913 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004914 return -1;
4915
4916 return load_binintx(self, s, 2);
4917}
4918
4919static int
4920load_long(UnpicklerObject *self)
4921{
4922 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004923 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004924 Py_ssize_t len;
4925
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004926 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004927 return -1;
4928 if (len < 2)
4929 return bad_readline();
4930
Mark Dickinson8dd05142009-01-20 20:43:58 +00004931 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4932 the 'L' before calling PyLong_FromString. In order to maintain
4933 compatibility with Python 3.0.0, we don't actually *require*
4934 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004935 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004936 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004937 /* XXX: Should the base argument explicitly set to 10? */
4938 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004939 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004940 return -1;
4941
4942 PDATA_PUSH(self->stack, value, -1);
4943 return 0;
4944}
4945
4946/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4947 * data following.
4948 */
4949static int
4950load_counted_long(UnpicklerObject *self, int size)
4951{
4952 PyObject *value;
4953 char *nbytes;
4954 char *pdata;
4955
4956 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004957 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004958 return -1;
4959
4960 size = calc_binint(nbytes, size);
4961 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004962 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004963 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004964 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004965 "LONG pickle has negative byte count");
4966 return -1;
4967 }
4968
4969 if (size == 0)
4970 value = PyLong_FromLong(0L);
4971 else {
4972 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004973 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004974 return -1;
4975 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4976 1 /* little endian */ , 1 /* signed */ );
4977 }
4978 if (value == NULL)
4979 return -1;
4980 PDATA_PUSH(self->stack, value, -1);
4981 return 0;
4982}
4983
4984static int
4985load_float(UnpicklerObject *self)
4986{
4987 PyObject *value;
4988 char *endptr, *s;
4989 Py_ssize_t len;
4990 double d;
4991
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004992 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004993 return -1;
4994 if (len < 2)
4995 return bad_readline();
4996
4997 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004998 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4999 if (d == -1.0 && PyErr_Occurred())
5000 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005001 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005002 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5003 return -1;
5004 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005005 value = PyFloat_FromDouble(d);
5006 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005007 return -1;
5008
5009 PDATA_PUSH(self->stack, value, -1);
5010 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005011}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005012
5013static int
5014load_binfloat(UnpicklerObject *self)
5015{
5016 PyObject *value;
5017 double x;
5018 char *s;
5019
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005020 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005021 return -1;
5022
5023 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5024 if (x == -1.0 && PyErr_Occurred())
5025 return -1;
5026
5027 if ((value = PyFloat_FromDouble(x)) == NULL)
5028 return -1;
5029
5030 PDATA_PUSH(self->stack, value, -1);
5031 return 0;
5032}
5033
5034static int
5035load_string(UnpicklerObject *self)
5036{
5037 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005038 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005039 Py_ssize_t len;
5040 char *s, *p;
5041
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005042 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005043 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005044 /* Strip the newline */
5045 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005046 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005047 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048 p = s + 1;
5049 len -= 2;
5050 }
5051 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005052 PickleState *st = _Pickle_GetGlobalState();
5053 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005054 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005055 return -1;
5056 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005057 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005058
5059 /* Use the PyBytes API to decode the string, since that is what is used
5060 to encode, and then coerce the result to Unicode. */
5061 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005062 if (bytes == NULL)
5063 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005064
5065 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5066 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5067 if (strcmp(self->encoding, "bytes") == 0) {
5068 obj = bytes;
5069 }
5070 else {
5071 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5072 Py_DECREF(bytes);
5073 if (obj == NULL) {
5074 return -1;
5075 }
5076 }
5077
5078 PDATA_PUSH(self->stack, obj, -1);
5079 return 0;
5080}
5081
5082static int
5083load_counted_binstring(UnpicklerObject *self, int nbytes)
5084{
5085 PyObject *obj;
5086 Py_ssize_t size;
5087 char *s;
5088
5089 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005090 return -1;
5091
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005092 size = calc_binsize(s, nbytes);
5093 if (size < 0) {
5094 PickleState *st = _Pickle_GetGlobalState();
5095 PyErr_Format(st->UnpicklingError,
5096 "BINSTRING exceeds system's maximum size of %zd bytes",
5097 PY_SSIZE_T_MAX);
5098 return -1;
5099 }
5100
5101 if (_Unpickler_Read(self, &s, size) < 0)
5102 return -1;
5103
5104 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5105 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5106 if (strcmp(self->encoding, "bytes") == 0) {
5107 obj = PyBytes_FromStringAndSize(s, size);
5108 }
5109 else {
5110 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5111 }
5112 if (obj == NULL) {
5113 return -1;
5114 }
5115
5116 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005117 return 0;
5118}
5119
5120static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005121load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005122{
5123 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005124 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005125 char *s;
5126
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005127 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005128 return -1;
5129
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005130 size = calc_binsize(s, nbytes);
5131 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005132 PyErr_Format(PyExc_OverflowError,
5133 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005134 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005135 return -1;
5136 }
5137
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005138 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005139 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005140
5141 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005142 if (bytes == NULL)
5143 return -1;
5144
5145 PDATA_PUSH(self->stack, bytes, -1);
5146 return 0;
5147}
5148
5149static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005150load_unicode(UnpicklerObject *self)
5151{
5152 PyObject *str;
5153 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005154 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005155
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005156 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005157 return -1;
5158 if (len < 1)
5159 return bad_readline();
5160
5161 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5162 if (str == NULL)
5163 return -1;
5164
5165 PDATA_PUSH(self->stack, str, -1);
5166 return 0;
5167}
5168
5169static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005170load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005171{
5172 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005173 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005174 char *s;
5175
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005176 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005177 return -1;
5178
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005179 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005180 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005181 PyErr_Format(PyExc_OverflowError,
5182 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005183 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005184 return -1;
5185 }
5186
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005187 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005188 return -1;
5189
Victor Stinner485fb562010-04-13 11:07:24 +00005190 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005191 if (str == NULL)
5192 return -1;
5193
5194 PDATA_PUSH(self->stack, str, -1);
5195 return 0;
5196}
5197
5198static int
Victor Stinner21b47112016-03-14 18:09:39 +01005199load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005200{
5201 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005202
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005203 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005204 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005205
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005206 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005207 if (tuple == NULL)
5208 return -1;
5209 PDATA_PUSH(self->stack, tuple, -1);
5210 return 0;
5211}
5212
5213static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005214load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005215{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005216 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005217
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005218 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005219 return -1;
5220
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005221 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005222}
5223
5224static int
5225load_empty_list(UnpicklerObject *self)
5226{
5227 PyObject *list;
5228
5229 if ((list = PyList_New(0)) == NULL)
5230 return -1;
5231 PDATA_PUSH(self->stack, list, -1);
5232 return 0;
5233}
5234
5235static int
5236load_empty_dict(UnpicklerObject *self)
5237{
5238 PyObject *dict;
5239
5240 if ((dict = PyDict_New()) == NULL)
5241 return -1;
5242 PDATA_PUSH(self->stack, dict, -1);
5243 return 0;
5244}
5245
5246static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005247load_empty_set(UnpicklerObject *self)
5248{
5249 PyObject *set;
5250
5251 if ((set = PySet_New(NULL)) == NULL)
5252 return -1;
5253 PDATA_PUSH(self->stack, set, -1);
5254 return 0;
5255}
5256
5257static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005258load_list(UnpicklerObject *self)
5259{
5260 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005261 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005262
5263 if ((i = marker(self)) < 0)
5264 return -1;
5265
5266 list = Pdata_poplist(self->stack, i);
5267 if (list == NULL)
5268 return -1;
5269 PDATA_PUSH(self->stack, list, -1);
5270 return 0;
5271}
5272
5273static int
5274load_dict(UnpicklerObject *self)
5275{
5276 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005277 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005278
5279 if ((i = marker(self)) < 0)
5280 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005281 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005282
5283 if ((dict = PyDict_New()) == NULL)
5284 return -1;
5285
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005286 if ((j - i) % 2 != 0) {
5287 PickleState *st = _Pickle_GetGlobalState();
5288 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005289 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005290 return -1;
5291 }
5292
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005293 for (k = i + 1; k < j; k += 2) {
5294 key = self->stack->data[k - 1];
5295 value = self->stack->data[k];
5296 if (PyDict_SetItem(dict, key, value) < 0) {
5297 Py_DECREF(dict);
5298 return -1;
5299 }
5300 }
5301 Pdata_clear(self->stack, i);
5302 PDATA_PUSH(self->stack, dict, -1);
5303 return 0;
5304}
5305
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005306static int
5307load_frozenset(UnpicklerObject *self)
5308{
5309 PyObject *items;
5310 PyObject *frozenset;
5311 Py_ssize_t i;
5312
5313 if ((i = marker(self)) < 0)
5314 return -1;
5315
5316 items = Pdata_poptuple(self->stack, i);
5317 if (items == NULL)
5318 return -1;
5319
5320 frozenset = PyFrozenSet_New(items);
5321 Py_DECREF(items);
5322 if (frozenset == NULL)
5323 return -1;
5324
5325 PDATA_PUSH(self->stack, frozenset, -1);
5326 return 0;
5327}
5328
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005329static PyObject *
5330instantiate(PyObject *cls, PyObject *args)
5331{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005332 /* Caller must assure args are a tuple. Normally, args come from
5333 Pdata_poptuple which packs objects from the top of the stack
5334 into a newly created tuple. */
5335 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005336 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5337 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005338 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005339 PyObject *func;
5340 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5341 return NULL;
5342 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005343 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005344 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5345 }
5346 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005347 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005348 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005349}
5350
5351static int
5352load_obj(UnpicklerObject *self)
5353{
5354 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005355 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005356
5357 if ((i = marker(self)) < 0)
5358 return -1;
5359
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005360 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005361 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005362
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005363 args = Pdata_poptuple(self->stack, i + 1);
5364 if (args == NULL)
5365 return -1;
5366
5367 PDATA_POP(self->stack, cls);
5368 if (cls) {
5369 obj = instantiate(cls, args);
5370 Py_DECREF(cls);
5371 }
5372 Py_DECREF(args);
5373 if (obj == NULL)
5374 return -1;
5375
5376 PDATA_PUSH(self->stack, obj, -1);
5377 return 0;
5378}
5379
5380static int
5381load_inst(UnpicklerObject *self)
5382{
5383 PyObject *cls = NULL;
5384 PyObject *args = NULL;
5385 PyObject *obj = NULL;
5386 PyObject *module_name;
5387 PyObject *class_name;
5388 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005389 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005390 char *s;
5391
5392 if ((i = marker(self)) < 0)
5393 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005394 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005395 return -1;
5396 if (len < 2)
5397 return bad_readline();
5398
5399 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5400 identifiers are permitted in Python 3.0, since the INST opcode is only
5401 supported by older protocols on Python 2.x. */
5402 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5403 if (module_name == NULL)
5404 return -1;
5405
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005406 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005407 if (len < 2) {
5408 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005409 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005410 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005411 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005412 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005413 cls = find_class(self, module_name, class_name);
5414 Py_DECREF(class_name);
5415 }
5416 }
5417 Py_DECREF(module_name);
5418
5419 if (cls == NULL)
5420 return -1;
5421
5422 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5423 obj = instantiate(cls, args);
5424 Py_DECREF(args);
5425 }
5426 Py_DECREF(cls);
5427
5428 if (obj == NULL)
5429 return -1;
5430
5431 PDATA_PUSH(self->stack, obj, -1);
5432 return 0;
5433}
5434
5435static int
5436load_newobj(UnpicklerObject *self)
5437{
5438 PyObject *args = NULL;
5439 PyObject *clsraw = NULL;
5440 PyTypeObject *cls; /* clsraw cast to its true type */
5441 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005442 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005443
5444 /* Stack is ... cls argtuple, and we want to call
5445 * cls.__new__(cls, *argtuple).
5446 */
5447 PDATA_POP(self->stack, args);
5448 if (args == NULL)
5449 goto error;
5450 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005451 PyErr_SetString(st->UnpicklingError,
5452 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005453 goto error;
5454 }
5455
5456 PDATA_POP(self->stack, clsraw);
5457 cls = (PyTypeObject *)clsraw;
5458 if (cls == NULL)
5459 goto error;
5460 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005461 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005462 "isn't a type object");
5463 goto error;
5464 }
5465 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005466 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005467 "has NULL tp_new");
5468 goto error;
5469 }
5470
5471 /* Call __new__. */
5472 obj = cls->tp_new(cls, args, NULL);
5473 if (obj == NULL)
5474 goto error;
5475
5476 Py_DECREF(args);
5477 Py_DECREF(clsraw);
5478 PDATA_PUSH(self->stack, obj, -1);
5479 return 0;
5480
5481 error:
5482 Py_XDECREF(args);
5483 Py_XDECREF(clsraw);
5484 return -1;
5485}
5486
5487static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005488load_newobj_ex(UnpicklerObject *self)
5489{
5490 PyObject *cls, *args, *kwargs;
5491 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005492 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005493
5494 PDATA_POP(self->stack, kwargs);
5495 if (kwargs == NULL) {
5496 return -1;
5497 }
5498 PDATA_POP(self->stack, args);
5499 if (args == NULL) {
5500 Py_DECREF(kwargs);
5501 return -1;
5502 }
5503 PDATA_POP(self->stack, cls);
5504 if (cls == NULL) {
5505 Py_DECREF(kwargs);
5506 Py_DECREF(args);
5507 return -1;
5508 }
Larry Hastings61272b72014-01-07 12:41:53 -08005509
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005510 if (!PyType_Check(cls)) {
5511 Py_DECREF(kwargs);
5512 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005513 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005514 "NEWOBJ_EX class argument must be a type, not %.200s",
5515 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005516 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005517 return -1;
5518 }
5519
5520 if (((PyTypeObject *)cls)->tp_new == NULL) {
5521 Py_DECREF(kwargs);
5522 Py_DECREF(args);
5523 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005524 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005525 "NEWOBJ_EX class argument doesn't have __new__");
5526 return -1;
5527 }
5528 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5529 Py_DECREF(kwargs);
5530 Py_DECREF(args);
5531 Py_DECREF(cls);
5532 if (obj == NULL) {
5533 return -1;
5534 }
5535 PDATA_PUSH(self->stack, obj, -1);
5536 return 0;
5537}
5538
5539static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005540load_global(UnpicklerObject *self)
5541{
5542 PyObject *global = NULL;
5543 PyObject *module_name;
5544 PyObject *global_name;
5545 Py_ssize_t len;
5546 char *s;
5547
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005548 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005549 return -1;
5550 if (len < 2)
5551 return bad_readline();
5552 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5553 if (!module_name)
5554 return -1;
5555
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005556 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005557 if (len < 2) {
5558 Py_DECREF(module_name);
5559 return bad_readline();
5560 }
5561 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5562 if (global_name) {
5563 global = find_class(self, module_name, global_name);
5564 Py_DECREF(global_name);
5565 }
5566 }
5567 Py_DECREF(module_name);
5568
5569 if (global == NULL)
5570 return -1;
5571 PDATA_PUSH(self->stack, global, -1);
5572 return 0;
5573}
5574
5575static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005576load_stack_global(UnpicklerObject *self)
5577{
5578 PyObject *global;
5579 PyObject *module_name;
5580 PyObject *global_name;
5581
5582 PDATA_POP(self->stack, global_name);
5583 PDATA_POP(self->stack, module_name);
5584 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5585 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005586 PickleState *st = _Pickle_GetGlobalState();
5587 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005588 Py_XDECREF(global_name);
5589 Py_XDECREF(module_name);
5590 return -1;
5591 }
5592 global = find_class(self, module_name, global_name);
5593 Py_DECREF(global_name);
5594 Py_DECREF(module_name);
5595 if (global == NULL)
5596 return -1;
5597 PDATA_PUSH(self->stack, global, -1);
5598 return 0;
5599}
5600
5601static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005602load_persid(UnpicklerObject *self)
5603{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005604 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005605 Py_ssize_t len;
5606 char *s;
5607
5608 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005609 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005610 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005611 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612 return bad_readline();
5613
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005614 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5615 if (pid == NULL) {
5616 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5617 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5618 "persistent IDs in protocol 0 must be "
5619 "ASCII strings");
5620 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005621 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005622 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005623
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005624 obj = call_method(self->pers_func, self->pers_func_self, pid);
5625 Py_DECREF(pid);
5626 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627 return -1;
5628
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005629 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 return 0;
5631 }
5632 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005633 PickleState *st = _Pickle_GetGlobalState();
5634 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635 "A load persistent id instruction was encountered,\n"
5636 "but no persistent_load function was specified.");
5637 return -1;
5638 }
5639}
5640
5641static int
5642load_binpersid(UnpicklerObject *self)
5643{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005644 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005645
5646 if (self->pers_func) {
5647 PDATA_POP(self->stack, pid);
5648 if (pid == NULL)
5649 return -1;
5650
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005651 obj = call_method(self->pers_func, self->pers_func_self, pid);
5652 Py_DECREF(pid);
5653 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005654 return -1;
5655
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005656 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657 return 0;
5658 }
5659 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005660 PickleState *st = _Pickle_GetGlobalState();
5661 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662 "A load persistent id instruction was encountered,\n"
5663 "but no persistent_load function was specified.");
5664 return -1;
5665 }
5666}
5667
5668static int
5669load_pop(UnpicklerObject *self)
5670{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005671 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005672
5673 /* Note that we split the (pickle.py) stack into two stacks,
5674 * an object stack and a mark stack. We have to be clever and
5675 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005676 * mark stack first, and only signalling a stack underflow if
5677 * the object stack is empty and the mark stack doesn't match
5678 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005679 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005680 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005681 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005682 self->stack->mark_set = self->num_marks != 0;
5683 self->stack->fence = self->num_marks ?
5684 self->marks[self->num_marks - 1] : 0;
5685 } else if (len <= self->stack->fence)
5686 return Pdata_stack_underflow(self->stack);
5687 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688 len--;
5689 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005690 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005691 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005692 return 0;
5693}
5694
5695static int
5696load_pop_mark(UnpicklerObject *self)
5697{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005698 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005699
5700 if ((i = marker(self)) < 0)
5701 return -1;
5702
5703 Pdata_clear(self->stack, i);
5704
5705 return 0;
5706}
5707
5708static int
5709load_dup(UnpicklerObject *self)
5710{
5711 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005712 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005713
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005714 if (len <= self->stack->fence)
5715 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005716 last = self->stack->data[len - 1];
5717 PDATA_APPEND(self->stack, last, -1);
5718 return 0;
5719}
5720
5721static int
5722load_get(UnpicklerObject *self)
5723{
5724 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005725 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005726 Py_ssize_t len;
5727 char *s;
5728
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005729 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005730 return -1;
5731 if (len < 2)
5732 return bad_readline();
5733
5734 key = PyLong_FromString(s, NULL, 10);
5735 if (key == NULL)
5736 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005737 idx = PyLong_AsSsize_t(key);
5738 if (idx == -1 && PyErr_Occurred()) {
5739 Py_DECREF(key);
5740 return -1;
5741 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005742
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005743 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005744 if (value == NULL) {
5745 if (!PyErr_Occurred())
5746 PyErr_SetObject(PyExc_KeyError, key);
5747 Py_DECREF(key);
5748 return -1;
5749 }
5750 Py_DECREF(key);
5751
5752 PDATA_APPEND(self->stack, value, -1);
5753 return 0;
5754}
5755
5756static int
5757load_binget(UnpicklerObject *self)
5758{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005759 PyObject *value;
5760 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005761 char *s;
5762
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005763 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764 return -1;
5765
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005768 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005769 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005770 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005771 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005772 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005773 Py_DECREF(key);
5774 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005775 return -1;
5776 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005777
5778 PDATA_APPEND(self->stack, value, -1);
5779 return 0;
5780}
5781
5782static int
5783load_long_binget(UnpicklerObject *self)
5784{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785 PyObject *value;
5786 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005787 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005788
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005789 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005790 return -1;
5791
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005792 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005794 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005795 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005796 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005797 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005798 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005799 Py_DECREF(key);
5800 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005801 return -1;
5802 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803
5804 PDATA_APPEND(self->stack, value, -1);
5805 return 0;
5806}
5807
5808/* Push an object from the extension registry (EXT[124]). nbytes is
5809 * the number of bytes following the opcode, holding the index (code) value.
5810 */
5811static int
5812load_extension(UnpicklerObject *self, int nbytes)
5813{
5814 char *codebytes; /* the nbytes bytes after the opcode */
5815 long code; /* calc_binint returns long */
5816 PyObject *py_code; /* code as a Python int */
5817 PyObject *obj; /* the object to push */
5818 PyObject *pair; /* (module_name, class_name) */
5819 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005820 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005821
5822 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005823 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005824 return -1;
5825 code = calc_binint(codebytes, nbytes);
5826 if (code <= 0) { /* note that 0 is forbidden */
5827 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005828 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005829 return -1;
5830 }
5831
5832 /* Look for the code in the cache. */
5833 py_code = PyLong_FromLong(code);
5834 if (py_code == NULL)
5835 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005836 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005837 if (obj != NULL) {
5838 /* Bingo. */
5839 Py_DECREF(py_code);
5840 PDATA_APPEND(self->stack, obj, -1);
5841 return 0;
5842 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005843 if (PyErr_Occurred()) {
5844 Py_DECREF(py_code);
5845 return -1;
5846 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005847
5848 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005849 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005850 if (pair == NULL) {
5851 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005852 if (!PyErr_Occurred()) {
5853 PyErr_Format(PyExc_ValueError, "unregistered extension "
5854 "code %ld", code);
5855 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005856 return -1;
5857 }
5858 /* Since the extension registry is manipulable via Python code,
5859 * confirm that pair is really a 2-tuple of strings.
5860 */
5861 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5862 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5863 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5864 Py_DECREF(py_code);
5865 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5866 "isn't a 2-tuple of strings", code);
5867 return -1;
5868 }
5869 /* Load the object. */
5870 obj = find_class(self, module_name, class_name);
5871 if (obj == NULL) {
5872 Py_DECREF(py_code);
5873 return -1;
5874 }
5875 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005876 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005877 Py_DECREF(py_code);
5878 if (code < 0) {
5879 Py_DECREF(obj);
5880 return -1;
5881 }
5882 PDATA_PUSH(self->stack, obj, -1);
5883 return 0;
5884}
5885
5886static int
5887load_put(UnpicklerObject *self)
5888{
5889 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005890 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005891 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005892 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005893
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005894 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005895 return -1;
5896 if (len < 2)
5897 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005898 if (Py_SIZE(self->stack) <= self->stack->fence)
5899 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005900 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005901
5902 key = PyLong_FromString(s, NULL, 10);
5903 if (key == NULL)
5904 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005905 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005906 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005907 if (idx < 0) {
5908 if (!PyErr_Occurred())
5909 PyErr_SetString(PyExc_ValueError,
5910 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005911 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005912 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005913
5914 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005915}
5916
5917static int
5918load_binput(UnpicklerObject *self)
5919{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005920 PyObject *value;
5921 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005922 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005924 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005925 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005926
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005927 if (Py_SIZE(self->stack) <= self->stack->fence)
5928 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005929 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005931 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005932
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005933 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005934}
5935
5936static int
5937load_long_binput(UnpicklerObject *self)
5938{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005939 PyObject *value;
5940 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005941 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005942
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005943 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005944 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005945
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005946 if (Py_SIZE(self->stack) <= self->stack->fence)
5947 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005948 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005949
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005950 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005951 if (idx < 0) {
5952 PyErr_SetString(PyExc_ValueError,
5953 "negative LONG_BINPUT argument");
5954 return -1;
5955 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005956
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005957 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005958}
5959
5960static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005961load_memoize(UnpicklerObject *self)
5962{
5963 PyObject *value;
5964
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005965 if (Py_SIZE(self->stack) <= self->stack->fence)
5966 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005967 value = self->stack->data[Py_SIZE(self->stack) - 1];
5968
5969 return _Unpickler_MemoPut(self, self->memo_len, value);
5970}
5971
5972static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005973do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005974{
5975 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005976 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005977 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005978 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005979 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005980
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005981 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005982 if (x > len || x <= self->stack->fence)
5983 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005984 if (len == x) /* nothing to do */
5985 return 0;
5986
5987 list = self->stack->data[x - 1];
5988
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005989 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005990 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005991 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005992
5993 slice = Pdata_poplist(self->stack, x);
5994 if (!slice)
5995 return -1;
5996 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005997 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005998 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005999 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006000 }
6001 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006002 PyObject *extend_func;
6003 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006004
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006005 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6006 if (extend_func != NULL) {
6007 slice = Pdata_poplist(self->stack, x);
6008 if (!slice) {
6009 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006010 return -1;
6011 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006012 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006013 Py_DECREF(extend_func);
6014 if (result == NULL)
6015 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006016 Py_DECREF(result);
6017 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006018 else {
6019 PyObject *append_func;
6020 _Py_IDENTIFIER(append);
6021
6022 /* Even if the PEP 307 requires extend() and append() methods,
6023 fall back on append() if the object has no extend() method
6024 for backward compatibility. */
6025 PyErr_Clear();
6026 append_func = _PyObject_GetAttrId(list, &PyId_append);
6027 if (append_func == NULL)
6028 return -1;
6029 for (i = x; i < len; i++) {
6030 value = self->stack->data[i];
6031 result = _Pickle_FastCall(append_func, value);
6032 if (result == NULL) {
6033 Pdata_clear(self->stack, i + 1);
6034 Py_SIZE(self->stack) = x;
6035 Py_DECREF(append_func);
6036 return -1;
6037 }
6038 Py_DECREF(result);
6039 }
6040 Py_SIZE(self->stack) = x;
6041 Py_DECREF(append_func);
6042 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006043 }
6044
6045 return 0;
6046}
6047
6048static int
6049load_append(UnpicklerObject *self)
6050{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006051 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6052 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006053 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006054}
6055
6056static int
6057load_appends(UnpicklerObject *self)
6058{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006059 Py_ssize_t i = marker(self);
6060 if (i < 0)
6061 return -1;
6062 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006063}
6064
6065static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006066do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006067{
6068 PyObject *value, *key;
6069 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006070 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006071 int status = 0;
6072
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006073 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006074 if (x > len || x <= self->stack->fence)
6075 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006076 if (len == x) /* nothing to do */
6077 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006078 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006079 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006080 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006081 PyErr_SetString(st->UnpicklingError,
6082 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006083 return -1;
6084 }
6085
6086 /* Here, dict does not actually need to be a PyDict; it could be anything
6087 that supports the __setitem__ attribute. */
6088 dict = self->stack->data[x - 1];
6089
6090 for (i = x + 1; i < len; i += 2) {
6091 key = self->stack->data[i - 1];
6092 value = self->stack->data[i];
6093 if (PyObject_SetItem(dict, key, value) < 0) {
6094 status = -1;
6095 break;
6096 }
6097 }
6098
6099 Pdata_clear(self->stack, x);
6100 return status;
6101}
6102
6103static int
6104load_setitem(UnpicklerObject *self)
6105{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006106 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006107}
6108
6109static int
6110load_setitems(UnpicklerObject *self)
6111{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006112 Py_ssize_t i = marker(self);
6113 if (i < 0)
6114 return -1;
6115 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006116}
6117
6118static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006119load_additems(UnpicklerObject *self)
6120{
6121 PyObject *set;
6122 Py_ssize_t mark, len, i;
6123
6124 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006125 if (mark < 0)
6126 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006127 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006128 if (mark > len || mark <= self->stack->fence)
6129 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006130 if (len == mark) /* nothing to do */
6131 return 0;
6132
6133 set = self->stack->data[mark - 1];
6134
6135 if (PySet_Check(set)) {
6136 PyObject *items;
6137 int status;
6138
6139 items = Pdata_poptuple(self->stack, mark);
6140 if (items == NULL)
6141 return -1;
6142
6143 status = _PySet_Update(set, items);
6144 Py_DECREF(items);
6145 return status;
6146 }
6147 else {
6148 PyObject *add_func;
6149 _Py_IDENTIFIER(add);
6150
6151 add_func = _PyObject_GetAttrId(set, &PyId_add);
6152 if (add_func == NULL)
6153 return -1;
6154 for (i = mark; i < len; i++) {
6155 PyObject *result;
6156 PyObject *item;
6157
6158 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006159 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006160 if (result == NULL) {
6161 Pdata_clear(self->stack, i + 1);
6162 Py_SIZE(self->stack) = mark;
6163 return -1;
6164 }
6165 Py_DECREF(result);
6166 }
6167 Py_SIZE(self->stack) = mark;
6168 }
6169
6170 return 0;
6171}
6172
6173static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006174load_build(UnpicklerObject *self)
6175{
6176 PyObject *state, *inst, *slotstate;
6177 PyObject *setstate;
6178 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006179 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006180
6181 /* Stack is ... instance, state. We want to leave instance at
6182 * the stack top, possibly mutated via instance.__setstate__(state).
6183 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006184 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6185 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006186
6187 PDATA_POP(self->stack, state);
6188 if (state == NULL)
6189 return -1;
6190
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006191 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006192
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006193 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6194 Py_DECREF(state);
6195 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006196 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006197 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006198 PyObject *result;
6199
6200 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006201 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006202 Py_DECREF(setstate);
6203 if (result == NULL)
6204 return -1;
6205 Py_DECREF(result);
6206 return 0;
6207 }
6208
6209 /* A default __setstate__. First see whether state embeds a
6210 * slot state dict too (a proto 2 addition).
6211 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006212 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006213 PyObject *tmp = state;
6214
6215 state = PyTuple_GET_ITEM(tmp, 0);
6216 slotstate = PyTuple_GET_ITEM(tmp, 1);
6217 Py_INCREF(state);
6218 Py_INCREF(slotstate);
6219 Py_DECREF(tmp);
6220 }
6221 else
6222 slotstate = NULL;
6223
6224 /* Set inst.__dict__ from the state dict (if any). */
6225 if (state != Py_None) {
6226 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006227 PyObject *d_key, *d_value;
6228 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006229 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006230
6231 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006232 PickleState *st = _Pickle_GetGlobalState();
6233 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006234 goto error;
6235 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006236 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006237 if (dict == NULL)
6238 goto error;
6239
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006240 i = 0;
6241 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6242 /* normally the keys for instance attributes are
6243 interned. we should try to do that here. */
6244 Py_INCREF(d_key);
6245 if (PyUnicode_CheckExact(d_key))
6246 PyUnicode_InternInPlace(&d_key);
6247 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6248 Py_DECREF(d_key);
6249 goto error;
6250 }
6251 Py_DECREF(d_key);
6252 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006253 Py_DECREF(dict);
6254 }
6255
6256 /* Also set instance attributes from the slotstate dict (if any). */
6257 if (slotstate != NULL) {
6258 PyObject *d_key, *d_value;
6259 Py_ssize_t i;
6260
6261 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006262 PickleState *st = _Pickle_GetGlobalState();
6263 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006264 "slot state is not a dictionary");
6265 goto error;
6266 }
6267 i = 0;
6268 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6269 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6270 goto error;
6271 }
6272 }
6273
6274 if (0) {
6275 error:
6276 status = -1;
6277 }
6278
6279 Py_DECREF(state);
6280 Py_XDECREF(slotstate);
6281 return status;
6282}
6283
6284static int
6285load_mark(UnpicklerObject *self)
6286{
6287
6288 /* Note that we split the (pickle.py) stack into two stacks, an
6289 * object stack and a mark stack. Here we push a mark onto the
6290 * mark stack.
6291 */
6292
6293 if ((self->num_marks + 1) >= self->marks_size) {
6294 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006295
6296 /* Use the size_t type to check for overflow. */
6297 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006298 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006299 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006300 PyErr_NoMemory();
6301 return -1;
6302 }
6303
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006304 Py_ssize_t *marks_old = self->marks;
6305 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006306 if (self->marks == NULL) {
Miss Islington (bot)962051e2018-08-16 00:53:00 -04006307 PyMem_FREE(marks_old);
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006308 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006309 PyErr_NoMemory();
6310 return -1;
6311 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006312 self->marks_size = (Py_ssize_t)alloc;
6313 }
6314
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006315 self->stack->mark_set = 1;
6316 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006317
6318 return 0;
6319}
6320
6321static int
6322load_reduce(UnpicklerObject *self)
6323{
6324 PyObject *callable = NULL;
6325 PyObject *argtup = NULL;
6326 PyObject *obj = NULL;
6327
6328 PDATA_POP(self->stack, argtup);
6329 if (argtup == NULL)
6330 return -1;
6331 PDATA_POP(self->stack, callable);
6332 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006333 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006334 Py_DECREF(callable);
6335 }
6336 Py_DECREF(argtup);
6337
6338 if (obj == NULL)
6339 return -1;
6340
6341 PDATA_PUSH(self->stack, obj, -1);
6342 return 0;
6343}
6344
6345/* Just raises an error if we don't know the protocol specified. PROTO
6346 * is the first opcode for protocols >= 2.
6347 */
6348static int
6349load_proto(UnpicklerObject *self)
6350{
6351 char *s;
6352 int i;
6353
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006354 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006355 return -1;
6356
6357 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006358 if (i <= HIGHEST_PROTOCOL) {
6359 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006360 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006361 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006362
6363 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6364 return -1;
6365}
6366
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006367static int
6368load_frame(UnpicklerObject *self)
6369{
6370 char *s;
6371 Py_ssize_t frame_len;
6372
6373 if (_Unpickler_Read(self, &s, 8) < 0)
6374 return -1;
6375
6376 frame_len = calc_binsize(s, 8);
6377 if (frame_len < 0) {
6378 PyErr_Format(PyExc_OverflowError,
6379 "FRAME length exceeds system's maximum of %zd bytes",
6380 PY_SSIZE_T_MAX);
6381 return -1;
6382 }
6383
6384 if (_Unpickler_Read(self, &s, frame_len) < 0)
6385 return -1;
6386
6387 /* Rewind to start of frame */
6388 self->next_read_idx -= frame_len;
6389 return 0;
6390}
6391
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006392static PyObject *
6393load(UnpicklerObject *self)
6394{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006395 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006396 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006397
6398 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006399 self->stack->mark_set = 0;
6400 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006401 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006402 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006403 Pdata_clear(self->stack, 0);
6404
6405 /* Convenient macros for the dispatch while-switch loop just below. */
6406#define OP(opcode, load_func) \
6407 case opcode: if (load_func(self) < 0) break; continue;
6408
6409#define OP_ARG(opcode, load_func, arg) \
6410 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6411
6412 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006413 if (_Unpickler_Read(self, &s, 1) < 0) {
6414 PickleState *st = _Pickle_GetGlobalState();
6415 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6416 PyErr_Format(PyExc_EOFError, "Ran out of input");
6417 }
6418 return NULL;
6419 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006420
6421 switch ((enum opcode)s[0]) {
6422 OP(NONE, load_none)
6423 OP(BININT, load_binint)
6424 OP(BININT1, load_binint1)
6425 OP(BININT2, load_binint2)
6426 OP(INT, load_int)
6427 OP(LONG, load_long)
6428 OP_ARG(LONG1, load_counted_long, 1)
6429 OP_ARG(LONG4, load_counted_long, 4)
6430 OP(FLOAT, load_float)
6431 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006432 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6433 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6434 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6435 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6436 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006437 OP(STRING, load_string)
6438 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006439 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6440 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6441 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006442 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6443 OP_ARG(TUPLE1, load_counted_tuple, 1)
6444 OP_ARG(TUPLE2, load_counted_tuple, 2)
6445 OP_ARG(TUPLE3, load_counted_tuple, 3)
6446 OP(TUPLE, load_tuple)
6447 OP(EMPTY_LIST, load_empty_list)
6448 OP(LIST, load_list)
6449 OP(EMPTY_DICT, load_empty_dict)
6450 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006451 OP(EMPTY_SET, load_empty_set)
6452 OP(ADDITEMS, load_additems)
6453 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006454 OP(OBJ, load_obj)
6455 OP(INST, load_inst)
6456 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006457 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006458 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006459 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006460 OP(APPEND, load_append)
6461 OP(APPENDS, load_appends)
6462 OP(BUILD, load_build)
6463 OP(DUP, load_dup)
6464 OP(BINGET, load_binget)
6465 OP(LONG_BINGET, load_long_binget)
6466 OP(GET, load_get)
6467 OP(MARK, load_mark)
6468 OP(BINPUT, load_binput)
6469 OP(LONG_BINPUT, load_long_binput)
6470 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006471 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006472 OP(POP, load_pop)
6473 OP(POP_MARK, load_pop_mark)
6474 OP(SETITEM, load_setitem)
6475 OP(SETITEMS, load_setitems)
6476 OP(PERSID, load_persid)
6477 OP(BINPERSID, load_binpersid)
6478 OP(REDUCE, load_reduce)
6479 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006480 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006481 OP_ARG(EXT1, load_extension, 1)
6482 OP_ARG(EXT2, load_extension, 2)
6483 OP_ARG(EXT4, load_extension, 4)
6484 OP_ARG(NEWTRUE, load_bool, Py_True)
6485 OP_ARG(NEWFALSE, load_bool, Py_False)
6486
6487 case STOP:
6488 break;
6489
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006490 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006491 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006492 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006493 unsigned char c = (unsigned char) *s;
6494 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6495 PyErr_Format(st->UnpicklingError,
6496 "invalid load key, '%c'.", c);
6497 }
6498 else {
6499 PyErr_Format(st->UnpicklingError,
6500 "invalid load key, '\\x%02x'.", c);
6501 }
6502 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006503 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006504 }
6505
6506 break; /* and we are done! */
6507 }
6508
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006509 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006510 return NULL;
6511 }
6512
Victor Stinner2ae57e32013-10-31 13:39:23 +01006513 if (_Unpickler_SkipConsumed(self) < 0)
6514 return NULL;
6515
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006516 PDATA_POP(self->stack, value);
6517 return value;
6518}
6519
Larry Hastings61272b72014-01-07 12:41:53 -08006520/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006521
6522_pickle.Unpickler.load
6523
6524Load a pickle.
6525
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006526Read a pickled object representation from the open file object given
6527in the constructor, and return the reconstituted object hierarchy
6528specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006529[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006530
Larry Hastings3cceb382014-01-04 11:09:09 -08006531static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006532_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006533/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006534{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006535 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006536
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006537 /* Check whether the Unpickler was initialized correctly. This prevents
6538 segfaulting if a subclass overridden __init__ with a function that does
6539 not call Unpickler.__init__(). Here, we simply ensure that self->read
6540 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006541 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006542 PickleState *st = _Pickle_GetGlobalState();
6543 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006544 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006545 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546 return NULL;
6547 }
6548
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006549 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006550}
6551
6552/* The name of find_class() is misleading. In newer pickle protocols, this
6553 function is used for loading any global (i.e., functions), not just
6554 classes. The name is kept only for backward compatibility. */
6555
Larry Hastings61272b72014-01-07 12:41:53 -08006556/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006557
6558_pickle.Unpickler.find_class
6559
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006560 module_name: object
6561 global_name: object
6562 /
6563
6564Return an object from a specified module.
6565
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006566If necessary, the module will be imported. Subclasses may override
6567this method (e.g. to restrict unpickling of arbitrary classes and
6568functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006569
6570This method is called whenever a class or a function object is
6571needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006572[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006573
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006574static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006575_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6576 PyObject *module_name,
6577 PyObject *global_name)
6578/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006579{
6580 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006581 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006582
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006583 /* Try to map the old names used in Python 2.x to the new ones used in
6584 Python 3.x. We do this only with old pickle protocols and when the
6585 user has not disabled the feature. */
6586 if (self->proto < 3 && self->fix_imports) {
6587 PyObject *key;
6588 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006589 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006590
6591 /* Check if the global (i.e., a function or a class) was renamed
6592 or moved to another module. */
6593 key = PyTuple_Pack(2, module_name, global_name);
6594 if (key == NULL)
6595 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006596 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006597 Py_DECREF(key);
6598 if (item) {
6599 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6600 PyErr_Format(PyExc_RuntimeError,
6601 "_compat_pickle.NAME_MAPPING values should be "
6602 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6603 return NULL;
6604 }
6605 module_name = PyTuple_GET_ITEM(item, 0);
6606 global_name = PyTuple_GET_ITEM(item, 1);
6607 if (!PyUnicode_Check(module_name) ||
6608 !PyUnicode_Check(global_name)) {
6609 PyErr_Format(PyExc_RuntimeError,
6610 "_compat_pickle.NAME_MAPPING values should be "
6611 "pairs of str, not (%.200s, %.200s)",
6612 Py_TYPE(module_name)->tp_name,
6613 Py_TYPE(global_name)->tp_name);
6614 return NULL;
6615 }
6616 }
6617 else if (PyErr_Occurred()) {
6618 return NULL;
6619 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006620 else {
6621 /* Check if the module was renamed. */
6622 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6623 if (item) {
6624 if (!PyUnicode_Check(item)) {
6625 PyErr_Format(PyExc_RuntimeError,
6626 "_compat_pickle.IMPORT_MAPPING values should be "
6627 "strings, not %.200s", Py_TYPE(item)->tp_name);
6628 return NULL;
6629 }
6630 module_name = item;
6631 }
6632 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006633 return NULL;
6634 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006635 }
6636 }
6637
Eric Snow3f9eee62017-09-15 16:35:20 -06006638 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006639 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006640 if (PyErr_Occurred())
6641 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006642 module = PyImport_Import(module_name);
6643 if (module == NULL)
6644 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006645 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006646 global = getattribute(module, global_name, self->proto >= 4);
6647 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006648 return global;
6649}
6650
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006651/*[clinic input]
6652
6653_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6654
6655Returns size in memory, in bytes.
6656[clinic start generated code]*/
6657
6658static Py_ssize_t
6659_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6660/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6661{
6662 Py_ssize_t res;
6663
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006664 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006665 if (self->memo != NULL)
6666 res += self->memo_size * sizeof(PyObject *);
6667 if (self->marks != NULL)
6668 res += self->marks_size * sizeof(Py_ssize_t);
6669 if (self->input_line != NULL)
6670 res += strlen(self->input_line) + 1;
6671 if (self->encoding != NULL)
6672 res += strlen(self->encoding) + 1;
6673 if (self->errors != NULL)
6674 res += strlen(self->errors) + 1;
6675 return res;
6676}
6677
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006678static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006679 _PICKLE_UNPICKLER_LOAD_METHODDEF
6680 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006681 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006682 {NULL, NULL} /* sentinel */
6683};
6684
6685static void
6686Unpickler_dealloc(UnpicklerObject *self)
6687{
6688 PyObject_GC_UnTrack((PyObject *)self);
6689 Py_XDECREF(self->readline);
6690 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006691 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006692 Py_XDECREF(self->stack);
6693 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006694 if (self->buffer.buf != NULL) {
6695 PyBuffer_Release(&self->buffer);
6696 self->buffer.buf = NULL;
6697 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006698
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006699 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006700 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006701 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006702 PyMem_Free(self->encoding);
6703 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006704
6705 Py_TYPE(self)->tp_free((PyObject *)self);
6706}
6707
6708static int
6709Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6710{
6711 Py_VISIT(self->readline);
6712 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006713 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006714 Py_VISIT(self->stack);
6715 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006716 return 0;
6717}
6718
6719static int
6720Unpickler_clear(UnpicklerObject *self)
6721{
6722 Py_CLEAR(self->readline);
6723 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006724 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006725 Py_CLEAR(self->stack);
6726 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006727 if (self->buffer.buf != NULL) {
6728 PyBuffer_Release(&self->buffer);
6729 self->buffer.buf = NULL;
6730 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006731
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006732 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006733 PyMem_Free(self->marks);
6734 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006735 PyMem_Free(self->input_line);
6736 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006737 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006738 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006739 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006740 self->errors = NULL;
6741
6742 return 0;
6743}
6744
Larry Hastings61272b72014-01-07 12:41:53 -08006745/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006746
6747_pickle.Unpickler.__init__
6748
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006749 file: object
6750 *
6751 fix_imports: bool = True
6752 encoding: str = 'ASCII'
6753 errors: str = 'strict'
6754
6755This takes a binary file for reading a pickle data stream.
6756
6757The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006758protocol argument is needed. Bytes past the pickled object's
6759representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006760
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006761The argument *file* must have two methods, a read() method that takes
6762an integer argument, and a readline() method that requires no
6763arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006764binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006765other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006766
6767Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006768which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006769generated by Python 2. If *fix_imports* is True, pickle will try to
6770map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006771*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006772instances pickled by Python 2; these default to 'ASCII' and 'strict',
6773respectively. The *encoding* can be 'bytes' to read these 8-bit
6774string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006775[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006776
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006777static int
Larry Hastings89964c42015-04-14 18:07:59 -04006778_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6779 int fix_imports, const char *encoding,
6780 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006781/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006782{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006783 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006784
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006785 /* In case of multiple __init__() calls, clear previous content. */
6786 if (self->read != NULL)
6787 (void)Unpickler_clear(self);
6788
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006789 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006790 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006791
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006792 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006793 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006794
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006795 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006796
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006797 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6798 &self->pers_func, &self->pers_func_self) < 0)
6799 {
6800 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006801 }
6802
6803 self->stack = (Pdata *)Pdata_New();
6804 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006805 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006806
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006807 self->memo_size = 32;
6808 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006809 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006810 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006811
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006812 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006813
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006814 return 0;
6815}
6816
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006817
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006818/* Define a proxy object for the Unpickler's internal memo object. This is to
6819 * avoid breaking code like:
6820 * unpickler.memo.clear()
6821 * and
6822 * unpickler.memo = saved_memo
6823 * Is this a good idea? Not really, but we don't want to break code that uses
6824 * it. Note that we don't implement the entire mapping API here. This is
6825 * intentional, as these should be treated as black-box implementation details.
6826 *
6827 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006828 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006829 */
6830
Larry Hastings61272b72014-01-07 12:41:53 -08006831/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006832_pickle.UnpicklerMemoProxy.clear
6833
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006834Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006835[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006836
Larry Hastings3cceb382014-01-04 11:09:09 -08006837static PyObject *
6838_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006839/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006840{
6841 _Unpickler_MemoCleanup(self->unpickler);
6842 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6843 if (self->unpickler->memo == NULL)
6844 return NULL;
6845 Py_RETURN_NONE;
6846}
6847
Larry Hastings61272b72014-01-07 12:41:53 -08006848/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006849_pickle.UnpicklerMemoProxy.copy
6850
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006851Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006852[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006853
Larry Hastings3cceb382014-01-04 11:09:09 -08006854static PyObject *
6855_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006856/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006857{
6858 Py_ssize_t i;
6859 PyObject *new_memo = PyDict_New();
6860 if (new_memo == NULL)
6861 return NULL;
6862
6863 for (i = 0; i < self->unpickler->memo_size; i++) {
6864 int status;
6865 PyObject *key, *value;
6866
6867 value = self->unpickler->memo[i];
6868 if (value == NULL)
6869 continue;
6870
6871 key = PyLong_FromSsize_t(i);
6872 if (key == NULL)
6873 goto error;
6874 status = PyDict_SetItem(new_memo, key, value);
6875 Py_DECREF(key);
6876 if (status < 0)
6877 goto error;
6878 }
6879 return new_memo;
6880
6881error:
6882 Py_DECREF(new_memo);
6883 return NULL;
6884}
6885
Larry Hastings61272b72014-01-07 12:41:53 -08006886/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006887_pickle.UnpicklerMemoProxy.__reduce__
6888
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006889Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006890[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006891
Larry Hastings3cceb382014-01-04 11:09:09 -08006892static PyObject *
6893_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006894/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006895{
6896 PyObject *reduce_value;
6897 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006898 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006899 if (contents == NULL)
6900 return NULL;
6901
6902 reduce_value = PyTuple_New(2);
6903 if (reduce_value == NULL) {
6904 Py_DECREF(contents);
6905 return NULL;
6906 }
6907 constructor_args = PyTuple_New(1);
6908 if (constructor_args == NULL) {
6909 Py_DECREF(contents);
6910 Py_DECREF(reduce_value);
6911 return NULL;
6912 }
6913 PyTuple_SET_ITEM(constructor_args, 0, contents);
6914 Py_INCREF((PyObject *)&PyDict_Type);
6915 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6916 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6917 return reduce_value;
6918}
6919
6920static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006921 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6922 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6923 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006924 {NULL, NULL} /* sentinel */
6925};
6926
6927static void
6928UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6929{
6930 PyObject_GC_UnTrack(self);
6931 Py_XDECREF(self->unpickler);
6932 PyObject_GC_Del((PyObject *)self);
6933}
6934
6935static int
6936UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6937 visitproc visit, void *arg)
6938{
6939 Py_VISIT(self->unpickler);
6940 return 0;
6941}
6942
6943static int
6944UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6945{
6946 Py_CLEAR(self->unpickler);
6947 return 0;
6948}
6949
6950static PyTypeObject UnpicklerMemoProxyType = {
6951 PyVarObject_HEAD_INIT(NULL, 0)
6952 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6953 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6954 0,
6955 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6956 0, /* tp_print */
6957 0, /* tp_getattr */
6958 0, /* tp_setattr */
6959 0, /* tp_compare */
6960 0, /* tp_repr */
6961 0, /* tp_as_number */
6962 0, /* tp_as_sequence */
6963 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006964 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006965 0, /* tp_call */
6966 0, /* tp_str */
6967 PyObject_GenericGetAttr, /* tp_getattro */
6968 PyObject_GenericSetAttr, /* tp_setattro */
6969 0, /* tp_as_buffer */
6970 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6971 0, /* tp_doc */
6972 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6973 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6974 0, /* tp_richcompare */
6975 0, /* tp_weaklistoffset */
6976 0, /* tp_iter */
6977 0, /* tp_iternext */
6978 unpicklerproxy_methods, /* tp_methods */
6979};
6980
6981static PyObject *
6982UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6983{
6984 UnpicklerMemoProxyObject *self;
6985
6986 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6987 &UnpicklerMemoProxyType);
6988 if (self == NULL)
6989 return NULL;
6990 Py_INCREF(unpickler);
6991 self->unpickler = unpickler;
6992 PyObject_GC_Track(self);
6993 return (PyObject *)self;
6994}
6995
6996/*****************************************************************************/
6997
6998
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006999static PyObject *
7000Unpickler_get_memo(UnpicklerObject *self)
7001{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007002 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007003}
7004
7005static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007006Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007007{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007008 PyObject **new_memo;
7009 Py_ssize_t new_memo_size = 0;
7010 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007011
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007012 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007013 PyErr_SetString(PyExc_TypeError,
7014 "attribute deletion is not supported");
7015 return -1;
7016 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007017
7018 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7019 UnpicklerObject *unpickler =
7020 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7021
7022 new_memo_size = unpickler->memo_size;
7023 new_memo = _Unpickler_NewMemo(new_memo_size);
7024 if (new_memo == NULL)
7025 return -1;
7026
7027 for (i = 0; i < new_memo_size; i++) {
7028 Py_XINCREF(unpickler->memo[i]);
7029 new_memo[i] = unpickler->memo[i];
7030 }
7031 }
7032 else if (PyDict_Check(obj)) {
7033 Py_ssize_t i = 0;
7034 PyObject *key, *value;
7035
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007036 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007037 new_memo = _Unpickler_NewMemo(new_memo_size);
7038 if (new_memo == NULL)
7039 return -1;
7040
7041 while (PyDict_Next(obj, &i, &key, &value)) {
7042 Py_ssize_t idx;
7043 if (!PyLong_Check(key)) {
7044 PyErr_SetString(PyExc_TypeError,
7045 "memo key must be integers");
7046 goto error;
7047 }
7048 idx = PyLong_AsSsize_t(key);
7049 if (idx == -1 && PyErr_Occurred())
7050 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007051 if (idx < 0) {
7052 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007053 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007054 goto error;
7055 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007056 if (_Unpickler_MemoPut(self, idx, value) < 0)
7057 goto error;
7058 }
7059 }
7060 else {
7061 PyErr_Format(PyExc_TypeError,
7062 "'memo' attribute must be an UnpicklerMemoProxy object"
7063 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007064 return -1;
7065 }
7066
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007067 _Unpickler_MemoCleanup(self);
7068 self->memo_size = new_memo_size;
7069 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007070
7071 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007072
7073 error:
7074 if (new_memo_size) {
7075 i = new_memo_size;
7076 while (--i >= 0) {
7077 Py_XDECREF(new_memo[i]);
7078 }
7079 PyMem_FREE(new_memo);
7080 }
7081 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007082}
7083
7084static PyObject *
7085Unpickler_get_persload(UnpicklerObject *self)
7086{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007087 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007088 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007089 return NULL;
7090 }
7091 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007092}
7093
7094static int
7095Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
7096{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007097 if (value == NULL) {
7098 PyErr_SetString(PyExc_TypeError,
7099 "attribute deletion is not supported");
7100 return -1;
7101 }
7102 if (!PyCallable_Check(value)) {
7103 PyErr_SetString(PyExc_TypeError,
7104 "persistent_load must be a callable taking "
7105 "one argument");
7106 return -1;
7107 }
7108
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007109 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007110 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007111 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007112
7113 return 0;
7114}
7115
7116static PyGetSetDef Unpickler_getsets[] = {
7117 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7118 {"persistent_load", (getter)Unpickler_get_persload,
7119 (setter)Unpickler_set_persload},
7120 {NULL}
7121};
7122
7123static PyTypeObject Unpickler_Type = {
7124 PyVarObject_HEAD_INIT(NULL, 0)
7125 "_pickle.Unpickler", /*tp_name*/
7126 sizeof(UnpicklerObject), /*tp_basicsize*/
7127 0, /*tp_itemsize*/
7128 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7129 0, /*tp_print*/
7130 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007131 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007132 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007133 0, /*tp_repr*/
7134 0, /*tp_as_number*/
7135 0, /*tp_as_sequence*/
7136 0, /*tp_as_mapping*/
7137 0, /*tp_hash*/
7138 0, /*tp_call*/
7139 0, /*tp_str*/
7140 0, /*tp_getattro*/
7141 0, /*tp_setattro*/
7142 0, /*tp_as_buffer*/
7143 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007144 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007145 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7146 (inquiry)Unpickler_clear, /*tp_clear*/
7147 0, /*tp_richcompare*/
7148 0, /*tp_weaklistoffset*/
7149 0, /*tp_iter*/
7150 0, /*tp_iternext*/
7151 Unpickler_methods, /*tp_methods*/
7152 0, /*tp_members*/
7153 Unpickler_getsets, /*tp_getset*/
7154 0, /*tp_base*/
7155 0, /*tp_dict*/
7156 0, /*tp_descr_get*/
7157 0, /*tp_descr_set*/
7158 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007159 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007160 PyType_GenericAlloc, /*tp_alloc*/
7161 PyType_GenericNew, /*tp_new*/
7162 PyObject_GC_Del, /*tp_free*/
7163 0, /*tp_is_gc*/
7164};
7165
Larry Hastings61272b72014-01-07 12:41:53 -08007166/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007167
7168_pickle.dump
7169
7170 obj: object
7171 file: object
7172 protocol: object = NULL
7173 *
7174 fix_imports: bool = True
7175
7176Write a pickled representation of obj to the open file object file.
7177
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007178This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7179be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007180
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007181The optional *protocol* argument tells the pickler to use the given
7182protocol supported protocols are 0, 1, 2, 3 and 4. The default
7183protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007184
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007185Specifying a negative protocol version selects the highest protocol
7186version supported. The higher the protocol used, the more recent the
7187version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007189The *file* argument must have a write() method that accepts a single
7190bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007191writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007192this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007193
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007194If *fix_imports* is True and protocol is less than 3, pickle will try
7195to map the new Python 3 names to the old module names used in Python
71962, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007197[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007198
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007199static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007200_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007201 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007202/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007203{
7204 PicklerObject *pickler = _Pickler_New();
7205
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007206 if (pickler == NULL)
7207 return NULL;
7208
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007209 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007210 goto error;
7211
7212 if (_Pickler_SetOutputStream(pickler, file) < 0)
7213 goto error;
7214
7215 if (dump(pickler, obj) < 0)
7216 goto error;
7217
7218 if (_Pickler_FlushToFile(pickler) < 0)
7219 goto error;
7220
7221 Py_DECREF(pickler);
7222 Py_RETURN_NONE;
7223
7224 error:
7225 Py_XDECREF(pickler);
7226 return NULL;
7227}
7228
Larry Hastings61272b72014-01-07 12:41:53 -08007229/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007230
7231_pickle.dumps
7232
7233 obj: object
7234 protocol: object = NULL
7235 *
7236 fix_imports: bool = True
7237
7238Return the pickled representation of the object as a bytes object.
7239
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007240The optional *protocol* argument tells the pickler to use the given
7241protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7242protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007243
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007244Specifying a negative protocol version selects the highest protocol
7245version supported. The higher the protocol used, the more recent the
7246version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007247
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007248If *fix_imports* is True and *protocol* is less than 3, pickle will
7249try to map the new Python 3 names to the old module names used in
7250Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007251[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007252
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007253static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007254_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007255 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007256/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007257{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007258 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007259 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007260
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007261 if (pickler == NULL)
7262 return NULL;
7263
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007264 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007265 goto error;
7266
7267 if (dump(pickler, obj) < 0)
7268 goto error;
7269
7270 result = _Pickler_GetString(pickler);
7271 Py_DECREF(pickler);
7272 return result;
7273
7274 error:
7275 Py_XDECREF(pickler);
7276 return NULL;
7277}
7278
Larry Hastings61272b72014-01-07 12:41:53 -08007279/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007280
7281_pickle.load
7282
7283 file: object
7284 *
7285 fix_imports: bool = True
7286 encoding: str = 'ASCII'
7287 errors: str = 'strict'
7288
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007289Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007290
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007291This is equivalent to ``Unpickler(file).load()``, but may be more
7292efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007293
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007294The protocol version of the pickle is detected automatically, so no
7295protocol argument is needed. Bytes past the pickled object's
7296representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007297
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007298The argument *file* must have two methods, a read() method that takes
7299an integer argument, and a readline() method that requires no
7300arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007301binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007302other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007303
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007304Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007305which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007306generated by Python 2. If *fix_imports* is True, pickle will try to
7307map the old Python 2 names to the new names used in Python 3. The
7308*encoding* and *errors* tell pickle how to decode 8-bit string
7309instances pickled by Python 2; these default to 'ASCII' and 'strict',
7310respectively. The *encoding* can be 'bytes' to read these 8-bit
7311string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007312[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007313
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007314static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007315_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007316 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007317/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007318{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007319 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007320 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007321
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007322 if (unpickler == NULL)
7323 return NULL;
7324
7325 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7326 goto error;
7327
7328 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7329 goto error;
7330
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007331 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007332
7333 result = load(unpickler);
7334 Py_DECREF(unpickler);
7335 return result;
7336
7337 error:
7338 Py_XDECREF(unpickler);
7339 return NULL;
7340}
7341
Larry Hastings61272b72014-01-07 12:41:53 -08007342/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007343
7344_pickle.loads
7345
7346 data: object
7347 *
7348 fix_imports: bool = True
7349 encoding: str = 'ASCII'
7350 errors: str = 'strict'
7351
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007352Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007353
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007354The protocol version of the pickle is detected automatically, so no
7355protocol argument is needed. Bytes past the pickled object's
7356representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007357
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007358Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007359which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007360generated by Python 2. If *fix_imports* is True, pickle will try to
7361map the old Python 2 names to the new names used in Python 3. The
7362*encoding* and *errors* tell pickle how to decode 8-bit string
7363instances pickled by Python 2; these default to 'ASCII' and 'strict',
7364respectively. The *encoding* can be 'bytes' to read these 8-bit
7365string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007366[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007367
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007368static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007369_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007370 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007371/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007372{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007373 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007374 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007375
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007376 if (unpickler == NULL)
7377 return NULL;
7378
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007379 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007380 goto error;
7381
7382 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7383 goto error;
7384
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007385 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007386
7387 result = load(unpickler);
7388 Py_DECREF(unpickler);
7389 return result;
7390
7391 error:
7392 Py_XDECREF(unpickler);
7393 return NULL;
7394}
7395
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007396static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007397 _PICKLE_DUMP_METHODDEF
7398 _PICKLE_DUMPS_METHODDEF
7399 _PICKLE_LOAD_METHODDEF
7400 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007401 {NULL, NULL} /* sentinel */
7402};
7403
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007404static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007405pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007406{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007407 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007408 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007409}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007410
Stefan Krahf483b0f2013-12-14 13:43:10 +01007411static void
7412pickle_free(PyObject *m)
7413{
7414 _Pickle_ClearState(_Pickle_GetState(m));
7415}
7416
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007417static int
7418pickle_traverse(PyObject *m, visitproc visit, void *arg)
7419{
7420 PickleState *st = _Pickle_GetState(m);
7421 Py_VISIT(st->PickleError);
7422 Py_VISIT(st->PicklingError);
7423 Py_VISIT(st->UnpicklingError);
7424 Py_VISIT(st->dispatch_table);
7425 Py_VISIT(st->extension_registry);
7426 Py_VISIT(st->extension_cache);
7427 Py_VISIT(st->inverted_registry);
7428 Py_VISIT(st->name_mapping_2to3);
7429 Py_VISIT(st->import_mapping_2to3);
7430 Py_VISIT(st->name_mapping_3to2);
7431 Py_VISIT(st->import_mapping_3to2);
7432 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007433 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007434 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007435}
7436
7437static struct PyModuleDef _picklemodule = {
7438 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007439 "_pickle", /* m_name */
7440 pickle_module_doc, /* m_doc */
7441 sizeof(PickleState), /* m_size */
7442 pickle_methods, /* m_methods */
7443 NULL, /* m_reload */
7444 pickle_traverse, /* m_traverse */
7445 pickle_clear, /* m_clear */
7446 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007447};
7448
7449PyMODINIT_FUNC
7450PyInit__pickle(void)
7451{
7452 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007453 PickleState *st;
7454
7455 m = PyState_FindModule(&_picklemodule);
7456 if (m) {
7457 Py_INCREF(m);
7458 return m;
7459 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007460
7461 if (PyType_Ready(&Unpickler_Type) < 0)
7462 return NULL;
7463 if (PyType_Ready(&Pickler_Type) < 0)
7464 return NULL;
7465 if (PyType_Ready(&Pdata_Type) < 0)
7466 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007467 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7468 return NULL;
7469 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7470 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007471
7472 /* Create the module and add the functions. */
7473 m = PyModule_Create(&_picklemodule);
7474 if (m == NULL)
7475 return NULL;
7476
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007477 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007478 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7479 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007480 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007481 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7482 return NULL;
7483
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007484 st = _Pickle_GetState(m);
7485
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007486 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007487 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7488 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007489 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007490 st->PicklingError = \
7491 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7492 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007493 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007494 st->UnpicklingError = \
7495 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7496 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007497 return NULL;
7498
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007499 Py_INCREF(st->PickleError);
7500 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007501 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007502 Py_INCREF(st->PicklingError);
7503 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007504 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007505 Py_INCREF(st->UnpicklingError);
7506 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007507 return NULL;
7508
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007509 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007510 return NULL;
7511
7512 return m;
7513}