blob: 55affb2c7c47968fd6624a3eb17ee937ca52f776 [file] [log] [blame]
Victor Stinner5c75f372019-04-17 23:02:26 +02001/* pickle accelerator C extensor: _pickle module.
2 *
3 * It is built as a built-in module (Py_BUILD_CORE_BUILTIN define) on Windows
4 * and as an extension module (Py_BUILD_CORE_MODULE define) on other
5 * platforms. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06006
Victor Stinner5c75f372019-04-17 23:02:26 +02007#if !defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE_MODULE)
8# error "Py_BUILD_CORE_BUILTIN or Py_BUILD_CORE_MODULE must be defined"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06009#endif
10
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000011#include "Python.h"
12#include "structmember.h"
13
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -080014PyDoc_STRVAR(pickle_module_doc,
15"Optimized C implementation for the Python pickle module.");
16
Larry Hastings61272b72014-01-07 12:41:53 -080017/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080018module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080019class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
20class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
21class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
22class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080023[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030024/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080025
Łukasz Langac51d8c92018-04-03 23:06:53 -070026/* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol.
27 Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python
28 already includes it. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029enum {
Antoine Pitrou91f43802019-05-26 17:10:09 +020030 HIGHEST_PROTOCOL = 5,
Łukasz Langac51d8c92018-04-03 23:06:53 -070031 DEFAULT_PROTOCOL = 4
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000032};
33
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000034/* Pickle opcodes. These must be kept updated with pickle.py.
35 Extensive docs are in pickletools.py. */
36enum opcode {
37 MARK = '(',
38 STOP = '.',
39 POP = '0',
40 POP_MARK = '1',
41 DUP = '2',
42 FLOAT = 'F',
43 INT = 'I',
44 BININT = 'J',
45 BININT1 = 'K',
46 LONG = 'L',
47 BININT2 = 'M',
48 NONE = 'N',
49 PERSID = 'P',
50 BINPERSID = 'Q',
51 REDUCE = 'R',
52 STRING = 'S',
53 BINSTRING = 'T',
54 SHORT_BINSTRING = 'U',
55 UNICODE = 'V',
56 BINUNICODE = 'X',
57 APPEND = 'a',
58 BUILD = 'b',
59 GLOBAL = 'c',
60 DICT = 'd',
61 EMPTY_DICT = '}',
62 APPENDS = 'e',
63 GET = 'g',
64 BINGET = 'h',
65 INST = 'i',
66 LONG_BINGET = 'j',
67 LIST = 'l',
68 EMPTY_LIST = ']',
69 OBJ = 'o',
70 PUT = 'p',
71 BINPUT = 'q',
72 LONG_BINPUT = 'r',
73 SETITEM = 's',
74 TUPLE = 't',
75 EMPTY_TUPLE = ')',
76 SETITEMS = 'u',
77 BINFLOAT = 'G',
78
79 /* Protocol 2. */
80 PROTO = '\x80',
81 NEWOBJ = '\x81',
82 EXT1 = '\x82',
83 EXT2 = '\x83',
84 EXT4 = '\x84',
85 TUPLE1 = '\x85',
86 TUPLE2 = '\x86',
87 TUPLE3 = '\x87',
88 NEWTRUE = '\x88',
89 NEWFALSE = '\x89',
90 LONG1 = '\x8a',
91 LONG4 = '\x8b',
92
93 /* Protocol 3 (Python 3.x) */
94 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010095 SHORT_BINBYTES = 'C',
96
97 /* Protocol 4 */
98 SHORT_BINUNICODE = '\x8c',
99 BINUNICODE8 = '\x8d',
100 BINBYTES8 = '\x8e',
101 EMPTY_SET = '\x8f',
102 ADDITEMS = '\x90',
103 FROZENSET = '\x91',
104 NEWOBJ_EX = '\x92',
105 STACK_GLOBAL = '\x93',
106 MEMOIZE = '\x94',
Antoine Pitrou91f43802019-05-26 17:10:09 +0200107 FRAME = '\x95',
108
109 /* Protocol 5 */
110 BYTEARRAY8 = '\x96',
111 NEXT_BUFFER = '\x97',
112 READONLY_BUFFER = '\x98'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000113};
114
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000115enum {
116 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
117 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
118 break if this gets out of synch with pickle.py, but it's unclear that would
119 help anything either. */
120 BATCHSIZE = 1000,
121
122 /* Nesting limit until Pickler, when running in "fast mode", starts
123 checking for self-referential data-structures. */
124 FAST_NESTING_LIMIT = 50,
125
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000126 /* Initial size of the write buffer of Pickler. */
127 WRITE_BUF_SIZE = 4096,
128
Antoine Pitrou04248a82010-10-12 20:51:21 +0000129 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100130 PREFETCH = 8192 * 16,
131
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200132 FRAME_SIZE_MIN = 4,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100133 FRAME_SIZE_TARGET = 64 * 1024,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100134 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000135};
136
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800137/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800139/* State of the pickle module, per PEP 3121. */
140typedef struct {
141 /* Exception classes for pickle. */
142 PyObject *PickleError;
143 PyObject *PicklingError;
144 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800145
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800146 /* copyreg.dispatch_table, {type_object: pickling_function} */
147 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000148
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800149 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000150
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800151 /* copyreg._extension_registry, {(module_name, function_name): code} */
152 PyObject *extension_registry;
153 /* copyreg._extension_cache, {code: object} */
154 PyObject *extension_cache;
155 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
156 PyObject *inverted_registry;
157
158 /* Import mappings for compatibility with Python 2.x */
159
160 /* _compat_pickle.NAME_MAPPING,
161 {(oldmodule, oldname): (newmodule, newname)} */
162 PyObject *name_mapping_2to3;
163 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
164 PyObject *import_mapping_2to3;
165 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
166 PyObject *name_mapping_3to2;
167 PyObject *import_mapping_3to2;
168
169 /* codecs.encode, used for saving bytes in older protocols */
170 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300171 /* builtins.getattr, used for saving nested names with protocol < 4 */
172 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300173 /* functools.partial, used for implementing __newobj_ex__ with protocols
174 2 and 3 */
175 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800176} PickleState;
177
178/* Forward declaration of the _pickle module definition. */
179static struct PyModuleDef _picklemodule;
180
181/* Given a module object, get its per-module state. */
182static PickleState *
183_Pickle_GetState(PyObject *module)
184{
185 return (PickleState *)PyModule_GetState(module);
186}
187
188/* Find the module instance imported in the currently running sub-interpreter
189 and get its state. */
190static PickleState *
191_Pickle_GetGlobalState(void)
192{
193 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
194}
195
196/* Clear the given pickle module state. */
197static void
198_Pickle_ClearState(PickleState *st)
199{
200 Py_CLEAR(st->PickleError);
201 Py_CLEAR(st->PicklingError);
202 Py_CLEAR(st->UnpicklingError);
203 Py_CLEAR(st->dispatch_table);
204 Py_CLEAR(st->extension_registry);
205 Py_CLEAR(st->extension_cache);
206 Py_CLEAR(st->inverted_registry);
207 Py_CLEAR(st->name_mapping_2to3);
208 Py_CLEAR(st->import_mapping_2to3);
209 Py_CLEAR(st->name_mapping_3to2);
210 Py_CLEAR(st->import_mapping_3to2);
211 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300212 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100213 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800214}
215
216/* Initialize the given pickle module state. */
217static int
218_Pickle_InitState(PickleState *st)
219{
220 PyObject *copyreg = NULL;
221 PyObject *compat_pickle = NULL;
222 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300223 PyObject *functools = NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200224 _Py_IDENTIFIER(getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800225
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200226 st->getattr = _PyEval_GetBuiltinId(&PyId_getattr);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300227 if (st->getattr == NULL)
228 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300229
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800230 copyreg = PyImport_ImportModule("copyreg");
231 if (!copyreg)
232 goto error;
233 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
234 if (!st->dispatch_table)
235 goto error;
236 if (!PyDict_CheckExact(st->dispatch_table)) {
237 PyErr_Format(PyExc_RuntimeError,
238 "copyreg.dispatch_table should be a dict, not %.200s",
239 Py_TYPE(st->dispatch_table)->tp_name);
240 goto error;
241 }
242 st->extension_registry = \
243 PyObject_GetAttrString(copyreg, "_extension_registry");
244 if (!st->extension_registry)
245 goto error;
246 if (!PyDict_CheckExact(st->extension_registry)) {
247 PyErr_Format(PyExc_RuntimeError,
248 "copyreg._extension_registry should be a dict, "
249 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
250 goto error;
251 }
252 st->inverted_registry = \
253 PyObject_GetAttrString(copyreg, "_inverted_registry");
254 if (!st->inverted_registry)
255 goto error;
256 if (!PyDict_CheckExact(st->inverted_registry)) {
257 PyErr_Format(PyExc_RuntimeError,
258 "copyreg._inverted_registry should be a dict, "
259 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
260 goto error;
261 }
262 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
263 if (!st->extension_cache)
264 goto error;
265 if (!PyDict_CheckExact(st->extension_cache)) {
266 PyErr_Format(PyExc_RuntimeError,
267 "copyreg._extension_cache should be a dict, "
268 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
269 goto error;
270 }
271 Py_CLEAR(copyreg);
272
273 /* Load the 2.x -> 3.x stdlib module mapping tables */
274 compat_pickle = PyImport_ImportModule("_compat_pickle");
275 if (!compat_pickle)
276 goto error;
277 st->name_mapping_2to3 = \
278 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
279 if (!st->name_mapping_2to3)
280 goto error;
281 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
282 PyErr_Format(PyExc_RuntimeError,
283 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
284 Py_TYPE(st->name_mapping_2to3)->tp_name);
285 goto error;
286 }
287 st->import_mapping_2to3 = \
288 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
289 if (!st->import_mapping_2to3)
290 goto error;
291 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
292 PyErr_Format(PyExc_RuntimeError,
293 "_compat_pickle.IMPORT_MAPPING should be a dict, "
294 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
295 goto error;
296 }
297 /* ... and the 3.x -> 2.x mapping tables */
298 st->name_mapping_3to2 = \
299 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
300 if (!st->name_mapping_3to2)
301 goto error;
302 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
303 PyErr_Format(PyExc_RuntimeError,
304 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
305 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
306 goto error;
307 }
308 st->import_mapping_3to2 = \
309 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
310 if (!st->import_mapping_3to2)
311 goto error;
312 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
313 PyErr_Format(PyExc_RuntimeError,
314 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
315 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
316 goto error;
317 }
318 Py_CLEAR(compat_pickle);
319
320 codecs = PyImport_ImportModule("codecs");
321 if (codecs == NULL)
322 goto error;
323 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
324 if (st->codecs_encode == NULL) {
325 goto error;
326 }
327 if (!PyCallable_Check(st->codecs_encode)) {
328 PyErr_Format(PyExc_RuntimeError,
329 "codecs.encode should be a callable, not %.200s",
330 Py_TYPE(st->codecs_encode)->tp_name);
331 goto error;
332 }
333 Py_CLEAR(codecs);
334
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300335 functools = PyImport_ImportModule("functools");
336 if (!functools)
337 goto error;
338 st->partial = PyObject_GetAttrString(functools, "partial");
339 if (!st->partial)
340 goto error;
341 Py_CLEAR(functools);
342
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800343 return 0;
344
345 error:
346 Py_CLEAR(copyreg);
347 Py_CLEAR(compat_pickle);
348 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300349 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 _Pickle_ClearState(st);
351 return -1;
352}
353
354/* Helper for calling a function with a single argument quickly.
355
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800356 This function steals the reference of the given argument. */
357static PyObject *
358_Pickle_FastCall(PyObject *func, PyObject *obj)
359{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800360 PyObject *result;
361
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100362 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200363 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800364 return result;
365}
366
367/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000368
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200369/* Retrieve and deconstruct a method for avoiding a reference cycle
370 (pickler -> bound method of pickler -> pickler) */
371static int
372init_method_ref(PyObject *self, _Py_Identifier *name,
373 PyObject **method_func, PyObject **method_self)
374{
375 PyObject *func, *func2;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200376 int ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200377
378 /* *method_func and *method_self should be consistent. All refcount decrements
379 should be occurred after setting *method_self and *method_func. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200380 ret = _PyObject_LookupAttrId(self, name, &func);
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200381 if (func == NULL) {
382 *method_self = NULL;
383 Py_CLEAR(*method_func);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200384 return ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200385 }
386
387 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
388 /* Deconstruct a bound Python method */
389 func2 = PyMethod_GET_FUNCTION(func);
390 Py_INCREF(func2);
391 *method_self = self; /* borrowed */
392 Py_XSETREF(*method_func, func2);
393 Py_DECREF(func);
394 return 0;
395 }
396 else {
397 *method_self = NULL;
398 Py_XSETREF(*method_func, func);
399 return 0;
400 }
401}
402
403/* Bind a method if it was deconstructed */
404static PyObject *
405reconstruct_method(PyObject *func, PyObject *self)
406{
407 if (self) {
408 return PyMethod_New(func, self);
409 }
410 else {
411 Py_INCREF(func);
412 return func;
413 }
414}
415
416static PyObject *
417call_method(PyObject *func, PyObject *self, PyObject *obj)
418{
419 if (self) {
420 return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
421 }
422 else {
423 return PyObject_CallFunctionObjArgs(func, obj, NULL);
424 }
425}
426
427/*************************************************************************/
428
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000429/* Internal data type used as the unpickling stack. */
430typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000431 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000432 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200433 int mark_set; /* is MARK set? */
434 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000435 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436} Pdata;
437
438static void
439Pdata_dealloc(Pdata *self)
440{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200441 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000442 while (--i >= 0) {
443 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000444 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000445 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000446 PyObject_Del(self);
447}
448
449static PyTypeObject Pdata_Type = {
450 PyVarObject_HEAD_INIT(NULL, 0)
451 "_pickle.Pdata", /*tp_name*/
452 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200453 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000454 (destructor)Pdata_dealloc, /*tp_dealloc*/
455};
456
457static PyObject *
458Pdata_New(void)
459{
460 Pdata *self;
461
462 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
463 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000464 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200465 self->mark_set = 0;
466 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000467 self->allocated = 8;
468 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000469 if (self->data)
470 return (PyObject *)self;
471 Py_DECREF(self);
472 return PyErr_NoMemory();
473}
474
475
476/* Retain only the initial clearto items. If clearto >= the current
477 * number of items, this is a (non-erroneous) NOP.
478 */
479static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200480Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000481{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200482 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000483
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200484 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000485 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000486 return 0;
487
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000488 while (--i >= clearto) {
489 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000490 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000491 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000492 return 0;
493}
494
495static int
496Pdata_grow(Pdata *self)
497{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000498 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200499 size_t allocated = (size_t)self->allocated;
500 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000501
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000502 new_allocated = (allocated >> 3) + 6;
503 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200504 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000505 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000506 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500507 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000508 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000509 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000510
511 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200512 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000513 return 0;
514
515 nomemory:
516 PyErr_NoMemory();
517 return -1;
518}
519
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200520static int
521Pdata_stack_underflow(Pdata *self)
522{
523 PickleState *st = _Pickle_GetGlobalState();
524 PyErr_SetString(st->UnpicklingError,
525 self->mark_set ?
526 "unexpected MARK found" :
527 "unpickling stack underflow");
528 return -1;
529}
530
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000531/* D is a Pdata*. Pop the topmost element and store it into V, which
532 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
533 * is raised and V is set to NULL.
534 */
535static PyObject *
536Pdata_pop(Pdata *self)
537{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200538 if (Py_SIZE(self) <= self->fence) {
539 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000540 return NULL;
541 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000542 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000543}
544#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
545
546static int
547Pdata_push(Pdata *self, PyObject *obj)
548{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000549 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000550 return -1;
551 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000552 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000553 return 0;
554}
555
556/* Push an object on stack, transferring its ownership to the stack. */
557#define PDATA_PUSH(D, O, ER) do { \
558 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
559
560/* Push an object on stack, adding a new reference to the object. */
561#define PDATA_APPEND(D, O, ER) do { \
562 Py_INCREF((O)); \
563 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
564
565static PyObject *
566Pdata_poptuple(Pdata *self, Py_ssize_t start)
567{
568 PyObject *tuple;
569 Py_ssize_t len, i, j;
570
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200571 if (start < self->fence) {
572 Pdata_stack_underflow(self);
573 return NULL;
574 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000575 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000576 tuple = PyTuple_New(len);
577 if (tuple == NULL)
578 return NULL;
579 for (i = start, j = 0; j < len; i++, j++)
580 PyTuple_SET_ITEM(tuple, j, self->data[i]);
581
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000582 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000583 return tuple;
584}
585
586static PyObject *
587Pdata_poplist(Pdata *self, Py_ssize_t start)
588{
589 PyObject *list;
590 Py_ssize_t len, i, j;
591
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000592 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000593 list = PyList_New(len);
594 if (list == NULL)
595 return NULL;
596 for (i = start, j = 0; j < len; i++, j++)
597 PyList_SET_ITEM(list, j, self->data[i]);
598
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000599 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000600 return list;
601}
602
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000603typedef struct {
604 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200605 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000606} PyMemoEntry;
607
608typedef struct {
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700609 size_t mt_mask;
610 size_t mt_used;
611 size_t mt_allocated;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000612 PyMemoEntry *mt_table;
613} PyMemoTable;
614
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000615typedef struct PicklerObject {
616 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000617 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000618 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000619 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000620 PyObject *pers_func; /* persistent_id() method, can be NULL */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200621 PyObject *pers_func_self; /* borrowed reference to self if pers_func
622 is an unbound method, NULL otherwise */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100623 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Pierre Glaser289f1f82019-05-08 23:08:25 +0200624 PyObject *reducer_override; /* hook for invoking user-defined callbacks
625 instead of save_global when pickling
626 functions and classes*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000627
628 PyObject *write; /* write() method of the output stream. */
629 PyObject *output_buffer; /* Write into a local bytearray buffer before
630 flushing to the stream. */
631 Py_ssize_t output_len; /* Length of output_buffer. */
632 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000633 int proto; /* Pickle protocol number, >= 0 */
634 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100635 int framing; /* True when framing is enabled, proto >= 4 */
636 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000637 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100638 is no frame currently open. */
639
640 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000641 int fast; /* Enable fast mode if set to a true value.
642 The fast mode disable the usage of memo,
643 therefore speeding the pickling process by
644 not generating superfluous PUT opcodes. It
645 should not be used if with self-referential
646 objects. */
647 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000648 int fix_imports; /* Indicate whether Pickler should fix
649 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000650 PyObject *fast_memo;
Antoine Pitrou91f43802019-05-26 17:10:09 +0200651 PyObject *buffer_callback; /* Callback for out-of-band buffers, or NULL */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000652} PicklerObject;
653
654typedef struct UnpicklerObject {
655 PyObject_HEAD
656 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000657
658 /* The unpickler memo is just an array of PyObject *s. Using a dict
659 is unnecessary, since the keys are contiguous ints. */
660 PyObject **memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700661 size_t memo_size; /* Capacity of the memo array */
662 size_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000663
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000664 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200665 PyObject *pers_func_self; /* borrowed reference to self if pers_func
666 is an unbound method, NULL otherwise */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000667
668 Py_buffer buffer;
669 char *input_buffer;
670 char *input_line;
671 Py_ssize_t input_len;
672 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000673 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100674
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000675 PyObject *read; /* read() method of the input stream. */
Antoine Pitrou91f43802019-05-26 17:10:09 +0200676 PyObject *readinto; /* readinto() method of the input stream. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000677 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000678 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrou91f43802019-05-26 17:10:09 +0200679 PyObject *buffers; /* iterable of out-of-band buffers, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000680
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000681 char *encoding; /* Name of the encoding to be used for
682 decoding strings pickled using Python
683 2.x. The default value is "ASCII" */
684 char *errors; /* Name of errors handling scheme to used when
685 decoding strings. The default value is
686 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500687 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000688 objects. */
689 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
690 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000691 int proto; /* Protocol of the pickle loaded. */
692 int fix_imports; /* Indicate whether Unpickler should fix
693 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000694} UnpicklerObject;
695
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200696typedef struct {
697 PyObject_HEAD
698 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
699} PicklerMemoProxyObject;
700
701typedef struct {
702 PyObject_HEAD
703 UnpicklerObject *unpickler;
704} UnpicklerMemoProxyObject;
705
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000706/* Forward declarations */
707static int save(PicklerObject *, PyObject *, int);
708static int save_reduce(PicklerObject *, PyObject *, PyObject *);
709static PyTypeObject Pickler_Type;
710static PyTypeObject Unpickler_Type;
711
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200712#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000713
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000714/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300715 A custom hashtable mapping void* to Python ints. This is used by the pickler
716 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000717 a bunch of unnecessary object creation. This makes a huge performance
718 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000719
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000720#define MT_MINSIZE 8
721#define PERTURB_SHIFT 5
722
723
724static PyMemoTable *
725PyMemoTable_New(void)
726{
727 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
728 if (memo == NULL) {
729 PyErr_NoMemory();
730 return NULL;
731 }
732
733 memo->mt_used = 0;
734 memo->mt_allocated = MT_MINSIZE;
735 memo->mt_mask = MT_MINSIZE - 1;
736 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
737 if (memo->mt_table == NULL) {
738 PyMem_FREE(memo);
739 PyErr_NoMemory();
740 return NULL;
741 }
742 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
743
744 return memo;
745}
746
747static PyMemoTable *
748PyMemoTable_Copy(PyMemoTable *self)
749{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000750 PyMemoTable *new = PyMemoTable_New();
751 if (new == NULL)
752 return NULL;
753
754 new->mt_used = self->mt_used;
755 new->mt_allocated = self->mt_allocated;
756 new->mt_mask = self->mt_mask;
757 /* The table we get from _New() is probably smaller than we wanted.
758 Free it and allocate one that's the right size. */
759 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500760 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000761 if (new->mt_table == NULL) {
762 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200763 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000764 return NULL;
765 }
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700766 for (size_t i = 0; i < self->mt_allocated; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000767 Py_XINCREF(self->mt_table[i].me_key);
768 }
769 memcpy(new->mt_table, self->mt_table,
770 sizeof(PyMemoEntry) * self->mt_allocated);
771
772 return new;
773}
774
775static Py_ssize_t
776PyMemoTable_Size(PyMemoTable *self)
777{
778 return self->mt_used;
779}
780
781static int
782PyMemoTable_Clear(PyMemoTable *self)
783{
784 Py_ssize_t i = self->mt_allocated;
785
786 while (--i >= 0) {
787 Py_XDECREF(self->mt_table[i].me_key);
788 }
789 self->mt_used = 0;
790 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
791 return 0;
792}
793
794static void
795PyMemoTable_Del(PyMemoTable *self)
796{
797 if (self == NULL)
798 return;
799 PyMemoTable_Clear(self);
800
801 PyMem_FREE(self->mt_table);
802 PyMem_FREE(self);
803}
804
805/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
806 can be considerably simpler than dictobject.c's lookdict(). */
807static PyMemoEntry *
808_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
809{
810 size_t i;
811 size_t perturb;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700812 size_t mask = self->mt_mask;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000813 PyMemoEntry *table = self->mt_table;
814 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000815 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000816
817 i = hash & mask;
818 entry = &table[i];
819 if (entry->me_key == NULL || entry->me_key == key)
820 return entry;
821
822 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
823 i = (i << 2) + i + perturb + 1;
824 entry = &table[i & mask];
825 if (entry->me_key == NULL || entry->me_key == key)
826 return entry;
827 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700828 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000829}
830
831/* Returns -1 on failure, 0 on success. */
832static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700833_PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000834{
835 PyMemoEntry *oldtable = NULL;
836 PyMemoEntry *oldentry, *newentry;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700837 size_t new_size = MT_MINSIZE;
838 size_t to_process;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000839
840 assert(min_size > 0);
841
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700842 if (min_size > PY_SSIZE_T_MAX) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000843 PyErr_NoMemory();
844 return -1;
845 }
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700846
847 /* Find the smallest valid table size >= min_size. */
848 while (new_size < min_size) {
849 new_size <<= 1;
850 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000851 /* new_size needs to be a power of two. */
852 assert((new_size & (new_size - 1)) == 0);
853
854 /* Allocate new table. */
855 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500856 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000857 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200858 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000859 PyErr_NoMemory();
860 return -1;
861 }
862 self->mt_allocated = new_size;
863 self->mt_mask = new_size - 1;
864 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
865
866 /* Copy entries from the old table. */
867 to_process = self->mt_used;
868 for (oldentry = oldtable; to_process > 0; oldentry++) {
869 if (oldentry->me_key != NULL) {
870 to_process--;
871 /* newentry is a pointer to a chunk of the new
872 mt_table, so we're setting the key:value pair
873 in-place. */
874 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
875 newentry->me_key = oldentry->me_key;
876 newentry->me_value = oldentry->me_value;
877 }
878 }
879
880 /* Deallocate the old table. */
881 PyMem_FREE(oldtable);
882 return 0;
883}
884
885/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200886static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000887PyMemoTable_Get(PyMemoTable *self, PyObject *key)
888{
889 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
890 if (entry->me_key == NULL)
891 return NULL;
892 return &entry->me_value;
893}
894
895/* Returns -1 on failure, 0 on success. */
896static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200897PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000898{
899 PyMemoEntry *entry;
900
901 assert(key != NULL);
902
903 entry = _PyMemoTable_Lookup(self, key);
904 if (entry->me_key != NULL) {
905 entry->me_value = value;
906 return 0;
907 }
908 Py_INCREF(key);
909 entry->me_key = key;
910 entry->me_value = value;
911 self->mt_used++;
912
913 /* If we added a key, we can safely resize. Otherwise just return!
914 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
915 *
916 * Quadrupling the size improves average table sparseness
917 * (reducing collisions) at the cost of some memory. It also halves
918 * the number of expensive resize operations in a growing memo table.
919 *
920 * Very large memo tables (over 50K items) use doubling instead.
921 * This may help applications with severe memory constraints.
922 */
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700923 if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000924 return 0;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700925 }
926 // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow.
927 size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used;
928 return _PyMemoTable_ResizeTable(self, desired_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000929}
930
931#undef MT_MINSIZE
932#undef PERTURB_SHIFT
933
934/*************************************************************************/
935
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000936
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000937static int
938_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000939{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300940 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200941 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000942 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000943 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000944 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100945 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000946 return 0;
947}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000948
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100949static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100950_write_size64(char *out, size_t value)
951{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200952 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800953
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200954 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800955
956 for (i = 0; i < sizeof(size_t); i++) {
957 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
958 }
959 for (i = sizeof(size_t); i < 8; i++) {
960 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800961 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100962}
963
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100964static int
965_Pickler_CommitFrame(PicklerObject *self)
966{
967 size_t frame_len;
968 char *qdata;
969
970 if (!self->framing || self->frame_start == -1)
971 return 0;
972 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
973 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200974 if (frame_len >= FRAME_SIZE_MIN) {
975 qdata[0] = FRAME;
976 _write_size64(qdata + 1, frame_len);
977 }
978 else {
979 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
980 self->output_len -= FRAME_HEADER_SIZE;
981 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 self->frame_start = -1;
983 return 0;
984}
985
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000986static PyObject *
987_Pickler_GetString(PicklerObject *self)
988{
989 PyObject *output_buffer = self->output_buffer;
990
991 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100992
993 if (_Pickler_CommitFrame(self))
994 return NULL;
995
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000996 self->output_buffer = NULL;
997 /* Resize down to exact size */
998 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
999 return NULL;
1000 return output_buffer;
1001}
1002
1003static int
1004_Pickler_FlushToFile(PicklerObject *self)
1005{
1006 PyObject *output, *result;
1007
1008 assert(self->write != NULL);
1009
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001010 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001011 output = _Pickler_GetString(self);
1012 if (output == NULL)
1013 return -1;
1014
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001015 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001016 Py_XDECREF(result);
1017 return (result == NULL) ? -1 : 0;
1018}
1019
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001020static int
1021_Pickler_OpcodeBoundary(PicklerObject *self)
1022{
1023 Py_ssize_t frame_len;
1024
1025 if (!self->framing || self->frame_start == -1) {
1026 return 0;
1027 }
1028 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1029 if (frame_len >= FRAME_SIZE_TARGET) {
1030 if(_Pickler_CommitFrame(self)) {
1031 return -1;
1032 }
Leo Ariasc3d95082018-02-03 18:36:10 -06001033 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001034 * file and reuse the pickler buffer for the next frame so as
1035 * to limit memory usage when dumping large complex objects to
1036 * a file.
1037 *
1038 * self->write is NULL when called via dumps.
1039 */
1040 if (self->write != NULL) {
1041 if (_Pickler_FlushToFile(self) < 0) {
1042 return -1;
1043 }
1044 if (_Pickler_ClearBuffer(self) < 0) {
1045 return -1;
1046 }
1047 }
1048 }
1049 return 0;
1050}
1051
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001052static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001053_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001054{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001055 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001056 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001057 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001058
1059 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001060 need_new_frame = (self->framing && self->frame_start == -1);
1061
1062 if (need_new_frame)
1063 n = data_len + FRAME_HEADER_SIZE;
1064 else
1065 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001066
1067 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001068 if (required > self->max_output_len) {
1069 /* Make place in buffer for the pickle chunk */
1070 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1071 PyErr_NoMemory();
1072 return -1;
1073 }
1074 self->max_output_len = (self->output_len + n) / 2 * 3;
1075 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1076 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001077 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001078 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001079 if (need_new_frame) {
1080 /* Setup new frame */
1081 Py_ssize_t frame_start = self->output_len;
1082 self->frame_start = frame_start;
1083 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1084 /* Write an invalid value, for debugging */
1085 buffer[frame_start + i] = 0xFE;
1086 }
1087 self->output_len += FRAME_HEADER_SIZE;
1088 }
1089 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001090 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001091 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001092 buffer[self->output_len + i] = s[i];
1093 }
1094 }
1095 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001096 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001097 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001098 self->output_len += data_len;
1099 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001100}
1101
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001102static PicklerObject *
1103_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001104{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001105 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001106
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001107 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1108 if (self == NULL)
1109 return NULL;
1110
1111 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001112 self->dispatch_table = NULL;
Antoine Pitrou91f43802019-05-26 17:10:09 +02001113 self->buffer_callback = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001114 self->write = NULL;
1115 self->proto = 0;
1116 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001117 self->framing = 0;
1118 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001119 self->fast = 0;
1120 self->fast_nesting = 0;
1121 self->fix_imports = 0;
1122 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001123 self->max_output_len = WRITE_BUF_SIZE;
1124 self->output_len = 0;
Pierre Glaser289f1f82019-05-08 23:08:25 +02001125 self->reducer_override = NULL;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001126
1127 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1129 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001130
1131 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001132 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001133 return NULL;
1134 }
Zackery Spytz359bd4f2019-04-23 05:56:08 -06001135
1136 PyObject_GC_Track(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001137 return self;
1138}
1139
1140static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001141_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001142{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001143 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144
Serhiy Storchakad322abb2019-09-14 13:31:50 +03001145 if (protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001146 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001147 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001148 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001149 proto = PyLong_AsLong(protocol);
1150 if (proto < 0) {
1151 if (proto == -1 && PyErr_Occurred())
1152 return -1;
1153 proto = HIGHEST_PROTOCOL;
1154 }
1155 else if (proto > HIGHEST_PROTOCOL) {
1156 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1157 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001158 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001159 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001160 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001161 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001162 self->bin = proto > 0;
1163 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001164 return 0;
1165}
1166
1167/* Returns -1 (with an exception set) on failure, 0 on success. This may
1168 be called once on a freshly created Pickler. */
1169static int
1170_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1171{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001172 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001173 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001174 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1175 return -1;
1176 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001177 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001178 PyErr_SetString(PyExc_TypeError,
1179 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001180 return -1;
1181 }
1182
1183 return 0;
1184}
1185
Antoine Pitrou91f43802019-05-26 17:10:09 +02001186static int
1187_Pickler_SetBufferCallback(PicklerObject *self, PyObject *buffer_callback)
1188{
1189 if (buffer_callback == Py_None) {
1190 buffer_callback = NULL;
1191 }
1192 if (buffer_callback != NULL && self->proto < 5) {
1193 PyErr_SetString(PyExc_ValueError,
1194 "buffer_callback needs protocol >= 5");
1195 return -1;
1196 }
1197
1198 Py_XINCREF(buffer_callback);
1199 self->buffer_callback = buffer_callback;
1200 return 0;
1201}
1202
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001203/* Returns the size of the input on success, -1 on failure. This takes its
1204 own reference to `input`. */
1205static Py_ssize_t
1206_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1207{
1208 if (self->buffer.buf != NULL)
1209 PyBuffer_Release(&self->buffer);
1210 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1211 return -1;
1212 self->input_buffer = self->buffer.buf;
1213 self->input_len = self->buffer.len;
1214 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001215 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001216 return self->input_len;
1217}
1218
Antoine Pitrou04248a82010-10-12 20:51:21 +00001219static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001220bad_readline(void)
1221{
1222 PickleState *st = _Pickle_GetGlobalState();
1223 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1224 return -1;
1225}
1226
Antoine Pitrou91f43802019-05-26 17:10:09 +02001227/* Skip any consumed data that was only prefetched using peek() */
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001228static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001229_Unpickler_SkipConsumed(UnpicklerObject *self)
1230{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001231 Py_ssize_t consumed;
1232 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001233
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001234 consumed = self->next_read_idx - self->prefetched_idx;
1235 if (consumed <= 0)
1236 return 0;
1237
1238 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001239 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001240 r = PyObject_CallFunction(self->read, "n", consumed);
1241 if (r == NULL)
1242 return -1;
1243 Py_DECREF(r);
1244
1245 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001246 return 0;
1247}
1248
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001249static const Py_ssize_t READ_WHOLE_LINE = -1;
1250
1251/* If reading from a file, we need to only pull the bytes we need, since there
1252 may be multiple pickle objects arranged contiguously in the same input
1253 buffer.
1254
1255 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1256 bytes from the input stream/buffer.
1257
1258 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1259 failure; on success, returns the number of bytes read from the file.
1260
1261 On success, self->input_len will be 0; this is intentional so that when
1262 unpickling from a file, the "we've run out of data" code paths will trigger,
1263 causing the Unpickler to go back to the file for more data. Use the returned
1264 size to tell you how much data you can process. */
1265static Py_ssize_t
1266_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1267{
1268 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001269 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001270
1271 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001272
Antoine Pitrou04248a82010-10-12 20:51:21 +00001273 if (_Unpickler_SkipConsumed(self) < 0)
1274 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001275
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001276 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001277 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001278 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001279 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001280 PyObject *len;
1281 /* Prefetch some data without advancing the file pointer, if possible */
1282 if (self->peek && n < PREFETCH) {
1283 len = PyLong_FromSsize_t(PREFETCH);
1284 if (len == NULL)
1285 return -1;
1286 data = _Pickle_FastCall(self->peek, len);
1287 if (data == NULL) {
1288 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1289 return -1;
1290 /* peek() is probably not supported by the given file object */
1291 PyErr_Clear();
1292 Py_CLEAR(self->peek);
1293 }
1294 else {
1295 read_size = _Unpickler_SetStringInput(self, data);
1296 Py_DECREF(data);
1297 self->prefetched_idx = 0;
1298 if (n <= read_size)
1299 return n;
1300 }
1301 }
1302 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001303 if (len == NULL)
1304 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001305 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001306 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001307 if (data == NULL)
1308 return -1;
1309
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001310 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001311 Py_DECREF(data);
1312 return read_size;
1313}
1314
Victor Stinner19ed27e2016-05-20 11:42:37 +02001315/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001316static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001317_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001318{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001319 Py_ssize_t num_read;
1320
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001321 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001322 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1323 PickleState *st = _Pickle_GetGlobalState();
1324 PyErr_SetString(st->UnpicklingError,
1325 "read would overflow (invalid bytecode)");
1326 return -1;
1327 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001328
1329 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1330 assert(self->next_read_idx + n > self->input_len);
1331
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001332 if (!self->read)
1333 return bad_readline();
1334
Antoine Pitrou91f43802019-05-26 17:10:09 +02001335 /* Extend the buffer to satisfy desired size */
Antoine Pitrou04248a82010-10-12 20:51:21 +00001336 num_read = _Unpickler_ReadFromFile(self, n);
1337 if (num_read < 0)
1338 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001339 if (num_read < n)
1340 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001341 *s = self->input_buffer;
1342 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001343 return n;
1344}
1345
Antoine Pitrou91f43802019-05-26 17:10:09 +02001346/* Read `n` bytes from the unpickler's data source, storing the result in `buf`.
1347 *
1348 * This should only be used for non-small data reads where potentially
1349 * avoiding a copy is beneficial. This method does not try to prefetch
1350 * more data into the input buffer.
1351 *
1352 * _Unpickler_Read() is recommended in most cases.
1353 */
1354static Py_ssize_t
1355_Unpickler_ReadInto(UnpicklerObject *self, char *buf, Py_ssize_t n)
1356{
1357 assert(n != READ_WHOLE_LINE);
1358
1359 /* Read from available buffer data, if any */
1360 Py_ssize_t in_buffer = self->input_len - self->next_read_idx;
1361 if (in_buffer > 0) {
1362 Py_ssize_t to_read = Py_MIN(in_buffer, n);
1363 memcpy(buf, self->input_buffer + self->next_read_idx, to_read);
1364 self->next_read_idx += to_read;
1365 buf += to_read;
1366 n -= to_read;
1367 if (n == 0) {
1368 /* Entire read was satisfied from buffer */
1369 return n;
1370 }
1371 }
1372
1373 /* Read from file */
Miss Islington (bot)b19f7ec2020-02-23 14:53:24 -08001374 if (!self->read) {
1375 /* We're unpickling memory, this means the input is truncated */
Antoine Pitrou91f43802019-05-26 17:10:09 +02001376 return bad_readline();
1377 }
1378 if (_Unpickler_SkipConsumed(self) < 0) {
1379 return -1;
1380 }
1381
Miss Islington (bot)b19f7ec2020-02-23 14:53:24 -08001382 if (!self->readinto) {
1383 /* readinto() not supported on file-like object, fall back to read()
1384 * and copy into destination buffer (bpo-39681) */
1385 PyObject* len = PyLong_FromSsize_t(n);
1386 if (len == NULL) {
1387 return -1;
1388 }
1389 PyObject* data = _Pickle_FastCall(self->read, len);
1390 if (data == NULL) {
1391 return -1;
1392 }
1393 if (!PyBytes_Check(data)) {
1394 PyErr_Format(PyExc_ValueError,
1395 "read() returned non-bytes object (%R)",
1396 Py_TYPE(data));
1397 Py_DECREF(data);
1398 return -1;
1399 }
1400 Py_ssize_t read_size = PyBytes_GET_SIZE(data);
1401 if (read_size < n) {
1402 Py_DECREF(data);
1403 return bad_readline();
1404 }
1405 memcpy(buf, PyBytes_AS_STRING(data), n);
1406 Py_DECREF(data);
1407 return n;
1408 }
1409
Antoine Pitrou91f43802019-05-26 17:10:09 +02001410 /* Call readinto() into user buffer */
1411 PyObject *buf_obj = PyMemoryView_FromMemory(buf, n, PyBUF_WRITE);
1412 if (buf_obj == NULL) {
1413 return -1;
1414 }
1415 PyObject *read_size_obj = _Pickle_FastCall(self->readinto, buf_obj);
1416 if (read_size_obj == NULL) {
1417 return -1;
1418 }
1419 Py_ssize_t read_size = PyLong_AsSsize_t(read_size_obj);
1420 Py_DECREF(read_size_obj);
1421
1422 if (read_size < 0) {
1423 if (!PyErr_Occurred()) {
1424 PyErr_SetString(PyExc_ValueError,
1425 "readinto() returned negative size");
1426 }
1427 return -1;
1428 }
1429 if (read_size < n) {
1430 return bad_readline();
1431 }
1432 return n;
1433}
1434
Victor Stinner19ed27e2016-05-20 11:42:37 +02001435/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1436
1437 This should be used for all data reads, rather than accessing the unpickler's
1438 input buffer directly. This method deals correctly with reading from input
1439 streams, which the input buffer doesn't deal with.
1440
1441 Note that when reading from a file-like object, self->next_read_idx won't
1442 be updated (it should remain at 0 for the entire unpickling process). You
1443 should use this function's return value to know how many bytes you can
1444 consume.
1445
1446 Returns -1 (with an exception set) on failure. On success, return the
1447 number of chars read. */
1448#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001449 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001450 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1451 (self)->next_read_idx += (n), \
1452 (n)) \
1453 : _Unpickler_ReadImpl(self, (s), (n)))
1454
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001455static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001456_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1457 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001458{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001459 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001460 if (input_line == NULL) {
1461 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001462 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001463 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001464
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001465 memcpy(input_line, line, len);
1466 input_line[len] = '\0';
1467 self->input_line = input_line;
1468 *result = self->input_line;
1469 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001470}
1471
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001472/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001473 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001474
1475 Returns the number of chars read, or -1 on failure. */
1476static Py_ssize_t
1477_Unpickler_Readline(UnpicklerObject *self, char **result)
1478{
1479 Py_ssize_t i, num_read;
1480
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001481 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001482 if (self->input_buffer[i] == '\n') {
1483 char *line_start = self->input_buffer + self->next_read_idx;
1484 num_read = i - self->next_read_idx + 1;
1485 self->next_read_idx = i + 1;
1486 return _Unpickler_CopyLine(self, line_start, num_read, result);
1487 }
1488 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001489 if (!self->read)
1490 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001491
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001492 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1493 if (num_read < 0)
1494 return -1;
1495 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1496 return bad_readline();
1497 self->next_read_idx = num_read;
1498 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001499}
1500
1501/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1502 will be modified in place. */
1503static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001504_Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001505{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001506 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001507
1508 assert(new_size > self->memo_size);
1509
Sergey Fedoseev67b9cc82018-08-16 09:27:50 +05001510 PyObject **memo_new = self->memo;
1511 PyMem_RESIZE(memo_new, PyObject *, new_size);
1512 if (memo_new == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001513 PyErr_NoMemory();
1514 return -1;
1515 }
Sergey Fedoseev67b9cc82018-08-16 09:27:50 +05001516 self->memo = memo_new;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001517 for (i = self->memo_size; i < new_size; i++)
1518 self->memo[i] = NULL;
1519 self->memo_size = new_size;
1520 return 0;
1521}
1522
1523/* Returns NULL if idx is out of bounds. */
1524static PyObject *
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001525_Unpickler_MemoGet(UnpicklerObject *self, size_t idx)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001526{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001527 if (idx >= self->memo_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001528 return NULL;
1529
1530 return self->memo[idx];
1531}
1532
1533/* Returns -1 (with an exception set) on failure, 0 on success.
1534 This takes its own reference to `value`. */
1535static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001536_Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001537{
1538 PyObject *old_item;
1539
1540 if (idx >= self->memo_size) {
1541 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1542 return -1;
1543 assert(idx < self->memo_size);
1544 }
1545 Py_INCREF(value);
1546 old_item = self->memo[idx];
1547 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001548 if (old_item != NULL) {
1549 Py_DECREF(old_item);
1550 }
1551 else {
1552 self->memo_len++;
1553 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001554 return 0;
1555}
1556
1557static PyObject **
1558_Unpickler_NewMemo(Py_ssize_t new_size)
1559{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001560 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001561 if (memo == NULL) {
1562 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001563 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001564 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001565 memset(memo, 0, new_size * sizeof(PyObject *));
1566 return memo;
1567}
1568
1569/* Free the unpickler's memo, taking care to decref any items left in it. */
1570static void
1571_Unpickler_MemoCleanup(UnpicklerObject *self)
1572{
1573 Py_ssize_t i;
1574 PyObject **memo = self->memo;
1575
1576 if (self->memo == NULL)
1577 return;
1578 self->memo = NULL;
1579 i = self->memo_size;
1580 while (--i >= 0) {
1581 Py_XDECREF(memo[i]);
1582 }
1583 PyMem_FREE(memo);
1584}
1585
1586static UnpicklerObject *
1587_Unpickler_New(void)
1588{
1589 UnpicklerObject *self;
1590
1591 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1592 if (self == NULL)
1593 return NULL;
1594
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001595 self->pers_func = NULL;
1596 self->input_buffer = NULL;
1597 self->input_line = NULL;
1598 self->input_len = 0;
1599 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001600 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001601 self->read = NULL;
Antoine Pitrou91f43802019-05-26 17:10:09 +02001602 self->readinto = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001603 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001604 self->peek = NULL;
Antoine Pitrou91f43802019-05-26 17:10:09 +02001605 self->buffers = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001606 self->encoding = NULL;
1607 self->errors = NULL;
1608 self->marks = NULL;
1609 self->num_marks = 0;
1610 self->marks_size = 0;
1611 self->proto = 0;
1612 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001613 memset(&self->buffer, 0, sizeof(Py_buffer));
1614 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001615 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001616 self->memo = _Unpickler_NewMemo(self->memo_size);
1617 self->stack = (Pdata *)Pdata_New();
1618
1619 if (self->memo == NULL || self->stack == NULL) {
1620 Py_DECREF(self);
1621 return NULL;
1622 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001623
Zackery Spytz359bd4f2019-04-23 05:56:08 -06001624 PyObject_GC_Track(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001625 return self;
1626}
1627
1628/* Returns -1 (with an exception set) on failure, 0 on success. This may
Antoine Pitrou91f43802019-05-26 17:10:09 +02001629 be called once on a freshly created Unpickler. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001630static int
1631_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1632{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001633 _Py_IDENTIFIER(peek);
1634 _Py_IDENTIFIER(read);
Antoine Pitrou91f43802019-05-26 17:10:09 +02001635 _Py_IDENTIFIER(readinto);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001636 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001637
Miss Islington (bot)b19f7ec2020-02-23 14:53:24 -08001638 /* Optional file methods */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001639 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1640 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001641 }
Miss Islington (bot)b19f7ec2020-02-23 14:53:24 -08001642 if (_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto) < 0) {
1643 return -1;
1644 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001645 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1646 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Miss Islington (bot)b19f7ec2020-02-23 14:53:24 -08001647 if (!self->readline || !self->read) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001648 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001649 PyErr_SetString(PyExc_TypeError,
Miss Islington (bot)b19f7ec2020-02-23 14:53:24 -08001650 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001651 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001652 Py_CLEAR(self->read);
Antoine Pitrou91f43802019-05-26 17:10:09 +02001653 Py_CLEAR(self->readinto);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001654 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001655 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001656 return -1;
1657 }
1658 return 0;
1659}
1660
1661/* Returns -1 (with an exception set) on failure, 0 on success. This may
Antoine Pitrou91f43802019-05-26 17:10:09 +02001662 be called once on a freshly created Unpickler. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001663static int
1664_Unpickler_SetInputEncoding(UnpicklerObject *self,
1665 const char *encoding,
1666 const char *errors)
1667{
1668 if (encoding == NULL)
1669 encoding = "ASCII";
1670 if (errors == NULL)
1671 errors = "strict";
1672
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001673 self->encoding = _PyMem_Strdup(encoding);
1674 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001675 if (self->encoding == NULL || self->errors == NULL) {
1676 PyErr_NoMemory();
1677 return -1;
1678 }
1679 return 0;
1680}
1681
Antoine Pitrou91f43802019-05-26 17:10:09 +02001682/* Returns -1 (with an exception set) on failure, 0 on success. This may
1683 be called once on a freshly created Unpickler. */
1684static int
1685_Unpickler_SetBuffers(UnpicklerObject *self, PyObject *buffers)
1686{
Miss Islington (bot)25cb4fd2019-07-25 09:18:20 -07001687 if (buffers == NULL || buffers == Py_None) {
Antoine Pitrou91f43802019-05-26 17:10:09 +02001688 self->buffers = NULL;
1689 }
1690 else {
1691 self->buffers = PyObject_GetIter(buffers);
1692 if (self->buffers == NULL) {
1693 return -1;
1694 }
1695 }
1696 return 0;
1697}
1698
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001699/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001700static int
1701memo_get(PicklerObject *self, PyObject *key)
1702{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001703 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001704 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001705 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001706
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001707 value = PyMemoTable_Get(self->memo, key);
1708 if (value == NULL) {
1709 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001710 return -1;
1711 }
1712
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001713 if (!self->bin) {
1714 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001715 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1716 "%" PY_FORMAT_SIZE_T "d\n", *value);
1717 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001718 }
1719 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001720 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001721 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001722 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001723 len = 2;
1724 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001725 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001726 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001727 pdata[1] = (unsigned char)(*value & 0xff);
1728 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1729 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1730 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001731 len = 5;
1732 }
1733 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001734 PickleState *st = _Pickle_GetGlobalState();
1735 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001736 "memo id too large for LONG_BINGET");
1737 return -1;
1738 }
1739 }
1740
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001741 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001742 return -1;
1743
1744 return 0;
1745}
1746
1747/* Store an object in the memo, assign it a new unique ID based on the number
1748 of objects currently stored in the memo and generate a PUT opcode. */
1749static int
1750memo_put(PicklerObject *self, PyObject *obj)
1751{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001752 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001753 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001754 Py_ssize_t idx;
1755
1756 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001757
1758 if (self->fast)
1759 return 0;
1760
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001761 idx = PyMemoTable_Size(self->memo);
1762 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1763 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001764
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001765 if (self->proto >= 4) {
1766 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1767 return -1;
1768 return 0;
1769 }
1770 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001771 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001772 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001773 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001774 len = strlen(pdata);
1775 }
1776 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001777 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001778 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001779 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001780 len = 2;
1781 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001782 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001783 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001784 pdata[1] = (unsigned char)(idx & 0xff);
1785 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1786 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1787 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001788 len = 5;
1789 }
1790 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001791 PickleState *st = _Pickle_GetGlobalState();
1792 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001793 "memo id too large for LONG_BINPUT");
1794 return -1;
1795 }
1796 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001797 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001798 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001799
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001800 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001801}
1802
1803static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001804get_dotted_path(PyObject *obj, PyObject *name)
1805{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001806 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001807 PyObject *dotted_path;
1808 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001809
1810 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001811 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001812 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001813 n = PyList_GET_SIZE(dotted_path);
1814 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001815 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001816 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001817 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001818 if (obj == NULL)
1819 PyErr_Format(PyExc_AttributeError,
1820 "Can't pickle local object %R", name);
1821 else
1822 PyErr_Format(PyExc_AttributeError,
1823 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001824 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001825 return NULL;
1826 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001827 }
1828 return dotted_path;
1829}
1830
1831static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001832get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001833{
1834 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001835 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001836
1837 assert(PyList_CheckExact(names));
1838 Py_INCREF(obj);
1839 n = PyList_GET_SIZE(names);
1840 for (i = 0; i < n; i++) {
1841 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001842 Py_XDECREF(parent);
1843 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001844 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001845 if (obj == NULL) {
1846 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001847 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001848 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001849 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001850 if (pparent != NULL)
1851 *pparent = parent;
1852 else
1853 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001854 return obj;
1855}
1856
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001857
1858static PyObject *
1859getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1860{
1861 PyObject *dotted_path, *attr;
1862
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001863 if (allow_qualname) {
1864 dotted_path = get_dotted_path(obj, name);
1865 if (dotted_path == NULL)
1866 return NULL;
1867 attr = get_deep_attribute(obj, dotted_path, NULL);
1868 Py_DECREF(dotted_path);
1869 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001870 else {
1871 (void)_PyObject_LookupAttr(obj, name, &attr);
1872 }
1873 if (attr == NULL && !PyErr_Occurred()) {
1874 PyErr_Format(PyExc_AttributeError,
1875 "Can't get attribute %R on %R", name, obj);
1876 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001877 return attr;
1878}
1879
Eric Snow3f9eee62017-09-15 16:35:20 -06001880static int
1881_checkmodule(PyObject *module_name, PyObject *module,
1882 PyObject *global, PyObject *dotted_path)
1883{
1884 if (module == Py_None) {
1885 return -1;
1886 }
1887 if (PyUnicode_Check(module_name) &&
1888 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1889 return -1;
1890 }
1891
1892 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1893 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001894 return -1;
1895 }
1896 if (candidate != global) {
1897 Py_DECREF(candidate);
1898 return -1;
1899 }
1900 Py_DECREF(candidate);
1901 return 0;
1902}
1903
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001904static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001905whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001907 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001908 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001909 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001910 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001911 _Py_IDENTIFIER(__module__);
1912 _Py_IDENTIFIER(modules);
1913 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001914
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001915 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1916 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001917 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001918 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001919 /* In some rare cases (e.g., bound methods of extension types),
1920 __module__ can be None. If it is so, then search sys.modules for
1921 the module of global. */
1922 if (module_name != Py_None)
1923 return module_name;
1924 Py_CLEAR(module_name);
1925 }
1926 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001927
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001928 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001929 modules = _PySys_GetObjectId(&PyId_modules);
1930 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001931 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001932 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001933 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001934 if (PyDict_CheckExact(modules)) {
1935 i = 0;
1936 while (PyDict_Next(modules, &i, &module_name, &module)) {
1937 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1938 Py_INCREF(module_name);
1939 return module_name;
1940 }
1941 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001942 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001943 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001944 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001945 }
1946 else {
1947 PyObject *iterator = PyObject_GetIter(modules);
1948 if (iterator == NULL) {
1949 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001950 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001951 while ((module_name = PyIter_Next(iterator))) {
1952 module = PyObject_GetItem(modules, module_name);
1953 if (module == NULL) {
1954 Py_DECREF(module_name);
1955 Py_DECREF(iterator);
1956 return NULL;
1957 }
1958 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1959 Py_DECREF(module);
1960 Py_DECREF(iterator);
1961 return module_name;
1962 }
1963 Py_DECREF(module);
1964 Py_DECREF(module_name);
1965 if (PyErr_Occurred()) {
1966 Py_DECREF(iterator);
1967 return NULL;
1968 }
1969 }
1970 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001971 }
1972
1973 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001974 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001975 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976 return module_name;
1977}
1978
1979/* fast_save_enter() and fast_save_leave() are guards against recursive
1980 objects when Pickler is used with the "fast mode" (i.e., with object
1981 memoization disabled). If the nesting of a list or dict object exceed
1982 FAST_NESTING_LIMIT, these guards will start keeping an internal
1983 reference to the seen list or dict objects and check whether these objects
1984 are recursive. These are not strictly necessary, since save() has a
1985 hard-coded recursion limit, but they give a nicer error message than the
1986 typical RuntimeError. */
1987static int
1988fast_save_enter(PicklerObject *self, PyObject *obj)
1989{
1990 /* if fast_nesting < 0, we're doing an error exit. */
1991 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1992 PyObject *key = NULL;
1993 if (self->fast_memo == NULL) {
1994 self->fast_memo = PyDict_New();
1995 if (self->fast_memo == NULL) {
1996 self->fast_nesting = -1;
1997 return 0;
1998 }
1999 }
2000 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05002001 if (key == NULL) {
2002 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002003 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05002004 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08002005 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002006 Py_DECREF(key);
2007 PyErr_Format(PyExc_ValueError,
2008 "fast mode: can't pickle cyclic objects "
2009 "including object type %.200s at %p",
2010 obj->ob_type->tp_name, obj);
2011 self->fast_nesting = -1;
2012 return 0;
2013 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08002014 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05002015 Py_DECREF(key);
2016 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08002017 return 0;
2018 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002019 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
2020 Py_DECREF(key);
2021 self->fast_nesting = -1;
2022 return 0;
2023 }
2024 Py_DECREF(key);
2025 }
2026 return 1;
2027}
2028
2029static int
2030fast_save_leave(PicklerObject *self, PyObject *obj)
2031{
2032 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
2033 PyObject *key = PyLong_FromVoidPtr(obj);
2034 if (key == NULL)
2035 return 0;
2036 if (PyDict_DelItem(self->fast_memo, key) < 0) {
2037 Py_DECREF(key);
2038 return 0;
2039 }
2040 Py_DECREF(key);
2041 }
2042 return 1;
2043}
2044
2045static int
2046save_none(PicklerObject *self, PyObject *obj)
2047{
2048 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002049 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002050 return -1;
2051
2052 return 0;
2053}
2054
2055static int
2056save_bool(PicklerObject *self, PyObject *obj)
2057{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002058 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08002059 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002060 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061 return -1;
2062 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08002063 else {
2064 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
2065 * so that unpicklers written before bools were introduced unpickle them
2066 * as ints, but unpicklers after can recognize that bools were intended.
2067 * Note that protocol 2 added direct ways to pickle bools.
2068 */
2069 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
2070 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
2071 return -1;
2072 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073 return 0;
2074}
2075
2076static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08002077save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002078{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08002079 PyObject *repr = NULL;
2080 Py_ssize_t size;
2081 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002082 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08002083 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002084
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002085 val= PyLong_AsLongAndOverflow(obj, &overflow);
2086 if (!overflow && (sizeof(long) <= 4 ||
2087 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
2088 {
Larry Hastings61272b72014-01-07 12:41:53 -08002089 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002090
2091 Note: we can't use -0x80000000L in the above condition because some
2092 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
2093 before applying the unary minus when sizeof(long) <= 4. The
2094 resulting value stays unsigned which is commonly not what we want,
2095 so MSVC happily warns us about it. However, that result would have
2096 been fine because we guard for sizeof(long) <= 4 which turns the
2097 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08002098 char pdata[32];
2099 Py_ssize_t len = 0;
2100
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002101 if (self->bin) {
2102 pdata[1] = (unsigned char)(val & 0xff);
2103 pdata[2] = (unsigned char)((val >> 8) & 0xff);
2104 pdata[3] = (unsigned char)((val >> 16) & 0xff);
2105 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002106
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002107 if ((pdata[4] != 0) || (pdata[3] != 0)) {
2108 pdata[0] = BININT;
2109 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002110 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002111 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002112 pdata[0] = BININT2;
2113 len = 3;
2114 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002115 else {
2116 pdata[0] = BININT1;
2117 len = 2;
2118 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002119 }
2120 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002121 sprintf(pdata, "%c%ld\n", INT, val);
2122 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002123 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002124 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002125 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08002126
2127 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002128 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002129 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002130
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002131 if (self->proto >= 2) {
2132 /* Linear-time pickling. */
2133 size_t nbits;
2134 size_t nbytes;
2135 unsigned char *pdata;
2136 char header[5];
2137 int i;
2138 int sign = _PyLong_Sign(obj);
2139
2140 if (sign == 0) {
2141 header[0] = LONG1;
2142 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002143 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002144 goto error;
2145 return 0;
2146 }
2147 nbits = _PyLong_NumBits(obj);
2148 if (nbits == (size_t)-1 && PyErr_Occurred())
2149 goto error;
2150 /* How many bytes do we need? There are nbits >> 3 full
2151 * bytes of data, and nbits & 7 leftover bits. If there
2152 * are any leftover bits, then we clearly need another
Kyle Stanley24b5b362019-07-21 22:48:45 -04002153 * byte. What's not so obvious is that we *probably*
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002154 * need another byte even if there aren't any leftovers:
2155 * the most-significant bit of the most-significant byte
2156 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002157 * opposite of the one we need. The exception is ints
2158 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002159 * its own 256's-complement, so has the right sign bit
2160 * even without the extra byte. That's a pain to check
2161 * for in advance, though, so we always grab an extra
2162 * byte at the start, and cut it back later if possible.
2163 */
2164 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002165 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002166 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002167 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002168 goto error;
2169 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002170 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002171 if (repr == NULL)
2172 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002173 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002174 i = _PyLong_AsByteArray((PyLongObject *)obj,
2175 pdata, nbytes,
2176 1 /* little endian */ , 1 /* signed */ );
2177 if (i < 0)
2178 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002179 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002180 * needed. This is so iff the MSB is all redundant sign
2181 * bits.
2182 */
2183 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002184 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002185 pdata[nbytes - 1] == 0xff &&
2186 (pdata[nbytes - 2] & 0x80) != 0) {
2187 nbytes--;
2188 }
2189
2190 if (nbytes < 256) {
2191 header[0] = LONG1;
2192 header[1] = (unsigned char)nbytes;
2193 size = 2;
2194 }
2195 else {
2196 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002197 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002198 for (i = 1; i < 5; i++) {
2199 header[i] = (unsigned char)(size & 0xff);
2200 size >>= 8;
2201 }
2202 size = 5;
2203 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002204 if (_Pickler_Write(self, header, size) < 0 ||
2205 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002206 goto error;
2207 }
2208 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002209 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002210 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002211
Mark Dickinson8dd05142009-01-20 20:43:58 +00002212 /* proto < 2: write the repr and newline. This is quadratic-time (in
2213 the number of digits), in both directions. We add a trailing 'L'
2214 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002215
2216 repr = PyObject_Repr(obj);
2217 if (repr == NULL)
2218 goto error;
2219
Serhiy Storchaka06515832016-11-20 09:13:07 +02002220 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002221 if (string == NULL)
2222 goto error;
2223
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002224 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2225 _Pickler_Write(self, string, size) < 0 ||
2226 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002227 goto error;
2228 }
2229
2230 if (0) {
2231 error:
2232 status = -1;
2233 }
2234 Py_XDECREF(repr);
2235
2236 return status;
2237}
2238
2239static int
2240save_float(PicklerObject *self, PyObject *obj)
2241{
2242 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2243
2244 if (self->bin) {
2245 char pdata[9];
2246 pdata[0] = BINFLOAT;
2247 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2248 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002249 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002250 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002251 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002253 int result = -1;
2254 char *buf = NULL;
2255 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002257 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002258 goto done;
2259
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002260 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002261 if (!buf) {
2262 PyErr_NoMemory();
2263 goto done;
2264 }
2265
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002266 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002267 goto done;
2268
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002269 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002270 goto done;
2271
2272 result = 0;
2273done:
2274 PyMem_Free(buf);
2275 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 }
2277
2278 return 0;
2279}
2280
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002281/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002282
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002283 The large contiguous data is written directly into the underlying file
2284 object, bypassing the output_buffer of the Pickler. We intentionally
2285 do not insert a protocol 4 frame opcode to make it possible to optimize
2286 file.read calls in the loader.
2287 */
2288static int
2289_Pickler_write_bytes(PicklerObject *self,
2290 const char *header, Py_ssize_t header_size,
2291 const char *data, Py_ssize_t data_size,
2292 PyObject *payload)
2293{
2294 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2295 int framing = self->framing;
2296
2297 if (bypass_buffer) {
2298 assert(self->output_buffer != NULL);
2299 /* Commit the previous frame. */
2300 if (_Pickler_CommitFrame(self)) {
2301 return -1;
2302 }
2303 /* Disable framing temporarily */
2304 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002305 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002306
2307 if (_Pickler_Write(self, header, header_size) < 0) {
2308 return -1;
2309 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002310
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002311 if (bypass_buffer && self->write != NULL) {
2312 /* Bypass the in-memory buffer to directly stream large data
2313 into the underlying file object. */
2314 PyObject *result, *mem = NULL;
2315 /* Dump the output buffer to the file. */
2316 if (_Pickler_FlushToFile(self) < 0) {
2317 return -1;
2318 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002319
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002320 /* Stream write the payload into the file without going through the
2321 output buffer. */
2322 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002323 /* TODO: It would be better to use a memoryview with a linked
2324 original string if this is possible. */
2325 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002326 if (payload == NULL) {
2327 return -1;
2328 }
2329 }
2330 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2331 Py_XDECREF(mem);
2332 if (result == NULL) {
2333 return -1;
2334 }
2335 Py_DECREF(result);
2336
2337 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2338 if (_Pickler_ClearBuffer(self) < 0) {
2339 return -1;
2340 }
2341 }
2342 else {
2343 if (_Pickler_Write(self, data, data_size) < 0) {
2344 return -1;
2345 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002346 }
2347
2348 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002349 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002350
2351 return 0;
2352}
2353
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002354static int
Antoine Pitrou91f43802019-05-26 17:10:09 +02002355_save_bytes_data(PicklerObject *self, PyObject *obj, const char *data,
2356 Py_ssize_t size)
2357{
2358 assert(self->proto >= 3);
2359
2360 char header[9];
2361 Py_ssize_t len;
2362
2363 if (size < 0)
2364 return -1;
2365
2366 if (size <= 0xff) {
2367 header[0] = SHORT_BINBYTES;
2368 header[1] = (unsigned char)size;
2369 len = 2;
2370 }
2371 else if ((size_t)size <= 0xffffffffUL) {
2372 header[0] = BINBYTES;
2373 header[1] = (unsigned char)(size & 0xff);
2374 header[2] = (unsigned char)((size >> 8) & 0xff);
2375 header[3] = (unsigned char)((size >> 16) & 0xff);
2376 header[4] = (unsigned char)((size >> 24) & 0xff);
2377 len = 5;
2378 }
2379 else if (self->proto >= 4) {
2380 header[0] = BINBYTES8;
2381 _write_size64(header + 1, size);
2382 len = 9;
2383 }
2384 else {
2385 PyErr_SetString(PyExc_OverflowError,
2386 "serializing a bytes object larger than 4 GiB "
2387 "requires pickle protocol 4 or higher");
2388 return -1;
2389 }
2390
2391 if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) {
2392 return -1;
2393 }
2394
2395 if (memo_put(self, obj) < 0) {
2396 return -1;
2397 }
2398
2399 return 0;
2400}
2401
2402static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002403save_bytes(PicklerObject *self, PyObject *obj)
2404{
2405 if (self->proto < 3) {
2406 /* Older pickle protocols do not have an opcode for pickling bytes
2407 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002408 the __reduce__ method) to permit bytes object unpickling.
2409
2410 Here we use a hack to be compatible with Python 2. Since in Python
2411 2 'bytes' is just an alias for 'str' (which has different
2412 parameters than the actual bytes object), we use codecs.encode
2413 to create the appropriate 'str' object when unpickled using
2414 Python 2 *and* the appropriate 'bytes' object when unpickled
2415 using Python 3. Again this is a hack and we don't need to do this
2416 with newer protocols. */
Pierre Glaser289f1f82019-05-08 23:08:25 +02002417 PyObject *reduce_value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002418 int status;
2419
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002420 if (PyBytes_GET_SIZE(obj) == 0) {
2421 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2422 }
2423 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002424 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002425 PyObject *unicode_str =
2426 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2427 PyBytes_GET_SIZE(obj),
2428 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002429 _Py_IDENTIFIER(latin1);
2430
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002431 if (unicode_str == NULL)
2432 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002433 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002434 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002435 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002436 Py_DECREF(unicode_str);
2437 }
2438
2439 if (reduce_value == NULL)
2440 return -1;
2441
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002442 /* save_reduce() will memoize the object automatically. */
2443 status = save_reduce(self, reduce_value, obj);
2444 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002445 return status;
2446 }
2447 else {
Antoine Pitrou91f43802019-05-26 17:10:09 +02002448 return _save_bytes_data(self, obj, PyBytes_AS_STRING(obj),
2449 PyBytes_GET_SIZE(obj));
2450 }
2451}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002452
Antoine Pitrou91f43802019-05-26 17:10:09 +02002453static int
2454_save_bytearray_data(PicklerObject *self, PyObject *obj, const char *data,
2455 Py_ssize_t size)
2456{
2457 assert(self->proto >= 5);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002458
Antoine Pitrou91f43802019-05-26 17:10:09 +02002459 char header[9];
2460 Py_ssize_t len;
2461
2462 if (size < 0)
2463 return -1;
2464
2465 header[0] = BYTEARRAY8;
2466 _write_size64(header + 1, size);
2467 len = 9;
2468
2469 if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) {
2470 return -1;
2471 }
2472
2473 if (memo_put(self, obj) < 0) {
2474 return -1;
2475 }
2476
2477 return 0;
2478}
2479
2480static int
2481save_bytearray(PicklerObject *self, PyObject *obj)
2482{
2483 if (self->proto < 5) {
2484 /* Older pickle protocols do not have an opcode for pickling
2485 * bytearrays. */
2486 PyObject *reduce_value = NULL;
2487 int status;
2488
2489 if (PyByteArray_GET_SIZE(obj) == 0) {
2490 reduce_value = Py_BuildValue("(O())",
2491 (PyObject *) &PyByteArray_Type);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002492 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002493 else {
Antoine Pitrou91f43802019-05-26 17:10:09 +02002494 PyObject *bytes_obj = PyBytes_FromObject(obj);
2495 if (bytes_obj != NULL) {
2496 reduce_value = Py_BuildValue("(O(O))",
2497 (PyObject *) &PyByteArray_Type,
2498 bytes_obj);
2499 Py_DECREF(bytes_obj);
2500 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002501 }
Antoine Pitrou91f43802019-05-26 17:10:09 +02002502 if (reduce_value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002503 return -1;
2504
Antoine Pitrou91f43802019-05-26 17:10:09 +02002505 /* save_reduce() will memoize the object automatically. */
2506 status = save_reduce(self, reduce_value, obj);
2507 Py_DECREF(reduce_value);
2508 return status;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002509 }
Antoine Pitrou91f43802019-05-26 17:10:09 +02002510 else {
2511 return _save_bytearray_data(self, obj, PyByteArray_AS_STRING(obj),
2512 PyByteArray_GET_SIZE(obj));
2513 }
2514}
2515
2516static int
2517save_picklebuffer(PicklerObject *self, PyObject *obj)
2518{
2519 if (self->proto < 5) {
2520 PickleState *st = _Pickle_GetGlobalState();
2521 PyErr_SetString(st->PicklingError,
2522 "PickleBuffer can only pickled with protocol >= 5");
2523 return -1;
2524 }
2525 const Py_buffer* view = PyPickleBuffer_GetBuffer(obj);
2526 if (view == NULL) {
2527 return -1;
2528 }
2529 if (view->suboffsets != NULL || !PyBuffer_IsContiguous(view, 'A')) {
2530 PickleState *st = _Pickle_GetGlobalState();
2531 PyErr_SetString(st->PicklingError,
2532 "PickleBuffer can not be pickled when "
2533 "pointing to a non-contiguous buffer");
2534 return -1;
2535 }
2536 int in_band = 1;
2537 if (self->buffer_callback != NULL) {
2538 PyObject *ret = PyObject_CallFunctionObjArgs(self->buffer_callback,
2539 obj, NULL);
2540 if (ret == NULL) {
2541 return -1;
2542 }
2543 in_band = PyObject_IsTrue(ret);
2544 Py_DECREF(ret);
2545 if (in_band == -1) {
2546 return -1;
2547 }
2548 }
2549 if (in_band) {
2550 /* Write data in-band */
2551 if (view->readonly) {
2552 return _save_bytes_data(self, obj, (const char*) view->buf,
2553 view->len);
2554 }
2555 else {
2556 return _save_bytearray_data(self, obj, (const char*) view->buf,
2557 view->len);
2558 }
2559 }
2560 else {
2561 /* Write data out-of-band */
2562 const char next_buffer_op = NEXT_BUFFER;
2563 if (_Pickler_Write(self, &next_buffer_op, 1) < 0) {
2564 return -1;
2565 }
2566 if (view->readonly) {
2567 const char readonly_buffer_op = READONLY_BUFFER;
2568 if (_Pickler_Write(self, &readonly_buffer_op, 1) < 0) {
2569 return -1;
2570 }
2571 }
2572 }
2573 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002574}
2575
2576/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2577 backslash and newline characters to \uXXXX escapes. */
2578static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002579raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002580{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002581 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002582 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002583 void *data;
2584 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002585 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002586
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002587 if (PyUnicode_READY(obj))
2588 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002589
Victor Stinner358af132015-10-12 22:36:57 +02002590 _PyBytesWriter_Init(&writer);
2591
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002592 size = PyUnicode_GET_LENGTH(obj);
2593 data = PyUnicode_DATA(obj);
2594 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002595
Victor Stinner358af132015-10-12 22:36:57 +02002596 p = _PyBytesWriter_Alloc(&writer, size);
2597 if (p == NULL)
2598 goto error;
2599 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002600
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002601 for (i=0; i < size; i++) {
2602 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002603 /* Map 32-bit characters to '\Uxxxxxxxx' */
2604 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002605 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002606 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2607 if (p == NULL)
2608 goto error;
2609
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002610 *p++ = '\\';
2611 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002612 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2613 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2614 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2615 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2616 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2617 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2618 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2619 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002620 }
Victor Stinner358af132015-10-12 22:36:57 +02002621 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Serhiy Storchaka38ab7d42019-05-31 11:29:39 +03002622 else if (ch >= 256 ||
2623 ch == '\\' || ch == 0 || ch == '\n' || ch == '\r' ||
2624 ch == 0x1a)
2625 {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002626 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002627 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2628 if (p == NULL)
2629 goto error;
2630
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002631 *p++ = '\\';
2632 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002633 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2634 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2635 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2636 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002637 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002638 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002639 else
2640 *p++ = (char) ch;
2641 }
Victor Stinner358af132015-10-12 22:36:57 +02002642
2643 return _PyBytesWriter_Finish(&writer, p);
2644
2645error:
2646 _PyBytesWriter_Dealloc(&writer);
2647 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002648}
2649
2650static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002651write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002652{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002653 char header[9];
2654 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002655 PyObject *encoded = NULL;
2656 Py_ssize_t size;
2657 const char *data;
2658
2659 if (PyUnicode_READY(obj))
2660 return -1;
2661
2662 data = PyUnicode_AsUTF8AndSize(obj, &size);
2663 if (data == NULL) {
2664 /* Issue #8383: for strings with lone surrogates, fallback on the
2665 "surrogatepass" error handler. */
2666 PyErr_Clear();
2667 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2668 if (encoded == NULL)
2669 return -1;
2670
2671 data = PyBytes_AS_STRING(encoded);
2672 size = PyBytes_GET_SIZE(encoded);
2673 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002674
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002675 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002676 if (size <= 0xff && self->proto >= 4) {
2677 header[0] = SHORT_BINUNICODE;
2678 header[1] = (unsigned char)(size & 0xff);
2679 len = 2;
2680 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002681 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002682 header[0] = BINUNICODE;
2683 header[1] = (unsigned char)(size & 0xff);
2684 header[2] = (unsigned char)((size >> 8) & 0xff);
2685 header[3] = (unsigned char)((size >> 16) & 0xff);
2686 header[4] = (unsigned char)((size >> 24) & 0xff);
2687 len = 5;
2688 }
2689 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002690 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002691 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002692 len = 9;
2693 }
2694 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002695 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou91f43802019-05-26 17:10:09 +02002696 "serializing a string larger than 4 GiB "
2697 "requires pickle protocol 4 or higher");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002698 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002699 return -1;
2700 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002701
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002702 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2703 Py_XDECREF(encoded);
2704 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002705 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002706 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002707 return 0;
2708}
2709
2710static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002711save_unicode(PicklerObject *self, PyObject *obj)
2712{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002713 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002714 if (write_unicode_binary(self, obj) < 0)
2715 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002716 }
2717 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002718 PyObject *encoded;
2719 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002720 const char unicode_op = UNICODE;
2721
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002722 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002723 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002724 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002725
Antoine Pitrou299978d2013-04-07 17:38:11 +02002726 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2727 Py_DECREF(encoded);
2728 return -1;
2729 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002730
2731 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002732 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2733 Py_DECREF(encoded);
2734 return -1;
2735 }
2736 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002737
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002738 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002739 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002740 }
2741 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002742 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002743
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002744 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002745}
2746
2747/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2748static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002749store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002750{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002751 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002752
2753 assert(PyTuple_Size(t) == len);
2754
2755 for (i = 0; i < len; i++) {
2756 PyObject *element = PyTuple_GET_ITEM(t, i);
2757
2758 if (element == NULL)
2759 return -1;
2760 if (save(self, element, 0) < 0)
2761 return -1;
2762 }
2763
2764 return 0;
2765}
2766
2767/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2768 * used across protocols to minimize the space needed to pickle them.
2769 * Tuples are also the only builtin immutable type that can be recursive
2770 * (a tuple can be reached from itself), and that requires some subtle
2771 * magic so that it works in all cases. IOW, this is a long routine.
2772 */
2773static int
2774save_tuple(PicklerObject *self, PyObject *obj)
2775{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002776 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002777
2778 const char mark_op = MARK;
2779 const char tuple_op = TUPLE;
2780 const char pop_op = POP;
2781 const char pop_mark_op = POP_MARK;
2782 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2783
2784 if ((len = PyTuple_Size(obj)) < 0)
2785 return -1;
2786
2787 if (len == 0) {
2788 char pdata[2];
2789
2790 if (self->proto) {
2791 pdata[0] = EMPTY_TUPLE;
2792 len = 1;
2793 }
2794 else {
2795 pdata[0] = MARK;
2796 pdata[1] = TUPLE;
2797 len = 2;
2798 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002799 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002800 return -1;
2801 return 0;
2802 }
2803
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002804 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002805 * saving the tuple elements, the tuple must be recursive, in
2806 * which case we'll pop everything we put on the stack, and fetch
2807 * its value from the memo.
2808 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002809 if (len <= 3 && self->proto >= 2) {
2810 /* Use TUPLE{1,2,3} opcodes. */
2811 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002812 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002813
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002814 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002815 /* pop the len elements */
2816 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002817 if (_Pickler_Write(self, &pop_op, 1) < 0)
2818 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002819 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002820 if (memo_get(self, obj) < 0)
2821 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002822
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002823 return 0;
2824 }
2825 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002826 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2827 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002828 }
2829 goto memoize;
2830 }
2831
2832 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2833 * Generate MARK e1 e2 ... TUPLE
2834 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002835 if (_Pickler_Write(self, &mark_op, 1) < 0)
2836 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002837
2838 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002839 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002840
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002841 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002842 /* pop the stack stuff we pushed */
2843 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002844 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2845 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002846 }
2847 else {
2848 /* Note that we pop one more than len, to remove
2849 * the MARK too.
2850 */
2851 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002852 if (_Pickler_Write(self, &pop_op, 1) < 0)
2853 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002854 }
2855 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002856 if (memo_get(self, obj) < 0)
2857 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002858
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002859 return 0;
2860 }
2861 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002862 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2863 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002864 }
2865
2866 memoize:
2867 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002868 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002869
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002870 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002871}
2872
2873/* iter is an iterator giving items, and we batch up chunks of
2874 * MARK item item ... item APPENDS
2875 * opcode sequences. Calling code should have arranged to first create an
2876 * empty list, or list-like object, for the APPENDS to operate on.
2877 * Returns 0 on success, <0 on error.
2878 */
2879static int
2880batch_list(PicklerObject *self, PyObject *iter)
2881{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002882 PyObject *obj = NULL;
2883 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002884 int i, n;
2885
2886 const char mark_op = MARK;
2887 const char append_op = APPEND;
2888 const char appends_op = APPENDS;
2889
2890 assert(iter != NULL);
2891
2892 /* XXX: I think this function could be made faster by avoiding the
2893 iterator interface and fetching objects directly from list using
2894 PyList_GET_ITEM.
2895 */
2896
2897 if (self->proto == 0) {
2898 /* APPENDS isn't available; do one at a time. */
2899 for (;;) {
2900 obj = PyIter_Next(iter);
2901 if (obj == NULL) {
2902 if (PyErr_Occurred())
2903 return -1;
2904 break;
2905 }
2906 i = save(self, obj, 0);
2907 Py_DECREF(obj);
2908 if (i < 0)
2909 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002910 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002911 return -1;
2912 }
2913 return 0;
2914 }
2915
2916 /* proto > 0: write in batches of BATCHSIZE. */
2917 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002918 /* Get first item */
2919 firstitem = PyIter_Next(iter);
2920 if (firstitem == NULL) {
2921 if (PyErr_Occurred())
2922 goto error;
2923
2924 /* nothing more to add */
2925 break;
2926 }
2927
2928 /* Try to get a second item */
2929 obj = PyIter_Next(iter);
2930 if (obj == NULL) {
2931 if (PyErr_Occurred())
2932 goto error;
2933
2934 /* Only one item to write */
2935 if (save(self, firstitem, 0) < 0)
2936 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002937 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002938 goto error;
2939 Py_CLEAR(firstitem);
2940 break;
2941 }
2942
2943 /* More than one item to write */
2944
2945 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002946 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002947 goto error;
2948
2949 if (save(self, firstitem, 0) < 0)
2950 goto error;
2951 Py_CLEAR(firstitem);
2952 n = 1;
2953
2954 /* Fetch and save up to BATCHSIZE items */
2955 while (obj) {
2956 if (save(self, obj, 0) < 0)
2957 goto error;
2958 Py_CLEAR(obj);
2959 n += 1;
2960
2961 if (n == BATCHSIZE)
2962 break;
2963
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002964 obj = PyIter_Next(iter);
2965 if (obj == NULL) {
2966 if (PyErr_Occurred())
2967 goto error;
2968 break;
2969 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002970 }
2971
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002972 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002973 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002974
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002975 } while (n == BATCHSIZE);
2976 return 0;
2977
2978 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002979 Py_XDECREF(firstitem);
2980 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002981 return -1;
2982}
2983
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002984/* This is a variant of batch_list() above, specialized for lists (with no
2985 * support for list subclasses). Like batch_list(), we batch up chunks of
2986 * MARK item item ... item APPENDS
2987 * opcode sequences. Calling code should have arranged to first create an
2988 * empty list, or list-like object, for the APPENDS to operate on.
2989 * Returns 0 on success, -1 on error.
2990 *
2991 * This version is considerably faster than batch_list(), if less general.
2992 *
2993 * Note that this only works for protocols > 0.
2994 */
2995static int
2996batch_list_exact(PicklerObject *self, PyObject *obj)
2997{
2998 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002999 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003000
3001 const char append_op = APPEND;
3002 const char appends_op = APPENDS;
3003 const char mark_op = MARK;
3004
3005 assert(obj != NULL);
3006 assert(self->proto > 0);
3007 assert(PyList_CheckExact(obj));
3008
3009 if (PyList_GET_SIZE(obj) == 1) {
3010 item = PyList_GET_ITEM(obj, 0);
3011 if (save(self, item, 0) < 0)
3012 return -1;
3013 if (_Pickler_Write(self, &append_op, 1) < 0)
3014 return -1;
3015 return 0;
3016 }
3017
3018 /* Write in batches of BATCHSIZE. */
3019 total = 0;
3020 do {
3021 this_batch = 0;
3022 if (_Pickler_Write(self, &mark_op, 1) < 0)
3023 return -1;
3024 while (total < PyList_GET_SIZE(obj)) {
3025 item = PyList_GET_ITEM(obj, total);
3026 if (save(self, item, 0) < 0)
3027 return -1;
3028 total++;
3029 if (++this_batch == BATCHSIZE)
3030 break;
3031 }
3032 if (_Pickler_Write(self, &appends_op, 1) < 0)
3033 return -1;
3034
3035 } while (total < PyList_GET_SIZE(obj));
3036
3037 return 0;
3038}
3039
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003040static int
3041save_list(PicklerObject *self, PyObject *obj)
3042{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003043 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003044 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003045 int status = 0;
3046
3047 if (self->fast && !fast_save_enter(self, obj))
3048 goto error;
3049
3050 /* Create an empty list. */
3051 if (self->bin) {
3052 header[0] = EMPTY_LIST;
3053 len = 1;
3054 }
3055 else {
3056 header[0] = MARK;
3057 header[1] = LIST;
3058 len = 2;
3059 }
3060
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003061 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003062 goto error;
3063
3064 /* Get list length, and bow out early if empty. */
3065 if ((len = PyList_Size(obj)) < 0)
3066 goto error;
3067
3068 if (memo_put(self, obj) < 0)
3069 goto error;
3070
3071 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003072 /* Materialize the list elements. */
3073 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003074 if (Py_EnterRecursiveCall(" while pickling an object"))
3075 goto error;
3076 status = batch_list_exact(self, obj);
3077 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003078 } else {
3079 PyObject *iter = PyObject_GetIter(obj);
3080 if (iter == NULL)
3081 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003082
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003083 if (Py_EnterRecursiveCall(" while pickling an object")) {
3084 Py_DECREF(iter);
3085 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003086 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003087 status = batch_list(self, iter);
3088 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003089 Py_DECREF(iter);
3090 }
3091 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003092 if (0) {
3093 error:
3094 status = -1;
3095 }
3096
3097 if (self->fast && !fast_save_leave(self, obj))
3098 status = -1;
3099
3100 return status;
3101}
3102
3103/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
3104 * MARK key value ... key value SETITEMS
3105 * opcode sequences. Calling code should have arranged to first create an
3106 * empty dict, or dict-like object, for the SETITEMS to operate on.
3107 * Returns 0 on success, <0 on error.
3108 *
3109 * This is very much like batch_list(). The difference between saving
3110 * elements directly, and picking apart two-tuples, is so long-winded at
3111 * the C level, though, that attempts to combine these routines were too
3112 * ugly to bear.
3113 */
3114static int
3115batch_dict(PicklerObject *self, PyObject *iter)
3116{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00003117 PyObject *obj = NULL;
3118 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003119 int i, n;
3120
3121 const char mark_op = MARK;
3122 const char setitem_op = SETITEM;
3123 const char setitems_op = SETITEMS;
3124
3125 assert(iter != NULL);
3126
3127 if (self->proto == 0) {
3128 /* SETITEMS isn't available; do one at a time. */
3129 for (;;) {
3130 obj = PyIter_Next(iter);
3131 if (obj == NULL) {
3132 if (PyErr_Occurred())
3133 return -1;
3134 break;
3135 }
3136 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
3137 PyErr_SetString(PyExc_TypeError, "dict items "
3138 "iterator must return 2-tuples");
3139 return -1;
3140 }
3141 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
3142 if (i >= 0)
3143 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
3144 Py_DECREF(obj);
3145 if (i < 0)
3146 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003147 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003148 return -1;
3149 }
3150 return 0;
3151 }
3152
3153 /* proto > 0: write in batches of BATCHSIZE. */
3154 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00003155 /* Get first item */
3156 firstitem = PyIter_Next(iter);
3157 if (firstitem == NULL) {
3158 if (PyErr_Occurred())
3159 goto error;
3160
3161 /* nothing more to add */
3162 break;
3163 }
3164 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
3165 PyErr_SetString(PyExc_TypeError, "dict items "
3166 "iterator must return 2-tuples");
3167 goto error;
3168 }
3169
3170 /* Try to get a second item */
3171 obj = PyIter_Next(iter);
3172 if (obj == NULL) {
3173 if (PyErr_Occurred())
3174 goto error;
3175
3176 /* Only one item to write */
3177 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
3178 goto error;
3179 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
3180 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003181 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00003182 goto error;
3183 Py_CLEAR(firstitem);
3184 break;
3185 }
3186
3187 /* More than one item to write */
3188
3189 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003190 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00003191 goto error;
3192
3193 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
3194 goto error;
3195 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
3196 goto error;
3197 Py_CLEAR(firstitem);
3198 n = 1;
3199
3200 /* Fetch and save up to BATCHSIZE items */
3201 while (obj) {
3202 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
3203 PyErr_SetString(PyExc_TypeError, "dict items "
3204 "iterator must return 2-tuples");
3205 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003206 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00003207 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
3208 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
3209 goto error;
3210 Py_CLEAR(obj);
3211 n += 1;
3212
3213 if (n == BATCHSIZE)
3214 break;
3215
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003216 obj = PyIter_Next(iter);
3217 if (obj == NULL) {
3218 if (PyErr_Occurred())
3219 goto error;
3220 break;
3221 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003222 }
3223
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003224 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00003225 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003226
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003227 } while (n == BATCHSIZE);
3228 return 0;
3229
3230 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00003231 Py_XDECREF(firstitem);
3232 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003233 return -1;
3234}
3235
Collin Winter5c9b02d2009-05-25 05:43:30 +00003236/* This is a variant of batch_dict() above that specializes for dicts, with no
3237 * support for dict subclasses. Like batch_dict(), we batch up chunks of
3238 * MARK key value ... key value SETITEMS
3239 * opcode sequences. Calling code should have arranged to first create an
3240 * empty dict, or dict-like object, for the SETITEMS to operate on.
3241 * Returns 0 on success, -1 on error.
3242 *
3243 * Note that this currently doesn't work for protocol 0.
3244 */
3245static int
3246batch_dict_exact(PicklerObject *self, PyObject *obj)
3247{
3248 PyObject *key = NULL, *value = NULL;
3249 int i;
3250 Py_ssize_t dict_size, ppos = 0;
3251
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00003252 const char mark_op = MARK;
3253 const char setitem_op = SETITEM;
3254 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00003255
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003256 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00003257 assert(self->proto > 0);
3258
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003259 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003260
3261 /* Special-case len(d) == 1 to save space. */
3262 if (dict_size == 1) {
3263 PyDict_Next(obj, &ppos, &key, &value);
3264 if (save(self, key, 0) < 0)
3265 return -1;
3266 if (save(self, value, 0) < 0)
3267 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003268 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003269 return -1;
3270 return 0;
3271 }
3272
3273 /* Write in batches of BATCHSIZE. */
3274 do {
3275 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003276 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003277 return -1;
3278 while (PyDict_Next(obj, &ppos, &key, &value)) {
3279 if (save(self, key, 0) < 0)
3280 return -1;
3281 if (save(self, value, 0) < 0)
3282 return -1;
3283 if (++i == BATCHSIZE)
3284 break;
3285 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003286 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003287 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003288 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003289 PyErr_Format(
3290 PyExc_RuntimeError,
3291 "dictionary changed size during iteration");
3292 return -1;
3293 }
3294
3295 } while (i == BATCHSIZE);
3296 return 0;
3297}
3298
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003299static int
3300save_dict(PicklerObject *self, PyObject *obj)
3301{
3302 PyObject *items, *iter;
3303 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003304 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003305 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003306 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003307
3308 if (self->fast && !fast_save_enter(self, obj))
3309 goto error;
3310
3311 /* Create an empty dict. */
3312 if (self->bin) {
3313 header[0] = EMPTY_DICT;
3314 len = 1;
3315 }
3316 else {
3317 header[0] = MARK;
3318 header[1] = DICT;
3319 len = 2;
3320 }
3321
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003322 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003323 goto error;
3324
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003325 if (memo_put(self, obj) < 0)
3326 goto error;
3327
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003328 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003329 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003330 if (PyDict_CheckExact(obj) && self->proto > 0) {
3331 /* We can take certain shortcuts if we know this is a dict and
3332 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003333 if (Py_EnterRecursiveCall(" while pickling an object"))
3334 goto error;
3335 status = batch_dict_exact(self, obj);
3336 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003337 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003338 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003339
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003340 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003341 if (items == NULL)
3342 goto error;
3343 iter = PyObject_GetIter(items);
3344 Py_DECREF(items);
3345 if (iter == NULL)
3346 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003347 if (Py_EnterRecursiveCall(" while pickling an object")) {
3348 Py_DECREF(iter);
3349 goto error;
3350 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003351 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003352 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003353 Py_DECREF(iter);
3354 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003355 }
3356
3357 if (0) {
3358 error:
3359 status = -1;
3360 }
3361
3362 if (self->fast && !fast_save_leave(self, obj))
3363 status = -1;
3364
3365 return status;
3366}
3367
3368static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003369save_set(PicklerObject *self, PyObject *obj)
3370{
3371 PyObject *item;
3372 int i;
3373 Py_ssize_t set_size, ppos = 0;
3374 Py_hash_t hash;
3375
3376 const char empty_set_op = EMPTY_SET;
3377 const char mark_op = MARK;
3378 const char additems_op = ADDITEMS;
3379
3380 if (self->proto < 4) {
3381 PyObject *items;
3382 PyObject *reduce_value;
3383 int status;
3384
3385 items = PySequence_List(obj);
3386 if (items == NULL) {
3387 return -1;
3388 }
3389 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3390 Py_DECREF(items);
3391 if (reduce_value == NULL) {
3392 return -1;
3393 }
3394 /* save_reduce() will memoize the object automatically. */
3395 status = save_reduce(self, reduce_value, obj);
3396 Py_DECREF(reduce_value);
3397 return status;
3398 }
3399
3400 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3401 return -1;
3402
3403 if (memo_put(self, obj) < 0)
3404 return -1;
3405
3406 set_size = PySet_GET_SIZE(obj);
3407 if (set_size == 0)
3408 return 0; /* nothing to do */
3409
3410 /* Write in batches of BATCHSIZE. */
3411 do {
3412 i = 0;
3413 if (_Pickler_Write(self, &mark_op, 1) < 0)
3414 return -1;
3415 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3416 if (save(self, item, 0) < 0)
3417 return -1;
3418 if (++i == BATCHSIZE)
3419 break;
3420 }
3421 if (_Pickler_Write(self, &additems_op, 1) < 0)
3422 return -1;
3423 if (PySet_GET_SIZE(obj) != set_size) {
3424 PyErr_Format(
3425 PyExc_RuntimeError,
3426 "set changed size during iteration");
3427 return -1;
3428 }
3429 } while (i == BATCHSIZE);
3430
3431 return 0;
3432}
3433
3434static int
3435save_frozenset(PicklerObject *self, PyObject *obj)
3436{
3437 PyObject *iter;
3438
3439 const char mark_op = MARK;
3440 const char frozenset_op = FROZENSET;
3441
3442 if (self->fast && !fast_save_enter(self, obj))
3443 return -1;
3444
3445 if (self->proto < 4) {
3446 PyObject *items;
3447 PyObject *reduce_value;
3448 int status;
3449
3450 items = PySequence_List(obj);
3451 if (items == NULL) {
3452 return -1;
3453 }
3454 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3455 items);
3456 Py_DECREF(items);
3457 if (reduce_value == NULL) {
3458 return -1;
3459 }
3460 /* save_reduce() will memoize the object automatically. */
3461 status = save_reduce(self, reduce_value, obj);
3462 Py_DECREF(reduce_value);
3463 return status;
3464 }
3465
3466 if (_Pickler_Write(self, &mark_op, 1) < 0)
3467 return -1;
3468
3469 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003470 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003471 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003472 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003473 for (;;) {
3474 PyObject *item;
3475
3476 item = PyIter_Next(iter);
3477 if (item == NULL) {
3478 if (PyErr_Occurred()) {
3479 Py_DECREF(iter);
3480 return -1;
3481 }
3482 break;
3483 }
3484 if (save(self, item, 0) < 0) {
3485 Py_DECREF(item);
3486 Py_DECREF(iter);
3487 return -1;
3488 }
3489 Py_DECREF(item);
3490 }
3491 Py_DECREF(iter);
3492
3493 /* If the object is already in the memo, this means it is
3494 recursive. In this case, throw away everything we put on the
3495 stack, and fetch the object back from the memo. */
3496 if (PyMemoTable_Get(self->memo, obj)) {
3497 const char pop_mark_op = POP_MARK;
3498
3499 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3500 return -1;
3501 if (memo_get(self, obj) < 0)
3502 return -1;
3503 return 0;
3504 }
3505
3506 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3507 return -1;
3508 if (memo_put(self, obj) < 0)
3509 return -1;
3510
3511 return 0;
3512}
3513
3514static int
3515fix_imports(PyObject **module_name, PyObject **global_name)
3516{
3517 PyObject *key;
3518 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003519 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003520
3521 key = PyTuple_Pack(2, *module_name, *global_name);
3522 if (key == NULL)
3523 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003524 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003525 Py_DECREF(key);
3526 if (item) {
3527 PyObject *fixed_module_name;
3528 PyObject *fixed_global_name;
3529
3530 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3531 PyErr_Format(PyExc_RuntimeError,
3532 "_compat_pickle.REVERSE_NAME_MAPPING values "
3533 "should be 2-tuples, not %.200s",
3534 Py_TYPE(item)->tp_name);
3535 return -1;
3536 }
3537 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3538 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3539 if (!PyUnicode_Check(fixed_module_name) ||
3540 !PyUnicode_Check(fixed_global_name)) {
3541 PyErr_Format(PyExc_RuntimeError,
3542 "_compat_pickle.REVERSE_NAME_MAPPING values "
3543 "should be pairs of str, not (%.200s, %.200s)",
3544 Py_TYPE(fixed_module_name)->tp_name,
3545 Py_TYPE(fixed_global_name)->tp_name);
3546 return -1;
3547 }
3548
3549 Py_CLEAR(*module_name);
3550 Py_CLEAR(*global_name);
3551 Py_INCREF(fixed_module_name);
3552 Py_INCREF(fixed_global_name);
3553 *module_name = fixed_module_name;
3554 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003555 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003556 }
3557 else if (PyErr_Occurred()) {
3558 return -1;
3559 }
3560
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003561 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003562 if (item) {
3563 if (!PyUnicode_Check(item)) {
3564 PyErr_Format(PyExc_RuntimeError,
3565 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3566 "should be strings, not %.200s",
3567 Py_TYPE(item)->tp_name);
3568 return -1;
3569 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003570 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003571 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003572 }
3573 else if (PyErr_Occurred()) {
3574 return -1;
3575 }
3576
3577 return 0;
3578}
3579
3580static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003581save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3582{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003583 PyObject *global_name = NULL;
3584 PyObject *module_name = NULL;
3585 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003586 PyObject *parent = NULL;
3587 PyObject *dotted_path = NULL;
3588 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003589 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003590 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003591 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003592 _Py_IDENTIFIER(__name__);
3593 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003594
3595 const char global_op = GLOBAL;
3596
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003597 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003598 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003599 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003600 }
3601 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003602 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3603 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003604 if (global_name == NULL) {
3605 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3606 if (global_name == NULL)
3607 goto error;
3608 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003609 }
3610
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003611 dotted_path = get_dotted_path(module, global_name);
3612 if (dotted_path == NULL)
3613 goto error;
3614 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003615 if (module_name == NULL)
3616 goto error;
3617
3618 /* XXX: Change to use the import C API directly with level=0 to disallow
3619 relative imports.
3620
3621 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3622 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3623 custom import functions (IMHO, this would be a nice security
3624 feature). The import C API would need to be extended to support the
3625 extra parameters of __import__ to fix that. */
3626 module = PyImport_Import(module_name);
3627 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003628 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003629 "Can't pickle %R: import of module %R failed",
3630 obj, module_name);
3631 goto error;
3632 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003633 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3634 Py_INCREF(lastname);
3635 cls = get_deep_attribute(module, dotted_path, &parent);
3636 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003637 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003638 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003639 "Can't pickle %R: attribute lookup %S on %S failed",
3640 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003641 goto error;
3642 }
3643 if (cls != obj) {
3644 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003645 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003646 "Can't pickle %R: it's not the same object as %S.%S",
3647 obj, module_name, global_name);
3648 goto error;
3649 }
3650 Py_DECREF(cls);
3651
3652 if (self->proto >= 2) {
3653 /* See whether this is in the extension registry, and if
3654 * so generate an EXT opcode.
3655 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003656 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003657 PyObject *code_obj; /* extension code as Python object */
3658 long code; /* extension code as C value */
3659 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003660 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003661
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003662 extension_key = PyTuple_Pack(2, module_name, global_name);
3663 if (extension_key == NULL) {
3664 goto error;
3665 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003666 code_obj = PyDict_GetItemWithError(st->extension_registry,
3667 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003668 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003669 /* The object is not registered in the extension registry.
3670 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003671 if (code_obj == NULL) {
3672 if (PyErr_Occurred()) {
3673 goto error;
3674 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003675 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003676 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003677
3678 /* XXX: pickle.py doesn't check neither the type, nor the range
3679 of the value returned by the extension_registry. It should for
3680 consistency. */
3681
3682 /* Verify code_obj has the right type and value. */
3683 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003684 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003685 "Can't pickle %R: extension code %R isn't an integer",
3686 obj, code_obj);
3687 goto error;
3688 }
3689 code = PyLong_AS_LONG(code_obj);
3690 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003691 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003692 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3693 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003694 goto error;
3695 }
3696
3697 /* Generate an EXT opcode. */
3698 if (code <= 0xff) {
3699 pdata[0] = EXT1;
3700 pdata[1] = (unsigned char)code;
3701 n = 2;
3702 }
3703 else if (code <= 0xffff) {
3704 pdata[0] = EXT2;
3705 pdata[1] = (unsigned char)(code & 0xff);
3706 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3707 n = 3;
3708 }
3709 else {
3710 pdata[0] = EXT4;
3711 pdata[1] = (unsigned char)(code & 0xff);
3712 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3713 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3714 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3715 n = 5;
3716 }
3717
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003718 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003719 goto error;
3720 }
3721 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003722 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003723 if (parent == module) {
3724 Py_INCREF(lastname);
3725 Py_DECREF(global_name);
3726 global_name = lastname;
3727 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003728 if (self->proto >= 4) {
3729 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003730
Christian Heimese8b1ba12013-11-23 21:13:39 +01003731 if (save(self, module_name, 0) < 0)
3732 goto error;
3733 if (save(self, global_name, 0) < 0)
3734 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003735
3736 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3737 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003738 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003739 else if (parent != module) {
3740 PickleState *st = _Pickle_GetGlobalState();
3741 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3742 st->getattr, parent, lastname);
Alexey Izbyshevf8c06b02018-08-22 07:51:25 +03003743 if (reduce_value == NULL)
3744 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003745 status = save_reduce(self, reduce_value, NULL);
3746 Py_DECREF(reduce_value);
3747 if (status < 0)
3748 goto error;
3749 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003750 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003751 /* Generate a normal global opcode if we are using a pickle
3752 protocol < 4, or if the object is not registered in the
3753 extension registry. */
3754 PyObject *encoded;
3755 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003756
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003757 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003758 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003759
3760 /* For protocol < 3 and if the user didn't request against doing
3761 so, we convert module names to the old 2.x module names. */
3762 if (self->proto < 3 && self->fix_imports) {
3763 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003764 goto error;
3765 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003766 }
3767
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003768 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3769 both the module name and the global name using UTF-8. We do so
3770 only when we are using the pickle protocol newer than version
3771 3. This is to ensure compatibility with older Unpickler running
3772 on Python 2.x. */
3773 if (self->proto == 3) {
3774 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003775 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003776 else {
3777 unicode_encoder = PyUnicode_AsASCIIString;
3778 }
3779 encoded = unicode_encoder(module_name);
3780 if (encoded == NULL) {
3781 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003782 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003783 "can't pickle module identifier '%S' using "
3784 "pickle protocol %i",
3785 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003786 goto error;
3787 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003788 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3789 PyBytes_GET_SIZE(encoded)) < 0) {
3790 Py_DECREF(encoded);
3791 goto error;
3792 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003793 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003794 if(_Pickler_Write(self, "\n", 1) < 0)
3795 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003796
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003797 /* Save the name of the module. */
3798 encoded = unicode_encoder(global_name);
3799 if (encoded == NULL) {
3800 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003801 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003802 "can't pickle global identifier '%S' using "
3803 "pickle protocol %i",
3804 global_name, self->proto);
3805 goto error;
3806 }
3807 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3808 PyBytes_GET_SIZE(encoded)) < 0) {
3809 Py_DECREF(encoded);
3810 goto error;
3811 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003812 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003813 if (_Pickler_Write(self, "\n", 1) < 0)
3814 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003815 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003816 /* Memoize the object. */
3817 if (memo_put(self, obj) < 0)
3818 goto error;
3819 }
3820
3821 if (0) {
3822 error:
3823 status = -1;
3824 }
3825 Py_XDECREF(module_name);
3826 Py_XDECREF(global_name);
3827 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003828 Py_XDECREF(parent);
3829 Py_XDECREF(dotted_path);
3830 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003831
3832 return status;
3833}
3834
3835static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003836save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3837{
3838 PyObject *reduce_value;
3839 int status;
3840
3841 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3842 if (reduce_value == NULL) {
3843 return -1;
3844 }
3845 status = save_reduce(self, reduce_value, obj);
3846 Py_DECREF(reduce_value);
3847 return status;
3848}
3849
3850static int
3851save_type(PicklerObject *self, PyObject *obj)
3852{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003853 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003854 return save_singleton_type(self, obj, Py_None);
3855 }
3856 else if (obj == (PyObject *)&PyEllipsis_Type) {
3857 return save_singleton_type(self, obj, Py_Ellipsis);
3858 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003859 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003860 return save_singleton_type(self, obj, Py_NotImplemented);
3861 }
3862 return save_global(self, obj, NULL);
3863}
3864
3865static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003866save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003867{
3868 PyObject *pid = NULL;
3869 int status = 0;
3870
3871 const char persid_op = PERSID;
3872 const char binpersid_op = BINPERSID;
3873
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003874 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003875 if (pid == NULL)
3876 return -1;
3877
3878 if (pid != Py_None) {
3879 if (self->bin) {
3880 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003881 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003882 goto error;
3883 }
3884 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003885 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003886
3887 pid_str = PyObject_Str(pid);
3888 if (pid_str == NULL)
3889 goto error;
3890
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003891 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003892 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003893 if (!PyUnicode_IS_ASCII(pid_str)) {
3894 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3895 "persistent IDs in protocol 0 must be "
3896 "ASCII strings");
3897 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003898 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003899 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003900
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003901 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003902 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3903 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3904 _Pickler_Write(self, "\n", 1) < 0) {
3905 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003906 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003907 }
3908 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003909 }
3910 status = 1;
3911 }
3912
3913 if (0) {
3914 error:
3915 status = -1;
3916 }
3917 Py_XDECREF(pid);
3918
3919 return status;
3920}
3921
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003922static PyObject *
3923get_class(PyObject *obj)
3924{
3925 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003926 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003927
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003928 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3929 cls = (PyObject *) Py_TYPE(obj);
3930 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003931 }
3932 return cls;
3933}
3934
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003935/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3936 * appropriate __reduce__ method for obj.
3937 */
3938static int
3939save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3940{
3941 PyObject *callable;
3942 PyObject *argtup;
3943 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003944 PyObject *listitems = Py_None;
3945 PyObject *dictitems = Py_None;
Pierre Glaser65d98d02019-05-08 21:40:25 +02003946 PyObject *state_setter = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003947 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003948 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003949 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003950
3951 const char reduce_op = REDUCE;
3952 const char build_op = BUILD;
3953 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003954 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003955
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003956 size = PyTuple_Size(args);
Pierre Glaser65d98d02019-05-08 21:40:25 +02003957 if (size < 2 || size > 6) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003958 PyErr_SetString(st->PicklingError, "tuple returned by "
Pierre Glaser65d98d02019-05-08 21:40:25 +02003959 "__reduce__ must contain 2 through 6 elements");
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003960 return -1;
3961 }
3962
Pierre Glaser65d98d02019-05-08 21:40:25 +02003963 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 6,
3964 &callable, &argtup, &state, &listitems, &dictitems,
3965 &state_setter))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003966 return -1;
3967
3968 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003969 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003970 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003971 return -1;
3972 }
3973 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003974 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003975 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003976 return -1;
3977 }
3978
3979 if (state == Py_None)
3980 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003981
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003982 if (listitems == Py_None)
3983 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003984 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003985 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003986 "returned by __reduce__ must be an iterator, not %s",
3987 Py_TYPE(listitems)->tp_name);
3988 return -1;
3989 }
3990
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003991 if (dictitems == Py_None)
3992 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003993 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003994 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003995 "returned by __reduce__ must be an iterator, not %s",
3996 Py_TYPE(dictitems)->tp_name);
3997 return -1;
3998 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003999
Pierre Glaser65d98d02019-05-08 21:40:25 +02004000 if (state_setter == Py_None)
4001 state_setter = NULL;
4002 else if (!PyCallable_Check(state_setter)) {
4003 PyErr_Format(st->PicklingError, "sixth element of the tuple "
4004 "returned by __reduce__ must be a function, not %s",
4005 Py_TYPE(state_setter)->tp_name);
4006 return -1;
4007 }
4008
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004009 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01004010 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004011 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004012
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004013 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
4014 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004015 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004016 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03004017 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02004018 use_newobj_ex = _PyUnicode_EqualToASCIIId(
4019 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02004020 if (!use_newobj_ex) {
4021 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02004022 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02004023 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004024 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02004025 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004026 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004027
4028 if (use_newobj_ex) {
4029 PyObject *cls;
4030 PyObject *args;
4031 PyObject *kwargs;
4032
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004033 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004034 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004035 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004036 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004037 return -1;
4038 }
4039
4040 cls = PyTuple_GET_ITEM(argtup, 0);
4041 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08004042 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004043 "first item from NEWOBJ_EX argument tuple must "
4044 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
4045 return -1;
4046 }
4047 args = PyTuple_GET_ITEM(argtup, 1);
4048 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08004049 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004050 "second item from NEWOBJ_EX argument tuple must "
4051 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
4052 return -1;
4053 }
4054 kwargs = PyTuple_GET_ITEM(argtup, 2);
4055 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08004056 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004057 "third item from NEWOBJ_EX argument tuple must "
4058 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
4059 return -1;
4060 }
4061
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03004062 if (self->proto >= 4) {
4063 if (save(self, cls, 0) < 0 ||
4064 save(self, args, 0) < 0 ||
4065 save(self, kwargs, 0) < 0 ||
4066 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
4067 return -1;
4068 }
4069 }
4070 else {
4071 PyObject *newargs;
4072 PyObject *cls_new;
4073 Py_ssize_t i;
4074 _Py_IDENTIFIER(__new__);
4075
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004076 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03004077 if (newargs == NULL)
4078 return -1;
4079
4080 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
4081 if (cls_new == NULL) {
4082 Py_DECREF(newargs);
4083 return -1;
4084 }
4085 PyTuple_SET_ITEM(newargs, 0, cls_new);
4086 Py_INCREF(cls);
4087 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004088 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03004089 PyObject *item = PyTuple_GET_ITEM(args, i);
4090 Py_INCREF(item);
4091 PyTuple_SET_ITEM(newargs, i + 2, item);
4092 }
4093
4094 callable = PyObject_Call(st->partial, newargs, kwargs);
4095 Py_DECREF(newargs);
4096 if (callable == NULL)
4097 return -1;
4098
4099 newargs = PyTuple_New(0);
4100 if (newargs == NULL) {
4101 Py_DECREF(callable);
4102 return -1;
4103 }
4104
4105 if (save(self, callable, 0) < 0 ||
4106 save(self, newargs, 0) < 0 ||
4107 _Pickler_Write(self, &reduce_op, 1) < 0) {
4108 Py_DECREF(newargs);
4109 Py_DECREF(callable);
4110 return -1;
4111 }
4112 Py_DECREF(newargs);
4113 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004114 }
4115 }
4116 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004117 PyObject *cls;
4118 PyObject *newargtup;
4119 PyObject *obj_class;
4120 int p;
4121
4122 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004123 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004124 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004125 return -1;
4126 }
4127
4128 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01004129 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004130 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01004131 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004132 return -1;
4133 }
4134
4135 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01004136 obj_class = get_class(obj);
Zackery Spytz25d38972018-12-05 11:29:20 -07004137 if (obj_class == NULL) {
4138 return -1;
4139 }
4140 p = obj_class != cls;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004141 Py_DECREF(obj_class);
4142 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004143 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004144 "__newobj__ args has the wrong class");
4145 return -1;
4146 }
4147 }
4148 /* XXX: These calls save() are prone to infinite recursion. Imagine
4149 what happen if the value returned by the __reduce__() method of
4150 some extension type contains another object of the same type. Ouch!
4151
4152 Here is a quick example, that I ran into, to illustrate what I
4153 mean:
4154
4155 >>> import pickle, copyreg
4156 >>> copyreg.dispatch_table.pop(complex)
4157 >>> pickle.dumps(1+2j)
4158 Traceback (most recent call last):
4159 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04004160 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004161
4162 Removing the complex class from copyreg.dispatch_table made the
4163 __reduce_ex__() method emit another complex object:
4164
4165 >>> (1+1j).__reduce_ex__(2)
4166 (<function __newobj__ at 0xb7b71c3c>,
4167 (<class 'complex'>, (1+1j)), None, None, None)
4168
4169 Thus when save() was called on newargstup (the 2nd item) recursion
4170 ensued. Of course, the bug was in the complex class which had a
4171 broken __getnewargs__() that emitted another complex object. But,
4172 the point, here, is it is quite easy to end up with a broken reduce
4173 function. */
4174
4175 /* Save the class and its __new__ arguments. */
4176 if (save(self, cls, 0) < 0)
4177 return -1;
4178
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004179 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004180 if (newargtup == NULL)
4181 return -1;
4182
4183 p = save(self, newargtup, 0);
4184 Py_DECREF(newargtup);
4185 if (p < 0)
4186 return -1;
4187
4188 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004189 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004190 return -1;
4191 }
4192 else { /* Not using NEWOBJ. */
4193 if (save(self, callable, 0) < 0 ||
4194 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004195 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004196 return -1;
4197 }
4198
4199 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
4200 the caller do not want to memoize the object. Not particularly useful,
4201 but that is to mimic the behavior save_reduce() in pickle.py when
4202 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004203 if (obj != NULL) {
4204 /* If the object is already in the memo, this means it is
4205 recursive. In this case, throw away everything we put on the
4206 stack, and fetch the object back from the memo. */
4207 if (PyMemoTable_Get(self->memo, obj)) {
4208 const char pop_op = POP;
4209
4210 if (_Pickler_Write(self, &pop_op, 1) < 0)
4211 return -1;
4212 if (memo_get(self, obj) < 0)
4213 return -1;
4214
4215 return 0;
4216 }
4217 else if (memo_put(self, obj) < 0)
4218 return -1;
4219 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004220
4221 if (listitems && batch_list(self, listitems) < 0)
4222 return -1;
4223
4224 if (dictitems && batch_dict(self, dictitems) < 0)
4225 return -1;
4226
4227 if (state) {
Pierre Glaser65d98d02019-05-08 21:40:25 +02004228 if (state_setter == NULL) {
4229 if (save(self, state, 0) < 0 ||
4230 _Pickler_Write(self, &build_op, 1) < 0)
4231 return -1;
4232 }
4233 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004234
Pierre Glaser65d98d02019-05-08 21:40:25 +02004235 /* If a state_setter is specified, call it instead of load_build to
4236 * update obj's with its previous state.
4237 * The first 4 save/write instructions push state_setter and its
4238 * tuple of expected arguments (obj, state) onto the stack. The
4239 * REDUCE opcode triggers the state_setter(obj, state) function
4240 * call. Finally, because state-updating routines only do in-place
4241 * modification, the whole operation has to be stack-transparent.
4242 * Thus, we finally pop the call's output from the stack.*/
4243
4244 const char tupletwo_op = TUPLE2;
4245 const char pop_op = POP;
4246 if (save(self, state_setter, 0) < 0 ||
4247 save(self, obj, 0) < 0 || save(self, state, 0) < 0 ||
4248 _Pickler_Write(self, &tupletwo_op, 1) < 0 ||
4249 _Pickler_Write(self, &reduce_op, 1) < 0 ||
4250 _Pickler_Write(self, &pop_op, 1) < 0)
4251 return -1;
4252 }
4253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004254 return 0;
4255}
4256
4257static int
4258save(PicklerObject *self, PyObject *obj, int pers_save)
4259{
4260 PyTypeObject *type;
4261 PyObject *reduce_func = NULL;
4262 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004263 int status = 0;
4264
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004265 if (_Pickler_OpcodeBoundary(self) < 0)
4266 return -1;
4267
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004268 /* The extra pers_save argument is necessary to avoid calling save_pers()
4269 on its returned object. */
4270 if (!pers_save && self->pers_func) {
4271 /* save_pers() returns:
4272 -1 to signal an error;
4273 0 if it did nothing successfully;
4274 1 if a persistent id was saved.
4275 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004276 if ((status = save_pers(self, obj)) != 0)
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004277 return status;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004278 }
4279
4280 type = Py_TYPE(obj);
4281
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004282 /* The old cPickle had an optimization that used switch-case statement
4283 dispatching on the first letter of the type name. This has was removed
4284 since benchmarks shown that this optimization was actually slowing
4285 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004286
4287 /* Atom types; these aren't memoized, so don't check the memo. */
4288
4289 if (obj == Py_None) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004290 return save_none(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004291 }
4292 else if (obj == Py_False || obj == Py_True) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004293 return save_bool(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004294 }
4295 else if (type == &PyLong_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004296 return save_long(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004297 }
4298 else if (type == &PyFloat_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004299 return save_float(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004300 }
4301
4302 /* Check the memo to see if it has the object. If so, generate
4303 a GET (or BINGET) opcode, instead of pickling the object
4304 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004305 if (PyMemoTable_Get(self->memo, obj)) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004306 return memo_get(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004307 }
4308
4309 if (type == &PyBytes_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004310 return save_bytes(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004311 }
4312 else if (type == &PyUnicode_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004313 return save_unicode(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004314 }
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03004315
4316 /* We're only calling Py_EnterRecursiveCall here so that atomic
4317 types above are pickled faster. */
4318 if (Py_EnterRecursiveCall(" while pickling an object")) {
4319 return -1;
4320 }
4321
4322 if (type == &PyDict_Type) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004323 status = save_dict(self, obj);
4324 goto done;
4325 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004326 else if (type == &PySet_Type) {
4327 status = save_set(self, obj);
4328 goto done;
4329 }
4330 else if (type == &PyFrozenSet_Type) {
4331 status = save_frozenset(self, obj);
4332 goto done;
4333 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004334 else if (type == &PyList_Type) {
4335 status = save_list(self, obj);
4336 goto done;
4337 }
4338 else if (type == &PyTuple_Type) {
4339 status = save_tuple(self, obj);
4340 goto done;
4341 }
Antoine Pitrou91f43802019-05-26 17:10:09 +02004342 else if (type == &PyByteArray_Type) {
4343 status = save_bytearray(self, obj);
4344 goto done;
4345 }
4346 else if (type == &PyPickleBuffer_Type) {
4347 status = save_picklebuffer(self, obj);
4348 goto done;
4349 }
Pierre Glaser289f1f82019-05-08 23:08:25 +02004350
4351 /* Now, check reducer_override. If it returns NotImplemented,
4352 * fallback to save_type or save_global, and then perhaps to the
4353 * regular reduction mechanism.
4354 */
4355 if (self->reducer_override != NULL) {
4356 reduce_value = PyObject_CallFunctionObjArgs(self->reducer_override,
4357 obj, NULL);
4358 if (reduce_value == NULL) {
4359 goto error;
4360 }
4361 if (reduce_value != Py_NotImplemented) {
4362 goto reduce;
4363 }
4364 Py_DECREF(reduce_value);
4365 reduce_value = NULL;
4366 }
4367
4368 if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004369 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004370 goto done;
4371 }
4372 else if (type == &PyFunction_Type) {
4373 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004374 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004375 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004376
4377 /* XXX: This part needs some unit tests. */
4378
4379 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004380 * self.dispatch_table, copyreg.dispatch_table, the object's
4381 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004382 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004383 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004384 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004385 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4386 (PyObject *)type);
4387 if (reduce_func == NULL) {
4388 if (PyErr_Occurred()) {
4389 goto error;
4390 }
4391 } else {
4392 /* PyDict_GetItemWithError() returns a borrowed reference.
4393 Increase the reference count to be consistent with
4394 PyObject_GetItem and _PyObject_GetAttrId used below. */
4395 Py_INCREF(reduce_func);
4396 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004397 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004398 reduce_func = PyObject_GetItem(self->dispatch_table,
4399 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004400 if (reduce_func == NULL) {
4401 if (PyErr_ExceptionMatches(PyExc_KeyError))
4402 PyErr_Clear();
4403 else
4404 goto error;
4405 }
4406 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004407 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004408 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004409 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004410 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004411 else if (PyType_IsSubtype(type, &PyType_Type)) {
4412 status = save_global(self, obj, NULL);
4413 goto done;
4414 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004415 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004416 _Py_IDENTIFIER(__reduce__);
4417 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004418
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004419 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4420 automatically defined as __reduce__. While this is convenient, this
4421 make it impossible to know which method was actually called. Of
4422 course, this is not a big deal. But still, it would be nice to let
4423 the user know which method was called when something go
4424 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4425 don't actually have to check for a __reduce__ method. */
4426
4427 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004428 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4429 goto error;
4430 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004431 if (reduce_func != NULL) {
4432 PyObject *proto;
4433 proto = PyLong_FromLong(self->proto);
4434 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004435 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004436 }
4437 }
4438 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004439 /* Check for a __reduce__ method. */
Serhiy Storchaka353053d2019-09-01 14:01:05 +03004440 if (_PyObject_LookupAttrId(obj, &PyId___reduce__, &reduce_func) < 0) {
4441 goto error;
4442 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004443 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004444 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004445 }
4446 else {
Serhiy Storchaka353053d2019-09-01 14:01:05 +03004447 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004448 PyErr_Format(st->PicklingError,
4449 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004450 type->tp_name, obj);
4451 goto error;
4452 }
4453 }
4454 }
4455
4456 if (reduce_value == NULL)
4457 goto error;
4458
Pierre Glaser289f1f82019-05-08 23:08:25 +02004459 reduce:
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004460 if (PyUnicode_Check(reduce_value)) {
4461 status = save_global(self, obj, reduce_value);
4462 goto done;
4463 }
4464
4465 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004466 PickleState *st = _Pickle_GetGlobalState();
4467 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004468 "__reduce__ must return a string or tuple");
4469 goto error;
4470 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004471
4472 status = save_reduce(self, reduce_value, obj);
4473
4474 if (0) {
4475 error:
4476 status = -1;
4477 }
4478 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004479
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004480 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004481 Py_XDECREF(reduce_func);
4482 Py_XDECREF(reduce_value);
4483
4484 return status;
4485}
4486
4487static int
4488dump(PicklerObject *self, PyObject *obj)
4489{
4490 const char stop_op = STOP;
Antoine Pitrou17236872020-02-02 21:22:57 +01004491 int status = -1;
Pierre Glaser289f1f82019-05-08 23:08:25 +02004492 PyObject *tmp;
4493 _Py_IDENTIFIER(reducer_override);
4494
4495 if (_PyObject_LookupAttrId((PyObject *)self, &PyId_reducer_override,
4496 &tmp) < 0) {
Antoine Pitrou17236872020-02-02 21:22:57 +01004497 goto error;
Pierre Glaser289f1f82019-05-08 23:08:25 +02004498 }
4499 /* Cache the reducer_override method, if it exists. */
4500 if (tmp != NULL) {
4501 Py_XSETREF(self->reducer_override, tmp);
4502 }
4503 else {
4504 Py_CLEAR(self->reducer_override);
4505 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004506
4507 if (self->proto >= 2) {
4508 char header[2];
4509
4510 header[0] = PROTO;
4511 assert(self->proto >= 0 && self->proto < 256);
4512 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004513 if (_Pickler_Write(self, header, 2) < 0)
Antoine Pitrou17236872020-02-02 21:22:57 +01004514 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004515 if (self->proto >= 4)
4516 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004517 }
4518
4519 if (save(self, obj, 0) < 0 ||
Serhiy Storchakac8695292018-04-04 00:11:27 +03004520 _Pickler_Write(self, &stop_op, 1) < 0 ||
4521 _Pickler_CommitFrame(self) < 0)
Antoine Pitrou17236872020-02-02 21:22:57 +01004522 goto error;
4523
4524 // Success
4525 status = 0;
4526
4527 error:
Serhiy Storchakac8695292018-04-04 00:11:27 +03004528 self->framing = 0;
Antoine Pitrou17236872020-02-02 21:22:57 +01004529
4530 /* Break the reference cycle we generated at the beginning this function
4531 * call when setting the reducer_override attribute of the Pickler instance
4532 * to a bound method of the same instance. This is important as the Pickler
4533 * instance holds a reference to each object it has pickled (through its
4534 * memo): thus, these objects wont be garbage-collected as long as the
4535 * Pickler itself is not collected. */
4536 Py_CLEAR(self->reducer_override);
4537 return status;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004538}
4539
Larry Hastings61272b72014-01-07 12:41:53 -08004540/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004541
4542_pickle.Pickler.clear_memo
4543
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004544Clears the pickler's "memo".
4545
4546The memo is the data structure that remembers which objects the
4547pickler has already seen, so that shared or recursive objects are
4548pickled by reference and not by value. This method is useful when
4549re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004550[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004551
Larry Hastings3cceb382014-01-04 11:09:09 -08004552static PyObject *
4553_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004554/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004555{
4556 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004557 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004558
4559 Py_RETURN_NONE;
4560}
4561
Larry Hastings61272b72014-01-07 12:41:53 -08004562/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004563
4564_pickle.Pickler.dump
4565
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004566 obj: object
4567 /
4568
4569Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004570[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004571
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004572static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004573_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004574/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004575{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004576 /* Check whether the Pickler was initialized correctly (issue3664).
4577 Developers often forget to call __init__() in their subclasses, which
4578 would trigger a segfault without this check. */
4579 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004580 PickleState *st = _Pickle_GetGlobalState();
4581 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004582 "Pickler.__init__() was not called by %s.__init__()",
4583 Py_TYPE(self)->tp_name);
4584 return NULL;
4585 }
4586
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004587 if (_Pickler_ClearBuffer(self) < 0)
4588 return NULL;
4589
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004590 if (dump(self, obj) < 0)
4591 return NULL;
4592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004593 if (_Pickler_FlushToFile(self) < 0)
4594 return NULL;
4595
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004596 Py_RETURN_NONE;
4597}
4598
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004599/*[clinic input]
4600
4601_pickle.Pickler.__sizeof__ -> Py_ssize_t
4602
4603Returns size in memory, in bytes.
4604[clinic start generated code]*/
4605
4606static Py_ssize_t
4607_pickle_Pickler___sizeof___impl(PicklerObject *self)
4608/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4609{
4610 Py_ssize_t res, s;
4611
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004612 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004613 if (self->memo != NULL) {
4614 res += sizeof(PyMemoTable);
4615 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4616 }
4617 if (self->output_buffer != NULL) {
4618 s = _PySys_GetSizeOf(self->output_buffer);
4619 if (s == -1)
4620 return -1;
4621 res += s;
4622 }
4623 return res;
4624}
4625
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004626static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004627 _PICKLE_PICKLER_DUMP_METHODDEF
4628 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004629 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004630 {NULL, NULL} /* sentinel */
4631};
4632
4633static void
4634Pickler_dealloc(PicklerObject *self)
4635{
4636 PyObject_GC_UnTrack(self);
4637
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004638 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004639 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004640 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004641 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004642 Py_XDECREF(self->fast_memo);
Pierre Glaser289f1f82019-05-08 23:08:25 +02004643 Py_XDECREF(self->reducer_override);
Antoine Pitrou91f43802019-05-26 17:10:09 +02004644 Py_XDECREF(self->buffer_callback);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004645
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004646 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004647
4648 Py_TYPE(self)->tp_free((PyObject *)self);
4649}
4650
4651static int
4652Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4653{
4654 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004655 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004656 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004657 Py_VISIT(self->fast_memo);
Pierre Glaser289f1f82019-05-08 23:08:25 +02004658 Py_VISIT(self->reducer_override);
Antoine Pitrou91f43802019-05-26 17:10:09 +02004659 Py_VISIT(self->buffer_callback);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004660 return 0;
4661}
4662
4663static int
4664Pickler_clear(PicklerObject *self)
4665{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004666 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004667 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004668 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004669 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004670 Py_CLEAR(self->fast_memo);
Pierre Glaser289f1f82019-05-08 23:08:25 +02004671 Py_CLEAR(self->reducer_override);
Antoine Pitrou91f43802019-05-26 17:10:09 +02004672 Py_CLEAR(self->buffer_callback);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004673
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004674 if (self->memo != NULL) {
4675 PyMemoTable *memo = self->memo;
4676 self->memo = NULL;
4677 PyMemoTable_Del(memo);
4678 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004679 return 0;
4680}
4681
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004682
Larry Hastings61272b72014-01-07 12:41:53 -08004683/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004684
4685_pickle.Pickler.__init__
4686
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004687 file: object
Serhiy Storchakad322abb2019-09-14 13:31:50 +03004688 protocol: object = None
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004689 fix_imports: bool = True
Serhiy Storchakad322abb2019-09-14 13:31:50 +03004690 buffer_callback: object = None
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004691
4692This takes a binary file for writing a pickle data stream.
4693
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004694The optional *protocol* argument tells the pickler to use the given
Miss Islington (bot)a46728a2020-01-24 02:20:35 -08004695protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
4696protocol is 4. It was introduced in Python 3.4, and is incompatible
4697with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004698
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004699Specifying a negative protocol version selects the highest protocol
4700version supported. The higher the protocol used, the more recent the
4701version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004702
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004703The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004704bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004705writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004706this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004707
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004708If *fix_imports* is True and protocol is less than 3, pickle will try
4709to map the new Python 3 names to the old module names used in Python
47102, so that the pickle data stream is readable with Python 2.
Antoine Pitrou91f43802019-05-26 17:10:09 +02004711
4712If *buffer_callback* is None (the default), buffer views are
4713serialized into *file* as part of the pickle stream.
4714
4715If *buffer_callback* is not None, then it can be called any number
4716of times with a buffer view. If the callback returns a false value
4717(such as None), the given buffer is out-of-band; otherwise the
4718buffer is serialized in-band, i.e. inside the pickle stream.
4719
4720It is an error if *buffer_callback* is not None and *protocol*
4721is None or smaller than 5.
4722
Larry Hastings61272b72014-01-07 12:41:53 -08004723[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004724
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004725static int
Larry Hastings89964c42015-04-14 18:07:59 -04004726_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
Antoine Pitrou91f43802019-05-26 17:10:09 +02004727 PyObject *protocol, int fix_imports,
4728 PyObject *buffer_callback)
Miss Islington (bot)a46728a2020-01-24 02:20:35 -08004729/*[clinic end generated code: output=0abedc50590d259b input=a7c969699bf5dad3]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004730{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004731 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004732 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004734 /* In case of multiple __init__() calls, clear previous content. */
4735 if (self->write != NULL)
4736 (void)Pickler_clear(self);
4737
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004738 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004739 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004740
4741 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004742 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004743
Antoine Pitrou91f43802019-05-26 17:10:09 +02004744 if (_Pickler_SetBufferCallback(self, buffer_callback) < 0)
4745 return -1;
4746
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004747 /* memo and output_buffer may have already been created in _Pickler_New */
4748 if (self->memo == NULL) {
4749 self->memo = PyMemoTable_New();
4750 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004751 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004752 }
4753 self->output_len = 0;
4754 if (self->output_buffer == NULL) {
4755 self->max_output_len = WRITE_BUF_SIZE;
4756 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4757 self->max_output_len);
4758 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004759 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004760 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004761
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004762 self->fast = 0;
4763 self->fast_nesting = 0;
4764 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004765
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004766 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4767 &self->pers_func, &self->pers_func_self) < 0)
4768 {
4769 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004770 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004771
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004772 if (_PyObject_LookupAttrId((PyObject *)self,
4773 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4774 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004775 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004776
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004777 return 0;
4778}
4779
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004780
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004781/* Define a proxy object for the Pickler's internal memo object. This is to
4782 * avoid breaking code like:
4783 * pickler.memo.clear()
4784 * and
4785 * pickler.memo = saved_memo
4786 * Is this a good idea? Not really, but we don't want to break code that uses
4787 * it. Note that we don't implement the entire mapping API here. This is
4788 * intentional, as these should be treated as black-box implementation details.
4789 */
4790
Larry Hastings61272b72014-01-07 12:41:53 -08004791/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004792_pickle.PicklerMemoProxy.clear
4793
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004794Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004795[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004796
Larry Hastings3cceb382014-01-04 11:09:09 -08004797static PyObject *
4798_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004799/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004800{
4801 if (self->pickler->memo)
4802 PyMemoTable_Clear(self->pickler->memo);
4803 Py_RETURN_NONE;
4804}
4805
Larry Hastings61272b72014-01-07 12:41:53 -08004806/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004807_pickle.PicklerMemoProxy.copy
4808
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004809Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004810[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004811
Larry Hastings3cceb382014-01-04 11:09:09 -08004812static PyObject *
4813_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004814/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004815{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004816 PyMemoTable *memo;
4817 PyObject *new_memo = PyDict_New();
4818 if (new_memo == NULL)
4819 return NULL;
4820
4821 memo = self->pickler->memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07004822 for (size_t i = 0; i < memo->mt_allocated; ++i) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004823 PyMemoEntry entry = memo->mt_table[i];
4824 if (entry.me_key != NULL) {
4825 int status;
4826 PyObject *key, *value;
4827
4828 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004829 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004830
4831 if (key == NULL || value == NULL) {
4832 Py_XDECREF(key);
4833 Py_XDECREF(value);
4834 goto error;
4835 }
4836 status = PyDict_SetItem(new_memo, key, value);
4837 Py_DECREF(key);
4838 Py_DECREF(value);
4839 if (status < 0)
4840 goto error;
4841 }
4842 }
4843 return new_memo;
4844
4845 error:
4846 Py_XDECREF(new_memo);
4847 return NULL;
4848}
4849
Larry Hastings61272b72014-01-07 12:41:53 -08004850/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004851_pickle.PicklerMemoProxy.__reduce__
4852
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004853Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004854[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004855
Larry Hastings3cceb382014-01-04 11:09:09 -08004856static PyObject *
4857_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004858/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004859{
4860 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004861 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004862 if (contents == NULL)
4863 return NULL;
4864
4865 reduce_value = PyTuple_New(2);
4866 if (reduce_value == NULL) {
4867 Py_DECREF(contents);
4868 return NULL;
4869 }
4870 dict_args = PyTuple_New(1);
4871 if (dict_args == NULL) {
4872 Py_DECREF(contents);
4873 Py_DECREF(reduce_value);
4874 return NULL;
4875 }
4876 PyTuple_SET_ITEM(dict_args, 0, contents);
4877 Py_INCREF((PyObject *)&PyDict_Type);
4878 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4879 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4880 return reduce_value;
4881}
4882
4883static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004884 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4885 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4886 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004887 {NULL, NULL} /* sentinel */
4888};
4889
4890static void
4891PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4892{
4893 PyObject_GC_UnTrack(self);
4894 Py_XDECREF(self->pickler);
4895 PyObject_GC_Del((PyObject *)self);
4896}
4897
4898static int
4899PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4900 visitproc visit, void *arg)
4901{
4902 Py_VISIT(self->pickler);
4903 return 0;
4904}
4905
4906static int
4907PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4908{
4909 Py_CLEAR(self->pickler);
4910 return 0;
4911}
4912
4913static PyTypeObject PicklerMemoProxyType = {
4914 PyVarObject_HEAD_INIT(NULL, 0)
4915 "_pickle.PicklerMemoProxy", /*tp_name*/
4916 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4917 0,
4918 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004919 0, /* tp_vectorcall_offset */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004920 0, /* tp_getattr */
4921 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004922 0, /* tp_as_async */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004923 0, /* tp_repr */
4924 0, /* tp_as_number */
4925 0, /* tp_as_sequence */
4926 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004927 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004928 0, /* tp_call */
4929 0, /* tp_str */
4930 PyObject_GenericGetAttr, /* tp_getattro */
4931 PyObject_GenericSetAttr, /* tp_setattro */
4932 0, /* tp_as_buffer */
4933 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4934 0, /* tp_doc */
4935 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4936 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4937 0, /* tp_richcompare */
4938 0, /* tp_weaklistoffset */
4939 0, /* tp_iter */
4940 0, /* tp_iternext */
4941 picklerproxy_methods, /* tp_methods */
4942};
4943
4944static PyObject *
4945PicklerMemoProxy_New(PicklerObject *pickler)
4946{
4947 PicklerMemoProxyObject *self;
4948
4949 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4950 if (self == NULL)
4951 return NULL;
4952 Py_INCREF(pickler);
4953 self->pickler = pickler;
4954 PyObject_GC_Track(self);
4955 return (PyObject *)self;
4956}
4957
4958/*****************************************************************************/
4959
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004960static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004961Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004962{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004963 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004964}
4965
4966static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004967Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004969 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004970
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004971 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004972 PyErr_SetString(PyExc_TypeError,
4973 "attribute deletion is not supported");
4974 return -1;
4975 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004976
4977 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4978 PicklerObject *pickler =
4979 ((PicklerMemoProxyObject *)obj)->pickler;
4980
4981 new_memo = PyMemoTable_Copy(pickler->memo);
4982 if (new_memo == NULL)
4983 return -1;
4984 }
4985 else if (PyDict_Check(obj)) {
4986 Py_ssize_t i = 0;
4987 PyObject *key, *value;
4988
4989 new_memo = PyMemoTable_New();
4990 if (new_memo == NULL)
4991 return -1;
4992
4993 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004994 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004995 PyObject *memo_obj;
4996
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004997 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004998 PyErr_SetString(PyExc_TypeError,
4999 "'memo' values must be 2-item tuples");
5000 goto error;
5001 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005002 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005003 if (memo_id == -1 && PyErr_Occurred())
5004 goto error;
5005 memo_obj = PyTuple_GET_ITEM(value, 1);
5006 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
5007 goto error;
5008 }
5009 }
5010 else {
5011 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +02005012 "'memo' attribute must be a PicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005013 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005014 return -1;
5015 }
5016
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005017 PyMemoTable_Del(self->memo);
5018 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005019
5020 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005021
5022 error:
5023 if (new_memo)
5024 PyMemoTable_Del(new_memo);
5025 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005026}
5027
5028static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02005029Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005030{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005031 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005032 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005033 return NULL;
5034 }
5035 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005036}
5037
5038static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02005039Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005041 if (value == NULL) {
5042 PyErr_SetString(PyExc_TypeError,
5043 "attribute deletion is not supported");
5044 return -1;
5045 }
5046 if (!PyCallable_Check(value)) {
5047 PyErr_SetString(PyExc_TypeError,
5048 "persistent_id must be a callable taking one argument");
5049 return -1;
5050 }
5051
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005052 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005053 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03005054 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005055
5056 return 0;
5057}
5058
5059static PyMemberDef Pickler_members[] = {
5060 {"bin", T_INT, offsetof(PicklerObject, bin)},
5061 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01005062 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005063 {NULL}
5064};
5065
5066static PyGetSetDef Pickler_getsets[] = {
5067 {"memo", (getter)Pickler_get_memo,
5068 (setter)Pickler_set_memo},
5069 {"persistent_id", (getter)Pickler_get_persid,
5070 (setter)Pickler_set_persid},
5071 {NULL}
5072};
5073
5074static PyTypeObject Pickler_Type = {
5075 PyVarObject_HEAD_INIT(NULL, 0)
5076 "_pickle.Pickler" , /*tp_name*/
5077 sizeof(PicklerObject), /*tp_basicsize*/
5078 0, /*tp_itemsize*/
5079 (destructor)Pickler_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005080 0, /*tp_vectorcall_offset*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005081 0, /*tp_getattr*/
5082 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005083 0, /*tp_as_async*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005084 0, /*tp_repr*/
5085 0, /*tp_as_number*/
5086 0, /*tp_as_sequence*/
5087 0, /*tp_as_mapping*/
5088 0, /*tp_hash*/
5089 0, /*tp_call*/
5090 0, /*tp_str*/
5091 0, /*tp_getattro*/
5092 0, /*tp_setattro*/
5093 0, /*tp_as_buffer*/
5094 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08005095 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005096 (traverseproc)Pickler_traverse, /*tp_traverse*/
5097 (inquiry)Pickler_clear, /*tp_clear*/
5098 0, /*tp_richcompare*/
5099 0, /*tp_weaklistoffset*/
5100 0, /*tp_iter*/
5101 0, /*tp_iternext*/
5102 Pickler_methods, /*tp_methods*/
5103 Pickler_members, /*tp_members*/
5104 Pickler_getsets, /*tp_getset*/
5105 0, /*tp_base*/
5106 0, /*tp_dict*/
5107 0, /*tp_descr_get*/
5108 0, /*tp_descr_set*/
5109 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08005110 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005111 PyType_GenericAlloc, /*tp_alloc*/
5112 PyType_GenericNew, /*tp_new*/
5113 PyObject_GC_Del, /*tp_free*/
5114 0, /*tp_is_gc*/
5115};
5116
Victor Stinner121aab42011-09-29 23:40:53 +02005117/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005118
5119 XXX: It would be nice to able to avoid Python function call overhead, by
5120 using directly the C version of find_class(), when find_class() is not
5121 overridden by a subclass. Although, this could become rather hackish. A
5122 simpler optimization would be to call the C function when self is not a
5123 subclass instance. */
5124static PyObject *
5125find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
5126{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005127 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005128
Victor Stinner55ba38a2016-12-09 16:09:30 +01005129 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
5130 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005131}
5132
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005133static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005134marker(UnpicklerObject *self)
5135{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005136 Py_ssize_t mark;
5137
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005138 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005139 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005140 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005141 return -1;
5142 }
5143
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005144 mark = self->marks[--self->num_marks];
5145 self->stack->mark_set = self->num_marks != 0;
5146 self->stack->fence = self->num_marks ?
5147 self->marks[self->num_marks - 1] : 0;
5148 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005149}
5150
5151static int
5152load_none(UnpicklerObject *self)
5153{
5154 PDATA_APPEND(self->stack, Py_None, -1);
5155 return 0;
5156}
5157
5158static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005159load_int(UnpicklerObject *self)
5160{
5161 PyObject *value;
5162 char *endptr, *s;
5163 Py_ssize_t len;
5164 long x;
5165
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005166 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005167 return -1;
5168 if (len < 2)
5169 return bad_readline();
5170
5171 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005172 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005173 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005174 x = strtol(s, &endptr, 0);
5175
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005176 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005177 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03005178 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005179 errno = 0;
5180 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005181 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005182 if (value == NULL) {
5183 PyErr_SetString(PyExc_ValueError,
5184 "could not convert string to int");
5185 return -1;
5186 }
5187 }
5188 else {
5189 if (len == 3 && (x == 0 || x == 1)) {
5190 if ((value = PyBool_FromLong(x)) == NULL)
5191 return -1;
5192 }
5193 else {
5194 if ((value = PyLong_FromLong(x)) == NULL)
5195 return -1;
5196 }
5197 }
5198
5199 PDATA_PUSH(self->stack, value, -1);
5200 return 0;
5201}
5202
5203static int
5204load_bool(UnpicklerObject *self, PyObject *boolean)
5205{
5206 assert(boolean == Py_True || boolean == Py_False);
5207 PDATA_APPEND(self->stack, boolean, -1);
5208 return 0;
5209}
5210
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005211/* s contains x bytes of an unsigned little-endian integer. Return its value
5212 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
5213 */
5214static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005215calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005216{
5217 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005218 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005219 size_t x = 0;
5220
Serhiy Storchakae0606192015-09-29 22:10:07 +03005221 if (nbytes > (int)sizeof(size_t)) {
5222 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
5223 * have 64-bit size that can't be represented on 32-bit platform.
5224 */
5225 for (i = (int)sizeof(size_t); i < nbytes; i++) {
5226 if (s[i])
5227 return -1;
5228 }
5229 nbytes = (int)sizeof(size_t);
5230 }
5231 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005232 x |= (size_t) s[i] << (8 * i);
5233 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005234
5235 if (x > PY_SSIZE_T_MAX)
5236 return -1;
5237 else
5238 return (Py_ssize_t) x;
5239}
5240
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005241/* s contains x bytes of a little-endian integer. Return its value as a
5242 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03005243 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005244 * of x-platform bugs.
5245 */
5246static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005247calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005248{
5249 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02005250 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005251 long x = 0;
5252
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005253 for (i = 0; i < nbytes; i++) {
5254 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005255 }
5256
5257 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
5258 * is signed, so on a box with longs bigger than 4 bytes we need
5259 * to extend a BININT's sign bit to the full width.
5260 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005261 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005262 x |= -(x & (1L << 31));
5263 }
5264
5265 return x;
5266}
5267
5268static int
5269load_binintx(UnpicklerObject *self, char *s, int size)
5270{
5271 PyObject *value;
5272 long x;
5273
5274 x = calc_binint(s, size);
5275
5276 if ((value = PyLong_FromLong(x)) == NULL)
5277 return -1;
5278
5279 PDATA_PUSH(self->stack, value, -1);
5280 return 0;
5281}
5282
5283static int
5284load_binint(UnpicklerObject *self)
5285{
5286 char *s;
5287
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005288 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005289 return -1;
5290
5291 return load_binintx(self, s, 4);
5292}
5293
5294static int
5295load_binint1(UnpicklerObject *self)
5296{
5297 char *s;
5298
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005299 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005300 return -1;
5301
5302 return load_binintx(self, s, 1);
5303}
5304
5305static int
5306load_binint2(UnpicklerObject *self)
5307{
5308 char *s;
5309
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005310 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005311 return -1;
5312
5313 return load_binintx(self, s, 2);
5314}
5315
5316static int
5317load_long(UnpicklerObject *self)
5318{
5319 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005320 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005321 Py_ssize_t len;
5322
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005323 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005324 return -1;
5325 if (len < 2)
5326 return bad_readline();
5327
Mark Dickinson8dd05142009-01-20 20:43:58 +00005328 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
5329 the 'L' before calling PyLong_FromString. In order to maintain
5330 compatibility with Python 3.0.0, we don't actually *require*
5331 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005332 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00005333 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00005334 /* XXX: Should the base argument explicitly set to 10? */
5335 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00005336 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005337 return -1;
5338
5339 PDATA_PUSH(self->stack, value, -1);
5340 return 0;
5341}
5342
5343/* 'size' bytes contain the # of bytes of little-endian 256's-complement
5344 * data following.
5345 */
5346static int
5347load_counted_long(UnpicklerObject *self, int size)
5348{
5349 PyObject *value;
5350 char *nbytes;
5351 char *pdata;
5352
5353 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005354 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005355 return -1;
5356
5357 size = calc_binint(nbytes, size);
5358 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005359 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005360 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005361 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005362 "LONG pickle has negative byte count");
5363 return -1;
5364 }
5365
5366 if (size == 0)
5367 value = PyLong_FromLong(0L);
5368 else {
5369 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005370 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005371 return -1;
5372 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
5373 1 /* little endian */ , 1 /* signed */ );
5374 }
5375 if (value == NULL)
5376 return -1;
5377 PDATA_PUSH(self->stack, value, -1);
5378 return 0;
5379}
5380
5381static int
5382load_float(UnpicklerObject *self)
5383{
5384 PyObject *value;
5385 char *endptr, *s;
5386 Py_ssize_t len;
5387 double d;
5388
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005389 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005390 return -1;
5391 if (len < 2)
5392 return bad_readline();
5393
5394 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00005395 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
5396 if (d == -1.0 && PyErr_Occurred())
5397 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005398 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005399 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5400 return -1;
5401 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005402 value = PyFloat_FromDouble(d);
5403 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005404 return -1;
5405
5406 PDATA_PUSH(self->stack, value, -1);
5407 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005408}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005409
5410static int
5411load_binfloat(UnpicklerObject *self)
5412{
5413 PyObject *value;
5414 double x;
5415 char *s;
5416
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005417 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005418 return -1;
5419
5420 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5421 if (x == -1.0 && PyErr_Occurred())
5422 return -1;
5423
5424 if ((value = PyFloat_FromDouble(x)) == NULL)
5425 return -1;
5426
5427 PDATA_PUSH(self->stack, value, -1);
5428 return 0;
5429}
5430
5431static int
5432load_string(UnpicklerObject *self)
5433{
5434 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005435 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005436 Py_ssize_t len;
5437 char *s, *p;
5438
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005439 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005440 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005441 /* Strip the newline */
5442 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005443 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005444 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005445 p = s + 1;
5446 len -= 2;
5447 }
5448 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005449 PickleState *st = _Pickle_GetGlobalState();
5450 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005451 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005452 return -1;
5453 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005454 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005455
5456 /* Use the PyBytes API to decode the string, since that is what is used
5457 to encode, and then coerce the result to Unicode. */
5458 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005459 if (bytes == NULL)
5460 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005461
5462 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5463 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5464 if (strcmp(self->encoding, "bytes") == 0) {
5465 obj = bytes;
5466 }
5467 else {
5468 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5469 Py_DECREF(bytes);
5470 if (obj == NULL) {
5471 return -1;
5472 }
5473 }
5474
5475 PDATA_PUSH(self->stack, obj, -1);
5476 return 0;
5477}
5478
5479static int
5480load_counted_binstring(UnpicklerObject *self, int nbytes)
5481{
5482 PyObject *obj;
5483 Py_ssize_t size;
5484 char *s;
5485
5486 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005487 return -1;
5488
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005489 size = calc_binsize(s, nbytes);
5490 if (size < 0) {
5491 PickleState *st = _Pickle_GetGlobalState();
5492 PyErr_Format(st->UnpicklingError,
5493 "BINSTRING exceeds system's maximum size of %zd bytes",
5494 PY_SSIZE_T_MAX);
5495 return -1;
5496 }
5497
5498 if (_Unpickler_Read(self, &s, size) < 0)
5499 return -1;
5500
5501 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5502 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5503 if (strcmp(self->encoding, "bytes") == 0) {
5504 obj = PyBytes_FromStringAndSize(s, size);
5505 }
5506 else {
5507 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5508 }
5509 if (obj == NULL) {
5510 return -1;
5511 }
5512
5513 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005514 return 0;
5515}
5516
5517static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005518load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005519{
5520 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005521 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005522 char *s;
5523
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005524 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005525 return -1;
5526
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005527 size = calc_binsize(s, nbytes);
5528 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005529 PyErr_Format(PyExc_OverflowError,
5530 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005531 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005532 return -1;
5533 }
5534
Antoine Pitrou91f43802019-05-26 17:10:09 +02005535 bytes = PyBytes_FromStringAndSize(NULL, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005536 if (bytes == NULL)
5537 return -1;
Antoine Pitrou91f43802019-05-26 17:10:09 +02005538 if (_Unpickler_ReadInto(self, PyBytes_AS_STRING(bytes), size) < 0) {
5539 Py_DECREF(bytes);
5540 return -1;
5541 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005542
5543 PDATA_PUSH(self->stack, bytes, -1);
5544 return 0;
5545}
5546
5547static int
Antoine Pitrou91f43802019-05-26 17:10:09 +02005548load_counted_bytearray(UnpicklerObject *self)
5549{
5550 PyObject *bytearray;
5551 Py_ssize_t size;
5552 char *s;
5553
5554 if (_Unpickler_Read(self, &s, 8) < 0) {
5555 return -1;
5556 }
5557
5558 size = calc_binsize(s, 8);
5559 if (size < 0) {
5560 PyErr_Format(PyExc_OverflowError,
5561 "BYTEARRAY8 exceeds system's maximum size of %zd bytes",
5562 PY_SSIZE_T_MAX);
5563 return -1;
5564 }
5565
5566 bytearray = PyByteArray_FromStringAndSize(NULL, size);
5567 if (bytearray == NULL) {
5568 return -1;
5569 }
5570 if (_Unpickler_ReadInto(self, PyByteArray_AS_STRING(bytearray), size) < 0) {
5571 Py_DECREF(bytearray);
5572 return -1;
5573 }
5574
5575 PDATA_PUSH(self->stack, bytearray, -1);
5576 return 0;
5577}
5578
5579static int
5580load_next_buffer(UnpicklerObject *self)
5581{
5582 if (self->buffers == NULL) {
5583 PickleState *st = _Pickle_GetGlobalState();
5584 PyErr_SetString(st->UnpicklingError,
5585 "pickle stream refers to out-of-band data "
5586 "but no *buffers* argument was given");
5587 return -1;
5588 }
5589 PyObject *buf = PyIter_Next(self->buffers);
5590 if (buf == NULL) {
5591 if (!PyErr_Occurred()) {
5592 PickleState *st = _Pickle_GetGlobalState();
5593 PyErr_SetString(st->UnpicklingError,
5594 "not enough out-of-band buffers");
5595 }
5596 return -1;
5597 }
5598
5599 PDATA_PUSH(self->stack, buf, -1);
5600 return 0;
5601}
5602
5603static int
5604load_readonly_buffer(UnpicklerObject *self)
5605{
5606 Py_ssize_t len = Py_SIZE(self->stack);
5607 if (len <= self->stack->fence) {
5608 return Pdata_stack_underflow(self->stack);
5609 }
5610
5611 PyObject *obj = self->stack->data[len - 1];
5612 PyObject *view = PyMemoryView_FromObject(obj);
5613 if (view == NULL) {
5614 return -1;
5615 }
5616 if (!PyMemoryView_GET_BUFFER(view)->readonly) {
5617 /* Original object is writable */
5618 PyMemoryView_GET_BUFFER(view)->readonly = 1;
5619 self->stack->data[len - 1] = view;
5620 Py_DECREF(obj);
5621 }
5622 else {
5623 /* Original object is read-only, no need to replace it */
5624 Py_DECREF(view);
5625 }
5626 return 0;
5627}
5628
5629static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630load_unicode(UnpicklerObject *self)
5631{
5632 PyObject *str;
5633 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005634 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005636 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005637 return -1;
5638 if (len < 1)
5639 return bad_readline();
5640
5641 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5642 if (str == NULL)
5643 return -1;
5644
5645 PDATA_PUSH(self->stack, str, -1);
5646 return 0;
5647}
5648
5649static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005650load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005651{
5652 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005653 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005654 char *s;
5655
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005656 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657 return -1;
5658
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005659 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005660 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005661 PyErr_Format(PyExc_OverflowError,
5662 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005663 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005664 return -1;
5665 }
5666
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005667 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005668 return -1;
5669
Victor Stinner485fb562010-04-13 11:07:24 +00005670 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005671 if (str == NULL)
5672 return -1;
5673
5674 PDATA_PUSH(self->stack, str, -1);
5675 return 0;
5676}
5677
5678static int
Victor Stinner21b47112016-03-14 18:09:39 +01005679load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005680{
5681 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005683 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005684 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005685
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005686 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687 if (tuple == NULL)
5688 return -1;
5689 PDATA_PUSH(self->stack, tuple, -1);
5690 return 0;
5691}
5692
5693static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005694load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005695{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005696 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005697
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005698 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005699 return -1;
5700
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005701 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005702}
5703
5704static int
5705load_empty_list(UnpicklerObject *self)
5706{
5707 PyObject *list;
5708
5709 if ((list = PyList_New(0)) == NULL)
5710 return -1;
5711 PDATA_PUSH(self->stack, list, -1);
5712 return 0;
5713}
5714
5715static int
5716load_empty_dict(UnpicklerObject *self)
5717{
5718 PyObject *dict;
5719
5720 if ((dict = PyDict_New()) == NULL)
5721 return -1;
5722 PDATA_PUSH(self->stack, dict, -1);
5723 return 0;
5724}
5725
5726static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005727load_empty_set(UnpicklerObject *self)
5728{
5729 PyObject *set;
5730
5731 if ((set = PySet_New(NULL)) == NULL)
5732 return -1;
5733 PDATA_PUSH(self->stack, set, -1);
5734 return 0;
5735}
5736
5737static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005738load_list(UnpicklerObject *self)
5739{
5740 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005741 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005742
5743 if ((i = marker(self)) < 0)
5744 return -1;
5745
5746 list = Pdata_poplist(self->stack, i);
5747 if (list == NULL)
5748 return -1;
5749 PDATA_PUSH(self->stack, list, -1);
5750 return 0;
5751}
5752
5753static int
5754load_dict(UnpicklerObject *self)
5755{
5756 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005757 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005758
5759 if ((i = marker(self)) < 0)
5760 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005761 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005762
5763 if ((dict = PyDict_New()) == NULL)
5764 return -1;
5765
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005766 if ((j - i) % 2 != 0) {
5767 PickleState *st = _Pickle_GetGlobalState();
5768 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005769 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005770 return -1;
5771 }
5772
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773 for (k = i + 1; k < j; k += 2) {
5774 key = self->stack->data[k - 1];
5775 value = self->stack->data[k];
5776 if (PyDict_SetItem(dict, key, value) < 0) {
5777 Py_DECREF(dict);
5778 return -1;
5779 }
5780 }
5781 Pdata_clear(self->stack, i);
5782 PDATA_PUSH(self->stack, dict, -1);
5783 return 0;
5784}
5785
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005786static int
5787load_frozenset(UnpicklerObject *self)
5788{
5789 PyObject *items;
5790 PyObject *frozenset;
5791 Py_ssize_t i;
5792
5793 if ((i = marker(self)) < 0)
5794 return -1;
5795
5796 items = Pdata_poptuple(self->stack, i);
5797 if (items == NULL)
5798 return -1;
5799
5800 frozenset = PyFrozenSet_New(items);
5801 Py_DECREF(items);
5802 if (frozenset == NULL)
5803 return -1;
5804
5805 PDATA_PUSH(self->stack, frozenset, -1);
5806 return 0;
5807}
5808
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005809static PyObject *
5810instantiate(PyObject *cls, PyObject *args)
5811{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005812 /* Caller must assure args are a tuple. Normally, args come from
5813 Pdata_poptuple which packs objects from the top of the stack
5814 into a newly created tuple. */
5815 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005816 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5817 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005818 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005819 PyObject *func;
5820 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5821 return NULL;
5822 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005823 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005824 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5825 }
5826 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005827 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005828 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005829}
5830
5831static int
5832load_obj(UnpicklerObject *self)
5833{
5834 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005835 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005836
5837 if ((i = marker(self)) < 0)
5838 return -1;
5839
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005840 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005841 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005842
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005843 args = Pdata_poptuple(self->stack, i + 1);
5844 if (args == NULL)
5845 return -1;
5846
5847 PDATA_POP(self->stack, cls);
5848 if (cls) {
5849 obj = instantiate(cls, args);
5850 Py_DECREF(cls);
5851 }
5852 Py_DECREF(args);
5853 if (obj == NULL)
5854 return -1;
5855
5856 PDATA_PUSH(self->stack, obj, -1);
5857 return 0;
5858}
5859
5860static int
5861load_inst(UnpicklerObject *self)
5862{
5863 PyObject *cls = NULL;
5864 PyObject *args = NULL;
5865 PyObject *obj = NULL;
5866 PyObject *module_name;
5867 PyObject *class_name;
5868 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005869 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005870 char *s;
5871
5872 if ((i = marker(self)) < 0)
5873 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005874 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005875 return -1;
5876 if (len < 2)
5877 return bad_readline();
5878
5879 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5880 identifiers are permitted in Python 3.0, since the INST opcode is only
5881 supported by older protocols on Python 2.x. */
5882 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5883 if (module_name == NULL)
5884 return -1;
5885
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005886 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005887 if (len < 2) {
5888 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005889 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005890 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005891 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005892 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005893 cls = find_class(self, module_name, class_name);
5894 Py_DECREF(class_name);
5895 }
5896 }
5897 Py_DECREF(module_name);
5898
5899 if (cls == NULL)
5900 return -1;
5901
5902 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5903 obj = instantiate(cls, args);
5904 Py_DECREF(args);
5905 }
5906 Py_DECREF(cls);
5907
5908 if (obj == NULL)
5909 return -1;
5910
5911 PDATA_PUSH(self->stack, obj, -1);
5912 return 0;
5913}
5914
5915static int
5916load_newobj(UnpicklerObject *self)
5917{
5918 PyObject *args = NULL;
5919 PyObject *clsraw = NULL;
5920 PyTypeObject *cls; /* clsraw cast to its true type */
5921 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005922 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923
5924 /* Stack is ... cls argtuple, and we want to call
5925 * cls.__new__(cls, *argtuple).
5926 */
5927 PDATA_POP(self->stack, args);
5928 if (args == NULL)
5929 goto error;
5930 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005931 PyErr_SetString(st->UnpicklingError,
5932 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005933 goto error;
5934 }
5935
5936 PDATA_POP(self->stack, clsraw);
5937 cls = (PyTypeObject *)clsraw;
5938 if (cls == NULL)
5939 goto error;
5940 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005941 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005942 "isn't a type object");
5943 goto error;
5944 }
5945 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005946 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005947 "has NULL tp_new");
5948 goto error;
5949 }
5950
5951 /* Call __new__. */
5952 obj = cls->tp_new(cls, args, NULL);
5953 if (obj == NULL)
5954 goto error;
5955
5956 Py_DECREF(args);
5957 Py_DECREF(clsraw);
5958 PDATA_PUSH(self->stack, obj, -1);
5959 return 0;
5960
5961 error:
5962 Py_XDECREF(args);
5963 Py_XDECREF(clsraw);
5964 return -1;
5965}
5966
5967static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005968load_newobj_ex(UnpicklerObject *self)
5969{
5970 PyObject *cls, *args, *kwargs;
5971 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005972 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005973
5974 PDATA_POP(self->stack, kwargs);
5975 if (kwargs == NULL) {
5976 return -1;
5977 }
5978 PDATA_POP(self->stack, args);
5979 if (args == NULL) {
5980 Py_DECREF(kwargs);
5981 return -1;
5982 }
5983 PDATA_POP(self->stack, cls);
5984 if (cls == NULL) {
5985 Py_DECREF(kwargs);
5986 Py_DECREF(args);
5987 return -1;
5988 }
Larry Hastings61272b72014-01-07 12:41:53 -08005989
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005990 if (!PyType_Check(cls)) {
5991 Py_DECREF(kwargs);
5992 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005993 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005994 "NEWOBJ_EX class argument must be a type, not %.200s",
5995 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005996 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005997 return -1;
5998 }
5999
6000 if (((PyTypeObject *)cls)->tp_new == NULL) {
6001 Py_DECREF(kwargs);
6002 Py_DECREF(args);
6003 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006004 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006005 "NEWOBJ_EX class argument doesn't have __new__");
6006 return -1;
6007 }
6008 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
6009 Py_DECREF(kwargs);
6010 Py_DECREF(args);
6011 Py_DECREF(cls);
6012 if (obj == NULL) {
6013 return -1;
6014 }
6015 PDATA_PUSH(self->stack, obj, -1);
6016 return 0;
6017}
6018
6019static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006020load_global(UnpicklerObject *self)
6021{
6022 PyObject *global = NULL;
6023 PyObject *module_name;
6024 PyObject *global_name;
6025 Py_ssize_t len;
6026 char *s;
6027
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006028 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006029 return -1;
6030 if (len < 2)
6031 return bad_readline();
6032 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
6033 if (!module_name)
6034 return -1;
6035
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006036 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006037 if (len < 2) {
6038 Py_DECREF(module_name);
6039 return bad_readline();
6040 }
6041 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
6042 if (global_name) {
6043 global = find_class(self, module_name, global_name);
6044 Py_DECREF(global_name);
6045 }
6046 }
6047 Py_DECREF(module_name);
6048
6049 if (global == NULL)
6050 return -1;
6051 PDATA_PUSH(self->stack, global, -1);
6052 return 0;
6053}
6054
6055static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006056load_stack_global(UnpicklerObject *self)
6057{
6058 PyObject *global;
6059 PyObject *module_name;
6060 PyObject *global_name;
6061
6062 PDATA_POP(self->stack, global_name);
6063 PDATA_POP(self->stack, module_name);
6064 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
6065 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006066 PickleState *st = _Pickle_GetGlobalState();
6067 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006068 Py_XDECREF(global_name);
6069 Py_XDECREF(module_name);
6070 return -1;
6071 }
6072 global = find_class(self, module_name, global_name);
6073 Py_DECREF(global_name);
6074 Py_DECREF(module_name);
6075 if (global == NULL)
6076 return -1;
6077 PDATA_PUSH(self->stack, global, -1);
6078 return 0;
6079}
6080
6081static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006082load_persid(UnpicklerObject *self)
6083{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006084 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006085 Py_ssize_t len;
6086 char *s;
6087
6088 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006089 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006090 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08006091 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006092 return bad_readline();
6093
Serhiy Storchakadec25af2016-07-17 11:24:17 +03006094 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
6095 if (pid == NULL) {
6096 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
6097 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
6098 "persistent IDs in protocol 0 must be "
6099 "ASCII strings");
6100 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006101 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03006102 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006103
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006104 obj = call_method(self->pers_func, self->pers_func_self, pid);
6105 Py_DECREF(pid);
6106 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006107 return -1;
6108
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006109 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006110 return 0;
6111 }
6112 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006113 PickleState *st = _Pickle_GetGlobalState();
6114 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006115 "A load persistent id instruction was encountered,\n"
6116 "but no persistent_load function was specified.");
6117 return -1;
6118 }
6119}
6120
6121static int
6122load_binpersid(UnpicklerObject *self)
6123{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006124 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006125
6126 if (self->pers_func) {
6127 PDATA_POP(self->stack, pid);
6128 if (pid == NULL)
6129 return -1;
6130
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006131 obj = call_method(self->pers_func, self->pers_func_self, pid);
6132 Py_DECREF(pid);
6133 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006134 return -1;
6135
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006136 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006137 return 0;
6138 }
6139 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006140 PickleState *st = _Pickle_GetGlobalState();
6141 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006142 "A load persistent id instruction was encountered,\n"
6143 "but no persistent_load function was specified.");
6144 return -1;
6145 }
6146}
6147
6148static int
6149load_pop(UnpicklerObject *self)
6150{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006151 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006152
6153 /* Note that we split the (pickle.py) stack into two stacks,
6154 * an object stack and a mark stack. We have to be clever and
6155 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00006156 * mark stack first, and only signalling a stack underflow if
6157 * the object stack is empty and the mark stack doesn't match
6158 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006159 */
Collin Winter8ca69de2009-05-26 16:53:41 +00006160 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006161 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006162 self->stack->mark_set = self->num_marks != 0;
6163 self->stack->fence = self->num_marks ?
6164 self->marks[self->num_marks - 1] : 0;
6165 } else if (len <= self->stack->fence)
6166 return Pdata_stack_underflow(self->stack);
6167 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006168 len--;
6169 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006170 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006171 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006172 return 0;
6173}
6174
6175static int
6176load_pop_mark(UnpicklerObject *self)
6177{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006178 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006179
6180 if ((i = marker(self)) < 0)
6181 return -1;
6182
6183 Pdata_clear(self->stack, i);
6184
6185 return 0;
6186}
6187
6188static int
6189load_dup(UnpicklerObject *self)
6190{
6191 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006192 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006193
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006194 if (len <= self->stack->fence)
6195 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006196 last = self->stack->data[len - 1];
6197 PDATA_APPEND(self->stack, last, -1);
6198 return 0;
6199}
6200
6201static int
6202load_get(UnpicklerObject *self)
6203{
6204 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006205 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206 Py_ssize_t len;
6207 char *s;
6208
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006209 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006210 return -1;
6211 if (len < 2)
6212 return bad_readline();
6213
6214 key = PyLong_FromString(s, NULL, 10);
6215 if (key == NULL)
6216 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006217 idx = PyLong_AsSsize_t(key);
6218 if (idx == -1 && PyErr_Occurred()) {
6219 Py_DECREF(key);
6220 return -1;
6221 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006222
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006223 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006224 if (value == NULL) {
6225 if (!PyErr_Occurred())
6226 PyErr_SetObject(PyExc_KeyError, key);
6227 Py_DECREF(key);
6228 return -1;
6229 }
6230 Py_DECREF(key);
6231
6232 PDATA_APPEND(self->stack, value, -1);
6233 return 0;
6234}
6235
6236static int
6237load_binget(UnpicklerObject *self)
6238{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006239 PyObject *value;
6240 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241 char *s;
6242
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006243 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006244 return -1;
6245
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006246 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006247
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006248 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006249 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006250 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02006251 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006252 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02006253 Py_DECREF(key);
6254 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006255 return -1;
6256 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006257
6258 PDATA_APPEND(self->stack, value, -1);
6259 return 0;
6260}
6261
6262static int
6263load_long_binget(UnpicklerObject *self)
6264{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006265 PyObject *value;
6266 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006267 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006268
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006269 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006270 return -1;
6271
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006272 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006273
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006274 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006275 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006276 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02006277 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006278 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02006279 Py_DECREF(key);
6280 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006281 return -1;
6282 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006283
6284 PDATA_APPEND(self->stack, value, -1);
6285 return 0;
6286}
6287
6288/* Push an object from the extension registry (EXT[124]). nbytes is
6289 * the number of bytes following the opcode, holding the index (code) value.
6290 */
6291static int
6292load_extension(UnpicklerObject *self, int nbytes)
6293{
6294 char *codebytes; /* the nbytes bytes after the opcode */
6295 long code; /* calc_binint returns long */
6296 PyObject *py_code; /* code as a Python int */
6297 PyObject *obj; /* the object to push */
6298 PyObject *pair; /* (module_name, class_name) */
6299 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006300 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006301
6302 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006303 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006304 return -1;
6305 code = calc_binint(codebytes, nbytes);
6306 if (code <= 0) { /* note that 0 is forbidden */
6307 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006308 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006309 return -1;
6310 }
6311
6312 /* Look for the code in the cache. */
6313 py_code = PyLong_FromLong(code);
6314 if (py_code == NULL)
6315 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08006316 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006317 if (obj != NULL) {
6318 /* Bingo. */
6319 Py_DECREF(py_code);
6320 PDATA_APPEND(self->stack, obj, -1);
6321 return 0;
6322 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08006323 if (PyErr_Occurred()) {
6324 Py_DECREF(py_code);
6325 return -1;
6326 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006327
6328 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08006329 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006330 if (pair == NULL) {
6331 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08006332 if (!PyErr_Occurred()) {
6333 PyErr_Format(PyExc_ValueError, "unregistered extension "
6334 "code %ld", code);
6335 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006336 return -1;
6337 }
6338 /* Since the extension registry is manipulable via Python code,
6339 * confirm that pair is really a 2-tuple of strings.
6340 */
Victor Stinnerb37672d2018-11-22 03:37:50 +01006341 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
6342 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006343 }
Victor Stinnerb37672d2018-11-22 03:37:50 +01006344
6345 module_name = PyTuple_GET_ITEM(pair, 0);
6346 if (!PyUnicode_Check(module_name)) {
6347 goto error;
6348 }
6349
6350 class_name = PyTuple_GET_ITEM(pair, 1);
6351 if (!PyUnicode_Check(class_name)) {
6352 goto error;
6353 }
6354
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006355 /* Load the object. */
6356 obj = find_class(self, module_name, class_name);
6357 if (obj == NULL) {
6358 Py_DECREF(py_code);
6359 return -1;
6360 }
6361 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006362 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006363 Py_DECREF(py_code);
6364 if (code < 0) {
6365 Py_DECREF(obj);
6366 return -1;
6367 }
6368 PDATA_PUSH(self->stack, obj, -1);
6369 return 0;
Victor Stinnerb37672d2018-11-22 03:37:50 +01006370
6371error:
6372 Py_DECREF(py_code);
6373 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
6374 "isn't a 2-tuple of strings", code);
6375 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006376}
6377
6378static int
6379load_put(UnpicklerObject *self)
6380{
6381 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006382 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006383 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01006384 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006385
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006386 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006387 return -1;
6388 if (len < 2)
6389 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006390 if (Py_SIZE(self->stack) <= self->stack->fence)
6391 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006392 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006393
6394 key = PyLong_FromString(s, NULL, 10);
6395 if (key == NULL)
6396 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006397 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006398 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02006399 if (idx < 0) {
6400 if (!PyErr_Occurred())
6401 PyErr_SetString(PyExc_ValueError,
6402 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006403 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02006404 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006405
6406 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006407}
6408
6409static int
6410load_binput(UnpicklerObject *self)
6411{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006412 PyObject *value;
6413 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006414 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006415
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006416 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006417 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006418
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006419 if (Py_SIZE(self->stack) <= self->stack->fence)
6420 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006421 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006422
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006423 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006424
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006425 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006426}
6427
6428static int
6429load_long_binput(UnpicklerObject *self)
6430{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006431 PyObject *value;
6432 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006433 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006434
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006435 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006436 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006437
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006438 if (Py_SIZE(self->stack) <= self->stack->fence)
6439 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006440 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006441
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006442 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02006443 if (idx < 0) {
6444 PyErr_SetString(PyExc_ValueError,
6445 "negative LONG_BINPUT argument");
6446 return -1;
6447 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006448
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006449 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006450}
6451
6452static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006453load_memoize(UnpicklerObject *self)
6454{
6455 PyObject *value;
6456
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006457 if (Py_SIZE(self->stack) <= self->stack->fence)
6458 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006459 value = self->stack->data[Py_SIZE(self->stack) - 1];
6460
6461 return _Unpickler_MemoPut(self, self->memo_len, value);
6462}
6463
6464static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006465do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006466{
6467 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006468 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006469 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006470 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006471 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006472
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006473 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006474 if (x > len || x <= self->stack->fence)
6475 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006476 if (len == x) /* nothing to do */
6477 return 0;
6478
6479 list = self->stack->data[x - 1];
6480
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006481 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006483 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006484
6485 slice = Pdata_poplist(self->stack, x);
6486 if (!slice)
6487 return -1;
6488 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006489 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006490 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006491 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006492 }
6493 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006494 PyObject *extend_func;
6495 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006496
Serhiy Storchaka353053d2019-09-01 14:01:05 +03006497 if (_PyObject_LookupAttrId(list, &PyId_extend, &extend_func) < 0) {
6498 return -1;
6499 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006500 if (extend_func != NULL) {
6501 slice = Pdata_poplist(self->stack, x);
6502 if (!slice) {
6503 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006504 return -1;
6505 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006506 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006507 Py_DECREF(extend_func);
6508 if (result == NULL)
6509 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006510 Py_DECREF(result);
6511 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006512 else {
6513 PyObject *append_func;
6514 _Py_IDENTIFIER(append);
6515
6516 /* Even if the PEP 307 requires extend() and append() methods,
6517 fall back on append() if the object has no extend() method
6518 for backward compatibility. */
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006519 append_func = _PyObject_GetAttrId(list, &PyId_append);
6520 if (append_func == NULL)
6521 return -1;
6522 for (i = x; i < len; i++) {
6523 value = self->stack->data[i];
6524 result = _Pickle_FastCall(append_func, value);
6525 if (result == NULL) {
6526 Pdata_clear(self->stack, i + 1);
6527 Py_SIZE(self->stack) = x;
6528 Py_DECREF(append_func);
6529 return -1;
6530 }
6531 Py_DECREF(result);
6532 }
6533 Py_SIZE(self->stack) = x;
6534 Py_DECREF(append_func);
6535 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006536 }
6537
6538 return 0;
6539}
6540
6541static int
6542load_append(UnpicklerObject *self)
6543{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006544 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6545 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006546 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006547}
6548
6549static int
6550load_appends(UnpicklerObject *self)
6551{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006552 Py_ssize_t i = marker(self);
6553 if (i < 0)
6554 return -1;
6555 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006556}
6557
6558static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006559do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006560{
6561 PyObject *value, *key;
6562 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006563 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006564 int status = 0;
6565
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006566 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006567 if (x > len || x <= self->stack->fence)
6568 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006569 if (len == x) /* nothing to do */
6570 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006571 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006572 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006573 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006574 PyErr_SetString(st->UnpicklingError,
6575 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006576 return -1;
6577 }
6578
6579 /* Here, dict does not actually need to be a PyDict; it could be anything
6580 that supports the __setitem__ attribute. */
6581 dict = self->stack->data[x - 1];
6582
6583 for (i = x + 1; i < len; i += 2) {
6584 key = self->stack->data[i - 1];
6585 value = self->stack->data[i];
6586 if (PyObject_SetItem(dict, key, value) < 0) {
6587 status = -1;
6588 break;
6589 }
6590 }
6591
6592 Pdata_clear(self->stack, x);
6593 return status;
6594}
6595
6596static int
6597load_setitem(UnpicklerObject *self)
6598{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006599 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006600}
6601
6602static int
6603load_setitems(UnpicklerObject *self)
6604{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006605 Py_ssize_t i = marker(self);
6606 if (i < 0)
6607 return -1;
6608 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006609}
6610
6611static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006612load_additems(UnpicklerObject *self)
6613{
6614 PyObject *set;
6615 Py_ssize_t mark, len, i;
6616
6617 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006618 if (mark < 0)
6619 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006620 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006621 if (mark > len || mark <= self->stack->fence)
6622 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006623 if (len == mark) /* nothing to do */
6624 return 0;
6625
6626 set = self->stack->data[mark - 1];
6627
6628 if (PySet_Check(set)) {
6629 PyObject *items;
6630 int status;
6631
6632 items = Pdata_poptuple(self->stack, mark);
6633 if (items == NULL)
6634 return -1;
6635
6636 status = _PySet_Update(set, items);
6637 Py_DECREF(items);
6638 return status;
6639 }
6640 else {
6641 PyObject *add_func;
6642 _Py_IDENTIFIER(add);
6643
6644 add_func = _PyObject_GetAttrId(set, &PyId_add);
6645 if (add_func == NULL)
6646 return -1;
6647 for (i = mark; i < len; i++) {
6648 PyObject *result;
6649 PyObject *item;
6650
6651 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006652 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006653 if (result == NULL) {
6654 Pdata_clear(self->stack, i + 1);
6655 Py_SIZE(self->stack) = mark;
6656 return -1;
6657 }
6658 Py_DECREF(result);
6659 }
6660 Py_SIZE(self->stack) = mark;
6661 }
6662
6663 return 0;
6664}
6665
6666static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006667load_build(UnpicklerObject *self)
6668{
6669 PyObject *state, *inst, *slotstate;
6670 PyObject *setstate;
6671 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006672 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006673
6674 /* Stack is ... instance, state. We want to leave instance at
6675 * the stack top, possibly mutated via instance.__setstate__(state).
6676 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006677 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6678 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006679
6680 PDATA_POP(self->stack, state);
6681 if (state == NULL)
6682 return -1;
6683
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006684 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006685
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006686 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6687 Py_DECREF(state);
6688 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006689 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006690 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006691 PyObject *result;
6692
6693 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006694 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006695 Py_DECREF(setstate);
6696 if (result == NULL)
6697 return -1;
6698 Py_DECREF(result);
6699 return 0;
6700 }
6701
6702 /* A default __setstate__. First see whether state embeds a
6703 * slot state dict too (a proto 2 addition).
6704 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006705 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006706 PyObject *tmp = state;
6707
6708 state = PyTuple_GET_ITEM(tmp, 0);
6709 slotstate = PyTuple_GET_ITEM(tmp, 1);
6710 Py_INCREF(state);
6711 Py_INCREF(slotstate);
6712 Py_DECREF(tmp);
6713 }
6714 else
6715 slotstate = NULL;
6716
6717 /* Set inst.__dict__ from the state dict (if any). */
6718 if (state != Py_None) {
6719 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006720 PyObject *d_key, *d_value;
6721 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006722 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006723
6724 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006725 PickleState *st = _Pickle_GetGlobalState();
6726 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006727 goto error;
6728 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006729 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006730 if (dict == NULL)
6731 goto error;
6732
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006733 i = 0;
6734 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6735 /* normally the keys for instance attributes are
6736 interned. we should try to do that here. */
6737 Py_INCREF(d_key);
6738 if (PyUnicode_CheckExact(d_key))
6739 PyUnicode_InternInPlace(&d_key);
6740 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6741 Py_DECREF(d_key);
6742 goto error;
6743 }
6744 Py_DECREF(d_key);
6745 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006746 Py_DECREF(dict);
6747 }
6748
6749 /* Also set instance attributes from the slotstate dict (if any). */
6750 if (slotstate != NULL) {
6751 PyObject *d_key, *d_value;
6752 Py_ssize_t i;
6753
6754 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006755 PickleState *st = _Pickle_GetGlobalState();
6756 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006757 "slot state is not a dictionary");
6758 goto error;
6759 }
6760 i = 0;
6761 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6762 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6763 goto error;
6764 }
6765 }
6766
6767 if (0) {
6768 error:
6769 status = -1;
6770 }
6771
6772 Py_DECREF(state);
6773 Py_XDECREF(slotstate);
6774 return status;
6775}
6776
6777static int
6778load_mark(UnpicklerObject *self)
6779{
6780
6781 /* Note that we split the (pickle.py) stack into two stacks, an
6782 * object stack and a mark stack. Here we push a mark onto the
6783 * mark stack.
6784 */
6785
Sergey Fedoseev86b89912018-08-25 12:54:40 +05006786 if (self->num_marks >= self->marks_size) {
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006787 size_t alloc = ((size_t)self->num_marks << 1) + 20;
6788 Py_ssize_t *marks_new = self->marks;
6789 PyMem_RESIZE(marks_new, Py_ssize_t, alloc);
6790 if (marks_new == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006791 PyErr_NoMemory();
6792 return -1;
6793 }
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006794 self->marks = marks_new;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006795 self->marks_size = (Py_ssize_t)alloc;
6796 }
6797
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006798 self->stack->mark_set = 1;
6799 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006800
6801 return 0;
6802}
6803
6804static int
6805load_reduce(UnpicklerObject *self)
6806{
6807 PyObject *callable = NULL;
6808 PyObject *argtup = NULL;
6809 PyObject *obj = NULL;
6810
6811 PDATA_POP(self->stack, argtup);
6812 if (argtup == NULL)
6813 return -1;
6814 PDATA_POP(self->stack, callable);
6815 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006816 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006817 Py_DECREF(callable);
6818 }
6819 Py_DECREF(argtup);
6820
6821 if (obj == NULL)
6822 return -1;
6823
6824 PDATA_PUSH(self->stack, obj, -1);
6825 return 0;
6826}
6827
6828/* Just raises an error if we don't know the protocol specified. PROTO
6829 * is the first opcode for protocols >= 2.
6830 */
6831static int
6832load_proto(UnpicklerObject *self)
6833{
6834 char *s;
6835 int i;
6836
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006837 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006838 return -1;
6839
6840 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006841 if (i <= HIGHEST_PROTOCOL) {
6842 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006843 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006844 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006845
6846 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6847 return -1;
6848}
6849
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006850static int
6851load_frame(UnpicklerObject *self)
6852{
6853 char *s;
6854 Py_ssize_t frame_len;
6855
6856 if (_Unpickler_Read(self, &s, 8) < 0)
6857 return -1;
6858
6859 frame_len = calc_binsize(s, 8);
6860 if (frame_len < 0) {
6861 PyErr_Format(PyExc_OverflowError,
6862 "FRAME length exceeds system's maximum of %zd bytes",
6863 PY_SSIZE_T_MAX);
6864 return -1;
6865 }
6866
6867 if (_Unpickler_Read(self, &s, frame_len) < 0)
6868 return -1;
6869
6870 /* Rewind to start of frame */
6871 self->next_read_idx -= frame_len;
6872 return 0;
6873}
6874
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006875static PyObject *
6876load(UnpicklerObject *self)
6877{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006878 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006879 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006880
6881 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006882 self->stack->mark_set = 0;
6883 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006884 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006885 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006886 Pdata_clear(self->stack, 0);
6887
6888 /* Convenient macros for the dispatch while-switch loop just below. */
6889#define OP(opcode, load_func) \
6890 case opcode: if (load_func(self) < 0) break; continue;
6891
6892#define OP_ARG(opcode, load_func, arg) \
6893 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6894
6895 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006896 if (_Unpickler_Read(self, &s, 1) < 0) {
6897 PickleState *st = _Pickle_GetGlobalState();
6898 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6899 PyErr_Format(PyExc_EOFError, "Ran out of input");
6900 }
6901 return NULL;
6902 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006903
6904 switch ((enum opcode)s[0]) {
6905 OP(NONE, load_none)
6906 OP(BININT, load_binint)
6907 OP(BININT1, load_binint1)
6908 OP(BININT2, load_binint2)
6909 OP(INT, load_int)
6910 OP(LONG, load_long)
6911 OP_ARG(LONG1, load_counted_long, 1)
6912 OP_ARG(LONG4, load_counted_long, 4)
6913 OP(FLOAT, load_float)
6914 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006915 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6916 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6917 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
Antoine Pitrou91f43802019-05-26 17:10:09 +02006918 OP(BYTEARRAY8, load_counted_bytearray)
6919 OP(NEXT_BUFFER, load_next_buffer)
6920 OP(READONLY_BUFFER, load_readonly_buffer)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006921 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6922 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006923 OP(STRING, load_string)
6924 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006925 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6926 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6927 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006928 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6929 OP_ARG(TUPLE1, load_counted_tuple, 1)
6930 OP_ARG(TUPLE2, load_counted_tuple, 2)
6931 OP_ARG(TUPLE3, load_counted_tuple, 3)
6932 OP(TUPLE, load_tuple)
6933 OP(EMPTY_LIST, load_empty_list)
6934 OP(LIST, load_list)
6935 OP(EMPTY_DICT, load_empty_dict)
6936 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006937 OP(EMPTY_SET, load_empty_set)
6938 OP(ADDITEMS, load_additems)
6939 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006940 OP(OBJ, load_obj)
6941 OP(INST, load_inst)
6942 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006943 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006944 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006945 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006946 OP(APPEND, load_append)
6947 OP(APPENDS, load_appends)
6948 OP(BUILD, load_build)
6949 OP(DUP, load_dup)
6950 OP(BINGET, load_binget)
6951 OP(LONG_BINGET, load_long_binget)
6952 OP(GET, load_get)
6953 OP(MARK, load_mark)
6954 OP(BINPUT, load_binput)
6955 OP(LONG_BINPUT, load_long_binput)
6956 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006957 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006958 OP(POP, load_pop)
6959 OP(POP_MARK, load_pop_mark)
6960 OP(SETITEM, load_setitem)
6961 OP(SETITEMS, load_setitems)
6962 OP(PERSID, load_persid)
6963 OP(BINPERSID, load_binpersid)
6964 OP(REDUCE, load_reduce)
6965 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006966 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006967 OP_ARG(EXT1, load_extension, 1)
6968 OP_ARG(EXT2, load_extension, 2)
6969 OP_ARG(EXT4, load_extension, 4)
6970 OP_ARG(NEWTRUE, load_bool, Py_True)
6971 OP_ARG(NEWFALSE, load_bool, Py_False)
6972
6973 case STOP:
6974 break;
6975
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006976 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006977 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006978 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006979 unsigned char c = (unsigned char) *s;
6980 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6981 PyErr_Format(st->UnpicklingError,
6982 "invalid load key, '%c'.", c);
6983 }
6984 else {
6985 PyErr_Format(st->UnpicklingError,
6986 "invalid load key, '\\x%02x'.", c);
6987 }
6988 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006989 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006990 }
6991
6992 break; /* and we are done! */
6993 }
6994
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006995 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006996 return NULL;
6997 }
6998
Victor Stinner2ae57e32013-10-31 13:39:23 +01006999 if (_Unpickler_SkipConsumed(self) < 0)
7000 return NULL;
7001
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007002 PDATA_POP(self->stack, value);
7003 return value;
7004}
7005
Larry Hastings61272b72014-01-07 12:41:53 -08007006/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007007
7008_pickle.Unpickler.load
7009
7010Load a pickle.
7011
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007012Read a pickled object representation from the open file object given
7013in the constructor, and return the reconstituted object hierarchy
7014specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08007015[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007016
Larry Hastings3cceb382014-01-04 11:09:09 -08007017static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08007018_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08007019/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007020{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007021 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007022
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007023 /* Check whether the Unpickler was initialized correctly. This prevents
7024 segfaulting if a subclass overridden __init__ with a function that does
7025 not call Unpickler.__init__(). Here, we simply ensure that self->read
7026 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007027 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007028 PickleState *st = _Pickle_GetGlobalState();
7029 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007030 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007031 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007032 return NULL;
7033 }
7034
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007035 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007036}
7037
7038/* The name of find_class() is misleading. In newer pickle protocols, this
7039 function is used for loading any global (i.e., functions), not just
7040 classes. The name is kept only for backward compatibility. */
7041
Larry Hastings61272b72014-01-07 12:41:53 -08007042/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007043
7044_pickle.Unpickler.find_class
7045
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007046 module_name: object
7047 global_name: object
7048 /
7049
7050Return an object from a specified module.
7051
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007052If necessary, the module will be imported. Subclasses may override
7053this method (e.g. to restrict unpickling of arbitrary classes and
7054functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007055
7056This method is called whenever a class or a function object is
7057needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007058[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007059
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007060static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007061_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
7062 PyObject *module_name,
7063 PyObject *global_name)
7064/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007065{
7066 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007067 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007068
Steve Dowerb82e17e2019-05-23 08:45:22 -07007069 if (PySys_Audit("pickle.find_class", "OO",
7070 module_name, global_name) < 0) {
7071 return NULL;
7072 }
7073
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007074 /* Try to map the old names used in Python 2.x to the new ones used in
7075 Python 3.x. We do this only with old pickle protocols and when the
7076 user has not disabled the feature. */
7077 if (self->proto < 3 && self->fix_imports) {
7078 PyObject *key;
7079 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007080 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007081
7082 /* Check if the global (i.e., a function or a class) was renamed
7083 or moved to another module. */
7084 key = PyTuple_Pack(2, module_name, global_name);
7085 if (key == NULL)
7086 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007087 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007088 Py_DECREF(key);
7089 if (item) {
7090 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
7091 PyErr_Format(PyExc_RuntimeError,
7092 "_compat_pickle.NAME_MAPPING values should be "
7093 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
7094 return NULL;
7095 }
7096 module_name = PyTuple_GET_ITEM(item, 0);
7097 global_name = PyTuple_GET_ITEM(item, 1);
7098 if (!PyUnicode_Check(module_name) ||
7099 !PyUnicode_Check(global_name)) {
7100 PyErr_Format(PyExc_RuntimeError,
7101 "_compat_pickle.NAME_MAPPING values should be "
7102 "pairs of str, not (%.200s, %.200s)",
7103 Py_TYPE(module_name)->tp_name,
7104 Py_TYPE(global_name)->tp_name);
7105 return NULL;
7106 }
7107 }
7108 else if (PyErr_Occurred()) {
7109 return NULL;
7110 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03007111 else {
7112 /* Check if the module was renamed. */
7113 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
7114 if (item) {
7115 if (!PyUnicode_Check(item)) {
7116 PyErr_Format(PyExc_RuntimeError,
7117 "_compat_pickle.IMPORT_MAPPING values should be "
7118 "strings, not %.200s", Py_TYPE(item)->tp_name);
7119 return NULL;
7120 }
7121 module_name = item;
7122 }
7123 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007124 return NULL;
7125 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007126 }
7127 }
7128
tjb9004371c0a2019-02-18 23:30:51 +08007129 /*
7130 * we don't use PyImport_GetModule here, because it can return partially-
7131 * initialised modules, which then cause the getattribute to fail.
7132 */
7133 module = PyImport_Import(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007134 if (module == NULL) {
tjb9004371c0a2019-02-18 23:30:51 +08007135 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007136 }
Eric Snow3f9eee62017-09-15 16:35:20 -06007137 global = getattribute(module, global_name, self->proto >= 4);
7138 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007139 return global;
7140}
7141
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02007142/*[clinic input]
7143
7144_pickle.Unpickler.__sizeof__ -> Py_ssize_t
7145
7146Returns size in memory, in bytes.
7147[clinic start generated code]*/
7148
7149static Py_ssize_t
7150_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
7151/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
7152{
7153 Py_ssize_t res;
7154
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02007155 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02007156 if (self->memo != NULL)
7157 res += self->memo_size * sizeof(PyObject *);
7158 if (self->marks != NULL)
7159 res += self->marks_size * sizeof(Py_ssize_t);
7160 if (self->input_line != NULL)
7161 res += strlen(self->input_line) + 1;
7162 if (self->encoding != NULL)
7163 res += strlen(self->encoding) + 1;
7164 if (self->errors != NULL)
7165 res += strlen(self->errors) + 1;
7166 return res;
7167}
7168
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007169static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007170 _PICKLE_UNPICKLER_LOAD_METHODDEF
7171 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02007172 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007173 {NULL, NULL} /* sentinel */
7174};
7175
7176static void
7177Unpickler_dealloc(UnpicklerObject *self)
7178{
7179 PyObject_GC_UnTrack((PyObject *)self);
7180 Py_XDECREF(self->readline);
Antoine Pitrou91f43802019-05-26 17:10:09 +02007181 Py_XDECREF(self->readinto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007182 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00007183 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007184 Py_XDECREF(self->stack);
7185 Py_XDECREF(self->pers_func);
Antoine Pitrou91f43802019-05-26 17:10:09 +02007186 Py_XDECREF(self->buffers);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007187 if (self->buffer.buf != NULL) {
7188 PyBuffer_Release(&self->buffer);
7189 self->buffer.buf = NULL;
7190 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007191
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007192 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007193 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007194 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02007195 PyMem_Free(self->encoding);
7196 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007197
7198 Py_TYPE(self)->tp_free((PyObject *)self);
7199}
7200
7201static int
7202Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
7203{
7204 Py_VISIT(self->readline);
Antoine Pitrou91f43802019-05-26 17:10:09 +02007205 Py_VISIT(self->readinto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007206 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00007207 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007208 Py_VISIT(self->stack);
7209 Py_VISIT(self->pers_func);
Antoine Pitrou91f43802019-05-26 17:10:09 +02007210 Py_VISIT(self->buffers);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007211 return 0;
7212}
7213
7214static int
7215Unpickler_clear(UnpicklerObject *self)
7216{
7217 Py_CLEAR(self->readline);
Antoine Pitrou91f43802019-05-26 17:10:09 +02007218 Py_CLEAR(self->readinto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007219 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00007220 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007221 Py_CLEAR(self->stack);
7222 Py_CLEAR(self->pers_func);
Antoine Pitrou91f43802019-05-26 17:10:09 +02007223 Py_CLEAR(self->buffers);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007224 if (self->buffer.buf != NULL) {
7225 PyBuffer_Release(&self->buffer);
7226 self->buffer.buf = NULL;
7227 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007228
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007229 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007230 PyMem_Free(self->marks);
7231 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007232 PyMem_Free(self->input_line);
7233 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02007234 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007235 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02007236 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007237 self->errors = NULL;
7238
7239 return 0;
7240}
7241
Larry Hastings61272b72014-01-07 12:41:53 -08007242/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007243
7244_pickle.Unpickler.__init__
7245
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007246 file: object
7247 *
7248 fix_imports: bool = True
7249 encoding: str = 'ASCII'
7250 errors: str = 'strict'
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007251 buffers: object(c_default="NULL") = ()
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007252
7253This takes a binary file for reading a pickle data stream.
7254
7255The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007256protocol argument is needed. Bytes past the pickled object's
7257representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007258
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007259The argument *file* must have two methods, a read() method that takes
7260an integer argument, and a readline() method that requires no
7261arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007262binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007263other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007264
7265Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007266which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007267generated by Python 2. If *fix_imports* is True, pickle will try to
7268map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007269*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007270instances pickled by Python 2; these default to 'ASCII' and 'strict',
7271respectively. The *encoding* can be 'bytes' to read these 8-bit
7272string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007273[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007274
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007275static int
Larry Hastings89964c42015-04-14 18:07:59 -04007276_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
7277 int fix_imports, const char *encoding,
Antoine Pitrou91f43802019-05-26 17:10:09 +02007278 const char *errors, PyObject *buffers)
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007279/*[clinic end generated code: output=09f0192649ea3f85 input=ca4c1faea9553121]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007280{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02007281 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007282
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007283 /* In case of multiple __init__() calls, clear previous content. */
7284 if (self->read != NULL)
7285 (void)Unpickler_clear(self);
7286
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007287 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007288 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007289
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007290 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007291 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007292
Antoine Pitrou91f43802019-05-26 17:10:09 +02007293 if (_Unpickler_SetBuffers(self, buffers) < 0)
7294 return -1;
7295
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007296 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007297
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007298 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
7299 &self->pers_func, &self->pers_func_self) < 0)
7300 {
7301 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007302 }
7303
7304 self->stack = (Pdata *)Pdata_New();
7305 if (self->stack == NULL)
Zackery Spytz4b430e52018-09-28 23:48:46 -06007306 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007307
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007308 self->memo_size = 32;
7309 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007310 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007311 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007312
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007313 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00007314
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007315 return 0;
7316}
7317
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007318
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007319/* Define a proxy object for the Unpickler's internal memo object. This is to
7320 * avoid breaking code like:
7321 * unpickler.memo.clear()
7322 * and
7323 * unpickler.memo = saved_memo
7324 * Is this a good idea? Not really, but we don't want to break code that uses
7325 * it. Note that we don't implement the entire mapping API here. This is
7326 * intentional, as these should be treated as black-box implementation details.
7327 *
7328 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02007329 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007330 */
7331
Larry Hastings61272b72014-01-07 12:41:53 -08007332/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007333_pickle.UnpicklerMemoProxy.clear
7334
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007335Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08007336[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007337
Larry Hastings3cceb382014-01-04 11:09:09 -08007338static PyObject *
7339_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08007340/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007341{
7342 _Unpickler_MemoCleanup(self->unpickler);
7343 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
7344 if (self->unpickler->memo == NULL)
7345 return NULL;
7346 Py_RETURN_NONE;
7347}
7348
Larry Hastings61272b72014-01-07 12:41:53 -08007349/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007350_pickle.UnpicklerMemoProxy.copy
7351
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007352Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08007353[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007354
Larry Hastings3cceb382014-01-04 11:09:09 -08007355static PyObject *
7356_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08007357/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007358{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007359 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007360 PyObject *new_memo = PyDict_New();
7361 if (new_memo == NULL)
7362 return NULL;
7363
7364 for (i = 0; i < self->unpickler->memo_size; i++) {
7365 int status;
7366 PyObject *key, *value;
7367
7368 value = self->unpickler->memo[i];
7369 if (value == NULL)
7370 continue;
7371
7372 key = PyLong_FromSsize_t(i);
7373 if (key == NULL)
7374 goto error;
7375 status = PyDict_SetItem(new_memo, key, value);
7376 Py_DECREF(key);
7377 if (status < 0)
7378 goto error;
7379 }
7380 return new_memo;
7381
7382error:
7383 Py_DECREF(new_memo);
7384 return NULL;
7385}
7386
Larry Hastings61272b72014-01-07 12:41:53 -08007387/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007388_pickle.UnpicklerMemoProxy.__reduce__
7389
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007390Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08007391[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007392
Larry Hastings3cceb382014-01-04 11:09:09 -08007393static PyObject *
7394_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08007395/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007396{
7397 PyObject *reduce_value;
7398 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08007399 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007400 if (contents == NULL)
7401 return NULL;
7402
7403 reduce_value = PyTuple_New(2);
7404 if (reduce_value == NULL) {
7405 Py_DECREF(contents);
7406 return NULL;
7407 }
7408 constructor_args = PyTuple_New(1);
7409 if (constructor_args == NULL) {
7410 Py_DECREF(contents);
7411 Py_DECREF(reduce_value);
7412 return NULL;
7413 }
7414 PyTuple_SET_ITEM(constructor_args, 0, contents);
7415 Py_INCREF((PyObject *)&PyDict_Type);
7416 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
7417 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
7418 return reduce_value;
7419}
7420
7421static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007422 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
7423 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
7424 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007425 {NULL, NULL} /* sentinel */
7426};
7427
7428static void
7429UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
7430{
7431 PyObject_GC_UnTrack(self);
7432 Py_XDECREF(self->unpickler);
7433 PyObject_GC_Del((PyObject *)self);
7434}
7435
7436static int
7437UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
7438 visitproc visit, void *arg)
7439{
7440 Py_VISIT(self->unpickler);
7441 return 0;
7442}
7443
7444static int
7445UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
7446{
7447 Py_CLEAR(self->unpickler);
7448 return 0;
7449}
7450
7451static PyTypeObject UnpicklerMemoProxyType = {
7452 PyVarObject_HEAD_INIT(NULL, 0)
7453 "_pickle.UnpicklerMemoProxy", /*tp_name*/
7454 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
7455 0,
7456 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02007457 0, /* tp_vectorcall_offset */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007458 0, /* tp_getattr */
7459 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02007460 0, /* tp_as_async */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007461 0, /* tp_repr */
7462 0, /* tp_as_number */
7463 0, /* tp_as_sequence */
7464 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00007465 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007466 0, /* tp_call */
7467 0, /* tp_str */
7468 PyObject_GenericGetAttr, /* tp_getattro */
7469 PyObject_GenericSetAttr, /* tp_setattro */
7470 0, /* tp_as_buffer */
7471 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
7472 0, /* tp_doc */
7473 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
7474 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
7475 0, /* tp_richcompare */
7476 0, /* tp_weaklistoffset */
7477 0, /* tp_iter */
7478 0, /* tp_iternext */
7479 unpicklerproxy_methods, /* tp_methods */
7480};
7481
7482static PyObject *
7483UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
7484{
7485 UnpicklerMemoProxyObject *self;
7486
7487 self = PyObject_GC_New(UnpicklerMemoProxyObject,
7488 &UnpicklerMemoProxyType);
7489 if (self == NULL)
7490 return NULL;
7491 Py_INCREF(unpickler);
7492 self->unpickler = unpickler;
7493 PyObject_GC_Track(self);
7494 return (PyObject *)self;
7495}
7496
7497/*****************************************************************************/
7498
7499
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007500static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007501Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007502{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007503 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007504}
7505
7506static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007507Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007508{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007509 PyObject **new_memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007510 size_t new_memo_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007511
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007512 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007513 PyErr_SetString(PyExc_TypeError,
7514 "attribute deletion is not supported");
7515 return -1;
7516 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007517
7518 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7519 UnpicklerObject *unpickler =
7520 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7521
7522 new_memo_size = unpickler->memo_size;
7523 new_memo = _Unpickler_NewMemo(new_memo_size);
7524 if (new_memo == NULL)
7525 return -1;
7526
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007527 for (size_t i = 0; i < new_memo_size; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007528 Py_XINCREF(unpickler->memo[i]);
7529 new_memo[i] = unpickler->memo[i];
7530 }
7531 }
7532 else if (PyDict_Check(obj)) {
7533 Py_ssize_t i = 0;
7534 PyObject *key, *value;
7535
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007536 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007537 new_memo = _Unpickler_NewMemo(new_memo_size);
7538 if (new_memo == NULL)
7539 return -1;
7540
7541 while (PyDict_Next(obj, &i, &key, &value)) {
7542 Py_ssize_t idx;
7543 if (!PyLong_Check(key)) {
7544 PyErr_SetString(PyExc_TypeError,
7545 "memo key must be integers");
7546 goto error;
7547 }
7548 idx = PyLong_AsSsize_t(key);
7549 if (idx == -1 && PyErr_Occurred())
7550 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007551 if (idx < 0) {
7552 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007553 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007554 goto error;
7555 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007556 if (_Unpickler_MemoPut(self, idx, value) < 0)
7557 goto error;
7558 }
7559 }
7560 else {
7561 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +02007562 "'memo' attribute must be an UnpicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007563 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007564 return -1;
7565 }
7566
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007567 _Unpickler_MemoCleanup(self);
7568 self->memo_size = new_memo_size;
7569 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007570
7571 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007572
7573 error:
7574 if (new_memo_size) {
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007575 for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007576 Py_XDECREF(new_memo[i]);
7577 }
7578 PyMem_FREE(new_memo);
7579 }
7580 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007581}
7582
7583static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007584Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007585{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007586 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007587 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007588 return NULL;
7589 }
7590 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007591}
7592
7593static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007594Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007595{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007596 if (value == NULL) {
7597 PyErr_SetString(PyExc_TypeError,
7598 "attribute deletion is not supported");
7599 return -1;
7600 }
7601 if (!PyCallable_Check(value)) {
7602 PyErr_SetString(PyExc_TypeError,
7603 "persistent_load must be a callable taking "
7604 "one argument");
7605 return -1;
7606 }
7607
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007608 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007609 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007610 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007611
7612 return 0;
7613}
7614
7615static PyGetSetDef Unpickler_getsets[] = {
7616 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7617 {"persistent_load", (getter)Unpickler_get_persload,
7618 (setter)Unpickler_set_persload},
7619 {NULL}
7620};
7621
7622static PyTypeObject Unpickler_Type = {
7623 PyVarObject_HEAD_INIT(NULL, 0)
7624 "_pickle.Unpickler", /*tp_name*/
7625 sizeof(UnpicklerObject), /*tp_basicsize*/
7626 0, /*tp_itemsize*/
7627 (destructor)Unpickler_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02007628 0, /*tp_vectorcall_offset*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007629 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007630 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02007631 0, /*tp_as_async*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007632 0, /*tp_repr*/
7633 0, /*tp_as_number*/
7634 0, /*tp_as_sequence*/
7635 0, /*tp_as_mapping*/
7636 0, /*tp_hash*/
7637 0, /*tp_call*/
7638 0, /*tp_str*/
7639 0, /*tp_getattro*/
7640 0, /*tp_setattro*/
7641 0, /*tp_as_buffer*/
7642 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007643 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007644 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7645 (inquiry)Unpickler_clear, /*tp_clear*/
7646 0, /*tp_richcompare*/
7647 0, /*tp_weaklistoffset*/
7648 0, /*tp_iter*/
7649 0, /*tp_iternext*/
7650 Unpickler_methods, /*tp_methods*/
7651 0, /*tp_members*/
7652 Unpickler_getsets, /*tp_getset*/
7653 0, /*tp_base*/
7654 0, /*tp_dict*/
7655 0, /*tp_descr_get*/
7656 0, /*tp_descr_set*/
7657 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007658 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007659 PyType_GenericAlloc, /*tp_alloc*/
7660 PyType_GenericNew, /*tp_new*/
7661 PyObject_GC_Del, /*tp_free*/
7662 0, /*tp_is_gc*/
7663};
7664
Larry Hastings61272b72014-01-07 12:41:53 -08007665/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007666
7667_pickle.dump
7668
7669 obj: object
7670 file: object
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007671 protocol: object = None
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007672 *
7673 fix_imports: bool = True
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007674 buffer_callback: object = None
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007675
7676Write a pickled representation of obj to the open file object file.
7677
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007678This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7679be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007680
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007681The optional *protocol* argument tells the pickler to use the given
Miss Islington (bot)a46728a2020-01-24 02:20:35 -08007682protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
7683protocol is 4. It was introduced in Python 3.4, and is incompatible
Łukasz Langac51d8c92018-04-03 23:06:53 -07007684with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007685
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007686Specifying a negative protocol version selects the highest protocol
7687version supported. The higher the protocol used, the more recent the
7688version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007689
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007690The *file* argument must have a write() method that accepts a single
7691bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007692writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007693this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007694
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007695If *fix_imports* is True and protocol is less than 3, pickle will try
7696to map the new Python 3 names to the old module names used in Python
76972, so that the pickle data stream is readable with Python 2.
Antoine Pitrou91f43802019-05-26 17:10:09 +02007698
7699If *buffer_callback* is None (the default), buffer views are serialized
7700into *file* as part of the pickle stream. It is an error if
7701*buffer_callback* is not None and *protocol* is None or smaller than 5.
7702
Larry Hastings61272b72014-01-07 12:41:53 -08007703[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007704
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007706_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Antoine Pitrou91f43802019-05-26 17:10:09 +02007707 PyObject *protocol, int fix_imports,
7708 PyObject *buffer_callback)
Miss Islington (bot)a46728a2020-01-24 02:20:35 -08007709/*[clinic end generated code: output=706186dba996490c input=5ed6653da99cd97c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007710{
7711 PicklerObject *pickler = _Pickler_New();
7712
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007713 if (pickler == NULL)
7714 return NULL;
7715
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007716 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007717 goto error;
7718
7719 if (_Pickler_SetOutputStream(pickler, file) < 0)
7720 goto error;
7721
Antoine Pitrou91f43802019-05-26 17:10:09 +02007722 if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0)
7723 goto error;
7724
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007725 if (dump(pickler, obj) < 0)
7726 goto error;
7727
7728 if (_Pickler_FlushToFile(pickler) < 0)
7729 goto error;
7730
7731 Py_DECREF(pickler);
7732 Py_RETURN_NONE;
7733
7734 error:
7735 Py_XDECREF(pickler);
7736 return NULL;
7737}
7738
Larry Hastings61272b72014-01-07 12:41:53 -08007739/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007740
7741_pickle.dumps
7742
7743 obj: object
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007744 protocol: object = None
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007745 *
7746 fix_imports: bool = True
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007747 buffer_callback: object = None
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007748
7749Return the pickled representation of the object as a bytes object.
7750
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007751The optional *protocol* argument tells the pickler to use the given
Miss Islington (bot)a46728a2020-01-24 02:20:35 -08007752protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
7753protocol is 4. It was introduced in Python 3.4, and is incompatible
Łukasz Langac51d8c92018-04-03 23:06:53 -07007754with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007755
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007756Specifying a negative protocol version selects the highest protocol
7757version supported. The higher the protocol used, the more recent the
7758version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007759
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007760If *fix_imports* is True and *protocol* is less than 3, pickle will
7761try to map the new Python 3 names to the old module names used in
7762Python 2, so that the pickle data stream is readable with Python 2.
Antoine Pitrou91f43802019-05-26 17:10:09 +02007763
7764If *buffer_callback* is None (the default), buffer views are serialized
7765into *file* as part of the pickle stream. It is an error if
7766*buffer_callback* is not None and *protocol* is None or smaller than 5.
7767
Larry Hastings61272b72014-01-07 12:41:53 -08007768[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007769
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007770static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007771_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Antoine Pitrou91f43802019-05-26 17:10:09 +02007772 int fix_imports, PyObject *buffer_callback)
Miss Islington (bot)a46728a2020-01-24 02:20:35 -08007773/*[clinic end generated code: output=fbab0093a5580fdf input=e543272436c6f987]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007774{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007775 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007776 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007777
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007778 if (pickler == NULL)
7779 return NULL;
7780
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007781 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007782 goto error;
7783
Antoine Pitrou91f43802019-05-26 17:10:09 +02007784 if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0)
7785 goto error;
7786
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007787 if (dump(pickler, obj) < 0)
7788 goto error;
7789
7790 result = _Pickler_GetString(pickler);
7791 Py_DECREF(pickler);
7792 return result;
7793
7794 error:
7795 Py_XDECREF(pickler);
7796 return NULL;
7797}
7798
Larry Hastings61272b72014-01-07 12:41:53 -08007799/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007800
7801_pickle.load
7802
7803 file: object
7804 *
7805 fix_imports: bool = True
7806 encoding: str = 'ASCII'
7807 errors: str = 'strict'
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007808 buffers: object(c_default="NULL") = ()
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007809
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007810Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007811
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007812This is equivalent to ``Unpickler(file).load()``, but may be more
7813efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007814
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007815The protocol version of the pickle is detected automatically, so no
7816protocol argument is needed. Bytes past the pickled object's
7817representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007818
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007819The argument *file* must have two methods, a read() method that takes
7820an integer argument, and a readline() method that requires no
7821arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007822binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007823other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007824
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007825Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007826which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007827generated by Python 2. If *fix_imports* is True, pickle will try to
7828map the old Python 2 names to the new names used in Python 3. The
7829*encoding* and *errors* tell pickle how to decode 8-bit string
7830instances pickled by Python 2; these default to 'ASCII' and 'strict',
7831respectively. The *encoding* can be 'bytes' to read these 8-bit
7832string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007833[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007834
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007835static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007836_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Antoine Pitrou91f43802019-05-26 17:10:09 +02007837 const char *encoding, const char *errors,
7838 PyObject *buffers)
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007839/*[clinic end generated code: output=250452d141c23e76 input=46c7c31c92f4f371]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007840{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007841 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007842 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007843
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007844 if (unpickler == NULL)
7845 return NULL;
7846
7847 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7848 goto error;
7849
7850 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7851 goto error;
7852
Antoine Pitrou91f43802019-05-26 17:10:09 +02007853 if (_Unpickler_SetBuffers(unpickler, buffers) < 0)
7854 goto error;
7855
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007856 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007857
7858 result = load(unpickler);
7859 Py_DECREF(unpickler);
7860 return result;
7861
7862 error:
7863 Py_XDECREF(unpickler);
7864 return NULL;
7865}
7866
Larry Hastings61272b72014-01-07 12:41:53 -08007867/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007868
7869_pickle.loads
7870
7871 data: object
7872 *
7873 fix_imports: bool = True
7874 encoding: str = 'ASCII'
7875 errors: str = 'strict'
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007876 buffers: object(c_default="NULL") = ()
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007877
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007878Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007879
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007880The protocol version of the pickle is detected automatically, so no
7881protocol argument is needed. Bytes past the pickled object's
7882representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007883
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007884Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007885which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007886generated by Python 2. If *fix_imports* is True, pickle will try to
7887map the old Python 2 names to the new names used in Python 3. The
7888*encoding* and *errors* tell pickle how to decode 8-bit string
7889instances pickled by Python 2; these default to 'ASCII' and 'strict',
7890respectively. The *encoding* can be 'bytes' to read these 8-bit
7891string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007892[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007893
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007894static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007895_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Antoine Pitrou91f43802019-05-26 17:10:09 +02007896 const char *encoding, const char *errors,
7897 PyObject *buffers)
Serhiy Storchakad322abb2019-09-14 13:31:50 +03007898/*[clinic end generated code: output=82ac1e6b588e6d02 input=9c2ab6a0960185ea]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007899{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007900 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007901 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007902
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007903 if (unpickler == NULL)
7904 return NULL;
7905
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007906 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007907 goto error;
7908
7909 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7910 goto error;
7911
Antoine Pitrou91f43802019-05-26 17:10:09 +02007912 if (_Unpickler_SetBuffers(unpickler, buffers) < 0)
7913 goto error;
7914
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007915 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007916
7917 result = load(unpickler);
7918 Py_DECREF(unpickler);
7919 return result;
7920
7921 error:
7922 Py_XDECREF(unpickler);
7923 return NULL;
7924}
7925
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007926static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007927 _PICKLE_DUMP_METHODDEF
7928 _PICKLE_DUMPS_METHODDEF
7929 _PICKLE_LOAD_METHODDEF
7930 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007931 {NULL, NULL} /* sentinel */
7932};
7933
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007934static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007935pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007936{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007937 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007938 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007939}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007940
Stefan Krahf483b0f2013-12-14 13:43:10 +01007941static void
7942pickle_free(PyObject *m)
7943{
7944 _Pickle_ClearState(_Pickle_GetState(m));
7945}
7946
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007947static int
7948pickle_traverse(PyObject *m, visitproc visit, void *arg)
7949{
7950 PickleState *st = _Pickle_GetState(m);
7951 Py_VISIT(st->PickleError);
7952 Py_VISIT(st->PicklingError);
7953 Py_VISIT(st->UnpicklingError);
7954 Py_VISIT(st->dispatch_table);
7955 Py_VISIT(st->extension_registry);
7956 Py_VISIT(st->extension_cache);
7957 Py_VISIT(st->inverted_registry);
7958 Py_VISIT(st->name_mapping_2to3);
7959 Py_VISIT(st->import_mapping_2to3);
7960 Py_VISIT(st->name_mapping_3to2);
7961 Py_VISIT(st->import_mapping_3to2);
7962 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007963 Py_VISIT(st->getattr);
Miss Islington (bot)5f2ade22020-03-01 22:47:23 -08007964 Py_VISIT(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007965 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007966}
7967
7968static struct PyModuleDef _picklemodule = {
7969 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007970 "_pickle", /* m_name */
7971 pickle_module_doc, /* m_doc */
7972 sizeof(PickleState), /* m_size */
7973 pickle_methods, /* m_methods */
7974 NULL, /* m_reload */
7975 pickle_traverse, /* m_traverse */
7976 pickle_clear, /* m_clear */
7977 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007978};
7979
7980PyMODINIT_FUNC
7981PyInit__pickle(void)
7982{
7983 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007984 PickleState *st;
7985
7986 m = PyState_FindModule(&_picklemodule);
7987 if (m) {
7988 Py_INCREF(m);
7989 return m;
7990 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007991
7992 if (PyType_Ready(&Unpickler_Type) < 0)
7993 return NULL;
7994 if (PyType_Ready(&Pickler_Type) < 0)
7995 return NULL;
7996 if (PyType_Ready(&Pdata_Type) < 0)
7997 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007998 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7999 return NULL;
8000 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
8001 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008002
8003 /* Create the module and add the functions. */
8004 m = PyModule_Create(&_picklemodule);
8005 if (m == NULL)
8006 return NULL;
8007
Antoine Pitrou91f43802019-05-26 17:10:09 +02008008 /* Add types */
Antoine Pitrou8391cf42011-07-15 21:01:21 +02008009 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008010 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
8011 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02008012 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008013 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
8014 return NULL;
Antoine Pitrou91f43802019-05-26 17:10:09 +02008015 Py_INCREF(&PyPickleBuffer_Type);
8016 if (PyModule_AddObject(m, "PickleBuffer",
8017 (PyObject *)&PyPickleBuffer_Type) < 0)
8018 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008019
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08008020 st = _Pickle_GetState(m);
8021
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008022 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08008023 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
8024 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008025 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08008026 st->PicklingError = \
8027 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
8028 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008029 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08008030 st->UnpicklingError = \
8031 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
8032 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008033 return NULL;
8034
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08008035 Py_INCREF(st->PickleError);
8036 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008037 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08008038 Py_INCREF(st->PicklingError);
8039 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008040 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08008041 Py_INCREF(st->UnpicklingError);
8042 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008043 return NULL;
8044
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08008045 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008046 return NULL;
8047
8048 return m;
8049}