blob: c4fe349187c488868ea0b25fc714f7beb0530855 [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);
Zackery Spytz25d38972018-12-05 11:29:20 -07003843 if (obj_class == NULL) {
3844 return -1;
3845 }
3846 p = obj_class != cls;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003847 Py_DECREF(obj_class);
3848 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003849 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003850 "__newobj__ args has the wrong class");
3851 return -1;
3852 }
3853 }
3854 /* XXX: These calls save() are prone to infinite recursion. Imagine
3855 what happen if the value returned by the __reduce__() method of
3856 some extension type contains another object of the same type. Ouch!
3857
3858 Here is a quick example, that I ran into, to illustrate what I
3859 mean:
3860
3861 >>> import pickle, copyreg
3862 >>> copyreg.dispatch_table.pop(complex)
3863 >>> pickle.dumps(1+2j)
3864 Traceback (most recent call last):
3865 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003866 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003867
3868 Removing the complex class from copyreg.dispatch_table made the
3869 __reduce_ex__() method emit another complex object:
3870
3871 >>> (1+1j).__reduce_ex__(2)
3872 (<function __newobj__ at 0xb7b71c3c>,
3873 (<class 'complex'>, (1+1j)), None, None, None)
3874
3875 Thus when save() was called on newargstup (the 2nd item) recursion
3876 ensued. Of course, the bug was in the complex class which had a
3877 broken __getnewargs__() that emitted another complex object. But,
3878 the point, here, is it is quite easy to end up with a broken reduce
3879 function. */
3880
3881 /* Save the class and its __new__ arguments. */
3882 if (save(self, cls, 0) < 0)
3883 return -1;
3884
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003885 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003886 if (newargtup == NULL)
3887 return -1;
3888
3889 p = save(self, newargtup, 0);
3890 Py_DECREF(newargtup);
3891 if (p < 0)
3892 return -1;
3893
3894 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003895 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003896 return -1;
3897 }
3898 else { /* Not using NEWOBJ. */
3899 if (save(self, callable, 0) < 0 ||
3900 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003901 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003902 return -1;
3903 }
3904
3905 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3906 the caller do not want to memoize the object. Not particularly useful,
3907 but that is to mimic the behavior save_reduce() in pickle.py when
3908 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003909 if (obj != NULL) {
3910 /* If the object is already in the memo, this means it is
3911 recursive. In this case, throw away everything we put on the
3912 stack, and fetch the object back from the memo. */
3913 if (PyMemoTable_Get(self->memo, obj)) {
3914 const char pop_op = POP;
3915
3916 if (_Pickler_Write(self, &pop_op, 1) < 0)
3917 return -1;
3918 if (memo_get(self, obj) < 0)
3919 return -1;
3920
3921 return 0;
3922 }
3923 else if (memo_put(self, obj) < 0)
3924 return -1;
3925 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003926
3927 if (listitems && batch_list(self, listitems) < 0)
3928 return -1;
3929
3930 if (dictitems && batch_dict(self, dictitems) < 0)
3931 return -1;
3932
3933 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003934 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003935 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003936 return -1;
3937 }
3938
3939 return 0;
3940}
3941
3942static int
3943save(PicklerObject *self, PyObject *obj, int pers_save)
3944{
3945 PyTypeObject *type;
3946 PyObject *reduce_func = NULL;
3947 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003948 int status = 0;
3949
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003950 if (_Pickler_OpcodeBoundary(self) < 0)
3951 return -1;
3952
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003953 /* The extra pers_save argument is necessary to avoid calling save_pers()
3954 on its returned object. */
3955 if (!pers_save && self->pers_func) {
3956 /* save_pers() returns:
3957 -1 to signal an error;
3958 0 if it did nothing successfully;
3959 1 if a persistent id was saved.
3960 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003961 if ((status = save_pers(self, obj)) != 0)
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003962 return status;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003963 }
3964
3965 type = Py_TYPE(obj);
3966
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003967 /* The old cPickle had an optimization that used switch-case statement
3968 dispatching on the first letter of the type name. This has was removed
3969 since benchmarks shown that this optimization was actually slowing
3970 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003971
3972 /* Atom types; these aren't memoized, so don't check the memo. */
3973
3974 if (obj == Py_None) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003975 return save_none(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003976 }
3977 else if (obj == Py_False || obj == Py_True) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003978 return save_bool(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003979 }
3980 else if (type == &PyLong_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003981 return save_long(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003982 }
3983 else if (type == &PyFloat_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003984 return save_float(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003985 }
3986
3987 /* Check the memo to see if it has the object. If so, generate
3988 a GET (or BINGET) opcode, instead of pickling the object
3989 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003990 if (PyMemoTable_Get(self->memo, obj)) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003991 return memo_get(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003992 }
3993
3994 if (type == &PyBytes_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003995 return save_bytes(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003996 }
3997 else if (type == &PyUnicode_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003998 return save_unicode(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003999 }
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004000
4001 /* We're only calling Py_EnterRecursiveCall here so that atomic
4002 types above are pickled faster. */
4003 if (Py_EnterRecursiveCall(" while pickling an object")) {
4004 return -1;
4005 }
4006
4007 if (type == &PyDict_Type) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004008 status = save_dict(self, obj);
4009 goto done;
4010 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004011 else if (type == &PySet_Type) {
4012 status = save_set(self, obj);
4013 goto done;
4014 }
4015 else if (type == &PyFrozenSet_Type) {
4016 status = save_frozenset(self, obj);
4017 goto done;
4018 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004019 else if (type == &PyList_Type) {
4020 status = save_list(self, obj);
4021 goto done;
4022 }
4023 else if (type == &PyTuple_Type) {
4024 status = save_tuple(self, obj);
4025 goto done;
4026 }
4027 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004028 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004029 goto done;
4030 }
4031 else if (type == &PyFunction_Type) {
4032 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004033 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004034 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004035
4036 /* XXX: This part needs some unit tests. */
4037
4038 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004039 * self.dispatch_table, copyreg.dispatch_table, the object's
4040 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004041 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004042 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004043 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004044 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4045 (PyObject *)type);
4046 if (reduce_func == NULL) {
4047 if (PyErr_Occurred()) {
4048 goto error;
4049 }
4050 } else {
4051 /* PyDict_GetItemWithError() returns a borrowed reference.
4052 Increase the reference count to be consistent with
4053 PyObject_GetItem and _PyObject_GetAttrId used below. */
4054 Py_INCREF(reduce_func);
4055 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004056 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004057 reduce_func = PyObject_GetItem(self->dispatch_table,
4058 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004059 if (reduce_func == NULL) {
4060 if (PyErr_ExceptionMatches(PyExc_KeyError))
4061 PyErr_Clear();
4062 else
4063 goto error;
4064 }
4065 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004066 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004067 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004068 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004069 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004070 else if (PyType_IsSubtype(type, &PyType_Type)) {
4071 status = save_global(self, obj, NULL);
4072 goto done;
4073 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004074 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004075 _Py_IDENTIFIER(__reduce__);
4076 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004077
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004078
4079 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4080 automatically defined as __reduce__. While this is convenient, this
4081 make it impossible to know which method was actually called. Of
4082 course, this is not a big deal. But still, it would be nice to let
4083 the user know which method was called when something go
4084 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4085 don't actually have to check for a __reduce__ method. */
4086
4087 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004088 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4089 goto error;
4090 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004091 if (reduce_func != NULL) {
4092 PyObject *proto;
4093 proto = PyLong_FromLong(self->proto);
4094 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004095 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004096 }
4097 }
4098 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004099 PickleState *st = _Pickle_GetGlobalState();
4100
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004101 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004102 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004103 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004104 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004105 }
4106 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004107 PyErr_Format(st->PicklingError,
4108 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004109 type->tp_name, obj);
4110 goto error;
4111 }
4112 }
4113 }
4114
4115 if (reduce_value == NULL)
4116 goto error;
4117
4118 if (PyUnicode_Check(reduce_value)) {
4119 status = save_global(self, obj, reduce_value);
4120 goto done;
4121 }
4122
4123 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004124 PickleState *st = _Pickle_GetGlobalState();
4125 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004126 "__reduce__ must return a string or tuple");
4127 goto error;
4128 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129
4130 status = save_reduce(self, reduce_value, obj);
4131
4132 if (0) {
4133 error:
4134 status = -1;
4135 }
4136 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004137
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004138 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004139 Py_XDECREF(reduce_func);
4140 Py_XDECREF(reduce_value);
4141
4142 return status;
4143}
4144
4145static int
4146dump(PicklerObject *self, PyObject *obj)
4147{
4148 const char stop_op = STOP;
4149
4150 if (self->proto >= 2) {
4151 char header[2];
4152
4153 header[0] = PROTO;
4154 assert(self->proto >= 0 && self->proto < 256);
4155 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004156 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004157 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004158 if (self->proto >= 4)
4159 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004160 }
4161
4162 if (save(self, obj, 0) < 0 ||
Serhiy Storchakac8695292018-04-04 00:11:27 +03004163 _Pickler_Write(self, &stop_op, 1) < 0 ||
4164 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004165 return -1;
Serhiy Storchakac8695292018-04-04 00:11:27 +03004166 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004167 return 0;
4168}
4169
Larry Hastings61272b72014-01-07 12:41:53 -08004170/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004171
4172_pickle.Pickler.clear_memo
4173
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004174Clears the pickler's "memo".
4175
4176The memo is the data structure that remembers which objects the
4177pickler has already seen, so that shared or recursive objects are
4178pickled by reference and not by value. This method is useful when
4179re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004180[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004181
Larry Hastings3cceb382014-01-04 11:09:09 -08004182static PyObject *
4183_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004184/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004185{
4186 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004187 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004188
4189 Py_RETURN_NONE;
4190}
4191
Larry Hastings61272b72014-01-07 12:41:53 -08004192/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004193
4194_pickle.Pickler.dump
4195
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004196 obj: object
4197 /
4198
4199Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004200[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004201
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004202static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004203_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004204/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004205{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004206 /* Check whether the Pickler was initialized correctly (issue3664).
4207 Developers often forget to call __init__() in their subclasses, which
4208 would trigger a segfault without this check. */
4209 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004210 PickleState *st = _Pickle_GetGlobalState();
4211 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004212 "Pickler.__init__() was not called by %s.__init__()",
4213 Py_TYPE(self)->tp_name);
4214 return NULL;
4215 }
4216
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004217 if (_Pickler_ClearBuffer(self) < 0)
4218 return NULL;
4219
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004220 if (dump(self, obj) < 0)
4221 return NULL;
4222
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004223 if (_Pickler_FlushToFile(self) < 0)
4224 return NULL;
4225
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004226 Py_RETURN_NONE;
4227}
4228
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004229/*[clinic input]
4230
4231_pickle.Pickler.__sizeof__ -> Py_ssize_t
4232
4233Returns size in memory, in bytes.
4234[clinic start generated code]*/
4235
4236static Py_ssize_t
4237_pickle_Pickler___sizeof___impl(PicklerObject *self)
4238/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4239{
4240 Py_ssize_t res, s;
4241
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004242 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004243 if (self->memo != NULL) {
4244 res += sizeof(PyMemoTable);
4245 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4246 }
4247 if (self->output_buffer != NULL) {
4248 s = _PySys_GetSizeOf(self->output_buffer);
4249 if (s == -1)
4250 return -1;
4251 res += s;
4252 }
4253 return res;
4254}
4255
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004256static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004257 _PICKLE_PICKLER_DUMP_METHODDEF
4258 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004259 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004260 {NULL, NULL} /* sentinel */
4261};
4262
4263static void
4264Pickler_dealloc(PicklerObject *self)
4265{
4266 PyObject_GC_UnTrack(self);
4267
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004268 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004269 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004270 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004271 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004272 Py_XDECREF(self->fast_memo);
4273
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004274 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004275
4276 Py_TYPE(self)->tp_free((PyObject *)self);
4277}
4278
4279static int
4280Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4281{
4282 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004283 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004284 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004285 Py_VISIT(self->fast_memo);
4286 return 0;
4287}
4288
4289static int
4290Pickler_clear(PicklerObject *self)
4291{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004292 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004293 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004294 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004295 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004296 Py_CLEAR(self->fast_memo);
4297
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004298 if (self->memo != NULL) {
4299 PyMemoTable *memo = self->memo;
4300 self->memo = NULL;
4301 PyMemoTable_Del(memo);
4302 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004303 return 0;
4304}
4305
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004306
Larry Hastings61272b72014-01-07 12:41:53 -08004307/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004308
4309_pickle.Pickler.__init__
4310
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004311 file: object
4312 protocol: object = NULL
4313 fix_imports: bool = True
4314
4315This takes a binary file for writing a pickle data stream.
4316
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004317The optional *protocol* argument tells the pickler to use the given
4318protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4319protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004320
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004321Specifying a negative protocol version selects the highest protocol
4322version supported. The higher the protocol used, the more recent the
4323version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004324
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004325The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004326bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004327writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004328this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004329
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004330If *fix_imports* is True and protocol is less than 3, pickle will try
4331to map the new Python 3 names to the old module names used in Python
43322, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004333[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004334
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004335static int
Larry Hastings89964c42015-04-14 18:07:59 -04004336_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4337 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004338/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004339{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004340 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004341 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004342
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004343 /* In case of multiple __init__() calls, clear previous content. */
4344 if (self->write != NULL)
4345 (void)Pickler_clear(self);
4346
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004347 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004348 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004349
4350 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004351 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004352
4353 /* memo and output_buffer may have already been created in _Pickler_New */
4354 if (self->memo == NULL) {
4355 self->memo = PyMemoTable_New();
4356 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004357 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004358 }
4359 self->output_len = 0;
4360 if (self->output_buffer == NULL) {
4361 self->max_output_len = WRITE_BUF_SIZE;
4362 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4363 self->max_output_len);
4364 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004365 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004366 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004367
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004368 self->fast = 0;
4369 self->fast_nesting = 0;
4370 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004371
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004372 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4373 &self->pers_func, &self->pers_func_self) < 0)
4374 {
4375 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004376 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004377
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004378 if (_PyObject_LookupAttrId((PyObject *)self,
4379 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4380 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004381 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004382
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004383 return 0;
4384}
4385
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004386
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004387/* Define a proxy object for the Pickler's internal memo object. This is to
4388 * avoid breaking code like:
4389 * pickler.memo.clear()
4390 * and
4391 * pickler.memo = saved_memo
4392 * Is this a good idea? Not really, but we don't want to break code that uses
4393 * it. Note that we don't implement the entire mapping API here. This is
4394 * intentional, as these should be treated as black-box implementation details.
4395 */
4396
Larry Hastings61272b72014-01-07 12:41:53 -08004397/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004398_pickle.PicklerMemoProxy.clear
4399
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004400Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004401[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004402
Larry Hastings3cceb382014-01-04 11:09:09 -08004403static PyObject *
4404_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004405/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004406{
4407 if (self->pickler->memo)
4408 PyMemoTable_Clear(self->pickler->memo);
4409 Py_RETURN_NONE;
4410}
4411
Larry Hastings61272b72014-01-07 12:41:53 -08004412/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004413_pickle.PicklerMemoProxy.copy
4414
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004415Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004416[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004417
Larry Hastings3cceb382014-01-04 11:09:09 -08004418static PyObject *
4419_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004420/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004421{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004422 PyMemoTable *memo;
4423 PyObject *new_memo = PyDict_New();
4424 if (new_memo == NULL)
4425 return NULL;
4426
4427 memo = self->pickler->memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07004428 for (size_t i = 0; i < memo->mt_allocated; ++i) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004429 PyMemoEntry entry = memo->mt_table[i];
4430 if (entry.me_key != NULL) {
4431 int status;
4432 PyObject *key, *value;
4433
4434 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004435 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004436
4437 if (key == NULL || value == NULL) {
4438 Py_XDECREF(key);
4439 Py_XDECREF(value);
4440 goto error;
4441 }
4442 status = PyDict_SetItem(new_memo, key, value);
4443 Py_DECREF(key);
4444 Py_DECREF(value);
4445 if (status < 0)
4446 goto error;
4447 }
4448 }
4449 return new_memo;
4450
4451 error:
4452 Py_XDECREF(new_memo);
4453 return NULL;
4454}
4455
Larry Hastings61272b72014-01-07 12:41:53 -08004456/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004457_pickle.PicklerMemoProxy.__reduce__
4458
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004459Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004460[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004461
Larry Hastings3cceb382014-01-04 11:09:09 -08004462static PyObject *
4463_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004464/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004465{
4466 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004467 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004468 if (contents == NULL)
4469 return NULL;
4470
4471 reduce_value = PyTuple_New(2);
4472 if (reduce_value == NULL) {
4473 Py_DECREF(contents);
4474 return NULL;
4475 }
4476 dict_args = PyTuple_New(1);
4477 if (dict_args == NULL) {
4478 Py_DECREF(contents);
4479 Py_DECREF(reduce_value);
4480 return NULL;
4481 }
4482 PyTuple_SET_ITEM(dict_args, 0, contents);
4483 Py_INCREF((PyObject *)&PyDict_Type);
4484 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4485 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4486 return reduce_value;
4487}
4488
4489static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004490 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4491 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4492 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004493 {NULL, NULL} /* sentinel */
4494};
4495
4496static void
4497PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4498{
4499 PyObject_GC_UnTrack(self);
4500 Py_XDECREF(self->pickler);
4501 PyObject_GC_Del((PyObject *)self);
4502}
4503
4504static int
4505PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4506 visitproc visit, void *arg)
4507{
4508 Py_VISIT(self->pickler);
4509 return 0;
4510}
4511
4512static int
4513PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4514{
4515 Py_CLEAR(self->pickler);
4516 return 0;
4517}
4518
4519static PyTypeObject PicklerMemoProxyType = {
4520 PyVarObject_HEAD_INIT(NULL, 0)
4521 "_pickle.PicklerMemoProxy", /*tp_name*/
4522 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4523 0,
4524 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4525 0, /* tp_print */
4526 0, /* tp_getattr */
4527 0, /* tp_setattr */
4528 0, /* tp_compare */
4529 0, /* tp_repr */
4530 0, /* tp_as_number */
4531 0, /* tp_as_sequence */
4532 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004533 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004534 0, /* tp_call */
4535 0, /* tp_str */
4536 PyObject_GenericGetAttr, /* tp_getattro */
4537 PyObject_GenericSetAttr, /* tp_setattro */
4538 0, /* tp_as_buffer */
4539 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4540 0, /* tp_doc */
4541 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4542 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4543 0, /* tp_richcompare */
4544 0, /* tp_weaklistoffset */
4545 0, /* tp_iter */
4546 0, /* tp_iternext */
4547 picklerproxy_methods, /* tp_methods */
4548};
4549
4550static PyObject *
4551PicklerMemoProxy_New(PicklerObject *pickler)
4552{
4553 PicklerMemoProxyObject *self;
4554
4555 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4556 if (self == NULL)
4557 return NULL;
4558 Py_INCREF(pickler);
4559 self->pickler = pickler;
4560 PyObject_GC_Track(self);
4561 return (PyObject *)self;
4562}
4563
4564/*****************************************************************************/
4565
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004566static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004567Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004568{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004569 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570}
4571
4572static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004573Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004574{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004575 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004576
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004577 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004578 PyErr_SetString(PyExc_TypeError,
4579 "attribute deletion is not supported");
4580 return -1;
4581 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004582
4583 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4584 PicklerObject *pickler =
4585 ((PicklerMemoProxyObject *)obj)->pickler;
4586
4587 new_memo = PyMemoTable_Copy(pickler->memo);
4588 if (new_memo == NULL)
4589 return -1;
4590 }
4591 else if (PyDict_Check(obj)) {
4592 Py_ssize_t i = 0;
4593 PyObject *key, *value;
4594
4595 new_memo = PyMemoTable_New();
4596 if (new_memo == NULL)
4597 return -1;
4598
4599 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004600 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004601 PyObject *memo_obj;
4602
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004603 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004604 PyErr_SetString(PyExc_TypeError,
4605 "'memo' values must be 2-item tuples");
4606 goto error;
4607 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004608 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004609 if (memo_id == -1 && PyErr_Occurred())
4610 goto error;
4611 memo_obj = PyTuple_GET_ITEM(value, 1);
4612 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4613 goto error;
4614 }
4615 }
4616 else {
4617 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +02004618 "'memo' attribute must be a PicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004619 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004620 return -1;
4621 }
4622
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004623 PyMemoTable_Del(self->memo);
4624 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004625
4626 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004627
4628 error:
4629 if (new_memo)
4630 PyMemoTable_Del(new_memo);
4631 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004632}
4633
4634static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004635Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004636{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004637 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004638 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004639 return NULL;
4640 }
4641 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004642}
4643
4644static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004645Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004646{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004647 if (value == NULL) {
4648 PyErr_SetString(PyExc_TypeError,
4649 "attribute deletion is not supported");
4650 return -1;
4651 }
4652 if (!PyCallable_Check(value)) {
4653 PyErr_SetString(PyExc_TypeError,
4654 "persistent_id must be a callable taking one argument");
4655 return -1;
4656 }
4657
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004658 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004659 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004660 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004661
4662 return 0;
4663}
4664
4665static PyMemberDef Pickler_members[] = {
4666 {"bin", T_INT, offsetof(PicklerObject, bin)},
4667 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004668 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004669 {NULL}
4670};
4671
4672static PyGetSetDef Pickler_getsets[] = {
4673 {"memo", (getter)Pickler_get_memo,
4674 (setter)Pickler_set_memo},
4675 {"persistent_id", (getter)Pickler_get_persid,
4676 (setter)Pickler_set_persid},
4677 {NULL}
4678};
4679
4680static PyTypeObject Pickler_Type = {
4681 PyVarObject_HEAD_INIT(NULL, 0)
4682 "_pickle.Pickler" , /*tp_name*/
4683 sizeof(PicklerObject), /*tp_basicsize*/
4684 0, /*tp_itemsize*/
4685 (destructor)Pickler_dealloc, /*tp_dealloc*/
4686 0, /*tp_print*/
4687 0, /*tp_getattr*/
4688 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004689 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004690 0, /*tp_repr*/
4691 0, /*tp_as_number*/
4692 0, /*tp_as_sequence*/
4693 0, /*tp_as_mapping*/
4694 0, /*tp_hash*/
4695 0, /*tp_call*/
4696 0, /*tp_str*/
4697 0, /*tp_getattro*/
4698 0, /*tp_setattro*/
4699 0, /*tp_as_buffer*/
4700 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004701 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004702 (traverseproc)Pickler_traverse, /*tp_traverse*/
4703 (inquiry)Pickler_clear, /*tp_clear*/
4704 0, /*tp_richcompare*/
4705 0, /*tp_weaklistoffset*/
4706 0, /*tp_iter*/
4707 0, /*tp_iternext*/
4708 Pickler_methods, /*tp_methods*/
4709 Pickler_members, /*tp_members*/
4710 Pickler_getsets, /*tp_getset*/
4711 0, /*tp_base*/
4712 0, /*tp_dict*/
4713 0, /*tp_descr_get*/
4714 0, /*tp_descr_set*/
4715 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004716 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004717 PyType_GenericAlloc, /*tp_alloc*/
4718 PyType_GenericNew, /*tp_new*/
4719 PyObject_GC_Del, /*tp_free*/
4720 0, /*tp_is_gc*/
4721};
4722
Victor Stinner121aab42011-09-29 23:40:53 +02004723/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004724
4725 XXX: It would be nice to able to avoid Python function call overhead, by
4726 using directly the C version of find_class(), when find_class() is not
4727 overridden by a subclass. Although, this could become rather hackish. A
4728 simpler optimization would be to call the C function when self is not a
4729 subclass instance. */
4730static PyObject *
4731find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4732{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004733 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004734
Victor Stinner55ba38a2016-12-09 16:09:30 +01004735 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4736 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004737}
4738
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004739static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004740marker(UnpicklerObject *self)
4741{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004742 Py_ssize_t mark;
4743
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004744 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004745 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004746 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004747 return -1;
4748 }
4749
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004750 mark = self->marks[--self->num_marks];
4751 self->stack->mark_set = self->num_marks != 0;
4752 self->stack->fence = self->num_marks ?
4753 self->marks[self->num_marks - 1] : 0;
4754 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004755}
4756
4757static int
4758load_none(UnpicklerObject *self)
4759{
4760 PDATA_APPEND(self->stack, Py_None, -1);
4761 return 0;
4762}
4763
4764static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004765load_int(UnpicklerObject *self)
4766{
4767 PyObject *value;
4768 char *endptr, *s;
4769 Py_ssize_t len;
4770 long x;
4771
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004772 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004773 return -1;
4774 if (len < 2)
4775 return bad_readline();
4776
4777 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004778 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004779 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004780 x = strtol(s, &endptr, 0);
4781
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004782 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004783 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004784 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004785 errno = 0;
4786 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004787 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004788 if (value == NULL) {
4789 PyErr_SetString(PyExc_ValueError,
4790 "could not convert string to int");
4791 return -1;
4792 }
4793 }
4794 else {
4795 if (len == 3 && (x == 0 || x == 1)) {
4796 if ((value = PyBool_FromLong(x)) == NULL)
4797 return -1;
4798 }
4799 else {
4800 if ((value = PyLong_FromLong(x)) == NULL)
4801 return -1;
4802 }
4803 }
4804
4805 PDATA_PUSH(self->stack, value, -1);
4806 return 0;
4807}
4808
4809static int
4810load_bool(UnpicklerObject *self, PyObject *boolean)
4811{
4812 assert(boolean == Py_True || boolean == Py_False);
4813 PDATA_APPEND(self->stack, boolean, -1);
4814 return 0;
4815}
4816
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004817/* s contains x bytes of an unsigned little-endian integer. Return its value
4818 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4819 */
4820static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004821calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004822{
4823 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004824 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004825 size_t x = 0;
4826
Serhiy Storchakae0606192015-09-29 22:10:07 +03004827 if (nbytes > (int)sizeof(size_t)) {
4828 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4829 * have 64-bit size that can't be represented on 32-bit platform.
4830 */
4831 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4832 if (s[i])
4833 return -1;
4834 }
4835 nbytes = (int)sizeof(size_t);
4836 }
4837 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004838 x |= (size_t) s[i] << (8 * i);
4839 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004840
4841 if (x > PY_SSIZE_T_MAX)
4842 return -1;
4843 else
4844 return (Py_ssize_t) x;
4845}
4846
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004847/* s contains x bytes of a little-endian integer. Return its value as a
4848 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004849 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004850 * of x-platform bugs.
4851 */
4852static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004853calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004854{
4855 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004856 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004857 long x = 0;
4858
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004859 for (i = 0; i < nbytes; i++) {
4860 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004861 }
4862
4863 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4864 * is signed, so on a box with longs bigger than 4 bytes we need
4865 * to extend a BININT's sign bit to the full width.
4866 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004867 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004868 x |= -(x & (1L << 31));
4869 }
4870
4871 return x;
4872}
4873
4874static int
4875load_binintx(UnpicklerObject *self, char *s, int size)
4876{
4877 PyObject *value;
4878 long x;
4879
4880 x = calc_binint(s, size);
4881
4882 if ((value = PyLong_FromLong(x)) == NULL)
4883 return -1;
4884
4885 PDATA_PUSH(self->stack, value, -1);
4886 return 0;
4887}
4888
4889static int
4890load_binint(UnpicklerObject *self)
4891{
4892 char *s;
4893
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004894 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004895 return -1;
4896
4897 return load_binintx(self, s, 4);
4898}
4899
4900static int
4901load_binint1(UnpicklerObject *self)
4902{
4903 char *s;
4904
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004905 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004906 return -1;
4907
4908 return load_binintx(self, s, 1);
4909}
4910
4911static int
4912load_binint2(UnpicklerObject *self)
4913{
4914 char *s;
4915
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004916 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004917 return -1;
4918
4919 return load_binintx(self, s, 2);
4920}
4921
4922static int
4923load_long(UnpicklerObject *self)
4924{
4925 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004926 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004927 Py_ssize_t len;
4928
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004929 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004930 return -1;
4931 if (len < 2)
4932 return bad_readline();
4933
Mark Dickinson8dd05142009-01-20 20:43:58 +00004934 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4935 the 'L' before calling PyLong_FromString. In order to maintain
4936 compatibility with Python 3.0.0, we don't actually *require*
4937 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004938 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004939 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004940 /* XXX: Should the base argument explicitly set to 10? */
4941 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004942 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004943 return -1;
4944
4945 PDATA_PUSH(self->stack, value, -1);
4946 return 0;
4947}
4948
4949/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4950 * data following.
4951 */
4952static int
4953load_counted_long(UnpicklerObject *self, int size)
4954{
4955 PyObject *value;
4956 char *nbytes;
4957 char *pdata;
4958
4959 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004960 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004961 return -1;
4962
4963 size = calc_binint(nbytes, size);
4964 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004965 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004966 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004967 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968 "LONG pickle has negative byte count");
4969 return -1;
4970 }
4971
4972 if (size == 0)
4973 value = PyLong_FromLong(0L);
4974 else {
4975 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004976 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004977 return -1;
4978 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4979 1 /* little endian */ , 1 /* signed */ );
4980 }
4981 if (value == NULL)
4982 return -1;
4983 PDATA_PUSH(self->stack, value, -1);
4984 return 0;
4985}
4986
4987static int
4988load_float(UnpicklerObject *self)
4989{
4990 PyObject *value;
4991 char *endptr, *s;
4992 Py_ssize_t len;
4993 double d;
4994
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004995 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004996 return -1;
4997 if (len < 2)
4998 return bad_readline();
4999
5000 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00005001 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
5002 if (d == -1.0 && PyErr_Occurred())
5003 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005004 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005005 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5006 return -1;
5007 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005008 value = PyFloat_FromDouble(d);
5009 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005010 return -1;
5011
5012 PDATA_PUSH(self->stack, value, -1);
5013 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005014}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005015
5016static int
5017load_binfloat(UnpicklerObject *self)
5018{
5019 PyObject *value;
5020 double x;
5021 char *s;
5022
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005023 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005024 return -1;
5025
5026 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5027 if (x == -1.0 && PyErr_Occurred())
5028 return -1;
5029
5030 if ((value = PyFloat_FromDouble(x)) == NULL)
5031 return -1;
5032
5033 PDATA_PUSH(self->stack, value, -1);
5034 return 0;
5035}
5036
5037static int
5038load_string(UnpicklerObject *self)
5039{
5040 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005041 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042 Py_ssize_t len;
5043 char *s, *p;
5044
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005045 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005046 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005047 /* Strip the newline */
5048 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005049 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005050 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005051 p = s + 1;
5052 len -= 2;
5053 }
5054 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005055 PickleState *st = _Pickle_GetGlobalState();
5056 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005057 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005058 return -1;
5059 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005060 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005061
5062 /* Use the PyBytes API to decode the string, since that is what is used
5063 to encode, and then coerce the result to Unicode. */
5064 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005065 if (bytes == NULL)
5066 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005067
5068 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5069 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5070 if (strcmp(self->encoding, "bytes") == 0) {
5071 obj = bytes;
5072 }
5073 else {
5074 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5075 Py_DECREF(bytes);
5076 if (obj == NULL) {
5077 return -1;
5078 }
5079 }
5080
5081 PDATA_PUSH(self->stack, obj, -1);
5082 return 0;
5083}
5084
5085static int
5086load_counted_binstring(UnpicklerObject *self, int nbytes)
5087{
5088 PyObject *obj;
5089 Py_ssize_t size;
5090 char *s;
5091
5092 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005093 return -1;
5094
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005095 size = calc_binsize(s, nbytes);
5096 if (size < 0) {
5097 PickleState *st = _Pickle_GetGlobalState();
5098 PyErr_Format(st->UnpicklingError,
5099 "BINSTRING exceeds system's maximum size of %zd bytes",
5100 PY_SSIZE_T_MAX);
5101 return -1;
5102 }
5103
5104 if (_Unpickler_Read(self, &s, size) < 0)
5105 return -1;
5106
5107 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5108 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5109 if (strcmp(self->encoding, "bytes") == 0) {
5110 obj = PyBytes_FromStringAndSize(s, size);
5111 }
5112 else {
5113 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5114 }
5115 if (obj == NULL) {
5116 return -1;
5117 }
5118
5119 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005120 return 0;
5121}
5122
5123static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005124load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005125{
5126 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005127 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005128 char *s;
5129
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005130 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005131 return -1;
5132
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005133 size = calc_binsize(s, nbytes);
5134 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005135 PyErr_Format(PyExc_OverflowError,
5136 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005137 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005138 return -1;
5139 }
5140
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005141 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005142 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005143
5144 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005145 if (bytes == NULL)
5146 return -1;
5147
5148 PDATA_PUSH(self->stack, bytes, -1);
5149 return 0;
5150}
5151
5152static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005153load_unicode(UnpicklerObject *self)
5154{
5155 PyObject *str;
5156 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005157 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005158
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005159 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005160 return -1;
5161 if (len < 1)
5162 return bad_readline();
5163
5164 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5165 if (str == NULL)
5166 return -1;
5167
5168 PDATA_PUSH(self->stack, str, -1);
5169 return 0;
5170}
5171
5172static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005173load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005174{
5175 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005176 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005177 char *s;
5178
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005179 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005180 return -1;
5181
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005182 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005183 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005184 PyErr_Format(PyExc_OverflowError,
5185 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005186 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005187 return -1;
5188 }
5189
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005190 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005191 return -1;
5192
Victor Stinner485fb562010-04-13 11:07:24 +00005193 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005194 if (str == NULL)
5195 return -1;
5196
5197 PDATA_PUSH(self->stack, str, -1);
5198 return 0;
5199}
5200
5201static int
Victor Stinner21b47112016-03-14 18:09:39 +01005202load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005203{
5204 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005205
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005206 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005207 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005208
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005209 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005210 if (tuple == NULL)
5211 return -1;
5212 PDATA_PUSH(self->stack, tuple, -1);
5213 return 0;
5214}
5215
5216static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005217load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005218{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005219 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005220
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005221 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005222 return -1;
5223
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005224 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005225}
5226
5227static int
5228load_empty_list(UnpicklerObject *self)
5229{
5230 PyObject *list;
5231
5232 if ((list = PyList_New(0)) == NULL)
5233 return -1;
5234 PDATA_PUSH(self->stack, list, -1);
5235 return 0;
5236}
5237
5238static int
5239load_empty_dict(UnpicklerObject *self)
5240{
5241 PyObject *dict;
5242
5243 if ((dict = PyDict_New()) == NULL)
5244 return -1;
5245 PDATA_PUSH(self->stack, dict, -1);
5246 return 0;
5247}
5248
5249static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005250load_empty_set(UnpicklerObject *self)
5251{
5252 PyObject *set;
5253
5254 if ((set = PySet_New(NULL)) == NULL)
5255 return -1;
5256 PDATA_PUSH(self->stack, set, -1);
5257 return 0;
5258}
5259
5260static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005261load_list(UnpicklerObject *self)
5262{
5263 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005264 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005265
5266 if ((i = marker(self)) < 0)
5267 return -1;
5268
5269 list = Pdata_poplist(self->stack, i);
5270 if (list == NULL)
5271 return -1;
5272 PDATA_PUSH(self->stack, list, -1);
5273 return 0;
5274}
5275
5276static int
5277load_dict(UnpicklerObject *self)
5278{
5279 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005280 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005281
5282 if ((i = marker(self)) < 0)
5283 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005284 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005285
5286 if ((dict = PyDict_New()) == NULL)
5287 return -1;
5288
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005289 if ((j - i) % 2 != 0) {
5290 PickleState *st = _Pickle_GetGlobalState();
5291 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005292 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005293 return -1;
5294 }
5295
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005296 for (k = i + 1; k < j; k += 2) {
5297 key = self->stack->data[k - 1];
5298 value = self->stack->data[k];
5299 if (PyDict_SetItem(dict, key, value) < 0) {
5300 Py_DECREF(dict);
5301 return -1;
5302 }
5303 }
5304 Pdata_clear(self->stack, i);
5305 PDATA_PUSH(self->stack, dict, -1);
5306 return 0;
5307}
5308
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005309static int
5310load_frozenset(UnpicklerObject *self)
5311{
5312 PyObject *items;
5313 PyObject *frozenset;
5314 Py_ssize_t i;
5315
5316 if ((i = marker(self)) < 0)
5317 return -1;
5318
5319 items = Pdata_poptuple(self->stack, i);
5320 if (items == NULL)
5321 return -1;
5322
5323 frozenset = PyFrozenSet_New(items);
5324 Py_DECREF(items);
5325 if (frozenset == NULL)
5326 return -1;
5327
5328 PDATA_PUSH(self->stack, frozenset, -1);
5329 return 0;
5330}
5331
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005332static PyObject *
5333instantiate(PyObject *cls, PyObject *args)
5334{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005335 /* Caller must assure args are a tuple. Normally, args come from
5336 Pdata_poptuple which packs objects from the top of the stack
5337 into a newly created tuple. */
5338 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005339 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5340 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005341 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005342 PyObject *func;
5343 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5344 return NULL;
5345 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005346 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005347 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5348 }
5349 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005350 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005351 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005352}
5353
5354static int
5355load_obj(UnpicklerObject *self)
5356{
5357 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005358 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005359
5360 if ((i = marker(self)) < 0)
5361 return -1;
5362
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005363 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005364 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005365
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005366 args = Pdata_poptuple(self->stack, i + 1);
5367 if (args == NULL)
5368 return -1;
5369
5370 PDATA_POP(self->stack, cls);
5371 if (cls) {
5372 obj = instantiate(cls, args);
5373 Py_DECREF(cls);
5374 }
5375 Py_DECREF(args);
5376 if (obj == NULL)
5377 return -1;
5378
5379 PDATA_PUSH(self->stack, obj, -1);
5380 return 0;
5381}
5382
5383static int
5384load_inst(UnpicklerObject *self)
5385{
5386 PyObject *cls = NULL;
5387 PyObject *args = NULL;
5388 PyObject *obj = NULL;
5389 PyObject *module_name;
5390 PyObject *class_name;
5391 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005392 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005393 char *s;
5394
5395 if ((i = marker(self)) < 0)
5396 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005397 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005398 return -1;
5399 if (len < 2)
5400 return bad_readline();
5401
5402 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5403 identifiers are permitted in Python 3.0, since the INST opcode is only
5404 supported by older protocols on Python 2.x. */
5405 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5406 if (module_name == NULL)
5407 return -1;
5408
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005409 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005410 if (len < 2) {
5411 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005412 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005413 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005414 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005415 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005416 cls = find_class(self, module_name, class_name);
5417 Py_DECREF(class_name);
5418 }
5419 }
5420 Py_DECREF(module_name);
5421
5422 if (cls == NULL)
5423 return -1;
5424
5425 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5426 obj = instantiate(cls, args);
5427 Py_DECREF(args);
5428 }
5429 Py_DECREF(cls);
5430
5431 if (obj == NULL)
5432 return -1;
5433
5434 PDATA_PUSH(self->stack, obj, -1);
5435 return 0;
5436}
5437
5438static int
5439load_newobj(UnpicklerObject *self)
5440{
5441 PyObject *args = NULL;
5442 PyObject *clsraw = NULL;
5443 PyTypeObject *cls; /* clsraw cast to its true type */
5444 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005445 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005446
5447 /* Stack is ... cls argtuple, and we want to call
5448 * cls.__new__(cls, *argtuple).
5449 */
5450 PDATA_POP(self->stack, args);
5451 if (args == NULL)
5452 goto error;
5453 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005454 PyErr_SetString(st->UnpicklingError,
5455 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005456 goto error;
5457 }
5458
5459 PDATA_POP(self->stack, clsraw);
5460 cls = (PyTypeObject *)clsraw;
5461 if (cls == NULL)
5462 goto error;
5463 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005464 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005465 "isn't a type object");
5466 goto error;
5467 }
5468 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005469 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005470 "has NULL tp_new");
5471 goto error;
5472 }
5473
5474 /* Call __new__. */
5475 obj = cls->tp_new(cls, args, NULL);
5476 if (obj == NULL)
5477 goto error;
5478
5479 Py_DECREF(args);
5480 Py_DECREF(clsraw);
5481 PDATA_PUSH(self->stack, obj, -1);
5482 return 0;
5483
5484 error:
5485 Py_XDECREF(args);
5486 Py_XDECREF(clsraw);
5487 return -1;
5488}
5489
5490static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005491load_newobj_ex(UnpicklerObject *self)
5492{
5493 PyObject *cls, *args, *kwargs;
5494 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005495 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005496
5497 PDATA_POP(self->stack, kwargs);
5498 if (kwargs == NULL) {
5499 return -1;
5500 }
5501 PDATA_POP(self->stack, args);
5502 if (args == NULL) {
5503 Py_DECREF(kwargs);
5504 return -1;
5505 }
5506 PDATA_POP(self->stack, cls);
5507 if (cls == NULL) {
5508 Py_DECREF(kwargs);
5509 Py_DECREF(args);
5510 return -1;
5511 }
Larry Hastings61272b72014-01-07 12:41:53 -08005512
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005513 if (!PyType_Check(cls)) {
5514 Py_DECREF(kwargs);
5515 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005516 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005517 "NEWOBJ_EX class argument must be a type, not %.200s",
5518 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005519 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005520 return -1;
5521 }
5522
5523 if (((PyTypeObject *)cls)->tp_new == NULL) {
5524 Py_DECREF(kwargs);
5525 Py_DECREF(args);
5526 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005527 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005528 "NEWOBJ_EX class argument doesn't have __new__");
5529 return -1;
5530 }
5531 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5532 Py_DECREF(kwargs);
5533 Py_DECREF(args);
5534 Py_DECREF(cls);
5535 if (obj == NULL) {
5536 return -1;
5537 }
5538 PDATA_PUSH(self->stack, obj, -1);
5539 return 0;
5540}
5541
5542static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005543load_global(UnpicklerObject *self)
5544{
5545 PyObject *global = NULL;
5546 PyObject *module_name;
5547 PyObject *global_name;
5548 Py_ssize_t len;
5549 char *s;
5550
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005551 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005552 return -1;
5553 if (len < 2)
5554 return bad_readline();
5555 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5556 if (!module_name)
5557 return -1;
5558
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005559 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560 if (len < 2) {
5561 Py_DECREF(module_name);
5562 return bad_readline();
5563 }
5564 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5565 if (global_name) {
5566 global = find_class(self, module_name, global_name);
5567 Py_DECREF(global_name);
5568 }
5569 }
5570 Py_DECREF(module_name);
5571
5572 if (global == NULL)
5573 return -1;
5574 PDATA_PUSH(self->stack, global, -1);
5575 return 0;
5576}
5577
5578static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005579load_stack_global(UnpicklerObject *self)
5580{
5581 PyObject *global;
5582 PyObject *module_name;
5583 PyObject *global_name;
5584
5585 PDATA_POP(self->stack, global_name);
5586 PDATA_POP(self->stack, module_name);
5587 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5588 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005589 PickleState *st = _Pickle_GetGlobalState();
5590 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005591 Py_XDECREF(global_name);
5592 Py_XDECREF(module_name);
5593 return -1;
5594 }
5595 global = find_class(self, module_name, global_name);
5596 Py_DECREF(global_name);
5597 Py_DECREF(module_name);
5598 if (global == NULL)
5599 return -1;
5600 PDATA_PUSH(self->stack, global, -1);
5601 return 0;
5602}
5603
5604static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005605load_persid(UnpicklerObject *self)
5606{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005607 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005608 Py_ssize_t len;
5609 char *s;
5610
5611 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005612 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005613 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005614 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615 return bad_readline();
5616
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005617 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5618 if (pid == NULL) {
5619 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5620 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5621 "persistent IDs in protocol 0 must be "
5622 "ASCII strings");
5623 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005624 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005625 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005626
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005627 obj = call_method(self->pers_func, self->pers_func_self, pid);
5628 Py_DECREF(pid);
5629 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 return -1;
5631
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005632 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633 return 0;
5634 }
5635 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005636 PickleState *st = _Pickle_GetGlobalState();
5637 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638 "A load persistent id instruction was encountered,\n"
5639 "but no persistent_load function was specified.");
5640 return -1;
5641 }
5642}
5643
5644static int
5645load_binpersid(UnpicklerObject *self)
5646{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005647 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005648
5649 if (self->pers_func) {
5650 PDATA_POP(self->stack, pid);
5651 if (pid == NULL)
5652 return -1;
5653
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005654 obj = call_method(self->pers_func, self->pers_func_self, pid);
5655 Py_DECREF(pid);
5656 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657 return -1;
5658
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005659 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005660 return 0;
5661 }
5662 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005663 PickleState *st = _Pickle_GetGlobalState();
5664 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005665 "A load persistent id instruction was encountered,\n"
5666 "but no persistent_load function was specified.");
5667 return -1;
5668 }
5669}
5670
5671static int
5672load_pop(UnpicklerObject *self)
5673{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005674 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005675
5676 /* Note that we split the (pickle.py) stack into two stacks,
5677 * an object stack and a mark stack. We have to be clever and
5678 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005679 * mark stack first, and only signalling a stack underflow if
5680 * the object stack is empty and the mark stack doesn't match
5681 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005683 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005685 self->stack->mark_set = self->num_marks != 0;
5686 self->stack->fence = self->num_marks ?
5687 self->marks[self->num_marks - 1] : 0;
5688 } else if (len <= self->stack->fence)
5689 return Pdata_stack_underflow(self->stack);
5690 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005691 len--;
5692 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005693 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005694 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005695 return 0;
5696}
5697
5698static int
5699load_pop_mark(UnpicklerObject *self)
5700{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005701 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005702
5703 if ((i = marker(self)) < 0)
5704 return -1;
5705
5706 Pdata_clear(self->stack, i);
5707
5708 return 0;
5709}
5710
5711static int
5712load_dup(UnpicklerObject *self)
5713{
5714 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005715 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005716
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005717 if (len <= self->stack->fence)
5718 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005719 last = self->stack->data[len - 1];
5720 PDATA_APPEND(self->stack, last, -1);
5721 return 0;
5722}
5723
5724static int
5725load_get(UnpicklerObject *self)
5726{
5727 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005728 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005729 Py_ssize_t len;
5730 char *s;
5731
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005732 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005733 return -1;
5734 if (len < 2)
5735 return bad_readline();
5736
5737 key = PyLong_FromString(s, NULL, 10);
5738 if (key == NULL)
5739 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005740 idx = PyLong_AsSsize_t(key);
5741 if (idx == -1 && PyErr_Occurred()) {
5742 Py_DECREF(key);
5743 return -1;
5744 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005745
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005746 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005747 if (value == NULL) {
5748 if (!PyErr_Occurred())
5749 PyErr_SetObject(PyExc_KeyError, key);
5750 Py_DECREF(key);
5751 return -1;
5752 }
5753 Py_DECREF(key);
5754
5755 PDATA_APPEND(self->stack, value, -1);
5756 return 0;
5757}
5758
5759static int
5760load_binget(UnpicklerObject *self)
5761{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005762 PyObject *value;
5763 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764 char *s;
5765
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767 return -1;
5768
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005769 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005770
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005771 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005772 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005773 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005774 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005775 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005776 Py_DECREF(key);
5777 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005778 return -1;
5779 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005780
5781 PDATA_APPEND(self->stack, value, -1);
5782 return 0;
5783}
5784
5785static int
5786load_long_binget(UnpicklerObject *self)
5787{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005788 PyObject *value;
5789 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005790 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005792 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793 return -1;
5794
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005795 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005796
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005797 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005798 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005799 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005800 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005801 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005802 Py_DECREF(key);
5803 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005804 return -1;
5805 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005806
5807 PDATA_APPEND(self->stack, value, -1);
5808 return 0;
5809}
5810
5811/* Push an object from the extension registry (EXT[124]). nbytes is
5812 * the number of bytes following the opcode, holding the index (code) value.
5813 */
5814static int
5815load_extension(UnpicklerObject *self, int nbytes)
5816{
5817 char *codebytes; /* the nbytes bytes after the opcode */
5818 long code; /* calc_binint returns long */
5819 PyObject *py_code; /* code as a Python int */
5820 PyObject *obj; /* the object to push */
5821 PyObject *pair; /* (module_name, class_name) */
5822 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005823 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005824
5825 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005826 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005827 return -1;
5828 code = calc_binint(codebytes, nbytes);
5829 if (code <= 0) { /* note that 0 is forbidden */
5830 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005831 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005832 return -1;
5833 }
5834
5835 /* Look for the code in the cache. */
5836 py_code = PyLong_FromLong(code);
5837 if (py_code == NULL)
5838 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005839 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005840 if (obj != NULL) {
5841 /* Bingo. */
5842 Py_DECREF(py_code);
5843 PDATA_APPEND(self->stack, obj, -1);
5844 return 0;
5845 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005846 if (PyErr_Occurred()) {
5847 Py_DECREF(py_code);
5848 return -1;
5849 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005850
5851 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005852 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005853 if (pair == NULL) {
5854 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005855 if (!PyErr_Occurred()) {
5856 PyErr_Format(PyExc_ValueError, "unregistered extension "
5857 "code %ld", code);
5858 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005859 return -1;
5860 }
5861 /* Since the extension registry is manipulable via Python code,
5862 * confirm that pair is really a 2-tuple of strings.
5863 */
Victor Stinnerb37672d2018-11-22 03:37:50 +01005864 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
5865 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005866 }
Victor Stinnerb37672d2018-11-22 03:37:50 +01005867
5868 module_name = PyTuple_GET_ITEM(pair, 0);
5869 if (!PyUnicode_Check(module_name)) {
5870 goto error;
5871 }
5872
5873 class_name = PyTuple_GET_ITEM(pair, 1);
5874 if (!PyUnicode_Check(class_name)) {
5875 goto error;
5876 }
5877
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005878 /* Load the object. */
5879 obj = find_class(self, module_name, class_name);
5880 if (obj == NULL) {
5881 Py_DECREF(py_code);
5882 return -1;
5883 }
5884 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005885 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005886 Py_DECREF(py_code);
5887 if (code < 0) {
5888 Py_DECREF(obj);
5889 return -1;
5890 }
5891 PDATA_PUSH(self->stack, obj, -1);
5892 return 0;
Victor Stinnerb37672d2018-11-22 03:37:50 +01005893
5894error:
5895 Py_DECREF(py_code);
5896 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5897 "isn't a 2-tuple of strings", code);
5898 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005899}
5900
5901static int
5902load_put(UnpicklerObject *self)
5903{
5904 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005905 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005906 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005907 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005908
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005909 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005910 return -1;
5911 if (len < 2)
5912 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005913 if (Py_SIZE(self->stack) <= self->stack->fence)
5914 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005915 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005916
5917 key = PyLong_FromString(s, NULL, 10);
5918 if (key == NULL)
5919 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005920 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005921 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005922 if (idx < 0) {
5923 if (!PyErr_Occurred())
5924 PyErr_SetString(PyExc_ValueError,
5925 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005926 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005927 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005928
5929 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005930}
5931
5932static int
5933load_binput(UnpicklerObject *self)
5934{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005935 PyObject *value;
5936 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005937 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005938
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005939 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005940 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005941
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005942 if (Py_SIZE(self->stack) <= self->stack->fence)
5943 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005944 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005945
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005946 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005947
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005948 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005949}
5950
5951static int
5952load_long_binput(UnpicklerObject *self)
5953{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005954 PyObject *value;
5955 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005956 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005957
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005958 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005959 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005960
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005961 if (Py_SIZE(self->stack) <= self->stack->fence)
5962 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005963 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005964
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005965 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005966 if (idx < 0) {
5967 PyErr_SetString(PyExc_ValueError,
5968 "negative LONG_BINPUT argument");
5969 return -1;
5970 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005971
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005972 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005973}
5974
5975static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005976load_memoize(UnpicklerObject *self)
5977{
5978 PyObject *value;
5979
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005980 if (Py_SIZE(self->stack) <= self->stack->fence)
5981 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005982 value = self->stack->data[Py_SIZE(self->stack) - 1];
5983
5984 return _Unpickler_MemoPut(self, self->memo_len, value);
5985}
5986
5987static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005988do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005989{
5990 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005991 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005992 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005993 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005994 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005995
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005996 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005997 if (x > len || x <= self->stack->fence)
5998 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005999 if (len == x) /* nothing to do */
6000 return 0;
6001
6002 list = self->stack->data[x - 1];
6003
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006004 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006005 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006006 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006007
6008 slice = Pdata_poplist(self->stack, x);
6009 if (!slice)
6010 return -1;
6011 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006012 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006013 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006014 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006015 }
6016 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006017 PyObject *extend_func;
6018 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006019
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006020 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6021 if (extend_func != NULL) {
6022 slice = Pdata_poplist(self->stack, x);
6023 if (!slice) {
6024 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006025 return -1;
6026 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006027 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006028 Py_DECREF(extend_func);
6029 if (result == NULL)
6030 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006031 Py_DECREF(result);
6032 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006033 else {
6034 PyObject *append_func;
6035 _Py_IDENTIFIER(append);
6036
6037 /* Even if the PEP 307 requires extend() and append() methods,
6038 fall back on append() if the object has no extend() method
6039 for backward compatibility. */
6040 PyErr_Clear();
6041 append_func = _PyObject_GetAttrId(list, &PyId_append);
6042 if (append_func == NULL)
6043 return -1;
6044 for (i = x; i < len; i++) {
6045 value = self->stack->data[i];
6046 result = _Pickle_FastCall(append_func, value);
6047 if (result == NULL) {
6048 Pdata_clear(self->stack, i + 1);
6049 Py_SIZE(self->stack) = x;
6050 Py_DECREF(append_func);
6051 return -1;
6052 }
6053 Py_DECREF(result);
6054 }
6055 Py_SIZE(self->stack) = x;
6056 Py_DECREF(append_func);
6057 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006058 }
6059
6060 return 0;
6061}
6062
6063static int
6064load_append(UnpicklerObject *self)
6065{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006066 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6067 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006068 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006069}
6070
6071static int
6072load_appends(UnpicklerObject *self)
6073{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006074 Py_ssize_t i = marker(self);
6075 if (i < 0)
6076 return -1;
6077 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006078}
6079
6080static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006081do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006082{
6083 PyObject *value, *key;
6084 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006085 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006086 int status = 0;
6087
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006088 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006089 if (x > len || x <= self->stack->fence)
6090 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006091 if (len == x) /* nothing to do */
6092 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006093 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006094 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006095 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006096 PyErr_SetString(st->UnpicklingError,
6097 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006098 return -1;
6099 }
6100
6101 /* Here, dict does not actually need to be a PyDict; it could be anything
6102 that supports the __setitem__ attribute. */
6103 dict = self->stack->data[x - 1];
6104
6105 for (i = x + 1; i < len; i += 2) {
6106 key = self->stack->data[i - 1];
6107 value = self->stack->data[i];
6108 if (PyObject_SetItem(dict, key, value) < 0) {
6109 status = -1;
6110 break;
6111 }
6112 }
6113
6114 Pdata_clear(self->stack, x);
6115 return status;
6116}
6117
6118static int
6119load_setitem(UnpicklerObject *self)
6120{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006121 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006122}
6123
6124static int
6125load_setitems(UnpicklerObject *self)
6126{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006127 Py_ssize_t i = marker(self);
6128 if (i < 0)
6129 return -1;
6130 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006131}
6132
6133static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006134load_additems(UnpicklerObject *self)
6135{
6136 PyObject *set;
6137 Py_ssize_t mark, len, i;
6138
6139 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006140 if (mark < 0)
6141 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006142 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006143 if (mark > len || mark <= self->stack->fence)
6144 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006145 if (len == mark) /* nothing to do */
6146 return 0;
6147
6148 set = self->stack->data[mark - 1];
6149
6150 if (PySet_Check(set)) {
6151 PyObject *items;
6152 int status;
6153
6154 items = Pdata_poptuple(self->stack, mark);
6155 if (items == NULL)
6156 return -1;
6157
6158 status = _PySet_Update(set, items);
6159 Py_DECREF(items);
6160 return status;
6161 }
6162 else {
6163 PyObject *add_func;
6164 _Py_IDENTIFIER(add);
6165
6166 add_func = _PyObject_GetAttrId(set, &PyId_add);
6167 if (add_func == NULL)
6168 return -1;
6169 for (i = mark; i < len; i++) {
6170 PyObject *result;
6171 PyObject *item;
6172
6173 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006174 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006175 if (result == NULL) {
6176 Pdata_clear(self->stack, i + 1);
6177 Py_SIZE(self->stack) = mark;
6178 return -1;
6179 }
6180 Py_DECREF(result);
6181 }
6182 Py_SIZE(self->stack) = mark;
6183 }
6184
6185 return 0;
6186}
6187
6188static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006189load_build(UnpicklerObject *self)
6190{
6191 PyObject *state, *inst, *slotstate;
6192 PyObject *setstate;
6193 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006194 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006195
6196 /* Stack is ... instance, state. We want to leave instance at
6197 * the stack top, possibly mutated via instance.__setstate__(state).
6198 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006199 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6200 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006201
6202 PDATA_POP(self->stack, state);
6203 if (state == NULL)
6204 return -1;
6205
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006206 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006208 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6209 Py_DECREF(state);
6210 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006211 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006212 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006213 PyObject *result;
6214
6215 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006216 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006217 Py_DECREF(setstate);
6218 if (result == NULL)
6219 return -1;
6220 Py_DECREF(result);
6221 return 0;
6222 }
6223
6224 /* A default __setstate__. First see whether state embeds a
6225 * slot state dict too (a proto 2 addition).
6226 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006227 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006228 PyObject *tmp = state;
6229
6230 state = PyTuple_GET_ITEM(tmp, 0);
6231 slotstate = PyTuple_GET_ITEM(tmp, 1);
6232 Py_INCREF(state);
6233 Py_INCREF(slotstate);
6234 Py_DECREF(tmp);
6235 }
6236 else
6237 slotstate = NULL;
6238
6239 /* Set inst.__dict__ from the state dict (if any). */
6240 if (state != Py_None) {
6241 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006242 PyObject *d_key, *d_value;
6243 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006244 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006245
6246 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006247 PickleState *st = _Pickle_GetGlobalState();
6248 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006249 goto error;
6250 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006251 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006252 if (dict == NULL)
6253 goto error;
6254
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006255 i = 0;
6256 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6257 /* normally the keys for instance attributes are
6258 interned. we should try to do that here. */
6259 Py_INCREF(d_key);
6260 if (PyUnicode_CheckExact(d_key))
6261 PyUnicode_InternInPlace(&d_key);
6262 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6263 Py_DECREF(d_key);
6264 goto error;
6265 }
6266 Py_DECREF(d_key);
6267 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006268 Py_DECREF(dict);
6269 }
6270
6271 /* Also set instance attributes from the slotstate dict (if any). */
6272 if (slotstate != NULL) {
6273 PyObject *d_key, *d_value;
6274 Py_ssize_t i;
6275
6276 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006277 PickleState *st = _Pickle_GetGlobalState();
6278 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006279 "slot state is not a dictionary");
6280 goto error;
6281 }
6282 i = 0;
6283 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6284 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6285 goto error;
6286 }
6287 }
6288
6289 if (0) {
6290 error:
6291 status = -1;
6292 }
6293
6294 Py_DECREF(state);
6295 Py_XDECREF(slotstate);
6296 return status;
6297}
6298
6299static int
6300load_mark(UnpicklerObject *self)
6301{
6302
6303 /* Note that we split the (pickle.py) stack into two stacks, an
6304 * object stack and a mark stack. Here we push a mark onto the
6305 * mark stack.
6306 */
6307
Sergey Fedoseev86b89912018-08-25 12:54:40 +05006308 if (self->num_marks >= self->marks_size) {
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006309 size_t alloc = ((size_t)self->num_marks << 1) + 20;
6310 Py_ssize_t *marks_new = self->marks;
6311 PyMem_RESIZE(marks_new, Py_ssize_t, alloc);
6312 if (marks_new == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006313 PyErr_NoMemory();
6314 return -1;
6315 }
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006316 self->marks = marks_new;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006317 self->marks_size = (Py_ssize_t)alloc;
6318 }
6319
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006320 self->stack->mark_set = 1;
6321 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006322
6323 return 0;
6324}
6325
6326static int
6327load_reduce(UnpicklerObject *self)
6328{
6329 PyObject *callable = NULL;
6330 PyObject *argtup = NULL;
6331 PyObject *obj = NULL;
6332
6333 PDATA_POP(self->stack, argtup);
6334 if (argtup == NULL)
6335 return -1;
6336 PDATA_POP(self->stack, callable);
6337 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006338 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006339 Py_DECREF(callable);
6340 }
6341 Py_DECREF(argtup);
6342
6343 if (obj == NULL)
6344 return -1;
6345
6346 PDATA_PUSH(self->stack, obj, -1);
6347 return 0;
6348}
6349
6350/* Just raises an error if we don't know the protocol specified. PROTO
6351 * is the first opcode for protocols >= 2.
6352 */
6353static int
6354load_proto(UnpicklerObject *self)
6355{
6356 char *s;
6357 int i;
6358
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006359 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006360 return -1;
6361
6362 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006363 if (i <= HIGHEST_PROTOCOL) {
6364 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006365 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006366 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006367
6368 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6369 return -1;
6370}
6371
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006372static int
6373load_frame(UnpicklerObject *self)
6374{
6375 char *s;
6376 Py_ssize_t frame_len;
6377
6378 if (_Unpickler_Read(self, &s, 8) < 0)
6379 return -1;
6380
6381 frame_len = calc_binsize(s, 8);
6382 if (frame_len < 0) {
6383 PyErr_Format(PyExc_OverflowError,
6384 "FRAME length exceeds system's maximum of %zd bytes",
6385 PY_SSIZE_T_MAX);
6386 return -1;
6387 }
6388
6389 if (_Unpickler_Read(self, &s, frame_len) < 0)
6390 return -1;
6391
6392 /* Rewind to start of frame */
6393 self->next_read_idx -= frame_len;
6394 return 0;
6395}
6396
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006397static PyObject *
6398load(UnpicklerObject *self)
6399{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006400 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006401 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006402
6403 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006404 self->stack->mark_set = 0;
6405 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006406 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006407 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006408 Pdata_clear(self->stack, 0);
6409
6410 /* Convenient macros for the dispatch while-switch loop just below. */
6411#define OP(opcode, load_func) \
6412 case opcode: if (load_func(self) < 0) break; continue;
6413
6414#define OP_ARG(opcode, load_func, arg) \
6415 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6416
6417 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006418 if (_Unpickler_Read(self, &s, 1) < 0) {
6419 PickleState *st = _Pickle_GetGlobalState();
6420 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6421 PyErr_Format(PyExc_EOFError, "Ran out of input");
6422 }
6423 return NULL;
6424 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006425
6426 switch ((enum opcode)s[0]) {
6427 OP(NONE, load_none)
6428 OP(BININT, load_binint)
6429 OP(BININT1, load_binint1)
6430 OP(BININT2, load_binint2)
6431 OP(INT, load_int)
6432 OP(LONG, load_long)
6433 OP_ARG(LONG1, load_counted_long, 1)
6434 OP_ARG(LONG4, load_counted_long, 4)
6435 OP(FLOAT, load_float)
6436 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006437 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6438 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6439 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6440 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6441 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006442 OP(STRING, load_string)
6443 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006444 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6445 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6446 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006447 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6448 OP_ARG(TUPLE1, load_counted_tuple, 1)
6449 OP_ARG(TUPLE2, load_counted_tuple, 2)
6450 OP_ARG(TUPLE3, load_counted_tuple, 3)
6451 OP(TUPLE, load_tuple)
6452 OP(EMPTY_LIST, load_empty_list)
6453 OP(LIST, load_list)
6454 OP(EMPTY_DICT, load_empty_dict)
6455 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006456 OP(EMPTY_SET, load_empty_set)
6457 OP(ADDITEMS, load_additems)
6458 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006459 OP(OBJ, load_obj)
6460 OP(INST, load_inst)
6461 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006462 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006463 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006464 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006465 OP(APPEND, load_append)
6466 OP(APPENDS, load_appends)
6467 OP(BUILD, load_build)
6468 OP(DUP, load_dup)
6469 OP(BINGET, load_binget)
6470 OP(LONG_BINGET, load_long_binget)
6471 OP(GET, load_get)
6472 OP(MARK, load_mark)
6473 OP(BINPUT, load_binput)
6474 OP(LONG_BINPUT, load_long_binput)
6475 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006476 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006477 OP(POP, load_pop)
6478 OP(POP_MARK, load_pop_mark)
6479 OP(SETITEM, load_setitem)
6480 OP(SETITEMS, load_setitems)
6481 OP(PERSID, load_persid)
6482 OP(BINPERSID, load_binpersid)
6483 OP(REDUCE, load_reduce)
6484 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006485 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006486 OP_ARG(EXT1, load_extension, 1)
6487 OP_ARG(EXT2, load_extension, 2)
6488 OP_ARG(EXT4, load_extension, 4)
6489 OP_ARG(NEWTRUE, load_bool, Py_True)
6490 OP_ARG(NEWFALSE, load_bool, Py_False)
6491
6492 case STOP:
6493 break;
6494
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006495 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006496 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006497 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006498 unsigned char c = (unsigned char) *s;
6499 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6500 PyErr_Format(st->UnpicklingError,
6501 "invalid load key, '%c'.", c);
6502 }
6503 else {
6504 PyErr_Format(st->UnpicklingError,
6505 "invalid load key, '\\x%02x'.", c);
6506 }
6507 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006508 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006509 }
6510
6511 break; /* and we are done! */
6512 }
6513
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006514 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006515 return NULL;
6516 }
6517
Victor Stinner2ae57e32013-10-31 13:39:23 +01006518 if (_Unpickler_SkipConsumed(self) < 0)
6519 return NULL;
6520
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006521 PDATA_POP(self->stack, value);
6522 return value;
6523}
6524
Larry Hastings61272b72014-01-07 12:41:53 -08006525/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006526
6527_pickle.Unpickler.load
6528
6529Load a pickle.
6530
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006531Read a pickled object representation from the open file object given
6532in the constructor, and return the reconstituted object hierarchy
6533specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006534[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006535
Larry Hastings3cceb382014-01-04 11:09:09 -08006536static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006537_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006538/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006540 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006541
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006542 /* Check whether the Unpickler was initialized correctly. This prevents
6543 segfaulting if a subclass overridden __init__ with a function that does
6544 not call Unpickler.__init__(). Here, we simply ensure that self->read
6545 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006546 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006547 PickleState *st = _Pickle_GetGlobalState();
6548 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006549 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006550 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006551 return NULL;
6552 }
6553
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006554 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006555}
6556
6557/* The name of find_class() is misleading. In newer pickle protocols, this
6558 function is used for loading any global (i.e., functions), not just
6559 classes. The name is kept only for backward compatibility. */
6560
Larry Hastings61272b72014-01-07 12:41:53 -08006561/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006562
6563_pickle.Unpickler.find_class
6564
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006565 module_name: object
6566 global_name: object
6567 /
6568
6569Return an object from a specified module.
6570
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006571If necessary, the module will be imported. Subclasses may override
6572this method (e.g. to restrict unpickling of arbitrary classes and
6573functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006574
6575This method is called whenever a class or a function object is
6576needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006577[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006578
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006579static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006580_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6581 PyObject *module_name,
6582 PyObject *global_name)
6583/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006584{
6585 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006586 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006587
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006588 /* Try to map the old names used in Python 2.x to the new ones used in
6589 Python 3.x. We do this only with old pickle protocols and when the
6590 user has not disabled the feature. */
6591 if (self->proto < 3 && self->fix_imports) {
6592 PyObject *key;
6593 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006594 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006595
6596 /* Check if the global (i.e., a function or a class) was renamed
6597 or moved to another module. */
6598 key = PyTuple_Pack(2, module_name, global_name);
6599 if (key == NULL)
6600 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006601 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006602 Py_DECREF(key);
6603 if (item) {
6604 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6605 PyErr_Format(PyExc_RuntimeError,
6606 "_compat_pickle.NAME_MAPPING values should be "
6607 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6608 return NULL;
6609 }
6610 module_name = PyTuple_GET_ITEM(item, 0);
6611 global_name = PyTuple_GET_ITEM(item, 1);
6612 if (!PyUnicode_Check(module_name) ||
6613 !PyUnicode_Check(global_name)) {
6614 PyErr_Format(PyExc_RuntimeError,
6615 "_compat_pickle.NAME_MAPPING values should be "
6616 "pairs of str, not (%.200s, %.200s)",
6617 Py_TYPE(module_name)->tp_name,
6618 Py_TYPE(global_name)->tp_name);
6619 return NULL;
6620 }
6621 }
6622 else if (PyErr_Occurred()) {
6623 return NULL;
6624 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006625 else {
6626 /* Check if the module was renamed. */
6627 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6628 if (item) {
6629 if (!PyUnicode_Check(item)) {
6630 PyErr_Format(PyExc_RuntimeError,
6631 "_compat_pickle.IMPORT_MAPPING values should be "
6632 "strings, not %.200s", Py_TYPE(item)->tp_name);
6633 return NULL;
6634 }
6635 module_name = item;
6636 }
6637 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006638 return NULL;
6639 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006640 }
6641 }
6642
Eric Snow3f9eee62017-09-15 16:35:20 -06006643 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006644 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006645 if (PyErr_Occurred())
6646 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006647 module = PyImport_Import(module_name);
6648 if (module == NULL)
6649 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006651 global = getattribute(module, global_name, self->proto >= 4);
6652 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006653 return global;
6654}
6655
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006656/*[clinic input]
6657
6658_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6659
6660Returns size in memory, in bytes.
6661[clinic start generated code]*/
6662
6663static Py_ssize_t
6664_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6665/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6666{
6667 Py_ssize_t res;
6668
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006669 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006670 if (self->memo != NULL)
6671 res += self->memo_size * sizeof(PyObject *);
6672 if (self->marks != NULL)
6673 res += self->marks_size * sizeof(Py_ssize_t);
6674 if (self->input_line != NULL)
6675 res += strlen(self->input_line) + 1;
6676 if (self->encoding != NULL)
6677 res += strlen(self->encoding) + 1;
6678 if (self->errors != NULL)
6679 res += strlen(self->errors) + 1;
6680 return res;
6681}
6682
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006683static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006684 _PICKLE_UNPICKLER_LOAD_METHODDEF
6685 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006686 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006687 {NULL, NULL} /* sentinel */
6688};
6689
6690static void
6691Unpickler_dealloc(UnpicklerObject *self)
6692{
6693 PyObject_GC_UnTrack((PyObject *)self);
6694 Py_XDECREF(self->readline);
6695 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006696 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006697 Py_XDECREF(self->stack);
6698 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006699 if (self->buffer.buf != NULL) {
6700 PyBuffer_Release(&self->buffer);
6701 self->buffer.buf = NULL;
6702 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006703
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006704 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006705 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006706 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006707 PyMem_Free(self->encoding);
6708 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006709
6710 Py_TYPE(self)->tp_free((PyObject *)self);
6711}
6712
6713static int
6714Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6715{
6716 Py_VISIT(self->readline);
6717 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006718 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006719 Py_VISIT(self->stack);
6720 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006721 return 0;
6722}
6723
6724static int
6725Unpickler_clear(UnpicklerObject *self)
6726{
6727 Py_CLEAR(self->readline);
6728 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006729 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006730 Py_CLEAR(self->stack);
6731 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006732 if (self->buffer.buf != NULL) {
6733 PyBuffer_Release(&self->buffer);
6734 self->buffer.buf = NULL;
6735 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006736
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006737 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006738 PyMem_Free(self->marks);
6739 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006740 PyMem_Free(self->input_line);
6741 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006742 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006743 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006744 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006745 self->errors = NULL;
6746
6747 return 0;
6748}
6749
Larry Hastings61272b72014-01-07 12:41:53 -08006750/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006751
6752_pickle.Unpickler.__init__
6753
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006754 file: object
6755 *
6756 fix_imports: bool = True
6757 encoding: str = 'ASCII'
6758 errors: str = 'strict'
6759
6760This takes a binary file for reading a pickle data stream.
6761
6762The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006763protocol argument is needed. Bytes past the pickled object's
6764representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006765
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006766The argument *file* must have two methods, a read() method that takes
6767an integer argument, and a readline() method that requires no
6768arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006769binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006770other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006771
6772Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006773which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006774generated by Python 2. If *fix_imports* is True, pickle will try to
6775map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006776*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006777instances pickled by Python 2; these default to 'ASCII' and 'strict',
6778respectively. The *encoding* can be 'bytes' to read these 8-bit
6779string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006780[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006781
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006782static int
Larry Hastings89964c42015-04-14 18:07:59 -04006783_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6784 int fix_imports, const char *encoding,
6785 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006786/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006787{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006788 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006789
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006790 /* In case of multiple __init__() calls, clear previous content. */
6791 if (self->read != NULL)
6792 (void)Unpickler_clear(self);
6793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006794 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006795 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006796
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006797 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006798 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006799
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006800 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006801
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006802 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6803 &self->pers_func, &self->pers_func_self) < 0)
6804 {
6805 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006806 }
6807
6808 self->stack = (Pdata *)Pdata_New();
6809 if (self->stack == NULL)
Zackery Spytz4b430e52018-09-28 23:48:46 -06006810 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006811
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006812 self->memo_size = 32;
6813 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006814 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006815 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006816
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006817 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006818
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006819 return 0;
6820}
6821
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006822
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006823/* Define a proxy object for the Unpickler's internal memo object. This is to
6824 * avoid breaking code like:
6825 * unpickler.memo.clear()
6826 * and
6827 * unpickler.memo = saved_memo
6828 * Is this a good idea? Not really, but we don't want to break code that uses
6829 * it. Note that we don't implement the entire mapping API here. This is
6830 * intentional, as these should be treated as black-box implementation details.
6831 *
6832 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006833 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006834 */
6835
Larry Hastings61272b72014-01-07 12:41:53 -08006836/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006837_pickle.UnpicklerMemoProxy.clear
6838
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006839Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006840[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006841
Larry Hastings3cceb382014-01-04 11:09:09 -08006842static PyObject *
6843_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006844/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006845{
6846 _Unpickler_MemoCleanup(self->unpickler);
6847 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6848 if (self->unpickler->memo == NULL)
6849 return NULL;
6850 Py_RETURN_NONE;
6851}
6852
Larry Hastings61272b72014-01-07 12:41:53 -08006853/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006854_pickle.UnpicklerMemoProxy.copy
6855
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006856Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006857[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006858
Larry Hastings3cceb382014-01-04 11:09:09 -08006859static PyObject *
6860_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006861/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006862{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07006863 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006864 PyObject *new_memo = PyDict_New();
6865 if (new_memo == NULL)
6866 return NULL;
6867
6868 for (i = 0; i < self->unpickler->memo_size; i++) {
6869 int status;
6870 PyObject *key, *value;
6871
6872 value = self->unpickler->memo[i];
6873 if (value == NULL)
6874 continue;
6875
6876 key = PyLong_FromSsize_t(i);
6877 if (key == NULL)
6878 goto error;
6879 status = PyDict_SetItem(new_memo, key, value);
6880 Py_DECREF(key);
6881 if (status < 0)
6882 goto error;
6883 }
6884 return new_memo;
6885
6886error:
6887 Py_DECREF(new_memo);
6888 return NULL;
6889}
6890
Larry Hastings61272b72014-01-07 12:41:53 -08006891/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006892_pickle.UnpicklerMemoProxy.__reduce__
6893
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006894Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006895[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006896
Larry Hastings3cceb382014-01-04 11:09:09 -08006897static PyObject *
6898_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006899/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006900{
6901 PyObject *reduce_value;
6902 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006903 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006904 if (contents == NULL)
6905 return NULL;
6906
6907 reduce_value = PyTuple_New(2);
6908 if (reduce_value == NULL) {
6909 Py_DECREF(contents);
6910 return NULL;
6911 }
6912 constructor_args = PyTuple_New(1);
6913 if (constructor_args == NULL) {
6914 Py_DECREF(contents);
6915 Py_DECREF(reduce_value);
6916 return NULL;
6917 }
6918 PyTuple_SET_ITEM(constructor_args, 0, contents);
6919 Py_INCREF((PyObject *)&PyDict_Type);
6920 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6921 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6922 return reduce_value;
6923}
6924
6925static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006926 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6927 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6928 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006929 {NULL, NULL} /* sentinel */
6930};
6931
6932static void
6933UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6934{
6935 PyObject_GC_UnTrack(self);
6936 Py_XDECREF(self->unpickler);
6937 PyObject_GC_Del((PyObject *)self);
6938}
6939
6940static int
6941UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6942 visitproc visit, void *arg)
6943{
6944 Py_VISIT(self->unpickler);
6945 return 0;
6946}
6947
6948static int
6949UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6950{
6951 Py_CLEAR(self->unpickler);
6952 return 0;
6953}
6954
6955static PyTypeObject UnpicklerMemoProxyType = {
6956 PyVarObject_HEAD_INIT(NULL, 0)
6957 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6958 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6959 0,
6960 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6961 0, /* tp_print */
6962 0, /* tp_getattr */
6963 0, /* tp_setattr */
6964 0, /* tp_compare */
6965 0, /* tp_repr */
6966 0, /* tp_as_number */
6967 0, /* tp_as_sequence */
6968 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006969 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006970 0, /* tp_call */
6971 0, /* tp_str */
6972 PyObject_GenericGetAttr, /* tp_getattro */
6973 PyObject_GenericSetAttr, /* tp_setattro */
6974 0, /* tp_as_buffer */
6975 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6976 0, /* tp_doc */
6977 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6978 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6979 0, /* tp_richcompare */
6980 0, /* tp_weaklistoffset */
6981 0, /* tp_iter */
6982 0, /* tp_iternext */
6983 unpicklerproxy_methods, /* tp_methods */
6984};
6985
6986static PyObject *
6987UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6988{
6989 UnpicklerMemoProxyObject *self;
6990
6991 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6992 &UnpicklerMemoProxyType);
6993 if (self == NULL)
6994 return NULL;
6995 Py_INCREF(unpickler);
6996 self->unpickler = unpickler;
6997 PyObject_GC_Track(self);
6998 return (PyObject *)self;
6999}
7000
7001/*****************************************************************************/
7002
7003
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007004static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007005Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007006{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007007 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007008}
7009
7010static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007011Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007012{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007013 PyObject **new_memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007014 size_t new_memo_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007015
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007016 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007017 PyErr_SetString(PyExc_TypeError,
7018 "attribute deletion is not supported");
7019 return -1;
7020 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007021
7022 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7023 UnpicklerObject *unpickler =
7024 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7025
7026 new_memo_size = unpickler->memo_size;
7027 new_memo = _Unpickler_NewMemo(new_memo_size);
7028 if (new_memo == NULL)
7029 return -1;
7030
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007031 for (size_t i = 0; i < new_memo_size; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007032 Py_XINCREF(unpickler->memo[i]);
7033 new_memo[i] = unpickler->memo[i];
7034 }
7035 }
7036 else if (PyDict_Check(obj)) {
7037 Py_ssize_t i = 0;
7038 PyObject *key, *value;
7039
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007040 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007041 new_memo = _Unpickler_NewMemo(new_memo_size);
7042 if (new_memo == NULL)
7043 return -1;
7044
7045 while (PyDict_Next(obj, &i, &key, &value)) {
7046 Py_ssize_t idx;
7047 if (!PyLong_Check(key)) {
7048 PyErr_SetString(PyExc_TypeError,
7049 "memo key must be integers");
7050 goto error;
7051 }
7052 idx = PyLong_AsSsize_t(key);
7053 if (idx == -1 && PyErr_Occurred())
7054 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007055 if (idx < 0) {
7056 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007057 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007058 goto error;
7059 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007060 if (_Unpickler_MemoPut(self, idx, value) < 0)
7061 goto error;
7062 }
7063 }
7064 else {
7065 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +02007066 "'memo' attribute must be an UnpicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007067 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007068 return -1;
7069 }
7070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007071 _Unpickler_MemoCleanup(self);
7072 self->memo_size = new_memo_size;
7073 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007074
7075 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007076
7077 error:
7078 if (new_memo_size) {
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007079 for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007080 Py_XDECREF(new_memo[i]);
7081 }
7082 PyMem_FREE(new_memo);
7083 }
7084 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007085}
7086
7087static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007088Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007089{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007090 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007091 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007092 return NULL;
7093 }
7094 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007095}
7096
7097static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007098Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007099{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007100 if (value == NULL) {
7101 PyErr_SetString(PyExc_TypeError,
7102 "attribute deletion is not supported");
7103 return -1;
7104 }
7105 if (!PyCallable_Check(value)) {
7106 PyErr_SetString(PyExc_TypeError,
7107 "persistent_load must be a callable taking "
7108 "one argument");
7109 return -1;
7110 }
7111
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007112 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007113 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007114 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007115
7116 return 0;
7117}
7118
7119static PyGetSetDef Unpickler_getsets[] = {
7120 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7121 {"persistent_load", (getter)Unpickler_get_persload,
7122 (setter)Unpickler_set_persload},
7123 {NULL}
7124};
7125
7126static PyTypeObject Unpickler_Type = {
7127 PyVarObject_HEAD_INIT(NULL, 0)
7128 "_pickle.Unpickler", /*tp_name*/
7129 sizeof(UnpicklerObject), /*tp_basicsize*/
7130 0, /*tp_itemsize*/
7131 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7132 0, /*tp_print*/
7133 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007134 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007135 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007136 0, /*tp_repr*/
7137 0, /*tp_as_number*/
7138 0, /*tp_as_sequence*/
7139 0, /*tp_as_mapping*/
7140 0, /*tp_hash*/
7141 0, /*tp_call*/
7142 0, /*tp_str*/
7143 0, /*tp_getattro*/
7144 0, /*tp_setattro*/
7145 0, /*tp_as_buffer*/
7146 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007147 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007148 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7149 (inquiry)Unpickler_clear, /*tp_clear*/
7150 0, /*tp_richcompare*/
7151 0, /*tp_weaklistoffset*/
7152 0, /*tp_iter*/
7153 0, /*tp_iternext*/
7154 Unpickler_methods, /*tp_methods*/
7155 0, /*tp_members*/
7156 Unpickler_getsets, /*tp_getset*/
7157 0, /*tp_base*/
7158 0, /*tp_dict*/
7159 0, /*tp_descr_get*/
7160 0, /*tp_descr_set*/
7161 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007162 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007163 PyType_GenericAlloc, /*tp_alloc*/
7164 PyType_GenericNew, /*tp_new*/
7165 PyObject_GC_Del, /*tp_free*/
7166 0, /*tp_is_gc*/
7167};
7168
Larry Hastings61272b72014-01-07 12:41:53 -08007169/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007170
7171_pickle.dump
7172
7173 obj: object
7174 file: object
7175 protocol: object = NULL
7176 *
7177 fix_imports: bool = True
7178
7179Write a pickled representation of obj to the open file object file.
7180
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007181This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7182be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007183
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007184The optional *protocol* argument tells the pickler to use the given
Łukasz Langac51d8c92018-04-03 23:06:53 -07007185protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7186protocol is 4. It was introduced in Python 3.4, it is incompatible
7187with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007189Specifying a negative protocol version selects the highest protocol
7190version supported. The higher the protocol used, the more recent the
7191version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007192
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007193The *file* argument must have a write() method that accepts a single
7194bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007195writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007196this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007197
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007198If *fix_imports* is True and protocol is less than 3, pickle will try
7199to map the new Python 3 names to the old module names used in Python
72002, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007201[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007202
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007203static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007204_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007205 PyObject *protocol, int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007206/*[clinic end generated code: output=a4774d5fde7d34de input=93f1408489a87472]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007207{
7208 PicklerObject *pickler = _Pickler_New();
7209
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007210 if (pickler == NULL)
7211 return NULL;
7212
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007213 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007214 goto error;
7215
7216 if (_Pickler_SetOutputStream(pickler, file) < 0)
7217 goto error;
7218
7219 if (dump(pickler, obj) < 0)
7220 goto error;
7221
7222 if (_Pickler_FlushToFile(pickler) < 0)
7223 goto error;
7224
7225 Py_DECREF(pickler);
7226 Py_RETURN_NONE;
7227
7228 error:
7229 Py_XDECREF(pickler);
7230 return NULL;
7231}
7232
Larry Hastings61272b72014-01-07 12:41:53 -08007233/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007234
7235_pickle.dumps
7236
7237 obj: object
7238 protocol: object = NULL
7239 *
7240 fix_imports: bool = True
7241
7242Return the pickled representation of the object as a bytes object.
7243
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007244The optional *protocol* argument tells the pickler to use the given
7245protocol; supported protocols are 0, 1, 2, 3 and 4. The default
Łukasz Langac51d8c92018-04-03 23:06:53 -07007246protocol is 4. It was introduced in Python 3.4, it is incompatible
7247with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007248
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007249Specifying a negative protocol version selects the highest protocol
7250version supported. The higher the protocol used, the more recent the
7251version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007252
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007253If *fix_imports* is True and *protocol* is less than 3, pickle will
7254try to map the new Python 3 names to the old module names used in
7255Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007256[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007257
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007258static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007259_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007260 int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007261/*[clinic end generated code: output=d75d5cda456fd261 input=b6efb45a7d19b5ab]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007262{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007263 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007264 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007265
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007266 if (pickler == NULL)
7267 return NULL;
7268
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007269 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007270 goto error;
7271
7272 if (dump(pickler, obj) < 0)
7273 goto error;
7274
7275 result = _Pickler_GetString(pickler);
7276 Py_DECREF(pickler);
7277 return result;
7278
7279 error:
7280 Py_XDECREF(pickler);
7281 return NULL;
7282}
7283
Larry Hastings61272b72014-01-07 12:41:53 -08007284/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007285
7286_pickle.load
7287
7288 file: object
7289 *
7290 fix_imports: bool = True
7291 encoding: str = 'ASCII'
7292 errors: str = 'strict'
7293
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007294Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007295
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007296This is equivalent to ``Unpickler(file).load()``, but may be more
7297efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007298
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007299The protocol version of the pickle is detected automatically, so no
7300protocol argument is needed. Bytes past the pickled object's
7301representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007302
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007303The argument *file* must have two methods, a read() method that takes
7304an integer argument, and a readline() method that requires no
7305arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007306binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007307other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007308
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007309Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007310which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007311generated by Python 2. If *fix_imports* is True, pickle will try to
7312map the old Python 2 names to the new names used in Python 3. The
7313*encoding* and *errors* tell pickle how to decode 8-bit string
7314instances pickled by Python 2; these default to 'ASCII' and 'strict',
7315respectively. The *encoding* can be 'bytes' to read these 8-bit
7316string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007317[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007318
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007319static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007320_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007321 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007322/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007323{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007324 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007325 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007326
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007327 if (unpickler == NULL)
7328 return NULL;
7329
7330 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7331 goto error;
7332
7333 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7334 goto error;
7335
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007336 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007337
7338 result = load(unpickler);
7339 Py_DECREF(unpickler);
7340 return result;
7341
7342 error:
7343 Py_XDECREF(unpickler);
7344 return NULL;
7345}
7346
Larry Hastings61272b72014-01-07 12:41:53 -08007347/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007348
7349_pickle.loads
7350
7351 data: object
7352 *
7353 fix_imports: bool = True
7354 encoding: str = 'ASCII'
7355 errors: str = 'strict'
7356
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007357Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007358
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007359The protocol version of the pickle is detected automatically, so no
7360protocol argument is needed. Bytes past the pickled object's
7361representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007362
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007363Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007364which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007365generated by Python 2. If *fix_imports* is True, pickle will try to
7366map the old Python 2 names to the new names used in Python 3. The
7367*encoding* and *errors* tell pickle how to decode 8-bit string
7368instances pickled by Python 2; these default to 'ASCII' and 'strict',
7369respectively. The *encoding* can be 'bytes' to read these 8-bit
7370string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007371[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007372
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007373static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007374_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007375 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007376/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007377{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007378 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007379 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007380
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007381 if (unpickler == NULL)
7382 return NULL;
7383
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007384 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007385 goto error;
7386
7387 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7388 goto error;
7389
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007390 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007391
7392 result = load(unpickler);
7393 Py_DECREF(unpickler);
7394 return result;
7395
7396 error:
7397 Py_XDECREF(unpickler);
7398 return NULL;
7399}
7400
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007401static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007402 _PICKLE_DUMP_METHODDEF
7403 _PICKLE_DUMPS_METHODDEF
7404 _PICKLE_LOAD_METHODDEF
7405 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007406 {NULL, NULL} /* sentinel */
7407};
7408
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007409static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007410pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007411{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007412 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007413 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007414}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007415
Stefan Krahf483b0f2013-12-14 13:43:10 +01007416static void
7417pickle_free(PyObject *m)
7418{
7419 _Pickle_ClearState(_Pickle_GetState(m));
7420}
7421
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007422static int
7423pickle_traverse(PyObject *m, visitproc visit, void *arg)
7424{
7425 PickleState *st = _Pickle_GetState(m);
7426 Py_VISIT(st->PickleError);
7427 Py_VISIT(st->PicklingError);
7428 Py_VISIT(st->UnpicklingError);
7429 Py_VISIT(st->dispatch_table);
7430 Py_VISIT(st->extension_registry);
7431 Py_VISIT(st->extension_cache);
7432 Py_VISIT(st->inverted_registry);
7433 Py_VISIT(st->name_mapping_2to3);
7434 Py_VISIT(st->import_mapping_2to3);
7435 Py_VISIT(st->name_mapping_3to2);
7436 Py_VISIT(st->import_mapping_3to2);
7437 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007438 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007439 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007440}
7441
7442static struct PyModuleDef _picklemodule = {
7443 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007444 "_pickle", /* m_name */
7445 pickle_module_doc, /* m_doc */
7446 sizeof(PickleState), /* m_size */
7447 pickle_methods, /* m_methods */
7448 NULL, /* m_reload */
7449 pickle_traverse, /* m_traverse */
7450 pickle_clear, /* m_clear */
7451 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007452};
7453
7454PyMODINIT_FUNC
7455PyInit__pickle(void)
7456{
7457 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007458 PickleState *st;
7459
7460 m = PyState_FindModule(&_picklemodule);
7461 if (m) {
7462 Py_INCREF(m);
7463 return m;
7464 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007465
7466 if (PyType_Ready(&Unpickler_Type) < 0)
7467 return NULL;
7468 if (PyType_Ready(&Pickler_Type) < 0)
7469 return NULL;
7470 if (PyType_Ready(&Pdata_Type) < 0)
7471 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007472 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7473 return NULL;
7474 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7475 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007476
7477 /* Create the module and add the functions. */
7478 m = PyModule_Create(&_picklemodule);
7479 if (m == NULL)
7480 return NULL;
7481
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007482 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007483 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7484 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007485 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007486 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7487 return NULL;
7488
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007489 st = _Pickle_GetState(m);
7490
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007491 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007492 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7493 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007494 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007495 st->PicklingError = \
7496 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7497 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007498 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007499 st->UnpicklingError = \
7500 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7501 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007502 return NULL;
7503
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007504 Py_INCREF(st->PickleError);
7505 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007506 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007507 Py_INCREF(st->PicklingError);
7508 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007509 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007510 Py_INCREF(st->UnpicklingError);
7511 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007512 return NULL;
7513
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007514 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007515 return NULL;
7516
7517 return m;
7518}