blob: 2b97294e1e86f803a45bf5e4779d3a1b4c420d84 [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001
2/* Core extension modules are built-in on some platforms (e.g. Windows). */
3#ifdef Py_BUILD_CORE
Eric Snowfc1bf872017-09-11 18:30:43 -07004#define Py_BUILD_CORE_BUILTIN
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#undef Py_BUILD_CORE
6#endif
7
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00008#include "Python.h"
9#include "structmember.h"
10
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -080011PyDoc_STRVAR(pickle_module_doc,
12"Optimized C implementation for the Python pickle module.");
13
Larry Hastings61272b72014-01-07 12:41:53 -080014/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080016class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
17class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
18class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
19class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080020[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080022
Łukasz Langac51d8c92018-04-03 23:06:53 -070023/* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol.
24 Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python
25 already includes it. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000026enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010027 HIGHEST_PROTOCOL = 4,
Łukasz Langac51d8c92018-04-03 23:06:53 -070028 DEFAULT_PROTOCOL = 4
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029};
30
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000031/* Pickle opcodes. These must be kept updated with pickle.py.
32 Extensive docs are in pickletools.py. */
33enum opcode {
34 MARK = '(',
35 STOP = '.',
36 POP = '0',
37 POP_MARK = '1',
38 DUP = '2',
39 FLOAT = 'F',
40 INT = 'I',
41 BININT = 'J',
42 BININT1 = 'K',
43 LONG = 'L',
44 BININT2 = 'M',
45 NONE = 'N',
46 PERSID = 'P',
47 BINPERSID = 'Q',
48 REDUCE = 'R',
49 STRING = 'S',
50 BINSTRING = 'T',
51 SHORT_BINSTRING = 'U',
52 UNICODE = 'V',
53 BINUNICODE = 'X',
54 APPEND = 'a',
55 BUILD = 'b',
56 GLOBAL = 'c',
57 DICT = 'd',
58 EMPTY_DICT = '}',
59 APPENDS = 'e',
60 GET = 'g',
61 BINGET = 'h',
62 INST = 'i',
63 LONG_BINGET = 'j',
64 LIST = 'l',
65 EMPTY_LIST = ']',
66 OBJ = 'o',
67 PUT = 'p',
68 BINPUT = 'q',
69 LONG_BINPUT = 'r',
70 SETITEM = 's',
71 TUPLE = 't',
72 EMPTY_TUPLE = ')',
73 SETITEMS = 'u',
74 BINFLOAT = 'G',
75
76 /* Protocol 2. */
77 PROTO = '\x80',
78 NEWOBJ = '\x81',
79 EXT1 = '\x82',
80 EXT2 = '\x83',
81 EXT4 = '\x84',
82 TUPLE1 = '\x85',
83 TUPLE2 = '\x86',
84 TUPLE3 = '\x87',
85 NEWTRUE = '\x88',
86 NEWFALSE = '\x89',
87 LONG1 = '\x8a',
88 LONG4 = '\x8b',
89
90 /* Protocol 3 (Python 3.x) */
91 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010092 SHORT_BINBYTES = 'C',
93
94 /* Protocol 4 */
95 SHORT_BINUNICODE = '\x8c',
96 BINUNICODE8 = '\x8d',
97 BINBYTES8 = '\x8e',
98 EMPTY_SET = '\x8f',
99 ADDITEMS = '\x90',
100 FROZENSET = '\x91',
101 NEWOBJ_EX = '\x92',
102 STACK_GLOBAL = '\x93',
103 MEMOIZE = '\x94',
104 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000105};
106
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000107enum {
108 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
109 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
110 break if this gets out of synch with pickle.py, but it's unclear that would
111 help anything either. */
112 BATCHSIZE = 1000,
113
114 /* Nesting limit until Pickler, when running in "fast mode", starts
115 checking for self-referential data-structures. */
116 FAST_NESTING_LIMIT = 50,
117
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000118 /* Initial size of the write buffer of Pickler. */
119 WRITE_BUF_SIZE = 4096,
120
Antoine Pitrou04248a82010-10-12 20:51:21 +0000121 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100122 PREFETCH = 8192 * 16,
123
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200124 FRAME_SIZE_MIN = 4,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100125 FRAME_SIZE_TARGET = 64 * 1024,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100126 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000127};
128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000130
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800131/* State of the pickle module, per PEP 3121. */
132typedef struct {
133 /* Exception classes for pickle. */
134 PyObject *PickleError;
135 PyObject *PicklingError;
136 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800137
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800138 /* copyreg.dispatch_table, {type_object: pickling_function} */
139 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000142
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800143 /* copyreg._extension_registry, {(module_name, function_name): code} */
144 PyObject *extension_registry;
145 /* copyreg._extension_cache, {code: object} */
146 PyObject *extension_cache;
147 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
148 PyObject *inverted_registry;
149
150 /* Import mappings for compatibility with Python 2.x */
151
152 /* _compat_pickle.NAME_MAPPING,
153 {(oldmodule, oldname): (newmodule, newname)} */
154 PyObject *name_mapping_2to3;
155 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
156 PyObject *import_mapping_2to3;
157 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
158 PyObject *name_mapping_3to2;
159 PyObject *import_mapping_3to2;
160
161 /* codecs.encode, used for saving bytes in older protocols */
162 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300163 /* builtins.getattr, used for saving nested names with protocol < 4 */
164 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300165 /* functools.partial, used for implementing __newobj_ex__ with protocols
166 2 and 3 */
167 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800168} PickleState;
169
170/* Forward declaration of the _pickle module definition. */
171static struct PyModuleDef _picklemodule;
172
173/* Given a module object, get its per-module state. */
174static PickleState *
175_Pickle_GetState(PyObject *module)
176{
177 return (PickleState *)PyModule_GetState(module);
178}
179
180/* Find the module instance imported in the currently running sub-interpreter
181 and get its state. */
182static PickleState *
183_Pickle_GetGlobalState(void)
184{
185 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
186}
187
188/* Clear the given pickle module state. */
189static void
190_Pickle_ClearState(PickleState *st)
191{
192 Py_CLEAR(st->PickleError);
193 Py_CLEAR(st->PicklingError);
194 Py_CLEAR(st->UnpicklingError);
195 Py_CLEAR(st->dispatch_table);
196 Py_CLEAR(st->extension_registry);
197 Py_CLEAR(st->extension_cache);
198 Py_CLEAR(st->inverted_registry);
199 Py_CLEAR(st->name_mapping_2to3);
200 Py_CLEAR(st->import_mapping_2to3);
201 Py_CLEAR(st->name_mapping_3to2);
202 Py_CLEAR(st->import_mapping_3to2);
203 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300204 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100205 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800206}
207
208/* Initialize the given pickle module state. */
209static int
210_Pickle_InitState(PickleState *st)
211{
212 PyObject *copyreg = NULL;
213 PyObject *compat_pickle = NULL;
214 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300215 PyObject *functools = NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200216 _Py_IDENTIFIER(getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800217
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200218 st->getattr = _PyEval_GetBuiltinId(&PyId_getattr);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300219 if (st->getattr == NULL)
220 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300221
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800222 copyreg = PyImport_ImportModule("copyreg");
223 if (!copyreg)
224 goto error;
225 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
226 if (!st->dispatch_table)
227 goto error;
228 if (!PyDict_CheckExact(st->dispatch_table)) {
229 PyErr_Format(PyExc_RuntimeError,
230 "copyreg.dispatch_table should be a dict, not %.200s",
231 Py_TYPE(st->dispatch_table)->tp_name);
232 goto error;
233 }
234 st->extension_registry = \
235 PyObject_GetAttrString(copyreg, "_extension_registry");
236 if (!st->extension_registry)
237 goto error;
238 if (!PyDict_CheckExact(st->extension_registry)) {
239 PyErr_Format(PyExc_RuntimeError,
240 "copyreg._extension_registry should be a dict, "
241 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
242 goto error;
243 }
244 st->inverted_registry = \
245 PyObject_GetAttrString(copyreg, "_inverted_registry");
246 if (!st->inverted_registry)
247 goto error;
248 if (!PyDict_CheckExact(st->inverted_registry)) {
249 PyErr_Format(PyExc_RuntimeError,
250 "copyreg._inverted_registry should be a dict, "
251 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
252 goto error;
253 }
254 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
255 if (!st->extension_cache)
256 goto error;
257 if (!PyDict_CheckExact(st->extension_cache)) {
258 PyErr_Format(PyExc_RuntimeError,
259 "copyreg._extension_cache should be a dict, "
260 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
261 goto error;
262 }
263 Py_CLEAR(copyreg);
264
265 /* Load the 2.x -> 3.x stdlib module mapping tables */
266 compat_pickle = PyImport_ImportModule("_compat_pickle");
267 if (!compat_pickle)
268 goto error;
269 st->name_mapping_2to3 = \
270 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
271 if (!st->name_mapping_2to3)
272 goto error;
273 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
274 PyErr_Format(PyExc_RuntimeError,
275 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
276 Py_TYPE(st->name_mapping_2to3)->tp_name);
277 goto error;
278 }
279 st->import_mapping_2to3 = \
280 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
281 if (!st->import_mapping_2to3)
282 goto error;
283 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
284 PyErr_Format(PyExc_RuntimeError,
285 "_compat_pickle.IMPORT_MAPPING should be a dict, "
286 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
287 goto error;
288 }
289 /* ... and the 3.x -> 2.x mapping tables */
290 st->name_mapping_3to2 = \
291 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
292 if (!st->name_mapping_3to2)
293 goto error;
294 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
295 PyErr_Format(PyExc_RuntimeError,
296 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
297 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
298 goto error;
299 }
300 st->import_mapping_3to2 = \
301 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
302 if (!st->import_mapping_3to2)
303 goto error;
304 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
305 PyErr_Format(PyExc_RuntimeError,
306 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
307 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
308 goto error;
309 }
310 Py_CLEAR(compat_pickle);
311
312 codecs = PyImport_ImportModule("codecs");
313 if (codecs == NULL)
314 goto error;
315 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
316 if (st->codecs_encode == NULL) {
317 goto error;
318 }
319 if (!PyCallable_Check(st->codecs_encode)) {
320 PyErr_Format(PyExc_RuntimeError,
321 "codecs.encode should be a callable, not %.200s",
322 Py_TYPE(st->codecs_encode)->tp_name);
323 goto error;
324 }
325 Py_CLEAR(codecs);
326
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300327 functools = PyImport_ImportModule("functools");
328 if (!functools)
329 goto error;
330 st->partial = PyObject_GetAttrString(functools, "partial");
331 if (!st->partial)
332 goto error;
333 Py_CLEAR(functools);
334
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800335 return 0;
336
337 error:
338 Py_CLEAR(copyreg);
339 Py_CLEAR(compat_pickle);
340 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300341 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800342 _Pickle_ClearState(st);
343 return -1;
344}
345
346/* Helper for calling a function with a single argument quickly.
347
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800348 This function steals the reference of the given argument. */
349static PyObject *
350_Pickle_FastCall(PyObject *func, PyObject *obj)
351{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800352 PyObject *result;
353
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100354 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200355 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800356 return result;
357}
358
359/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000360
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200361/* Retrieve and deconstruct a method for avoiding a reference cycle
362 (pickler -> bound method of pickler -> pickler) */
363static int
364init_method_ref(PyObject *self, _Py_Identifier *name,
365 PyObject **method_func, PyObject **method_self)
366{
367 PyObject *func, *func2;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200368 int ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200369
370 /* *method_func and *method_self should be consistent. All refcount decrements
371 should be occurred after setting *method_self and *method_func. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200372 ret = _PyObject_LookupAttrId(self, name, &func);
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200373 if (func == NULL) {
374 *method_self = NULL;
375 Py_CLEAR(*method_func);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200376 return ret;
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200377 }
378
379 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
380 /* Deconstruct a bound Python method */
381 func2 = PyMethod_GET_FUNCTION(func);
382 Py_INCREF(func2);
383 *method_self = self; /* borrowed */
384 Py_XSETREF(*method_func, func2);
385 Py_DECREF(func);
386 return 0;
387 }
388 else {
389 *method_self = NULL;
390 Py_XSETREF(*method_func, func);
391 return 0;
392 }
393}
394
395/* Bind a method if it was deconstructed */
396static PyObject *
397reconstruct_method(PyObject *func, PyObject *self)
398{
399 if (self) {
400 return PyMethod_New(func, self);
401 }
402 else {
403 Py_INCREF(func);
404 return func;
405 }
406}
407
408static PyObject *
409call_method(PyObject *func, PyObject *self, PyObject *obj)
410{
411 if (self) {
412 return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
413 }
414 else {
415 return PyObject_CallFunctionObjArgs(func, obj, NULL);
416 }
417}
418
419/*************************************************************************/
420
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000421/* Internal data type used as the unpickling stack. */
422typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000423 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000424 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200425 int mark_set; /* is MARK set? */
426 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000427 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000428} Pdata;
429
430static void
431Pdata_dealloc(Pdata *self)
432{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200433 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000434 while (--i >= 0) {
435 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000437 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000438 PyObject_Del(self);
439}
440
441static PyTypeObject Pdata_Type = {
442 PyVarObject_HEAD_INIT(NULL, 0)
443 "_pickle.Pdata", /*tp_name*/
444 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200445 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000446 (destructor)Pdata_dealloc, /*tp_dealloc*/
447};
448
449static PyObject *
450Pdata_New(void)
451{
452 Pdata *self;
453
454 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
455 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000456 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200457 self->mark_set = 0;
458 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000459 self->allocated = 8;
460 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000461 if (self->data)
462 return (PyObject *)self;
463 Py_DECREF(self);
464 return PyErr_NoMemory();
465}
466
467
468/* Retain only the initial clearto items. If clearto >= the current
469 * number of items, this is a (non-erroneous) NOP.
470 */
471static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200472Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000473{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200474 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000475
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200476 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000477 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000478 return 0;
479
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000480 while (--i >= clearto) {
481 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000482 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000483 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000484 return 0;
485}
486
487static int
488Pdata_grow(Pdata *self)
489{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000490 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200491 size_t allocated = (size_t)self->allocated;
492 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000493
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000494 new_allocated = (allocated >> 3) + 6;
495 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200496 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000497 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000498 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500499 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000500 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000501 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000502
503 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200504 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000505 return 0;
506
507 nomemory:
508 PyErr_NoMemory();
509 return -1;
510}
511
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200512static int
513Pdata_stack_underflow(Pdata *self)
514{
515 PickleState *st = _Pickle_GetGlobalState();
516 PyErr_SetString(st->UnpicklingError,
517 self->mark_set ?
518 "unexpected MARK found" :
519 "unpickling stack underflow");
520 return -1;
521}
522
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000523/* D is a Pdata*. Pop the topmost element and store it into V, which
524 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
525 * is raised and V is set to NULL.
526 */
527static PyObject *
528Pdata_pop(Pdata *self)
529{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200530 if (Py_SIZE(self) <= self->fence) {
531 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000532 return NULL;
533 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000534 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000535}
536#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
537
538static int
539Pdata_push(Pdata *self, PyObject *obj)
540{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000541 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000542 return -1;
543 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000544 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000545 return 0;
546}
547
548/* Push an object on stack, transferring its ownership to the stack. */
549#define PDATA_PUSH(D, O, ER) do { \
550 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
551
552/* Push an object on stack, adding a new reference to the object. */
553#define PDATA_APPEND(D, O, ER) do { \
554 Py_INCREF((O)); \
555 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
556
557static PyObject *
558Pdata_poptuple(Pdata *self, Py_ssize_t start)
559{
560 PyObject *tuple;
561 Py_ssize_t len, i, j;
562
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200563 if (start < self->fence) {
564 Pdata_stack_underflow(self);
565 return NULL;
566 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000567 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000568 tuple = PyTuple_New(len);
569 if (tuple == NULL)
570 return NULL;
571 for (i = start, j = 0; j < len; i++, j++)
572 PyTuple_SET_ITEM(tuple, j, self->data[i]);
573
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000574 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000575 return tuple;
576}
577
578static PyObject *
579Pdata_poplist(Pdata *self, Py_ssize_t start)
580{
581 PyObject *list;
582 Py_ssize_t len, i, j;
583
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000584 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000585 list = PyList_New(len);
586 if (list == NULL)
587 return NULL;
588 for (i = start, j = 0; j < len; i++, j++)
589 PyList_SET_ITEM(list, j, self->data[i]);
590
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000591 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000592 return list;
593}
594
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000595typedef struct {
596 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200597 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000598} PyMemoEntry;
599
600typedef struct {
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700601 size_t mt_mask;
602 size_t mt_used;
603 size_t mt_allocated;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000604 PyMemoEntry *mt_table;
605} PyMemoTable;
606
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000607typedef struct PicklerObject {
608 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000609 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000610 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000611 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000612 PyObject *pers_func; /* persistent_id() method, can be NULL */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200613 PyObject *pers_func_self; /* borrowed reference to self if pers_func
614 is an unbound method, NULL otherwise */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100615 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000616
617 PyObject *write; /* write() method of the output stream. */
618 PyObject *output_buffer; /* Write into a local bytearray buffer before
619 flushing to the stream. */
620 Py_ssize_t output_len; /* Length of output_buffer. */
621 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000622 int proto; /* Pickle protocol number, >= 0 */
623 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100624 int framing; /* True when framing is enabled, proto >= 4 */
625 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000626 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100627 is no frame currently open. */
628
629 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000630 int fast; /* Enable fast mode if set to a true value.
631 The fast mode disable the usage of memo,
632 therefore speeding the pickling process by
633 not generating superfluous PUT opcodes. It
634 should not be used if with self-referential
635 objects. */
636 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000637 int fix_imports; /* Indicate whether Pickler should fix
638 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000639 PyObject *fast_memo;
640} PicklerObject;
641
642typedef struct UnpicklerObject {
643 PyObject_HEAD
644 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000645
646 /* The unpickler memo is just an array of PyObject *s. Using a dict
647 is unnecessary, since the keys are contiguous ints. */
648 PyObject **memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700649 size_t memo_size; /* Capacity of the memo array */
650 size_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000651
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000652 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200653 PyObject *pers_func_self; /* borrowed reference to self if pers_func
654 is an unbound method, NULL otherwise */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000655
656 Py_buffer buffer;
657 char *input_buffer;
658 char *input_line;
659 Py_ssize_t input_len;
660 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000661 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100662
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000663 PyObject *read; /* read() method of the input stream. */
664 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000665 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000666
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000667 char *encoding; /* Name of the encoding to be used for
668 decoding strings pickled using Python
669 2.x. The default value is "ASCII" */
670 char *errors; /* Name of errors handling scheme to used when
671 decoding strings. The default value is
672 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500673 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000674 objects. */
675 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
676 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000677 int proto; /* Protocol of the pickle loaded. */
678 int fix_imports; /* Indicate whether Unpickler should fix
679 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000680} UnpicklerObject;
681
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200682typedef struct {
683 PyObject_HEAD
684 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
685} PicklerMemoProxyObject;
686
687typedef struct {
688 PyObject_HEAD
689 UnpicklerObject *unpickler;
690} UnpicklerMemoProxyObject;
691
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000692/* Forward declarations */
693static int save(PicklerObject *, PyObject *, int);
694static int save_reduce(PicklerObject *, PyObject *, PyObject *);
695static PyTypeObject Pickler_Type;
696static PyTypeObject Unpickler_Type;
697
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200698#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000699
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000700/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300701 A custom hashtable mapping void* to Python ints. This is used by the pickler
702 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000703 a bunch of unnecessary object creation. This makes a huge performance
704 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000705
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000706#define MT_MINSIZE 8
707#define PERTURB_SHIFT 5
708
709
710static PyMemoTable *
711PyMemoTable_New(void)
712{
713 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
714 if (memo == NULL) {
715 PyErr_NoMemory();
716 return NULL;
717 }
718
719 memo->mt_used = 0;
720 memo->mt_allocated = MT_MINSIZE;
721 memo->mt_mask = MT_MINSIZE - 1;
722 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
723 if (memo->mt_table == NULL) {
724 PyMem_FREE(memo);
725 PyErr_NoMemory();
726 return NULL;
727 }
728 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
729
730 return memo;
731}
732
733static PyMemoTable *
734PyMemoTable_Copy(PyMemoTable *self)
735{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000736 PyMemoTable *new = PyMemoTable_New();
737 if (new == NULL)
738 return NULL;
739
740 new->mt_used = self->mt_used;
741 new->mt_allocated = self->mt_allocated;
742 new->mt_mask = self->mt_mask;
743 /* The table we get from _New() is probably smaller than we wanted.
744 Free it and allocate one that's the right size. */
745 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500746 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000747 if (new->mt_table == NULL) {
748 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200749 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000750 return NULL;
751 }
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700752 for (size_t i = 0; i < self->mt_allocated; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000753 Py_XINCREF(self->mt_table[i].me_key);
754 }
755 memcpy(new->mt_table, self->mt_table,
756 sizeof(PyMemoEntry) * self->mt_allocated);
757
758 return new;
759}
760
761static Py_ssize_t
762PyMemoTable_Size(PyMemoTable *self)
763{
764 return self->mt_used;
765}
766
767static int
768PyMemoTable_Clear(PyMemoTable *self)
769{
770 Py_ssize_t i = self->mt_allocated;
771
772 while (--i >= 0) {
773 Py_XDECREF(self->mt_table[i].me_key);
774 }
775 self->mt_used = 0;
776 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
777 return 0;
778}
779
780static void
781PyMemoTable_Del(PyMemoTable *self)
782{
783 if (self == NULL)
784 return;
785 PyMemoTable_Clear(self);
786
787 PyMem_FREE(self->mt_table);
788 PyMem_FREE(self);
789}
790
791/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
792 can be considerably simpler than dictobject.c's lookdict(). */
793static PyMemoEntry *
794_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
795{
796 size_t i;
797 size_t perturb;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700798 size_t mask = self->mt_mask;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000799 PyMemoEntry *table = self->mt_table;
800 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000801 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000802
803 i = hash & mask;
804 entry = &table[i];
805 if (entry->me_key == NULL || entry->me_key == key)
806 return entry;
807
808 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
809 i = (i << 2) + i + perturb + 1;
810 entry = &table[i & mask];
811 if (entry->me_key == NULL || entry->me_key == key)
812 return entry;
813 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700814 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000815}
816
817/* Returns -1 on failure, 0 on success. */
818static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700819_PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000820{
821 PyMemoEntry *oldtable = NULL;
822 PyMemoEntry *oldentry, *newentry;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700823 size_t new_size = MT_MINSIZE;
824 size_t to_process;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000825
826 assert(min_size > 0);
827
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700828 if (min_size > PY_SSIZE_T_MAX) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000829 PyErr_NoMemory();
830 return -1;
831 }
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700832
833 /* Find the smallest valid table size >= min_size. */
834 while (new_size < min_size) {
835 new_size <<= 1;
836 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000837 /* new_size needs to be a power of two. */
838 assert((new_size & (new_size - 1)) == 0);
839
840 /* Allocate new table. */
841 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500842 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000843 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200844 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000845 PyErr_NoMemory();
846 return -1;
847 }
848 self->mt_allocated = new_size;
849 self->mt_mask = new_size - 1;
850 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
851
852 /* Copy entries from the old table. */
853 to_process = self->mt_used;
854 for (oldentry = oldtable; to_process > 0; oldentry++) {
855 if (oldentry->me_key != NULL) {
856 to_process--;
857 /* newentry is a pointer to a chunk of the new
858 mt_table, so we're setting the key:value pair
859 in-place. */
860 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
861 newentry->me_key = oldentry->me_key;
862 newentry->me_value = oldentry->me_value;
863 }
864 }
865
866 /* Deallocate the old table. */
867 PyMem_FREE(oldtable);
868 return 0;
869}
870
871/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200872static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000873PyMemoTable_Get(PyMemoTable *self, PyObject *key)
874{
875 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
876 if (entry->me_key == NULL)
877 return NULL;
878 return &entry->me_value;
879}
880
881/* Returns -1 on failure, 0 on success. */
882static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200883PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000884{
885 PyMemoEntry *entry;
886
887 assert(key != NULL);
888
889 entry = _PyMemoTable_Lookup(self, key);
890 if (entry->me_key != NULL) {
891 entry->me_value = value;
892 return 0;
893 }
894 Py_INCREF(key);
895 entry->me_key = key;
896 entry->me_value = value;
897 self->mt_used++;
898
899 /* If we added a key, we can safely resize. Otherwise just return!
900 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
901 *
902 * Quadrupling the size improves average table sparseness
903 * (reducing collisions) at the cost of some memory. It also halves
904 * the number of expensive resize operations in a growing memo table.
905 *
906 * Very large memo tables (over 50K items) use doubling instead.
907 * This may help applications with severe memory constraints.
908 */
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700909 if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000910 return 0;
Benjamin Petersona4ae8282018-09-20 18:36:40 -0700911 }
912 // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow.
913 size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used;
914 return _PyMemoTable_ResizeTable(self, desired_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000915}
916
917#undef MT_MINSIZE
918#undef PERTURB_SHIFT
919
920/*************************************************************************/
921
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000922
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000923static int
924_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000925{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300926 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200927 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000928 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000929 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000930 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100931 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000932 return 0;
933}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000934
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100935static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100936_write_size64(char *out, size_t value)
937{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200938 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800939
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200940 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800941
942 for (i = 0; i < sizeof(size_t); i++) {
943 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
944 }
945 for (i = sizeof(size_t); i < 8; i++) {
946 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800947 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100948}
949
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100950static int
951_Pickler_CommitFrame(PicklerObject *self)
952{
953 size_t frame_len;
954 char *qdata;
955
956 if (!self->framing || self->frame_start == -1)
957 return 0;
958 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
959 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200960 if (frame_len >= FRAME_SIZE_MIN) {
961 qdata[0] = FRAME;
962 _write_size64(qdata + 1, frame_len);
963 }
964 else {
965 memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
966 self->output_len -= FRAME_HEADER_SIZE;
967 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100968 self->frame_start = -1;
969 return 0;
970}
971
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000972static PyObject *
973_Pickler_GetString(PicklerObject *self)
974{
975 PyObject *output_buffer = self->output_buffer;
976
977 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100978
979 if (_Pickler_CommitFrame(self))
980 return NULL;
981
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000982 self->output_buffer = NULL;
983 /* Resize down to exact size */
984 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
985 return NULL;
986 return output_buffer;
987}
988
989static int
990_Pickler_FlushToFile(PicklerObject *self)
991{
992 PyObject *output, *result;
993
994 assert(self->write != NULL);
995
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100996 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000997 output = _Pickler_GetString(self);
998 if (output == NULL)
999 return -1;
1000
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001001 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001002 Py_XDECREF(result);
1003 return (result == NULL) ? -1 : 0;
1004}
1005
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001006static int
1007_Pickler_OpcodeBoundary(PicklerObject *self)
1008{
1009 Py_ssize_t frame_len;
1010
1011 if (!self->framing || self->frame_start == -1) {
1012 return 0;
1013 }
1014 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1015 if (frame_len >= FRAME_SIZE_TARGET) {
1016 if(_Pickler_CommitFrame(self)) {
1017 return -1;
1018 }
Leo Ariasc3d95082018-02-03 18:36:10 -06001019 /* Flush the content of the committed frame to the underlying
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001020 * file and reuse the pickler buffer for the next frame so as
1021 * to limit memory usage when dumping large complex objects to
1022 * a file.
1023 *
1024 * self->write is NULL when called via dumps.
1025 */
1026 if (self->write != NULL) {
1027 if (_Pickler_FlushToFile(self) < 0) {
1028 return -1;
1029 }
1030 if (_Pickler_ClearBuffer(self) < 0) {
1031 return -1;
1032 }
1033 }
1034 }
1035 return 0;
1036}
1037
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001038static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001039_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001040{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001041 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001043 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044
1045 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001046 need_new_frame = (self->framing && self->frame_start == -1);
1047
1048 if (need_new_frame)
1049 n = data_len + FRAME_HEADER_SIZE;
1050 else
1051 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052
1053 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001054 if (required > self->max_output_len) {
1055 /* Make place in buffer for the pickle chunk */
1056 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1057 PyErr_NoMemory();
1058 return -1;
1059 }
1060 self->max_output_len = (self->output_len + n) / 2 * 3;
1061 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1062 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001063 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001064 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001065 if (need_new_frame) {
1066 /* Setup new frame */
1067 Py_ssize_t frame_start = self->output_len;
1068 self->frame_start = frame_start;
1069 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1070 /* Write an invalid value, for debugging */
1071 buffer[frame_start + i] = 0xFE;
1072 }
1073 self->output_len += FRAME_HEADER_SIZE;
1074 }
1075 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001076 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001077 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001078 buffer[self->output_len + i] = s[i];
1079 }
1080 }
1081 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001082 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001083 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001084 self->output_len += data_len;
1085 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001086}
1087
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001088static PicklerObject *
1089_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001090{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001091 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001092
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001093 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1094 if (self == NULL)
1095 return NULL;
1096
1097 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001098 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001099 self->write = NULL;
1100 self->proto = 0;
1101 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001102 self->framing = 0;
1103 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001104 self->fast = 0;
1105 self->fast_nesting = 0;
1106 self->fix_imports = 0;
1107 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001108 self->max_output_len = WRITE_BUF_SIZE;
1109 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001110
1111 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001112 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1113 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001114
1115 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001116 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001117 return NULL;
1118 }
1119 return self;
1120}
1121
1122static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001123_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001124{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001125 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001126
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001127 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001129 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001131 proto = PyLong_AsLong(protocol);
1132 if (proto < 0) {
1133 if (proto == -1 && PyErr_Occurred())
1134 return -1;
1135 proto = HIGHEST_PROTOCOL;
1136 }
1137 else if (proto > HIGHEST_PROTOCOL) {
1138 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1139 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001140 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001141 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001142 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001143 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144 self->bin = proto > 0;
1145 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001146 return 0;
1147}
1148
1149/* Returns -1 (with an exception set) on failure, 0 on success. This may
1150 be called once on a freshly created Pickler. */
1151static int
1152_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1153{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001154 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001155 assert(file != NULL);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001156 if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) {
1157 return -1;
1158 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001159 if (self->write == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001160 PyErr_SetString(PyExc_TypeError,
1161 "file must have a 'write' attribute");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001162 return -1;
1163 }
1164
1165 return 0;
1166}
1167
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001168/* Returns the size of the input on success, -1 on failure. This takes its
1169 own reference to `input`. */
1170static Py_ssize_t
1171_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1172{
1173 if (self->buffer.buf != NULL)
1174 PyBuffer_Release(&self->buffer);
1175 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1176 return -1;
1177 self->input_buffer = self->buffer.buf;
1178 self->input_len = self->buffer.len;
1179 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001180 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001181 return self->input_len;
1182}
1183
Antoine Pitrou04248a82010-10-12 20:51:21 +00001184static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001185bad_readline(void)
1186{
1187 PickleState *st = _Pickle_GetGlobalState();
1188 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1189 return -1;
1190}
1191
1192static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001193_Unpickler_SkipConsumed(UnpicklerObject *self)
1194{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001195 Py_ssize_t consumed;
1196 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001197
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001198 consumed = self->next_read_idx - self->prefetched_idx;
1199 if (consumed <= 0)
1200 return 0;
1201
1202 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001203 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001204 r = PyObject_CallFunction(self->read, "n", consumed);
1205 if (r == NULL)
1206 return -1;
1207 Py_DECREF(r);
1208
1209 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001210 return 0;
1211}
1212
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001213static const Py_ssize_t READ_WHOLE_LINE = -1;
1214
1215/* If reading from a file, we need to only pull the bytes we need, since there
1216 may be multiple pickle objects arranged contiguously in the same input
1217 buffer.
1218
1219 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1220 bytes from the input stream/buffer.
1221
1222 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1223 failure; on success, returns the number of bytes read from the file.
1224
1225 On success, self->input_len will be 0; this is intentional so that when
1226 unpickling from a file, the "we've run out of data" code paths will trigger,
1227 causing the Unpickler to go back to the file for more data. Use the returned
1228 size to tell you how much data you can process. */
1229static Py_ssize_t
1230_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1231{
1232 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001233 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001234
1235 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001236
Antoine Pitrou04248a82010-10-12 20:51:21 +00001237 if (_Unpickler_SkipConsumed(self) < 0)
1238 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001239
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001240 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001241 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001242 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001243 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001244 PyObject *len;
1245 /* Prefetch some data without advancing the file pointer, if possible */
1246 if (self->peek && n < PREFETCH) {
1247 len = PyLong_FromSsize_t(PREFETCH);
1248 if (len == NULL)
1249 return -1;
1250 data = _Pickle_FastCall(self->peek, len);
1251 if (data == NULL) {
1252 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1253 return -1;
1254 /* peek() is probably not supported by the given file object */
1255 PyErr_Clear();
1256 Py_CLEAR(self->peek);
1257 }
1258 else {
1259 read_size = _Unpickler_SetStringInput(self, data);
1260 Py_DECREF(data);
1261 self->prefetched_idx = 0;
1262 if (n <= read_size)
1263 return n;
1264 }
1265 }
1266 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001267 if (len == NULL)
1268 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001269 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001270 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001271 if (data == NULL)
1272 return -1;
1273
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001274 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001275 Py_DECREF(data);
1276 return read_size;
1277}
1278
Victor Stinner19ed27e2016-05-20 11:42:37 +02001279/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001280static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001281_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001282{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001283 Py_ssize_t num_read;
1284
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001285 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001286 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1287 PickleState *st = _Pickle_GetGlobalState();
1288 PyErr_SetString(st->UnpicklingError,
1289 "read would overflow (invalid bytecode)");
1290 return -1;
1291 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001292
1293 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1294 assert(self->next_read_idx + n > self->input_len);
1295
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001296 if (!self->read)
1297 return bad_readline();
1298
Antoine Pitrou04248a82010-10-12 20:51:21 +00001299 num_read = _Unpickler_ReadFromFile(self, n);
1300 if (num_read < 0)
1301 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001302 if (num_read < n)
1303 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001304 *s = self->input_buffer;
1305 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001306 return n;
1307}
1308
Victor Stinner19ed27e2016-05-20 11:42:37 +02001309/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1310
1311 This should be used for all data reads, rather than accessing the unpickler's
1312 input buffer directly. This method deals correctly with reading from input
1313 streams, which the input buffer doesn't deal with.
1314
1315 Note that when reading from a file-like object, self->next_read_idx won't
1316 be updated (it should remain at 0 for the entire unpickling process). You
1317 should use this function's return value to know how many bytes you can
1318 consume.
1319
1320 Returns -1 (with an exception set) on failure. On success, return the
1321 number of chars read. */
1322#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001323 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001324 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1325 (self)->next_read_idx += (n), \
1326 (n)) \
1327 : _Unpickler_ReadImpl(self, (s), (n)))
1328
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001329static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001330_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1331 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001332{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001333 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001334 if (input_line == NULL) {
1335 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001336 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001337 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001338
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001339 memcpy(input_line, line, len);
1340 input_line[len] = '\0';
1341 self->input_line = input_line;
1342 *result = self->input_line;
1343 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001344}
1345
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001346/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001347 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001348
1349 Returns the number of chars read, or -1 on failure. */
1350static Py_ssize_t
1351_Unpickler_Readline(UnpicklerObject *self, char **result)
1352{
1353 Py_ssize_t i, num_read;
1354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001355 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001356 if (self->input_buffer[i] == '\n') {
1357 char *line_start = self->input_buffer + self->next_read_idx;
1358 num_read = i - self->next_read_idx + 1;
1359 self->next_read_idx = i + 1;
1360 return _Unpickler_CopyLine(self, line_start, num_read, result);
1361 }
1362 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001363 if (!self->read)
1364 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001365
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001366 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1367 if (num_read < 0)
1368 return -1;
1369 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1370 return bad_readline();
1371 self->next_read_idx = num_read;
1372 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001373}
1374
1375/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1376 will be modified in place. */
1377static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001378_Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001379{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001380 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001381
1382 assert(new_size > self->memo_size);
1383
Sergey Fedoseev67b9cc82018-08-16 09:27:50 +05001384 PyObject **memo_new = self->memo;
1385 PyMem_RESIZE(memo_new, PyObject *, new_size);
1386 if (memo_new == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001387 PyErr_NoMemory();
1388 return -1;
1389 }
Sergey Fedoseev67b9cc82018-08-16 09:27:50 +05001390 self->memo = memo_new;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001391 for (i = self->memo_size; i < new_size; i++)
1392 self->memo[i] = NULL;
1393 self->memo_size = new_size;
1394 return 0;
1395}
1396
1397/* Returns NULL if idx is out of bounds. */
1398static PyObject *
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001399_Unpickler_MemoGet(UnpicklerObject *self, size_t idx)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001400{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001401 if (idx >= self->memo_size)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001402 return NULL;
1403
1404 return self->memo[idx];
1405}
1406
1407/* Returns -1 (with an exception set) on failure, 0 on success.
1408 This takes its own reference to `value`. */
1409static int
Benjamin Petersona4ae8282018-09-20 18:36:40 -07001410_Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001411{
1412 PyObject *old_item;
1413
1414 if (idx >= self->memo_size) {
1415 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1416 return -1;
1417 assert(idx < self->memo_size);
1418 }
1419 Py_INCREF(value);
1420 old_item = self->memo[idx];
1421 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001422 if (old_item != NULL) {
1423 Py_DECREF(old_item);
1424 }
1425 else {
1426 self->memo_len++;
1427 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001428 return 0;
1429}
1430
1431static PyObject **
1432_Unpickler_NewMemo(Py_ssize_t new_size)
1433{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001434 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001435 if (memo == NULL) {
1436 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001437 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001438 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001439 memset(memo, 0, new_size * sizeof(PyObject *));
1440 return memo;
1441}
1442
1443/* Free the unpickler's memo, taking care to decref any items left in it. */
1444static void
1445_Unpickler_MemoCleanup(UnpicklerObject *self)
1446{
1447 Py_ssize_t i;
1448 PyObject **memo = self->memo;
1449
1450 if (self->memo == NULL)
1451 return;
1452 self->memo = NULL;
1453 i = self->memo_size;
1454 while (--i >= 0) {
1455 Py_XDECREF(memo[i]);
1456 }
1457 PyMem_FREE(memo);
1458}
1459
1460static UnpicklerObject *
1461_Unpickler_New(void)
1462{
1463 UnpicklerObject *self;
1464
1465 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1466 if (self == NULL)
1467 return NULL;
1468
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001469 self->pers_func = NULL;
1470 self->input_buffer = NULL;
1471 self->input_line = NULL;
1472 self->input_len = 0;
1473 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001474 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001475 self->read = NULL;
1476 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001477 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001478 self->encoding = NULL;
1479 self->errors = NULL;
1480 self->marks = NULL;
1481 self->num_marks = 0;
1482 self->marks_size = 0;
1483 self->proto = 0;
1484 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001485 memset(&self->buffer, 0, sizeof(Py_buffer));
1486 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001487 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001488 self->memo = _Unpickler_NewMemo(self->memo_size);
1489 self->stack = (Pdata *)Pdata_New();
1490
1491 if (self->memo == NULL || self->stack == NULL) {
1492 Py_DECREF(self);
1493 return NULL;
1494 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001495
1496 return self;
1497}
1498
1499/* Returns -1 (with an exception set) on failure, 0 on success. This may
1500 be called once on a freshly created Pickler. */
1501static int
1502_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1503{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001504 _Py_IDENTIFIER(peek);
1505 _Py_IDENTIFIER(read);
1506 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001507
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001508 if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
1509 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001510 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001511 (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1512 (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001513 if (self->readline == NULL || self->read == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001514 if (!PyErr_Occurred()) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001515 PyErr_SetString(PyExc_TypeError,
1516 "file must have 'read' and 'readline' attributes");
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001517 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001518 Py_CLEAR(self->read);
1519 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001520 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001521 return -1;
1522 }
1523 return 0;
1524}
1525
1526/* Returns -1 (with an exception set) on failure, 0 on success. This may
1527 be called once on a freshly created Pickler. */
1528static int
1529_Unpickler_SetInputEncoding(UnpicklerObject *self,
1530 const char *encoding,
1531 const char *errors)
1532{
1533 if (encoding == NULL)
1534 encoding = "ASCII";
1535 if (errors == NULL)
1536 errors = "strict";
1537
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001538 self->encoding = _PyMem_Strdup(encoding);
1539 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001540 if (self->encoding == NULL || self->errors == NULL) {
1541 PyErr_NoMemory();
1542 return -1;
1543 }
1544 return 0;
1545}
1546
1547/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001548static int
1549memo_get(PicklerObject *self, PyObject *key)
1550{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001551 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001552 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001553 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001554
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001555 value = PyMemoTable_Get(self->memo, key);
1556 if (value == NULL) {
1557 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558 return -1;
1559 }
1560
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001561 if (!self->bin) {
1562 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001563 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1564 "%" PY_FORMAT_SIZE_T "d\n", *value);
1565 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001566 }
1567 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001568 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001569 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001570 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001571 len = 2;
1572 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001573 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001574 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001575 pdata[1] = (unsigned char)(*value & 0xff);
1576 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1577 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1578 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001579 len = 5;
1580 }
1581 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001582 PickleState *st = _Pickle_GetGlobalState();
1583 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001584 "memo id too large for LONG_BINGET");
1585 return -1;
1586 }
1587 }
1588
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001589 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001590 return -1;
1591
1592 return 0;
1593}
1594
1595/* Store an object in the memo, assign it a new unique ID based on the number
1596 of objects currently stored in the memo and generate a PUT opcode. */
1597static int
1598memo_put(PicklerObject *self, PyObject *obj)
1599{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001600 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001601 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001602 Py_ssize_t idx;
1603
1604 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001605
1606 if (self->fast)
1607 return 0;
1608
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001609 idx = PyMemoTable_Size(self->memo);
1610 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1611 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001612
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001613 if (self->proto >= 4) {
1614 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1615 return -1;
1616 return 0;
1617 }
1618 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001619 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001620 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001621 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001622 len = strlen(pdata);
1623 }
1624 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001625 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001626 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001627 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001628 len = 2;
1629 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001630 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001631 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001632 pdata[1] = (unsigned char)(idx & 0xff);
1633 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1634 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1635 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001636 len = 5;
1637 }
1638 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001639 PickleState *st = _Pickle_GetGlobalState();
1640 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001641 "memo id too large for LONG_BINPUT");
1642 return -1;
1643 }
1644 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001645 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001646 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001647
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649}
1650
1651static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001652get_dotted_path(PyObject *obj, PyObject *name)
1653{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001654 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001655 PyObject *dotted_path;
1656 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001657
1658 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001659 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001660 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001661 n = PyList_GET_SIZE(dotted_path);
1662 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001663 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001664 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001665 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001666 if (obj == NULL)
1667 PyErr_Format(PyExc_AttributeError,
1668 "Can't pickle local object %R", name);
1669 else
1670 PyErr_Format(PyExc_AttributeError,
1671 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001672 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001673 return NULL;
1674 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001675 }
1676 return dotted_path;
1677}
1678
1679static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001680get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001681{
1682 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001683 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001684
1685 assert(PyList_CheckExact(names));
1686 Py_INCREF(obj);
1687 n = PyList_GET_SIZE(names);
1688 for (i = 0; i < n; i++) {
1689 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001690 Py_XDECREF(parent);
1691 parent = obj;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001692 (void)_PyObject_LookupAttr(parent, name, &obj);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001693 if (obj == NULL) {
1694 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001695 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001696 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001697 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001698 if (pparent != NULL)
1699 *pparent = parent;
1700 else
1701 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001702 return obj;
1703}
1704
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001705
1706static PyObject *
1707getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1708{
1709 PyObject *dotted_path, *attr;
1710
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001711 if (allow_qualname) {
1712 dotted_path = get_dotted_path(obj, name);
1713 if (dotted_path == NULL)
1714 return NULL;
1715 attr = get_deep_attribute(obj, dotted_path, NULL);
1716 Py_DECREF(dotted_path);
1717 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001718 else {
1719 (void)_PyObject_LookupAttr(obj, name, &attr);
1720 }
1721 if (attr == NULL && !PyErr_Occurred()) {
1722 PyErr_Format(PyExc_AttributeError,
1723 "Can't get attribute %R on %R", name, obj);
1724 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001725 return attr;
1726}
1727
Eric Snow3f9eee62017-09-15 16:35:20 -06001728static int
1729_checkmodule(PyObject *module_name, PyObject *module,
1730 PyObject *global, PyObject *dotted_path)
1731{
1732 if (module == Py_None) {
1733 return -1;
1734 }
1735 if (PyUnicode_Check(module_name) &&
1736 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1737 return -1;
1738 }
1739
1740 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1741 if (candidate == NULL) {
Eric Snow3f9eee62017-09-15 16:35:20 -06001742 return -1;
1743 }
1744 if (candidate != global) {
1745 Py_DECREF(candidate);
1746 return -1;
1747 }
1748 Py_DECREF(candidate);
1749 return 0;
1750}
1751
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001752static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001753whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001754{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001755 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001756 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001757 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001758 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001759 _Py_IDENTIFIER(__module__);
1760 _Py_IDENTIFIER(modules);
1761 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001762
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001763 if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) {
1764 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001765 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001766 if (module_name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001767 /* In some rare cases (e.g., bound methods of extension types),
1768 __module__ can be None. If it is so, then search sys.modules for
1769 the module of global. */
1770 if (module_name != Py_None)
1771 return module_name;
1772 Py_CLEAR(module_name);
1773 }
1774 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001775
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001776 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001777 modules = _PySys_GetObjectId(&PyId_modules);
1778 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001779 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001780 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001781 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001782 if (PyDict_CheckExact(modules)) {
1783 i = 0;
1784 while (PyDict_Next(modules, &i, &module_name, &module)) {
1785 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1786 Py_INCREF(module_name);
1787 return module_name;
1788 }
1789 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001790 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001791 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001792 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001793 }
1794 else {
1795 PyObject *iterator = PyObject_GetIter(modules);
1796 if (iterator == NULL) {
1797 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001798 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001799 while ((module_name = PyIter_Next(iterator))) {
1800 module = PyObject_GetItem(modules, module_name);
1801 if (module == NULL) {
1802 Py_DECREF(module_name);
1803 Py_DECREF(iterator);
1804 return NULL;
1805 }
1806 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1807 Py_DECREF(module);
1808 Py_DECREF(iterator);
1809 return module_name;
1810 }
1811 Py_DECREF(module);
1812 Py_DECREF(module_name);
1813 if (PyErr_Occurred()) {
1814 Py_DECREF(iterator);
1815 return NULL;
1816 }
1817 }
1818 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001819 }
1820
1821 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001822 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001823 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001824 return module_name;
1825}
1826
1827/* fast_save_enter() and fast_save_leave() are guards against recursive
1828 objects when Pickler is used with the "fast mode" (i.e., with object
1829 memoization disabled). If the nesting of a list or dict object exceed
1830 FAST_NESTING_LIMIT, these guards will start keeping an internal
1831 reference to the seen list or dict objects and check whether these objects
1832 are recursive. These are not strictly necessary, since save() has a
1833 hard-coded recursion limit, but they give a nicer error message than the
1834 typical RuntimeError. */
1835static int
1836fast_save_enter(PicklerObject *self, PyObject *obj)
1837{
1838 /* if fast_nesting < 0, we're doing an error exit. */
1839 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1840 PyObject *key = NULL;
1841 if (self->fast_memo == NULL) {
1842 self->fast_memo = PyDict_New();
1843 if (self->fast_memo == NULL) {
1844 self->fast_nesting = -1;
1845 return 0;
1846 }
1847 }
1848 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001849 if (key == NULL) {
1850 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001851 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001852 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001853 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001854 Py_DECREF(key);
1855 PyErr_Format(PyExc_ValueError,
1856 "fast mode: can't pickle cyclic objects "
1857 "including object type %.200s at %p",
1858 obj->ob_type->tp_name, obj);
1859 self->fast_nesting = -1;
1860 return 0;
1861 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001862 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001863 Py_DECREF(key);
1864 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001865 return 0;
1866 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001867 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1868 Py_DECREF(key);
1869 self->fast_nesting = -1;
1870 return 0;
1871 }
1872 Py_DECREF(key);
1873 }
1874 return 1;
1875}
1876
1877static int
1878fast_save_leave(PicklerObject *self, PyObject *obj)
1879{
1880 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1881 PyObject *key = PyLong_FromVoidPtr(obj);
1882 if (key == NULL)
1883 return 0;
1884 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1885 Py_DECREF(key);
1886 return 0;
1887 }
1888 Py_DECREF(key);
1889 }
1890 return 1;
1891}
1892
1893static int
1894save_none(PicklerObject *self, PyObject *obj)
1895{
1896 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001897 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001898 return -1;
1899
1900 return 0;
1901}
1902
1903static int
1904save_bool(PicklerObject *self, PyObject *obj)
1905{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001907 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001908 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001909 return -1;
1910 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001911 else {
1912 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1913 * so that unpicklers written before bools were introduced unpickle them
1914 * as ints, but unpicklers after can recognize that bools were intended.
1915 * Note that protocol 2 added direct ways to pickle bools.
1916 */
1917 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1918 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1919 return -1;
1920 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001921 return 0;
1922}
1923
1924static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001925save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001926{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001927 PyObject *repr = NULL;
1928 Py_ssize_t size;
1929 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001930 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001931 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001932
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001933 val= PyLong_AsLongAndOverflow(obj, &overflow);
1934 if (!overflow && (sizeof(long) <= 4 ||
1935 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1936 {
Larry Hastings61272b72014-01-07 12:41:53 -08001937 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001938
1939 Note: we can't use -0x80000000L in the above condition because some
1940 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1941 before applying the unary minus when sizeof(long) <= 4. The
1942 resulting value stays unsigned which is commonly not what we want,
1943 so MSVC happily warns us about it. However, that result would have
1944 been fine because we guard for sizeof(long) <= 4 which turns the
1945 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001946 char pdata[32];
1947 Py_ssize_t len = 0;
1948
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001949 if (self->bin) {
1950 pdata[1] = (unsigned char)(val & 0xff);
1951 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1952 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1953 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001954
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001955 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1956 pdata[0] = BININT;
1957 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001958 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001959 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001960 pdata[0] = BININT2;
1961 len = 3;
1962 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001963 else {
1964 pdata[0] = BININT1;
1965 len = 2;
1966 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001967 }
1968 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001969 sprintf(pdata, "%c%ld\n", INT, val);
1970 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001971 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001972 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001973 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001974
1975 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001977 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001978
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001979 if (self->proto >= 2) {
1980 /* Linear-time pickling. */
1981 size_t nbits;
1982 size_t nbytes;
1983 unsigned char *pdata;
1984 char header[5];
1985 int i;
1986 int sign = _PyLong_Sign(obj);
1987
1988 if (sign == 0) {
1989 header[0] = LONG1;
1990 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001991 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001992 goto error;
1993 return 0;
1994 }
1995 nbits = _PyLong_NumBits(obj);
1996 if (nbits == (size_t)-1 && PyErr_Occurred())
1997 goto error;
1998 /* How many bytes do we need? There are nbits >> 3 full
1999 * bytes of data, and nbits & 7 leftover bits. If there
2000 * are any leftover bits, then we clearly need another
2001 * byte. Wnat's not so obvious is that we *probably*
2002 * need another byte even if there aren't any leftovers:
2003 * the most-significant bit of the most-significant byte
2004 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002005 * opposite of the one we need. The exception is ints
2006 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002007 * its own 256's-complement, so has the right sign bit
2008 * even without the extra byte. That's a pain to check
2009 * for in advance, though, so we always grab an extra
2010 * byte at the start, and cut it back later if possible.
2011 */
2012 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002013 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002014 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002015 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002016 goto error;
2017 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002018 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002019 if (repr == NULL)
2020 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002021 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002022 i = _PyLong_AsByteArray((PyLongObject *)obj,
2023 pdata, nbytes,
2024 1 /* little endian */ , 1 /* signed */ );
2025 if (i < 0)
2026 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002027 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002028 * needed. This is so iff the MSB is all redundant sign
2029 * bits.
2030 */
2031 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002032 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002033 pdata[nbytes - 1] == 0xff &&
2034 (pdata[nbytes - 2] & 0x80) != 0) {
2035 nbytes--;
2036 }
2037
2038 if (nbytes < 256) {
2039 header[0] = LONG1;
2040 header[1] = (unsigned char)nbytes;
2041 size = 2;
2042 }
2043 else {
2044 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002045 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002046 for (i = 1; i < 5; i++) {
2047 header[i] = (unsigned char)(size & 0xff);
2048 size >>= 8;
2049 }
2050 size = 5;
2051 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002052 if (_Pickler_Write(self, header, size) < 0 ||
2053 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002054 goto error;
2055 }
2056 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002057 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002058 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002059
Mark Dickinson8dd05142009-01-20 20:43:58 +00002060 /* proto < 2: write the repr and newline. This is quadratic-time (in
2061 the number of digits), in both directions. We add a trailing 'L'
2062 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002063
2064 repr = PyObject_Repr(obj);
2065 if (repr == NULL)
2066 goto error;
2067
Serhiy Storchaka06515832016-11-20 09:13:07 +02002068 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002069 if (string == NULL)
2070 goto error;
2071
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002072 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2073 _Pickler_Write(self, string, size) < 0 ||
2074 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002075 goto error;
2076 }
2077
2078 if (0) {
2079 error:
2080 status = -1;
2081 }
2082 Py_XDECREF(repr);
2083
2084 return status;
2085}
2086
2087static int
2088save_float(PicklerObject *self, PyObject *obj)
2089{
2090 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2091
2092 if (self->bin) {
2093 char pdata[9];
2094 pdata[0] = BINFLOAT;
2095 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2096 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002097 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002099 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002100 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002101 int result = -1;
2102 char *buf = NULL;
2103 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002104
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002105 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002106 goto done;
2107
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002108 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002109 if (!buf) {
2110 PyErr_NoMemory();
2111 goto done;
2112 }
2113
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002114 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002115 goto done;
2116
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002117 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002118 goto done;
2119
2120 result = 0;
2121done:
2122 PyMem_Free(buf);
2123 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002124 }
2125
2126 return 0;
2127}
2128
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002129/* Perform direct write of the header and payload of the binary object.
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002130
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002131 The large contiguous data is written directly into the underlying file
2132 object, bypassing the output_buffer of the Pickler. We intentionally
2133 do not insert a protocol 4 frame opcode to make it possible to optimize
2134 file.read calls in the loader.
2135 */
2136static int
2137_Pickler_write_bytes(PicklerObject *self,
2138 const char *header, Py_ssize_t header_size,
2139 const char *data, Py_ssize_t data_size,
2140 PyObject *payload)
2141{
2142 int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2143 int framing = self->framing;
2144
2145 if (bypass_buffer) {
2146 assert(self->output_buffer != NULL);
2147 /* Commit the previous frame. */
2148 if (_Pickler_CommitFrame(self)) {
2149 return -1;
2150 }
2151 /* Disable framing temporarily */
2152 self->framing = 0;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002153 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002154
2155 if (_Pickler_Write(self, header, header_size) < 0) {
2156 return -1;
2157 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002158
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002159 if (bypass_buffer && self->write != NULL) {
2160 /* Bypass the in-memory buffer to directly stream large data
2161 into the underlying file object. */
2162 PyObject *result, *mem = NULL;
2163 /* Dump the output buffer to the file. */
2164 if (_Pickler_FlushToFile(self) < 0) {
2165 return -1;
2166 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002167
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002168 /* Stream write the payload into the file without going through the
2169 output buffer. */
2170 if (payload == NULL) {
Serhiy Storchaka5b76bdb2018-01-13 00:28:31 +02002171 /* TODO: It would be better to use a memoryview with a linked
2172 original string if this is possible. */
2173 payload = mem = PyBytes_FromStringAndSize(data, data_size);
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002174 if (payload == NULL) {
2175 return -1;
2176 }
2177 }
2178 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2179 Py_XDECREF(mem);
2180 if (result == NULL) {
2181 return -1;
2182 }
2183 Py_DECREF(result);
2184
2185 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2186 if (_Pickler_ClearBuffer(self) < 0) {
2187 return -1;
2188 }
2189 }
2190 else {
2191 if (_Pickler_Write(self, data, data_size) < 0) {
2192 return -1;
2193 }
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002194 }
2195
2196 /* Re-enable framing for subsequent calls to _Pickler_Write. */
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002197 self->framing = framing;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002198
2199 return 0;
2200}
2201
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002202static int
2203save_bytes(PicklerObject *self, PyObject *obj)
2204{
2205 if (self->proto < 3) {
2206 /* Older pickle protocols do not have an opcode for pickling bytes
2207 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002208 the __reduce__ method) to permit bytes object unpickling.
2209
2210 Here we use a hack to be compatible with Python 2. Since in Python
2211 2 'bytes' is just an alias for 'str' (which has different
2212 parameters than the actual bytes object), we use codecs.encode
2213 to create the appropriate 'str' object when unpickled using
2214 Python 2 *and* the appropriate 'bytes' object when unpickled
2215 using Python 3. Again this is a hack and we don't need to do this
2216 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002217 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002218 int status;
2219
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002220 if (PyBytes_GET_SIZE(obj) == 0) {
2221 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2222 }
2223 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002224 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002225 PyObject *unicode_str =
2226 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2227 PyBytes_GET_SIZE(obj),
2228 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002229 _Py_IDENTIFIER(latin1);
2230
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002231 if (unicode_str == NULL)
2232 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002233 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002234 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002235 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002236 Py_DECREF(unicode_str);
2237 }
2238
2239 if (reduce_value == NULL)
2240 return -1;
2241
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002242 /* save_reduce() will memoize the object automatically. */
2243 status = save_reduce(self, reduce_value, obj);
2244 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002245 return status;
2246 }
2247 else {
2248 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002249 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002250 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002251
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002252 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002253 if (size < 0)
2254 return -1;
2255
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002256 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257 header[0] = SHORT_BINBYTES;
2258 header[1] = (unsigned char)size;
2259 len = 2;
2260 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002261 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002262 header[0] = BINBYTES;
2263 header[1] = (unsigned char)(size & 0xff);
2264 header[2] = (unsigned char)((size >> 8) & 0xff);
2265 header[3] = (unsigned char)((size >> 16) & 0xff);
2266 header[4] = (unsigned char)((size >> 24) & 0xff);
2267 len = 5;
2268 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002269 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002270 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002271 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002272 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002273 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002274 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002275 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002276 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002277 return -1; /* string too large */
2278 }
2279
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002280 if (_Pickler_write_bytes(self, header, len,
2281 PyBytes_AS_STRING(obj), size, obj) < 0)
2282 {
2283 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002284 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002285
2286 if (memo_put(self, obj) < 0)
2287 return -1;
2288
2289 return 0;
2290 }
2291}
2292
2293/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2294 backslash and newline characters to \uXXXX escapes. */
2295static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002296raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002297{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002298 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002299 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002300 void *data;
2301 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002302 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002303
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002304 if (PyUnicode_READY(obj))
2305 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002306
Victor Stinner358af132015-10-12 22:36:57 +02002307 _PyBytesWriter_Init(&writer);
2308
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002309 size = PyUnicode_GET_LENGTH(obj);
2310 data = PyUnicode_DATA(obj);
2311 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002312
Victor Stinner358af132015-10-12 22:36:57 +02002313 p = _PyBytesWriter_Alloc(&writer, size);
2314 if (p == NULL)
2315 goto error;
2316 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002317
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002318 for (i=0; i < size; i++) {
2319 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002320 /* Map 32-bit characters to '\Uxxxxxxxx' */
2321 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002322 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002323 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2324 if (p == NULL)
2325 goto error;
2326
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002327 *p++ = '\\';
2328 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002329 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2330 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2331 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2332 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2333 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2334 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2335 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2336 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002337 }
Victor Stinner358af132015-10-12 22:36:57 +02002338 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002339 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002340 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002341 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2342 if (p == NULL)
2343 goto error;
2344
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002345 *p++ = '\\';
2346 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002347 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2348 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2349 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2350 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002352 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002353 else
2354 *p++ = (char) ch;
2355 }
Victor Stinner358af132015-10-12 22:36:57 +02002356
2357 return _PyBytesWriter_Finish(&writer, p);
2358
2359error:
2360 _PyBytesWriter_Dealloc(&writer);
2361 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002362}
2363
2364static int
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002365write_unicode_binary(PicklerObject *self, PyObject *obj)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002366{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002367 char header[9];
2368 Py_ssize_t len;
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002369 PyObject *encoded = NULL;
2370 Py_ssize_t size;
2371 const char *data;
2372
2373 if (PyUnicode_READY(obj))
2374 return -1;
2375
2376 data = PyUnicode_AsUTF8AndSize(obj, &size);
2377 if (data == NULL) {
2378 /* Issue #8383: for strings with lone surrogates, fallback on the
2379 "surrogatepass" error handler. */
2380 PyErr_Clear();
2381 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2382 if (encoded == NULL)
2383 return -1;
2384
2385 data = PyBytes_AS_STRING(encoded);
2386 size = PyBytes_GET_SIZE(encoded);
2387 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002388
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002389 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002390 if (size <= 0xff && self->proto >= 4) {
2391 header[0] = SHORT_BINUNICODE;
2392 header[1] = (unsigned char)(size & 0xff);
2393 len = 2;
2394 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002395 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002396 header[0] = BINUNICODE;
2397 header[1] = (unsigned char)(size & 0xff);
2398 header[2] = (unsigned char)((size >> 8) & 0xff);
2399 header[3] = (unsigned char)((size >> 16) & 0xff);
2400 header[4] = (unsigned char)((size >> 24) & 0xff);
2401 len = 5;
2402 }
2403 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002404 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002405 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002406 len = 9;
2407 }
2408 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002409 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002410 "cannot serialize a string larger than 4GiB");
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002411 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002412 return -1;
2413 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002414
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002415 if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2416 Py_XDECREF(encoded);
2417 return -1;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002418 }
Serhiy Storchaka0a2da502018-01-11 13:03:20 +02002419 Py_XDECREF(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002420 return 0;
2421}
2422
2423static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002424save_unicode(PicklerObject *self, PyObject *obj)
2425{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002426 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002427 if (write_unicode_binary(self, obj) < 0)
2428 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002429 }
2430 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002431 PyObject *encoded;
2432 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002433 const char unicode_op = UNICODE;
2434
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002435 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002436 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002437 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002438
Antoine Pitrou299978d2013-04-07 17:38:11 +02002439 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2440 Py_DECREF(encoded);
2441 return -1;
2442 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002443
2444 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002445 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2446 Py_DECREF(encoded);
2447 return -1;
2448 }
2449 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002450
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002451 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002452 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002453 }
2454 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002455 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002456
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002457 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002458}
2459
2460/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2461static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002462store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002463{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002464 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002465
2466 assert(PyTuple_Size(t) == len);
2467
2468 for (i = 0; i < len; i++) {
2469 PyObject *element = PyTuple_GET_ITEM(t, i);
2470
2471 if (element == NULL)
2472 return -1;
2473 if (save(self, element, 0) < 0)
2474 return -1;
2475 }
2476
2477 return 0;
2478}
2479
2480/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2481 * used across protocols to minimize the space needed to pickle them.
2482 * Tuples are also the only builtin immutable type that can be recursive
2483 * (a tuple can be reached from itself), and that requires some subtle
2484 * magic so that it works in all cases. IOW, this is a long routine.
2485 */
2486static int
2487save_tuple(PicklerObject *self, PyObject *obj)
2488{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002489 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002490
2491 const char mark_op = MARK;
2492 const char tuple_op = TUPLE;
2493 const char pop_op = POP;
2494 const char pop_mark_op = POP_MARK;
2495 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2496
2497 if ((len = PyTuple_Size(obj)) < 0)
2498 return -1;
2499
2500 if (len == 0) {
2501 char pdata[2];
2502
2503 if (self->proto) {
2504 pdata[0] = EMPTY_TUPLE;
2505 len = 1;
2506 }
2507 else {
2508 pdata[0] = MARK;
2509 pdata[1] = TUPLE;
2510 len = 2;
2511 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002512 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002513 return -1;
2514 return 0;
2515 }
2516
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002517 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002518 * saving the tuple elements, the tuple must be recursive, in
2519 * which case we'll pop everything we put on the stack, and fetch
2520 * its value from the memo.
2521 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002522 if (len <= 3 && self->proto >= 2) {
2523 /* Use TUPLE{1,2,3} opcodes. */
2524 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002525 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002526
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002527 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002528 /* pop the len elements */
2529 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002530 if (_Pickler_Write(self, &pop_op, 1) < 0)
2531 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002532 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002533 if (memo_get(self, obj) < 0)
2534 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002535
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002536 return 0;
2537 }
2538 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002539 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2540 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002541 }
2542 goto memoize;
2543 }
2544
2545 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2546 * Generate MARK e1 e2 ... TUPLE
2547 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002548 if (_Pickler_Write(self, &mark_op, 1) < 0)
2549 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002550
2551 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002552 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002553
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002554 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002555 /* pop the stack stuff we pushed */
2556 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002557 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2558 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002559 }
2560 else {
2561 /* Note that we pop one more than len, to remove
2562 * the MARK too.
2563 */
2564 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002565 if (_Pickler_Write(self, &pop_op, 1) < 0)
2566 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002567 }
2568 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002569 if (memo_get(self, obj) < 0)
2570 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002571
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002572 return 0;
2573 }
2574 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002575 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2576 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002577 }
2578
2579 memoize:
2580 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002581 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002582
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002583 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002584}
2585
2586/* iter is an iterator giving items, and we batch up chunks of
2587 * MARK item item ... item APPENDS
2588 * opcode sequences. Calling code should have arranged to first create an
2589 * empty list, or list-like object, for the APPENDS to operate on.
2590 * Returns 0 on success, <0 on error.
2591 */
2592static int
2593batch_list(PicklerObject *self, PyObject *iter)
2594{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002595 PyObject *obj = NULL;
2596 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002597 int i, n;
2598
2599 const char mark_op = MARK;
2600 const char append_op = APPEND;
2601 const char appends_op = APPENDS;
2602
2603 assert(iter != NULL);
2604
2605 /* XXX: I think this function could be made faster by avoiding the
2606 iterator interface and fetching objects directly from list using
2607 PyList_GET_ITEM.
2608 */
2609
2610 if (self->proto == 0) {
2611 /* APPENDS isn't available; do one at a time. */
2612 for (;;) {
2613 obj = PyIter_Next(iter);
2614 if (obj == NULL) {
2615 if (PyErr_Occurred())
2616 return -1;
2617 break;
2618 }
2619 i = save(self, obj, 0);
2620 Py_DECREF(obj);
2621 if (i < 0)
2622 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002623 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002624 return -1;
2625 }
2626 return 0;
2627 }
2628
2629 /* proto > 0: write in batches of BATCHSIZE. */
2630 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002631 /* Get first item */
2632 firstitem = PyIter_Next(iter);
2633 if (firstitem == NULL) {
2634 if (PyErr_Occurred())
2635 goto error;
2636
2637 /* nothing more to add */
2638 break;
2639 }
2640
2641 /* Try to get a second item */
2642 obj = PyIter_Next(iter);
2643 if (obj == NULL) {
2644 if (PyErr_Occurred())
2645 goto error;
2646
2647 /* Only one item to write */
2648 if (save(self, firstitem, 0) < 0)
2649 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002650 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002651 goto error;
2652 Py_CLEAR(firstitem);
2653 break;
2654 }
2655
2656 /* More than one item to write */
2657
2658 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002659 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002660 goto error;
2661
2662 if (save(self, firstitem, 0) < 0)
2663 goto error;
2664 Py_CLEAR(firstitem);
2665 n = 1;
2666
2667 /* Fetch and save up to BATCHSIZE items */
2668 while (obj) {
2669 if (save(self, obj, 0) < 0)
2670 goto error;
2671 Py_CLEAR(obj);
2672 n += 1;
2673
2674 if (n == BATCHSIZE)
2675 break;
2676
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002677 obj = PyIter_Next(iter);
2678 if (obj == NULL) {
2679 if (PyErr_Occurred())
2680 goto error;
2681 break;
2682 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002683 }
2684
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002685 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002686 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002687
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002688 } while (n == BATCHSIZE);
2689 return 0;
2690
2691 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002692 Py_XDECREF(firstitem);
2693 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002694 return -1;
2695}
2696
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002697/* This is a variant of batch_list() above, specialized for lists (with no
2698 * support for list subclasses). Like batch_list(), we batch up chunks of
2699 * MARK item item ... item APPENDS
2700 * opcode sequences. Calling code should have arranged to first create an
2701 * empty list, or list-like object, for the APPENDS to operate on.
2702 * Returns 0 on success, -1 on error.
2703 *
2704 * This version is considerably faster than batch_list(), if less general.
2705 *
2706 * Note that this only works for protocols > 0.
2707 */
2708static int
2709batch_list_exact(PicklerObject *self, PyObject *obj)
2710{
2711 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002712 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002713
2714 const char append_op = APPEND;
2715 const char appends_op = APPENDS;
2716 const char mark_op = MARK;
2717
2718 assert(obj != NULL);
2719 assert(self->proto > 0);
2720 assert(PyList_CheckExact(obj));
2721
2722 if (PyList_GET_SIZE(obj) == 1) {
2723 item = PyList_GET_ITEM(obj, 0);
2724 if (save(self, item, 0) < 0)
2725 return -1;
2726 if (_Pickler_Write(self, &append_op, 1) < 0)
2727 return -1;
2728 return 0;
2729 }
2730
2731 /* Write in batches of BATCHSIZE. */
2732 total = 0;
2733 do {
2734 this_batch = 0;
2735 if (_Pickler_Write(self, &mark_op, 1) < 0)
2736 return -1;
2737 while (total < PyList_GET_SIZE(obj)) {
2738 item = PyList_GET_ITEM(obj, total);
2739 if (save(self, item, 0) < 0)
2740 return -1;
2741 total++;
2742 if (++this_batch == BATCHSIZE)
2743 break;
2744 }
2745 if (_Pickler_Write(self, &appends_op, 1) < 0)
2746 return -1;
2747
2748 } while (total < PyList_GET_SIZE(obj));
2749
2750 return 0;
2751}
2752
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002753static int
2754save_list(PicklerObject *self, PyObject *obj)
2755{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002756 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002757 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002758 int status = 0;
2759
2760 if (self->fast && !fast_save_enter(self, obj))
2761 goto error;
2762
2763 /* Create an empty list. */
2764 if (self->bin) {
2765 header[0] = EMPTY_LIST;
2766 len = 1;
2767 }
2768 else {
2769 header[0] = MARK;
2770 header[1] = LIST;
2771 len = 2;
2772 }
2773
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002774 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002775 goto error;
2776
2777 /* Get list length, and bow out early if empty. */
2778 if ((len = PyList_Size(obj)) < 0)
2779 goto error;
2780
2781 if (memo_put(self, obj) < 0)
2782 goto error;
2783
2784 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002785 /* Materialize the list elements. */
2786 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002787 if (Py_EnterRecursiveCall(" while pickling an object"))
2788 goto error;
2789 status = batch_list_exact(self, obj);
2790 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002791 } else {
2792 PyObject *iter = PyObject_GetIter(obj);
2793 if (iter == NULL)
2794 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002795
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002796 if (Py_EnterRecursiveCall(" while pickling an object")) {
2797 Py_DECREF(iter);
2798 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002799 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002800 status = batch_list(self, iter);
2801 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002802 Py_DECREF(iter);
2803 }
2804 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002805 if (0) {
2806 error:
2807 status = -1;
2808 }
2809
2810 if (self->fast && !fast_save_leave(self, obj))
2811 status = -1;
2812
2813 return status;
2814}
2815
2816/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2817 * MARK key value ... key value SETITEMS
2818 * opcode sequences. Calling code should have arranged to first create an
2819 * empty dict, or dict-like object, for the SETITEMS to operate on.
2820 * Returns 0 on success, <0 on error.
2821 *
2822 * This is very much like batch_list(). The difference between saving
2823 * elements directly, and picking apart two-tuples, is so long-winded at
2824 * the C level, though, that attempts to combine these routines were too
2825 * ugly to bear.
2826 */
2827static int
2828batch_dict(PicklerObject *self, PyObject *iter)
2829{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002830 PyObject *obj = NULL;
2831 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002832 int i, n;
2833
2834 const char mark_op = MARK;
2835 const char setitem_op = SETITEM;
2836 const char setitems_op = SETITEMS;
2837
2838 assert(iter != NULL);
2839
2840 if (self->proto == 0) {
2841 /* SETITEMS isn't available; do one at a time. */
2842 for (;;) {
2843 obj = PyIter_Next(iter);
2844 if (obj == NULL) {
2845 if (PyErr_Occurred())
2846 return -1;
2847 break;
2848 }
2849 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2850 PyErr_SetString(PyExc_TypeError, "dict items "
2851 "iterator must return 2-tuples");
2852 return -1;
2853 }
2854 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2855 if (i >= 0)
2856 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2857 Py_DECREF(obj);
2858 if (i < 0)
2859 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002860 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002861 return -1;
2862 }
2863 return 0;
2864 }
2865
2866 /* proto > 0: write in batches of BATCHSIZE. */
2867 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002868 /* Get first item */
2869 firstitem = PyIter_Next(iter);
2870 if (firstitem == NULL) {
2871 if (PyErr_Occurred())
2872 goto error;
2873
2874 /* nothing more to add */
2875 break;
2876 }
2877 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2878 PyErr_SetString(PyExc_TypeError, "dict items "
2879 "iterator must return 2-tuples");
2880 goto error;
2881 }
2882
2883 /* Try to get a second item */
2884 obj = PyIter_Next(iter);
2885 if (obj == NULL) {
2886 if (PyErr_Occurred())
2887 goto error;
2888
2889 /* Only one item to write */
2890 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2891 goto error;
2892 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2893 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002894 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002895 goto error;
2896 Py_CLEAR(firstitem);
2897 break;
2898 }
2899
2900 /* More than one item to write */
2901
2902 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002903 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002904 goto error;
2905
2906 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2907 goto error;
2908 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2909 goto error;
2910 Py_CLEAR(firstitem);
2911 n = 1;
2912
2913 /* Fetch and save up to BATCHSIZE items */
2914 while (obj) {
2915 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2916 PyErr_SetString(PyExc_TypeError, "dict items "
2917 "iterator must return 2-tuples");
2918 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002919 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002920 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2921 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2922 goto error;
2923 Py_CLEAR(obj);
2924 n += 1;
2925
2926 if (n == BATCHSIZE)
2927 break;
2928
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002929 obj = PyIter_Next(iter);
2930 if (obj == NULL) {
2931 if (PyErr_Occurred())
2932 goto error;
2933 break;
2934 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002935 }
2936
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002937 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002938 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002939
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002940 } while (n == BATCHSIZE);
2941 return 0;
2942
2943 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002944 Py_XDECREF(firstitem);
2945 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002946 return -1;
2947}
2948
Collin Winter5c9b02d2009-05-25 05:43:30 +00002949/* This is a variant of batch_dict() above that specializes for dicts, with no
2950 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2951 * MARK key value ... key value SETITEMS
2952 * opcode sequences. Calling code should have arranged to first create an
2953 * empty dict, or dict-like object, for the SETITEMS to operate on.
2954 * Returns 0 on success, -1 on error.
2955 *
2956 * Note that this currently doesn't work for protocol 0.
2957 */
2958static int
2959batch_dict_exact(PicklerObject *self, PyObject *obj)
2960{
2961 PyObject *key = NULL, *value = NULL;
2962 int i;
2963 Py_ssize_t dict_size, ppos = 0;
2964
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002965 const char mark_op = MARK;
2966 const char setitem_op = SETITEM;
2967 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002968
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002969 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002970 assert(self->proto > 0);
2971
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002972 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002973
2974 /* Special-case len(d) == 1 to save space. */
2975 if (dict_size == 1) {
2976 PyDict_Next(obj, &ppos, &key, &value);
2977 if (save(self, key, 0) < 0)
2978 return -1;
2979 if (save(self, value, 0) < 0)
2980 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002981 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002982 return -1;
2983 return 0;
2984 }
2985
2986 /* Write in batches of BATCHSIZE. */
2987 do {
2988 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002989 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002990 return -1;
2991 while (PyDict_Next(obj, &ppos, &key, &value)) {
2992 if (save(self, key, 0) < 0)
2993 return -1;
2994 if (save(self, value, 0) < 0)
2995 return -1;
2996 if (++i == BATCHSIZE)
2997 break;
2998 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002999 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003000 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003001 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003002 PyErr_Format(
3003 PyExc_RuntimeError,
3004 "dictionary changed size during iteration");
3005 return -1;
3006 }
3007
3008 } while (i == BATCHSIZE);
3009 return 0;
3010}
3011
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003012static int
3013save_dict(PicklerObject *self, PyObject *obj)
3014{
3015 PyObject *items, *iter;
3016 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003017 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003018 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003019 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003020
3021 if (self->fast && !fast_save_enter(self, obj))
3022 goto error;
3023
3024 /* Create an empty dict. */
3025 if (self->bin) {
3026 header[0] = EMPTY_DICT;
3027 len = 1;
3028 }
3029 else {
3030 header[0] = MARK;
3031 header[1] = DICT;
3032 len = 2;
3033 }
3034
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003035 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003036 goto error;
3037
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003038 if (memo_put(self, obj) < 0)
3039 goto error;
3040
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003041 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003042 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003043 if (PyDict_CheckExact(obj) && self->proto > 0) {
3044 /* We can take certain shortcuts if we know this is a dict and
3045 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003046 if (Py_EnterRecursiveCall(" while pickling an object"))
3047 goto error;
3048 status = batch_dict_exact(self, obj);
3049 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003050 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003051 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003052
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003053 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003054 if (items == NULL)
3055 goto error;
3056 iter = PyObject_GetIter(items);
3057 Py_DECREF(items);
3058 if (iter == NULL)
3059 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003060 if (Py_EnterRecursiveCall(" while pickling an object")) {
3061 Py_DECREF(iter);
3062 goto error;
3063 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003064 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003065 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003066 Py_DECREF(iter);
3067 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003068 }
3069
3070 if (0) {
3071 error:
3072 status = -1;
3073 }
3074
3075 if (self->fast && !fast_save_leave(self, obj))
3076 status = -1;
3077
3078 return status;
3079}
3080
3081static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003082save_set(PicklerObject *self, PyObject *obj)
3083{
3084 PyObject *item;
3085 int i;
3086 Py_ssize_t set_size, ppos = 0;
3087 Py_hash_t hash;
3088
3089 const char empty_set_op = EMPTY_SET;
3090 const char mark_op = MARK;
3091 const char additems_op = ADDITEMS;
3092
3093 if (self->proto < 4) {
3094 PyObject *items;
3095 PyObject *reduce_value;
3096 int status;
3097
3098 items = PySequence_List(obj);
3099 if (items == NULL) {
3100 return -1;
3101 }
3102 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3103 Py_DECREF(items);
3104 if (reduce_value == NULL) {
3105 return -1;
3106 }
3107 /* save_reduce() will memoize the object automatically. */
3108 status = save_reduce(self, reduce_value, obj);
3109 Py_DECREF(reduce_value);
3110 return status;
3111 }
3112
3113 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3114 return -1;
3115
3116 if (memo_put(self, obj) < 0)
3117 return -1;
3118
3119 set_size = PySet_GET_SIZE(obj);
3120 if (set_size == 0)
3121 return 0; /* nothing to do */
3122
3123 /* Write in batches of BATCHSIZE. */
3124 do {
3125 i = 0;
3126 if (_Pickler_Write(self, &mark_op, 1) < 0)
3127 return -1;
3128 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3129 if (save(self, item, 0) < 0)
3130 return -1;
3131 if (++i == BATCHSIZE)
3132 break;
3133 }
3134 if (_Pickler_Write(self, &additems_op, 1) < 0)
3135 return -1;
3136 if (PySet_GET_SIZE(obj) != set_size) {
3137 PyErr_Format(
3138 PyExc_RuntimeError,
3139 "set changed size during iteration");
3140 return -1;
3141 }
3142 } while (i == BATCHSIZE);
3143
3144 return 0;
3145}
3146
3147static int
3148save_frozenset(PicklerObject *self, PyObject *obj)
3149{
3150 PyObject *iter;
3151
3152 const char mark_op = MARK;
3153 const char frozenset_op = FROZENSET;
3154
3155 if (self->fast && !fast_save_enter(self, obj))
3156 return -1;
3157
3158 if (self->proto < 4) {
3159 PyObject *items;
3160 PyObject *reduce_value;
3161 int status;
3162
3163 items = PySequence_List(obj);
3164 if (items == NULL) {
3165 return -1;
3166 }
3167 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3168 items);
3169 Py_DECREF(items);
3170 if (reduce_value == NULL) {
3171 return -1;
3172 }
3173 /* save_reduce() will memoize the object automatically. */
3174 status = save_reduce(self, reduce_value, obj);
3175 Py_DECREF(reduce_value);
3176 return status;
3177 }
3178
3179 if (_Pickler_Write(self, &mark_op, 1) < 0)
3180 return -1;
3181
3182 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003183 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003184 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003185 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003186 for (;;) {
3187 PyObject *item;
3188
3189 item = PyIter_Next(iter);
3190 if (item == NULL) {
3191 if (PyErr_Occurred()) {
3192 Py_DECREF(iter);
3193 return -1;
3194 }
3195 break;
3196 }
3197 if (save(self, item, 0) < 0) {
3198 Py_DECREF(item);
3199 Py_DECREF(iter);
3200 return -1;
3201 }
3202 Py_DECREF(item);
3203 }
3204 Py_DECREF(iter);
3205
3206 /* If the object is already in the memo, this means it is
3207 recursive. In this case, throw away everything we put on the
3208 stack, and fetch the object back from the memo. */
3209 if (PyMemoTable_Get(self->memo, obj)) {
3210 const char pop_mark_op = POP_MARK;
3211
3212 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3213 return -1;
3214 if (memo_get(self, obj) < 0)
3215 return -1;
3216 return 0;
3217 }
3218
3219 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3220 return -1;
3221 if (memo_put(self, obj) < 0)
3222 return -1;
3223
3224 return 0;
3225}
3226
3227static int
3228fix_imports(PyObject **module_name, PyObject **global_name)
3229{
3230 PyObject *key;
3231 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003232 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003233
3234 key = PyTuple_Pack(2, *module_name, *global_name);
3235 if (key == NULL)
3236 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003237 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003238 Py_DECREF(key);
3239 if (item) {
3240 PyObject *fixed_module_name;
3241 PyObject *fixed_global_name;
3242
3243 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3244 PyErr_Format(PyExc_RuntimeError,
3245 "_compat_pickle.REVERSE_NAME_MAPPING values "
3246 "should be 2-tuples, not %.200s",
3247 Py_TYPE(item)->tp_name);
3248 return -1;
3249 }
3250 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3251 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3252 if (!PyUnicode_Check(fixed_module_name) ||
3253 !PyUnicode_Check(fixed_global_name)) {
3254 PyErr_Format(PyExc_RuntimeError,
3255 "_compat_pickle.REVERSE_NAME_MAPPING values "
3256 "should be pairs of str, not (%.200s, %.200s)",
3257 Py_TYPE(fixed_module_name)->tp_name,
3258 Py_TYPE(fixed_global_name)->tp_name);
3259 return -1;
3260 }
3261
3262 Py_CLEAR(*module_name);
3263 Py_CLEAR(*global_name);
3264 Py_INCREF(fixed_module_name);
3265 Py_INCREF(fixed_global_name);
3266 *module_name = fixed_module_name;
3267 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003268 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003269 }
3270 else if (PyErr_Occurred()) {
3271 return -1;
3272 }
3273
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003274 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003275 if (item) {
3276 if (!PyUnicode_Check(item)) {
3277 PyErr_Format(PyExc_RuntimeError,
3278 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3279 "should be strings, not %.200s",
3280 Py_TYPE(item)->tp_name);
3281 return -1;
3282 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003283 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003284 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003285 }
3286 else if (PyErr_Occurred()) {
3287 return -1;
3288 }
3289
3290 return 0;
3291}
3292
3293static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003294save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3295{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003296 PyObject *global_name = NULL;
3297 PyObject *module_name = NULL;
3298 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003299 PyObject *parent = NULL;
3300 PyObject *dotted_path = NULL;
3301 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003302 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003303 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003304 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003305 _Py_IDENTIFIER(__name__);
3306 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003307
3308 const char global_op = GLOBAL;
3309
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003310 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003311 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003312 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003313 }
3314 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003315 if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0)
3316 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003317 if (global_name == NULL) {
3318 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3319 if (global_name == NULL)
3320 goto error;
3321 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003322 }
3323
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003324 dotted_path = get_dotted_path(module, global_name);
3325 if (dotted_path == NULL)
3326 goto error;
3327 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003328 if (module_name == NULL)
3329 goto error;
3330
3331 /* XXX: Change to use the import C API directly with level=0 to disallow
3332 relative imports.
3333
3334 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3335 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3336 custom import functions (IMHO, this would be a nice security
3337 feature). The import C API would need to be extended to support the
3338 extra parameters of __import__ to fix that. */
3339 module = PyImport_Import(module_name);
3340 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003341 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003342 "Can't pickle %R: import of module %R failed",
3343 obj, module_name);
3344 goto error;
3345 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003346 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3347 Py_INCREF(lastname);
3348 cls = get_deep_attribute(module, dotted_path, &parent);
3349 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003350 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003351 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003352 "Can't pickle %R: attribute lookup %S on %S failed",
3353 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003354 goto error;
3355 }
3356 if (cls != obj) {
3357 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003358 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003359 "Can't pickle %R: it's not the same object as %S.%S",
3360 obj, module_name, global_name);
3361 goto error;
3362 }
3363 Py_DECREF(cls);
3364
3365 if (self->proto >= 2) {
3366 /* See whether this is in the extension registry, and if
3367 * so generate an EXT opcode.
3368 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003369 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003370 PyObject *code_obj; /* extension code as Python object */
3371 long code; /* extension code as C value */
3372 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003373 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003374
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003375 extension_key = PyTuple_Pack(2, module_name, global_name);
3376 if (extension_key == NULL) {
3377 goto error;
3378 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003379 code_obj = PyDict_GetItemWithError(st->extension_registry,
3380 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003381 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003382 /* The object is not registered in the extension registry.
3383 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003384 if (code_obj == NULL) {
3385 if (PyErr_Occurred()) {
3386 goto error;
3387 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003388 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003389 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003390
3391 /* XXX: pickle.py doesn't check neither the type, nor the range
3392 of the value returned by the extension_registry. It should for
3393 consistency. */
3394
3395 /* Verify code_obj has the right type and value. */
3396 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003397 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003398 "Can't pickle %R: extension code %R isn't an integer",
3399 obj, code_obj);
3400 goto error;
3401 }
3402 code = PyLong_AS_LONG(code_obj);
3403 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003404 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003405 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3406 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003407 goto error;
3408 }
3409
3410 /* Generate an EXT opcode. */
3411 if (code <= 0xff) {
3412 pdata[0] = EXT1;
3413 pdata[1] = (unsigned char)code;
3414 n = 2;
3415 }
3416 else if (code <= 0xffff) {
3417 pdata[0] = EXT2;
3418 pdata[1] = (unsigned char)(code & 0xff);
3419 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3420 n = 3;
3421 }
3422 else {
3423 pdata[0] = EXT4;
3424 pdata[1] = (unsigned char)(code & 0xff);
3425 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3426 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3427 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3428 n = 5;
3429 }
3430
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003431 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003432 goto error;
3433 }
3434 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003435 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003436 if (parent == module) {
3437 Py_INCREF(lastname);
3438 Py_DECREF(global_name);
3439 global_name = lastname;
3440 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003441 if (self->proto >= 4) {
3442 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003443
Christian Heimese8b1ba12013-11-23 21:13:39 +01003444 if (save(self, module_name, 0) < 0)
3445 goto error;
3446 if (save(self, global_name, 0) < 0)
3447 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003448
3449 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3450 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003451 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003452 else if (parent != module) {
3453 PickleState *st = _Pickle_GetGlobalState();
3454 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3455 st->getattr, parent, lastname);
Alexey Izbyshevf8c06b02018-08-22 07:51:25 +03003456 if (reduce_value == NULL)
3457 goto error;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003458 status = save_reduce(self, reduce_value, NULL);
3459 Py_DECREF(reduce_value);
3460 if (status < 0)
3461 goto error;
3462 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003463 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003464 /* Generate a normal global opcode if we are using a pickle
3465 protocol < 4, or if the object is not registered in the
3466 extension registry. */
3467 PyObject *encoded;
3468 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003469
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003470 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003471 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003472
3473 /* For protocol < 3 and if the user didn't request against doing
3474 so, we convert module names to the old 2.x module names. */
3475 if (self->proto < 3 && self->fix_imports) {
3476 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003477 goto error;
3478 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003479 }
3480
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003481 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3482 both the module name and the global name using UTF-8. We do so
3483 only when we are using the pickle protocol newer than version
3484 3. This is to ensure compatibility with older Unpickler running
3485 on Python 2.x. */
3486 if (self->proto == 3) {
3487 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003488 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003489 else {
3490 unicode_encoder = PyUnicode_AsASCIIString;
3491 }
3492 encoded = unicode_encoder(module_name);
3493 if (encoded == NULL) {
3494 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003495 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003496 "can't pickle module identifier '%S' using "
3497 "pickle protocol %i",
3498 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003499 goto error;
3500 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003501 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3502 PyBytes_GET_SIZE(encoded)) < 0) {
3503 Py_DECREF(encoded);
3504 goto error;
3505 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003506 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003507 if(_Pickler_Write(self, "\n", 1) < 0)
3508 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003509
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003510 /* Save the name of the module. */
3511 encoded = unicode_encoder(global_name);
3512 if (encoded == NULL) {
3513 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003514 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003515 "can't pickle global identifier '%S' using "
3516 "pickle protocol %i",
3517 global_name, self->proto);
3518 goto error;
3519 }
3520 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3521 PyBytes_GET_SIZE(encoded)) < 0) {
3522 Py_DECREF(encoded);
3523 goto error;
3524 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003525 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003526 if (_Pickler_Write(self, "\n", 1) < 0)
3527 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003528 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003529 /* Memoize the object. */
3530 if (memo_put(self, obj) < 0)
3531 goto error;
3532 }
3533
3534 if (0) {
3535 error:
3536 status = -1;
3537 }
3538 Py_XDECREF(module_name);
3539 Py_XDECREF(global_name);
3540 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003541 Py_XDECREF(parent);
3542 Py_XDECREF(dotted_path);
3543 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003544
3545 return status;
3546}
3547
3548static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003549save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3550{
3551 PyObject *reduce_value;
3552 int status;
3553
3554 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3555 if (reduce_value == NULL) {
3556 return -1;
3557 }
3558 status = save_reduce(self, reduce_value, obj);
3559 Py_DECREF(reduce_value);
3560 return status;
3561}
3562
3563static int
3564save_type(PicklerObject *self, PyObject *obj)
3565{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003566 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003567 return save_singleton_type(self, obj, Py_None);
3568 }
3569 else if (obj == (PyObject *)&PyEllipsis_Type) {
3570 return save_singleton_type(self, obj, Py_Ellipsis);
3571 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003572 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003573 return save_singleton_type(self, obj, Py_NotImplemented);
3574 }
3575 return save_global(self, obj, NULL);
3576}
3577
3578static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003579save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003580{
3581 PyObject *pid = NULL;
3582 int status = 0;
3583
3584 const char persid_op = PERSID;
3585 const char binpersid_op = BINPERSID;
3586
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003587 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003588 if (pid == NULL)
3589 return -1;
3590
3591 if (pid != Py_None) {
3592 if (self->bin) {
3593 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003594 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003595 goto error;
3596 }
3597 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003598 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003599
3600 pid_str = PyObject_Str(pid);
3601 if (pid_str == NULL)
3602 goto error;
3603
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003604 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003605 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003606 if (!PyUnicode_IS_ASCII(pid_str)) {
3607 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3608 "persistent IDs in protocol 0 must be "
3609 "ASCII strings");
3610 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003611 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003612 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003613
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003614 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003615 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3616 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3617 _Pickler_Write(self, "\n", 1) < 0) {
3618 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003619 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003620 }
3621 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003622 }
3623 status = 1;
3624 }
3625
3626 if (0) {
3627 error:
3628 status = -1;
3629 }
3630 Py_XDECREF(pid);
3631
3632 return status;
3633}
3634
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003635static PyObject *
3636get_class(PyObject *obj)
3637{
3638 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003639 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003640
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003641 if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) {
3642 cls = (PyObject *) Py_TYPE(obj);
3643 Py_INCREF(cls);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003644 }
3645 return cls;
3646}
3647
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003648/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3649 * appropriate __reduce__ method for obj.
3650 */
3651static int
3652save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3653{
3654 PyObject *callable;
3655 PyObject *argtup;
3656 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003657 PyObject *listitems = Py_None;
3658 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003659 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003660 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003661 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003662
3663 const char reduce_op = REDUCE;
3664 const char build_op = BUILD;
3665 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003666 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003667
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003668 size = PyTuple_Size(args);
3669 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003670 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003671 "__reduce__ must contain 2 through 5 elements");
3672 return -1;
3673 }
3674
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003675 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3676 &callable, &argtup, &state, &listitems, &dictitems))
3677 return -1;
3678
3679 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003680 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003681 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003682 return -1;
3683 }
3684 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003685 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003686 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003687 return -1;
3688 }
3689
3690 if (state == Py_None)
3691 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003692
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003693 if (listitems == Py_None)
3694 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003695 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003696 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003697 "returned by __reduce__ must be an iterator, not %s",
3698 Py_TYPE(listitems)->tp_name);
3699 return -1;
3700 }
3701
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003702 if (dictitems == Py_None)
3703 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003704 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003705 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003706 "returned by __reduce__ must be an iterator, not %s",
3707 Py_TYPE(dictitems)->tp_name);
3708 return -1;
3709 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003710
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003711 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003712 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003713 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003714
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003715 if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) {
3716 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003717 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02003718 if (name != NULL && PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003719 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003720 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3721 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003722 if (!use_newobj_ex) {
3723 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003724 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003725 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003726 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003727 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003728 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003729
3730 if (use_newobj_ex) {
3731 PyObject *cls;
3732 PyObject *args;
3733 PyObject *kwargs;
3734
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003735 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003736 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003737 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003738 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003739 return -1;
3740 }
3741
3742 cls = PyTuple_GET_ITEM(argtup, 0);
3743 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003744 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003745 "first item from NEWOBJ_EX argument tuple must "
3746 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3747 return -1;
3748 }
3749 args = PyTuple_GET_ITEM(argtup, 1);
3750 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003751 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003752 "second item from NEWOBJ_EX argument tuple must "
3753 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3754 return -1;
3755 }
3756 kwargs = PyTuple_GET_ITEM(argtup, 2);
3757 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003758 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003759 "third item from NEWOBJ_EX argument tuple must "
3760 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3761 return -1;
3762 }
3763
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003764 if (self->proto >= 4) {
3765 if (save(self, cls, 0) < 0 ||
3766 save(self, args, 0) < 0 ||
3767 save(self, kwargs, 0) < 0 ||
3768 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3769 return -1;
3770 }
3771 }
3772 else {
3773 PyObject *newargs;
3774 PyObject *cls_new;
3775 Py_ssize_t i;
3776 _Py_IDENTIFIER(__new__);
3777
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003778 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003779 if (newargs == NULL)
3780 return -1;
3781
3782 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3783 if (cls_new == NULL) {
3784 Py_DECREF(newargs);
3785 return -1;
3786 }
3787 PyTuple_SET_ITEM(newargs, 0, cls_new);
3788 Py_INCREF(cls);
3789 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003790 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003791 PyObject *item = PyTuple_GET_ITEM(args, i);
3792 Py_INCREF(item);
3793 PyTuple_SET_ITEM(newargs, i + 2, item);
3794 }
3795
3796 callable = PyObject_Call(st->partial, newargs, kwargs);
3797 Py_DECREF(newargs);
3798 if (callable == NULL)
3799 return -1;
3800
3801 newargs = PyTuple_New(0);
3802 if (newargs == NULL) {
3803 Py_DECREF(callable);
3804 return -1;
3805 }
3806
3807 if (save(self, callable, 0) < 0 ||
3808 save(self, newargs, 0) < 0 ||
3809 _Pickler_Write(self, &reduce_op, 1) < 0) {
3810 Py_DECREF(newargs);
3811 Py_DECREF(callable);
3812 return -1;
3813 }
3814 Py_DECREF(newargs);
3815 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003816 }
3817 }
3818 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003819 PyObject *cls;
3820 PyObject *newargtup;
3821 PyObject *obj_class;
3822 int p;
3823
3824 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003825 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003826 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003827 return -1;
3828 }
3829
3830 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003831 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003832 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003833 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003834 return -1;
3835 }
3836
3837 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003838 obj_class = get_class(obj);
Zackery Spytz25d38972018-12-05 11:29:20 -07003839 if (obj_class == NULL) {
3840 return -1;
3841 }
3842 p = obj_class != cls;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003843 Py_DECREF(obj_class);
3844 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003845 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003846 "__newobj__ args has the wrong class");
3847 return -1;
3848 }
3849 }
3850 /* XXX: These calls save() are prone to infinite recursion. Imagine
3851 what happen if the value returned by the __reduce__() method of
3852 some extension type contains another object of the same type. Ouch!
3853
3854 Here is a quick example, that I ran into, to illustrate what I
3855 mean:
3856
3857 >>> import pickle, copyreg
3858 >>> copyreg.dispatch_table.pop(complex)
3859 >>> pickle.dumps(1+2j)
3860 Traceback (most recent call last):
3861 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003862 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003863
3864 Removing the complex class from copyreg.dispatch_table made the
3865 __reduce_ex__() method emit another complex object:
3866
3867 >>> (1+1j).__reduce_ex__(2)
3868 (<function __newobj__ at 0xb7b71c3c>,
3869 (<class 'complex'>, (1+1j)), None, None, None)
3870
3871 Thus when save() was called on newargstup (the 2nd item) recursion
3872 ensued. Of course, the bug was in the complex class which had a
3873 broken __getnewargs__() that emitted another complex object. But,
3874 the point, here, is it is quite easy to end up with a broken reduce
3875 function. */
3876
3877 /* Save the class and its __new__ arguments. */
3878 if (save(self, cls, 0) < 0)
3879 return -1;
3880
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003881 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003882 if (newargtup == NULL)
3883 return -1;
3884
3885 p = save(self, newargtup, 0);
3886 Py_DECREF(newargtup);
3887 if (p < 0)
3888 return -1;
3889
3890 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003891 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003892 return -1;
3893 }
3894 else { /* Not using NEWOBJ. */
3895 if (save(self, callable, 0) < 0 ||
3896 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003897 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003898 return -1;
3899 }
3900
3901 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3902 the caller do not want to memoize the object. Not particularly useful,
3903 but that is to mimic the behavior save_reduce() in pickle.py when
3904 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003905 if (obj != NULL) {
3906 /* If the object is already in the memo, this means it is
3907 recursive. In this case, throw away everything we put on the
3908 stack, and fetch the object back from the memo. */
3909 if (PyMemoTable_Get(self->memo, obj)) {
3910 const char pop_op = POP;
3911
3912 if (_Pickler_Write(self, &pop_op, 1) < 0)
3913 return -1;
3914 if (memo_get(self, obj) < 0)
3915 return -1;
3916
3917 return 0;
3918 }
3919 else if (memo_put(self, obj) < 0)
3920 return -1;
3921 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003922
3923 if (listitems && batch_list(self, listitems) < 0)
3924 return -1;
3925
3926 if (dictitems && batch_dict(self, dictitems) < 0)
3927 return -1;
3928
3929 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003930 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003931 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003932 return -1;
3933 }
3934
3935 return 0;
3936}
3937
3938static int
3939save(PicklerObject *self, PyObject *obj, int pers_save)
3940{
3941 PyTypeObject *type;
3942 PyObject *reduce_func = NULL;
3943 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003944 int status = 0;
3945
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003946 if (_Pickler_OpcodeBoundary(self) < 0)
3947 return -1;
3948
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003949 /* The extra pers_save argument is necessary to avoid calling save_pers()
3950 on its returned object. */
3951 if (!pers_save && self->pers_func) {
3952 /* save_pers() returns:
3953 -1 to signal an error;
3954 0 if it did nothing successfully;
3955 1 if a persistent id was saved.
3956 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003957 if ((status = save_pers(self, obj)) != 0)
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003958 return status;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003959 }
3960
3961 type = Py_TYPE(obj);
3962
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003963 /* The old cPickle had an optimization that used switch-case statement
3964 dispatching on the first letter of the type name. This has was removed
3965 since benchmarks shown that this optimization was actually slowing
3966 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003967
3968 /* Atom types; these aren't memoized, so don't check the memo. */
3969
3970 if (obj == Py_None) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003971 return save_none(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003972 }
3973 else if (obj == Py_False || obj == Py_True) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003974 return save_bool(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003975 }
3976 else if (type == &PyLong_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003977 return save_long(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003978 }
3979 else if (type == &PyFloat_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003980 return save_float(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003981 }
3982
3983 /* Check the memo to see if it has the object. If so, generate
3984 a GET (or BINGET) opcode, instead of pickling the object
3985 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003986 if (PyMemoTable_Get(self->memo, obj)) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003987 return memo_get(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003988 }
3989
3990 if (type == &PyBytes_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003991 return save_bytes(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003992 }
3993 else if (type == &PyUnicode_Type) {
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003994 return save_unicode(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003995 }
Serhiy Storchaka5d4cb542018-07-18 10:10:49 +03003996
3997 /* We're only calling Py_EnterRecursiveCall here so that atomic
3998 types above are pickled faster. */
3999 if (Py_EnterRecursiveCall(" while pickling an object")) {
4000 return -1;
4001 }
4002
4003 if (type == &PyDict_Type) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004004 status = save_dict(self, obj);
4005 goto done;
4006 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004007 else if (type == &PySet_Type) {
4008 status = save_set(self, obj);
4009 goto done;
4010 }
4011 else if (type == &PyFrozenSet_Type) {
4012 status = save_frozenset(self, obj);
4013 goto done;
4014 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004015 else if (type == &PyList_Type) {
4016 status = save_list(self, obj);
4017 goto done;
4018 }
4019 else if (type == &PyTuple_Type) {
4020 status = save_tuple(self, obj);
4021 goto done;
4022 }
4023 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004024 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004025 goto done;
4026 }
4027 else if (type == &PyFunction_Type) {
4028 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004029 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004030 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031
4032 /* XXX: This part needs some unit tests. */
4033
4034 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004035 * self.dispatch_table, copyreg.dispatch_table, the object's
4036 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004037 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004038 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004039 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004040 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4041 (PyObject *)type);
4042 if (reduce_func == NULL) {
4043 if (PyErr_Occurred()) {
4044 goto error;
4045 }
4046 } else {
4047 /* PyDict_GetItemWithError() returns a borrowed reference.
4048 Increase the reference count to be consistent with
4049 PyObject_GetItem and _PyObject_GetAttrId used below. */
4050 Py_INCREF(reduce_func);
4051 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004052 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004053 reduce_func = PyObject_GetItem(self->dispatch_table,
4054 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004055 if (reduce_func == NULL) {
4056 if (PyErr_ExceptionMatches(PyExc_KeyError))
4057 PyErr_Clear();
4058 else
4059 goto error;
4060 }
4061 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004062 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004063 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004064 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004065 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004066 else if (PyType_IsSubtype(type, &PyType_Type)) {
4067 status = save_global(self, obj, NULL);
4068 goto done;
4069 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004070 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004071 _Py_IDENTIFIER(__reduce__);
4072 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004073
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004074
4075 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4076 automatically defined as __reduce__. While this is convenient, this
4077 make it impossible to know which method was actually called. Of
4078 course, this is not a big deal. But still, it would be nice to let
4079 the user know which method was called when something go
4080 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4081 don't actually have to check for a __reduce__ method. */
4082
4083 /* Check for a __reduce_ex__ method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004084 if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) {
4085 goto error;
4086 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004087 if (reduce_func != NULL) {
4088 PyObject *proto;
4089 proto = PyLong_FromLong(self->proto);
4090 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004091 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004092 }
4093 }
4094 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004095 PickleState *st = _Pickle_GetGlobalState();
4096
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004097 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004098 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004100 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004101 }
4102 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004103 PyErr_Format(st->PicklingError,
4104 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004105 type->tp_name, obj);
4106 goto error;
4107 }
4108 }
4109 }
4110
4111 if (reduce_value == NULL)
4112 goto error;
4113
4114 if (PyUnicode_Check(reduce_value)) {
4115 status = save_global(self, obj, reduce_value);
4116 goto done;
4117 }
4118
4119 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004120 PickleState *st = _Pickle_GetGlobalState();
4121 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004122 "__reduce__ must return a string or tuple");
4123 goto error;
4124 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004125
4126 status = save_reduce(self, reduce_value, obj);
4127
4128 if (0) {
4129 error:
4130 status = -1;
4131 }
4132 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004133
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004134 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004135 Py_XDECREF(reduce_func);
4136 Py_XDECREF(reduce_value);
4137
4138 return status;
4139}
4140
4141static int
4142dump(PicklerObject *self, PyObject *obj)
4143{
4144 const char stop_op = STOP;
4145
4146 if (self->proto >= 2) {
4147 char header[2];
4148
4149 header[0] = PROTO;
4150 assert(self->proto >= 0 && self->proto < 256);
4151 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004152 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004153 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004154 if (self->proto >= 4)
4155 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004156 }
4157
4158 if (save(self, obj, 0) < 0 ||
Serhiy Storchakac8695292018-04-04 00:11:27 +03004159 _Pickler_Write(self, &stop_op, 1) < 0 ||
4160 _Pickler_CommitFrame(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004161 return -1;
Serhiy Storchakac8695292018-04-04 00:11:27 +03004162 self->framing = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004163 return 0;
4164}
4165
Larry Hastings61272b72014-01-07 12:41:53 -08004166/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004167
4168_pickle.Pickler.clear_memo
4169
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170Clears the pickler's "memo".
4171
4172The memo is the data structure that remembers which objects the
4173pickler has already seen, so that shared or recursive objects are
4174pickled by reference and not by value. This method is useful when
4175re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004176[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004177
Larry Hastings3cceb382014-01-04 11:09:09 -08004178static PyObject *
4179_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004180/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004181{
4182 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004183 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004184
4185 Py_RETURN_NONE;
4186}
4187
Larry Hastings61272b72014-01-07 12:41:53 -08004188/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004189
4190_pickle.Pickler.dump
4191
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004192 obj: object
4193 /
4194
4195Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004196[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004197
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004198static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004199_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004200/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004201{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004202 /* Check whether the Pickler was initialized correctly (issue3664).
4203 Developers often forget to call __init__() in their subclasses, which
4204 would trigger a segfault without this check. */
4205 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004206 PickleState *st = _Pickle_GetGlobalState();
4207 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004208 "Pickler.__init__() was not called by %s.__init__()",
4209 Py_TYPE(self)->tp_name);
4210 return NULL;
4211 }
4212
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004213 if (_Pickler_ClearBuffer(self) < 0)
4214 return NULL;
4215
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004216 if (dump(self, obj) < 0)
4217 return NULL;
4218
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004219 if (_Pickler_FlushToFile(self) < 0)
4220 return NULL;
4221
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004222 Py_RETURN_NONE;
4223}
4224
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004225/*[clinic input]
4226
4227_pickle.Pickler.__sizeof__ -> Py_ssize_t
4228
4229Returns size in memory, in bytes.
4230[clinic start generated code]*/
4231
4232static Py_ssize_t
4233_pickle_Pickler___sizeof___impl(PicklerObject *self)
4234/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4235{
4236 Py_ssize_t res, s;
4237
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004238 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004239 if (self->memo != NULL) {
4240 res += sizeof(PyMemoTable);
4241 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4242 }
4243 if (self->output_buffer != NULL) {
4244 s = _PySys_GetSizeOf(self->output_buffer);
4245 if (s == -1)
4246 return -1;
4247 res += s;
4248 }
4249 return res;
4250}
4251
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004252static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004253 _PICKLE_PICKLER_DUMP_METHODDEF
4254 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004255 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004256 {NULL, NULL} /* sentinel */
4257};
4258
4259static void
4260Pickler_dealloc(PicklerObject *self)
4261{
4262 PyObject_GC_UnTrack(self);
4263
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004264 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004265 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004266 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004267 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004268 Py_XDECREF(self->fast_memo);
4269
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004270 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004271
4272 Py_TYPE(self)->tp_free((PyObject *)self);
4273}
4274
4275static int
4276Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4277{
4278 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004279 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004280 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004281 Py_VISIT(self->fast_memo);
4282 return 0;
4283}
4284
4285static int
4286Pickler_clear(PicklerObject *self)
4287{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004288 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004289 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004290 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004291 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004292 Py_CLEAR(self->fast_memo);
4293
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004294 if (self->memo != NULL) {
4295 PyMemoTable *memo = self->memo;
4296 self->memo = NULL;
4297 PyMemoTable_Del(memo);
4298 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004299 return 0;
4300}
4301
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004302
Larry Hastings61272b72014-01-07 12:41:53 -08004303/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004304
4305_pickle.Pickler.__init__
4306
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004307 file: object
4308 protocol: object = NULL
4309 fix_imports: bool = True
4310
4311This takes a binary file for writing a pickle data stream.
4312
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004313The optional *protocol* argument tells the pickler to use the given
4314protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4315protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004316
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004317Specifying a negative protocol version selects the highest protocol
4318version supported. The higher the protocol used, the more recent the
4319version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004320
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004321The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004322bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004323writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004324this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004325
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004326If *fix_imports* is True and protocol is less than 3, pickle will try
4327to map the new Python 3 names to the old module names used in Python
43282, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004329[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004330
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004331static int
Larry Hastings89964c42015-04-14 18:07:59 -04004332_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4333 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004334/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004335{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004336 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004337 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004338
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004339 /* In case of multiple __init__() calls, clear previous content. */
4340 if (self->write != NULL)
4341 (void)Pickler_clear(self);
4342
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004343 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004344 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004345
4346 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004347 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004348
4349 /* memo and output_buffer may have already been created in _Pickler_New */
4350 if (self->memo == NULL) {
4351 self->memo = PyMemoTable_New();
4352 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004353 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004354 }
4355 self->output_len = 0;
4356 if (self->output_buffer == NULL) {
4357 self->max_output_len = WRITE_BUF_SIZE;
4358 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4359 self->max_output_len);
4360 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004361 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004362 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004363
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004364 self->fast = 0;
4365 self->fast_nesting = 0;
4366 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004367
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004368 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4369 &self->pers_func, &self->pers_func_self) < 0)
4370 {
4371 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004372 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004373
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004374 if (_PyObject_LookupAttrId((PyObject *)self,
4375 &PyId_dispatch_table, &self->dispatch_table) < 0) {
4376 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004377 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004378
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004379 return 0;
4380}
4381
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004382
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004383/* Define a proxy object for the Pickler's internal memo object. This is to
4384 * avoid breaking code like:
4385 * pickler.memo.clear()
4386 * and
4387 * pickler.memo = saved_memo
4388 * Is this a good idea? Not really, but we don't want to break code that uses
4389 * it. Note that we don't implement the entire mapping API here. This is
4390 * intentional, as these should be treated as black-box implementation details.
4391 */
4392
Larry Hastings61272b72014-01-07 12:41:53 -08004393/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004394_pickle.PicklerMemoProxy.clear
4395
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004396Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004397[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004398
Larry Hastings3cceb382014-01-04 11:09:09 -08004399static PyObject *
4400_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004401/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004402{
4403 if (self->pickler->memo)
4404 PyMemoTable_Clear(self->pickler->memo);
4405 Py_RETURN_NONE;
4406}
4407
Larry Hastings61272b72014-01-07 12:41:53 -08004408/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004409_pickle.PicklerMemoProxy.copy
4410
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004411Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004412[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004413
Larry Hastings3cceb382014-01-04 11:09:09 -08004414static PyObject *
4415_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004416/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004417{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004418 PyMemoTable *memo;
4419 PyObject *new_memo = PyDict_New();
4420 if (new_memo == NULL)
4421 return NULL;
4422
4423 memo = self->pickler->memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07004424 for (size_t i = 0; i < memo->mt_allocated; ++i) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004425 PyMemoEntry entry = memo->mt_table[i];
4426 if (entry.me_key != NULL) {
4427 int status;
4428 PyObject *key, *value;
4429
4430 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004431 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004432
4433 if (key == NULL || value == NULL) {
4434 Py_XDECREF(key);
4435 Py_XDECREF(value);
4436 goto error;
4437 }
4438 status = PyDict_SetItem(new_memo, key, value);
4439 Py_DECREF(key);
4440 Py_DECREF(value);
4441 if (status < 0)
4442 goto error;
4443 }
4444 }
4445 return new_memo;
4446
4447 error:
4448 Py_XDECREF(new_memo);
4449 return NULL;
4450}
4451
Larry Hastings61272b72014-01-07 12:41:53 -08004452/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004453_pickle.PicklerMemoProxy.__reduce__
4454
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004455Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004456[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004457
Larry Hastings3cceb382014-01-04 11:09:09 -08004458static PyObject *
4459_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004460/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004461{
4462 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004463 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004464 if (contents == NULL)
4465 return NULL;
4466
4467 reduce_value = PyTuple_New(2);
4468 if (reduce_value == NULL) {
4469 Py_DECREF(contents);
4470 return NULL;
4471 }
4472 dict_args = PyTuple_New(1);
4473 if (dict_args == NULL) {
4474 Py_DECREF(contents);
4475 Py_DECREF(reduce_value);
4476 return NULL;
4477 }
4478 PyTuple_SET_ITEM(dict_args, 0, contents);
4479 Py_INCREF((PyObject *)&PyDict_Type);
4480 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4481 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4482 return reduce_value;
4483}
4484
4485static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004486 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4487 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4488 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004489 {NULL, NULL} /* sentinel */
4490};
4491
4492static void
4493PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4494{
4495 PyObject_GC_UnTrack(self);
4496 Py_XDECREF(self->pickler);
4497 PyObject_GC_Del((PyObject *)self);
4498}
4499
4500static int
4501PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4502 visitproc visit, void *arg)
4503{
4504 Py_VISIT(self->pickler);
4505 return 0;
4506}
4507
4508static int
4509PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4510{
4511 Py_CLEAR(self->pickler);
4512 return 0;
4513}
4514
4515static PyTypeObject PicklerMemoProxyType = {
4516 PyVarObject_HEAD_INIT(NULL, 0)
4517 "_pickle.PicklerMemoProxy", /*tp_name*/
4518 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4519 0,
4520 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4521 0, /* tp_print */
4522 0, /* tp_getattr */
4523 0, /* tp_setattr */
4524 0, /* tp_compare */
4525 0, /* tp_repr */
4526 0, /* tp_as_number */
4527 0, /* tp_as_sequence */
4528 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004529 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004530 0, /* tp_call */
4531 0, /* tp_str */
4532 PyObject_GenericGetAttr, /* tp_getattro */
4533 PyObject_GenericSetAttr, /* tp_setattro */
4534 0, /* tp_as_buffer */
4535 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4536 0, /* tp_doc */
4537 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4538 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4539 0, /* tp_richcompare */
4540 0, /* tp_weaklistoffset */
4541 0, /* tp_iter */
4542 0, /* tp_iternext */
4543 picklerproxy_methods, /* tp_methods */
4544};
4545
4546static PyObject *
4547PicklerMemoProxy_New(PicklerObject *pickler)
4548{
4549 PicklerMemoProxyObject *self;
4550
4551 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4552 if (self == NULL)
4553 return NULL;
4554 Py_INCREF(pickler);
4555 self->pickler = pickler;
4556 PyObject_GC_Track(self);
4557 return (PyObject *)self;
4558}
4559
4560/*****************************************************************************/
4561
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004562static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004563Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004564{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004565 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004566}
4567
4568static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004569Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004571 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004572
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004573 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004574 PyErr_SetString(PyExc_TypeError,
4575 "attribute deletion is not supported");
4576 return -1;
4577 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004578
4579 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4580 PicklerObject *pickler =
4581 ((PicklerMemoProxyObject *)obj)->pickler;
4582
4583 new_memo = PyMemoTable_Copy(pickler->memo);
4584 if (new_memo == NULL)
4585 return -1;
4586 }
4587 else if (PyDict_Check(obj)) {
4588 Py_ssize_t i = 0;
4589 PyObject *key, *value;
4590
4591 new_memo = PyMemoTable_New();
4592 if (new_memo == NULL)
4593 return -1;
4594
4595 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004596 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004597 PyObject *memo_obj;
4598
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004599 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004600 PyErr_SetString(PyExc_TypeError,
4601 "'memo' values must be 2-item tuples");
4602 goto error;
4603 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004604 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004605 if (memo_id == -1 && PyErr_Occurred())
4606 goto error;
4607 memo_obj = PyTuple_GET_ITEM(value, 1);
4608 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4609 goto error;
4610 }
4611 }
4612 else {
4613 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +02004614 "'memo' attribute must be a PicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004615 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004616 return -1;
4617 }
4618
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004619 PyMemoTable_Del(self->memo);
4620 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004621
4622 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004623
4624 error:
4625 if (new_memo)
4626 PyMemoTable_Del(new_memo);
4627 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004628}
4629
4630static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004631Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004632{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004633 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004634 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004635 return NULL;
4636 }
4637 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004638}
4639
4640static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02004641Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004642{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004643 if (value == NULL) {
4644 PyErr_SetString(PyExc_TypeError,
4645 "attribute deletion is not supported");
4646 return -1;
4647 }
4648 if (!PyCallable_Check(value)) {
4649 PyErr_SetString(PyExc_TypeError,
4650 "persistent_id must be a callable taking one argument");
4651 return -1;
4652 }
4653
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004654 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004655 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004656 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004657
4658 return 0;
4659}
4660
4661static PyMemberDef Pickler_members[] = {
4662 {"bin", T_INT, offsetof(PicklerObject, bin)},
4663 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004664 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004665 {NULL}
4666};
4667
4668static PyGetSetDef Pickler_getsets[] = {
4669 {"memo", (getter)Pickler_get_memo,
4670 (setter)Pickler_set_memo},
4671 {"persistent_id", (getter)Pickler_get_persid,
4672 (setter)Pickler_set_persid},
4673 {NULL}
4674};
4675
4676static PyTypeObject Pickler_Type = {
4677 PyVarObject_HEAD_INIT(NULL, 0)
4678 "_pickle.Pickler" , /*tp_name*/
4679 sizeof(PicklerObject), /*tp_basicsize*/
4680 0, /*tp_itemsize*/
4681 (destructor)Pickler_dealloc, /*tp_dealloc*/
4682 0, /*tp_print*/
4683 0, /*tp_getattr*/
4684 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004685 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004686 0, /*tp_repr*/
4687 0, /*tp_as_number*/
4688 0, /*tp_as_sequence*/
4689 0, /*tp_as_mapping*/
4690 0, /*tp_hash*/
4691 0, /*tp_call*/
4692 0, /*tp_str*/
4693 0, /*tp_getattro*/
4694 0, /*tp_setattro*/
4695 0, /*tp_as_buffer*/
4696 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004697 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004698 (traverseproc)Pickler_traverse, /*tp_traverse*/
4699 (inquiry)Pickler_clear, /*tp_clear*/
4700 0, /*tp_richcompare*/
4701 0, /*tp_weaklistoffset*/
4702 0, /*tp_iter*/
4703 0, /*tp_iternext*/
4704 Pickler_methods, /*tp_methods*/
4705 Pickler_members, /*tp_members*/
4706 Pickler_getsets, /*tp_getset*/
4707 0, /*tp_base*/
4708 0, /*tp_dict*/
4709 0, /*tp_descr_get*/
4710 0, /*tp_descr_set*/
4711 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004712 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004713 PyType_GenericAlloc, /*tp_alloc*/
4714 PyType_GenericNew, /*tp_new*/
4715 PyObject_GC_Del, /*tp_free*/
4716 0, /*tp_is_gc*/
4717};
4718
Victor Stinner121aab42011-09-29 23:40:53 +02004719/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004720
4721 XXX: It would be nice to able to avoid Python function call overhead, by
4722 using directly the C version of find_class(), when find_class() is not
4723 overridden by a subclass. Although, this could become rather hackish. A
4724 simpler optimization would be to call the C function when self is not a
4725 subclass instance. */
4726static PyObject *
4727find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4728{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004729 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004730
Victor Stinner55ba38a2016-12-09 16:09:30 +01004731 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4732 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733}
4734
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004735static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004736marker(UnpicklerObject *self)
4737{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004738 Py_ssize_t mark;
4739
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004740 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004741 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004742 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004743 return -1;
4744 }
4745
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004746 mark = self->marks[--self->num_marks];
4747 self->stack->mark_set = self->num_marks != 0;
4748 self->stack->fence = self->num_marks ?
4749 self->marks[self->num_marks - 1] : 0;
4750 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004751}
4752
4753static int
4754load_none(UnpicklerObject *self)
4755{
4756 PDATA_APPEND(self->stack, Py_None, -1);
4757 return 0;
4758}
4759
4760static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004761load_int(UnpicklerObject *self)
4762{
4763 PyObject *value;
4764 char *endptr, *s;
4765 Py_ssize_t len;
4766 long x;
4767
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004768 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004769 return -1;
4770 if (len < 2)
4771 return bad_readline();
4772
4773 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004774 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004775 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004776 x = strtol(s, &endptr, 0);
4777
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004778 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004779 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004780 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004781 errno = 0;
4782 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004783 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004784 if (value == NULL) {
4785 PyErr_SetString(PyExc_ValueError,
4786 "could not convert string to int");
4787 return -1;
4788 }
4789 }
4790 else {
4791 if (len == 3 && (x == 0 || x == 1)) {
4792 if ((value = PyBool_FromLong(x)) == NULL)
4793 return -1;
4794 }
4795 else {
4796 if ((value = PyLong_FromLong(x)) == NULL)
4797 return -1;
4798 }
4799 }
4800
4801 PDATA_PUSH(self->stack, value, -1);
4802 return 0;
4803}
4804
4805static int
4806load_bool(UnpicklerObject *self, PyObject *boolean)
4807{
4808 assert(boolean == Py_True || boolean == Py_False);
4809 PDATA_APPEND(self->stack, boolean, -1);
4810 return 0;
4811}
4812
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004813/* s contains x bytes of an unsigned little-endian integer. Return its value
4814 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4815 */
4816static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004817calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004818{
4819 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004820 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004821 size_t x = 0;
4822
Serhiy Storchakae0606192015-09-29 22:10:07 +03004823 if (nbytes > (int)sizeof(size_t)) {
4824 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4825 * have 64-bit size that can't be represented on 32-bit platform.
4826 */
4827 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4828 if (s[i])
4829 return -1;
4830 }
4831 nbytes = (int)sizeof(size_t);
4832 }
4833 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004834 x |= (size_t) s[i] << (8 * i);
4835 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004836
4837 if (x > PY_SSIZE_T_MAX)
4838 return -1;
4839 else
4840 return (Py_ssize_t) x;
4841}
4842
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004843/* s contains x bytes of a little-endian integer. Return its value as a
4844 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004845 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004846 * of x-platform bugs.
4847 */
4848static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004849calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004850{
4851 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004852 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004853 long x = 0;
4854
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004855 for (i = 0; i < nbytes; i++) {
4856 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004857 }
4858
4859 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4860 * is signed, so on a box with longs bigger than 4 bytes we need
4861 * to extend a BININT's sign bit to the full width.
4862 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004863 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004864 x |= -(x & (1L << 31));
4865 }
4866
4867 return x;
4868}
4869
4870static int
4871load_binintx(UnpicklerObject *self, char *s, int size)
4872{
4873 PyObject *value;
4874 long x;
4875
4876 x = calc_binint(s, size);
4877
4878 if ((value = PyLong_FromLong(x)) == NULL)
4879 return -1;
4880
4881 PDATA_PUSH(self->stack, value, -1);
4882 return 0;
4883}
4884
4885static int
4886load_binint(UnpicklerObject *self)
4887{
4888 char *s;
4889
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004890 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004891 return -1;
4892
4893 return load_binintx(self, s, 4);
4894}
4895
4896static int
4897load_binint1(UnpicklerObject *self)
4898{
4899 char *s;
4900
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004901 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004902 return -1;
4903
4904 return load_binintx(self, s, 1);
4905}
4906
4907static int
4908load_binint2(UnpicklerObject *self)
4909{
4910 char *s;
4911
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004912 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004913 return -1;
4914
4915 return load_binintx(self, s, 2);
4916}
4917
4918static int
4919load_long(UnpicklerObject *self)
4920{
4921 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004922 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004923 Py_ssize_t len;
4924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004925 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004926 return -1;
4927 if (len < 2)
4928 return bad_readline();
4929
Mark Dickinson8dd05142009-01-20 20:43:58 +00004930 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4931 the 'L' before calling PyLong_FromString. In order to maintain
4932 compatibility with Python 3.0.0, we don't actually *require*
4933 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004934 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004935 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004936 /* XXX: Should the base argument explicitly set to 10? */
4937 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004938 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004939 return -1;
4940
4941 PDATA_PUSH(self->stack, value, -1);
4942 return 0;
4943}
4944
4945/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4946 * data following.
4947 */
4948static int
4949load_counted_long(UnpicklerObject *self, int size)
4950{
4951 PyObject *value;
4952 char *nbytes;
4953 char *pdata;
4954
4955 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004956 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004957 return -1;
4958
4959 size = calc_binint(nbytes, size);
4960 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004961 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004962 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004963 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004964 "LONG pickle has negative byte count");
4965 return -1;
4966 }
4967
4968 if (size == 0)
4969 value = PyLong_FromLong(0L);
4970 else {
4971 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004972 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004973 return -1;
4974 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4975 1 /* little endian */ , 1 /* signed */ );
4976 }
4977 if (value == NULL)
4978 return -1;
4979 PDATA_PUSH(self->stack, value, -1);
4980 return 0;
4981}
4982
4983static int
4984load_float(UnpicklerObject *self)
4985{
4986 PyObject *value;
4987 char *endptr, *s;
4988 Py_ssize_t len;
4989 double d;
4990
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004991 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004992 return -1;
4993 if (len < 2)
4994 return bad_readline();
4995
4996 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004997 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4998 if (d == -1.0 && PyErr_Occurred())
4999 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005000 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005001 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5002 return -1;
5003 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005004 value = PyFloat_FromDouble(d);
5005 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005006 return -1;
5007
5008 PDATA_PUSH(self->stack, value, -1);
5009 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005010}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005011
5012static int
5013load_binfloat(UnpicklerObject *self)
5014{
5015 PyObject *value;
5016 double x;
5017 char *s;
5018
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005019 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005020 return -1;
5021
5022 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5023 if (x == -1.0 && PyErr_Occurred())
5024 return -1;
5025
5026 if ((value = PyFloat_FromDouble(x)) == NULL)
5027 return -1;
5028
5029 PDATA_PUSH(self->stack, value, -1);
5030 return 0;
5031}
5032
5033static int
5034load_string(UnpicklerObject *self)
5035{
5036 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005037 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005038 Py_ssize_t len;
5039 char *s, *p;
5040
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005041 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005043 /* Strip the newline */
5044 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005045 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005046 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005047 p = s + 1;
5048 len -= 2;
5049 }
5050 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005051 PickleState *st = _Pickle_GetGlobalState();
5052 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005053 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005054 return -1;
5055 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005056 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005057
5058 /* Use the PyBytes API to decode the string, since that is what is used
5059 to encode, and then coerce the result to Unicode. */
5060 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005061 if (bytes == NULL)
5062 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005063
5064 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5065 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5066 if (strcmp(self->encoding, "bytes") == 0) {
5067 obj = bytes;
5068 }
5069 else {
5070 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5071 Py_DECREF(bytes);
5072 if (obj == NULL) {
5073 return -1;
5074 }
5075 }
5076
5077 PDATA_PUSH(self->stack, obj, -1);
5078 return 0;
5079}
5080
5081static int
5082load_counted_binstring(UnpicklerObject *self, int nbytes)
5083{
5084 PyObject *obj;
5085 Py_ssize_t size;
5086 char *s;
5087
5088 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005089 return -1;
5090
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005091 size = calc_binsize(s, nbytes);
5092 if (size < 0) {
5093 PickleState *st = _Pickle_GetGlobalState();
5094 PyErr_Format(st->UnpicklingError,
5095 "BINSTRING exceeds system's maximum size of %zd bytes",
5096 PY_SSIZE_T_MAX);
5097 return -1;
5098 }
5099
5100 if (_Unpickler_Read(self, &s, size) < 0)
5101 return -1;
5102
5103 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5104 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5105 if (strcmp(self->encoding, "bytes") == 0) {
5106 obj = PyBytes_FromStringAndSize(s, size);
5107 }
5108 else {
5109 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5110 }
5111 if (obj == NULL) {
5112 return -1;
5113 }
5114
5115 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005116 return 0;
5117}
5118
5119static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005120load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005121{
5122 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005123 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005124 char *s;
5125
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005126 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005127 return -1;
5128
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005129 size = calc_binsize(s, nbytes);
5130 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005131 PyErr_Format(PyExc_OverflowError,
5132 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005133 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005134 return -1;
5135 }
5136
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005137 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005138 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005139
5140 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005141 if (bytes == NULL)
5142 return -1;
5143
5144 PDATA_PUSH(self->stack, bytes, -1);
5145 return 0;
5146}
5147
5148static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005149load_unicode(UnpicklerObject *self)
5150{
5151 PyObject *str;
5152 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005153 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005154
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005155 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005156 return -1;
5157 if (len < 1)
5158 return bad_readline();
5159
5160 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5161 if (str == NULL)
5162 return -1;
5163
5164 PDATA_PUSH(self->stack, str, -1);
5165 return 0;
5166}
5167
5168static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005169load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005170{
5171 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005172 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005173 char *s;
5174
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005175 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005176 return -1;
5177
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005178 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005179 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005180 PyErr_Format(PyExc_OverflowError,
5181 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005182 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005183 return -1;
5184 }
5185
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005186 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005187 return -1;
5188
Victor Stinner485fb562010-04-13 11:07:24 +00005189 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005190 if (str == NULL)
5191 return -1;
5192
5193 PDATA_PUSH(self->stack, str, -1);
5194 return 0;
5195}
5196
5197static int
Victor Stinner21b47112016-03-14 18:09:39 +01005198load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005199{
5200 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005201
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005202 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005203 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005204
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005205 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005206 if (tuple == NULL)
5207 return -1;
5208 PDATA_PUSH(self->stack, tuple, -1);
5209 return 0;
5210}
5211
5212static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005213load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005214{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005215 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005216
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005217 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005218 return -1;
5219
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005220 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005221}
5222
5223static int
5224load_empty_list(UnpicklerObject *self)
5225{
5226 PyObject *list;
5227
5228 if ((list = PyList_New(0)) == NULL)
5229 return -1;
5230 PDATA_PUSH(self->stack, list, -1);
5231 return 0;
5232}
5233
5234static int
5235load_empty_dict(UnpicklerObject *self)
5236{
5237 PyObject *dict;
5238
5239 if ((dict = PyDict_New()) == NULL)
5240 return -1;
5241 PDATA_PUSH(self->stack, dict, -1);
5242 return 0;
5243}
5244
5245static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005246load_empty_set(UnpicklerObject *self)
5247{
5248 PyObject *set;
5249
5250 if ((set = PySet_New(NULL)) == NULL)
5251 return -1;
5252 PDATA_PUSH(self->stack, set, -1);
5253 return 0;
5254}
5255
5256static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005257load_list(UnpicklerObject *self)
5258{
5259 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005260 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005261
5262 if ((i = marker(self)) < 0)
5263 return -1;
5264
5265 list = Pdata_poplist(self->stack, i);
5266 if (list == NULL)
5267 return -1;
5268 PDATA_PUSH(self->stack, list, -1);
5269 return 0;
5270}
5271
5272static int
5273load_dict(UnpicklerObject *self)
5274{
5275 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005276 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005277
5278 if ((i = marker(self)) < 0)
5279 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005280 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005281
5282 if ((dict = PyDict_New()) == NULL)
5283 return -1;
5284
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005285 if ((j - i) % 2 != 0) {
5286 PickleState *st = _Pickle_GetGlobalState();
5287 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005288 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005289 return -1;
5290 }
5291
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005292 for (k = i + 1; k < j; k += 2) {
5293 key = self->stack->data[k - 1];
5294 value = self->stack->data[k];
5295 if (PyDict_SetItem(dict, key, value) < 0) {
5296 Py_DECREF(dict);
5297 return -1;
5298 }
5299 }
5300 Pdata_clear(self->stack, i);
5301 PDATA_PUSH(self->stack, dict, -1);
5302 return 0;
5303}
5304
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005305static int
5306load_frozenset(UnpicklerObject *self)
5307{
5308 PyObject *items;
5309 PyObject *frozenset;
5310 Py_ssize_t i;
5311
5312 if ((i = marker(self)) < 0)
5313 return -1;
5314
5315 items = Pdata_poptuple(self->stack, i);
5316 if (items == NULL)
5317 return -1;
5318
5319 frozenset = PyFrozenSet_New(items);
5320 Py_DECREF(items);
5321 if (frozenset == NULL)
5322 return -1;
5323
5324 PDATA_PUSH(self->stack, frozenset, -1);
5325 return 0;
5326}
5327
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005328static PyObject *
5329instantiate(PyObject *cls, PyObject *args)
5330{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005331 /* Caller must assure args are a tuple. Normally, args come from
5332 Pdata_poptuple which packs objects from the top of the stack
5333 into a newly created tuple. */
5334 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005335 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5336 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005337 _Py_IDENTIFIER(__new__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005338 PyObject *func;
5339 if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) {
5340 return NULL;
5341 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005342 if (func == NULL) {
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005343 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5344 }
5345 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005346 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005347 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005348}
5349
5350static int
5351load_obj(UnpicklerObject *self)
5352{
5353 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005354 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005355
5356 if ((i = marker(self)) < 0)
5357 return -1;
5358
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005359 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005360 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005361
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005362 args = Pdata_poptuple(self->stack, i + 1);
5363 if (args == NULL)
5364 return -1;
5365
5366 PDATA_POP(self->stack, cls);
5367 if (cls) {
5368 obj = instantiate(cls, args);
5369 Py_DECREF(cls);
5370 }
5371 Py_DECREF(args);
5372 if (obj == NULL)
5373 return -1;
5374
5375 PDATA_PUSH(self->stack, obj, -1);
5376 return 0;
5377}
5378
5379static int
5380load_inst(UnpicklerObject *self)
5381{
5382 PyObject *cls = NULL;
5383 PyObject *args = NULL;
5384 PyObject *obj = NULL;
5385 PyObject *module_name;
5386 PyObject *class_name;
5387 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005388 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005389 char *s;
5390
5391 if ((i = marker(self)) < 0)
5392 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005393 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005394 return -1;
5395 if (len < 2)
5396 return bad_readline();
5397
5398 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5399 identifiers are permitted in Python 3.0, since the INST opcode is only
5400 supported by older protocols on Python 2.x. */
5401 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5402 if (module_name == NULL)
5403 return -1;
5404
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005405 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005406 if (len < 2) {
5407 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005408 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005409 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005410 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005411 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005412 cls = find_class(self, module_name, class_name);
5413 Py_DECREF(class_name);
5414 }
5415 }
5416 Py_DECREF(module_name);
5417
5418 if (cls == NULL)
5419 return -1;
5420
5421 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5422 obj = instantiate(cls, args);
5423 Py_DECREF(args);
5424 }
5425 Py_DECREF(cls);
5426
5427 if (obj == NULL)
5428 return -1;
5429
5430 PDATA_PUSH(self->stack, obj, -1);
5431 return 0;
5432}
5433
5434static int
5435load_newobj(UnpicklerObject *self)
5436{
5437 PyObject *args = NULL;
5438 PyObject *clsraw = NULL;
5439 PyTypeObject *cls; /* clsraw cast to its true type */
5440 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005441 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005442
5443 /* Stack is ... cls argtuple, and we want to call
5444 * cls.__new__(cls, *argtuple).
5445 */
5446 PDATA_POP(self->stack, args);
5447 if (args == NULL)
5448 goto error;
5449 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005450 PyErr_SetString(st->UnpicklingError,
5451 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005452 goto error;
5453 }
5454
5455 PDATA_POP(self->stack, clsraw);
5456 cls = (PyTypeObject *)clsraw;
5457 if (cls == NULL)
5458 goto error;
5459 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005460 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461 "isn't a type object");
5462 goto error;
5463 }
5464 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005465 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005466 "has NULL tp_new");
5467 goto error;
5468 }
5469
5470 /* Call __new__. */
5471 obj = cls->tp_new(cls, args, NULL);
5472 if (obj == NULL)
5473 goto error;
5474
5475 Py_DECREF(args);
5476 Py_DECREF(clsraw);
5477 PDATA_PUSH(self->stack, obj, -1);
5478 return 0;
5479
5480 error:
5481 Py_XDECREF(args);
5482 Py_XDECREF(clsraw);
5483 return -1;
5484}
5485
5486static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005487load_newobj_ex(UnpicklerObject *self)
5488{
5489 PyObject *cls, *args, *kwargs;
5490 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005491 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005492
5493 PDATA_POP(self->stack, kwargs);
5494 if (kwargs == NULL) {
5495 return -1;
5496 }
5497 PDATA_POP(self->stack, args);
5498 if (args == NULL) {
5499 Py_DECREF(kwargs);
5500 return -1;
5501 }
5502 PDATA_POP(self->stack, cls);
5503 if (cls == NULL) {
5504 Py_DECREF(kwargs);
5505 Py_DECREF(args);
5506 return -1;
5507 }
Larry Hastings61272b72014-01-07 12:41:53 -08005508
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005509 if (!PyType_Check(cls)) {
5510 Py_DECREF(kwargs);
5511 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005512 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005513 "NEWOBJ_EX class argument must be a type, not %.200s",
5514 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005515 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005516 return -1;
5517 }
5518
5519 if (((PyTypeObject *)cls)->tp_new == NULL) {
5520 Py_DECREF(kwargs);
5521 Py_DECREF(args);
5522 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005523 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005524 "NEWOBJ_EX class argument doesn't have __new__");
5525 return -1;
5526 }
5527 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5528 Py_DECREF(kwargs);
5529 Py_DECREF(args);
5530 Py_DECREF(cls);
5531 if (obj == NULL) {
5532 return -1;
5533 }
5534 PDATA_PUSH(self->stack, obj, -1);
5535 return 0;
5536}
5537
5538static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005539load_global(UnpicklerObject *self)
5540{
5541 PyObject *global = NULL;
5542 PyObject *module_name;
5543 PyObject *global_name;
5544 Py_ssize_t len;
5545 char *s;
5546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005547 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005548 return -1;
5549 if (len < 2)
5550 return bad_readline();
5551 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5552 if (!module_name)
5553 return -1;
5554
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005555 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005556 if (len < 2) {
5557 Py_DECREF(module_name);
5558 return bad_readline();
5559 }
5560 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5561 if (global_name) {
5562 global = find_class(self, module_name, global_name);
5563 Py_DECREF(global_name);
5564 }
5565 }
5566 Py_DECREF(module_name);
5567
5568 if (global == NULL)
5569 return -1;
5570 PDATA_PUSH(self->stack, global, -1);
5571 return 0;
5572}
5573
5574static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005575load_stack_global(UnpicklerObject *self)
5576{
5577 PyObject *global;
5578 PyObject *module_name;
5579 PyObject *global_name;
5580
5581 PDATA_POP(self->stack, global_name);
5582 PDATA_POP(self->stack, module_name);
5583 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5584 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005585 PickleState *st = _Pickle_GetGlobalState();
5586 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005587 Py_XDECREF(global_name);
5588 Py_XDECREF(module_name);
5589 return -1;
5590 }
5591 global = find_class(self, module_name, global_name);
5592 Py_DECREF(global_name);
5593 Py_DECREF(module_name);
5594 if (global == NULL)
5595 return -1;
5596 PDATA_PUSH(self->stack, global, -1);
5597 return 0;
5598}
5599
5600static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601load_persid(UnpicklerObject *self)
5602{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005603 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604 Py_ssize_t len;
5605 char *s;
5606
5607 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005608 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005610 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005611 return bad_readline();
5612
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005613 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5614 if (pid == NULL) {
5615 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5616 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5617 "persistent IDs in protocol 0 must be "
5618 "ASCII strings");
5619 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005620 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005621 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005623 obj = call_method(self->pers_func, self->pers_func_self, pid);
5624 Py_DECREF(pid);
5625 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005626 return -1;
5627
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005628 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005629 return 0;
5630 }
5631 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005632 PickleState *st = _Pickle_GetGlobalState();
5633 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634 "A load persistent id instruction was encountered,\n"
5635 "but no persistent_load function was specified.");
5636 return -1;
5637 }
5638}
5639
5640static int
5641load_binpersid(UnpicklerObject *self)
5642{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005643 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644
5645 if (self->pers_func) {
5646 PDATA_POP(self->stack, pid);
5647 if (pid == NULL)
5648 return -1;
5649
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005650 obj = call_method(self->pers_func, self->pers_func_self, pid);
5651 Py_DECREF(pid);
5652 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005653 return -1;
5654
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005655 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005656 return 0;
5657 }
5658 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005659 PickleState *st = _Pickle_GetGlobalState();
5660 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005661 "A load persistent id instruction was encountered,\n"
5662 "but no persistent_load function was specified.");
5663 return -1;
5664 }
5665}
5666
5667static int
5668load_pop(UnpicklerObject *self)
5669{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005670 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005671
5672 /* Note that we split the (pickle.py) stack into two stacks,
5673 * an object stack and a mark stack. We have to be clever and
5674 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005675 * mark stack first, and only signalling a stack underflow if
5676 * the object stack is empty and the mark stack doesn't match
5677 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005678 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005679 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005680 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005681 self->stack->mark_set = self->num_marks != 0;
5682 self->stack->fence = self->num_marks ?
5683 self->marks[self->num_marks - 1] : 0;
5684 } else if (len <= self->stack->fence)
5685 return Pdata_stack_underflow(self->stack);
5686 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687 len--;
5688 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005689 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005691 return 0;
5692}
5693
5694static int
5695load_pop_mark(UnpicklerObject *self)
5696{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005697 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005698
5699 if ((i = marker(self)) < 0)
5700 return -1;
5701
5702 Pdata_clear(self->stack, i);
5703
5704 return 0;
5705}
5706
5707static int
5708load_dup(UnpicklerObject *self)
5709{
5710 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005711 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005712
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005713 if (len <= self->stack->fence)
5714 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005715 last = self->stack->data[len - 1];
5716 PDATA_APPEND(self->stack, last, -1);
5717 return 0;
5718}
5719
5720static int
5721load_get(UnpicklerObject *self)
5722{
5723 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005724 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005725 Py_ssize_t len;
5726 char *s;
5727
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005728 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005729 return -1;
5730 if (len < 2)
5731 return bad_readline();
5732
5733 key = PyLong_FromString(s, NULL, 10);
5734 if (key == NULL)
5735 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005736 idx = PyLong_AsSsize_t(key);
5737 if (idx == -1 && PyErr_Occurred()) {
5738 Py_DECREF(key);
5739 return -1;
5740 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005741
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005742 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005743 if (value == NULL) {
5744 if (!PyErr_Occurred())
5745 PyErr_SetObject(PyExc_KeyError, key);
5746 Py_DECREF(key);
5747 return -1;
5748 }
5749 Py_DECREF(key);
5750
5751 PDATA_APPEND(self->stack, value, -1);
5752 return 0;
5753}
5754
5755static int
5756load_binget(UnpicklerObject *self)
5757{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005758 PyObject *value;
5759 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005760 char *s;
5761
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005762 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763 return -1;
5764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005765 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005767 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005768 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005769 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005770 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005772 Py_DECREF(key);
5773 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774 return -1;
5775 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776
5777 PDATA_APPEND(self->stack, value, -1);
5778 return 0;
5779}
5780
5781static int
5782load_long_binget(UnpicklerObject *self)
5783{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005784 PyObject *value;
5785 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005786 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005787
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005788 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789 return -1;
5790
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005791 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005792
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005793 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005794 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005795 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005796 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005798 Py_DECREF(key);
5799 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005800 return -1;
5801 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005802
5803 PDATA_APPEND(self->stack, value, -1);
5804 return 0;
5805}
5806
5807/* Push an object from the extension registry (EXT[124]). nbytes is
5808 * the number of bytes following the opcode, holding the index (code) value.
5809 */
5810static int
5811load_extension(UnpicklerObject *self, int nbytes)
5812{
5813 char *codebytes; /* the nbytes bytes after the opcode */
5814 long code; /* calc_binint returns long */
5815 PyObject *py_code; /* code as a Python int */
5816 PyObject *obj; /* the object to push */
5817 PyObject *pair; /* (module_name, class_name) */
5818 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005819 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005820
5821 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005822 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005823 return -1;
5824 code = calc_binint(codebytes, nbytes);
5825 if (code <= 0) { /* note that 0 is forbidden */
5826 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005827 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005828 return -1;
5829 }
5830
5831 /* Look for the code in the cache. */
5832 py_code = PyLong_FromLong(code);
5833 if (py_code == NULL)
5834 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005835 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005836 if (obj != NULL) {
5837 /* Bingo. */
5838 Py_DECREF(py_code);
5839 PDATA_APPEND(self->stack, obj, -1);
5840 return 0;
5841 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005842 if (PyErr_Occurred()) {
5843 Py_DECREF(py_code);
5844 return -1;
5845 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846
5847 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005848 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005849 if (pair == NULL) {
5850 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005851 if (!PyErr_Occurred()) {
5852 PyErr_Format(PyExc_ValueError, "unregistered extension "
5853 "code %ld", code);
5854 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005855 return -1;
5856 }
5857 /* Since the extension registry is manipulable via Python code,
5858 * confirm that pair is really a 2-tuple of strings.
5859 */
Victor Stinnerb37672d2018-11-22 03:37:50 +01005860 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
5861 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005862 }
Victor Stinnerb37672d2018-11-22 03:37:50 +01005863
5864 module_name = PyTuple_GET_ITEM(pair, 0);
5865 if (!PyUnicode_Check(module_name)) {
5866 goto error;
5867 }
5868
5869 class_name = PyTuple_GET_ITEM(pair, 1);
5870 if (!PyUnicode_Check(class_name)) {
5871 goto error;
5872 }
5873
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005874 /* Load the object. */
5875 obj = find_class(self, module_name, class_name);
5876 if (obj == NULL) {
5877 Py_DECREF(py_code);
5878 return -1;
5879 }
5880 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005881 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005882 Py_DECREF(py_code);
5883 if (code < 0) {
5884 Py_DECREF(obj);
5885 return -1;
5886 }
5887 PDATA_PUSH(self->stack, obj, -1);
5888 return 0;
Victor Stinnerb37672d2018-11-22 03:37:50 +01005889
5890error:
5891 Py_DECREF(py_code);
5892 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5893 "isn't a 2-tuple of strings", code);
5894 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005895}
5896
5897static int
5898load_put(UnpicklerObject *self)
5899{
5900 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005901 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005902 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005903 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005904
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005905 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005906 return -1;
5907 if (len < 2)
5908 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005909 if (Py_SIZE(self->stack) <= self->stack->fence)
5910 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005911 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005912
5913 key = PyLong_FromString(s, NULL, 10);
5914 if (key == NULL)
5915 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005916 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005917 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005918 if (idx < 0) {
5919 if (!PyErr_Occurred())
5920 PyErr_SetString(PyExc_ValueError,
5921 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005922 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005923 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005924
5925 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005926}
5927
5928static int
5929load_binput(UnpicklerObject *self)
5930{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005931 PyObject *value;
5932 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005933 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005934
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005935 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005936 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005937
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005938 if (Py_SIZE(self->stack) <= self->stack->fence)
5939 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005940 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005941
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005942 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005943
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005944 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005945}
5946
5947static int
5948load_long_binput(UnpicklerObject *self)
5949{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005950 PyObject *value;
5951 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005952 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005953
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005954 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005955 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005956
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005957 if (Py_SIZE(self->stack) <= self->stack->fence)
5958 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005959 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005960
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005961 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005962 if (idx < 0) {
5963 PyErr_SetString(PyExc_ValueError,
5964 "negative LONG_BINPUT argument");
5965 return -1;
5966 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005967
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005968 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005969}
5970
5971static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005972load_memoize(UnpicklerObject *self)
5973{
5974 PyObject *value;
5975
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005976 if (Py_SIZE(self->stack) <= self->stack->fence)
5977 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005978 value = self->stack->data[Py_SIZE(self->stack) - 1];
5979
5980 return _Unpickler_MemoPut(self, self->memo_len, value);
5981}
5982
5983static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005984do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005985{
5986 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005987 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005988 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005989 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005990 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005991
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005992 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005993 if (x > len || x <= self->stack->fence)
5994 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005995 if (len == x) /* nothing to do */
5996 return 0;
5997
5998 list = self->stack->data[x - 1];
5999
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006000 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006001 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006002 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006003
6004 slice = Pdata_poplist(self->stack, x);
6005 if (!slice)
6006 return -1;
6007 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006008 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006009 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006010 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006011 }
6012 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006013 PyObject *extend_func;
6014 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006015
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006016 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6017 if (extend_func != NULL) {
6018 slice = Pdata_poplist(self->stack, x);
6019 if (!slice) {
6020 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006021 return -1;
6022 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006023 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006024 Py_DECREF(extend_func);
6025 if (result == NULL)
6026 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006027 Py_DECREF(result);
6028 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006029 else {
6030 PyObject *append_func;
6031 _Py_IDENTIFIER(append);
6032
6033 /* Even if the PEP 307 requires extend() and append() methods,
6034 fall back on append() if the object has no extend() method
6035 for backward compatibility. */
6036 PyErr_Clear();
6037 append_func = _PyObject_GetAttrId(list, &PyId_append);
6038 if (append_func == NULL)
6039 return -1;
6040 for (i = x; i < len; i++) {
6041 value = self->stack->data[i];
6042 result = _Pickle_FastCall(append_func, value);
6043 if (result == NULL) {
6044 Pdata_clear(self->stack, i + 1);
6045 Py_SIZE(self->stack) = x;
6046 Py_DECREF(append_func);
6047 return -1;
6048 }
6049 Py_DECREF(result);
6050 }
6051 Py_SIZE(self->stack) = x;
6052 Py_DECREF(append_func);
6053 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006054 }
6055
6056 return 0;
6057}
6058
6059static int
6060load_append(UnpicklerObject *self)
6061{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006062 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6063 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006064 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006065}
6066
6067static int
6068load_appends(UnpicklerObject *self)
6069{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006070 Py_ssize_t i = marker(self);
6071 if (i < 0)
6072 return -1;
6073 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006074}
6075
6076static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006077do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006078{
6079 PyObject *value, *key;
6080 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006081 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006082 int status = 0;
6083
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006084 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006085 if (x > len || x <= self->stack->fence)
6086 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006087 if (len == x) /* nothing to do */
6088 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006089 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006090 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006091 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006092 PyErr_SetString(st->UnpicklingError,
6093 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006094 return -1;
6095 }
6096
6097 /* Here, dict does not actually need to be a PyDict; it could be anything
6098 that supports the __setitem__ attribute. */
6099 dict = self->stack->data[x - 1];
6100
6101 for (i = x + 1; i < len; i += 2) {
6102 key = self->stack->data[i - 1];
6103 value = self->stack->data[i];
6104 if (PyObject_SetItem(dict, key, value) < 0) {
6105 status = -1;
6106 break;
6107 }
6108 }
6109
6110 Pdata_clear(self->stack, x);
6111 return status;
6112}
6113
6114static int
6115load_setitem(UnpicklerObject *self)
6116{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006117 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006118}
6119
6120static int
6121load_setitems(UnpicklerObject *self)
6122{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006123 Py_ssize_t i = marker(self);
6124 if (i < 0)
6125 return -1;
6126 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006127}
6128
6129static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006130load_additems(UnpicklerObject *self)
6131{
6132 PyObject *set;
6133 Py_ssize_t mark, len, i;
6134
6135 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006136 if (mark < 0)
6137 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006138 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006139 if (mark > len || mark <= self->stack->fence)
6140 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006141 if (len == mark) /* nothing to do */
6142 return 0;
6143
6144 set = self->stack->data[mark - 1];
6145
6146 if (PySet_Check(set)) {
6147 PyObject *items;
6148 int status;
6149
6150 items = Pdata_poptuple(self->stack, mark);
6151 if (items == NULL)
6152 return -1;
6153
6154 status = _PySet_Update(set, items);
6155 Py_DECREF(items);
6156 return status;
6157 }
6158 else {
6159 PyObject *add_func;
6160 _Py_IDENTIFIER(add);
6161
6162 add_func = _PyObject_GetAttrId(set, &PyId_add);
6163 if (add_func == NULL)
6164 return -1;
6165 for (i = mark; i < len; i++) {
6166 PyObject *result;
6167 PyObject *item;
6168
6169 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006170 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006171 if (result == NULL) {
6172 Pdata_clear(self->stack, i + 1);
6173 Py_SIZE(self->stack) = mark;
6174 return -1;
6175 }
6176 Py_DECREF(result);
6177 }
6178 Py_SIZE(self->stack) = mark;
6179 }
6180
6181 return 0;
6182}
6183
6184static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006185load_build(UnpicklerObject *self)
6186{
6187 PyObject *state, *inst, *slotstate;
6188 PyObject *setstate;
6189 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006190 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006191
6192 /* Stack is ... instance, state. We want to leave instance at
6193 * the stack top, possibly mutated via instance.__setstate__(state).
6194 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006195 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6196 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006197
6198 PDATA_POP(self->stack, state);
6199 if (state == NULL)
6200 return -1;
6201
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006202 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006203
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006204 if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) {
6205 Py_DECREF(state);
6206 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006208 if (setstate != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006209 PyObject *result;
6210
6211 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006212 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006213 Py_DECREF(setstate);
6214 if (result == NULL)
6215 return -1;
6216 Py_DECREF(result);
6217 return 0;
6218 }
6219
6220 /* A default __setstate__. First see whether state embeds a
6221 * slot state dict too (a proto 2 addition).
6222 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006223 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006224 PyObject *tmp = state;
6225
6226 state = PyTuple_GET_ITEM(tmp, 0);
6227 slotstate = PyTuple_GET_ITEM(tmp, 1);
6228 Py_INCREF(state);
6229 Py_INCREF(slotstate);
6230 Py_DECREF(tmp);
6231 }
6232 else
6233 slotstate = NULL;
6234
6235 /* Set inst.__dict__ from the state dict (if any). */
6236 if (state != Py_None) {
6237 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006238 PyObject *d_key, *d_value;
6239 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006240 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241
6242 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006243 PickleState *st = _Pickle_GetGlobalState();
6244 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006245 goto error;
6246 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006247 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006248 if (dict == NULL)
6249 goto error;
6250
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006251 i = 0;
6252 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6253 /* normally the keys for instance attributes are
6254 interned. we should try to do that here. */
6255 Py_INCREF(d_key);
6256 if (PyUnicode_CheckExact(d_key))
6257 PyUnicode_InternInPlace(&d_key);
6258 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6259 Py_DECREF(d_key);
6260 goto error;
6261 }
6262 Py_DECREF(d_key);
6263 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006264 Py_DECREF(dict);
6265 }
6266
6267 /* Also set instance attributes from the slotstate dict (if any). */
6268 if (slotstate != NULL) {
6269 PyObject *d_key, *d_value;
6270 Py_ssize_t i;
6271
6272 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006273 PickleState *st = _Pickle_GetGlobalState();
6274 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006275 "slot state is not a dictionary");
6276 goto error;
6277 }
6278 i = 0;
6279 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6280 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6281 goto error;
6282 }
6283 }
6284
6285 if (0) {
6286 error:
6287 status = -1;
6288 }
6289
6290 Py_DECREF(state);
6291 Py_XDECREF(slotstate);
6292 return status;
6293}
6294
6295static int
6296load_mark(UnpicklerObject *self)
6297{
6298
6299 /* Note that we split the (pickle.py) stack into two stacks, an
6300 * object stack and a mark stack. Here we push a mark onto the
6301 * mark stack.
6302 */
6303
Sergey Fedoseev86b89912018-08-25 12:54:40 +05006304 if (self->num_marks >= self->marks_size) {
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006305 size_t alloc = ((size_t)self->num_marks << 1) + 20;
6306 Py_ssize_t *marks_new = self->marks;
6307 PyMem_RESIZE(marks_new, Py_ssize_t, alloc);
6308 if (marks_new == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006309 PyErr_NoMemory();
6310 return -1;
6311 }
Sergey Fedoseev90555ec2018-08-25 15:41:58 +05006312 self->marks = marks_new;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006313 self->marks_size = (Py_ssize_t)alloc;
6314 }
6315
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006316 self->stack->mark_set = 1;
6317 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006318
6319 return 0;
6320}
6321
6322static int
6323load_reduce(UnpicklerObject *self)
6324{
6325 PyObject *callable = NULL;
6326 PyObject *argtup = NULL;
6327 PyObject *obj = NULL;
6328
6329 PDATA_POP(self->stack, argtup);
6330 if (argtup == NULL)
6331 return -1;
6332 PDATA_POP(self->stack, callable);
6333 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006334 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006335 Py_DECREF(callable);
6336 }
6337 Py_DECREF(argtup);
6338
6339 if (obj == NULL)
6340 return -1;
6341
6342 PDATA_PUSH(self->stack, obj, -1);
6343 return 0;
6344}
6345
6346/* Just raises an error if we don't know the protocol specified. PROTO
6347 * is the first opcode for protocols >= 2.
6348 */
6349static int
6350load_proto(UnpicklerObject *self)
6351{
6352 char *s;
6353 int i;
6354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006355 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006356 return -1;
6357
6358 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006359 if (i <= HIGHEST_PROTOCOL) {
6360 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006361 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006362 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006363
6364 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6365 return -1;
6366}
6367
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006368static int
6369load_frame(UnpicklerObject *self)
6370{
6371 char *s;
6372 Py_ssize_t frame_len;
6373
6374 if (_Unpickler_Read(self, &s, 8) < 0)
6375 return -1;
6376
6377 frame_len = calc_binsize(s, 8);
6378 if (frame_len < 0) {
6379 PyErr_Format(PyExc_OverflowError,
6380 "FRAME length exceeds system's maximum of %zd bytes",
6381 PY_SSIZE_T_MAX);
6382 return -1;
6383 }
6384
6385 if (_Unpickler_Read(self, &s, frame_len) < 0)
6386 return -1;
6387
6388 /* Rewind to start of frame */
6389 self->next_read_idx -= frame_len;
6390 return 0;
6391}
6392
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006393static PyObject *
6394load(UnpicklerObject *self)
6395{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006397 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006398
6399 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006400 self->stack->mark_set = 0;
6401 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006402 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006403 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006404 Pdata_clear(self->stack, 0);
6405
6406 /* Convenient macros for the dispatch while-switch loop just below. */
6407#define OP(opcode, load_func) \
6408 case opcode: if (load_func(self) < 0) break; continue;
6409
6410#define OP_ARG(opcode, load_func, arg) \
6411 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6412
6413 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006414 if (_Unpickler_Read(self, &s, 1) < 0) {
6415 PickleState *st = _Pickle_GetGlobalState();
6416 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6417 PyErr_Format(PyExc_EOFError, "Ran out of input");
6418 }
6419 return NULL;
6420 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006421
6422 switch ((enum opcode)s[0]) {
6423 OP(NONE, load_none)
6424 OP(BININT, load_binint)
6425 OP(BININT1, load_binint1)
6426 OP(BININT2, load_binint2)
6427 OP(INT, load_int)
6428 OP(LONG, load_long)
6429 OP_ARG(LONG1, load_counted_long, 1)
6430 OP_ARG(LONG4, load_counted_long, 4)
6431 OP(FLOAT, load_float)
6432 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006433 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6434 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6435 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6436 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6437 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006438 OP(STRING, load_string)
6439 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006440 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6441 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6442 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006443 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6444 OP_ARG(TUPLE1, load_counted_tuple, 1)
6445 OP_ARG(TUPLE2, load_counted_tuple, 2)
6446 OP_ARG(TUPLE3, load_counted_tuple, 3)
6447 OP(TUPLE, load_tuple)
6448 OP(EMPTY_LIST, load_empty_list)
6449 OP(LIST, load_list)
6450 OP(EMPTY_DICT, load_empty_dict)
6451 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006452 OP(EMPTY_SET, load_empty_set)
6453 OP(ADDITEMS, load_additems)
6454 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006455 OP(OBJ, load_obj)
6456 OP(INST, load_inst)
6457 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006458 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006459 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006460 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006461 OP(APPEND, load_append)
6462 OP(APPENDS, load_appends)
6463 OP(BUILD, load_build)
6464 OP(DUP, load_dup)
6465 OP(BINGET, load_binget)
6466 OP(LONG_BINGET, load_long_binget)
6467 OP(GET, load_get)
6468 OP(MARK, load_mark)
6469 OP(BINPUT, load_binput)
6470 OP(LONG_BINPUT, load_long_binput)
6471 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006472 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006473 OP(POP, load_pop)
6474 OP(POP_MARK, load_pop_mark)
6475 OP(SETITEM, load_setitem)
6476 OP(SETITEMS, load_setitems)
6477 OP(PERSID, load_persid)
6478 OP(BINPERSID, load_binpersid)
6479 OP(REDUCE, load_reduce)
6480 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006481 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482 OP_ARG(EXT1, load_extension, 1)
6483 OP_ARG(EXT2, load_extension, 2)
6484 OP_ARG(EXT4, load_extension, 4)
6485 OP_ARG(NEWTRUE, load_bool, Py_True)
6486 OP_ARG(NEWFALSE, load_bool, Py_False)
6487
6488 case STOP:
6489 break;
6490
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006491 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006492 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006493 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006494 unsigned char c = (unsigned char) *s;
6495 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6496 PyErr_Format(st->UnpicklingError,
6497 "invalid load key, '%c'.", c);
6498 }
6499 else {
6500 PyErr_Format(st->UnpicklingError,
6501 "invalid load key, '\\x%02x'.", c);
6502 }
6503 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006504 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006505 }
6506
6507 break; /* and we are done! */
6508 }
6509
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006510 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006511 return NULL;
6512 }
6513
Victor Stinner2ae57e32013-10-31 13:39:23 +01006514 if (_Unpickler_SkipConsumed(self) < 0)
6515 return NULL;
6516
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006517 PDATA_POP(self->stack, value);
6518 return value;
6519}
6520
Larry Hastings61272b72014-01-07 12:41:53 -08006521/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006522
6523_pickle.Unpickler.load
6524
6525Load a pickle.
6526
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006527Read a pickled object representation from the open file object given
6528in the constructor, and return the reconstituted object hierarchy
6529specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006530[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006531
Larry Hastings3cceb382014-01-04 11:09:09 -08006532static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006533_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006534/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006535{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006536 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006537
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006538 /* Check whether the Unpickler was initialized correctly. This prevents
6539 segfaulting if a subclass overridden __init__ with a function that does
6540 not call Unpickler.__init__(). Here, we simply ensure that self->read
6541 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006542 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006543 PickleState *st = _Pickle_GetGlobalState();
6544 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006545 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006546 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006547 return NULL;
6548 }
6549
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006550 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006551}
6552
6553/* The name of find_class() is misleading. In newer pickle protocols, this
6554 function is used for loading any global (i.e., functions), not just
6555 classes. The name is kept only for backward compatibility. */
6556
Larry Hastings61272b72014-01-07 12:41:53 -08006557/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006558
6559_pickle.Unpickler.find_class
6560
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006561 module_name: object
6562 global_name: object
6563 /
6564
6565Return an object from a specified module.
6566
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006567If necessary, the module will be imported. Subclasses may override
6568this method (e.g. to restrict unpickling of arbitrary classes and
6569functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006570
6571This method is called whenever a class or a function object is
6572needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006573[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006574
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006575static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006576_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6577 PyObject *module_name,
6578 PyObject *global_name)
6579/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006580{
6581 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006582 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006583
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006584 /* Try to map the old names used in Python 2.x to the new ones used in
6585 Python 3.x. We do this only with old pickle protocols and when the
6586 user has not disabled the feature. */
6587 if (self->proto < 3 && self->fix_imports) {
6588 PyObject *key;
6589 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006590 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006591
6592 /* Check if the global (i.e., a function or a class) was renamed
6593 or moved to another module. */
6594 key = PyTuple_Pack(2, module_name, global_name);
6595 if (key == NULL)
6596 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006597 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006598 Py_DECREF(key);
6599 if (item) {
6600 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6601 PyErr_Format(PyExc_RuntimeError,
6602 "_compat_pickle.NAME_MAPPING values should be "
6603 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6604 return NULL;
6605 }
6606 module_name = PyTuple_GET_ITEM(item, 0);
6607 global_name = PyTuple_GET_ITEM(item, 1);
6608 if (!PyUnicode_Check(module_name) ||
6609 !PyUnicode_Check(global_name)) {
6610 PyErr_Format(PyExc_RuntimeError,
6611 "_compat_pickle.NAME_MAPPING values should be "
6612 "pairs of str, not (%.200s, %.200s)",
6613 Py_TYPE(module_name)->tp_name,
6614 Py_TYPE(global_name)->tp_name);
6615 return NULL;
6616 }
6617 }
6618 else if (PyErr_Occurred()) {
6619 return NULL;
6620 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006621 else {
6622 /* Check if the module was renamed. */
6623 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6624 if (item) {
6625 if (!PyUnicode_Check(item)) {
6626 PyErr_Format(PyExc_RuntimeError,
6627 "_compat_pickle.IMPORT_MAPPING values should be "
6628 "strings, not %.200s", Py_TYPE(item)->tp_name);
6629 return NULL;
6630 }
6631 module_name = item;
6632 }
6633 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006634 return NULL;
6635 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006636 }
6637 }
6638
tjb9004371c0a2019-02-18 23:30:51 +08006639 /*
6640 * we don't use PyImport_GetModule here, because it can return partially-
6641 * initialised modules, which then cause the getattribute to fail.
6642 */
6643 module = PyImport_Import(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006644 if (module == NULL) {
tjb9004371c0a2019-02-18 23:30:51 +08006645 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006646 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006647 global = getattribute(module, global_name, self->proto >= 4);
6648 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006649 return global;
6650}
6651
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006652/*[clinic input]
6653
6654_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6655
6656Returns size in memory, in bytes.
6657[clinic start generated code]*/
6658
6659static Py_ssize_t
6660_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6661/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6662{
6663 Py_ssize_t res;
6664
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006665 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006666 if (self->memo != NULL)
6667 res += self->memo_size * sizeof(PyObject *);
6668 if (self->marks != NULL)
6669 res += self->marks_size * sizeof(Py_ssize_t);
6670 if (self->input_line != NULL)
6671 res += strlen(self->input_line) + 1;
6672 if (self->encoding != NULL)
6673 res += strlen(self->encoding) + 1;
6674 if (self->errors != NULL)
6675 res += strlen(self->errors) + 1;
6676 return res;
6677}
6678
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006679static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006680 _PICKLE_UNPICKLER_LOAD_METHODDEF
6681 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006682 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006683 {NULL, NULL} /* sentinel */
6684};
6685
6686static void
6687Unpickler_dealloc(UnpicklerObject *self)
6688{
6689 PyObject_GC_UnTrack((PyObject *)self);
6690 Py_XDECREF(self->readline);
6691 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006692 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006693 Py_XDECREF(self->stack);
6694 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006695 if (self->buffer.buf != NULL) {
6696 PyBuffer_Release(&self->buffer);
6697 self->buffer.buf = NULL;
6698 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006699
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006700 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006701 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006702 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006703 PyMem_Free(self->encoding);
6704 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006705
6706 Py_TYPE(self)->tp_free((PyObject *)self);
6707}
6708
6709static int
6710Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6711{
6712 Py_VISIT(self->readline);
6713 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006714 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006715 Py_VISIT(self->stack);
6716 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006717 return 0;
6718}
6719
6720static int
6721Unpickler_clear(UnpicklerObject *self)
6722{
6723 Py_CLEAR(self->readline);
6724 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006725 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006726 Py_CLEAR(self->stack);
6727 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006728 if (self->buffer.buf != NULL) {
6729 PyBuffer_Release(&self->buffer);
6730 self->buffer.buf = NULL;
6731 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006732
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006733 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006734 PyMem_Free(self->marks);
6735 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006736 PyMem_Free(self->input_line);
6737 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006738 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006739 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006740 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006741 self->errors = NULL;
6742
6743 return 0;
6744}
6745
Larry Hastings61272b72014-01-07 12:41:53 -08006746/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006747
6748_pickle.Unpickler.__init__
6749
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006750 file: object
6751 *
6752 fix_imports: bool = True
6753 encoding: str = 'ASCII'
6754 errors: str = 'strict'
6755
6756This takes a binary file for reading a pickle data stream.
6757
6758The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006759protocol argument is needed. Bytes past the pickled object's
6760representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006761
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006762The argument *file* must have two methods, a read() method that takes
6763an integer argument, and a readline() method that requires no
6764arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006765binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006766other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006767
6768Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006769which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006770generated by Python 2. If *fix_imports* is True, pickle will try to
6771map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006772*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006773instances pickled by Python 2; these default to 'ASCII' and 'strict',
6774respectively. The *encoding* can be 'bytes' to read these 8-bit
6775string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006776[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006777
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006778static int
Larry Hastings89964c42015-04-14 18:07:59 -04006779_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6780 int fix_imports, const char *encoding,
6781 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006782/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006783{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006784 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006785
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006786 /* In case of multiple __init__() calls, clear previous content. */
6787 if (self->read != NULL)
6788 (void)Unpickler_clear(self);
6789
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006790 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006791 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006792
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006793 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006794 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006795
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006796 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006797
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006798 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6799 &self->pers_func, &self->pers_func_self) < 0)
6800 {
6801 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006802 }
6803
6804 self->stack = (Pdata *)Pdata_New();
6805 if (self->stack == NULL)
Zackery Spytz4b430e52018-09-28 23:48:46 -06006806 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006807
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006808 self->memo_size = 32;
6809 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006810 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006811 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006812
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006813 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006814
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006815 return 0;
6816}
6817
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006818
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006819/* Define a proxy object for the Unpickler's internal memo object. This is to
6820 * avoid breaking code like:
6821 * unpickler.memo.clear()
6822 * and
6823 * unpickler.memo = saved_memo
6824 * Is this a good idea? Not really, but we don't want to break code that uses
6825 * it. Note that we don't implement the entire mapping API here. This is
6826 * intentional, as these should be treated as black-box implementation details.
6827 *
6828 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006829 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006830 */
6831
Larry Hastings61272b72014-01-07 12:41:53 -08006832/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006833_pickle.UnpicklerMemoProxy.clear
6834
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006835Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006836[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006837
Larry Hastings3cceb382014-01-04 11:09:09 -08006838static PyObject *
6839_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006840/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006841{
6842 _Unpickler_MemoCleanup(self->unpickler);
6843 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6844 if (self->unpickler->memo == NULL)
6845 return NULL;
6846 Py_RETURN_NONE;
6847}
6848
Larry Hastings61272b72014-01-07 12:41:53 -08006849/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006850_pickle.UnpicklerMemoProxy.copy
6851
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006852Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006853[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006854
Larry Hastings3cceb382014-01-04 11:09:09 -08006855static PyObject *
6856_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006857/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006858{
Benjamin Petersona4ae8282018-09-20 18:36:40 -07006859 size_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006860 PyObject *new_memo = PyDict_New();
6861 if (new_memo == NULL)
6862 return NULL;
6863
6864 for (i = 0; i < self->unpickler->memo_size; i++) {
6865 int status;
6866 PyObject *key, *value;
6867
6868 value = self->unpickler->memo[i];
6869 if (value == NULL)
6870 continue;
6871
6872 key = PyLong_FromSsize_t(i);
6873 if (key == NULL)
6874 goto error;
6875 status = PyDict_SetItem(new_memo, key, value);
6876 Py_DECREF(key);
6877 if (status < 0)
6878 goto error;
6879 }
6880 return new_memo;
6881
6882error:
6883 Py_DECREF(new_memo);
6884 return NULL;
6885}
6886
Larry Hastings61272b72014-01-07 12:41:53 -08006887/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006888_pickle.UnpicklerMemoProxy.__reduce__
6889
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006890Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006891[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006892
Larry Hastings3cceb382014-01-04 11:09:09 -08006893static PyObject *
6894_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006895/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006896{
6897 PyObject *reduce_value;
6898 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006899 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006900 if (contents == NULL)
6901 return NULL;
6902
6903 reduce_value = PyTuple_New(2);
6904 if (reduce_value == NULL) {
6905 Py_DECREF(contents);
6906 return NULL;
6907 }
6908 constructor_args = PyTuple_New(1);
6909 if (constructor_args == NULL) {
6910 Py_DECREF(contents);
6911 Py_DECREF(reduce_value);
6912 return NULL;
6913 }
6914 PyTuple_SET_ITEM(constructor_args, 0, contents);
6915 Py_INCREF((PyObject *)&PyDict_Type);
6916 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6917 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6918 return reduce_value;
6919}
6920
6921static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006922 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6923 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6924 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006925 {NULL, NULL} /* sentinel */
6926};
6927
6928static void
6929UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6930{
6931 PyObject_GC_UnTrack(self);
6932 Py_XDECREF(self->unpickler);
6933 PyObject_GC_Del((PyObject *)self);
6934}
6935
6936static int
6937UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6938 visitproc visit, void *arg)
6939{
6940 Py_VISIT(self->unpickler);
6941 return 0;
6942}
6943
6944static int
6945UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6946{
6947 Py_CLEAR(self->unpickler);
6948 return 0;
6949}
6950
6951static PyTypeObject UnpicklerMemoProxyType = {
6952 PyVarObject_HEAD_INIT(NULL, 0)
6953 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6954 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6955 0,
6956 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6957 0, /* tp_print */
6958 0, /* tp_getattr */
6959 0, /* tp_setattr */
6960 0, /* tp_compare */
6961 0, /* tp_repr */
6962 0, /* tp_as_number */
6963 0, /* tp_as_sequence */
6964 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006965 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006966 0, /* tp_call */
6967 0, /* tp_str */
6968 PyObject_GenericGetAttr, /* tp_getattro */
6969 PyObject_GenericSetAttr, /* tp_setattro */
6970 0, /* tp_as_buffer */
6971 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6972 0, /* tp_doc */
6973 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6974 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6975 0, /* tp_richcompare */
6976 0, /* tp_weaklistoffset */
6977 0, /* tp_iter */
6978 0, /* tp_iternext */
6979 unpicklerproxy_methods, /* tp_methods */
6980};
6981
6982static PyObject *
6983UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6984{
6985 UnpicklerMemoProxyObject *self;
6986
6987 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6988 &UnpicklerMemoProxyType);
6989 if (self == NULL)
6990 return NULL;
6991 Py_INCREF(unpickler);
6992 self->unpickler = unpickler;
6993 PyObject_GC_Track(self);
6994 return (PyObject *)self;
6995}
6996
6997/*****************************************************************************/
6998
6999
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007000static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007001Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007002{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007003 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007004}
7005
7006static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007007Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007008{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007009 PyObject **new_memo;
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007010 size_t new_memo_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007011
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007012 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007013 PyErr_SetString(PyExc_TypeError,
7014 "attribute deletion is not supported");
7015 return -1;
7016 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007017
7018 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7019 UnpicklerObject *unpickler =
7020 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7021
7022 new_memo_size = unpickler->memo_size;
7023 new_memo = _Unpickler_NewMemo(new_memo_size);
7024 if (new_memo == NULL)
7025 return -1;
7026
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007027 for (size_t i = 0; i < new_memo_size; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007028 Py_XINCREF(unpickler->memo[i]);
7029 new_memo[i] = unpickler->memo[i];
7030 }
7031 }
7032 else if (PyDict_Check(obj)) {
7033 Py_ssize_t i = 0;
7034 PyObject *key, *value;
7035
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007036 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007037 new_memo = _Unpickler_NewMemo(new_memo_size);
7038 if (new_memo == NULL)
7039 return -1;
7040
7041 while (PyDict_Next(obj, &i, &key, &value)) {
7042 Py_ssize_t idx;
7043 if (!PyLong_Check(key)) {
7044 PyErr_SetString(PyExc_TypeError,
7045 "memo key must be integers");
7046 goto error;
7047 }
7048 idx = PyLong_AsSsize_t(key);
7049 if (idx == -1 && PyErr_Occurred())
7050 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007051 if (idx < 0) {
7052 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007053 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007054 goto error;
7055 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007056 if (_Unpickler_MemoPut(self, idx, value) < 0)
7057 goto error;
7058 }
7059 }
7060 else {
7061 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +02007062 "'memo' attribute must be an UnpicklerMemoProxy object "
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007063 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007064 return -1;
7065 }
7066
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007067 _Unpickler_MemoCleanup(self);
7068 self->memo_size = new_memo_size;
7069 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007070
7071 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007072
7073 error:
7074 if (new_memo_size) {
Benjamin Petersona4ae8282018-09-20 18:36:40 -07007075 for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007076 Py_XDECREF(new_memo[i]);
7077 }
7078 PyMem_FREE(new_memo);
7079 }
7080 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007081}
7082
7083static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007084Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007085{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007086 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007087 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007088 return NULL;
7089 }
7090 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007091}
7092
7093static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02007094Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007095{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007096 if (value == NULL) {
7097 PyErr_SetString(PyExc_TypeError,
7098 "attribute deletion is not supported");
7099 return -1;
7100 }
7101 if (!PyCallable_Check(value)) {
7102 PyErr_SetString(PyExc_TypeError,
7103 "persistent_load must be a callable taking "
7104 "one argument");
7105 return -1;
7106 }
7107
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007108 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007109 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007110 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007111
7112 return 0;
7113}
7114
7115static PyGetSetDef Unpickler_getsets[] = {
7116 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7117 {"persistent_load", (getter)Unpickler_get_persload,
7118 (setter)Unpickler_set_persload},
7119 {NULL}
7120};
7121
7122static PyTypeObject Unpickler_Type = {
7123 PyVarObject_HEAD_INIT(NULL, 0)
7124 "_pickle.Unpickler", /*tp_name*/
7125 sizeof(UnpicklerObject), /*tp_basicsize*/
7126 0, /*tp_itemsize*/
7127 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7128 0, /*tp_print*/
7129 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007130 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007131 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007132 0, /*tp_repr*/
7133 0, /*tp_as_number*/
7134 0, /*tp_as_sequence*/
7135 0, /*tp_as_mapping*/
7136 0, /*tp_hash*/
7137 0, /*tp_call*/
7138 0, /*tp_str*/
7139 0, /*tp_getattro*/
7140 0, /*tp_setattro*/
7141 0, /*tp_as_buffer*/
7142 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007143 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007144 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7145 (inquiry)Unpickler_clear, /*tp_clear*/
7146 0, /*tp_richcompare*/
7147 0, /*tp_weaklistoffset*/
7148 0, /*tp_iter*/
7149 0, /*tp_iternext*/
7150 Unpickler_methods, /*tp_methods*/
7151 0, /*tp_members*/
7152 Unpickler_getsets, /*tp_getset*/
7153 0, /*tp_base*/
7154 0, /*tp_dict*/
7155 0, /*tp_descr_get*/
7156 0, /*tp_descr_set*/
7157 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007158 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007159 PyType_GenericAlloc, /*tp_alloc*/
7160 PyType_GenericNew, /*tp_new*/
7161 PyObject_GC_Del, /*tp_free*/
7162 0, /*tp_is_gc*/
7163};
7164
Larry Hastings61272b72014-01-07 12:41:53 -08007165/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007166
7167_pickle.dump
7168
7169 obj: object
7170 file: object
7171 protocol: object = NULL
7172 *
7173 fix_imports: bool = True
7174
7175Write a pickled representation of obj to the open file object file.
7176
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007177This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7178be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007179
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007180The optional *protocol* argument tells the pickler to use the given
Łukasz Langac51d8c92018-04-03 23:06:53 -07007181protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7182protocol is 4. It was introduced in Python 3.4, it is incompatible
7183with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007184
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007185Specifying a negative protocol version selects the highest protocol
7186version supported. The higher the protocol used, the more recent the
7187version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007189The *file* argument must have a write() method that accepts a single
7190bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007191writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007192this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007193
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007194If *fix_imports* is True and protocol is less than 3, pickle will try
7195to map the new Python 3 names to the old module names used in Python
71962, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007197[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007198
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007199static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007200_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007201 PyObject *protocol, int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007202/*[clinic end generated code: output=a4774d5fde7d34de input=93f1408489a87472]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007203{
7204 PicklerObject *pickler = _Pickler_New();
7205
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007206 if (pickler == NULL)
7207 return NULL;
7208
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007209 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007210 goto error;
7211
7212 if (_Pickler_SetOutputStream(pickler, file) < 0)
7213 goto error;
7214
7215 if (dump(pickler, obj) < 0)
7216 goto error;
7217
7218 if (_Pickler_FlushToFile(pickler) < 0)
7219 goto error;
7220
7221 Py_DECREF(pickler);
7222 Py_RETURN_NONE;
7223
7224 error:
7225 Py_XDECREF(pickler);
7226 return NULL;
7227}
7228
Larry Hastings61272b72014-01-07 12:41:53 -08007229/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007230
7231_pickle.dumps
7232
7233 obj: object
7234 protocol: object = NULL
7235 *
7236 fix_imports: bool = True
7237
7238Return the pickled representation of the object as a bytes object.
7239
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007240The optional *protocol* argument tells the pickler to use the given
7241protocol; supported protocols are 0, 1, 2, 3 and 4. The default
Łukasz Langac51d8c92018-04-03 23:06:53 -07007242protocol is 4. It was introduced in Python 3.4, it is incompatible
7243with previous versions.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007244
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007245Specifying a negative protocol version selects the highest protocol
7246version supported. The higher the protocol used, the more recent the
7247version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007248
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007249If *fix_imports* is True and *protocol* is less than 3, pickle will
7250try to map the new Python 3 names to the old module names used in
7251Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007252[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007253
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007254static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007255_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007256 int fix_imports)
Łukasz Langac51d8c92018-04-03 23:06:53 -07007257/*[clinic end generated code: output=d75d5cda456fd261 input=b6efb45a7d19b5ab]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007258{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007259 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007260 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007261
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007262 if (pickler == NULL)
7263 return NULL;
7264
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007265 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007266 goto error;
7267
7268 if (dump(pickler, obj) < 0)
7269 goto error;
7270
7271 result = _Pickler_GetString(pickler);
7272 Py_DECREF(pickler);
7273 return result;
7274
7275 error:
7276 Py_XDECREF(pickler);
7277 return NULL;
7278}
7279
Larry Hastings61272b72014-01-07 12:41:53 -08007280/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007281
7282_pickle.load
7283
7284 file: object
7285 *
7286 fix_imports: bool = True
7287 encoding: str = 'ASCII'
7288 errors: str = 'strict'
7289
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007290Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007291
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007292This is equivalent to ``Unpickler(file).load()``, but may be more
7293efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007294
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007295The protocol version of the pickle is detected automatically, so no
7296protocol argument is needed. Bytes past the pickled object's
7297representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007298
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007299The argument *file* must have two methods, a read() method that takes
7300an integer argument, and a readline() method that requires no
7301arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007302binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007303other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007304
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007305Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007306which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007307generated by Python 2. If *fix_imports* is True, pickle will try to
7308map the old Python 2 names to the new names used in Python 3. The
7309*encoding* and *errors* tell pickle how to decode 8-bit string
7310instances pickled by Python 2; these default to 'ASCII' and 'strict',
7311respectively. The *encoding* can be 'bytes' to read these 8-bit
7312string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007313[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007314
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007315static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007316_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007317 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007318/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007319{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007320 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007321 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007322
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007323 if (unpickler == NULL)
7324 return NULL;
7325
7326 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7327 goto error;
7328
7329 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7330 goto error;
7331
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007332 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007333
7334 result = load(unpickler);
7335 Py_DECREF(unpickler);
7336 return result;
7337
7338 error:
7339 Py_XDECREF(unpickler);
7340 return NULL;
7341}
7342
Larry Hastings61272b72014-01-07 12:41:53 -08007343/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007344
7345_pickle.loads
7346
7347 data: object
7348 *
7349 fix_imports: bool = True
7350 encoding: str = 'ASCII'
7351 errors: str = 'strict'
7352
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007353Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007354
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007355The protocol version of the pickle is detected automatically, so no
7356protocol argument is needed. Bytes past the pickled object's
7357representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007358
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007359Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007360which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007361generated by Python 2. If *fix_imports* is True, pickle will try to
7362map the old Python 2 names to the new names used in Python 3. The
7363*encoding* and *errors* tell pickle how to decode 8-bit string
7364instances pickled by Python 2; these default to 'ASCII' and 'strict',
7365respectively. The *encoding* can be 'bytes' to read these 8-bit
7366string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007367[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007368
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007369static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007370_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007371 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007372/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007373{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007374 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007375 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007376
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007377 if (unpickler == NULL)
7378 return NULL;
7379
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007380 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007381 goto error;
7382
7383 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7384 goto error;
7385
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007386 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007387
7388 result = load(unpickler);
7389 Py_DECREF(unpickler);
7390 return result;
7391
7392 error:
7393 Py_XDECREF(unpickler);
7394 return NULL;
7395}
7396
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007397static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007398 _PICKLE_DUMP_METHODDEF
7399 _PICKLE_DUMPS_METHODDEF
7400 _PICKLE_LOAD_METHODDEF
7401 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007402 {NULL, NULL} /* sentinel */
7403};
7404
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007405static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007406pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007407{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007408 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007409 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007410}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007411
Stefan Krahf483b0f2013-12-14 13:43:10 +01007412static void
7413pickle_free(PyObject *m)
7414{
7415 _Pickle_ClearState(_Pickle_GetState(m));
7416}
7417
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007418static int
7419pickle_traverse(PyObject *m, visitproc visit, void *arg)
7420{
7421 PickleState *st = _Pickle_GetState(m);
7422 Py_VISIT(st->PickleError);
7423 Py_VISIT(st->PicklingError);
7424 Py_VISIT(st->UnpicklingError);
7425 Py_VISIT(st->dispatch_table);
7426 Py_VISIT(st->extension_registry);
7427 Py_VISIT(st->extension_cache);
7428 Py_VISIT(st->inverted_registry);
7429 Py_VISIT(st->name_mapping_2to3);
7430 Py_VISIT(st->import_mapping_2to3);
7431 Py_VISIT(st->name_mapping_3to2);
7432 Py_VISIT(st->import_mapping_3to2);
7433 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007434 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007435 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007436}
7437
7438static struct PyModuleDef _picklemodule = {
7439 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007440 "_pickle", /* m_name */
7441 pickle_module_doc, /* m_doc */
7442 sizeof(PickleState), /* m_size */
7443 pickle_methods, /* m_methods */
7444 NULL, /* m_reload */
7445 pickle_traverse, /* m_traverse */
7446 pickle_clear, /* m_clear */
7447 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007448};
7449
7450PyMODINIT_FUNC
7451PyInit__pickle(void)
7452{
7453 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007454 PickleState *st;
7455
7456 m = PyState_FindModule(&_picklemodule);
7457 if (m) {
7458 Py_INCREF(m);
7459 return m;
7460 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007461
7462 if (PyType_Ready(&Unpickler_Type) < 0)
7463 return NULL;
7464 if (PyType_Ready(&Pickler_Type) < 0)
7465 return NULL;
7466 if (PyType_Ready(&Pdata_Type) < 0)
7467 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007468 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7469 return NULL;
7470 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7471 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007472
7473 /* Create the module and add the functions. */
7474 m = PyModule_Create(&_picklemodule);
7475 if (m == NULL)
7476 return NULL;
7477
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007478 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007479 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7480 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007481 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007482 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7483 return NULL;
7484
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007485 st = _Pickle_GetState(m);
7486
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007487 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007488 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7489 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007490 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007491 st->PicklingError = \
7492 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7493 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007494 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007495 st->UnpicklingError = \
7496 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7497 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007498 return NULL;
7499
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007500 Py_INCREF(st->PickleError);
7501 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007502 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007503 Py_INCREF(st->PicklingError);
7504 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007505 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007506 Py_INCREF(st->UnpicklingError);
7507 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007508 return NULL;
7509
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007510 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007511 return NULL;
7512
7513 return m;
7514}