blob: da915efd831595bacfb6c7b175fb8c750bab4ce6 [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
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000023/* Bump this when new opcodes are added to the pickle protocol. */
24enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010025 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000026 DEFAULT_PROTOCOL = 3
27};
28
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000029/* Pickle opcodes. These must be kept updated with pickle.py.
30 Extensive docs are in pickletools.py. */
31enum opcode {
32 MARK = '(',
33 STOP = '.',
34 POP = '0',
35 POP_MARK = '1',
36 DUP = '2',
37 FLOAT = 'F',
38 INT = 'I',
39 BININT = 'J',
40 BININT1 = 'K',
41 LONG = 'L',
42 BININT2 = 'M',
43 NONE = 'N',
44 PERSID = 'P',
45 BINPERSID = 'Q',
46 REDUCE = 'R',
47 STRING = 'S',
48 BINSTRING = 'T',
49 SHORT_BINSTRING = 'U',
50 UNICODE = 'V',
51 BINUNICODE = 'X',
52 APPEND = 'a',
53 BUILD = 'b',
54 GLOBAL = 'c',
55 DICT = 'd',
56 EMPTY_DICT = '}',
57 APPENDS = 'e',
58 GET = 'g',
59 BINGET = 'h',
60 INST = 'i',
61 LONG_BINGET = 'j',
62 LIST = 'l',
63 EMPTY_LIST = ']',
64 OBJ = 'o',
65 PUT = 'p',
66 BINPUT = 'q',
67 LONG_BINPUT = 'r',
68 SETITEM = 's',
69 TUPLE = 't',
70 EMPTY_TUPLE = ')',
71 SETITEMS = 'u',
72 BINFLOAT = 'G',
73
74 /* Protocol 2. */
75 PROTO = '\x80',
76 NEWOBJ = '\x81',
77 EXT1 = '\x82',
78 EXT2 = '\x83',
79 EXT4 = '\x84',
80 TUPLE1 = '\x85',
81 TUPLE2 = '\x86',
82 TUPLE3 = '\x87',
83 NEWTRUE = '\x88',
84 NEWFALSE = '\x89',
85 LONG1 = '\x8a',
86 LONG4 = '\x8b',
87
88 /* Protocol 3 (Python 3.x) */
89 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010090 SHORT_BINBYTES = 'C',
91
92 /* Protocol 4 */
93 SHORT_BINUNICODE = '\x8c',
94 BINUNICODE8 = '\x8d',
95 BINBYTES8 = '\x8e',
96 EMPTY_SET = '\x8f',
97 ADDITEMS = '\x90',
98 FROZENSET = '\x91',
99 NEWOBJ_EX = '\x92',
100 STACK_GLOBAL = '\x93',
101 MEMOIZE = '\x94',
102 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000103};
104
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000105enum {
106 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
107 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
108 break if this gets out of synch with pickle.py, but it's unclear that would
109 help anything either. */
110 BATCHSIZE = 1000,
111
112 /* Nesting limit until Pickler, when running in "fast mode", starts
113 checking for self-referential data-structures. */
114 FAST_NESTING_LIMIT = 50,
115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000116 /* Initial size of the write buffer of Pickler. */
117 WRITE_BUF_SIZE = 4096,
118
Antoine Pitrou04248a82010-10-12 20:51:21 +0000119 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100120 PREFETCH = 8192 * 16,
121
122 FRAME_SIZE_TARGET = 64 * 1024,
123
124 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000125};
126
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800127/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129/* State of the pickle module, per PEP 3121. */
130typedef struct {
131 /* Exception classes for pickle. */
132 PyObject *PickleError;
133 PyObject *PicklingError;
134 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800136 /* copyreg.dispatch_table, {type_object: pickling_function} */
137 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800139 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000140
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800141 /* copyreg._extension_registry, {(module_name, function_name): code} */
142 PyObject *extension_registry;
143 /* copyreg._extension_cache, {code: object} */
144 PyObject *extension_cache;
145 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
146 PyObject *inverted_registry;
147
148 /* Import mappings for compatibility with Python 2.x */
149
150 /* _compat_pickle.NAME_MAPPING,
151 {(oldmodule, oldname): (newmodule, newname)} */
152 PyObject *name_mapping_2to3;
153 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
154 PyObject *import_mapping_2to3;
155 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
156 PyObject *name_mapping_3to2;
157 PyObject *import_mapping_3to2;
158
159 /* codecs.encode, used for saving bytes in older protocols */
160 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300161 /* builtins.getattr, used for saving nested names with protocol < 4 */
162 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300163 /* functools.partial, used for implementing __newobj_ex__ with protocols
164 2 and 3 */
165 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800166} PickleState;
167
168/* Forward declaration of the _pickle module definition. */
169static struct PyModuleDef _picklemodule;
170
171/* Given a module object, get its per-module state. */
172static PickleState *
173_Pickle_GetState(PyObject *module)
174{
175 return (PickleState *)PyModule_GetState(module);
176}
177
178/* Find the module instance imported in the currently running sub-interpreter
179 and get its state. */
180static PickleState *
181_Pickle_GetGlobalState(void)
182{
183 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
184}
185
186/* Clear the given pickle module state. */
187static void
188_Pickle_ClearState(PickleState *st)
189{
190 Py_CLEAR(st->PickleError);
191 Py_CLEAR(st->PicklingError);
192 Py_CLEAR(st->UnpicklingError);
193 Py_CLEAR(st->dispatch_table);
194 Py_CLEAR(st->extension_registry);
195 Py_CLEAR(st->extension_cache);
196 Py_CLEAR(st->inverted_registry);
197 Py_CLEAR(st->name_mapping_2to3);
198 Py_CLEAR(st->import_mapping_2to3);
199 Py_CLEAR(st->name_mapping_3to2);
200 Py_CLEAR(st->import_mapping_3to2);
201 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300202 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100203 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204}
205
206/* Initialize the given pickle module state. */
207static int
208_Pickle_InitState(PickleState *st)
209{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300210 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800211 PyObject *copyreg = NULL;
212 PyObject *compat_pickle = NULL;
213 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300214 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800215
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300216 builtins = PyEval_GetBuiltins();
217 if (builtins == NULL)
218 goto error;
219 st->getattr = PyDict_GetItemString(builtins, "getattr");
220 if (st->getattr == NULL)
221 goto error;
222 Py_INCREF(st->getattr);
223
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800224 copyreg = PyImport_ImportModule("copyreg");
225 if (!copyreg)
226 goto error;
227 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
228 if (!st->dispatch_table)
229 goto error;
230 if (!PyDict_CheckExact(st->dispatch_table)) {
231 PyErr_Format(PyExc_RuntimeError,
232 "copyreg.dispatch_table should be a dict, not %.200s",
233 Py_TYPE(st->dispatch_table)->tp_name);
234 goto error;
235 }
236 st->extension_registry = \
237 PyObject_GetAttrString(copyreg, "_extension_registry");
238 if (!st->extension_registry)
239 goto error;
240 if (!PyDict_CheckExact(st->extension_registry)) {
241 PyErr_Format(PyExc_RuntimeError,
242 "copyreg._extension_registry should be a dict, "
243 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
244 goto error;
245 }
246 st->inverted_registry = \
247 PyObject_GetAttrString(copyreg, "_inverted_registry");
248 if (!st->inverted_registry)
249 goto error;
250 if (!PyDict_CheckExact(st->inverted_registry)) {
251 PyErr_Format(PyExc_RuntimeError,
252 "copyreg._inverted_registry should be a dict, "
253 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
254 goto error;
255 }
256 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
257 if (!st->extension_cache)
258 goto error;
259 if (!PyDict_CheckExact(st->extension_cache)) {
260 PyErr_Format(PyExc_RuntimeError,
261 "copyreg._extension_cache should be a dict, "
262 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
263 goto error;
264 }
265 Py_CLEAR(copyreg);
266
267 /* Load the 2.x -> 3.x stdlib module mapping tables */
268 compat_pickle = PyImport_ImportModule("_compat_pickle");
269 if (!compat_pickle)
270 goto error;
271 st->name_mapping_2to3 = \
272 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
273 if (!st->name_mapping_2to3)
274 goto error;
275 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
276 PyErr_Format(PyExc_RuntimeError,
277 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
278 Py_TYPE(st->name_mapping_2to3)->tp_name);
279 goto error;
280 }
281 st->import_mapping_2to3 = \
282 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
283 if (!st->import_mapping_2to3)
284 goto error;
285 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
286 PyErr_Format(PyExc_RuntimeError,
287 "_compat_pickle.IMPORT_MAPPING should be a dict, "
288 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
289 goto error;
290 }
291 /* ... and the 3.x -> 2.x mapping tables */
292 st->name_mapping_3to2 = \
293 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
294 if (!st->name_mapping_3to2)
295 goto error;
296 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
297 PyErr_Format(PyExc_RuntimeError,
298 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
299 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
300 goto error;
301 }
302 st->import_mapping_3to2 = \
303 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
304 if (!st->import_mapping_3to2)
305 goto error;
306 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
307 PyErr_Format(PyExc_RuntimeError,
308 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
309 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
310 goto error;
311 }
312 Py_CLEAR(compat_pickle);
313
314 codecs = PyImport_ImportModule("codecs");
315 if (codecs == NULL)
316 goto error;
317 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
318 if (st->codecs_encode == NULL) {
319 goto error;
320 }
321 if (!PyCallable_Check(st->codecs_encode)) {
322 PyErr_Format(PyExc_RuntimeError,
323 "codecs.encode should be a callable, not %.200s",
324 Py_TYPE(st->codecs_encode)->tp_name);
325 goto error;
326 }
327 Py_CLEAR(codecs);
328
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300329 functools = PyImport_ImportModule("functools");
330 if (!functools)
331 goto error;
332 st->partial = PyObject_GetAttrString(functools, "partial");
333 if (!st->partial)
334 goto error;
335 Py_CLEAR(functools);
336
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 return 0;
338
339 error:
340 Py_CLEAR(copyreg);
341 Py_CLEAR(compat_pickle);
342 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300343 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800344 _Pickle_ClearState(st);
345 return -1;
346}
347
348/* Helper for calling a function with a single argument quickly.
349
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 This function steals the reference of the given argument. */
351static PyObject *
352_Pickle_FastCall(PyObject *func, PyObject *obj)
353{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 PyObject *result;
355
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100356 result = PyObject_CallFunctionObjArgs(func, obj, NULL);
Victor Stinner75210692016-08-19 18:59:15 +0200357 Py_DECREF(obj);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800358 return result;
359}
360
361/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000362
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200363/* Retrieve and deconstruct a method for avoiding a reference cycle
364 (pickler -> bound method of pickler -> pickler) */
365static int
366init_method_ref(PyObject *self, _Py_Identifier *name,
367 PyObject **method_func, PyObject **method_self)
368{
369 PyObject *func, *func2;
370
371 /* *method_func and *method_self should be consistent. All refcount decrements
372 should be occurred after setting *method_self and *method_func. */
373 func = _PyObject_GetAttrId(self, name);
374 if (func == NULL) {
375 *method_self = NULL;
376 Py_CLEAR(*method_func);
377 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
378 return -1;
379 }
380 PyErr_Clear();
381 return 0;
382 }
383
384 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
385 /* Deconstruct a bound Python method */
386 func2 = PyMethod_GET_FUNCTION(func);
387 Py_INCREF(func2);
388 *method_self = self; /* borrowed */
389 Py_XSETREF(*method_func, func2);
390 Py_DECREF(func);
391 return 0;
392 }
393 else {
394 *method_self = NULL;
395 Py_XSETREF(*method_func, func);
396 return 0;
397 }
398}
399
400/* Bind a method if it was deconstructed */
401static PyObject *
402reconstruct_method(PyObject *func, PyObject *self)
403{
404 if (self) {
405 return PyMethod_New(func, self);
406 }
407 else {
408 Py_INCREF(func);
409 return func;
410 }
411}
412
413static PyObject *
414call_method(PyObject *func, PyObject *self, PyObject *obj)
415{
416 if (self) {
417 return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
418 }
419 else {
420 return PyObject_CallFunctionObjArgs(func, obj, NULL);
421 }
422}
423
424/*************************************************************************/
425
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000426/* Internal data type used as the unpickling stack. */
427typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000428 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000429 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200430 int mark_set; /* is MARK set? */
431 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000433} Pdata;
434
435static void
436Pdata_dealloc(Pdata *self)
437{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200438 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000439 while (--i >= 0) {
440 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000441 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000442 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000443 PyObject_Del(self);
444}
445
446static PyTypeObject Pdata_Type = {
447 PyVarObject_HEAD_INIT(NULL, 0)
448 "_pickle.Pdata", /*tp_name*/
449 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200450 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000451 (destructor)Pdata_dealloc, /*tp_dealloc*/
452};
453
454static PyObject *
455Pdata_New(void)
456{
457 Pdata *self;
458
459 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
460 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000461 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200462 self->mark_set = 0;
463 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000464 self->allocated = 8;
465 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000466 if (self->data)
467 return (PyObject *)self;
468 Py_DECREF(self);
469 return PyErr_NoMemory();
470}
471
472
473/* Retain only the initial clearto items. If clearto >= the current
474 * number of items, this is a (non-erroneous) NOP.
475 */
476static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200477Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000478{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200479 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200481 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000482 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000483 return 0;
484
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000485 while (--i >= clearto) {
486 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000487 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000488 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000489 return 0;
490}
491
492static int
493Pdata_grow(Pdata *self)
494{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000495 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200496 size_t allocated = (size_t)self->allocated;
497 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000498
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000499 new_allocated = (allocated >> 3) + 6;
500 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200501 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000502 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000503 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500504 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000505 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000506 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000507
508 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200509 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000510 return 0;
511
512 nomemory:
513 PyErr_NoMemory();
514 return -1;
515}
516
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200517static int
518Pdata_stack_underflow(Pdata *self)
519{
520 PickleState *st = _Pickle_GetGlobalState();
521 PyErr_SetString(st->UnpicklingError,
522 self->mark_set ?
523 "unexpected MARK found" :
524 "unpickling stack underflow");
525 return -1;
526}
527
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000528/* D is a Pdata*. Pop the topmost element and store it into V, which
529 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
530 * is raised and V is set to NULL.
531 */
532static PyObject *
533Pdata_pop(Pdata *self)
534{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200535 if (Py_SIZE(self) <= self->fence) {
536 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000537 return NULL;
538 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000539 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000540}
541#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
542
543static int
544Pdata_push(Pdata *self, PyObject *obj)
545{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000546 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000547 return -1;
548 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000549 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000550 return 0;
551}
552
553/* Push an object on stack, transferring its ownership to the stack. */
554#define PDATA_PUSH(D, O, ER) do { \
555 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
556
557/* Push an object on stack, adding a new reference to the object. */
558#define PDATA_APPEND(D, O, ER) do { \
559 Py_INCREF((O)); \
560 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
561
562static PyObject *
563Pdata_poptuple(Pdata *self, Py_ssize_t start)
564{
565 PyObject *tuple;
566 Py_ssize_t len, i, j;
567
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200568 if (start < self->fence) {
569 Pdata_stack_underflow(self);
570 return NULL;
571 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000572 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000573 tuple = PyTuple_New(len);
574 if (tuple == NULL)
575 return NULL;
576 for (i = start, j = 0; j < len; i++, j++)
577 PyTuple_SET_ITEM(tuple, j, self->data[i]);
578
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000579 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000580 return tuple;
581}
582
583static PyObject *
584Pdata_poplist(Pdata *self, Py_ssize_t start)
585{
586 PyObject *list;
587 Py_ssize_t len, i, j;
588
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000589 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000590 list = PyList_New(len);
591 if (list == NULL)
592 return NULL;
593 for (i = start, j = 0; j < len; i++, j++)
594 PyList_SET_ITEM(list, j, self->data[i]);
595
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000596 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000597 return list;
598}
599
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000600typedef struct {
601 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200602 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000603} PyMemoEntry;
604
605typedef struct {
606 Py_ssize_t mt_mask;
607 Py_ssize_t mt_used;
608 Py_ssize_t mt_allocated;
609 PyMemoEntry *mt_table;
610} PyMemoTable;
611
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000612typedef struct PicklerObject {
613 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000614 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000615 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000616 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000617 PyObject *pers_func; /* persistent_id() method, can be NULL */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200618 PyObject *pers_func_self; /* borrowed reference to self if pers_func
619 is an unbound method, NULL otherwise */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100620 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000621
622 PyObject *write; /* write() method of the output stream. */
623 PyObject *output_buffer; /* Write into a local bytearray buffer before
624 flushing to the stream. */
625 Py_ssize_t output_len; /* Length of output_buffer. */
626 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000627 int proto; /* Pickle protocol number, >= 0 */
628 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100629 int framing; /* True when framing is enabled, proto >= 4 */
630 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000631 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100632 is no frame currently open. */
633
634 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000635 int fast; /* Enable fast mode if set to a true value.
636 The fast mode disable the usage of memo,
637 therefore speeding the pickling process by
638 not generating superfluous PUT opcodes. It
639 should not be used if with self-referential
640 objects. */
641 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000642 int fix_imports; /* Indicate whether Pickler should fix
643 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000644 PyObject *fast_memo;
645} PicklerObject;
646
647typedef struct UnpicklerObject {
648 PyObject_HEAD
649 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000650
651 /* The unpickler memo is just an array of PyObject *s. Using a dict
652 is unnecessary, since the keys are contiguous ints. */
653 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100654 Py_ssize_t memo_size; /* Capacity of the memo array */
655 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000656
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000657 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Serhiy Storchaka986375e2017-11-30 22:48:31 +0200658 PyObject *pers_func_self; /* borrowed reference to self if pers_func
659 is an unbound method, NULL otherwise */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000660
661 Py_buffer buffer;
662 char *input_buffer;
663 char *input_line;
664 Py_ssize_t input_len;
665 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000666 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100667
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000668 PyObject *read; /* read() method of the input stream. */
669 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000670 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000671
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000672 char *encoding; /* Name of the encoding to be used for
673 decoding strings pickled using Python
674 2.x. The default value is "ASCII" */
675 char *errors; /* Name of errors handling scheme to used when
676 decoding strings. The default value is
677 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500678 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000679 objects. */
680 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
681 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000682 int proto; /* Protocol of the pickle loaded. */
683 int fix_imports; /* Indicate whether Unpickler should fix
684 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000685} UnpicklerObject;
686
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200687typedef struct {
688 PyObject_HEAD
689 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
690} PicklerMemoProxyObject;
691
692typedef struct {
693 PyObject_HEAD
694 UnpicklerObject *unpickler;
695} UnpicklerMemoProxyObject;
696
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000697/* Forward declarations */
698static int save(PicklerObject *, PyObject *, int);
699static int save_reduce(PicklerObject *, PyObject *, PyObject *);
700static PyTypeObject Pickler_Type;
701static PyTypeObject Unpickler_Type;
702
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200703#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000704
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000705/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300706 A custom hashtable mapping void* to Python ints. This is used by the pickler
707 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000708 a bunch of unnecessary object creation. This makes a huge performance
709 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000710
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000711#define MT_MINSIZE 8
712#define PERTURB_SHIFT 5
713
714
715static PyMemoTable *
716PyMemoTable_New(void)
717{
718 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
719 if (memo == NULL) {
720 PyErr_NoMemory();
721 return NULL;
722 }
723
724 memo->mt_used = 0;
725 memo->mt_allocated = MT_MINSIZE;
726 memo->mt_mask = MT_MINSIZE - 1;
727 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
728 if (memo->mt_table == NULL) {
729 PyMem_FREE(memo);
730 PyErr_NoMemory();
731 return NULL;
732 }
733 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
734
735 return memo;
736}
737
738static PyMemoTable *
739PyMemoTable_Copy(PyMemoTable *self)
740{
741 Py_ssize_t i;
742 PyMemoTable *new = PyMemoTable_New();
743 if (new == NULL)
744 return NULL;
745
746 new->mt_used = self->mt_used;
747 new->mt_allocated = self->mt_allocated;
748 new->mt_mask = self->mt_mask;
749 /* The table we get from _New() is probably smaller than we wanted.
750 Free it and allocate one that's the right size. */
751 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500752 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000753 if (new->mt_table == NULL) {
754 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200755 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000756 return NULL;
757 }
758 for (i = 0; i < self->mt_allocated; i++) {
759 Py_XINCREF(self->mt_table[i].me_key);
760 }
761 memcpy(new->mt_table, self->mt_table,
762 sizeof(PyMemoEntry) * self->mt_allocated);
763
764 return new;
765}
766
767static Py_ssize_t
768PyMemoTable_Size(PyMemoTable *self)
769{
770 return self->mt_used;
771}
772
773static int
774PyMemoTable_Clear(PyMemoTable *self)
775{
776 Py_ssize_t i = self->mt_allocated;
777
778 while (--i >= 0) {
779 Py_XDECREF(self->mt_table[i].me_key);
780 }
781 self->mt_used = 0;
782 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
783 return 0;
784}
785
786static void
787PyMemoTable_Del(PyMemoTable *self)
788{
789 if (self == NULL)
790 return;
791 PyMemoTable_Clear(self);
792
793 PyMem_FREE(self->mt_table);
794 PyMem_FREE(self);
795}
796
797/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
798 can be considerably simpler than dictobject.c's lookdict(). */
799static PyMemoEntry *
800_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
801{
802 size_t i;
803 size_t perturb;
804 size_t mask = (size_t)self->mt_mask;
805 PyMemoEntry *table = self->mt_table;
806 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000807 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000808
809 i = hash & mask;
810 entry = &table[i];
811 if (entry->me_key == NULL || entry->me_key == key)
812 return entry;
813
814 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
815 i = (i << 2) + i + perturb + 1;
816 entry = &table[i & mask];
817 if (entry->me_key == NULL || entry->me_key == key)
818 return entry;
819 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700820 Py_UNREACHABLE();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000821}
822
823/* Returns -1 on failure, 0 on success. */
824static int
825_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
826{
827 PyMemoEntry *oldtable = NULL;
828 PyMemoEntry *oldentry, *newentry;
829 Py_ssize_t new_size = MT_MINSIZE;
830 Py_ssize_t to_process;
831
832 assert(min_size > 0);
833
834 /* Find the smallest valid table size >= min_size. */
835 while (new_size < min_size && new_size > 0)
836 new_size <<= 1;
837 if (new_size <= 0) {
838 PyErr_NoMemory();
839 return -1;
840 }
841 /* new_size needs to be a power of two. */
842 assert((new_size & (new_size - 1)) == 0);
843
844 /* Allocate new table. */
845 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500846 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000847 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200848 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000849 PyErr_NoMemory();
850 return -1;
851 }
852 self->mt_allocated = new_size;
853 self->mt_mask = new_size - 1;
854 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
855
856 /* Copy entries from the old table. */
857 to_process = self->mt_used;
858 for (oldentry = oldtable; to_process > 0; oldentry++) {
859 if (oldentry->me_key != NULL) {
860 to_process--;
861 /* newentry is a pointer to a chunk of the new
862 mt_table, so we're setting the key:value pair
863 in-place. */
864 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
865 newentry->me_key = oldentry->me_key;
866 newentry->me_value = oldentry->me_value;
867 }
868 }
869
870 /* Deallocate the old table. */
871 PyMem_FREE(oldtable);
872 return 0;
873}
874
875/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200876static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000877PyMemoTable_Get(PyMemoTable *self, PyObject *key)
878{
879 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
880 if (entry->me_key == NULL)
881 return NULL;
882 return &entry->me_value;
883}
884
885/* Returns -1 on failure, 0 on success. */
886static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200887PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000888{
889 PyMemoEntry *entry;
890
891 assert(key != NULL);
892
893 entry = _PyMemoTable_Lookup(self, key);
894 if (entry->me_key != NULL) {
895 entry->me_value = value;
896 return 0;
897 }
898 Py_INCREF(key);
899 entry->me_key = key;
900 entry->me_value = value;
901 self->mt_used++;
902
903 /* If we added a key, we can safely resize. Otherwise just return!
904 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
905 *
906 * Quadrupling the size improves average table sparseness
907 * (reducing collisions) at the cost of some memory. It also halves
908 * the number of expensive resize operations in a growing memo table.
909 *
910 * Very large memo tables (over 50K items) use doubling instead.
911 * This may help applications with severe memory constraints.
912 */
913 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
914 return 0;
915 return _PyMemoTable_ResizeTable(self,
916 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
917}
918
919#undef MT_MINSIZE
920#undef PERTURB_SHIFT
921
922/*************************************************************************/
923
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000925static int
926_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000927{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300928 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200929 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000930 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000931 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000932 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100933 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 return 0;
935}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000936
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100937static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100938_write_size64(char *out, size_t value)
939{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200940 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800941
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200942 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800943
944 for (i = 0; i < sizeof(size_t); i++) {
945 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
946 }
947 for (i = sizeof(size_t); i < 8; i++) {
948 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800949 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100950}
951
952static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100953_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
954{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100955 qdata[0] = FRAME;
956 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100957}
958
959static int
960_Pickler_CommitFrame(PicklerObject *self)
961{
962 size_t frame_len;
963 char *qdata;
964
965 if (!self->framing || self->frame_start == -1)
966 return 0;
967 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
968 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
969 _Pickler_WriteFrameHeader(self, qdata, frame_len);
970 self->frame_start = -1;
971 return 0;
972}
973
974static int
975_Pickler_OpcodeBoundary(PicklerObject *self)
976{
977 Py_ssize_t frame_len;
978
979 if (!self->framing || self->frame_start == -1)
980 return 0;
981 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
982 if (frame_len >= FRAME_SIZE_TARGET)
983 return _Pickler_CommitFrame(self);
984 else
985 return 0;
986}
987
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000988static PyObject *
989_Pickler_GetString(PicklerObject *self)
990{
991 PyObject *output_buffer = self->output_buffer;
992
993 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100994
995 if (_Pickler_CommitFrame(self))
996 return NULL;
997
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000998 self->output_buffer = NULL;
999 /* Resize down to exact size */
1000 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
1001 return NULL;
1002 return output_buffer;
1003}
1004
1005static int
1006_Pickler_FlushToFile(PicklerObject *self)
1007{
1008 PyObject *output, *result;
1009
1010 assert(self->write != NULL);
1011
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001012 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001013 output = _Pickler_GetString(self);
1014 if (output == NULL)
1015 return -1;
1016
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001017 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001018 Py_XDECREF(result);
1019 return (result == NULL) ? -1 : 0;
1020}
1021
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001022static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001023_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001024{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001025 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001026 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001027 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001028
1029 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001030 need_new_frame = (self->framing && self->frame_start == -1);
1031
1032 if (need_new_frame)
1033 n = data_len + FRAME_HEADER_SIZE;
1034 else
1035 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001036
1037 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001038 if (required > self->max_output_len) {
1039 /* Make place in buffer for the pickle chunk */
1040 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1041 PyErr_NoMemory();
1042 return -1;
1043 }
1044 self->max_output_len = (self->output_len + n) / 2 * 3;
1045 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1046 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001047 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001048 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001049 if (need_new_frame) {
1050 /* Setup new frame */
1051 Py_ssize_t frame_start = self->output_len;
1052 self->frame_start = frame_start;
1053 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1054 /* Write an invalid value, for debugging */
1055 buffer[frame_start + i] = 0xFE;
1056 }
1057 self->output_len += FRAME_HEADER_SIZE;
1058 }
1059 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001060 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001061 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001062 buffer[self->output_len + i] = s[i];
1063 }
1064 }
1065 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001066 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001067 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001068 self->output_len += data_len;
1069 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001070}
1071
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001072static PicklerObject *
1073_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001074{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001075 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001076
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001077 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1078 if (self == NULL)
1079 return NULL;
1080
1081 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001082 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001083 self->write = NULL;
1084 self->proto = 0;
1085 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001086 self->framing = 0;
1087 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001088 self->fast = 0;
1089 self->fast_nesting = 0;
1090 self->fix_imports = 0;
1091 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001092 self->max_output_len = WRITE_BUF_SIZE;
1093 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001094
1095 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001096 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1097 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001098
1099 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001100 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001101 return NULL;
1102 }
1103 return self;
1104}
1105
1106static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001107_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001108{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001109 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001110
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001111 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001112 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001113 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001114 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001115 proto = PyLong_AsLong(protocol);
1116 if (proto < 0) {
1117 if (proto == -1 && PyErr_Occurred())
1118 return -1;
1119 proto = HIGHEST_PROTOCOL;
1120 }
1121 else if (proto > HIGHEST_PROTOCOL) {
1122 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1123 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001124 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001125 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001126 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001127 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128 self->bin = proto > 0;
1129 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130 return 0;
1131}
1132
1133/* Returns -1 (with an exception set) on failure, 0 on success. This may
1134 be called once on a freshly created Pickler. */
1135static int
1136_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1137{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001138 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001139 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001140 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001141 if (self->write == NULL) {
1142 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1143 PyErr_SetString(PyExc_TypeError,
1144 "file must have a 'write' attribute");
1145 return -1;
1146 }
1147
1148 return 0;
1149}
1150
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001151/* Returns the size of the input on success, -1 on failure. This takes its
1152 own reference to `input`. */
1153static Py_ssize_t
1154_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1155{
1156 if (self->buffer.buf != NULL)
1157 PyBuffer_Release(&self->buffer);
1158 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1159 return -1;
1160 self->input_buffer = self->buffer.buf;
1161 self->input_len = self->buffer.len;
1162 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001163 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001164 return self->input_len;
1165}
1166
Antoine Pitrou04248a82010-10-12 20:51:21 +00001167static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001168bad_readline(void)
1169{
1170 PickleState *st = _Pickle_GetGlobalState();
1171 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1172 return -1;
1173}
1174
1175static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001176_Unpickler_SkipConsumed(UnpicklerObject *self)
1177{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001178 Py_ssize_t consumed;
1179 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001180
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001181 consumed = self->next_read_idx - self->prefetched_idx;
1182 if (consumed <= 0)
1183 return 0;
1184
1185 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001186 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001187 r = PyObject_CallFunction(self->read, "n", consumed);
1188 if (r == NULL)
1189 return -1;
1190 Py_DECREF(r);
1191
1192 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001193 return 0;
1194}
1195
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001196static const Py_ssize_t READ_WHOLE_LINE = -1;
1197
1198/* If reading from a file, we need to only pull the bytes we need, since there
1199 may be multiple pickle objects arranged contiguously in the same input
1200 buffer.
1201
1202 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1203 bytes from the input stream/buffer.
1204
1205 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1206 failure; on success, returns the number of bytes read from the file.
1207
1208 On success, self->input_len will be 0; this is intentional so that when
1209 unpickling from a file, the "we've run out of data" code paths will trigger,
1210 causing the Unpickler to go back to the file for more data. Use the returned
1211 size to tell you how much data you can process. */
1212static Py_ssize_t
1213_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1214{
1215 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001216 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001217
1218 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001219
Antoine Pitrou04248a82010-10-12 20:51:21 +00001220 if (_Unpickler_SkipConsumed(self) < 0)
1221 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001222
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001223 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001224 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001225 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001226 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001227 PyObject *len;
1228 /* Prefetch some data without advancing the file pointer, if possible */
1229 if (self->peek && n < PREFETCH) {
1230 len = PyLong_FromSsize_t(PREFETCH);
1231 if (len == NULL)
1232 return -1;
1233 data = _Pickle_FastCall(self->peek, len);
1234 if (data == NULL) {
1235 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1236 return -1;
1237 /* peek() is probably not supported by the given file object */
1238 PyErr_Clear();
1239 Py_CLEAR(self->peek);
1240 }
1241 else {
1242 read_size = _Unpickler_SetStringInput(self, data);
1243 Py_DECREF(data);
1244 self->prefetched_idx = 0;
1245 if (n <= read_size)
1246 return n;
1247 }
1248 }
1249 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001250 if (len == NULL)
1251 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001252 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001253 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001254 if (data == NULL)
1255 return -1;
1256
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001257 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001258 Py_DECREF(data);
1259 return read_size;
1260}
1261
Victor Stinner19ed27e2016-05-20 11:42:37 +02001262/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001263static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001264_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001265{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001266 Py_ssize_t num_read;
1267
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001268 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001269 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1270 PickleState *st = _Pickle_GetGlobalState();
1271 PyErr_SetString(st->UnpicklingError,
1272 "read would overflow (invalid bytecode)");
1273 return -1;
1274 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001275
1276 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1277 assert(self->next_read_idx + n > self->input_len);
1278
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001279 if (!self->read)
1280 return bad_readline();
1281
Antoine Pitrou04248a82010-10-12 20:51:21 +00001282 num_read = _Unpickler_ReadFromFile(self, n);
1283 if (num_read < 0)
1284 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001285 if (num_read < n)
1286 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001287 *s = self->input_buffer;
1288 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001289 return n;
1290}
1291
Victor Stinner19ed27e2016-05-20 11:42:37 +02001292/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1293
1294 This should be used for all data reads, rather than accessing the unpickler's
1295 input buffer directly. This method deals correctly with reading from input
1296 streams, which the input buffer doesn't deal with.
1297
1298 Note that when reading from a file-like object, self->next_read_idx won't
1299 be updated (it should remain at 0 for the entire unpickling process). You
1300 should use this function's return value to know how many bytes you can
1301 consume.
1302
1303 Returns -1 (with an exception set) on failure. On success, return the
1304 number of chars read. */
1305#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001306 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001307 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1308 (self)->next_read_idx += (n), \
1309 (n)) \
1310 : _Unpickler_ReadImpl(self, (s), (n)))
1311
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001312static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001313_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1314 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001315{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001316 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001317 if (input_line == NULL) {
1318 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001319 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001320 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001321
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001322 memcpy(input_line, line, len);
1323 input_line[len] = '\0';
1324 self->input_line = input_line;
1325 *result = self->input_line;
1326 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001327}
1328
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001329/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001330 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001331
1332 Returns the number of chars read, or -1 on failure. */
1333static Py_ssize_t
1334_Unpickler_Readline(UnpicklerObject *self, char **result)
1335{
1336 Py_ssize_t i, num_read;
1337
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001338 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001339 if (self->input_buffer[i] == '\n') {
1340 char *line_start = self->input_buffer + self->next_read_idx;
1341 num_read = i - self->next_read_idx + 1;
1342 self->next_read_idx = i + 1;
1343 return _Unpickler_CopyLine(self, line_start, num_read, result);
1344 }
1345 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001346 if (!self->read)
1347 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001348
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001349 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1350 if (num_read < 0)
1351 return -1;
1352 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1353 return bad_readline();
1354 self->next_read_idx = num_read;
1355 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001356}
1357
1358/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1359 will be modified in place. */
1360static int
1361_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1362{
1363 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001364
1365 assert(new_size > self->memo_size);
1366
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001367 PyMem_RESIZE(self->memo, PyObject *, new_size);
1368 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001369 PyErr_NoMemory();
1370 return -1;
1371 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001372 for (i = self->memo_size; i < new_size; i++)
1373 self->memo[i] = NULL;
1374 self->memo_size = new_size;
1375 return 0;
1376}
1377
1378/* Returns NULL if idx is out of bounds. */
1379static PyObject *
1380_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1381{
1382 if (idx < 0 || idx >= self->memo_size)
1383 return NULL;
1384
1385 return self->memo[idx];
1386}
1387
1388/* Returns -1 (with an exception set) on failure, 0 on success.
1389 This takes its own reference to `value`. */
1390static int
1391_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1392{
1393 PyObject *old_item;
1394
1395 if (idx >= self->memo_size) {
1396 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1397 return -1;
1398 assert(idx < self->memo_size);
1399 }
1400 Py_INCREF(value);
1401 old_item = self->memo[idx];
1402 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001403 if (old_item != NULL) {
1404 Py_DECREF(old_item);
1405 }
1406 else {
1407 self->memo_len++;
1408 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001409 return 0;
1410}
1411
1412static PyObject **
1413_Unpickler_NewMemo(Py_ssize_t new_size)
1414{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001415 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001416 if (memo == NULL) {
1417 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001418 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001419 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001420 memset(memo, 0, new_size * sizeof(PyObject *));
1421 return memo;
1422}
1423
1424/* Free the unpickler's memo, taking care to decref any items left in it. */
1425static void
1426_Unpickler_MemoCleanup(UnpicklerObject *self)
1427{
1428 Py_ssize_t i;
1429 PyObject **memo = self->memo;
1430
1431 if (self->memo == NULL)
1432 return;
1433 self->memo = NULL;
1434 i = self->memo_size;
1435 while (--i >= 0) {
1436 Py_XDECREF(memo[i]);
1437 }
1438 PyMem_FREE(memo);
1439}
1440
1441static UnpicklerObject *
1442_Unpickler_New(void)
1443{
1444 UnpicklerObject *self;
1445
1446 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1447 if (self == NULL)
1448 return NULL;
1449
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001450 self->pers_func = NULL;
1451 self->input_buffer = NULL;
1452 self->input_line = NULL;
1453 self->input_len = 0;
1454 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001455 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001456 self->read = NULL;
1457 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001458 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001459 self->encoding = NULL;
1460 self->errors = NULL;
1461 self->marks = NULL;
1462 self->num_marks = 0;
1463 self->marks_size = 0;
1464 self->proto = 0;
1465 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001466 memset(&self->buffer, 0, sizeof(Py_buffer));
1467 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001468 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001469 self->memo = _Unpickler_NewMemo(self->memo_size);
1470 self->stack = (Pdata *)Pdata_New();
1471
1472 if (self->memo == NULL || self->stack == NULL) {
1473 Py_DECREF(self);
1474 return NULL;
1475 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001476
1477 return self;
1478}
1479
1480/* Returns -1 (with an exception set) on failure, 0 on success. This may
1481 be called once on a freshly created Pickler. */
1482static int
1483_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1484{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001485 _Py_IDENTIFIER(peek);
1486 _Py_IDENTIFIER(read);
1487 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001488
1489 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001490 if (self->peek == NULL) {
1491 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1492 PyErr_Clear();
1493 else
1494 return -1;
1495 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001496 self->read = _PyObject_GetAttrId(file, &PyId_read);
1497 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001498 if (self->readline == NULL || self->read == NULL) {
1499 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1500 PyErr_SetString(PyExc_TypeError,
1501 "file must have 'read' and 'readline' attributes");
1502 Py_CLEAR(self->read);
1503 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001504 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001505 return -1;
1506 }
1507 return 0;
1508}
1509
1510/* Returns -1 (with an exception set) on failure, 0 on success. This may
1511 be called once on a freshly created Pickler. */
1512static int
1513_Unpickler_SetInputEncoding(UnpicklerObject *self,
1514 const char *encoding,
1515 const char *errors)
1516{
1517 if (encoding == NULL)
1518 encoding = "ASCII";
1519 if (errors == NULL)
1520 errors = "strict";
1521
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001522 self->encoding = _PyMem_Strdup(encoding);
1523 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001524 if (self->encoding == NULL || self->errors == NULL) {
1525 PyErr_NoMemory();
1526 return -1;
1527 }
1528 return 0;
1529}
1530
1531/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001532static int
1533memo_get(PicklerObject *self, PyObject *key)
1534{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001535 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001536 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001537 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001538
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001539 value = PyMemoTable_Get(self->memo, key);
1540 if (value == NULL) {
1541 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001542 return -1;
1543 }
1544
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001545 if (!self->bin) {
1546 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001547 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1548 "%" PY_FORMAT_SIZE_T "d\n", *value);
1549 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001550 }
1551 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001552 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001553 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001554 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001555 len = 2;
1556 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001557 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001559 pdata[1] = (unsigned char)(*value & 0xff);
1560 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1561 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1562 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001563 len = 5;
1564 }
1565 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001566 PickleState *st = _Pickle_GetGlobalState();
1567 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001568 "memo id too large for LONG_BINGET");
1569 return -1;
1570 }
1571 }
1572
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001573 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001574 return -1;
1575
1576 return 0;
1577}
1578
1579/* Store an object in the memo, assign it a new unique ID based on the number
1580 of objects currently stored in the memo and generate a PUT opcode. */
1581static int
1582memo_put(PicklerObject *self, PyObject *obj)
1583{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001584 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001585 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001586 Py_ssize_t idx;
1587
1588 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001589
1590 if (self->fast)
1591 return 0;
1592
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001593 idx = PyMemoTable_Size(self->memo);
1594 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1595 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001596
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001597 if (self->proto >= 4) {
1598 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1599 return -1;
1600 return 0;
1601 }
1602 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001603 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001604 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001605 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001606 len = strlen(pdata);
1607 }
1608 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001609 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001610 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001612 len = 2;
1613 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001614 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001615 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001616 pdata[1] = (unsigned char)(idx & 0xff);
1617 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1618 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1619 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001620 len = 5;
1621 }
1622 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001623 PickleState *st = _Pickle_GetGlobalState();
1624 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001625 "memo id too large for LONG_BINPUT");
1626 return -1;
1627 }
1628 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001629 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001630 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001631
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001632 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001633}
1634
1635static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001636get_dotted_path(PyObject *obj, PyObject *name)
1637{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001638 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001639 PyObject *dotted_path;
1640 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001641
1642 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001643 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001644 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001645 n = PyList_GET_SIZE(dotted_path);
1646 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001647 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001649 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001650 if (obj == NULL)
1651 PyErr_Format(PyExc_AttributeError,
1652 "Can't pickle local object %R", name);
1653 else
1654 PyErr_Format(PyExc_AttributeError,
1655 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001656 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001657 return NULL;
1658 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001659 }
1660 return dotted_path;
1661}
1662
1663static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001664get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001665{
1666 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001667 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001668
1669 assert(PyList_CheckExact(names));
1670 Py_INCREF(obj);
1671 n = PyList_GET_SIZE(names);
1672 for (i = 0; i < n; i++) {
1673 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001674 Py_XDECREF(parent);
1675 parent = obj;
1676 obj = PyObject_GetAttr(parent, name);
1677 if (obj == NULL) {
1678 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001679 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001680 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001681 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001682 if (pparent != NULL)
1683 *pparent = parent;
1684 else
1685 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001686 return obj;
1687}
1688
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001689static void
1690reformat_attribute_error(PyObject *obj, PyObject *name)
1691{
1692 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1693 PyErr_Clear();
1694 PyErr_Format(PyExc_AttributeError,
1695 "Can't get attribute %R on %R", name, obj);
1696 }
1697}
1698
1699
1700static PyObject *
1701getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1702{
1703 PyObject *dotted_path, *attr;
1704
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001705 if (allow_qualname) {
1706 dotted_path = get_dotted_path(obj, name);
1707 if (dotted_path == NULL)
1708 return NULL;
1709 attr = get_deep_attribute(obj, dotted_path, NULL);
1710 Py_DECREF(dotted_path);
1711 }
1712 else
1713 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001714 if (attr == NULL)
1715 reformat_attribute_error(obj, name);
1716 return attr;
1717}
1718
Eric Snow3f9eee62017-09-15 16:35:20 -06001719static int
1720_checkmodule(PyObject *module_name, PyObject *module,
1721 PyObject *global, PyObject *dotted_path)
1722{
1723 if (module == Py_None) {
1724 return -1;
1725 }
1726 if (PyUnicode_Check(module_name) &&
1727 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1728 return -1;
1729 }
1730
1731 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1732 if (candidate == NULL) {
1733 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1734 PyErr_Clear();
1735 }
1736 return -1;
1737 }
1738 if (candidate != global) {
1739 Py_DECREF(candidate);
1740 return -1;
1741 }
1742 Py_DECREF(candidate);
1743 return 0;
1744}
1745
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001746static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001747whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001748{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001749 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001750 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001751 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001752 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001753 _Py_IDENTIFIER(__module__);
1754 _Py_IDENTIFIER(modules);
1755 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001756
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001757 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1758
1759 if (module_name == NULL) {
1760 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001761 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001762 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001763 }
1764 else {
1765 /* In some rare cases (e.g., bound methods of extension types),
1766 __module__ can be None. If it is so, then search sys.modules for
1767 the module of global. */
1768 if (module_name != Py_None)
1769 return module_name;
1770 Py_CLEAR(module_name);
1771 }
1772 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001773
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001774 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001775 modules = _PySys_GetObjectId(&PyId_modules);
1776 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001777 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001778 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001779 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001780 if (PyDict_CheckExact(modules)) {
1781 i = 0;
1782 while (PyDict_Next(modules, &i, &module_name, &module)) {
1783 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1784 Py_INCREF(module_name);
1785 return module_name;
1786 }
1787 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001788 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001789 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001790 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001791 }
1792 else {
1793 PyObject *iterator = PyObject_GetIter(modules);
1794 if (iterator == NULL) {
1795 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001796 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001797 while ((module_name = PyIter_Next(iterator))) {
1798 module = PyObject_GetItem(modules, module_name);
1799 if (module == NULL) {
1800 Py_DECREF(module_name);
1801 Py_DECREF(iterator);
1802 return NULL;
1803 }
1804 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1805 Py_DECREF(module);
1806 Py_DECREF(iterator);
1807 return module_name;
1808 }
1809 Py_DECREF(module);
1810 Py_DECREF(module_name);
1811 if (PyErr_Occurred()) {
1812 Py_DECREF(iterator);
1813 return NULL;
1814 }
1815 }
1816 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001817 }
1818
1819 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001820 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001821 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001822 return module_name;
1823}
1824
1825/* fast_save_enter() and fast_save_leave() are guards against recursive
1826 objects when Pickler is used with the "fast mode" (i.e., with object
1827 memoization disabled). If the nesting of a list or dict object exceed
1828 FAST_NESTING_LIMIT, these guards will start keeping an internal
1829 reference to the seen list or dict objects and check whether these objects
1830 are recursive. These are not strictly necessary, since save() has a
1831 hard-coded recursion limit, but they give a nicer error message than the
1832 typical RuntimeError. */
1833static int
1834fast_save_enter(PicklerObject *self, PyObject *obj)
1835{
1836 /* if fast_nesting < 0, we're doing an error exit. */
1837 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1838 PyObject *key = NULL;
1839 if (self->fast_memo == NULL) {
1840 self->fast_memo = PyDict_New();
1841 if (self->fast_memo == NULL) {
1842 self->fast_nesting = -1;
1843 return 0;
1844 }
1845 }
1846 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001847 if (key == NULL) {
1848 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001849 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001850 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001851 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001852 Py_DECREF(key);
1853 PyErr_Format(PyExc_ValueError,
1854 "fast mode: can't pickle cyclic objects "
1855 "including object type %.200s at %p",
1856 obj->ob_type->tp_name, obj);
1857 self->fast_nesting = -1;
1858 return 0;
1859 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001860 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001861 Py_DECREF(key);
1862 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001863 return 0;
1864 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1866 Py_DECREF(key);
1867 self->fast_nesting = -1;
1868 return 0;
1869 }
1870 Py_DECREF(key);
1871 }
1872 return 1;
1873}
1874
1875static int
1876fast_save_leave(PicklerObject *self, PyObject *obj)
1877{
1878 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1879 PyObject *key = PyLong_FromVoidPtr(obj);
1880 if (key == NULL)
1881 return 0;
1882 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1883 Py_DECREF(key);
1884 return 0;
1885 }
1886 Py_DECREF(key);
1887 }
1888 return 1;
1889}
1890
1891static int
1892save_none(PicklerObject *self, PyObject *obj)
1893{
1894 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001895 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001896 return -1;
1897
1898 return 0;
1899}
1900
1901static int
1902save_bool(PicklerObject *self, PyObject *obj)
1903{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001904 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001905 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001906 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001907 return -1;
1908 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001909 else {
1910 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1911 * so that unpicklers written before bools were introduced unpickle them
1912 * as ints, but unpicklers after can recognize that bools were intended.
1913 * Note that protocol 2 added direct ways to pickle bools.
1914 */
1915 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1916 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1917 return -1;
1918 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001919 return 0;
1920}
1921
1922static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001923save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001924{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001925 PyObject *repr = NULL;
1926 Py_ssize_t size;
1927 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001928 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001929 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001930
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001931 val= PyLong_AsLongAndOverflow(obj, &overflow);
1932 if (!overflow && (sizeof(long) <= 4 ||
1933 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1934 {
Larry Hastings61272b72014-01-07 12:41:53 -08001935 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001936
1937 Note: we can't use -0x80000000L in the above condition because some
1938 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1939 before applying the unary minus when sizeof(long) <= 4. The
1940 resulting value stays unsigned which is commonly not what we want,
1941 so MSVC happily warns us about it. However, that result would have
1942 been fine because we guard for sizeof(long) <= 4 which turns the
1943 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001944 char pdata[32];
1945 Py_ssize_t len = 0;
1946
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001947 if (self->bin) {
1948 pdata[1] = (unsigned char)(val & 0xff);
1949 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1950 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1951 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001952
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001953 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1954 pdata[0] = BININT;
1955 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001956 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001957 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001958 pdata[0] = BININT2;
1959 len = 3;
1960 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001961 else {
1962 pdata[0] = BININT1;
1963 len = 2;
1964 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001965 }
1966 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001967 sprintf(pdata, "%c%ld\n", INT, val);
1968 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001969 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001970 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001971 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001972
1973 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001974 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001975 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001977 if (self->proto >= 2) {
1978 /* Linear-time pickling. */
1979 size_t nbits;
1980 size_t nbytes;
1981 unsigned char *pdata;
1982 char header[5];
1983 int i;
1984 int sign = _PyLong_Sign(obj);
1985
1986 if (sign == 0) {
1987 header[0] = LONG1;
1988 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001989 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001990 goto error;
1991 return 0;
1992 }
1993 nbits = _PyLong_NumBits(obj);
1994 if (nbits == (size_t)-1 && PyErr_Occurred())
1995 goto error;
1996 /* How many bytes do we need? There are nbits >> 3 full
1997 * bytes of data, and nbits & 7 leftover bits. If there
1998 * are any leftover bits, then we clearly need another
1999 * byte. Wnat's not so obvious is that we *probably*
2000 * need another byte even if there aren't any leftovers:
2001 * the most-significant bit of the most-significant byte
2002 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002003 * opposite of the one we need. The exception is ints
2004 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002005 * its own 256's-complement, so has the right sign bit
2006 * even without the extra byte. That's a pain to check
2007 * for in advance, though, so we always grab an extra
2008 * byte at the start, and cut it back later if possible.
2009 */
2010 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002011 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002012 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002013 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002014 goto error;
2015 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002016 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002017 if (repr == NULL)
2018 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002019 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002020 i = _PyLong_AsByteArray((PyLongObject *)obj,
2021 pdata, nbytes,
2022 1 /* little endian */ , 1 /* signed */ );
2023 if (i < 0)
2024 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002025 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002026 * needed. This is so iff the MSB is all redundant sign
2027 * bits.
2028 */
2029 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002030 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002031 pdata[nbytes - 1] == 0xff &&
2032 (pdata[nbytes - 2] & 0x80) != 0) {
2033 nbytes--;
2034 }
2035
2036 if (nbytes < 256) {
2037 header[0] = LONG1;
2038 header[1] = (unsigned char)nbytes;
2039 size = 2;
2040 }
2041 else {
2042 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002043 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002044 for (i = 1; i < 5; i++) {
2045 header[i] = (unsigned char)(size & 0xff);
2046 size >>= 8;
2047 }
2048 size = 5;
2049 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002050 if (_Pickler_Write(self, header, size) < 0 ||
2051 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002052 goto error;
2053 }
2054 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002055 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002056 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002057
Mark Dickinson8dd05142009-01-20 20:43:58 +00002058 /* proto < 2: write the repr and newline. This is quadratic-time (in
2059 the number of digits), in both directions. We add a trailing 'L'
2060 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061
2062 repr = PyObject_Repr(obj);
2063 if (repr == NULL)
2064 goto error;
2065
Serhiy Storchaka06515832016-11-20 09:13:07 +02002066 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002067 if (string == NULL)
2068 goto error;
2069
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002070 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2071 _Pickler_Write(self, string, size) < 0 ||
2072 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073 goto error;
2074 }
2075
2076 if (0) {
2077 error:
2078 status = -1;
2079 }
2080 Py_XDECREF(repr);
2081
2082 return status;
2083}
2084
2085static int
2086save_float(PicklerObject *self, PyObject *obj)
2087{
2088 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2089
2090 if (self->bin) {
2091 char pdata[9];
2092 pdata[0] = BINFLOAT;
2093 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2094 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002095 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002097 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002099 int result = -1;
2100 char *buf = NULL;
2101 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002102
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002103 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002104 goto done;
2105
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002106 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002107 if (!buf) {
2108 PyErr_NoMemory();
2109 goto done;
2110 }
2111
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002112 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002113 goto done;
2114
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002115 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002116 goto done;
2117
2118 result = 0;
2119done:
2120 PyMem_Free(buf);
2121 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002122 }
2123
2124 return 0;
2125}
2126
2127static int
2128save_bytes(PicklerObject *self, PyObject *obj)
2129{
2130 if (self->proto < 3) {
2131 /* Older pickle protocols do not have an opcode for pickling bytes
2132 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002133 the __reduce__ method) to permit bytes object unpickling.
2134
2135 Here we use a hack to be compatible with Python 2. Since in Python
2136 2 'bytes' is just an alias for 'str' (which has different
2137 parameters than the actual bytes object), we use codecs.encode
2138 to create the appropriate 'str' object when unpickled using
2139 Python 2 *and* the appropriate 'bytes' object when unpickled
2140 using Python 3. Again this is a hack and we don't need to do this
2141 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002142 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002143 int status;
2144
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002145 if (PyBytes_GET_SIZE(obj) == 0) {
2146 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2147 }
2148 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002149 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002150 PyObject *unicode_str =
2151 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2152 PyBytes_GET_SIZE(obj),
2153 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002154 _Py_IDENTIFIER(latin1);
2155
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002156 if (unicode_str == NULL)
2157 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002158 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002159 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002160 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002161 Py_DECREF(unicode_str);
2162 }
2163
2164 if (reduce_value == NULL)
2165 return -1;
2166
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002167 /* save_reduce() will memoize the object automatically. */
2168 status = save_reduce(self, reduce_value, obj);
2169 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002170 return status;
2171 }
2172 else {
2173 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002174 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002175 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002176
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002177 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002178 if (size < 0)
2179 return -1;
2180
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002181 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002182 header[0] = SHORT_BINBYTES;
2183 header[1] = (unsigned char)size;
2184 len = 2;
2185 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002186 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002187 header[0] = BINBYTES;
2188 header[1] = (unsigned char)(size & 0xff);
2189 header[2] = (unsigned char)((size >> 8) & 0xff);
2190 header[3] = (unsigned char)((size >> 16) & 0xff);
2191 header[4] = (unsigned char)((size >> 24) & 0xff);
2192 len = 5;
2193 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002194 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002195 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002196 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002197 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002198 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002199 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002200 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002201 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002202 return -1; /* string too large */
2203 }
2204
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002205 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002206 return -1;
2207
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002208 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002209 return -1;
2210
2211 if (memo_put(self, obj) < 0)
2212 return -1;
2213
2214 return 0;
2215 }
2216}
2217
2218/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2219 backslash and newline characters to \uXXXX escapes. */
2220static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002221raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002222{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002223 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002224 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002225 void *data;
2226 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002227 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002228
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002229 if (PyUnicode_READY(obj))
2230 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002231
Victor Stinner358af132015-10-12 22:36:57 +02002232 _PyBytesWriter_Init(&writer);
2233
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002234 size = PyUnicode_GET_LENGTH(obj);
2235 data = PyUnicode_DATA(obj);
2236 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002237
Victor Stinner358af132015-10-12 22:36:57 +02002238 p = _PyBytesWriter_Alloc(&writer, size);
2239 if (p == NULL)
2240 goto error;
2241 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002242
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002243 for (i=0; i < size; i++) {
2244 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002245 /* Map 32-bit characters to '\Uxxxxxxxx' */
2246 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002247 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002248 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2249 if (p == NULL)
2250 goto error;
2251
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 *p++ = '\\';
2253 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002254 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2255 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2256 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2257 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2258 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2259 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2260 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2261 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002262 }
Victor Stinner358af132015-10-12 22:36:57 +02002263 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002264 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002265 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002266 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2267 if (p == NULL)
2268 goto error;
2269
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002270 *p++ = '\\';
2271 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002272 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2273 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2274 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2275 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002277 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002278 else
2279 *p++ = (char) ch;
2280 }
Victor Stinner358af132015-10-12 22:36:57 +02002281
2282 return _PyBytesWriter_Finish(&writer, p);
2283
2284error:
2285 _PyBytesWriter_Dealloc(&writer);
2286 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287}
2288
2289static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002290write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002291{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002292 char header[9];
2293 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002294
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002295 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002296 if (size <= 0xff && self->proto >= 4) {
2297 header[0] = SHORT_BINUNICODE;
2298 header[1] = (unsigned char)(size & 0xff);
2299 len = 2;
2300 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002301 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002302 header[0] = BINUNICODE;
2303 header[1] = (unsigned char)(size & 0xff);
2304 header[2] = (unsigned char)((size >> 8) & 0xff);
2305 header[3] = (unsigned char)((size >> 16) & 0xff);
2306 header[4] = (unsigned char)((size >> 24) & 0xff);
2307 len = 5;
2308 }
2309 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002310 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002311 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002312 len = 9;
2313 }
2314 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002315 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002316 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002317 return -1;
2318 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002319
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002320 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002321 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002322 if (_Pickler_Write(self, data, size) < 0)
2323 return -1;
2324
2325 return 0;
2326}
2327
2328static int
2329write_unicode_binary(PicklerObject *self, PyObject *obj)
2330{
2331 PyObject *encoded = NULL;
2332 Py_ssize_t size;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002333 const char *data;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002334 int r;
2335
2336 if (PyUnicode_READY(obj))
2337 return -1;
2338
2339 data = PyUnicode_AsUTF8AndSize(obj, &size);
2340 if (data != NULL)
2341 return write_utf8(self, data, size);
2342
2343 /* Issue #8383: for strings with lone surrogates, fallback on the
2344 "surrogatepass" error handler. */
2345 PyErr_Clear();
2346 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2347 if (encoded == NULL)
2348 return -1;
2349
2350 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2351 PyBytes_GET_SIZE(encoded));
2352 Py_DECREF(encoded);
2353 return r;
2354}
2355
2356static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002357save_unicode(PicklerObject *self, PyObject *obj)
2358{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002359 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002360 if (write_unicode_binary(self, obj) < 0)
2361 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002362 }
2363 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002364 PyObject *encoded;
2365 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002366 const char unicode_op = UNICODE;
2367
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002368 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002369 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002370 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002371
Antoine Pitrou299978d2013-04-07 17:38:11 +02002372 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2373 Py_DECREF(encoded);
2374 return -1;
2375 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002376
2377 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002378 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2379 Py_DECREF(encoded);
2380 return -1;
2381 }
2382 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002383
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002384 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002385 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002386 }
2387 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002388 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002389
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002390 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002391}
2392
2393/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2394static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002395store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002396{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002397 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002398
2399 assert(PyTuple_Size(t) == len);
2400
2401 for (i = 0; i < len; i++) {
2402 PyObject *element = PyTuple_GET_ITEM(t, i);
2403
2404 if (element == NULL)
2405 return -1;
2406 if (save(self, element, 0) < 0)
2407 return -1;
2408 }
2409
2410 return 0;
2411}
2412
2413/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2414 * used across protocols to minimize the space needed to pickle them.
2415 * Tuples are also the only builtin immutable type that can be recursive
2416 * (a tuple can be reached from itself), and that requires some subtle
2417 * magic so that it works in all cases. IOW, this is a long routine.
2418 */
2419static int
2420save_tuple(PicklerObject *self, PyObject *obj)
2421{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002422 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002423
2424 const char mark_op = MARK;
2425 const char tuple_op = TUPLE;
2426 const char pop_op = POP;
2427 const char pop_mark_op = POP_MARK;
2428 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2429
2430 if ((len = PyTuple_Size(obj)) < 0)
2431 return -1;
2432
2433 if (len == 0) {
2434 char pdata[2];
2435
2436 if (self->proto) {
2437 pdata[0] = EMPTY_TUPLE;
2438 len = 1;
2439 }
2440 else {
2441 pdata[0] = MARK;
2442 pdata[1] = TUPLE;
2443 len = 2;
2444 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002445 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002446 return -1;
2447 return 0;
2448 }
2449
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002450 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002451 * saving the tuple elements, the tuple must be recursive, in
2452 * which case we'll pop everything we put on the stack, and fetch
2453 * its value from the memo.
2454 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455 if (len <= 3 && self->proto >= 2) {
2456 /* Use TUPLE{1,2,3} opcodes. */
2457 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002458 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002459
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002460 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002461 /* pop the len elements */
2462 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002463 if (_Pickler_Write(self, &pop_op, 1) < 0)
2464 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002465 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002466 if (memo_get(self, obj) < 0)
2467 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002468
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002469 return 0;
2470 }
2471 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002472 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2473 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002474 }
2475 goto memoize;
2476 }
2477
2478 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2479 * Generate MARK e1 e2 ... TUPLE
2480 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002481 if (_Pickler_Write(self, &mark_op, 1) < 0)
2482 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002483
2484 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002485 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002486
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002487 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002488 /* pop the stack stuff we pushed */
2489 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002490 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2491 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002492 }
2493 else {
2494 /* Note that we pop one more than len, to remove
2495 * the MARK too.
2496 */
2497 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002498 if (_Pickler_Write(self, &pop_op, 1) < 0)
2499 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002500 }
2501 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002502 if (memo_get(self, obj) < 0)
2503 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002504
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002505 return 0;
2506 }
2507 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002508 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2509 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002510 }
2511
2512 memoize:
2513 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002514 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002516 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517}
2518
2519/* iter is an iterator giving items, and we batch up chunks of
2520 * MARK item item ... item APPENDS
2521 * opcode sequences. Calling code should have arranged to first create an
2522 * empty list, or list-like object, for the APPENDS to operate on.
2523 * Returns 0 on success, <0 on error.
2524 */
2525static int
2526batch_list(PicklerObject *self, PyObject *iter)
2527{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002528 PyObject *obj = NULL;
2529 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002530 int i, n;
2531
2532 const char mark_op = MARK;
2533 const char append_op = APPEND;
2534 const char appends_op = APPENDS;
2535
2536 assert(iter != NULL);
2537
2538 /* XXX: I think this function could be made faster by avoiding the
2539 iterator interface and fetching objects directly from list using
2540 PyList_GET_ITEM.
2541 */
2542
2543 if (self->proto == 0) {
2544 /* APPENDS isn't available; do one at a time. */
2545 for (;;) {
2546 obj = PyIter_Next(iter);
2547 if (obj == NULL) {
2548 if (PyErr_Occurred())
2549 return -1;
2550 break;
2551 }
2552 i = save(self, obj, 0);
2553 Py_DECREF(obj);
2554 if (i < 0)
2555 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002556 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002557 return -1;
2558 }
2559 return 0;
2560 }
2561
2562 /* proto > 0: write in batches of BATCHSIZE. */
2563 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002564 /* Get first item */
2565 firstitem = PyIter_Next(iter);
2566 if (firstitem == NULL) {
2567 if (PyErr_Occurred())
2568 goto error;
2569
2570 /* nothing more to add */
2571 break;
2572 }
2573
2574 /* Try to get a second item */
2575 obj = PyIter_Next(iter);
2576 if (obj == NULL) {
2577 if (PyErr_Occurred())
2578 goto error;
2579
2580 /* Only one item to write */
2581 if (save(self, firstitem, 0) < 0)
2582 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002583 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002584 goto error;
2585 Py_CLEAR(firstitem);
2586 break;
2587 }
2588
2589 /* More than one item to write */
2590
2591 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002592 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002593 goto error;
2594
2595 if (save(self, firstitem, 0) < 0)
2596 goto error;
2597 Py_CLEAR(firstitem);
2598 n = 1;
2599
2600 /* Fetch and save up to BATCHSIZE items */
2601 while (obj) {
2602 if (save(self, obj, 0) < 0)
2603 goto error;
2604 Py_CLEAR(obj);
2605 n += 1;
2606
2607 if (n == BATCHSIZE)
2608 break;
2609
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002610 obj = PyIter_Next(iter);
2611 if (obj == NULL) {
2612 if (PyErr_Occurred())
2613 goto error;
2614 break;
2615 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002616 }
2617
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002618 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002619 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002620
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002621 } while (n == BATCHSIZE);
2622 return 0;
2623
2624 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002625 Py_XDECREF(firstitem);
2626 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002627 return -1;
2628}
2629
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002630/* This is a variant of batch_list() above, specialized for lists (with no
2631 * support for list subclasses). Like batch_list(), we batch up chunks of
2632 * MARK item item ... item APPENDS
2633 * opcode sequences. Calling code should have arranged to first create an
2634 * empty list, or list-like object, for the APPENDS to operate on.
2635 * Returns 0 on success, -1 on error.
2636 *
2637 * This version is considerably faster than batch_list(), if less general.
2638 *
2639 * Note that this only works for protocols > 0.
2640 */
2641static int
2642batch_list_exact(PicklerObject *self, PyObject *obj)
2643{
2644 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002645 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002646
2647 const char append_op = APPEND;
2648 const char appends_op = APPENDS;
2649 const char mark_op = MARK;
2650
2651 assert(obj != NULL);
2652 assert(self->proto > 0);
2653 assert(PyList_CheckExact(obj));
2654
2655 if (PyList_GET_SIZE(obj) == 1) {
2656 item = PyList_GET_ITEM(obj, 0);
2657 if (save(self, item, 0) < 0)
2658 return -1;
2659 if (_Pickler_Write(self, &append_op, 1) < 0)
2660 return -1;
2661 return 0;
2662 }
2663
2664 /* Write in batches of BATCHSIZE. */
2665 total = 0;
2666 do {
2667 this_batch = 0;
2668 if (_Pickler_Write(self, &mark_op, 1) < 0)
2669 return -1;
2670 while (total < PyList_GET_SIZE(obj)) {
2671 item = PyList_GET_ITEM(obj, total);
2672 if (save(self, item, 0) < 0)
2673 return -1;
2674 total++;
2675 if (++this_batch == BATCHSIZE)
2676 break;
2677 }
2678 if (_Pickler_Write(self, &appends_op, 1) < 0)
2679 return -1;
2680
2681 } while (total < PyList_GET_SIZE(obj));
2682
2683 return 0;
2684}
2685
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002686static int
2687save_list(PicklerObject *self, PyObject *obj)
2688{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002689 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002690 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002691 int status = 0;
2692
2693 if (self->fast && !fast_save_enter(self, obj))
2694 goto error;
2695
2696 /* Create an empty list. */
2697 if (self->bin) {
2698 header[0] = EMPTY_LIST;
2699 len = 1;
2700 }
2701 else {
2702 header[0] = MARK;
2703 header[1] = LIST;
2704 len = 2;
2705 }
2706
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002707 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002708 goto error;
2709
2710 /* Get list length, and bow out early if empty. */
2711 if ((len = PyList_Size(obj)) < 0)
2712 goto error;
2713
2714 if (memo_put(self, obj) < 0)
2715 goto error;
2716
2717 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002718 /* Materialize the list elements. */
2719 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002720 if (Py_EnterRecursiveCall(" while pickling an object"))
2721 goto error;
2722 status = batch_list_exact(self, obj);
2723 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002724 } else {
2725 PyObject *iter = PyObject_GetIter(obj);
2726 if (iter == NULL)
2727 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002728
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002729 if (Py_EnterRecursiveCall(" while pickling an object")) {
2730 Py_DECREF(iter);
2731 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002732 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002733 status = batch_list(self, iter);
2734 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002735 Py_DECREF(iter);
2736 }
2737 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002738 if (0) {
2739 error:
2740 status = -1;
2741 }
2742
2743 if (self->fast && !fast_save_leave(self, obj))
2744 status = -1;
2745
2746 return status;
2747}
2748
2749/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2750 * MARK key value ... key value SETITEMS
2751 * opcode sequences. Calling code should have arranged to first create an
2752 * empty dict, or dict-like object, for the SETITEMS to operate on.
2753 * Returns 0 on success, <0 on error.
2754 *
2755 * This is very much like batch_list(). The difference between saving
2756 * elements directly, and picking apart two-tuples, is so long-winded at
2757 * the C level, though, that attempts to combine these routines were too
2758 * ugly to bear.
2759 */
2760static int
2761batch_dict(PicklerObject *self, PyObject *iter)
2762{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002763 PyObject *obj = NULL;
2764 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002765 int i, n;
2766
2767 const char mark_op = MARK;
2768 const char setitem_op = SETITEM;
2769 const char setitems_op = SETITEMS;
2770
2771 assert(iter != NULL);
2772
2773 if (self->proto == 0) {
2774 /* SETITEMS isn't available; do one at a time. */
2775 for (;;) {
2776 obj = PyIter_Next(iter);
2777 if (obj == NULL) {
2778 if (PyErr_Occurred())
2779 return -1;
2780 break;
2781 }
2782 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2783 PyErr_SetString(PyExc_TypeError, "dict items "
2784 "iterator must return 2-tuples");
2785 return -1;
2786 }
2787 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2788 if (i >= 0)
2789 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2790 Py_DECREF(obj);
2791 if (i < 0)
2792 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002793 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002794 return -1;
2795 }
2796 return 0;
2797 }
2798
2799 /* proto > 0: write in batches of BATCHSIZE. */
2800 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002801 /* Get first item */
2802 firstitem = PyIter_Next(iter);
2803 if (firstitem == NULL) {
2804 if (PyErr_Occurred())
2805 goto error;
2806
2807 /* nothing more to add */
2808 break;
2809 }
2810 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2811 PyErr_SetString(PyExc_TypeError, "dict items "
2812 "iterator must return 2-tuples");
2813 goto error;
2814 }
2815
2816 /* Try to get a second item */
2817 obj = PyIter_Next(iter);
2818 if (obj == NULL) {
2819 if (PyErr_Occurred())
2820 goto error;
2821
2822 /* Only one item to write */
2823 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2824 goto error;
2825 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2826 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002827 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002828 goto error;
2829 Py_CLEAR(firstitem);
2830 break;
2831 }
2832
2833 /* More than one item to write */
2834
2835 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002836 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002837 goto error;
2838
2839 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2840 goto error;
2841 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2842 goto error;
2843 Py_CLEAR(firstitem);
2844 n = 1;
2845
2846 /* Fetch and save up to BATCHSIZE items */
2847 while (obj) {
2848 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2849 PyErr_SetString(PyExc_TypeError, "dict items "
2850 "iterator must return 2-tuples");
2851 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002852 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002853 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2854 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2855 goto error;
2856 Py_CLEAR(obj);
2857 n += 1;
2858
2859 if (n == BATCHSIZE)
2860 break;
2861
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002862 obj = PyIter_Next(iter);
2863 if (obj == NULL) {
2864 if (PyErr_Occurred())
2865 goto error;
2866 break;
2867 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002868 }
2869
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002870 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002871 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002872
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002873 } while (n == BATCHSIZE);
2874 return 0;
2875
2876 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002877 Py_XDECREF(firstitem);
2878 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002879 return -1;
2880}
2881
Collin Winter5c9b02d2009-05-25 05:43:30 +00002882/* This is a variant of batch_dict() above that specializes for dicts, with no
2883 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2884 * MARK key value ... key value SETITEMS
2885 * opcode sequences. Calling code should have arranged to first create an
2886 * empty dict, or dict-like object, for the SETITEMS to operate on.
2887 * Returns 0 on success, -1 on error.
2888 *
2889 * Note that this currently doesn't work for protocol 0.
2890 */
2891static int
2892batch_dict_exact(PicklerObject *self, PyObject *obj)
2893{
2894 PyObject *key = NULL, *value = NULL;
2895 int i;
2896 Py_ssize_t dict_size, ppos = 0;
2897
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002898 const char mark_op = MARK;
2899 const char setitem_op = SETITEM;
2900 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002901
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002902 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002903 assert(self->proto > 0);
2904
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002905 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002906
2907 /* Special-case len(d) == 1 to save space. */
2908 if (dict_size == 1) {
2909 PyDict_Next(obj, &ppos, &key, &value);
2910 if (save(self, key, 0) < 0)
2911 return -1;
2912 if (save(self, value, 0) < 0)
2913 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002914 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002915 return -1;
2916 return 0;
2917 }
2918
2919 /* Write in batches of BATCHSIZE. */
2920 do {
2921 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002922 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002923 return -1;
2924 while (PyDict_Next(obj, &ppos, &key, &value)) {
2925 if (save(self, key, 0) < 0)
2926 return -1;
2927 if (save(self, value, 0) < 0)
2928 return -1;
2929 if (++i == BATCHSIZE)
2930 break;
2931 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002932 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002933 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002934 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00002935 PyErr_Format(
2936 PyExc_RuntimeError,
2937 "dictionary changed size during iteration");
2938 return -1;
2939 }
2940
2941 } while (i == BATCHSIZE);
2942 return 0;
2943}
2944
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002945static int
2946save_dict(PicklerObject *self, PyObject *obj)
2947{
2948 PyObject *items, *iter;
2949 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002950 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002951 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002952 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002953
2954 if (self->fast && !fast_save_enter(self, obj))
2955 goto error;
2956
2957 /* Create an empty dict. */
2958 if (self->bin) {
2959 header[0] = EMPTY_DICT;
2960 len = 1;
2961 }
2962 else {
2963 header[0] = MARK;
2964 header[1] = DICT;
2965 len = 2;
2966 }
2967
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002968 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002969 goto error;
2970
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002971 if (memo_put(self, obj) < 0)
2972 goto error;
2973
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002974 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002975 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002976 if (PyDict_CheckExact(obj) && self->proto > 0) {
2977 /* We can take certain shortcuts if we know this is a dict and
2978 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002979 if (Py_EnterRecursiveCall(" while pickling an object"))
2980 goto error;
2981 status = batch_dict_exact(self, obj);
2982 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002983 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002984 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002985
Victor Stinnerad8c83a2016-09-05 17:53:15 -07002986 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002987 if (items == NULL)
2988 goto error;
2989 iter = PyObject_GetIter(items);
2990 Py_DECREF(items);
2991 if (iter == NULL)
2992 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002993 if (Py_EnterRecursiveCall(" while pickling an object")) {
2994 Py_DECREF(iter);
2995 goto error;
2996 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002997 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002998 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002999 Py_DECREF(iter);
3000 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003001 }
3002
3003 if (0) {
3004 error:
3005 status = -1;
3006 }
3007
3008 if (self->fast && !fast_save_leave(self, obj))
3009 status = -1;
3010
3011 return status;
3012}
3013
3014static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003015save_set(PicklerObject *self, PyObject *obj)
3016{
3017 PyObject *item;
3018 int i;
3019 Py_ssize_t set_size, ppos = 0;
3020 Py_hash_t hash;
3021
3022 const char empty_set_op = EMPTY_SET;
3023 const char mark_op = MARK;
3024 const char additems_op = ADDITEMS;
3025
3026 if (self->proto < 4) {
3027 PyObject *items;
3028 PyObject *reduce_value;
3029 int status;
3030
3031 items = PySequence_List(obj);
3032 if (items == NULL) {
3033 return -1;
3034 }
3035 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3036 Py_DECREF(items);
3037 if (reduce_value == NULL) {
3038 return -1;
3039 }
3040 /* save_reduce() will memoize the object automatically. */
3041 status = save_reduce(self, reduce_value, obj);
3042 Py_DECREF(reduce_value);
3043 return status;
3044 }
3045
3046 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3047 return -1;
3048
3049 if (memo_put(self, obj) < 0)
3050 return -1;
3051
3052 set_size = PySet_GET_SIZE(obj);
3053 if (set_size == 0)
3054 return 0; /* nothing to do */
3055
3056 /* Write in batches of BATCHSIZE. */
3057 do {
3058 i = 0;
3059 if (_Pickler_Write(self, &mark_op, 1) < 0)
3060 return -1;
3061 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3062 if (save(self, item, 0) < 0)
3063 return -1;
3064 if (++i == BATCHSIZE)
3065 break;
3066 }
3067 if (_Pickler_Write(self, &additems_op, 1) < 0)
3068 return -1;
3069 if (PySet_GET_SIZE(obj) != set_size) {
3070 PyErr_Format(
3071 PyExc_RuntimeError,
3072 "set changed size during iteration");
3073 return -1;
3074 }
3075 } while (i == BATCHSIZE);
3076
3077 return 0;
3078}
3079
3080static int
3081save_frozenset(PicklerObject *self, PyObject *obj)
3082{
3083 PyObject *iter;
3084
3085 const char mark_op = MARK;
3086 const char frozenset_op = FROZENSET;
3087
3088 if (self->fast && !fast_save_enter(self, obj))
3089 return -1;
3090
3091 if (self->proto < 4) {
3092 PyObject *items;
3093 PyObject *reduce_value;
3094 int status;
3095
3096 items = PySequence_List(obj);
3097 if (items == NULL) {
3098 return -1;
3099 }
3100 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3101 items);
3102 Py_DECREF(items);
3103 if (reduce_value == NULL) {
3104 return -1;
3105 }
3106 /* save_reduce() will memoize the object automatically. */
3107 status = save_reduce(self, reduce_value, obj);
3108 Py_DECREF(reduce_value);
3109 return status;
3110 }
3111
3112 if (_Pickler_Write(self, &mark_op, 1) < 0)
3113 return -1;
3114
3115 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003116 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003117 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003118 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003119 for (;;) {
3120 PyObject *item;
3121
3122 item = PyIter_Next(iter);
3123 if (item == NULL) {
3124 if (PyErr_Occurred()) {
3125 Py_DECREF(iter);
3126 return -1;
3127 }
3128 break;
3129 }
3130 if (save(self, item, 0) < 0) {
3131 Py_DECREF(item);
3132 Py_DECREF(iter);
3133 return -1;
3134 }
3135 Py_DECREF(item);
3136 }
3137 Py_DECREF(iter);
3138
3139 /* If the object is already in the memo, this means it is
3140 recursive. In this case, throw away everything we put on the
3141 stack, and fetch the object back from the memo. */
3142 if (PyMemoTable_Get(self->memo, obj)) {
3143 const char pop_mark_op = POP_MARK;
3144
3145 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3146 return -1;
3147 if (memo_get(self, obj) < 0)
3148 return -1;
3149 return 0;
3150 }
3151
3152 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3153 return -1;
3154 if (memo_put(self, obj) < 0)
3155 return -1;
3156
3157 return 0;
3158}
3159
3160static int
3161fix_imports(PyObject **module_name, PyObject **global_name)
3162{
3163 PyObject *key;
3164 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003165 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003166
3167 key = PyTuple_Pack(2, *module_name, *global_name);
3168 if (key == NULL)
3169 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003170 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003171 Py_DECREF(key);
3172 if (item) {
3173 PyObject *fixed_module_name;
3174 PyObject *fixed_global_name;
3175
3176 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3177 PyErr_Format(PyExc_RuntimeError,
3178 "_compat_pickle.REVERSE_NAME_MAPPING values "
3179 "should be 2-tuples, not %.200s",
3180 Py_TYPE(item)->tp_name);
3181 return -1;
3182 }
3183 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3184 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3185 if (!PyUnicode_Check(fixed_module_name) ||
3186 !PyUnicode_Check(fixed_global_name)) {
3187 PyErr_Format(PyExc_RuntimeError,
3188 "_compat_pickle.REVERSE_NAME_MAPPING values "
3189 "should be pairs of str, not (%.200s, %.200s)",
3190 Py_TYPE(fixed_module_name)->tp_name,
3191 Py_TYPE(fixed_global_name)->tp_name);
3192 return -1;
3193 }
3194
3195 Py_CLEAR(*module_name);
3196 Py_CLEAR(*global_name);
3197 Py_INCREF(fixed_module_name);
3198 Py_INCREF(fixed_global_name);
3199 *module_name = fixed_module_name;
3200 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003201 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003202 }
3203 else if (PyErr_Occurred()) {
3204 return -1;
3205 }
3206
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003207 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003208 if (item) {
3209 if (!PyUnicode_Check(item)) {
3210 PyErr_Format(PyExc_RuntimeError,
3211 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3212 "should be strings, not %.200s",
3213 Py_TYPE(item)->tp_name);
3214 return -1;
3215 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003216 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003217 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003218 }
3219 else if (PyErr_Occurred()) {
3220 return -1;
3221 }
3222
3223 return 0;
3224}
3225
3226static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003227save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3228{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003229 PyObject *global_name = NULL;
3230 PyObject *module_name = NULL;
3231 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003232 PyObject *parent = NULL;
3233 PyObject *dotted_path = NULL;
3234 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003235 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003236 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003237 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003238 _Py_IDENTIFIER(__name__);
3239 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003240
3241 const char global_op = GLOBAL;
3242
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003243 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003244 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003245 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003246 }
3247 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003248 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3249 if (global_name == NULL) {
3250 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3251 goto error;
3252 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003253 }
3254 if (global_name == NULL) {
3255 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3256 if (global_name == NULL)
3257 goto error;
3258 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003259 }
3260
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003261 dotted_path = get_dotted_path(module, global_name);
3262 if (dotted_path == NULL)
3263 goto error;
3264 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003265 if (module_name == NULL)
3266 goto error;
3267
3268 /* XXX: Change to use the import C API directly with level=0 to disallow
3269 relative imports.
3270
3271 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3272 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3273 custom import functions (IMHO, this would be a nice security
3274 feature). The import C API would need to be extended to support the
3275 extra parameters of __import__ to fix that. */
3276 module = PyImport_Import(module_name);
3277 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003278 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003279 "Can't pickle %R: import of module %R failed",
3280 obj, module_name);
3281 goto error;
3282 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003283 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3284 Py_INCREF(lastname);
3285 cls = get_deep_attribute(module, dotted_path, &parent);
3286 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003287 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003288 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003289 "Can't pickle %R: attribute lookup %S on %S failed",
3290 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003291 goto error;
3292 }
3293 if (cls != obj) {
3294 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003295 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003296 "Can't pickle %R: it's not the same object as %S.%S",
3297 obj, module_name, global_name);
3298 goto error;
3299 }
3300 Py_DECREF(cls);
3301
3302 if (self->proto >= 2) {
3303 /* See whether this is in the extension registry, and if
3304 * so generate an EXT opcode.
3305 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003306 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003307 PyObject *code_obj; /* extension code as Python object */
3308 long code; /* extension code as C value */
3309 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003310 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003311
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003312 extension_key = PyTuple_Pack(2, module_name, global_name);
3313 if (extension_key == NULL) {
3314 goto error;
3315 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003316 code_obj = PyDict_GetItemWithError(st->extension_registry,
3317 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003318 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003319 /* The object is not registered in the extension registry.
3320 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003321 if (code_obj == NULL) {
3322 if (PyErr_Occurred()) {
3323 goto error;
3324 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003325 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003326 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003327
3328 /* XXX: pickle.py doesn't check neither the type, nor the range
3329 of the value returned by the extension_registry. It should for
3330 consistency. */
3331
3332 /* Verify code_obj has the right type and value. */
3333 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003334 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003335 "Can't pickle %R: extension code %R isn't an integer",
3336 obj, code_obj);
3337 goto error;
3338 }
3339 code = PyLong_AS_LONG(code_obj);
3340 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003341 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003342 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3343 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003344 goto error;
3345 }
3346
3347 /* Generate an EXT opcode. */
3348 if (code <= 0xff) {
3349 pdata[0] = EXT1;
3350 pdata[1] = (unsigned char)code;
3351 n = 2;
3352 }
3353 else if (code <= 0xffff) {
3354 pdata[0] = EXT2;
3355 pdata[1] = (unsigned char)(code & 0xff);
3356 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3357 n = 3;
3358 }
3359 else {
3360 pdata[0] = EXT4;
3361 pdata[1] = (unsigned char)(code & 0xff);
3362 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3363 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3364 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3365 n = 5;
3366 }
3367
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003368 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003369 goto error;
3370 }
3371 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003372 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003373 if (parent == module) {
3374 Py_INCREF(lastname);
3375 Py_DECREF(global_name);
3376 global_name = lastname;
3377 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003378 if (self->proto >= 4) {
3379 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003380
Christian Heimese8b1ba12013-11-23 21:13:39 +01003381 if (save(self, module_name, 0) < 0)
3382 goto error;
3383 if (save(self, global_name, 0) < 0)
3384 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003385
3386 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3387 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003388 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003389 else if (parent != module) {
3390 PickleState *st = _Pickle_GetGlobalState();
3391 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3392 st->getattr, parent, lastname);
3393 status = save_reduce(self, reduce_value, NULL);
3394 Py_DECREF(reduce_value);
3395 if (status < 0)
3396 goto error;
3397 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003398 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003399 /* Generate a normal global opcode if we are using a pickle
3400 protocol < 4, or if the object is not registered in the
3401 extension registry. */
3402 PyObject *encoded;
3403 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003404
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003405 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003406 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003407
3408 /* For protocol < 3 and if the user didn't request against doing
3409 so, we convert module names to the old 2.x module names. */
3410 if (self->proto < 3 && self->fix_imports) {
3411 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003412 goto error;
3413 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003414 }
3415
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003416 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3417 both the module name and the global name using UTF-8. We do so
3418 only when we are using the pickle protocol newer than version
3419 3. This is to ensure compatibility with older Unpickler running
3420 on Python 2.x. */
3421 if (self->proto == 3) {
3422 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003423 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003424 else {
3425 unicode_encoder = PyUnicode_AsASCIIString;
3426 }
3427 encoded = unicode_encoder(module_name);
3428 if (encoded == NULL) {
3429 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003430 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003431 "can't pickle module identifier '%S' using "
3432 "pickle protocol %i",
3433 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003434 goto error;
3435 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003436 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3437 PyBytes_GET_SIZE(encoded)) < 0) {
3438 Py_DECREF(encoded);
3439 goto error;
3440 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003441 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003442 if(_Pickler_Write(self, "\n", 1) < 0)
3443 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003444
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003445 /* Save the name of the module. */
3446 encoded = unicode_encoder(global_name);
3447 if (encoded == NULL) {
3448 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003449 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003450 "can't pickle global identifier '%S' using "
3451 "pickle protocol %i",
3452 global_name, self->proto);
3453 goto error;
3454 }
3455 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3456 PyBytes_GET_SIZE(encoded)) < 0) {
3457 Py_DECREF(encoded);
3458 goto error;
3459 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003460 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003461 if (_Pickler_Write(self, "\n", 1) < 0)
3462 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003463 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003464 /* Memoize the object. */
3465 if (memo_put(self, obj) < 0)
3466 goto error;
3467 }
3468
3469 if (0) {
3470 error:
3471 status = -1;
3472 }
3473 Py_XDECREF(module_name);
3474 Py_XDECREF(global_name);
3475 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003476 Py_XDECREF(parent);
3477 Py_XDECREF(dotted_path);
3478 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003479
3480 return status;
3481}
3482
3483static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003484save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3485{
3486 PyObject *reduce_value;
3487 int status;
3488
3489 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3490 if (reduce_value == NULL) {
3491 return -1;
3492 }
3493 status = save_reduce(self, reduce_value, obj);
3494 Py_DECREF(reduce_value);
3495 return status;
3496}
3497
3498static int
3499save_type(PicklerObject *self, PyObject *obj)
3500{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003501 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003502 return save_singleton_type(self, obj, Py_None);
3503 }
3504 else if (obj == (PyObject *)&PyEllipsis_Type) {
3505 return save_singleton_type(self, obj, Py_Ellipsis);
3506 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003507 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003508 return save_singleton_type(self, obj, Py_NotImplemented);
3509 }
3510 return save_global(self, obj, NULL);
3511}
3512
3513static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003514save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003515{
3516 PyObject *pid = NULL;
3517 int status = 0;
3518
3519 const char persid_op = PERSID;
3520 const char binpersid_op = BINPERSID;
3521
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003522 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003523 if (pid == NULL)
3524 return -1;
3525
3526 if (pid != Py_None) {
3527 if (self->bin) {
3528 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003529 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003530 goto error;
3531 }
3532 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003533 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003534
3535 pid_str = PyObject_Str(pid);
3536 if (pid_str == NULL)
3537 goto error;
3538
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003539 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003540 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003541 if (!PyUnicode_IS_ASCII(pid_str)) {
3542 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3543 "persistent IDs in protocol 0 must be "
3544 "ASCII strings");
3545 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003546 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003547 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003548
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003549 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003550 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3551 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3552 _Pickler_Write(self, "\n", 1) < 0) {
3553 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003554 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003555 }
3556 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003557 }
3558 status = 1;
3559 }
3560
3561 if (0) {
3562 error:
3563 status = -1;
3564 }
3565 Py_XDECREF(pid);
3566
3567 return status;
3568}
3569
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003570static PyObject *
3571get_class(PyObject *obj)
3572{
3573 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003574 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003575
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003576 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003577 if (cls == NULL) {
3578 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3579 PyErr_Clear();
3580 cls = (PyObject *) Py_TYPE(obj);
3581 Py_INCREF(cls);
3582 }
3583 }
3584 return cls;
3585}
3586
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003587/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3588 * appropriate __reduce__ method for obj.
3589 */
3590static int
3591save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3592{
3593 PyObject *callable;
3594 PyObject *argtup;
3595 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003596 PyObject *listitems = Py_None;
3597 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003598 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003599 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003600 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003601
3602 const char reduce_op = REDUCE;
3603 const char build_op = BUILD;
3604 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003605 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003606
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003607 size = PyTuple_Size(args);
3608 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003609 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003610 "__reduce__ must contain 2 through 5 elements");
3611 return -1;
3612 }
3613
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003614 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3615 &callable, &argtup, &state, &listitems, &dictitems))
3616 return -1;
3617
3618 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003619 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003620 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003621 return -1;
3622 }
3623 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003624 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003625 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003626 return -1;
3627 }
3628
3629 if (state == Py_None)
3630 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003631
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003632 if (listitems == Py_None)
3633 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003634 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003635 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003636 "returned by __reduce__ must be an iterator, not %s",
3637 Py_TYPE(listitems)->tp_name);
3638 return -1;
3639 }
3640
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003641 if (dictitems == Py_None)
3642 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003643 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003644 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003645 "returned by __reduce__ must be an iterator, not %s",
3646 Py_TYPE(dictitems)->tp_name);
3647 return -1;
3648 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003649
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003650 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003651 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003652 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003653
Victor Stinner804e05e2013-11-14 01:26:17 +01003654 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003655 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003656 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003657 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003658 }
3659 PyErr_Clear();
3660 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003661 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003662 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003663 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3664 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003665 if (!use_newobj_ex) {
3666 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003667 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003668 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003669 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003670 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003671 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003672
3673 if (use_newobj_ex) {
3674 PyObject *cls;
3675 PyObject *args;
3676 PyObject *kwargs;
3677
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003678 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003679 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003680 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003681 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003682 return -1;
3683 }
3684
3685 cls = PyTuple_GET_ITEM(argtup, 0);
3686 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003687 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003688 "first item from NEWOBJ_EX argument tuple must "
3689 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3690 return -1;
3691 }
3692 args = PyTuple_GET_ITEM(argtup, 1);
3693 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003694 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003695 "second item from NEWOBJ_EX argument tuple must "
3696 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3697 return -1;
3698 }
3699 kwargs = PyTuple_GET_ITEM(argtup, 2);
3700 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003701 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003702 "third item from NEWOBJ_EX argument tuple must "
3703 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3704 return -1;
3705 }
3706
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003707 if (self->proto >= 4) {
3708 if (save(self, cls, 0) < 0 ||
3709 save(self, args, 0) < 0 ||
3710 save(self, kwargs, 0) < 0 ||
3711 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3712 return -1;
3713 }
3714 }
3715 else {
3716 PyObject *newargs;
3717 PyObject *cls_new;
3718 Py_ssize_t i;
3719 _Py_IDENTIFIER(__new__);
3720
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003721 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003722 if (newargs == NULL)
3723 return -1;
3724
3725 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3726 if (cls_new == NULL) {
3727 Py_DECREF(newargs);
3728 return -1;
3729 }
3730 PyTuple_SET_ITEM(newargs, 0, cls_new);
3731 Py_INCREF(cls);
3732 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003733 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003734 PyObject *item = PyTuple_GET_ITEM(args, i);
3735 Py_INCREF(item);
3736 PyTuple_SET_ITEM(newargs, i + 2, item);
3737 }
3738
3739 callable = PyObject_Call(st->partial, newargs, kwargs);
3740 Py_DECREF(newargs);
3741 if (callable == NULL)
3742 return -1;
3743
3744 newargs = PyTuple_New(0);
3745 if (newargs == NULL) {
3746 Py_DECREF(callable);
3747 return -1;
3748 }
3749
3750 if (save(self, callable, 0) < 0 ||
3751 save(self, newargs, 0) < 0 ||
3752 _Pickler_Write(self, &reduce_op, 1) < 0) {
3753 Py_DECREF(newargs);
3754 Py_DECREF(callable);
3755 return -1;
3756 }
3757 Py_DECREF(newargs);
3758 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003759 }
3760 }
3761 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003762 PyObject *cls;
3763 PyObject *newargtup;
3764 PyObject *obj_class;
3765 int p;
3766
3767 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003768 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003769 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003770 return -1;
3771 }
3772
3773 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003774 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003775 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003776 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003777 return -1;
3778 }
3779
3780 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003781 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003782 p = obj_class != cls; /* true iff a problem */
3783 Py_DECREF(obj_class);
3784 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003785 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003786 "__newobj__ args has the wrong class");
3787 return -1;
3788 }
3789 }
3790 /* XXX: These calls save() are prone to infinite recursion. Imagine
3791 what happen if the value returned by the __reduce__() method of
3792 some extension type contains another object of the same type. Ouch!
3793
3794 Here is a quick example, that I ran into, to illustrate what I
3795 mean:
3796
3797 >>> import pickle, copyreg
3798 >>> copyreg.dispatch_table.pop(complex)
3799 >>> pickle.dumps(1+2j)
3800 Traceback (most recent call last):
3801 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003802 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003803
3804 Removing the complex class from copyreg.dispatch_table made the
3805 __reduce_ex__() method emit another complex object:
3806
3807 >>> (1+1j).__reduce_ex__(2)
3808 (<function __newobj__ at 0xb7b71c3c>,
3809 (<class 'complex'>, (1+1j)), None, None, None)
3810
3811 Thus when save() was called on newargstup (the 2nd item) recursion
3812 ensued. Of course, the bug was in the complex class which had a
3813 broken __getnewargs__() that emitted another complex object. But,
3814 the point, here, is it is quite easy to end up with a broken reduce
3815 function. */
3816
3817 /* Save the class and its __new__ arguments. */
3818 if (save(self, cls, 0) < 0)
3819 return -1;
3820
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003821 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003822 if (newargtup == NULL)
3823 return -1;
3824
3825 p = save(self, newargtup, 0);
3826 Py_DECREF(newargtup);
3827 if (p < 0)
3828 return -1;
3829
3830 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003831 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003832 return -1;
3833 }
3834 else { /* Not using NEWOBJ. */
3835 if (save(self, callable, 0) < 0 ||
3836 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003837 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003838 return -1;
3839 }
3840
3841 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3842 the caller do not want to memoize the object. Not particularly useful,
3843 but that is to mimic the behavior save_reduce() in pickle.py when
3844 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003845 if (obj != NULL) {
3846 /* If the object is already in the memo, this means it is
3847 recursive. In this case, throw away everything we put on the
3848 stack, and fetch the object back from the memo. */
3849 if (PyMemoTable_Get(self->memo, obj)) {
3850 const char pop_op = POP;
3851
3852 if (_Pickler_Write(self, &pop_op, 1) < 0)
3853 return -1;
3854 if (memo_get(self, obj) < 0)
3855 return -1;
3856
3857 return 0;
3858 }
3859 else if (memo_put(self, obj) < 0)
3860 return -1;
3861 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003862
3863 if (listitems && batch_list(self, listitems) < 0)
3864 return -1;
3865
3866 if (dictitems && batch_dict(self, dictitems) < 0)
3867 return -1;
3868
3869 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003870 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003871 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003872 return -1;
3873 }
3874
3875 return 0;
3876}
3877
3878static int
3879save(PicklerObject *self, PyObject *obj, int pers_save)
3880{
3881 PyTypeObject *type;
3882 PyObject *reduce_func = NULL;
3883 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003884 int status = 0;
3885
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003886 if (_Pickler_OpcodeBoundary(self) < 0)
3887 return -1;
3888
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003889 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003890 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003891
3892 /* The extra pers_save argument is necessary to avoid calling save_pers()
3893 on its returned object. */
3894 if (!pers_save && self->pers_func) {
3895 /* save_pers() returns:
3896 -1 to signal an error;
3897 0 if it did nothing successfully;
3898 1 if a persistent id was saved.
3899 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003900 if ((status = save_pers(self, obj)) != 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003901 goto done;
3902 }
3903
3904 type = Py_TYPE(obj);
3905
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003906 /* The old cPickle had an optimization that used switch-case statement
3907 dispatching on the first letter of the type name. This has was removed
3908 since benchmarks shown that this optimization was actually slowing
3909 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003910
3911 /* Atom types; these aren't memoized, so don't check the memo. */
3912
3913 if (obj == Py_None) {
3914 status = save_none(self, obj);
3915 goto done;
3916 }
3917 else if (obj == Py_False || obj == Py_True) {
3918 status = save_bool(self, obj);
3919 goto done;
3920 }
3921 else if (type == &PyLong_Type) {
3922 status = save_long(self, obj);
3923 goto done;
3924 }
3925 else if (type == &PyFloat_Type) {
3926 status = save_float(self, obj);
3927 goto done;
3928 }
3929
3930 /* Check the memo to see if it has the object. If so, generate
3931 a GET (or BINGET) opcode, instead of pickling the object
3932 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003933 if (PyMemoTable_Get(self->memo, obj)) {
3934 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003935 goto error;
3936 goto done;
3937 }
3938
3939 if (type == &PyBytes_Type) {
3940 status = save_bytes(self, obj);
3941 goto done;
3942 }
3943 else if (type == &PyUnicode_Type) {
3944 status = save_unicode(self, obj);
3945 goto done;
3946 }
3947 else if (type == &PyDict_Type) {
3948 status = save_dict(self, obj);
3949 goto done;
3950 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003951 else if (type == &PySet_Type) {
3952 status = save_set(self, obj);
3953 goto done;
3954 }
3955 else if (type == &PyFrozenSet_Type) {
3956 status = save_frozenset(self, obj);
3957 goto done;
3958 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003959 else if (type == &PyList_Type) {
3960 status = save_list(self, obj);
3961 goto done;
3962 }
3963 else if (type == &PyTuple_Type) {
3964 status = save_tuple(self, obj);
3965 goto done;
3966 }
3967 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003968 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003969 goto done;
3970 }
3971 else if (type == &PyFunction_Type) {
3972 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003973 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003974 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003975
3976 /* XXX: This part needs some unit tests. */
3977
3978 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003979 * self.dispatch_table, copyreg.dispatch_table, the object's
3980 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003981 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003982 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003983 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003984 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3985 (PyObject *)type);
3986 if (reduce_func == NULL) {
3987 if (PyErr_Occurred()) {
3988 goto error;
3989 }
3990 } else {
3991 /* PyDict_GetItemWithError() returns a borrowed reference.
3992 Increase the reference count to be consistent with
3993 PyObject_GetItem and _PyObject_GetAttrId used below. */
3994 Py_INCREF(reduce_func);
3995 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003996 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003997 reduce_func = PyObject_GetItem(self->dispatch_table,
3998 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003999 if (reduce_func == NULL) {
4000 if (PyErr_ExceptionMatches(PyExc_KeyError))
4001 PyErr_Clear();
4002 else
4003 goto error;
4004 }
4005 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004006 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004007 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004008 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004009 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004010 else if (PyType_IsSubtype(type, &PyType_Type)) {
4011 status = save_global(self, obj, NULL);
4012 goto done;
4013 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004014 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004015 _Py_IDENTIFIER(__reduce__);
4016 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004017
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004018
4019 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4020 automatically defined as __reduce__. While this is convenient, this
4021 make it impossible to know which method was actually called. Of
4022 course, this is not a big deal. But still, it would be nice to let
4023 the user know which method was called when something go
4024 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4025 don't actually have to check for a __reduce__ method. */
4026
4027 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004028 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004029 if (reduce_func != NULL) {
4030 PyObject *proto;
4031 proto = PyLong_FromLong(self->proto);
4032 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004033 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004034 }
4035 }
4036 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004037 PickleState *st = _Pickle_GetGlobalState();
4038
4039 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004040 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004041 }
4042 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004043 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004044 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004045 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004046 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004047 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004048 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004049 }
4050 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004051 PyErr_Format(st->PicklingError,
4052 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004053 type->tp_name, obj);
4054 goto error;
4055 }
4056 }
4057 }
4058
4059 if (reduce_value == NULL)
4060 goto error;
4061
4062 if (PyUnicode_Check(reduce_value)) {
4063 status = save_global(self, obj, reduce_value);
4064 goto done;
4065 }
4066
4067 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004068 PickleState *st = _Pickle_GetGlobalState();
4069 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004070 "__reduce__ must return a string or tuple");
4071 goto error;
4072 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004073
4074 status = save_reduce(self, reduce_value, obj);
4075
4076 if (0) {
4077 error:
4078 status = -1;
4079 }
4080 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004081
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004082 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004083 Py_XDECREF(reduce_func);
4084 Py_XDECREF(reduce_value);
4085
4086 return status;
4087}
4088
4089static int
4090dump(PicklerObject *self, PyObject *obj)
4091{
4092 const char stop_op = STOP;
4093
4094 if (self->proto >= 2) {
4095 char header[2];
4096
4097 header[0] = PROTO;
4098 assert(self->proto >= 0 && self->proto < 256);
4099 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004100 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004101 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004102 if (self->proto >= 4)
4103 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004104 }
4105
4106 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004107 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004108 return -1;
4109
4110 return 0;
4111}
4112
Larry Hastings61272b72014-01-07 12:41:53 -08004113/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004114
4115_pickle.Pickler.clear_memo
4116
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004117Clears the pickler's "memo".
4118
4119The memo is the data structure that remembers which objects the
4120pickler has already seen, so that shared or recursive objects are
4121pickled by reference and not by value. This method is useful when
4122re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004123[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004124
Larry Hastings3cceb382014-01-04 11:09:09 -08004125static PyObject *
4126_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004127/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004128{
4129 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004130 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004131
4132 Py_RETURN_NONE;
4133}
4134
Larry Hastings61272b72014-01-07 12:41:53 -08004135/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004136
4137_pickle.Pickler.dump
4138
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004139 obj: object
4140 /
4141
4142Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004143[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004144
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004145static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004146_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004147/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004148{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004149 /* Check whether the Pickler was initialized correctly (issue3664).
4150 Developers often forget to call __init__() in their subclasses, which
4151 would trigger a segfault without this check. */
4152 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004153 PickleState *st = _Pickle_GetGlobalState();
4154 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004155 "Pickler.__init__() was not called by %s.__init__()",
4156 Py_TYPE(self)->tp_name);
4157 return NULL;
4158 }
4159
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004160 if (_Pickler_ClearBuffer(self) < 0)
4161 return NULL;
4162
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004163 if (dump(self, obj) < 0)
4164 return NULL;
4165
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004166 if (_Pickler_FlushToFile(self) < 0)
4167 return NULL;
4168
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004169 Py_RETURN_NONE;
4170}
4171
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004172/*[clinic input]
4173
4174_pickle.Pickler.__sizeof__ -> Py_ssize_t
4175
4176Returns size in memory, in bytes.
4177[clinic start generated code]*/
4178
4179static Py_ssize_t
4180_pickle_Pickler___sizeof___impl(PicklerObject *self)
4181/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4182{
4183 Py_ssize_t res, s;
4184
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004185 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004186 if (self->memo != NULL) {
4187 res += sizeof(PyMemoTable);
4188 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4189 }
4190 if (self->output_buffer != NULL) {
4191 s = _PySys_GetSizeOf(self->output_buffer);
4192 if (s == -1)
4193 return -1;
4194 res += s;
4195 }
4196 return res;
4197}
4198
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004199static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004200 _PICKLE_PICKLER_DUMP_METHODDEF
4201 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004202 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004203 {NULL, NULL} /* sentinel */
4204};
4205
4206static void
4207Pickler_dealloc(PicklerObject *self)
4208{
4209 PyObject_GC_UnTrack(self);
4210
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004211 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004212 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004213 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004214 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004215 Py_XDECREF(self->fast_memo);
4216
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004217 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004218
4219 Py_TYPE(self)->tp_free((PyObject *)self);
4220}
4221
4222static int
4223Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4224{
4225 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004226 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004227 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004228 Py_VISIT(self->fast_memo);
4229 return 0;
4230}
4231
4232static int
4233Pickler_clear(PicklerObject *self)
4234{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004235 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004236 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004237 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004238 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004239 Py_CLEAR(self->fast_memo);
4240
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004241 if (self->memo != NULL) {
4242 PyMemoTable *memo = self->memo;
4243 self->memo = NULL;
4244 PyMemoTable_Del(memo);
4245 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004246 return 0;
4247}
4248
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004249
Larry Hastings61272b72014-01-07 12:41:53 -08004250/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004251
4252_pickle.Pickler.__init__
4253
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004254 file: object
4255 protocol: object = NULL
4256 fix_imports: bool = True
4257
4258This takes a binary file for writing a pickle data stream.
4259
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004260The optional *protocol* argument tells the pickler to use the given
4261protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4262protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004263
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004264Specifying a negative protocol version selects the highest protocol
4265version supported. The higher the protocol used, the more recent the
4266version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004267
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004268The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004269bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004270writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004271this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004272
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004273If *fix_imports* is True and protocol is less than 3, pickle will try
4274to map the new Python 3 names to the old module names used in Python
42752, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004276[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004277
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004278static int
Larry Hastings89964c42015-04-14 18:07:59 -04004279_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4280 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004281/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004282{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004283 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004284 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004285
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004286 /* In case of multiple __init__() calls, clear previous content. */
4287 if (self->write != NULL)
4288 (void)Pickler_clear(self);
4289
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004290 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004291 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004292
4293 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004294 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004295
4296 /* memo and output_buffer may have already been created in _Pickler_New */
4297 if (self->memo == NULL) {
4298 self->memo = PyMemoTable_New();
4299 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004300 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004301 }
4302 self->output_len = 0;
4303 if (self->output_buffer == NULL) {
4304 self->max_output_len = WRITE_BUF_SIZE;
4305 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4306 self->max_output_len);
4307 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004308 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004309 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004310
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004311 self->fast = 0;
4312 self->fast_nesting = 0;
4313 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004314
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004315 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4316 &self->pers_func, &self->pers_func_self) < 0)
4317 {
4318 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004319 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004320
4321 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4322 &PyId_dispatch_table);
4323 if (self->dispatch_table == NULL) {
4324 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004325 return -1;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004326 }
4327 PyErr_Clear();
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004328 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004329
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004330 return 0;
4331}
4332
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004333
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004334/* Define a proxy object for the Pickler's internal memo object. This is to
4335 * avoid breaking code like:
4336 * pickler.memo.clear()
4337 * and
4338 * pickler.memo = saved_memo
4339 * Is this a good idea? Not really, but we don't want to break code that uses
4340 * it. Note that we don't implement the entire mapping API here. This is
4341 * intentional, as these should be treated as black-box implementation details.
4342 */
4343
Larry Hastings61272b72014-01-07 12:41:53 -08004344/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004345_pickle.PicklerMemoProxy.clear
4346
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004347Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004348[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004349
Larry Hastings3cceb382014-01-04 11:09:09 -08004350static PyObject *
4351_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004352/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004353{
4354 if (self->pickler->memo)
4355 PyMemoTable_Clear(self->pickler->memo);
4356 Py_RETURN_NONE;
4357}
4358
Larry Hastings61272b72014-01-07 12:41:53 -08004359/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004360_pickle.PicklerMemoProxy.copy
4361
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004362Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004363[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004364
Larry Hastings3cceb382014-01-04 11:09:09 -08004365static PyObject *
4366_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004367/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004368{
4369 Py_ssize_t i;
4370 PyMemoTable *memo;
4371 PyObject *new_memo = PyDict_New();
4372 if (new_memo == NULL)
4373 return NULL;
4374
4375 memo = self->pickler->memo;
4376 for (i = 0; i < memo->mt_allocated; ++i) {
4377 PyMemoEntry entry = memo->mt_table[i];
4378 if (entry.me_key != NULL) {
4379 int status;
4380 PyObject *key, *value;
4381
4382 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004383 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004384
4385 if (key == NULL || value == NULL) {
4386 Py_XDECREF(key);
4387 Py_XDECREF(value);
4388 goto error;
4389 }
4390 status = PyDict_SetItem(new_memo, key, value);
4391 Py_DECREF(key);
4392 Py_DECREF(value);
4393 if (status < 0)
4394 goto error;
4395 }
4396 }
4397 return new_memo;
4398
4399 error:
4400 Py_XDECREF(new_memo);
4401 return NULL;
4402}
4403
Larry Hastings61272b72014-01-07 12:41:53 -08004404/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004405_pickle.PicklerMemoProxy.__reduce__
4406
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004407Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004408[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004409
Larry Hastings3cceb382014-01-04 11:09:09 -08004410static PyObject *
4411_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004412/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004413{
4414 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004415 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004416 if (contents == NULL)
4417 return NULL;
4418
4419 reduce_value = PyTuple_New(2);
4420 if (reduce_value == NULL) {
4421 Py_DECREF(contents);
4422 return NULL;
4423 }
4424 dict_args = PyTuple_New(1);
4425 if (dict_args == NULL) {
4426 Py_DECREF(contents);
4427 Py_DECREF(reduce_value);
4428 return NULL;
4429 }
4430 PyTuple_SET_ITEM(dict_args, 0, contents);
4431 Py_INCREF((PyObject *)&PyDict_Type);
4432 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4433 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4434 return reduce_value;
4435}
4436
4437static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004438 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4439 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4440 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004441 {NULL, NULL} /* sentinel */
4442};
4443
4444static void
4445PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4446{
4447 PyObject_GC_UnTrack(self);
4448 Py_XDECREF(self->pickler);
4449 PyObject_GC_Del((PyObject *)self);
4450}
4451
4452static int
4453PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4454 visitproc visit, void *arg)
4455{
4456 Py_VISIT(self->pickler);
4457 return 0;
4458}
4459
4460static int
4461PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4462{
4463 Py_CLEAR(self->pickler);
4464 return 0;
4465}
4466
4467static PyTypeObject PicklerMemoProxyType = {
4468 PyVarObject_HEAD_INIT(NULL, 0)
4469 "_pickle.PicklerMemoProxy", /*tp_name*/
4470 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4471 0,
4472 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4473 0, /* tp_print */
4474 0, /* tp_getattr */
4475 0, /* tp_setattr */
4476 0, /* tp_compare */
4477 0, /* tp_repr */
4478 0, /* tp_as_number */
4479 0, /* tp_as_sequence */
4480 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004481 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004482 0, /* tp_call */
4483 0, /* tp_str */
4484 PyObject_GenericGetAttr, /* tp_getattro */
4485 PyObject_GenericSetAttr, /* tp_setattro */
4486 0, /* tp_as_buffer */
4487 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4488 0, /* tp_doc */
4489 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4490 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4491 0, /* tp_richcompare */
4492 0, /* tp_weaklistoffset */
4493 0, /* tp_iter */
4494 0, /* tp_iternext */
4495 picklerproxy_methods, /* tp_methods */
4496};
4497
4498static PyObject *
4499PicklerMemoProxy_New(PicklerObject *pickler)
4500{
4501 PicklerMemoProxyObject *self;
4502
4503 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4504 if (self == NULL)
4505 return NULL;
4506 Py_INCREF(pickler);
4507 self->pickler = pickler;
4508 PyObject_GC_Track(self);
4509 return (PyObject *)self;
4510}
4511
4512/*****************************************************************************/
4513
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004514static PyObject *
4515Pickler_get_memo(PicklerObject *self)
4516{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004517 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004518}
4519
4520static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004521Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004522{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004523 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004524
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004525 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004526 PyErr_SetString(PyExc_TypeError,
4527 "attribute deletion is not supported");
4528 return -1;
4529 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004530
4531 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4532 PicklerObject *pickler =
4533 ((PicklerMemoProxyObject *)obj)->pickler;
4534
4535 new_memo = PyMemoTable_Copy(pickler->memo);
4536 if (new_memo == NULL)
4537 return -1;
4538 }
4539 else if (PyDict_Check(obj)) {
4540 Py_ssize_t i = 0;
4541 PyObject *key, *value;
4542
4543 new_memo = PyMemoTable_New();
4544 if (new_memo == NULL)
4545 return -1;
4546
4547 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004548 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004549 PyObject *memo_obj;
4550
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004551 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004552 PyErr_SetString(PyExc_TypeError,
4553 "'memo' values must be 2-item tuples");
4554 goto error;
4555 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004556 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004557 if (memo_id == -1 && PyErr_Occurred())
4558 goto error;
4559 memo_obj = PyTuple_GET_ITEM(value, 1);
4560 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4561 goto error;
4562 }
4563 }
4564 else {
4565 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004566 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004567 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004568 return -1;
4569 }
4570
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004571 PyMemoTable_Del(self->memo);
4572 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004573
4574 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004575
4576 error:
4577 if (new_memo)
4578 PyMemoTable_Del(new_memo);
4579 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004580}
4581
4582static PyObject *
4583Pickler_get_persid(PicklerObject *self)
4584{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004585 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004587 return NULL;
4588 }
4589 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004590}
4591
4592static int
4593Pickler_set_persid(PicklerObject *self, PyObject *value)
4594{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004595 if (value == NULL) {
4596 PyErr_SetString(PyExc_TypeError,
4597 "attribute deletion is not supported");
4598 return -1;
4599 }
4600 if (!PyCallable_Check(value)) {
4601 PyErr_SetString(PyExc_TypeError,
4602 "persistent_id must be a callable taking one argument");
4603 return -1;
4604 }
4605
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004606 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004607 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004608 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004609
4610 return 0;
4611}
4612
4613static PyMemberDef Pickler_members[] = {
4614 {"bin", T_INT, offsetof(PicklerObject, bin)},
4615 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004616 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004617 {NULL}
4618};
4619
4620static PyGetSetDef Pickler_getsets[] = {
4621 {"memo", (getter)Pickler_get_memo,
4622 (setter)Pickler_set_memo},
4623 {"persistent_id", (getter)Pickler_get_persid,
4624 (setter)Pickler_set_persid},
4625 {NULL}
4626};
4627
4628static PyTypeObject Pickler_Type = {
4629 PyVarObject_HEAD_INIT(NULL, 0)
4630 "_pickle.Pickler" , /*tp_name*/
4631 sizeof(PicklerObject), /*tp_basicsize*/
4632 0, /*tp_itemsize*/
4633 (destructor)Pickler_dealloc, /*tp_dealloc*/
4634 0, /*tp_print*/
4635 0, /*tp_getattr*/
4636 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004637 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004638 0, /*tp_repr*/
4639 0, /*tp_as_number*/
4640 0, /*tp_as_sequence*/
4641 0, /*tp_as_mapping*/
4642 0, /*tp_hash*/
4643 0, /*tp_call*/
4644 0, /*tp_str*/
4645 0, /*tp_getattro*/
4646 0, /*tp_setattro*/
4647 0, /*tp_as_buffer*/
4648 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004649 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004650 (traverseproc)Pickler_traverse, /*tp_traverse*/
4651 (inquiry)Pickler_clear, /*tp_clear*/
4652 0, /*tp_richcompare*/
4653 0, /*tp_weaklistoffset*/
4654 0, /*tp_iter*/
4655 0, /*tp_iternext*/
4656 Pickler_methods, /*tp_methods*/
4657 Pickler_members, /*tp_members*/
4658 Pickler_getsets, /*tp_getset*/
4659 0, /*tp_base*/
4660 0, /*tp_dict*/
4661 0, /*tp_descr_get*/
4662 0, /*tp_descr_set*/
4663 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004664 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004665 PyType_GenericAlloc, /*tp_alloc*/
4666 PyType_GenericNew, /*tp_new*/
4667 PyObject_GC_Del, /*tp_free*/
4668 0, /*tp_is_gc*/
4669};
4670
Victor Stinner121aab42011-09-29 23:40:53 +02004671/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004672
4673 XXX: It would be nice to able to avoid Python function call overhead, by
4674 using directly the C version of find_class(), when find_class() is not
4675 overridden by a subclass. Although, this could become rather hackish. A
4676 simpler optimization would be to call the C function when self is not a
4677 subclass instance. */
4678static PyObject *
4679find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4680{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004681 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004682
Victor Stinner55ba38a2016-12-09 16:09:30 +01004683 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4684 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004685}
4686
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004687static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004688marker(UnpicklerObject *self)
4689{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004690 Py_ssize_t mark;
4691
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004692 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004693 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004694 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004695 return -1;
4696 }
4697
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004698 mark = self->marks[--self->num_marks];
4699 self->stack->mark_set = self->num_marks != 0;
4700 self->stack->fence = self->num_marks ?
4701 self->marks[self->num_marks - 1] : 0;
4702 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004703}
4704
4705static int
4706load_none(UnpicklerObject *self)
4707{
4708 PDATA_APPEND(self->stack, Py_None, -1);
4709 return 0;
4710}
4711
4712static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004713load_int(UnpicklerObject *self)
4714{
4715 PyObject *value;
4716 char *endptr, *s;
4717 Py_ssize_t len;
4718 long x;
4719
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004720 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004721 return -1;
4722 if (len < 2)
4723 return bad_readline();
4724
4725 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004726 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004727 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004728 x = strtol(s, &endptr, 0);
4729
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004730 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004731 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004732 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733 errno = 0;
4734 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004735 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004736 if (value == NULL) {
4737 PyErr_SetString(PyExc_ValueError,
4738 "could not convert string to int");
4739 return -1;
4740 }
4741 }
4742 else {
4743 if (len == 3 && (x == 0 || x == 1)) {
4744 if ((value = PyBool_FromLong(x)) == NULL)
4745 return -1;
4746 }
4747 else {
4748 if ((value = PyLong_FromLong(x)) == NULL)
4749 return -1;
4750 }
4751 }
4752
4753 PDATA_PUSH(self->stack, value, -1);
4754 return 0;
4755}
4756
4757static int
4758load_bool(UnpicklerObject *self, PyObject *boolean)
4759{
4760 assert(boolean == Py_True || boolean == Py_False);
4761 PDATA_APPEND(self->stack, boolean, -1);
4762 return 0;
4763}
4764
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004765/* s contains x bytes of an unsigned little-endian integer. Return its value
4766 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4767 */
4768static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004769calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004770{
4771 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004772 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004773 size_t x = 0;
4774
Serhiy Storchakae0606192015-09-29 22:10:07 +03004775 if (nbytes > (int)sizeof(size_t)) {
4776 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4777 * have 64-bit size that can't be represented on 32-bit platform.
4778 */
4779 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4780 if (s[i])
4781 return -1;
4782 }
4783 nbytes = (int)sizeof(size_t);
4784 }
4785 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004786 x |= (size_t) s[i] << (8 * i);
4787 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004788
4789 if (x > PY_SSIZE_T_MAX)
4790 return -1;
4791 else
4792 return (Py_ssize_t) x;
4793}
4794
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004795/* s contains x bytes of a little-endian integer. Return its value as a
4796 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004797 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004798 * of x-platform bugs.
4799 */
4800static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004801calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004802{
4803 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004804 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004805 long x = 0;
4806
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004807 for (i = 0; i < nbytes; i++) {
4808 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004809 }
4810
4811 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4812 * is signed, so on a box with longs bigger than 4 bytes we need
4813 * to extend a BININT's sign bit to the full width.
4814 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004815 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004816 x |= -(x & (1L << 31));
4817 }
4818
4819 return x;
4820}
4821
4822static int
4823load_binintx(UnpicklerObject *self, char *s, int size)
4824{
4825 PyObject *value;
4826 long x;
4827
4828 x = calc_binint(s, size);
4829
4830 if ((value = PyLong_FromLong(x)) == NULL)
4831 return -1;
4832
4833 PDATA_PUSH(self->stack, value, -1);
4834 return 0;
4835}
4836
4837static int
4838load_binint(UnpicklerObject *self)
4839{
4840 char *s;
4841
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004842 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004843 return -1;
4844
4845 return load_binintx(self, s, 4);
4846}
4847
4848static int
4849load_binint1(UnpicklerObject *self)
4850{
4851 char *s;
4852
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004853 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004854 return -1;
4855
4856 return load_binintx(self, s, 1);
4857}
4858
4859static int
4860load_binint2(UnpicklerObject *self)
4861{
4862 char *s;
4863
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004864 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004865 return -1;
4866
4867 return load_binintx(self, s, 2);
4868}
4869
4870static int
4871load_long(UnpicklerObject *self)
4872{
4873 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004874 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004875 Py_ssize_t len;
4876
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004877 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004878 return -1;
4879 if (len < 2)
4880 return bad_readline();
4881
Mark Dickinson8dd05142009-01-20 20:43:58 +00004882 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4883 the 'L' before calling PyLong_FromString. In order to maintain
4884 compatibility with Python 3.0.0, we don't actually *require*
4885 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004886 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004887 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004888 /* XXX: Should the base argument explicitly set to 10? */
4889 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004890 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004891 return -1;
4892
4893 PDATA_PUSH(self->stack, value, -1);
4894 return 0;
4895}
4896
4897/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4898 * data following.
4899 */
4900static int
4901load_counted_long(UnpicklerObject *self, int size)
4902{
4903 PyObject *value;
4904 char *nbytes;
4905 char *pdata;
4906
4907 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004908 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004909 return -1;
4910
4911 size = calc_binint(nbytes, size);
4912 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004913 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004914 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004915 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004916 "LONG pickle has negative byte count");
4917 return -1;
4918 }
4919
4920 if (size == 0)
4921 value = PyLong_FromLong(0L);
4922 else {
4923 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004924 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004925 return -1;
4926 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4927 1 /* little endian */ , 1 /* signed */ );
4928 }
4929 if (value == NULL)
4930 return -1;
4931 PDATA_PUSH(self->stack, value, -1);
4932 return 0;
4933}
4934
4935static int
4936load_float(UnpicklerObject *self)
4937{
4938 PyObject *value;
4939 char *endptr, *s;
4940 Py_ssize_t len;
4941 double d;
4942
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004943 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004944 return -1;
4945 if (len < 2)
4946 return bad_readline();
4947
4948 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004949 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4950 if (d == -1.0 && PyErr_Occurred())
4951 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004952 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004953 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4954 return -1;
4955 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004956 value = PyFloat_FromDouble(d);
4957 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004958 return -1;
4959
4960 PDATA_PUSH(self->stack, value, -1);
4961 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004962}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004963
4964static int
4965load_binfloat(UnpicklerObject *self)
4966{
4967 PyObject *value;
4968 double x;
4969 char *s;
4970
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004971 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004972 return -1;
4973
4974 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4975 if (x == -1.0 && PyErr_Occurred())
4976 return -1;
4977
4978 if ((value = PyFloat_FromDouble(x)) == NULL)
4979 return -1;
4980
4981 PDATA_PUSH(self->stack, value, -1);
4982 return 0;
4983}
4984
4985static int
4986load_string(UnpicklerObject *self)
4987{
4988 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004989 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004990 Py_ssize_t len;
4991 char *s, *p;
4992
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004993 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004994 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004995 /* Strip the newline */
4996 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004997 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004998 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004999 p = s + 1;
5000 len -= 2;
5001 }
5002 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005003 PickleState *st = _Pickle_GetGlobalState();
5004 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005005 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005006 return -1;
5007 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005008 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005009
5010 /* Use the PyBytes API to decode the string, since that is what is used
5011 to encode, and then coerce the result to Unicode. */
5012 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005013 if (bytes == NULL)
5014 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005015
5016 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5017 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5018 if (strcmp(self->encoding, "bytes") == 0) {
5019 obj = bytes;
5020 }
5021 else {
5022 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5023 Py_DECREF(bytes);
5024 if (obj == NULL) {
5025 return -1;
5026 }
5027 }
5028
5029 PDATA_PUSH(self->stack, obj, -1);
5030 return 0;
5031}
5032
5033static int
5034load_counted_binstring(UnpicklerObject *self, int nbytes)
5035{
5036 PyObject *obj;
5037 Py_ssize_t size;
5038 char *s;
5039
5040 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005041 return -1;
5042
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005043 size = calc_binsize(s, nbytes);
5044 if (size < 0) {
5045 PickleState *st = _Pickle_GetGlobalState();
5046 PyErr_Format(st->UnpicklingError,
5047 "BINSTRING exceeds system's maximum size of %zd bytes",
5048 PY_SSIZE_T_MAX);
5049 return -1;
5050 }
5051
5052 if (_Unpickler_Read(self, &s, size) < 0)
5053 return -1;
5054
5055 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5056 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5057 if (strcmp(self->encoding, "bytes") == 0) {
5058 obj = PyBytes_FromStringAndSize(s, size);
5059 }
5060 else {
5061 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5062 }
5063 if (obj == NULL) {
5064 return -1;
5065 }
5066
5067 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005068 return 0;
5069}
5070
5071static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005072load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005073{
5074 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005075 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005076 char *s;
5077
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005078 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005079 return -1;
5080
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005081 size = calc_binsize(s, nbytes);
5082 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005083 PyErr_Format(PyExc_OverflowError,
5084 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005085 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005086 return -1;
5087 }
5088
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005089 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005090 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005091
5092 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005093 if (bytes == NULL)
5094 return -1;
5095
5096 PDATA_PUSH(self->stack, bytes, -1);
5097 return 0;
5098}
5099
5100static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005101load_unicode(UnpicklerObject *self)
5102{
5103 PyObject *str;
5104 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005105 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005106
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005107 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005108 return -1;
5109 if (len < 1)
5110 return bad_readline();
5111
5112 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5113 if (str == NULL)
5114 return -1;
5115
5116 PDATA_PUSH(self->stack, str, -1);
5117 return 0;
5118}
5119
5120static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005121load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005122{
5123 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005124 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005125 char *s;
5126
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005127 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005128 return -1;
5129
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005130 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005131 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005132 PyErr_Format(PyExc_OverflowError,
5133 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005134 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005135 return -1;
5136 }
5137
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005138 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005139 return -1;
5140
Victor Stinner485fb562010-04-13 11:07:24 +00005141 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005142 if (str == NULL)
5143 return -1;
5144
5145 PDATA_PUSH(self->stack, str, -1);
5146 return 0;
5147}
5148
5149static int
Victor Stinner21b47112016-03-14 18:09:39 +01005150load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005151{
5152 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005153
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005154 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005155 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005156
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005157 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005158 if (tuple == NULL)
5159 return -1;
5160 PDATA_PUSH(self->stack, tuple, -1);
5161 return 0;
5162}
5163
5164static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005165load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005166{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005167 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005168
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005169 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005170 return -1;
5171
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005172 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005173}
5174
5175static int
5176load_empty_list(UnpicklerObject *self)
5177{
5178 PyObject *list;
5179
5180 if ((list = PyList_New(0)) == NULL)
5181 return -1;
5182 PDATA_PUSH(self->stack, list, -1);
5183 return 0;
5184}
5185
5186static int
5187load_empty_dict(UnpicklerObject *self)
5188{
5189 PyObject *dict;
5190
5191 if ((dict = PyDict_New()) == NULL)
5192 return -1;
5193 PDATA_PUSH(self->stack, dict, -1);
5194 return 0;
5195}
5196
5197static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005198load_empty_set(UnpicklerObject *self)
5199{
5200 PyObject *set;
5201
5202 if ((set = PySet_New(NULL)) == NULL)
5203 return -1;
5204 PDATA_PUSH(self->stack, set, -1);
5205 return 0;
5206}
5207
5208static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005209load_list(UnpicklerObject *self)
5210{
5211 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005212 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005213
5214 if ((i = marker(self)) < 0)
5215 return -1;
5216
5217 list = Pdata_poplist(self->stack, i);
5218 if (list == NULL)
5219 return -1;
5220 PDATA_PUSH(self->stack, list, -1);
5221 return 0;
5222}
5223
5224static int
5225load_dict(UnpicklerObject *self)
5226{
5227 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005228 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005229
5230 if ((i = marker(self)) < 0)
5231 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005232 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005233
5234 if ((dict = PyDict_New()) == NULL)
5235 return -1;
5236
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005237 if ((j - i) % 2 != 0) {
5238 PickleState *st = _Pickle_GetGlobalState();
5239 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005240 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005241 return -1;
5242 }
5243
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005244 for (k = i + 1; k < j; k += 2) {
5245 key = self->stack->data[k - 1];
5246 value = self->stack->data[k];
5247 if (PyDict_SetItem(dict, key, value) < 0) {
5248 Py_DECREF(dict);
5249 return -1;
5250 }
5251 }
5252 Pdata_clear(self->stack, i);
5253 PDATA_PUSH(self->stack, dict, -1);
5254 return 0;
5255}
5256
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005257static int
5258load_frozenset(UnpicklerObject *self)
5259{
5260 PyObject *items;
5261 PyObject *frozenset;
5262 Py_ssize_t i;
5263
5264 if ((i = marker(self)) < 0)
5265 return -1;
5266
5267 items = Pdata_poptuple(self->stack, i);
5268 if (items == NULL)
5269 return -1;
5270
5271 frozenset = PyFrozenSet_New(items);
5272 Py_DECREF(items);
5273 if (frozenset == NULL)
5274 return -1;
5275
5276 PDATA_PUSH(self->stack, frozenset, -1);
5277 return 0;
5278}
5279
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005280static PyObject *
5281instantiate(PyObject *cls, PyObject *args)
5282{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005283 /* Caller must assure args are a tuple. Normally, args come from
5284 Pdata_poptuple which packs objects from the top of the stack
5285 into a newly created tuple. */
5286 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005287 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5288 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005289 _Py_IDENTIFIER(__new__);
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005290 PyObject *func = _PyObject_GetAttrId(cls, &PyId___getinitargs__);
5291 if (func == NULL) {
5292 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
5293 return NULL;
5294 }
5295 PyErr_Clear();
5296 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5297 }
5298 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005299 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005300 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005301}
5302
5303static int
5304load_obj(UnpicklerObject *self)
5305{
5306 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005307 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005308
5309 if ((i = marker(self)) < 0)
5310 return -1;
5311
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005312 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005313 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005314
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005315 args = Pdata_poptuple(self->stack, i + 1);
5316 if (args == NULL)
5317 return -1;
5318
5319 PDATA_POP(self->stack, cls);
5320 if (cls) {
5321 obj = instantiate(cls, args);
5322 Py_DECREF(cls);
5323 }
5324 Py_DECREF(args);
5325 if (obj == NULL)
5326 return -1;
5327
5328 PDATA_PUSH(self->stack, obj, -1);
5329 return 0;
5330}
5331
5332static int
5333load_inst(UnpicklerObject *self)
5334{
5335 PyObject *cls = NULL;
5336 PyObject *args = NULL;
5337 PyObject *obj = NULL;
5338 PyObject *module_name;
5339 PyObject *class_name;
5340 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005341 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005342 char *s;
5343
5344 if ((i = marker(self)) < 0)
5345 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005346 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005347 return -1;
5348 if (len < 2)
5349 return bad_readline();
5350
5351 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5352 identifiers are permitted in Python 3.0, since the INST opcode is only
5353 supported by older protocols on Python 2.x. */
5354 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5355 if (module_name == NULL)
5356 return -1;
5357
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005358 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005359 if (len < 2) {
5360 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005361 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005362 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005363 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005364 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005365 cls = find_class(self, module_name, class_name);
5366 Py_DECREF(class_name);
5367 }
5368 }
5369 Py_DECREF(module_name);
5370
5371 if (cls == NULL)
5372 return -1;
5373
5374 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5375 obj = instantiate(cls, args);
5376 Py_DECREF(args);
5377 }
5378 Py_DECREF(cls);
5379
5380 if (obj == NULL)
5381 return -1;
5382
5383 PDATA_PUSH(self->stack, obj, -1);
5384 return 0;
5385}
5386
5387static int
5388load_newobj(UnpicklerObject *self)
5389{
5390 PyObject *args = NULL;
5391 PyObject *clsraw = NULL;
5392 PyTypeObject *cls; /* clsraw cast to its true type */
5393 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005394 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005395
5396 /* Stack is ... cls argtuple, and we want to call
5397 * cls.__new__(cls, *argtuple).
5398 */
5399 PDATA_POP(self->stack, args);
5400 if (args == NULL)
5401 goto error;
5402 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005403 PyErr_SetString(st->UnpicklingError,
5404 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005405 goto error;
5406 }
5407
5408 PDATA_POP(self->stack, clsraw);
5409 cls = (PyTypeObject *)clsraw;
5410 if (cls == NULL)
5411 goto error;
5412 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005413 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005414 "isn't a type object");
5415 goto error;
5416 }
5417 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005418 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005419 "has NULL tp_new");
5420 goto error;
5421 }
5422
5423 /* Call __new__. */
5424 obj = cls->tp_new(cls, args, NULL);
5425 if (obj == NULL)
5426 goto error;
5427
5428 Py_DECREF(args);
5429 Py_DECREF(clsraw);
5430 PDATA_PUSH(self->stack, obj, -1);
5431 return 0;
5432
5433 error:
5434 Py_XDECREF(args);
5435 Py_XDECREF(clsraw);
5436 return -1;
5437}
5438
5439static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005440load_newobj_ex(UnpicklerObject *self)
5441{
5442 PyObject *cls, *args, *kwargs;
5443 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005444 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005445
5446 PDATA_POP(self->stack, kwargs);
5447 if (kwargs == NULL) {
5448 return -1;
5449 }
5450 PDATA_POP(self->stack, args);
5451 if (args == NULL) {
5452 Py_DECREF(kwargs);
5453 return -1;
5454 }
5455 PDATA_POP(self->stack, cls);
5456 if (cls == NULL) {
5457 Py_DECREF(kwargs);
5458 Py_DECREF(args);
5459 return -1;
5460 }
Larry Hastings61272b72014-01-07 12:41:53 -08005461
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005462 if (!PyType_Check(cls)) {
5463 Py_DECREF(kwargs);
5464 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005465 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005466 "NEWOBJ_EX class argument must be a type, not %.200s",
5467 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005468 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005469 return -1;
5470 }
5471
5472 if (((PyTypeObject *)cls)->tp_new == NULL) {
5473 Py_DECREF(kwargs);
5474 Py_DECREF(args);
5475 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005476 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005477 "NEWOBJ_EX class argument doesn't have __new__");
5478 return -1;
5479 }
5480 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5481 Py_DECREF(kwargs);
5482 Py_DECREF(args);
5483 Py_DECREF(cls);
5484 if (obj == NULL) {
5485 return -1;
5486 }
5487 PDATA_PUSH(self->stack, obj, -1);
5488 return 0;
5489}
5490
5491static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005492load_global(UnpicklerObject *self)
5493{
5494 PyObject *global = NULL;
5495 PyObject *module_name;
5496 PyObject *global_name;
5497 Py_ssize_t len;
5498 char *s;
5499
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005500 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005501 return -1;
5502 if (len < 2)
5503 return bad_readline();
5504 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5505 if (!module_name)
5506 return -1;
5507
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005508 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005509 if (len < 2) {
5510 Py_DECREF(module_name);
5511 return bad_readline();
5512 }
5513 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5514 if (global_name) {
5515 global = find_class(self, module_name, global_name);
5516 Py_DECREF(global_name);
5517 }
5518 }
5519 Py_DECREF(module_name);
5520
5521 if (global == NULL)
5522 return -1;
5523 PDATA_PUSH(self->stack, global, -1);
5524 return 0;
5525}
5526
5527static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005528load_stack_global(UnpicklerObject *self)
5529{
5530 PyObject *global;
5531 PyObject *module_name;
5532 PyObject *global_name;
5533
5534 PDATA_POP(self->stack, global_name);
5535 PDATA_POP(self->stack, module_name);
5536 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5537 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005538 PickleState *st = _Pickle_GetGlobalState();
5539 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005540 Py_XDECREF(global_name);
5541 Py_XDECREF(module_name);
5542 return -1;
5543 }
5544 global = find_class(self, module_name, global_name);
5545 Py_DECREF(global_name);
5546 Py_DECREF(module_name);
5547 if (global == NULL)
5548 return -1;
5549 PDATA_PUSH(self->stack, global, -1);
5550 return 0;
5551}
5552
5553static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005554load_persid(UnpicklerObject *self)
5555{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005556 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005557 Py_ssize_t len;
5558 char *s;
5559
5560 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005561 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005562 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005563 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005564 return bad_readline();
5565
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005566 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5567 if (pid == NULL) {
5568 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5569 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5570 "persistent IDs in protocol 0 must be "
5571 "ASCII strings");
5572 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005573 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005574 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005575
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005576 obj = call_method(self->pers_func, self->pers_func_self, pid);
5577 Py_DECREF(pid);
5578 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005579 return -1;
5580
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005581 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005582 return 0;
5583 }
5584 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005585 PickleState *st = _Pickle_GetGlobalState();
5586 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005587 "A load persistent id instruction was encountered,\n"
5588 "but no persistent_load function was specified.");
5589 return -1;
5590 }
5591}
5592
5593static int
5594load_binpersid(UnpicklerObject *self)
5595{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005596 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005597
5598 if (self->pers_func) {
5599 PDATA_POP(self->stack, pid);
5600 if (pid == NULL)
5601 return -1;
5602
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005603 obj = call_method(self->pers_func, self->pers_func_self, pid);
5604 Py_DECREF(pid);
5605 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 return -1;
5607
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005608 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609 return 0;
5610 }
5611 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005612 PickleState *st = _Pickle_GetGlobalState();
5613 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005614 "A load persistent id instruction was encountered,\n"
5615 "but no persistent_load function was specified.");
5616 return -1;
5617 }
5618}
5619
5620static int
5621load_pop(UnpicklerObject *self)
5622{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005623 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005624
5625 /* Note that we split the (pickle.py) stack into two stacks,
5626 * an object stack and a mark stack. We have to be clever and
5627 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005628 * mark stack first, and only signalling a stack underflow if
5629 * the object stack is empty and the mark stack doesn't match
5630 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005631 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005632 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005634 self->stack->mark_set = self->num_marks != 0;
5635 self->stack->fence = self->num_marks ?
5636 self->marks[self->num_marks - 1] : 0;
5637 } else if (len <= self->stack->fence)
5638 return Pdata_stack_underflow(self->stack);
5639 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005640 len--;
5641 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005642 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005643 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644 return 0;
5645}
5646
5647static int
5648load_pop_mark(UnpicklerObject *self)
5649{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005650 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005651
5652 if ((i = marker(self)) < 0)
5653 return -1;
5654
5655 Pdata_clear(self->stack, i);
5656
5657 return 0;
5658}
5659
5660static int
5661load_dup(UnpicklerObject *self)
5662{
5663 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005664 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005665
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005666 if (len <= self->stack->fence)
5667 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005668 last = self->stack->data[len - 1];
5669 PDATA_APPEND(self->stack, last, -1);
5670 return 0;
5671}
5672
5673static int
5674load_get(UnpicklerObject *self)
5675{
5676 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005677 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005678 Py_ssize_t len;
5679 char *s;
5680
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005681 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682 return -1;
5683 if (len < 2)
5684 return bad_readline();
5685
5686 key = PyLong_FromString(s, NULL, 10);
5687 if (key == NULL)
5688 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005689 idx = PyLong_AsSsize_t(key);
5690 if (idx == -1 && PyErr_Occurred()) {
5691 Py_DECREF(key);
5692 return -1;
5693 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005694
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005695 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696 if (value == NULL) {
5697 if (!PyErr_Occurred())
5698 PyErr_SetObject(PyExc_KeyError, key);
5699 Py_DECREF(key);
5700 return -1;
5701 }
5702 Py_DECREF(key);
5703
5704 PDATA_APPEND(self->stack, value, -1);
5705 return 0;
5706}
5707
5708static int
5709load_binget(UnpicklerObject *self)
5710{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005711 PyObject *value;
5712 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005713 char *s;
5714
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005715 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005716 return -1;
5717
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005718 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005719
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005720 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005721 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005722 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005723 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005724 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005725 Py_DECREF(key);
5726 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005727 return -1;
5728 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005729
5730 PDATA_APPEND(self->stack, value, -1);
5731 return 0;
5732}
5733
5734static int
5735load_long_binget(UnpicklerObject *self)
5736{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005737 PyObject *value;
5738 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005739 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005740
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005741 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005742 return -1;
5743
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005744 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005745
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005746 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005747 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005748 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005749 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005750 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005751 Py_DECREF(key);
5752 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005753 return -1;
5754 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005755
5756 PDATA_APPEND(self->stack, value, -1);
5757 return 0;
5758}
5759
5760/* Push an object from the extension registry (EXT[124]). nbytes is
5761 * the number of bytes following the opcode, holding the index (code) value.
5762 */
5763static int
5764load_extension(UnpicklerObject *self, int nbytes)
5765{
5766 char *codebytes; /* the nbytes bytes after the opcode */
5767 long code; /* calc_binint returns long */
5768 PyObject *py_code; /* code as a Python int */
5769 PyObject *obj; /* the object to push */
5770 PyObject *pair; /* (module_name, class_name) */
5771 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005772 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773
5774 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005775 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776 return -1;
5777 code = calc_binint(codebytes, nbytes);
5778 if (code <= 0) { /* note that 0 is forbidden */
5779 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005780 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781 return -1;
5782 }
5783
5784 /* Look for the code in the cache. */
5785 py_code = PyLong_FromLong(code);
5786 if (py_code == NULL)
5787 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005788 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789 if (obj != NULL) {
5790 /* Bingo. */
5791 Py_DECREF(py_code);
5792 PDATA_APPEND(self->stack, obj, -1);
5793 return 0;
5794 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005795 if (PyErr_Occurred()) {
5796 Py_DECREF(py_code);
5797 return -1;
5798 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005799
5800 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005801 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005802 if (pair == NULL) {
5803 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005804 if (!PyErr_Occurred()) {
5805 PyErr_Format(PyExc_ValueError, "unregistered extension "
5806 "code %ld", code);
5807 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005808 return -1;
5809 }
5810 /* Since the extension registry is manipulable via Python code,
5811 * confirm that pair is really a 2-tuple of strings.
5812 */
5813 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5814 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5815 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5816 Py_DECREF(py_code);
5817 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5818 "isn't a 2-tuple of strings", code);
5819 return -1;
5820 }
5821 /* Load the object. */
5822 obj = find_class(self, module_name, class_name);
5823 if (obj == NULL) {
5824 Py_DECREF(py_code);
5825 return -1;
5826 }
5827 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005828 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005829 Py_DECREF(py_code);
5830 if (code < 0) {
5831 Py_DECREF(obj);
5832 return -1;
5833 }
5834 PDATA_PUSH(self->stack, obj, -1);
5835 return 0;
5836}
5837
5838static int
5839load_put(UnpicklerObject *self)
5840{
5841 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005842 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005843 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005844 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005845
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005846 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005847 return -1;
5848 if (len < 2)
5849 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005850 if (Py_SIZE(self->stack) <= self->stack->fence)
5851 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005852 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005853
5854 key = PyLong_FromString(s, NULL, 10);
5855 if (key == NULL)
5856 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005857 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005858 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005859 if (idx < 0) {
5860 if (!PyErr_Occurred())
5861 PyErr_SetString(PyExc_ValueError,
5862 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005863 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005864 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005865
5866 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005867}
5868
5869static int
5870load_binput(UnpicklerObject *self)
5871{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005872 PyObject *value;
5873 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005874 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005875
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005876 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005877 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005878
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005879 if (Py_SIZE(self->stack) <= self->stack->fence)
5880 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005881 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005882
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005883 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005884
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005885 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005886}
5887
5888static int
5889load_long_binput(UnpicklerObject *self)
5890{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005891 PyObject *value;
5892 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005893 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005894
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005895 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005896 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005897
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005898 if (Py_SIZE(self->stack) <= self->stack->fence)
5899 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005900 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005901
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005902 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005903 if (idx < 0) {
5904 PyErr_SetString(PyExc_ValueError,
5905 "negative LONG_BINPUT argument");
5906 return -1;
5907 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005908
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005909 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005910}
5911
5912static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005913load_memoize(UnpicklerObject *self)
5914{
5915 PyObject *value;
5916
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005917 if (Py_SIZE(self->stack) <= self->stack->fence)
5918 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005919 value = self->stack->data[Py_SIZE(self->stack) - 1];
5920
5921 return _Unpickler_MemoPut(self, self->memo_len, value);
5922}
5923
5924static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005925do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005926{
5927 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005928 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005929 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005930 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005931 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005932
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005933 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005934 if (x > len || x <= self->stack->fence)
5935 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005936 if (len == x) /* nothing to do */
5937 return 0;
5938
5939 list = self->stack->data[x - 1];
5940
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005941 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005942 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005943 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005944
5945 slice = Pdata_poplist(self->stack, x);
5946 if (!slice)
5947 return -1;
5948 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005949 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005950 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005951 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005952 }
5953 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005954 PyObject *extend_func;
5955 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005956
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005957 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
5958 if (extend_func != NULL) {
5959 slice = Pdata_poplist(self->stack, x);
5960 if (!slice) {
5961 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005962 return -1;
5963 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005964 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005965 Py_DECREF(extend_func);
5966 if (result == NULL)
5967 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005968 Py_DECREF(result);
5969 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02005970 else {
5971 PyObject *append_func;
5972 _Py_IDENTIFIER(append);
5973
5974 /* Even if the PEP 307 requires extend() and append() methods,
5975 fall back on append() if the object has no extend() method
5976 for backward compatibility. */
5977 PyErr_Clear();
5978 append_func = _PyObject_GetAttrId(list, &PyId_append);
5979 if (append_func == NULL)
5980 return -1;
5981 for (i = x; i < len; i++) {
5982 value = self->stack->data[i];
5983 result = _Pickle_FastCall(append_func, value);
5984 if (result == NULL) {
5985 Pdata_clear(self->stack, i + 1);
5986 Py_SIZE(self->stack) = x;
5987 Py_DECREF(append_func);
5988 return -1;
5989 }
5990 Py_DECREF(result);
5991 }
5992 Py_SIZE(self->stack) = x;
5993 Py_DECREF(append_func);
5994 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005995 }
5996
5997 return 0;
5998}
5999
6000static int
6001load_append(UnpicklerObject *self)
6002{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006003 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6004 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006005 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006006}
6007
6008static int
6009load_appends(UnpicklerObject *self)
6010{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006011 Py_ssize_t i = marker(self);
6012 if (i < 0)
6013 return -1;
6014 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006015}
6016
6017static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006018do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006019{
6020 PyObject *value, *key;
6021 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006022 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006023 int status = 0;
6024
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006025 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006026 if (x > len || x <= self->stack->fence)
6027 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006028 if (len == x) /* nothing to do */
6029 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006030 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006031 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006032 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006033 PyErr_SetString(st->UnpicklingError,
6034 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006035 return -1;
6036 }
6037
6038 /* Here, dict does not actually need to be a PyDict; it could be anything
6039 that supports the __setitem__ attribute. */
6040 dict = self->stack->data[x - 1];
6041
6042 for (i = x + 1; i < len; i += 2) {
6043 key = self->stack->data[i - 1];
6044 value = self->stack->data[i];
6045 if (PyObject_SetItem(dict, key, value) < 0) {
6046 status = -1;
6047 break;
6048 }
6049 }
6050
6051 Pdata_clear(self->stack, x);
6052 return status;
6053}
6054
6055static int
6056load_setitem(UnpicklerObject *self)
6057{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006058 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006059}
6060
6061static int
6062load_setitems(UnpicklerObject *self)
6063{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006064 Py_ssize_t i = marker(self);
6065 if (i < 0)
6066 return -1;
6067 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006068}
6069
6070static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006071load_additems(UnpicklerObject *self)
6072{
6073 PyObject *set;
6074 Py_ssize_t mark, len, i;
6075
6076 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006077 if (mark < 0)
6078 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006079 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006080 if (mark > len || mark <= self->stack->fence)
6081 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006082 if (len == mark) /* nothing to do */
6083 return 0;
6084
6085 set = self->stack->data[mark - 1];
6086
6087 if (PySet_Check(set)) {
6088 PyObject *items;
6089 int status;
6090
6091 items = Pdata_poptuple(self->stack, mark);
6092 if (items == NULL)
6093 return -1;
6094
6095 status = _PySet_Update(set, items);
6096 Py_DECREF(items);
6097 return status;
6098 }
6099 else {
6100 PyObject *add_func;
6101 _Py_IDENTIFIER(add);
6102
6103 add_func = _PyObject_GetAttrId(set, &PyId_add);
6104 if (add_func == NULL)
6105 return -1;
6106 for (i = mark; i < len; i++) {
6107 PyObject *result;
6108 PyObject *item;
6109
6110 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006111 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006112 if (result == NULL) {
6113 Pdata_clear(self->stack, i + 1);
6114 Py_SIZE(self->stack) = mark;
6115 return -1;
6116 }
6117 Py_DECREF(result);
6118 }
6119 Py_SIZE(self->stack) = mark;
6120 }
6121
6122 return 0;
6123}
6124
6125static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006126load_build(UnpicklerObject *self)
6127{
6128 PyObject *state, *inst, *slotstate;
6129 PyObject *setstate;
6130 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006131 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006132
6133 /* Stack is ... instance, state. We want to leave instance at
6134 * the stack top, possibly mutated via instance.__setstate__(state).
6135 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006136 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6137 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006138
6139 PDATA_POP(self->stack, state);
6140 if (state == NULL)
6141 return -1;
6142
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006143 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006144
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006145 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006146 if (setstate == NULL) {
6147 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6148 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006149 else {
6150 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006151 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006152 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006153 }
6154 else {
6155 PyObject *result;
6156
6157 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006158 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006159 Py_DECREF(setstate);
6160 if (result == NULL)
6161 return -1;
6162 Py_DECREF(result);
6163 return 0;
6164 }
6165
6166 /* A default __setstate__. First see whether state embeds a
6167 * slot state dict too (a proto 2 addition).
6168 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006169 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006170 PyObject *tmp = state;
6171
6172 state = PyTuple_GET_ITEM(tmp, 0);
6173 slotstate = PyTuple_GET_ITEM(tmp, 1);
6174 Py_INCREF(state);
6175 Py_INCREF(slotstate);
6176 Py_DECREF(tmp);
6177 }
6178 else
6179 slotstate = NULL;
6180
6181 /* Set inst.__dict__ from the state dict (if any). */
6182 if (state != Py_None) {
6183 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006184 PyObject *d_key, *d_value;
6185 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006186 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006187
6188 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006189 PickleState *st = _Pickle_GetGlobalState();
6190 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006191 goto error;
6192 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006193 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006194 if (dict == NULL)
6195 goto error;
6196
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006197 i = 0;
6198 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6199 /* normally the keys for instance attributes are
6200 interned. we should try to do that here. */
6201 Py_INCREF(d_key);
6202 if (PyUnicode_CheckExact(d_key))
6203 PyUnicode_InternInPlace(&d_key);
6204 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6205 Py_DECREF(d_key);
6206 goto error;
6207 }
6208 Py_DECREF(d_key);
6209 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006210 Py_DECREF(dict);
6211 }
6212
6213 /* Also set instance attributes from the slotstate dict (if any). */
6214 if (slotstate != NULL) {
6215 PyObject *d_key, *d_value;
6216 Py_ssize_t i;
6217
6218 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006219 PickleState *st = _Pickle_GetGlobalState();
6220 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006221 "slot state is not a dictionary");
6222 goto error;
6223 }
6224 i = 0;
6225 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6226 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6227 goto error;
6228 }
6229 }
6230
6231 if (0) {
6232 error:
6233 status = -1;
6234 }
6235
6236 Py_DECREF(state);
6237 Py_XDECREF(slotstate);
6238 return status;
6239}
6240
6241static int
6242load_mark(UnpicklerObject *self)
6243{
6244
6245 /* Note that we split the (pickle.py) stack into two stacks, an
6246 * object stack and a mark stack. Here we push a mark onto the
6247 * mark stack.
6248 */
6249
6250 if ((self->num_marks + 1) >= self->marks_size) {
6251 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006252
6253 /* Use the size_t type to check for overflow. */
6254 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006255 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006256 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006257 PyErr_NoMemory();
6258 return -1;
6259 }
6260
6261 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006262 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006263 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006264 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6265 if (self->marks == NULL) {
6266 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006267 PyErr_NoMemory();
6268 return -1;
6269 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006270 self->marks_size = (Py_ssize_t)alloc;
6271 }
6272
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006273 self->stack->mark_set = 1;
6274 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006275
6276 return 0;
6277}
6278
6279static int
6280load_reduce(UnpicklerObject *self)
6281{
6282 PyObject *callable = NULL;
6283 PyObject *argtup = NULL;
6284 PyObject *obj = NULL;
6285
6286 PDATA_POP(self->stack, argtup);
6287 if (argtup == NULL)
6288 return -1;
6289 PDATA_POP(self->stack, callable);
6290 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006291 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006292 Py_DECREF(callable);
6293 }
6294 Py_DECREF(argtup);
6295
6296 if (obj == NULL)
6297 return -1;
6298
6299 PDATA_PUSH(self->stack, obj, -1);
6300 return 0;
6301}
6302
6303/* Just raises an error if we don't know the protocol specified. PROTO
6304 * is the first opcode for protocols >= 2.
6305 */
6306static int
6307load_proto(UnpicklerObject *self)
6308{
6309 char *s;
6310 int i;
6311
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006312 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006313 return -1;
6314
6315 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006316 if (i <= HIGHEST_PROTOCOL) {
6317 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006318 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006319 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006320
6321 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6322 return -1;
6323}
6324
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006325static int
6326load_frame(UnpicklerObject *self)
6327{
6328 char *s;
6329 Py_ssize_t frame_len;
6330
6331 if (_Unpickler_Read(self, &s, 8) < 0)
6332 return -1;
6333
6334 frame_len = calc_binsize(s, 8);
6335 if (frame_len < 0) {
6336 PyErr_Format(PyExc_OverflowError,
6337 "FRAME length exceeds system's maximum of %zd bytes",
6338 PY_SSIZE_T_MAX);
6339 return -1;
6340 }
6341
6342 if (_Unpickler_Read(self, &s, frame_len) < 0)
6343 return -1;
6344
6345 /* Rewind to start of frame */
6346 self->next_read_idx -= frame_len;
6347 return 0;
6348}
6349
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006350static PyObject *
6351load(UnpicklerObject *self)
6352{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006353 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006354 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006355
6356 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006357 self->stack->mark_set = 0;
6358 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006359 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006360 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006361 Pdata_clear(self->stack, 0);
6362
6363 /* Convenient macros for the dispatch while-switch loop just below. */
6364#define OP(opcode, load_func) \
6365 case opcode: if (load_func(self) < 0) break; continue;
6366
6367#define OP_ARG(opcode, load_func, arg) \
6368 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6369
6370 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006371 if (_Unpickler_Read(self, &s, 1) < 0) {
6372 PickleState *st = _Pickle_GetGlobalState();
6373 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6374 PyErr_Format(PyExc_EOFError, "Ran out of input");
6375 }
6376 return NULL;
6377 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006378
6379 switch ((enum opcode)s[0]) {
6380 OP(NONE, load_none)
6381 OP(BININT, load_binint)
6382 OP(BININT1, load_binint1)
6383 OP(BININT2, load_binint2)
6384 OP(INT, load_int)
6385 OP(LONG, load_long)
6386 OP_ARG(LONG1, load_counted_long, 1)
6387 OP_ARG(LONG4, load_counted_long, 4)
6388 OP(FLOAT, load_float)
6389 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006390 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6391 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6392 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6393 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6394 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006395 OP(STRING, load_string)
6396 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006397 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6398 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6399 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006400 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6401 OP_ARG(TUPLE1, load_counted_tuple, 1)
6402 OP_ARG(TUPLE2, load_counted_tuple, 2)
6403 OP_ARG(TUPLE3, load_counted_tuple, 3)
6404 OP(TUPLE, load_tuple)
6405 OP(EMPTY_LIST, load_empty_list)
6406 OP(LIST, load_list)
6407 OP(EMPTY_DICT, load_empty_dict)
6408 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006409 OP(EMPTY_SET, load_empty_set)
6410 OP(ADDITEMS, load_additems)
6411 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006412 OP(OBJ, load_obj)
6413 OP(INST, load_inst)
6414 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006415 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006416 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006417 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006418 OP(APPEND, load_append)
6419 OP(APPENDS, load_appends)
6420 OP(BUILD, load_build)
6421 OP(DUP, load_dup)
6422 OP(BINGET, load_binget)
6423 OP(LONG_BINGET, load_long_binget)
6424 OP(GET, load_get)
6425 OP(MARK, load_mark)
6426 OP(BINPUT, load_binput)
6427 OP(LONG_BINPUT, load_long_binput)
6428 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006429 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006430 OP(POP, load_pop)
6431 OP(POP_MARK, load_pop_mark)
6432 OP(SETITEM, load_setitem)
6433 OP(SETITEMS, load_setitems)
6434 OP(PERSID, load_persid)
6435 OP(BINPERSID, load_binpersid)
6436 OP(REDUCE, load_reduce)
6437 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006438 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006439 OP_ARG(EXT1, load_extension, 1)
6440 OP_ARG(EXT2, load_extension, 2)
6441 OP_ARG(EXT4, load_extension, 4)
6442 OP_ARG(NEWTRUE, load_bool, Py_True)
6443 OP_ARG(NEWFALSE, load_bool, Py_False)
6444
6445 case STOP:
6446 break;
6447
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006448 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006449 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006450 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006451 unsigned char c = (unsigned char) *s;
6452 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6453 PyErr_Format(st->UnpicklingError,
6454 "invalid load key, '%c'.", c);
6455 }
6456 else {
6457 PyErr_Format(st->UnpicklingError,
6458 "invalid load key, '\\x%02x'.", c);
6459 }
6460 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006461 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006462 }
6463
6464 break; /* and we are done! */
6465 }
6466
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006467 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006468 return NULL;
6469 }
6470
Victor Stinner2ae57e32013-10-31 13:39:23 +01006471 if (_Unpickler_SkipConsumed(self) < 0)
6472 return NULL;
6473
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006474 PDATA_POP(self->stack, value);
6475 return value;
6476}
6477
Larry Hastings61272b72014-01-07 12:41:53 -08006478/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006479
6480_pickle.Unpickler.load
6481
6482Load a pickle.
6483
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006484Read a pickled object representation from the open file object given
6485in the constructor, and return the reconstituted object hierarchy
6486specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006487[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006488
Larry Hastings3cceb382014-01-04 11:09:09 -08006489static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006490_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006491/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006492{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006493 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006494
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006495 /* Check whether the Unpickler was initialized correctly. This prevents
6496 segfaulting if a subclass overridden __init__ with a function that does
6497 not call Unpickler.__init__(). Here, we simply ensure that self->read
6498 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006499 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006500 PickleState *st = _Pickle_GetGlobalState();
6501 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006502 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006503 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006504 return NULL;
6505 }
6506
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006507 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006508}
6509
6510/* The name of find_class() is misleading. In newer pickle protocols, this
6511 function is used for loading any global (i.e., functions), not just
6512 classes. The name is kept only for backward compatibility. */
6513
Larry Hastings61272b72014-01-07 12:41:53 -08006514/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006515
6516_pickle.Unpickler.find_class
6517
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006518 module_name: object
6519 global_name: object
6520 /
6521
6522Return an object from a specified module.
6523
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006524If necessary, the module will be imported. Subclasses may override
6525this method (e.g. to restrict unpickling of arbitrary classes and
6526functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006527
6528This method is called whenever a class or a function object is
6529needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006530[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006531
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006532static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006533_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6534 PyObject *module_name,
6535 PyObject *global_name)
6536/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006537{
6538 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006541 /* Try to map the old names used in Python 2.x to the new ones used in
6542 Python 3.x. We do this only with old pickle protocols and when the
6543 user has not disabled the feature. */
6544 if (self->proto < 3 && self->fix_imports) {
6545 PyObject *key;
6546 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006547 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006548
6549 /* Check if the global (i.e., a function or a class) was renamed
6550 or moved to another module. */
6551 key = PyTuple_Pack(2, module_name, global_name);
6552 if (key == NULL)
6553 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006554 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006555 Py_DECREF(key);
6556 if (item) {
6557 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6558 PyErr_Format(PyExc_RuntimeError,
6559 "_compat_pickle.NAME_MAPPING values should be "
6560 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6561 return NULL;
6562 }
6563 module_name = PyTuple_GET_ITEM(item, 0);
6564 global_name = PyTuple_GET_ITEM(item, 1);
6565 if (!PyUnicode_Check(module_name) ||
6566 !PyUnicode_Check(global_name)) {
6567 PyErr_Format(PyExc_RuntimeError,
6568 "_compat_pickle.NAME_MAPPING values should be "
6569 "pairs of str, not (%.200s, %.200s)",
6570 Py_TYPE(module_name)->tp_name,
6571 Py_TYPE(global_name)->tp_name);
6572 return NULL;
6573 }
6574 }
6575 else if (PyErr_Occurred()) {
6576 return NULL;
6577 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006578 else {
6579 /* Check if the module was renamed. */
6580 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6581 if (item) {
6582 if (!PyUnicode_Check(item)) {
6583 PyErr_Format(PyExc_RuntimeError,
6584 "_compat_pickle.IMPORT_MAPPING values should be "
6585 "strings, not %.200s", Py_TYPE(item)->tp_name);
6586 return NULL;
6587 }
6588 module_name = item;
6589 }
6590 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006591 return NULL;
6592 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006593 }
6594 }
6595
Eric Snow3f9eee62017-09-15 16:35:20 -06006596 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006597 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006598 if (PyErr_Occurred())
6599 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006600 module = PyImport_Import(module_name);
6601 if (module == NULL)
6602 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006603 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006604 global = getattribute(module, global_name, self->proto >= 4);
6605 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006606 return global;
6607}
6608
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006609/*[clinic input]
6610
6611_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6612
6613Returns size in memory, in bytes.
6614[clinic start generated code]*/
6615
6616static Py_ssize_t
6617_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6618/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6619{
6620 Py_ssize_t res;
6621
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006622 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006623 if (self->memo != NULL)
6624 res += self->memo_size * sizeof(PyObject *);
6625 if (self->marks != NULL)
6626 res += self->marks_size * sizeof(Py_ssize_t);
6627 if (self->input_line != NULL)
6628 res += strlen(self->input_line) + 1;
6629 if (self->encoding != NULL)
6630 res += strlen(self->encoding) + 1;
6631 if (self->errors != NULL)
6632 res += strlen(self->errors) + 1;
6633 return res;
6634}
6635
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006636static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006637 _PICKLE_UNPICKLER_LOAD_METHODDEF
6638 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006639 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006640 {NULL, NULL} /* sentinel */
6641};
6642
6643static void
6644Unpickler_dealloc(UnpicklerObject *self)
6645{
6646 PyObject_GC_UnTrack((PyObject *)self);
6647 Py_XDECREF(self->readline);
6648 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006649 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650 Py_XDECREF(self->stack);
6651 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006652 if (self->buffer.buf != NULL) {
6653 PyBuffer_Release(&self->buffer);
6654 self->buffer.buf = NULL;
6655 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006656
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006657 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006658 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006659 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006660 PyMem_Free(self->encoding);
6661 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006662
6663 Py_TYPE(self)->tp_free((PyObject *)self);
6664}
6665
6666static int
6667Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6668{
6669 Py_VISIT(self->readline);
6670 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006671 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006672 Py_VISIT(self->stack);
6673 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006674 return 0;
6675}
6676
6677static int
6678Unpickler_clear(UnpicklerObject *self)
6679{
6680 Py_CLEAR(self->readline);
6681 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006682 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006683 Py_CLEAR(self->stack);
6684 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006685 if (self->buffer.buf != NULL) {
6686 PyBuffer_Release(&self->buffer);
6687 self->buffer.buf = NULL;
6688 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006689
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006690 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006691 PyMem_Free(self->marks);
6692 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006693 PyMem_Free(self->input_line);
6694 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006695 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006696 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006697 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006698 self->errors = NULL;
6699
6700 return 0;
6701}
6702
Larry Hastings61272b72014-01-07 12:41:53 -08006703/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006704
6705_pickle.Unpickler.__init__
6706
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006707 file: object
6708 *
6709 fix_imports: bool = True
6710 encoding: str = 'ASCII'
6711 errors: str = 'strict'
6712
6713This takes a binary file for reading a pickle data stream.
6714
6715The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006716protocol argument is needed. Bytes past the pickled object's
6717representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006718
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006719The argument *file* must have two methods, a read() method that takes
6720an integer argument, and a readline() method that requires no
6721arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006722binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006723other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006724
6725Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006726which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006727generated by Python 2. If *fix_imports* is True, pickle will try to
6728map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006729*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006730instances pickled by Python 2; these default to 'ASCII' and 'strict',
6731respectively. The *encoding* can be 'bytes' to read these 8-bit
6732string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006733[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006734
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006735static int
Larry Hastings89964c42015-04-14 18:07:59 -04006736_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6737 int fix_imports, const char *encoding,
6738 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006739/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006740{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006741 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006742
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006743 /* In case of multiple __init__() calls, clear previous content. */
6744 if (self->read != NULL)
6745 (void)Unpickler_clear(self);
6746
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006747 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006748 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006749
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006750 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006751 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006752
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006753 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006754
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006755 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6756 &self->pers_func, &self->pers_func_self) < 0)
6757 {
6758 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006759 }
6760
6761 self->stack = (Pdata *)Pdata_New();
6762 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006763 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006765 self->memo_size = 32;
6766 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006767 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006768 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006769
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006770 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006771
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006772 return 0;
6773}
6774
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006775
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006776/* Define a proxy object for the Unpickler's internal memo object. This is to
6777 * avoid breaking code like:
6778 * unpickler.memo.clear()
6779 * and
6780 * unpickler.memo = saved_memo
6781 * Is this a good idea? Not really, but we don't want to break code that uses
6782 * it. Note that we don't implement the entire mapping API here. This is
6783 * intentional, as these should be treated as black-box implementation details.
6784 *
6785 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006786 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006787 */
6788
Larry Hastings61272b72014-01-07 12:41:53 -08006789/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006790_pickle.UnpicklerMemoProxy.clear
6791
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006792Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006793[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006794
Larry Hastings3cceb382014-01-04 11:09:09 -08006795static PyObject *
6796_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006797/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006798{
6799 _Unpickler_MemoCleanup(self->unpickler);
6800 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6801 if (self->unpickler->memo == NULL)
6802 return NULL;
6803 Py_RETURN_NONE;
6804}
6805
Larry Hastings61272b72014-01-07 12:41:53 -08006806/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006807_pickle.UnpicklerMemoProxy.copy
6808
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006809Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006810[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006811
Larry Hastings3cceb382014-01-04 11:09:09 -08006812static PyObject *
6813_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006814/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006815{
6816 Py_ssize_t i;
6817 PyObject *new_memo = PyDict_New();
6818 if (new_memo == NULL)
6819 return NULL;
6820
6821 for (i = 0; i < self->unpickler->memo_size; i++) {
6822 int status;
6823 PyObject *key, *value;
6824
6825 value = self->unpickler->memo[i];
6826 if (value == NULL)
6827 continue;
6828
6829 key = PyLong_FromSsize_t(i);
6830 if (key == NULL)
6831 goto error;
6832 status = PyDict_SetItem(new_memo, key, value);
6833 Py_DECREF(key);
6834 if (status < 0)
6835 goto error;
6836 }
6837 return new_memo;
6838
6839error:
6840 Py_DECREF(new_memo);
6841 return NULL;
6842}
6843
Larry Hastings61272b72014-01-07 12:41:53 -08006844/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006845_pickle.UnpicklerMemoProxy.__reduce__
6846
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006847Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006848[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006849
Larry Hastings3cceb382014-01-04 11:09:09 -08006850static PyObject *
6851_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006852/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006853{
6854 PyObject *reduce_value;
6855 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006856 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006857 if (contents == NULL)
6858 return NULL;
6859
6860 reduce_value = PyTuple_New(2);
6861 if (reduce_value == NULL) {
6862 Py_DECREF(contents);
6863 return NULL;
6864 }
6865 constructor_args = PyTuple_New(1);
6866 if (constructor_args == NULL) {
6867 Py_DECREF(contents);
6868 Py_DECREF(reduce_value);
6869 return NULL;
6870 }
6871 PyTuple_SET_ITEM(constructor_args, 0, contents);
6872 Py_INCREF((PyObject *)&PyDict_Type);
6873 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6874 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6875 return reduce_value;
6876}
6877
6878static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006879 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6880 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6881 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006882 {NULL, NULL} /* sentinel */
6883};
6884
6885static void
6886UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6887{
6888 PyObject_GC_UnTrack(self);
6889 Py_XDECREF(self->unpickler);
6890 PyObject_GC_Del((PyObject *)self);
6891}
6892
6893static int
6894UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6895 visitproc visit, void *arg)
6896{
6897 Py_VISIT(self->unpickler);
6898 return 0;
6899}
6900
6901static int
6902UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6903{
6904 Py_CLEAR(self->unpickler);
6905 return 0;
6906}
6907
6908static PyTypeObject UnpicklerMemoProxyType = {
6909 PyVarObject_HEAD_INIT(NULL, 0)
6910 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6911 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6912 0,
6913 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6914 0, /* tp_print */
6915 0, /* tp_getattr */
6916 0, /* tp_setattr */
6917 0, /* tp_compare */
6918 0, /* tp_repr */
6919 0, /* tp_as_number */
6920 0, /* tp_as_sequence */
6921 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006922 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006923 0, /* tp_call */
6924 0, /* tp_str */
6925 PyObject_GenericGetAttr, /* tp_getattro */
6926 PyObject_GenericSetAttr, /* tp_setattro */
6927 0, /* tp_as_buffer */
6928 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6929 0, /* tp_doc */
6930 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6931 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6932 0, /* tp_richcompare */
6933 0, /* tp_weaklistoffset */
6934 0, /* tp_iter */
6935 0, /* tp_iternext */
6936 unpicklerproxy_methods, /* tp_methods */
6937};
6938
6939static PyObject *
6940UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6941{
6942 UnpicklerMemoProxyObject *self;
6943
6944 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6945 &UnpicklerMemoProxyType);
6946 if (self == NULL)
6947 return NULL;
6948 Py_INCREF(unpickler);
6949 self->unpickler = unpickler;
6950 PyObject_GC_Track(self);
6951 return (PyObject *)self;
6952}
6953
6954/*****************************************************************************/
6955
6956
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006957static PyObject *
6958Unpickler_get_memo(UnpicklerObject *self)
6959{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006960 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006961}
6962
6963static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006964Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006965{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006966 PyObject **new_memo;
6967 Py_ssize_t new_memo_size = 0;
6968 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006969
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006970 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006971 PyErr_SetString(PyExc_TypeError,
6972 "attribute deletion is not supported");
6973 return -1;
6974 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006975
6976 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6977 UnpicklerObject *unpickler =
6978 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6979
6980 new_memo_size = unpickler->memo_size;
6981 new_memo = _Unpickler_NewMemo(new_memo_size);
6982 if (new_memo == NULL)
6983 return -1;
6984
6985 for (i = 0; i < new_memo_size; i++) {
6986 Py_XINCREF(unpickler->memo[i]);
6987 new_memo[i] = unpickler->memo[i];
6988 }
6989 }
6990 else if (PyDict_Check(obj)) {
6991 Py_ssize_t i = 0;
6992 PyObject *key, *value;
6993
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006994 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006995 new_memo = _Unpickler_NewMemo(new_memo_size);
6996 if (new_memo == NULL)
6997 return -1;
6998
6999 while (PyDict_Next(obj, &i, &key, &value)) {
7000 Py_ssize_t idx;
7001 if (!PyLong_Check(key)) {
7002 PyErr_SetString(PyExc_TypeError,
7003 "memo key must be integers");
7004 goto error;
7005 }
7006 idx = PyLong_AsSsize_t(key);
7007 if (idx == -1 && PyErr_Occurred())
7008 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007009 if (idx < 0) {
7010 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007011 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007012 goto error;
7013 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007014 if (_Unpickler_MemoPut(self, idx, value) < 0)
7015 goto error;
7016 }
7017 }
7018 else {
7019 PyErr_Format(PyExc_TypeError,
7020 "'memo' attribute must be an UnpicklerMemoProxy object"
7021 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007022 return -1;
7023 }
7024
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007025 _Unpickler_MemoCleanup(self);
7026 self->memo_size = new_memo_size;
7027 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007028
7029 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007030
7031 error:
7032 if (new_memo_size) {
7033 i = new_memo_size;
7034 while (--i >= 0) {
7035 Py_XDECREF(new_memo[i]);
7036 }
7037 PyMem_FREE(new_memo);
7038 }
7039 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007040}
7041
7042static PyObject *
7043Unpickler_get_persload(UnpicklerObject *self)
7044{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007045 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007046 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007047 return NULL;
7048 }
7049 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007050}
7051
7052static int
7053Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
7054{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007055 if (value == NULL) {
7056 PyErr_SetString(PyExc_TypeError,
7057 "attribute deletion is not supported");
7058 return -1;
7059 }
7060 if (!PyCallable_Check(value)) {
7061 PyErr_SetString(PyExc_TypeError,
7062 "persistent_load must be a callable taking "
7063 "one argument");
7064 return -1;
7065 }
7066
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007067 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007068 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007069 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007070
7071 return 0;
7072}
7073
7074static PyGetSetDef Unpickler_getsets[] = {
7075 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7076 {"persistent_load", (getter)Unpickler_get_persload,
7077 (setter)Unpickler_set_persload},
7078 {NULL}
7079};
7080
7081static PyTypeObject Unpickler_Type = {
7082 PyVarObject_HEAD_INIT(NULL, 0)
7083 "_pickle.Unpickler", /*tp_name*/
7084 sizeof(UnpicklerObject), /*tp_basicsize*/
7085 0, /*tp_itemsize*/
7086 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7087 0, /*tp_print*/
7088 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007089 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007090 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007091 0, /*tp_repr*/
7092 0, /*tp_as_number*/
7093 0, /*tp_as_sequence*/
7094 0, /*tp_as_mapping*/
7095 0, /*tp_hash*/
7096 0, /*tp_call*/
7097 0, /*tp_str*/
7098 0, /*tp_getattro*/
7099 0, /*tp_setattro*/
7100 0, /*tp_as_buffer*/
7101 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007102 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007103 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7104 (inquiry)Unpickler_clear, /*tp_clear*/
7105 0, /*tp_richcompare*/
7106 0, /*tp_weaklistoffset*/
7107 0, /*tp_iter*/
7108 0, /*tp_iternext*/
7109 Unpickler_methods, /*tp_methods*/
7110 0, /*tp_members*/
7111 Unpickler_getsets, /*tp_getset*/
7112 0, /*tp_base*/
7113 0, /*tp_dict*/
7114 0, /*tp_descr_get*/
7115 0, /*tp_descr_set*/
7116 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007117 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007118 PyType_GenericAlloc, /*tp_alloc*/
7119 PyType_GenericNew, /*tp_new*/
7120 PyObject_GC_Del, /*tp_free*/
7121 0, /*tp_is_gc*/
7122};
7123
Larry Hastings61272b72014-01-07 12:41:53 -08007124/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007125
7126_pickle.dump
7127
7128 obj: object
7129 file: object
7130 protocol: object = NULL
7131 *
7132 fix_imports: bool = True
7133
7134Write a pickled representation of obj to the open file object file.
7135
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007136This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7137be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007138
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007139The optional *protocol* argument tells the pickler to use the given
7140protocol supported protocols are 0, 1, 2, 3 and 4. The default
7141protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007142
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007143Specifying a negative protocol version selects the highest protocol
7144version supported. The higher the protocol used, the more recent the
7145version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007146
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007147The *file* argument must have a write() method that accepts a single
7148bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007149writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007150this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007151
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007152If *fix_imports* is True and protocol is less than 3, pickle will try
7153to map the new Python 3 names to the old module names used in Python
71542, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007155[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007156
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007157static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007158_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007159 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007160/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007161{
7162 PicklerObject *pickler = _Pickler_New();
7163
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007164 if (pickler == NULL)
7165 return NULL;
7166
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007167 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007168 goto error;
7169
7170 if (_Pickler_SetOutputStream(pickler, file) < 0)
7171 goto error;
7172
7173 if (dump(pickler, obj) < 0)
7174 goto error;
7175
7176 if (_Pickler_FlushToFile(pickler) < 0)
7177 goto error;
7178
7179 Py_DECREF(pickler);
7180 Py_RETURN_NONE;
7181
7182 error:
7183 Py_XDECREF(pickler);
7184 return NULL;
7185}
7186
Larry Hastings61272b72014-01-07 12:41:53 -08007187/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188
7189_pickle.dumps
7190
7191 obj: object
7192 protocol: object = NULL
7193 *
7194 fix_imports: bool = True
7195
7196Return the pickled representation of the object as a bytes object.
7197
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007198The optional *protocol* argument tells the pickler to use the given
7199protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7200protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007201
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007202Specifying a negative protocol version selects the highest protocol
7203version supported. The higher the protocol used, the more recent the
7204version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007205
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007206If *fix_imports* is True and *protocol* is less than 3, pickle will
7207try to map the new Python 3 names to the old module names used in
7208Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007209[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007210
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007211static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007212_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007213 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007214/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007215{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007216 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007217 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007218
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007219 if (pickler == NULL)
7220 return NULL;
7221
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007222 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007223 goto error;
7224
7225 if (dump(pickler, obj) < 0)
7226 goto error;
7227
7228 result = _Pickler_GetString(pickler);
7229 Py_DECREF(pickler);
7230 return result;
7231
7232 error:
7233 Py_XDECREF(pickler);
7234 return NULL;
7235}
7236
Larry Hastings61272b72014-01-07 12:41:53 -08007237/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007238
7239_pickle.load
7240
7241 file: object
7242 *
7243 fix_imports: bool = True
7244 encoding: str = 'ASCII'
7245 errors: str = 'strict'
7246
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007247Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007248
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007249This is equivalent to ``Unpickler(file).load()``, but may be more
7250efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007251
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007252The protocol version of the pickle is detected automatically, so no
7253protocol argument is needed. Bytes past the pickled object's
7254representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007255
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007256The argument *file* must have two methods, a read() method that takes
7257an integer argument, and a readline() method that requires no
7258arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007259binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007260other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007261
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007262Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007263which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007264generated by Python 2. If *fix_imports* is True, pickle will try to
7265map the old Python 2 names to the new names used in Python 3. The
7266*encoding* and *errors* tell pickle how to decode 8-bit string
7267instances pickled by Python 2; these default to 'ASCII' and 'strict',
7268respectively. The *encoding* can be 'bytes' to read these 8-bit
7269string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007270[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007271
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007272static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007273_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007274 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007275/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007276{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007277 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007278 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007279
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007280 if (unpickler == NULL)
7281 return NULL;
7282
7283 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7284 goto error;
7285
7286 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7287 goto error;
7288
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007289 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007290
7291 result = load(unpickler);
7292 Py_DECREF(unpickler);
7293 return result;
7294
7295 error:
7296 Py_XDECREF(unpickler);
7297 return NULL;
7298}
7299
Larry Hastings61272b72014-01-07 12:41:53 -08007300/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007301
7302_pickle.loads
7303
7304 data: object
7305 *
7306 fix_imports: bool = True
7307 encoding: str = 'ASCII'
7308 errors: str = 'strict'
7309
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007310Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007311
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007312The protocol version of the pickle is detected automatically, so no
7313protocol argument is needed. Bytes past the pickled object's
7314representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007315
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007316Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007317which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007318generated by Python 2. If *fix_imports* is True, pickle will try to
7319map the old Python 2 names to the new names used in Python 3. The
7320*encoding* and *errors* tell pickle how to decode 8-bit string
7321instances pickled by Python 2; these default to 'ASCII' and 'strict',
7322respectively. The *encoding* can be 'bytes' to read these 8-bit
7323string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007324[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007325
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007326static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007327_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007328 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007329/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007330{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007331 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007332 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007333
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007334 if (unpickler == NULL)
7335 return NULL;
7336
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007337 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007338 goto error;
7339
7340 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7341 goto error;
7342
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007343 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007344
7345 result = load(unpickler);
7346 Py_DECREF(unpickler);
7347 return result;
7348
7349 error:
7350 Py_XDECREF(unpickler);
7351 return NULL;
7352}
7353
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007354static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007355 _PICKLE_DUMP_METHODDEF
7356 _PICKLE_DUMPS_METHODDEF
7357 _PICKLE_LOAD_METHODDEF
7358 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007359 {NULL, NULL} /* sentinel */
7360};
7361
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007362static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007363pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007364{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007365 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007366 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007367}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007368
Stefan Krahf483b0f2013-12-14 13:43:10 +01007369static void
7370pickle_free(PyObject *m)
7371{
7372 _Pickle_ClearState(_Pickle_GetState(m));
7373}
7374
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007375static int
7376pickle_traverse(PyObject *m, visitproc visit, void *arg)
7377{
7378 PickleState *st = _Pickle_GetState(m);
7379 Py_VISIT(st->PickleError);
7380 Py_VISIT(st->PicklingError);
7381 Py_VISIT(st->UnpicklingError);
7382 Py_VISIT(st->dispatch_table);
7383 Py_VISIT(st->extension_registry);
7384 Py_VISIT(st->extension_cache);
7385 Py_VISIT(st->inverted_registry);
7386 Py_VISIT(st->name_mapping_2to3);
7387 Py_VISIT(st->import_mapping_2to3);
7388 Py_VISIT(st->name_mapping_3to2);
7389 Py_VISIT(st->import_mapping_3to2);
7390 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007391 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007392 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007393}
7394
7395static struct PyModuleDef _picklemodule = {
7396 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007397 "_pickle", /* m_name */
7398 pickle_module_doc, /* m_doc */
7399 sizeof(PickleState), /* m_size */
7400 pickle_methods, /* m_methods */
7401 NULL, /* m_reload */
7402 pickle_traverse, /* m_traverse */
7403 pickle_clear, /* m_clear */
7404 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007405};
7406
7407PyMODINIT_FUNC
7408PyInit__pickle(void)
7409{
7410 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007411 PickleState *st;
7412
7413 m = PyState_FindModule(&_picklemodule);
7414 if (m) {
7415 Py_INCREF(m);
7416 return m;
7417 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007418
7419 if (PyType_Ready(&Unpickler_Type) < 0)
7420 return NULL;
7421 if (PyType_Ready(&Pickler_Type) < 0)
7422 return NULL;
7423 if (PyType_Ready(&Pdata_Type) < 0)
7424 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007425 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7426 return NULL;
7427 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7428 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007429
7430 /* Create the module and add the functions. */
7431 m = PyModule_Create(&_picklemodule);
7432 if (m == NULL)
7433 return NULL;
7434
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007435 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007436 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7437 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007438 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007439 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7440 return NULL;
7441
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007442 st = _Pickle_GetState(m);
7443
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007444 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007445 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7446 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007447 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007448 st->PicklingError = \
7449 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7450 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007451 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007452 st->UnpicklingError = \
7453 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7454 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007455 return NULL;
7456
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007457 Py_INCREF(st->PickleError);
7458 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007459 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007460 Py_INCREF(st->PicklingError);
7461 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007462 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007463 Py_INCREF(st->UnpicklingError);
7464 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007465 return NULL;
7466
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007467 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007468 return NULL;
7469
7470 return m;
7471}