blob: 294f33419cb5b17f1f9692dcebd169741e6e1b9c [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 {
605 Py_ssize_t mt_mask;
606 Py_ssize_t mt_used;
607 Py_ssize_t mt_allocated;
608 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;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100653 Py_ssize_t memo_size; /* Capacity of the memo array */
654 Py_ssize_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{
740 Py_ssize_t i;
741 PyMemoTable *new = PyMemoTable_New();
742 if (new == NULL)
743 return NULL;
744
745 new->mt_used = self->mt_used;
746 new->mt_allocated = self->mt_allocated;
747 new->mt_mask = self->mt_mask;
748 /* The table we get from _New() is probably smaller than we wanted.
749 Free it and allocate one that's the right size. */
750 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500751 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000752 if (new->mt_table == NULL) {
753 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200754 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000755 return NULL;
756 }
757 for (i = 0; i < self->mt_allocated; i++) {
758 Py_XINCREF(self->mt_table[i].me_key);
759 }
760 memcpy(new->mt_table, self->mt_table,
761 sizeof(PyMemoEntry) * self->mt_allocated);
762
763 return new;
764}
765
766static Py_ssize_t
767PyMemoTable_Size(PyMemoTable *self)
768{
769 return self->mt_used;
770}
771
772static int
773PyMemoTable_Clear(PyMemoTable *self)
774{
775 Py_ssize_t i = self->mt_allocated;
776
777 while (--i >= 0) {
778 Py_XDECREF(self->mt_table[i].me_key);
779 }
780 self->mt_used = 0;
781 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
782 return 0;
783}
784
785static void
786PyMemoTable_Del(PyMemoTable *self)
787{
788 if (self == NULL)
789 return;
790 PyMemoTable_Clear(self);
791
792 PyMem_FREE(self->mt_table);
793 PyMem_FREE(self);
794}
795
796/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
797 can be considerably simpler than dictobject.c's lookdict(). */
798static PyMemoEntry *
799_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
800{
801 size_t i;
802 size_t perturb;
803 size_t mask = (size_t)self->mt_mask;
804 PyMemoEntry *table = self->mt_table;
805 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000806 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000807
808 i = hash & mask;
809 entry = &table[i];
810 if (entry->me_key == NULL || entry->me_key == key)
811 return entry;
812
813 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
814 i = (i << 2) + i + perturb + 1;
815 entry = &table[i & mask];
816 if (entry->me_key == NULL || entry->me_key == key)
817 return entry;
818 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700819 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000820}
821
822/* Returns -1 on failure, 0 on success. */
823static int
824_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
825{
826 PyMemoEntry *oldtable = NULL;
827 PyMemoEntry *oldentry, *newentry;
828 Py_ssize_t new_size = MT_MINSIZE;
829 Py_ssize_t to_process;
830
831 assert(min_size > 0);
832
833 /* Find the smallest valid table size >= min_size. */
834 while (new_size < min_size && new_size > 0)
835 new_size <<= 1;
836 if (new_size <= 0) {
837 PyErr_NoMemory();
838 return -1;
839 }
840 /* new_size needs to be a power of two. */
841 assert((new_size & (new_size - 1)) == 0);
842
843 /* Allocate new table. */
844 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500845 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000846 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200847 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000848 PyErr_NoMemory();
849 return -1;
850 }
851 self->mt_allocated = new_size;
852 self->mt_mask = new_size - 1;
853 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
854
855 /* Copy entries from the old table. */
856 to_process = self->mt_used;
857 for (oldentry = oldtable; to_process > 0; oldentry++) {
858 if (oldentry->me_key != NULL) {
859 to_process--;
860 /* newentry is a pointer to a chunk of the new
861 mt_table, so we're setting the key:value pair
862 in-place. */
863 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
864 newentry->me_key = oldentry->me_key;
865 newentry->me_value = oldentry->me_value;
866 }
867 }
868
869 /* Deallocate the old table. */
870 PyMem_FREE(oldtable);
871 return 0;
872}
873
874/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200875static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000876PyMemoTable_Get(PyMemoTable *self, PyObject *key)
877{
878 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
879 if (entry->me_key == NULL)
880 return NULL;
881 return &entry->me_value;
882}
883
884/* Returns -1 on failure, 0 on success. */
885static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200886PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000887{
888 PyMemoEntry *entry;
889
890 assert(key != NULL);
891
892 entry = _PyMemoTable_Lookup(self, key);
893 if (entry->me_key != NULL) {
894 entry->me_value = value;
895 return 0;
896 }
897 Py_INCREF(key);
898 entry->me_key = key;
899 entry->me_value = value;
900 self->mt_used++;
901
902 /* If we added a key, we can safely resize. Otherwise just return!
903 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
904 *
905 * Quadrupling the size improves average table sparseness
906 * (reducing collisions) at the cost of some memory. It also halves
907 * the number of expensive resize operations in a growing memo table.
908 *
909 * Very large memo tables (over 50K items) use doubling instead.
910 * This may help applications with severe memory constraints.
911 */
912 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
913 return 0;
914 return _PyMemoTable_ResizeTable(self,
915 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
916}
917
918#undef MT_MINSIZE
919#undef PERTURB_SHIFT
920
921/*************************************************************************/
922
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000923
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000924static int
925_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000926{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300927 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200928 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000929 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000930 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000931 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100932 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000933 return 0;
934}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000935
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100936static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100937_write_size64(char *out, size_t value)
938{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200939 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800940
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200941 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800942
943 for (i = 0; i < sizeof(size_t); i++) {
944 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
945 }
946 for (i = sizeof(size_t); i < 8; i++) {
947 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800948 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100949}
950
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100951static int
952_Pickler_CommitFrame(PicklerObject *self)
953{
954 size_t frame_len;
955 char *qdata;
956
957 if (!self->framing || self->frame_start == -1)
958 return 0;
959 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
960 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200961 if (frame_len >= FRAME_SIZE_MIN) {
962 qdata[0] = FRAME;
963 _write_size64(qdata + 1, frame_len);
964 }
965 else {
966 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
967 self->output_len -= FRAME_HEADER_SIZE;
968 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100969 self->frame_start = -1;
970 return 0;
971}
972
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000973static PyObject *
974_Pickler_GetString(PicklerObject *self)
975{
976 PyObject *output_buffer = self->output_buffer;
977
978 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100979
980 if (_Pickler_CommitFrame(self))
981 return NULL;
982
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000983 self->output_buffer = NULL;
984 /* Resize down to exact size */
985 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
986 return NULL;
987 return output_buffer;
988}
989
990static int
991_Pickler_FlushToFile(PicklerObject *self)
992{
993 PyObject *output, *result;
994
995 assert(self->write != NULL);
996
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100997 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000998 output = _Pickler_GetString(self);
999 if (output == NULL)
1000 return -1;
1001
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001002 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001003 Py_XDECREF(result);
1004 return (result == NULL) ? -1 : 0;
1005}
1006
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001007static int
1008_Pickler_OpcodeBoundary(PicklerObject *self)
1009{
1010 Py_ssize_t frame_len;
1011
1012 if (!self->framing || self->frame_start == -1) {
1013 return 0;
1014 }
1015 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1016 if (frame_len >= FRAME_SIZE_TARGET) {
1017 if(_Pickler_CommitFrame(self)) {
1018 return -1;
1019 }
Leo Ariasc3d95082018-02-03 18:36:10 -06001020 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001021 * file and reuse the pickler buffer for the next frame so as
1022 * to limit memory usage when dumping large complex objects to
1023 * a file.
1024 *
1025 * self->write is NULL when called via dumps.
1026 */
1027 if (self->write != NULL) {
1028 if (_Pickler_FlushToFile(self) < 0) {
1029 return -1;
1030 }
1031 if (_Pickler_ClearBuffer(self) < 0) {
1032 return -1;
1033 }
1034 }
1035 }
1036 return 0;
1037}
1038
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001039static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001040_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001042 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001043 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001044 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001045
1046 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001047 need_new_frame = (self->framing && self->frame_start == -1);
1048
1049 if (need_new_frame)
1050 n = data_len + FRAME_HEADER_SIZE;
1051 else
1052 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001053
1054 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001055 if (required > self->max_output_len) {
1056 /* Make place in buffer for the pickle chunk */
1057 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1058 PyErr_NoMemory();
1059 return -1;
1060 }
1061 self->max_output_len = (self->output_len + n) / 2 * 3;
1062 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1063 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001064 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001065 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001066 if (need_new_frame) {
1067 /* Setup new frame */
1068 Py_ssize_t frame_start = self->output_len;
1069 self->frame_start = frame_start;
1070 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1071 /* Write an invalid value, for debugging */
1072 buffer[frame_start + i] = 0xFE;
1073 }
1074 self->output_len += FRAME_HEADER_SIZE;
1075 }
1076 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001077 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001078 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001079 buffer[self->output_len + i] = s[i];
1080 }
1081 }
1082 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001083 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001084 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001085 self->output_len += data_len;
1086 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001087}
1088
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001089static PicklerObject *
1090_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001091{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001092 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001093
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001094 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1095 if (self == NULL)
1096 return NULL;
1097
1098 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001099 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001100 self->write = NULL;
1101 self->proto = 0;
1102 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001103 self->framing = 0;
1104 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001105 self->fast = 0;
1106 self->fast_nesting = 0;
1107 self->fix_imports = 0;
1108 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001109 self->max_output_len = WRITE_BUF_SIZE;
1110 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001111
1112 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001113 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1114 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001115
1116 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001117 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001118 return NULL;
1119 }
1120 return self;
1121}
1122
1123static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001124_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001125{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001126 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001127
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001128 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001129 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001130 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001131 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001132 proto = PyLong_AsLong(protocol);
1133 if (proto < 0) {
1134 if (proto == -1 && PyErr_Occurred())
1135 return -1;
1136 proto = HIGHEST_PROTOCOL;
1137 }
1138 else if (proto > HIGHEST_PROTOCOL) {
1139 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1140 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001141 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001142 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001143 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001144 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001145 self->bin = proto > 0;
1146 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001147 return 0;
1148}
1149
1150/* Returns -1 (with an exception set) on failure, 0 on success. This may
1151 be called once on a freshly created Pickler. */
1152static int
1153_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1154{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001155 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001156 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001157 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1158 return -1;
1159 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001160 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001161 PyErr_SetString(PyExc_TypeError,
1162 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001163 return -1;
1164 }
1165
1166 return 0;
1167}
1168
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001169/* Returns the size of the input on success, -1 on failure. This takes its
1170 own reference to `input`. */
1171static Py_ssize_t
1172_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1173{
1174 if (self->buffer.buf != NULL)
1175 PyBuffer_Release(&self->buffer);
1176 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1177 return -1;
1178 self->input_buffer = self->buffer.buf;
1179 self->input_len = self->buffer.len;
1180 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001181 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001182 return self->input_len;
1183}
1184
Antoine Pitrou04248a82010-10-12 20:51:21 +00001185static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001186bad_readline(void)
1187{
1188 PickleState *st = _Pickle_GetGlobalState();
1189 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1190 return -1;
1191}
1192
1193static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001194_Unpickler_SkipConsumed(UnpicklerObject *self)
1195{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001196 Py_ssize_t consumed;
1197 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001198
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001199 consumed = self->next_read_idx - self->prefetched_idx;
1200 if (consumed <= 0)
1201 return 0;
1202
1203 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001204 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001205 r = PyObject_CallFunction(self->read, "n", consumed);
1206 if (r == NULL)
1207 return -1;
1208 Py_DECREF(r);
1209
1210 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001211 return 0;
1212}
1213
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001214static const Py_ssize_t READ_WHOLE_LINE = -1;
1215
1216/* If reading from a file, we need to only pull the bytes we need, since there
1217 may be multiple pickle objects arranged contiguously in the same input
1218 buffer.
1219
1220 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1221 bytes from the input stream/buffer.
1222
1223 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1224 failure; on success, returns the number of bytes read from the file.
1225
1226 On success, self->input_len will be 0; this is intentional so that when
1227 unpickling from a file, the "we've run out of data" code paths will trigger,
1228 causing the Unpickler to go back to the file for more data. Use the returned
1229 size to tell you how much data you can process. */
1230static Py_ssize_t
1231_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1232{
1233 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001234 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001235
1236 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001237
Antoine Pitrou04248a82010-10-12 20:51:21 +00001238 if (_Unpickler_SkipConsumed(self) < 0)
1239 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001240
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001241 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001242 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001243 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001244 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001245 PyObject *len;
1246 /* Prefetch some data without advancing the file pointer, if possible */
1247 if (self->peek && n < PREFETCH) {
1248 len = PyLong_FromSsize_t(PREFETCH);
1249 if (len == NULL)
1250 return -1;
1251 data = _Pickle_FastCall(self->peek, len);
1252 if (data == NULL) {
1253 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1254 return -1;
1255 /* peek() is probably not supported by the given file object */
1256 PyErr_Clear();
1257 Py_CLEAR(self->peek);
1258 }
1259 else {
1260 read_size = _Unpickler_SetStringInput(self, data);
1261 Py_DECREF(data);
1262 self->prefetched_idx = 0;
1263 if (n <= read_size)
1264 return n;
1265 }
1266 }
1267 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001268 if (len == NULL)
1269 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001270 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001271 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001272 if (data == NULL)
1273 return -1;
1274
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001275 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001276 Py_DECREF(data);
1277 return read_size;
1278}
1279
Victor Stinner19ed27e2016-05-20 11:42:37 +02001280/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001281static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001282_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001283{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001284 Py_ssize_t num_read;
1285
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001286 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001287 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1288 PickleState *st = _Pickle_GetGlobalState();
1289 PyErr_SetString(st->UnpicklingError,
1290 "read would overflow (invalid bytecode)");
1291 return -1;
1292 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001293
1294 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1295 assert(self->next_read_idx + n > self->input_len);
1296
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001297 if (!self->read)
1298 return bad_readline();
1299
Antoine Pitrou04248a82010-10-12 20:51:21 +00001300 num_read = _Unpickler_ReadFromFile(self, n);
1301 if (num_read < 0)
1302 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001303 if (num_read < n)
1304 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001305 *s = self->input_buffer;
1306 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001307 return n;
1308}
1309
Victor Stinner19ed27e2016-05-20 11:42:37 +02001310/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1311
1312 This should be used for all data reads, rather than accessing the unpickler's
1313 input buffer directly. This method deals correctly with reading from input
1314 streams, which the input buffer doesn't deal with.
1315
1316 Note that when reading from a file-like object, self->next_read_idx won't
1317 be updated (it should remain at 0 for the entire unpickling process). You
1318 should use this function's return value to know how many bytes you can
1319 consume.
1320
1321 Returns -1 (with an exception set) on failure. On success, return the
1322 number of chars read. */
1323#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001324 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001325 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1326 (self)->next_read_idx += (n), \
1327 (n)) \
1328 : _Unpickler_ReadImpl(self, (s), (n)))
1329
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001330static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001331_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1332 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001333{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001334 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001335 if (input_line == NULL) {
1336 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001337 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001338 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001339
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001340 memcpy(input_line, line, len);
1341 input_line[len] = '\0';
1342 self->input_line = input_line;
1343 *result = self->input_line;
1344 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001345}
1346
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001347/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001348 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001349
1350 Returns the number of chars read, or -1 on failure. */
1351static Py_ssize_t
1352_Unpickler_Readline(UnpicklerObject *self, char **result)
1353{
1354 Py_ssize_t i, num_read;
1355
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001356 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001357 if (self->input_buffer[i] == '\n') {
1358 char *line_start = self->input_buffer + self->next_read_idx;
1359 num_read = i - self->next_read_idx + 1;
1360 self->next_read_idx = i + 1;
1361 return _Unpickler_CopyLine(self, line_start, num_read, result);
1362 }
1363 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001364 if (!self->read)
1365 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001366
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001367 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1368 if (num_read < 0)
1369 return -1;
1370 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1371 return bad_readline();
1372 self->next_read_idx = num_read;
1373 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001374}
1375
1376/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1377 will be modified in place. */
1378static int
1379_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1380{
1381 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001382
1383 assert(new_size > self->memo_size);
1384
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001385 PyMem_RESIZE(self->memo, PyObject *, new_size);
1386 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001387 PyErr_NoMemory();
1388 return -1;
1389 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001390 for (i = self->memo_size; i < new_size; i++)
1391 self->memo[i] = NULL;
1392 self->memo_size = new_size;
1393 return 0;
1394}
1395
1396/* Returns NULL if idx is out of bounds. */
1397static PyObject *
1398_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1399{
1400 if (idx < 0 || idx >= self->memo_size)
1401 return NULL;
1402
1403 return self->memo[idx];
1404}
1405
1406/* Returns -1 (with an exception set) on failure, 0 on success.
1407 This takes its own reference to `value`. */
1408static int
1409_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1410{
1411 PyObject *old_item;
1412
1413 if (idx >= self->memo_size) {
1414 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1415 return -1;
1416 assert(idx < self->memo_size);
1417 }
1418 Py_INCREF(value);
1419 old_item = self->memo[idx];
1420 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001421 if (old_item != NULL) {
1422 Py_DECREF(old_item);
1423 }
1424 else {
1425 self->memo_len++;
1426 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001427 return 0;
1428}
1429
1430static PyObject **
1431_Unpickler_NewMemo(Py_ssize_t new_size)
1432{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001433 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001434 if (memo == NULL) {
1435 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001436 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001437 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001438 memset(memo, 0, new_size * sizeof(PyObject *));
1439 return memo;
1440}
1441
1442/* Free the unpickler's memo, taking care to decref any items left in it. */
1443static void
1444_Unpickler_MemoCleanup(UnpicklerObject *self)
1445{
1446 Py_ssize_t i;
1447 PyObject **memo = self->memo;
1448
1449 if (self->memo == NULL)
1450 return;
1451 self->memo = NULL;
1452 i = self->memo_size;
1453 while (--i >= 0) {
1454 Py_XDECREF(memo[i]);
1455 }
1456 PyMem_FREE(memo);
1457}
1458
1459static UnpicklerObject *
1460_Unpickler_New(void)
1461{
1462 UnpicklerObject *self;
1463
1464 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1465 if (self == NULL)
1466 return NULL;
1467
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001468 self->pers_func = NULL;
1469 self->input_buffer = NULL;
1470 self->input_line = NULL;
1471 self->input_len = 0;
1472 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001473 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001474 self->read = NULL;
1475 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001476 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001477 self->encoding = NULL;
1478 self->errors = NULL;
1479 self->marks = NULL;
1480 self->num_marks = 0;
1481 self->marks_size = 0;
1482 self->proto = 0;
1483 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001484 memset(&self->buffer, 0, sizeof(Py_buffer));
1485 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001486 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001487 self->memo = _Unpickler_NewMemo(self->memo_size);
1488 self->stack = (Pdata *)Pdata_New();
1489
1490 if (self->memo == NULL || self->stack == NULL) {
1491 Py_DECREF(self);
1492 return NULL;
1493 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001494
1495 return self;
1496}
1497
1498/* Returns -1 (with an exception set) on failure, 0 on success. This may
1499 be called once on a freshly created Pickler. */
1500static int
1501_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1502{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001503 _Py_IDENTIFIER(peek);
1504 _Py_IDENTIFIER(read);
1505 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001506
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001507 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1508 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001509 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001510 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1511 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001512 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001513 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001514 PyErr_SetString(PyExc_TypeError,
1515 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001516 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001517 Py_CLEAR(self->read);
1518 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001519 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001520 return -1;
1521 }
1522 return 0;
1523}
1524
1525/* Returns -1 (with an exception set) on failure, 0 on success. This may
1526 be called once on a freshly created Pickler. */
1527static int
1528_Unpickler_SetInputEncoding(UnpicklerObject *self,
1529 const char *encoding,
1530 const char *errors)
1531{
1532 if (encoding == NULL)
1533 encoding = "ASCII";
1534 if (errors == NULL)
1535 errors = "strict";
1536
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001537 self->encoding = _PyMem_Strdup(encoding);
1538 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001539 if (self->encoding == NULL || self->errors == NULL) {
1540 PyErr_NoMemory();
1541 return -1;
1542 }
1543 return 0;
1544}
1545
1546/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001547static int
1548memo_get(PicklerObject *self, PyObject *key)
1549{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001550 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001551 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001552 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001553
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001554 value = PyMemoTable_Get(self->memo, key);
1555 if (value == NULL) {
1556 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001557 return -1;
1558 }
1559
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001560 if (!self->bin) {
1561 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001562 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1563 "%" PY_FORMAT_SIZE_T "d\n", *value);
1564 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001565 }
1566 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001567 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001568 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001569 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001570 len = 2;
1571 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001572 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001573 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001574 pdata[1] = (unsigned char)(*value & 0xff);
1575 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1576 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1577 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001578 len = 5;
1579 }
1580 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001581 PickleState *st = _Pickle_GetGlobalState();
1582 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001583 "memo id too large for LONG_BINGET");
1584 return -1;
1585 }
1586 }
1587
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001588 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001589 return -1;
1590
1591 return 0;
1592}
1593
1594/* Store an object in the memo, assign it a new unique ID based on the number
1595 of objects currently stored in the memo and generate a PUT opcode. */
1596static int
1597memo_put(PicklerObject *self, PyObject *obj)
1598{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001599 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001600 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001601 Py_ssize_t idx;
1602
1603 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001604
1605 if (self->fast)
1606 return 0;
1607
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001608 idx = PyMemoTable_Size(self->memo);
1609 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1610 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001611
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001612 if (self->proto >= 4) {
1613 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1614 return -1;
1615 return 0;
1616 }
1617 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001618 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001619 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001620 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001621 len = strlen(pdata);
1622 }
1623 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001624 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001625 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001626 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001627 len = 2;
1628 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001629 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001630 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001631 pdata[1] = (unsigned char)(idx & 0xff);
1632 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1633 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1634 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001635 len = 5;
1636 }
1637 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001638 PickleState *st = _Pickle_GetGlobalState();
1639 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001640 "memo id too large for LONG_BINPUT");
1641 return -1;
1642 }
1643 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001644 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001645 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001646
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001647 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001648}
1649
1650static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001651get_dotted_path(PyObject *obj, PyObject *name)
1652{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001653 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001654 PyObject *dotted_path;
1655 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001656
1657 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001658 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001659 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001660 n = PyList_GET_SIZE(dotted_path);
1661 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001662 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001663 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001664 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001665 if (obj == NULL)
1666 PyErr_Format(PyExc_AttributeError,
1667 "Can't pickle local object %R", name);
1668 else
1669 PyErr_Format(PyExc_AttributeError,
1670 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001671 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001672 return NULL;
1673 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001674 }
1675 return dotted_path;
1676}
1677
1678static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001679get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001680{
1681 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001682 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001683
1684 assert(PyList_CheckExact(names));
1685 Py_INCREF(obj);
1686 n = PyList_GET_SIZE(names);
1687 for (i = 0; i < n; i++) {
1688 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001689 Py_XDECREF(parent);
1690 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001691 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001692 if (obj == NULL) {
1693 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001694 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001695 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001696 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001697 if (pparent != NULL)
1698 *pparent = parent;
1699 else
1700 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001701 return obj;
1702}
1703
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001704
1705static PyObject *
1706getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1707{
1708 PyObject *dotted_path, *attr;
1709
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001710 if (allow_qualname) {
1711 dotted_path = get_dotted_path(obj, name);
1712 if (dotted_path == NULL)
1713 return NULL;
1714 attr = get_deep_attribute(obj, dotted_path, NULL);
1715 Py_DECREF(dotted_path);
1716 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001717 else {
1718 (void)_PyObject_LookupAttr(obj, name, &attr);
1719 }
1720 if (attr == NULL && !PyErr_Occurred()) {
1721 PyErr_Format(PyExc_AttributeError,
1722 "Can't get attribute %R on %R", name, obj);
1723 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001724 return attr;
1725}
1726
Eric Snow3f9eee62017-09-15 16:35:20 -06001727static int
1728_checkmodule(PyObject *module_name, PyObject *module,
1729 PyObject *global, PyObject *dotted_path)
1730{
1731 if (module == Py_None) {
1732 return -1;
1733 }
1734 if (PyUnicode_Check(module_name) &&
1735 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1736 return -1;
1737 }
1738
1739 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1740 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001741 return -1;
1742 }
1743 if (candidate != global) {
1744 Py_DECREF(candidate);
1745 return -1;
1746 }
1747 Py_DECREF(candidate);
1748 return 0;
1749}
1750
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001751static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001752whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001753{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001754 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001755 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001756 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001757 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001758 _Py_IDENTIFIER(__module__);
1759 _Py_IDENTIFIER(modules);
1760 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001761
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001762 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1763 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001764 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001765 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001766 /* In some rare cases (e.g., bound methods of extension types),
1767 __module__ can be None. If it is so, then search sys.modules for
1768 the module of global. */
1769 if (module_name != Py_None)
1770 return module_name;
1771 Py_CLEAR(module_name);
1772 }
1773 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001774
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001775 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001776 modules = _PySys_GetObjectId(&PyId_modules);
1777 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001778 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001779 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001780 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001781 if (PyDict_CheckExact(modules)) {
1782 i = 0;
1783 while (PyDict_Next(modules, &i, &module_name, &module)) {
1784 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1785 Py_INCREF(module_name);
1786 return module_name;
1787 }
1788 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001789 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001790 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001791 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001792 }
1793 else {
1794 PyObject *iterator = PyObject_GetIter(modules);
1795 if (iterator == NULL) {
1796 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001797 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001798 while ((module_name = PyIter_Next(iterator))) {
1799 module = PyObject_GetItem(modules, module_name);
1800 if (module == NULL) {
1801 Py_DECREF(module_name);
1802 Py_DECREF(iterator);
1803 return NULL;
1804 }
1805 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1806 Py_DECREF(module);
1807 Py_DECREF(iterator);
1808 return module_name;
1809 }
1810 Py_DECREF(module);
1811 Py_DECREF(module_name);
1812 if (PyErr_Occurred()) {
1813 Py_DECREF(iterator);
1814 return NULL;
1815 }
1816 }
1817 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001818 }
1819
1820 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001821 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001822 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001823 return module_name;
1824}
1825
1826/* fast_save_enter() and fast_save_leave() are guards against recursive
1827 objects when Pickler is used with the "fast mode" (i.e., with object
1828 memoization disabled). If the nesting of a list or dict object exceed
1829 FAST_NESTING_LIMIT, these guards will start keeping an internal
1830 reference to the seen list or dict objects and check whether these objects
1831 are recursive. These are not strictly necessary, since save() has a
1832 hard-coded recursion limit, but they give a nicer error message than the
1833 typical RuntimeError. */
1834static int
1835fast_save_enter(PicklerObject *self, PyObject *obj)
1836{
1837 /* if fast_nesting < 0, we're doing an error exit. */
1838 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1839 PyObject *key = NULL;
1840 if (self->fast_memo == NULL) {
1841 self->fast_memo = PyDict_New();
1842 if (self->fast_memo == NULL) {
1843 self->fast_nesting = -1;
1844 return 0;
1845 }
1846 }
1847 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001848 if (key == NULL) {
1849 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001850 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001851 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001852 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001853 Py_DECREF(key);
1854 PyErr_Format(PyExc_ValueError,
1855 "fast mode: can't pickle cyclic objects "
1856 "including object type %.200s at %p",
1857 obj->ob_type->tp_name, obj);
1858 self->fast_nesting = -1;
1859 return 0;
1860 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001861 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001862 Py_DECREF(key);
1863 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001864 return 0;
1865 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001866 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1867 Py_DECREF(key);
1868 self->fast_nesting = -1;
1869 return 0;
1870 }
1871 Py_DECREF(key);
1872 }
1873 return 1;
1874}
1875
1876static int
1877fast_save_leave(PicklerObject *self, PyObject *obj)
1878{
1879 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1880 PyObject *key = PyLong_FromVoidPtr(obj);
1881 if (key == NULL)
1882 return 0;
1883 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1884 Py_DECREF(key);
1885 return 0;
1886 }
1887 Py_DECREF(key);
1888 }
1889 return 1;
1890}
1891
1892static int
1893save_none(PicklerObject *self, PyObject *obj)
1894{
1895 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001896 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001897 return -1;
1898
1899 return 0;
1900}
1901
1902static int
1903save_bool(PicklerObject *self, PyObject *obj)
1904{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001905 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001906 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001907 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001908 return -1;
1909 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001910 else {
1911 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1912 * so that unpicklers written before bools were introduced unpickle them
1913 * as ints, but unpicklers after can recognize that bools were intended.
1914 * Note that protocol 2 added direct ways to pickle bools.
1915 */
1916 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1917 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1918 return -1;
1919 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001920 return 0;
1921}
1922
1923static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001924save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001925{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001926 PyObject *repr = NULL;
1927 Py_ssize_t size;
1928 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001929 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001930 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001931
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001932 val= PyLong_AsLongAndOverflow(obj, &overflow);
1933 if (!overflow && (sizeof(long) <= 4 ||
1934 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1935 {
Larry Hastings61272b72014-01-07 12:41:53 -08001936 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001937
1938 Note: we can't use -0x80000000L in the above condition because some
1939 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1940 before applying the unary minus when sizeof(long) <= 4. The
1941 resulting value stays unsigned which is commonly not what we want,
1942 so MSVC happily warns us about it. However, that result would have
1943 been fine because we guard for sizeof(long) <= 4 which turns the
1944 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001945 char pdata[32];
1946 Py_ssize_t len = 0;
1947
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001948 if (self->bin) {
1949 pdata[1] = (unsigned char)(val & 0xff);
1950 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1951 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1952 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001953
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001954 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1955 pdata[0] = BININT;
1956 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001957 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001958 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001959 pdata[0] = BININT2;
1960 len = 3;
1961 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001962 else {
1963 pdata[0] = BININT1;
1964 len = 2;
1965 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001966 }
1967 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001968 sprintf(pdata, "%c%ld\n", INT, val);
1969 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001970 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001971 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001972 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001973
1974 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001975 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001976 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001977
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001978 if (self->proto >= 2) {
1979 /* Linear-time pickling. */
1980 size_t nbits;
1981 size_t nbytes;
1982 unsigned char *pdata;
1983 char header[5];
1984 int i;
1985 int sign = _PyLong_Sign(obj);
1986
1987 if (sign == 0) {
1988 header[0] = LONG1;
1989 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001990 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001991 goto error;
1992 return 0;
1993 }
1994 nbits = _PyLong_NumBits(obj);
1995 if (nbits == (size_t)-1 && PyErr_Occurred())
1996 goto error;
1997 /* How many bytes do we need? There are nbits >> 3 full
1998 * bytes of data, and nbits & 7 leftover bits. If there
1999 * are any leftover bits, then we clearly need another
2000 * byte. Wnat's not so obvious is that we *probably*
2001 * need another byte even if there aren't any leftovers:
2002 * the most-significant bit of the most-significant byte
2003 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002004 * opposite of the one we need. The exception is ints
2005 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002006 * its own 256's-complement, so has the right sign bit
2007 * even without the extra byte. That's a pain to check
2008 * for in advance, though, so we always grab an extra
2009 * byte at the start, and cut it back later if possible.
2010 */
2011 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002012 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002013 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002014 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002015 goto error;
2016 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002017 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002018 if (repr == NULL)
2019 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002020 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002021 i = _PyLong_AsByteArray((PyLongObject *)obj,
2022 pdata, nbytes,
2023 1 /* little endian */ , 1 /* signed */ );
2024 if (i < 0)
2025 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002026 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002027 * needed. This is so iff the MSB is all redundant sign
2028 * bits.
2029 */
2030 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002031 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 pdata[nbytes - 1] == 0xff &&
2033 (pdata[nbytes - 2] & 0x80) != 0) {
2034 nbytes--;
2035 }
2036
2037 if (nbytes < 256) {
2038 header[0] = LONG1;
2039 header[1] = (unsigned char)nbytes;
2040 size = 2;
2041 }
2042 else {
2043 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002044 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002045 for (i = 1; i < 5; i++) {
2046 header[i] = (unsigned char)(size & 0xff);
2047 size >>= 8;
2048 }
2049 size = 5;
2050 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002051 if (_Pickler_Write(self, header, size) < 0 ||
2052 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002053 goto error;
2054 }
2055 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002056 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002057 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002058
Mark Dickinson8dd05142009-01-20 20:43:58 +00002059 /* proto < 2: write the repr and newline. This is quadratic-time (in
2060 the number of digits), in both directions. We add a trailing 'L'
2061 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002062
2063 repr = PyObject_Repr(obj);
2064 if (repr == NULL)
2065 goto error;
2066
Serhiy Storchaka06515832016-11-20 09:13:07 +02002067 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002068 if (string == NULL)
2069 goto error;
2070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002071 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2072 _Pickler_Write(self, string, size) < 0 ||
2073 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002074 goto error;
2075 }
2076
2077 if (0) {
2078 error:
2079 status = -1;
2080 }
2081 Py_XDECREF(repr);
2082
2083 return status;
2084}
2085
2086static int
2087save_float(PicklerObject *self, PyObject *obj)
2088{
2089 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2090
2091 if (self->bin) {
2092 char pdata[9];
2093 pdata[0] = BINFLOAT;
2094 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2095 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002096 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002097 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002098 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002100 int result = -1;
2101 char *buf = NULL;
2102 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002103
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002104 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002105 goto done;
2106
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002107 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002108 if (!buf) {
2109 PyErr_NoMemory();
2110 goto done;
2111 }
2112
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002113 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002114 goto done;
2115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002116 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002117 goto done;
2118
2119 result = 0;
2120done:
2121 PyMem_Free(buf);
2122 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002123 }
2124
2125 return 0;
2126}
2127
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002128/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002129
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002130 The large contiguous data is written directly into the underlying file
2131 object, bypassing the output_buffer of the Pickler. We intentionally
2132 do not insert a protocol 4 frame opcode to make it possible to optimize
2133 file.read calls in the loader.
2134 */
2135static int
2136_Pickler_write_bytes(PicklerObject *self,
2137 const char *header, Py_ssize_t header_size,
2138 const char *data, Py_ssize_t data_size,
2139 PyObject *payload)
2140{
2141 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2142 int framing = self->framing;
2143
2144 if (bypass_buffer) {
2145 assert(self->output_buffer != NULL);
2146 /* Commit the previous frame. */
2147 if (_Pickler_CommitFrame(self)) {
2148 return -1;
2149 }
2150 /* Disable framing temporarily */
2151 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002152 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002153
2154 if (_Pickler_Write(self, header, header_size) < 0) {
2155 return -1;
2156 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002157
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002158 if (bypass_buffer && self->write != NULL) {
2159 /* Bypass the in-memory buffer to directly stream large data
2160 into the underlying file object. */
2161 PyObject *result, *mem = NULL;
2162 /* Dump the output buffer to the file. */
2163 if (_Pickler_FlushToFile(self) < 0) {
2164 return -1;
2165 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002166
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002167 /* Stream write the payload into the file without going through the
2168 output buffer. */
2169 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002170 /* TODO: It would be better to use a memoryview with a linked
2171 original string if this is possible. */
2172 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002173 if (payload == NULL) {
2174 return -1;
2175 }
2176 }
2177 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2178 Py_XDECREF(mem);
2179 if (result == NULL) {
2180 return -1;
2181 }
2182 Py_DECREF(result);
2183
2184 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2185 if (_Pickler_ClearBuffer(self) < 0) {
2186 return -1;
2187 }
2188 }
2189 else {
2190 if (_Pickler_Write(self, data, data_size) < 0) {
2191 return -1;
2192 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002193 }
2194
2195 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002196 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002197
2198 return 0;
2199}
2200
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002201static int
2202save_bytes(PicklerObject *self, PyObject *obj)
2203{
2204 if (self->proto < 3) {
2205 /* Older pickle protocols do not have an opcode for pickling bytes
2206 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002207 the __reduce__ method) to permit bytes object unpickling.
2208
2209 Here we use a hack to be compatible with Python 2. Since in Python
2210 2 'bytes' is just an alias for 'str' (which has different
2211 parameters than the actual bytes object), we use codecs.encode
2212 to create the appropriate 'str' object when unpickled using
2213 Python 2 *and* the appropriate 'bytes' object when unpickled
2214 using Python 3. Again this is a hack and we don't need to do this
2215 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002216 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002217 int status;
2218
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002219 if (PyBytes_GET_SIZE(obj) == 0) {
2220 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2221 }
2222 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002223 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002224 PyObject *unicode_str =
2225 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2226 PyBytes_GET_SIZE(obj),
2227 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002228 _Py_IDENTIFIER(latin1);
2229
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002230 if (unicode_str == NULL)
2231 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002232 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002233 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002234 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002235 Py_DECREF(unicode_str);
2236 }
2237
2238 if (reduce_value == NULL)
2239 return -1;
2240
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002241 /* save_reduce() will memoize the object automatically. */
2242 status = save_reduce(self, reduce_value, obj);
2243 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002244 return status;
2245 }
2246 else {
2247 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002248 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002249 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002250
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002251 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 if (size < 0)
2253 return -1;
2254
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002255 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256 header[0] = SHORT_BINBYTES;
2257 header[1] = (unsigned char)size;
2258 len = 2;
2259 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002260 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002261 header[0] = BINBYTES;
2262 header[1] = (unsigned char)(size & 0xff);
2263 header[2] = (unsigned char)((size >> 8) & 0xff);
2264 header[3] = (unsigned char)((size >> 16) & 0xff);
2265 header[4] = (unsigned char)((size >> 24) & 0xff);
2266 len = 5;
2267 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002268 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002269 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002270 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002271 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002272 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002273 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002274 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002275 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 return -1; /* string too large */
2277 }
2278
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002279 if (_Pickler_write_bytes(self, header, len,
2280 PyBytes_AS_STRING(obj), size, obj) < 0)
2281 {
2282 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002283 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002284
2285 if (memo_put(self, obj) < 0)
2286 return -1;
2287
2288 return 0;
2289 }
2290}
2291
2292/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2293 backslash and newline characters to \uXXXX escapes. */
2294static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002295raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002297 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002298 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002299 void *data;
2300 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002301 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002302
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002303 if (PyUnicode_READY(obj))
2304 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002305
Victor Stinner358af132015-10-12 22:36:57 +02002306 _PyBytesWriter_Init(&writer);
2307
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002308 size = PyUnicode_GET_LENGTH(obj);
2309 data = PyUnicode_DATA(obj);
2310 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002311
Victor Stinner358af132015-10-12 22:36:57 +02002312 p = _PyBytesWriter_Alloc(&writer, size);
2313 if (p == NULL)
2314 goto error;
2315 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002316
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002317 for (i=0; i < size; i++) {
2318 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002319 /* Map 32-bit characters to '\Uxxxxxxxx' */
2320 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002321 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002322 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2323 if (p == NULL)
2324 goto error;
2325
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002326 *p++ = '\\';
2327 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002328 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2329 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2330 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2331 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2332 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2333 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2334 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2335 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002336 }
Victor Stinner358af132015-10-12 22:36:57 +02002337 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002338 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002339 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002340 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2341 if (p == NULL)
2342 goto error;
2343
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002344 *p++ = '\\';
2345 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002346 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2347 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2348 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2349 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002350 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002351 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002352 else
2353 *p++ = (char) ch;
2354 }
Victor Stinner358af132015-10-12 22:36:57 +02002355
2356 return _PyBytesWriter_Finish(&writer, p);
2357
2358error:
2359 _PyBytesWriter_Dealloc(&writer);
2360 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002361}
2362
2363static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002364write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002365{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002366 char header[9];
2367 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002368 PyObject *encoded = NULL;
2369 Py_ssize_t size;
2370 const char *data;
2371
2372 if (PyUnicode_READY(obj))
2373 return -1;
2374
2375 data = PyUnicode_AsUTF8AndSize(obj, &size);
2376 if (data == NULL) {
2377 /* Issue #8383: for strings with lone surrogates, fallback on the
2378 "surrogatepass" error handler. */
2379 PyErr_Clear();
2380 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2381 if (encoded == NULL)
2382 return -1;
2383
2384 data = PyBytes_AS_STRING(encoded);
2385 size = PyBytes_GET_SIZE(encoded);
2386 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002387
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002388 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002389 if (size <= 0xff && self->proto >= 4) {
2390 header[0] = SHORT_BINUNICODE;
2391 header[1] = (unsigned char)(size & 0xff);
2392 len = 2;
2393 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002394 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002395 header[0] = BINUNICODE;
2396 header[1] = (unsigned char)(size & 0xff);
2397 header[2] = (unsigned char)((size >> 8) & 0xff);
2398 header[3] = (unsigned char)((size >> 16) & 0xff);
2399 header[4] = (unsigned char)((size >> 24) & 0xff);
2400 len = 5;
2401 }
2402 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002403 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002404 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002405 len = 9;
2406 }
2407 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002408 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002409 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002410 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002411 return -1;
2412 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002413
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002414 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2415 Py_XDECREF(encoded);
2416 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002417 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002418 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002419 return 0;
2420}
2421
2422static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002423save_unicode(PicklerObject *self, PyObject *obj)
2424{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002425 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002426 if (write_unicode_binary(self, obj) < 0)
2427 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002428 }
2429 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002430 PyObject *encoded;
2431 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002432 const char unicode_op = UNICODE;
2433
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002434 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002435 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002436 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002437
Antoine Pitrou299978d2013-04-07 17:38:11 +02002438 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2439 Py_DECREF(encoded);
2440 return -1;
2441 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002442
2443 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002444 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2445 Py_DECREF(encoded);
2446 return -1;
2447 }
2448 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002449
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002450 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002451 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002452 }
2453 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002454 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002456 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002457}
2458
2459/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2460static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002461store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002462{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002463 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002464
2465 assert(PyTuple_Size(t) == len);
2466
2467 for (i = 0; i < len; i++) {
2468 PyObject *element = PyTuple_GET_ITEM(t, i);
2469
2470 if (element == NULL)
2471 return -1;
2472 if (save(self, element, 0) < 0)
2473 return -1;
2474 }
2475
2476 return 0;
2477}
2478
2479/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2480 * used across protocols to minimize the space needed to pickle them.
2481 * Tuples are also the only builtin immutable type that can be recursive
2482 * (a tuple can be reached from itself), and that requires some subtle
2483 * magic so that it works in all cases. IOW, this is a long routine.
2484 */
2485static int
2486save_tuple(PicklerObject *self, PyObject *obj)
2487{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002488 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002489
2490 const char mark_op = MARK;
2491 const char tuple_op = TUPLE;
2492 const char pop_op = POP;
2493 const char pop_mark_op = POP_MARK;
2494 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2495
2496 if ((len = PyTuple_Size(obj)) < 0)
2497 return -1;
2498
2499 if (len == 0) {
2500 char pdata[2];
2501
2502 if (self->proto) {
2503 pdata[0] = EMPTY_TUPLE;
2504 len = 1;
2505 }
2506 else {
2507 pdata[0] = MARK;
2508 pdata[1] = TUPLE;
2509 len = 2;
2510 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002511 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002512 return -1;
2513 return 0;
2514 }
2515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002516 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517 * saving the tuple elements, the tuple must be recursive, in
2518 * which case we'll pop everything we put on the stack, and fetch
2519 * its value from the memo.
2520 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002521 if (len <= 3 && self->proto >= 2) {
2522 /* Use TUPLE{1,2,3} opcodes. */
2523 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002524 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002526 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002527 /* pop the len elements */
2528 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002529 if (_Pickler_Write(self, &pop_op, 1) < 0)
2530 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002531 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002532 if (memo_get(self, obj) < 0)
2533 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002534
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002535 return 0;
2536 }
2537 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002538 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2539 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002540 }
2541 goto memoize;
2542 }
2543
2544 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2545 * Generate MARK e1 e2 ... TUPLE
2546 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002547 if (_Pickler_Write(self, &mark_op, 1) < 0)
2548 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002549
2550 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002551 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002552
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002553 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002554 /* pop the stack stuff we pushed */
2555 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002556 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2557 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002558 }
2559 else {
2560 /* Note that we pop one more than len, to remove
2561 * the MARK too.
2562 */
2563 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002564 if (_Pickler_Write(self, &pop_op, 1) < 0)
2565 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002566 }
2567 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002568 if (memo_get(self, obj) < 0)
2569 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002570
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002571 return 0;
2572 }
2573 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002574 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2575 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002576 }
2577
2578 memoize:
2579 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002580 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002581
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002582 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002583}
2584
2585/* iter is an iterator giving items, and we batch up chunks of
2586 * MARK item item ... item APPENDS
2587 * opcode sequences. Calling code should have arranged to first create an
2588 * empty list, or list-like object, for the APPENDS to operate on.
2589 * Returns 0 on success, <0 on error.
2590 */
2591static int
2592batch_list(PicklerObject *self, PyObject *iter)
2593{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002594 PyObject *obj = NULL;
2595 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002596 int i, n;
2597
2598 const char mark_op = MARK;
2599 const char append_op = APPEND;
2600 const char appends_op = APPENDS;
2601
2602 assert(iter != NULL);
2603
2604 /* XXX: I think this function could be made faster by avoiding the
2605 iterator interface and fetching objects directly from list using
2606 PyList_GET_ITEM.
2607 */
2608
2609 if (self->proto == 0) {
2610 /* APPENDS isn't available; do one at a time. */
2611 for (;;) {
2612 obj = PyIter_Next(iter);
2613 if (obj == NULL) {
2614 if (PyErr_Occurred())
2615 return -1;
2616 break;
2617 }
2618 i = save(self, obj, 0);
2619 Py_DECREF(obj);
2620 if (i < 0)
2621 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002622 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002623 return -1;
2624 }
2625 return 0;
2626 }
2627
2628 /* proto > 0: write in batches of BATCHSIZE. */
2629 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002630 /* Get first item */
2631 firstitem = PyIter_Next(iter);
2632 if (firstitem == NULL) {
2633 if (PyErr_Occurred())
2634 goto error;
2635
2636 /* nothing more to add */
2637 break;
2638 }
2639
2640 /* Try to get a second item */
2641 obj = PyIter_Next(iter);
2642 if (obj == NULL) {
2643 if (PyErr_Occurred())
2644 goto error;
2645
2646 /* Only one item to write */
2647 if (save(self, firstitem, 0) < 0)
2648 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002649 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002650 goto error;
2651 Py_CLEAR(firstitem);
2652 break;
2653 }
2654
2655 /* More than one item to write */
2656
2657 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002658 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002659 goto error;
2660
2661 if (save(self, firstitem, 0) < 0)
2662 goto error;
2663 Py_CLEAR(firstitem);
2664 n = 1;
2665
2666 /* Fetch and save up to BATCHSIZE items */
2667 while (obj) {
2668 if (save(self, obj, 0) < 0)
2669 goto error;
2670 Py_CLEAR(obj);
2671 n += 1;
2672
2673 if (n == BATCHSIZE)
2674 break;
2675
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002676 obj = PyIter_Next(iter);
2677 if (obj == NULL) {
2678 if (PyErr_Occurred())
2679 goto error;
2680 break;
2681 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002682 }
2683
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002684 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002685 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002686
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002687 } while (n == BATCHSIZE);
2688 return 0;
2689
2690 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002691 Py_XDECREF(firstitem);
2692 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002693 return -1;
2694}
2695
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002696/* This is a variant of batch_list() above, specialized for lists (with no
2697 * support for list subclasses). Like batch_list(), we batch up chunks of
2698 * MARK item item ... item APPENDS
2699 * opcode sequences. Calling code should have arranged to first create an
2700 * empty list, or list-like object, for the APPENDS to operate on.
2701 * Returns 0 on success, -1 on error.
2702 *
2703 * This version is considerably faster than batch_list(), if less general.
2704 *
2705 * Note that this only works for protocols > 0.
2706 */
2707static int
2708batch_list_exact(PicklerObject *self, PyObject *obj)
2709{
2710 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002711 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002712
2713 const char append_op = APPEND;
2714 const char appends_op = APPENDS;
2715 const char mark_op = MARK;
2716
2717 assert(obj != NULL);
2718 assert(self->proto > 0);
2719 assert(PyList_CheckExact(obj));
2720
2721 if (PyList_GET_SIZE(obj) == 1) {
2722 item = PyList_GET_ITEM(obj, 0);
2723 if (save(self, item, 0) < 0)
2724 return -1;
2725 if (_Pickler_Write(self, &append_op, 1) < 0)
2726 return -1;
2727 return 0;
2728 }
2729
2730 /* Write in batches of BATCHSIZE. */
2731 total = 0;
2732 do {
2733 this_batch = 0;
2734 if (_Pickler_Write(self, &mark_op, 1) < 0)
2735 return -1;
2736 while (total < PyList_GET_SIZE(obj)) {
2737 item = PyList_GET_ITEM(obj, total);
2738 if (save(self, item, 0) < 0)
2739 return -1;
2740 total++;
2741 if (++this_batch == BATCHSIZE)
2742 break;
2743 }
2744 if (_Pickler_Write(self, &appends_op, 1) < 0)
2745 return -1;
2746
2747 } while (total < PyList_GET_SIZE(obj));
2748
2749 return 0;
2750}
2751
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002752static int
2753save_list(PicklerObject *self, PyObject *obj)
2754{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002755 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002756 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002757 int status = 0;
2758
2759 if (self->fast && !fast_save_enter(self, obj))
2760 goto error;
2761
2762 /* Create an empty list. */
2763 if (self->bin) {
2764 header[0] = EMPTY_LIST;
2765 len = 1;
2766 }
2767 else {
2768 header[0] = MARK;
2769 header[1] = LIST;
2770 len = 2;
2771 }
2772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002773 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002774 goto error;
2775
2776 /* Get list length, and bow out early if empty. */
2777 if ((len = PyList_Size(obj)) < 0)
2778 goto error;
2779
2780 if (memo_put(self, obj) < 0)
2781 goto error;
2782
2783 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002784 /* Materialize the list elements. */
2785 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002786 if (Py_EnterRecursiveCall(" while pickling an object"))
2787 goto error;
2788 status = batch_list_exact(self, obj);
2789 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002790 } else {
2791 PyObject *iter = PyObject_GetIter(obj);
2792 if (iter == NULL)
2793 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002794
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002795 if (Py_EnterRecursiveCall(" while pickling an object")) {
2796 Py_DECREF(iter);
2797 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002798 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002799 status = batch_list(self, iter);
2800 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002801 Py_DECREF(iter);
2802 }
2803 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002804 if (0) {
2805 error:
2806 status = -1;
2807 }
2808
2809 if (self->fast && !fast_save_leave(self, obj))
2810 status = -1;
2811
2812 return status;
2813}
2814
2815/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2816 * MARK key value ... key value SETITEMS
2817 * opcode sequences. Calling code should have arranged to first create an
2818 * empty dict, or dict-like object, for the SETITEMS to operate on.
2819 * Returns 0 on success, <0 on error.
2820 *
2821 * This is very much like batch_list(). The difference between saving
2822 * elements directly, and picking apart two-tuples, is so long-winded at
2823 * the C level, though, that attempts to combine these routines were too
2824 * ugly to bear.
2825 */
2826static int
2827batch_dict(PicklerObject *self, PyObject *iter)
2828{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002829 PyObject *obj = NULL;
2830 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002831 int i, n;
2832
2833 const char mark_op = MARK;
2834 const char setitem_op = SETITEM;
2835 const char setitems_op = SETITEMS;
2836
2837 assert(iter != NULL);
2838
2839 if (self->proto == 0) {
2840 /* SETITEMS isn't available; do one at a time. */
2841 for (;;) {
2842 obj = PyIter_Next(iter);
2843 if (obj == NULL) {
2844 if (PyErr_Occurred())
2845 return -1;
2846 break;
2847 }
2848 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2849 PyErr_SetString(PyExc_TypeError, "dict items "
2850 "iterator must return 2-tuples");
2851 return -1;
2852 }
2853 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2854 if (i >= 0)
2855 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2856 Py_DECREF(obj);
2857 if (i < 0)
2858 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002859 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002860 return -1;
2861 }
2862 return 0;
2863 }
2864
2865 /* proto > 0: write in batches of BATCHSIZE. */
2866 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002867 /* Get first item */
2868 firstitem = PyIter_Next(iter);
2869 if (firstitem == NULL) {
2870 if (PyErr_Occurred())
2871 goto error;
2872
2873 /* nothing more to add */
2874 break;
2875 }
2876 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2877 PyErr_SetString(PyExc_TypeError, "dict items "
2878 "iterator must return 2-tuples");
2879 goto error;
2880 }
2881
2882 /* Try to get a second item */
2883 obj = PyIter_Next(iter);
2884 if (obj == NULL) {
2885 if (PyErr_Occurred())
2886 goto error;
2887
2888 /* Only one item to write */
2889 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2890 goto error;
2891 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2892 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002893 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002894 goto error;
2895 Py_CLEAR(firstitem);
2896 break;
2897 }
2898
2899 /* More than one item to write */
2900
2901 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002902 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002903 goto error;
2904
2905 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2906 goto error;
2907 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2908 goto error;
2909 Py_CLEAR(firstitem);
2910 n = 1;
2911
2912 /* Fetch and save up to BATCHSIZE items */
2913 while (obj) {
2914 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2915 PyErr_SetString(PyExc_TypeError, "dict items "
2916 "iterator must return 2-tuples");
2917 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002918 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002919 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2920 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2921 goto error;
2922 Py_CLEAR(obj);
2923 n += 1;
2924
2925 if (n == BATCHSIZE)
2926 break;
2927
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002928 obj = PyIter_Next(iter);
2929 if (obj == NULL) {
2930 if (PyErr_Occurred())
2931 goto error;
2932 break;
2933 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002934 }
2935
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002936 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002937 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002938
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002939 } while (n == BATCHSIZE);
2940 return 0;
2941
2942 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002943 Py_XDECREF(firstitem);
2944 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002945 return -1;
2946}
2947
Collin Winter5c9b02d2009-05-25 05:43:30 +00002948/* This is a variant of batch_dict() above that specializes for dicts, with no
2949 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2950 * MARK key value ... key value SETITEMS
2951 * opcode sequences. Calling code should have arranged to first create an
2952 * empty dict, or dict-like object, for the SETITEMS to operate on.
2953 * Returns 0 on success, -1 on error.
2954 *
2955 * Note that this currently doesn't work for protocol 0.
2956 */
2957static int
2958batch_dict_exact(PicklerObject *self, PyObject *obj)
2959{
2960 PyObject *key = NULL, *value = NULL;
2961 int i;
2962 Py_ssize_t dict_size, ppos = 0;
2963
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002964 const char mark_op = MARK;
2965 const char setitem_op = SETITEM;
2966 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002967
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002968 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002969 assert(self->proto > 0);
2970
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002971 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002972
2973 /* Special-case len(d) == 1 to save space. */
2974 if (dict_size == 1) {
2975 PyDict_Next(obj, &ppos, &key, &value);
2976 if (save(self, key, 0) < 0)
2977 return -1;
2978 if (save(self, value, 0) < 0)
2979 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002980 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002981 return -1;
2982 return 0;
2983 }
2984
2985 /* Write in batches of BATCHSIZE. */
2986 do {
2987 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002988 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002989 return -1;
2990 while (PyDict_Next(obj, &ppos, &key, &value)) {
2991 if (save(self, key, 0) < 0)
2992 return -1;
2993 if (save(self, value, 0) < 0)
2994 return -1;
2995 if (++i == BATCHSIZE)
2996 break;
2997 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002998 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002999 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003000 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003001 PyErr_Format(
3002 PyExc_RuntimeError,
3003 "dictionary changed size during iteration");
3004 return -1;
3005 }
3006
3007 } while (i == BATCHSIZE);
3008 return 0;
3009}
3010
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003011static int
3012save_dict(PicklerObject *self, PyObject *obj)
3013{
3014 PyObject *items, *iter;
3015 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003016 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003017 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003018 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003019
3020 if (self->fast && !fast_save_enter(self, obj))
3021 goto error;
3022
3023 /* Create an empty dict. */
3024 if (self->bin) {
3025 header[0] = EMPTY_DICT;
3026 len = 1;
3027 }
3028 else {
3029 header[0] = MARK;
3030 header[1] = DICT;
3031 len = 2;
3032 }
3033
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003034 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003035 goto error;
3036
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003037 if (memo_put(self, obj) < 0)
3038 goto error;
3039
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003040 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003041 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003042 if (PyDict_CheckExact(obj) && self->proto > 0) {
3043 /* We can take certain shortcuts if we know this is a dict and
3044 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003045 if (Py_EnterRecursiveCall(" while pickling an object"))
3046 goto error;
3047 status = batch_dict_exact(self, obj);
3048 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003049 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003050 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003051
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003052 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003053 if (items == NULL)
3054 goto error;
3055 iter = PyObject_GetIter(items);
3056 Py_DECREF(items);
3057 if (iter == NULL)
3058 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003059 if (Py_EnterRecursiveCall(" while pickling an object")) {
3060 Py_DECREF(iter);
3061 goto error;
3062 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003063 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003064 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003065 Py_DECREF(iter);
3066 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003067 }
3068
3069 if (0) {
3070 error:
3071 status = -1;
3072 }
3073
3074 if (self->fast && !fast_save_leave(self, obj))
3075 status = -1;
3076
3077 return status;
3078}
3079
3080static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003081save_set(PicklerObject *self, PyObject *obj)
3082{
3083 PyObject *item;
3084 int i;
3085 Py_ssize_t set_size, ppos = 0;
3086 Py_hash_t hash;
3087
3088 const char empty_set_op = EMPTY_SET;
3089 const char mark_op = MARK;
3090 const char additems_op = ADDITEMS;
3091
3092 if (self->proto < 4) {
3093 PyObject *items;
3094 PyObject *reduce_value;
3095 int status;
3096
3097 items = PySequence_List(obj);
3098 if (items == NULL) {
3099 return -1;
3100 }
3101 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3102 Py_DECREF(items);
3103 if (reduce_value == NULL) {
3104 return -1;
3105 }
3106 /* save_reduce() will memoize the object automatically. */
3107 status = save_reduce(self, reduce_value, obj);
3108 Py_DECREF(reduce_value);
3109 return status;
3110 }
3111
3112 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3113 return -1;
3114
3115 if (memo_put(self, obj) < 0)
3116 return -1;
3117
3118 set_size = PySet_GET_SIZE(obj);
3119 if (set_size == 0)
3120 return 0; /* nothing to do */
3121
3122 /* Write in batches of BATCHSIZE. */
3123 do {
3124 i = 0;
3125 if (_Pickler_Write(self, &mark_op, 1) < 0)
3126 return -1;
3127 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3128 if (save(self, item, 0) < 0)
3129 return -1;
3130 if (++i == BATCHSIZE)
3131 break;
3132 }
3133 if (_Pickler_Write(self, &additems_op, 1) < 0)
3134 return -1;
3135 if (PySet_GET_SIZE(obj) != set_size) {
3136 PyErr_Format(
3137 PyExc_RuntimeError,
3138 "set changed size during iteration");
3139 return -1;
3140 }
3141 } while (i == BATCHSIZE);
3142
3143 return 0;
3144}
3145
3146static int
3147save_frozenset(PicklerObject *self, PyObject *obj)
3148{
3149 PyObject *iter;
3150
3151 const char mark_op = MARK;
3152 const char frozenset_op = FROZENSET;
3153
3154 if (self->fast && !fast_save_enter(self, obj))
3155 return -1;
3156
3157 if (self->proto < 4) {
3158 PyObject *items;
3159 PyObject *reduce_value;
3160 int status;
3161
3162 items = PySequence_List(obj);
3163 if (items == NULL) {
3164 return -1;
3165 }
3166 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3167 items);
3168 Py_DECREF(items);
3169 if (reduce_value == NULL) {
3170 return -1;
3171 }
3172 /* save_reduce() will memoize the object automatically. */
3173 status = save_reduce(self, reduce_value, obj);
3174 Py_DECREF(reduce_value);
3175 return status;
3176 }
3177
3178 if (_Pickler_Write(self, &mark_op, 1) < 0)
3179 return -1;
3180
3181 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003182 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003183 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003184 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003185 for (;;) {
3186 PyObject *item;
3187
3188 item = PyIter_Next(iter);
3189 if (item == NULL) {
3190 if (PyErr_Occurred()) {
3191 Py_DECREF(iter);
3192 return -1;
3193 }
3194 break;
3195 }
3196 if (save(self, item, 0) < 0) {
3197 Py_DECREF(item);
3198 Py_DECREF(iter);
3199 return -1;
3200 }
3201 Py_DECREF(item);
3202 }
3203 Py_DECREF(iter);
3204
3205 /* If the object is already in the memo, this means it is
3206 recursive. In this case, throw away everything we put on the
3207 stack, and fetch the object back from the memo. */
3208 if (PyMemoTable_Get(self->memo, obj)) {
3209 const char pop_mark_op = POP_MARK;
3210
3211 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3212 return -1;
3213 if (memo_get(self, obj) < 0)
3214 return -1;
3215 return 0;
3216 }
3217
3218 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3219 return -1;
3220 if (memo_put(self, obj) < 0)
3221 return -1;
3222
3223 return 0;
3224}
3225
3226static int
3227fix_imports(PyObject **module_name, PyObject **global_name)
3228{
3229 PyObject *key;
3230 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003231 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003232
3233 key = PyTuple_Pack(2, *module_name, *global_name);
3234 if (key == NULL)
3235 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003236 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003237 Py_DECREF(key);
3238 if (item) {
3239 PyObject *fixed_module_name;
3240 PyObject *fixed_global_name;
3241
3242 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3243 PyErr_Format(PyExc_RuntimeError,
3244 "_compat_pickle.REVERSE_NAME_MAPPING values "
3245 "should be 2-tuples, not %.200s",
3246 Py_TYPE(item)->tp_name);
3247 return -1;
3248 }
3249 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3250 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3251 if (!PyUnicode_Check(fixed_module_name) ||
3252 !PyUnicode_Check(fixed_global_name)) {
3253 PyErr_Format(PyExc_RuntimeError,
3254 "_compat_pickle.REVERSE_NAME_MAPPING values "
3255 "should be pairs of str, not (%.200s, %.200s)",
3256 Py_TYPE(fixed_module_name)->tp_name,
3257 Py_TYPE(fixed_global_name)->tp_name);
3258 return -1;
3259 }
3260
3261 Py_CLEAR(*module_name);
3262 Py_CLEAR(*global_name);
3263 Py_INCREF(fixed_module_name);
3264 Py_INCREF(fixed_global_name);
3265 *module_name = fixed_module_name;
3266 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003267 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003268 }
3269 else if (PyErr_Occurred()) {
3270 return -1;
3271 }
3272
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003273 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003274 if (item) {
3275 if (!PyUnicode_Check(item)) {
3276 PyErr_Format(PyExc_RuntimeError,
3277 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3278 "should be strings, not %.200s",
3279 Py_TYPE(item)->tp_name);
3280 return -1;
3281 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003282 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003283 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003284 }
3285 else if (PyErr_Occurred()) {
3286 return -1;
3287 }
3288
3289 return 0;
3290}
3291
3292static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003293save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3294{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003295 PyObject *global_name = NULL;
3296 PyObject *module_name = NULL;
3297 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003298 PyObject *parent = NULL;
3299 PyObject *dotted_path = NULL;
3300 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003301 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003302 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003303 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003304 _Py_IDENTIFIER(__name__);
3305 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003306
3307 const char global_op = GLOBAL;
3308
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003309 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003310 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003311 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003312 }
3313 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003314 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3315 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003316 if (global_name == NULL) {
3317 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3318 if (global_name == NULL)
3319 goto error;
3320 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003321 }
3322
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003323 dotted_path = get_dotted_path(module, global_name);
3324 if (dotted_path == NULL)
3325 goto error;
3326 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003327 if (module_name == NULL)
3328 goto error;
3329
3330 /* XXX: Change to use the import C API directly with level=0 to disallow
3331 relative imports.
3332
3333 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3334 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3335 custom import functions (IMHO, this would be a nice security
3336 feature). The import C API would need to be extended to support the
3337 extra parameters of __import__ to fix that. */
3338 module = PyImport_Import(module_name);
3339 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003340 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003341 "Can't pickle %R: import of module %R failed",
3342 obj, module_name);
3343 goto error;
3344 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003345 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3346 Py_INCREF(lastname);
3347 cls = get_deep_attribute(module, dotted_path, &parent);
3348 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003349 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003350 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003351 "Can't pickle %R: attribute lookup %S on %S failed",
3352 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003353 goto error;
3354 }
3355 if (cls != obj) {
3356 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003357 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003358 "Can't pickle %R: it's not the same object as %S.%S",
3359 obj, module_name, global_name);
3360 goto error;
3361 }
3362 Py_DECREF(cls);
3363
3364 if (self->proto >= 2) {
3365 /* See whether this is in the extension registry, and if
3366 * so generate an EXT opcode.
3367 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003368 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003369 PyObject *code_obj; /* extension code as Python object */
3370 long code; /* extension code as C value */
3371 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003372 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003373
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003374 extension_key = PyTuple_Pack(2, module_name, global_name);
3375 if (extension_key == NULL) {
3376 goto error;
3377 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003378 code_obj = PyDict_GetItemWithError(st->extension_registry,
3379 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003380 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003381 /* The object is not registered in the extension registry.
3382 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003383 if (code_obj == NULL) {
3384 if (PyErr_Occurred()) {
3385 goto error;
3386 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003387 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003388 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003389
3390 /* XXX: pickle.py doesn't check neither the type, nor the range
3391 of the value returned by the extension_registry. It should for
3392 consistency. */
3393
3394 /* Verify code_obj has the right type and value. */
3395 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003396 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003397 "Can't pickle %R: extension code %R isn't an integer",
3398 obj, code_obj);
3399 goto error;
3400 }
3401 code = PyLong_AS_LONG(code_obj);
3402 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003403 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003404 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3405 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003406 goto error;
3407 }
3408
3409 /* Generate an EXT opcode. */
3410 if (code <= 0xff) {
3411 pdata[0] = EXT1;
3412 pdata[1] = (unsigned char)code;
3413 n = 2;
3414 }
3415 else if (code <= 0xffff) {
3416 pdata[0] = EXT2;
3417 pdata[1] = (unsigned char)(code & 0xff);
3418 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3419 n = 3;
3420 }
3421 else {
3422 pdata[0] = EXT4;
3423 pdata[1] = (unsigned char)(code & 0xff);
3424 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3425 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3426 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3427 n = 5;
3428 }
3429
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003430 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003431 goto error;
3432 }
3433 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003434 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003435 if (parent == module) {
3436 Py_INCREF(lastname);
3437 Py_DECREF(global_name);
3438 global_name = lastname;
3439 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003440 if (self->proto >= 4) {
3441 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003442
Christian Heimese8b1ba12013-11-23 21:13:39 +01003443 if (save(self, module_name, 0) < 0)
3444 goto error;
3445 if (save(self, global_name, 0) < 0)
3446 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003447
3448 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3449 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003450 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003451 else if (parent != module) {
3452 PickleState *st = _Pickle_GetGlobalState();
3453 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3454 st->getattr, parent, lastname);
3455 status = save_reduce(self, reduce_value, NULL);
3456 Py_DECREF(reduce_value);
3457 if (status < 0)
3458 goto error;
3459 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003460 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003461 /* Generate a normal global opcode if we are using a pickle
3462 protocol < 4, or if the object is not registered in the
3463 extension registry. */
3464 PyObject *encoded;
3465 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003466
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003467 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003468 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003469
3470 /* For protocol < 3 and if the user didn't request against doing
3471 so, we convert module names to the old 2.x module names. */
3472 if (self->proto < 3 && self->fix_imports) {
3473 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003474 goto error;
3475 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003476 }
3477
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003478 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3479 both the module name and the global name using UTF-8. We do so
3480 only when we are using the pickle protocol newer than version
3481 3. This is to ensure compatibility with older Unpickler running
3482 on Python 2.x. */
3483 if (self->proto == 3) {
3484 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003485 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003486 else {
3487 unicode_encoder = PyUnicode_AsASCIIString;
3488 }
3489 encoded = unicode_encoder(module_name);
3490 if (encoded == NULL) {
3491 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003492 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003493 "can't pickle module identifier '%S' using "
3494 "pickle protocol %i",
3495 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003496 goto error;
3497 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003498 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3499 PyBytes_GET_SIZE(encoded)) < 0) {
3500 Py_DECREF(encoded);
3501 goto error;
3502 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003503 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003504 if(_Pickler_Write(self, "\n", 1) < 0)
3505 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003506
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003507 /* Save the name of the module. */
3508 encoded = unicode_encoder(global_name);
3509 if (encoded == NULL) {
3510 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003511 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003512 "can't pickle global identifier '%S' using "
3513 "pickle protocol %i",
3514 global_name, self->proto);
3515 goto error;
3516 }
3517 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3518 PyBytes_GET_SIZE(encoded)) < 0) {
3519 Py_DECREF(encoded);
3520 goto error;
3521 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003522 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003523 if (_Pickler_Write(self, "\n", 1) < 0)
3524 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003525 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003526 /* Memoize the object. */
3527 if (memo_put(self, obj) < 0)
3528 goto error;
3529 }
3530
3531 if (0) {
3532 error:
3533 status = -1;
3534 }
3535 Py_XDECREF(module_name);
3536 Py_XDECREF(global_name);
3537 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003538 Py_XDECREF(parent);
3539 Py_XDECREF(dotted_path);
3540 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003541
3542 return status;
3543}
3544
3545static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003546save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3547{
3548 PyObject *reduce_value;
3549 int status;
3550
3551 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3552 if (reduce_value == NULL) {
3553 return -1;
3554 }
3555 status = save_reduce(self, reduce_value, obj);
3556 Py_DECREF(reduce_value);
3557 return status;
3558}
3559
3560static int
3561save_type(PicklerObject *self, PyObject *obj)
3562{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003563 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003564 return save_singleton_type(self, obj, Py_None);
3565 }
3566 else if (obj == (PyObject *)&PyEllipsis_Type) {
3567 return save_singleton_type(self, obj, Py_Ellipsis);
3568 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003569 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003570 return save_singleton_type(self, obj, Py_NotImplemented);
3571 }
3572 return save_global(self, obj, NULL);
3573}
3574
3575static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003576save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003577{
3578 PyObject *pid = NULL;
3579 int status = 0;
3580
3581 const char persid_op = PERSID;
3582 const char binpersid_op = BINPERSID;
3583
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003584 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003585 if (pid == NULL)
3586 return -1;
3587
3588 if (pid != Py_None) {
3589 if (self->bin) {
3590 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003591 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003592 goto error;
3593 }
3594 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003595 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003596
3597 pid_str = PyObject_Str(pid);
3598 if (pid_str == NULL)
3599 goto error;
3600
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003601 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003602 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003603 if (!PyUnicode_IS_ASCII(pid_str)) {
3604 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3605 "persistent IDs in protocol 0 must be "
3606 "ASCII strings");
3607 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003608 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003609 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003610
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003611 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003612 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3613 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3614 _Pickler_Write(self, "\n", 1) < 0) {
3615 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003616 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003617 }
3618 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003619 }
3620 status = 1;
3621 }
3622
3623 if (0) {
3624 error:
3625 status = -1;
3626 }
3627 Py_XDECREF(pid);
3628
3629 return status;
3630}
3631
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003632static PyObject *
3633get_class(PyObject *obj)
3634{
3635 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003636 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003637
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003638 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3639 cls = (PyObject *) Py_TYPE(obj);
3640 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003641 }
3642 return cls;
3643}
3644
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003645/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3646 * appropriate __reduce__ method for obj.
3647 */
3648static int
3649save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3650{
3651 PyObject *callable;
3652 PyObject *argtup;
3653 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003654 PyObject *listitems = Py_None;
3655 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003656 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003657 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003658 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003659
3660 const char reduce_op = REDUCE;
3661 const char build_op = BUILD;
3662 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003663 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003664
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003665 size = PyTuple_Size(args);
3666 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003667 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003668 "__reduce__ must contain 2 through 5 elements");
3669 return -1;
3670 }
3671
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003672 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3673 &callable, &argtup, &state, &listitems, &dictitems))
3674 return -1;
3675
3676 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003677 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003678 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003679 return -1;
3680 }
3681 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003682 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003683 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003684 return -1;
3685 }
3686
3687 if (state == Py_None)
3688 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003689
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003690 if (listitems == Py_None)
3691 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003692 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003693 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003694 "returned by __reduce__ must be an iterator, not %s",
3695 Py_TYPE(listitems)->tp_name);
3696 return -1;
3697 }
3698
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003699 if (dictitems == Py_None)
3700 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003701 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003702 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003703 "returned by __reduce__ must be an iterator, not %s",
3704 Py_TYPE(dictitems)->tp_name);
3705 return -1;
3706 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003707
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003708 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003709 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003710 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003711
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003712 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3713 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003714 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003715 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003716 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003717 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3718 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003719 if (!use_newobj_ex) {
3720 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003721 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003722 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003723 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003724 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003725 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003726
3727 if (use_newobj_ex) {
3728 PyObject *cls;
3729 PyObject *args;
3730 PyObject *kwargs;
3731
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003732 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003733 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003734 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003735 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003736 return -1;
3737 }
3738
3739 cls = PyTuple_GET_ITEM(argtup, 0);
3740 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003741 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003742 "first item from NEWOBJ_EX argument tuple must "
3743 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3744 return -1;
3745 }
3746 args = PyTuple_GET_ITEM(argtup, 1);
3747 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003748 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003749 "second item from NEWOBJ_EX argument tuple must "
3750 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3751 return -1;
3752 }
3753 kwargs = PyTuple_GET_ITEM(argtup, 2);
3754 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003755 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003756 "third item from NEWOBJ_EX argument tuple must "
3757 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3758 return -1;
3759 }
3760
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003761 if (self->proto >= 4) {
3762 if (save(self, cls, 0) < 0 ||
3763 save(self, args, 0) < 0 ||
3764 save(self, kwargs, 0) < 0 ||
3765 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3766 return -1;
3767 }
3768 }
3769 else {
3770 PyObject *newargs;
3771 PyObject *cls_new;
3772 Py_ssize_t i;
3773 _Py_IDENTIFIER(__new__);
3774
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003775 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003776 if (newargs == NULL)
3777 return -1;
3778
3779 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3780 if (cls_new == NULL) {
3781 Py_DECREF(newargs);
3782 return -1;
3783 }
3784 PyTuple_SET_ITEM(newargs, 0, cls_new);
3785 Py_INCREF(cls);
3786 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003787 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003788 PyObject *item = PyTuple_GET_ITEM(args, i);
3789 Py_INCREF(item);
3790 PyTuple_SET_ITEM(newargs, i + 2, item);
3791 }
3792
3793 callable = PyObject_Call(st->partial, newargs, kwargs);
3794 Py_DECREF(newargs);
3795 if (callable == NULL)
3796 return -1;
3797
3798 newargs = PyTuple_New(0);
3799 if (newargs == NULL) {
3800 Py_DECREF(callable);
3801 return -1;
3802 }
3803
3804 if (save(self, callable, 0) < 0 ||
3805 save(self, newargs, 0) < 0 ||
3806 _Pickler_Write(self, &reduce_op, 1) < 0) {
3807 Py_DECREF(newargs);
3808 Py_DECREF(callable);
3809 return -1;
3810 }
3811 Py_DECREF(newargs);
3812 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003813 }
3814 }
3815 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003816 PyObject *cls;
3817 PyObject *newargtup;
3818 PyObject *obj_class;
3819 int p;
3820
3821 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003822 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003823 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003824 return -1;
3825 }
3826
3827 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003828 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003829 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003830 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003831 return -1;
3832 }
3833
3834 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003835 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003836 p = obj_class != cls; /* true iff a problem */
3837 Py_DECREF(obj_class);
3838 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003839 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003840 "__newobj__ args has the wrong class");
3841 return -1;
3842 }
3843 }
3844 /* XXX: These calls save() are prone to infinite recursion. Imagine
3845 what happen if the value returned by the __reduce__() method of
3846 some extension type contains another object of the same type. Ouch!
3847
3848 Here is a quick example, that I ran into, to illustrate what I
3849 mean:
3850
3851 >>> import pickle, copyreg
3852 >>> copyreg.dispatch_table.pop(complex)
3853 >>> pickle.dumps(1+2j)
3854 Traceback (most recent call last):
3855 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003856 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003857
3858 Removing the complex class from copyreg.dispatch_table made the
3859 __reduce_ex__() method emit another complex object:
3860
3861 >>> (1+1j).__reduce_ex__(2)
3862 (<function __newobj__ at 0xb7b71c3c>,
3863 (<class 'complex'>, (1+1j)), None, None, None)
3864
3865 Thus when save() was called on newargstup (the 2nd item) recursion
3866 ensued. Of course, the bug was in the complex class which had a
3867 broken __getnewargs__() that emitted another complex object. But,
3868 the point, here, is it is quite easy to end up with a broken reduce
3869 function. */
3870
3871 /* Save the class and its __new__ arguments. */
3872 if (save(self, cls, 0) < 0)
3873 return -1;
3874
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003875 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003876 if (newargtup == NULL)
3877 return -1;
3878
3879 p = save(self, newargtup, 0);
3880 Py_DECREF(newargtup);
3881 if (p < 0)
3882 return -1;
3883
3884 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003885 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003886 return -1;
3887 }
3888 else { /* Not using NEWOBJ. */
3889 if (save(self, callable, 0) < 0 ||
3890 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003891 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003892 return -1;
3893 }
3894
3895 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3896 the caller do not want to memoize the object. Not particularly useful,
3897 but that is to mimic the behavior save_reduce() in pickle.py when
3898 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003899 if (obj != NULL) {
3900 /* If the object is already in the memo, this means it is
3901 recursive. In this case, throw away everything we put on the
3902 stack, and fetch the object back from the memo. */
3903 if (PyMemoTable_Get(self->memo, obj)) {
3904 const char pop_op = POP;
3905
3906 if (_Pickler_Write(self, &pop_op, 1) < 0)
3907 return -1;
3908 if (memo_get(self, obj) < 0)
3909 return -1;
3910
3911 return 0;
3912 }
3913 else if (memo_put(self, obj) < 0)
3914 return -1;
3915 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003916
3917 if (listitems && batch_list(self, listitems) < 0)
3918 return -1;
3919
3920 if (dictitems && batch_dict(self, dictitems) < 0)
3921 return -1;
3922
3923 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003924 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003925 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003926 return -1;
3927 }
3928
3929 return 0;
3930}
3931
3932static int
3933save(PicklerObject *self, PyObject *obj, int pers_save)
3934{
3935 PyTypeObject *type;
3936 PyObject *reduce_func = NULL;
3937 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003938 int status = 0;
3939
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003940 if (_Pickler_OpcodeBoundary(self) < 0)
3941 return -1;
3942
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943 /* The extra pers_save argument is necessary to avoid calling save_pers()
3944 on its returned object. */
3945 if (!pers_save && self->pers_func) {
3946 /* save_pers() returns:
3947 -1 to signal an error;
3948 0 if it did nothing successfully;
3949 1 if a persistent id was saved.
3950 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003951 if ((status = save_pers(self, obj)) != 0)
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003952 return status;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003953 }
3954
3955 type = Py_TYPE(obj);
3956
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003957 /* The old cPickle had an optimization that used switch-case statement
3958 dispatching on the first letter of the type name. This has was removed
3959 since benchmarks shown that this optimization was actually slowing
3960 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003961
3962 /* Atom types; these aren't memoized, so don't check the memo. */
3963
3964 if (obj == Py_None) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003965 return save_none(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003966 }
3967 else if (obj == Py_False || obj == Py_True) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003968 return save_bool(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003969 }
3970 else if (type == &PyLong_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003971 return save_long(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003972 }
3973 else if (type == &PyFloat_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003974 return save_float(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003975 }
3976
3977 /* Check the memo to see if it has the object. If so, generate
3978 a GET (or BINGET) opcode, instead of pickling the object
3979 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003980 if (PyMemoTable_Get(self->memo, obj)) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003981 return memo_get(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003982 }
3983
3984 if (type == &PyBytes_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003985 return save_bytes(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003986 }
3987 else if (type == &PyUnicode_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003988 return save_unicode(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003989 }
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003990
3991 /* We're only calling Py_EnterRecursiveCall here so that atomic
3992 types above are pickled faster. */
3993 if (Py_EnterRecursiveCall(" while pickling an object")) {
3994 return -1;
3995 }
3996
3997 if (type == &PyDict_Type) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003998 status = save_dict(self, obj);
3999 goto done;
4000 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004001 else if (type == &PySet_Type) {
4002 status = save_set(self, obj);
4003 goto done;
4004 }
4005 else if (type == &PyFrozenSet_Type) {
4006 status = save_frozenset(self, obj);
4007 goto done;
4008 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004009 else if (type == &PyList_Type) {
4010 status = save_list(self, obj);
4011 goto done;
4012 }
4013 else if (type == &PyTuple_Type) {
4014 status = save_tuple(self, obj);
4015 goto done;
4016 }
4017 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004018 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004019 goto done;
4020 }
4021 else if (type == &PyFunction_Type) {
4022 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004023 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004024 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004025
4026 /* XXX: This part needs some unit tests. */
4027
4028 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004029 * self.dispatch_table, copyreg.dispatch_table, the object's
4030 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004032 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004033 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004034 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4035 (PyObject *)type);
4036 if (reduce_func == NULL) {
4037 if (PyErr_Occurred()) {
4038 goto error;
4039 }
4040 } else {
4041 /* PyDict_GetItemWithError() returns a borrowed reference.
4042 Increase the reference count to be consistent with
4043 PyObject_GetItem and _PyObject_GetAttrId used below. */
4044 Py_INCREF(reduce_func);
4045 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004046 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004047 reduce_func = PyObject_GetItem(self->dispatch_table,
4048 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004049 if (reduce_func == NULL) {
4050 if (PyErr_ExceptionMatches(PyExc_KeyError))
4051 PyErr_Clear();
4052 else
4053 goto error;
4054 }
4055 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004056 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004057 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004058 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004059 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004060 else if (PyType_IsSubtype(type, &PyType_Type)) {
4061 status = save_global(self, obj, NULL);
4062 goto done;
4063 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004064 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004065 _Py_IDENTIFIER(__reduce__);
4066 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004067
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004068
4069 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4070 automatically defined as __reduce__. While this is convenient, this
4071 make it impossible to know which method was actually called. Of
4072 course, this is not a big deal. But still, it would be nice to let
4073 the user know which method was called when something go
4074 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4075 don't actually have to check for a __reduce__ method. */
4076
4077 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004078 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4079 goto error;
4080 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004081 if (reduce_func != NULL) {
4082 PyObject *proto;
4083 proto = PyLong_FromLong(self->proto);
4084 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004085 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004086 }
4087 }
4088 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004089 PickleState *st = _Pickle_GetGlobalState();
4090
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004091 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004092 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004094 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004095 }
4096 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004097 PyErr_Format(st->PicklingError,
4098 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099 type->tp_name, obj);
4100 goto error;
4101 }
4102 }
4103 }
4104
4105 if (reduce_value == NULL)
4106 goto error;
4107
4108 if (PyUnicode_Check(reduce_value)) {
4109 status = save_global(self, obj, reduce_value);
4110 goto done;
4111 }
4112
4113 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004114 PickleState *st = _Pickle_GetGlobalState();
4115 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004116 "__reduce__ must return a string or tuple");
4117 goto error;
4118 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004119
4120 status = save_reduce(self, reduce_value, obj);
4121
4122 if (0) {
4123 error:
4124 status = -1;
4125 }
4126 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004127
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004128 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129 Py_XDECREF(reduce_func);
4130 Py_XDECREF(reduce_value);
4131
4132 return status;
4133}
4134
4135static int
4136dump(PicklerObject *self, PyObject *obj)
4137{
4138 const char stop_op = STOP;
4139
4140 if (self->proto >= 2) {
4141 char header[2];
4142
4143 header[0] = PROTO;
4144 assert(self->proto >= 0 && self->proto < 256);
4145 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004146 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004147 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004148 if (self->proto >= 4)
4149 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004150 }
4151
4152 if (save(self, obj, 0) < 0 ||
Serhiy Storchakac8695292018-04-04 00:11:27 +03004153 _Pickler_Write(self, &stop_op, 1) < 0 ||
4154 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004155 return -1;
Serhiy Storchakac8695292018-04-04 00:11:27 +03004156 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004157 return 0;
4158}
4159
Larry Hastings61272b72014-01-07 12:41:53 -08004160/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004161
4162_pickle.Pickler.clear_memo
4163
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004164Clears the pickler's "memo".
4165
4166The memo is the data structure that remembers which objects the
4167pickler has already seen, so that shared or recursive objects are
4168pickled by reference and not by value. This method is useful when
4169re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004170[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004171
Larry Hastings3cceb382014-01-04 11:09:09 -08004172static PyObject *
4173_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004174/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004175{
4176 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004177 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004178
4179 Py_RETURN_NONE;
4180}
4181
Larry Hastings61272b72014-01-07 12:41:53 -08004182/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004183
4184_pickle.Pickler.dump
4185
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004186 obj: object
4187 /
4188
4189Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004190[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004191
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004192static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004193_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004194/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004195{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004196 /* Check whether the Pickler was initialized correctly (issue3664).
4197 Developers often forget to call __init__() in their subclasses, which
4198 would trigger a segfault without this check. */
4199 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004200 PickleState *st = _Pickle_GetGlobalState();
4201 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004202 "Pickler.__init__() was not called by %s.__init__()",
4203 Py_TYPE(self)->tp_name);
4204 return NULL;
4205 }
4206
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004207 if (_Pickler_ClearBuffer(self) < 0)
4208 return NULL;
4209
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004210 if (dump(self, obj) < 0)
4211 return NULL;
4212
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004213 if (_Pickler_FlushToFile(self) < 0)
4214 return NULL;
4215
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004216 Py_RETURN_NONE;
4217}
4218
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004219/*[clinic input]
4220
4221_pickle.Pickler.__sizeof__ -> Py_ssize_t
4222
4223Returns size in memory, in bytes.
4224[clinic start generated code]*/
4225
4226static Py_ssize_t
4227_pickle_Pickler___sizeof___impl(PicklerObject *self)
4228/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4229{
4230 Py_ssize_t res, s;
4231
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004232 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004233 if (self->memo != NULL) {
4234 res += sizeof(PyMemoTable);
4235 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4236 }
4237 if (self->output_buffer != NULL) {
4238 s = _PySys_GetSizeOf(self->output_buffer);
4239 if (s == -1)
4240 return -1;
4241 res += s;
4242 }
4243 return res;
4244}
4245
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004246static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004247 _PICKLE_PICKLER_DUMP_METHODDEF
4248 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004249 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004250 {NULL, NULL} /* sentinel */
4251};
4252
4253static void
4254Pickler_dealloc(PicklerObject *self)
4255{
4256 PyObject_GC_UnTrack(self);
4257
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004258 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004259 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004260 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004261 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004262 Py_XDECREF(self->fast_memo);
4263
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004264 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004265
4266 Py_TYPE(self)->tp_free((PyObject *)self);
4267}
4268
4269static int
4270Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4271{
4272 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004273 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004274 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004275 Py_VISIT(self->fast_memo);
4276 return 0;
4277}
4278
4279static int
4280Pickler_clear(PicklerObject *self)
4281{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004282 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004283 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004284 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004285 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004286 Py_CLEAR(self->fast_memo);
4287
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004288 if (self->memo != NULL) {
4289 PyMemoTable *memo = self->memo;
4290 self->memo = NULL;
4291 PyMemoTable_Del(memo);
4292 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004293 return 0;
4294}
4295
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004296
Larry Hastings61272b72014-01-07 12:41:53 -08004297/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004298
4299_pickle.Pickler.__init__
4300
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004301 file: object
4302 protocol: object = NULL
4303 fix_imports: bool = True
4304
4305This takes a binary file for writing a pickle data stream.
4306
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004307The optional *protocol* argument tells the pickler to use the given
4308protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4309protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004310
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004311Specifying a negative protocol version selects the highest protocol
4312version supported. The higher the protocol used, the more recent the
4313version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004314
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004315The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004316bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004317writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004318this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004319
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004320If *fix_imports* is True and protocol is less than 3, pickle will try
4321to map the new Python 3 names to the old module names used in Python
43222, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004323[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004324
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004325static int
Larry Hastings89964c42015-04-14 18:07:59 -04004326_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4327 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004328/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004329{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004330 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004331 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004332
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004333 /* In case of multiple __init__() calls, clear previous content. */
4334 if (self->write != NULL)
4335 (void)Pickler_clear(self);
4336
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004337 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004338 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004339
4340 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004341 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004342
4343 /* memo and output_buffer may have already been created in _Pickler_New */
4344 if (self->memo == NULL) {
4345 self->memo = PyMemoTable_New();
4346 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004347 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004348 }
4349 self->output_len = 0;
4350 if (self->output_buffer == NULL) {
4351 self->max_output_len = WRITE_BUF_SIZE;
4352 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4353 self->max_output_len);
4354 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004355 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004356 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004357
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004358 self->fast = 0;
4359 self->fast_nesting = 0;
4360 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004361
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004362 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4363 &self->pers_func, &self->pers_func_self) < 0)
4364 {
4365 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004366 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004367
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004368 if (_PyObject_LookupAttrId((PyObject *)self,
4369 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4370 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004371 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004372
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004373 return 0;
4374}
4375
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004376
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004377/* Define a proxy object for the Pickler's internal memo object. This is to
4378 * avoid breaking code like:
4379 * pickler.memo.clear()
4380 * and
4381 * pickler.memo = saved_memo
4382 * Is this a good idea? Not really, but we don't want to break code that uses
4383 * it. Note that we don't implement the entire mapping API here. This is
4384 * intentional, as these should be treated as black-box implementation details.
4385 */
4386
Larry Hastings61272b72014-01-07 12:41:53 -08004387/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004388_pickle.PicklerMemoProxy.clear
4389
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004390Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004391[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004392
Larry Hastings3cceb382014-01-04 11:09:09 -08004393static PyObject *
4394_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004395/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004396{
4397 if (self->pickler->memo)
4398 PyMemoTable_Clear(self->pickler->memo);
4399 Py_RETURN_NONE;
4400}
4401
Larry Hastings61272b72014-01-07 12:41:53 -08004402/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004403_pickle.PicklerMemoProxy.copy
4404
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004405Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004406[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004407
Larry Hastings3cceb382014-01-04 11:09:09 -08004408static PyObject *
4409_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004410/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004411{
4412 Py_ssize_t i;
4413 PyMemoTable *memo;
4414 PyObject *new_memo = PyDict_New();
4415 if (new_memo == NULL)
4416 return NULL;
4417
4418 memo = self->pickler->memo;
4419 for (i = 0; i < memo->mt_allocated; ++i) {
4420 PyMemoEntry entry = memo->mt_table[i];
4421 if (entry.me_key != NULL) {
4422 int status;
4423 PyObject *key, *value;
4424
4425 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004426 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004427
4428 if (key == NULL || value == NULL) {
4429 Py_XDECREF(key);
4430 Py_XDECREF(value);
4431 goto error;
4432 }
4433 status = PyDict_SetItem(new_memo, key, value);
4434 Py_DECREF(key);
4435 Py_DECREF(value);
4436 if (status < 0)
4437 goto error;
4438 }
4439 }
4440 return new_memo;
4441
4442 error:
4443 Py_XDECREF(new_memo);
4444 return NULL;
4445}
4446
Larry Hastings61272b72014-01-07 12:41:53 -08004447/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004448_pickle.PicklerMemoProxy.__reduce__
4449
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004450Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004451[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004452
Larry Hastings3cceb382014-01-04 11:09:09 -08004453static PyObject *
4454_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004455/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004456{
4457 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004458 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004459 if (contents == NULL)
4460 return NULL;
4461
4462 reduce_value = PyTuple_New(2);
4463 if (reduce_value == NULL) {
4464 Py_DECREF(contents);
4465 return NULL;
4466 }
4467 dict_args = PyTuple_New(1);
4468 if (dict_args == NULL) {
4469 Py_DECREF(contents);
4470 Py_DECREF(reduce_value);
4471 return NULL;
4472 }
4473 PyTuple_SET_ITEM(dict_args, 0, contents);
4474 Py_INCREF((PyObject *)&PyDict_Type);
4475 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4476 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4477 return reduce_value;
4478}
4479
4480static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004481 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4482 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4483 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004484 {NULL, NULL} /* sentinel */
4485};
4486
4487static void
4488PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4489{
4490 PyObject_GC_UnTrack(self);
4491 Py_XDECREF(self->pickler);
4492 PyObject_GC_Del((PyObject *)self);
4493}
4494
4495static int
4496PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4497 visitproc visit, void *arg)
4498{
4499 Py_VISIT(self->pickler);
4500 return 0;
4501}
4502
4503static int
4504PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4505{
4506 Py_CLEAR(self->pickler);
4507 return 0;
4508}
4509
4510static PyTypeObject PicklerMemoProxyType = {
4511 PyVarObject_HEAD_INIT(NULL, 0)
4512 "_pickle.PicklerMemoProxy", /*tp_name*/
4513 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4514 0,
4515 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4516 0, /* tp_print */
4517 0, /* tp_getattr */
4518 0, /* tp_setattr */
4519 0, /* tp_compare */
4520 0, /* tp_repr */
4521 0, /* tp_as_number */
4522 0, /* tp_as_sequence */
4523 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004524 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004525 0, /* tp_call */
4526 0, /* tp_str */
4527 PyObject_GenericGetAttr, /* tp_getattro */
4528 PyObject_GenericSetAttr, /* tp_setattro */
4529 0, /* tp_as_buffer */
4530 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4531 0, /* tp_doc */
4532 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4533 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4534 0, /* tp_richcompare */
4535 0, /* tp_weaklistoffset */
4536 0, /* tp_iter */
4537 0, /* tp_iternext */
4538 picklerproxy_methods, /* tp_methods */
4539};
4540
4541static PyObject *
4542PicklerMemoProxy_New(PicklerObject *pickler)
4543{
4544 PicklerMemoProxyObject *self;
4545
4546 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4547 if (self == NULL)
4548 return NULL;
4549 Py_INCREF(pickler);
4550 self->pickler = pickler;
4551 PyObject_GC_Track(self);
4552 return (PyObject *)self;
4553}
4554
4555/*****************************************************************************/
4556
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004557static PyObject *
4558Pickler_get_memo(PicklerObject *self)
4559{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004560 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004561}
4562
4563static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004564Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004565{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004566 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004567
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004568 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004569 PyErr_SetString(PyExc_TypeError,
4570 "attribute deletion is not supported");
4571 return -1;
4572 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004573
4574 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4575 PicklerObject *pickler =
4576 ((PicklerMemoProxyObject *)obj)->pickler;
4577
4578 new_memo = PyMemoTable_Copy(pickler->memo);
4579 if (new_memo == NULL)
4580 return -1;
4581 }
4582 else if (PyDict_Check(obj)) {
4583 Py_ssize_t i = 0;
4584 PyObject *key, *value;
4585
4586 new_memo = PyMemoTable_New();
4587 if (new_memo == NULL)
4588 return -1;
4589
4590 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004591 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004592 PyObject *memo_obj;
4593
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004594 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004595 PyErr_SetString(PyExc_TypeError,
4596 "'memo' values must be 2-item tuples");
4597 goto error;
4598 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004599 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004600 if (memo_id == -1 && PyErr_Occurred())
4601 goto error;
4602 memo_obj = PyTuple_GET_ITEM(value, 1);
4603 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4604 goto error;
4605 }
4606 }
4607 else {
4608 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004609 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004610 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004611 return -1;
4612 }
4613
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004614 PyMemoTable_Del(self->memo);
4615 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004616
4617 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004618
4619 error:
4620 if (new_memo)
4621 PyMemoTable_Del(new_memo);
4622 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004623}
4624
4625static PyObject *
4626Pickler_get_persid(PicklerObject *self)
4627{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004628 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004629 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004630 return NULL;
4631 }
4632 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004633}
4634
4635static int
4636Pickler_set_persid(PicklerObject *self, PyObject *value)
4637{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004638 if (value == NULL) {
4639 PyErr_SetString(PyExc_TypeError,
4640 "attribute deletion is not supported");
4641 return -1;
4642 }
4643 if (!PyCallable_Check(value)) {
4644 PyErr_SetString(PyExc_TypeError,
4645 "persistent_id must be a callable taking one argument");
4646 return -1;
4647 }
4648
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004649 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004650 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004651 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004652
4653 return 0;
4654}
4655
4656static PyMemberDef Pickler_members[] = {
4657 {"bin", T_INT, offsetof(PicklerObject, bin)},
4658 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004659 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004660 {NULL}
4661};
4662
4663static PyGetSetDef Pickler_getsets[] = {
4664 {"memo", (getter)Pickler_get_memo,
4665 (setter)Pickler_set_memo},
4666 {"persistent_id", (getter)Pickler_get_persid,
4667 (setter)Pickler_set_persid},
4668 {NULL}
4669};
4670
4671static PyTypeObject Pickler_Type = {
4672 PyVarObject_HEAD_INIT(NULL, 0)
4673 "_pickle.Pickler" , /*tp_name*/
4674 sizeof(PicklerObject), /*tp_basicsize*/
4675 0, /*tp_itemsize*/
4676 (destructor)Pickler_dealloc, /*tp_dealloc*/
4677 0, /*tp_print*/
4678 0, /*tp_getattr*/
4679 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004680 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004681 0, /*tp_repr*/
4682 0, /*tp_as_number*/
4683 0, /*tp_as_sequence*/
4684 0, /*tp_as_mapping*/
4685 0, /*tp_hash*/
4686 0, /*tp_call*/
4687 0, /*tp_str*/
4688 0, /*tp_getattro*/
4689 0, /*tp_setattro*/
4690 0, /*tp_as_buffer*/
4691 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004692 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004693 (traverseproc)Pickler_traverse, /*tp_traverse*/
4694 (inquiry)Pickler_clear, /*tp_clear*/
4695 0, /*tp_richcompare*/
4696 0, /*tp_weaklistoffset*/
4697 0, /*tp_iter*/
4698 0, /*tp_iternext*/
4699 Pickler_methods, /*tp_methods*/
4700 Pickler_members, /*tp_members*/
4701 Pickler_getsets, /*tp_getset*/
4702 0, /*tp_base*/
4703 0, /*tp_dict*/
4704 0, /*tp_descr_get*/
4705 0, /*tp_descr_set*/
4706 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004707 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004708 PyType_GenericAlloc, /*tp_alloc*/
4709 PyType_GenericNew, /*tp_new*/
4710 PyObject_GC_Del, /*tp_free*/
4711 0, /*tp_is_gc*/
4712};
4713
Victor Stinner121aab42011-09-29 23:40:53 +02004714/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004715
4716 XXX: It would be nice to able to avoid Python function call overhead, by
4717 using directly the C version of find_class(), when find_class() is not
4718 overridden by a subclass. Although, this could become rather hackish. A
4719 simpler optimization would be to call the C function when self is not a
4720 subclass instance. */
4721static PyObject *
4722find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4723{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004724 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004725
Victor Stinner55ba38a2016-12-09 16:09:30 +01004726 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4727 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004728}
4729
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004730static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004731marker(UnpicklerObject *self)
4732{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004733 Py_ssize_t mark;
4734
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004735 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004736 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004737 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004738 return -1;
4739 }
4740
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004741 mark = self->marks[--self->num_marks];
4742 self->stack->mark_set = self->num_marks != 0;
4743 self->stack->fence = self->num_marks ?
4744 self->marks[self->num_marks - 1] : 0;
4745 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004746}
4747
4748static int
4749load_none(UnpicklerObject *self)
4750{
4751 PDATA_APPEND(self->stack, Py_None, -1);
4752 return 0;
4753}
4754
4755static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004756load_int(UnpicklerObject *self)
4757{
4758 PyObject *value;
4759 char *endptr, *s;
4760 Py_ssize_t len;
4761 long x;
4762
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004763 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004764 return -1;
4765 if (len < 2)
4766 return bad_readline();
4767
4768 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004769 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004770 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004771 x = strtol(s, &endptr, 0);
4772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004773 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004774 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004775 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004776 errno = 0;
4777 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004778 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004779 if (value == NULL) {
4780 PyErr_SetString(PyExc_ValueError,
4781 "could not convert string to int");
4782 return -1;
4783 }
4784 }
4785 else {
4786 if (len == 3 && (x == 0 || x == 1)) {
4787 if ((value = PyBool_FromLong(x)) == NULL)
4788 return -1;
4789 }
4790 else {
4791 if ((value = PyLong_FromLong(x)) == NULL)
4792 return -1;
4793 }
4794 }
4795
4796 PDATA_PUSH(self->stack, value, -1);
4797 return 0;
4798}
4799
4800static int
4801load_bool(UnpicklerObject *self, PyObject *boolean)
4802{
4803 assert(boolean == Py_True || boolean == Py_False);
4804 PDATA_APPEND(self->stack, boolean, -1);
4805 return 0;
4806}
4807
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004808/* s contains x bytes of an unsigned little-endian integer. Return its value
4809 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4810 */
4811static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004812calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004813{
4814 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004815 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004816 size_t x = 0;
4817
Serhiy Storchakae0606192015-09-29 22:10:07 +03004818 if (nbytes > (int)sizeof(size_t)) {
4819 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4820 * have 64-bit size that can't be represented on 32-bit platform.
4821 */
4822 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4823 if (s[i])
4824 return -1;
4825 }
4826 nbytes = (int)sizeof(size_t);
4827 }
4828 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004829 x |= (size_t) s[i] << (8 * i);
4830 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004831
4832 if (x > PY_SSIZE_T_MAX)
4833 return -1;
4834 else
4835 return (Py_ssize_t) x;
4836}
4837
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004838/* s contains x bytes of a little-endian integer. Return its value as a
4839 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004840 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004841 * of x-platform bugs.
4842 */
4843static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004844calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004845{
4846 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004847 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004848 long x = 0;
4849
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004850 for (i = 0; i < nbytes; i++) {
4851 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004852 }
4853
4854 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4855 * is signed, so on a box with longs bigger than 4 bytes we need
4856 * to extend a BININT's sign bit to the full width.
4857 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004858 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004859 x |= -(x & (1L << 31));
4860 }
4861
4862 return x;
4863}
4864
4865static int
4866load_binintx(UnpicklerObject *self, char *s, int size)
4867{
4868 PyObject *value;
4869 long x;
4870
4871 x = calc_binint(s, size);
4872
4873 if ((value = PyLong_FromLong(x)) == NULL)
4874 return -1;
4875
4876 PDATA_PUSH(self->stack, value, -1);
4877 return 0;
4878}
4879
4880static int
4881load_binint(UnpicklerObject *self)
4882{
4883 char *s;
4884
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004885 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004886 return -1;
4887
4888 return load_binintx(self, s, 4);
4889}
4890
4891static int
4892load_binint1(UnpicklerObject *self)
4893{
4894 char *s;
4895
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004896 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004897 return -1;
4898
4899 return load_binintx(self, s, 1);
4900}
4901
4902static int
4903load_binint2(UnpicklerObject *self)
4904{
4905 char *s;
4906
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004907 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004908 return -1;
4909
4910 return load_binintx(self, s, 2);
4911}
4912
4913static int
4914load_long(UnpicklerObject *self)
4915{
4916 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004917 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004918 Py_ssize_t len;
4919
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004920 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004921 return -1;
4922 if (len < 2)
4923 return bad_readline();
4924
Mark Dickinson8dd05142009-01-20 20:43:58 +00004925 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4926 the 'L' before calling PyLong_FromString. In order to maintain
4927 compatibility with Python 3.0.0, we don't actually *require*
4928 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004929 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004930 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004931 /* XXX: Should the base argument explicitly set to 10? */
4932 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004933 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004934 return -1;
4935
4936 PDATA_PUSH(self->stack, value, -1);
4937 return 0;
4938}
4939
4940/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4941 * data following.
4942 */
4943static int
4944load_counted_long(UnpicklerObject *self, int size)
4945{
4946 PyObject *value;
4947 char *nbytes;
4948 char *pdata;
4949
4950 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004951 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004952 return -1;
4953
4954 size = calc_binint(nbytes, size);
4955 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004956 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004957 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004958 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004959 "LONG pickle has negative byte count");
4960 return -1;
4961 }
4962
4963 if (size == 0)
4964 value = PyLong_FromLong(0L);
4965 else {
4966 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004967 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968 return -1;
4969 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4970 1 /* little endian */ , 1 /* signed */ );
4971 }
4972 if (value == NULL)
4973 return -1;
4974 PDATA_PUSH(self->stack, value, -1);
4975 return 0;
4976}
4977
4978static int
4979load_float(UnpicklerObject *self)
4980{
4981 PyObject *value;
4982 char *endptr, *s;
4983 Py_ssize_t len;
4984 double d;
4985
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004986 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004987 return -1;
4988 if (len < 2)
4989 return bad_readline();
4990
4991 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004992 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4993 if (d == -1.0 && PyErr_Occurred())
4994 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004995 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004996 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4997 return -1;
4998 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004999 value = PyFloat_FromDouble(d);
5000 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005001 return -1;
5002
5003 PDATA_PUSH(self->stack, value, -1);
5004 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005005}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005006
5007static int
5008load_binfloat(UnpicklerObject *self)
5009{
5010 PyObject *value;
5011 double x;
5012 char *s;
5013
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005014 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005015 return -1;
5016
5017 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5018 if (x == -1.0 && PyErr_Occurred())
5019 return -1;
5020
5021 if ((value = PyFloat_FromDouble(x)) == NULL)
5022 return -1;
5023
5024 PDATA_PUSH(self->stack, value, -1);
5025 return 0;
5026}
5027
5028static int
5029load_string(UnpicklerObject *self)
5030{
5031 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005032 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005033 Py_ssize_t len;
5034 char *s, *p;
5035
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005036 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005037 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005038 /* Strip the newline */
5039 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005041 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042 p = s + 1;
5043 len -= 2;
5044 }
5045 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005046 PickleState *st = _Pickle_GetGlobalState();
5047 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005048 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005049 return -1;
5050 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005051 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005052
5053 /* Use the PyBytes API to decode the string, since that is what is used
5054 to encode, and then coerce the result to Unicode. */
5055 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005056 if (bytes == NULL)
5057 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005058
5059 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5060 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5061 if (strcmp(self->encoding, "bytes") == 0) {
5062 obj = bytes;
5063 }
5064 else {
5065 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5066 Py_DECREF(bytes);
5067 if (obj == NULL) {
5068 return -1;
5069 }
5070 }
5071
5072 PDATA_PUSH(self->stack, obj, -1);
5073 return 0;
5074}
5075
5076static int
5077load_counted_binstring(UnpicklerObject *self, int nbytes)
5078{
5079 PyObject *obj;
5080 Py_ssize_t size;
5081 char *s;
5082
5083 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005084 return -1;
5085
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005086 size = calc_binsize(s, nbytes);
5087 if (size < 0) {
5088 PickleState *st = _Pickle_GetGlobalState();
5089 PyErr_Format(st->UnpicklingError,
5090 "BINSTRING exceeds system's maximum size of %zd bytes",
5091 PY_SSIZE_T_MAX);
5092 return -1;
5093 }
5094
5095 if (_Unpickler_Read(self, &s, size) < 0)
5096 return -1;
5097
5098 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5099 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5100 if (strcmp(self->encoding, "bytes") == 0) {
5101 obj = PyBytes_FromStringAndSize(s, size);
5102 }
5103 else {
5104 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5105 }
5106 if (obj == NULL) {
5107 return -1;
5108 }
5109
5110 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005111 return 0;
5112}
5113
5114static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005115load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005116{
5117 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005118 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005119 char *s;
5120
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005121 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005122 return -1;
5123
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005124 size = calc_binsize(s, nbytes);
5125 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005126 PyErr_Format(PyExc_OverflowError,
5127 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005128 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005129 return -1;
5130 }
5131
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005132 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005133 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005134
5135 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005136 if (bytes == NULL)
5137 return -1;
5138
5139 PDATA_PUSH(self->stack, bytes, -1);
5140 return 0;
5141}
5142
5143static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005144load_unicode(UnpicklerObject *self)
5145{
5146 PyObject *str;
5147 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005148 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005149
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005150 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005151 return -1;
5152 if (len < 1)
5153 return bad_readline();
5154
5155 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5156 if (str == NULL)
5157 return -1;
5158
5159 PDATA_PUSH(self->stack, str, -1);
5160 return 0;
5161}
5162
5163static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005164load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005165{
5166 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005167 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005168 char *s;
5169
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005170 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005171 return -1;
5172
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005173 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005174 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005175 PyErr_Format(PyExc_OverflowError,
5176 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005177 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005178 return -1;
5179 }
5180
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005181 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005182 return -1;
5183
Victor Stinner485fb562010-04-13 11:07:24 +00005184 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005185 if (str == NULL)
5186 return -1;
5187
5188 PDATA_PUSH(self->stack, str, -1);
5189 return 0;
5190}
5191
5192static int
Victor Stinner21b47112016-03-14 18:09:39 +01005193load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005194{
5195 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005196
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005197 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005198 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005199
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005200 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005201 if (tuple == NULL)
5202 return -1;
5203 PDATA_PUSH(self->stack, tuple, -1);
5204 return 0;
5205}
5206
5207static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005208load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005209{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005210 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005211
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005212 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005213 return -1;
5214
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005215 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005216}
5217
5218static int
5219load_empty_list(UnpicklerObject *self)
5220{
5221 PyObject *list;
5222
5223 if ((list = PyList_New(0)) == NULL)
5224 return -1;
5225 PDATA_PUSH(self->stack, list, -1);
5226 return 0;
5227}
5228
5229static int
5230load_empty_dict(UnpicklerObject *self)
5231{
5232 PyObject *dict;
5233
5234 if ((dict = PyDict_New()) == NULL)
5235 return -1;
5236 PDATA_PUSH(self->stack, dict, -1);
5237 return 0;
5238}
5239
5240static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005241load_empty_set(UnpicklerObject *self)
5242{
5243 PyObject *set;
5244
5245 if ((set = PySet_New(NULL)) == NULL)
5246 return -1;
5247 PDATA_PUSH(self->stack, set, -1);
5248 return 0;
5249}
5250
5251static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005252load_list(UnpicklerObject *self)
5253{
5254 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005255 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005256
5257 if ((i = marker(self)) < 0)
5258 return -1;
5259
5260 list = Pdata_poplist(self->stack, i);
5261 if (list == NULL)
5262 return -1;
5263 PDATA_PUSH(self->stack, list, -1);
5264 return 0;
5265}
5266
5267static int
5268load_dict(UnpicklerObject *self)
5269{
5270 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005271 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005272
5273 if ((i = marker(self)) < 0)
5274 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005275 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005276
5277 if ((dict = PyDict_New()) == NULL)
5278 return -1;
5279
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005280 if ((j - i) % 2 != 0) {
5281 PickleState *st = _Pickle_GetGlobalState();
5282 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005283 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005284 return -1;
5285 }
5286
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005287 for (k = i + 1; k < j; k += 2) {
5288 key = self->stack->data[k - 1];
5289 value = self->stack->data[k];
5290 if (PyDict_SetItem(dict, key, value) < 0) {
5291 Py_DECREF(dict);
5292 return -1;
5293 }
5294 }
5295 Pdata_clear(self->stack, i);
5296 PDATA_PUSH(self->stack, dict, -1);
5297 return 0;
5298}
5299
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005300static int
5301load_frozenset(UnpicklerObject *self)
5302{
5303 PyObject *items;
5304 PyObject *frozenset;
5305 Py_ssize_t i;
5306
5307 if ((i = marker(self)) < 0)
5308 return -1;
5309
5310 items = Pdata_poptuple(self->stack, i);
5311 if (items == NULL)
5312 return -1;
5313
5314 frozenset = PyFrozenSet_New(items);
5315 Py_DECREF(items);
5316 if (frozenset == NULL)
5317 return -1;
5318
5319 PDATA_PUSH(self->stack, frozenset, -1);
5320 return 0;
5321}
5322
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005323static PyObject *
5324instantiate(PyObject *cls, PyObject *args)
5325{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005326 /* Caller must assure args are a tuple. Normally, args come from
5327 Pdata_poptuple which packs objects from the top of the stack
5328 into a newly created tuple. */
5329 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005330 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5331 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005332 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005333 PyObject *func;
5334 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5335 return NULL;
5336 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005337 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005338 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5339 }
5340 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005341 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005342 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005343}
5344
5345static int
5346load_obj(UnpicklerObject *self)
5347{
5348 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005349 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005350
5351 if ((i = marker(self)) < 0)
5352 return -1;
5353
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005354 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005355 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005356
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005357 args = Pdata_poptuple(self->stack, i + 1);
5358 if (args == NULL)
5359 return -1;
5360
5361 PDATA_POP(self->stack, cls);
5362 if (cls) {
5363 obj = instantiate(cls, args);
5364 Py_DECREF(cls);
5365 }
5366 Py_DECREF(args);
5367 if (obj == NULL)
5368 return -1;
5369
5370 PDATA_PUSH(self->stack, obj, -1);
5371 return 0;
5372}
5373
5374static int
5375load_inst(UnpicklerObject *self)
5376{
5377 PyObject *cls = NULL;
5378 PyObject *args = NULL;
5379 PyObject *obj = NULL;
5380 PyObject *module_name;
5381 PyObject *class_name;
5382 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005383 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005384 char *s;
5385
5386 if ((i = marker(self)) < 0)
5387 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005388 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005389 return -1;
5390 if (len < 2)
5391 return bad_readline();
5392
5393 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5394 identifiers are permitted in Python 3.0, since the INST opcode is only
5395 supported by older protocols on Python 2.x. */
5396 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5397 if (module_name == NULL)
5398 return -1;
5399
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005400 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005401 if (len < 2) {
5402 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005403 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005404 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005405 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005406 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005407 cls = find_class(self, module_name, class_name);
5408 Py_DECREF(class_name);
5409 }
5410 }
5411 Py_DECREF(module_name);
5412
5413 if (cls == NULL)
5414 return -1;
5415
5416 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5417 obj = instantiate(cls, args);
5418 Py_DECREF(args);
5419 }
5420 Py_DECREF(cls);
5421
5422 if (obj == NULL)
5423 return -1;
5424
5425 PDATA_PUSH(self->stack, obj, -1);
5426 return 0;
5427}
5428
5429static int
5430load_newobj(UnpicklerObject *self)
5431{
5432 PyObject *args = NULL;
5433 PyObject *clsraw = NULL;
5434 PyTypeObject *cls; /* clsraw cast to its true type */
5435 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005436 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005437
5438 /* Stack is ... cls argtuple, and we want to call
5439 * cls.__new__(cls, *argtuple).
5440 */
5441 PDATA_POP(self->stack, args);
5442 if (args == NULL)
5443 goto error;
5444 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005445 PyErr_SetString(st->UnpicklingError,
5446 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005447 goto error;
5448 }
5449
5450 PDATA_POP(self->stack, clsraw);
5451 cls = (PyTypeObject *)clsraw;
5452 if (cls == NULL)
5453 goto error;
5454 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005455 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005456 "isn't a type object");
5457 goto error;
5458 }
5459 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005460 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461 "has NULL tp_new");
5462 goto error;
5463 }
5464
5465 /* Call __new__. */
5466 obj = cls->tp_new(cls, args, NULL);
5467 if (obj == NULL)
5468 goto error;
5469
5470 Py_DECREF(args);
5471 Py_DECREF(clsraw);
5472 PDATA_PUSH(self->stack, obj, -1);
5473 return 0;
5474
5475 error:
5476 Py_XDECREF(args);
5477 Py_XDECREF(clsraw);
5478 return -1;
5479}
5480
5481static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005482load_newobj_ex(UnpicklerObject *self)
5483{
5484 PyObject *cls, *args, *kwargs;
5485 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005486 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005487
5488 PDATA_POP(self->stack, kwargs);
5489 if (kwargs == NULL) {
5490 return -1;
5491 }
5492 PDATA_POP(self->stack, args);
5493 if (args == NULL) {
5494 Py_DECREF(kwargs);
5495 return -1;
5496 }
5497 PDATA_POP(self->stack, cls);
5498 if (cls == NULL) {
5499 Py_DECREF(kwargs);
5500 Py_DECREF(args);
5501 return -1;
5502 }
Larry Hastings61272b72014-01-07 12:41:53 -08005503
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005504 if (!PyType_Check(cls)) {
5505 Py_DECREF(kwargs);
5506 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005507 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005508 "NEWOBJ_EX class argument must be a type, not %.200s",
5509 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005510 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005511 return -1;
5512 }
5513
5514 if (((PyTypeObject *)cls)->tp_new == NULL) {
5515 Py_DECREF(kwargs);
5516 Py_DECREF(args);
5517 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005518 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005519 "NEWOBJ_EX class argument doesn't have __new__");
5520 return -1;
5521 }
5522 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5523 Py_DECREF(kwargs);
5524 Py_DECREF(args);
5525 Py_DECREF(cls);
5526 if (obj == NULL) {
5527 return -1;
5528 }
5529 PDATA_PUSH(self->stack, obj, -1);
5530 return 0;
5531}
5532
5533static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005534load_global(UnpicklerObject *self)
5535{
5536 PyObject *global = NULL;
5537 PyObject *module_name;
5538 PyObject *global_name;
5539 Py_ssize_t len;
5540 char *s;
5541
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005542 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005543 return -1;
5544 if (len < 2)
5545 return bad_readline();
5546 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5547 if (!module_name)
5548 return -1;
5549
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005550 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005551 if (len < 2) {
5552 Py_DECREF(module_name);
5553 return bad_readline();
5554 }
5555 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5556 if (global_name) {
5557 global = find_class(self, module_name, global_name);
5558 Py_DECREF(global_name);
5559 }
5560 }
5561 Py_DECREF(module_name);
5562
5563 if (global == NULL)
5564 return -1;
5565 PDATA_PUSH(self->stack, global, -1);
5566 return 0;
5567}
5568
5569static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005570load_stack_global(UnpicklerObject *self)
5571{
5572 PyObject *global;
5573 PyObject *module_name;
5574 PyObject *global_name;
5575
5576 PDATA_POP(self->stack, global_name);
5577 PDATA_POP(self->stack, module_name);
5578 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5579 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005580 PickleState *st = _Pickle_GetGlobalState();
5581 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005582 Py_XDECREF(global_name);
5583 Py_XDECREF(module_name);
5584 return -1;
5585 }
5586 global = find_class(self, module_name, global_name);
5587 Py_DECREF(global_name);
5588 Py_DECREF(module_name);
5589 if (global == NULL)
5590 return -1;
5591 PDATA_PUSH(self->stack, global, -1);
5592 return 0;
5593}
5594
5595static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005596load_persid(UnpicklerObject *self)
5597{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005598 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599 Py_ssize_t len;
5600 char *s;
5601
5602 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005603 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005605 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 return bad_readline();
5607
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005608 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5609 if (pid == NULL) {
5610 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5611 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5612 "persistent IDs in protocol 0 must be "
5613 "ASCII strings");
5614 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005616 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005617
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005618 obj = call_method(self->pers_func, self->pers_func_self, pid);
5619 Py_DECREF(pid);
5620 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005621 return -1;
5622
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005623 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005624 return 0;
5625 }
5626 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005627 PickleState *st = _Pickle_GetGlobalState();
5628 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005629 "A load persistent id instruction was encountered,\n"
5630 "but no persistent_load function was specified.");
5631 return -1;
5632 }
5633}
5634
5635static int
5636load_binpersid(UnpicklerObject *self)
5637{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005638 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005639
5640 if (self->pers_func) {
5641 PDATA_POP(self->stack, pid);
5642 if (pid == NULL)
5643 return -1;
5644
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005645 obj = call_method(self->pers_func, self->pers_func_self, pid);
5646 Py_DECREF(pid);
5647 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005648 return -1;
5649
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005650 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005651 return 0;
5652 }
5653 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005654 PickleState *st = _Pickle_GetGlobalState();
5655 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005656 "A load persistent id instruction was encountered,\n"
5657 "but no persistent_load function was specified.");
5658 return -1;
5659 }
5660}
5661
5662static int
5663load_pop(UnpicklerObject *self)
5664{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005665 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005666
5667 /* Note that we split the (pickle.py) stack into two stacks,
5668 * an object stack and a mark stack. We have to be clever and
5669 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005670 * mark stack first, and only signalling a stack underflow if
5671 * the object stack is empty and the mark stack doesn't match
5672 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005673 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005674 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005675 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005676 self->stack->mark_set = self->num_marks != 0;
5677 self->stack->fence = self->num_marks ?
5678 self->marks[self->num_marks - 1] : 0;
5679 } else if (len <= self->stack->fence)
5680 return Pdata_stack_underflow(self->stack);
5681 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682 len--;
5683 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005684 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005685 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005686 return 0;
5687}
5688
5689static int
5690load_pop_mark(UnpicklerObject *self)
5691{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005692 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005693
5694 if ((i = marker(self)) < 0)
5695 return -1;
5696
5697 Pdata_clear(self->stack, i);
5698
5699 return 0;
5700}
5701
5702static int
5703load_dup(UnpicklerObject *self)
5704{
5705 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005706 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005707
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005708 if (len <= self->stack->fence)
5709 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005710 last = self->stack->data[len - 1];
5711 PDATA_APPEND(self->stack, last, -1);
5712 return 0;
5713}
5714
5715static int
5716load_get(UnpicklerObject *self)
5717{
5718 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005719 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005720 Py_ssize_t len;
5721 char *s;
5722
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005723 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005724 return -1;
5725 if (len < 2)
5726 return bad_readline();
5727
5728 key = PyLong_FromString(s, NULL, 10);
5729 if (key == NULL)
5730 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005731 idx = PyLong_AsSsize_t(key);
5732 if (idx == -1 && PyErr_Occurred()) {
5733 Py_DECREF(key);
5734 return -1;
5735 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005736
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005737 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005738 if (value == NULL) {
5739 if (!PyErr_Occurred())
5740 PyErr_SetObject(PyExc_KeyError, key);
5741 Py_DECREF(key);
5742 return -1;
5743 }
5744 Py_DECREF(key);
5745
5746 PDATA_APPEND(self->stack, value, -1);
5747 return 0;
5748}
5749
5750static int
5751load_binget(UnpicklerObject *self)
5752{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005753 PyObject *value;
5754 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005755 char *s;
5756
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005757 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005758 return -1;
5759
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005760 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005761
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005762 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005764 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005765 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005767 Py_DECREF(key);
5768 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005769 return -1;
5770 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771
5772 PDATA_APPEND(self->stack, value, -1);
5773 return 0;
5774}
5775
5776static int
5777load_long_binget(UnpicklerObject *self)
5778{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005779 PyObject *value;
5780 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005782
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005783 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784 return -1;
5785
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005786 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005787
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005788 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005791 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005792 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005793 Py_DECREF(key);
5794 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005795 return -1;
5796 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797
5798 PDATA_APPEND(self->stack, value, -1);
5799 return 0;
5800}
5801
5802/* Push an object from the extension registry (EXT[124]). nbytes is
5803 * the number of bytes following the opcode, holding the index (code) value.
5804 */
5805static int
5806load_extension(UnpicklerObject *self, int nbytes)
5807{
5808 char *codebytes; /* the nbytes bytes after the opcode */
5809 long code; /* calc_binint returns long */
5810 PyObject *py_code; /* code as a Python int */
5811 PyObject *obj; /* the object to push */
5812 PyObject *pair; /* (module_name, class_name) */
5813 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005814 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005815
5816 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005817 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005818 return -1;
5819 code = calc_binint(codebytes, nbytes);
5820 if (code <= 0) { /* note that 0 is forbidden */
5821 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005822 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005823 return -1;
5824 }
5825
5826 /* Look for the code in the cache. */
5827 py_code = PyLong_FromLong(code);
5828 if (py_code == NULL)
5829 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005830 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005831 if (obj != NULL) {
5832 /* Bingo. */
5833 Py_DECREF(py_code);
5834 PDATA_APPEND(self->stack, obj, -1);
5835 return 0;
5836 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005837 if (PyErr_Occurred()) {
5838 Py_DECREF(py_code);
5839 return -1;
5840 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005841
5842 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005843 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005844 if (pair == NULL) {
5845 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005846 if (!PyErr_Occurred()) {
5847 PyErr_Format(PyExc_ValueError, "unregistered extension "
5848 "code %ld", code);
5849 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005850 return -1;
5851 }
5852 /* Since the extension registry is manipulable via Python code,
5853 * confirm that pair is really a 2-tuple of strings.
5854 */
5855 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5856 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5857 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5858 Py_DECREF(py_code);
5859 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5860 "isn't a 2-tuple of strings", code);
5861 return -1;
5862 }
5863 /* Load the object. */
5864 obj = find_class(self, module_name, class_name);
5865 if (obj == NULL) {
5866 Py_DECREF(py_code);
5867 return -1;
5868 }
5869 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005870 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005871 Py_DECREF(py_code);
5872 if (code < 0) {
5873 Py_DECREF(obj);
5874 return -1;
5875 }
5876 PDATA_PUSH(self->stack, obj, -1);
5877 return 0;
5878}
5879
5880static int
5881load_put(UnpicklerObject *self)
5882{
5883 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005884 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005885 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005886 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005887
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005888 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005889 return -1;
5890 if (len < 2)
5891 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005892 if (Py_SIZE(self->stack) <= self->stack->fence)
5893 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005894 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005895
5896 key = PyLong_FromString(s, NULL, 10);
5897 if (key == NULL)
5898 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005899 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005900 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005901 if (idx < 0) {
5902 if (!PyErr_Occurred())
5903 PyErr_SetString(PyExc_ValueError,
5904 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005905 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005906 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005907
5908 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005909}
5910
5911static int
5912load_binput(UnpicklerObject *self)
5913{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005914 PyObject *value;
5915 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005916 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005917
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005918 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005919 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005920
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005921 if (Py_SIZE(self->stack) <= self->stack->fence)
5922 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005923 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005925 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005926
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005927 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005928}
5929
5930static int
5931load_long_binput(UnpicklerObject *self)
5932{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005933 PyObject *value;
5934 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005935 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005936
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005937 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005938 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005939
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005940 if (Py_SIZE(self->stack) <= self->stack->fence)
5941 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005942 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005943
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005944 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005945 if (idx < 0) {
5946 PyErr_SetString(PyExc_ValueError,
5947 "negative LONG_BINPUT argument");
5948 return -1;
5949 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005950
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005951 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005952}
5953
5954static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005955load_memoize(UnpicklerObject *self)
5956{
5957 PyObject *value;
5958
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005959 if (Py_SIZE(self->stack) <= self->stack->fence)
5960 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005961 value = self->stack->data[Py_SIZE(self->stack) - 1];
5962
5963 return _Unpickler_MemoPut(self, self->memo_len, value);
5964}
5965
5966static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005967do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005968{
5969 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005970 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005971 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005972 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005973 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005974
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005975 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005976 if (x > len || x <= self->stack->fence)
5977 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005978 if (len == x) /* nothing to do */
5979 return 0;
5980
5981 list = self->stack->data[x - 1];
5982
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005983 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005984 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005985 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005986
5987 slice = Pdata_poplist(self->stack, x);
5988 if (!slice)
5989 return -1;
5990 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005991 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005992 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005993 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005994 }
5995 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005996 PyObject *extend_func;
5997 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005998
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005999 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6000 if (extend_func != NULL) {
6001 slice = Pdata_poplist(self->stack, x);
6002 if (!slice) {
6003 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006004 return -1;
6005 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006006 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006007 Py_DECREF(extend_func);
6008 if (result == NULL)
6009 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006010 Py_DECREF(result);
6011 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006012 else {
6013 PyObject *append_func;
6014 _Py_IDENTIFIER(append);
6015
6016 /* Even if the PEP 307 requires extend() and append() methods,
6017 fall back on append() if the object has no extend() method
6018 for backward compatibility. */
6019 PyErr_Clear();
6020 append_func = _PyObject_GetAttrId(list, &PyId_append);
6021 if (append_func == NULL)
6022 return -1;
6023 for (i = x; i < len; i++) {
6024 value = self->stack->data[i];
6025 result = _Pickle_FastCall(append_func, value);
6026 if (result == NULL) {
6027 Pdata_clear(self->stack, i + 1);
6028 Py_SIZE(self->stack) = x;
6029 Py_DECREF(append_func);
6030 return -1;
6031 }
6032 Py_DECREF(result);
6033 }
6034 Py_SIZE(self->stack) = x;
6035 Py_DECREF(append_func);
6036 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006037 }
6038
6039 return 0;
6040}
6041
6042static int
6043load_append(UnpicklerObject *self)
6044{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006045 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6046 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006047 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006048}
6049
6050static int
6051load_appends(UnpicklerObject *self)
6052{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006053 Py_ssize_t i = marker(self);
6054 if (i < 0)
6055 return -1;
6056 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006057}
6058
6059static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006060do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006061{
6062 PyObject *value, *key;
6063 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006064 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006065 int status = 0;
6066
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006067 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006068 if (x > len || x <= self->stack->fence)
6069 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006070 if (len == x) /* nothing to do */
6071 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006072 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006073 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006074 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006075 PyErr_SetString(st->UnpicklingError,
6076 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006077 return -1;
6078 }
6079
6080 /* Here, dict does not actually need to be a PyDict; it could be anything
6081 that supports the __setitem__ attribute. */
6082 dict = self->stack->data[x - 1];
6083
6084 for (i = x + 1; i < len; i += 2) {
6085 key = self->stack->data[i - 1];
6086 value = self->stack->data[i];
6087 if (PyObject_SetItem(dict, key, value) < 0) {
6088 status = -1;
6089 break;
6090 }
6091 }
6092
6093 Pdata_clear(self->stack, x);
6094 return status;
6095}
6096
6097static int
6098load_setitem(UnpicklerObject *self)
6099{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006100 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006101}
6102
6103static int
6104load_setitems(UnpicklerObject *self)
6105{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006106 Py_ssize_t i = marker(self);
6107 if (i < 0)
6108 return -1;
6109 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006110}
6111
6112static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006113load_additems(UnpicklerObject *self)
6114{
6115 PyObject *set;
6116 Py_ssize_t mark, len, i;
6117
6118 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006119 if (mark < 0)
6120 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006121 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006122 if (mark > len || mark <= self->stack->fence)
6123 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006124 if (len == mark) /* nothing to do */
6125 return 0;
6126
6127 set = self->stack->data[mark - 1];
6128
6129 if (PySet_Check(set)) {
6130 PyObject *items;
6131 int status;
6132
6133 items = Pdata_poptuple(self->stack, mark);
6134 if (items == NULL)
6135 return -1;
6136
6137 status = _PySet_Update(set, items);
6138 Py_DECREF(items);
6139 return status;
6140 }
6141 else {
6142 PyObject *add_func;
6143 _Py_IDENTIFIER(add);
6144
6145 add_func = _PyObject_GetAttrId(set, &PyId_add);
6146 if (add_func == NULL)
6147 return -1;
6148 for (i = mark; i < len; i++) {
6149 PyObject *result;
6150 PyObject *item;
6151
6152 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006153 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006154 if (result == NULL) {
6155 Pdata_clear(self->stack, i + 1);
6156 Py_SIZE(self->stack) = mark;
6157 return -1;
6158 }
6159 Py_DECREF(result);
6160 }
6161 Py_SIZE(self->stack) = mark;
6162 }
6163
6164 return 0;
6165}
6166
6167static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006168load_build(UnpicklerObject *self)
6169{
6170 PyObject *state, *inst, *slotstate;
6171 PyObject *setstate;
6172 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006173 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006174
6175 /* Stack is ... instance, state. We want to leave instance at
6176 * the stack top, possibly mutated via instance.__setstate__(state).
6177 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006178 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6179 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006180
6181 PDATA_POP(self->stack, state);
6182 if (state == NULL)
6183 return -1;
6184
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006185 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006186
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006187 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6188 Py_DECREF(state);
6189 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006190 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006191 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006192 PyObject *result;
6193
6194 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006195 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006196 Py_DECREF(setstate);
6197 if (result == NULL)
6198 return -1;
6199 Py_DECREF(result);
6200 return 0;
6201 }
6202
6203 /* A default __setstate__. First see whether state embeds a
6204 * slot state dict too (a proto 2 addition).
6205 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006206 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207 PyObject *tmp = state;
6208
6209 state = PyTuple_GET_ITEM(tmp, 0);
6210 slotstate = PyTuple_GET_ITEM(tmp, 1);
6211 Py_INCREF(state);
6212 Py_INCREF(slotstate);
6213 Py_DECREF(tmp);
6214 }
6215 else
6216 slotstate = NULL;
6217
6218 /* Set inst.__dict__ from the state dict (if any). */
6219 if (state != Py_None) {
6220 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006221 PyObject *d_key, *d_value;
6222 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006223 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006224
6225 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006226 PickleState *st = _Pickle_GetGlobalState();
6227 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006228 goto error;
6229 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006230 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006231 if (dict == NULL)
6232 goto error;
6233
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006234 i = 0;
6235 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6236 /* normally the keys for instance attributes are
6237 interned. we should try to do that here. */
6238 Py_INCREF(d_key);
6239 if (PyUnicode_CheckExact(d_key))
6240 PyUnicode_InternInPlace(&d_key);
6241 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6242 Py_DECREF(d_key);
6243 goto error;
6244 }
6245 Py_DECREF(d_key);
6246 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006247 Py_DECREF(dict);
6248 }
6249
6250 /* Also set instance attributes from the slotstate dict (if any). */
6251 if (slotstate != NULL) {
6252 PyObject *d_key, *d_value;
6253 Py_ssize_t i;
6254
6255 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006256 PickleState *st = _Pickle_GetGlobalState();
6257 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006258 "slot state is not a dictionary");
6259 goto error;
6260 }
6261 i = 0;
6262 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6263 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6264 goto error;
6265 }
6266 }
6267
6268 if (0) {
6269 error:
6270 status = -1;
6271 }
6272
6273 Py_DECREF(state);
6274 Py_XDECREF(slotstate);
6275 return status;
6276}
6277
6278static int
6279load_mark(UnpicklerObject *self)
6280{
6281
6282 /* Note that we split the (pickle.py) stack into two stacks, an
6283 * object stack and a mark stack. Here we push a mark onto the
6284 * mark stack.
6285 */
6286
6287 if ((self->num_marks + 1) >= self->marks_size) {
6288 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006289
6290 /* Use the size_t type to check for overflow. */
6291 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006292 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006293 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006294 PyErr_NoMemory();
6295 return -1;
6296 }
6297
6298 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006299 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006300 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006301 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6302 if (self->marks == NULL) {
6303 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006304 PyErr_NoMemory();
6305 return -1;
6306 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006307 self->marks_size = (Py_ssize_t)alloc;
6308 }
6309
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006310 self->stack->mark_set = 1;
6311 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006312
6313 return 0;
6314}
6315
6316static int
6317load_reduce(UnpicklerObject *self)
6318{
6319 PyObject *callable = NULL;
6320 PyObject *argtup = NULL;
6321 PyObject *obj = NULL;
6322
6323 PDATA_POP(self->stack, argtup);
6324 if (argtup == NULL)
6325 return -1;
6326 PDATA_POP(self->stack, callable);
6327 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006328 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006329 Py_DECREF(callable);
6330 }
6331 Py_DECREF(argtup);
6332
6333 if (obj == NULL)
6334 return -1;
6335
6336 PDATA_PUSH(self->stack, obj, -1);
6337 return 0;
6338}
6339
6340/* Just raises an error if we don't know the protocol specified. PROTO
6341 * is the first opcode for protocols >= 2.
6342 */
6343static int
6344load_proto(UnpicklerObject *self)
6345{
6346 char *s;
6347 int i;
6348
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006349 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006350 return -1;
6351
6352 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006353 if (i <= HIGHEST_PROTOCOL) {
6354 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006355 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006356 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006357
6358 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6359 return -1;
6360}
6361
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006362static int
6363load_frame(UnpicklerObject *self)
6364{
6365 char *s;
6366 Py_ssize_t frame_len;
6367
6368 if (_Unpickler_Read(self, &s, 8) < 0)
6369 return -1;
6370
6371 frame_len = calc_binsize(s, 8);
6372 if (frame_len < 0) {
6373 PyErr_Format(PyExc_OverflowError,
6374 "FRAME length exceeds system's maximum of %zd bytes",
6375 PY_SSIZE_T_MAX);
6376 return -1;
6377 }
6378
6379 if (_Unpickler_Read(self, &s, frame_len) < 0)
6380 return -1;
6381
6382 /* Rewind to start of frame */
6383 self->next_read_idx -= frame_len;
6384 return 0;
6385}
6386
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006387static PyObject *
6388load(UnpicklerObject *self)
6389{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006390 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006391 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006392
6393 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006394 self->stack->mark_set = 0;
6395 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006396 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006397 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006398 Pdata_clear(self->stack, 0);
6399
6400 /* Convenient macros for the dispatch while-switch loop just below. */
6401#define OP(opcode, load_func) \
6402 case opcode: if (load_func(self) < 0) break; continue;
6403
6404#define OP_ARG(opcode, load_func, arg) \
6405 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6406
6407 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006408 if (_Unpickler_Read(self, &s, 1) < 0) {
6409 PickleState *st = _Pickle_GetGlobalState();
6410 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6411 PyErr_Format(PyExc_EOFError, "Ran out of input");
6412 }
6413 return NULL;
6414 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006415
6416 switch ((enum opcode)s[0]) {
6417 OP(NONE, load_none)
6418 OP(BININT, load_binint)
6419 OP(BININT1, load_binint1)
6420 OP(BININT2, load_binint2)
6421 OP(INT, load_int)
6422 OP(LONG, load_long)
6423 OP_ARG(LONG1, load_counted_long, 1)
6424 OP_ARG(LONG4, load_counted_long, 4)
6425 OP(FLOAT, load_float)
6426 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006427 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6428 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6429 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6430 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6431 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006432 OP(STRING, load_string)
6433 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006434 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6435 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6436 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006437 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6438 OP_ARG(TUPLE1, load_counted_tuple, 1)
6439 OP_ARG(TUPLE2, load_counted_tuple, 2)
6440 OP_ARG(TUPLE3, load_counted_tuple, 3)
6441 OP(TUPLE, load_tuple)
6442 OP(EMPTY_LIST, load_empty_list)
6443 OP(LIST, load_list)
6444 OP(EMPTY_DICT, load_empty_dict)
6445 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006446 OP(EMPTY_SET, load_empty_set)
6447 OP(ADDITEMS, load_additems)
6448 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006449 OP(OBJ, load_obj)
6450 OP(INST, load_inst)
6451 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006452 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006453 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006454 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006455 OP(APPEND, load_append)
6456 OP(APPENDS, load_appends)
6457 OP(BUILD, load_build)
6458 OP(DUP, load_dup)
6459 OP(BINGET, load_binget)
6460 OP(LONG_BINGET, load_long_binget)
6461 OP(GET, load_get)
6462 OP(MARK, load_mark)
6463 OP(BINPUT, load_binput)
6464 OP(LONG_BINPUT, load_long_binput)
6465 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006466 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006467 OP(POP, load_pop)
6468 OP(POP_MARK, load_pop_mark)
6469 OP(SETITEM, load_setitem)
6470 OP(SETITEMS, load_setitems)
6471 OP(PERSID, load_persid)
6472 OP(BINPERSID, load_binpersid)
6473 OP(REDUCE, load_reduce)
6474 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006475 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006476 OP_ARG(EXT1, load_extension, 1)
6477 OP_ARG(EXT2, load_extension, 2)
6478 OP_ARG(EXT4, load_extension, 4)
6479 OP_ARG(NEWTRUE, load_bool, Py_True)
6480 OP_ARG(NEWFALSE, load_bool, Py_False)
6481
6482 case STOP:
6483 break;
6484
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006485 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006486 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006487 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006488 unsigned char c = (unsigned char) *s;
6489 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6490 PyErr_Format(st->UnpicklingError,
6491 "invalid load key, '%c'.", c);
6492 }
6493 else {
6494 PyErr_Format(st->UnpicklingError,
6495 "invalid load key, '\\x%02x'.", c);
6496 }
6497 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006498 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006499 }
6500
6501 break; /* and we are done! */
6502 }
6503
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006504 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006505 return NULL;
6506 }
6507
Victor Stinner2ae57e32013-10-31 13:39:23 +01006508 if (_Unpickler_SkipConsumed(self) < 0)
6509 return NULL;
6510
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006511 PDATA_POP(self->stack, value);
6512 return value;
6513}
6514
Larry Hastings61272b72014-01-07 12:41:53 -08006515/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006516
6517_pickle.Unpickler.load
6518
6519Load a pickle.
6520
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006521Read a pickled object representation from the open file object given
6522in the constructor, and return the reconstituted object hierarchy
6523specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006524[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006525
Larry Hastings3cceb382014-01-04 11:09:09 -08006526static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006527_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006528/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006529{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006530 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006531
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006532 /* Check whether the Unpickler was initialized correctly. This prevents
6533 segfaulting if a subclass overridden __init__ with a function that does
6534 not call Unpickler.__init__(). Here, we simply ensure that self->read
6535 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006536 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006537 PickleState *st = _Pickle_GetGlobalState();
6538 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006540 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006541 return NULL;
6542 }
6543
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006544 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006545}
6546
6547/* The name of find_class() is misleading. In newer pickle protocols, this
6548 function is used for loading any global (i.e., functions), not just
6549 classes. The name is kept only for backward compatibility. */
6550
Larry Hastings61272b72014-01-07 12:41:53 -08006551/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006552
6553_pickle.Unpickler.find_class
6554
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006555 module_name: object
6556 global_name: object
6557 /
6558
6559Return an object from a specified module.
6560
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006561If necessary, the module will be imported. Subclasses may override
6562this method (e.g. to restrict unpickling of arbitrary classes and
6563functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006564
6565This method is called whenever a class or a function object is
6566needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006567[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006568
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006569static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006570_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6571 PyObject *module_name,
6572 PyObject *global_name)
6573/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006574{
6575 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006576 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006577
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006578 /* Try to map the old names used in Python 2.x to the new ones used in
6579 Python 3.x. We do this only with old pickle protocols and when the
6580 user has not disabled the feature. */
6581 if (self->proto < 3 && self->fix_imports) {
6582 PyObject *key;
6583 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006584 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006585
6586 /* Check if the global (i.e., a function or a class) was renamed
6587 or moved to another module. */
6588 key = PyTuple_Pack(2, module_name, global_name);
6589 if (key == NULL)
6590 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006591 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006592 Py_DECREF(key);
6593 if (item) {
6594 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6595 PyErr_Format(PyExc_RuntimeError,
6596 "_compat_pickle.NAME_MAPPING values should be "
6597 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6598 return NULL;
6599 }
6600 module_name = PyTuple_GET_ITEM(item, 0);
6601 global_name = PyTuple_GET_ITEM(item, 1);
6602 if (!PyUnicode_Check(module_name) ||
6603 !PyUnicode_Check(global_name)) {
6604 PyErr_Format(PyExc_RuntimeError,
6605 "_compat_pickle.NAME_MAPPING values should be "
6606 "pairs of str, not (%.200s, %.200s)",
6607 Py_TYPE(module_name)->tp_name,
6608 Py_TYPE(global_name)->tp_name);
6609 return NULL;
6610 }
6611 }
6612 else if (PyErr_Occurred()) {
6613 return NULL;
6614 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006615 else {
6616 /* Check if the module was renamed. */
6617 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6618 if (item) {
6619 if (!PyUnicode_Check(item)) {
6620 PyErr_Format(PyExc_RuntimeError,
6621 "_compat_pickle.IMPORT_MAPPING values should be "
6622 "strings, not %.200s", Py_TYPE(item)->tp_name);
6623 return NULL;
6624 }
6625 module_name = item;
6626 }
6627 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006628 return NULL;
6629 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006630 }
6631 }
6632
Eric Snow3f9eee62017-09-15 16:35:20 -06006633 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006634 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006635 if (PyErr_Occurred())
6636 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006637 module = PyImport_Import(module_name);
6638 if (module == NULL)
6639 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006640 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006641 global = getattribute(module, global_name, self->proto >= 4);
6642 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006643 return global;
6644}
6645
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006646/*[clinic input]
6647
6648_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6649
6650Returns size in memory, in bytes.
6651[clinic start generated code]*/
6652
6653static Py_ssize_t
6654_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6655/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6656{
6657 Py_ssize_t res;
6658
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006659 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006660 if (self->memo != NULL)
6661 res += self->memo_size * sizeof(PyObject *);
6662 if (self->marks != NULL)
6663 res += self->marks_size * sizeof(Py_ssize_t);
6664 if (self->input_line != NULL)
6665 res += strlen(self->input_line) + 1;
6666 if (self->encoding != NULL)
6667 res += strlen(self->encoding) + 1;
6668 if (self->errors != NULL)
6669 res += strlen(self->errors) + 1;
6670 return res;
6671}
6672
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006673static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006674 _PICKLE_UNPICKLER_LOAD_METHODDEF
6675 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006676 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006677 {NULL, NULL} /* sentinel */
6678};
6679
6680static void
6681Unpickler_dealloc(UnpicklerObject *self)
6682{
6683 PyObject_GC_UnTrack((PyObject *)self);
6684 Py_XDECREF(self->readline);
6685 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006686 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006687 Py_XDECREF(self->stack);
6688 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006689 if (self->buffer.buf != NULL) {
6690 PyBuffer_Release(&self->buffer);
6691 self->buffer.buf = NULL;
6692 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006693
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006694 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006695 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006696 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006697 PyMem_Free(self->encoding);
6698 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006699
6700 Py_TYPE(self)->tp_free((PyObject *)self);
6701}
6702
6703static int
6704Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6705{
6706 Py_VISIT(self->readline);
6707 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006708 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006709 Py_VISIT(self->stack);
6710 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006711 return 0;
6712}
6713
6714static int
6715Unpickler_clear(UnpicklerObject *self)
6716{
6717 Py_CLEAR(self->readline);
6718 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006719 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006720 Py_CLEAR(self->stack);
6721 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006722 if (self->buffer.buf != NULL) {
6723 PyBuffer_Release(&self->buffer);
6724 self->buffer.buf = NULL;
6725 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006726
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006727 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006728 PyMem_Free(self->marks);
6729 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006730 PyMem_Free(self->input_line);
6731 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006732 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006733 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006734 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006735 self->errors = NULL;
6736
6737 return 0;
6738}
6739
Larry Hastings61272b72014-01-07 12:41:53 -08006740/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006741
6742_pickle.Unpickler.__init__
6743
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006744 file: object
6745 *
6746 fix_imports: bool = True
6747 encoding: str = 'ASCII'
6748 errors: str = 'strict'
6749
6750This takes a binary file for reading a pickle data stream.
6751
6752The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006753protocol argument is needed. Bytes past the pickled object's
6754representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006755
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006756The argument *file* must have two methods, a read() method that takes
6757an integer argument, and a readline() method that requires no
6758arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006759binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006760other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006761
6762Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006763which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006764generated by Python 2. If *fix_imports* is True, pickle will try to
6765map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006766*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006767instances pickled by Python 2; these default to 'ASCII' and 'strict',
6768respectively. The *encoding* can be 'bytes' to read these 8-bit
6769string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006770[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006771
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006772static int
Larry Hastings89964c42015-04-14 18:07:59 -04006773_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6774 int fix_imports, const char *encoding,
6775 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006776/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006777{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006778 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006779
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006780 /* In case of multiple __init__() calls, clear previous content. */
6781 if (self->read != NULL)
6782 (void)Unpickler_clear(self);
6783
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006784 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006785 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006786
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006787 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006788 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006789
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006790 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006791
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006792 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6793 &self->pers_func, &self->pers_func_self) < 0)
6794 {
6795 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006796 }
6797
6798 self->stack = (Pdata *)Pdata_New();
6799 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006800 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006801
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006802 self->memo_size = 32;
6803 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006804 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006805 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006806
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006807 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006808
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006809 return 0;
6810}
6811
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006812
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006813/* Define a proxy object for the Unpickler's internal memo object. This is to
6814 * avoid breaking code like:
6815 * unpickler.memo.clear()
6816 * and
6817 * unpickler.memo = saved_memo
6818 * Is this a good idea? Not really, but we don't want to break code that uses
6819 * it. Note that we don't implement the entire mapping API here. This is
6820 * intentional, as these should be treated as black-box implementation details.
6821 *
6822 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006823 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006824 */
6825
Larry Hastings61272b72014-01-07 12:41:53 -08006826/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006827_pickle.UnpicklerMemoProxy.clear
6828
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006829Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006830[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006831
Larry Hastings3cceb382014-01-04 11:09:09 -08006832static PyObject *
6833_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006834/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006835{
6836 _Unpickler_MemoCleanup(self->unpickler);
6837 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6838 if (self->unpickler->memo == NULL)
6839 return NULL;
6840 Py_RETURN_NONE;
6841}
6842
Larry Hastings61272b72014-01-07 12:41:53 -08006843/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006844_pickle.UnpicklerMemoProxy.copy
6845
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006846Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006847[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006848
Larry Hastings3cceb382014-01-04 11:09:09 -08006849static PyObject *
6850_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006851/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006852{
6853 Py_ssize_t i;
6854 PyObject *new_memo = PyDict_New();
6855 if (new_memo == NULL)
6856 return NULL;
6857
6858 for (i = 0; i < self->unpickler->memo_size; i++) {
6859 int status;
6860 PyObject *key, *value;
6861
6862 value = self->unpickler->memo[i];
6863 if (value == NULL)
6864 continue;
6865
6866 key = PyLong_FromSsize_t(i);
6867 if (key == NULL)
6868 goto error;
6869 status = PyDict_SetItem(new_memo, key, value);
6870 Py_DECREF(key);
6871 if (status < 0)
6872 goto error;
6873 }
6874 return new_memo;
6875
6876error:
6877 Py_DECREF(new_memo);
6878 return NULL;
6879}
6880
Larry Hastings61272b72014-01-07 12:41:53 -08006881/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006882_pickle.UnpicklerMemoProxy.__reduce__
6883
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006884Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006885[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006886
Larry Hastings3cceb382014-01-04 11:09:09 -08006887static PyObject *
6888_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006889/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006890{
6891 PyObject *reduce_value;
6892 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006893 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006894 if (contents == NULL)
6895 return NULL;
6896
6897 reduce_value = PyTuple_New(2);
6898 if (reduce_value == NULL) {
6899 Py_DECREF(contents);
6900 return NULL;
6901 }
6902 constructor_args = PyTuple_New(1);
6903 if (constructor_args == NULL) {
6904 Py_DECREF(contents);
6905 Py_DECREF(reduce_value);
6906 return NULL;
6907 }
6908 PyTuple_SET_ITEM(constructor_args, 0, contents);
6909 Py_INCREF((PyObject *)&PyDict_Type);
6910 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6911 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6912 return reduce_value;
6913}
6914
6915static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006916 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6917 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6918 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006919 {NULL, NULL} /* sentinel */
6920};
6921
6922static void
6923UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6924{
6925 PyObject_GC_UnTrack(self);
6926 Py_XDECREF(self->unpickler);
6927 PyObject_GC_Del((PyObject *)self);
6928}
6929
6930static int
6931UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6932 visitproc visit, void *arg)
6933{
6934 Py_VISIT(self->unpickler);
6935 return 0;
6936}
6937
6938static int
6939UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6940{
6941 Py_CLEAR(self->unpickler);
6942 return 0;
6943}
6944
6945static PyTypeObject UnpicklerMemoProxyType = {
6946 PyVarObject_HEAD_INIT(NULL, 0)
6947 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6948 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6949 0,
6950 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6951 0, /* tp_print */
6952 0, /* tp_getattr */
6953 0, /* tp_setattr */
6954 0, /* tp_compare */
6955 0, /* tp_repr */
6956 0, /* tp_as_number */
6957 0, /* tp_as_sequence */
6958 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006959 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006960 0, /* tp_call */
6961 0, /* tp_str */
6962 PyObject_GenericGetAttr, /* tp_getattro */
6963 PyObject_GenericSetAttr, /* tp_setattro */
6964 0, /* tp_as_buffer */
6965 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6966 0, /* tp_doc */
6967 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6968 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6969 0, /* tp_richcompare */
6970 0, /* tp_weaklistoffset */
6971 0, /* tp_iter */
6972 0, /* tp_iternext */
6973 unpicklerproxy_methods, /* tp_methods */
6974};
6975
6976static PyObject *
6977UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6978{
6979 UnpicklerMemoProxyObject *self;
6980
6981 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6982 &UnpicklerMemoProxyType);
6983 if (self == NULL)
6984 return NULL;
6985 Py_INCREF(unpickler);
6986 self->unpickler = unpickler;
6987 PyObject_GC_Track(self);
6988 return (PyObject *)self;
6989}
6990
6991/*****************************************************************************/
6992
6993
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006994static PyObject *
6995Unpickler_get_memo(UnpicklerObject *self)
6996{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006997 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006998}
6999
7000static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007001Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007002{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007003 PyObject **new_memo;
7004 Py_ssize_t new_memo_size = 0;
7005 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007006
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007007 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007008 PyErr_SetString(PyExc_TypeError,
7009 "attribute deletion is not supported");
7010 return -1;
7011 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007012
7013 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7014 UnpicklerObject *unpickler =
7015 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7016
7017 new_memo_size = unpickler->memo_size;
7018 new_memo = _Unpickler_NewMemo(new_memo_size);
7019 if (new_memo == NULL)
7020 return -1;
7021
7022 for (i = 0; i < new_memo_size; i++) {
7023 Py_XINCREF(unpickler->memo[i]);
7024 new_memo[i] = unpickler->memo[i];
7025 }
7026 }
7027 else if (PyDict_Check(obj)) {
7028 Py_ssize_t i = 0;
7029 PyObject *key, *value;
7030
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007031 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007032 new_memo = _Unpickler_NewMemo(new_memo_size);
7033 if (new_memo == NULL)
7034 return -1;
7035
7036 while (PyDict_Next(obj, &i, &key, &value)) {
7037 Py_ssize_t idx;
7038 if (!PyLong_Check(key)) {
7039 PyErr_SetString(PyExc_TypeError,
7040 "memo key must be integers");
7041 goto error;
7042 }
7043 idx = PyLong_AsSsize_t(key);
7044 if (idx == -1 && PyErr_Occurred())
7045 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007046 if (idx < 0) {
7047 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007048 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007049 goto error;
7050 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007051 if (_Unpickler_MemoPut(self, idx, value) < 0)
7052 goto error;
7053 }
7054 }
7055 else {
7056 PyErr_Format(PyExc_TypeError,
7057 "'memo' attribute must be an UnpicklerMemoProxy object"
7058 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007059 return -1;
7060 }
7061
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007062 _Unpickler_MemoCleanup(self);
7063 self->memo_size = new_memo_size;
7064 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007065
7066 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007067
7068 error:
7069 if (new_memo_size) {
7070 i = new_memo_size;
7071 while (--i >= 0) {
7072 Py_XDECREF(new_memo[i]);
7073 }
7074 PyMem_FREE(new_memo);
7075 }
7076 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007077}
7078
7079static PyObject *
7080Unpickler_get_persload(UnpicklerObject *self)
7081{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007082 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007083 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007084 return NULL;
7085 }
7086 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007087}
7088
7089static int
7090Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
7091{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007092 if (value == NULL) {
7093 PyErr_SetString(PyExc_TypeError,
7094 "attribute deletion is not supported");
7095 return -1;
7096 }
7097 if (!PyCallable_Check(value)) {
7098 PyErr_SetString(PyExc_TypeError,
7099 "persistent_load must be a callable taking "
7100 "one argument");
7101 return -1;
7102 }
7103
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007104 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007105 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007106 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007107
7108 return 0;
7109}
7110
7111static PyGetSetDef Unpickler_getsets[] = {
7112 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7113 {"persistent_load", (getter)Unpickler_get_persload,
7114 (setter)Unpickler_set_persload},
7115 {NULL}
7116};
7117
7118static PyTypeObject Unpickler_Type = {
7119 PyVarObject_HEAD_INIT(NULL, 0)
7120 "_pickle.Unpickler", /*tp_name*/
7121 sizeof(UnpicklerObject), /*tp_basicsize*/
7122 0, /*tp_itemsize*/
7123 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7124 0, /*tp_print*/
7125 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007126 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007127 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007128 0, /*tp_repr*/
7129 0, /*tp_as_number*/
7130 0, /*tp_as_sequence*/
7131 0, /*tp_as_mapping*/
7132 0, /*tp_hash*/
7133 0, /*tp_call*/
7134 0, /*tp_str*/
7135 0, /*tp_getattro*/
7136 0, /*tp_setattro*/
7137 0, /*tp_as_buffer*/
7138 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007139 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007140 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7141 (inquiry)Unpickler_clear, /*tp_clear*/
7142 0, /*tp_richcompare*/
7143 0, /*tp_weaklistoffset*/
7144 0, /*tp_iter*/
7145 0, /*tp_iternext*/
7146 Unpickler_methods, /*tp_methods*/
7147 0, /*tp_members*/
7148 Unpickler_getsets, /*tp_getset*/
7149 0, /*tp_base*/
7150 0, /*tp_dict*/
7151 0, /*tp_descr_get*/
7152 0, /*tp_descr_set*/
7153 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007154 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007155 PyType_GenericAlloc, /*tp_alloc*/
7156 PyType_GenericNew, /*tp_new*/
7157 PyObject_GC_Del, /*tp_free*/
7158 0, /*tp_is_gc*/
7159};
7160
Larry Hastings61272b72014-01-07 12:41:53 -08007161/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007162
7163_pickle.dump
7164
7165 obj: object
7166 file: object
7167 protocol: object = NULL
7168 *
7169 fix_imports: bool = True
7170
7171Write a pickled representation of obj to the open file object file.
7172
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007173This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7174be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007175
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007176The optional *protocol* argument tells the pickler to use the given
Łukasz Langac51d8c92018-04-03 23:06:53 -07007177protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7178protocol is 4. It was introduced in Python 3.4, it is incompatible
7179with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007180
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007181Specifying a negative protocol version selects the highest protocol
7182version supported. The higher the protocol used, the more recent the
7183version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007184
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007185The *file* argument must have a write() method that accepts a single
7186bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007187writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007188this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007189
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007190If *fix_imports* is True and protocol is less than 3, pickle will try
7191to map the new Python 3 names to the old module names used in Python
71922, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007193[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007194
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007195static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007196_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007197 PyObject *protocol, int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007198/*[clinic end generated code: output=a4774d5fde7d34de input=93f1408489a87472]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007199{
7200 PicklerObject *pickler = _Pickler_New();
7201
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007202 if (pickler == NULL)
7203 return NULL;
7204
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007205 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007206 goto error;
7207
7208 if (_Pickler_SetOutputStream(pickler, file) < 0)
7209 goto error;
7210
7211 if (dump(pickler, obj) < 0)
7212 goto error;
7213
7214 if (_Pickler_FlushToFile(pickler) < 0)
7215 goto error;
7216
7217 Py_DECREF(pickler);
7218 Py_RETURN_NONE;
7219
7220 error:
7221 Py_XDECREF(pickler);
7222 return NULL;
7223}
7224
Larry Hastings61272b72014-01-07 12:41:53 -08007225/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007226
7227_pickle.dumps
7228
7229 obj: object
7230 protocol: object = NULL
7231 *
7232 fix_imports: bool = True
7233
7234Return the pickled representation of the object as a bytes object.
7235
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007236The optional *protocol* argument tells the pickler to use the given
7237protocol; supported protocols are 0, 1, 2, 3 and 4. The default
Łukasz Langac51d8c92018-04-03 23:06:53 -07007238protocol is 4. It was introduced in Python 3.4, it is incompatible
7239with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007240
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007241Specifying a negative protocol version selects the highest protocol
7242version supported. The higher the protocol used, the more recent the
7243version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007244
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007245If *fix_imports* is True and *protocol* is less than 3, pickle will
7246try to map the new Python 3 names to the old module names used in
7247Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007248[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007249
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007250static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007251_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007252 int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007253/*[clinic end generated code: output=d75d5cda456fd261 input=b6efb45a7d19b5ab]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007254{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007255 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007256 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007257
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007258 if (pickler == NULL)
7259 return NULL;
7260
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007261 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007262 goto error;
7263
7264 if (dump(pickler, obj) < 0)
7265 goto error;
7266
7267 result = _Pickler_GetString(pickler);
7268 Py_DECREF(pickler);
7269 return result;
7270
7271 error:
7272 Py_XDECREF(pickler);
7273 return NULL;
7274}
7275
Larry Hastings61272b72014-01-07 12:41:53 -08007276/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007277
7278_pickle.load
7279
7280 file: object
7281 *
7282 fix_imports: bool = True
7283 encoding: str = 'ASCII'
7284 errors: str = 'strict'
7285
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007286Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007287
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007288This is equivalent to ``Unpickler(file).load()``, but may be more
7289efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007290
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007291The protocol version of the pickle is detected automatically, so no
7292protocol argument is needed. Bytes past the pickled object's
7293representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007294
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007295The argument *file* must have two methods, a read() method that takes
7296an integer argument, and a readline() method that requires no
7297arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007298binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007299other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007300
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007301Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007302which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007303generated by Python 2. If *fix_imports* is True, pickle will try to
7304map the old Python 2 names to the new names used in Python 3. The
7305*encoding* and *errors* tell pickle how to decode 8-bit string
7306instances pickled by Python 2; these default to 'ASCII' and 'strict',
7307respectively. The *encoding* can be 'bytes' to read these 8-bit
7308string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007309[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007310
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007311static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007312_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007313 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007314/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007315{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007316 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007317 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007318
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007319 if (unpickler == NULL)
7320 return NULL;
7321
7322 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7323 goto error;
7324
7325 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7326 goto error;
7327
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007328 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007329
7330 result = load(unpickler);
7331 Py_DECREF(unpickler);
7332 return result;
7333
7334 error:
7335 Py_XDECREF(unpickler);
7336 return NULL;
7337}
7338
Larry Hastings61272b72014-01-07 12:41:53 -08007339/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007340
7341_pickle.loads
7342
7343 data: object
7344 *
7345 fix_imports: bool = True
7346 encoding: str = 'ASCII'
7347 errors: str = 'strict'
7348
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007349Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007350
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007351The protocol version of the pickle is detected automatically, so no
7352protocol argument is needed. Bytes past the pickled object's
7353representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007354
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007355Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007356which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007357generated by Python 2. If *fix_imports* is True, pickle will try to
7358map the old Python 2 names to the new names used in Python 3. The
7359*encoding* and *errors* tell pickle how to decode 8-bit string
7360instances pickled by Python 2; these default to 'ASCII' and 'strict',
7361respectively. The *encoding* can be 'bytes' to read these 8-bit
7362string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007363[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007364
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007365static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007366_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007367 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007368/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007369{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007370 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007371 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007372
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007373 if (unpickler == NULL)
7374 return NULL;
7375
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007376 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007377 goto error;
7378
7379 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7380 goto error;
7381
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007382 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007383
7384 result = load(unpickler);
7385 Py_DECREF(unpickler);
7386 return result;
7387
7388 error:
7389 Py_XDECREF(unpickler);
7390 return NULL;
7391}
7392
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007393static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007394 _PICKLE_DUMP_METHODDEF
7395 _PICKLE_DUMPS_METHODDEF
7396 _PICKLE_LOAD_METHODDEF
7397 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007398 {NULL, NULL} /* sentinel */
7399};
7400
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007401static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007402pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007403{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007404 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007405 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007406}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007407
Stefan Krahf483b0f2013-12-14 13:43:10 +01007408static void
7409pickle_free(PyObject *m)
7410{
7411 _Pickle_ClearState(_Pickle_GetState(m));
7412}
7413
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007414static int
7415pickle_traverse(PyObject *m, visitproc visit, void *arg)
7416{
7417 PickleState *st = _Pickle_GetState(m);
7418 Py_VISIT(st->PickleError);
7419 Py_VISIT(st->PicklingError);
7420 Py_VISIT(st->UnpicklingError);
7421 Py_VISIT(st->dispatch_table);
7422 Py_VISIT(st->extension_registry);
7423 Py_VISIT(st->extension_cache);
7424 Py_VISIT(st->inverted_registry);
7425 Py_VISIT(st->name_mapping_2to3);
7426 Py_VISIT(st->import_mapping_2to3);
7427 Py_VISIT(st->name_mapping_3to2);
7428 Py_VISIT(st->import_mapping_3to2);
7429 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007430 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007431 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007432}
7433
7434static struct PyModuleDef _picklemodule = {
7435 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007436 "_pickle", /* m_name */
7437 pickle_module_doc, /* m_doc */
7438 sizeof(PickleState), /* m_size */
7439 pickle_methods, /* m_methods */
7440 NULL, /* m_reload */
7441 pickle_traverse, /* m_traverse */
7442 pickle_clear, /* m_clear */
7443 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007444};
7445
7446PyMODINIT_FUNC
7447PyInit__pickle(void)
7448{
7449 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007450 PickleState *st;
7451
7452 m = PyState_FindModule(&_picklemodule);
7453 if (m) {
7454 Py_INCREF(m);
7455 return m;
7456 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007457
7458 if (PyType_Ready(&Unpickler_Type) < 0)
7459 return NULL;
7460 if (PyType_Ready(&Pickler_Type) < 0)
7461 return NULL;
7462 if (PyType_Ready(&Pdata_Type) < 0)
7463 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007464 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7465 return NULL;
7466 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7467 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007468
7469 /* Create the module and add the functions. */
7470 m = PyModule_Create(&_picklemodule);
7471 if (m == NULL)
7472 return NULL;
7473
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007474 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007475 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7476 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007477 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007478 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7479 return NULL;
7480
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007481 st = _Pickle_GetState(m);
7482
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007483 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007484 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7485 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007486 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007487 st->PicklingError = \
7488 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7489 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007490 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007491 st->UnpicklingError = \
7492 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7493 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007494 return NULL;
7495
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007496 Py_INCREF(st->PickleError);
7497 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007498 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007499 Py_INCREF(st->PicklingError);
7500 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007501 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007502 Py_INCREF(st->UnpicklingError);
7503 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007504 return NULL;
7505
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007506 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007507 return NULL;
7508
7509 return m;
7510}