blob: 3a77005533971f1e898bcdd15530e74d1ff1dbd9 [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
Łukasz Langac51d8c92018-04-03 23:06:53 -070023/* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol.
24 Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python
25 already includes it. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000026enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010027 HIGHEST_PROTOCOL = 4,
Łukasz Langac51d8c92018-04-03 23:06:53 -070028 DEFAULT_PROTOCOL = 4
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029};
30
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000031/* Pickle opcodes. These must be kept updated with pickle.py.
32 Extensive docs are in pickletools.py. */
33enum opcode {
34 MARK = '(',
35 STOP = '.',
36 POP = '0',
37 POP_MARK = '1',
38 DUP = '2',
39 FLOAT = 'F',
40 INT = 'I',
41 BININT = 'J',
42 BININT1 = 'K',
43 LONG = 'L',
44 BININT2 = 'M',
45 NONE = 'N',
46 PERSID = 'P',
47 BINPERSID = 'Q',
48 REDUCE = 'R',
49 STRING = 'S',
50 BINSTRING = 'T',
51 SHORT_BINSTRING = 'U',
52 UNICODE = 'V',
53 BINUNICODE = 'X',
54 APPEND = 'a',
55 BUILD = 'b',
56 GLOBAL = 'c',
57 DICT = 'd',
58 EMPTY_DICT = '}',
59 APPENDS = 'e',
60 GET = 'g',
61 BINGET = 'h',
62 INST = 'i',
63 LONG_BINGET = 'j',
64 LIST = 'l',
65 EMPTY_LIST = ']',
66 OBJ = 'o',
67 PUT = 'p',
68 BINPUT = 'q',
69 LONG_BINPUT = 'r',
70 SETITEM = 's',
71 TUPLE = 't',
72 EMPTY_TUPLE = ')',
73 SETITEMS = 'u',
74 BINFLOAT = 'G',
75
76 /* Protocol 2. */
77 PROTO = '\x80',
78 NEWOBJ = '\x81',
79 EXT1 = '\x82',
80 EXT2 = '\x83',
81 EXT4 = '\x84',
82 TUPLE1 = '\x85',
83 TUPLE2 = '\x86',
84 TUPLE3 = '\x87',
85 NEWTRUE = '\x88',
86 NEWFALSE = '\x89',
87 LONG1 = '\x8a',
88 LONG4 = '\x8b',
89
90 /* Protocol 3 (Python 3.x) */
91 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010092 SHORT_BINBYTES = 'C',
93
94 /* Protocol 4 */
95 SHORT_BINUNICODE = '\x8c',
96 BINUNICODE8 = '\x8d',
97 BINBYTES8 = '\x8e',
98 EMPTY_SET = '\x8f',
99 ADDITEMS = '\x90',
100 FROZENSET = '\x91',
101 NEWOBJ_EX = '\x92',
102 STACK_GLOBAL = '\x93',
103 MEMOIZE = '\x94',
104 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000105};
106
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000107enum {
108 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
109 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
110 break if this gets out of synch with pickle.py, but it's unclear that would
111 help anything either. */
112 BATCHSIZE = 1000,
113
114 /* Nesting limit until Pickler, when running in "fast mode", starts
115 checking for self-referential data-structures. */
116 FAST_NESTING_LIMIT = 50,
117
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000118 /* Initial size of the write buffer of Pickler. */
119 WRITE_BUF_SIZE = 4096,
120
Antoine Pitrou04248a82010-10-12 20:51:21 +0000121 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100122 PREFETCH = 8192 * 16,
123
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200124 FRAME_SIZE_MIN = 4,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100125 FRAME_SIZE_TARGET = 64 * 1024,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100126 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000127};
128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000130
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800131/* State of the pickle module, per PEP 3121. */
132typedef struct {
133 /* Exception classes for pickle. */
134 PyObject *PickleError;
135 PyObject *PicklingError;
136 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800137
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800138 /* copyreg.dispatch_table, {type_object: pickling_function} */
139 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000142
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800143 /* copyreg._extension_registry, {(module_name, function_name): code} */
144 PyObject *extension_registry;
145 /* copyreg._extension_cache, {code: object} */
146 PyObject *extension_cache;
147 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
148 PyObject *inverted_registry;
149
150 /* Import mappings for compatibility with Python 2.x */
151
152 /* _compat_pickle.NAME_MAPPING,
153 {(oldmodule, oldname): (newmodule, newname)} */
154 PyObject *name_mapping_2to3;
155 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
156 PyObject *import_mapping_2to3;
157 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
158 PyObject *name_mapping_3to2;
159 PyObject *import_mapping_3to2;
160
161 /* codecs.encode, used for saving bytes in older protocols */
162 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300163 /* builtins.getattr, used for saving nested names with protocol < 4 */
164 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300165 /* functools.partial, used for implementing __newobj_ex__ with protocols
166 2 and 3 */
167 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800168} PickleState;
169
170/* Forward declaration of the _pickle module definition. */
171static struct PyModuleDef _picklemodule;
172
173/* Given a module object, get its per-module state. */
174static PickleState *
175_Pickle_GetState(PyObject *module)
176{
177 return (PickleState *)PyModule_GetState(module);
178}
179
180/* Find the module instance imported in the currently running sub-interpreter
181 and get its state. */
182static PickleState *
183_Pickle_GetGlobalState(void)
184{
185 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
186}
187
188/* Clear the given pickle module state. */
189static void
190_Pickle_ClearState(PickleState *st)
191{
192 Py_CLEAR(st->PickleError);
193 Py_CLEAR(st->PicklingError);
194 Py_CLEAR(st->UnpicklingError);
195 Py_CLEAR(st->dispatch_table);
196 Py_CLEAR(st->extension_registry);
197 Py_CLEAR(st->extension_cache);
198 Py_CLEAR(st->inverted_registry);
199 Py_CLEAR(st->name_mapping_2to3);
200 Py_CLEAR(st->import_mapping_2to3);
201 Py_CLEAR(st->name_mapping_3to2);
202 Py_CLEAR(st->import_mapping_3to2);
203 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300204 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100205 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800206}
207
208/* Initialize the given pickle module state. */
209static int
210_Pickle_InitState(PickleState *st)
211{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300212 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800213 PyObject *copyreg = NULL;
214 PyObject *compat_pickle = NULL;
215 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300216 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800217
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300218 builtins = PyEval_GetBuiltins();
219 if (builtins == NULL)
220 goto error;
221 st->getattr = PyDict_GetItemString(builtins, "getattr");
222 if (st->getattr == NULL)
223 goto error;
224 Py_INCREF(st->getattr);
225
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800226 copyreg = PyImport_ImportModule("copyreg");
227 if (!copyreg)
228 goto error;
229 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
230 if (!st->dispatch_table)
231 goto error;
232 if (!PyDict_CheckExact(st->dispatch_table)) {
233 PyErr_Format(PyExc_RuntimeError,
234 "copyreg.dispatch_table should be a dict, not %.200s",
235 Py_TYPE(st->dispatch_table)->tp_name);
236 goto error;
237 }
238 st->extension_registry = \
239 PyObject_GetAttrString(copyreg, "_extension_registry");
240 if (!st->extension_registry)
241 goto error;
242 if (!PyDict_CheckExact(st->extension_registry)) {
243 PyErr_Format(PyExc_RuntimeError,
244 "copyreg._extension_registry should be a dict, "
245 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
246 goto error;
247 }
248 st->inverted_registry = \
249 PyObject_GetAttrString(copyreg, "_inverted_registry");
250 if (!st->inverted_registry)
251 goto error;
252 if (!PyDict_CheckExact(st->inverted_registry)) {
253 PyErr_Format(PyExc_RuntimeError,
254 "copyreg._inverted_registry should be a dict, "
255 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
256 goto error;
257 }
258 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
259 if (!st->extension_cache)
260 goto error;
261 if (!PyDict_CheckExact(st->extension_cache)) {
262 PyErr_Format(PyExc_RuntimeError,
263 "copyreg._extension_cache should be a dict, "
264 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
265 goto error;
266 }
267 Py_CLEAR(copyreg);
268
269 /* Load the 2.x -> 3.x stdlib module mapping tables */
270 compat_pickle = PyImport_ImportModule("_compat_pickle");
271 if (!compat_pickle)
272 goto error;
273 st->name_mapping_2to3 = \
274 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
275 if (!st->name_mapping_2to3)
276 goto error;
277 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
278 PyErr_Format(PyExc_RuntimeError,
279 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
280 Py_TYPE(st->name_mapping_2to3)->tp_name);
281 goto error;
282 }
283 st->import_mapping_2to3 = \
284 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
285 if (!st->import_mapping_2to3)
286 goto error;
287 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
288 PyErr_Format(PyExc_RuntimeError,
289 "_compat_pickle.IMPORT_MAPPING should be a dict, "
290 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
291 goto error;
292 }
293 /* ... and the 3.x -> 2.x mapping tables */
294 st->name_mapping_3to2 = \
295 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
296 if (!st->name_mapping_3to2)
297 goto error;
298 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
299 PyErr_Format(PyExc_RuntimeError,
300 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
301 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
302 goto error;
303 }
304 st->import_mapping_3to2 = \
305 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
306 if (!st->import_mapping_3to2)
307 goto error;
308 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
309 PyErr_Format(PyExc_RuntimeError,
310 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
311 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
312 goto error;
313 }
314 Py_CLEAR(compat_pickle);
315
316 codecs = PyImport_ImportModule("codecs");
317 if (codecs == NULL)
318 goto error;
319 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
320 if (st->codecs_encode == NULL) {
321 goto error;
322 }
323 if (!PyCallable_Check(st->codecs_encode)) {
324 PyErr_Format(PyExc_RuntimeError,
325 "codecs.encode should be a callable, not %.200s",
326 Py_TYPE(st->codecs_encode)->tp_name);
327 goto error;
328 }
329 Py_CLEAR(codecs);
330
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300331 functools = PyImport_ImportModule("functools");
332 if (!functools)
333 goto error;
334 st->partial = PyObject_GetAttrString(functools, "partial");
335 if (!st->partial)
336 goto error;
337 Py_CLEAR(functools);
338
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800339 return 0;
340
341 error:
342 Py_CLEAR(copyreg);
343 Py_CLEAR(compat_pickle);
344 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300345 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800346 _Pickle_ClearState(st);
347 return -1;
348}
349
350/* Helper for calling a function with a single argument quickly.
351
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800352 This function steals the reference of the given argument. */
353static PyObject *
354_Pickle_FastCall(PyObject *func, PyObject *obj)
355{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800356 PyObject *result;
357
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100358 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200359 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800360 return result;
361}
362
363/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000364
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200365/* Retrieve and deconstruct a method for avoiding a reference cycle
366 (pickler -> bound method of pickler -> pickler) */
367static int
368init_method_ref(PyObject *self, _Py_Identifier *name,
369 PyObject **method_func, PyObject **method_self)
370{
371 PyObject *func, *func2;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200372 int ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200373
374 /* *method_func and *method_self should be consistent. All refcount decrements
375 should be occurred after setting *method_self and *method_func. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200376 ret = _PyObject_LookupAttrId(self, name, &func);
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200377 if (func == NULL) {
378 *method_self = NULL;
379 Py_CLEAR(*method_func);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200380 return ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200381 }
382
383 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
384 /* Deconstruct a bound Python method */
385 func2 = PyMethod_GET_FUNCTION(func);
386 Py_INCREF(func2);
387 *method_self = self; /* borrowed */
388 Py_XSETREF(*method_func, func2);
389 Py_DECREF(func);
390 return 0;
391 }
392 else {
393 *method_self = NULL;
394 Py_XSETREF(*method_func, func);
395 return 0;
396 }
397}
398
399/* Bind a method if it was deconstructed */
400static PyObject *
401reconstruct_method(PyObject *func, PyObject *self)
402{
403 if (self) {
404 return PyMethod_New(func, self);
405 }
406 else {
407 Py_INCREF(func);
408 return func;
409 }
410}
411
412static PyObject *
413call_method(PyObject *func, PyObject *self, PyObject *obj)
414{
415 if (self) {
416 return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
417 }
418 else {
419 return PyObject_CallFunctionObjArgs(func, obj, NULL);
420 }
421}
422
423/*************************************************************************/
424
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000425/* Internal data type used as the unpickling stack. */
426typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000427 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000428 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200429 int mark_set; /* is MARK set? */
430 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000431 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000432} Pdata;
433
434static void
435Pdata_dealloc(Pdata *self)
436{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200437 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000438 while (--i >= 0) {
439 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000440 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000441 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000442 PyObject_Del(self);
443}
444
445static PyTypeObject Pdata_Type = {
446 PyVarObject_HEAD_INIT(NULL, 0)
447 "_pickle.Pdata", /*tp_name*/
448 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200449 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000450 (destructor)Pdata_dealloc, /*tp_dealloc*/
451};
452
453static PyObject *
454Pdata_New(void)
455{
456 Pdata *self;
457
458 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
459 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000460 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200461 self->mark_set = 0;
462 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000463 self->allocated = 8;
464 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000465 if (self->data)
466 return (PyObject *)self;
467 Py_DECREF(self);
468 return PyErr_NoMemory();
469}
470
471
472/* Retain only the initial clearto items. If clearto >= the current
473 * number of items, this is a (non-erroneous) NOP.
474 */
475static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200476Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200478 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000479
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200480 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000481 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000482 return 0;
483
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000484 while (--i >= clearto) {
485 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000486 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000487 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000488 return 0;
489}
490
491static int
492Pdata_grow(Pdata *self)
493{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000494 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200495 size_t allocated = (size_t)self->allocated;
496 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000497
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000498 new_allocated = (allocated >> 3) + 6;
499 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200500 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000501 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000502 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500503 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000504 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000505 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000506
507 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200508 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000509 return 0;
510
511 nomemory:
512 PyErr_NoMemory();
513 return -1;
514}
515
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200516static int
517Pdata_stack_underflow(Pdata *self)
518{
519 PickleState *st = _Pickle_GetGlobalState();
520 PyErr_SetString(st->UnpicklingError,
521 self->mark_set ?
522 "unexpected MARK found" :
523 "unpickling stack underflow");
524 return -1;
525}
526
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000527/* D is a Pdata*. Pop the topmost element and store it into V, which
528 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
529 * is raised and V is set to NULL.
530 */
531static PyObject *
532Pdata_pop(Pdata *self)
533{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200534 if (Py_SIZE(self) <= self->fence) {
535 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000536 return NULL;
537 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000538 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000539}
540#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
541
542static int
543Pdata_push(Pdata *self, PyObject *obj)
544{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000545 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000546 return -1;
547 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000548 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000549 return 0;
550}
551
552/* Push an object on stack, transferring its ownership to the stack. */
553#define PDATA_PUSH(D, O, ER) do { \
554 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
555
556/* Push an object on stack, adding a new reference to the object. */
557#define PDATA_APPEND(D, O, ER) do { \
558 Py_INCREF((O)); \
559 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
560
561static PyObject *
562Pdata_poptuple(Pdata *self, Py_ssize_t start)
563{
564 PyObject *tuple;
565 Py_ssize_t len, i, j;
566
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200567 if (start < self->fence) {
568 Pdata_stack_underflow(self);
569 return NULL;
570 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000571 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000572 tuple = PyTuple_New(len);
573 if (tuple == NULL)
574 return NULL;
575 for (i = start, j = 0; j < len; i++, j++)
576 PyTuple_SET_ITEM(tuple, j, self->data[i]);
577
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000578 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000579 return tuple;
580}
581
582static PyObject *
583Pdata_poplist(Pdata *self, Py_ssize_t start)
584{
585 PyObject *list;
586 Py_ssize_t len, i, j;
587
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000588 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000589 list = PyList_New(len);
590 if (list == NULL)
591 return NULL;
592 for (i = start, j = 0; j < len; i++, j++)
593 PyList_SET_ITEM(list, j, self->data[i]);
594
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000595 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000596 return list;
597}
598
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000599typedef struct {
600 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200601 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000602} PyMemoEntry;
603
604typedef struct {
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700605 size_t mt_mask;
606 size_t mt_used;
607 size_t mt_allocated;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000608 PyMemoEntry *mt_table;
609} PyMemoTable;
610
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000611typedef struct PicklerObject {
612 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000613 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000614 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000615 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000616 PyObject *pers_func; /* persistent_id() method, can be NULL */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200617 PyObject *pers_func_self; /* borrowed reference to self if pers_func
618 is an unbound method, NULL otherwise */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100619 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000620
621 PyObject *write; /* write() method of the output stream. */
622 PyObject *output_buffer; /* Write into a local bytearray buffer before
623 flushing to the stream. */
624 Py_ssize_t output_len; /* Length of output_buffer. */
625 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000626 int proto; /* Pickle protocol number, >= 0 */
627 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100628 int framing; /* True when framing is enabled, proto >= 4 */
629 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000630 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100631 is no frame currently open. */
632
633 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000634 int fast; /* Enable fast mode if set to a true value.
635 The fast mode disable the usage of memo,
636 therefore speeding the pickling process by
637 not generating superfluous PUT opcodes. It
638 should not be used if with self-referential
639 objects. */
640 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000641 int fix_imports; /* Indicate whether Pickler should fix
642 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000643 PyObject *fast_memo;
644} PicklerObject;
645
646typedef struct UnpicklerObject {
647 PyObject_HEAD
648 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000649
650 /* The unpickler memo is just an array of PyObject *s. Using a dict
651 is unnecessary, since the keys are contiguous ints. */
652 PyObject **memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700653 size_t memo_size; /* Capacity of the memo array */
654 size_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000655
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000656 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200657 PyObject *pers_func_self; /* borrowed reference to self if pers_func
658 is an unbound method, NULL otherwise */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000659
660 Py_buffer buffer;
661 char *input_buffer;
662 char *input_line;
663 Py_ssize_t input_len;
664 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000665 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100666
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000667 PyObject *read; /* read() method of the input stream. */
668 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000669 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000670
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000671 char *encoding; /* Name of the encoding to be used for
672 decoding strings pickled using Python
673 2.x. The default value is "ASCII" */
674 char *errors; /* Name of errors handling scheme to used when
675 decoding strings. The default value is
676 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500677 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000678 objects. */
679 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
680 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000681 int proto; /* Protocol of the pickle loaded. */
682 int fix_imports; /* Indicate whether Unpickler should fix
683 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000684} UnpicklerObject;
685
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200686typedef struct {
687 PyObject_HEAD
688 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
689} PicklerMemoProxyObject;
690
691typedef struct {
692 PyObject_HEAD
693 UnpicklerObject *unpickler;
694} UnpicklerMemoProxyObject;
695
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000696/* Forward declarations */
697static int save(PicklerObject *, PyObject *, int);
698static int save_reduce(PicklerObject *, PyObject *, PyObject *);
699static PyTypeObject Pickler_Type;
700static PyTypeObject Unpickler_Type;
701
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200702#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000703
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000704/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300705 A custom hashtable mapping void* to Python ints. This is used by the pickler
706 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000707 a bunch of unnecessary object creation. This makes a huge performance
708 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000709
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000710#define MT_MINSIZE 8
711#define PERTURB_SHIFT 5
712
713
714static PyMemoTable *
715PyMemoTable_New(void)
716{
717 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
718 if (memo == NULL) {
719 PyErr_NoMemory();
720 return NULL;
721 }
722
723 memo->mt_used = 0;
724 memo->mt_allocated = MT_MINSIZE;
725 memo->mt_mask = MT_MINSIZE - 1;
726 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
727 if (memo->mt_table == NULL) {
728 PyMem_FREE(memo);
729 PyErr_NoMemory();
730 return NULL;
731 }
732 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
733
734 return memo;
735}
736
737static PyMemoTable *
738PyMemoTable_Copy(PyMemoTable *self)
739{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000740 PyMemoTable *new = PyMemoTable_New();
741 if (new == NULL)
742 return NULL;
743
744 new->mt_used = self->mt_used;
745 new->mt_allocated = self->mt_allocated;
746 new->mt_mask = self->mt_mask;
747 /* The table we get from _New() is probably smaller than we wanted.
748 Free it and allocate one that's the right size. */
749 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500750 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000751 if (new->mt_table == NULL) {
752 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200753 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000754 return NULL;
755 }
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700756 for (size_t i = 0; i < self->mt_allocated; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000757 Py_XINCREF(self->mt_table[i].me_key);
758 }
759 memcpy(new->mt_table, self->mt_table,
760 sizeof(PyMemoEntry) * self->mt_allocated);
761
762 return new;
763}
764
765static Py_ssize_t
766PyMemoTable_Size(PyMemoTable *self)
767{
768 return self->mt_used;
769}
770
771static int
772PyMemoTable_Clear(PyMemoTable *self)
773{
774 Py_ssize_t i = self->mt_allocated;
775
776 while (--i >= 0) {
777 Py_XDECREF(self->mt_table[i].me_key);
778 }
779 self->mt_used = 0;
780 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
781 return 0;
782}
783
784static void
785PyMemoTable_Del(PyMemoTable *self)
786{
787 if (self == NULL)
788 return;
789 PyMemoTable_Clear(self);
790
791 PyMem_FREE(self->mt_table);
792 PyMem_FREE(self);
793}
794
795/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
796 can be considerably simpler than dictobject.c's lookdict(). */
797static PyMemoEntry *
798_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
799{
800 size_t i;
801 size_t perturb;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700802 size_t mask = self->mt_mask;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000803 PyMemoEntry *table = self->mt_table;
804 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000805 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000806
807 i = hash & mask;
808 entry = &table[i];
809 if (entry->me_key == NULL || entry->me_key == key)
810 return entry;
811
812 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
813 i = (i << 2) + i + perturb + 1;
814 entry = &table[i & mask];
815 if (entry->me_key == NULL || entry->me_key == key)
816 return entry;
817 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700818 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000819}
820
821/* Returns -1 on failure, 0 on success. */
822static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700823_PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000824{
825 PyMemoEntry *oldtable = NULL;
826 PyMemoEntry *oldentry, *newentry;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700827 size_t new_size = MT_MINSIZE;
828 size_t to_process;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000829
830 assert(min_size > 0);
831
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700832 if (min_size > PY_SSIZE_T_MAX) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000833 PyErr_NoMemory();
834 return -1;
835 }
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700836
837 /* Find the smallest valid table size >= min_size. */
838 while (new_size < min_size) {
839 new_size <<= 1;
840 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000841 /* new_size needs to be a power of two. */
842 assert((new_size & (new_size - 1)) == 0);
843
844 /* Allocate new table. */
845 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500846 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000847 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200848 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000849 PyErr_NoMemory();
850 return -1;
851 }
852 self->mt_allocated = new_size;
853 self->mt_mask = new_size - 1;
854 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
855
856 /* Copy entries from the old table. */
857 to_process = self->mt_used;
858 for (oldentry = oldtable; to_process > 0; oldentry++) {
859 if (oldentry->me_key != NULL) {
860 to_process--;
861 /* newentry is a pointer to a chunk of the new
862 mt_table, so we're setting the key:value pair
863 in-place. */
864 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
865 newentry->me_key = oldentry->me_key;
866 newentry->me_value = oldentry->me_value;
867 }
868 }
869
870 /* Deallocate the old table. */
871 PyMem_FREE(oldtable);
872 return 0;
873}
874
875/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200876static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000877PyMemoTable_Get(PyMemoTable *self, PyObject *key)
878{
879 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
880 if (entry->me_key == NULL)
881 return NULL;
882 return &entry->me_value;
883}
884
885/* Returns -1 on failure, 0 on success. */
886static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200887PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000888{
889 PyMemoEntry *entry;
890
891 assert(key != NULL);
892
893 entry = _PyMemoTable_Lookup(self, key);
894 if (entry->me_key != NULL) {
895 entry->me_value = value;
896 return 0;
897 }
898 Py_INCREF(key);
899 entry->me_key = key;
900 entry->me_value = value;
901 self->mt_used++;
902
903 /* If we added a key, we can safely resize. Otherwise just return!
904 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
905 *
906 * Quadrupling the size improves average table sparseness
907 * (reducing collisions) at the cost of some memory. It also halves
908 * the number of expensive resize operations in a growing memo table.
909 *
910 * Very large memo tables (over 50K items) use doubling instead.
911 * This may help applications with severe memory constraints.
912 */
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700913 if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000914 return 0;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700915 }
916 // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow.
917 size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used;
918 return _PyMemoTable_ResizeTable(self, desired_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000919}
920
921#undef MT_MINSIZE
922#undef PERTURB_SHIFT
923
924/*************************************************************************/
925
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000926
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000927static int
928_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000929{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300930 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200931 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000932 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000933 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100935 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000936 return 0;
937}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000938
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100939static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100940_write_size64(char *out, size_t value)
941{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200942 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800943
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200944 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800945
946 for (i = 0; i < sizeof(size_t); i++) {
947 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
948 }
949 for (i = sizeof(size_t); i < 8; i++) {
950 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800951 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100952}
953
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100954static int
955_Pickler_CommitFrame(PicklerObject *self)
956{
957 size_t frame_len;
958 char *qdata;
959
960 if (!self->framing || self->frame_start == -1)
961 return 0;
962 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
963 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200964 if (frame_len >= FRAME_SIZE_MIN) {
965 qdata[0] = FRAME;
966 _write_size64(qdata + 1, frame_len);
967 }
968 else {
969 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
970 self->output_len -= FRAME_HEADER_SIZE;
971 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100972 self->frame_start = -1;
973 return 0;
974}
975
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000976static PyObject *
977_Pickler_GetString(PicklerObject *self)
978{
979 PyObject *output_buffer = self->output_buffer;
980
981 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982
983 if (_Pickler_CommitFrame(self))
984 return NULL;
985
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000986 self->output_buffer = NULL;
987 /* Resize down to exact size */
988 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
989 return NULL;
990 return output_buffer;
991}
992
993static int
994_Pickler_FlushToFile(PicklerObject *self)
995{
996 PyObject *output, *result;
997
998 assert(self->write != NULL);
999
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001000 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001001 output = _Pickler_GetString(self);
1002 if (output == NULL)
1003 return -1;
1004
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001005 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001006 Py_XDECREF(result);
1007 return (result == NULL) ? -1 : 0;
1008}
1009
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001010static int
1011_Pickler_OpcodeBoundary(PicklerObject *self)
1012{
1013 Py_ssize_t frame_len;
1014
1015 if (!self->framing || self->frame_start == -1) {
1016 return 0;
1017 }
1018 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1019 if (frame_len >= FRAME_SIZE_TARGET) {
1020 if(_Pickler_CommitFrame(self)) {
1021 return -1;
1022 }
Leo Ariasc3d95082018-02-03 18:36:10 -06001023 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001024 * file and reuse the pickler buffer for the next frame so as
1025 * to limit memory usage when dumping large complex objects to
1026 * a file.
1027 *
1028 * self->write is NULL when called via dumps.
1029 */
1030 if (self->write != NULL) {
1031 if (_Pickler_FlushToFile(self) < 0) {
1032 return -1;
1033 }
1034 if (_Pickler_ClearBuffer(self) < 0) {
1035 return -1;
1036 }
1037 }
1038 }
1039 return 0;
1040}
1041
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001042static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001043_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001045 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001047 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001048
1049 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001050 need_new_frame = (self->framing && self->frame_start == -1);
1051
1052 if (need_new_frame)
1053 n = data_len + FRAME_HEADER_SIZE;
1054 else
1055 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001056
1057 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001058 if (required > self->max_output_len) {
1059 /* Make place in buffer for the pickle chunk */
1060 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1061 PyErr_NoMemory();
1062 return -1;
1063 }
1064 self->max_output_len = (self->output_len + n) / 2 * 3;
1065 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1066 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001067 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001068 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001069 if (need_new_frame) {
1070 /* Setup new frame */
1071 Py_ssize_t frame_start = self->output_len;
1072 self->frame_start = frame_start;
1073 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1074 /* Write an invalid value, for debugging */
1075 buffer[frame_start + i] = 0xFE;
1076 }
1077 self->output_len += FRAME_HEADER_SIZE;
1078 }
1079 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001080 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001081 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001082 buffer[self->output_len + i] = s[i];
1083 }
1084 }
1085 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001086 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001087 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001088 self->output_len += data_len;
1089 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001090}
1091
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001092static PicklerObject *
1093_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001094{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001095 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001096
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001097 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1098 if (self == NULL)
1099 return NULL;
1100
1101 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001102 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001103 self->write = NULL;
1104 self->proto = 0;
1105 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001106 self->framing = 0;
1107 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001108 self->fast = 0;
1109 self->fast_nesting = 0;
1110 self->fix_imports = 0;
1111 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001112 self->max_output_len = WRITE_BUF_SIZE;
1113 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001114
1115 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001116 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1117 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001118
1119 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001120 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001121 return NULL;
1122 }
1123 return self;
1124}
1125
1126static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001127_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001129 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001131 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001132 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001133 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001134 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001135 proto = PyLong_AsLong(protocol);
1136 if (proto < 0) {
1137 if (proto == -1 && PyErr_Occurred())
1138 return -1;
1139 proto = HIGHEST_PROTOCOL;
1140 }
1141 else if (proto > HIGHEST_PROTOCOL) {
1142 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1143 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001145 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001146 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001147 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001148 self->bin = proto > 0;
1149 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001150 return 0;
1151}
1152
1153/* Returns -1 (with an exception set) on failure, 0 on success. This may
1154 be called once on a freshly created Pickler. */
1155static int
1156_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1157{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001158 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001159 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001160 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1161 return -1;
1162 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001163 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001164 PyErr_SetString(PyExc_TypeError,
1165 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001166 return -1;
1167 }
1168
1169 return 0;
1170}
1171
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001172/* Returns the size of the input on success, -1 on failure. This takes its
1173 own reference to `input`. */
1174static Py_ssize_t
1175_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1176{
1177 if (self->buffer.buf != NULL)
1178 PyBuffer_Release(&self->buffer);
1179 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1180 return -1;
1181 self->input_buffer = self->buffer.buf;
1182 self->input_len = self->buffer.len;
1183 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001184 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001185 return self->input_len;
1186}
1187
Antoine Pitrou04248a82010-10-12 20:51:21 +00001188static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001189bad_readline(void)
1190{
1191 PickleState *st = _Pickle_GetGlobalState();
1192 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1193 return -1;
1194}
1195
1196static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001197_Unpickler_SkipConsumed(UnpicklerObject *self)
1198{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001199 Py_ssize_t consumed;
1200 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001201
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001202 consumed = self->next_read_idx - self->prefetched_idx;
1203 if (consumed <= 0)
1204 return 0;
1205
1206 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001207 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001208 r = PyObject_CallFunction(self->read, "n", consumed);
1209 if (r == NULL)
1210 return -1;
1211 Py_DECREF(r);
1212
1213 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001214 return 0;
1215}
1216
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001217static const Py_ssize_t READ_WHOLE_LINE = -1;
1218
1219/* If reading from a file, we need to only pull the bytes we need, since there
1220 may be multiple pickle objects arranged contiguously in the same input
1221 buffer.
1222
1223 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1224 bytes from the input stream/buffer.
1225
1226 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1227 failure; on success, returns the number of bytes read from the file.
1228
1229 On success, self->input_len will be 0; this is intentional so that when
1230 unpickling from a file, the "we've run out of data" code paths will trigger,
1231 causing the Unpickler to go back to the file for more data. Use the returned
1232 size to tell you how much data you can process. */
1233static Py_ssize_t
1234_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1235{
1236 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001237 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001238
1239 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001240
Antoine Pitrou04248a82010-10-12 20:51:21 +00001241 if (_Unpickler_SkipConsumed(self) < 0)
1242 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001243
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001244 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001245 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001246 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001247 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001248 PyObject *len;
1249 /* Prefetch some data without advancing the file pointer, if possible */
1250 if (self->peek && n < PREFETCH) {
1251 len = PyLong_FromSsize_t(PREFETCH);
1252 if (len == NULL)
1253 return -1;
1254 data = _Pickle_FastCall(self->peek, len);
1255 if (data == NULL) {
1256 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1257 return -1;
1258 /* peek() is probably not supported by the given file object */
1259 PyErr_Clear();
1260 Py_CLEAR(self->peek);
1261 }
1262 else {
1263 read_size = _Unpickler_SetStringInput(self, data);
1264 Py_DECREF(data);
1265 self->prefetched_idx = 0;
1266 if (n <= read_size)
1267 return n;
1268 }
1269 }
1270 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001271 if (len == NULL)
1272 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001273 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001274 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001275 if (data == NULL)
1276 return -1;
1277
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001278 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001279 Py_DECREF(data);
1280 return read_size;
1281}
1282
Victor Stinner19ed27e2016-05-20 11:42:37 +02001283/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001284static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001285_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001286{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001287 Py_ssize_t num_read;
1288
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001289 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001290 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1291 PickleState *st = _Pickle_GetGlobalState();
1292 PyErr_SetString(st->UnpicklingError,
1293 "read would overflow (invalid bytecode)");
1294 return -1;
1295 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001296
1297 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1298 assert(self->next_read_idx + n > self->input_len);
1299
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001300 if (!self->read)
1301 return bad_readline();
1302
Antoine Pitrou04248a82010-10-12 20:51:21 +00001303 num_read = _Unpickler_ReadFromFile(self, n);
1304 if (num_read < 0)
1305 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001306 if (num_read < n)
1307 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001308 *s = self->input_buffer;
1309 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001310 return n;
1311}
1312
Victor Stinner19ed27e2016-05-20 11:42:37 +02001313/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1314
1315 This should be used for all data reads, rather than accessing the unpickler's
1316 input buffer directly. This method deals correctly with reading from input
1317 streams, which the input buffer doesn't deal with.
1318
1319 Note that when reading from a file-like object, self->next_read_idx won't
1320 be updated (it should remain at 0 for the entire unpickling process). You
1321 should use this function's return value to know how many bytes you can
1322 consume.
1323
1324 Returns -1 (with an exception set) on failure. On success, return the
1325 number of chars read. */
1326#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001327 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001328 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1329 (self)->next_read_idx += (n), \
1330 (n)) \
1331 : _Unpickler_ReadImpl(self, (s), (n)))
1332
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001333static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001334_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1335 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001336{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001337 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001338 if (input_line == NULL) {
1339 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001340 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001341 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001342
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001343 memcpy(input_line, line, len);
1344 input_line[len] = '\0';
1345 self->input_line = input_line;
1346 *result = self->input_line;
1347 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001348}
1349
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001350/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001351 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001352
1353 Returns the number of chars read, or -1 on failure. */
1354static Py_ssize_t
1355_Unpickler_Readline(UnpicklerObject *self, char **result)
1356{
1357 Py_ssize_t i, num_read;
1358
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001359 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001360 if (self->input_buffer[i] == '\n') {
1361 char *line_start = self->input_buffer + self->next_read_idx;
1362 num_read = i - self->next_read_idx + 1;
1363 self->next_read_idx = i + 1;
1364 return _Unpickler_CopyLine(self, line_start, num_read, result);
1365 }
1366 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001367 if (!self->read)
1368 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001369
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001370 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1371 if (num_read < 0)
1372 return -1;
1373 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1374 return bad_readline();
1375 self->next_read_idx = num_read;
1376 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001377}
1378
1379/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1380 will be modified in place. */
1381static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001382_Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001383{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001384 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001385
1386 assert(new_size > self->memo_size);
1387
Sergey Fedoseev67b9cc82018-08-16 09:27:50 +05001388 PyObject **memo_new = self->memo;
1389 PyMem_RESIZE(memo_new, PyObject *, new_size);
1390 if (memo_new == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001391 PyErr_NoMemory();
1392 return -1;
1393 }
Sergey Fedoseev67b9cc82018-08-16 09:27:50 +05001394 self->memo = memo_new;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001395 for (i = self->memo_size; i < new_size; i++)
1396 self->memo[i] = NULL;
1397 self->memo_size = new_size;
1398 return 0;
1399}
1400
1401/* Returns NULL if idx is out of bounds. */
1402static PyObject *
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001403_Unpickler_MemoGet(UnpicklerObject *self, size_t idx)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001404{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001405 if (idx >= self->memo_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001406 return NULL;
1407
1408 return self->memo[idx];
1409}
1410
1411/* Returns -1 (with an exception set) on failure, 0 on success.
1412 This takes its own reference to `value`. */
1413static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001414_Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001415{
1416 PyObject *old_item;
1417
1418 if (idx >= self->memo_size) {
1419 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1420 return -1;
1421 assert(idx < self->memo_size);
1422 }
1423 Py_INCREF(value);
1424 old_item = self->memo[idx];
1425 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001426 if (old_item != NULL) {
1427 Py_DECREF(old_item);
1428 }
1429 else {
1430 self->memo_len++;
1431 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001432 return 0;
1433}
1434
1435static PyObject **
1436_Unpickler_NewMemo(Py_ssize_t new_size)
1437{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001438 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001439 if (memo == NULL) {
1440 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001441 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001442 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001443 memset(memo, 0, new_size * sizeof(PyObject *));
1444 return memo;
1445}
1446
1447/* Free the unpickler's memo, taking care to decref any items left in it. */
1448static void
1449_Unpickler_MemoCleanup(UnpicklerObject *self)
1450{
1451 Py_ssize_t i;
1452 PyObject **memo = self->memo;
1453
1454 if (self->memo == NULL)
1455 return;
1456 self->memo = NULL;
1457 i = self->memo_size;
1458 while (--i >= 0) {
1459 Py_XDECREF(memo[i]);
1460 }
1461 PyMem_FREE(memo);
1462}
1463
1464static UnpicklerObject *
1465_Unpickler_New(void)
1466{
1467 UnpicklerObject *self;
1468
1469 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1470 if (self == NULL)
1471 return NULL;
1472
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001473 self->pers_func = NULL;
1474 self->input_buffer = NULL;
1475 self->input_line = NULL;
1476 self->input_len = 0;
1477 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001478 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001479 self->read = NULL;
1480 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001481 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001482 self->encoding = NULL;
1483 self->errors = NULL;
1484 self->marks = NULL;
1485 self->num_marks = 0;
1486 self->marks_size = 0;
1487 self->proto = 0;
1488 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001489 memset(&self->buffer, 0, sizeof(Py_buffer));
1490 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001491 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001492 self->memo = _Unpickler_NewMemo(self->memo_size);
1493 self->stack = (Pdata *)Pdata_New();
1494
1495 if (self->memo == NULL || self->stack == NULL) {
1496 Py_DECREF(self);
1497 return NULL;
1498 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001499
1500 return self;
1501}
1502
1503/* Returns -1 (with an exception set) on failure, 0 on success. This may
1504 be called once on a freshly created Pickler. */
1505static int
1506_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1507{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001508 _Py_IDENTIFIER(peek);
1509 _Py_IDENTIFIER(read);
1510 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001511
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001512 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1513 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001514 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001515 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1516 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001517 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001518 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001519 PyErr_SetString(PyExc_TypeError,
1520 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001521 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001522 Py_CLEAR(self->read);
1523 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001524 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001525 return -1;
1526 }
1527 return 0;
1528}
1529
1530/* Returns -1 (with an exception set) on failure, 0 on success. This may
1531 be called once on a freshly created Pickler. */
1532static int
1533_Unpickler_SetInputEncoding(UnpicklerObject *self,
1534 const char *encoding,
1535 const char *errors)
1536{
1537 if (encoding == NULL)
1538 encoding = "ASCII";
1539 if (errors == NULL)
1540 errors = "strict";
1541
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001542 self->encoding = _PyMem_Strdup(encoding);
1543 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001544 if (self->encoding == NULL || self->errors == NULL) {
1545 PyErr_NoMemory();
1546 return -1;
1547 }
1548 return 0;
1549}
1550
1551/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001552static int
1553memo_get(PicklerObject *self, PyObject *key)
1554{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001555 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001556 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001557 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001559 value = PyMemoTable_Get(self->memo, key);
1560 if (value == NULL) {
1561 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001562 return -1;
1563 }
1564
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001565 if (!self->bin) {
1566 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001567 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1568 "%" PY_FORMAT_SIZE_T "d\n", *value);
1569 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001570 }
1571 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001572 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001573 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001574 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001575 len = 2;
1576 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001577 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001578 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001579 pdata[1] = (unsigned char)(*value & 0xff);
1580 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1581 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1582 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001583 len = 5;
1584 }
1585 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001586 PickleState *st = _Pickle_GetGlobalState();
1587 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001588 "memo id too large for LONG_BINGET");
1589 return -1;
1590 }
1591 }
1592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001593 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001594 return -1;
1595
1596 return 0;
1597}
1598
1599/* Store an object in the memo, assign it a new unique ID based on the number
1600 of objects currently stored in the memo and generate a PUT opcode. */
1601static int
1602memo_put(PicklerObject *self, PyObject *obj)
1603{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001604 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001605 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001606 Py_ssize_t idx;
1607
1608 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001609
1610 if (self->fast)
1611 return 0;
1612
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001613 idx = PyMemoTable_Size(self->memo);
1614 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1615 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001616
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001617 if (self->proto >= 4) {
1618 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1619 return -1;
1620 return 0;
1621 }
1622 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001623 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001624 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001625 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001626 len = strlen(pdata);
1627 }
1628 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001629 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001630 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001631 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001632 len = 2;
1633 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001634 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001635 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001636 pdata[1] = (unsigned char)(idx & 0xff);
1637 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1638 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1639 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001640 len = 5;
1641 }
1642 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001643 PickleState *st = _Pickle_GetGlobalState();
1644 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001645 "memo id too large for LONG_BINPUT");
1646 return -1;
1647 }
1648 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001649 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001651
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001652 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001653}
1654
1655static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001656get_dotted_path(PyObject *obj, PyObject *name)
1657{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001658 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001659 PyObject *dotted_path;
1660 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001661
1662 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001663 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001664 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001665 n = PyList_GET_SIZE(dotted_path);
1666 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001667 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001668 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001669 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001670 if (obj == NULL)
1671 PyErr_Format(PyExc_AttributeError,
1672 "Can't pickle local object %R", name);
1673 else
1674 PyErr_Format(PyExc_AttributeError,
1675 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001676 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001677 return NULL;
1678 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001679 }
1680 return dotted_path;
1681}
1682
1683static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001684get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001685{
1686 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001687 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001688
1689 assert(PyList_CheckExact(names));
1690 Py_INCREF(obj);
1691 n = PyList_GET_SIZE(names);
1692 for (i = 0; i < n; i++) {
1693 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001694 Py_XDECREF(parent);
1695 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001696 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001697 if (obj == NULL) {
1698 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001699 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001700 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001701 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001702 if (pparent != NULL)
1703 *pparent = parent;
1704 else
1705 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001706 return obj;
1707}
1708
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001709
1710static PyObject *
1711getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1712{
1713 PyObject *dotted_path, *attr;
1714
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001715 if (allow_qualname) {
1716 dotted_path = get_dotted_path(obj, name);
1717 if (dotted_path == NULL)
1718 return NULL;
1719 attr = get_deep_attribute(obj, dotted_path, NULL);
1720 Py_DECREF(dotted_path);
1721 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001722 else {
1723 (void)_PyObject_LookupAttr(obj, name, &attr);
1724 }
1725 if (attr == NULL && !PyErr_Occurred()) {
1726 PyErr_Format(PyExc_AttributeError,
1727 "Can't get attribute %R on %R", name, obj);
1728 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001729 return attr;
1730}
1731
Eric Snow3f9eee62017-09-15 16:35:20 -06001732static int
1733_checkmodule(PyObject *module_name, PyObject *module,
1734 PyObject *global, PyObject *dotted_path)
1735{
1736 if (module == Py_None) {
1737 return -1;
1738 }
1739 if (PyUnicode_Check(module_name) &&
1740 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1741 return -1;
1742 }
1743
1744 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1745 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001746 return -1;
1747 }
1748 if (candidate != global) {
1749 Py_DECREF(candidate);
1750 return -1;
1751 }
1752 Py_DECREF(candidate);
1753 return 0;
1754}
1755
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001756static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001757whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001758{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001759 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001760 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001761 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001762 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001763 _Py_IDENTIFIER(__module__);
1764 _Py_IDENTIFIER(modules);
1765 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001766
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001767 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1768 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001769 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001770 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001771 /* In some rare cases (e.g., bound methods of extension types),
1772 __module__ can be None. If it is so, then search sys.modules for
1773 the module of global. */
1774 if (module_name != Py_None)
1775 return module_name;
1776 Py_CLEAR(module_name);
1777 }
1778 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001779
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001780 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001781 modules = _PySys_GetObjectId(&PyId_modules);
1782 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001783 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001784 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001785 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001786 if (PyDict_CheckExact(modules)) {
1787 i = 0;
1788 while (PyDict_Next(modules, &i, &module_name, &module)) {
1789 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1790 Py_INCREF(module_name);
1791 return module_name;
1792 }
1793 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001794 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001795 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001796 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001797 }
1798 else {
1799 PyObject *iterator = PyObject_GetIter(modules);
1800 if (iterator == NULL) {
1801 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001802 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001803 while ((module_name = PyIter_Next(iterator))) {
1804 module = PyObject_GetItem(modules, module_name);
1805 if (module == NULL) {
1806 Py_DECREF(module_name);
1807 Py_DECREF(iterator);
1808 return NULL;
1809 }
1810 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1811 Py_DECREF(module);
1812 Py_DECREF(iterator);
1813 return module_name;
1814 }
1815 Py_DECREF(module);
1816 Py_DECREF(module_name);
1817 if (PyErr_Occurred()) {
1818 Py_DECREF(iterator);
1819 return NULL;
1820 }
1821 }
1822 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001823 }
1824
1825 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001826 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001827 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001828 return module_name;
1829}
1830
1831/* fast_save_enter() and fast_save_leave() are guards against recursive
1832 objects when Pickler is used with the "fast mode" (i.e., with object
1833 memoization disabled). If the nesting of a list or dict object exceed
1834 FAST_NESTING_LIMIT, these guards will start keeping an internal
1835 reference to the seen list or dict objects and check whether these objects
1836 are recursive. These are not strictly necessary, since save() has a
1837 hard-coded recursion limit, but they give a nicer error message than the
1838 typical RuntimeError. */
1839static int
1840fast_save_enter(PicklerObject *self, PyObject *obj)
1841{
1842 /* if fast_nesting < 0, we're doing an error exit. */
1843 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1844 PyObject *key = NULL;
1845 if (self->fast_memo == NULL) {
1846 self->fast_memo = PyDict_New();
1847 if (self->fast_memo == NULL) {
1848 self->fast_nesting = -1;
1849 return 0;
1850 }
1851 }
1852 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001853 if (key == NULL) {
1854 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001855 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001856 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001857 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001858 Py_DECREF(key);
1859 PyErr_Format(PyExc_ValueError,
1860 "fast mode: can't pickle cyclic objects "
1861 "including object type %.200s at %p",
1862 obj->ob_type->tp_name, obj);
1863 self->fast_nesting = -1;
1864 return 0;
1865 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001866 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001867 Py_DECREF(key);
1868 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001869 return 0;
1870 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001871 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1872 Py_DECREF(key);
1873 self->fast_nesting = -1;
1874 return 0;
1875 }
1876 Py_DECREF(key);
1877 }
1878 return 1;
1879}
1880
1881static int
1882fast_save_leave(PicklerObject *self, PyObject *obj)
1883{
1884 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1885 PyObject *key = PyLong_FromVoidPtr(obj);
1886 if (key == NULL)
1887 return 0;
1888 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1889 Py_DECREF(key);
1890 return 0;
1891 }
1892 Py_DECREF(key);
1893 }
1894 return 1;
1895}
1896
1897static int
1898save_none(PicklerObject *self, PyObject *obj)
1899{
1900 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001901 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001902 return -1;
1903
1904 return 0;
1905}
1906
1907static int
1908save_bool(PicklerObject *self, PyObject *obj)
1909{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001910 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001911 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001912 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001913 return -1;
1914 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001915 else {
1916 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1917 * so that unpicklers written before bools were introduced unpickle them
1918 * as ints, but unpicklers after can recognize that bools were intended.
1919 * Note that protocol 2 added direct ways to pickle bools.
1920 */
1921 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1922 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1923 return -1;
1924 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001925 return 0;
1926}
1927
1928static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001929save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001930{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001931 PyObject *repr = NULL;
1932 Py_ssize_t size;
1933 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001934 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001935 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001936
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001937 val= PyLong_AsLongAndOverflow(obj, &overflow);
1938 if (!overflow && (sizeof(long) <= 4 ||
1939 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1940 {
Larry Hastings61272b72014-01-07 12:41:53 -08001941 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001942
1943 Note: we can't use -0x80000000L in the above condition because some
1944 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1945 before applying the unary minus when sizeof(long) <= 4. The
1946 resulting value stays unsigned which is commonly not what we want,
1947 so MSVC happily warns us about it. However, that result would have
1948 been fine because we guard for sizeof(long) <= 4 which turns the
1949 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001950 char pdata[32];
1951 Py_ssize_t len = 0;
1952
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001953 if (self->bin) {
1954 pdata[1] = (unsigned char)(val & 0xff);
1955 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1956 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1957 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001958
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001959 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1960 pdata[0] = BININT;
1961 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001962 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001963 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001964 pdata[0] = BININT2;
1965 len = 3;
1966 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001967 else {
1968 pdata[0] = BININT1;
1969 len = 2;
1970 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001971 }
1972 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001973 sprintf(pdata, "%c%ld\n", INT, val);
1974 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001975 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001976 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001977 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001978
1979 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001980 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001981 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001982
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001983 if (self->proto >= 2) {
1984 /* Linear-time pickling. */
1985 size_t nbits;
1986 size_t nbytes;
1987 unsigned char *pdata;
1988 char header[5];
1989 int i;
1990 int sign = _PyLong_Sign(obj);
1991
1992 if (sign == 0) {
1993 header[0] = LONG1;
1994 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001995 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001996 goto error;
1997 return 0;
1998 }
1999 nbits = _PyLong_NumBits(obj);
2000 if (nbits == (size_t)-1 && PyErr_Occurred())
2001 goto error;
2002 /* How many bytes do we need? There are nbits >> 3 full
2003 * bytes of data, and nbits & 7 leftover bits. If there
2004 * are any leftover bits, then we clearly need another
2005 * byte. Wnat's not so obvious is that we *probably*
2006 * need another byte even if there aren't any leftovers:
2007 * the most-significant bit of the most-significant byte
2008 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002009 * opposite of the one we need. The exception is ints
2010 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002011 * its own 256's-complement, so has the right sign bit
2012 * even without the extra byte. That's a pain to check
2013 * for in advance, though, so we always grab an extra
2014 * byte at the start, and cut it back later if possible.
2015 */
2016 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002017 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002018 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002019 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002020 goto error;
2021 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002022 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002023 if (repr == NULL)
2024 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002025 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002026 i = _PyLong_AsByteArray((PyLongObject *)obj,
2027 pdata, nbytes,
2028 1 /* little endian */ , 1 /* signed */ );
2029 if (i < 0)
2030 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002031 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 * needed. This is so iff the MSB is all redundant sign
2033 * bits.
2034 */
2035 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002036 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002037 pdata[nbytes - 1] == 0xff &&
2038 (pdata[nbytes - 2] & 0x80) != 0) {
2039 nbytes--;
2040 }
2041
2042 if (nbytes < 256) {
2043 header[0] = LONG1;
2044 header[1] = (unsigned char)nbytes;
2045 size = 2;
2046 }
2047 else {
2048 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002049 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002050 for (i = 1; i < 5; i++) {
2051 header[i] = (unsigned char)(size & 0xff);
2052 size >>= 8;
2053 }
2054 size = 5;
2055 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002056 if (_Pickler_Write(self, header, size) < 0 ||
2057 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002058 goto error;
2059 }
2060 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002061 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002062 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002063
Mark Dickinson8dd05142009-01-20 20:43:58 +00002064 /* proto < 2: write the repr and newline. This is quadratic-time (in
2065 the number of digits), in both directions. We add a trailing 'L'
2066 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002067
2068 repr = PyObject_Repr(obj);
2069 if (repr == NULL)
2070 goto error;
2071
Serhiy Storchaka06515832016-11-20 09:13:07 +02002072 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073 if (string == NULL)
2074 goto error;
2075
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002076 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2077 _Pickler_Write(self, string, size) < 0 ||
2078 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002079 goto error;
2080 }
2081
2082 if (0) {
2083 error:
2084 status = -1;
2085 }
2086 Py_XDECREF(repr);
2087
2088 return status;
2089}
2090
2091static int
2092save_float(PicklerObject *self, PyObject *obj)
2093{
2094 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2095
2096 if (self->bin) {
2097 char pdata[9];
2098 pdata[0] = BINFLOAT;
2099 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2100 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002101 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002102 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002103 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002104 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002105 int result = -1;
2106 char *buf = NULL;
2107 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002108
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002109 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002110 goto done;
2111
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002112 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002113 if (!buf) {
2114 PyErr_NoMemory();
2115 goto done;
2116 }
2117
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002118 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002119 goto done;
2120
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002121 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002122 goto done;
2123
2124 result = 0;
2125done:
2126 PyMem_Free(buf);
2127 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002128 }
2129
2130 return 0;
2131}
2132
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002133/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002134
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002135 The large contiguous data is written directly into the underlying file
2136 object, bypassing the output_buffer of the Pickler. We intentionally
2137 do not insert a protocol 4 frame opcode to make it possible to optimize
2138 file.read calls in the loader.
2139 */
2140static int
2141_Pickler_write_bytes(PicklerObject *self,
2142 const char *header, Py_ssize_t header_size,
2143 const char *data, Py_ssize_t data_size,
2144 PyObject *payload)
2145{
2146 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2147 int framing = self->framing;
2148
2149 if (bypass_buffer) {
2150 assert(self->output_buffer != NULL);
2151 /* Commit the previous frame. */
2152 if (_Pickler_CommitFrame(self)) {
2153 return -1;
2154 }
2155 /* Disable framing temporarily */
2156 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002157 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002158
2159 if (_Pickler_Write(self, header, header_size) < 0) {
2160 return -1;
2161 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002162
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002163 if (bypass_buffer && self->write != NULL) {
2164 /* Bypass the in-memory buffer to directly stream large data
2165 into the underlying file object. */
2166 PyObject *result, *mem = NULL;
2167 /* Dump the output buffer to the file. */
2168 if (_Pickler_FlushToFile(self) < 0) {
2169 return -1;
2170 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002171
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002172 /* Stream write the payload into the file without going through the
2173 output buffer. */
2174 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002175 /* TODO: It would be better to use a memoryview with a linked
2176 original string if this is possible. */
2177 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002178 if (payload == NULL) {
2179 return -1;
2180 }
2181 }
2182 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2183 Py_XDECREF(mem);
2184 if (result == NULL) {
2185 return -1;
2186 }
2187 Py_DECREF(result);
2188
2189 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2190 if (_Pickler_ClearBuffer(self) < 0) {
2191 return -1;
2192 }
2193 }
2194 else {
2195 if (_Pickler_Write(self, data, data_size) < 0) {
2196 return -1;
2197 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002198 }
2199
2200 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002201 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002202
2203 return 0;
2204}
2205
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002206static int
2207save_bytes(PicklerObject *self, PyObject *obj)
2208{
2209 if (self->proto < 3) {
2210 /* Older pickle protocols do not have an opcode for pickling bytes
2211 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002212 the __reduce__ method) to permit bytes object unpickling.
2213
2214 Here we use a hack to be compatible with Python 2. Since in Python
2215 2 'bytes' is just an alias for 'str' (which has different
2216 parameters than the actual bytes object), we use codecs.encode
2217 to create the appropriate 'str' object when unpickled using
2218 Python 2 *and* the appropriate 'bytes' object when unpickled
2219 using Python 3. Again this is a hack and we don't need to do this
2220 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002221 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002222 int status;
2223
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002224 if (PyBytes_GET_SIZE(obj) == 0) {
2225 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2226 }
2227 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002228 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002229 PyObject *unicode_str =
2230 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2231 PyBytes_GET_SIZE(obj),
2232 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002233 _Py_IDENTIFIER(latin1);
2234
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002235 if (unicode_str == NULL)
2236 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002237 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002238 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002239 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002240 Py_DECREF(unicode_str);
2241 }
2242
2243 if (reduce_value == NULL)
2244 return -1;
2245
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002246 /* save_reduce() will memoize the object automatically. */
2247 status = save_reduce(self, reduce_value, obj);
2248 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002249 return status;
2250 }
2251 else {
2252 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002253 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002254 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002256 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257 if (size < 0)
2258 return -1;
2259
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002260 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002261 header[0] = SHORT_BINBYTES;
2262 header[1] = (unsigned char)size;
2263 len = 2;
2264 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002265 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002266 header[0] = BINBYTES;
2267 header[1] = (unsigned char)(size & 0xff);
2268 header[2] = (unsigned char)((size >> 8) & 0xff);
2269 header[3] = (unsigned char)((size >> 16) & 0xff);
2270 header[4] = (unsigned char)((size >> 24) & 0xff);
2271 len = 5;
2272 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002273 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002274 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002275 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002276 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002277 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002278 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002279 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002280 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002281 return -1; /* string too large */
2282 }
2283
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002284 if (_Pickler_write_bytes(self, header, len,
2285 PyBytes_AS_STRING(obj), size, obj) < 0)
2286 {
2287 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002288 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289
2290 if (memo_put(self, obj) < 0)
2291 return -1;
2292
2293 return 0;
2294 }
2295}
2296
2297/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2298 backslash and newline characters to \uXXXX escapes. */
2299static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002300raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002301{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002302 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002303 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002304 void *data;
2305 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002306 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002307
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002308 if (PyUnicode_READY(obj))
2309 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002310
Victor Stinner358af132015-10-12 22:36:57 +02002311 _PyBytesWriter_Init(&writer);
2312
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002313 size = PyUnicode_GET_LENGTH(obj);
2314 data = PyUnicode_DATA(obj);
2315 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002316
Victor Stinner358af132015-10-12 22:36:57 +02002317 p = _PyBytesWriter_Alloc(&writer, size);
2318 if (p == NULL)
2319 goto error;
2320 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002321
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002322 for (i=0; i < size; i++) {
2323 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002324 /* Map 32-bit characters to '\Uxxxxxxxx' */
2325 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002326 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002327 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2328 if (p == NULL)
2329 goto error;
2330
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002331 *p++ = '\\';
2332 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002333 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2334 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2335 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2336 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2337 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2338 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2339 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2340 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002341 }
Victor Stinner358af132015-10-12 22:36:57 +02002342 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002343 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002344 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002345 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2346 if (p == NULL)
2347 goto error;
2348
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002349 *p++ = '\\';
2350 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002351 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2352 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2353 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2354 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002355 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002356 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002357 else
2358 *p++ = (char) ch;
2359 }
Victor Stinner358af132015-10-12 22:36:57 +02002360
2361 return _PyBytesWriter_Finish(&writer, p);
2362
2363error:
2364 _PyBytesWriter_Dealloc(&writer);
2365 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002366}
2367
2368static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002369write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002370{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002371 char header[9];
2372 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002373 PyObject *encoded = NULL;
2374 Py_ssize_t size;
2375 const char *data;
2376
2377 if (PyUnicode_READY(obj))
2378 return -1;
2379
2380 data = PyUnicode_AsUTF8AndSize(obj, &size);
2381 if (data == NULL) {
2382 /* Issue #8383: for strings with lone surrogates, fallback on the
2383 "surrogatepass" error handler. */
2384 PyErr_Clear();
2385 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2386 if (encoded == NULL)
2387 return -1;
2388
2389 data = PyBytes_AS_STRING(encoded);
2390 size = PyBytes_GET_SIZE(encoded);
2391 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002392
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002393 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002394 if (size <= 0xff && self->proto >= 4) {
2395 header[0] = SHORT_BINUNICODE;
2396 header[1] = (unsigned char)(size & 0xff);
2397 len = 2;
2398 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002399 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002400 header[0] = BINUNICODE;
2401 header[1] = (unsigned char)(size & 0xff);
2402 header[2] = (unsigned char)((size >> 8) & 0xff);
2403 header[3] = (unsigned char)((size >> 16) & 0xff);
2404 header[4] = (unsigned char)((size >> 24) & 0xff);
2405 len = 5;
2406 }
2407 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002408 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002409 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002410 len = 9;
2411 }
2412 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002413 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002414 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002415 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002416 return -1;
2417 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002418
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002419 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2420 Py_XDECREF(encoded);
2421 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002422 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002423 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002424 return 0;
2425}
2426
2427static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002428save_unicode(PicklerObject *self, PyObject *obj)
2429{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002430 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002431 if (write_unicode_binary(self, obj) < 0)
2432 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002433 }
2434 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002435 PyObject *encoded;
2436 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002437 const char unicode_op = UNICODE;
2438
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002439 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002440 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002441 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002442
Antoine Pitrou299978d2013-04-07 17:38:11 +02002443 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2444 Py_DECREF(encoded);
2445 return -1;
2446 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002447
2448 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002449 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2450 Py_DECREF(encoded);
2451 return -1;
2452 }
2453 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002454
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002455 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002456 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002457 }
2458 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002459 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002460
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002461 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002462}
2463
2464/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2465static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002466store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002467{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002468 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002469
2470 assert(PyTuple_Size(t) == len);
2471
2472 for (i = 0; i < len; i++) {
2473 PyObject *element = PyTuple_GET_ITEM(t, i);
2474
2475 if (element == NULL)
2476 return -1;
2477 if (save(self, element, 0) < 0)
2478 return -1;
2479 }
2480
2481 return 0;
2482}
2483
2484/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2485 * used across protocols to minimize the space needed to pickle them.
2486 * Tuples are also the only builtin immutable type that can be recursive
2487 * (a tuple can be reached from itself), and that requires some subtle
2488 * magic so that it works in all cases. IOW, this is a long routine.
2489 */
2490static int
2491save_tuple(PicklerObject *self, PyObject *obj)
2492{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002493 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002494
2495 const char mark_op = MARK;
2496 const char tuple_op = TUPLE;
2497 const char pop_op = POP;
2498 const char pop_mark_op = POP_MARK;
2499 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2500
2501 if ((len = PyTuple_Size(obj)) < 0)
2502 return -1;
2503
2504 if (len == 0) {
2505 char pdata[2];
2506
2507 if (self->proto) {
2508 pdata[0] = EMPTY_TUPLE;
2509 len = 1;
2510 }
2511 else {
2512 pdata[0] = MARK;
2513 pdata[1] = TUPLE;
2514 len = 2;
2515 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002516 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517 return -1;
2518 return 0;
2519 }
2520
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002521 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002522 * saving the tuple elements, the tuple must be recursive, in
2523 * which case we'll pop everything we put on the stack, and fetch
2524 * its value from the memo.
2525 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002526 if (len <= 3 && self->proto >= 2) {
2527 /* Use TUPLE{1,2,3} opcodes. */
2528 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002529 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002530
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002531 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002532 /* pop the len elements */
2533 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002534 if (_Pickler_Write(self, &pop_op, 1) < 0)
2535 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002536 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002537 if (memo_get(self, obj) < 0)
2538 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002539
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002540 return 0;
2541 }
2542 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002543 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2544 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002545 }
2546 goto memoize;
2547 }
2548
2549 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2550 * Generate MARK e1 e2 ... TUPLE
2551 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002552 if (_Pickler_Write(self, &mark_op, 1) < 0)
2553 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002554
2555 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002556 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002557
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002558 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002559 /* pop the stack stuff we pushed */
2560 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002561 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2562 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002563 }
2564 else {
2565 /* Note that we pop one more than len, to remove
2566 * the MARK too.
2567 */
2568 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002569 if (_Pickler_Write(self, &pop_op, 1) < 0)
2570 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002571 }
2572 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002573 if (memo_get(self, obj) < 0)
2574 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002575
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002576 return 0;
2577 }
2578 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002579 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2580 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002581 }
2582
2583 memoize:
2584 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002585 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002586
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002587 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002588}
2589
2590/* iter is an iterator giving items, and we batch up chunks of
2591 * MARK item item ... item APPENDS
2592 * opcode sequences. Calling code should have arranged to first create an
2593 * empty list, or list-like object, for the APPENDS to operate on.
2594 * Returns 0 on success, <0 on error.
2595 */
2596static int
2597batch_list(PicklerObject *self, PyObject *iter)
2598{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002599 PyObject *obj = NULL;
2600 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002601 int i, n;
2602
2603 const char mark_op = MARK;
2604 const char append_op = APPEND;
2605 const char appends_op = APPENDS;
2606
2607 assert(iter != NULL);
2608
2609 /* XXX: I think this function could be made faster by avoiding the
2610 iterator interface and fetching objects directly from list using
2611 PyList_GET_ITEM.
2612 */
2613
2614 if (self->proto == 0) {
2615 /* APPENDS isn't available; do one at a time. */
2616 for (;;) {
2617 obj = PyIter_Next(iter);
2618 if (obj == NULL) {
2619 if (PyErr_Occurred())
2620 return -1;
2621 break;
2622 }
2623 i = save(self, obj, 0);
2624 Py_DECREF(obj);
2625 if (i < 0)
2626 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002627 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002628 return -1;
2629 }
2630 return 0;
2631 }
2632
2633 /* proto > 0: write in batches of BATCHSIZE. */
2634 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002635 /* Get first item */
2636 firstitem = PyIter_Next(iter);
2637 if (firstitem == NULL) {
2638 if (PyErr_Occurred())
2639 goto error;
2640
2641 /* nothing more to add */
2642 break;
2643 }
2644
2645 /* Try to get a second item */
2646 obj = PyIter_Next(iter);
2647 if (obj == NULL) {
2648 if (PyErr_Occurred())
2649 goto error;
2650
2651 /* Only one item to write */
2652 if (save(self, firstitem, 0) < 0)
2653 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002654 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002655 goto error;
2656 Py_CLEAR(firstitem);
2657 break;
2658 }
2659
2660 /* More than one item to write */
2661
2662 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002663 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002664 goto error;
2665
2666 if (save(self, firstitem, 0) < 0)
2667 goto error;
2668 Py_CLEAR(firstitem);
2669 n = 1;
2670
2671 /* Fetch and save up to BATCHSIZE items */
2672 while (obj) {
2673 if (save(self, obj, 0) < 0)
2674 goto error;
2675 Py_CLEAR(obj);
2676 n += 1;
2677
2678 if (n == BATCHSIZE)
2679 break;
2680
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002681 obj = PyIter_Next(iter);
2682 if (obj == NULL) {
2683 if (PyErr_Occurred())
2684 goto error;
2685 break;
2686 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002687 }
2688
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002689 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002690 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002691
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002692 } while (n == BATCHSIZE);
2693 return 0;
2694
2695 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002696 Py_XDECREF(firstitem);
2697 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002698 return -1;
2699}
2700
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002701/* This is a variant of batch_list() above, specialized for lists (with no
2702 * support for list subclasses). Like batch_list(), we batch up chunks of
2703 * MARK item item ... item APPENDS
2704 * opcode sequences. Calling code should have arranged to first create an
2705 * empty list, or list-like object, for the APPENDS to operate on.
2706 * Returns 0 on success, -1 on error.
2707 *
2708 * This version is considerably faster than batch_list(), if less general.
2709 *
2710 * Note that this only works for protocols > 0.
2711 */
2712static int
2713batch_list_exact(PicklerObject *self, PyObject *obj)
2714{
2715 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002716 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002717
2718 const char append_op = APPEND;
2719 const char appends_op = APPENDS;
2720 const char mark_op = MARK;
2721
2722 assert(obj != NULL);
2723 assert(self->proto > 0);
2724 assert(PyList_CheckExact(obj));
2725
2726 if (PyList_GET_SIZE(obj) == 1) {
2727 item = PyList_GET_ITEM(obj, 0);
2728 if (save(self, item, 0) < 0)
2729 return -1;
2730 if (_Pickler_Write(self, &append_op, 1) < 0)
2731 return -1;
2732 return 0;
2733 }
2734
2735 /* Write in batches of BATCHSIZE. */
2736 total = 0;
2737 do {
2738 this_batch = 0;
2739 if (_Pickler_Write(self, &mark_op, 1) < 0)
2740 return -1;
2741 while (total < PyList_GET_SIZE(obj)) {
2742 item = PyList_GET_ITEM(obj, total);
2743 if (save(self, item, 0) < 0)
2744 return -1;
2745 total++;
2746 if (++this_batch == BATCHSIZE)
2747 break;
2748 }
2749 if (_Pickler_Write(self, &appends_op, 1) < 0)
2750 return -1;
2751
2752 } while (total < PyList_GET_SIZE(obj));
2753
2754 return 0;
2755}
2756
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002757static int
2758save_list(PicklerObject *self, PyObject *obj)
2759{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002760 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002761 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002762 int status = 0;
2763
2764 if (self->fast && !fast_save_enter(self, obj))
2765 goto error;
2766
2767 /* Create an empty list. */
2768 if (self->bin) {
2769 header[0] = EMPTY_LIST;
2770 len = 1;
2771 }
2772 else {
2773 header[0] = MARK;
2774 header[1] = LIST;
2775 len = 2;
2776 }
2777
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002778 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002779 goto error;
2780
2781 /* Get list length, and bow out early if empty. */
2782 if ((len = PyList_Size(obj)) < 0)
2783 goto error;
2784
2785 if (memo_put(self, obj) < 0)
2786 goto error;
2787
2788 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002789 /* Materialize the list elements. */
2790 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002791 if (Py_EnterRecursiveCall(" while pickling an object"))
2792 goto error;
2793 status = batch_list_exact(self, obj);
2794 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002795 } else {
2796 PyObject *iter = PyObject_GetIter(obj);
2797 if (iter == NULL)
2798 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002799
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002800 if (Py_EnterRecursiveCall(" while pickling an object")) {
2801 Py_DECREF(iter);
2802 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002803 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002804 status = batch_list(self, iter);
2805 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002806 Py_DECREF(iter);
2807 }
2808 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002809 if (0) {
2810 error:
2811 status = -1;
2812 }
2813
2814 if (self->fast && !fast_save_leave(self, obj))
2815 status = -1;
2816
2817 return status;
2818}
2819
2820/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2821 * MARK key value ... key value SETITEMS
2822 * opcode sequences. Calling code should have arranged to first create an
2823 * empty dict, or dict-like object, for the SETITEMS to operate on.
2824 * Returns 0 on success, <0 on error.
2825 *
2826 * This is very much like batch_list(). The difference between saving
2827 * elements directly, and picking apart two-tuples, is so long-winded at
2828 * the C level, though, that attempts to combine these routines were too
2829 * ugly to bear.
2830 */
2831static int
2832batch_dict(PicklerObject *self, PyObject *iter)
2833{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002834 PyObject *obj = NULL;
2835 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002836 int i, n;
2837
2838 const char mark_op = MARK;
2839 const char setitem_op = SETITEM;
2840 const char setitems_op = SETITEMS;
2841
2842 assert(iter != NULL);
2843
2844 if (self->proto == 0) {
2845 /* SETITEMS isn't available; do one at a time. */
2846 for (;;) {
2847 obj = PyIter_Next(iter);
2848 if (obj == NULL) {
2849 if (PyErr_Occurred())
2850 return -1;
2851 break;
2852 }
2853 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2854 PyErr_SetString(PyExc_TypeError, "dict items "
2855 "iterator must return 2-tuples");
2856 return -1;
2857 }
2858 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2859 if (i >= 0)
2860 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2861 Py_DECREF(obj);
2862 if (i < 0)
2863 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002864 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002865 return -1;
2866 }
2867 return 0;
2868 }
2869
2870 /* proto > 0: write in batches of BATCHSIZE. */
2871 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002872 /* Get first item */
2873 firstitem = PyIter_Next(iter);
2874 if (firstitem == NULL) {
2875 if (PyErr_Occurred())
2876 goto error;
2877
2878 /* nothing more to add */
2879 break;
2880 }
2881 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2882 PyErr_SetString(PyExc_TypeError, "dict items "
2883 "iterator must return 2-tuples");
2884 goto error;
2885 }
2886
2887 /* Try to get a second item */
2888 obj = PyIter_Next(iter);
2889 if (obj == NULL) {
2890 if (PyErr_Occurred())
2891 goto error;
2892
2893 /* Only one item to write */
2894 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2895 goto error;
2896 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2897 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002898 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002899 goto error;
2900 Py_CLEAR(firstitem);
2901 break;
2902 }
2903
2904 /* More than one item to write */
2905
2906 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002907 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002908 goto error;
2909
2910 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2911 goto error;
2912 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2913 goto error;
2914 Py_CLEAR(firstitem);
2915 n = 1;
2916
2917 /* Fetch and save up to BATCHSIZE items */
2918 while (obj) {
2919 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2920 PyErr_SetString(PyExc_TypeError, "dict items "
2921 "iterator must return 2-tuples");
2922 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002923 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002924 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2925 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2926 goto error;
2927 Py_CLEAR(obj);
2928 n += 1;
2929
2930 if (n == BATCHSIZE)
2931 break;
2932
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002933 obj = PyIter_Next(iter);
2934 if (obj == NULL) {
2935 if (PyErr_Occurred())
2936 goto error;
2937 break;
2938 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002939 }
2940
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002941 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002942 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002943
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002944 } while (n == BATCHSIZE);
2945 return 0;
2946
2947 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002948 Py_XDECREF(firstitem);
2949 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002950 return -1;
2951}
2952
Collin Winter5c9b02d2009-05-25 05:43:30 +00002953/* This is a variant of batch_dict() above that specializes for dicts, with no
2954 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2955 * MARK key value ... key value SETITEMS
2956 * opcode sequences. Calling code should have arranged to first create an
2957 * empty dict, or dict-like object, for the SETITEMS to operate on.
2958 * Returns 0 on success, -1 on error.
2959 *
2960 * Note that this currently doesn't work for protocol 0.
2961 */
2962static int
2963batch_dict_exact(PicklerObject *self, PyObject *obj)
2964{
2965 PyObject *key = NULL, *value = NULL;
2966 int i;
2967 Py_ssize_t dict_size, ppos = 0;
2968
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002969 const char mark_op = MARK;
2970 const char setitem_op = SETITEM;
2971 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002972
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002973 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002974 assert(self->proto > 0);
2975
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002976 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002977
2978 /* Special-case len(d) == 1 to save space. */
2979 if (dict_size == 1) {
2980 PyDict_Next(obj, &ppos, &key, &value);
2981 if (save(self, key, 0) < 0)
2982 return -1;
2983 if (save(self, value, 0) < 0)
2984 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002985 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002986 return -1;
2987 return 0;
2988 }
2989
2990 /* Write in batches of BATCHSIZE. */
2991 do {
2992 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002993 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002994 return -1;
2995 while (PyDict_Next(obj, &ppos, &key, &value)) {
2996 if (save(self, key, 0) < 0)
2997 return -1;
2998 if (save(self, value, 0) < 0)
2999 return -1;
3000 if (++i == BATCHSIZE)
3001 break;
3002 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003003 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003004 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003005 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003006 PyErr_Format(
3007 PyExc_RuntimeError,
3008 "dictionary changed size during iteration");
3009 return -1;
3010 }
3011
3012 } while (i == BATCHSIZE);
3013 return 0;
3014}
3015
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003016static int
3017save_dict(PicklerObject *self, PyObject *obj)
3018{
3019 PyObject *items, *iter;
3020 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003021 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003022 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003023 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003024
3025 if (self->fast && !fast_save_enter(self, obj))
3026 goto error;
3027
3028 /* Create an empty dict. */
3029 if (self->bin) {
3030 header[0] = EMPTY_DICT;
3031 len = 1;
3032 }
3033 else {
3034 header[0] = MARK;
3035 header[1] = DICT;
3036 len = 2;
3037 }
3038
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003039 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003040 goto error;
3041
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003042 if (memo_put(self, obj) < 0)
3043 goto error;
3044
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003045 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003046 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003047 if (PyDict_CheckExact(obj) && self->proto > 0) {
3048 /* We can take certain shortcuts if we know this is a dict and
3049 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003050 if (Py_EnterRecursiveCall(" while pickling an object"))
3051 goto error;
3052 status = batch_dict_exact(self, obj);
3053 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003054 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003055 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003056
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003057 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003058 if (items == NULL)
3059 goto error;
3060 iter = PyObject_GetIter(items);
3061 Py_DECREF(items);
3062 if (iter == NULL)
3063 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003064 if (Py_EnterRecursiveCall(" while pickling an object")) {
3065 Py_DECREF(iter);
3066 goto error;
3067 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003068 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003069 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003070 Py_DECREF(iter);
3071 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003072 }
3073
3074 if (0) {
3075 error:
3076 status = -1;
3077 }
3078
3079 if (self->fast && !fast_save_leave(self, obj))
3080 status = -1;
3081
3082 return status;
3083}
3084
3085static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003086save_set(PicklerObject *self, PyObject *obj)
3087{
3088 PyObject *item;
3089 int i;
3090 Py_ssize_t set_size, ppos = 0;
3091 Py_hash_t hash;
3092
3093 const char empty_set_op = EMPTY_SET;
3094 const char mark_op = MARK;
3095 const char additems_op = ADDITEMS;
3096
3097 if (self->proto < 4) {
3098 PyObject *items;
3099 PyObject *reduce_value;
3100 int status;
3101
3102 items = PySequence_List(obj);
3103 if (items == NULL) {
3104 return -1;
3105 }
3106 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3107 Py_DECREF(items);
3108 if (reduce_value == NULL) {
3109 return -1;
3110 }
3111 /* save_reduce() will memoize the object automatically. */
3112 status = save_reduce(self, reduce_value, obj);
3113 Py_DECREF(reduce_value);
3114 return status;
3115 }
3116
3117 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3118 return -1;
3119
3120 if (memo_put(self, obj) < 0)
3121 return -1;
3122
3123 set_size = PySet_GET_SIZE(obj);
3124 if (set_size == 0)
3125 return 0; /* nothing to do */
3126
3127 /* Write in batches of BATCHSIZE. */
3128 do {
3129 i = 0;
3130 if (_Pickler_Write(self, &mark_op, 1) < 0)
3131 return -1;
3132 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3133 if (save(self, item, 0) < 0)
3134 return -1;
3135 if (++i == BATCHSIZE)
3136 break;
3137 }
3138 if (_Pickler_Write(self, &additems_op, 1) < 0)
3139 return -1;
3140 if (PySet_GET_SIZE(obj) != set_size) {
3141 PyErr_Format(
3142 PyExc_RuntimeError,
3143 "set changed size during iteration");
3144 return -1;
3145 }
3146 } while (i == BATCHSIZE);
3147
3148 return 0;
3149}
3150
3151static int
3152save_frozenset(PicklerObject *self, PyObject *obj)
3153{
3154 PyObject *iter;
3155
3156 const char mark_op = MARK;
3157 const char frozenset_op = FROZENSET;
3158
3159 if (self->fast && !fast_save_enter(self, obj))
3160 return -1;
3161
3162 if (self->proto < 4) {
3163 PyObject *items;
3164 PyObject *reduce_value;
3165 int status;
3166
3167 items = PySequence_List(obj);
3168 if (items == NULL) {
3169 return -1;
3170 }
3171 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3172 items);
3173 Py_DECREF(items);
3174 if (reduce_value == NULL) {
3175 return -1;
3176 }
3177 /* save_reduce() will memoize the object automatically. */
3178 status = save_reduce(self, reduce_value, obj);
3179 Py_DECREF(reduce_value);
3180 return status;
3181 }
3182
3183 if (_Pickler_Write(self, &mark_op, 1) < 0)
3184 return -1;
3185
3186 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003187 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003188 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003189 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003190 for (;;) {
3191 PyObject *item;
3192
3193 item = PyIter_Next(iter);
3194 if (item == NULL) {
3195 if (PyErr_Occurred()) {
3196 Py_DECREF(iter);
3197 return -1;
3198 }
3199 break;
3200 }
3201 if (save(self, item, 0) < 0) {
3202 Py_DECREF(item);
3203 Py_DECREF(iter);
3204 return -1;
3205 }
3206 Py_DECREF(item);
3207 }
3208 Py_DECREF(iter);
3209
3210 /* If the object is already in the memo, this means it is
3211 recursive. In this case, throw away everything we put on the
3212 stack, and fetch the object back from the memo. */
3213 if (PyMemoTable_Get(self->memo, obj)) {
3214 const char pop_mark_op = POP_MARK;
3215
3216 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3217 return -1;
3218 if (memo_get(self, obj) < 0)
3219 return -1;
3220 return 0;
3221 }
3222
3223 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3224 return -1;
3225 if (memo_put(self, obj) < 0)
3226 return -1;
3227
3228 return 0;
3229}
3230
3231static int
3232fix_imports(PyObject **module_name, PyObject **global_name)
3233{
3234 PyObject *key;
3235 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003236 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003237
3238 key = PyTuple_Pack(2, *module_name, *global_name);
3239 if (key == NULL)
3240 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003241 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003242 Py_DECREF(key);
3243 if (item) {
3244 PyObject *fixed_module_name;
3245 PyObject *fixed_global_name;
3246
3247 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3248 PyErr_Format(PyExc_RuntimeError,
3249 "_compat_pickle.REVERSE_NAME_MAPPING values "
3250 "should be 2-tuples, not %.200s",
3251 Py_TYPE(item)->tp_name);
3252 return -1;
3253 }
3254 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3255 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3256 if (!PyUnicode_Check(fixed_module_name) ||
3257 !PyUnicode_Check(fixed_global_name)) {
3258 PyErr_Format(PyExc_RuntimeError,
3259 "_compat_pickle.REVERSE_NAME_MAPPING values "
3260 "should be pairs of str, not (%.200s, %.200s)",
3261 Py_TYPE(fixed_module_name)->tp_name,
3262 Py_TYPE(fixed_global_name)->tp_name);
3263 return -1;
3264 }
3265
3266 Py_CLEAR(*module_name);
3267 Py_CLEAR(*global_name);
3268 Py_INCREF(fixed_module_name);
3269 Py_INCREF(fixed_global_name);
3270 *module_name = fixed_module_name;
3271 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003272 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003273 }
3274 else if (PyErr_Occurred()) {
3275 return -1;
3276 }
3277
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003278 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003279 if (item) {
3280 if (!PyUnicode_Check(item)) {
3281 PyErr_Format(PyExc_RuntimeError,
3282 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3283 "should be strings, not %.200s",
3284 Py_TYPE(item)->tp_name);
3285 return -1;
3286 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003287 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003288 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003289 }
3290 else if (PyErr_Occurred()) {
3291 return -1;
3292 }
3293
3294 return 0;
3295}
3296
3297static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003298save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3299{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003300 PyObject *global_name = NULL;
3301 PyObject *module_name = NULL;
3302 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003303 PyObject *parent = NULL;
3304 PyObject *dotted_path = NULL;
3305 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003306 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003307 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003308 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003309 _Py_IDENTIFIER(__name__);
3310 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003311
3312 const char global_op = GLOBAL;
3313
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003314 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003315 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003316 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003317 }
3318 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003319 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3320 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003321 if (global_name == NULL) {
3322 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3323 if (global_name == NULL)
3324 goto error;
3325 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003326 }
3327
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003328 dotted_path = get_dotted_path(module, global_name);
3329 if (dotted_path == NULL)
3330 goto error;
3331 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003332 if (module_name == NULL)
3333 goto error;
3334
3335 /* XXX: Change to use the import C API directly with level=0 to disallow
3336 relative imports.
3337
3338 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3339 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3340 custom import functions (IMHO, this would be a nice security
3341 feature). The import C API would need to be extended to support the
3342 extra parameters of __import__ to fix that. */
3343 module = PyImport_Import(module_name);
3344 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003345 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003346 "Can't pickle %R: import of module %R failed",
3347 obj, module_name);
3348 goto error;
3349 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003350 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3351 Py_INCREF(lastname);
3352 cls = get_deep_attribute(module, dotted_path, &parent);
3353 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003354 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003355 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003356 "Can't pickle %R: attribute lookup %S on %S failed",
3357 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003358 goto error;
3359 }
3360 if (cls != obj) {
3361 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003362 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003363 "Can't pickle %R: it's not the same object as %S.%S",
3364 obj, module_name, global_name);
3365 goto error;
3366 }
3367 Py_DECREF(cls);
3368
3369 if (self->proto >= 2) {
3370 /* See whether this is in the extension registry, and if
3371 * so generate an EXT opcode.
3372 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003373 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003374 PyObject *code_obj; /* extension code as Python object */
3375 long code; /* extension code as C value */
3376 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003377 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003378
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003379 extension_key = PyTuple_Pack(2, module_name, global_name);
3380 if (extension_key == NULL) {
3381 goto error;
3382 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003383 code_obj = PyDict_GetItemWithError(st->extension_registry,
3384 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003385 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003386 /* The object is not registered in the extension registry.
3387 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003388 if (code_obj == NULL) {
3389 if (PyErr_Occurred()) {
3390 goto error;
3391 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003392 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003393 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003394
3395 /* XXX: pickle.py doesn't check neither the type, nor the range
3396 of the value returned by the extension_registry. It should for
3397 consistency. */
3398
3399 /* Verify code_obj has the right type and value. */
3400 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003401 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003402 "Can't pickle %R: extension code %R isn't an integer",
3403 obj, code_obj);
3404 goto error;
3405 }
3406 code = PyLong_AS_LONG(code_obj);
3407 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003408 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003409 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3410 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003411 goto error;
3412 }
3413
3414 /* Generate an EXT opcode. */
3415 if (code <= 0xff) {
3416 pdata[0] = EXT1;
3417 pdata[1] = (unsigned char)code;
3418 n = 2;
3419 }
3420 else if (code <= 0xffff) {
3421 pdata[0] = EXT2;
3422 pdata[1] = (unsigned char)(code & 0xff);
3423 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3424 n = 3;
3425 }
3426 else {
3427 pdata[0] = EXT4;
3428 pdata[1] = (unsigned char)(code & 0xff);
3429 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3430 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3431 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3432 n = 5;
3433 }
3434
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003435 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003436 goto error;
3437 }
3438 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003439 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003440 if (parent == module) {
3441 Py_INCREF(lastname);
3442 Py_DECREF(global_name);
3443 global_name = lastname;
3444 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003445 if (self->proto >= 4) {
3446 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003447
Christian Heimese8b1ba12013-11-23 21:13:39 +01003448 if (save(self, module_name, 0) < 0)
3449 goto error;
3450 if (save(self, global_name, 0) < 0)
3451 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003452
3453 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3454 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003455 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003456 else if (parent != module) {
3457 PickleState *st = _Pickle_GetGlobalState();
3458 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3459 st->getattr, parent, lastname);
Alexey Izbyshevf8c06b02018-08-22 07:51:25 +03003460 if (reduce_value == NULL)
3461 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003462 status = save_reduce(self, reduce_value, NULL);
3463 Py_DECREF(reduce_value);
3464 if (status < 0)
3465 goto error;
3466 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003467 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003468 /* Generate a normal global opcode if we are using a pickle
3469 protocol < 4, or if the object is not registered in the
3470 extension registry. */
3471 PyObject *encoded;
3472 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003473
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003474 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003475 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003476
3477 /* For protocol < 3 and if the user didn't request against doing
3478 so, we convert module names to the old 2.x module names. */
3479 if (self->proto < 3 && self->fix_imports) {
3480 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003481 goto error;
3482 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003483 }
3484
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003485 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3486 both the module name and the global name using UTF-8. We do so
3487 only when we are using the pickle protocol newer than version
3488 3. This is to ensure compatibility with older Unpickler running
3489 on Python 2.x. */
3490 if (self->proto == 3) {
3491 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003492 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003493 else {
3494 unicode_encoder = PyUnicode_AsASCIIString;
3495 }
3496 encoded = unicode_encoder(module_name);
3497 if (encoded == NULL) {
3498 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003499 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003500 "can't pickle module identifier '%S' using "
3501 "pickle protocol %i",
3502 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003503 goto error;
3504 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003505 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3506 PyBytes_GET_SIZE(encoded)) < 0) {
3507 Py_DECREF(encoded);
3508 goto error;
3509 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003510 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003511 if(_Pickler_Write(self, "\n", 1) < 0)
3512 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003513
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003514 /* Save the name of the module. */
3515 encoded = unicode_encoder(global_name);
3516 if (encoded == NULL) {
3517 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003518 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003519 "can't pickle global identifier '%S' using "
3520 "pickle protocol %i",
3521 global_name, self->proto);
3522 goto error;
3523 }
3524 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3525 PyBytes_GET_SIZE(encoded)) < 0) {
3526 Py_DECREF(encoded);
3527 goto error;
3528 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003529 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003530 if (_Pickler_Write(self, "\n", 1) < 0)
3531 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003532 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003533 /* Memoize the object. */
3534 if (memo_put(self, obj) < 0)
3535 goto error;
3536 }
3537
3538 if (0) {
3539 error:
3540 status = -1;
3541 }
3542 Py_XDECREF(module_name);
3543 Py_XDECREF(global_name);
3544 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003545 Py_XDECREF(parent);
3546 Py_XDECREF(dotted_path);
3547 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003548
3549 return status;
3550}
3551
3552static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003553save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3554{
3555 PyObject *reduce_value;
3556 int status;
3557
3558 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3559 if (reduce_value == NULL) {
3560 return -1;
3561 }
3562 status = save_reduce(self, reduce_value, obj);
3563 Py_DECREF(reduce_value);
3564 return status;
3565}
3566
3567static int
3568save_type(PicklerObject *self, PyObject *obj)
3569{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003570 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003571 return save_singleton_type(self, obj, Py_None);
3572 }
3573 else if (obj == (PyObject *)&PyEllipsis_Type) {
3574 return save_singleton_type(self, obj, Py_Ellipsis);
3575 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003576 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003577 return save_singleton_type(self, obj, Py_NotImplemented);
3578 }
3579 return save_global(self, obj, NULL);
3580}
3581
3582static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003583save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003584{
3585 PyObject *pid = NULL;
3586 int status = 0;
3587
3588 const char persid_op = PERSID;
3589 const char binpersid_op = BINPERSID;
3590
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003591 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003592 if (pid == NULL)
3593 return -1;
3594
3595 if (pid != Py_None) {
3596 if (self->bin) {
3597 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003598 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003599 goto error;
3600 }
3601 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003602 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003603
3604 pid_str = PyObject_Str(pid);
3605 if (pid_str == NULL)
3606 goto error;
3607
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003608 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003609 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003610 if (!PyUnicode_IS_ASCII(pid_str)) {
3611 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3612 "persistent IDs in protocol 0 must be "
3613 "ASCII strings");
3614 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003615 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003616 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003617
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003618 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003619 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3620 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3621 _Pickler_Write(self, "\n", 1) < 0) {
3622 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003623 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003624 }
3625 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003626 }
3627 status = 1;
3628 }
3629
3630 if (0) {
3631 error:
3632 status = -1;
3633 }
3634 Py_XDECREF(pid);
3635
3636 return status;
3637}
3638
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003639static PyObject *
3640get_class(PyObject *obj)
3641{
3642 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003643 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003644
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003645 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3646 cls = (PyObject *) Py_TYPE(obj);
3647 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003648 }
3649 return cls;
3650}
3651
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003652/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3653 * appropriate __reduce__ method for obj.
3654 */
3655static int
3656save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3657{
3658 PyObject *callable;
3659 PyObject *argtup;
3660 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003661 PyObject *listitems = Py_None;
3662 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003663 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003664 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003665 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003666
3667 const char reduce_op = REDUCE;
3668 const char build_op = BUILD;
3669 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003670 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003671
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003672 size = PyTuple_Size(args);
3673 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003674 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003675 "__reduce__ must contain 2 through 5 elements");
3676 return -1;
3677 }
3678
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003679 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3680 &callable, &argtup, &state, &listitems, &dictitems))
3681 return -1;
3682
3683 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003684 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003685 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003686 return -1;
3687 }
3688 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003689 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003690 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003691 return -1;
3692 }
3693
3694 if (state == Py_None)
3695 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003696
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003697 if (listitems == Py_None)
3698 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003699 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003700 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003701 "returned by __reduce__ must be an iterator, not %s",
3702 Py_TYPE(listitems)->tp_name);
3703 return -1;
3704 }
3705
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003706 if (dictitems == Py_None)
3707 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003708 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003709 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003710 "returned by __reduce__ must be an iterator, not %s",
3711 Py_TYPE(dictitems)->tp_name);
3712 return -1;
3713 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003714
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003715 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003716 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003717 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003718
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003719 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3720 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003721 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003722 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003723 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003724 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3725 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003726 if (!use_newobj_ex) {
3727 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003728 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003729 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003730 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003731 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003732 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003733
3734 if (use_newobj_ex) {
3735 PyObject *cls;
3736 PyObject *args;
3737 PyObject *kwargs;
3738
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003739 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003740 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003741 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003742 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003743 return -1;
3744 }
3745
3746 cls = PyTuple_GET_ITEM(argtup, 0);
3747 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003748 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003749 "first item from NEWOBJ_EX argument tuple must "
3750 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3751 return -1;
3752 }
3753 args = PyTuple_GET_ITEM(argtup, 1);
3754 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003755 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003756 "second item from NEWOBJ_EX argument tuple must "
3757 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3758 return -1;
3759 }
3760 kwargs = PyTuple_GET_ITEM(argtup, 2);
3761 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003762 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003763 "third item from NEWOBJ_EX argument tuple must "
3764 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3765 return -1;
3766 }
3767
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003768 if (self->proto >= 4) {
3769 if (save(self, cls, 0) < 0 ||
3770 save(self, args, 0) < 0 ||
3771 save(self, kwargs, 0) < 0 ||
3772 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3773 return -1;
3774 }
3775 }
3776 else {
3777 PyObject *newargs;
3778 PyObject *cls_new;
3779 Py_ssize_t i;
3780 _Py_IDENTIFIER(__new__);
3781
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003782 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003783 if (newargs == NULL)
3784 return -1;
3785
3786 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3787 if (cls_new == NULL) {
3788 Py_DECREF(newargs);
3789 return -1;
3790 }
3791 PyTuple_SET_ITEM(newargs, 0, cls_new);
3792 Py_INCREF(cls);
3793 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003794 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003795 PyObject *item = PyTuple_GET_ITEM(args, i);
3796 Py_INCREF(item);
3797 PyTuple_SET_ITEM(newargs, i + 2, item);
3798 }
3799
3800 callable = PyObject_Call(st->partial, newargs, kwargs);
3801 Py_DECREF(newargs);
3802 if (callable == NULL)
3803 return -1;
3804
3805 newargs = PyTuple_New(0);
3806 if (newargs == NULL) {
3807 Py_DECREF(callable);
3808 return -1;
3809 }
3810
3811 if (save(self, callable, 0) < 0 ||
3812 save(self, newargs, 0) < 0 ||
3813 _Pickler_Write(self, &reduce_op, 1) < 0) {
3814 Py_DECREF(newargs);
3815 Py_DECREF(callable);
3816 return -1;
3817 }
3818 Py_DECREF(newargs);
3819 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003820 }
3821 }
3822 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003823 PyObject *cls;
3824 PyObject *newargtup;
3825 PyObject *obj_class;
3826 int p;
3827
3828 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003829 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003830 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003831 return -1;
3832 }
3833
3834 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003835 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003836 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003837 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003838 return -1;
3839 }
3840
3841 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003842 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003843 p = obj_class != cls; /* true iff a problem */
3844 Py_DECREF(obj_class);
3845 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003846 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003847 "__newobj__ args has the wrong class");
3848 return -1;
3849 }
3850 }
3851 /* XXX: These calls save() are prone to infinite recursion. Imagine
3852 what happen if the value returned by the __reduce__() method of
3853 some extension type contains another object of the same type. Ouch!
3854
3855 Here is a quick example, that I ran into, to illustrate what I
3856 mean:
3857
3858 >>> import pickle, copyreg
3859 >>> copyreg.dispatch_table.pop(complex)
3860 >>> pickle.dumps(1+2j)
3861 Traceback (most recent call last):
3862 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003863 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003864
3865 Removing the complex class from copyreg.dispatch_table made the
3866 __reduce_ex__() method emit another complex object:
3867
3868 >>> (1+1j).__reduce_ex__(2)
3869 (<function __newobj__ at 0xb7b71c3c>,
3870 (<class 'complex'>, (1+1j)), None, None, None)
3871
3872 Thus when save() was called on newargstup (the 2nd item) recursion
3873 ensued. Of course, the bug was in the complex class which had a
3874 broken __getnewargs__() that emitted another complex object. But,
3875 the point, here, is it is quite easy to end up with a broken reduce
3876 function. */
3877
3878 /* Save the class and its __new__ arguments. */
3879 if (save(self, cls, 0) < 0)
3880 return -1;
3881
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003882 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003883 if (newargtup == NULL)
3884 return -1;
3885
3886 p = save(self, newargtup, 0);
3887 Py_DECREF(newargtup);
3888 if (p < 0)
3889 return -1;
3890
3891 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003892 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003893 return -1;
3894 }
3895 else { /* Not using NEWOBJ. */
3896 if (save(self, callable, 0) < 0 ||
3897 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003898 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 return -1;
3900 }
3901
3902 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3903 the caller do not want to memoize the object. Not particularly useful,
3904 but that is to mimic the behavior save_reduce() in pickle.py when
3905 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003906 if (obj != NULL) {
3907 /* If the object is already in the memo, this means it is
3908 recursive. In this case, throw away everything we put on the
3909 stack, and fetch the object back from the memo. */
3910 if (PyMemoTable_Get(self->memo, obj)) {
3911 const char pop_op = POP;
3912
3913 if (_Pickler_Write(self, &pop_op, 1) < 0)
3914 return -1;
3915 if (memo_get(self, obj) < 0)
3916 return -1;
3917
3918 return 0;
3919 }
3920 else if (memo_put(self, obj) < 0)
3921 return -1;
3922 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003923
3924 if (listitems && batch_list(self, listitems) < 0)
3925 return -1;
3926
3927 if (dictitems && batch_dict(self, dictitems) < 0)
3928 return -1;
3929
3930 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003931 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003932 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 return -1;
3934 }
3935
3936 return 0;
3937}
3938
3939static int
3940save(PicklerObject *self, PyObject *obj, int pers_save)
3941{
3942 PyTypeObject *type;
3943 PyObject *reduce_func = NULL;
3944 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003945 int status = 0;
3946
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003947 if (_Pickler_OpcodeBoundary(self) < 0)
3948 return -1;
3949
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003950 /* The extra pers_save argument is necessary to avoid calling save_pers()
3951 on its returned object. */
3952 if (!pers_save && self->pers_func) {
3953 /* save_pers() returns:
3954 -1 to signal an error;
3955 0 if it did nothing successfully;
3956 1 if a persistent id was saved.
3957 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003958 if ((status = save_pers(self, obj)) != 0)
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003959 return status;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003960 }
3961
3962 type = Py_TYPE(obj);
3963
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003964 /* The old cPickle had an optimization that used switch-case statement
3965 dispatching on the first letter of the type name. This has was removed
3966 since benchmarks shown that this optimization was actually slowing
3967 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003968
3969 /* Atom types; these aren't memoized, so don't check the memo. */
3970
3971 if (obj == Py_None) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003972 return save_none(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 }
3974 else if (obj == Py_False || obj == Py_True) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003975 return save_bool(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003976 }
3977 else if (type == &PyLong_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003978 return save_long(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003979 }
3980 else if (type == &PyFloat_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003981 return save_float(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003982 }
3983
3984 /* Check the memo to see if it has the object. If so, generate
3985 a GET (or BINGET) opcode, instead of pickling the object
3986 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003987 if (PyMemoTable_Get(self->memo, obj)) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003988 return memo_get(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003989 }
3990
3991 if (type == &PyBytes_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003992 return save_bytes(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003993 }
3994 else if (type == &PyUnicode_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003995 return save_unicode(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003996 }
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003997
3998 /* We're only calling Py_EnterRecursiveCall here so that atomic
3999 types above are pickled faster. */
4000 if (Py_EnterRecursiveCall(" while pickling an object")) {
4001 return -1;
4002 }
4003
4004 if (type == &PyDict_Type) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004005 status = save_dict(self, obj);
4006 goto done;
4007 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004008 else if (type == &PySet_Type) {
4009 status = save_set(self, obj);
4010 goto done;
4011 }
4012 else if (type == &PyFrozenSet_Type) {
4013 status = save_frozenset(self, obj);
4014 goto done;
4015 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004016 else if (type == &PyList_Type) {
4017 status = save_list(self, obj);
4018 goto done;
4019 }
4020 else if (type == &PyTuple_Type) {
4021 status = save_tuple(self, obj);
4022 goto done;
4023 }
4024 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004025 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004026 goto done;
4027 }
4028 else if (type == &PyFunction_Type) {
4029 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004030 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004032
4033 /* XXX: This part needs some unit tests. */
4034
4035 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004036 * self.dispatch_table, copyreg.dispatch_table, the object's
4037 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004038 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004039 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004040 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004041 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4042 (PyObject *)type);
4043 if (reduce_func == NULL) {
4044 if (PyErr_Occurred()) {
4045 goto error;
4046 }
4047 } else {
4048 /* PyDict_GetItemWithError() returns a borrowed reference.
4049 Increase the reference count to be consistent with
4050 PyObject_GetItem and _PyObject_GetAttrId used below. */
4051 Py_INCREF(reduce_func);
4052 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004053 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004054 reduce_func = PyObject_GetItem(self->dispatch_table,
4055 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004056 if (reduce_func == NULL) {
4057 if (PyErr_ExceptionMatches(PyExc_KeyError))
4058 PyErr_Clear();
4059 else
4060 goto error;
4061 }
4062 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004063 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004064 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004065 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004066 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004067 else if (PyType_IsSubtype(type, &PyType_Type)) {
4068 status = save_global(self, obj, NULL);
4069 goto done;
4070 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004071 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004072 _Py_IDENTIFIER(__reduce__);
4073 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004074
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004075
4076 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4077 automatically defined as __reduce__. While this is convenient, this
4078 make it impossible to know which method was actually called. Of
4079 course, this is not a big deal. But still, it would be nice to let
4080 the user know which method was called when something go
4081 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4082 don't actually have to check for a __reduce__ method. */
4083
4084 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004085 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4086 goto error;
4087 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004088 if (reduce_func != NULL) {
4089 PyObject *proto;
4090 proto = PyLong_FromLong(self->proto);
4091 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004092 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 }
4094 }
4095 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004096 PickleState *st = _Pickle_GetGlobalState();
4097
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004098 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004099 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004100 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004101 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004102 }
4103 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004104 PyErr_Format(st->PicklingError,
4105 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004106 type->tp_name, obj);
4107 goto error;
4108 }
4109 }
4110 }
4111
4112 if (reduce_value == NULL)
4113 goto error;
4114
4115 if (PyUnicode_Check(reduce_value)) {
4116 status = save_global(self, obj, reduce_value);
4117 goto done;
4118 }
4119
4120 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004121 PickleState *st = _Pickle_GetGlobalState();
4122 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004123 "__reduce__ must return a string or tuple");
4124 goto error;
4125 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004126
4127 status = save_reduce(self, reduce_value, obj);
4128
4129 if (0) {
4130 error:
4131 status = -1;
4132 }
4133 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004134
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004135 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004136 Py_XDECREF(reduce_func);
4137 Py_XDECREF(reduce_value);
4138
4139 return status;
4140}
4141
4142static int
4143dump(PicklerObject *self, PyObject *obj)
4144{
4145 const char stop_op = STOP;
4146
4147 if (self->proto >= 2) {
4148 char header[2];
4149
4150 header[0] = PROTO;
4151 assert(self->proto >= 0 && self->proto < 256);
4152 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004153 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004154 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004155 if (self->proto >= 4)
4156 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004157 }
4158
4159 if (save(self, obj, 0) < 0 ||
Serhiy Storchakac8695292018-04-04 00:11:27 +03004160 _Pickler_Write(self, &stop_op, 1) < 0 ||
4161 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004162 return -1;
Serhiy Storchakac8695292018-04-04 00:11:27 +03004163 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004164 return 0;
4165}
4166
Larry Hastings61272b72014-01-07 12:41:53 -08004167/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004168
4169_pickle.Pickler.clear_memo
4170
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004171Clears the pickler's "memo".
4172
4173The memo is the data structure that remembers which objects the
4174pickler has already seen, so that shared or recursive objects are
4175pickled by reference and not by value. This method is useful when
4176re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004177[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004178
Larry Hastings3cceb382014-01-04 11:09:09 -08004179static PyObject *
4180_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004181/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004182{
4183 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004184 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004185
4186 Py_RETURN_NONE;
4187}
4188
Larry Hastings61272b72014-01-07 12:41:53 -08004189/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004190
4191_pickle.Pickler.dump
4192
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004193 obj: object
4194 /
4195
4196Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004197[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004198
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004199static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004200_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004201/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004202{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004203 /* Check whether the Pickler was initialized correctly (issue3664).
4204 Developers often forget to call __init__() in their subclasses, which
4205 would trigger a segfault without this check. */
4206 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004207 PickleState *st = _Pickle_GetGlobalState();
4208 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004209 "Pickler.__init__() was not called by %s.__init__()",
4210 Py_TYPE(self)->tp_name);
4211 return NULL;
4212 }
4213
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004214 if (_Pickler_ClearBuffer(self) < 0)
4215 return NULL;
4216
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004217 if (dump(self, obj) < 0)
4218 return NULL;
4219
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004220 if (_Pickler_FlushToFile(self) < 0)
4221 return NULL;
4222
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004223 Py_RETURN_NONE;
4224}
4225
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004226/*[clinic input]
4227
4228_pickle.Pickler.__sizeof__ -> Py_ssize_t
4229
4230Returns size in memory, in bytes.
4231[clinic start generated code]*/
4232
4233static Py_ssize_t
4234_pickle_Pickler___sizeof___impl(PicklerObject *self)
4235/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4236{
4237 Py_ssize_t res, s;
4238
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004239 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004240 if (self->memo != NULL) {
4241 res += sizeof(PyMemoTable);
4242 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4243 }
4244 if (self->output_buffer != NULL) {
4245 s = _PySys_GetSizeOf(self->output_buffer);
4246 if (s == -1)
4247 return -1;
4248 res += s;
4249 }
4250 return res;
4251}
4252
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004253static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004254 _PICKLE_PICKLER_DUMP_METHODDEF
4255 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004256 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004257 {NULL, NULL} /* sentinel */
4258};
4259
4260static void
4261Pickler_dealloc(PicklerObject *self)
4262{
4263 PyObject_GC_UnTrack(self);
4264
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004265 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004266 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004267 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004268 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004269 Py_XDECREF(self->fast_memo);
4270
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004271 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004272
4273 Py_TYPE(self)->tp_free((PyObject *)self);
4274}
4275
4276static int
4277Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4278{
4279 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004280 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004281 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004282 Py_VISIT(self->fast_memo);
4283 return 0;
4284}
4285
4286static int
4287Pickler_clear(PicklerObject *self)
4288{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004289 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004290 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004291 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004292 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004293 Py_CLEAR(self->fast_memo);
4294
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004295 if (self->memo != NULL) {
4296 PyMemoTable *memo = self->memo;
4297 self->memo = NULL;
4298 PyMemoTable_Del(memo);
4299 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004300 return 0;
4301}
4302
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004303
Larry Hastings61272b72014-01-07 12:41:53 -08004304/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004305
4306_pickle.Pickler.__init__
4307
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004308 file: object
4309 protocol: object = NULL
4310 fix_imports: bool = True
4311
4312This takes a binary file for writing a pickle data stream.
4313
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004314The optional *protocol* argument tells the pickler to use the given
4315protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4316protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004317
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004318Specifying a negative protocol version selects the highest protocol
4319version supported. The higher the protocol used, the more recent the
4320version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004321
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004322The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004323bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004324writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004325this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004326
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004327If *fix_imports* is True and protocol is less than 3, pickle will try
4328to map the new Python 3 names to the old module names used in Python
43292, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004330[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004331
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004332static int
Larry Hastings89964c42015-04-14 18:07:59 -04004333_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4334 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004335/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004336{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004337 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004338 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004339
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004340 /* In case of multiple __init__() calls, clear previous content. */
4341 if (self->write != NULL)
4342 (void)Pickler_clear(self);
4343
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004344 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004345 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004346
4347 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004348 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004349
4350 /* memo and output_buffer may have already been created in _Pickler_New */
4351 if (self->memo == NULL) {
4352 self->memo = PyMemoTable_New();
4353 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004354 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004355 }
4356 self->output_len = 0;
4357 if (self->output_buffer == NULL) {
4358 self->max_output_len = WRITE_BUF_SIZE;
4359 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4360 self->max_output_len);
4361 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004362 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004363 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004364
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004365 self->fast = 0;
4366 self->fast_nesting = 0;
4367 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004368
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004369 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4370 &self->pers_func, &self->pers_func_self) < 0)
4371 {
4372 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004373 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004374
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004375 if (_PyObject_LookupAttrId((PyObject *)self,
4376 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4377 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004378 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004379
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004380 return 0;
4381}
4382
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004383
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004384/* Define a proxy object for the Pickler's internal memo object. This is to
4385 * avoid breaking code like:
4386 * pickler.memo.clear()
4387 * and
4388 * pickler.memo = saved_memo
4389 * Is this a good idea? Not really, but we don't want to break code that uses
4390 * it. Note that we don't implement the entire mapping API here. This is
4391 * intentional, as these should be treated as black-box implementation details.
4392 */
4393
Larry Hastings61272b72014-01-07 12:41:53 -08004394/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004395_pickle.PicklerMemoProxy.clear
4396
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004397Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004398[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004399
Larry Hastings3cceb382014-01-04 11:09:09 -08004400static PyObject *
4401_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004402/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004403{
4404 if (self->pickler->memo)
4405 PyMemoTable_Clear(self->pickler->memo);
4406 Py_RETURN_NONE;
4407}
4408
Larry Hastings61272b72014-01-07 12:41:53 -08004409/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004410_pickle.PicklerMemoProxy.copy
4411
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004412Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004413[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004414
Larry Hastings3cceb382014-01-04 11:09:09 -08004415static PyObject *
4416_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004417/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004418{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004419 PyMemoTable *memo;
4420 PyObject *new_memo = PyDict_New();
4421 if (new_memo == NULL)
4422 return NULL;
4423
4424 memo = self->pickler->memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07004425 for (size_t i = 0; i < memo->mt_allocated; ++i) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004426 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 Storchaka34fd4c22018-11-05 16:20:25 +02004615 "'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 */
Victor Stinnerb37672d2018-11-22 03:37:50 +01005861 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
5862 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005863 }
Victor Stinnerb37672d2018-11-22 03:37:50 +01005864
5865 module_name = PyTuple_GET_ITEM(pair, 0);
5866 if (!PyUnicode_Check(module_name)) {
5867 goto error;
5868 }
5869
5870 class_name = PyTuple_GET_ITEM(pair, 1);
5871 if (!PyUnicode_Check(class_name)) {
5872 goto error;
5873 }
5874
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005875 /* Load the object. */
5876 obj = find_class(self, module_name, class_name);
5877 if (obj == NULL) {
5878 Py_DECREF(py_code);
5879 return -1;
5880 }
5881 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005882 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005883 Py_DECREF(py_code);
5884 if (code < 0) {
5885 Py_DECREF(obj);
5886 return -1;
5887 }
5888 PDATA_PUSH(self->stack, obj, -1);
5889 return 0;
Victor Stinnerb37672d2018-11-22 03:37:50 +01005890
5891error:
5892 Py_DECREF(py_code);
5893 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5894 "isn't a 2-tuple of strings", code);
5895 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005896}
5897
5898static int
5899load_put(UnpicklerObject *self)
5900{
5901 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005902 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005903 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005904 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005905
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005906 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005907 return -1;
5908 if (len < 2)
5909 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005910 if (Py_SIZE(self->stack) <= self->stack->fence)
5911 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005912 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005913
5914 key = PyLong_FromString(s, NULL, 10);
5915 if (key == NULL)
5916 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005917 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005918 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005919 if (idx < 0) {
5920 if (!PyErr_Occurred())
5921 PyErr_SetString(PyExc_ValueError,
5922 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005923 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005924 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005925
5926 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005927}
5928
5929static int
5930load_binput(UnpicklerObject *self)
5931{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005932 PyObject *value;
5933 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005934 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005935
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005936 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005937 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005938
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005939 if (Py_SIZE(self->stack) <= self->stack->fence)
5940 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005941 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005942
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005943 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005944
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005945 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005946}
5947
5948static int
5949load_long_binput(UnpicklerObject *self)
5950{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005951 PyObject *value;
5952 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005953 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005954
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005955 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005956 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005957
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005958 if (Py_SIZE(self->stack) <= self->stack->fence)
5959 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005960 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005961
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005962 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005963 if (idx < 0) {
5964 PyErr_SetString(PyExc_ValueError,
5965 "negative LONG_BINPUT argument");
5966 return -1;
5967 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005968
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005969 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005970}
5971
5972static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005973load_memoize(UnpicklerObject *self)
5974{
5975 PyObject *value;
5976
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005977 if (Py_SIZE(self->stack) <= self->stack->fence)
5978 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005979 value = self->stack->data[Py_SIZE(self->stack) - 1];
5980
5981 return _Unpickler_MemoPut(self, self->memo_len, value);
5982}
5983
5984static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005985do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005986{
5987 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005988 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005989 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005990 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005991 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005992
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005993 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005994 if (x > len || x <= self->stack->fence)
5995 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005996 if (len == x) /* nothing to do */
5997 return 0;
5998
5999 list = self->stack->data[x - 1];
6000
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006001 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006002 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006003 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006004
6005 slice = Pdata_poplist(self->stack, x);
6006 if (!slice)
6007 return -1;
6008 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006009 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006010 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006011 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006012 }
6013 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006014 PyObject *extend_func;
6015 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006016
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006017 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6018 if (extend_func != NULL) {
6019 slice = Pdata_poplist(self->stack, x);
6020 if (!slice) {
6021 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006022 return -1;
6023 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006024 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006025 Py_DECREF(extend_func);
6026 if (result == NULL)
6027 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006028 Py_DECREF(result);
6029 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006030 else {
6031 PyObject *append_func;
6032 _Py_IDENTIFIER(append);
6033
6034 /* Even if the PEP 307 requires extend() and append() methods,
6035 fall back on append() if the object has no extend() method
6036 for backward compatibility. */
6037 PyErr_Clear();
6038 append_func = _PyObject_GetAttrId(list, &PyId_append);
6039 if (append_func == NULL)
6040 return -1;
6041 for (i = x; i < len; i++) {
6042 value = self->stack->data[i];
6043 result = _Pickle_FastCall(append_func, value);
6044 if (result == NULL) {
6045 Pdata_clear(self->stack, i + 1);
6046 Py_SIZE(self->stack) = x;
6047 Py_DECREF(append_func);
6048 return -1;
6049 }
6050 Py_DECREF(result);
6051 }
6052 Py_SIZE(self->stack) = x;
6053 Py_DECREF(append_func);
6054 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006055 }
6056
6057 return 0;
6058}
6059
6060static int
6061load_append(UnpicklerObject *self)
6062{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006063 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6064 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006065 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006066}
6067
6068static int
6069load_appends(UnpicklerObject *self)
6070{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006071 Py_ssize_t i = marker(self);
6072 if (i < 0)
6073 return -1;
6074 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075}
6076
6077static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006078do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079{
6080 PyObject *value, *key;
6081 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006082 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006083 int status = 0;
6084
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006085 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006086 if (x > len || x <= self->stack->fence)
6087 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006088 if (len == x) /* nothing to do */
6089 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006090 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006091 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006092 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006093 PyErr_SetString(st->UnpicklingError,
6094 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006095 return -1;
6096 }
6097
6098 /* Here, dict does not actually need to be a PyDict; it could be anything
6099 that supports the __setitem__ attribute. */
6100 dict = self->stack->data[x - 1];
6101
6102 for (i = x + 1; i < len; i += 2) {
6103 key = self->stack->data[i - 1];
6104 value = self->stack->data[i];
6105 if (PyObject_SetItem(dict, key, value) < 0) {
6106 status = -1;
6107 break;
6108 }
6109 }
6110
6111 Pdata_clear(self->stack, x);
6112 return status;
6113}
6114
6115static int
6116load_setitem(UnpicklerObject *self)
6117{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006118 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006119}
6120
6121static int
6122load_setitems(UnpicklerObject *self)
6123{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006124 Py_ssize_t i = marker(self);
6125 if (i < 0)
6126 return -1;
6127 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006128}
6129
6130static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006131load_additems(UnpicklerObject *self)
6132{
6133 PyObject *set;
6134 Py_ssize_t mark, len, i;
6135
6136 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006137 if (mark < 0)
6138 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006139 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006140 if (mark > len || mark <= self->stack->fence)
6141 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006142 if (len == mark) /* nothing to do */
6143 return 0;
6144
6145 set = self->stack->data[mark - 1];
6146
6147 if (PySet_Check(set)) {
6148 PyObject *items;
6149 int status;
6150
6151 items = Pdata_poptuple(self->stack, mark);
6152 if (items == NULL)
6153 return -1;
6154
6155 status = _PySet_Update(set, items);
6156 Py_DECREF(items);
6157 return status;
6158 }
6159 else {
6160 PyObject *add_func;
6161 _Py_IDENTIFIER(add);
6162
6163 add_func = _PyObject_GetAttrId(set, &PyId_add);
6164 if (add_func == NULL)
6165 return -1;
6166 for (i = mark; i < len; i++) {
6167 PyObject *result;
6168 PyObject *item;
6169
6170 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006171 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006172 if (result == NULL) {
6173 Pdata_clear(self->stack, i + 1);
6174 Py_SIZE(self->stack) = mark;
6175 return -1;
6176 }
6177 Py_DECREF(result);
6178 }
6179 Py_SIZE(self->stack) = mark;
6180 }
6181
6182 return 0;
6183}
6184
6185static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006186load_build(UnpicklerObject *self)
6187{
6188 PyObject *state, *inst, *slotstate;
6189 PyObject *setstate;
6190 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006191 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006192
6193 /* Stack is ... instance, state. We want to leave instance at
6194 * the stack top, possibly mutated via instance.__setstate__(state).
6195 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006196 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6197 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006198
6199 PDATA_POP(self->stack, state);
6200 if (state == NULL)
6201 return -1;
6202
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006203 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006204
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006205 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6206 Py_DECREF(state);
6207 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006208 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006209 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006210 PyObject *result;
6211
6212 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006213 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006214 Py_DECREF(setstate);
6215 if (result == NULL)
6216 return -1;
6217 Py_DECREF(result);
6218 return 0;
6219 }
6220
6221 /* A default __setstate__. First see whether state embeds a
6222 * slot state dict too (a proto 2 addition).
6223 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006224 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006225 PyObject *tmp = state;
6226
6227 state = PyTuple_GET_ITEM(tmp, 0);
6228 slotstate = PyTuple_GET_ITEM(tmp, 1);
6229 Py_INCREF(state);
6230 Py_INCREF(slotstate);
6231 Py_DECREF(tmp);
6232 }
6233 else
6234 slotstate = NULL;
6235
6236 /* Set inst.__dict__ from the state dict (if any). */
6237 if (state != Py_None) {
6238 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006239 PyObject *d_key, *d_value;
6240 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006241 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006242
6243 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006244 PickleState *st = _Pickle_GetGlobalState();
6245 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006246 goto error;
6247 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006248 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006249 if (dict == NULL)
6250 goto error;
6251
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006252 i = 0;
6253 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6254 /* normally the keys for instance attributes are
6255 interned. we should try to do that here. */
6256 Py_INCREF(d_key);
6257 if (PyUnicode_CheckExact(d_key))
6258 PyUnicode_InternInPlace(&d_key);
6259 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6260 Py_DECREF(d_key);
6261 goto error;
6262 }
6263 Py_DECREF(d_key);
6264 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006265 Py_DECREF(dict);
6266 }
6267
6268 /* Also set instance attributes from the slotstate dict (if any). */
6269 if (slotstate != NULL) {
6270 PyObject *d_key, *d_value;
6271 Py_ssize_t i;
6272
6273 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006274 PickleState *st = _Pickle_GetGlobalState();
6275 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006276 "slot state is not a dictionary");
6277 goto error;
6278 }
6279 i = 0;
6280 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6281 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6282 goto error;
6283 }
6284 }
6285
6286 if (0) {
6287 error:
6288 status = -1;
6289 }
6290
6291 Py_DECREF(state);
6292 Py_XDECREF(slotstate);
6293 return status;
6294}
6295
6296static int
6297load_mark(UnpicklerObject *self)
6298{
6299
6300 /* Note that we split the (pickle.py) stack into two stacks, an
6301 * object stack and a mark stack. Here we push a mark onto the
6302 * mark stack.
6303 */
6304
Sergey Fedoseev86b89912018-08-25 12:54:40 +05006305 if (self->num_marks >= self->marks_size) {
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006306 size_t alloc = ((size_t)self->num_marks << 1) + 20;
6307 Py_ssize_t *marks_new = self->marks;
6308 PyMem_RESIZE(marks_new, Py_ssize_t, alloc);
6309 if (marks_new == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006310 PyErr_NoMemory();
6311 return -1;
6312 }
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006313 self->marks = marks_new;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006314 self->marks_size = (Py_ssize_t)alloc;
6315 }
6316
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006317 self->stack->mark_set = 1;
6318 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006319
6320 return 0;
6321}
6322
6323static int
6324load_reduce(UnpicklerObject *self)
6325{
6326 PyObject *callable = NULL;
6327 PyObject *argtup = NULL;
6328 PyObject *obj = NULL;
6329
6330 PDATA_POP(self->stack, argtup);
6331 if (argtup == NULL)
6332 return -1;
6333 PDATA_POP(self->stack, callable);
6334 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006335 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006336 Py_DECREF(callable);
6337 }
6338 Py_DECREF(argtup);
6339
6340 if (obj == NULL)
6341 return -1;
6342
6343 PDATA_PUSH(self->stack, obj, -1);
6344 return 0;
6345}
6346
6347/* Just raises an error if we don't know the protocol specified. PROTO
6348 * is the first opcode for protocols >= 2.
6349 */
6350static int
6351load_proto(UnpicklerObject *self)
6352{
6353 char *s;
6354 int i;
6355
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006356 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006357 return -1;
6358
6359 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006360 if (i <= HIGHEST_PROTOCOL) {
6361 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006362 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006363 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006364
6365 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6366 return -1;
6367}
6368
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006369static int
6370load_frame(UnpicklerObject *self)
6371{
6372 char *s;
6373 Py_ssize_t frame_len;
6374
6375 if (_Unpickler_Read(self, &s, 8) < 0)
6376 return -1;
6377
6378 frame_len = calc_binsize(s, 8);
6379 if (frame_len < 0) {
6380 PyErr_Format(PyExc_OverflowError,
6381 "FRAME length exceeds system's maximum of %zd bytes",
6382 PY_SSIZE_T_MAX);
6383 return -1;
6384 }
6385
6386 if (_Unpickler_Read(self, &s, frame_len) < 0)
6387 return -1;
6388
6389 /* Rewind to start of frame */
6390 self->next_read_idx -= frame_len;
6391 return 0;
6392}
6393
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006394static PyObject *
6395load(UnpicklerObject *self)
6396{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006397 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006398 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006399
6400 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006401 self->stack->mark_set = 0;
6402 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006403 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006404 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006405 Pdata_clear(self->stack, 0);
6406
6407 /* Convenient macros for the dispatch while-switch loop just below. */
6408#define OP(opcode, load_func) \
6409 case opcode: if (load_func(self) < 0) break; continue;
6410
6411#define OP_ARG(opcode, load_func, arg) \
6412 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6413
6414 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006415 if (_Unpickler_Read(self, &s, 1) < 0) {
6416 PickleState *st = _Pickle_GetGlobalState();
6417 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6418 PyErr_Format(PyExc_EOFError, "Ran out of input");
6419 }
6420 return NULL;
6421 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006422
6423 switch ((enum opcode)s[0]) {
6424 OP(NONE, load_none)
6425 OP(BININT, load_binint)
6426 OP(BININT1, load_binint1)
6427 OP(BININT2, load_binint2)
6428 OP(INT, load_int)
6429 OP(LONG, load_long)
6430 OP_ARG(LONG1, load_counted_long, 1)
6431 OP_ARG(LONG4, load_counted_long, 4)
6432 OP(FLOAT, load_float)
6433 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006434 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6435 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6436 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6437 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6438 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006439 OP(STRING, load_string)
6440 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006441 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6442 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6443 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006444 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6445 OP_ARG(TUPLE1, load_counted_tuple, 1)
6446 OP_ARG(TUPLE2, load_counted_tuple, 2)
6447 OP_ARG(TUPLE3, load_counted_tuple, 3)
6448 OP(TUPLE, load_tuple)
6449 OP(EMPTY_LIST, load_empty_list)
6450 OP(LIST, load_list)
6451 OP(EMPTY_DICT, load_empty_dict)
6452 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006453 OP(EMPTY_SET, load_empty_set)
6454 OP(ADDITEMS, load_additems)
6455 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006456 OP(OBJ, load_obj)
6457 OP(INST, load_inst)
6458 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006459 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006460 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006461 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006462 OP(APPEND, load_append)
6463 OP(APPENDS, load_appends)
6464 OP(BUILD, load_build)
6465 OP(DUP, load_dup)
6466 OP(BINGET, load_binget)
6467 OP(LONG_BINGET, load_long_binget)
6468 OP(GET, load_get)
6469 OP(MARK, load_mark)
6470 OP(BINPUT, load_binput)
6471 OP(LONG_BINPUT, load_long_binput)
6472 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006473 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006474 OP(POP, load_pop)
6475 OP(POP_MARK, load_pop_mark)
6476 OP(SETITEM, load_setitem)
6477 OP(SETITEMS, load_setitems)
6478 OP(PERSID, load_persid)
6479 OP(BINPERSID, load_binpersid)
6480 OP(REDUCE, load_reduce)
6481 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006482 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006483 OP_ARG(EXT1, load_extension, 1)
6484 OP_ARG(EXT2, load_extension, 2)
6485 OP_ARG(EXT4, load_extension, 4)
6486 OP_ARG(NEWTRUE, load_bool, Py_True)
6487 OP_ARG(NEWFALSE, load_bool, Py_False)
6488
6489 case STOP:
6490 break;
6491
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006492 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006493 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006494 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006495 unsigned char c = (unsigned char) *s;
6496 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6497 PyErr_Format(st->UnpicklingError,
6498 "invalid load key, '%c'.", c);
6499 }
6500 else {
6501 PyErr_Format(st->UnpicklingError,
6502 "invalid load key, '\\x%02x'.", c);
6503 }
6504 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006505 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006506 }
6507
6508 break; /* and we are done! */
6509 }
6510
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006511 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006512 return NULL;
6513 }
6514
Victor Stinner2ae57e32013-10-31 13:39:23 +01006515 if (_Unpickler_SkipConsumed(self) < 0)
6516 return NULL;
6517
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006518 PDATA_POP(self->stack, value);
6519 return value;
6520}
6521
Larry Hastings61272b72014-01-07 12:41:53 -08006522/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006523
6524_pickle.Unpickler.load
6525
6526Load a pickle.
6527
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006528Read a pickled object representation from the open file object given
6529in the constructor, and return the reconstituted object hierarchy
6530specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006531[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006532
Larry Hastings3cceb382014-01-04 11:09:09 -08006533static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006534_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006535/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006536{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006537 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006538
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539 /* Check whether the Unpickler was initialized correctly. This prevents
6540 segfaulting if a subclass overridden __init__ with a function that does
6541 not call Unpickler.__init__(). Here, we simply ensure that self->read
6542 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006543 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006544 PickleState *st = _Pickle_GetGlobalState();
6545 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006547 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006548 return NULL;
6549 }
6550
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006551 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006552}
6553
6554/* The name of find_class() is misleading. In newer pickle protocols, this
6555 function is used for loading any global (i.e., functions), not just
6556 classes. The name is kept only for backward compatibility. */
6557
Larry Hastings61272b72014-01-07 12:41:53 -08006558/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006559
6560_pickle.Unpickler.find_class
6561
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006562 module_name: object
6563 global_name: object
6564 /
6565
6566Return an object from a specified module.
6567
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006568If necessary, the module will be imported. Subclasses may override
6569this method (e.g. to restrict unpickling of arbitrary classes and
6570functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006571
6572This method is called whenever a class or a function object is
6573needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006574[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006575
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006576static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006577_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6578 PyObject *module_name,
6579 PyObject *global_name)
6580/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006581{
6582 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006583 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006584
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006585 /* Try to map the old names used in Python 2.x to the new ones used in
6586 Python 3.x. We do this only with old pickle protocols and when the
6587 user has not disabled the feature. */
6588 if (self->proto < 3 && self->fix_imports) {
6589 PyObject *key;
6590 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006591 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006592
6593 /* Check if the global (i.e., a function or a class) was renamed
6594 or moved to another module. */
6595 key = PyTuple_Pack(2, module_name, global_name);
6596 if (key == NULL)
6597 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006598 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006599 Py_DECREF(key);
6600 if (item) {
6601 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6602 PyErr_Format(PyExc_RuntimeError,
6603 "_compat_pickle.NAME_MAPPING values should be "
6604 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6605 return NULL;
6606 }
6607 module_name = PyTuple_GET_ITEM(item, 0);
6608 global_name = PyTuple_GET_ITEM(item, 1);
6609 if (!PyUnicode_Check(module_name) ||
6610 !PyUnicode_Check(global_name)) {
6611 PyErr_Format(PyExc_RuntimeError,
6612 "_compat_pickle.NAME_MAPPING values should be "
6613 "pairs of str, not (%.200s, %.200s)",
6614 Py_TYPE(module_name)->tp_name,
6615 Py_TYPE(global_name)->tp_name);
6616 return NULL;
6617 }
6618 }
6619 else if (PyErr_Occurred()) {
6620 return NULL;
6621 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006622 else {
6623 /* Check if the module was renamed. */
6624 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6625 if (item) {
6626 if (!PyUnicode_Check(item)) {
6627 PyErr_Format(PyExc_RuntimeError,
6628 "_compat_pickle.IMPORT_MAPPING values should be "
6629 "strings, not %.200s", Py_TYPE(item)->tp_name);
6630 return NULL;
6631 }
6632 module_name = item;
6633 }
6634 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006635 return NULL;
6636 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006637 }
6638 }
6639
Eric Snow3f9eee62017-09-15 16:35:20 -06006640 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006641 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006642 if (PyErr_Occurred())
6643 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006644 module = PyImport_Import(module_name);
6645 if (module == NULL)
6646 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006647 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006648 global = getattribute(module, global_name, self->proto >= 4);
6649 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650 return global;
6651}
6652
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006653/*[clinic input]
6654
6655_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6656
6657Returns size in memory, in bytes.
6658[clinic start generated code]*/
6659
6660static Py_ssize_t
6661_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6662/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6663{
6664 Py_ssize_t res;
6665
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006666 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006667 if (self->memo != NULL)
6668 res += self->memo_size * sizeof(PyObject *);
6669 if (self->marks != NULL)
6670 res += self->marks_size * sizeof(Py_ssize_t);
6671 if (self->input_line != NULL)
6672 res += strlen(self->input_line) + 1;
6673 if (self->encoding != NULL)
6674 res += strlen(self->encoding) + 1;
6675 if (self->errors != NULL)
6676 res += strlen(self->errors) + 1;
6677 return res;
6678}
6679
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006680static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006681 _PICKLE_UNPICKLER_LOAD_METHODDEF
6682 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006683 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006684 {NULL, NULL} /* sentinel */
6685};
6686
6687static void
6688Unpickler_dealloc(UnpicklerObject *self)
6689{
6690 PyObject_GC_UnTrack((PyObject *)self);
6691 Py_XDECREF(self->readline);
6692 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006693 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006694 Py_XDECREF(self->stack);
6695 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006696 if (self->buffer.buf != NULL) {
6697 PyBuffer_Release(&self->buffer);
6698 self->buffer.buf = NULL;
6699 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006700
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006701 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006702 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006703 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006704 PyMem_Free(self->encoding);
6705 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006706
6707 Py_TYPE(self)->tp_free((PyObject *)self);
6708}
6709
6710static int
6711Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6712{
6713 Py_VISIT(self->readline);
6714 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006715 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006716 Py_VISIT(self->stack);
6717 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006718 return 0;
6719}
6720
6721static int
6722Unpickler_clear(UnpicklerObject *self)
6723{
6724 Py_CLEAR(self->readline);
6725 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006726 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006727 Py_CLEAR(self->stack);
6728 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006729 if (self->buffer.buf != NULL) {
6730 PyBuffer_Release(&self->buffer);
6731 self->buffer.buf = NULL;
6732 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006733
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006734 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006735 PyMem_Free(self->marks);
6736 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006737 PyMem_Free(self->input_line);
6738 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006739 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006740 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006741 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006742 self->errors = NULL;
6743
6744 return 0;
6745}
6746
Larry Hastings61272b72014-01-07 12:41:53 -08006747/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006748
6749_pickle.Unpickler.__init__
6750
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006751 file: object
6752 *
6753 fix_imports: bool = True
6754 encoding: str = 'ASCII'
6755 errors: str = 'strict'
6756
6757This takes a binary file for reading a pickle data stream.
6758
6759The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006760protocol argument is needed. Bytes past the pickled object's
6761representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006762
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006763The argument *file* must have two methods, a read() method that takes
6764an integer argument, and a readline() method that requires no
6765arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006766binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006767other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006768
6769Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006770which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006771generated by Python 2. If *fix_imports* is True, pickle will try to
6772map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006773*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006774instances pickled by Python 2; these default to 'ASCII' and 'strict',
6775respectively. The *encoding* can be 'bytes' to read these 8-bit
6776string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006777[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006778
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006779static int
Larry Hastings89964c42015-04-14 18:07:59 -04006780_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6781 int fix_imports, const char *encoding,
6782 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006783/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006784{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006785 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006786
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006787 /* In case of multiple __init__() calls, clear previous content. */
6788 if (self->read != NULL)
6789 (void)Unpickler_clear(self);
6790
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006791 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006792 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006794 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006795 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006796
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006797 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006798
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006799 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6800 &self->pers_func, &self->pers_func_self) < 0)
6801 {
6802 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006803 }
6804
6805 self->stack = (Pdata *)Pdata_New();
6806 if (self->stack == NULL)
Zackery Spytz4b430e52018-09-28 23:48:46 -06006807 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006808
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006809 self->memo_size = 32;
6810 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006811 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006812 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006813
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006814 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006815
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006816 return 0;
6817}
6818
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006819
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006820/* Define a proxy object for the Unpickler's internal memo object. This is to
6821 * avoid breaking code like:
6822 * unpickler.memo.clear()
6823 * and
6824 * unpickler.memo = saved_memo
6825 * Is this a good idea? Not really, but we don't want to break code that uses
6826 * it. Note that we don't implement the entire mapping API here. This is
6827 * intentional, as these should be treated as black-box implementation details.
6828 *
6829 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006830 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006831 */
6832
Larry Hastings61272b72014-01-07 12:41:53 -08006833/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006834_pickle.UnpicklerMemoProxy.clear
6835
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006836Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006837[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006838
Larry Hastings3cceb382014-01-04 11:09:09 -08006839static PyObject *
6840_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006841/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006842{
6843 _Unpickler_MemoCleanup(self->unpickler);
6844 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6845 if (self->unpickler->memo == NULL)
6846 return NULL;
6847 Py_RETURN_NONE;
6848}
6849
Larry Hastings61272b72014-01-07 12:41:53 -08006850/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006851_pickle.UnpicklerMemoProxy.copy
6852
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006853Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006854[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006855
Larry Hastings3cceb382014-01-04 11:09:09 -08006856static PyObject *
6857_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006858/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006859{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07006860 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006861 PyObject *new_memo = PyDict_New();
6862 if (new_memo == NULL)
6863 return NULL;
6864
6865 for (i = 0; i < self->unpickler->memo_size; i++) {
6866 int status;
6867 PyObject *key, *value;
6868
6869 value = self->unpickler->memo[i];
6870 if (value == NULL)
6871 continue;
6872
6873 key = PyLong_FromSsize_t(i);
6874 if (key == NULL)
6875 goto error;
6876 status = PyDict_SetItem(new_memo, key, value);
6877 Py_DECREF(key);
6878 if (status < 0)
6879 goto error;
6880 }
6881 return new_memo;
6882
6883error:
6884 Py_DECREF(new_memo);
6885 return NULL;
6886}
6887
Larry Hastings61272b72014-01-07 12:41:53 -08006888/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006889_pickle.UnpicklerMemoProxy.__reduce__
6890
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006891Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006892[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006893
Larry Hastings3cceb382014-01-04 11:09:09 -08006894static PyObject *
6895_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006896/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006897{
6898 PyObject *reduce_value;
6899 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006900 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006901 if (contents == NULL)
6902 return NULL;
6903
6904 reduce_value = PyTuple_New(2);
6905 if (reduce_value == NULL) {
6906 Py_DECREF(contents);
6907 return NULL;
6908 }
6909 constructor_args = PyTuple_New(1);
6910 if (constructor_args == NULL) {
6911 Py_DECREF(contents);
6912 Py_DECREF(reduce_value);
6913 return NULL;
6914 }
6915 PyTuple_SET_ITEM(constructor_args, 0, contents);
6916 Py_INCREF((PyObject *)&PyDict_Type);
6917 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6918 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6919 return reduce_value;
6920}
6921
6922static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006923 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6924 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6925 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006926 {NULL, NULL} /* sentinel */
6927};
6928
6929static void
6930UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6931{
6932 PyObject_GC_UnTrack(self);
6933 Py_XDECREF(self->unpickler);
6934 PyObject_GC_Del((PyObject *)self);
6935}
6936
6937static int
6938UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6939 visitproc visit, void *arg)
6940{
6941 Py_VISIT(self->unpickler);
6942 return 0;
6943}
6944
6945static int
6946UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6947{
6948 Py_CLEAR(self->unpickler);
6949 return 0;
6950}
6951
6952static PyTypeObject UnpicklerMemoProxyType = {
6953 PyVarObject_HEAD_INIT(NULL, 0)
6954 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6955 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6956 0,
6957 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6958 0, /* tp_print */
6959 0, /* tp_getattr */
6960 0, /* tp_setattr */
6961 0, /* tp_compare */
6962 0, /* tp_repr */
6963 0, /* tp_as_number */
6964 0, /* tp_as_sequence */
6965 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006966 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006967 0, /* tp_call */
6968 0, /* tp_str */
6969 PyObject_GenericGetAttr, /* tp_getattro */
6970 PyObject_GenericSetAttr, /* tp_setattro */
6971 0, /* tp_as_buffer */
6972 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6973 0, /* tp_doc */
6974 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6975 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6976 0, /* tp_richcompare */
6977 0, /* tp_weaklistoffset */
6978 0, /* tp_iter */
6979 0, /* tp_iternext */
6980 unpicklerproxy_methods, /* tp_methods */
6981};
6982
6983static PyObject *
6984UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6985{
6986 UnpicklerMemoProxyObject *self;
6987
6988 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6989 &UnpicklerMemoProxyType);
6990 if (self == NULL)
6991 return NULL;
6992 Py_INCREF(unpickler);
6993 self->unpickler = unpickler;
6994 PyObject_GC_Track(self);
6995 return (PyObject *)self;
6996}
6997
6998/*****************************************************************************/
6999
7000
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007001static PyObject *
7002Unpickler_get_memo(UnpicklerObject *self)
7003{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007004 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007005}
7006
7007static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007008Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007009{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007010 PyObject **new_memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007011 size_t new_memo_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007012
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007013 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007014 PyErr_SetString(PyExc_TypeError,
7015 "attribute deletion is not supported");
7016 return -1;
7017 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007018
7019 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7020 UnpicklerObject *unpickler =
7021 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7022
7023 new_memo_size = unpickler->memo_size;
7024 new_memo = _Unpickler_NewMemo(new_memo_size);
7025 if (new_memo == NULL)
7026 return -1;
7027
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007028 for (size_t i = 0; i < new_memo_size; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007029 Py_XINCREF(unpickler->memo[i]);
7030 new_memo[i] = unpickler->memo[i];
7031 }
7032 }
7033 else if (PyDict_Check(obj)) {
7034 Py_ssize_t i = 0;
7035 PyObject *key, *value;
7036
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007037 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007038 new_memo = _Unpickler_NewMemo(new_memo_size);
7039 if (new_memo == NULL)
7040 return -1;
7041
7042 while (PyDict_Next(obj, &i, &key, &value)) {
7043 Py_ssize_t idx;
7044 if (!PyLong_Check(key)) {
7045 PyErr_SetString(PyExc_TypeError,
7046 "memo key must be integers");
7047 goto error;
7048 }
7049 idx = PyLong_AsSsize_t(key);
7050 if (idx == -1 && PyErr_Occurred())
7051 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007052 if (idx < 0) {
7053 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007054 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007055 goto error;
7056 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007057 if (_Unpickler_MemoPut(self, idx, value) < 0)
7058 goto error;
7059 }
7060 }
7061 else {
7062 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +02007063 "'memo' attribute must be an UnpicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007064 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007065 return -1;
7066 }
7067
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007068 _Unpickler_MemoCleanup(self);
7069 self->memo_size = new_memo_size;
7070 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007071
7072 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007073
7074 error:
7075 if (new_memo_size) {
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007076 for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007077 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
Łukasz Langac51d8c92018-04-03 23:06:53 -07007182protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7183protocol is 4. It was introduced in Python 3.4, it is incompatible
7184with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007185
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007186Specifying a negative protocol version selects the highest protocol
7187version supported. The higher the protocol used, the more recent the
7188version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007189
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007190The *file* argument must have a write() method that accepts a single
7191bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007192writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007193this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007194
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007195If *fix_imports* is True and protocol is less than 3, pickle will try
7196to map the new Python 3 names to the old module names used in Python
71972, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007198[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007199
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007200static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007201_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007202 PyObject *protocol, int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007203/*[clinic end generated code: output=a4774d5fde7d34de input=93f1408489a87472]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007204{
7205 PicklerObject *pickler = _Pickler_New();
7206
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007207 if (pickler == NULL)
7208 return NULL;
7209
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007210 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007211 goto error;
7212
7213 if (_Pickler_SetOutputStream(pickler, file) < 0)
7214 goto error;
7215
7216 if (dump(pickler, obj) < 0)
7217 goto error;
7218
7219 if (_Pickler_FlushToFile(pickler) < 0)
7220 goto error;
7221
7222 Py_DECREF(pickler);
7223 Py_RETURN_NONE;
7224
7225 error:
7226 Py_XDECREF(pickler);
7227 return NULL;
7228}
7229
Larry Hastings61272b72014-01-07 12:41:53 -08007230/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007231
7232_pickle.dumps
7233
7234 obj: object
7235 protocol: object = NULL
7236 *
7237 fix_imports: bool = True
7238
7239Return the pickled representation of the object as a bytes object.
7240
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007241The optional *protocol* argument tells the pickler to use the given
7242protocol; supported protocols are 0, 1, 2, 3 and 4. The default
Łukasz Langac51d8c92018-04-03 23:06:53 -07007243protocol is 4. It was introduced in Python 3.4, it is incompatible
7244with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007245
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007246Specifying a negative protocol version selects the highest protocol
7247version supported. The higher the protocol used, the more recent the
7248version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007249
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007250If *fix_imports* is True and *protocol* is less than 3, pickle will
7251try to map the new Python 3 names to the old module names used in
7252Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007253[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007254
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007255static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007256_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007257 int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007258/*[clinic end generated code: output=d75d5cda456fd261 input=b6efb45a7d19b5ab]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007259{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007260 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007261 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007262
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007263 if (pickler == NULL)
7264 return NULL;
7265
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007266 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007267 goto error;
7268
7269 if (dump(pickler, obj) < 0)
7270 goto error;
7271
7272 result = _Pickler_GetString(pickler);
7273 Py_DECREF(pickler);
7274 return result;
7275
7276 error:
7277 Py_XDECREF(pickler);
7278 return NULL;
7279}
7280
Larry Hastings61272b72014-01-07 12:41:53 -08007281/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007282
7283_pickle.load
7284
7285 file: object
7286 *
7287 fix_imports: bool = True
7288 encoding: str = 'ASCII'
7289 errors: str = 'strict'
7290
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007291Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007292
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007293This is equivalent to ``Unpickler(file).load()``, but may be more
7294efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007295
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007296The protocol version of the pickle is detected automatically, so no
7297protocol argument is needed. Bytes past the pickled object's
7298representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007299
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007300The argument *file* must have two methods, a read() method that takes
7301an integer argument, and a readline() method that requires no
7302arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007303binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007304other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007305
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007306Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007307which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007308generated by Python 2. If *fix_imports* is True, pickle will try to
7309map the old Python 2 names to the new names used in Python 3. The
7310*encoding* and *errors* tell pickle how to decode 8-bit string
7311instances pickled by Python 2; these default to 'ASCII' and 'strict',
7312respectively. The *encoding* can be 'bytes' to read these 8-bit
7313string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007314[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007315
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007316static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007317_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007318 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007319/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007320{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007321 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007322 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007323
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007324 if (unpickler == NULL)
7325 return NULL;
7326
7327 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7328 goto error;
7329
7330 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7331 goto error;
7332
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007333 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007334
7335 result = load(unpickler);
7336 Py_DECREF(unpickler);
7337 return result;
7338
7339 error:
7340 Py_XDECREF(unpickler);
7341 return NULL;
7342}
7343
Larry Hastings61272b72014-01-07 12:41:53 -08007344/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007345
7346_pickle.loads
7347
7348 data: object
7349 *
7350 fix_imports: bool = True
7351 encoding: str = 'ASCII'
7352 errors: str = 'strict'
7353
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007354Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007355
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007356The protocol version of the pickle is detected automatically, so no
7357protocol argument is needed. Bytes past the pickled object's
7358representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007359
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007360Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007361which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007362generated by Python 2. If *fix_imports* is True, pickle will try to
7363map the old Python 2 names to the new names used in Python 3. The
7364*encoding* and *errors* tell pickle how to decode 8-bit string
7365instances pickled by Python 2; these default to 'ASCII' and 'strict',
7366respectively. The *encoding* can be 'bytes' to read these 8-bit
7367string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007368[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007369
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007370static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007371_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007372 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007373/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007374{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007375 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007376 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007377
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007378 if (unpickler == NULL)
7379 return NULL;
7380
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007381 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007382 goto error;
7383
7384 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7385 goto error;
7386
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007387 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007388
7389 result = load(unpickler);
7390 Py_DECREF(unpickler);
7391 return result;
7392
7393 error:
7394 Py_XDECREF(unpickler);
7395 return NULL;
7396}
7397
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007398static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007399 _PICKLE_DUMP_METHODDEF
7400 _PICKLE_DUMPS_METHODDEF
7401 _PICKLE_LOAD_METHODDEF
7402 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007403 {NULL, NULL} /* sentinel */
7404};
7405
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007406static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007407pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007408{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007409 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007410 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007411}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007412
Stefan Krahf483b0f2013-12-14 13:43:10 +01007413static void
7414pickle_free(PyObject *m)
7415{
7416 _Pickle_ClearState(_Pickle_GetState(m));
7417}
7418
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007419static int
7420pickle_traverse(PyObject *m, visitproc visit, void *arg)
7421{
7422 PickleState *st = _Pickle_GetState(m);
7423 Py_VISIT(st->PickleError);
7424 Py_VISIT(st->PicklingError);
7425 Py_VISIT(st->UnpicklingError);
7426 Py_VISIT(st->dispatch_table);
7427 Py_VISIT(st->extension_registry);
7428 Py_VISIT(st->extension_cache);
7429 Py_VISIT(st->inverted_registry);
7430 Py_VISIT(st->name_mapping_2to3);
7431 Py_VISIT(st->import_mapping_2to3);
7432 Py_VISIT(st->name_mapping_3to2);
7433 Py_VISIT(st->import_mapping_3to2);
7434 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007435 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007436 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007437}
7438
7439static struct PyModuleDef _picklemodule = {
7440 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007441 "_pickle", /* m_name */
7442 pickle_module_doc, /* m_doc */
7443 sizeof(PickleState), /* m_size */
7444 pickle_methods, /* m_methods */
7445 NULL, /* m_reload */
7446 pickle_traverse, /* m_traverse */
7447 pickle_clear, /* m_clear */
7448 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007449};
7450
7451PyMODINIT_FUNC
7452PyInit__pickle(void)
7453{
7454 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007455 PickleState *st;
7456
7457 m = PyState_FindModule(&_picklemodule);
7458 if (m) {
7459 Py_INCREF(m);
7460 return m;
7461 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007462
7463 if (PyType_Ready(&Unpickler_Type) < 0)
7464 return NULL;
7465 if (PyType_Ready(&Pickler_Type) < 0)
7466 return NULL;
7467 if (PyType_Ready(&Pdata_Type) < 0)
7468 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007469 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7470 return NULL;
7471 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7472 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007473
7474 /* Create the module and add the functions. */
7475 m = PyModule_Create(&_picklemodule);
7476 if (m == NULL)
7477 return NULL;
7478
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007479 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007480 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7481 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007482 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007483 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7484 return NULL;
7485
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007486 st = _Pickle_GetState(m);
7487
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007488 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007489 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7490 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007491 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007492 st->PicklingError = \
7493 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7494 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007495 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007496 st->UnpicklingError = \
7497 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7498 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007499 return NULL;
7500
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007501 Py_INCREF(st->PickleError);
7502 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007503 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007504 Py_INCREF(st->PicklingError);
7505 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007506 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007507 Py_INCREF(st->UnpicklingError);
7508 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007509 return NULL;
7510
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007511 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007512 return NULL;
7513
7514 return m;
7515}