blob: 5cb1fba6cc2918bda211c764404ee1d2ebadc443 [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
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000974static PyObject *
975_Pickler_GetString(PicklerObject *self)
976{
977 PyObject *output_buffer = self->output_buffer;
978
979 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100980
981 if (_Pickler_CommitFrame(self))
982 return NULL;
983
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000984 self->output_buffer = NULL;
985 /* Resize down to exact size */
986 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
987 return NULL;
988 return output_buffer;
989}
990
991static int
992_Pickler_FlushToFile(PicklerObject *self)
993{
994 PyObject *output, *result;
995
996 assert(self->write != NULL);
997
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100998 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 output = _Pickler_GetString(self);
1000 if (output == NULL)
1001 return -1;
1002
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001003 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001004 Py_XDECREF(result);
1005 return (result == NULL) ? -1 : 0;
1006}
1007
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01001008static int
1009_Pickler_OpcodeBoundary(PicklerObject *self)
1010{
1011 Py_ssize_t frame_len;
1012
1013 if (!self->framing || self->frame_start == -1) {
1014 return 0;
1015 }
1016 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1017 if (frame_len >= FRAME_SIZE_TARGET) {
1018 if(_Pickler_CommitFrame(self)) {
1019 return -1;
1020 }
1021 /* Flush the content of the commited frame to the underlying
1022 * file and reuse the pickler buffer for the next frame so as
1023 * to limit memory usage when dumping large complex objects to
1024 * a file.
1025 *
1026 * self->write is NULL when called via dumps.
1027 */
1028 if (self->write != NULL) {
1029 if (_Pickler_FlushToFile(self) < 0) {
1030 return -1;
1031 }
1032 if (_Pickler_ClearBuffer(self) < 0) {
1033 return -1;
1034 }
1035 }
1036 }
1037 return 0;
1038}
1039
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001040static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001041_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001043 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001045 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046
1047 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001048 need_new_frame = (self->framing && self->frame_start == -1);
1049
1050 if (need_new_frame)
1051 n = data_len + FRAME_HEADER_SIZE;
1052 else
1053 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001054
1055 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001056 if (required > self->max_output_len) {
1057 /* Make place in buffer for the pickle chunk */
1058 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1059 PyErr_NoMemory();
1060 return -1;
1061 }
1062 self->max_output_len = (self->output_len + n) / 2 * 3;
1063 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1064 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001065 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001066 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001067 if (need_new_frame) {
1068 /* Setup new frame */
1069 Py_ssize_t frame_start = self->output_len;
1070 self->frame_start = frame_start;
1071 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1072 /* Write an invalid value, for debugging */
1073 buffer[frame_start + i] = 0xFE;
1074 }
1075 self->output_len += FRAME_HEADER_SIZE;
1076 }
1077 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001078 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001079 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001080 buffer[self->output_len + i] = s[i];
1081 }
1082 }
1083 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001084 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001085 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001086 self->output_len += data_len;
1087 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001088}
1089
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001090static PicklerObject *
1091_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001092{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001093 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001094
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001095 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1096 if (self == NULL)
1097 return NULL;
1098
1099 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001100 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001101 self->write = NULL;
1102 self->proto = 0;
1103 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001104 self->framing = 0;
1105 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001106 self->fast = 0;
1107 self->fast_nesting = 0;
1108 self->fix_imports = 0;
1109 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001110 self->max_output_len = WRITE_BUF_SIZE;
1111 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001112
1113 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001114 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1115 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001116
1117 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001118 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001119 return NULL;
1120 }
1121 return self;
1122}
1123
1124static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001125_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001126{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001127 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001128
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001129 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001131 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001132 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001133 proto = PyLong_AsLong(protocol);
1134 if (proto < 0) {
1135 if (proto == -1 && PyErr_Occurred())
1136 return -1;
1137 proto = HIGHEST_PROTOCOL;
1138 }
1139 else if (proto > HIGHEST_PROTOCOL) {
1140 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1141 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001142 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001143 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001145 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001146 self->bin = proto > 0;
1147 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001148 return 0;
1149}
1150
1151/* Returns -1 (with an exception set) on failure, 0 on success. This may
1152 be called once on a freshly created Pickler. */
1153static int
1154_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1155{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001156 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001157 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001158 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001159 if (self->write == NULL) {
1160 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1161 PyErr_SetString(PyExc_TypeError,
1162 "file must have a 'write' attribute");
1163 return -1;
1164 }
1165
1166 return 0;
1167}
1168
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001169/* Returns the size of the input on success, -1 on failure. This takes its
1170 own reference to `input`. */
1171static Py_ssize_t
1172_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1173{
1174 if (self->buffer.buf != NULL)
1175 PyBuffer_Release(&self->buffer);
1176 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1177 return -1;
1178 self->input_buffer = self->buffer.buf;
1179 self->input_len = self->buffer.len;
1180 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001181 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001182 return self->input_len;
1183}
1184
Antoine Pitrou04248a82010-10-12 20:51:21 +00001185static int
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001186bad_readline(void)
1187{
1188 PickleState *st = _Pickle_GetGlobalState();
1189 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1190 return -1;
1191}
1192
1193static int
Antoine Pitrou04248a82010-10-12 20:51:21 +00001194_Unpickler_SkipConsumed(UnpicklerObject *self)
1195{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001196 Py_ssize_t consumed;
1197 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001198
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001199 consumed = self->next_read_idx - self->prefetched_idx;
1200 if (consumed <= 0)
1201 return 0;
1202
1203 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001204 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001205 r = PyObject_CallFunction(self->read, "n", consumed);
1206 if (r == NULL)
1207 return -1;
1208 Py_DECREF(r);
1209
1210 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001211 return 0;
1212}
1213
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001214static const Py_ssize_t READ_WHOLE_LINE = -1;
1215
1216/* If reading from a file, we need to only pull the bytes we need, since there
1217 may be multiple pickle objects arranged contiguously in the same input
1218 buffer.
1219
1220 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1221 bytes from the input stream/buffer.
1222
1223 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1224 failure; on success, returns the number of bytes read from the file.
1225
1226 On success, self->input_len will be 0; this is intentional so that when
1227 unpickling from a file, the "we've run out of data" code paths will trigger,
1228 causing the Unpickler to go back to the file for more data. Use the returned
1229 size to tell you how much data you can process. */
1230static Py_ssize_t
1231_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1232{
1233 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001234 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001235
1236 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001237
Antoine Pitrou04248a82010-10-12 20:51:21 +00001238 if (_Unpickler_SkipConsumed(self) < 0)
1239 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001240
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001241 if (n == READ_WHOLE_LINE) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02001242 data = _PyObject_CallNoArg(self->readline);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001243 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001244 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001245 PyObject *len;
1246 /* Prefetch some data without advancing the file pointer, if possible */
1247 if (self->peek && n < PREFETCH) {
1248 len = PyLong_FromSsize_t(PREFETCH);
1249 if (len == NULL)
1250 return -1;
1251 data = _Pickle_FastCall(self->peek, len);
1252 if (data == NULL) {
1253 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1254 return -1;
1255 /* peek() is probably not supported by the given file object */
1256 PyErr_Clear();
1257 Py_CLEAR(self->peek);
1258 }
1259 else {
1260 read_size = _Unpickler_SetStringInput(self, data);
1261 Py_DECREF(data);
1262 self->prefetched_idx = 0;
1263 if (n <= read_size)
1264 return n;
1265 }
1266 }
1267 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001268 if (len == NULL)
1269 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001270 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001271 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001272 if (data == NULL)
1273 return -1;
1274
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001275 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001276 Py_DECREF(data);
1277 return read_size;
1278}
1279
Victor Stinner19ed27e2016-05-20 11:42:37 +02001280/* Don't call it directly: use _Unpickler_Read() */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001281static Py_ssize_t
Victor Stinner19ed27e2016-05-20 11:42:37 +02001282_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001283{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001284 Py_ssize_t num_read;
1285
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001286 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001287 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1288 PickleState *st = _Pickle_GetGlobalState();
1289 PyErr_SetString(st->UnpicklingError,
1290 "read would overflow (invalid bytecode)");
1291 return -1;
1292 }
Victor Stinner19ed27e2016-05-20 11:42:37 +02001293
1294 /* This case is handled by the _Unpickler_Read() macro for efficiency */
1295 assert(self->next_read_idx + n > self->input_len);
1296
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001297 if (!self->read)
1298 return bad_readline();
1299
Antoine Pitrou04248a82010-10-12 20:51:21 +00001300 num_read = _Unpickler_ReadFromFile(self, n);
1301 if (num_read < 0)
1302 return -1;
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001303 if (num_read < n)
1304 return bad_readline();
Antoine Pitrou04248a82010-10-12 20:51:21 +00001305 *s = self->input_buffer;
1306 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001307 return n;
1308}
1309
Victor Stinner19ed27e2016-05-20 11:42:37 +02001310/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1311
1312 This should be used for all data reads, rather than accessing the unpickler's
1313 input buffer directly. This method deals correctly with reading from input
1314 streams, which the input buffer doesn't deal with.
1315
1316 Note that when reading from a file-like object, self->next_read_idx won't
1317 be updated (it should remain at 0 for the entire unpickling process). You
1318 should use this function's return value to know how many bytes you can
1319 consume.
1320
1321 Returns -1 (with an exception set) on failure. On success, return the
1322 number of chars read. */
1323#define _Unpickler_Read(self, s, n) \
Victor Stinnerda230562016-05-20 21:16:59 +02001324 (((n) <= (self)->input_len - (self)->next_read_idx) \
Victor Stinner19ed27e2016-05-20 11:42:37 +02001325 ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1326 (self)->next_read_idx += (n), \
1327 (n)) \
1328 : _Unpickler_ReadImpl(self, (s), (n)))
1329
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001330static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001331_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1332 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001333{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001334 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001335 if (input_line == NULL) {
1336 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001337 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001338 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001339
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001340 memcpy(input_line, line, len);
1341 input_line[len] = '\0';
1342 self->input_line = input_line;
1343 *result = self->input_line;
1344 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001345}
1346
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001347/* Read a line from the input stream/buffer. If we run off the end of the input
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001348 before hitting \n, raise an error.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001349
1350 Returns the number of chars read, or -1 on failure. */
1351static Py_ssize_t
1352_Unpickler_Readline(UnpicklerObject *self, char **result)
1353{
1354 Py_ssize_t i, num_read;
1355
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001356 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001357 if (self->input_buffer[i] == '\n') {
1358 char *line_start = self->input_buffer + self->next_read_idx;
1359 num_read = i - self->next_read_idx + 1;
1360 self->next_read_idx = i + 1;
1361 return _Unpickler_CopyLine(self, line_start, num_read, result);
1362 }
1363 }
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001364 if (!self->read)
1365 return bad_readline();
Victor Stinner121aab42011-09-29 23:40:53 +02001366
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03001367 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1368 if (num_read < 0)
1369 return -1;
1370 if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1371 return bad_readline();
1372 self->next_read_idx = num_read;
1373 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001374}
1375
1376/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1377 will be modified in place. */
1378static int
1379_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1380{
1381 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001382
1383 assert(new_size > self->memo_size);
1384
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001385 PyMem_RESIZE(self->memo, PyObject *, new_size);
1386 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001387 PyErr_NoMemory();
1388 return -1;
1389 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001390 for (i = self->memo_size; i < new_size; i++)
1391 self->memo[i] = NULL;
1392 self->memo_size = new_size;
1393 return 0;
1394}
1395
1396/* Returns NULL if idx is out of bounds. */
1397static PyObject *
1398_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1399{
1400 if (idx < 0 || idx >= self->memo_size)
1401 return NULL;
1402
1403 return self->memo[idx];
1404}
1405
1406/* Returns -1 (with an exception set) on failure, 0 on success.
1407 This takes its own reference to `value`. */
1408static int
1409_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1410{
1411 PyObject *old_item;
1412
1413 if (idx >= self->memo_size) {
1414 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1415 return -1;
1416 assert(idx < self->memo_size);
1417 }
1418 Py_INCREF(value);
1419 old_item = self->memo[idx];
1420 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001421 if (old_item != NULL) {
1422 Py_DECREF(old_item);
1423 }
1424 else {
1425 self->memo_len++;
1426 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001427 return 0;
1428}
1429
1430static PyObject **
1431_Unpickler_NewMemo(Py_ssize_t new_size)
1432{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001433 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001434 if (memo == NULL) {
1435 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001436 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001437 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001438 memset(memo, 0, new_size * sizeof(PyObject *));
1439 return memo;
1440}
1441
1442/* Free the unpickler's memo, taking care to decref any items left in it. */
1443static void
1444_Unpickler_MemoCleanup(UnpicklerObject *self)
1445{
1446 Py_ssize_t i;
1447 PyObject **memo = self->memo;
1448
1449 if (self->memo == NULL)
1450 return;
1451 self->memo = NULL;
1452 i = self->memo_size;
1453 while (--i >= 0) {
1454 Py_XDECREF(memo[i]);
1455 }
1456 PyMem_FREE(memo);
1457}
1458
1459static UnpicklerObject *
1460_Unpickler_New(void)
1461{
1462 UnpicklerObject *self;
1463
1464 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1465 if (self == NULL)
1466 return NULL;
1467
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001468 self->pers_func = NULL;
1469 self->input_buffer = NULL;
1470 self->input_line = NULL;
1471 self->input_len = 0;
1472 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001473 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001474 self->read = NULL;
1475 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001476 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001477 self->encoding = NULL;
1478 self->errors = NULL;
1479 self->marks = NULL;
1480 self->num_marks = 0;
1481 self->marks_size = 0;
1482 self->proto = 0;
1483 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001484 memset(&self->buffer, 0, sizeof(Py_buffer));
1485 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001486 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001487 self->memo = _Unpickler_NewMemo(self->memo_size);
1488 self->stack = (Pdata *)Pdata_New();
1489
1490 if (self->memo == NULL || self->stack == NULL) {
1491 Py_DECREF(self);
1492 return NULL;
1493 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001494
1495 return self;
1496}
1497
1498/* Returns -1 (with an exception set) on failure, 0 on success. This may
1499 be called once on a freshly created Pickler. */
1500static int
1501_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1502{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001503 _Py_IDENTIFIER(peek);
1504 _Py_IDENTIFIER(read);
1505 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001506
1507 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001508 if (self->peek == NULL) {
1509 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1510 PyErr_Clear();
1511 else
1512 return -1;
1513 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001514 self->read = _PyObject_GetAttrId(file, &PyId_read);
1515 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001516 if (self->readline == NULL || self->read == NULL) {
1517 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1518 PyErr_SetString(PyExc_TypeError,
1519 "file must have 'read' and 'readline' attributes");
1520 Py_CLEAR(self->read);
1521 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001522 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001523 return -1;
1524 }
1525 return 0;
1526}
1527
1528/* Returns -1 (with an exception set) on failure, 0 on success. This may
1529 be called once on a freshly created Pickler. */
1530static int
1531_Unpickler_SetInputEncoding(UnpicklerObject *self,
1532 const char *encoding,
1533 const char *errors)
1534{
1535 if (encoding == NULL)
1536 encoding = "ASCII";
1537 if (errors == NULL)
1538 errors = "strict";
1539
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001540 self->encoding = _PyMem_Strdup(encoding);
1541 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001542 if (self->encoding == NULL || self->errors == NULL) {
1543 PyErr_NoMemory();
1544 return -1;
1545 }
1546 return 0;
1547}
1548
1549/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001550static int
1551memo_get(PicklerObject *self, PyObject *key)
1552{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001553 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001554 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001555 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001556
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001557 value = PyMemoTable_Get(self->memo, key);
1558 if (value == NULL) {
1559 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001560 return -1;
1561 }
1562
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001563 if (!self->bin) {
1564 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001565 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1566 "%" PY_FORMAT_SIZE_T "d\n", *value);
1567 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001568 }
1569 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001570 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001571 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001572 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001573 len = 2;
1574 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001575 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001576 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001577 pdata[1] = (unsigned char)(*value & 0xff);
1578 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1579 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1580 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001581 len = 5;
1582 }
1583 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001584 PickleState *st = _Pickle_GetGlobalState();
1585 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001586 "memo id too large for LONG_BINGET");
1587 return -1;
1588 }
1589 }
1590
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001591 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001592 return -1;
1593
1594 return 0;
1595}
1596
1597/* Store an object in the memo, assign it a new unique ID based on the number
1598 of objects currently stored in the memo and generate a PUT opcode. */
1599static int
1600memo_put(PicklerObject *self, PyObject *obj)
1601{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001602 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001603 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001604 Py_ssize_t idx;
1605
1606 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001607
1608 if (self->fast)
1609 return 0;
1610
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 idx = PyMemoTable_Size(self->memo);
1612 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1613 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001614
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001615 if (self->proto >= 4) {
1616 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1617 return -1;
1618 return 0;
1619 }
1620 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001621 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001622 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001623 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001624 len = strlen(pdata);
1625 }
1626 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001627 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001628 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001629 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001630 len = 2;
1631 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001632 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001633 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001634 pdata[1] = (unsigned char)(idx & 0xff);
1635 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1636 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1637 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 len = 5;
1639 }
1640 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001641 PickleState *st = _Pickle_GetGlobalState();
1642 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001643 "memo id too large for LONG_BINPUT");
1644 return -1;
1645 }
1646 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001647 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001651}
1652
1653static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001654get_dotted_path(PyObject *obj, PyObject *name)
1655{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001656 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001657 PyObject *dotted_path;
1658 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001659
1660 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001661 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001662 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001663 n = PyList_GET_SIZE(dotted_path);
1664 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001665 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001666 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001667 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001668 if (obj == NULL)
1669 PyErr_Format(PyExc_AttributeError,
1670 "Can't pickle local object %R", name);
1671 else
1672 PyErr_Format(PyExc_AttributeError,
1673 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001674 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001675 return NULL;
1676 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001677 }
1678 return dotted_path;
1679}
1680
1681static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001682get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001683{
1684 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001685 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001686
1687 assert(PyList_CheckExact(names));
1688 Py_INCREF(obj);
1689 n = PyList_GET_SIZE(names);
1690 for (i = 0; i < n; i++) {
1691 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001692 Py_XDECREF(parent);
1693 parent = obj;
1694 obj = PyObject_GetAttr(parent, name);
1695 if (obj == NULL) {
1696 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001697 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001698 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001699 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001700 if (pparent != NULL)
1701 *pparent = parent;
1702 else
1703 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001704 return obj;
1705}
1706
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001707static void
1708reformat_attribute_error(PyObject *obj, PyObject *name)
1709{
1710 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1711 PyErr_Clear();
1712 PyErr_Format(PyExc_AttributeError,
1713 "Can't get attribute %R on %R", name, obj);
1714 }
1715}
1716
1717
1718static PyObject *
1719getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1720{
1721 PyObject *dotted_path, *attr;
1722
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001723 if (allow_qualname) {
1724 dotted_path = get_dotted_path(obj, name);
1725 if (dotted_path == NULL)
1726 return NULL;
1727 attr = get_deep_attribute(obj, dotted_path, NULL);
1728 Py_DECREF(dotted_path);
1729 }
1730 else
1731 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001732 if (attr == NULL)
1733 reformat_attribute_error(obj, name);
1734 return attr;
1735}
1736
Eric Snow3f9eee62017-09-15 16:35:20 -06001737static int
1738_checkmodule(PyObject *module_name, PyObject *module,
1739 PyObject *global, PyObject *dotted_path)
1740{
1741 if (module == Py_None) {
1742 return -1;
1743 }
1744 if (PyUnicode_Check(module_name) &&
1745 _PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1746 return -1;
1747 }
1748
1749 PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1750 if (candidate == NULL) {
1751 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1752 PyErr_Clear();
1753 }
1754 return -1;
1755 }
1756 if (candidate != global) {
1757 Py_DECREF(candidate);
1758 return -1;
1759 }
1760 Py_DECREF(candidate);
1761 return 0;
1762}
1763
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001764static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001765whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001766{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001767 PyObject *module_name;
Eric Snow3f9eee62017-09-15 16:35:20 -06001768 PyObject *module = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001769 Py_ssize_t i;
Eric Snow3f9eee62017-09-15 16:35:20 -06001770 PyObject *modules;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001771 _Py_IDENTIFIER(__module__);
1772 _Py_IDENTIFIER(modules);
1773 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001774
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001775 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1776
1777 if (module_name == NULL) {
1778 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001779 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001780 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001781 }
1782 else {
1783 /* In some rare cases (e.g., bound methods of extension types),
1784 __module__ can be None. If it is so, then search sys.modules for
1785 the module of global. */
1786 if (module_name != Py_None)
1787 return module_name;
1788 Py_CLEAR(module_name);
1789 }
1790 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001791
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001792 /* Fallback on walking sys.modules */
Eric Snow3f9eee62017-09-15 16:35:20 -06001793 modules = _PySys_GetObjectId(&PyId_modules);
1794 if (modules == NULL) {
Victor Stinner1e53bba2013-07-16 22:26:05 +02001795 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001796 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001797 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001798 if (PyDict_CheckExact(modules)) {
1799 i = 0;
1800 while (PyDict_Next(modules, &i, &module_name, &module)) {
1801 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1802 Py_INCREF(module_name);
1803 return module_name;
1804 }
1805 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001806 return NULL;
Eric Snow3f9eee62017-09-15 16:35:20 -06001807 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001808 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001809 }
1810 else {
1811 PyObject *iterator = PyObject_GetIter(modules);
1812 if (iterator == NULL) {
1813 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001814 }
Eric Snow3f9eee62017-09-15 16:35:20 -06001815 while ((module_name = PyIter_Next(iterator))) {
1816 module = PyObject_GetItem(modules, module_name);
1817 if (module == NULL) {
1818 Py_DECREF(module_name);
1819 Py_DECREF(iterator);
1820 return NULL;
1821 }
1822 if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1823 Py_DECREF(module);
1824 Py_DECREF(iterator);
1825 return module_name;
1826 }
1827 Py_DECREF(module);
1828 Py_DECREF(module_name);
1829 if (PyErr_Occurred()) {
1830 Py_DECREF(iterator);
1831 return NULL;
1832 }
1833 }
1834 Py_DECREF(iterator);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001835 }
1836
1837 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001838 module_name = _PyUnicode_FromId(&PyId___main__);
Victor Stinneraf46eb82017-09-05 23:30:16 +02001839 Py_XINCREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001840 return module_name;
1841}
1842
1843/* fast_save_enter() and fast_save_leave() are guards against recursive
1844 objects when Pickler is used with the "fast mode" (i.e., with object
1845 memoization disabled). If the nesting of a list or dict object exceed
1846 FAST_NESTING_LIMIT, these guards will start keeping an internal
1847 reference to the seen list or dict objects and check whether these objects
1848 are recursive. These are not strictly necessary, since save() has a
1849 hard-coded recursion limit, but they give a nicer error message than the
1850 typical RuntimeError. */
1851static int
1852fast_save_enter(PicklerObject *self, PyObject *obj)
1853{
1854 /* if fast_nesting < 0, we're doing an error exit. */
1855 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1856 PyObject *key = NULL;
1857 if (self->fast_memo == NULL) {
1858 self->fast_memo = PyDict_New();
1859 if (self->fast_memo == NULL) {
1860 self->fast_nesting = -1;
1861 return 0;
1862 }
1863 }
1864 key = PyLong_FromVoidPtr(obj);
Mat Mf76231f2017-11-13 02:50:16 -05001865 if (key == NULL) {
1866 self->fast_nesting = -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001867 return 0;
Mat Mf76231f2017-11-13 02:50:16 -05001868 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001869 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001870 Py_DECREF(key);
1871 PyErr_Format(PyExc_ValueError,
1872 "fast mode: can't pickle cyclic objects "
1873 "including object type %.200s at %p",
1874 obj->ob_type->tp_name, obj);
1875 self->fast_nesting = -1;
1876 return 0;
1877 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001878 if (PyErr_Occurred()) {
Mat Mf76231f2017-11-13 02:50:16 -05001879 Py_DECREF(key);
1880 self->fast_nesting = -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001881 return 0;
1882 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001883 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1884 Py_DECREF(key);
1885 self->fast_nesting = -1;
1886 return 0;
1887 }
1888 Py_DECREF(key);
1889 }
1890 return 1;
1891}
1892
1893static int
1894fast_save_leave(PicklerObject *self, PyObject *obj)
1895{
1896 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1897 PyObject *key = PyLong_FromVoidPtr(obj);
1898 if (key == NULL)
1899 return 0;
1900 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1901 Py_DECREF(key);
1902 return 0;
1903 }
1904 Py_DECREF(key);
1905 }
1906 return 1;
1907}
1908
1909static int
1910save_none(PicklerObject *self, PyObject *obj)
1911{
1912 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001913 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001914 return -1;
1915
1916 return 0;
1917}
1918
1919static int
1920save_bool(PicklerObject *self, PyObject *obj)
1921{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001922 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001923 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001924 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001925 return -1;
1926 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001927 else {
1928 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1929 * so that unpicklers written before bools were introduced unpickle them
1930 * as ints, but unpicklers after can recognize that bools were intended.
1931 * Note that protocol 2 added direct ways to pickle bools.
1932 */
1933 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1934 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1935 return -1;
1936 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001937 return 0;
1938}
1939
1940static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001941save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001942{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001943 PyObject *repr = NULL;
1944 Py_ssize_t size;
1945 long val;
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001946 int overflow;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001947 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001948
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001949 val= PyLong_AsLongAndOverflow(obj, &overflow);
1950 if (!overflow && (sizeof(long) <= 4 ||
1951 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
1952 {
Larry Hastings61272b72014-01-07 12:41:53 -08001953 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001954
1955 Note: we can't use -0x80000000L in the above condition because some
1956 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1957 before applying the unary minus when sizeof(long) <= 4. The
1958 resulting value stays unsigned which is commonly not what we want,
1959 so MSVC happily warns us about it. However, that result would have
1960 been fine because we guard for sizeof(long) <= 4 which turns the
1961 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001962 char pdata[32];
1963 Py_ssize_t len = 0;
1964
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001965 if (self->bin) {
1966 pdata[1] = (unsigned char)(val & 0xff);
1967 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1968 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1969 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001970
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001971 if ((pdata[4] != 0) || (pdata[3] != 0)) {
1972 pdata[0] = BININT;
1973 len = 5;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001974 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001975 else if (pdata[2] != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976 pdata[0] = BININT2;
1977 len = 3;
1978 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001979 else {
1980 pdata[0] = BININT1;
1981 len = 2;
1982 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001983 }
1984 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001985 sprintf(pdata, "%c%ld\n", INT, val);
1986 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001987 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001988 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001989 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001990
1991 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001992 }
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02001993 assert(!PyErr_Occurred());
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001994
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001995 if (self->proto >= 2) {
1996 /* Linear-time pickling. */
1997 size_t nbits;
1998 size_t nbytes;
1999 unsigned char *pdata;
2000 char header[5];
2001 int i;
2002 int sign = _PyLong_Sign(obj);
2003
2004 if (sign == 0) {
2005 header[0] = LONG1;
2006 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002007 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002008 goto error;
2009 return 0;
2010 }
2011 nbits = _PyLong_NumBits(obj);
2012 if (nbits == (size_t)-1 && PyErr_Occurred())
2013 goto error;
2014 /* How many bytes do we need? There are nbits >> 3 full
2015 * bytes of data, and nbits & 7 leftover bits. If there
2016 * are any leftover bits, then we clearly need another
2017 * byte. Wnat's not so obvious is that we *probably*
2018 * need another byte even if there aren't any leftovers:
2019 * the most-significant bit of the most-significant byte
2020 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03002021 * opposite of the one we need. The exception is ints
2022 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002023 * its own 256's-complement, so has the right sign bit
2024 * even without the extra byte. That's a pain to check
2025 * for in advance, though, so we always grab an extra
2026 * byte at the start, and cut it back later if possible.
2027 */
2028 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01002029 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002030 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03002031 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 goto error;
2033 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002034 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002035 if (repr == NULL)
2036 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00002037 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002038 i = _PyLong_AsByteArray((PyLongObject *)obj,
2039 pdata, nbytes,
2040 1 /* little endian */ , 1 /* signed */ );
2041 if (i < 0)
2042 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03002043 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002044 * needed. This is so iff the MSB is all redundant sign
2045 * bits.
2046 */
2047 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02002048 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002049 pdata[nbytes - 1] == 0xff &&
2050 (pdata[nbytes - 2] & 0x80) != 0) {
2051 nbytes--;
2052 }
2053
2054 if (nbytes < 256) {
2055 header[0] = LONG1;
2056 header[1] = (unsigned char)nbytes;
2057 size = 2;
2058 }
2059 else {
2060 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002061 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002062 for (i = 1; i < 5; i++) {
2063 header[i] = (unsigned char)(size & 0xff);
2064 size >>= 8;
2065 }
2066 size = 5;
2067 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002068 if (_Pickler_Write(self, header, size) < 0 ||
2069 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002070 goto error;
2071 }
2072 else {
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +02002073 const char long_op = LONG;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002074 const char *string;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002075
Mark Dickinson8dd05142009-01-20 20:43:58 +00002076 /* proto < 2: write the repr and newline. This is quadratic-time (in
2077 the number of digits), in both directions. We add a trailing 'L'
2078 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002079
2080 repr = PyObject_Repr(obj);
2081 if (repr == NULL)
2082 goto error;
2083
Serhiy Storchaka06515832016-11-20 09:13:07 +02002084 string = PyUnicode_AsUTF8AndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002085 if (string == NULL)
2086 goto error;
2087
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002088 if (_Pickler_Write(self, &long_op, 1) < 0 ||
2089 _Pickler_Write(self, string, size) < 0 ||
2090 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002091 goto error;
2092 }
2093
2094 if (0) {
2095 error:
2096 status = -1;
2097 }
2098 Py_XDECREF(repr);
2099
2100 return status;
2101}
2102
2103static int
2104save_float(PicklerObject *self, PyObject *obj)
2105{
2106 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2107
2108 if (self->bin) {
2109 char pdata[9];
2110 pdata[0] = BINFLOAT;
2111 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
2112 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002113 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002114 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02002115 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002116 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00002117 int result = -1;
2118 char *buf = NULL;
2119 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002120
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002121 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002122 goto done;
2123
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002124 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002125 if (!buf) {
2126 PyErr_NoMemory();
2127 goto done;
2128 }
2129
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002130 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002131 goto done;
2132
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002133 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002134 goto done;
2135
2136 result = 0;
2137done:
2138 PyMem_Free(buf);
2139 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002140 }
2141
2142 return 0;
2143}
2144
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002145/* No-copy code-path to write large contiguous data directly into the
2146 underlying file object, bypassing the output_buffer of the Pickler. */
2147static int
2148_Pickler_write_large_bytes(
2149 PicklerObject *self, const char *header, Py_ssize_t header_size,
2150 PyObject *payload)
2151{
2152 assert(self->output_buffer != NULL);
2153 assert(self->write != NULL);
2154 PyObject *result;
2155
2156 /* Commit the previous frame. */
2157 if (_Pickler_CommitFrame(self)) {
2158 return -1;
2159 }
2160 /* Disable frameing temporarily */
2161 self->framing = 0;
2162
2163 if (_Pickler_Write(self, header, header_size) < 0) {
2164 return -1;
2165 }
2166 /* Dump the output buffer to the file. */
2167 if (_Pickler_FlushToFile(self) < 0) {
2168 return -1;
2169 }
2170
2171 /* Stream write the payload into the file without going through the
2172 output buffer. */
2173 result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
2174 if (result == NULL) {
2175 return -1;
2176 }
2177 Py_DECREF(result);
2178
2179 /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2180 if (_Pickler_ClearBuffer(self) < 0) {
2181 return -1;
2182 }
2183
2184 /* Re-enable framing for subsequent calls to _Pickler_Write. */
2185 self->framing = 1;
2186
2187 return 0;
2188}
2189
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002190static int
2191save_bytes(PicklerObject *self, PyObject *obj)
2192{
2193 if (self->proto < 3) {
2194 /* Older pickle protocols do not have an opcode for pickling bytes
2195 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002196 the __reduce__ method) to permit bytes object unpickling.
2197
2198 Here we use a hack to be compatible with Python 2. Since in Python
2199 2 'bytes' is just an alias for 'str' (which has different
2200 parameters than the actual bytes object), we use codecs.encode
2201 to create the appropriate 'str' object when unpickled using
2202 Python 2 *and* the appropriate 'bytes' object when unpickled
2203 using Python 3. Again this is a hack and we don't need to do this
2204 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002205 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002206 int status;
2207
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002208 if (PyBytes_GET_SIZE(obj) == 0) {
2209 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2210 }
2211 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002212 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002213 PyObject *unicode_str =
2214 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2215 PyBytes_GET_SIZE(obj),
2216 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002217 _Py_IDENTIFIER(latin1);
2218
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002219 if (unicode_str == NULL)
2220 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002221 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002222 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002223 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002224 Py_DECREF(unicode_str);
2225 }
2226
2227 if (reduce_value == NULL)
2228 return -1;
2229
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002230 /* save_reduce() will memoize the object automatically. */
2231 status = save_reduce(self, reduce_value, obj);
2232 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002233 return status;
2234 }
2235 else {
2236 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002237 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002238 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002239
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002240 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002241 if (size < 0)
2242 return -1;
2243
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002244 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002245 header[0] = SHORT_BINBYTES;
2246 header[1] = (unsigned char)size;
2247 len = 2;
2248 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002249 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002250 header[0] = BINBYTES;
2251 header[1] = (unsigned char)(size & 0xff);
2252 header[2] = (unsigned char)((size >> 8) & 0xff);
2253 header[3] = (unsigned char)((size >> 16) & 0xff);
2254 header[4] = (unsigned char)((size >> 24) & 0xff);
2255 len = 5;
2256 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002257 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002258 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002259 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002260 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002261 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002262 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002263 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002264 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002265 return -1; /* string too large */
2266 }
2267
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002268 if (size < FRAME_SIZE_TARGET || self->write == NULL) {
2269 if (_Pickler_Write(self, header, len) < 0) {
2270 return -1;
2271 }
2272 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) {
2273 return -1;
2274 }
2275 }
2276 else {
2277 /* Bypass the in-memory buffer to directly stream large data
2278 into the underlying file object. */
2279 if (_Pickler_write_large_bytes(self, header, len, obj) < 0) {
2280 return -1;
2281 }
2282 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002283
2284 if (memo_put(self, obj) < 0)
2285 return -1;
2286
2287 return 0;
2288 }
2289}
2290
2291/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2292 backslash and newline characters to \uXXXX escapes. */
2293static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002294raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002297 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002298 void *data;
2299 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002300 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002301
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002302 if (PyUnicode_READY(obj))
2303 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002304
Victor Stinner358af132015-10-12 22:36:57 +02002305 _PyBytesWriter_Init(&writer);
2306
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002307 size = PyUnicode_GET_LENGTH(obj);
2308 data = PyUnicode_DATA(obj);
2309 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002310
Victor Stinner358af132015-10-12 22:36:57 +02002311 p = _PyBytesWriter_Alloc(&writer, size);
2312 if (p == NULL)
2313 goto error;
2314 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002315
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002316 for (i=0; i < size; i++) {
2317 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002318 /* Map 32-bit characters to '\Uxxxxxxxx' */
2319 if (ch >= 0x10000) {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002320 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002321 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2322 if (p == NULL)
2323 goto error;
2324
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002325 *p++ = '\\';
2326 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002327 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2328 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2329 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2330 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2331 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2332 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2333 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2334 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002335 }
Victor Stinner358af132015-10-12 22:36:57 +02002336 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002337 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002338 /* -1: subtract 1 preallocated byte */
Victor Stinner358af132015-10-12 22:36:57 +02002339 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2340 if (p == NULL)
2341 goto error;
2342
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002343 *p++ = '\\';
2344 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002345 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2346 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2347 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2348 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002349 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002350 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351 else
2352 *p++ = (char) ch;
2353 }
Victor Stinner358af132015-10-12 22:36:57 +02002354
2355 return _PyBytesWriter_Finish(&writer, p);
2356
2357error:
2358 _PyBytesWriter_Dealloc(&writer);
2359 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002360}
2361
2362static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002363write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002364{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002365 char header[9];
2366 Py_ssize_t len;
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002367 PyObject *mem;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002368
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002369 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002370 if (size <= 0xff && self->proto >= 4) {
2371 header[0] = SHORT_BINUNICODE;
2372 header[1] = (unsigned char)(size & 0xff);
2373 len = 2;
2374 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002375 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002376 header[0] = BINUNICODE;
2377 header[1] = (unsigned char)(size & 0xff);
2378 header[2] = (unsigned char)((size >> 8) & 0xff);
2379 header[3] = (unsigned char)((size >> 16) & 0xff);
2380 header[4] = (unsigned char)((size >> 24) & 0xff);
2381 len = 5;
2382 }
2383 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002384 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002385 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002386 len = 9;
2387 }
2388 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002389 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002390 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002391 return -1;
2392 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002393
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +01002394 if (size < FRAME_SIZE_TARGET || self->write == NULL) {
2395 if (_Pickler_Write(self, header, len) < 0) {
2396 return -1;
2397 }
2398 if (_Pickler_Write(self, data, size) < 0) {
2399 return -1;
2400 }
2401 }
2402 else {
2403 /* Bypass the in-memory buffer to directly stream large data
2404 into the underlying file object. */
2405 mem = PyMemoryView_FromMemory((char *) data, size, PyBUF_READ);
2406 if (mem == NULL) {
2407 return -1;
2408 }
2409 if (_Pickler_write_large_bytes(self, header, len, mem) < 0) {
2410 Py_DECREF(mem);
2411 return -1;
2412 }
2413 Py_DECREF(mem);
2414 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002415 return 0;
2416}
2417
2418static int
2419write_unicode_binary(PicklerObject *self, PyObject *obj)
2420{
2421 PyObject *encoded = NULL;
2422 Py_ssize_t size;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002423 const char *data;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002424 int r;
2425
2426 if (PyUnicode_READY(obj))
2427 return -1;
2428
2429 data = PyUnicode_AsUTF8AndSize(obj, &size);
2430 if (data != NULL)
2431 return write_utf8(self, data, size);
2432
2433 /* Issue #8383: for strings with lone surrogates, fallback on the
2434 "surrogatepass" error handler. */
2435 PyErr_Clear();
2436 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2437 if (encoded == NULL)
2438 return -1;
2439
2440 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2441 PyBytes_GET_SIZE(encoded));
2442 Py_DECREF(encoded);
2443 return r;
2444}
2445
2446static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002447save_unicode(PicklerObject *self, PyObject *obj)
2448{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002449 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002450 if (write_unicode_binary(self, obj) < 0)
2451 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002452 }
2453 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002454 PyObject *encoded;
2455 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002456 const char unicode_op = UNICODE;
2457
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002458 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002459 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002460 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002461
Antoine Pitrou299978d2013-04-07 17:38:11 +02002462 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2463 Py_DECREF(encoded);
2464 return -1;
2465 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002466
2467 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002468 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2469 Py_DECREF(encoded);
2470 return -1;
2471 }
2472 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002473
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002474 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002475 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002476 }
2477 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002478 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002479
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002480 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002481}
2482
2483/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2484static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002485store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002486{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002487 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002488
2489 assert(PyTuple_Size(t) == len);
2490
2491 for (i = 0; i < len; i++) {
2492 PyObject *element = PyTuple_GET_ITEM(t, i);
2493
2494 if (element == NULL)
2495 return -1;
2496 if (save(self, element, 0) < 0)
2497 return -1;
2498 }
2499
2500 return 0;
2501}
2502
2503/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2504 * used across protocols to minimize the space needed to pickle them.
2505 * Tuples are also the only builtin immutable type that can be recursive
2506 * (a tuple can be reached from itself), and that requires some subtle
2507 * magic so that it works in all cases. IOW, this is a long routine.
2508 */
2509static int
2510save_tuple(PicklerObject *self, PyObject *obj)
2511{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002512 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002513
2514 const char mark_op = MARK;
2515 const char tuple_op = TUPLE;
2516 const char pop_op = POP;
2517 const char pop_mark_op = POP_MARK;
2518 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2519
2520 if ((len = PyTuple_Size(obj)) < 0)
2521 return -1;
2522
2523 if (len == 0) {
2524 char pdata[2];
2525
2526 if (self->proto) {
2527 pdata[0] = EMPTY_TUPLE;
2528 len = 1;
2529 }
2530 else {
2531 pdata[0] = MARK;
2532 pdata[1] = TUPLE;
2533 len = 2;
2534 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002535 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002536 return -1;
2537 return 0;
2538 }
2539
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002540 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002541 * saving the tuple elements, the tuple must be recursive, in
2542 * which case we'll pop everything we put on the stack, and fetch
2543 * its value from the memo.
2544 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002545 if (len <= 3 && self->proto >= 2) {
2546 /* Use TUPLE{1,2,3} opcodes. */
2547 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002548 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002549
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002550 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002551 /* pop the len elements */
2552 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002553 if (_Pickler_Write(self, &pop_op, 1) < 0)
2554 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002555 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002556 if (memo_get(self, obj) < 0)
2557 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002558
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002559 return 0;
2560 }
2561 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002562 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2563 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002564 }
2565 goto memoize;
2566 }
2567
2568 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2569 * Generate MARK e1 e2 ... TUPLE
2570 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002571 if (_Pickler_Write(self, &mark_op, 1) < 0)
2572 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002573
2574 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002575 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002576
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002577 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002578 /* pop the stack stuff we pushed */
2579 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002580 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2581 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002582 }
2583 else {
2584 /* Note that we pop one more than len, to remove
2585 * the MARK too.
2586 */
2587 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002588 if (_Pickler_Write(self, &pop_op, 1) < 0)
2589 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002590 }
2591 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002592 if (memo_get(self, obj) < 0)
2593 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002594
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002595 return 0;
2596 }
2597 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002598 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2599 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002600 }
2601
2602 memoize:
2603 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002604 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002605
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002606 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002607}
2608
2609/* iter is an iterator giving items, and we batch up chunks of
2610 * MARK item item ... item APPENDS
2611 * opcode sequences. Calling code should have arranged to first create an
2612 * empty list, or list-like object, for the APPENDS to operate on.
2613 * Returns 0 on success, <0 on error.
2614 */
2615static int
2616batch_list(PicklerObject *self, PyObject *iter)
2617{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002618 PyObject *obj = NULL;
2619 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002620 int i, n;
2621
2622 const char mark_op = MARK;
2623 const char append_op = APPEND;
2624 const char appends_op = APPENDS;
2625
2626 assert(iter != NULL);
2627
2628 /* XXX: I think this function could be made faster by avoiding the
2629 iterator interface and fetching objects directly from list using
2630 PyList_GET_ITEM.
2631 */
2632
2633 if (self->proto == 0) {
2634 /* APPENDS isn't available; do one at a time. */
2635 for (;;) {
2636 obj = PyIter_Next(iter);
2637 if (obj == NULL) {
2638 if (PyErr_Occurred())
2639 return -1;
2640 break;
2641 }
2642 i = save(self, obj, 0);
2643 Py_DECREF(obj);
2644 if (i < 0)
2645 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002646 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002647 return -1;
2648 }
2649 return 0;
2650 }
2651
2652 /* proto > 0: write in batches of BATCHSIZE. */
2653 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002654 /* Get first item */
2655 firstitem = PyIter_Next(iter);
2656 if (firstitem == NULL) {
2657 if (PyErr_Occurred())
2658 goto error;
2659
2660 /* nothing more to add */
2661 break;
2662 }
2663
2664 /* Try to get a second item */
2665 obj = PyIter_Next(iter);
2666 if (obj == NULL) {
2667 if (PyErr_Occurred())
2668 goto error;
2669
2670 /* Only one item to write */
2671 if (save(self, firstitem, 0) < 0)
2672 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002673 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002674 goto error;
2675 Py_CLEAR(firstitem);
2676 break;
2677 }
2678
2679 /* More than one item to write */
2680
2681 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002682 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002683 goto error;
2684
2685 if (save(self, firstitem, 0) < 0)
2686 goto error;
2687 Py_CLEAR(firstitem);
2688 n = 1;
2689
2690 /* Fetch and save up to BATCHSIZE items */
2691 while (obj) {
2692 if (save(self, obj, 0) < 0)
2693 goto error;
2694 Py_CLEAR(obj);
2695 n += 1;
2696
2697 if (n == BATCHSIZE)
2698 break;
2699
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002700 obj = PyIter_Next(iter);
2701 if (obj == NULL) {
2702 if (PyErr_Occurred())
2703 goto error;
2704 break;
2705 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002706 }
2707
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002708 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002709 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002710
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002711 } while (n == BATCHSIZE);
2712 return 0;
2713
2714 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002715 Py_XDECREF(firstitem);
2716 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002717 return -1;
2718}
2719
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002720/* This is a variant of batch_list() above, specialized for lists (with no
2721 * support for list subclasses). Like batch_list(), we batch up chunks of
2722 * MARK item item ... item APPENDS
2723 * opcode sequences. Calling code should have arranged to first create an
2724 * empty list, or list-like object, for the APPENDS to operate on.
2725 * Returns 0 on success, -1 on error.
2726 *
2727 * This version is considerably faster than batch_list(), if less general.
2728 *
2729 * Note that this only works for protocols > 0.
2730 */
2731static int
2732batch_list_exact(PicklerObject *self, PyObject *obj)
2733{
2734 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002735 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002736
2737 const char append_op = APPEND;
2738 const char appends_op = APPENDS;
2739 const char mark_op = MARK;
2740
2741 assert(obj != NULL);
2742 assert(self->proto > 0);
2743 assert(PyList_CheckExact(obj));
2744
2745 if (PyList_GET_SIZE(obj) == 1) {
2746 item = PyList_GET_ITEM(obj, 0);
2747 if (save(self, item, 0) < 0)
2748 return -1;
2749 if (_Pickler_Write(self, &append_op, 1) < 0)
2750 return -1;
2751 return 0;
2752 }
2753
2754 /* Write in batches of BATCHSIZE. */
2755 total = 0;
2756 do {
2757 this_batch = 0;
2758 if (_Pickler_Write(self, &mark_op, 1) < 0)
2759 return -1;
2760 while (total < PyList_GET_SIZE(obj)) {
2761 item = PyList_GET_ITEM(obj, total);
2762 if (save(self, item, 0) < 0)
2763 return -1;
2764 total++;
2765 if (++this_batch == BATCHSIZE)
2766 break;
2767 }
2768 if (_Pickler_Write(self, &appends_op, 1) < 0)
2769 return -1;
2770
2771 } while (total < PyList_GET_SIZE(obj));
2772
2773 return 0;
2774}
2775
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002776static int
2777save_list(PicklerObject *self, PyObject *obj)
2778{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002779 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002780 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002781 int status = 0;
2782
2783 if (self->fast && !fast_save_enter(self, obj))
2784 goto error;
2785
2786 /* Create an empty list. */
2787 if (self->bin) {
2788 header[0] = EMPTY_LIST;
2789 len = 1;
2790 }
2791 else {
2792 header[0] = MARK;
2793 header[1] = LIST;
2794 len = 2;
2795 }
2796
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002797 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002798 goto error;
2799
2800 /* Get list length, and bow out early if empty. */
2801 if ((len = PyList_Size(obj)) < 0)
2802 goto error;
2803
2804 if (memo_put(self, obj) < 0)
2805 goto error;
2806
2807 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002808 /* Materialize the list elements. */
2809 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002810 if (Py_EnterRecursiveCall(" while pickling an object"))
2811 goto error;
2812 status = batch_list_exact(self, obj);
2813 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002814 } else {
2815 PyObject *iter = PyObject_GetIter(obj);
2816 if (iter == NULL)
2817 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002818
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002819 if (Py_EnterRecursiveCall(" while pickling an object")) {
2820 Py_DECREF(iter);
2821 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002822 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002823 status = batch_list(self, iter);
2824 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002825 Py_DECREF(iter);
2826 }
2827 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002828 if (0) {
2829 error:
2830 status = -1;
2831 }
2832
2833 if (self->fast && !fast_save_leave(self, obj))
2834 status = -1;
2835
2836 return status;
2837}
2838
2839/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2840 * MARK key value ... key value SETITEMS
2841 * opcode sequences. Calling code should have arranged to first create an
2842 * empty dict, or dict-like object, for the SETITEMS to operate on.
2843 * Returns 0 on success, <0 on error.
2844 *
2845 * This is very much like batch_list(). The difference between saving
2846 * elements directly, and picking apart two-tuples, is so long-winded at
2847 * the C level, though, that attempts to combine these routines were too
2848 * ugly to bear.
2849 */
2850static int
2851batch_dict(PicklerObject *self, PyObject *iter)
2852{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002853 PyObject *obj = NULL;
2854 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002855 int i, n;
2856
2857 const char mark_op = MARK;
2858 const char setitem_op = SETITEM;
2859 const char setitems_op = SETITEMS;
2860
2861 assert(iter != NULL);
2862
2863 if (self->proto == 0) {
2864 /* SETITEMS isn't available; do one at a time. */
2865 for (;;) {
2866 obj = PyIter_Next(iter);
2867 if (obj == NULL) {
2868 if (PyErr_Occurred())
2869 return -1;
2870 break;
2871 }
2872 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2873 PyErr_SetString(PyExc_TypeError, "dict items "
2874 "iterator must return 2-tuples");
2875 return -1;
2876 }
2877 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2878 if (i >= 0)
2879 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2880 Py_DECREF(obj);
2881 if (i < 0)
2882 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002883 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002884 return -1;
2885 }
2886 return 0;
2887 }
2888
2889 /* proto > 0: write in batches of BATCHSIZE. */
2890 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002891 /* Get first item */
2892 firstitem = PyIter_Next(iter);
2893 if (firstitem == NULL) {
2894 if (PyErr_Occurred())
2895 goto error;
2896
2897 /* nothing more to add */
2898 break;
2899 }
2900 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2901 PyErr_SetString(PyExc_TypeError, "dict items "
2902 "iterator must return 2-tuples");
2903 goto error;
2904 }
2905
2906 /* Try to get a second item */
2907 obj = PyIter_Next(iter);
2908 if (obj == NULL) {
2909 if (PyErr_Occurred())
2910 goto error;
2911
2912 /* Only one item to write */
2913 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2914 goto error;
2915 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2916 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002917 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002918 goto error;
2919 Py_CLEAR(firstitem);
2920 break;
2921 }
2922
2923 /* More than one item to write */
2924
2925 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002926 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002927 goto error;
2928
2929 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2930 goto error;
2931 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2932 goto error;
2933 Py_CLEAR(firstitem);
2934 n = 1;
2935
2936 /* Fetch and save up to BATCHSIZE items */
2937 while (obj) {
2938 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2939 PyErr_SetString(PyExc_TypeError, "dict items "
2940 "iterator must return 2-tuples");
2941 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002942 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002943 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2944 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2945 goto error;
2946 Py_CLEAR(obj);
2947 n += 1;
2948
2949 if (n == BATCHSIZE)
2950 break;
2951
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002952 obj = PyIter_Next(iter);
2953 if (obj == NULL) {
2954 if (PyErr_Occurred())
2955 goto error;
2956 break;
2957 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002958 }
2959
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002960 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002961 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002962
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002963 } while (n == BATCHSIZE);
2964 return 0;
2965
2966 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002967 Py_XDECREF(firstitem);
2968 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002969 return -1;
2970}
2971
Collin Winter5c9b02d2009-05-25 05:43:30 +00002972/* This is a variant of batch_dict() above that specializes for dicts, with no
2973 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2974 * MARK key value ... key value SETITEMS
2975 * opcode sequences. Calling code should have arranged to first create an
2976 * empty dict, or dict-like object, for the SETITEMS to operate on.
2977 * Returns 0 on success, -1 on error.
2978 *
2979 * Note that this currently doesn't work for protocol 0.
2980 */
2981static int
2982batch_dict_exact(PicklerObject *self, PyObject *obj)
2983{
2984 PyObject *key = NULL, *value = NULL;
2985 int i;
2986 Py_ssize_t dict_size, ppos = 0;
2987
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002988 const char mark_op = MARK;
2989 const char setitem_op = SETITEM;
2990 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002991
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002992 assert(obj != NULL && PyDict_CheckExact(obj));
Collin Winter5c9b02d2009-05-25 05:43:30 +00002993 assert(self->proto > 0);
2994
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002995 dict_size = PyDict_GET_SIZE(obj);
Collin Winter5c9b02d2009-05-25 05:43:30 +00002996
2997 /* Special-case len(d) == 1 to save space. */
2998 if (dict_size == 1) {
2999 PyDict_Next(obj, &ppos, &key, &value);
3000 if (save(self, key, 0) < 0)
3001 return -1;
3002 if (save(self, value, 0) < 0)
3003 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003004 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003005 return -1;
3006 return 0;
3007 }
3008
3009 /* Write in batches of BATCHSIZE. */
3010 do {
3011 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003012 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003013 return -1;
3014 while (PyDict_Next(obj, &ppos, &key, &value)) {
3015 if (save(self, key, 0) < 0)
3016 return -1;
3017 if (save(self, value, 0) < 0)
3018 return -1;
3019 if (++i == BATCHSIZE)
3020 break;
3021 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003022 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00003023 return -1;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003024 if (PyDict_GET_SIZE(obj) != dict_size) {
Collin Winter5c9b02d2009-05-25 05:43:30 +00003025 PyErr_Format(
3026 PyExc_RuntimeError,
3027 "dictionary changed size during iteration");
3028 return -1;
3029 }
3030
3031 } while (i == BATCHSIZE);
3032 return 0;
3033}
3034
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003035static int
3036save_dict(PicklerObject *self, PyObject *obj)
3037{
3038 PyObject *items, *iter;
3039 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003040 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003041 int status = 0;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003042 assert(PyDict_Check(obj));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003043
3044 if (self->fast && !fast_save_enter(self, obj))
3045 goto error;
3046
3047 /* Create an empty dict. */
3048 if (self->bin) {
3049 header[0] = EMPTY_DICT;
3050 len = 1;
3051 }
3052 else {
3053 header[0] = MARK;
3054 header[1] = DICT;
3055 len = 2;
3056 }
3057
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003058 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003059 goto error;
3060
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003061 if (memo_put(self, obj) < 0)
3062 goto error;
3063
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02003064 if (PyDict_GET_SIZE(obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003065 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00003066 if (PyDict_CheckExact(obj) && self->proto > 0) {
3067 /* We can take certain shortcuts if we know this is a dict and
3068 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003069 if (Py_EnterRecursiveCall(" while pickling an object"))
3070 goto error;
3071 status = batch_dict_exact(self, obj);
3072 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003073 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003074 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003075
Victor Stinnerad8c83a2016-09-05 17:53:15 -07003076 items = _PyObject_CallMethodId(obj, &PyId_items, NULL);
Collin Winter5c9b02d2009-05-25 05:43:30 +00003077 if (items == NULL)
3078 goto error;
3079 iter = PyObject_GetIter(items);
3080 Py_DECREF(items);
3081 if (iter == NULL)
3082 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003083 if (Py_EnterRecursiveCall(" while pickling an object")) {
3084 Py_DECREF(iter);
3085 goto error;
3086 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00003087 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003088 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00003089 Py_DECREF(iter);
3090 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003091 }
3092
3093 if (0) {
3094 error:
3095 status = -1;
3096 }
3097
3098 if (self->fast && !fast_save_leave(self, obj))
3099 status = -1;
3100
3101 return status;
3102}
3103
3104static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003105save_set(PicklerObject *self, PyObject *obj)
3106{
3107 PyObject *item;
3108 int i;
3109 Py_ssize_t set_size, ppos = 0;
3110 Py_hash_t hash;
3111
3112 const char empty_set_op = EMPTY_SET;
3113 const char mark_op = MARK;
3114 const char additems_op = ADDITEMS;
3115
3116 if (self->proto < 4) {
3117 PyObject *items;
3118 PyObject *reduce_value;
3119 int status;
3120
3121 items = PySequence_List(obj);
3122 if (items == NULL) {
3123 return -1;
3124 }
3125 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3126 Py_DECREF(items);
3127 if (reduce_value == NULL) {
3128 return -1;
3129 }
3130 /* save_reduce() will memoize the object automatically. */
3131 status = save_reduce(self, reduce_value, obj);
3132 Py_DECREF(reduce_value);
3133 return status;
3134 }
3135
3136 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3137 return -1;
3138
3139 if (memo_put(self, obj) < 0)
3140 return -1;
3141
3142 set_size = PySet_GET_SIZE(obj);
3143 if (set_size == 0)
3144 return 0; /* nothing to do */
3145
3146 /* Write in batches of BATCHSIZE. */
3147 do {
3148 i = 0;
3149 if (_Pickler_Write(self, &mark_op, 1) < 0)
3150 return -1;
3151 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3152 if (save(self, item, 0) < 0)
3153 return -1;
3154 if (++i == BATCHSIZE)
3155 break;
3156 }
3157 if (_Pickler_Write(self, &additems_op, 1) < 0)
3158 return -1;
3159 if (PySet_GET_SIZE(obj) != set_size) {
3160 PyErr_Format(
3161 PyExc_RuntimeError,
3162 "set changed size during iteration");
3163 return -1;
3164 }
3165 } while (i == BATCHSIZE);
3166
3167 return 0;
3168}
3169
3170static int
3171save_frozenset(PicklerObject *self, PyObject *obj)
3172{
3173 PyObject *iter;
3174
3175 const char mark_op = MARK;
3176 const char frozenset_op = FROZENSET;
3177
3178 if (self->fast && !fast_save_enter(self, obj))
3179 return -1;
3180
3181 if (self->proto < 4) {
3182 PyObject *items;
3183 PyObject *reduce_value;
3184 int status;
3185
3186 items = PySequence_List(obj);
3187 if (items == NULL) {
3188 return -1;
3189 }
3190 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3191 items);
3192 Py_DECREF(items);
3193 if (reduce_value == NULL) {
3194 return -1;
3195 }
3196 /* save_reduce() will memoize the object automatically. */
3197 status = save_reduce(self, reduce_value, obj);
3198 Py_DECREF(reduce_value);
3199 return status;
3200 }
3201
3202 if (_Pickler_Write(self, &mark_op, 1) < 0)
3203 return -1;
3204
3205 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003206 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003207 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003208 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003209 for (;;) {
3210 PyObject *item;
3211
3212 item = PyIter_Next(iter);
3213 if (item == NULL) {
3214 if (PyErr_Occurred()) {
3215 Py_DECREF(iter);
3216 return -1;
3217 }
3218 break;
3219 }
3220 if (save(self, item, 0) < 0) {
3221 Py_DECREF(item);
3222 Py_DECREF(iter);
3223 return -1;
3224 }
3225 Py_DECREF(item);
3226 }
3227 Py_DECREF(iter);
3228
3229 /* If the object is already in the memo, this means it is
3230 recursive. In this case, throw away everything we put on the
3231 stack, and fetch the object back from the memo. */
3232 if (PyMemoTable_Get(self->memo, obj)) {
3233 const char pop_mark_op = POP_MARK;
3234
3235 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3236 return -1;
3237 if (memo_get(self, obj) < 0)
3238 return -1;
3239 return 0;
3240 }
3241
3242 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3243 return -1;
3244 if (memo_put(self, obj) < 0)
3245 return -1;
3246
3247 return 0;
3248}
3249
3250static int
3251fix_imports(PyObject **module_name, PyObject **global_name)
3252{
3253 PyObject *key;
3254 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003255 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003256
3257 key = PyTuple_Pack(2, *module_name, *global_name);
3258 if (key == NULL)
3259 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003260 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003261 Py_DECREF(key);
3262 if (item) {
3263 PyObject *fixed_module_name;
3264 PyObject *fixed_global_name;
3265
3266 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3267 PyErr_Format(PyExc_RuntimeError,
3268 "_compat_pickle.REVERSE_NAME_MAPPING values "
3269 "should be 2-tuples, not %.200s",
3270 Py_TYPE(item)->tp_name);
3271 return -1;
3272 }
3273 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3274 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3275 if (!PyUnicode_Check(fixed_module_name) ||
3276 !PyUnicode_Check(fixed_global_name)) {
3277 PyErr_Format(PyExc_RuntimeError,
3278 "_compat_pickle.REVERSE_NAME_MAPPING values "
3279 "should be pairs of str, not (%.200s, %.200s)",
3280 Py_TYPE(fixed_module_name)->tp_name,
3281 Py_TYPE(fixed_global_name)->tp_name);
3282 return -1;
3283 }
3284
3285 Py_CLEAR(*module_name);
3286 Py_CLEAR(*global_name);
3287 Py_INCREF(fixed_module_name);
3288 Py_INCREF(fixed_global_name);
3289 *module_name = fixed_module_name;
3290 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003291 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003292 }
3293 else if (PyErr_Occurred()) {
3294 return -1;
3295 }
3296
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003297 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003298 if (item) {
3299 if (!PyUnicode_Check(item)) {
3300 PyErr_Format(PyExc_RuntimeError,
3301 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3302 "should be strings, not %.200s",
3303 Py_TYPE(item)->tp_name);
3304 return -1;
3305 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003306 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003307 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003308 }
3309 else if (PyErr_Occurred()) {
3310 return -1;
3311 }
3312
3313 return 0;
3314}
3315
3316static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003317save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3318{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003319 PyObject *global_name = NULL;
3320 PyObject *module_name = NULL;
3321 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003322 PyObject *parent = NULL;
3323 PyObject *dotted_path = NULL;
3324 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003325 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003326 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003327 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003328 _Py_IDENTIFIER(__name__);
3329 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003330
3331 const char global_op = GLOBAL;
3332
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003333 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003334 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003335 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003336 }
3337 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003338 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3339 if (global_name == NULL) {
3340 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3341 goto error;
3342 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003343 }
3344 if (global_name == NULL) {
3345 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3346 if (global_name == NULL)
3347 goto error;
3348 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003349 }
3350
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003351 dotted_path = get_dotted_path(module, global_name);
3352 if (dotted_path == NULL)
3353 goto error;
3354 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003355 if (module_name == NULL)
3356 goto error;
3357
3358 /* XXX: Change to use the import C API directly with level=0 to disallow
3359 relative imports.
3360
3361 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3362 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3363 custom import functions (IMHO, this would be a nice security
3364 feature). The import C API would need to be extended to support the
3365 extra parameters of __import__ to fix that. */
3366 module = PyImport_Import(module_name);
3367 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003368 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003369 "Can't pickle %R: import of module %R failed",
3370 obj, module_name);
3371 goto error;
3372 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003373 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3374 Py_INCREF(lastname);
3375 cls = get_deep_attribute(module, dotted_path, &parent);
3376 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003377 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003378 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003379 "Can't pickle %R: attribute lookup %S on %S failed",
3380 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003381 goto error;
3382 }
3383 if (cls != obj) {
3384 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003385 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003386 "Can't pickle %R: it's not the same object as %S.%S",
3387 obj, module_name, global_name);
3388 goto error;
3389 }
3390 Py_DECREF(cls);
3391
3392 if (self->proto >= 2) {
3393 /* See whether this is in the extension registry, and if
3394 * so generate an EXT opcode.
3395 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003396 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003397 PyObject *code_obj; /* extension code as Python object */
3398 long code; /* extension code as C value */
3399 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003400 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003401
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003402 extension_key = PyTuple_Pack(2, module_name, global_name);
3403 if (extension_key == NULL) {
3404 goto error;
3405 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003406 code_obj = PyDict_GetItemWithError(st->extension_registry,
3407 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003408 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003409 /* The object is not registered in the extension registry.
3410 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003411 if (code_obj == NULL) {
3412 if (PyErr_Occurred()) {
3413 goto error;
3414 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003415 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003416 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003417
3418 /* XXX: pickle.py doesn't check neither the type, nor the range
3419 of the value returned by the extension_registry. It should for
3420 consistency. */
3421
3422 /* Verify code_obj has the right type and value. */
3423 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003424 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003425 "Can't pickle %R: extension code %R isn't an integer",
3426 obj, code_obj);
3427 goto error;
3428 }
3429 code = PyLong_AS_LONG(code_obj);
3430 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003431 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003432 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3433 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003434 goto error;
3435 }
3436
3437 /* Generate an EXT opcode. */
3438 if (code <= 0xff) {
3439 pdata[0] = EXT1;
3440 pdata[1] = (unsigned char)code;
3441 n = 2;
3442 }
3443 else if (code <= 0xffff) {
3444 pdata[0] = EXT2;
3445 pdata[1] = (unsigned char)(code & 0xff);
3446 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3447 n = 3;
3448 }
3449 else {
3450 pdata[0] = EXT4;
3451 pdata[1] = (unsigned char)(code & 0xff);
3452 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3453 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3454 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3455 n = 5;
3456 }
3457
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003458 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003459 goto error;
3460 }
3461 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003462 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003463 if (parent == module) {
3464 Py_INCREF(lastname);
3465 Py_DECREF(global_name);
3466 global_name = lastname;
3467 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003468 if (self->proto >= 4) {
3469 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003470
Christian Heimese8b1ba12013-11-23 21:13:39 +01003471 if (save(self, module_name, 0) < 0)
3472 goto error;
3473 if (save(self, global_name, 0) < 0)
3474 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003475
3476 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3477 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003478 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003479 else if (parent != module) {
3480 PickleState *st = _Pickle_GetGlobalState();
3481 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3482 st->getattr, parent, lastname);
3483 status = save_reduce(self, reduce_value, NULL);
3484 Py_DECREF(reduce_value);
3485 if (status < 0)
3486 goto error;
3487 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003488 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003489 /* Generate a normal global opcode if we are using a pickle
3490 protocol < 4, or if the object is not registered in the
3491 extension registry. */
3492 PyObject *encoded;
3493 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003494
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003495 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003496 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003497
3498 /* For protocol < 3 and if the user didn't request against doing
3499 so, we convert module names to the old 2.x module names. */
3500 if (self->proto < 3 && self->fix_imports) {
3501 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003502 goto error;
3503 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003504 }
3505
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003506 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3507 both the module name and the global name using UTF-8. We do so
3508 only when we are using the pickle protocol newer than version
3509 3. This is to ensure compatibility with older Unpickler running
3510 on Python 2.x. */
3511 if (self->proto == 3) {
3512 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003513 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003514 else {
3515 unicode_encoder = PyUnicode_AsASCIIString;
3516 }
3517 encoded = unicode_encoder(module_name);
3518 if (encoded == NULL) {
3519 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003520 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003521 "can't pickle module identifier '%S' using "
3522 "pickle protocol %i",
3523 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003524 goto error;
3525 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003526 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3527 PyBytes_GET_SIZE(encoded)) < 0) {
3528 Py_DECREF(encoded);
3529 goto error;
3530 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003531 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003532 if(_Pickler_Write(self, "\n", 1) < 0)
3533 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003534
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003535 /* Save the name of the module. */
3536 encoded = unicode_encoder(global_name);
3537 if (encoded == NULL) {
3538 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003539 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003540 "can't pickle global identifier '%S' using "
3541 "pickle protocol %i",
3542 global_name, self->proto);
3543 goto error;
3544 }
3545 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3546 PyBytes_GET_SIZE(encoded)) < 0) {
3547 Py_DECREF(encoded);
3548 goto error;
3549 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003550 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003551 if (_Pickler_Write(self, "\n", 1) < 0)
3552 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003553 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003554 /* Memoize the object. */
3555 if (memo_put(self, obj) < 0)
3556 goto error;
3557 }
3558
3559 if (0) {
3560 error:
3561 status = -1;
3562 }
3563 Py_XDECREF(module_name);
3564 Py_XDECREF(global_name);
3565 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003566 Py_XDECREF(parent);
3567 Py_XDECREF(dotted_path);
3568 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003569
3570 return status;
3571}
3572
3573static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003574save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3575{
3576 PyObject *reduce_value;
3577 int status;
3578
3579 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3580 if (reduce_value == NULL) {
3581 return -1;
3582 }
3583 status = save_reduce(self, reduce_value, obj);
3584 Py_DECREF(reduce_value);
3585 return status;
3586}
3587
3588static int
3589save_type(PicklerObject *self, PyObject *obj)
3590{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003591 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003592 return save_singleton_type(self, obj, Py_None);
3593 }
3594 else if (obj == (PyObject *)&PyEllipsis_Type) {
3595 return save_singleton_type(self, obj, Py_Ellipsis);
3596 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003597 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003598 return save_singleton_type(self, obj, Py_NotImplemented);
3599 }
3600 return save_global(self, obj, NULL);
3601}
3602
3603static int
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003604save_pers(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003605{
3606 PyObject *pid = NULL;
3607 int status = 0;
3608
3609 const char persid_op = PERSID;
3610 const char binpersid_op = BINPERSID;
3611
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003612 pid = call_method(self->pers_func, self->pers_func_self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003613 if (pid == NULL)
3614 return -1;
3615
3616 if (pid != Py_None) {
3617 if (self->bin) {
3618 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003619 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003620 goto error;
3621 }
3622 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003623 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003624
3625 pid_str = PyObject_Str(pid);
3626 if (pid_str == NULL)
3627 goto error;
3628
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003629 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003630 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003631 if (!PyUnicode_IS_ASCII(pid_str)) {
3632 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3633 "persistent IDs in protocol 0 must be "
3634 "ASCII strings");
3635 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003636 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003637 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003638
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003639 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003640 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3641 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3642 _Pickler_Write(self, "\n", 1) < 0) {
3643 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003644 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003645 }
3646 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003647 }
3648 status = 1;
3649 }
3650
3651 if (0) {
3652 error:
3653 status = -1;
3654 }
3655 Py_XDECREF(pid);
3656
3657 return status;
3658}
3659
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003660static PyObject *
3661get_class(PyObject *obj)
3662{
3663 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003664 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003665
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003666 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003667 if (cls == NULL) {
3668 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3669 PyErr_Clear();
3670 cls = (PyObject *) Py_TYPE(obj);
3671 Py_INCREF(cls);
3672 }
3673 }
3674 return cls;
3675}
3676
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003677/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3678 * appropriate __reduce__ method for obj.
3679 */
3680static int
3681save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3682{
3683 PyObject *callable;
3684 PyObject *argtup;
3685 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003686 PyObject *listitems = Py_None;
3687 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003688 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003689 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003690 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003691
3692 const char reduce_op = REDUCE;
3693 const char build_op = BUILD;
3694 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003695 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003696
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003697 size = PyTuple_Size(args);
3698 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003699 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003700 "__reduce__ must contain 2 through 5 elements");
3701 return -1;
3702 }
3703
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003704 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3705 &callable, &argtup, &state, &listitems, &dictitems))
3706 return -1;
3707
3708 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003709 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003710 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003711 return -1;
3712 }
3713 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003714 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003715 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003716 return -1;
3717 }
3718
3719 if (state == Py_None)
3720 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003721
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003722 if (listitems == Py_None)
3723 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003724 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003725 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003726 "returned by __reduce__ must be an iterator, not %s",
3727 Py_TYPE(listitems)->tp_name);
3728 return -1;
3729 }
3730
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003731 if (dictitems == Py_None)
3732 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003733 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003734 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003735 "returned by __reduce__ must be an iterator, not %s",
3736 Py_TYPE(dictitems)->tp_name);
3737 return -1;
3738 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003739
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003740 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003741 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003742 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003743
Victor Stinner804e05e2013-11-14 01:26:17 +01003744 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003745 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003746 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003747 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003748 }
3749 PyErr_Clear();
3750 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003751 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003752 _Py_IDENTIFIER(__newobj_ex__);
Serhiy Storchakaf0f35a62017-01-09 10:09:43 +02003753 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3754 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003755 if (!use_newobj_ex) {
3756 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003757 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003758 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003759 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003760 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003761 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003762
3763 if (use_newobj_ex) {
3764 PyObject *cls;
3765 PyObject *args;
3766 PyObject *kwargs;
3767
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003768 if (PyTuple_GET_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003769 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003770 "length of the NEWOBJ_EX argument tuple must be "
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003771 "exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003772 return -1;
3773 }
3774
3775 cls = PyTuple_GET_ITEM(argtup, 0);
3776 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003777 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003778 "first item from NEWOBJ_EX argument tuple must "
3779 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3780 return -1;
3781 }
3782 args = PyTuple_GET_ITEM(argtup, 1);
3783 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003784 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003785 "second item from NEWOBJ_EX argument tuple must "
3786 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3787 return -1;
3788 }
3789 kwargs = PyTuple_GET_ITEM(argtup, 2);
3790 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003791 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003792 "third item from NEWOBJ_EX argument tuple must "
3793 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3794 return -1;
3795 }
3796
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003797 if (self->proto >= 4) {
3798 if (save(self, cls, 0) < 0 ||
3799 save(self, args, 0) < 0 ||
3800 save(self, kwargs, 0) < 0 ||
3801 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3802 return -1;
3803 }
3804 }
3805 else {
3806 PyObject *newargs;
3807 PyObject *cls_new;
3808 Py_ssize_t i;
3809 _Py_IDENTIFIER(__new__);
3810
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003811 newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003812 if (newargs == NULL)
3813 return -1;
3814
3815 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3816 if (cls_new == NULL) {
3817 Py_DECREF(newargs);
3818 return -1;
3819 }
3820 PyTuple_SET_ITEM(newargs, 0, cls_new);
3821 Py_INCREF(cls);
3822 PyTuple_SET_ITEM(newargs, 1, cls);
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003823 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003824 PyObject *item = PyTuple_GET_ITEM(args, i);
3825 Py_INCREF(item);
3826 PyTuple_SET_ITEM(newargs, i + 2, item);
3827 }
3828
3829 callable = PyObject_Call(st->partial, newargs, kwargs);
3830 Py_DECREF(newargs);
3831 if (callable == NULL)
3832 return -1;
3833
3834 newargs = PyTuple_New(0);
3835 if (newargs == NULL) {
3836 Py_DECREF(callable);
3837 return -1;
3838 }
3839
3840 if (save(self, callable, 0) < 0 ||
3841 save(self, newargs, 0) < 0 ||
3842 _Pickler_Write(self, &reduce_op, 1) < 0) {
3843 Py_DECREF(newargs);
3844 Py_DECREF(callable);
3845 return -1;
3846 }
3847 Py_DECREF(newargs);
3848 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003849 }
3850 }
3851 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003852 PyObject *cls;
3853 PyObject *newargtup;
3854 PyObject *obj_class;
3855 int p;
3856
3857 /* Sanity checks. */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003858 if (PyTuple_GET_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003859 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003860 return -1;
3861 }
3862
3863 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003864 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003865 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003866 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003867 return -1;
3868 }
3869
3870 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003871 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003872 p = obj_class != cls; /* true iff a problem */
3873 Py_DECREF(obj_class);
3874 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003875 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003876 "__newobj__ args has the wrong class");
3877 return -1;
3878 }
3879 }
3880 /* XXX: These calls save() are prone to infinite recursion. Imagine
3881 what happen if the value returned by the __reduce__() method of
3882 some extension type contains another object of the same type. Ouch!
3883
3884 Here is a quick example, that I ran into, to illustrate what I
3885 mean:
3886
3887 >>> import pickle, copyreg
3888 >>> copyreg.dispatch_table.pop(complex)
3889 >>> pickle.dumps(1+2j)
3890 Traceback (most recent call last):
3891 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003892 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003893
3894 Removing the complex class from copyreg.dispatch_table made the
3895 __reduce_ex__() method emit another complex object:
3896
3897 >>> (1+1j).__reduce_ex__(2)
3898 (<function __newobj__ at 0xb7b71c3c>,
3899 (<class 'complex'>, (1+1j)), None, None, None)
3900
3901 Thus when save() was called on newargstup (the 2nd item) recursion
3902 ensued. Of course, the bug was in the complex class which had a
3903 broken __getnewargs__() that emitted another complex object. But,
3904 the point, here, is it is quite easy to end up with a broken reduce
3905 function. */
3906
3907 /* Save the class and its __new__ arguments. */
3908 if (save(self, cls, 0) < 0)
3909 return -1;
3910
Serhiy Storchakafff9a312017-03-21 08:53:25 +02003911 newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003912 if (newargtup == NULL)
3913 return -1;
3914
3915 p = save(self, newargtup, 0);
3916 Py_DECREF(newargtup);
3917 if (p < 0)
3918 return -1;
3919
3920 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003921 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003922 return -1;
3923 }
3924 else { /* Not using NEWOBJ. */
3925 if (save(self, callable, 0) < 0 ||
3926 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003927 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003928 return -1;
3929 }
3930
3931 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3932 the caller do not want to memoize the object. Not particularly useful,
3933 but that is to mimic the behavior save_reduce() in pickle.py when
3934 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003935 if (obj != NULL) {
3936 /* If the object is already in the memo, this means it is
3937 recursive. In this case, throw away everything we put on the
3938 stack, and fetch the object back from the memo. */
3939 if (PyMemoTable_Get(self->memo, obj)) {
3940 const char pop_op = POP;
3941
3942 if (_Pickler_Write(self, &pop_op, 1) < 0)
3943 return -1;
3944 if (memo_get(self, obj) < 0)
3945 return -1;
3946
3947 return 0;
3948 }
3949 else if (memo_put(self, obj) < 0)
3950 return -1;
3951 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003952
3953 if (listitems && batch_list(self, listitems) < 0)
3954 return -1;
3955
3956 if (dictitems && batch_dict(self, dictitems) < 0)
3957 return -1;
3958
3959 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003960 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003961 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003962 return -1;
3963 }
3964
3965 return 0;
3966}
3967
3968static int
3969save(PicklerObject *self, PyObject *obj, int pers_save)
3970{
3971 PyTypeObject *type;
3972 PyObject *reduce_func = NULL;
3973 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003974 int status = 0;
3975
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003976 if (_Pickler_OpcodeBoundary(self) < 0)
3977 return -1;
3978
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003979 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003980 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003981
3982 /* The extra pers_save argument is necessary to avoid calling save_pers()
3983 on its returned object. */
3984 if (!pers_save && self->pers_func) {
3985 /* save_pers() returns:
3986 -1 to signal an error;
3987 0 if it did nothing successfully;
3988 1 if a persistent id was saved.
3989 */
Serhiy Storchaka986375e2017-11-30 22:48:31 +02003990 if ((status = save_pers(self, obj)) != 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003991 goto done;
3992 }
3993
3994 type = Py_TYPE(obj);
3995
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003996 /* The old cPickle had an optimization that used switch-case statement
3997 dispatching on the first letter of the type name. This has was removed
3998 since benchmarks shown that this optimization was actually slowing
3999 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004000
4001 /* Atom types; these aren't memoized, so don't check the memo. */
4002
4003 if (obj == Py_None) {
4004 status = save_none(self, obj);
4005 goto done;
4006 }
4007 else if (obj == Py_False || obj == Py_True) {
4008 status = save_bool(self, obj);
4009 goto done;
4010 }
4011 else if (type == &PyLong_Type) {
4012 status = save_long(self, obj);
4013 goto done;
4014 }
4015 else if (type == &PyFloat_Type) {
4016 status = save_float(self, obj);
4017 goto done;
4018 }
4019
4020 /* Check the memo to see if it has the object. If so, generate
4021 a GET (or BINGET) opcode, instead of pickling the object
4022 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004023 if (PyMemoTable_Get(self->memo, obj)) {
4024 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004025 goto error;
4026 goto done;
4027 }
4028
4029 if (type == &PyBytes_Type) {
4030 status = save_bytes(self, obj);
4031 goto done;
4032 }
4033 else if (type == &PyUnicode_Type) {
4034 status = save_unicode(self, obj);
4035 goto done;
4036 }
4037 else if (type == &PyDict_Type) {
4038 status = save_dict(self, obj);
4039 goto done;
4040 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004041 else if (type == &PySet_Type) {
4042 status = save_set(self, obj);
4043 goto done;
4044 }
4045 else if (type == &PyFrozenSet_Type) {
4046 status = save_frozenset(self, obj);
4047 goto done;
4048 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004049 else if (type == &PyList_Type) {
4050 status = save_list(self, obj);
4051 goto done;
4052 }
4053 else if (type == &PyTuple_Type) {
4054 status = save_tuple(self, obj);
4055 goto done;
4056 }
4057 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08004058 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004059 goto done;
4060 }
4061 else if (type == &PyFunction_Type) {
4062 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08004063 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004064 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004065
4066 /* XXX: This part needs some unit tests. */
4067
4068 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004069 * self.dispatch_table, copyreg.dispatch_table, the object's
4070 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004071 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004072 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004073 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004074 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4075 (PyObject *)type);
4076 if (reduce_func == NULL) {
4077 if (PyErr_Occurred()) {
4078 goto error;
4079 }
4080 } else {
4081 /* PyDict_GetItemWithError() returns a borrowed reference.
4082 Increase the reference count to be consistent with
4083 PyObject_GetItem and _PyObject_GetAttrId used below. */
4084 Py_INCREF(reduce_func);
4085 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004086 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08004087 reduce_func = PyObject_GetItem(self->dispatch_table,
4088 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004089 if (reduce_func == NULL) {
4090 if (PyErr_ExceptionMatches(PyExc_KeyError))
4091 PyErr_Clear();
4092 else
4093 goto error;
4094 }
4095 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004096 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004097 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004098 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02004100 else if (PyType_IsSubtype(type, &PyType_Type)) {
4101 status = save_global(self, obj, NULL);
4102 goto done;
4103 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004104 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004105 _Py_IDENTIFIER(__reduce__);
4106 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004107
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004108
4109 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4110 automatically defined as __reduce__. While this is convenient, this
4111 make it impossible to know which method was actually called. Of
4112 course, this is not a big deal. But still, it would be nice to let
4113 the user know which method was called when something go
4114 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4115 don't actually have to check for a __reduce__ method. */
4116
4117 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004118 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004119 if (reduce_func != NULL) {
4120 PyObject *proto;
4121 proto = PyLong_FromLong(self->proto);
4122 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08004123 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004124 }
4125 }
4126 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004127 PickleState *st = _Pickle_GetGlobalState();
4128
4129 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004130 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004131 }
4132 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004133 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004134 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004135 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004136 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004137 if (reduce_func != NULL) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02004138 reduce_value = _PyObject_CallNoArg(reduce_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004139 }
4140 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004141 PyErr_Format(st->PicklingError,
4142 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004143 type->tp_name, obj);
4144 goto error;
4145 }
4146 }
4147 }
4148
4149 if (reduce_value == NULL)
4150 goto error;
4151
4152 if (PyUnicode_Check(reduce_value)) {
4153 status = save_global(self, obj, reduce_value);
4154 goto done;
4155 }
4156
4157 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004158 PickleState *st = _Pickle_GetGlobalState();
4159 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004160 "__reduce__ must return a string or tuple");
4161 goto error;
4162 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004163
4164 status = save_reduce(self, reduce_value, obj);
4165
4166 if (0) {
4167 error:
4168 status = -1;
4169 }
4170 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004171
Alexandre Vassalottidff18342008-07-13 18:48:30 +00004172 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004173 Py_XDECREF(reduce_func);
4174 Py_XDECREF(reduce_value);
4175
4176 return status;
4177}
4178
4179static int
4180dump(PicklerObject *self, PyObject *obj)
4181{
4182 const char stop_op = STOP;
4183
4184 if (self->proto >= 2) {
4185 char header[2];
4186
4187 header[0] = PROTO;
4188 assert(self->proto >= 0 && self->proto < 256);
4189 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004190 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004191 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004192 if (self->proto >= 4)
4193 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004194 }
4195
4196 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004197 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004198 return -1;
4199
4200 return 0;
4201}
4202
Larry Hastings61272b72014-01-07 12:41:53 -08004203/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004204
4205_pickle.Pickler.clear_memo
4206
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004207Clears the pickler's "memo".
4208
4209The memo is the data structure that remembers which objects the
4210pickler has already seen, so that shared or recursive objects are
4211pickled by reference and not by value. This method is useful when
4212re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004213[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004214
Larry Hastings3cceb382014-01-04 11:09:09 -08004215static PyObject *
4216_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004217/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004218{
4219 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004220 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004221
4222 Py_RETURN_NONE;
4223}
4224
Larry Hastings61272b72014-01-07 12:41:53 -08004225/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004226
4227_pickle.Pickler.dump
4228
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004229 obj: object
4230 /
4231
4232Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004233[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004234
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004235static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004236_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004237/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004238{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004239 /* Check whether the Pickler was initialized correctly (issue3664).
4240 Developers often forget to call __init__() in their subclasses, which
4241 would trigger a segfault without this check. */
4242 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004243 PickleState *st = _Pickle_GetGlobalState();
4244 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004245 "Pickler.__init__() was not called by %s.__init__()",
4246 Py_TYPE(self)->tp_name);
4247 return NULL;
4248 }
4249
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004250 if (_Pickler_ClearBuffer(self) < 0)
4251 return NULL;
4252
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004253 if (dump(self, obj) < 0)
4254 return NULL;
4255
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004256 if (_Pickler_FlushToFile(self) < 0)
4257 return NULL;
4258
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004259 Py_RETURN_NONE;
4260}
4261
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004262/*[clinic input]
4263
4264_pickle.Pickler.__sizeof__ -> Py_ssize_t
4265
4266Returns size in memory, in bytes.
4267[clinic start generated code]*/
4268
4269static Py_ssize_t
4270_pickle_Pickler___sizeof___impl(PicklerObject *self)
4271/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4272{
4273 Py_ssize_t res, s;
4274
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004275 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004276 if (self->memo != NULL) {
4277 res += sizeof(PyMemoTable);
4278 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4279 }
4280 if (self->output_buffer != NULL) {
4281 s = _PySys_GetSizeOf(self->output_buffer);
4282 if (s == -1)
4283 return -1;
4284 res += s;
4285 }
4286 return res;
4287}
4288
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004289static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004290 _PICKLE_PICKLER_DUMP_METHODDEF
4291 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004292 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004293 {NULL, NULL} /* sentinel */
4294};
4295
4296static void
4297Pickler_dealloc(PicklerObject *self)
4298{
4299 PyObject_GC_UnTrack(self);
4300
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004301 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004302 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004303 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004304 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004305 Py_XDECREF(self->fast_memo);
4306
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004307 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004308
4309 Py_TYPE(self)->tp_free((PyObject *)self);
4310}
4311
4312static int
4313Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4314{
4315 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004316 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004317 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004318 Py_VISIT(self->fast_memo);
4319 return 0;
4320}
4321
4322static int
4323Pickler_clear(PicklerObject *self)
4324{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004325 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004326 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004327 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004328 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004329 Py_CLEAR(self->fast_memo);
4330
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004331 if (self->memo != NULL) {
4332 PyMemoTable *memo = self->memo;
4333 self->memo = NULL;
4334 PyMemoTable_Del(memo);
4335 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004336 return 0;
4337}
4338
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004339
Larry Hastings61272b72014-01-07 12:41:53 -08004340/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004341
4342_pickle.Pickler.__init__
4343
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004344 file: object
4345 protocol: object = NULL
4346 fix_imports: bool = True
4347
4348This takes a binary file for writing a pickle data stream.
4349
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004350The optional *protocol* argument tells the pickler to use the given
4351protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4352protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004353
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004354Specifying a negative protocol version selects the highest protocol
4355version supported. The higher the protocol used, the more recent the
4356version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004357
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004358The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004359bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004360writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004361this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004362
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004363If *fix_imports* is True and protocol is less than 3, pickle will try
4364to map the new Python 3 names to the old module names used in Python
43652, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004366[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004367
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004368static int
Larry Hastings89964c42015-04-14 18:07:59 -04004369_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4370 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004371/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004372{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004373 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004374 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004375
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004376 /* In case of multiple __init__() calls, clear previous content. */
4377 if (self->write != NULL)
4378 (void)Pickler_clear(self);
4379
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004380 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004381 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004382
4383 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004384 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004385
4386 /* memo and output_buffer may have already been created in _Pickler_New */
4387 if (self->memo == NULL) {
4388 self->memo = PyMemoTable_New();
4389 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004390 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004391 }
4392 self->output_len = 0;
4393 if (self->output_buffer == NULL) {
4394 self->max_output_len = WRITE_BUF_SIZE;
4395 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4396 self->max_output_len);
4397 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004398 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004399 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004400
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004401 self->fast = 0;
4402 self->fast_nesting = 0;
4403 self->fast_memo = NULL;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004404
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004405 if (init_method_ref((PyObject *)self, &PyId_persistent_id,
4406 &self->pers_func, &self->pers_func_self) < 0)
4407 {
4408 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004409 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004410
4411 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4412 &PyId_dispatch_table);
4413 if (self->dispatch_table == NULL) {
4414 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004415 return -1;
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03004416 }
4417 PyErr_Clear();
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004418 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004419
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004420 return 0;
4421}
4422
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004423
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004424/* Define a proxy object for the Pickler's internal memo object. This is to
4425 * avoid breaking code like:
4426 * pickler.memo.clear()
4427 * and
4428 * pickler.memo = saved_memo
4429 * Is this a good idea? Not really, but we don't want to break code that uses
4430 * it. Note that we don't implement the entire mapping API here. This is
4431 * intentional, as these should be treated as black-box implementation details.
4432 */
4433
Larry Hastings61272b72014-01-07 12:41:53 -08004434/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004435_pickle.PicklerMemoProxy.clear
4436
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004437Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004438[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004439
Larry Hastings3cceb382014-01-04 11:09:09 -08004440static PyObject *
4441_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004442/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004443{
4444 if (self->pickler->memo)
4445 PyMemoTable_Clear(self->pickler->memo);
4446 Py_RETURN_NONE;
4447}
4448
Larry Hastings61272b72014-01-07 12:41:53 -08004449/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004450_pickle.PicklerMemoProxy.copy
4451
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004452Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004453[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004454
Larry Hastings3cceb382014-01-04 11:09:09 -08004455static PyObject *
4456_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004457/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004458{
4459 Py_ssize_t i;
4460 PyMemoTable *memo;
4461 PyObject *new_memo = PyDict_New();
4462 if (new_memo == NULL)
4463 return NULL;
4464
4465 memo = self->pickler->memo;
4466 for (i = 0; i < memo->mt_allocated; ++i) {
4467 PyMemoEntry entry = memo->mt_table[i];
4468 if (entry.me_key != NULL) {
4469 int status;
4470 PyObject *key, *value;
4471
4472 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004473 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004474
4475 if (key == NULL || value == NULL) {
4476 Py_XDECREF(key);
4477 Py_XDECREF(value);
4478 goto error;
4479 }
4480 status = PyDict_SetItem(new_memo, key, value);
4481 Py_DECREF(key);
4482 Py_DECREF(value);
4483 if (status < 0)
4484 goto error;
4485 }
4486 }
4487 return new_memo;
4488
4489 error:
4490 Py_XDECREF(new_memo);
4491 return NULL;
4492}
4493
Larry Hastings61272b72014-01-07 12:41:53 -08004494/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004495_pickle.PicklerMemoProxy.__reduce__
4496
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004497Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004498[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004499
Larry Hastings3cceb382014-01-04 11:09:09 -08004500static PyObject *
4501_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004502/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004503{
4504 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004505 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004506 if (contents == NULL)
4507 return NULL;
4508
4509 reduce_value = PyTuple_New(2);
4510 if (reduce_value == NULL) {
4511 Py_DECREF(contents);
4512 return NULL;
4513 }
4514 dict_args = PyTuple_New(1);
4515 if (dict_args == NULL) {
4516 Py_DECREF(contents);
4517 Py_DECREF(reduce_value);
4518 return NULL;
4519 }
4520 PyTuple_SET_ITEM(dict_args, 0, contents);
4521 Py_INCREF((PyObject *)&PyDict_Type);
4522 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4523 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4524 return reduce_value;
4525}
4526
4527static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004528 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4529 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4530 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004531 {NULL, NULL} /* sentinel */
4532};
4533
4534static void
4535PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4536{
4537 PyObject_GC_UnTrack(self);
4538 Py_XDECREF(self->pickler);
4539 PyObject_GC_Del((PyObject *)self);
4540}
4541
4542static int
4543PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4544 visitproc visit, void *arg)
4545{
4546 Py_VISIT(self->pickler);
4547 return 0;
4548}
4549
4550static int
4551PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4552{
4553 Py_CLEAR(self->pickler);
4554 return 0;
4555}
4556
4557static PyTypeObject PicklerMemoProxyType = {
4558 PyVarObject_HEAD_INIT(NULL, 0)
4559 "_pickle.PicklerMemoProxy", /*tp_name*/
4560 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4561 0,
4562 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4563 0, /* tp_print */
4564 0, /* tp_getattr */
4565 0, /* tp_setattr */
4566 0, /* tp_compare */
4567 0, /* tp_repr */
4568 0, /* tp_as_number */
4569 0, /* tp_as_sequence */
4570 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004571 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004572 0, /* tp_call */
4573 0, /* tp_str */
4574 PyObject_GenericGetAttr, /* tp_getattro */
4575 PyObject_GenericSetAttr, /* tp_setattro */
4576 0, /* tp_as_buffer */
4577 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4578 0, /* tp_doc */
4579 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4580 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4581 0, /* tp_richcompare */
4582 0, /* tp_weaklistoffset */
4583 0, /* tp_iter */
4584 0, /* tp_iternext */
4585 picklerproxy_methods, /* tp_methods */
4586};
4587
4588static PyObject *
4589PicklerMemoProxy_New(PicklerObject *pickler)
4590{
4591 PicklerMemoProxyObject *self;
4592
4593 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4594 if (self == NULL)
4595 return NULL;
4596 Py_INCREF(pickler);
4597 self->pickler = pickler;
4598 PyObject_GC_Track(self);
4599 return (PyObject *)self;
4600}
4601
4602/*****************************************************************************/
4603
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004604static PyObject *
4605Pickler_get_memo(PicklerObject *self)
4606{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004607 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004608}
4609
4610static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004611Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004612{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004613 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004614
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004615 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004616 PyErr_SetString(PyExc_TypeError,
4617 "attribute deletion is not supported");
4618 return -1;
4619 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004620
4621 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4622 PicklerObject *pickler =
4623 ((PicklerMemoProxyObject *)obj)->pickler;
4624
4625 new_memo = PyMemoTable_Copy(pickler->memo);
4626 if (new_memo == NULL)
4627 return -1;
4628 }
4629 else if (PyDict_Check(obj)) {
4630 Py_ssize_t i = 0;
4631 PyObject *key, *value;
4632
4633 new_memo = PyMemoTable_New();
4634 if (new_memo == NULL)
4635 return -1;
4636
4637 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004638 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004639 PyObject *memo_obj;
4640
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004641 if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004642 PyErr_SetString(PyExc_TypeError,
4643 "'memo' values must be 2-item tuples");
4644 goto error;
4645 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004646 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004647 if (memo_id == -1 && PyErr_Occurred())
4648 goto error;
4649 memo_obj = PyTuple_GET_ITEM(value, 1);
4650 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4651 goto error;
4652 }
4653 }
4654 else {
4655 PyErr_Format(PyExc_TypeError,
Serhiy Storchakab6a9c972016-04-17 09:39:28 +03004656 "'memo' attribute must be a PicklerMemoProxy object"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004657 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004658 return -1;
4659 }
4660
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004661 PyMemoTable_Del(self->memo);
4662 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004663
4664 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004665
4666 error:
4667 if (new_memo)
4668 PyMemoTable_Del(new_memo);
4669 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004670}
4671
4672static PyObject *
4673Pickler_get_persid(PicklerObject *self)
4674{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004675 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004676 PyErr_SetString(PyExc_AttributeError, "persistent_id");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004677 return NULL;
4678 }
4679 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004680}
4681
4682static int
4683Pickler_set_persid(PicklerObject *self, PyObject *value)
4684{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004685 if (value == NULL) {
4686 PyErr_SetString(PyExc_TypeError,
4687 "attribute deletion is not supported");
4688 return -1;
4689 }
4690 if (!PyCallable_Check(value)) {
4691 PyErr_SetString(PyExc_TypeError,
4692 "persistent_id must be a callable taking one argument");
4693 return -1;
4694 }
4695
Serhiy Storchaka986375e2017-11-30 22:48:31 +02004696 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004697 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03004698 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004699
4700 return 0;
4701}
4702
4703static PyMemberDef Pickler_members[] = {
4704 {"bin", T_INT, offsetof(PicklerObject, bin)},
4705 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004706 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004707 {NULL}
4708};
4709
4710static PyGetSetDef Pickler_getsets[] = {
4711 {"memo", (getter)Pickler_get_memo,
4712 (setter)Pickler_set_memo},
4713 {"persistent_id", (getter)Pickler_get_persid,
4714 (setter)Pickler_set_persid},
4715 {NULL}
4716};
4717
4718static PyTypeObject Pickler_Type = {
4719 PyVarObject_HEAD_INIT(NULL, 0)
4720 "_pickle.Pickler" , /*tp_name*/
4721 sizeof(PicklerObject), /*tp_basicsize*/
4722 0, /*tp_itemsize*/
4723 (destructor)Pickler_dealloc, /*tp_dealloc*/
4724 0, /*tp_print*/
4725 0, /*tp_getattr*/
4726 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004727 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004728 0, /*tp_repr*/
4729 0, /*tp_as_number*/
4730 0, /*tp_as_sequence*/
4731 0, /*tp_as_mapping*/
4732 0, /*tp_hash*/
4733 0, /*tp_call*/
4734 0, /*tp_str*/
4735 0, /*tp_getattro*/
4736 0, /*tp_setattro*/
4737 0, /*tp_as_buffer*/
4738 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004739 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004740 (traverseproc)Pickler_traverse, /*tp_traverse*/
4741 (inquiry)Pickler_clear, /*tp_clear*/
4742 0, /*tp_richcompare*/
4743 0, /*tp_weaklistoffset*/
4744 0, /*tp_iter*/
4745 0, /*tp_iternext*/
4746 Pickler_methods, /*tp_methods*/
4747 Pickler_members, /*tp_members*/
4748 Pickler_getsets, /*tp_getset*/
4749 0, /*tp_base*/
4750 0, /*tp_dict*/
4751 0, /*tp_descr_get*/
4752 0, /*tp_descr_set*/
4753 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004754 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004755 PyType_GenericAlloc, /*tp_alloc*/
4756 PyType_GenericNew, /*tp_new*/
4757 PyObject_GC_Del, /*tp_free*/
4758 0, /*tp_is_gc*/
4759};
4760
Victor Stinner121aab42011-09-29 23:40:53 +02004761/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004762
4763 XXX: It would be nice to able to avoid Python function call overhead, by
4764 using directly the C version of find_class(), when find_class() is not
4765 overridden by a subclass. Although, this could become rather hackish. A
4766 simpler optimization would be to call the C function when self is not a
4767 subclass instance. */
4768static PyObject *
4769find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4770{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004771 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004772
Victor Stinner55ba38a2016-12-09 16:09:30 +01004773 return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class,
4774 module_name, global_name, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004775}
4776
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004777static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004778marker(UnpicklerObject *self)
4779{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004780 Py_ssize_t mark;
4781
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004782 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004783 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004784 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004785 return -1;
4786 }
4787
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004788 mark = self->marks[--self->num_marks];
4789 self->stack->mark_set = self->num_marks != 0;
4790 self->stack->fence = self->num_marks ?
4791 self->marks[self->num_marks - 1] : 0;
4792 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004793}
4794
4795static int
4796load_none(UnpicklerObject *self)
4797{
4798 PDATA_APPEND(self->stack, Py_None, -1);
4799 return 0;
4800}
4801
4802static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004803load_int(UnpicklerObject *self)
4804{
4805 PyObject *value;
4806 char *endptr, *s;
4807 Py_ssize_t len;
4808 long x;
4809
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004810 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004811 return -1;
4812 if (len < 2)
4813 return bad_readline();
4814
4815 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004816 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004817 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004818 x = strtol(s, &endptr, 0);
4819
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004820 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004821 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004822 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004823 errno = 0;
4824 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004825 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004826 if (value == NULL) {
4827 PyErr_SetString(PyExc_ValueError,
4828 "could not convert string to int");
4829 return -1;
4830 }
4831 }
4832 else {
4833 if (len == 3 && (x == 0 || x == 1)) {
4834 if ((value = PyBool_FromLong(x)) == NULL)
4835 return -1;
4836 }
4837 else {
4838 if ((value = PyLong_FromLong(x)) == NULL)
4839 return -1;
4840 }
4841 }
4842
4843 PDATA_PUSH(self->stack, value, -1);
4844 return 0;
4845}
4846
4847static int
4848load_bool(UnpicklerObject *self, PyObject *boolean)
4849{
4850 assert(boolean == Py_True || boolean == Py_False);
4851 PDATA_APPEND(self->stack, boolean, -1);
4852 return 0;
4853}
4854
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004855/* s contains x bytes of an unsigned little-endian integer. Return its value
4856 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4857 */
4858static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004859calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004860{
4861 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004862 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004863 size_t x = 0;
4864
Serhiy Storchakae0606192015-09-29 22:10:07 +03004865 if (nbytes > (int)sizeof(size_t)) {
4866 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4867 * have 64-bit size that can't be represented on 32-bit platform.
4868 */
4869 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4870 if (s[i])
4871 return -1;
4872 }
4873 nbytes = (int)sizeof(size_t);
4874 }
4875 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004876 x |= (size_t) s[i] << (8 * i);
4877 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004878
4879 if (x > PY_SSIZE_T_MAX)
4880 return -1;
4881 else
4882 return (Py_ssize_t) x;
4883}
4884
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004885/* s contains x bytes of a little-endian integer. Return its value as a
4886 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004887 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004888 * of x-platform bugs.
4889 */
4890static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004891calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004892{
4893 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004894 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004895 long x = 0;
4896
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004897 for (i = 0; i < nbytes; i++) {
4898 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899 }
4900
4901 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4902 * is signed, so on a box with longs bigger than 4 bytes we need
4903 * to extend a BININT's sign bit to the full width.
4904 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004905 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004906 x |= -(x & (1L << 31));
4907 }
4908
4909 return x;
4910}
4911
4912static int
4913load_binintx(UnpicklerObject *self, char *s, int size)
4914{
4915 PyObject *value;
4916 long x;
4917
4918 x = calc_binint(s, size);
4919
4920 if ((value = PyLong_FromLong(x)) == NULL)
4921 return -1;
4922
4923 PDATA_PUSH(self->stack, value, -1);
4924 return 0;
4925}
4926
4927static int
4928load_binint(UnpicklerObject *self)
4929{
4930 char *s;
4931
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004932 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004933 return -1;
4934
4935 return load_binintx(self, s, 4);
4936}
4937
4938static int
4939load_binint1(UnpicklerObject *self)
4940{
4941 char *s;
4942
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004943 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004944 return -1;
4945
4946 return load_binintx(self, s, 1);
4947}
4948
4949static int
4950load_binint2(UnpicklerObject *self)
4951{
4952 char *s;
4953
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004954 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004955 return -1;
4956
4957 return load_binintx(self, s, 2);
4958}
4959
4960static int
4961load_long(UnpicklerObject *self)
4962{
4963 PyObject *value;
Victor Stinnerb110dad2016-12-09 17:06:43 +01004964 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004965 Py_ssize_t len;
4966
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004967 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968 return -1;
4969 if (len < 2)
4970 return bad_readline();
4971
Mark Dickinson8dd05142009-01-20 20:43:58 +00004972 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4973 the 'L' before calling PyLong_FromString. In order to maintain
4974 compatibility with Python 3.0.0, we don't actually *require*
4975 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004976 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004977 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004978 /* XXX: Should the base argument explicitly set to 10? */
4979 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004980 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004981 return -1;
4982
4983 PDATA_PUSH(self->stack, value, -1);
4984 return 0;
4985}
4986
4987/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4988 * data following.
4989 */
4990static int
4991load_counted_long(UnpicklerObject *self, int size)
4992{
4993 PyObject *value;
4994 char *nbytes;
4995 char *pdata;
4996
4997 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004998 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004999 return -1;
5000
5001 size = calc_binint(nbytes, size);
5002 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005003 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005004 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005005 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005006 "LONG pickle has negative byte count");
5007 return -1;
5008 }
5009
5010 if (size == 0)
5011 value = PyLong_FromLong(0L);
5012 else {
5013 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005014 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005015 return -1;
5016 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
5017 1 /* little endian */ , 1 /* signed */ );
5018 }
5019 if (value == NULL)
5020 return -1;
5021 PDATA_PUSH(self->stack, value, -1);
5022 return 0;
5023}
5024
5025static int
5026load_float(UnpicklerObject *self)
5027{
5028 PyObject *value;
5029 char *endptr, *s;
5030 Py_ssize_t len;
5031 double d;
5032
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005033 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005034 return -1;
5035 if (len < 2)
5036 return bad_readline();
5037
5038 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00005039 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
5040 if (d == -1.0 && PyErr_Occurred())
5041 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005042 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005043 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5044 return -1;
5045 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00005046 value = PyFloat_FromDouble(d);
5047 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048 return -1;
5049
5050 PDATA_PUSH(self->stack, value, -1);
5051 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005052}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005053
5054static int
5055load_binfloat(UnpicklerObject *self)
5056{
5057 PyObject *value;
5058 double x;
5059 char *s;
5060
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005061 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005062 return -1;
5063
5064 x = _PyFloat_Unpack8((unsigned char *)s, 0);
5065 if (x == -1.0 && PyErr_Occurred())
5066 return -1;
5067
5068 if ((value = PyFloat_FromDouble(x)) == NULL)
5069 return -1;
5070
5071 PDATA_PUSH(self->stack, value, -1);
5072 return 0;
5073}
5074
5075static int
5076load_string(UnpicklerObject *self)
5077{
5078 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005079 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005080 Py_ssize_t len;
5081 char *s, *p;
5082
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005083 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005084 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005085 /* Strip the newline */
5086 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005087 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005088 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005089 p = s + 1;
5090 len -= 2;
5091 }
5092 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005093 PickleState *st = _Pickle_GetGlobalState();
5094 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005095 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005096 return -1;
5097 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07005098 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005099
5100 /* Use the PyBytes API to decode the string, since that is what is used
5101 to encode, and then coerce the result to Unicode. */
5102 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005103 if (bytes == NULL)
5104 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005105
5106 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
5107 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5108 if (strcmp(self->encoding, "bytes") == 0) {
5109 obj = bytes;
5110 }
5111 else {
5112 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5113 Py_DECREF(bytes);
5114 if (obj == NULL) {
5115 return -1;
5116 }
5117 }
5118
5119 PDATA_PUSH(self->stack, obj, -1);
5120 return 0;
5121}
5122
5123static int
5124load_counted_binstring(UnpicklerObject *self, int nbytes)
5125{
5126 PyObject *obj;
5127 Py_ssize_t size;
5128 char *s;
5129
5130 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005131 return -1;
5132
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08005133 size = calc_binsize(s, nbytes);
5134 if (size < 0) {
5135 PickleState *st = _Pickle_GetGlobalState();
5136 PyErr_Format(st->UnpicklingError,
5137 "BINSTRING exceeds system's maximum size of %zd bytes",
5138 PY_SSIZE_T_MAX);
5139 return -1;
5140 }
5141
5142 if (_Unpickler_Read(self, &s, size) < 0)
5143 return -1;
5144
5145 /* Convert Python 2.x strings to bytes if the *encoding* given to the
5146 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5147 if (strcmp(self->encoding, "bytes") == 0) {
5148 obj = PyBytes_FromStringAndSize(s, size);
5149 }
5150 else {
5151 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5152 }
5153 if (obj == NULL) {
5154 return -1;
5155 }
5156
5157 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005158 return 0;
5159}
5160
5161static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005162load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005163{
5164 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005165 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005166 char *s;
5167
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005168 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005169 return -1;
5170
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005171 size = calc_binsize(s, nbytes);
5172 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005173 PyErr_Format(PyExc_OverflowError,
5174 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005175 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005176 return -1;
5177 }
5178
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005179 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005180 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005181
5182 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005183 if (bytes == NULL)
5184 return -1;
5185
5186 PDATA_PUSH(self->stack, bytes, -1);
5187 return 0;
5188}
5189
5190static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005191load_unicode(UnpicklerObject *self)
5192{
5193 PyObject *str;
5194 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005195 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005196
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005197 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005198 return -1;
5199 if (len < 1)
5200 return bad_readline();
5201
5202 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5203 if (str == NULL)
5204 return -1;
5205
5206 PDATA_PUSH(self->stack, str, -1);
5207 return 0;
5208}
5209
5210static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005211load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005212{
5213 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005214 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005215 char *s;
5216
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005217 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005218 return -1;
5219
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005220 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005221 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005222 PyErr_Format(PyExc_OverflowError,
5223 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005224 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005225 return -1;
5226 }
5227
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005228 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005229 return -1;
5230
Victor Stinner485fb562010-04-13 11:07:24 +00005231 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005232 if (str == NULL)
5233 return -1;
5234
5235 PDATA_PUSH(self->stack, str, -1);
5236 return 0;
5237}
5238
5239static int
Victor Stinner21b47112016-03-14 18:09:39 +01005240load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005241{
5242 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005243
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005244 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005245 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005246
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005247 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005248 if (tuple == NULL)
5249 return -1;
5250 PDATA_PUSH(self->stack, tuple, -1);
5251 return 0;
5252}
5253
5254static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005255load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005256{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005257 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005258
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005259 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005260 return -1;
5261
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005262 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005263}
5264
5265static int
5266load_empty_list(UnpicklerObject *self)
5267{
5268 PyObject *list;
5269
5270 if ((list = PyList_New(0)) == NULL)
5271 return -1;
5272 PDATA_PUSH(self->stack, list, -1);
5273 return 0;
5274}
5275
5276static int
5277load_empty_dict(UnpicklerObject *self)
5278{
5279 PyObject *dict;
5280
5281 if ((dict = PyDict_New()) == NULL)
5282 return -1;
5283 PDATA_PUSH(self->stack, dict, -1);
5284 return 0;
5285}
5286
5287static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005288load_empty_set(UnpicklerObject *self)
5289{
5290 PyObject *set;
5291
5292 if ((set = PySet_New(NULL)) == NULL)
5293 return -1;
5294 PDATA_PUSH(self->stack, set, -1);
5295 return 0;
5296}
5297
5298static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005299load_list(UnpicklerObject *self)
5300{
5301 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005302 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005303
5304 if ((i = marker(self)) < 0)
5305 return -1;
5306
5307 list = Pdata_poplist(self->stack, i);
5308 if (list == NULL)
5309 return -1;
5310 PDATA_PUSH(self->stack, list, -1);
5311 return 0;
5312}
5313
5314static int
5315load_dict(UnpicklerObject *self)
5316{
5317 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005318 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005319
5320 if ((i = marker(self)) < 0)
5321 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005322 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005323
5324 if ((dict = PyDict_New()) == NULL)
5325 return -1;
5326
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005327 if ((j - i) % 2 != 0) {
5328 PickleState *st = _Pickle_GetGlobalState();
5329 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005330 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005331 return -1;
5332 }
5333
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005334 for (k = i + 1; k < j; k += 2) {
5335 key = self->stack->data[k - 1];
5336 value = self->stack->data[k];
5337 if (PyDict_SetItem(dict, key, value) < 0) {
5338 Py_DECREF(dict);
5339 return -1;
5340 }
5341 }
5342 Pdata_clear(self->stack, i);
5343 PDATA_PUSH(self->stack, dict, -1);
5344 return 0;
5345}
5346
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005347static int
5348load_frozenset(UnpicklerObject *self)
5349{
5350 PyObject *items;
5351 PyObject *frozenset;
5352 Py_ssize_t i;
5353
5354 if ((i = marker(self)) < 0)
5355 return -1;
5356
5357 items = Pdata_poptuple(self->stack, i);
5358 if (items == NULL)
5359 return -1;
5360
5361 frozenset = PyFrozenSet_New(items);
5362 Py_DECREF(items);
5363 if (frozenset == NULL)
5364 return -1;
5365
5366 PDATA_PUSH(self->stack, frozenset, -1);
5367 return 0;
5368}
5369
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005370static PyObject *
5371instantiate(PyObject *cls, PyObject *args)
5372{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005373 /* Caller must assure args are a tuple. Normally, args come from
5374 Pdata_poptuple which packs objects from the top of the stack
5375 into a newly created tuple. */
5376 assert(PyTuple_Check(args));
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005377 if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5378 _Py_IDENTIFIER(__getinitargs__);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005379 _Py_IDENTIFIER(__new__);
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005380 PyObject *func = _PyObject_GetAttrId(cls, &PyId___getinitargs__);
5381 if (func == NULL) {
5382 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
5383 return NULL;
5384 }
5385 PyErr_Clear();
5386 return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5387 }
5388 Py_DECREF(func);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005389 }
Serhiy Storchaka04e36af2017-10-22 21:31:34 +03005390 return PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005391}
5392
5393static int
5394load_obj(UnpicklerObject *self)
5395{
5396 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005397 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005398
5399 if ((i = marker(self)) < 0)
5400 return -1;
5401
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005402 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005403 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005404
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005405 args = Pdata_poptuple(self->stack, i + 1);
5406 if (args == NULL)
5407 return -1;
5408
5409 PDATA_POP(self->stack, cls);
5410 if (cls) {
5411 obj = instantiate(cls, args);
5412 Py_DECREF(cls);
5413 }
5414 Py_DECREF(args);
5415 if (obj == NULL)
5416 return -1;
5417
5418 PDATA_PUSH(self->stack, obj, -1);
5419 return 0;
5420}
5421
5422static int
5423load_inst(UnpicklerObject *self)
5424{
5425 PyObject *cls = NULL;
5426 PyObject *args = NULL;
5427 PyObject *obj = NULL;
5428 PyObject *module_name;
5429 PyObject *class_name;
5430 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005431 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005432 char *s;
5433
5434 if ((i = marker(self)) < 0)
5435 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005436 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005437 return -1;
5438 if (len < 2)
5439 return bad_readline();
5440
5441 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5442 identifiers are permitted in Python 3.0, since the INST opcode is only
5443 supported by older protocols on Python 2.x. */
5444 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5445 if (module_name == NULL)
5446 return -1;
5447
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005448 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005449 if (len < 2) {
5450 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005451 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005452 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005453 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005454 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005455 cls = find_class(self, module_name, class_name);
5456 Py_DECREF(class_name);
5457 }
5458 }
5459 Py_DECREF(module_name);
5460
5461 if (cls == NULL)
5462 return -1;
5463
5464 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5465 obj = instantiate(cls, args);
5466 Py_DECREF(args);
5467 }
5468 Py_DECREF(cls);
5469
5470 if (obj == NULL)
5471 return -1;
5472
5473 PDATA_PUSH(self->stack, obj, -1);
5474 return 0;
5475}
5476
5477static int
5478load_newobj(UnpicklerObject *self)
5479{
5480 PyObject *args = NULL;
5481 PyObject *clsraw = NULL;
5482 PyTypeObject *cls; /* clsraw cast to its true type */
5483 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005484 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005485
5486 /* Stack is ... cls argtuple, and we want to call
5487 * cls.__new__(cls, *argtuple).
5488 */
5489 PDATA_POP(self->stack, args);
5490 if (args == NULL)
5491 goto error;
5492 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005493 PyErr_SetString(st->UnpicklingError,
5494 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005495 goto error;
5496 }
5497
5498 PDATA_POP(self->stack, clsraw);
5499 cls = (PyTypeObject *)clsraw;
5500 if (cls == NULL)
5501 goto error;
5502 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005503 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005504 "isn't a type object");
5505 goto error;
5506 }
5507 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005508 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005509 "has NULL tp_new");
5510 goto error;
5511 }
5512
5513 /* Call __new__. */
5514 obj = cls->tp_new(cls, args, NULL);
5515 if (obj == NULL)
5516 goto error;
5517
5518 Py_DECREF(args);
5519 Py_DECREF(clsraw);
5520 PDATA_PUSH(self->stack, obj, -1);
5521 return 0;
5522
5523 error:
5524 Py_XDECREF(args);
5525 Py_XDECREF(clsraw);
5526 return -1;
5527}
5528
5529static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005530load_newobj_ex(UnpicklerObject *self)
5531{
5532 PyObject *cls, *args, *kwargs;
5533 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005534 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005535
5536 PDATA_POP(self->stack, kwargs);
5537 if (kwargs == NULL) {
5538 return -1;
5539 }
5540 PDATA_POP(self->stack, args);
5541 if (args == NULL) {
5542 Py_DECREF(kwargs);
5543 return -1;
5544 }
5545 PDATA_POP(self->stack, cls);
5546 if (cls == NULL) {
5547 Py_DECREF(kwargs);
5548 Py_DECREF(args);
5549 return -1;
5550 }
Larry Hastings61272b72014-01-07 12:41:53 -08005551
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005552 if (!PyType_Check(cls)) {
5553 Py_DECREF(kwargs);
5554 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005555 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005556 "NEWOBJ_EX class argument must be a type, not %.200s",
5557 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005558 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005559 return -1;
5560 }
5561
5562 if (((PyTypeObject *)cls)->tp_new == NULL) {
5563 Py_DECREF(kwargs);
5564 Py_DECREF(args);
5565 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005566 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005567 "NEWOBJ_EX class argument doesn't have __new__");
5568 return -1;
5569 }
5570 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5571 Py_DECREF(kwargs);
5572 Py_DECREF(args);
5573 Py_DECREF(cls);
5574 if (obj == NULL) {
5575 return -1;
5576 }
5577 PDATA_PUSH(self->stack, obj, -1);
5578 return 0;
5579}
5580
5581static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005582load_global(UnpicklerObject *self)
5583{
5584 PyObject *global = NULL;
5585 PyObject *module_name;
5586 PyObject *global_name;
5587 Py_ssize_t len;
5588 char *s;
5589
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005590 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005591 return -1;
5592 if (len < 2)
5593 return bad_readline();
5594 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5595 if (!module_name)
5596 return -1;
5597
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599 if (len < 2) {
5600 Py_DECREF(module_name);
5601 return bad_readline();
5602 }
5603 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5604 if (global_name) {
5605 global = find_class(self, module_name, global_name);
5606 Py_DECREF(global_name);
5607 }
5608 }
5609 Py_DECREF(module_name);
5610
5611 if (global == NULL)
5612 return -1;
5613 PDATA_PUSH(self->stack, global, -1);
5614 return 0;
5615}
5616
5617static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005618load_stack_global(UnpicklerObject *self)
5619{
5620 PyObject *global;
5621 PyObject *module_name;
5622 PyObject *global_name;
5623
5624 PDATA_POP(self->stack, global_name);
5625 PDATA_POP(self->stack, module_name);
5626 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5627 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005628 PickleState *st = _Pickle_GetGlobalState();
5629 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005630 Py_XDECREF(global_name);
5631 Py_XDECREF(module_name);
5632 return -1;
5633 }
5634 global = find_class(self, module_name, global_name);
5635 Py_DECREF(global_name);
5636 Py_DECREF(module_name);
5637 if (global == NULL)
5638 return -1;
5639 PDATA_PUSH(self->stack, global, -1);
5640 return 0;
5641}
5642
5643static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644load_persid(UnpicklerObject *self)
5645{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005646 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005647 Py_ssize_t len;
5648 char *s;
5649
5650 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005651 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005652 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005653 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005654 return bad_readline();
5655
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005656 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5657 if (pid == NULL) {
5658 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5659 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5660 "persistent IDs in protocol 0 must be "
5661 "ASCII strings");
5662 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005663 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005664 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005665
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005666 obj = call_method(self->pers_func, self->pers_func_self, pid);
5667 Py_DECREF(pid);
5668 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005669 return -1;
5670
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005671 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005672 return 0;
5673 }
5674 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005675 PickleState *st = _Pickle_GetGlobalState();
5676 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005677 "A load persistent id instruction was encountered,\n"
5678 "but no persistent_load function was specified.");
5679 return -1;
5680 }
5681}
5682
5683static int
5684load_binpersid(UnpicklerObject *self)
5685{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005686 PyObject *pid, *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687
5688 if (self->pers_func) {
5689 PDATA_POP(self->stack, pid);
5690 if (pid == NULL)
5691 return -1;
5692
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005693 obj = call_method(self->pers_func, self->pers_func_self, pid);
5694 Py_DECREF(pid);
5695 if (obj == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696 return -1;
5697
Serhiy Storchaka986375e2017-11-30 22:48:31 +02005698 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005699 return 0;
5700 }
5701 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005702 PickleState *st = _Pickle_GetGlobalState();
5703 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005704 "A load persistent id instruction was encountered,\n"
5705 "but no persistent_load function was specified.");
5706 return -1;
5707 }
5708}
5709
5710static int
5711load_pop(UnpicklerObject *self)
5712{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005713 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005714
5715 /* Note that we split the (pickle.py) stack into two stacks,
5716 * an object stack and a mark stack. We have to be clever and
5717 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005718 * mark stack first, and only signalling a stack underflow if
5719 * the object stack is empty and the mark stack doesn't match
5720 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005721 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005722 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005723 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005724 self->stack->mark_set = self->num_marks != 0;
5725 self->stack->fence = self->num_marks ?
5726 self->marks[self->num_marks - 1] : 0;
5727 } else if (len <= self->stack->fence)
5728 return Pdata_stack_underflow(self->stack);
5729 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005730 len--;
5731 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005732 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005733 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005734 return 0;
5735}
5736
5737static int
5738load_pop_mark(UnpicklerObject *self)
5739{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005740 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005741
5742 if ((i = marker(self)) < 0)
5743 return -1;
5744
5745 Pdata_clear(self->stack, i);
5746
5747 return 0;
5748}
5749
5750static int
5751load_dup(UnpicklerObject *self)
5752{
5753 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005754 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005755
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005756 if (len <= self->stack->fence)
5757 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005758 last = self->stack->data[len - 1];
5759 PDATA_APPEND(self->stack, last, -1);
5760 return 0;
5761}
5762
5763static int
5764load_get(UnpicklerObject *self)
5765{
5766 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005767 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005768 Py_ssize_t len;
5769 char *s;
5770
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005771 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005772 return -1;
5773 if (len < 2)
5774 return bad_readline();
5775
5776 key = PyLong_FromString(s, NULL, 10);
5777 if (key == NULL)
5778 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005779 idx = PyLong_AsSsize_t(key);
5780 if (idx == -1 && PyErr_Occurred()) {
5781 Py_DECREF(key);
5782 return -1;
5783 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005786 if (value == NULL) {
5787 if (!PyErr_Occurred())
5788 PyErr_SetObject(PyExc_KeyError, key);
5789 Py_DECREF(key);
5790 return -1;
5791 }
5792 Py_DECREF(key);
5793
5794 PDATA_APPEND(self->stack, value, -1);
5795 return 0;
5796}
5797
5798static int
5799load_binget(UnpicklerObject *self)
5800{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005801 PyObject *value;
5802 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803 char *s;
5804
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005805 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005806 return -1;
5807
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005808 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005809
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005810 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005811 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005812 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005813 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005814 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005815 Py_DECREF(key);
5816 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817 return -1;
5818 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005819
5820 PDATA_APPEND(self->stack, value, -1);
5821 return 0;
5822}
5823
5824static int
5825load_long_binget(UnpicklerObject *self)
5826{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005827 PyObject *value;
5828 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005829 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005830
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005831 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005832 return -1;
5833
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005834 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005835
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005836 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005837 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005838 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005839 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005840 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005841 Py_DECREF(key);
5842 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005843 return -1;
5844 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005845
5846 PDATA_APPEND(self->stack, value, -1);
5847 return 0;
5848}
5849
5850/* Push an object from the extension registry (EXT[124]). nbytes is
5851 * the number of bytes following the opcode, holding the index (code) value.
5852 */
5853static int
5854load_extension(UnpicklerObject *self, int nbytes)
5855{
5856 char *codebytes; /* the nbytes bytes after the opcode */
5857 long code; /* calc_binint returns long */
5858 PyObject *py_code; /* code as a Python int */
5859 PyObject *obj; /* the object to push */
5860 PyObject *pair; /* (module_name, class_name) */
5861 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005862 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005863
5864 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005865 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005866 return -1;
5867 code = calc_binint(codebytes, nbytes);
5868 if (code <= 0) { /* note that 0 is forbidden */
5869 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005870 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005871 return -1;
5872 }
5873
5874 /* Look for the code in the cache. */
5875 py_code = PyLong_FromLong(code);
5876 if (py_code == NULL)
5877 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005878 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005879 if (obj != NULL) {
5880 /* Bingo. */
5881 Py_DECREF(py_code);
5882 PDATA_APPEND(self->stack, obj, -1);
5883 return 0;
5884 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005885 if (PyErr_Occurred()) {
5886 Py_DECREF(py_code);
5887 return -1;
5888 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005889
5890 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005891 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005892 if (pair == NULL) {
5893 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005894 if (!PyErr_Occurred()) {
5895 PyErr_Format(PyExc_ValueError, "unregistered extension "
5896 "code %ld", code);
5897 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005898 return -1;
5899 }
5900 /* Since the extension registry is manipulable via Python code,
5901 * confirm that pair is really a 2-tuple of strings.
5902 */
5903 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5904 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5905 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5906 Py_DECREF(py_code);
5907 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5908 "isn't a 2-tuple of strings", code);
5909 return -1;
5910 }
5911 /* Load the object. */
5912 obj = find_class(self, module_name, class_name);
5913 if (obj == NULL) {
5914 Py_DECREF(py_code);
5915 return -1;
5916 }
5917 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005918 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005919 Py_DECREF(py_code);
5920 if (code < 0) {
5921 Py_DECREF(obj);
5922 return -1;
5923 }
5924 PDATA_PUSH(self->stack, obj, -1);
5925 return 0;
5926}
5927
5928static int
5929load_put(UnpicklerObject *self)
5930{
5931 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005932 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005933 Py_ssize_t len;
Victor Stinnerb110dad2016-12-09 17:06:43 +01005934 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005935
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005936 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005937 return -1;
5938 if (len < 2)
5939 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005940 if (Py_SIZE(self->stack) <= self->stack->fence)
5941 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005942 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005943
5944 key = PyLong_FromString(s, NULL, 10);
5945 if (key == NULL)
5946 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005947 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005948 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005949 if (idx < 0) {
5950 if (!PyErr_Occurred())
5951 PyErr_SetString(PyExc_ValueError,
5952 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005953 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005954 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005955
5956 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005957}
5958
5959static int
5960load_binput(UnpicklerObject *self)
5961{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005962 PyObject *value;
5963 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005964 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005965
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005966 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005967 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005968
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005969 if (Py_SIZE(self->stack) <= self->stack->fence)
5970 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005971 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005972
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005973 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005974
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005975 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005976}
5977
5978static int
5979load_long_binput(UnpicklerObject *self)
5980{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005981 PyObject *value;
5982 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005983 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005984
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005985 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005986 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005987
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005988 if (Py_SIZE(self->stack) <= self->stack->fence)
5989 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005990 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005991
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005992 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005993 if (idx < 0) {
5994 PyErr_SetString(PyExc_ValueError,
5995 "negative LONG_BINPUT argument");
5996 return -1;
5997 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005998
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005999 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006000}
6001
6002static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006003load_memoize(UnpicklerObject *self)
6004{
6005 PyObject *value;
6006
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006007 if (Py_SIZE(self->stack) <= self->stack->fence)
6008 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006009 value = self->stack->data[Py_SIZE(self->stack) - 1];
6010
6011 return _Unpickler_MemoPut(self, self->memo_len, value);
6012}
6013
6014static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006015do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006016{
6017 PyObject *value;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006018 PyObject *slice;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006019 PyObject *list;
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006020 PyObject *result;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006021 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006022
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006023 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006024 if (x > len || x <= self->stack->fence)
6025 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006026 if (len == x) /* nothing to do */
6027 return 0;
6028
6029 list = self->stack->data[x - 1];
6030
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006031 if (PyList_CheckExact(list)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006032 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006033 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006034
6035 slice = Pdata_poplist(self->stack, x);
6036 if (!slice)
6037 return -1;
6038 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006039 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006040 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006041 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006042 }
6043 else {
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006044 PyObject *extend_func;
6045 _Py_IDENTIFIER(extend);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006046
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006047 extend_func = _PyObject_GetAttrId(list, &PyId_extend);
6048 if (extend_func != NULL) {
6049 slice = Pdata_poplist(self->stack, x);
6050 if (!slice) {
6051 Py_DECREF(extend_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006052 return -1;
6053 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006054 result = _Pickle_FastCall(extend_func, slice);
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006055 Py_DECREF(extend_func);
6056 if (result == NULL)
6057 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006058 Py_DECREF(result);
6059 }
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02006060 else {
6061 PyObject *append_func;
6062 _Py_IDENTIFIER(append);
6063
6064 /* Even if the PEP 307 requires extend() and append() methods,
6065 fall back on append() if the object has no extend() method
6066 for backward compatibility. */
6067 PyErr_Clear();
6068 append_func = _PyObject_GetAttrId(list, &PyId_append);
6069 if (append_func == NULL)
6070 return -1;
6071 for (i = x; i < len; i++) {
6072 value = self->stack->data[i];
6073 result = _Pickle_FastCall(append_func, value);
6074 if (result == NULL) {
6075 Pdata_clear(self->stack, i + 1);
6076 Py_SIZE(self->stack) = x;
6077 Py_DECREF(append_func);
6078 return -1;
6079 }
6080 Py_DECREF(result);
6081 }
6082 Py_SIZE(self->stack) = x;
6083 Py_DECREF(append_func);
6084 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006085 }
6086
6087 return 0;
6088}
6089
6090static int
6091load_append(UnpicklerObject *self)
6092{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006093 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6094 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006095 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006096}
6097
6098static int
6099load_appends(UnpicklerObject *self)
6100{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006101 Py_ssize_t i = marker(self);
6102 if (i < 0)
6103 return -1;
6104 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006105}
6106
6107static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006108do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006109{
6110 PyObject *value, *key;
6111 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006112 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006113 int status = 0;
6114
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006115 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006116 if (x > len || x <= self->stack->fence)
6117 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006118 if (len == x) /* nothing to do */
6119 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02006120 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006121 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006122 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006123 PyErr_SetString(st->UnpicklingError,
6124 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006125 return -1;
6126 }
6127
6128 /* Here, dict does not actually need to be a PyDict; it could be anything
6129 that supports the __setitem__ attribute. */
6130 dict = self->stack->data[x - 1];
6131
6132 for (i = x + 1; i < len; i += 2) {
6133 key = self->stack->data[i - 1];
6134 value = self->stack->data[i];
6135 if (PyObject_SetItem(dict, key, value) < 0) {
6136 status = -1;
6137 break;
6138 }
6139 }
6140
6141 Pdata_clear(self->stack, x);
6142 return status;
6143}
6144
6145static int
6146load_setitem(UnpicklerObject *self)
6147{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006148 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006149}
6150
6151static int
6152load_setitems(UnpicklerObject *self)
6153{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006154 Py_ssize_t i = marker(self);
6155 if (i < 0)
6156 return -1;
6157 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006158}
6159
6160static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006161load_additems(UnpicklerObject *self)
6162{
6163 PyObject *set;
6164 Py_ssize_t mark, len, i;
6165
6166 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02006167 if (mark < 0)
6168 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006169 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006170 if (mark > len || mark <= self->stack->fence)
6171 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006172 if (len == mark) /* nothing to do */
6173 return 0;
6174
6175 set = self->stack->data[mark - 1];
6176
6177 if (PySet_Check(set)) {
6178 PyObject *items;
6179 int status;
6180
6181 items = Pdata_poptuple(self->stack, mark);
6182 if (items == NULL)
6183 return -1;
6184
6185 status = _PySet_Update(set, items);
6186 Py_DECREF(items);
6187 return status;
6188 }
6189 else {
6190 PyObject *add_func;
6191 _Py_IDENTIFIER(add);
6192
6193 add_func = _PyObject_GetAttrId(set, &PyId_add);
6194 if (add_func == NULL)
6195 return -1;
6196 for (i = mark; i < len; i++) {
6197 PyObject *result;
6198 PyObject *item;
6199
6200 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006201 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006202 if (result == NULL) {
6203 Pdata_clear(self->stack, i + 1);
6204 Py_SIZE(self->stack) = mark;
6205 return -1;
6206 }
6207 Py_DECREF(result);
6208 }
6209 Py_SIZE(self->stack) = mark;
6210 }
6211
6212 return 0;
6213}
6214
6215static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006216load_build(UnpicklerObject *self)
6217{
6218 PyObject *state, *inst, *slotstate;
6219 PyObject *setstate;
6220 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006221 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006222
6223 /* Stack is ... instance, state. We want to leave instance at
6224 * the stack top, possibly mutated via instance.__setstate__(state).
6225 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006226 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6227 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006228
6229 PDATA_POP(self->stack, state);
6230 if (state == NULL)
6231 return -1;
6232
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006233 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006234
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006235 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006236 if (setstate == NULL) {
6237 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6238 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006239 else {
6240 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006241 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006242 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006243 }
6244 else {
6245 PyObject *result;
6246
6247 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006248 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006249 Py_DECREF(setstate);
6250 if (result == NULL)
6251 return -1;
6252 Py_DECREF(result);
6253 return 0;
6254 }
6255
6256 /* A default __setstate__. First see whether state embeds a
6257 * slot state dict too (a proto 2 addition).
6258 */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02006259 if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006260 PyObject *tmp = state;
6261
6262 state = PyTuple_GET_ITEM(tmp, 0);
6263 slotstate = PyTuple_GET_ITEM(tmp, 1);
6264 Py_INCREF(state);
6265 Py_INCREF(slotstate);
6266 Py_DECREF(tmp);
6267 }
6268 else
6269 slotstate = NULL;
6270
6271 /* Set inst.__dict__ from the state dict (if any). */
6272 if (state != Py_None) {
6273 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006274 PyObject *d_key, *d_value;
6275 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006276 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006277
6278 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006279 PickleState *st = _Pickle_GetGlobalState();
6280 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006281 goto error;
6282 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006283 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006284 if (dict == NULL)
6285 goto error;
6286
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006287 i = 0;
6288 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6289 /* normally the keys for instance attributes are
6290 interned. we should try to do that here. */
6291 Py_INCREF(d_key);
6292 if (PyUnicode_CheckExact(d_key))
6293 PyUnicode_InternInPlace(&d_key);
6294 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6295 Py_DECREF(d_key);
6296 goto error;
6297 }
6298 Py_DECREF(d_key);
6299 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006300 Py_DECREF(dict);
6301 }
6302
6303 /* Also set instance attributes from the slotstate dict (if any). */
6304 if (slotstate != NULL) {
6305 PyObject *d_key, *d_value;
6306 Py_ssize_t i;
6307
6308 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006309 PickleState *st = _Pickle_GetGlobalState();
6310 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006311 "slot state is not a dictionary");
6312 goto error;
6313 }
6314 i = 0;
6315 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6316 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6317 goto error;
6318 }
6319 }
6320
6321 if (0) {
6322 error:
6323 status = -1;
6324 }
6325
6326 Py_DECREF(state);
6327 Py_XDECREF(slotstate);
6328 return status;
6329}
6330
6331static int
6332load_mark(UnpicklerObject *self)
6333{
6334
6335 /* Note that we split the (pickle.py) stack into two stacks, an
6336 * object stack and a mark stack. Here we push a mark onto the
6337 * mark stack.
6338 */
6339
6340 if ((self->num_marks + 1) >= self->marks_size) {
6341 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006342
6343 /* Use the size_t type to check for overflow. */
6344 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006345 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006346 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006347 PyErr_NoMemory();
6348 return -1;
6349 }
6350
6351 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006352 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006353 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006354 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6355 if (self->marks == NULL) {
6356 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006357 PyErr_NoMemory();
6358 return -1;
6359 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006360 self->marks_size = (Py_ssize_t)alloc;
6361 }
6362
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006363 self->stack->mark_set = 1;
6364 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006365
6366 return 0;
6367}
6368
6369static int
6370load_reduce(UnpicklerObject *self)
6371{
6372 PyObject *callable = NULL;
6373 PyObject *argtup = NULL;
6374 PyObject *obj = NULL;
6375
6376 PDATA_POP(self->stack, argtup);
6377 if (argtup == NULL)
6378 return -1;
6379 PDATA_POP(self->stack, callable);
6380 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006381 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006382 Py_DECREF(callable);
6383 }
6384 Py_DECREF(argtup);
6385
6386 if (obj == NULL)
6387 return -1;
6388
6389 PDATA_PUSH(self->stack, obj, -1);
6390 return 0;
6391}
6392
6393/* Just raises an error if we don't know the protocol specified. PROTO
6394 * is the first opcode for protocols >= 2.
6395 */
6396static int
6397load_proto(UnpicklerObject *self)
6398{
6399 char *s;
6400 int i;
6401
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006402 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006403 return -1;
6404
6405 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006406 if (i <= HIGHEST_PROTOCOL) {
6407 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006408 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006409 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006410
6411 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6412 return -1;
6413}
6414
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006415static int
6416load_frame(UnpicklerObject *self)
6417{
6418 char *s;
6419 Py_ssize_t frame_len;
6420
6421 if (_Unpickler_Read(self, &s, 8) < 0)
6422 return -1;
6423
6424 frame_len = calc_binsize(s, 8);
6425 if (frame_len < 0) {
6426 PyErr_Format(PyExc_OverflowError,
6427 "FRAME length exceeds system's maximum of %zd bytes",
6428 PY_SSIZE_T_MAX);
6429 return -1;
6430 }
6431
6432 if (_Unpickler_Read(self, &s, frame_len) < 0)
6433 return -1;
6434
6435 /* Rewind to start of frame */
6436 self->next_read_idx -= frame_len;
6437 return 0;
6438}
6439
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006440static PyObject *
6441load(UnpicklerObject *self)
6442{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006443 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006444 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006445
6446 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006447 self->stack->mark_set = 0;
6448 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006449 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006450 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006451 Pdata_clear(self->stack, 0);
6452
6453 /* Convenient macros for the dispatch while-switch loop just below. */
6454#define OP(opcode, load_func) \
6455 case opcode: if (load_func(self) < 0) break; continue;
6456
6457#define OP_ARG(opcode, load_func, arg) \
6458 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6459
6460 while (1) {
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006461 if (_Unpickler_Read(self, &s, 1) < 0) {
6462 PickleState *st = _Pickle_GetGlobalState();
6463 if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6464 PyErr_Format(PyExc_EOFError, "Ran out of input");
6465 }
6466 return NULL;
6467 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006468
6469 switch ((enum opcode)s[0]) {
6470 OP(NONE, load_none)
6471 OP(BININT, load_binint)
6472 OP(BININT1, load_binint1)
6473 OP(BININT2, load_binint2)
6474 OP(INT, load_int)
6475 OP(LONG, load_long)
6476 OP_ARG(LONG1, load_counted_long, 1)
6477 OP_ARG(LONG4, load_counted_long, 4)
6478 OP(FLOAT, load_float)
6479 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006480 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6481 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6482 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6483 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6484 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006485 OP(STRING, load_string)
6486 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006487 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6488 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6489 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006490 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6491 OP_ARG(TUPLE1, load_counted_tuple, 1)
6492 OP_ARG(TUPLE2, load_counted_tuple, 2)
6493 OP_ARG(TUPLE3, load_counted_tuple, 3)
6494 OP(TUPLE, load_tuple)
6495 OP(EMPTY_LIST, load_empty_list)
6496 OP(LIST, load_list)
6497 OP(EMPTY_DICT, load_empty_dict)
6498 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006499 OP(EMPTY_SET, load_empty_set)
6500 OP(ADDITEMS, load_additems)
6501 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006502 OP(OBJ, load_obj)
6503 OP(INST, load_inst)
6504 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006505 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006506 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006507 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006508 OP(APPEND, load_append)
6509 OP(APPENDS, load_appends)
6510 OP(BUILD, load_build)
6511 OP(DUP, load_dup)
6512 OP(BINGET, load_binget)
6513 OP(LONG_BINGET, load_long_binget)
6514 OP(GET, load_get)
6515 OP(MARK, load_mark)
6516 OP(BINPUT, load_binput)
6517 OP(LONG_BINPUT, load_long_binput)
6518 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006519 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006520 OP(POP, load_pop)
6521 OP(POP_MARK, load_pop_mark)
6522 OP(SETITEM, load_setitem)
6523 OP(SETITEMS, load_setitems)
6524 OP(PERSID, load_persid)
6525 OP(BINPERSID, load_binpersid)
6526 OP(REDUCE, load_reduce)
6527 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006528 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006529 OP_ARG(EXT1, load_extension, 1)
6530 OP_ARG(EXT2, load_extension, 2)
6531 OP_ARG(EXT4, load_extension, 4)
6532 OP_ARG(NEWTRUE, load_bool, Py_True)
6533 OP_ARG(NEWFALSE, load_bool, Py_False)
6534
6535 case STOP:
6536 break;
6537
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006538 default:
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006539 {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006540 PickleState *st = _Pickle_GetGlobalState();
Serhiy Storchaka90493ab2016-09-06 23:55:11 +03006541 unsigned char c = (unsigned char) *s;
6542 if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6543 PyErr_Format(st->UnpicklingError,
6544 "invalid load key, '%c'.", c);
6545 }
6546 else {
6547 PyErr_Format(st->UnpicklingError,
6548 "invalid load key, '\\x%02x'.", c);
6549 }
6550 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006551 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006552 }
6553
6554 break; /* and we are done! */
6555 }
6556
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006557 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006558 return NULL;
6559 }
6560
Victor Stinner2ae57e32013-10-31 13:39:23 +01006561 if (_Unpickler_SkipConsumed(self) < 0)
6562 return NULL;
6563
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006564 PDATA_POP(self->stack, value);
6565 return value;
6566}
6567
Larry Hastings61272b72014-01-07 12:41:53 -08006568/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006569
6570_pickle.Unpickler.load
6571
6572Load a pickle.
6573
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006574Read a pickled object representation from the open file object given
6575in the constructor, and return the reconstituted object hierarchy
6576specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006577[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006578
Larry Hastings3cceb382014-01-04 11:09:09 -08006579static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006580_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006581/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006582{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006583 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006584
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006585 /* Check whether the Unpickler was initialized correctly. This prevents
6586 segfaulting if a subclass overridden __init__ with a function that does
6587 not call Unpickler.__init__(). Here, we simply ensure that self->read
6588 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006589 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006590 PickleState *st = _Pickle_GetGlobalState();
6591 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006592 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006593 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006594 return NULL;
6595 }
6596
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006597 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006598}
6599
6600/* The name of find_class() is misleading. In newer pickle protocols, this
6601 function is used for loading any global (i.e., functions), not just
6602 classes. The name is kept only for backward compatibility. */
6603
Larry Hastings61272b72014-01-07 12:41:53 -08006604/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006605
6606_pickle.Unpickler.find_class
6607
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006608 module_name: object
6609 global_name: object
6610 /
6611
6612Return an object from a specified module.
6613
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006614If necessary, the module will be imported. Subclasses may override
6615this method (e.g. to restrict unpickling of arbitrary classes and
6616functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006617
6618This method is called whenever a class or a function object is
6619needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006620[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006621
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006622static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006623_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6624 PyObject *module_name,
6625 PyObject *global_name)
6626/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006627{
6628 PyObject *global;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006629 PyObject *module;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006630
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006631 /* Try to map the old names used in Python 2.x to the new ones used in
6632 Python 3.x. We do this only with old pickle protocols and when the
6633 user has not disabled the feature. */
6634 if (self->proto < 3 && self->fix_imports) {
6635 PyObject *key;
6636 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006637 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006638
6639 /* Check if the global (i.e., a function or a class) was renamed
6640 or moved to another module. */
6641 key = PyTuple_Pack(2, module_name, global_name);
6642 if (key == NULL)
6643 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006644 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006645 Py_DECREF(key);
6646 if (item) {
6647 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6648 PyErr_Format(PyExc_RuntimeError,
6649 "_compat_pickle.NAME_MAPPING values should be "
6650 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6651 return NULL;
6652 }
6653 module_name = PyTuple_GET_ITEM(item, 0);
6654 global_name = PyTuple_GET_ITEM(item, 1);
6655 if (!PyUnicode_Check(module_name) ||
6656 !PyUnicode_Check(global_name)) {
6657 PyErr_Format(PyExc_RuntimeError,
6658 "_compat_pickle.NAME_MAPPING values should be "
6659 "pairs of str, not (%.200s, %.200s)",
6660 Py_TYPE(module_name)->tp_name,
6661 Py_TYPE(global_name)->tp_name);
6662 return NULL;
6663 }
6664 }
6665 else if (PyErr_Occurred()) {
6666 return NULL;
6667 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006668 else {
6669 /* Check if the module was renamed. */
6670 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6671 if (item) {
6672 if (!PyUnicode_Check(item)) {
6673 PyErr_Format(PyExc_RuntimeError,
6674 "_compat_pickle.IMPORT_MAPPING values should be "
6675 "strings, not %.200s", Py_TYPE(item)->tp_name);
6676 return NULL;
6677 }
6678 module_name = item;
6679 }
6680 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006681 return NULL;
6682 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006683 }
6684 }
6685
Eric Snow3f9eee62017-09-15 16:35:20 -06006686 module = PyImport_GetModule(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006687 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006688 if (PyErr_Occurred())
6689 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006690 module = PyImport_Import(module_name);
6691 if (module == NULL)
6692 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006693 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006694 global = getattribute(module, global_name, self->proto >= 4);
6695 Py_DECREF(module);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006696 return global;
6697}
6698
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006699/*[clinic input]
6700
6701_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6702
6703Returns size in memory, in bytes.
6704[clinic start generated code]*/
6705
6706static Py_ssize_t
6707_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6708/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6709{
6710 Py_ssize_t res;
6711
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006712 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006713 if (self->memo != NULL)
6714 res += self->memo_size * sizeof(PyObject *);
6715 if (self->marks != NULL)
6716 res += self->marks_size * sizeof(Py_ssize_t);
6717 if (self->input_line != NULL)
6718 res += strlen(self->input_line) + 1;
6719 if (self->encoding != NULL)
6720 res += strlen(self->encoding) + 1;
6721 if (self->errors != NULL)
6722 res += strlen(self->errors) + 1;
6723 return res;
6724}
6725
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006726static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006727 _PICKLE_UNPICKLER_LOAD_METHODDEF
6728 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006729 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006730 {NULL, NULL} /* sentinel */
6731};
6732
6733static void
6734Unpickler_dealloc(UnpicklerObject *self)
6735{
6736 PyObject_GC_UnTrack((PyObject *)self);
6737 Py_XDECREF(self->readline);
6738 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006739 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006740 Py_XDECREF(self->stack);
6741 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006742 if (self->buffer.buf != NULL) {
6743 PyBuffer_Release(&self->buffer);
6744 self->buffer.buf = NULL;
6745 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006746
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006747 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006748 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006749 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006750 PyMem_Free(self->encoding);
6751 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006752
6753 Py_TYPE(self)->tp_free((PyObject *)self);
6754}
6755
6756static int
6757Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6758{
6759 Py_VISIT(self->readline);
6760 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006761 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006762 Py_VISIT(self->stack);
6763 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006764 return 0;
6765}
6766
6767static int
6768Unpickler_clear(UnpicklerObject *self)
6769{
6770 Py_CLEAR(self->readline);
6771 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006772 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006773 Py_CLEAR(self->stack);
6774 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006775 if (self->buffer.buf != NULL) {
6776 PyBuffer_Release(&self->buffer);
6777 self->buffer.buf = NULL;
6778 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006779
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006780 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006781 PyMem_Free(self->marks);
6782 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006783 PyMem_Free(self->input_line);
6784 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006785 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006786 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006787 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006788 self->errors = NULL;
6789
6790 return 0;
6791}
6792
Larry Hastings61272b72014-01-07 12:41:53 -08006793/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006794
6795_pickle.Unpickler.__init__
6796
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006797 file: object
6798 *
6799 fix_imports: bool = True
6800 encoding: str = 'ASCII'
6801 errors: str = 'strict'
6802
6803This takes a binary file for reading a pickle data stream.
6804
6805The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006806protocol argument is needed. Bytes past the pickled object's
6807representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006808
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006809The argument *file* must have two methods, a read() method that takes
6810an integer argument, and a readline() method that requires no
6811arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006812binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006813other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006814
6815Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006816which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006817generated by Python 2. If *fix_imports* is True, pickle will try to
6818map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006819*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006820instances pickled by Python 2; these default to 'ASCII' and 'strict',
6821respectively. The *encoding* can be 'bytes' to read these 8-bit
6822string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006823[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006824
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006825static int
Larry Hastings89964c42015-04-14 18:07:59 -04006826_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6827 int fix_imports, const char *encoding,
6828 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006829/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006830{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006831 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006832
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006833 /* In case of multiple __init__() calls, clear previous content. */
6834 if (self->read != NULL)
6835 (void)Unpickler_clear(self);
6836
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006837 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006838 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006839
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006840 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006841 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006842
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006843 self->fix_imports = fix_imports;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006844
Serhiy Storchaka986375e2017-11-30 22:48:31 +02006845 if (init_method_ref((PyObject *)self, &PyId_persistent_load,
6846 &self->pers_func, &self->pers_func_self) < 0)
6847 {
6848 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006849 }
6850
6851 self->stack = (Pdata *)Pdata_New();
6852 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006853 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006854
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006855 self->memo_size = 32;
6856 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006857 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006858 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006859
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006860 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006861
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006862 return 0;
6863}
6864
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006865
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006866/* Define a proxy object for the Unpickler's internal memo object. This is to
6867 * avoid breaking code like:
6868 * unpickler.memo.clear()
6869 * and
6870 * unpickler.memo = saved_memo
6871 * Is this a good idea? Not really, but we don't want to break code that uses
6872 * it. Note that we don't implement the entire mapping API here. This is
6873 * intentional, as these should be treated as black-box implementation details.
6874 *
6875 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006876 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006877 */
6878
Larry Hastings61272b72014-01-07 12:41:53 -08006879/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006880_pickle.UnpicklerMemoProxy.clear
6881
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006882Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006883[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006884
Larry Hastings3cceb382014-01-04 11:09:09 -08006885static PyObject *
6886_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006887/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006888{
6889 _Unpickler_MemoCleanup(self->unpickler);
6890 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6891 if (self->unpickler->memo == NULL)
6892 return NULL;
6893 Py_RETURN_NONE;
6894}
6895
Larry Hastings61272b72014-01-07 12:41:53 -08006896/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006897_pickle.UnpicklerMemoProxy.copy
6898
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006899Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006900[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006901
Larry Hastings3cceb382014-01-04 11:09:09 -08006902static PyObject *
6903_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006904/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006905{
6906 Py_ssize_t i;
6907 PyObject *new_memo = PyDict_New();
6908 if (new_memo == NULL)
6909 return NULL;
6910
6911 for (i = 0; i < self->unpickler->memo_size; i++) {
6912 int status;
6913 PyObject *key, *value;
6914
6915 value = self->unpickler->memo[i];
6916 if (value == NULL)
6917 continue;
6918
6919 key = PyLong_FromSsize_t(i);
6920 if (key == NULL)
6921 goto error;
6922 status = PyDict_SetItem(new_memo, key, value);
6923 Py_DECREF(key);
6924 if (status < 0)
6925 goto error;
6926 }
6927 return new_memo;
6928
6929error:
6930 Py_DECREF(new_memo);
6931 return NULL;
6932}
6933
Larry Hastings61272b72014-01-07 12:41:53 -08006934/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006935_pickle.UnpicklerMemoProxy.__reduce__
6936
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006937Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006938[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006939
Larry Hastings3cceb382014-01-04 11:09:09 -08006940static PyObject *
6941_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006942/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006943{
6944 PyObject *reduce_value;
6945 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006946 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006947 if (contents == NULL)
6948 return NULL;
6949
6950 reduce_value = PyTuple_New(2);
6951 if (reduce_value == NULL) {
6952 Py_DECREF(contents);
6953 return NULL;
6954 }
6955 constructor_args = PyTuple_New(1);
6956 if (constructor_args == NULL) {
6957 Py_DECREF(contents);
6958 Py_DECREF(reduce_value);
6959 return NULL;
6960 }
6961 PyTuple_SET_ITEM(constructor_args, 0, contents);
6962 Py_INCREF((PyObject *)&PyDict_Type);
6963 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6964 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6965 return reduce_value;
6966}
6967
6968static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006969 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6970 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6971 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006972 {NULL, NULL} /* sentinel */
6973};
6974
6975static void
6976UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6977{
6978 PyObject_GC_UnTrack(self);
6979 Py_XDECREF(self->unpickler);
6980 PyObject_GC_Del((PyObject *)self);
6981}
6982
6983static int
6984UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6985 visitproc visit, void *arg)
6986{
6987 Py_VISIT(self->unpickler);
6988 return 0;
6989}
6990
6991static int
6992UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6993{
6994 Py_CLEAR(self->unpickler);
6995 return 0;
6996}
6997
6998static PyTypeObject UnpicklerMemoProxyType = {
6999 PyVarObject_HEAD_INIT(NULL, 0)
7000 "_pickle.UnpicklerMemoProxy", /*tp_name*/
7001 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
7002 0,
7003 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
7004 0, /* tp_print */
7005 0, /* tp_getattr */
7006 0, /* tp_setattr */
7007 0, /* tp_compare */
7008 0, /* tp_repr */
7009 0, /* tp_as_number */
7010 0, /* tp_as_sequence */
7011 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00007012 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007013 0, /* tp_call */
7014 0, /* tp_str */
7015 PyObject_GenericGetAttr, /* tp_getattro */
7016 PyObject_GenericSetAttr, /* tp_setattro */
7017 0, /* tp_as_buffer */
7018 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
7019 0, /* tp_doc */
7020 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
7021 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
7022 0, /* tp_richcompare */
7023 0, /* tp_weaklistoffset */
7024 0, /* tp_iter */
7025 0, /* tp_iternext */
7026 unpicklerproxy_methods, /* tp_methods */
7027};
7028
7029static PyObject *
7030UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
7031{
7032 UnpicklerMemoProxyObject *self;
7033
7034 self = PyObject_GC_New(UnpicklerMemoProxyObject,
7035 &UnpicklerMemoProxyType);
7036 if (self == NULL)
7037 return NULL;
7038 Py_INCREF(unpickler);
7039 self->unpickler = unpickler;
7040 PyObject_GC_Track(self);
7041 return (PyObject *)self;
7042}
7043
7044/*****************************************************************************/
7045
7046
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007047static PyObject *
7048Unpickler_get_memo(UnpicklerObject *self)
7049{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007050 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007051}
7052
7053static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007054Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007055{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007056 PyObject **new_memo;
7057 Py_ssize_t new_memo_size = 0;
7058 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007059
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007060 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007061 PyErr_SetString(PyExc_TypeError,
7062 "attribute deletion is not supported");
7063 return -1;
7064 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007065
7066 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
7067 UnpicklerObject *unpickler =
7068 ((UnpicklerMemoProxyObject *)obj)->unpickler;
7069
7070 new_memo_size = unpickler->memo_size;
7071 new_memo = _Unpickler_NewMemo(new_memo_size);
7072 if (new_memo == NULL)
7073 return -1;
7074
7075 for (i = 0; i < new_memo_size; i++) {
7076 Py_XINCREF(unpickler->memo[i]);
7077 new_memo[i] = unpickler->memo[i];
7078 }
7079 }
7080 else if (PyDict_Check(obj)) {
7081 Py_ssize_t i = 0;
7082 PyObject *key, *value;
7083
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02007084 new_memo_size = PyDict_GET_SIZE(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007085 new_memo = _Unpickler_NewMemo(new_memo_size);
7086 if (new_memo == NULL)
7087 return -1;
7088
7089 while (PyDict_Next(obj, &i, &key, &value)) {
7090 Py_ssize_t idx;
7091 if (!PyLong_Check(key)) {
7092 PyErr_SetString(PyExc_TypeError,
7093 "memo key must be integers");
7094 goto error;
7095 }
7096 idx = PyLong_AsSsize_t(key);
7097 if (idx == -1 && PyErr_Occurred())
7098 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007099 if (idx < 0) {
7100 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007101 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007102 goto error;
7103 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007104 if (_Unpickler_MemoPut(self, idx, value) < 0)
7105 goto error;
7106 }
7107 }
7108 else {
7109 PyErr_Format(PyExc_TypeError,
7110 "'memo' attribute must be an UnpicklerMemoProxy object"
7111 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007112 return -1;
7113 }
7114
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007115 _Unpickler_MemoCleanup(self);
7116 self->memo_size = new_memo_size;
7117 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007118
7119 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007120
7121 error:
7122 if (new_memo_size) {
7123 i = new_memo_size;
7124 while (--i >= 0) {
7125 Py_XDECREF(new_memo[i]);
7126 }
7127 PyMem_FREE(new_memo);
7128 }
7129 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007130}
7131
7132static PyObject *
7133Unpickler_get_persload(UnpicklerObject *self)
7134{
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007135 if (self->pers_func == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007136 PyErr_SetString(PyExc_AttributeError, "persistent_load");
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007137 return NULL;
7138 }
7139 return reconstruct_method(self->pers_func, self->pers_func_self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007140}
7141
7142static int
7143Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
7144{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007145 if (value == NULL) {
7146 PyErr_SetString(PyExc_TypeError,
7147 "attribute deletion is not supported");
7148 return -1;
7149 }
7150 if (!PyCallable_Check(value)) {
7151 PyErr_SetString(PyExc_TypeError,
7152 "persistent_load must be a callable taking "
7153 "one argument");
7154 return -1;
7155 }
7156
Serhiy Storchaka986375e2017-11-30 22:48:31 +02007157 self->pers_func_self = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007158 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03007159 Py_XSETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007160
7161 return 0;
7162}
7163
7164static PyGetSetDef Unpickler_getsets[] = {
7165 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7166 {"persistent_load", (getter)Unpickler_get_persload,
7167 (setter)Unpickler_set_persload},
7168 {NULL}
7169};
7170
7171static PyTypeObject Unpickler_Type = {
7172 PyVarObject_HEAD_INIT(NULL, 0)
7173 "_pickle.Unpickler", /*tp_name*/
7174 sizeof(UnpicklerObject), /*tp_basicsize*/
7175 0, /*tp_itemsize*/
7176 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7177 0, /*tp_print*/
7178 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007179 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007180 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007181 0, /*tp_repr*/
7182 0, /*tp_as_number*/
7183 0, /*tp_as_sequence*/
7184 0, /*tp_as_mapping*/
7185 0, /*tp_hash*/
7186 0, /*tp_call*/
7187 0, /*tp_str*/
7188 0, /*tp_getattro*/
7189 0, /*tp_setattro*/
7190 0, /*tp_as_buffer*/
7191 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007192 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007193 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7194 (inquiry)Unpickler_clear, /*tp_clear*/
7195 0, /*tp_richcompare*/
7196 0, /*tp_weaklistoffset*/
7197 0, /*tp_iter*/
7198 0, /*tp_iternext*/
7199 Unpickler_methods, /*tp_methods*/
7200 0, /*tp_members*/
7201 Unpickler_getsets, /*tp_getset*/
7202 0, /*tp_base*/
7203 0, /*tp_dict*/
7204 0, /*tp_descr_get*/
7205 0, /*tp_descr_set*/
7206 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007207 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007208 PyType_GenericAlloc, /*tp_alloc*/
7209 PyType_GenericNew, /*tp_new*/
7210 PyObject_GC_Del, /*tp_free*/
7211 0, /*tp_is_gc*/
7212};
7213
Larry Hastings61272b72014-01-07 12:41:53 -08007214/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007215
7216_pickle.dump
7217
7218 obj: object
7219 file: object
7220 protocol: object = NULL
7221 *
7222 fix_imports: bool = True
7223
7224Write a pickled representation of obj to the open file object file.
7225
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007226This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7227be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007228
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007229The optional *protocol* argument tells the pickler to use the given
7230protocol supported protocols are 0, 1, 2, 3 and 4. The default
7231protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007232
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007233Specifying a negative protocol version selects the highest protocol
7234version supported. The higher the protocol used, the more recent the
7235version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007236
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007237The *file* argument must have a write() method that accepts a single
7238bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007239writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007240this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007241
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007242If *fix_imports* is True and protocol is less than 3, pickle will try
7243to map the new Python 3 names to the old module names used in Python
72442, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007245[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007246
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007247static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007248_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04007249 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007250/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007251{
7252 PicklerObject *pickler = _Pickler_New();
7253
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007254 if (pickler == NULL)
7255 return NULL;
7256
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007257 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007258 goto error;
7259
7260 if (_Pickler_SetOutputStream(pickler, file) < 0)
7261 goto error;
7262
7263 if (dump(pickler, obj) < 0)
7264 goto error;
7265
7266 if (_Pickler_FlushToFile(pickler) < 0)
7267 goto error;
7268
7269 Py_DECREF(pickler);
7270 Py_RETURN_NONE;
7271
7272 error:
7273 Py_XDECREF(pickler);
7274 return NULL;
7275}
7276
Larry Hastings61272b72014-01-07 12:41:53 -08007277/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007278
7279_pickle.dumps
7280
7281 obj: object
7282 protocol: object = NULL
7283 *
7284 fix_imports: bool = True
7285
7286Return the pickled representation of the object as a bytes object.
7287
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007288The optional *protocol* argument tells the pickler to use the given
7289protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7290protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007291
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007292Specifying a negative protocol version selects the highest protocol
7293version supported. The higher the protocol used, the more recent the
7294version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007295
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007296If *fix_imports* is True and *protocol* is less than 3, pickle will
7297try to map the new Python 3 names to the old module names used in
7298Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007299[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007300
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007301static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007302_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007303 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007304/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007305{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007306 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007307 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007308
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007309 if (pickler == NULL)
7310 return NULL;
7311
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007312 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007313 goto error;
7314
7315 if (dump(pickler, obj) < 0)
7316 goto error;
7317
7318 result = _Pickler_GetString(pickler);
7319 Py_DECREF(pickler);
7320 return result;
7321
7322 error:
7323 Py_XDECREF(pickler);
7324 return NULL;
7325}
7326
Larry Hastings61272b72014-01-07 12:41:53 -08007327/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007328
7329_pickle.load
7330
7331 file: object
7332 *
7333 fix_imports: bool = True
7334 encoding: str = 'ASCII'
7335 errors: str = 'strict'
7336
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007337Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007338
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007339This is equivalent to ``Unpickler(file).load()``, but may be more
7340efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007341
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007342The protocol version of the pickle is detected automatically, so no
7343protocol argument is needed. Bytes past the pickled object's
7344representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007345
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007346The argument *file* must have two methods, a read() method that takes
7347an integer argument, and a readline() method that requires no
7348arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007349binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007350other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007351
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007352Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007353which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007354generated by Python 2. If *fix_imports* is True, pickle will try to
7355map the old Python 2 names to the new names used in Python 3. The
7356*encoding* and *errors* tell pickle how to decode 8-bit string
7357instances pickled by Python 2; these default to 'ASCII' and 'strict',
7358respectively. The *encoding* can be 'bytes' to read these 8-bit
7359string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007360[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007361
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007362static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007363_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007364 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007365/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007366{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007367 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007368 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007369
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007370 if (unpickler == NULL)
7371 return NULL;
7372
7373 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7374 goto error;
7375
7376 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7377 goto error;
7378
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007379 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007380
7381 result = load(unpickler);
7382 Py_DECREF(unpickler);
7383 return result;
7384
7385 error:
7386 Py_XDECREF(unpickler);
7387 return NULL;
7388}
7389
Larry Hastings61272b72014-01-07 12:41:53 -08007390/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007391
7392_pickle.loads
7393
7394 data: object
7395 *
7396 fix_imports: bool = True
7397 encoding: str = 'ASCII'
7398 errors: str = 'strict'
7399
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007400Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007401
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007402The protocol version of the pickle is detected automatically, so no
7403protocol argument is needed. Bytes past the pickled object's
7404representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007405
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007406Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007407which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007408generated by Python 2. If *fix_imports* is True, pickle will try to
7409map the old Python 2 names to the new names used in Python 3. The
7410*encoding* and *errors* tell pickle how to decode 8-bit string
7411instances pickled by Python 2; these default to 'ASCII' and 'strict',
7412respectively. The *encoding* can be 'bytes' to read these 8-bit
7413string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007414[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007415
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007416static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007417_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007418 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007419/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007420{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007421 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007422 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007423
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007424 if (unpickler == NULL)
7425 return NULL;
7426
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007427 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007428 goto error;
7429
7430 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7431 goto error;
7432
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007433 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007434
7435 result = load(unpickler);
7436 Py_DECREF(unpickler);
7437 return result;
7438
7439 error:
7440 Py_XDECREF(unpickler);
7441 return NULL;
7442}
7443
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007444static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007445 _PICKLE_DUMP_METHODDEF
7446 _PICKLE_DUMPS_METHODDEF
7447 _PICKLE_LOAD_METHODDEF
7448 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007449 {NULL, NULL} /* sentinel */
7450};
7451
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007452static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007453pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007454{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007455 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007456 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007457}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007458
Stefan Krahf483b0f2013-12-14 13:43:10 +01007459static void
7460pickle_free(PyObject *m)
7461{
7462 _Pickle_ClearState(_Pickle_GetState(m));
7463}
7464
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007465static int
7466pickle_traverse(PyObject *m, visitproc visit, void *arg)
7467{
7468 PickleState *st = _Pickle_GetState(m);
7469 Py_VISIT(st->PickleError);
7470 Py_VISIT(st->PicklingError);
7471 Py_VISIT(st->UnpicklingError);
7472 Py_VISIT(st->dispatch_table);
7473 Py_VISIT(st->extension_registry);
7474 Py_VISIT(st->extension_cache);
7475 Py_VISIT(st->inverted_registry);
7476 Py_VISIT(st->name_mapping_2to3);
7477 Py_VISIT(st->import_mapping_2to3);
7478 Py_VISIT(st->name_mapping_3to2);
7479 Py_VISIT(st->import_mapping_3to2);
7480 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007481 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007482 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007483}
7484
7485static struct PyModuleDef _picklemodule = {
7486 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007487 "_pickle", /* m_name */
7488 pickle_module_doc, /* m_doc */
7489 sizeof(PickleState), /* m_size */
7490 pickle_methods, /* m_methods */
7491 NULL, /* m_reload */
7492 pickle_traverse, /* m_traverse */
7493 pickle_clear, /* m_clear */
7494 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007495};
7496
7497PyMODINIT_FUNC
7498PyInit__pickle(void)
7499{
7500 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007501 PickleState *st;
7502
7503 m = PyState_FindModule(&_picklemodule);
7504 if (m) {
7505 Py_INCREF(m);
7506 return m;
7507 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007508
7509 if (PyType_Ready(&Unpickler_Type) < 0)
7510 return NULL;
7511 if (PyType_Ready(&Pickler_Type) < 0)
7512 return NULL;
7513 if (PyType_Ready(&Pdata_Type) < 0)
7514 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007515 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7516 return NULL;
7517 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7518 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007519
7520 /* Create the module and add the functions. */
7521 m = PyModule_Create(&_picklemodule);
7522 if (m == NULL)
7523 return NULL;
7524
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007525 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007526 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7527 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007528 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007529 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7530 return NULL;
7531
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007532 st = _Pickle_GetState(m);
7533
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007534 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007535 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7536 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007537 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007538 st->PicklingError = \
7539 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7540 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007541 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007542 st->UnpicklingError = \
7543 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7544 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007545 return NULL;
7546
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007547 Py_INCREF(st->PickleError);
7548 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007549 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007550 Py_INCREF(st->PicklingError);
7551 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007552 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007553 Py_INCREF(st->UnpicklingError);
7554 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007555 return NULL;
7556
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007557 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007558 return NULL;
7559
7560 return m;
7561}