Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Modules/_pickle.c
12 views
1
/* pickle accelerator C extensor: _pickle module.
2
*
3
* It is built as a built-in module (Py_BUILD_CORE_BUILTIN define) on Windows
4
* and as an extension module (Py_BUILD_CORE_MODULE define) on other
5
* platforms. */
6
7
#ifndef Py_BUILD_CORE_BUILTIN
8
# define Py_BUILD_CORE_MODULE 1
9
#endif
10
11
#include "Python.h"
12
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
13
#include "pycore_moduleobject.h" // _PyModule_GetState()
14
#include "pycore_runtime.h" // _Py_ID()
15
#include "pycore_pystate.h" // _PyThreadState_GET()
16
#include "structmember.h" // PyMemberDef
17
18
#include <stdlib.h> // strtol()
19
20
PyDoc_STRVAR(pickle_module_doc,
21
"Optimized C implementation for the Python pickle module.");
22
23
/*[clinic input]
24
module _pickle
25
class _pickle.Pickler "PicklerObject *" ""
26
class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" ""
27
class _pickle.Unpickler "UnpicklerObject *" ""
28
class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" ""
29
[clinic start generated code]*/
30
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b6d7191ab6466cda]*/
31
32
/* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol.
33
Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python
34
already includes it. */
35
enum {
36
HIGHEST_PROTOCOL = 5,
37
DEFAULT_PROTOCOL = 4
38
};
39
40
#ifdef MS_WINDOWS
41
// These are already typedefs from windows.h, pulled in via pycore_runtime.h.
42
#define FLOAT FLOAT_
43
#define INT INT_
44
#define LONG LONG_
45
46
/* This can already be defined on Windows to set the character set
47
the Windows header files treat as default */
48
#ifdef UNICODE
49
#undef UNICODE
50
#endif
51
#endif
52
53
/* Pickle opcodes. These must be kept updated with pickle.py.
54
Extensive docs are in pickletools.py. */
55
enum opcode {
56
MARK = '(',
57
STOP = '.',
58
POP = '0',
59
POP_MARK = '1',
60
DUP = '2',
61
FLOAT = 'F',
62
INT = 'I',
63
BININT = 'J',
64
BININT1 = 'K',
65
LONG = 'L',
66
BININT2 = 'M',
67
NONE = 'N',
68
PERSID = 'P',
69
BINPERSID = 'Q',
70
REDUCE = 'R',
71
STRING = 'S',
72
BINSTRING = 'T',
73
SHORT_BINSTRING = 'U',
74
UNICODE = 'V',
75
BINUNICODE = 'X',
76
APPEND = 'a',
77
BUILD = 'b',
78
GLOBAL = 'c',
79
DICT = 'd',
80
EMPTY_DICT = '}',
81
APPENDS = 'e',
82
GET = 'g',
83
BINGET = 'h',
84
INST = 'i',
85
LONG_BINGET = 'j',
86
LIST = 'l',
87
EMPTY_LIST = ']',
88
OBJ = 'o',
89
PUT = 'p',
90
BINPUT = 'q',
91
LONG_BINPUT = 'r',
92
SETITEM = 's',
93
TUPLE = 't',
94
EMPTY_TUPLE = ')',
95
SETITEMS = 'u',
96
BINFLOAT = 'G',
97
98
/* Protocol 2. */
99
PROTO = '\x80',
100
NEWOBJ = '\x81',
101
EXT1 = '\x82',
102
EXT2 = '\x83',
103
EXT4 = '\x84',
104
TUPLE1 = '\x85',
105
TUPLE2 = '\x86',
106
TUPLE3 = '\x87',
107
NEWTRUE = '\x88',
108
NEWFALSE = '\x89',
109
LONG1 = '\x8a',
110
LONG4 = '\x8b',
111
112
/* Protocol 3 (Python 3.x) */
113
BINBYTES = 'B',
114
SHORT_BINBYTES = 'C',
115
116
/* Protocol 4 */
117
SHORT_BINUNICODE = '\x8c',
118
BINUNICODE8 = '\x8d',
119
BINBYTES8 = '\x8e',
120
EMPTY_SET = '\x8f',
121
ADDITEMS = '\x90',
122
FROZENSET = '\x91',
123
NEWOBJ_EX = '\x92',
124
STACK_GLOBAL = '\x93',
125
MEMOIZE = '\x94',
126
FRAME = '\x95',
127
128
/* Protocol 5 */
129
BYTEARRAY8 = '\x96',
130
NEXT_BUFFER = '\x97',
131
READONLY_BUFFER = '\x98'
132
};
133
134
enum {
135
/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
136
batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
137
break if this gets out of synch with pickle.py, but it's unclear that would
138
help anything either. */
139
BATCHSIZE = 1000,
140
141
/* Nesting limit until Pickler, when running in "fast mode", starts
142
checking for self-referential data-structures. */
143
FAST_NESTING_LIMIT = 50,
144
145
/* Initial size of the write buffer of Pickler. */
146
WRITE_BUF_SIZE = 4096,
147
148
/* Prefetch size when unpickling (disabled on unpeekable streams) */
149
PREFETCH = 8192 * 16,
150
151
FRAME_SIZE_MIN = 4,
152
FRAME_SIZE_TARGET = 64 * 1024,
153
FRAME_HEADER_SIZE = 9
154
};
155
156
/*************************************************************************/
157
158
/* State of the pickle module, per PEP 3121. */
159
typedef struct {
160
/* Exception classes for pickle. */
161
PyObject *PickleError;
162
PyObject *PicklingError;
163
PyObject *UnpicklingError;
164
165
/* copyreg.dispatch_table, {type_object: pickling_function} */
166
PyObject *dispatch_table;
167
168
/* For the extension opcodes EXT1, EXT2 and EXT4. */
169
170
/* copyreg._extension_registry, {(module_name, function_name): code} */
171
PyObject *extension_registry;
172
/* copyreg._extension_cache, {code: object} */
173
PyObject *extension_cache;
174
/* copyreg._inverted_registry, {code: (module_name, function_name)} */
175
PyObject *inverted_registry;
176
177
/* Import mappings for compatibility with Python 2.x */
178
179
/* _compat_pickle.NAME_MAPPING,
180
{(oldmodule, oldname): (newmodule, newname)} */
181
PyObject *name_mapping_2to3;
182
/* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
183
PyObject *import_mapping_2to3;
184
/* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
185
PyObject *name_mapping_3to2;
186
PyObject *import_mapping_3to2;
187
188
/* codecs.encode, used for saving bytes in older protocols */
189
PyObject *codecs_encode;
190
/* builtins.getattr, used for saving nested names with protocol < 4 */
191
PyObject *getattr;
192
/* functools.partial, used for implementing __newobj_ex__ with protocols
193
2 and 3 */
194
PyObject *partial;
195
196
/* Types */
197
PyTypeObject *Pickler_Type;
198
PyTypeObject *Unpickler_Type;
199
PyTypeObject *Pdata_Type;
200
PyTypeObject *PicklerMemoProxyType;
201
PyTypeObject *UnpicklerMemoProxyType;
202
} PickleState;
203
204
/* Forward declaration of the _pickle module definition. */
205
static struct PyModuleDef _picklemodule;
206
207
/* Given a module object, get its per-module state. */
208
static inline PickleState *
209
_Pickle_GetState(PyObject *module)
210
{
211
void *state = _PyModule_GetState(module);
212
assert(state != NULL);
213
return (PickleState *)state;
214
}
215
216
static inline PickleState *
217
_Pickle_GetStateByClass(PyTypeObject *cls)
218
{
219
void *state = _PyType_GetModuleState(cls);
220
assert(state != NULL);
221
return (PickleState *)state;
222
}
223
224
static inline PickleState *
225
_Pickle_FindStateByType(PyTypeObject *tp)
226
{
227
PyObject *module = PyType_GetModuleByDef(tp, &_picklemodule);
228
assert(module != NULL);
229
return _Pickle_GetState(module);
230
}
231
232
/* Clear the given pickle module state. */
233
static void
234
_Pickle_ClearState(PickleState *st)
235
{
236
Py_CLEAR(st->PickleError);
237
Py_CLEAR(st->PicklingError);
238
Py_CLEAR(st->UnpicklingError);
239
Py_CLEAR(st->dispatch_table);
240
Py_CLEAR(st->extension_registry);
241
Py_CLEAR(st->extension_cache);
242
Py_CLEAR(st->inverted_registry);
243
Py_CLEAR(st->name_mapping_2to3);
244
Py_CLEAR(st->import_mapping_2to3);
245
Py_CLEAR(st->name_mapping_3to2);
246
Py_CLEAR(st->import_mapping_3to2);
247
Py_CLEAR(st->codecs_encode);
248
Py_CLEAR(st->getattr);
249
Py_CLEAR(st->partial);
250
Py_CLEAR(st->Pickler_Type);
251
Py_CLEAR(st->Unpickler_Type);
252
Py_CLEAR(st->Pdata_Type);
253
Py_CLEAR(st->PicklerMemoProxyType);
254
Py_CLEAR(st->UnpicklerMemoProxyType);
255
}
256
257
/* Initialize the given pickle module state. */
258
static int
259
_Pickle_InitState(PickleState *st)
260
{
261
PyObject *copyreg = NULL;
262
PyObject *compat_pickle = NULL;
263
264
st->getattr = _PyEval_GetBuiltin(&_Py_ID(getattr));
265
if (st->getattr == NULL)
266
goto error;
267
268
copyreg = PyImport_ImportModule("copyreg");
269
if (!copyreg)
270
goto error;
271
st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
272
if (!st->dispatch_table)
273
goto error;
274
if (!PyDict_CheckExact(st->dispatch_table)) {
275
PyErr_Format(PyExc_RuntimeError,
276
"copyreg.dispatch_table should be a dict, not %.200s",
277
Py_TYPE(st->dispatch_table)->tp_name);
278
goto error;
279
}
280
st->extension_registry = \
281
PyObject_GetAttrString(copyreg, "_extension_registry");
282
if (!st->extension_registry)
283
goto error;
284
if (!PyDict_CheckExact(st->extension_registry)) {
285
PyErr_Format(PyExc_RuntimeError,
286
"copyreg._extension_registry should be a dict, "
287
"not %.200s", Py_TYPE(st->extension_registry)->tp_name);
288
goto error;
289
}
290
st->inverted_registry = \
291
PyObject_GetAttrString(copyreg, "_inverted_registry");
292
if (!st->inverted_registry)
293
goto error;
294
if (!PyDict_CheckExact(st->inverted_registry)) {
295
PyErr_Format(PyExc_RuntimeError,
296
"copyreg._inverted_registry should be a dict, "
297
"not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
298
goto error;
299
}
300
st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
301
if (!st->extension_cache)
302
goto error;
303
if (!PyDict_CheckExact(st->extension_cache)) {
304
PyErr_Format(PyExc_RuntimeError,
305
"copyreg._extension_cache should be a dict, "
306
"not %.200s", Py_TYPE(st->extension_cache)->tp_name);
307
goto error;
308
}
309
Py_CLEAR(copyreg);
310
311
/* Load the 2.x -> 3.x stdlib module mapping tables */
312
compat_pickle = PyImport_ImportModule("_compat_pickle");
313
if (!compat_pickle)
314
goto error;
315
st->name_mapping_2to3 = \
316
PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
317
if (!st->name_mapping_2to3)
318
goto error;
319
if (!PyDict_CheckExact(st->name_mapping_2to3)) {
320
PyErr_Format(PyExc_RuntimeError,
321
"_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
322
Py_TYPE(st->name_mapping_2to3)->tp_name);
323
goto error;
324
}
325
st->import_mapping_2to3 = \
326
PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
327
if (!st->import_mapping_2to3)
328
goto error;
329
if (!PyDict_CheckExact(st->import_mapping_2to3)) {
330
PyErr_Format(PyExc_RuntimeError,
331
"_compat_pickle.IMPORT_MAPPING should be a dict, "
332
"not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
333
goto error;
334
}
335
/* ... and the 3.x -> 2.x mapping tables */
336
st->name_mapping_3to2 = \
337
PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
338
if (!st->name_mapping_3to2)
339
goto error;
340
if (!PyDict_CheckExact(st->name_mapping_3to2)) {
341
PyErr_Format(PyExc_RuntimeError,
342
"_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
343
"not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
344
goto error;
345
}
346
st->import_mapping_3to2 = \
347
PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
348
if (!st->import_mapping_3to2)
349
goto error;
350
if (!PyDict_CheckExact(st->import_mapping_3to2)) {
351
PyErr_Format(PyExc_RuntimeError,
352
"_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
353
"not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
354
goto error;
355
}
356
Py_CLEAR(compat_pickle);
357
358
st->codecs_encode = _PyImport_GetModuleAttrString("codecs", "encode");
359
if (st->codecs_encode == NULL) {
360
goto error;
361
}
362
if (!PyCallable_Check(st->codecs_encode)) {
363
PyErr_Format(PyExc_RuntimeError,
364
"codecs.encode should be a callable, not %.200s",
365
Py_TYPE(st->codecs_encode)->tp_name);
366
goto error;
367
}
368
369
st->partial = _PyImport_GetModuleAttrString("functools", "partial");
370
if (!st->partial)
371
goto error;
372
373
return 0;
374
375
error:
376
Py_CLEAR(copyreg);
377
Py_CLEAR(compat_pickle);
378
_Pickle_ClearState(st);
379
return -1;
380
}
381
382
/* Helper for calling a function with a single argument quickly.
383
384
This function steals the reference of the given argument. */
385
static PyObject *
386
_Pickle_FastCall(PyObject *func, PyObject *obj)
387
{
388
PyObject *result;
389
390
result = PyObject_CallOneArg(func, obj);
391
Py_DECREF(obj);
392
return result;
393
}
394
395
/*************************************************************************/
396
397
/* Retrieve and deconstruct a method for avoiding a reference cycle
398
(pickler -> bound method of pickler -> pickler) */
399
static int
400
init_method_ref(PyObject *self, PyObject *name,
401
PyObject **method_func, PyObject **method_self)
402
{
403
PyObject *func, *func2;
404
int ret;
405
406
/* *method_func and *method_self should be consistent. All refcount decrements
407
should be occurred after setting *method_self and *method_func. */
408
ret = _PyObject_LookupAttr(self, name, &func);
409
if (func == NULL) {
410
*method_self = NULL;
411
Py_CLEAR(*method_func);
412
return ret;
413
}
414
415
if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
416
/* Deconstruct a bound Python method */
417
*method_self = self; /* borrowed */
418
func2 = PyMethod_GET_FUNCTION(func);
419
Py_XSETREF(*method_func, Py_NewRef(func2));
420
Py_DECREF(func);
421
return 0;
422
}
423
else {
424
*method_self = NULL;
425
Py_XSETREF(*method_func, func);
426
return 0;
427
}
428
}
429
430
/* Bind a method if it was deconstructed */
431
static PyObject *
432
reconstruct_method(PyObject *func, PyObject *self)
433
{
434
if (self) {
435
return PyMethod_New(func, self);
436
}
437
else {
438
return Py_NewRef(func);
439
}
440
}
441
442
static PyObject *
443
call_method(PyObject *func, PyObject *self, PyObject *obj)
444
{
445
if (self) {
446
return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
447
}
448
else {
449
return PyObject_CallOneArg(func, obj);
450
}
451
}
452
453
/*************************************************************************/
454
455
/* Internal data type used as the unpickling stack. */
456
typedef struct {
457
PyObject_VAR_HEAD
458
PyObject **data;
459
int mark_set; /* is MARK set? */
460
Py_ssize_t fence; /* position of top MARK or 0 */
461
Py_ssize_t allocated; /* number of slots in data allocated */
462
} Pdata;
463
464
static int
465
Pdata_traverse(Pdata *self, visitproc visit, void *arg)
466
{
467
Py_VISIT(Py_TYPE(self));
468
return 0;
469
}
470
471
static void
472
Pdata_dealloc(Pdata *self)
473
{
474
PyTypeObject *tp = Py_TYPE(self);
475
PyObject_GC_UnTrack(self);
476
Py_ssize_t i = Py_SIZE(self);
477
while (--i >= 0) {
478
Py_DECREF(self->data[i]);
479
}
480
PyMem_Free(self->data);
481
tp->tp_free((PyObject *)self);
482
Py_DECREF(tp);
483
}
484
485
static PyType_Slot pdata_slots[] = {
486
{Py_tp_dealloc, Pdata_dealloc},
487
{Py_tp_traverse, Pdata_traverse},
488
{0, NULL},
489
};
490
491
static PyType_Spec pdata_spec = {
492
.name = "_pickle.Pdata",
493
.basicsize = sizeof(Pdata),
494
.itemsize = sizeof(PyObject *),
495
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
496
Py_TPFLAGS_IMMUTABLETYPE),
497
.slots = pdata_slots,
498
};
499
500
static PyObject *
501
Pdata_New(PickleState *state)
502
{
503
Pdata *self;
504
505
if (!(self = PyObject_GC_New(Pdata, state->Pdata_Type)))
506
return NULL;
507
Py_SET_SIZE(self, 0);
508
self->mark_set = 0;
509
self->fence = 0;
510
self->allocated = 8;
511
self->data = PyMem_Malloc(self->allocated * sizeof(PyObject *));
512
if (self->data) {
513
PyObject_GC_Track(self);
514
return (PyObject *)self;
515
}
516
Py_DECREF(self);
517
return PyErr_NoMemory();
518
}
519
520
521
/* Retain only the initial clearto items. If clearto >= the current
522
* number of items, this is a (non-erroneous) NOP.
523
*/
524
static int
525
Pdata_clear(Pdata *self, Py_ssize_t clearto)
526
{
527
Py_ssize_t i = Py_SIZE(self);
528
529
assert(clearto >= self->fence);
530
if (clearto >= i)
531
return 0;
532
533
while (--i >= clearto) {
534
Py_CLEAR(self->data[i]);
535
}
536
Py_SET_SIZE(self, clearto);
537
return 0;
538
}
539
540
static int
541
Pdata_grow(Pdata *self)
542
{
543
PyObject **data = self->data;
544
size_t allocated = (size_t)self->allocated;
545
size_t new_allocated;
546
547
new_allocated = (allocated >> 3) + 6;
548
/* check for integer overflow */
549
if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
550
goto nomemory;
551
new_allocated += allocated;
552
PyMem_RESIZE(data, PyObject *, new_allocated);
553
if (data == NULL)
554
goto nomemory;
555
556
self->data = data;
557
self->allocated = (Py_ssize_t)new_allocated;
558
return 0;
559
560
nomemory:
561
PyErr_NoMemory();
562
return -1;
563
}
564
565
static int
566
Pdata_stack_underflow(PickleState *st, Pdata *self)
567
{
568
PyErr_SetString(st->UnpicklingError,
569
self->mark_set ?
570
"unexpected MARK found" :
571
"unpickling stack underflow");
572
return -1;
573
}
574
575
/* D is a Pdata*. Pop the topmost element and store it into V, which
576
* must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
577
* is raised and V is set to NULL.
578
*/
579
static PyObject *
580
Pdata_pop(PickleState *state, Pdata *self)
581
{
582
if (Py_SIZE(self) <= self->fence) {
583
Pdata_stack_underflow(state, self);
584
return NULL;
585
}
586
Py_SET_SIZE(self, Py_SIZE(self) - 1);
587
return self->data[Py_SIZE(self)];
588
}
589
#define PDATA_POP(S, D, V) do { (V) = Pdata_pop(S, (D)); } while (0)
590
591
static int
592
Pdata_push(Pdata *self, PyObject *obj)
593
{
594
if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
595
return -1;
596
}
597
self->data[Py_SIZE(self)] = obj;
598
Py_SET_SIZE(self, Py_SIZE(self) + 1);
599
return 0;
600
}
601
602
/* Push an object on stack, transferring its ownership to the stack. */
603
#define PDATA_PUSH(D, O, ER) do { \
604
if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
605
606
/* Push an object on stack, adding a new reference to the object. */
607
#define PDATA_APPEND(D, O, ER) do { \
608
Py_INCREF((O)); \
609
if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
610
611
static PyObject *
612
Pdata_poptuple(PickleState *state, Pdata *self, Py_ssize_t start)
613
{
614
PyObject *tuple;
615
Py_ssize_t len, i, j;
616
617
if (start < self->fence) {
618
Pdata_stack_underflow(state, self);
619
return NULL;
620
}
621
len = Py_SIZE(self) - start;
622
tuple = PyTuple_New(len);
623
if (tuple == NULL)
624
return NULL;
625
for (i = start, j = 0; j < len; i++, j++)
626
PyTuple_SET_ITEM(tuple, j, self->data[i]);
627
628
Py_SET_SIZE(self, start);
629
return tuple;
630
}
631
632
static PyObject *
633
Pdata_poplist(Pdata *self, Py_ssize_t start)
634
{
635
PyObject *list;
636
Py_ssize_t len, i, j;
637
638
len = Py_SIZE(self) - start;
639
list = PyList_New(len);
640
if (list == NULL)
641
return NULL;
642
for (i = start, j = 0; j < len; i++, j++)
643
PyList_SET_ITEM(list, j, self->data[i]);
644
645
Py_SET_SIZE(self, start);
646
return list;
647
}
648
649
typedef struct {
650
PyObject *me_key;
651
Py_ssize_t me_value;
652
} PyMemoEntry;
653
654
typedef struct {
655
size_t mt_mask;
656
size_t mt_used;
657
size_t mt_allocated;
658
PyMemoEntry *mt_table;
659
} PyMemoTable;
660
661
typedef struct PicklerObject {
662
PyObject_HEAD
663
PyMemoTable *memo; /* Memo table, keep track of the seen
664
objects to support self-referential objects
665
pickling. */
666
PyObject *pers_func; /* persistent_id() method, can be NULL */
667
PyObject *pers_func_self; /* borrowed reference to self if pers_func
668
is an unbound method, NULL otherwise */
669
PyObject *dispatch_table; /* private dispatch_table, can be NULL */
670
PyObject *reducer_override; /* hook for invoking user-defined callbacks
671
instead of save_global when pickling
672
functions and classes*/
673
674
PyObject *write; /* write() method of the output stream. */
675
PyObject *output_buffer; /* Write into a local bytearray buffer before
676
flushing to the stream. */
677
Py_ssize_t output_len; /* Length of output_buffer. */
678
Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
679
int proto; /* Pickle protocol number, >= 0 */
680
int bin; /* Boolean, true if proto > 0 */
681
int framing; /* True when framing is enabled, proto >= 4 */
682
Py_ssize_t frame_start; /* Position in output_buffer where the
683
current frame begins. -1 if there
684
is no frame currently open. */
685
686
Py_ssize_t buf_size; /* Size of the current buffered pickle data */
687
int fast; /* Enable fast mode if set to a true value.
688
The fast mode disable the usage of memo,
689
therefore speeding the pickling process by
690
not generating superfluous PUT opcodes. It
691
should not be used if with self-referential
692
objects. */
693
int fast_nesting;
694
int fix_imports; /* Indicate whether Pickler should fix
695
the name of globals for Python 2.x. */
696
PyObject *fast_memo;
697
PyObject *buffer_callback; /* Callback for out-of-band buffers, or NULL */
698
} PicklerObject;
699
700
typedef struct UnpicklerObject {
701
PyObject_HEAD
702
Pdata *stack; /* Pickle data stack, store unpickled objects. */
703
704
/* The unpickler memo is just an array of PyObject *s. Using a dict
705
is unnecessary, since the keys are contiguous ints. */
706
PyObject **memo;
707
size_t memo_size; /* Capacity of the memo array */
708
size_t memo_len; /* Number of objects in the memo */
709
710
PyObject *pers_func; /* persistent_load() method, can be NULL. */
711
PyObject *pers_func_self; /* borrowed reference to self if pers_func
712
is an unbound method, NULL otherwise */
713
714
Py_buffer buffer;
715
char *input_buffer;
716
char *input_line;
717
Py_ssize_t input_len;
718
Py_ssize_t next_read_idx;
719
Py_ssize_t prefetched_idx; /* index of first prefetched byte */
720
721
PyObject *read; /* read() method of the input stream. */
722
PyObject *readinto; /* readinto() method of the input stream. */
723
PyObject *readline; /* readline() method of the input stream. */
724
PyObject *peek; /* peek() method of the input stream, or NULL */
725
PyObject *buffers; /* iterable of out-of-band buffers, or NULL */
726
727
char *encoding; /* Name of the encoding to be used for
728
decoding strings pickled using Python
729
2.x. The default value is "ASCII" */
730
char *errors; /* Name of errors handling scheme to used when
731
decoding strings. The default value is
732
"strict". */
733
Py_ssize_t *marks; /* Mark stack, used for unpickling container
734
objects. */
735
Py_ssize_t num_marks; /* Number of marks in the mark stack. */
736
Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
737
int proto; /* Protocol of the pickle loaded. */
738
int fix_imports; /* Indicate whether Unpickler should fix
739
the name of globals pickled by Python 2.x. */
740
} UnpicklerObject;
741
742
typedef struct {
743
PyObject_HEAD
744
PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
745
} PicklerMemoProxyObject;
746
747
typedef struct {
748
PyObject_HEAD
749
UnpicklerObject *unpickler;
750
} UnpicklerMemoProxyObject;
751
752
/* Forward declarations */
753
static int save(PickleState *state, PicklerObject *, PyObject *, int);
754
static int save_reduce(PickleState *, PicklerObject *, PyObject *, PyObject *);
755
756
#include "clinic/_pickle.c.h"
757
758
/*************************************************************************
759
A custom hashtable mapping void* to Python ints. This is used by the pickler
760
for memoization. Using a custom hashtable rather than PyDict allows us to skip
761
a bunch of unnecessary object creation. This makes a huge performance
762
difference. */
763
764
#define MT_MINSIZE 8
765
#define PERTURB_SHIFT 5
766
767
768
static PyMemoTable *
769
PyMemoTable_New(void)
770
{
771
PyMemoTable *memo = PyMem_Malloc(sizeof(PyMemoTable));
772
if (memo == NULL) {
773
PyErr_NoMemory();
774
return NULL;
775
}
776
777
memo->mt_used = 0;
778
memo->mt_allocated = MT_MINSIZE;
779
memo->mt_mask = MT_MINSIZE - 1;
780
memo->mt_table = PyMem_Malloc(MT_MINSIZE * sizeof(PyMemoEntry));
781
if (memo->mt_table == NULL) {
782
PyMem_Free(memo);
783
PyErr_NoMemory();
784
return NULL;
785
}
786
memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
787
788
return memo;
789
}
790
791
static PyMemoTable *
792
PyMemoTable_Copy(PyMemoTable *self)
793
{
794
PyMemoTable *new = PyMemoTable_New();
795
if (new == NULL)
796
return NULL;
797
798
new->mt_used = self->mt_used;
799
new->mt_allocated = self->mt_allocated;
800
new->mt_mask = self->mt_mask;
801
/* The table we get from _New() is probably smaller than we wanted.
802
Free it and allocate one that's the right size. */
803
PyMem_Free(new->mt_table);
804
new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
805
if (new->mt_table == NULL) {
806
PyMem_Free(new);
807
PyErr_NoMemory();
808
return NULL;
809
}
810
for (size_t i = 0; i < self->mt_allocated; i++) {
811
Py_XINCREF(self->mt_table[i].me_key);
812
}
813
memcpy(new->mt_table, self->mt_table,
814
sizeof(PyMemoEntry) * self->mt_allocated);
815
816
return new;
817
}
818
819
static Py_ssize_t
820
PyMemoTable_Size(PyMemoTable *self)
821
{
822
return self->mt_used;
823
}
824
825
static int
826
PyMemoTable_Clear(PyMemoTable *self)
827
{
828
Py_ssize_t i = self->mt_allocated;
829
830
while (--i >= 0) {
831
Py_XDECREF(self->mt_table[i].me_key);
832
}
833
self->mt_used = 0;
834
memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
835
return 0;
836
}
837
838
static void
839
PyMemoTable_Del(PyMemoTable *self)
840
{
841
if (self == NULL)
842
return;
843
PyMemoTable_Clear(self);
844
845
PyMem_Free(self->mt_table);
846
PyMem_Free(self);
847
}
848
849
/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
850
can be considerably simpler than dictobject.c's lookdict(). */
851
static PyMemoEntry *
852
_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
853
{
854
size_t i;
855
size_t perturb;
856
size_t mask = self->mt_mask;
857
PyMemoEntry *table = self->mt_table;
858
PyMemoEntry *entry;
859
Py_hash_t hash = (Py_hash_t)key >> 3;
860
861
i = hash & mask;
862
entry = &table[i];
863
if (entry->me_key == NULL || entry->me_key == key)
864
return entry;
865
866
for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
867
i = (i << 2) + i + perturb + 1;
868
entry = &table[i & mask];
869
if (entry->me_key == NULL || entry->me_key == key)
870
return entry;
871
}
872
Py_UNREACHABLE();
873
}
874
875
/* Returns -1 on failure, 0 on success. */
876
static int
877
_PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
878
{
879
PyMemoEntry *oldtable = NULL;
880
PyMemoEntry *oldentry, *newentry;
881
size_t new_size = MT_MINSIZE;
882
size_t to_process;
883
884
assert(min_size > 0);
885
886
if (min_size > PY_SSIZE_T_MAX) {
887
PyErr_NoMemory();
888
return -1;
889
}
890
891
/* Find the smallest valid table size >= min_size. */
892
while (new_size < min_size) {
893
new_size <<= 1;
894
}
895
/* new_size needs to be a power of two. */
896
assert((new_size & (new_size - 1)) == 0);
897
898
/* Allocate new table. */
899
oldtable = self->mt_table;
900
self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
901
if (self->mt_table == NULL) {
902
self->mt_table = oldtable;
903
PyErr_NoMemory();
904
return -1;
905
}
906
self->mt_allocated = new_size;
907
self->mt_mask = new_size - 1;
908
memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
909
910
/* Copy entries from the old table. */
911
to_process = self->mt_used;
912
for (oldentry = oldtable; to_process > 0; oldentry++) {
913
if (oldentry->me_key != NULL) {
914
to_process--;
915
/* newentry is a pointer to a chunk of the new
916
mt_table, so we're setting the key:value pair
917
in-place. */
918
newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
919
newentry->me_key = oldentry->me_key;
920
newentry->me_value = oldentry->me_value;
921
}
922
}
923
924
/* Deallocate the old table. */
925
PyMem_Free(oldtable);
926
return 0;
927
}
928
929
/* Returns NULL on failure, a pointer to the value otherwise. */
930
static Py_ssize_t *
931
PyMemoTable_Get(PyMemoTable *self, PyObject *key)
932
{
933
PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
934
if (entry->me_key == NULL)
935
return NULL;
936
return &entry->me_value;
937
}
938
939
/* Returns -1 on failure, 0 on success. */
940
static int
941
PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
942
{
943
PyMemoEntry *entry;
944
945
assert(key != NULL);
946
947
entry = _PyMemoTable_Lookup(self, key);
948
if (entry->me_key != NULL) {
949
entry->me_value = value;
950
return 0;
951
}
952
entry->me_key = Py_NewRef(key);
953
entry->me_value = value;
954
self->mt_used++;
955
956
/* If we added a key, we can safely resize. Otherwise just return!
957
* If used >= 2/3 size, adjust size. Normally, this quaduples the size.
958
*
959
* Quadrupling the size improves average table sparseness
960
* (reducing collisions) at the cost of some memory. It also halves
961
* the number of expensive resize operations in a growing memo table.
962
*
963
* Very large memo tables (over 50K items) use doubling instead.
964
* This may help applications with severe memory constraints.
965
*/
966
if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) {
967
return 0;
968
}
969
// self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow.
970
size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used;
971
return _PyMemoTable_ResizeTable(self, desired_size);
972
}
973
974
#undef MT_MINSIZE
975
#undef PERTURB_SHIFT
976
977
/*************************************************************************/
978
979
980
static int
981
_Pickler_ClearBuffer(PicklerObject *self)
982
{
983
Py_XSETREF(self->output_buffer,
984
PyBytes_FromStringAndSize(NULL, self->max_output_len));
985
if (self->output_buffer == NULL)
986
return -1;
987
self->output_len = 0;
988
self->frame_start = -1;
989
return 0;
990
}
991
992
static void
993
_write_size64(char *out, size_t value)
994
{
995
size_t i;
996
997
static_assert(sizeof(size_t) <= 8, "size_t is larger than 64-bit");
998
999
for (i = 0; i < sizeof(size_t); i++) {
1000
out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
1001
}
1002
for (i = sizeof(size_t); i < 8; i++) {
1003
out[i] = 0;
1004
}
1005
}
1006
1007
static int
1008
_Pickler_CommitFrame(PicklerObject *self)
1009
{
1010
size_t frame_len;
1011
char *qdata;
1012
1013
if (!self->framing || self->frame_start == -1)
1014
return 0;
1015
frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1016
qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
1017
if (frame_len >= FRAME_SIZE_MIN) {
1018
qdata[0] = FRAME;
1019
_write_size64(qdata + 1, frame_len);
1020
}
1021
else {
1022
memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len);
1023
self->output_len -= FRAME_HEADER_SIZE;
1024
}
1025
self->frame_start = -1;
1026
return 0;
1027
}
1028
1029
static PyObject *
1030
_Pickler_GetString(PicklerObject *self)
1031
{
1032
PyObject *output_buffer = self->output_buffer;
1033
1034
assert(self->output_buffer != NULL);
1035
1036
if (_Pickler_CommitFrame(self))
1037
return NULL;
1038
1039
self->output_buffer = NULL;
1040
/* Resize down to exact size */
1041
if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
1042
return NULL;
1043
return output_buffer;
1044
}
1045
1046
static int
1047
_Pickler_FlushToFile(PicklerObject *self)
1048
{
1049
PyObject *output, *result;
1050
1051
assert(self->write != NULL);
1052
1053
/* This will commit the frame first */
1054
output = _Pickler_GetString(self);
1055
if (output == NULL)
1056
return -1;
1057
1058
result = _Pickle_FastCall(self->write, output);
1059
Py_XDECREF(result);
1060
return (result == NULL) ? -1 : 0;
1061
}
1062
1063
static int
1064
_Pickler_OpcodeBoundary(PicklerObject *self)
1065
{
1066
Py_ssize_t frame_len;
1067
1068
if (!self->framing || self->frame_start == -1) {
1069
return 0;
1070
}
1071
frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
1072
if (frame_len >= FRAME_SIZE_TARGET) {
1073
if(_Pickler_CommitFrame(self)) {
1074
return -1;
1075
}
1076
/* Flush the content of the committed frame to the underlying
1077
* file and reuse the pickler buffer for the next frame so as
1078
* to limit memory usage when dumping large complex objects to
1079
* a file.
1080
*
1081
* self->write is NULL when called via dumps.
1082
*/
1083
if (self->write != NULL) {
1084
if (_Pickler_FlushToFile(self) < 0) {
1085
return -1;
1086
}
1087
if (_Pickler_ClearBuffer(self) < 0) {
1088
return -1;
1089
}
1090
}
1091
}
1092
return 0;
1093
}
1094
1095
static Py_ssize_t
1096
_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
1097
{
1098
Py_ssize_t i, n, required;
1099
char *buffer;
1100
int need_new_frame;
1101
1102
assert(s != NULL);
1103
need_new_frame = (self->framing && self->frame_start == -1);
1104
1105
if (need_new_frame)
1106
n = data_len + FRAME_HEADER_SIZE;
1107
else
1108
n = data_len;
1109
1110
required = self->output_len + n;
1111
if (required > self->max_output_len) {
1112
/* Make place in buffer for the pickle chunk */
1113
if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
1114
PyErr_NoMemory();
1115
return -1;
1116
}
1117
self->max_output_len = (self->output_len + n) / 2 * 3;
1118
if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
1119
return -1;
1120
}
1121
buffer = PyBytes_AS_STRING(self->output_buffer);
1122
if (need_new_frame) {
1123
/* Setup new frame */
1124
Py_ssize_t frame_start = self->output_len;
1125
self->frame_start = frame_start;
1126
for (i = 0; i < FRAME_HEADER_SIZE; i++) {
1127
/* Write an invalid value, for debugging */
1128
buffer[frame_start + i] = 0xFE;
1129
}
1130
self->output_len += FRAME_HEADER_SIZE;
1131
}
1132
if (data_len < 8) {
1133
/* This is faster than memcpy when the string is short. */
1134
for (i = 0; i < data_len; i++) {
1135
buffer[self->output_len + i] = s[i];
1136
}
1137
}
1138
else {
1139
memcpy(buffer + self->output_len, s, data_len);
1140
}
1141
self->output_len += data_len;
1142
return data_len;
1143
}
1144
1145
static PicklerObject *
1146
_Pickler_New(PickleState *st)
1147
{
1148
PyMemoTable *memo = PyMemoTable_New();
1149
if (memo == NULL) {
1150
return NULL;
1151
}
1152
1153
const Py_ssize_t max_output_len = WRITE_BUF_SIZE;
1154
PyObject *output_buffer = PyBytes_FromStringAndSize(NULL, max_output_len);
1155
if (output_buffer == NULL) {
1156
goto error;
1157
}
1158
1159
PicklerObject *self = PyObject_GC_New(PicklerObject, st->Pickler_Type);
1160
if (self == NULL) {
1161
goto error;
1162
}
1163
1164
self->memo = memo;
1165
self->pers_func = NULL;
1166
self->pers_func_self = NULL;
1167
self->dispatch_table = NULL;
1168
self->reducer_override = NULL;
1169
self->write = NULL;
1170
self->output_buffer = output_buffer;
1171
self->output_len = 0;
1172
self->max_output_len = max_output_len;
1173
self->proto = 0;
1174
self->bin = 0;
1175
self->framing = 0;
1176
self->frame_start = -1;
1177
self->buf_size = 0;
1178
self->fast = 0;
1179
self->fast_nesting = 0;
1180
self->fix_imports = 0;
1181
self->fast_memo = NULL;
1182
self->buffer_callback = NULL;
1183
1184
PyObject_GC_Track(self);
1185
return self;
1186
1187
error:
1188
PyMem_Free(memo);
1189
Py_XDECREF(output_buffer);
1190
return NULL;
1191
}
1192
1193
static int
1194
_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
1195
{
1196
long proto;
1197
1198
if (protocol == Py_None) {
1199
proto = DEFAULT_PROTOCOL;
1200
}
1201
else {
1202
proto = PyLong_AsLong(protocol);
1203
if (proto < 0) {
1204
if (proto == -1 && PyErr_Occurred())
1205
return -1;
1206
proto = HIGHEST_PROTOCOL;
1207
}
1208
else if (proto > HIGHEST_PROTOCOL) {
1209
PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1210
HIGHEST_PROTOCOL);
1211
return -1;
1212
}
1213
}
1214
self->proto = (int)proto;
1215
self->bin = proto > 0;
1216
self->fix_imports = fix_imports && proto < 3;
1217
return 0;
1218
}
1219
1220
/* Returns -1 (with an exception set) on failure, 0 on success. This may
1221
be called once on a freshly created Pickler. */
1222
static int
1223
_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1224
{
1225
assert(file != NULL);
1226
if (_PyObject_LookupAttr(file, &_Py_ID(write), &self->write) < 0) {
1227
return -1;
1228
}
1229
if (self->write == NULL) {
1230
PyErr_SetString(PyExc_TypeError,
1231
"file must have a 'write' attribute");
1232
return -1;
1233
}
1234
1235
return 0;
1236
}
1237
1238
static int
1239
_Pickler_SetBufferCallback(PicklerObject *self, PyObject *buffer_callback)
1240
{
1241
if (buffer_callback == Py_None) {
1242
buffer_callback = NULL;
1243
}
1244
if (buffer_callback != NULL && self->proto < 5) {
1245
PyErr_SetString(PyExc_ValueError,
1246
"buffer_callback needs protocol >= 5");
1247
return -1;
1248
}
1249
1250
self->buffer_callback = Py_XNewRef(buffer_callback);
1251
return 0;
1252
}
1253
1254
/* Returns the size of the input on success, -1 on failure. This takes its
1255
own reference to `input`. */
1256
static Py_ssize_t
1257
_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1258
{
1259
if (self->buffer.buf != NULL)
1260
PyBuffer_Release(&self->buffer);
1261
if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1262
return -1;
1263
self->input_buffer = self->buffer.buf;
1264
self->input_len = self->buffer.len;
1265
self->next_read_idx = 0;
1266
self->prefetched_idx = self->input_len;
1267
return self->input_len;
1268
}
1269
1270
static int
1271
bad_readline(PickleState *st)
1272
{
1273
PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
1274
return -1;
1275
}
1276
1277
/* Skip any consumed data that was only prefetched using peek() */
1278
static int
1279
_Unpickler_SkipConsumed(UnpicklerObject *self)
1280
{
1281
Py_ssize_t consumed;
1282
PyObject *r;
1283
1284
consumed = self->next_read_idx - self->prefetched_idx;
1285
if (consumed <= 0)
1286
return 0;
1287
1288
assert(self->peek); /* otherwise we did something wrong */
1289
/* This makes a useless copy... */
1290
r = PyObject_CallFunction(self->read, "n", consumed);
1291
if (r == NULL)
1292
return -1;
1293
Py_DECREF(r);
1294
1295
self->prefetched_idx = self->next_read_idx;
1296
return 0;
1297
}
1298
1299
static const Py_ssize_t READ_WHOLE_LINE = -1;
1300
1301
/* If reading from a file, we need to only pull the bytes we need, since there
1302
may be multiple pickle objects arranged contiguously in the same input
1303
buffer.
1304
1305
If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1306
bytes from the input stream/buffer.
1307
1308
Update the unpickler's input buffer with the newly-read data. Returns -1 on
1309
failure; on success, returns the number of bytes read from the file.
1310
1311
On success, self->input_len will be 0; this is intentional so that when
1312
unpickling from a file, the "we've run out of data" code paths will trigger,
1313
causing the Unpickler to go back to the file for more data. Use the returned
1314
size to tell you how much data you can process. */
1315
static Py_ssize_t
1316
_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1317
{
1318
PyObject *data;
1319
Py_ssize_t read_size;
1320
1321
assert(self->read != NULL);
1322
1323
if (_Unpickler_SkipConsumed(self) < 0)
1324
return -1;
1325
1326
if (n == READ_WHOLE_LINE) {
1327
data = PyObject_CallNoArgs(self->readline);
1328
}
1329
else {
1330
PyObject *len;
1331
/* Prefetch some data without advancing the file pointer, if possible */
1332
if (self->peek && n < PREFETCH) {
1333
len = PyLong_FromSsize_t(PREFETCH);
1334
if (len == NULL)
1335
return -1;
1336
data = _Pickle_FastCall(self->peek, len);
1337
if (data == NULL) {
1338
if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1339
return -1;
1340
/* peek() is probably not supported by the given file object */
1341
PyErr_Clear();
1342
Py_CLEAR(self->peek);
1343
}
1344
else {
1345
read_size = _Unpickler_SetStringInput(self, data);
1346
Py_DECREF(data);
1347
self->prefetched_idx = 0;
1348
if (n <= read_size)
1349
return n;
1350
}
1351
}
1352
len = PyLong_FromSsize_t(n);
1353
if (len == NULL)
1354
return -1;
1355
data = _Pickle_FastCall(self->read, len);
1356
}
1357
if (data == NULL)
1358
return -1;
1359
1360
read_size = _Unpickler_SetStringInput(self, data);
1361
Py_DECREF(data);
1362
return read_size;
1363
}
1364
1365
/* Don't call it directly: use _Unpickler_Read() */
1366
static Py_ssize_t
1367
_Unpickler_ReadImpl(UnpicklerObject *self, PickleState *st, char **s, Py_ssize_t n)
1368
{
1369
Py_ssize_t num_read;
1370
1371
*s = NULL;
1372
if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1373
PyErr_SetString(st->UnpicklingError,
1374
"read would overflow (invalid bytecode)");
1375
return -1;
1376
}
1377
1378
/* This case is handled by the _Unpickler_Read() macro for efficiency */
1379
assert(self->next_read_idx + n > self->input_len);
1380
1381
if (!self->read)
1382
return bad_readline(st);
1383
1384
/* Extend the buffer to satisfy desired size */
1385
num_read = _Unpickler_ReadFromFile(self, n);
1386
if (num_read < 0)
1387
return -1;
1388
if (num_read < n)
1389
return bad_readline(st);
1390
*s = self->input_buffer;
1391
self->next_read_idx = n;
1392
return n;
1393
}
1394
1395
/* Read `n` bytes from the unpickler's data source, storing the result in `buf`.
1396
*
1397
* This should only be used for non-small data reads where potentially
1398
* avoiding a copy is beneficial. This method does not try to prefetch
1399
* more data into the input buffer.
1400
*
1401
* _Unpickler_Read() is recommended in most cases.
1402
*/
1403
static Py_ssize_t
1404
_Unpickler_ReadInto(PickleState *state, UnpicklerObject *self, char *buf,
1405
Py_ssize_t n)
1406
{
1407
assert(n != READ_WHOLE_LINE);
1408
1409
/* Read from available buffer data, if any */
1410
Py_ssize_t in_buffer = self->input_len - self->next_read_idx;
1411
if (in_buffer > 0) {
1412
Py_ssize_t to_read = Py_MIN(in_buffer, n);
1413
memcpy(buf, self->input_buffer + self->next_read_idx, to_read);
1414
self->next_read_idx += to_read;
1415
buf += to_read;
1416
n -= to_read;
1417
if (n == 0) {
1418
/* Entire read was satisfied from buffer */
1419
return n;
1420
}
1421
}
1422
1423
/* Read from file */
1424
if (!self->read) {
1425
/* We're unpickling memory, this means the input is truncated */
1426
return bad_readline(state);
1427
}
1428
if (_Unpickler_SkipConsumed(self) < 0) {
1429
return -1;
1430
}
1431
1432
if (!self->readinto) {
1433
/* readinto() not supported on file-like object, fall back to read()
1434
* and copy into destination buffer (bpo-39681) */
1435
PyObject* len = PyLong_FromSsize_t(n);
1436
if (len == NULL) {
1437
return -1;
1438
}
1439
PyObject* data = _Pickle_FastCall(self->read, len);
1440
if (data == NULL) {
1441
return -1;
1442
}
1443
if (!PyBytes_Check(data)) {
1444
PyErr_Format(PyExc_ValueError,
1445
"read() returned non-bytes object (%R)",
1446
Py_TYPE(data));
1447
Py_DECREF(data);
1448
return -1;
1449
}
1450
Py_ssize_t read_size = PyBytes_GET_SIZE(data);
1451
if (read_size < n) {
1452
Py_DECREF(data);
1453
return bad_readline(state);
1454
}
1455
memcpy(buf, PyBytes_AS_STRING(data), n);
1456
Py_DECREF(data);
1457
return n;
1458
}
1459
1460
/* Call readinto() into user buffer */
1461
PyObject *buf_obj = PyMemoryView_FromMemory(buf, n, PyBUF_WRITE);
1462
if (buf_obj == NULL) {
1463
return -1;
1464
}
1465
PyObject *read_size_obj = _Pickle_FastCall(self->readinto, buf_obj);
1466
if (read_size_obj == NULL) {
1467
return -1;
1468
}
1469
Py_ssize_t read_size = PyLong_AsSsize_t(read_size_obj);
1470
Py_DECREF(read_size_obj);
1471
1472
if (read_size < 0) {
1473
if (!PyErr_Occurred()) {
1474
PyErr_SetString(PyExc_ValueError,
1475
"readinto() returned negative size");
1476
}
1477
return -1;
1478
}
1479
if (read_size < n) {
1480
return bad_readline(state);
1481
}
1482
return n;
1483
}
1484
1485
/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1486
1487
This should be used for all data reads, rather than accessing the unpickler's
1488
input buffer directly. This method deals correctly with reading from input
1489
streams, which the input buffer doesn't deal with.
1490
1491
Note that when reading from a file-like object, self->next_read_idx won't
1492
be updated (it should remain at 0 for the entire unpickling process). You
1493
should use this function's return value to know how many bytes you can
1494
consume.
1495
1496
Returns -1 (with an exception set) on failure. On success, return the
1497
number of chars read. */
1498
#define _Unpickler_Read(self, state, s, n) \
1499
(((n) <= (self)->input_len - (self)->next_read_idx) \
1500
? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
1501
(self)->next_read_idx += (n), \
1502
(n)) \
1503
: _Unpickler_ReadImpl(self, state, (s), (n)))
1504
1505
static Py_ssize_t
1506
_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1507
char **result)
1508
{
1509
char *input_line = PyMem_Realloc(self->input_line, len + 1);
1510
if (input_line == NULL) {
1511
PyErr_NoMemory();
1512
return -1;
1513
}
1514
1515
memcpy(input_line, line, len);
1516
input_line[len] = '\0';
1517
self->input_line = input_line;
1518
*result = self->input_line;
1519
return len;
1520
}
1521
1522
/* Read a line from the input stream/buffer. If we run off the end of the input
1523
before hitting \n, raise an error.
1524
1525
Returns the number of chars read, or -1 on failure. */
1526
static Py_ssize_t
1527
_Unpickler_Readline(PickleState *state, UnpicklerObject *self, char **result)
1528
{
1529
Py_ssize_t i, num_read;
1530
1531
for (i = self->next_read_idx; i < self->input_len; i++) {
1532
if (self->input_buffer[i] == '\n') {
1533
char *line_start = self->input_buffer + self->next_read_idx;
1534
num_read = i - self->next_read_idx + 1;
1535
self->next_read_idx = i + 1;
1536
return _Unpickler_CopyLine(self, line_start, num_read, result);
1537
}
1538
}
1539
if (!self->read)
1540
return bad_readline(state);
1541
1542
num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1543
if (num_read < 0)
1544
return -1;
1545
if (num_read == 0 || self->input_buffer[num_read - 1] != '\n')
1546
return bad_readline(state);
1547
self->next_read_idx = num_read;
1548
return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
1549
}
1550
1551
/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1552
will be modified in place. */
1553
static int
1554
_Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size)
1555
{
1556
size_t i;
1557
1558
assert(new_size > self->memo_size);
1559
1560
PyObject **memo_new = self->memo;
1561
PyMem_RESIZE(memo_new, PyObject *, new_size);
1562
if (memo_new == NULL) {
1563
PyErr_NoMemory();
1564
return -1;
1565
}
1566
self->memo = memo_new;
1567
for (i = self->memo_size; i < new_size; i++)
1568
self->memo[i] = NULL;
1569
self->memo_size = new_size;
1570
return 0;
1571
}
1572
1573
/* Returns NULL if idx is out of bounds. */
1574
static PyObject *
1575
_Unpickler_MemoGet(UnpicklerObject *self, size_t idx)
1576
{
1577
if (idx >= self->memo_size)
1578
return NULL;
1579
1580
return self->memo[idx];
1581
}
1582
1583
/* Returns -1 (with an exception set) on failure, 0 on success.
1584
This takes its own reference to `value`. */
1585
static int
1586
_Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
1587
{
1588
PyObject *old_item;
1589
1590
if (idx >= self->memo_size) {
1591
if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1592
return -1;
1593
assert(idx < self->memo_size);
1594
}
1595
old_item = self->memo[idx];
1596
self->memo[idx] = Py_NewRef(value);
1597
if (old_item != NULL) {
1598
Py_DECREF(old_item);
1599
}
1600
else {
1601
self->memo_len++;
1602
}
1603
return 0;
1604
}
1605
1606
static PyObject **
1607
_Unpickler_NewMemo(Py_ssize_t new_size)
1608
{
1609
PyObject **memo = PyMem_NEW(PyObject *, new_size);
1610
if (memo == NULL) {
1611
PyErr_NoMemory();
1612
return NULL;
1613
}
1614
memset(memo, 0, new_size * sizeof(PyObject *));
1615
return memo;
1616
}
1617
1618
/* Free the unpickler's memo, taking care to decref any items left in it. */
1619
static void
1620
_Unpickler_MemoCleanup(UnpicklerObject *self)
1621
{
1622
Py_ssize_t i;
1623
PyObject **memo = self->memo;
1624
1625
if (self->memo == NULL)
1626
return;
1627
self->memo = NULL;
1628
i = self->memo_size;
1629
while (--i >= 0) {
1630
Py_XDECREF(memo[i]);
1631
}
1632
PyMem_Free(memo);
1633
}
1634
1635
static UnpicklerObject *
1636
_Unpickler_New(PyObject *module)
1637
{
1638
const int MEMO_SIZE = 32;
1639
PyObject **memo = _Unpickler_NewMemo(MEMO_SIZE);
1640
if (memo == NULL) {
1641
return NULL;
1642
}
1643
1644
PickleState *st = _Pickle_GetState(module);
1645
PyObject *stack = Pdata_New(st);
1646
if (stack == NULL) {
1647
goto error;
1648
}
1649
1650
UnpicklerObject *self = PyObject_GC_New(UnpicklerObject,
1651
st->Unpickler_Type);
1652
if (self == NULL) {
1653
goto error;
1654
}
1655
1656
self->stack = (Pdata *)stack;
1657
self->memo = memo;
1658
self->memo_size = MEMO_SIZE;
1659
self->memo_len = 0;
1660
self->pers_func = NULL;
1661
self->pers_func_self = NULL;
1662
memset(&self->buffer, 0, sizeof(Py_buffer));
1663
self->input_buffer = NULL;
1664
self->input_line = NULL;
1665
self->input_len = 0;
1666
self->next_read_idx = 0;
1667
self->prefetched_idx = 0;
1668
self->read = NULL;
1669
self->readinto = NULL;
1670
self->readline = NULL;
1671
self->peek = NULL;
1672
self->buffers = NULL;
1673
self->encoding = NULL;
1674
self->errors = NULL;
1675
self->marks = NULL;
1676
self->num_marks = 0;
1677
self->marks_size = 0;
1678
self->proto = 0;
1679
self->fix_imports = 0;
1680
1681
PyObject_GC_Track(self);
1682
return self;
1683
1684
error:
1685
PyMem_Free(memo);
1686
Py_XDECREF(stack);
1687
return NULL;
1688
}
1689
1690
/* Returns -1 (with an exception set) on failure, 0 on success. This may
1691
be called once on a freshly created Unpickler. */
1692
static int
1693
_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1694
{
1695
/* Optional file methods */
1696
if (_PyObject_LookupAttr(file, &_Py_ID(peek), &self->peek) < 0) {
1697
goto error;
1698
}
1699
if (_PyObject_LookupAttr(file, &_Py_ID(readinto), &self->readinto) < 0) {
1700
goto error;
1701
}
1702
if (_PyObject_LookupAttr(file, &_Py_ID(read), &self->read) < 0) {
1703
goto error;
1704
}
1705
if (_PyObject_LookupAttr(file, &_Py_ID(readline), &self->readline) < 0) {
1706
goto error;
1707
}
1708
if (!self->readline || !self->read) {
1709
PyErr_SetString(PyExc_TypeError,
1710
"file must have 'read' and 'readline' attributes");
1711
goto error;
1712
}
1713
return 0;
1714
1715
error:
1716
Py_CLEAR(self->read);
1717
Py_CLEAR(self->readinto);
1718
Py_CLEAR(self->readline);
1719
Py_CLEAR(self->peek);
1720
return -1;
1721
}
1722
1723
/* Returns -1 (with an exception set) on failure, 0 on success. This may
1724
be called once on a freshly created Unpickler. */
1725
static int
1726
_Unpickler_SetInputEncoding(UnpicklerObject *self,
1727
const char *encoding,
1728
const char *errors)
1729
{
1730
if (encoding == NULL)
1731
encoding = "ASCII";
1732
if (errors == NULL)
1733
errors = "strict";
1734
1735
self->encoding = _PyMem_Strdup(encoding);
1736
self->errors = _PyMem_Strdup(errors);
1737
if (self->encoding == NULL || self->errors == NULL) {
1738
PyErr_NoMemory();
1739
return -1;
1740
}
1741
return 0;
1742
}
1743
1744
/* Returns -1 (with an exception set) on failure, 0 on success. This may
1745
be called once on a freshly created Unpickler. */
1746
static int
1747
_Unpickler_SetBuffers(UnpicklerObject *self, PyObject *buffers)
1748
{
1749
if (buffers == NULL || buffers == Py_None) {
1750
self->buffers = NULL;
1751
}
1752
else {
1753
self->buffers = PyObject_GetIter(buffers);
1754
if (self->buffers == NULL) {
1755
return -1;
1756
}
1757
}
1758
return 0;
1759
}
1760
1761
/* Generate a GET opcode for an object stored in the memo. */
1762
static int
1763
memo_get(PickleState *st, PicklerObject *self, PyObject *key)
1764
{
1765
Py_ssize_t *value;
1766
char pdata[30];
1767
Py_ssize_t len;
1768
1769
value = PyMemoTable_Get(self->memo, key);
1770
if (value == NULL) {
1771
PyErr_SetObject(PyExc_KeyError, key);
1772
return -1;
1773
}
1774
1775
if (!self->bin) {
1776
pdata[0] = GET;
1777
PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1778
"%zd\n", *value);
1779
len = strlen(pdata);
1780
}
1781
else {
1782
if (*value < 256) {
1783
pdata[0] = BINGET;
1784
pdata[1] = (unsigned char)(*value & 0xff);
1785
len = 2;
1786
}
1787
else if ((size_t)*value <= 0xffffffffUL) {
1788
pdata[0] = LONG_BINGET;
1789
pdata[1] = (unsigned char)(*value & 0xff);
1790
pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1791
pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1792
pdata[4] = (unsigned char)((*value >> 24) & 0xff);
1793
len = 5;
1794
}
1795
else { /* unlikely */
1796
PyErr_SetString(st->PicklingError,
1797
"memo id too large for LONG_BINGET");
1798
return -1;
1799
}
1800
}
1801
1802
if (_Pickler_Write(self, pdata, len) < 0)
1803
return -1;
1804
1805
return 0;
1806
}
1807
1808
/* Store an object in the memo, assign it a new unique ID based on the number
1809
of objects currently stored in the memo and generate a PUT opcode. */
1810
static int
1811
memo_put(PickleState *st, PicklerObject *self, PyObject *obj)
1812
{
1813
char pdata[30];
1814
Py_ssize_t len;
1815
Py_ssize_t idx;
1816
1817
const char memoize_op = MEMOIZE;
1818
1819
if (self->fast)
1820
return 0;
1821
1822
idx = PyMemoTable_Size(self->memo);
1823
if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1824
return -1;
1825
1826
if (self->proto >= 4) {
1827
if (_Pickler_Write(self, &memoize_op, 1) < 0)
1828
return -1;
1829
return 0;
1830
}
1831
else if (!self->bin) {
1832
pdata[0] = PUT;
1833
PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1834
"%zd\n", idx);
1835
len = strlen(pdata);
1836
}
1837
else {
1838
if (idx < 256) {
1839
pdata[0] = BINPUT;
1840
pdata[1] = (unsigned char)idx;
1841
len = 2;
1842
}
1843
else if ((size_t)idx <= 0xffffffffUL) {
1844
pdata[0] = LONG_BINPUT;
1845
pdata[1] = (unsigned char)(idx & 0xff);
1846
pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1847
pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1848
pdata[4] = (unsigned char)((idx >> 24) & 0xff);
1849
len = 5;
1850
}
1851
else { /* unlikely */
1852
PyErr_SetString(st->PicklingError,
1853
"memo id too large for LONG_BINPUT");
1854
return -1;
1855
}
1856
}
1857
if (_Pickler_Write(self, pdata, len) < 0)
1858
return -1;
1859
1860
return 0;
1861
}
1862
1863
static PyObject *
1864
get_dotted_path(PyObject *obj, PyObject *name)
1865
{
1866
PyObject *dotted_path;
1867
Py_ssize_t i, n;
1868
_Py_DECLARE_STR(dot, ".");
1869
dotted_path = PyUnicode_Split(name, &_Py_STR(dot), -1);
1870
if (dotted_path == NULL)
1871
return NULL;
1872
n = PyList_GET_SIZE(dotted_path);
1873
assert(n >= 1);
1874
for (i = 0; i < n; i++) {
1875
PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
1876
if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
1877
if (obj == NULL)
1878
PyErr_Format(PyExc_AttributeError,
1879
"Can't pickle local object %R", name);
1880
else
1881
PyErr_Format(PyExc_AttributeError,
1882
"Can't pickle local attribute %R on %R", name, obj);
1883
Py_DECREF(dotted_path);
1884
return NULL;
1885
}
1886
}
1887
return dotted_path;
1888
}
1889
1890
static PyObject *
1891
get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
1892
{
1893
Py_ssize_t i, n;
1894
PyObject *parent = NULL;
1895
1896
assert(PyList_CheckExact(names));
1897
Py_INCREF(obj);
1898
n = PyList_GET_SIZE(names);
1899
for (i = 0; i < n; i++) {
1900
PyObject *name = PyList_GET_ITEM(names, i);
1901
Py_XSETREF(parent, obj);
1902
(void)_PyObject_LookupAttr(parent, name, &obj);
1903
if (obj == NULL) {
1904
Py_DECREF(parent);
1905
return NULL;
1906
}
1907
}
1908
if (pparent != NULL)
1909
*pparent = parent;
1910
else
1911
Py_XDECREF(parent);
1912
return obj;
1913
}
1914
1915
1916
static PyObject *
1917
getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1918
{
1919
PyObject *dotted_path, *attr;
1920
1921
if (allow_qualname) {
1922
dotted_path = get_dotted_path(obj, name);
1923
if (dotted_path == NULL)
1924
return NULL;
1925
attr = get_deep_attribute(obj, dotted_path, NULL);
1926
Py_DECREF(dotted_path);
1927
}
1928
else {
1929
(void)_PyObject_LookupAttr(obj, name, &attr);
1930
}
1931
if (attr == NULL && !PyErr_Occurred()) {
1932
PyErr_Format(PyExc_AttributeError,
1933
"Can't get attribute %R on %R", name, obj);
1934
}
1935
return attr;
1936
}
1937
1938
static int
1939
_checkmodule(PyObject *module_name, PyObject *module,
1940
PyObject *global, PyObject *dotted_path)
1941
{
1942
if (module == Py_None) {
1943
return -1;
1944
}
1945
if (PyUnicode_Check(module_name) &&
1946
_PyUnicode_EqualToASCIIString(module_name, "__main__")) {
1947
return -1;
1948
}
1949
1950
PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
1951
if (candidate == NULL) {
1952
return -1;
1953
}
1954
if (candidate != global) {
1955
Py_DECREF(candidate);
1956
return -1;
1957
}
1958
Py_DECREF(candidate);
1959
return 0;
1960
}
1961
1962
static PyObject *
1963
whichmodule(PyObject *global, PyObject *dotted_path)
1964
{
1965
PyObject *module_name;
1966
PyObject *module = NULL;
1967
Py_ssize_t i;
1968
PyObject *modules;
1969
1970
if (_PyObject_LookupAttr(global, &_Py_ID(__module__), &module_name) < 0) {
1971
return NULL;
1972
}
1973
if (module_name) {
1974
/* In some rare cases (e.g., bound methods of extension types),
1975
__module__ can be None. If it is so, then search sys.modules for
1976
the module of global. */
1977
if (module_name != Py_None)
1978
return module_name;
1979
Py_CLEAR(module_name);
1980
}
1981
assert(module_name == NULL);
1982
1983
/* Fallback on walking sys.modules */
1984
PyThreadState *tstate = _PyThreadState_GET();
1985
modules = _PySys_GetAttr(tstate, &_Py_ID(modules));
1986
if (modules == NULL) {
1987
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
1988
return NULL;
1989
}
1990
if (PyDict_CheckExact(modules)) {
1991
i = 0;
1992
while (PyDict_Next(modules, &i, &module_name, &module)) {
1993
if (_checkmodule(module_name, module, global, dotted_path) == 0) {
1994
return Py_NewRef(module_name);
1995
}
1996
if (PyErr_Occurred()) {
1997
return NULL;
1998
}
1999
}
2000
}
2001
else {
2002
PyObject *iterator = PyObject_GetIter(modules);
2003
if (iterator == NULL) {
2004
return NULL;
2005
}
2006
while ((module_name = PyIter_Next(iterator))) {
2007
module = PyObject_GetItem(modules, module_name);
2008
if (module == NULL) {
2009
Py_DECREF(module_name);
2010
Py_DECREF(iterator);
2011
return NULL;
2012
}
2013
if (_checkmodule(module_name, module, global, dotted_path) == 0) {
2014
Py_DECREF(module);
2015
Py_DECREF(iterator);
2016
return module_name;
2017
}
2018
Py_DECREF(module);
2019
Py_DECREF(module_name);
2020
if (PyErr_Occurred()) {
2021
Py_DECREF(iterator);
2022
return NULL;
2023
}
2024
}
2025
Py_DECREF(iterator);
2026
}
2027
2028
/* If no module is found, use __main__. */
2029
module_name = &_Py_ID(__main__);
2030
return Py_NewRef(module_name);
2031
}
2032
2033
/* fast_save_enter() and fast_save_leave() are guards against recursive
2034
objects when Pickler is used with the "fast mode" (i.e., with object
2035
memoization disabled). If the nesting of a list or dict object exceed
2036
FAST_NESTING_LIMIT, these guards will start keeping an internal
2037
reference to the seen list or dict objects and check whether these objects
2038
are recursive. These are not strictly necessary, since save() has a
2039
hard-coded recursion limit, but they give a nicer error message than the
2040
typical RuntimeError. */
2041
static int
2042
fast_save_enter(PicklerObject *self, PyObject *obj)
2043
{
2044
/* if fast_nesting < 0, we're doing an error exit. */
2045
if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
2046
PyObject *key = NULL;
2047
if (self->fast_memo == NULL) {
2048
self->fast_memo = PyDict_New();
2049
if (self->fast_memo == NULL) {
2050
self->fast_nesting = -1;
2051
return 0;
2052
}
2053
}
2054
key = PyLong_FromVoidPtr(obj);
2055
if (key == NULL) {
2056
self->fast_nesting = -1;
2057
return 0;
2058
}
2059
int r = PyDict_Contains(self->fast_memo, key);
2060
if (r > 0) {
2061
PyErr_Format(PyExc_ValueError,
2062
"fast mode: can't pickle cyclic objects "
2063
"including object type %.200s at %p",
2064
Py_TYPE(obj)->tp_name, obj);
2065
}
2066
else if (r == 0) {
2067
r = PyDict_SetItem(self->fast_memo, key, Py_None);
2068
}
2069
Py_DECREF(key);
2070
if (r != 0) {
2071
self->fast_nesting = -1;
2072
return 0;
2073
}
2074
}
2075
return 1;
2076
}
2077
2078
static int
2079
fast_save_leave(PicklerObject *self, PyObject *obj)
2080
{
2081
if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
2082
PyObject *key = PyLong_FromVoidPtr(obj);
2083
if (key == NULL)
2084
return 0;
2085
if (PyDict_DelItem(self->fast_memo, key) < 0) {
2086
Py_DECREF(key);
2087
return 0;
2088
}
2089
Py_DECREF(key);
2090
}
2091
return 1;
2092
}
2093
2094
static int
2095
save_none(PicklerObject *self, PyObject *obj)
2096
{
2097
const char none_op = NONE;
2098
if (_Pickler_Write(self, &none_op, 1) < 0)
2099
return -1;
2100
2101
return 0;
2102
}
2103
2104
static int
2105
save_bool(PicklerObject *self, PyObject *obj)
2106
{
2107
if (self->proto >= 2) {
2108
const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
2109
if (_Pickler_Write(self, &bool_op, 1) < 0)
2110
return -1;
2111
}
2112
else {
2113
/* These aren't opcodes -- they're ways to pickle bools before protocol 2
2114
* so that unpicklers written before bools were introduced unpickle them
2115
* as ints, but unpicklers after can recognize that bools were intended.
2116
* Note that protocol 2 added direct ways to pickle bools.
2117
*/
2118
const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
2119
if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
2120
return -1;
2121
}
2122
return 0;
2123
}
2124
2125
static int
2126
save_long(PicklerObject *self, PyObject *obj)
2127
{
2128
PyObject *repr = NULL;
2129
Py_ssize_t size;
2130
long val;
2131
int overflow;
2132
int status = 0;
2133
2134
val= PyLong_AsLongAndOverflow(obj, &overflow);
2135
if (!overflow && (sizeof(long) <= 4 ||
2136
(val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
2137
{
2138
/* result fits in a signed 4-byte integer.
2139
2140
Note: we can't use -0x80000000L in the above condition because some
2141
compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
2142
before applying the unary minus when sizeof(long) <= 4. The
2143
resulting value stays unsigned which is commonly not what we want,
2144
so MSVC happily warns us about it. However, that result would have
2145
been fine because we guard for sizeof(long) <= 4 which turns the
2146
condition true in that particular case. */
2147
char pdata[32];
2148
Py_ssize_t len = 0;
2149
2150
if (self->bin) {
2151
pdata[1] = (unsigned char)(val & 0xff);
2152
pdata[2] = (unsigned char)((val >> 8) & 0xff);
2153
pdata[3] = (unsigned char)((val >> 16) & 0xff);
2154
pdata[4] = (unsigned char)((val >> 24) & 0xff);
2155
2156
if ((pdata[4] != 0) || (pdata[3] != 0)) {
2157
pdata[0] = BININT;
2158
len = 5;
2159
}
2160
else if (pdata[2] != 0) {
2161
pdata[0] = BININT2;
2162
len = 3;
2163
}
2164
else {
2165
pdata[0] = BININT1;
2166
len = 2;
2167
}
2168
}
2169
else {
2170
sprintf(pdata, "%c%ld\n", INT, val);
2171
len = strlen(pdata);
2172
}
2173
if (_Pickler_Write(self, pdata, len) < 0)
2174
return -1;
2175
2176
return 0;
2177
}
2178
assert(!PyErr_Occurred());
2179
2180
if (self->proto >= 2) {
2181
/* Linear-time pickling. */
2182
size_t nbits;
2183
size_t nbytes;
2184
unsigned char *pdata;
2185
char header[5];
2186
int i;
2187
int sign = _PyLong_Sign(obj);
2188
2189
if (sign == 0) {
2190
header[0] = LONG1;
2191
header[1] = 0; /* It's 0 -- an empty bytestring. */
2192
if (_Pickler_Write(self, header, 2) < 0)
2193
goto error;
2194
return 0;
2195
}
2196
nbits = _PyLong_NumBits(obj);
2197
if (nbits == (size_t)-1 && PyErr_Occurred())
2198
goto error;
2199
/* How many bytes do we need? There are nbits >> 3 full
2200
* bytes of data, and nbits & 7 leftover bits. If there
2201
* are any leftover bits, then we clearly need another
2202
* byte. What's not so obvious is that we *probably*
2203
* need another byte even if there aren't any leftovers:
2204
* the most-significant bit of the most-significant byte
2205
* acts like a sign bit, and it's usually got a sense
2206
* opposite of the one we need. The exception is ints
2207
* of the form -(2**(8*j-1)) for j > 0. Such an int is
2208
* its own 256's-complement, so has the right sign bit
2209
* even without the extra byte. That's a pain to check
2210
* for in advance, though, so we always grab an extra
2211
* byte at the start, and cut it back later if possible.
2212
*/
2213
nbytes = (nbits >> 3) + 1;
2214
if (nbytes > 0x7fffffffL) {
2215
PyErr_SetString(PyExc_OverflowError,
2216
"int too large to pickle");
2217
goto error;
2218
}
2219
repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
2220
if (repr == NULL)
2221
goto error;
2222
pdata = (unsigned char *)PyBytes_AS_STRING(repr);
2223
i = _PyLong_AsByteArray((PyLongObject *)obj,
2224
pdata, nbytes,
2225
1 /* little endian */ , 1 /* signed */ );
2226
if (i < 0)
2227
goto error;
2228
/* If the int is negative, this may be a byte more than
2229
* needed. This is so iff the MSB is all redundant sign
2230
* bits.
2231
*/
2232
if (sign < 0 &&
2233
nbytes > 1 &&
2234
pdata[nbytes - 1] == 0xff &&
2235
(pdata[nbytes - 2] & 0x80) != 0) {
2236
nbytes--;
2237
}
2238
2239
if (nbytes < 256) {
2240
header[0] = LONG1;
2241
header[1] = (unsigned char)nbytes;
2242
size = 2;
2243
}
2244
else {
2245
header[0] = LONG4;
2246
size = (Py_ssize_t) nbytes;
2247
for (i = 1; i < 5; i++) {
2248
header[i] = (unsigned char)(size & 0xff);
2249
size >>= 8;
2250
}
2251
size = 5;
2252
}
2253
if (_Pickler_Write(self, header, size) < 0 ||
2254
_Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
2255
goto error;
2256
}
2257
else {
2258
const char long_op = LONG;
2259
const char *string;
2260
2261
/* proto < 2: write the repr and newline. This is quadratic-time (in
2262
the number of digits), in both directions. We add a trailing 'L'
2263
to the repr, for compatibility with Python 2.x. */
2264
2265
repr = PyObject_Repr(obj);
2266
if (repr == NULL)
2267
goto error;
2268
2269
string = PyUnicode_AsUTF8AndSize(repr, &size);
2270
if (string == NULL)
2271
goto error;
2272
2273
if (_Pickler_Write(self, &long_op, 1) < 0 ||
2274
_Pickler_Write(self, string, size) < 0 ||
2275
_Pickler_Write(self, "L\n", 2) < 0)
2276
goto error;
2277
}
2278
2279
if (0) {
2280
error:
2281
status = -1;
2282
}
2283
Py_XDECREF(repr);
2284
2285
return status;
2286
}
2287
2288
static int
2289
save_float(PicklerObject *self, PyObject *obj)
2290
{
2291
double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
2292
2293
if (self->bin) {
2294
char pdata[9];
2295
pdata[0] = BINFLOAT;
2296
if (PyFloat_Pack8(x, &pdata[1], 0) < 0)
2297
return -1;
2298
if (_Pickler_Write(self, pdata, 9) < 0)
2299
return -1;
2300
}
2301
else {
2302
int result = -1;
2303
char *buf = NULL;
2304
char op = FLOAT;
2305
2306
if (_Pickler_Write(self, &op, 1) < 0)
2307
goto done;
2308
2309
buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
2310
if (!buf) {
2311
PyErr_NoMemory();
2312
goto done;
2313
}
2314
2315
if (_Pickler_Write(self, buf, strlen(buf)) < 0)
2316
goto done;
2317
2318
if (_Pickler_Write(self, "\n", 1) < 0)
2319
goto done;
2320
2321
result = 0;
2322
done:
2323
PyMem_Free(buf);
2324
return result;
2325
}
2326
2327
return 0;
2328
}
2329
2330
/* Perform direct write of the header and payload of the binary object.
2331
2332
The large contiguous data is written directly into the underlying file
2333
object, bypassing the output_buffer of the Pickler. We intentionally
2334
do not insert a protocol 4 frame opcode to make it possible to optimize
2335
file.read calls in the loader.
2336
*/
2337
static int
2338
_Pickler_write_bytes(PicklerObject *self,
2339
const char *header, Py_ssize_t header_size,
2340
const char *data, Py_ssize_t data_size,
2341
PyObject *payload)
2342
{
2343
int bypass_buffer = (data_size >= FRAME_SIZE_TARGET);
2344
int framing = self->framing;
2345
2346
if (bypass_buffer) {
2347
assert(self->output_buffer != NULL);
2348
/* Commit the previous frame. */
2349
if (_Pickler_CommitFrame(self)) {
2350
return -1;
2351
}
2352
/* Disable framing temporarily */
2353
self->framing = 0;
2354
}
2355
2356
if (_Pickler_Write(self, header, header_size) < 0) {
2357
return -1;
2358
}
2359
2360
if (bypass_buffer && self->write != NULL) {
2361
/* Bypass the in-memory buffer to directly stream large data
2362
into the underlying file object. */
2363
PyObject *result, *mem = NULL;
2364
/* Dump the output buffer to the file. */
2365
if (_Pickler_FlushToFile(self) < 0) {
2366
return -1;
2367
}
2368
2369
/* Stream write the payload into the file without going through the
2370
output buffer. */
2371
if (payload == NULL) {
2372
/* TODO: It would be better to use a memoryview with a linked
2373
original string if this is possible. */
2374
payload = mem = PyBytes_FromStringAndSize(data, data_size);
2375
if (payload == NULL) {
2376
return -1;
2377
}
2378
}
2379
result = PyObject_CallOneArg(self->write, payload);
2380
Py_XDECREF(mem);
2381
if (result == NULL) {
2382
return -1;
2383
}
2384
Py_DECREF(result);
2385
2386
/* Reinitialize the buffer for subsequent calls to _Pickler_Write. */
2387
if (_Pickler_ClearBuffer(self) < 0) {
2388
return -1;
2389
}
2390
}
2391
else {
2392
if (_Pickler_Write(self, data, data_size) < 0) {
2393
return -1;
2394
}
2395
}
2396
2397
/* Re-enable framing for subsequent calls to _Pickler_Write. */
2398
self->framing = framing;
2399
2400
return 0;
2401
}
2402
2403
static int
2404
_save_bytes_data(PickleState *st, PicklerObject *self, PyObject *obj,
2405
const char *data, Py_ssize_t size)
2406
{
2407
assert(self->proto >= 3);
2408
2409
char header[9];
2410
Py_ssize_t len;
2411
2412
if (size < 0)
2413
return -1;
2414
2415
if (size <= 0xff) {
2416
header[0] = SHORT_BINBYTES;
2417
header[1] = (unsigned char)size;
2418
len = 2;
2419
}
2420
else if ((size_t)size <= 0xffffffffUL) {
2421
header[0] = BINBYTES;
2422
header[1] = (unsigned char)(size & 0xff);
2423
header[2] = (unsigned char)((size >> 8) & 0xff);
2424
header[3] = (unsigned char)((size >> 16) & 0xff);
2425
header[4] = (unsigned char)((size >> 24) & 0xff);
2426
len = 5;
2427
}
2428
else if (self->proto >= 4) {
2429
header[0] = BINBYTES8;
2430
_write_size64(header + 1, size);
2431
len = 9;
2432
}
2433
else {
2434
PyErr_SetString(PyExc_OverflowError,
2435
"serializing a bytes object larger than 4 GiB "
2436
"requires pickle protocol 4 or higher");
2437
return -1;
2438
}
2439
2440
if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) {
2441
return -1;
2442
}
2443
2444
if (memo_put(st, self, obj) < 0) {
2445
return -1;
2446
}
2447
2448
return 0;
2449
}
2450
2451
static int
2452
save_bytes(PickleState *st, PicklerObject *self, PyObject *obj)
2453
{
2454
if (self->proto < 3) {
2455
/* Older pickle protocols do not have an opcode for pickling bytes
2456
objects. Therefore, we need to fake the copy protocol (i.e.,
2457
the __reduce__ method) to permit bytes object unpickling.
2458
2459
Here we use a hack to be compatible with Python 2. Since in Python
2460
2 'bytes' is just an alias for 'str' (which has different
2461
parameters than the actual bytes object), we use codecs.encode
2462
to create the appropriate 'str' object when unpickled using
2463
Python 2 *and* the appropriate 'bytes' object when unpickled
2464
using Python 3. Again this is a hack and we don't need to do this
2465
with newer protocols. */
2466
PyObject *reduce_value;
2467
int status;
2468
2469
if (PyBytes_GET_SIZE(obj) == 0) {
2470
reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2471
}
2472
else {
2473
PyObject *unicode_str =
2474
PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2475
PyBytes_GET_SIZE(obj),
2476
"strict");
2477
2478
if (unicode_str == NULL)
2479
return -1;
2480
reduce_value = Py_BuildValue("(O(OO))",
2481
st->codecs_encode, unicode_str,
2482
&_Py_ID(latin1));
2483
Py_DECREF(unicode_str);
2484
}
2485
2486
if (reduce_value == NULL)
2487
return -1;
2488
2489
/* save_reduce() will memoize the object automatically. */
2490
status = save_reduce(st, self, reduce_value, obj);
2491
Py_DECREF(reduce_value);
2492
return status;
2493
}
2494
else {
2495
return _save_bytes_data(st, self, obj, PyBytes_AS_STRING(obj),
2496
PyBytes_GET_SIZE(obj));
2497
}
2498
}
2499
2500
static int
2501
_save_bytearray_data(PickleState *state, PicklerObject *self, PyObject *obj,
2502
const char *data, Py_ssize_t size)
2503
{
2504
assert(self->proto >= 5);
2505
2506
char header[9];
2507
Py_ssize_t len;
2508
2509
if (size < 0)
2510
return -1;
2511
2512
header[0] = BYTEARRAY8;
2513
_write_size64(header + 1, size);
2514
len = 9;
2515
2516
if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) {
2517
return -1;
2518
}
2519
2520
if (memo_put(state, self, obj) < 0) {
2521
return -1;
2522
}
2523
2524
return 0;
2525
}
2526
2527
static int
2528
save_bytearray(PickleState *state, PicklerObject *self, PyObject *obj)
2529
{
2530
if (self->proto < 5) {
2531
/* Older pickle protocols do not have an opcode for pickling
2532
* bytearrays. */
2533
PyObject *reduce_value = NULL;
2534
int status;
2535
2536
if (PyByteArray_GET_SIZE(obj) == 0) {
2537
reduce_value = Py_BuildValue("(O())",
2538
(PyObject *) &PyByteArray_Type);
2539
}
2540
else {
2541
PyObject *bytes_obj = PyBytes_FromObject(obj);
2542
if (bytes_obj != NULL) {
2543
reduce_value = Py_BuildValue("(O(O))",
2544
(PyObject *) &PyByteArray_Type,
2545
bytes_obj);
2546
Py_DECREF(bytes_obj);
2547
}
2548
}
2549
if (reduce_value == NULL)
2550
return -1;
2551
2552
/* save_reduce() will memoize the object automatically. */
2553
status = save_reduce(state, self, reduce_value, obj);
2554
Py_DECREF(reduce_value);
2555
return status;
2556
}
2557
else {
2558
return _save_bytearray_data(state, self, obj,
2559
PyByteArray_AS_STRING(obj),
2560
PyByteArray_GET_SIZE(obj));
2561
}
2562
}
2563
2564
static int
2565
save_picklebuffer(PickleState *st, PicklerObject *self, PyObject *obj)
2566
{
2567
if (self->proto < 5) {
2568
PyErr_SetString(st->PicklingError,
2569
"PickleBuffer can only pickled with protocol >= 5");
2570
return -1;
2571
}
2572
const Py_buffer* view = PyPickleBuffer_GetBuffer(obj);
2573
if (view == NULL) {
2574
return -1;
2575
}
2576
if (view->suboffsets != NULL || !PyBuffer_IsContiguous(view, 'A')) {
2577
PyErr_SetString(st->PicklingError,
2578
"PickleBuffer can not be pickled when "
2579
"pointing to a non-contiguous buffer");
2580
return -1;
2581
}
2582
int in_band = 1;
2583
if (self->buffer_callback != NULL) {
2584
PyObject *ret = PyObject_CallOneArg(self->buffer_callback, obj);
2585
if (ret == NULL) {
2586
return -1;
2587
}
2588
in_band = PyObject_IsTrue(ret);
2589
Py_DECREF(ret);
2590
if (in_band == -1) {
2591
return -1;
2592
}
2593
}
2594
if (in_band) {
2595
/* Write data in-band */
2596
if (view->readonly) {
2597
return _save_bytes_data(st, self, obj, (const char *)view->buf,
2598
view->len);
2599
}
2600
else {
2601
return _save_bytearray_data(st, self, obj, (const char *)view->buf,
2602
view->len);
2603
}
2604
}
2605
else {
2606
/* Write data out-of-band */
2607
const char next_buffer_op = NEXT_BUFFER;
2608
if (_Pickler_Write(self, &next_buffer_op, 1) < 0) {
2609
return -1;
2610
}
2611
if (view->readonly) {
2612
const char readonly_buffer_op = READONLY_BUFFER;
2613
if (_Pickler_Write(self, &readonly_buffer_op, 1) < 0) {
2614
return -1;
2615
}
2616
}
2617
}
2618
return 0;
2619
}
2620
2621
/* A copy of PyUnicode_AsRawUnicodeEscapeString() that also translates
2622
backslash and newline characters to \uXXXX escapes. */
2623
static PyObject *
2624
raw_unicode_escape(PyObject *obj)
2625
{
2626
char *p;
2627
Py_ssize_t i, size;
2628
const void *data;
2629
int kind;
2630
_PyBytesWriter writer;
2631
2632
_PyBytesWriter_Init(&writer);
2633
2634
size = PyUnicode_GET_LENGTH(obj);
2635
data = PyUnicode_DATA(obj);
2636
kind = PyUnicode_KIND(obj);
2637
2638
p = _PyBytesWriter_Alloc(&writer, size);
2639
if (p == NULL)
2640
goto error;
2641
writer.overallocate = 1;
2642
2643
for (i=0; i < size; i++) {
2644
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
2645
/* Map 32-bit characters to '\Uxxxxxxxx' */
2646
if (ch >= 0x10000) {
2647
/* -1: subtract 1 preallocated byte */
2648
p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2649
if (p == NULL)
2650
goto error;
2651
2652
*p++ = '\\';
2653
*p++ = 'U';
2654
*p++ = Py_hexdigits[(ch >> 28) & 0xf];
2655
*p++ = Py_hexdigits[(ch >> 24) & 0xf];
2656
*p++ = Py_hexdigits[(ch >> 20) & 0xf];
2657
*p++ = Py_hexdigits[(ch >> 16) & 0xf];
2658
*p++ = Py_hexdigits[(ch >> 12) & 0xf];
2659
*p++ = Py_hexdigits[(ch >> 8) & 0xf];
2660
*p++ = Py_hexdigits[(ch >> 4) & 0xf];
2661
*p++ = Py_hexdigits[ch & 15];
2662
}
2663
/* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
2664
else if (ch >= 256 ||
2665
ch == '\\' || ch == 0 || ch == '\n' || ch == '\r' ||
2666
ch == 0x1a)
2667
{
2668
/* -1: subtract 1 preallocated byte */
2669
p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2670
if (p == NULL)
2671
goto error;
2672
2673
*p++ = '\\';
2674
*p++ = 'u';
2675
*p++ = Py_hexdigits[(ch >> 12) & 0xf];
2676
*p++ = Py_hexdigits[(ch >> 8) & 0xf];
2677
*p++ = Py_hexdigits[(ch >> 4) & 0xf];
2678
*p++ = Py_hexdigits[ch & 15];
2679
}
2680
/* Copy everything else as-is */
2681
else
2682
*p++ = (char) ch;
2683
}
2684
2685
return _PyBytesWriter_Finish(&writer, p);
2686
2687
error:
2688
_PyBytesWriter_Dealloc(&writer);
2689
return NULL;
2690
}
2691
2692
static int
2693
write_unicode_binary(PicklerObject *self, PyObject *obj)
2694
{
2695
char header[9];
2696
Py_ssize_t len;
2697
PyObject *encoded = NULL;
2698
Py_ssize_t size;
2699
const char *data;
2700
2701
data = PyUnicode_AsUTF8AndSize(obj, &size);
2702
if (data == NULL) {
2703
/* Issue #8383: for strings with lone surrogates, fallback on the
2704
"surrogatepass" error handler. */
2705
PyErr_Clear();
2706
encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2707
if (encoded == NULL)
2708
return -1;
2709
2710
data = PyBytes_AS_STRING(encoded);
2711
size = PyBytes_GET_SIZE(encoded);
2712
}
2713
2714
assert(size >= 0);
2715
if (size <= 0xff && self->proto >= 4) {
2716
header[0] = SHORT_BINUNICODE;
2717
header[1] = (unsigned char)(size & 0xff);
2718
len = 2;
2719
}
2720
else if ((size_t)size <= 0xffffffffUL) {
2721
header[0] = BINUNICODE;
2722
header[1] = (unsigned char)(size & 0xff);
2723
header[2] = (unsigned char)((size >> 8) & 0xff);
2724
header[3] = (unsigned char)((size >> 16) & 0xff);
2725
header[4] = (unsigned char)((size >> 24) & 0xff);
2726
len = 5;
2727
}
2728
else if (self->proto >= 4) {
2729
header[0] = BINUNICODE8;
2730
_write_size64(header + 1, size);
2731
len = 9;
2732
}
2733
else {
2734
PyErr_SetString(PyExc_OverflowError,
2735
"serializing a string larger than 4 GiB "
2736
"requires pickle protocol 4 or higher");
2737
Py_XDECREF(encoded);
2738
return -1;
2739
}
2740
2741
if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) {
2742
Py_XDECREF(encoded);
2743
return -1;
2744
}
2745
Py_XDECREF(encoded);
2746
return 0;
2747
}
2748
2749
static int
2750
save_unicode(PickleState *state, PicklerObject *self, PyObject *obj)
2751
{
2752
if (self->bin) {
2753
if (write_unicode_binary(self, obj) < 0)
2754
return -1;
2755
}
2756
else {
2757
PyObject *encoded;
2758
Py_ssize_t size;
2759
const char unicode_op = UNICODE;
2760
2761
encoded = raw_unicode_escape(obj);
2762
if (encoded == NULL)
2763
return -1;
2764
2765
if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2766
Py_DECREF(encoded);
2767
return -1;
2768
}
2769
2770
size = PyBytes_GET_SIZE(encoded);
2771
if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2772
Py_DECREF(encoded);
2773
return -1;
2774
}
2775
Py_DECREF(encoded);
2776
2777
if (_Pickler_Write(self, "\n", 1) < 0)
2778
return -1;
2779
}
2780
if (memo_put(state, self, obj) < 0)
2781
return -1;
2782
2783
return 0;
2784
}
2785
2786
/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2787
static int
2788
store_tuple_elements(PickleState *state, PicklerObject *self, PyObject *t,
2789
Py_ssize_t len)
2790
{
2791
Py_ssize_t i;
2792
2793
assert(PyTuple_Size(t) == len);
2794
2795
for (i = 0; i < len; i++) {
2796
PyObject *element = PyTuple_GET_ITEM(t, i);
2797
2798
if (element == NULL)
2799
return -1;
2800
if (save(state, self, element, 0) < 0)
2801
return -1;
2802
}
2803
2804
return 0;
2805
}
2806
2807
/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2808
* used across protocols to minimize the space needed to pickle them.
2809
* Tuples are also the only builtin immutable type that can be recursive
2810
* (a tuple can be reached from itself), and that requires some subtle
2811
* magic so that it works in all cases. IOW, this is a long routine.
2812
*/
2813
static int
2814
save_tuple(PickleState *state, PicklerObject *self, PyObject *obj)
2815
{
2816
Py_ssize_t len, i;
2817
2818
const char mark_op = MARK;
2819
const char tuple_op = TUPLE;
2820
const char pop_op = POP;
2821
const char pop_mark_op = POP_MARK;
2822
const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2823
2824
if ((len = PyTuple_Size(obj)) < 0)
2825
return -1;
2826
2827
if (len == 0) {
2828
char pdata[2];
2829
2830
if (self->proto) {
2831
pdata[0] = EMPTY_TUPLE;
2832
len = 1;
2833
}
2834
else {
2835
pdata[0] = MARK;
2836
pdata[1] = TUPLE;
2837
len = 2;
2838
}
2839
if (_Pickler_Write(self, pdata, len) < 0)
2840
return -1;
2841
return 0;
2842
}
2843
2844
/* The tuple isn't in the memo now. If it shows up there after
2845
* saving the tuple elements, the tuple must be recursive, in
2846
* which case we'll pop everything we put on the stack, and fetch
2847
* its value from the memo.
2848
*/
2849
if (len <= 3 && self->proto >= 2) {
2850
/* Use TUPLE{1,2,3} opcodes. */
2851
if (store_tuple_elements(state, self, obj, len) < 0)
2852
return -1;
2853
2854
if (PyMemoTable_Get(self->memo, obj)) {
2855
/* pop the len elements */
2856
for (i = 0; i < len; i++)
2857
if (_Pickler_Write(self, &pop_op, 1) < 0)
2858
return -1;
2859
/* fetch from memo */
2860
if (memo_get(state, self, obj) < 0)
2861
return -1;
2862
2863
return 0;
2864
}
2865
else { /* Not recursive. */
2866
if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2867
return -1;
2868
}
2869
goto memoize;
2870
}
2871
2872
/* proto < 2 and len > 0, or proto >= 2 and len > 3.
2873
* Generate MARK e1 e2 ... TUPLE
2874
*/
2875
if (_Pickler_Write(self, &mark_op, 1) < 0)
2876
return -1;
2877
2878
if (store_tuple_elements(state, self, obj, len) < 0)
2879
return -1;
2880
2881
if (PyMemoTable_Get(self->memo, obj)) {
2882
/* pop the stack stuff we pushed */
2883
if (self->bin) {
2884
if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2885
return -1;
2886
}
2887
else {
2888
/* Note that we pop one more than len, to remove
2889
* the MARK too.
2890
*/
2891
for (i = 0; i <= len; i++)
2892
if (_Pickler_Write(self, &pop_op, 1) < 0)
2893
return -1;
2894
}
2895
/* fetch from memo */
2896
if (memo_get(state, self, obj) < 0)
2897
return -1;
2898
2899
return 0;
2900
}
2901
else { /* Not recursive. */
2902
if (_Pickler_Write(self, &tuple_op, 1) < 0)
2903
return -1;
2904
}
2905
2906
memoize:
2907
if (memo_put(state, self, obj) < 0)
2908
return -1;
2909
2910
return 0;
2911
}
2912
2913
/* iter is an iterator giving items, and we batch up chunks of
2914
* MARK item item ... item APPENDS
2915
* opcode sequences. Calling code should have arranged to first create an
2916
* empty list, or list-like object, for the APPENDS to operate on.
2917
* Returns 0 on success, <0 on error.
2918
*/
2919
static int
2920
batch_list(PickleState *state, PicklerObject *self, PyObject *iter)
2921
{
2922
PyObject *obj = NULL;
2923
PyObject *firstitem = NULL;
2924
int i, n;
2925
2926
const char mark_op = MARK;
2927
const char append_op = APPEND;
2928
const char appends_op = APPENDS;
2929
2930
assert(iter != NULL);
2931
2932
/* XXX: I think this function could be made faster by avoiding the
2933
iterator interface and fetching objects directly from list using
2934
PyList_GET_ITEM.
2935
*/
2936
2937
if (self->proto == 0) {
2938
/* APPENDS isn't available; do one at a time. */
2939
for (;;) {
2940
obj = PyIter_Next(iter);
2941
if (obj == NULL) {
2942
if (PyErr_Occurred())
2943
return -1;
2944
break;
2945
}
2946
i = save(state, self, obj, 0);
2947
Py_DECREF(obj);
2948
if (i < 0)
2949
return -1;
2950
if (_Pickler_Write(self, &append_op, 1) < 0)
2951
return -1;
2952
}
2953
return 0;
2954
}
2955
2956
/* proto > 0: write in batches of BATCHSIZE. */
2957
do {
2958
/* Get first item */
2959
firstitem = PyIter_Next(iter);
2960
if (firstitem == NULL) {
2961
if (PyErr_Occurred())
2962
goto error;
2963
2964
/* nothing more to add */
2965
break;
2966
}
2967
2968
/* Try to get a second item */
2969
obj = PyIter_Next(iter);
2970
if (obj == NULL) {
2971
if (PyErr_Occurred())
2972
goto error;
2973
2974
/* Only one item to write */
2975
if (save(state, self, firstitem, 0) < 0)
2976
goto error;
2977
if (_Pickler_Write(self, &append_op, 1) < 0)
2978
goto error;
2979
Py_CLEAR(firstitem);
2980
break;
2981
}
2982
2983
/* More than one item to write */
2984
2985
/* Pump out MARK, items, APPENDS. */
2986
if (_Pickler_Write(self, &mark_op, 1) < 0)
2987
goto error;
2988
2989
if (save(state, self, firstitem, 0) < 0)
2990
goto error;
2991
Py_CLEAR(firstitem);
2992
n = 1;
2993
2994
/* Fetch and save up to BATCHSIZE items */
2995
while (obj) {
2996
if (save(state, self, obj, 0) < 0)
2997
goto error;
2998
Py_CLEAR(obj);
2999
n += 1;
3000
3001
if (n == BATCHSIZE)
3002
break;
3003
3004
obj = PyIter_Next(iter);
3005
if (obj == NULL) {
3006
if (PyErr_Occurred())
3007
goto error;
3008
break;
3009
}
3010
}
3011
3012
if (_Pickler_Write(self, &appends_op, 1) < 0)
3013
goto error;
3014
3015
} while (n == BATCHSIZE);
3016
return 0;
3017
3018
error:
3019
Py_XDECREF(firstitem);
3020
Py_XDECREF(obj);
3021
return -1;
3022
}
3023
3024
/* This is a variant of batch_list() above, specialized for lists (with no
3025
* support for list subclasses). Like batch_list(), we batch up chunks of
3026
* MARK item item ... item APPENDS
3027
* opcode sequences. Calling code should have arranged to first create an
3028
* empty list, or list-like object, for the APPENDS to operate on.
3029
* Returns 0 on success, -1 on error.
3030
*
3031
* This version is considerably faster than batch_list(), if less general.
3032
*
3033
* Note that this only works for protocols > 0.
3034
*/
3035
static int
3036
batch_list_exact(PickleState *state, PicklerObject *self, PyObject *obj)
3037
{
3038
PyObject *item = NULL;
3039
Py_ssize_t this_batch, total;
3040
3041
const char append_op = APPEND;
3042
const char appends_op = APPENDS;
3043
const char mark_op = MARK;
3044
3045
assert(obj != NULL);
3046
assert(self->proto > 0);
3047
assert(PyList_CheckExact(obj));
3048
3049
if (PyList_GET_SIZE(obj) == 1) {
3050
item = PyList_GET_ITEM(obj, 0);
3051
Py_INCREF(item);
3052
int err = save(state, self, item, 0);
3053
Py_DECREF(item);
3054
if (err < 0)
3055
return -1;
3056
if (_Pickler_Write(self, &append_op, 1) < 0)
3057
return -1;
3058
return 0;
3059
}
3060
3061
/* Write in batches of BATCHSIZE. */
3062
total = 0;
3063
do {
3064
this_batch = 0;
3065
if (_Pickler_Write(self, &mark_op, 1) < 0)
3066
return -1;
3067
while (total < PyList_GET_SIZE(obj)) {
3068
item = PyList_GET_ITEM(obj, total);
3069
Py_INCREF(item);
3070
int err = save(state, self, item, 0);
3071
Py_DECREF(item);
3072
if (err < 0)
3073
return -1;
3074
total++;
3075
if (++this_batch == BATCHSIZE)
3076
break;
3077
}
3078
if (_Pickler_Write(self, &appends_op, 1) < 0)
3079
return -1;
3080
3081
} while (total < PyList_GET_SIZE(obj));
3082
3083
return 0;
3084
}
3085
3086
static int
3087
save_list(PickleState *state, PicklerObject *self, PyObject *obj)
3088
{
3089
char header[3];
3090
Py_ssize_t len;
3091
int status = 0;
3092
3093
if (self->fast && !fast_save_enter(self, obj))
3094
goto error;
3095
3096
/* Create an empty list. */
3097
if (self->bin) {
3098
header[0] = EMPTY_LIST;
3099
len = 1;
3100
}
3101
else {
3102
header[0] = MARK;
3103
header[1] = LIST;
3104
len = 2;
3105
}
3106
3107
if (_Pickler_Write(self, header, len) < 0)
3108
goto error;
3109
3110
/* Get list length, and bow out early if empty. */
3111
if ((len = PyList_Size(obj)) < 0)
3112
goto error;
3113
3114
if (memo_put(state, self, obj) < 0)
3115
goto error;
3116
3117
if (len != 0) {
3118
/* Materialize the list elements. */
3119
if (PyList_CheckExact(obj) && self->proto > 0) {
3120
if (_Py_EnterRecursiveCall(" while pickling an object"))
3121
goto error;
3122
status = batch_list_exact(state, self, obj);
3123
_Py_LeaveRecursiveCall();
3124
} else {
3125
PyObject *iter = PyObject_GetIter(obj);
3126
if (iter == NULL)
3127
goto error;
3128
3129
if (_Py_EnterRecursiveCall(" while pickling an object")) {
3130
Py_DECREF(iter);
3131
goto error;
3132
}
3133
status = batch_list(state, self, iter);
3134
_Py_LeaveRecursiveCall();
3135
Py_DECREF(iter);
3136
}
3137
}
3138
if (0) {
3139
error:
3140
status = -1;
3141
}
3142
3143
if (self->fast && !fast_save_leave(self, obj))
3144
status = -1;
3145
3146
return status;
3147
}
3148
3149
/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
3150
* MARK key value ... key value SETITEMS
3151
* opcode sequences. Calling code should have arranged to first create an
3152
* empty dict, or dict-like object, for the SETITEMS to operate on.
3153
* Returns 0 on success, <0 on error.
3154
*
3155
* This is very much like batch_list(). The difference between saving
3156
* elements directly, and picking apart two-tuples, is so long-winded at
3157
* the C level, though, that attempts to combine these routines were too
3158
* ugly to bear.
3159
*/
3160
static int
3161
batch_dict(PickleState *state, PicklerObject *self, PyObject *iter)
3162
{
3163
PyObject *obj = NULL;
3164
PyObject *firstitem = NULL;
3165
int i, n;
3166
3167
const char mark_op = MARK;
3168
const char setitem_op = SETITEM;
3169
const char setitems_op = SETITEMS;
3170
3171
assert(iter != NULL);
3172
3173
if (self->proto == 0) {
3174
/* SETITEMS isn't available; do one at a time. */
3175
for (;;) {
3176
obj = PyIter_Next(iter);
3177
if (obj == NULL) {
3178
if (PyErr_Occurred())
3179
return -1;
3180
break;
3181
}
3182
if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
3183
PyErr_SetString(PyExc_TypeError, "dict items "
3184
"iterator must return 2-tuples");
3185
return -1;
3186
}
3187
i = save(state, self, PyTuple_GET_ITEM(obj, 0), 0);
3188
if (i >= 0)
3189
i = save(state, self, PyTuple_GET_ITEM(obj, 1), 0);
3190
Py_DECREF(obj);
3191
if (i < 0)
3192
return -1;
3193
if (_Pickler_Write(self, &setitem_op, 1) < 0)
3194
return -1;
3195
}
3196
return 0;
3197
}
3198
3199
/* proto > 0: write in batches of BATCHSIZE. */
3200
do {
3201
/* Get first item */
3202
firstitem = PyIter_Next(iter);
3203
if (firstitem == NULL) {
3204
if (PyErr_Occurred())
3205
goto error;
3206
3207
/* nothing more to add */
3208
break;
3209
}
3210
if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
3211
PyErr_SetString(PyExc_TypeError, "dict items "
3212
"iterator must return 2-tuples");
3213
goto error;
3214
}
3215
3216
/* Try to get a second item */
3217
obj = PyIter_Next(iter);
3218
if (obj == NULL) {
3219
if (PyErr_Occurred())
3220
goto error;
3221
3222
/* Only one item to write */
3223
if (save(state, self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
3224
goto error;
3225
if (save(state, self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
3226
goto error;
3227
if (_Pickler_Write(self, &setitem_op, 1) < 0)
3228
goto error;
3229
Py_CLEAR(firstitem);
3230
break;
3231
}
3232
3233
/* More than one item to write */
3234
3235
/* Pump out MARK, items, SETITEMS. */
3236
if (_Pickler_Write(self, &mark_op, 1) < 0)
3237
goto error;
3238
3239
if (save(state, self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
3240
goto error;
3241
if (save(state, self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
3242
goto error;
3243
Py_CLEAR(firstitem);
3244
n = 1;
3245
3246
/* Fetch and save up to BATCHSIZE items */
3247
while (obj) {
3248
if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
3249
PyErr_SetString(PyExc_TypeError, "dict items "
3250
"iterator must return 2-tuples");
3251
goto error;
3252
}
3253
if (save(state, self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
3254
save(state, self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
3255
goto error;
3256
Py_CLEAR(obj);
3257
n += 1;
3258
3259
if (n == BATCHSIZE)
3260
break;
3261
3262
obj = PyIter_Next(iter);
3263
if (obj == NULL) {
3264
if (PyErr_Occurred())
3265
goto error;
3266
break;
3267
}
3268
}
3269
3270
if (_Pickler_Write(self, &setitems_op, 1) < 0)
3271
goto error;
3272
3273
} while (n == BATCHSIZE);
3274
return 0;
3275
3276
error:
3277
Py_XDECREF(firstitem);
3278
Py_XDECREF(obj);
3279
return -1;
3280
}
3281
3282
/* This is a variant of batch_dict() above that specializes for dicts, with no
3283
* support for dict subclasses. Like batch_dict(), we batch up chunks of
3284
* MARK key value ... key value SETITEMS
3285
* opcode sequences. Calling code should have arranged to first create an
3286
* empty dict, or dict-like object, for the SETITEMS to operate on.
3287
* Returns 0 on success, -1 on error.
3288
*
3289
* Note that this currently doesn't work for protocol 0.
3290
*/
3291
static int
3292
batch_dict_exact(PickleState *state, PicklerObject *self, PyObject *obj)
3293
{
3294
PyObject *key = NULL, *value = NULL;
3295
int i;
3296
Py_ssize_t dict_size, ppos = 0;
3297
3298
const char mark_op = MARK;
3299
const char setitem_op = SETITEM;
3300
const char setitems_op = SETITEMS;
3301
3302
assert(obj != NULL && PyDict_CheckExact(obj));
3303
assert(self->proto > 0);
3304
3305
dict_size = PyDict_GET_SIZE(obj);
3306
3307
/* Special-case len(d) == 1 to save space. */
3308
if (dict_size == 1) {
3309
PyDict_Next(obj, &ppos, &key, &value);
3310
Py_INCREF(key);
3311
Py_INCREF(value);
3312
if (save(state, self, key, 0) < 0) {
3313
goto error;
3314
}
3315
if (save(state, self, value, 0) < 0) {
3316
goto error;
3317
}
3318
Py_CLEAR(key);
3319
Py_CLEAR(value);
3320
if (_Pickler_Write(self, &setitem_op, 1) < 0)
3321
return -1;
3322
return 0;
3323
}
3324
3325
/* Write in batches of BATCHSIZE. */
3326
do {
3327
i = 0;
3328
if (_Pickler_Write(self, &mark_op, 1) < 0)
3329
return -1;
3330
while (PyDict_Next(obj, &ppos, &key, &value)) {
3331
Py_INCREF(key);
3332
Py_INCREF(value);
3333
if (save(state, self, key, 0) < 0) {
3334
goto error;
3335
}
3336
if (save(state, self, value, 0) < 0) {
3337
goto error;
3338
}
3339
Py_CLEAR(key);
3340
Py_CLEAR(value);
3341
if (++i == BATCHSIZE)
3342
break;
3343
}
3344
if (_Pickler_Write(self, &setitems_op, 1) < 0)
3345
return -1;
3346
if (PyDict_GET_SIZE(obj) != dict_size) {
3347
PyErr_Format(
3348
PyExc_RuntimeError,
3349
"dictionary changed size during iteration");
3350
return -1;
3351
}
3352
3353
} while (i == BATCHSIZE);
3354
return 0;
3355
error:
3356
Py_XDECREF(key);
3357
Py_XDECREF(value);
3358
return -1;
3359
}
3360
3361
static int
3362
save_dict(PickleState *state, PicklerObject *self, PyObject *obj)
3363
{
3364
PyObject *items, *iter;
3365
char header[3];
3366
Py_ssize_t len;
3367
int status = 0;
3368
assert(PyDict_Check(obj));
3369
3370
if (self->fast && !fast_save_enter(self, obj))
3371
goto error;
3372
3373
/* Create an empty dict. */
3374
if (self->bin) {
3375
header[0] = EMPTY_DICT;
3376
len = 1;
3377
}
3378
else {
3379
header[0] = MARK;
3380
header[1] = DICT;
3381
len = 2;
3382
}
3383
3384
if (_Pickler_Write(self, header, len) < 0)
3385
goto error;
3386
3387
if (memo_put(state, self, obj) < 0)
3388
goto error;
3389
3390
if (PyDict_GET_SIZE(obj)) {
3391
/* Save the dict items. */
3392
if (PyDict_CheckExact(obj) && self->proto > 0) {
3393
/* We can take certain shortcuts if we know this is a dict and
3394
not a dict subclass. */
3395
if (_Py_EnterRecursiveCall(" while pickling an object"))
3396
goto error;
3397
status = batch_dict_exact(state, self, obj);
3398
_Py_LeaveRecursiveCall();
3399
} else {
3400
items = PyObject_CallMethodNoArgs(obj, &_Py_ID(items));
3401
if (items == NULL)
3402
goto error;
3403
iter = PyObject_GetIter(items);
3404
Py_DECREF(items);
3405
if (iter == NULL)
3406
goto error;
3407
if (_Py_EnterRecursiveCall(" while pickling an object")) {
3408
Py_DECREF(iter);
3409
goto error;
3410
}
3411
status = batch_dict(state, self, iter);
3412
_Py_LeaveRecursiveCall();
3413
Py_DECREF(iter);
3414
}
3415
}
3416
3417
if (0) {
3418
error:
3419
status = -1;
3420
}
3421
3422
if (self->fast && !fast_save_leave(self, obj))
3423
status = -1;
3424
3425
return status;
3426
}
3427
3428
static int
3429
save_set(PickleState *state, PicklerObject *self, PyObject *obj)
3430
{
3431
PyObject *item;
3432
int i;
3433
Py_ssize_t set_size, ppos = 0;
3434
Py_hash_t hash;
3435
3436
const char empty_set_op = EMPTY_SET;
3437
const char mark_op = MARK;
3438
const char additems_op = ADDITEMS;
3439
3440
if (self->proto < 4) {
3441
PyObject *items;
3442
PyObject *reduce_value;
3443
int status;
3444
3445
items = PySequence_List(obj);
3446
if (items == NULL) {
3447
return -1;
3448
}
3449
reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
3450
Py_DECREF(items);
3451
if (reduce_value == NULL) {
3452
return -1;
3453
}
3454
/* save_reduce() will memoize the object automatically. */
3455
status = save_reduce(state, self, reduce_value, obj);
3456
Py_DECREF(reduce_value);
3457
return status;
3458
}
3459
3460
if (_Pickler_Write(self, &empty_set_op, 1) < 0)
3461
return -1;
3462
3463
if (memo_put(state, self, obj) < 0)
3464
return -1;
3465
3466
set_size = PySet_GET_SIZE(obj);
3467
if (set_size == 0)
3468
return 0; /* nothing to do */
3469
3470
/* Write in batches of BATCHSIZE. */
3471
do {
3472
i = 0;
3473
if (_Pickler_Write(self, &mark_op, 1) < 0)
3474
return -1;
3475
while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
3476
Py_INCREF(item);
3477
int err = save(state, self, item, 0);
3478
Py_CLEAR(item);
3479
if (err < 0)
3480
return -1;
3481
if (++i == BATCHSIZE)
3482
break;
3483
}
3484
if (_Pickler_Write(self, &additems_op, 1) < 0)
3485
return -1;
3486
if (PySet_GET_SIZE(obj) != set_size) {
3487
PyErr_Format(
3488
PyExc_RuntimeError,
3489
"set changed size during iteration");
3490
return -1;
3491
}
3492
} while (i == BATCHSIZE);
3493
3494
return 0;
3495
}
3496
3497
static int
3498
save_frozenset(PickleState *state, PicklerObject *self, PyObject *obj)
3499
{
3500
PyObject *iter;
3501
3502
const char mark_op = MARK;
3503
const char frozenset_op = FROZENSET;
3504
3505
if (self->fast && !fast_save_enter(self, obj))
3506
return -1;
3507
3508
if (self->proto < 4) {
3509
PyObject *items;
3510
PyObject *reduce_value;
3511
int status;
3512
3513
items = PySequence_List(obj);
3514
if (items == NULL) {
3515
return -1;
3516
}
3517
reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3518
items);
3519
Py_DECREF(items);
3520
if (reduce_value == NULL) {
3521
return -1;
3522
}
3523
/* save_reduce() will memoize the object automatically. */
3524
status = save_reduce(state, self, reduce_value, obj);
3525
Py_DECREF(reduce_value);
3526
return status;
3527
}
3528
3529
if (_Pickler_Write(self, &mark_op, 1) < 0)
3530
return -1;
3531
3532
iter = PyObject_GetIter(obj);
3533
if (iter == NULL) {
3534
return -1;
3535
}
3536
for (;;) {
3537
PyObject *item;
3538
3539
item = PyIter_Next(iter);
3540
if (item == NULL) {
3541
if (PyErr_Occurred()) {
3542
Py_DECREF(iter);
3543
return -1;
3544
}
3545
break;
3546
}
3547
if (save(state, self, item, 0) < 0) {
3548
Py_DECREF(item);
3549
Py_DECREF(iter);
3550
return -1;
3551
}
3552
Py_DECREF(item);
3553
}
3554
Py_DECREF(iter);
3555
3556
/* If the object is already in the memo, this means it is
3557
recursive. In this case, throw away everything we put on the
3558
stack, and fetch the object back from the memo. */
3559
if (PyMemoTable_Get(self->memo, obj)) {
3560
const char pop_mark_op = POP_MARK;
3561
3562
if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3563
return -1;
3564
if (memo_get(state, self, obj) < 0)
3565
return -1;
3566
return 0;
3567
}
3568
3569
if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3570
return -1;
3571
if (memo_put(state, self, obj) < 0)
3572
return -1;
3573
3574
return 0;
3575
}
3576
3577
static int
3578
fix_imports(PickleState *st, PyObject **module_name, PyObject **global_name)
3579
{
3580
PyObject *key;
3581
PyObject *item;
3582
3583
key = PyTuple_Pack(2, *module_name, *global_name);
3584
if (key == NULL)
3585
return -1;
3586
item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
3587
Py_DECREF(key);
3588
if (item) {
3589
PyObject *fixed_module_name;
3590
PyObject *fixed_global_name;
3591
3592
if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3593
PyErr_Format(PyExc_RuntimeError,
3594
"_compat_pickle.REVERSE_NAME_MAPPING values "
3595
"should be 2-tuples, not %.200s",
3596
Py_TYPE(item)->tp_name);
3597
return -1;
3598
}
3599
fixed_module_name = PyTuple_GET_ITEM(item, 0);
3600
fixed_global_name = PyTuple_GET_ITEM(item, 1);
3601
if (!PyUnicode_Check(fixed_module_name) ||
3602
!PyUnicode_Check(fixed_global_name)) {
3603
PyErr_Format(PyExc_RuntimeError,
3604
"_compat_pickle.REVERSE_NAME_MAPPING values "
3605
"should be pairs of str, not (%.200s, %.200s)",
3606
Py_TYPE(fixed_module_name)->tp_name,
3607
Py_TYPE(fixed_global_name)->tp_name);
3608
return -1;
3609
}
3610
3611
Py_CLEAR(*module_name);
3612
Py_CLEAR(*global_name);
3613
*module_name = Py_NewRef(fixed_module_name);
3614
*global_name = Py_NewRef(fixed_global_name);
3615
return 0;
3616
}
3617
else if (PyErr_Occurred()) {
3618
return -1;
3619
}
3620
3621
item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
3622
if (item) {
3623
if (!PyUnicode_Check(item)) {
3624
PyErr_Format(PyExc_RuntimeError,
3625
"_compat_pickle.REVERSE_IMPORT_MAPPING values "
3626
"should be strings, not %.200s",
3627
Py_TYPE(item)->tp_name);
3628
return -1;
3629
}
3630
Py_XSETREF(*module_name, Py_NewRef(item));
3631
}
3632
else if (PyErr_Occurred()) {
3633
return -1;
3634
}
3635
3636
return 0;
3637
}
3638
3639
static int
3640
save_global(PickleState *st, PicklerObject *self, PyObject *obj,
3641
PyObject *name)
3642
{
3643
PyObject *global_name = NULL;
3644
PyObject *module_name = NULL;
3645
PyObject *module = NULL;
3646
PyObject *parent = NULL;
3647
PyObject *dotted_path = NULL;
3648
PyObject *lastname = NULL;
3649
PyObject *cls;
3650
int status = 0;
3651
3652
const char global_op = GLOBAL;
3653
3654
if (name) {
3655
global_name = Py_NewRef(name);
3656
}
3657
else {
3658
if (_PyObject_LookupAttr(obj, &_Py_ID(__qualname__), &global_name) < 0)
3659
goto error;
3660
if (global_name == NULL) {
3661
global_name = PyObject_GetAttr(obj, &_Py_ID(__name__));
3662
if (global_name == NULL)
3663
goto error;
3664
}
3665
}
3666
3667
dotted_path = get_dotted_path(module, global_name);
3668
if (dotted_path == NULL)
3669
goto error;
3670
module_name = whichmodule(obj, dotted_path);
3671
if (module_name == NULL)
3672
goto error;
3673
3674
/* XXX: Change to use the import C API directly with level=0 to disallow
3675
relative imports.
3676
3677
XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3678
builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3679
custom import functions (IMHO, this would be a nice security
3680
feature). The import C API would need to be extended to support the
3681
extra parameters of __import__ to fix that. */
3682
module = PyImport_Import(module_name);
3683
if (module == NULL) {
3684
PyErr_Format(st->PicklingError,
3685
"Can't pickle %R: import of module %R failed",
3686
obj, module_name);
3687
goto error;
3688
}
3689
lastname = Py_NewRef(PyList_GET_ITEM(dotted_path,
3690
PyList_GET_SIZE(dotted_path) - 1));
3691
cls = get_deep_attribute(module, dotted_path, &parent);
3692
Py_CLEAR(dotted_path);
3693
if (cls == NULL) {
3694
PyErr_Format(st->PicklingError,
3695
"Can't pickle %R: attribute lookup %S on %S failed",
3696
obj, global_name, module_name);
3697
goto error;
3698
}
3699
if (cls != obj) {
3700
Py_DECREF(cls);
3701
PyErr_Format(st->PicklingError,
3702
"Can't pickle %R: it's not the same object as %S.%S",
3703
obj, module_name, global_name);
3704
goto error;
3705
}
3706
Py_DECREF(cls);
3707
3708
if (self->proto >= 2) {
3709
/* See whether this is in the extension registry, and if
3710
* so generate an EXT opcode.
3711
*/
3712
PyObject *extension_key;
3713
PyObject *code_obj; /* extension code as Python object */
3714
long code; /* extension code as C value */
3715
char pdata[5];
3716
Py_ssize_t n;
3717
3718
extension_key = PyTuple_Pack(2, module_name, global_name);
3719
if (extension_key == NULL) {
3720
goto error;
3721
}
3722
code_obj = PyDict_GetItemWithError(st->extension_registry,
3723
extension_key);
3724
Py_DECREF(extension_key);
3725
/* The object is not registered in the extension registry.
3726
This is the most likely code path. */
3727
if (code_obj == NULL) {
3728
if (PyErr_Occurred()) {
3729
goto error;
3730
}
3731
goto gen_global;
3732
}
3733
3734
/* XXX: pickle.py doesn't check neither the type, nor the range
3735
of the value returned by the extension_registry. It should for
3736
consistency. */
3737
3738
/* Verify code_obj has the right type and value. */
3739
if (!PyLong_Check(code_obj)) {
3740
PyErr_Format(st->PicklingError,
3741
"Can't pickle %R: extension code %R isn't an integer",
3742
obj, code_obj);
3743
goto error;
3744
}
3745
code = PyLong_AS_LONG(code_obj);
3746
if (code <= 0 || code > 0x7fffffffL) {
3747
if (!PyErr_Occurred())
3748
PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3749
"code %ld is out of range", obj, code);
3750
goto error;
3751
}
3752
3753
/* Generate an EXT opcode. */
3754
if (code <= 0xff) {
3755
pdata[0] = EXT1;
3756
pdata[1] = (unsigned char)code;
3757
n = 2;
3758
}
3759
else if (code <= 0xffff) {
3760
pdata[0] = EXT2;
3761
pdata[1] = (unsigned char)(code & 0xff);
3762
pdata[2] = (unsigned char)((code >> 8) & 0xff);
3763
n = 3;
3764
}
3765
else {
3766
pdata[0] = EXT4;
3767
pdata[1] = (unsigned char)(code & 0xff);
3768
pdata[2] = (unsigned char)((code >> 8) & 0xff);
3769
pdata[3] = (unsigned char)((code >> 16) & 0xff);
3770
pdata[4] = (unsigned char)((code >> 24) & 0xff);
3771
n = 5;
3772
}
3773
3774
if (_Pickler_Write(self, pdata, n) < 0)
3775
goto error;
3776
}
3777
else {
3778
gen_global:
3779
if (parent == module) {
3780
Py_SETREF(global_name, Py_NewRef(lastname));
3781
}
3782
if (self->proto >= 4) {
3783
const char stack_global_op = STACK_GLOBAL;
3784
3785
if (save(st, self, module_name, 0) < 0)
3786
goto error;
3787
if (save(st, self, global_name, 0) < 0)
3788
goto error;
3789
3790
if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3791
goto error;
3792
}
3793
else if (parent != module) {
3794
PyObject *reduce_value = Py_BuildValue("(O(OO))",
3795
st->getattr, parent, lastname);
3796
if (reduce_value == NULL)
3797
goto error;
3798
status = save_reduce(st, self, reduce_value, NULL);
3799
Py_DECREF(reduce_value);
3800
if (status < 0)
3801
goto error;
3802
}
3803
else {
3804
/* Generate a normal global opcode if we are using a pickle
3805
protocol < 4, or if the object is not registered in the
3806
extension registry. */
3807
PyObject *encoded;
3808
PyObject *(*unicode_encoder)(PyObject *);
3809
3810
if (_Pickler_Write(self, &global_op, 1) < 0)
3811
goto error;
3812
3813
/* For protocol < 3 and if the user didn't request against doing
3814
so, we convert module names to the old 2.x module names. */
3815
if (self->proto < 3 && self->fix_imports) {
3816
if (fix_imports(st, &module_name, &global_name) < 0) {
3817
goto error;
3818
}
3819
}
3820
3821
/* Since Python 3.0 now supports non-ASCII identifiers, we encode
3822
both the module name and the global name using UTF-8. We do so
3823
only when we are using the pickle protocol newer than version
3824
3. This is to ensure compatibility with older Unpickler running
3825
on Python 2.x. */
3826
if (self->proto == 3) {
3827
unicode_encoder = PyUnicode_AsUTF8String;
3828
}
3829
else {
3830
unicode_encoder = PyUnicode_AsASCIIString;
3831
}
3832
encoded = unicode_encoder(module_name);
3833
if (encoded == NULL) {
3834
if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
3835
PyErr_Format(st->PicklingError,
3836
"can't pickle module identifier '%S' using "
3837
"pickle protocol %i",
3838
module_name, self->proto);
3839
goto error;
3840
}
3841
if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3842
PyBytes_GET_SIZE(encoded)) < 0) {
3843
Py_DECREF(encoded);
3844
goto error;
3845
}
3846
Py_DECREF(encoded);
3847
if(_Pickler_Write(self, "\n", 1) < 0)
3848
goto error;
3849
3850
/* Save the name of the module. */
3851
encoded = unicode_encoder(global_name);
3852
if (encoded == NULL) {
3853
if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
3854
PyErr_Format(st->PicklingError,
3855
"can't pickle global identifier '%S' using "
3856
"pickle protocol %i",
3857
global_name, self->proto);
3858
goto error;
3859
}
3860
if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3861
PyBytes_GET_SIZE(encoded)) < 0) {
3862
Py_DECREF(encoded);
3863
goto error;
3864
}
3865
Py_DECREF(encoded);
3866
if (_Pickler_Write(self, "\n", 1) < 0)
3867
goto error;
3868
}
3869
/* Memoize the object. */
3870
if (memo_put(st, self, obj) < 0)
3871
goto error;
3872
}
3873
3874
if (0) {
3875
error:
3876
status = -1;
3877
}
3878
Py_XDECREF(module_name);
3879
Py_XDECREF(global_name);
3880
Py_XDECREF(module);
3881
Py_XDECREF(parent);
3882
Py_XDECREF(dotted_path);
3883
Py_XDECREF(lastname);
3884
3885
return status;
3886
}
3887
3888
static int
3889
save_singleton_type(PickleState *state, PicklerObject *self, PyObject *obj,
3890
PyObject *singleton)
3891
{
3892
PyObject *reduce_value;
3893
int status;
3894
3895
reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3896
if (reduce_value == NULL) {
3897
return -1;
3898
}
3899
status = save_reduce(state, self, reduce_value, obj);
3900
Py_DECREF(reduce_value);
3901
return status;
3902
}
3903
3904
static int
3905
save_type(PickleState *state, PicklerObject *self, PyObject *obj)
3906
{
3907
if (obj == (PyObject *)&_PyNone_Type) {
3908
return save_singleton_type(state, self, obj, Py_None);
3909
}
3910
else if (obj == (PyObject *)&PyEllipsis_Type) {
3911
return save_singleton_type(state, self, obj, Py_Ellipsis);
3912
}
3913
else if (obj == (PyObject *)&_PyNotImplemented_Type) {
3914
return save_singleton_type(state, self, obj, Py_NotImplemented);
3915
}
3916
return save_global(state, self, obj, NULL);
3917
}
3918
3919
static int
3920
save_pers(PickleState *state, PicklerObject *self, PyObject *obj)
3921
{
3922
PyObject *pid = NULL;
3923
int status = 0;
3924
3925
const char persid_op = PERSID;
3926
const char binpersid_op = BINPERSID;
3927
3928
pid = call_method(self->pers_func, self->pers_func_self, obj);
3929
if (pid == NULL)
3930
return -1;
3931
3932
if (pid != Py_None) {
3933
if (self->bin) {
3934
if (save(state, self, pid, 1) < 0 ||
3935
_Pickler_Write(self, &binpersid_op, 1) < 0)
3936
goto error;
3937
}
3938
else {
3939
PyObject *pid_str;
3940
3941
pid_str = PyObject_Str(pid);
3942
if (pid_str == NULL)
3943
goto error;
3944
3945
/* XXX: Should it check whether the pid contains embedded
3946
newlines? */
3947
if (!PyUnicode_IS_ASCII(pid_str)) {
3948
PyErr_SetString(state->PicklingError,
3949
"persistent IDs in protocol 0 must be "
3950
"ASCII strings");
3951
Py_DECREF(pid_str);
3952
goto error;
3953
}
3954
3955
if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3956
_Pickler_Write(self, PyUnicode_DATA(pid_str),
3957
PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3958
_Pickler_Write(self, "\n", 1) < 0) {
3959
Py_DECREF(pid_str);
3960
goto error;
3961
}
3962
Py_DECREF(pid_str);
3963
}
3964
status = 1;
3965
}
3966
3967
if (0) {
3968
error:
3969
status = -1;
3970
}
3971
Py_XDECREF(pid);
3972
3973
return status;
3974
}
3975
3976
static PyObject *
3977
get_class(PyObject *obj)
3978
{
3979
PyObject *cls;
3980
3981
if (_PyObject_LookupAttr(obj, &_Py_ID(__class__), &cls) == 0) {
3982
cls = Py_NewRef(Py_TYPE(obj));
3983
}
3984
return cls;
3985
}
3986
3987
/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3988
* appropriate __reduce__ method for obj.
3989
*/
3990
static int
3991
save_reduce(PickleState *st, PicklerObject *self, PyObject *args,
3992
PyObject *obj)
3993
{
3994
PyObject *callable;
3995
PyObject *argtup;
3996
PyObject *state = NULL;
3997
PyObject *listitems = Py_None;
3998
PyObject *dictitems = Py_None;
3999
PyObject *state_setter = Py_None;
4000
Py_ssize_t size;
4001
int use_newobj = 0, use_newobj_ex = 0;
4002
4003
const char reduce_op = REDUCE;
4004
const char build_op = BUILD;
4005
const char newobj_op = NEWOBJ;
4006
const char newobj_ex_op = NEWOBJ_EX;
4007
4008
size = PyTuple_Size(args);
4009
if (size < 2 || size > 6) {
4010
PyErr_SetString(st->PicklingError, "tuple returned by "
4011
"__reduce__ must contain 2 through 6 elements");
4012
return -1;
4013
}
4014
4015
if (!PyArg_UnpackTuple(args, "save_reduce", 2, 6,
4016
&callable, &argtup, &state, &listitems, &dictitems,
4017
&state_setter))
4018
return -1;
4019
4020
if (!PyCallable_Check(callable)) {
4021
PyErr_SetString(st->PicklingError, "first item of the tuple "
4022
"returned by __reduce__ must be callable");
4023
return -1;
4024
}
4025
if (!PyTuple_Check(argtup)) {
4026
PyErr_SetString(st->PicklingError, "second item of the tuple "
4027
"returned by __reduce__ must be a tuple");
4028
return -1;
4029
}
4030
4031
if (state == Py_None)
4032
state = NULL;
4033
4034
if (listitems == Py_None)
4035
listitems = NULL;
4036
else if (!PyIter_Check(listitems)) {
4037
PyErr_Format(st->PicklingError, "fourth element of the tuple "
4038
"returned by __reduce__ must be an iterator, not %s",
4039
Py_TYPE(listitems)->tp_name);
4040
return -1;
4041
}
4042
4043
if (dictitems == Py_None)
4044
dictitems = NULL;
4045
else if (!PyIter_Check(dictitems)) {
4046
PyErr_Format(st->PicklingError, "fifth element of the tuple "
4047
"returned by __reduce__ must be an iterator, not %s",
4048
Py_TYPE(dictitems)->tp_name);
4049
return -1;
4050
}
4051
4052
if (state_setter == Py_None)
4053
state_setter = NULL;
4054
else if (!PyCallable_Check(state_setter)) {
4055
PyErr_Format(st->PicklingError, "sixth element of the tuple "
4056
"returned by __reduce__ must be a function, not %s",
4057
Py_TYPE(state_setter)->tp_name);
4058
return -1;
4059
}
4060
4061
if (self->proto >= 2) {
4062
PyObject *name;
4063
4064
if (_PyObject_LookupAttr(callable, &_Py_ID(__name__), &name) < 0) {
4065
return -1;
4066
}
4067
if (name != NULL && PyUnicode_Check(name)) {
4068
use_newobj_ex = _PyUnicode_Equal(name, &_Py_ID(__newobj_ex__));
4069
if (!use_newobj_ex) {
4070
use_newobj = _PyUnicode_Equal(name, &_Py_ID(__newobj__));
4071
}
4072
}
4073
Py_XDECREF(name);
4074
}
4075
4076
if (use_newobj_ex) {
4077
PyObject *cls;
4078
PyObject *args;
4079
PyObject *kwargs;
4080
4081
if (PyTuple_GET_SIZE(argtup) != 3) {
4082
PyErr_Format(st->PicklingError,
4083
"length of the NEWOBJ_EX argument tuple must be "
4084
"exactly 3, not %zd", PyTuple_GET_SIZE(argtup));
4085
return -1;
4086
}
4087
4088
cls = PyTuple_GET_ITEM(argtup, 0);
4089
if (!PyType_Check(cls)) {
4090
PyErr_Format(st->PicklingError,
4091
"first item from NEWOBJ_EX argument tuple must "
4092
"be a class, not %.200s", Py_TYPE(cls)->tp_name);
4093
return -1;
4094
}
4095
args = PyTuple_GET_ITEM(argtup, 1);
4096
if (!PyTuple_Check(args)) {
4097
PyErr_Format(st->PicklingError,
4098
"second item from NEWOBJ_EX argument tuple must "
4099
"be a tuple, not %.200s", Py_TYPE(args)->tp_name);
4100
return -1;
4101
}
4102
kwargs = PyTuple_GET_ITEM(argtup, 2);
4103
if (!PyDict_Check(kwargs)) {
4104
PyErr_Format(st->PicklingError,
4105
"third item from NEWOBJ_EX argument tuple must "
4106
"be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
4107
return -1;
4108
}
4109
4110
if (self->proto >= 4) {
4111
if (save(st, self, cls, 0) < 0 ||
4112
save(st, self, args, 0) < 0 ||
4113
save(st, self, kwargs, 0) < 0 ||
4114
_Pickler_Write(self, &newobj_ex_op, 1) < 0) {
4115
return -1;
4116
}
4117
}
4118
else {
4119
PyObject *newargs;
4120
PyObject *cls_new;
4121
Py_ssize_t i;
4122
4123
newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2);
4124
if (newargs == NULL)
4125
return -1;
4126
4127
cls_new = PyObject_GetAttr(cls, &_Py_ID(__new__));
4128
if (cls_new == NULL) {
4129
Py_DECREF(newargs);
4130
return -1;
4131
}
4132
PyTuple_SET_ITEM(newargs, 0, cls_new);
4133
PyTuple_SET_ITEM(newargs, 1, Py_NewRef(cls));
4134
for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
4135
PyObject *item = PyTuple_GET_ITEM(args, i);
4136
PyTuple_SET_ITEM(newargs, i + 2, Py_NewRef(item));
4137
}
4138
4139
callable = PyObject_Call(st->partial, newargs, kwargs);
4140
Py_DECREF(newargs);
4141
if (callable == NULL)
4142
return -1;
4143
4144
newargs = PyTuple_New(0);
4145
if (newargs == NULL) {
4146
Py_DECREF(callable);
4147
return -1;
4148
}
4149
4150
if (save(st, self, callable, 0) < 0 ||
4151
save(st, self, newargs, 0) < 0 ||
4152
_Pickler_Write(self, &reduce_op, 1) < 0) {
4153
Py_DECREF(newargs);
4154
Py_DECREF(callable);
4155
return -1;
4156
}
4157
Py_DECREF(newargs);
4158
Py_DECREF(callable);
4159
}
4160
}
4161
else if (use_newobj) {
4162
PyObject *cls;
4163
PyObject *newargtup;
4164
PyObject *obj_class;
4165
int p;
4166
4167
/* Sanity checks. */
4168
if (PyTuple_GET_SIZE(argtup) < 1) {
4169
PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
4170
return -1;
4171
}
4172
4173
cls = PyTuple_GET_ITEM(argtup, 0);
4174
if (!PyType_Check(cls)) {
4175
PyErr_SetString(st->PicklingError, "args[0] from "
4176
"__newobj__ args is not a type");
4177
return -1;
4178
}
4179
4180
if (obj != NULL) {
4181
obj_class = get_class(obj);
4182
if (obj_class == NULL) {
4183
return -1;
4184
}
4185
p = obj_class != cls;
4186
Py_DECREF(obj_class);
4187
if (p) {
4188
PyErr_SetString(st->PicklingError, "args[0] from "
4189
"__newobj__ args has the wrong class");
4190
return -1;
4191
}
4192
}
4193
/* XXX: These calls save() are prone to infinite recursion. Imagine
4194
what happen if the value returned by the __reduce__() method of
4195
some extension type contains another object of the same type. Ouch!
4196
4197
Here is a quick example, that I ran into, to illustrate what I
4198
mean:
4199
4200
>>> import pickle, copyreg
4201
>>> copyreg.dispatch_table.pop(complex)
4202
>>> pickle.dumps(1+2j)
4203
Traceback (most recent call last):
4204
...
4205
RecursionError: maximum recursion depth exceeded
4206
4207
Removing the complex class from copyreg.dispatch_table made the
4208
__reduce_ex__() method emit another complex object:
4209
4210
>>> (1+1j).__reduce_ex__(2)
4211
(<function __newobj__ at 0xb7b71c3c>,
4212
(<class 'complex'>, (1+1j)), None, None, None)
4213
4214
Thus when save() was called on newargstup (the 2nd item) recursion
4215
ensued. Of course, the bug was in the complex class which had a
4216
broken __getnewargs__() that emitted another complex object. But,
4217
the point, here, is it is quite easy to end up with a broken reduce
4218
function. */
4219
4220
/* Save the class and its __new__ arguments. */
4221
if (save(st, self, cls, 0) < 0) {
4222
return -1;
4223
}
4224
4225
newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup));
4226
if (newargtup == NULL)
4227
return -1;
4228
4229
p = save(st, self, newargtup, 0);
4230
Py_DECREF(newargtup);
4231
if (p < 0)
4232
return -1;
4233
4234
/* Add NEWOBJ opcode. */
4235
if (_Pickler_Write(self, &newobj_op, 1) < 0)
4236
return -1;
4237
}
4238
else { /* Not using NEWOBJ. */
4239
if (save(st, self, callable, 0) < 0 ||
4240
save(st, self, argtup, 0) < 0 ||
4241
_Pickler_Write(self, &reduce_op, 1) < 0)
4242
return -1;
4243
}
4244
4245
/* obj can be NULL when save_reduce() is used directly. A NULL obj means
4246
the caller do not want to memoize the object. Not particularly useful,
4247
but that is to mimic the behavior save_reduce() in pickle.py when
4248
obj is None. */
4249
if (obj != NULL) {
4250
/* If the object is already in the memo, this means it is
4251
recursive. In this case, throw away everything we put on the
4252
stack, and fetch the object back from the memo. */
4253
if (PyMemoTable_Get(self->memo, obj)) {
4254
const char pop_op = POP;
4255
4256
if (_Pickler_Write(self, &pop_op, 1) < 0)
4257
return -1;
4258
if (memo_get(st, self, obj) < 0)
4259
return -1;
4260
4261
return 0;
4262
}
4263
else if (memo_put(st, self, obj) < 0)
4264
return -1;
4265
}
4266
4267
if (listitems && batch_list(st, self, listitems) < 0)
4268
return -1;
4269
4270
if (dictitems && batch_dict(st, self, dictitems) < 0)
4271
return -1;
4272
4273
if (state) {
4274
if (state_setter == NULL) {
4275
if (save(st, self, state, 0) < 0 ||
4276
_Pickler_Write(self, &build_op, 1) < 0)
4277
return -1;
4278
}
4279
else {
4280
4281
/* If a state_setter is specified, call it instead of load_build to
4282
* update obj's with its previous state.
4283
* The first 4 save/write instructions push state_setter and its
4284
* tuple of expected arguments (obj, state) onto the stack. The
4285
* REDUCE opcode triggers the state_setter(obj, state) function
4286
* call. Finally, because state-updating routines only do in-place
4287
* modification, the whole operation has to be stack-transparent.
4288
* Thus, we finally pop the call's output from the stack.*/
4289
4290
const char tupletwo_op = TUPLE2;
4291
const char pop_op = POP;
4292
if (save(st, self, state_setter, 0) < 0 ||
4293
save(st, self, obj, 0) < 0 || save(st, self, state, 0) < 0 ||
4294
_Pickler_Write(self, &tupletwo_op, 1) < 0 ||
4295
_Pickler_Write(self, &reduce_op, 1) < 0 ||
4296
_Pickler_Write(self, &pop_op, 1) < 0)
4297
return -1;
4298
}
4299
}
4300
return 0;
4301
}
4302
4303
static int
4304
save(PickleState *st, PicklerObject *self, PyObject *obj, int pers_save)
4305
{
4306
PyTypeObject *type;
4307
PyObject *reduce_func = NULL;
4308
PyObject *reduce_value = NULL;
4309
int status = 0;
4310
4311
if (_Pickler_OpcodeBoundary(self) < 0)
4312
return -1;
4313
4314
/* The extra pers_save argument is necessary to avoid calling save_pers()
4315
on its returned object. */
4316
if (!pers_save && self->pers_func) {
4317
/* save_pers() returns:
4318
-1 to signal an error;
4319
0 if it did nothing successfully;
4320
1 if a persistent id was saved.
4321
*/
4322
if ((status = save_pers(st, self, obj)) != 0)
4323
return status;
4324
}
4325
4326
type = Py_TYPE(obj);
4327
4328
/* The old cPickle had an optimization that used switch-case statement
4329
dispatching on the first letter of the type name. This has was removed
4330
since benchmarks shown that this optimization was actually slowing
4331
things down. */
4332
4333
/* Atom types; these aren't memoized, so don't check the memo. */
4334
4335
if (obj == Py_None) {
4336
return save_none(self, obj);
4337
}
4338
else if (obj == Py_False || obj == Py_True) {
4339
return save_bool(self, obj);
4340
}
4341
else if (type == &PyLong_Type) {
4342
return save_long(self, obj);
4343
}
4344
else if (type == &PyFloat_Type) {
4345
return save_float(self, obj);
4346
}
4347
4348
/* Check the memo to see if it has the object. If so, generate
4349
a GET (or BINGET) opcode, instead of pickling the object
4350
once again. */
4351
if (PyMemoTable_Get(self->memo, obj)) {
4352
return memo_get(st, self, obj);
4353
}
4354
4355
if (type == &PyBytes_Type) {
4356
return save_bytes(st, self, obj);
4357
}
4358
else if (type == &PyUnicode_Type) {
4359
return save_unicode(st, self, obj);
4360
}
4361
4362
/* We're only calling _Py_EnterRecursiveCall here so that atomic
4363
types above are pickled faster. */
4364
if (_Py_EnterRecursiveCall(" while pickling an object")) {
4365
return -1;
4366
}
4367
4368
if (type == &PyDict_Type) {
4369
status = save_dict(st, self, obj);
4370
goto done;
4371
}
4372
else if (type == &PySet_Type) {
4373
status = save_set(st, self, obj);
4374
goto done;
4375
}
4376
else if (type == &PyFrozenSet_Type) {
4377
status = save_frozenset(st, self, obj);
4378
goto done;
4379
}
4380
else if (type == &PyList_Type) {
4381
status = save_list(st, self, obj);
4382
goto done;
4383
}
4384
else if (type == &PyTuple_Type) {
4385
status = save_tuple(st, self, obj);
4386
goto done;
4387
}
4388
else if (type == &PyByteArray_Type) {
4389
status = save_bytearray(st, self, obj);
4390
goto done;
4391
}
4392
else if (type == &PyPickleBuffer_Type) {
4393
status = save_picklebuffer(st, self, obj);
4394
goto done;
4395
}
4396
4397
/* Now, check reducer_override. If it returns NotImplemented,
4398
* fallback to save_type or save_global, and then perhaps to the
4399
* regular reduction mechanism.
4400
*/
4401
if (self->reducer_override != NULL) {
4402
reduce_value = PyObject_CallOneArg(self->reducer_override, obj);
4403
if (reduce_value == NULL) {
4404
goto error;
4405
}
4406
if (reduce_value != Py_NotImplemented) {
4407
goto reduce;
4408
}
4409
Py_SETREF(reduce_value, NULL);
4410
}
4411
4412
if (type == &PyType_Type) {
4413
status = save_type(st, self, obj);
4414
goto done;
4415
}
4416
else if (type == &PyFunction_Type) {
4417
status = save_global(st, self, obj, NULL);
4418
goto done;
4419
}
4420
4421
/* XXX: This part needs some unit tests. */
4422
4423
/* Get a reduction callable, and call it. This may come from
4424
* self.dispatch_table, copyreg.dispatch_table, the object's
4425
* __reduce_ex__ method, or the object's __reduce__ method.
4426
*/
4427
if (self->dispatch_table == NULL) {
4428
reduce_func = PyDict_GetItemWithError(st->dispatch_table,
4429
(PyObject *)type);
4430
if (reduce_func == NULL) {
4431
if (PyErr_Occurred()) {
4432
goto error;
4433
}
4434
} else {
4435
/* PyDict_GetItemWithError() returns a borrowed reference.
4436
Increase the reference count to be consistent with
4437
PyObject_GetItem and _PyObject_GetAttrId used below. */
4438
Py_INCREF(reduce_func);
4439
}
4440
} else {
4441
reduce_func = PyObject_GetItem(self->dispatch_table,
4442
(PyObject *)type);
4443
if (reduce_func == NULL) {
4444
if (PyErr_ExceptionMatches(PyExc_KeyError))
4445
PyErr_Clear();
4446
else
4447
goto error;
4448
}
4449
}
4450
if (reduce_func != NULL) {
4451
reduce_value = _Pickle_FastCall(reduce_func, Py_NewRef(obj));
4452
}
4453
else if (PyType_IsSubtype(type, &PyType_Type)) {
4454
status = save_global(st, self, obj, NULL);
4455
goto done;
4456
}
4457
else {
4458
/* XXX: If the __reduce__ method is defined, __reduce_ex__ is
4459
automatically defined as __reduce__. While this is convenient, this
4460
make it impossible to know which method was actually called. Of
4461
course, this is not a big deal. But still, it would be nice to let
4462
the user know which method was called when something go
4463
wrong. Incidentally, this means if __reduce_ex__ is not defined, we
4464
don't actually have to check for a __reduce__ method. */
4465
4466
/* Check for a __reduce_ex__ method. */
4467
if (_PyObject_LookupAttr(obj, &_Py_ID(__reduce_ex__), &reduce_func) < 0) {
4468
goto error;
4469
}
4470
if (reduce_func != NULL) {
4471
PyObject *proto;
4472
proto = PyLong_FromLong(self->proto);
4473
if (proto != NULL) {
4474
reduce_value = _Pickle_FastCall(reduce_func, proto);
4475
}
4476
}
4477
else {
4478
/* Check for a __reduce__ method. */
4479
if (_PyObject_LookupAttr(obj, &_Py_ID(__reduce__), &reduce_func) < 0) {
4480
goto error;
4481
}
4482
if (reduce_func != NULL) {
4483
reduce_value = PyObject_CallNoArgs(reduce_func);
4484
}
4485
else {
4486
PyErr_Format(st->PicklingError,
4487
"can't pickle '%.200s' object: %R",
4488
type->tp_name, obj);
4489
goto error;
4490
}
4491
}
4492
}
4493
4494
if (reduce_value == NULL)
4495
goto error;
4496
4497
reduce:
4498
if (PyUnicode_Check(reduce_value)) {
4499
status = save_global(st, self, obj, reduce_value);
4500
goto done;
4501
}
4502
4503
if (!PyTuple_Check(reduce_value)) {
4504
PyErr_SetString(st->PicklingError,
4505
"__reduce__ must return a string or tuple");
4506
goto error;
4507
}
4508
4509
status = save_reduce(st, self, reduce_value, obj);
4510
4511
if (0) {
4512
error:
4513
status = -1;
4514
}
4515
done:
4516
4517
_Py_LeaveRecursiveCall();
4518
Py_XDECREF(reduce_func);
4519
Py_XDECREF(reduce_value);
4520
4521
return status;
4522
}
4523
4524
static int
4525
dump(PickleState *state, PicklerObject *self, PyObject *obj)
4526
{
4527
const char stop_op = STOP;
4528
int status = -1;
4529
PyObject *tmp;
4530
4531
if (_PyObject_LookupAttr((PyObject *)self, &_Py_ID(reducer_override),
4532
&tmp) < 0) {
4533
goto error;
4534
}
4535
/* Cache the reducer_override method, if it exists. */
4536
if (tmp != NULL) {
4537
Py_XSETREF(self->reducer_override, tmp);
4538
}
4539
else {
4540
Py_CLEAR(self->reducer_override);
4541
}
4542
4543
if (self->proto >= 2) {
4544
char header[2];
4545
4546
header[0] = PROTO;
4547
assert(self->proto >= 0 && self->proto < 256);
4548
header[1] = (unsigned char)self->proto;
4549
if (_Pickler_Write(self, header, 2) < 0)
4550
goto error;
4551
if (self->proto >= 4)
4552
self->framing = 1;
4553
}
4554
4555
if (save(state, self, obj, 0) < 0 ||
4556
_Pickler_Write(self, &stop_op, 1) < 0 ||
4557
_Pickler_CommitFrame(self) < 0)
4558
goto error;
4559
4560
// Success
4561
status = 0;
4562
4563
error:
4564
self->framing = 0;
4565
4566
/* Break the reference cycle we generated at the beginning this function
4567
* call when setting the reducer_override attribute of the Pickler instance
4568
* to a bound method of the same instance. This is important as the Pickler
4569
* instance holds a reference to each object it has pickled (through its
4570
* memo): thus, these objects won't be garbage-collected as long as the
4571
* Pickler itself is not collected. */
4572
Py_CLEAR(self->reducer_override);
4573
return status;
4574
}
4575
4576
/*[clinic input]
4577
4578
_pickle.Pickler.clear_memo
4579
4580
Clears the pickler's "memo".
4581
4582
The memo is the data structure that remembers which objects the
4583
pickler has already seen, so that shared or recursive objects are
4584
pickled by reference and not by value. This method is useful when
4585
re-using picklers.
4586
[clinic start generated code]*/
4587
4588
static PyObject *
4589
_pickle_Pickler_clear_memo_impl(PicklerObject *self)
4590
/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
4591
{
4592
if (self->memo)
4593
PyMemoTable_Clear(self->memo);
4594
4595
Py_RETURN_NONE;
4596
}
4597
4598
/*[clinic input]
4599
4600
_pickle.Pickler.dump
4601
4602
cls: defining_class
4603
obj: object
4604
/
4605
4606
Write a pickled representation of the given object to the open file.
4607
[clinic start generated code]*/
4608
4609
static PyObject *
4610
_pickle_Pickler_dump_impl(PicklerObject *self, PyTypeObject *cls,
4611
PyObject *obj)
4612
/*[clinic end generated code: output=952cf7f68b1445bb input=f949d84151983594]*/
4613
{
4614
PickleState *st = _Pickle_GetStateByClass(cls);
4615
/* Check whether the Pickler was initialized correctly (issue3664).
4616
Developers often forget to call __init__() in their subclasses, which
4617
would trigger a segfault without this check. */
4618
if (self->write == NULL) {
4619
PyErr_Format(st->PicklingError,
4620
"Pickler.__init__() was not called by %s.__init__()",
4621
Py_TYPE(self)->tp_name);
4622
return NULL;
4623
}
4624
4625
if (_Pickler_ClearBuffer(self) < 0)
4626
return NULL;
4627
4628
if (dump(st, self, obj) < 0)
4629
return NULL;
4630
4631
if (_Pickler_FlushToFile(self) < 0)
4632
return NULL;
4633
4634
Py_RETURN_NONE;
4635
}
4636
4637
/*[clinic input]
4638
4639
_pickle.Pickler.__sizeof__ -> size_t
4640
4641
Returns size in memory, in bytes.
4642
[clinic start generated code]*/
4643
4644
static size_t
4645
_pickle_Pickler___sizeof___impl(PicklerObject *self)
4646
/*[clinic end generated code: output=23ad75658d3b59ff input=d8127c8e7012ebd7]*/
4647
{
4648
size_t res = _PyObject_SIZE(Py_TYPE(self));
4649
if (self->memo != NULL) {
4650
res += sizeof(PyMemoTable);
4651
res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4652
}
4653
if (self->output_buffer != NULL) {
4654
size_t s = _PySys_GetSizeOf(self->output_buffer);
4655
if (s == (size_t)-1) {
4656
return -1;
4657
}
4658
res += s;
4659
}
4660
return res;
4661
}
4662
4663
static struct PyMethodDef Pickler_methods[] = {
4664
_PICKLE_PICKLER_DUMP_METHODDEF
4665
_PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
4666
_PICKLE_PICKLER___SIZEOF___METHODDEF
4667
{NULL, NULL} /* sentinel */
4668
};
4669
4670
static int
4671
Pickler_clear(PicklerObject *self)
4672
{
4673
Py_CLEAR(self->output_buffer);
4674
Py_CLEAR(self->write);
4675
Py_CLEAR(self->pers_func);
4676
Py_CLEAR(self->dispatch_table);
4677
Py_CLEAR(self->fast_memo);
4678
Py_CLEAR(self->reducer_override);
4679
Py_CLEAR(self->buffer_callback);
4680
4681
if (self->memo != NULL) {
4682
PyMemoTable *memo = self->memo;
4683
self->memo = NULL;
4684
PyMemoTable_Del(memo);
4685
}
4686
return 0;
4687
}
4688
4689
static void
4690
Pickler_dealloc(PicklerObject *self)
4691
{
4692
PyTypeObject *tp = Py_TYPE(self);
4693
PyObject_GC_UnTrack(self);
4694
(void)Pickler_clear(self);
4695
tp->tp_free((PyObject *)self);
4696
Py_DECREF(tp);
4697
}
4698
4699
static int
4700
Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4701
{
4702
Py_VISIT(Py_TYPE(self));
4703
Py_VISIT(self->write);
4704
Py_VISIT(self->pers_func);
4705
Py_VISIT(self->dispatch_table);
4706
Py_VISIT(self->fast_memo);
4707
Py_VISIT(self->reducer_override);
4708
Py_VISIT(self->buffer_callback);
4709
return 0;
4710
}
4711
4712
4713
/*[clinic input]
4714
4715
_pickle.Pickler.__init__
4716
4717
file: object
4718
protocol: object = None
4719
fix_imports: bool = True
4720
buffer_callback: object = None
4721
4722
This takes a binary file for writing a pickle data stream.
4723
4724
The optional *protocol* argument tells the pickler to use the given
4725
protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
4726
protocol is 4. It was introduced in Python 3.4, and is incompatible
4727
with previous versions.
4728
4729
Specifying a negative protocol version selects the highest protocol
4730
version supported. The higher the protocol used, the more recent the
4731
version of Python needed to read the pickle produced.
4732
4733
The *file* argument must have a write() method that accepts a single
4734
bytes argument. It can thus be a file object opened for binary
4735
writing, an io.BytesIO instance, or any other custom object that meets
4736
this interface.
4737
4738
If *fix_imports* is True and protocol is less than 3, pickle will try
4739
to map the new Python 3 names to the old module names used in Python
4740
2, so that the pickle data stream is readable with Python 2.
4741
4742
If *buffer_callback* is None (the default), buffer views are
4743
serialized into *file* as part of the pickle stream.
4744
4745
If *buffer_callback* is not None, then it can be called any number
4746
of times with a buffer view. If the callback returns a false value
4747
(such as None), the given buffer is out-of-band; otherwise the
4748
buffer is serialized in-band, i.e. inside the pickle stream.
4749
4750
It is an error if *buffer_callback* is not None and *protocol*
4751
is None or smaller than 5.
4752
4753
[clinic start generated code]*/
4754
4755
static int
4756
_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4757
PyObject *protocol, int fix_imports,
4758
PyObject *buffer_callback)
4759
/*[clinic end generated code: output=0abedc50590d259b input=a7c969699bf5dad3]*/
4760
{
4761
/* In case of multiple __init__() calls, clear previous content. */
4762
if (self->write != NULL)
4763
(void)Pickler_clear(self);
4764
4765
if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
4766
return -1;
4767
4768
if (_Pickler_SetOutputStream(self, file) < 0)
4769
return -1;
4770
4771
if (_Pickler_SetBufferCallback(self, buffer_callback) < 0)
4772
return -1;
4773
4774
/* memo and output_buffer may have already been created in _Pickler_New */
4775
if (self->memo == NULL) {
4776
self->memo = PyMemoTable_New();
4777
if (self->memo == NULL)
4778
return -1;
4779
}
4780
self->output_len = 0;
4781
if (self->output_buffer == NULL) {
4782
self->max_output_len = WRITE_BUF_SIZE;
4783
self->output_buffer = PyBytes_FromStringAndSize(NULL,
4784
self->max_output_len);
4785
if (self->output_buffer == NULL)
4786
return -1;
4787
}
4788
4789
self->fast = 0;
4790
self->fast_nesting = 0;
4791
self->fast_memo = NULL;
4792
4793
if (init_method_ref((PyObject *)self, &_Py_ID(persistent_id),
4794
&self->pers_func, &self->pers_func_self) < 0)
4795
{
4796
return -1;
4797
}
4798
if (self->dispatch_table != NULL) {
4799
return 0;
4800
}
4801
if (_PyObject_LookupAttr((PyObject *)self, &_Py_ID(dispatch_table),
4802
&self->dispatch_table) < 0) {
4803
return -1;
4804
}
4805
4806
return 0;
4807
}
4808
4809
4810
/* Define a proxy object for the Pickler's internal memo object. This is to
4811
* avoid breaking code like:
4812
* pickler.memo.clear()
4813
* and
4814
* pickler.memo = saved_memo
4815
* Is this a good idea? Not really, but we don't want to break code that uses
4816
* it. Note that we don't implement the entire mapping API here. This is
4817
* intentional, as these should be treated as black-box implementation details.
4818
*/
4819
4820
/*[clinic input]
4821
_pickle.PicklerMemoProxy.clear
4822
4823
Remove all items from memo.
4824
[clinic start generated code]*/
4825
4826
static PyObject *
4827
_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
4828
/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
4829
{
4830
if (self->pickler->memo)
4831
PyMemoTable_Clear(self->pickler->memo);
4832
Py_RETURN_NONE;
4833
}
4834
4835
/*[clinic input]
4836
_pickle.PicklerMemoProxy.copy
4837
4838
Copy the memo to a new object.
4839
[clinic start generated code]*/
4840
4841
static PyObject *
4842
_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
4843
/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
4844
{
4845
PyMemoTable *memo;
4846
PyObject *new_memo = PyDict_New();
4847
if (new_memo == NULL)
4848
return NULL;
4849
4850
memo = self->pickler->memo;
4851
for (size_t i = 0; i < memo->mt_allocated; ++i) {
4852
PyMemoEntry entry = memo->mt_table[i];
4853
if (entry.me_key != NULL) {
4854
int status;
4855
PyObject *key, *value;
4856
4857
key = PyLong_FromVoidPtr(entry.me_key);
4858
if (key == NULL) {
4859
goto error;
4860
}
4861
value = Py_BuildValue("nO", entry.me_value, entry.me_key);
4862
if (value == NULL) {
4863
Py_DECREF(key);
4864
goto error;
4865
}
4866
status = PyDict_SetItem(new_memo, key, value);
4867
Py_DECREF(key);
4868
Py_DECREF(value);
4869
if (status < 0)
4870
goto error;
4871
}
4872
}
4873
return new_memo;
4874
4875
error:
4876
Py_XDECREF(new_memo);
4877
return NULL;
4878
}
4879
4880
/*[clinic input]
4881
_pickle.PicklerMemoProxy.__reduce__
4882
4883
Implement pickle support.
4884
[clinic start generated code]*/
4885
4886
static PyObject *
4887
_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
4888
/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
4889
{
4890
PyObject *reduce_value, *dict_args;
4891
PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
4892
if (contents == NULL)
4893
return NULL;
4894
4895
reduce_value = PyTuple_New(2);
4896
if (reduce_value == NULL) {
4897
Py_DECREF(contents);
4898
return NULL;
4899
}
4900
dict_args = PyTuple_New(1);
4901
if (dict_args == NULL) {
4902
Py_DECREF(contents);
4903
Py_DECREF(reduce_value);
4904
return NULL;
4905
}
4906
PyTuple_SET_ITEM(dict_args, 0, contents);
4907
PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
4908
PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4909
return reduce_value;
4910
}
4911
4912
static PyMethodDef picklerproxy_methods[] = {
4913
_PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4914
_PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4915
_PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
4916
{NULL, NULL} /* sentinel */
4917
};
4918
4919
static void
4920
PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4921
{
4922
PyTypeObject *tp = Py_TYPE(self);
4923
PyObject_GC_UnTrack(self);
4924
Py_CLEAR(self->pickler);
4925
tp->tp_free((PyObject *)self);
4926
Py_DECREF(tp);
4927
}
4928
4929
static int
4930
PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4931
visitproc visit, void *arg)
4932
{
4933
Py_VISIT(Py_TYPE(self));
4934
Py_VISIT(self->pickler);
4935
return 0;
4936
}
4937
4938
static int
4939
PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4940
{
4941
Py_CLEAR(self->pickler);
4942
return 0;
4943
}
4944
4945
static PyType_Slot memoproxy_slots[] = {
4946
{Py_tp_dealloc, PicklerMemoProxy_dealloc},
4947
{Py_tp_traverse, PicklerMemoProxy_traverse},
4948
{Py_tp_clear, PicklerMemoProxy_clear},
4949
{Py_tp_methods, picklerproxy_methods},
4950
{Py_tp_hash, PyObject_HashNotImplemented},
4951
{0, NULL},
4952
};
4953
4954
static PyType_Spec memoproxy_spec = {
4955
.name = "_pickle.PicklerMemoProxy",
4956
.basicsize = sizeof(PicklerMemoProxyObject),
4957
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
4958
Py_TPFLAGS_IMMUTABLETYPE),
4959
.slots = memoproxy_slots,
4960
};
4961
4962
static PyObject *
4963
PicklerMemoProxy_New(PicklerObject *pickler)
4964
{
4965
PicklerMemoProxyObject *self;
4966
PickleState *st = _Pickle_FindStateByType(Py_TYPE(pickler));
4967
self = PyObject_GC_New(PicklerMemoProxyObject, st->PicklerMemoProxyType);
4968
if (self == NULL)
4969
return NULL;
4970
self->pickler = (PicklerObject*)Py_NewRef(pickler);
4971
PyObject_GC_Track(self);
4972
return (PyObject *)self;
4973
}
4974
4975
/*****************************************************************************/
4976
4977
static PyObject *
4978
Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored))
4979
{
4980
return PicklerMemoProxy_New(self);
4981
}
4982
4983
static int
4984
Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
4985
{
4986
PyMemoTable *new_memo = NULL;
4987
4988
if (obj == NULL) {
4989
PyErr_SetString(PyExc_TypeError,
4990
"attribute deletion is not supported");
4991
return -1;
4992
}
4993
4994
PickleState *st = _Pickle_FindStateByType(Py_TYPE(self));
4995
if (Py_IS_TYPE(obj, st->PicklerMemoProxyType)) {
4996
PicklerObject *pickler =
4997
((PicklerMemoProxyObject *)obj)->pickler;
4998
4999
new_memo = PyMemoTable_Copy(pickler->memo);
5000
if (new_memo == NULL)
5001
return -1;
5002
}
5003
else if (PyDict_Check(obj)) {
5004
Py_ssize_t i = 0;
5005
PyObject *key, *value;
5006
5007
new_memo = PyMemoTable_New();
5008
if (new_memo == NULL)
5009
return -1;
5010
5011
while (PyDict_Next(obj, &i, &key, &value)) {
5012
Py_ssize_t memo_id;
5013
PyObject *memo_obj;
5014
5015
if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) {
5016
PyErr_SetString(PyExc_TypeError,
5017
"'memo' values must be 2-item tuples");
5018
goto error;
5019
}
5020
memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
5021
if (memo_id == -1 && PyErr_Occurred())
5022
goto error;
5023
memo_obj = PyTuple_GET_ITEM(value, 1);
5024
if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
5025
goto error;
5026
}
5027
}
5028
else {
5029
PyErr_Format(PyExc_TypeError,
5030
"'memo' attribute must be a PicklerMemoProxy object "
5031
"or dict, not %.200s", Py_TYPE(obj)->tp_name);
5032
return -1;
5033
}
5034
5035
PyMemoTable_Del(self->memo);
5036
self->memo = new_memo;
5037
5038
return 0;
5039
5040
error:
5041
if (new_memo)
5042
PyMemoTable_Del(new_memo);
5043
return -1;
5044
}
5045
5046
static PyObject *
5047
Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored))
5048
{
5049
if (self->pers_func == NULL) {
5050
PyErr_SetString(PyExc_AttributeError, "persistent_id");
5051
return NULL;
5052
}
5053
return reconstruct_method(self->pers_func, self->pers_func_self);
5054
}
5055
5056
static int
5057
Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
5058
{
5059
if (value == NULL) {
5060
PyErr_SetString(PyExc_TypeError,
5061
"attribute deletion is not supported");
5062
return -1;
5063
}
5064
if (!PyCallable_Check(value)) {
5065
PyErr_SetString(PyExc_TypeError,
5066
"persistent_id must be a callable taking one argument");
5067
return -1;
5068
}
5069
5070
self->pers_func_self = NULL;
5071
Py_XSETREF(self->pers_func, Py_NewRef(value));
5072
5073
return 0;
5074
}
5075
5076
static PyMemberDef Pickler_members[] = {
5077
{"bin", T_INT, offsetof(PicklerObject, bin)},
5078
{"fast", T_INT, offsetof(PicklerObject, fast)},
5079
{"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
5080
{NULL}
5081
};
5082
5083
static PyGetSetDef Pickler_getsets[] = {
5084
{"memo", (getter)Pickler_get_memo,
5085
(setter)Pickler_set_memo},
5086
{"persistent_id", (getter)Pickler_get_persid,
5087
(setter)Pickler_set_persid},
5088
{NULL}
5089
};
5090
5091
static PyType_Slot pickler_type_slots[] = {
5092
{Py_tp_dealloc, Pickler_dealloc},
5093
{Py_tp_methods, Pickler_methods},
5094
{Py_tp_members, Pickler_members},
5095
{Py_tp_getset, Pickler_getsets},
5096
{Py_tp_clear, Pickler_clear},
5097
{Py_tp_doc, (char*)_pickle_Pickler___init____doc__},
5098
{Py_tp_traverse, Pickler_traverse},
5099
{Py_tp_init, _pickle_Pickler___init__},
5100
{Py_tp_new, PyType_GenericNew},
5101
{Py_tp_alloc, PyType_GenericAlloc},
5102
{Py_tp_free, PyObject_GC_Del},
5103
{0, NULL},
5104
};
5105
5106
static PyType_Spec pickler_type_spec = {
5107
.name = "_pickle.Pickler",
5108
.basicsize = sizeof(PicklerObject),
5109
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
5110
Py_TPFLAGS_IMMUTABLETYPE),
5111
.slots = pickler_type_slots,
5112
};
5113
5114
/* Temporary helper for calling self.find_class().
5115
5116
XXX: It would be nice to able to avoid Python function call overhead, by
5117
using directly the C version of find_class(), when find_class() is not
5118
overridden by a subclass. Although, this could become rather hackish. A
5119
simpler optimization would be to call the C function when self is not a
5120
subclass instance. */
5121
static PyObject *
5122
find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
5123
{
5124
return PyObject_CallMethodObjArgs((PyObject *)self, &_Py_ID(find_class),
5125
module_name, global_name, NULL);
5126
}
5127
5128
static Py_ssize_t
5129
marker(PickleState *st, UnpicklerObject *self)
5130
{
5131
if (self->num_marks < 1) {
5132
PyErr_SetString(st->UnpicklingError, "could not find MARK");
5133
return -1;
5134
}
5135
5136
Py_ssize_t mark = self->marks[--self->num_marks];
5137
self->stack->mark_set = self->num_marks != 0;
5138
self->stack->fence = self->num_marks ?
5139
self->marks[self->num_marks - 1] : 0;
5140
return mark;
5141
}
5142
5143
static int
5144
load_none(PickleState *state, UnpicklerObject *self)
5145
{
5146
PDATA_APPEND(self->stack, Py_None, -1);
5147
return 0;
5148
}
5149
5150
static int
5151
load_int(PickleState *state, UnpicklerObject *self)
5152
{
5153
PyObject *value;
5154
char *endptr, *s;
5155
Py_ssize_t len;
5156
long x;
5157
5158
if ((len = _Unpickler_Readline(state, self, &s)) < 0)
5159
return -1;
5160
if (len < 2)
5161
return bad_readline(state);
5162
5163
errno = 0;
5164
/* XXX: Should the base argument of strtol() be explicitly set to 10?
5165
XXX(avassalotti): Should this uses PyOS_strtol()? */
5166
x = strtol(s, &endptr, 0);
5167
5168
if (errno || (*endptr != '\n' && *endptr != '\0')) {
5169
/* Hm, maybe we've got something long. Let's try reading
5170
* it as a Python int object. */
5171
errno = 0;
5172
/* XXX: Same thing about the base here. */
5173
value = PyLong_FromString(s, NULL, 0);
5174
if (value == NULL) {
5175
PyErr_SetString(PyExc_ValueError,
5176
"could not convert string to int");
5177
return -1;
5178
}
5179
}
5180
else {
5181
if (len == 3 && (x == 0 || x == 1)) {
5182
if ((value = PyBool_FromLong(x)) == NULL)
5183
return -1;
5184
}
5185
else {
5186
if ((value = PyLong_FromLong(x)) == NULL)
5187
return -1;
5188
}
5189
}
5190
5191
PDATA_PUSH(self->stack, value, -1);
5192
return 0;
5193
}
5194
5195
static int
5196
load_bool(PickleState *state, UnpicklerObject *self, PyObject *boolean)
5197
{
5198
assert(boolean == Py_True || boolean == Py_False);
5199
PDATA_APPEND(self->stack, boolean, -1);
5200
return 0;
5201
}
5202
5203
/* s contains x bytes of an unsigned little-endian integer. Return its value
5204
* as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
5205
*/
5206
static Py_ssize_t
5207
calc_binsize(char *bytes, int nbytes)
5208
{
5209
unsigned char *s = (unsigned char *)bytes;
5210
int i;
5211
size_t x = 0;
5212
5213
if (nbytes > (int)sizeof(size_t)) {
5214
/* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
5215
* have 64-bit size that can't be represented on 32-bit platform.
5216
*/
5217
for (i = (int)sizeof(size_t); i < nbytes; i++) {
5218
if (s[i])
5219
return -1;
5220
}
5221
nbytes = (int)sizeof(size_t);
5222
}
5223
for (i = 0; i < nbytes; i++) {
5224
x |= (size_t) s[i] << (8 * i);
5225
}
5226
5227
if (x > PY_SSIZE_T_MAX)
5228
return -1;
5229
else
5230
return (Py_ssize_t) x;
5231
}
5232
5233
/* s contains x bytes of a little-endian integer. Return its value as a
5234
* C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
5235
* int, but when x is 4 it's a signed one. This is a historical source
5236
* of x-platform bugs.
5237
*/
5238
static long
5239
calc_binint(char *bytes, int nbytes)
5240
{
5241
unsigned char *s = (unsigned char *)bytes;
5242
Py_ssize_t i;
5243
long x = 0;
5244
5245
for (i = 0; i < nbytes; i++) {
5246
x |= (long)s[i] << (8 * i);
5247
}
5248
5249
/* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
5250
* is signed, so on a box with longs bigger than 4 bytes we need
5251
* to extend a BININT's sign bit to the full width.
5252
*/
5253
if (SIZEOF_LONG > 4 && nbytes == 4) {
5254
x |= -(x & (1L << 31));
5255
}
5256
5257
return x;
5258
}
5259
5260
static int
5261
load_binintx(UnpicklerObject *self, char *s, int size)
5262
{
5263
PyObject *value;
5264
long x;
5265
5266
x = calc_binint(s, size);
5267
5268
if ((value = PyLong_FromLong(x)) == NULL)
5269
return -1;
5270
5271
PDATA_PUSH(self->stack, value, -1);
5272
return 0;
5273
}
5274
5275
static int
5276
load_binint(PickleState *state, UnpicklerObject *self)
5277
{
5278
char *s;
5279
if (_Unpickler_Read(self, state, &s, 4) < 0)
5280
return -1;
5281
5282
return load_binintx(self, s, 4);
5283
}
5284
5285
static int
5286
load_binint1(PickleState *state, UnpicklerObject *self)
5287
{
5288
char *s;
5289
if (_Unpickler_Read(self, state, &s, 1) < 0)
5290
return -1;
5291
5292
return load_binintx(self, s, 1);
5293
}
5294
5295
static int
5296
load_binint2(PickleState *state, UnpicklerObject *self)
5297
{
5298
char *s;
5299
if (_Unpickler_Read(self, state, &s, 2) < 0)
5300
return -1;
5301
5302
return load_binintx(self, s, 2);
5303
}
5304
5305
static int
5306
load_long(PickleState *state, UnpicklerObject *self)
5307
{
5308
PyObject *value;
5309
char *s = NULL;
5310
Py_ssize_t len;
5311
5312
if ((len = _Unpickler_Readline(state, self, &s)) < 0)
5313
return -1;
5314
if (len < 2)
5315
return bad_readline(state);
5316
5317
/* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
5318
the 'L' before calling PyLong_FromString. In order to maintain
5319
compatibility with Python 3.0.0, we don't actually *require*
5320
the 'L' to be present. */
5321
if (s[len-2] == 'L')
5322
s[len-2] = '\0';
5323
/* XXX: Should the base argument explicitly set to 10? */
5324
value = PyLong_FromString(s, NULL, 0);
5325
if (value == NULL)
5326
return -1;
5327
5328
PDATA_PUSH(self->stack, value, -1);
5329
return 0;
5330
}
5331
5332
/* 'size' bytes contain the # of bytes of little-endian 256's-complement
5333
* data following.
5334
*/
5335
static int
5336
load_counted_long(PickleState *st, UnpicklerObject *self, int size)
5337
{
5338
PyObject *value;
5339
char *nbytes;
5340
char *pdata;
5341
5342
assert(size == 1 || size == 4);
5343
if (_Unpickler_Read(self, st, &nbytes, size) < 0)
5344
return -1;
5345
5346
size = calc_binint(nbytes, size);
5347
if (size < 0) {
5348
/* Corrupt or hostile pickle -- we never write one like this */
5349
PyErr_SetString(st->UnpicklingError,
5350
"LONG pickle has negative byte count");
5351
return -1;
5352
}
5353
5354
if (size == 0)
5355
value = PyLong_FromLong(0L);
5356
else {
5357
/* Read the raw little-endian bytes and convert. */
5358
if (_Unpickler_Read(self, st, &pdata, size) < 0)
5359
return -1;
5360
value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
5361
1 /* little endian */ , 1 /* signed */ );
5362
}
5363
if (value == NULL)
5364
return -1;
5365
PDATA_PUSH(self->stack, value, -1);
5366
return 0;
5367
}
5368
5369
static int
5370
load_float(PickleState *state, UnpicklerObject *self)
5371
{
5372
PyObject *value;
5373
char *endptr, *s;
5374
Py_ssize_t len;
5375
double d;
5376
5377
if ((len = _Unpickler_Readline(state, self, &s)) < 0)
5378
return -1;
5379
if (len < 2)
5380
return bad_readline(state);
5381
5382
errno = 0;
5383
d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
5384
if (d == -1.0 && PyErr_Occurred())
5385
return -1;
5386
if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
5387
PyErr_SetString(PyExc_ValueError, "could not convert string to float");
5388
return -1;
5389
}
5390
value = PyFloat_FromDouble(d);
5391
if (value == NULL)
5392
return -1;
5393
5394
PDATA_PUSH(self->stack, value, -1);
5395
return 0;
5396
}
5397
5398
static int
5399
load_binfloat(PickleState *state, UnpicklerObject *self)
5400
{
5401
PyObject *value;
5402
double x;
5403
char *s;
5404
5405
if (_Unpickler_Read(self, state, &s, 8) < 0)
5406
return -1;
5407
5408
x = PyFloat_Unpack8(s, 0);
5409
if (x == -1.0 && PyErr_Occurred())
5410
return -1;
5411
5412
if ((value = PyFloat_FromDouble(x)) == NULL)
5413
return -1;
5414
5415
PDATA_PUSH(self->stack, value, -1);
5416
return 0;
5417
}
5418
5419
static int
5420
load_string(PickleState *st, UnpicklerObject *self)
5421
{
5422
PyObject *bytes;
5423
PyObject *obj;
5424
Py_ssize_t len;
5425
char *s, *p;
5426
5427
if ((len = _Unpickler_Readline(st, self, &s)) < 0)
5428
return -1;
5429
/* Strip the newline */
5430
len--;
5431
/* Strip outermost quotes */
5432
if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
5433
p = s + 1;
5434
len -= 2;
5435
}
5436
else {
5437
PyErr_SetString(st->UnpicklingError,
5438
"the STRING opcode argument must be quoted");
5439
return -1;
5440
}
5441
assert(len >= 0);
5442
5443
/* Use the PyBytes API to decode the string, since that is what is used
5444
to encode, and then coerce the result to Unicode. */
5445
bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
5446
if (bytes == NULL)
5447
return -1;
5448
5449
/* Leave the Python 2.x strings as bytes if the *encoding* given to the
5450
Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5451
if (strcmp(self->encoding, "bytes") == 0) {
5452
obj = bytes;
5453
}
5454
else {
5455
obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
5456
Py_DECREF(bytes);
5457
if (obj == NULL) {
5458
return -1;
5459
}
5460
}
5461
5462
PDATA_PUSH(self->stack, obj, -1);
5463
return 0;
5464
}
5465
5466
static int
5467
load_counted_binstring(PickleState *st, UnpicklerObject *self, int nbytes)
5468
{
5469
PyObject *obj;
5470
Py_ssize_t size;
5471
char *s;
5472
5473
if (_Unpickler_Read(self, st, &s, nbytes) < 0)
5474
return -1;
5475
5476
size = calc_binsize(s, nbytes);
5477
if (size < 0) {
5478
PyErr_Format(st->UnpicklingError,
5479
"BINSTRING exceeds system's maximum size of %zd bytes",
5480
PY_SSIZE_T_MAX);
5481
return -1;
5482
}
5483
5484
if (_Unpickler_Read(self, st, &s, size) < 0)
5485
return -1;
5486
5487
/* Convert Python 2.x strings to bytes if the *encoding* given to the
5488
Unpickler was 'bytes'. Otherwise, convert them to unicode. */
5489
if (strcmp(self->encoding, "bytes") == 0) {
5490
obj = PyBytes_FromStringAndSize(s, size);
5491
}
5492
else {
5493
obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
5494
}
5495
if (obj == NULL) {
5496
return -1;
5497
}
5498
5499
PDATA_PUSH(self->stack, obj, -1);
5500
return 0;
5501
}
5502
5503
static int
5504
load_counted_binbytes(PickleState *state, UnpicklerObject *self, int nbytes)
5505
{
5506
PyObject *bytes;
5507
Py_ssize_t size;
5508
char *s;
5509
5510
if (_Unpickler_Read(self, state, &s, nbytes) < 0)
5511
return -1;
5512
5513
size = calc_binsize(s, nbytes);
5514
if (size < 0) {
5515
PyErr_Format(PyExc_OverflowError,
5516
"BINBYTES exceeds system's maximum size of %zd bytes",
5517
PY_SSIZE_T_MAX);
5518
return -1;
5519
}
5520
5521
bytes = PyBytes_FromStringAndSize(NULL, size);
5522
if (bytes == NULL)
5523
return -1;
5524
if (_Unpickler_ReadInto(state, self, PyBytes_AS_STRING(bytes), size) < 0) {
5525
Py_DECREF(bytes);
5526
return -1;
5527
}
5528
5529
PDATA_PUSH(self->stack, bytes, -1);
5530
return 0;
5531
}
5532
5533
static int
5534
load_counted_bytearray(PickleState *state, UnpicklerObject *self)
5535
{
5536
PyObject *bytearray;
5537
Py_ssize_t size;
5538
char *s;
5539
5540
if (_Unpickler_Read(self, state, &s, 8) < 0) {
5541
return -1;
5542
}
5543
5544
size = calc_binsize(s, 8);
5545
if (size < 0) {
5546
PyErr_Format(PyExc_OverflowError,
5547
"BYTEARRAY8 exceeds system's maximum size of %zd bytes",
5548
PY_SSIZE_T_MAX);
5549
return -1;
5550
}
5551
5552
bytearray = PyByteArray_FromStringAndSize(NULL, size);
5553
if (bytearray == NULL) {
5554
return -1;
5555
}
5556
char *str = PyByteArray_AS_STRING(bytearray);
5557
if (_Unpickler_ReadInto(state, self, str, size) < 0) {
5558
Py_DECREF(bytearray);
5559
return -1;
5560
}
5561
5562
PDATA_PUSH(self->stack, bytearray, -1);
5563
return 0;
5564
}
5565
5566
static int
5567
load_next_buffer(PickleState *st, UnpicklerObject *self)
5568
{
5569
if (self->buffers == NULL) {
5570
PyErr_SetString(st->UnpicklingError,
5571
"pickle stream refers to out-of-band data "
5572
"but no *buffers* argument was given");
5573
return -1;
5574
}
5575
PyObject *buf = PyIter_Next(self->buffers);
5576
if (buf == NULL) {
5577
if (!PyErr_Occurred()) {
5578
PyErr_SetString(st->UnpicklingError,
5579
"not enough out-of-band buffers");
5580
}
5581
return -1;
5582
}
5583
5584
PDATA_PUSH(self->stack, buf, -1);
5585
return 0;
5586
}
5587
5588
static int
5589
load_readonly_buffer(PickleState *state, UnpicklerObject *self)
5590
{
5591
Py_ssize_t len = Py_SIZE(self->stack);
5592
if (len <= self->stack->fence) {
5593
return Pdata_stack_underflow(state, self->stack);
5594
}
5595
5596
PyObject *obj = self->stack->data[len - 1];
5597
PyObject *view = PyMemoryView_FromObject(obj);
5598
if (view == NULL) {
5599
return -1;
5600
}
5601
if (!PyMemoryView_GET_BUFFER(view)->readonly) {
5602
/* Original object is writable */
5603
PyMemoryView_GET_BUFFER(view)->readonly = 1;
5604
self->stack->data[len - 1] = view;
5605
Py_DECREF(obj);
5606
}
5607
else {
5608
/* Original object is read-only, no need to replace it */
5609
Py_DECREF(view);
5610
}
5611
return 0;
5612
}
5613
5614
static int
5615
load_unicode(PickleState *state, UnpicklerObject *self)
5616
{
5617
PyObject *str;
5618
Py_ssize_t len;
5619
char *s = NULL;
5620
5621
if ((len = _Unpickler_Readline(state, self, &s)) < 0)
5622
return -1;
5623
if (len < 1)
5624
return bad_readline(state);
5625
5626
str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5627
if (str == NULL)
5628
return -1;
5629
5630
PDATA_PUSH(self->stack, str, -1);
5631
return 0;
5632
}
5633
5634
static int
5635
load_counted_binunicode(PickleState *state, UnpicklerObject *self, int nbytes)
5636
{
5637
PyObject *str;
5638
Py_ssize_t size;
5639
char *s;
5640
5641
if (_Unpickler_Read(self, state, &s, nbytes) < 0)
5642
return -1;
5643
5644
size = calc_binsize(s, nbytes);
5645
if (size < 0) {
5646
PyErr_Format(PyExc_OverflowError,
5647
"BINUNICODE exceeds system's maximum size of %zd bytes",
5648
PY_SSIZE_T_MAX);
5649
return -1;
5650
}
5651
5652
if (_Unpickler_Read(self, state, &s, size) < 0)
5653
return -1;
5654
5655
str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
5656
if (str == NULL)
5657
return -1;
5658
5659
PDATA_PUSH(self->stack, str, -1);
5660
return 0;
5661
}
5662
5663
static int
5664
load_counted_tuple(PickleState *state, UnpicklerObject *self, Py_ssize_t len)
5665
{
5666
PyObject *tuple;
5667
5668
if (Py_SIZE(self->stack) < len)
5669
return Pdata_stack_underflow(state, self->stack);
5670
5671
tuple = Pdata_poptuple(state, self->stack, Py_SIZE(self->stack) - len);
5672
if (tuple == NULL)
5673
return -1;
5674
PDATA_PUSH(self->stack, tuple, -1);
5675
return 0;
5676
}
5677
5678
static int
5679
load_tuple(PickleState *state, UnpicklerObject *self)
5680
{
5681
Py_ssize_t i;
5682
5683
if ((i = marker(state, self)) < 0)
5684
return -1;
5685
5686
return load_counted_tuple(state, self, Py_SIZE(self->stack) - i);
5687
}
5688
5689
static int
5690
load_empty_list(PickleState *state, UnpicklerObject *self)
5691
{
5692
PyObject *list;
5693
5694
if ((list = PyList_New(0)) == NULL)
5695
return -1;
5696
PDATA_PUSH(self->stack, list, -1);
5697
return 0;
5698
}
5699
5700
static int
5701
load_empty_dict(PickleState *state, UnpicklerObject *self)
5702
{
5703
PyObject *dict;
5704
5705
if ((dict = PyDict_New()) == NULL)
5706
return -1;
5707
PDATA_PUSH(self->stack, dict, -1);
5708
return 0;
5709
}
5710
5711
static int
5712
load_empty_set(PickleState *state, UnpicklerObject *self)
5713
{
5714
PyObject *set;
5715
5716
if ((set = PySet_New(NULL)) == NULL)
5717
return -1;
5718
PDATA_PUSH(self->stack, set, -1);
5719
return 0;
5720
}
5721
5722
static int
5723
load_list(PickleState *state, UnpicklerObject *self)
5724
{
5725
PyObject *list;
5726
Py_ssize_t i;
5727
5728
if ((i = marker(state, self)) < 0)
5729
return -1;
5730
5731
list = Pdata_poplist(self->stack, i);
5732
if (list == NULL)
5733
return -1;
5734
PDATA_PUSH(self->stack, list, -1);
5735
return 0;
5736
}
5737
5738
static int
5739
load_dict(PickleState *st, UnpicklerObject *self)
5740
{
5741
PyObject *dict, *key, *value;
5742
Py_ssize_t i, j, k;
5743
5744
if ((i = marker(st, self)) < 0)
5745
return -1;
5746
j = Py_SIZE(self->stack);
5747
5748
if ((dict = PyDict_New()) == NULL)
5749
return -1;
5750
5751
if ((j - i) % 2 != 0) {
5752
PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
5753
Py_DECREF(dict);
5754
return -1;
5755
}
5756
5757
for (k = i + 1; k < j; k += 2) {
5758
key = self->stack->data[k - 1];
5759
value = self->stack->data[k];
5760
if (PyDict_SetItem(dict, key, value) < 0) {
5761
Py_DECREF(dict);
5762
return -1;
5763
}
5764
}
5765
Pdata_clear(self->stack, i);
5766
PDATA_PUSH(self->stack, dict, -1);
5767
return 0;
5768
}
5769
5770
static int
5771
load_frozenset(PickleState *state, UnpicklerObject *self)
5772
{
5773
PyObject *items;
5774
PyObject *frozenset;
5775
Py_ssize_t i;
5776
5777
if ((i = marker(state, self)) < 0)
5778
return -1;
5779
5780
items = Pdata_poptuple(state, self->stack, i);
5781
if (items == NULL)
5782
return -1;
5783
5784
frozenset = PyFrozenSet_New(items);
5785
Py_DECREF(items);
5786
if (frozenset == NULL)
5787
return -1;
5788
5789
PDATA_PUSH(self->stack, frozenset, -1);
5790
return 0;
5791
}
5792
5793
static PyObject *
5794
instantiate(PyObject *cls, PyObject *args)
5795
{
5796
/* Caller must assure args are a tuple. Normally, args come from
5797
Pdata_poptuple which packs objects from the top of the stack
5798
into a newly created tuple. */
5799
assert(PyTuple_Check(args));
5800
if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5801
PyObject *func;
5802
if (_PyObject_LookupAttr(cls, &_Py_ID(__getinitargs__), &func) < 0) {
5803
return NULL;
5804
}
5805
if (func == NULL) {
5806
return PyObject_CallMethodOneArg(cls, &_Py_ID(__new__), cls);
5807
}
5808
Py_DECREF(func);
5809
}
5810
return PyObject_CallObject(cls, args);
5811
}
5812
5813
static int
5814
load_obj(PickleState *state, UnpicklerObject *self)
5815
{
5816
PyObject *cls, *args, *obj = NULL;
5817
Py_ssize_t i;
5818
5819
if ((i = marker(state, self)) < 0)
5820
return -1;
5821
5822
if (Py_SIZE(self->stack) - i < 1)
5823
return Pdata_stack_underflow(state, self->stack);
5824
5825
args = Pdata_poptuple(state, self->stack, i + 1);
5826
if (args == NULL)
5827
return -1;
5828
5829
PDATA_POP(state, self->stack, cls);
5830
if (cls) {
5831
obj = instantiate(cls, args);
5832
Py_DECREF(cls);
5833
}
5834
Py_DECREF(args);
5835
if (obj == NULL)
5836
return -1;
5837
5838
PDATA_PUSH(self->stack, obj, -1);
5839
return 0;
5840
}
5841
5842
static int
5843
load_inst(PickleState *state, UnpicklerObject *self)
5844
{
5845
PyObject *cls = NULL;
5846
PyObject *args = NULL;
5847
PyObject *obj = NULL;
5848
PyObject *module_name;
5849
PyObject *class_name;
5850
Py_ssize_t len;
5851
Py_ssize_t i;
5852
char *s;
5853
5854
if ((i = marker(state, self)) < 0)
5855
return -1;
5856
if ((len = _Unpickler_Readline(state, self, &s)) < 0)
5857
return -1;
5858
if (len < 2)
5859
return bad_readline(state);
5860
5861
/* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5862
identifiers are permitted in Python 3.0, since the INST opcode is only
5863
supported by older protocols on Python 2.x. */
5864
module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5865
if (module_name == NULL)
5866
return -1;
5867
5868
if ((len = _Unpickler_Readline(state, self, &s)) >= 0) {
5869
if (len < 2) {
5870
Py_DECREF(module_name);
5871
return bad_readline(state);
5872
}
5873
class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5874
if (class_name != NULL) {
5875
cls = find_class(self, module_name, class_name);
5876
Py_DECREF(class_name);
5877
}
5878
}
5879
Py_DECREF(module_name);
5880
5881
if (cls == NULL)
5882
return -1;
5883
5884
if ((args = Pdata_poptuple(state, self->stack, i)) != NULL) {
5885
obj = instantiate(cls, args);
5886
Py_DECREF(args);
5887
}
5888
Py_DECREF(cls);
5889
5890
if (obj == NULL)
5891
return -1;
5892
5893
PDATA_PUSH(self->stack, obj, -1);
5894
return 0;
5895
}
5896
5897
static void
5898
newobj_unpickling_error(PickleState *st, const char *msg, int use_kwargs,
5899
PyObject *arg)
5900
{
5901
PyErr_Format(st->UnpicklingError, msg,
5902
use_kwargs ? "NEWOBJ_EX" : "NEWOBJ",
5903
Py_TYPE(arg)->tp_name);
5904
}
5905
5906
static int
5907
load_newobj(PickleState *state, UnpicklerObject *self, int use_kwargs)
5908
{
5909
PyObject *cls, *args, *kwargs = NULL;
5910
PyObject *obj;
5911
5912
/* Stack is ... cls args [kwargs], and we want to call
5913
* cls.__new__(cls, *args, **kwargs).
5914
*/
5915
if (use_kwargs) {
5916
PDATA_POP(state, self->stack, kwargs);
5917
if (kwargs == NULL) {
5918
return -1;
5919
}
5920
}
5921
PDATA_POP(state, self->stack, args);
5922
if (args == NULL) {
5923
Py_XDECREF(kwargs);
5924
return -1;
5925
}
5926
PDATA_POP(state, self->stack, cls);
5927
if (cls == NULL) {
5928
Py_XDECREF(kwargs);
5929
Py_DECREF(args);
5930
return -1;
5931
}
5932
5933
if (!PyType_Check(cls)) {
5934
newobj_unpickling_error(state,
5935
"%s class argument must be a type, not %.200s",
5936
use_kwargs, cls);
5937
goto error;
5938
}
5939
if (((PyTypeObject *)cls)->tp_new == NULL) {
5940
newobj_unpickling_error(state,
5941
"%s class argument '%.200s' doesn't have __new__",
5942
use_kwargs, cls);
5943
goto error;
5944
}
5945
if (!PyTuple_Check(args)) {
5946
newobj_unpickling_error(state,
5947
"%s args argument must be a tuple, not %.200s",
5948
use_kwargs, args);
5949
goto error;
5950
}
5951
if (use_kwargs && !PyDict_Check(kwargs)) {
5952
newobj_unpickling_error(state,
5953
"%s kwargs argument must be a dict, not %.200s",
5954
use_kwargs, kwargs);
5955
goto error;
5956
}
5957
5958
obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5959
if (obj == NULL) {
5960
goto error;
5961
}
5962
Py_XDECREF(kwargs);
5963
Py_DECREF(args);
5964
Py_DECREF(cls);
5965
PDATA_PUSH(self->stack, obj, -1);
5966
return 0;
5967
5968
error:
5969
Py_XDECREF(kwargs);
5970
Py_DECREF(args);
5971
Py_DECREF(cls);
5972
return -1;
5973
}
5974
5975
static int
5976
load_global(PickleState *state, UnpicklerObject *self)
5977
{
5978
PyObject *global = NULL;
5979
PyObject *module_name;
5980
PyObject *global_name;
5981
Py_ssize_t len;
5982
char *s;
5983
5984
if ((len = _Unpickler_Readline(state, self, &s)) < 0)
5985
return -1;
5986
if (len < 2)
5987
return bad_readline(state);
5988
module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5989
if (!module_name)
5990
return -1;
5991
5992
if ((len = _Unpickler_Readline(state, self, &s)) >= 0) {
5993
if (len < 2) {
5994
Py_DECREF(module_name);
5995
return bad_readline(state);
5996
}
5997
global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5998
if (global_name) {
5999
global = find_class(self, module_name, global_name);
6000
Py_DECREF(global_name);
6001
}
6002
}
6003
Py_DECREF(module_name);
6004
6005
if (global == NULL)
6006
return -1;
6007
PDATA_PUSH(self->stack, global, -1);
6008
return 0;
6009
}
6010
6011
static int
6012
load_stack_global(PickleState *st, UnpicklerObject *self)
6013
{
6014
PyObject *global;
6015
PyObject *module_name;
6016
PyObject *global_name;
6017
6018
PDATA_POP(st, self->stack, global_name);
6019
if (global_name == NULL) {
6020
return -1;
6021
}
6022
PDATA_POP(st, self->stack, module_name);
6023
if (module_name == NULL) {
6024
Py_DECREF(global_name);
6025
return -1;
6026
}
6027
if (!PyUnicode_CheckExact(module_name) ||
6028
!PyUnicode_CheckExact(global_name))
6029
{
6030
PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
6031
Py_DECREF(global_name);
6032
Py_DECREF(module_name);
6033
return -1;
6034
}
6035
global = find_class(self, module_name, global_name);
6036
Py_DECREF(global_name);
6037
Py_DECREF(module_name);
6038
if (global == NULL)
6039
return -1;
6040
PDATA_PUSH(self->stack, global, -1);
6041
return 0;
6042
}
6043
6044
static int
6045
load_persid(PickleState *st, UnpicklerObject *self)
6046
{
6047
PyObject *pid, *obj;
6048
Py_ssize_t len;
6049
char *s;
6050
6051
if (self->pers_func) {
6052
if ((len = _Unpickler_Readline(st, self, &s)) < 0)
6053
return -1;
6054
if (len < 1)
6055
return bad_readline(st);
6056
6057
pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
6058
if (pid == NULL) {
6059
if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
6060
PyErr_SetString(st->UnpicklingError,
6061
"persistent IDs in protocol 0 must be "
6062
"ASCII strings");
6063
}
6064
return -1;
6065
}
6066
6067
obj = call_method(self->pers_func, self->pers_func_self, pid);
6068
Py_DECREF(pid);
6069
if (obj == NULL)
6070
return -1;
6071
6072
PDATA_PUSH(self->stack, obj, -1);
6073
return 0;
6074
}
6075
else {
6076
PyErr_SetString(st->UnpicklingError,
6077
"A load persistent id instruction was encountered, "
6078
"but no persistent_load function was specified.");
6079
return -1;
6080
}
6081
}
6082
6083
static int
6084
load_binpersid(PickleState *st, UnpicklerObject *self)
6085
{
6086
PyObject *pid, *obj;
6087
6088
if (self->pers_func) {
6089
PDATA_POP(st, self->stack, pid);
6090
if (pid == NULL)
6091
return -1;
6092
6093
obj = call_method(self->pers_func, self->pers_func_self, pid);
6094
Py_DECREF(pid);
6095
if (obj == NULL)
6096
return -1;
6097
6098
PDATA_PUSH(self->stack, obj, -1);
6099
return 0;
6100
}
6101
else {
6102
PyErr_SetString(st->UnpicklingError,
6103
"A load persistent id instruction was encountered, "
6104
"but no persistent_load function was specified.");
6105
return -1;
6106
}
6107
}
6108
6109
static int
6110
load_pop(PickleState *state, UnpicklerObject *self)
6111
{
6112
Py_ssize_t len = Py_SIZE(self->stack);
6113
6114
/* Note that we split the (pickle.py) stack into two stacks,
6115
* an object stack and a mark stack. We have to be clever and
6116
* pop the right one. We do this by looking at the top of the
6117
* mark stack first, and only signalling a stack underflow if
6118
* the object stack is empty and the mark stack doesn't match
6119
* our expectations.
6120
*/
6121
if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
6122
self->num_marks--;
6123
self->stack->mark_set = self->num_marks != 0;
6124
self->stack->fence = self->num_marks ?
6125
self->marks[self->num_marks - 1] : 0;
6126
} else if (len <= self->stack->fence)
6127
return Pdata_stack_underflow(state, self->stack);
6128
else {
6129
len--;
6130
Py_DECREF(self->stack->data[len]);
6131
Py_SET_SIZE(self->stack, len);
6132
}
6133
return 0;
6134
}
6135
6136
static int
6137
load_pop_mark(PickleState *state, UnpicklerObject *self)
6138
{
6139
Py_ssize_t i;
6140
if ((i = marker(state, self)) < 0)
6141
return -1;
6142
6143
Pdata_clear(self->stack, i);
6144
6145
return 0;
6146
}
6147
6148
static int
6149
load_dup(PickleState *state, UnpicklerObject *self)
6150
{
6151
PyObject *last;
6152
Py_ssize_t len = Py_SIZE(self->stack);
6153
6154
if (len <= self->stack->fence)
6155
return Pdata_stack_underflow(state, self->stack);
6156
last = self->stack->data[len - 1];
6157
PDATA_APPEND(self->stack, last, -1);
6158
return 0;
6159
}
6160
6161
static int
6162
load_get(PickleState *st, UnpicklerObject *self)
6163
{
6164
PyObject *key, *value;
6165
Py_ssize_t idx;
6166
Py_ssize_t len;
6167
char *s;
6168
6169
if ((len = _Unpickler_Readline(st, self, &s)) < 0)
6170
return -1;
6171
if (len < 2)
6172
return bad_readline(st);
6173
6174
key = PyLong_FromString(s, NULL, 10);
6175
if (key == NULL)
6176
return -1;
6177
idx = PyLong_AsSsize_t(key);
6178
if (idx == -1 && PyErr_Occurred()) {
6179
Py_DECREF(key);
6180
return -1;
6181
}
6182
6183
value = _Unpickler_MemoGet(self, idx);
6184
if (value == NULL) {
6185
if (!PyErr_Occurred()) {
6186
PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
6187
}
6188
Py_DECREF(key);
6189
return -1;
6190
}
6191
Py_DECREF(key);
6192
6193
PDATA_APPEND(self->stack, value, -1);
6194
return 0;
6195
}
6196
6197
static int
6198
load_binget(PickleState *st, UnpicklerObject *self)
6199
{
6200
PyObject *value;
6201
Py_ssize_t idx;
6202
char *s;
6203
6204
if (_Unpickler_Read(self, st, &s, 1) < 0)
6205
return -1;
6206
6207
idx = Py_CHARMASK(s[0]);
6208
6209
value = _Unpickler_MemoGet(self, idx);
6210
if (value == NULL) {
6211
PyObject *key = PyLong_FromSsize_t(idx);
6212
if (key != NULL) {
6213
PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
6214
Py_DECREF(key);
6215
}
6216
return -1;
6217
}
6218
6219
PDATA_APPEND(self->stack, value, -1);
6220
return 0;
6221
}
6222
6223
static int
6224
load_long_binget(PickleState *st, UnpicklerObject *self)
6225
{
6226
PyObject *value;
6227
Py_ssize_t idx;
6228
char *s;
6229
6230
if (_Unpickler_Read(self, st, &s, 4) < 0)
6231
return -1;
6232
6233
idx = calc_binsize(s, 4);
6234
6235
value = _Unpickler_MemoGet(self, idx);
6236
if (value == NULL) {
6237
PyObject *key = PyLong_FromSsize_t(idx);
6238
if (key != NULL) {
6239
PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
6240
Py_DECREF(key);
6241
}
6242
return -1;
6243
}
6244
6245
PDATA_APPEND(self->stack, value, -1);
6246
return 0;
6247
}
6248
6249
/* Push an object from the extension registry (EXT[124]). nbytes is
6250
* the number of bytes following the opcode, holding the index (code) value.
6251
*/
6252
static int
6253
load_extension(PickleState *st, UnpicklerObject *self, int nbytes)
6254
{
6255
char *codebytes; /* the nbytes bytes after the opcode */
6256
long code; /* calc_binint returns long */
6257
PyObject *py_code; /* code as a Python int */
6258
PyObject *obj; /* the object to push */
6259
PyObject *pair; /* (module_name, class_name) */
6260
PyObject *module_name, *class_name;
6261
6262
assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
6263
if (_Unpickler_Read(self, st, &codebytes, nbytes) < 0)
6264
return -1;
6265
code = calc_binint(codebytes, nbytes);
6266
if (code <= 0) { /* note that 0 is forbidden */
6267
/* Corrupt or hostile pickle. */
6268
PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
6269
return -1;
6270
}
6271
6272
/* Look for the code in the cache. */
6273
py_code = PyLong_FromLong(code);
6274
if (py_code == NULL)
6275
return -1;
6276
obj = PyDict_GetItemWithError(st->extension_cache, py_code);
6277
if (obj != NULL) {
6278
/* Bingo. */
6279
Py_DECREF(py_code);
6280
PDATA_APPEND(self->stack, obj, -1);
6281
return 0;
6282
}
6283
if (PyErr_Occurred()) {
6284
Py_DECREF(py_code);
6285
return -1;
6286
}
6287
6288
/* Look up the (module_name, class_name) pair. */
6289
pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
6290
if (pair == NULL) {
6291
Py_DECREF(py_code);
6292
if (!PyErr_Occurred()) {
6293
PyErr_Format(PyExc_ValueError, "unregistered extension "
6294
"code %ld", code);
6295
}
6296
return -1;
6297
}
6298
/* Since the extension registry is manipulable via Python code,
6299
* confirm that pair is really a 2-tuple of strings.
6300
*/
6301
if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
6302
goto error;
6303
}
6304
6305
module_name = PyTuple_GET_ITEM(pair, 0);
6306
if (!PyUnicode_Check(module_name)) {
6307
goto error;
6308
}
6309
6310
class_name = PyTuple_GET_ITEM(pair, 1);
6311
if (!PyUnicode_Check(class_name)) {
6312
goto error;
6313
}
6314
6315
/* Load the object. */
6316
obj = find_class(self, module_name, class_name);
6317
if (obj == NULL) {
6318
Py_DECREF(py_code);
6319
return -1;
6320
}
6321
/* Cache code -> obj. */
6322
code = PyDict_SetItem(st->extension_cache, py_code, obj);
6323
Py_DECREF(py_code);
6324
if (code < 0) {
6325
Py_DECREF(obj);
6326
return -1;
6327
}
6328
PDATA_PUSH(self->stack, obj, -1);
6329
return 0;
6330
6331
error:
6332
Py_DECREF(py_code);
6333
PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
6334
"isn't a 2-tuple of strings", code);
6335
return -1;
6336
}
6337
6338
static int
6339
load_put(PickleState *state, UnpicklerObject *self)
6340
{
6341
PyObject *key, *value;
6342
Py_ssize_t idx;
6343
Py_ssize_t len;
6344
char *s = NULL;
6345
6346
if ((len = _Unpickler_Readline(state, self, &s)) < 0)
6347
return -1;
6348
if (len < 2)
6349
return bad_readline(state);
6350
if (Py_SIZE(self->stack) <= self->stack->fence)
6351
return Pdata_stack_underflow(state, self->stack);
6352
value = self->stack->data[Py_SIZE(self->stack) - 1];
6353
6354
key = PyLong_FromString(s, NULL, 10);
6355
if (key == NULL)
6356
return -1;
6357
idx = PyLong_AsSsize_t(key);
6358
Py_DECREF(key);
6359
if (idx < 0) {
6360
if (!PyErr_Occurred())
6361
PyErr_SetString(PyExc_ValueError,
6362
"negative PUT argument");
6363
return -1;
6364
}
6365
6366
return _Unpickler_MemoPut(self, idx, value);
6367
}
6368
6369
static int
6370
load_binput(PickleState *state, UnpicklerObject *self)
6371
{
6372
PyObject *value;
6373
Py_ssize_t idx;
6374
char *s;
6375
6376
if (_Unpickler_Read(self, state, &s, 1) < 0)
6377
return -1;
6378
6379
if (Py_SIZE(self->stack) <= self->stack->fence)
6380
return Pdata_stack_underflow(state, self->stack);
6381
value = self->stack->data[Py_SIZE(self->stack) - 1];
6382
6383
idx = Py_CHARMASK(s[0]);
6384
6385
return _Unpickler_MemoPut(self, idx, value);
6386
}
6387
6388
static int
6389
load_long_binput(PickleState *state, UnpicklerObject *self)
6390
{
6391
PyObject *value;
6392
Py_ssize_t idx;
6393
char *s;
6394
6395
if (_Unpickler_Read(self, state, &s, 4) < 0)
6396
return -1;
6397
6398
if (Py_SIZE(self->stack) <= self->stack->fence)
6399
return Pdata_stack_underflow(state, self->stack);
6400
value = self->stack->data[Py_SIZE(self->stack) - 1];
6401
6402
idx = calc_binsize(s, 4);
6403
if (idx < 0) {
6404
PyErr_SetString(PyExc_ValueError,
6405
"negative LONG_BINPUT argument");
6406
return -1;
6407
}
6408
6409
return _Unpickler_MemoPut(self, idx, value);
6410
}
6411
6412
static int
6413
load_memoize(PickleState *state, UnpicklerObject *self)
6414
{
6415
PyObject *value;
6416
6417
if (Py_SIZE(self->stack) <= self->stack->fence)
6418
return Pdata_stack_underflow(state, self->stack);
6419
value = self->stack->data[Py_SIZE(self->stack) - 1];
6420
6421
return _Unpickler_MemoPut(self, self->memo_len, value);
6422
}
6423
6424
static int
6425
do_append(PickleState *state, UnpicklerObject *self, Py_ssize_t x)
6426
{
6427
PyObject *value;
6428
PyObject *slice;
6429
PyObject *list;
6430
PyObject *result;
6431
Py_ssize_t len, i;
6432
6433
len = Py_SIZE(self->stack);
6434
if (x > len || x <= self->stack->fence)
6435
return Pdata_stack_underflow(state, self->stack);
6436
if (len == x) /* nothing to do */
6437
return 0;
6438
6439
list = self->stack->data[x - 1];
6440
6441
if (PyList_CheckExact(list)) {
6442
Py_ssize_t list_len;
6443
int ret;
6444
6445
slice = Pdata_poplist(self->stack, x);
6446
if (!slice)
6447
return -1;
6448
list_len = PyList_GET_SIZE(list);
6449
ret = PyList_SetSlice(list, list_len, list_len, slice);
6450
Py_DECREF(slice);
6451
return ret;
6452
}
6453
else {
6454
PyObject *extend_func;
6455
6456
if (_PyObject_LookupAttr(list, &_Py_ID(extend), &extend_func) < 0) {
6457
return -1;
6458
}
6459
if (extend_func != NULL) {
6460
slice = Pdata_poplist(self->stack, x);
6461
if (!slice) {
6462
Py_DECREF(extend_func);
6463
return -1;
6464
}
6465
result = _Pickle_FastCall(extend_func, slice);
6466
Py_DECREF(extend_func);
6467
if (result == NULL)
6468
return -1;
6469
Py_DECREF(result);
6470
}
6471
else {
6472
PyObject *append_func;
6473
6474
/* Even if the PEP 307 requires extend() and append() methods,
6475
fall back on append() if the object has no extend() method
6476
for backward compatibility. */
6477
append_func = PyObject_GetAttr(list, &_Py_ID(append));
6478
if (append_func == NULL)
6479
return -1;
6480
for (i = x; i < len; i++) {
6481
value = self->stack->data[i];
6482
result = _Pickle_FastCall(append_func, value);
6483
if (result == NULL) {
6484
Pdata_clear(self->stack, i + 1);
6485
Py_SET_SIZE(self->stack, x);
6486
Py_DECREF(append_func);
6487
return -1;
6488
}
6489
Py_DECREF(result);
6490
}
6491
Py_SET_SIZE(self->stack, x);
6492
Py_DECREF(append_func);
6493
}
6494
}
6495
6496
return 0;
6497
}
6498
6499
static int
6500
load_append(PickleState *state, UnpicklerObject *self)
6501
{
6502
if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
6503
return Pdata_stack_underflow(state, self->stack);
6504
return do_append(state, self, Py_SIZE(self->stack) - 1);
6505
}
6506
6507
static int
6508
load_appends(PickleState *state, UnpicklerObject *self)
6509
{
6510
Py_ssize_t i = marker(state, self);
6511
if (i < 0)
6512
return -1;
6513
return do_append(state, self, i);
6514
}
6515
6516
static int
6517
do_setitems(PickleState *st, UnpicklerObject *self, Py_ssize_t x)
6518
{
6519
PyObject *value, *key;
6520
PyObject *dict;
6521
Py_ssize_t len, i;
6522
int status = 0;
6523
6524
len = Py_SIZE(self->stack);
6525
if (x > len || x <= self->stack->fence)
6526
return Pdata_stack_underflow(st, self->stack);
6527
if (len == x) /* nothing to do */
6528
return 0;
6529
if ((len - x) % 2 != 0) {
6530
/* Corrupt or hostile pickle -- we never write one like this. */
6531
PyErr_SetString(st->UnpicklingError,
6532
"odd number of items for SETITEMS");
6533
return -1;
6534
}
6535
6536
/* Here, dict does not actually need to be a PyDict; it could be anything
6537
that supports the __setitem__ attribute. */
6538
dict = self->stack->data[x - 1];
6539
6540
for (i = x + 1; i < len; i += 2) {
6541
key = self->stack->data[i - 1];
6542
value = self->stack->data[i];
6543
if (PyObject_SetItem(dict, key, value) < 0) {
6544
status = -1;
6545
break;
6546
}
6547
}
6548
6549
Pdata_clear(self->stack, x);
6550
return status;
6551
}
6552
6553
static int
6554
load_setitem(PickleState *state, UnpicklerObject *self)
6555
{
6556
return do_setitems(state, self, Py_SIZE(self->stack) - 2);
6557
}
6558
6559
static int
6560
load_setitems(PickleState *state, UnpicklerObject *self)
6561
{
6562
Py_ssize_t i = marker(state, self);
6563
if (i < 0)
6564
return -1;
6565
return do_setitems(state, self, i);
6566
}
6567
6568
static int
6569
load_additems(PickleState *state, UnpicklerObject *self)
6570
{
6571
PyObject *set;
6572
Py_ssize_t mark, len, i;
6573
6574
mark = marker(state, self);
6575
if (mark < 0)
6576
return -1;
6577
len = Py_SIZE(self->stack);
6578
if (mark > len || mark <= self->stack->fence)
6579
return Pdata_stack_underflow(state, self->stack);
6580
if (len == mark) /* nothing to do */
6581
return 0;
6582
6583
set = self->stack->data[mark - 1];
6584
6585
if (PySet_Check(set)) {
6586
PyObject *items;
6587
int status;
6588
6589
items = Pdata_poptuple(state, self->stack, mark);
6590
if (items == NULL)
6591
return -1;
6592
6593
status = _PySet_Update(set, items);
6594
Py_DECREF(items);
6595
return status;
6596
}
6597
else {
6598
PyObject *add_func;
6599
6600
add_func = PyObject_GetAttr(set, &_Py_ID(add));
6601
if (add_func == NULL)
6602
return -1;
6603
for (i = mark; i < len; i++) {
6604
PyObject *result;
6605
PyObject *item;
6606
6607
item = self->stack->data[i];
6608
result = _Pickle_FastCall(add_func, item);
6609
if (result == NULL) {
6610
Pdata_clear(self->stack, i + 1);
6611
Py_SET_SIZE(self->stack, mark);
6612
return -1;
6613
}
6614
Py_DECREF(result);
6615
}
6616
Py_SET_SIZE(self->stack, mark);
6617
}
6618
6619
return 0;
6620
}
6621
6622
static int
6623
load_build(PickleState *st, UnpicklerObject *self)
6624
{
6625
PyObject *inst, *slotstate;
6626
PyObject *setstate;
6627
int status = 0;
6628
6629
/* Stack is ... instance, state. We want to leave instance at
6630
* the stack top, possibly mutated via instance.__setstate__(state).
6631
*/
6632
if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6633
return Pdata_stack_underflow(st, self->stack);
6634
6635
PyObject *state;
6636
PDATA_POP(st, self->stack, state);
6637
if (state == NULL)
6638
return -1;
6639
6640
inst = self->stack->data[Py_SIZE(self->stack) - 1];
6641
6642
if (_PyObject_LookupAttr(inst, &_Py_ID(__setstate__), &setstate) < 0) {
6643
Py_DECREF(state);
6644
return -1;
6645
}
6646
if (setstate != NULL) {
6647
PyObject *result;
6648
6649
/* The explicit __setstate__ is responsible for everything. */
6650
result = _Pickle_FastCall(setstate, state);
6651
Py_DECREF(setstate);
6652
if (result == NULL)
6653
return -1;
6654
Py_DECREF(result);
6655
return 0;
6656
}
6657
6658
/* A default __setstate__. First see whether state embeds a
6659
* slot state dict too (a proto 2 addition).
6660
*/
6661
if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) {
6662
PyObject *tmp = state;
6663
6664
state = PyTuple_GET_ITEM(tmp, 0);
6665
slotstate = PyTuple_GET_ITEM(tmp, 1);
6666
Py_INCREF(state);
6667
Py_INCREF(slotstate);
6668
Py_DECREF(tmp);
6669
}
6670
else
6671
slotstate = NULL;
6672
6673
/* Set inst.__dict__ from the state dict (if any). */
6674
if (state != Py_None) {
6675
PyObject *dict;
6676
PyObject *d_key, *d_value;
6677
Py_ssize_t i;
6678
6679
if (!PyDict_Check(state)) {
6680
PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
6681
goto error;
6682
}
6683
dict = PyObject_GetAttr(inst, &_Py_ID(__dict__));
6684
if (dict == NULL)
6685
goto error;
6686
6687
i = 0;
6688
while (PyDict_Next(state, &i, &d_key, &d_value)) {
6689
/* normally the keys for instance attributes are
6690
interned. we should try to do that here. */
6691
Py_INCREF(d_key);
6692
if (PyUnicode_CheckExact(d_key))
6693
PyUnicode_InternInPlace(&d_key);
6694
if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6695
Py_DECREF(d_key);
6696
goto error;
6697
}
6698
Py_DECREF(d_key);
6699
}
6700
Py_DECREF(dict);
6701
}
6702
6703
/* Also set instance attributes from the slotstate dict (if any). */
6704
if (slotstate != NULL) {
6705
PyObject *d_key, *d_value;
6706
Py_ssize_t i;
6707
6708
if (!PyDict_Check(slotstate)) {
6709
PyErr_SetString(st->UnpicklingError,
6710
"slot state is not a dictionary");
6711
goto error;
6712
}
6713
i = 0;
6714
while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6715
if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6716
goto error;
6717
}
6718
}
6719
6720
if (0) {
6721
error:
6722
status = -1;
6723
}
6724
6725
Py_DECREF(state);
6726
Py_XDECREF(slotstate);
6727
return status;
6728
}
6729
6730
static int
6731
load_mark(PickleState *state, UnpicklerObject *self)
6732
{
6733
6734
/* Note that we split the (pickle.py) stack into two stacks, an
6735
* object stack and a mark stack. Here we push a mark onto the
6736
* mark stack.
6737
*/
6738
6739
if (self->num_marks >= self->marks_size) {
6740
size_t alloc = ((size_t)self->num_marks << 1) + 20;
6741
Py_ssize_t *marks_new = self->marks;
6742
PyMem_RESIZE(marks_new, Py_ssize_t, alloc);
6743
if (marks_new == NULL) {
6744
PyErr_NoMemory();
6745
return -1;
6746
}
6747
self->marks = marks_new;
6748
self->marks_size = (Py_ssize_t)alloc;
6749
}
6750
6751
self->stack->mark_set = 1;
6752
self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
6753
6754
return 0;
6755
}
6756
6757
static int
6758
load_reduce(PickleState *state, UnpicklerObject *self)
6759
{
6760
PyObject *callable = NULL;
6761
PyObject *argtup = NULL;
6762
PyObject *obj = NULL;
6763
6764
PDATA_POP(state, self->stack, argtup);
6765
if (argtup == NULL)
6766
return -1;
6767
PDATA_POP(state, self->stack, callable);
6768
if (callable) {
6769
obj = PyObject_CallObject(callable, argtup);
6770
Py_DECREF(callable);
6771
}
6772
Py_DECREF(argtup);
6773
6774
if (obj == NULL)
6775
return -1;
6776
6777
PDATA_PUSH(self->stack, obj, -1);
6778
return 0;
6779
}
6780
6781
/* Just raises an error if we don't know the protocol specified. PROTO
6782
* is the first opcode for protocols >= 2.
6783
*/
6784
static int
6785
load_proto(PickleState *state, UnpicklerObject *self)
6786
{
6787
char *s;
6788
int i;
6789
6790
if (_Unpickler_Read(self, state, &s, 1) < 0)
6791
return -1;
6792
6793
i = (unsigned char)s[0];
6794
if (i <= HIGHEST_PROTOCOL) {
6795
self->proto = i;
6796
return 0;
6797
}
6798
6799
PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6800
return -1;
6801
}
6802
6803
static int
6804
load_frame(PickleState *state, UnpicklerObject *self)
6805
{
6806
char *s;
6807
Py_ssize_t frame_len;
6808
6809
if (_Unpickler_Read(self, state, &s, 8) < 0)
6810
return -1;
6811
6812
frame_len = calc_binsize(s, 8);
6813
if (frame_len < 0) {
6814
PyErr_Format(PyExc_OverflowError,
6815
"FRAME length exceeds system's maximum of %zd bytes",
6816
PY_SSIZE_T_MAX);
6817
return -1;
6818
}
6819
6820
if (_Unpickler_Read(self, state, &s, frame_len) < 0)
6821
return -1;
6822
6823
/* Rewind to start of frame */
6824
self->next_read_idx -= frame_len;
6825
return 0;
6826
}
6827
6828
static PyObject *
6829
load(PickleState *st, UnpicklerObject *self)
6830
{
6831
PyObject *value = NULL;
6832
char *s = NULL;
6833
6834
self->num_marks = 0;
6835
self->stack->mark_set = 0;
6836
self->stack->fence = 0;
6837
self->proto = 0;
6838
if (Py_SIZE(self->stack))
6839
Pdata_clear(self->stack, 0);
6840
6841
/* Convenient macros for the dispatch while-switch loop just below. */
6842
#define OP(opcode, load_func) \
6843
case opcode: if (load_func(st, self) < 0) break; continue;
6844
6845
#define OP_ARG(opcode, load_func, arg) \
6846
case opcode: if (load_func(st, self, (arg)) < 0) break; continue;
6847
6848
while (1) {
6849
if (_Unpickler_Read(self, st, &s, 1) < 0) {
6850
if (PyErr_ExceptionMatches(st->UnpicklingError)) {
6851
PyErr_Format(PyExc_EOFError, "Ran out of input");
6852
}
6853
return NULL;
6854
}
6855
6856
switch ((enum opcode)s[0]) {
6857
OP(NONE, load_none)
6858
OP(BININT, load_binint)
6859
OP(BININT1, load_binint1)
6860
OP(BININT2, load_binint2)
6861
OP(INT, load_int)
6862
OP(LONG, load_long)
6863
OP_ARG(LONG1, load_counted_long, 1)
6864
OP_ARG(LONG4, load_counted_long, 4)
6865
OP(FLOAT, load_float)
6866
OP(BINFLOAT, load_binfloat)
6867
OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6868
OP_ARG(BINBYTES, load_counted_binbytes, 4)
6869
OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6870
OP(BYTEARRAY8, load_counted_bytearray)
6871
OP(NEXT_BUFFER, load_next_buffer)
6872
OP(READONLY_BUFFER, load_readonly_buffer)
6873
OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6874
OP_ARG(BINSTRING, load_counted_binstring, 4)
6875
OP(STRING, load_string)
6876
OP(UNICODE, load_unicode)
6877
OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6878
OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6879
OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
6880
OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6881
OP_ARG(TUPLE1, load_counted_tuple, 1)
6882
OP_ARG(TUPLE2, load_counted_tuple, 2)
6883
OP_ARG(TUPLE3, load_counted_tuple, 3)
6884
OP(TUPLE, load_tuple)
6885
OP(EMPTY_LIST, load_empty_list)
6886
OP(LIST, load_list)
6887
OP(EMPTY_DICT, load_empty_dict)
6888
OP(DICT, load_dict)
6889
OP(EMPTY_SET, load_empty_set)
6890
OP(ADDITEMS, load_additems)
6891
OP(FROZENSET, load_frozenset)
6892
OP(OBJ, load_obj)
6893
OP(INST, load_inst)
6894
OP_ARG(NEWOBJ, load_newobj, 0)
6895
OP_ARG(NEWOBJ_EX, load_newobj, 1)
6896
OP(GLOBAL, load_global)
6897
OP(STACK_GLOBAL, load_stack_global)
6898
OP(APPEND, load_append)
6899
OP(APPENDS, load_appends)
6900
OP(BUILD, load_build)
6901
OP(DUP, load_dup)
6902
OP(BINGET, load_binget)
6903
OP(LONG_BINGET, load_long_binget)
6904
OP(GET, load_get)
6905
OP(MARK, load_mark)
6906
OP(BINPUT, load_binput)
6907
OP(LONG_BINPUT, load_long_binput)
6908
OP(PUT, load_put)
6909
OP(MEMOIZE, load_memoize)
6910
OP(POP, load_pop)
6911
OP(POP_MARK, load_pop_mark)
6912
OP(SETITEM, load_setitem)
6913
OP(SETITEMS, load_setitems)
6914
OP(PERSID, load_persid)
6915
OP(BINPERSID, load_binpersid)
6916
OP(REDUCE, load_reduce)
6917
OP(PROTO, load_proto)
6918
OP(FRAME, load_frame)
6919
OP_ARG(EXT1, load_extension, 1)
6920
OP_ARG(EXT2, load_extension, 2)
6921
OP_ARG(EXT4, load_extension, 4)
6922
OP_ARG(NEWTRUE, load_bool, Py_True)
6923
OP_ARG(NEWFALSE, load_bool, Py_False)
6924
6925
case STOP:
6926
break;
6927
6928
default:
6929
{
6930
unsigned char c = (unsigned char) *s;
6931
if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') {
6932
PyErr_Format(st->UnpicklingError,
6933
"invalid load key, '%c'.", c);
6934
}
6935
else {
6936
PyErr_Format(st->UnpicklingError,
6937
"invalid load key, '\\x%02x'.", c);
6938
}
6939
return NULL;
6940
}
6941
}
6942
6943
break; /* and we are done! */
6944
}
6945
6946
if (PyErr_Occurred()) {
6947
return NULL;
6948
}
6949
6950
if (_Unpickler_SkipConsumed(self) < 0)
6951
return NULL;
6952
6953
PDATA_POP(st, self->stack, value);
6954
return value;
6955
}
6956
6957
/*[clinic input]
6958
6959
_pickle.Unpickler.load
6960
6961
cls: defining_class
6962
6963
Load a pickle.
6964
6965
Read a pickled object representation from the open file object given
6966
in the constructor, and return the reconstituted object hierarchy
6967
specified therein.
6968
[clinic start generated code]*/
6969
6970
static PyObject *
6971
_pickle_Unpickler_load_impl(UnpicklerObject *self, PyTypeObject *cls)
6972
/*[clinic end generated code: output=cc88168f608e3007 input=f5d2f87e61d5f07f]*/
6973
{
6974
UnpicklerObject *unpickler = (UnpicklerObject*)self;
6975
6976
PickleState *st = _Pickle_GetStateByClass(cls);
6977
6978
/* Check whether the Unpickler was initialized correctly. This prevents
6979
segfaulting if a subclass overridden __init__ with a function that does
6980
not call Unpickler.__init__(). Here, we simply ensure that self->read
6981
is not NULL. */
6982
if (unpickler->read == NULL) {
6983
PyErr_Format(st->UnpicklingError,
6984
"Unpickler.__init__() was not called by %s.__init__()",
6985
Py_TYPE(unpickler)->tp_name);
6986
return NULL;
6987
}
6988
6989
return load(st, unpickler);
6990
}
6991
6992
/* The name of find_class() is misleading. In newer pickle protocols, this
6993
function is used for loading any global (i.e., functions), not just
6994
classes. The name is kept only for backward compatibility. */
6995
6996
/*[clinic input]
6997
6998
_pickle.Unpickler.find_class
6999
7000
cls: defining_class
7001
module_name: object
7002
global_name: object
7003
/
7004
7005
Return an object from a specified module.
7006
7007
If necessary, the module will be imported. Subclasses may override
7008
this method (e.g. to restrict unpickling of arbitrary classes and
7009
functions).
7010
7011
This method is called whenever a class or a function object is
7012
needed. Both arguments passed are str objects.
7013
[clinic start generated code]*/
7014
7015
static PyObject *
7016
_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyTypeObject *cls,
7017
PyObject *module_name,
7018
PyObject *global_name)
7019
/*[clinic end generated code: output=99577948abb0be81 input=9577745719219fc7]*/
7020
{
7021
PyObject *global;
7022
PyObject *module;
7023
7024
if (PySys_Audit("pickle.find_class", "OO",
7025
module_name, global_name) < 0) {
7026
return NULL;
7027
}
7028
7029
/* Try to map the old names used in Python 2.x to the new ones used in
7030
Python 3.x. We do this only with old pickle protocols and when the
7031
user has not disabled the feature. */
7032
if (self->proto < 3 && self->fix_imports) {
7033
PyObject *key;
7034
PyObject *item;
7035
PickleState *st = _Pickle_GetStateByClass(cls);
7036
7037
/* Check if the global (i.e., a function or a class) was renamed
7038
or moved to another module. */
7039
key = PyTuple_Pack(2, module_name, global_name);
7040
if (key == NULL)
7041
return NULL;
7042
item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
7043
Py_DECREF(key);
7044
if (item) {
7045
if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
7046
PyErr_Format(PyExc_RuntimeError,
7047
"_compat_pickle.NAME_MAPPING values should be "
7048
"2-tuples, not %.200s", Py_TYPE(item)->tp_name);
7049
return NULL;
7050
}
7051
module_name = PyTuple_GET_ITEM(item, 0);
7052
global_name = PyTuple_GET_ITEM(item, 1);
7053
if (!PyUnicode_Check(module_name) ||
7054
!PyUnicode_Check(global_name)) {
7055
PyErr_Format(PyExc_RuntimeError,
7056
"_compat_pickle.NAME_MAPPING values should be "
7057
"pairs of str, not (%.200s, %.200s)",
7058
Py_TYPE(module_name)->tp_name,
7059
Py_TYPE(global_name)->tp_name);
7060
return NULL;
7061
}
7062
}
7063
else if (PyErr_Occurred()) {
7064
return NULL;
7065
}
7066
else {
7067
/* Check if the module was renamed. */
7068
item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
7069
if (item) {
7070
if (!PyUnicode_Check(item)) {
7071
PyErr_Format(PyExc_RuntimeError,
7072
"_compat_pickle.IMPORT_MAPPING values should be "
7073
"strings, not %.200s", Py_TYPE(item)->tp_name);
7074
return NULL;
7075
}
7076
module_name = item;
7077
}
7078
else if (PyErr_Occurred()) {
7079
return NULL;
7080
}
7081
}
7082
}
7083
7084
/*
7085
* we don't use PyImport_GetModule here, because it can return partially-
7086
* initialised modules, which then cause the getattribute to fail.
7087
*/
7088
module = PyImport_Import(module_name);
7089
if (module == NULL) {
7090
return NULL;
7091
}
7092
global = getattribute(module, global_name, self->proto >= 4);
7093
Py_DECREF(module);
7094
return global;
7095
}
7096
7097
/*[clinic input]
7098
7099
_pickle.Unpickler.__sizeof__ -> size_t
7100
7101
Returns size in memory, in bytes.
7102
[clinic start generated code]*/
7103
7104
static size_t
7105
_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
7106
/*[clinic end generated code: output=4648d84c228196df input=27180b2b6b524012]*/
7107
{
7108
size_t res = _PyObject_SIZE(Py_TYPE(self));
7109
if (self->memo != NULL)
7110
res += self->memo_size * sizeof(PyObject *);
7111
if (self->marks != NULL)
7112
res += (size_t)self->marks_size * sizeof(Py_ssize_t);
7113
if (self->input_line != NULL)
7114
res += strlen(self->input_line) + 1;
7115
if (self->encoding != NULL)
7116
res += strlen(self->encoding) + 1;
7117
if (self->errors != NULL)
7118
res += strlen(self->errors) + 1;
7119
return res;
7120
}
7121
7122
static struct PyMethodDef Unpickler_methods[] = {
7123
_PICKLE_UNPICKLER_LOAD_METHODDEF
7124
_PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
7125
_PICKLE_UNPICKLER___SIZEOF___METHODDEF
7126
{NULL, NULL} /* sentinel */
7127
};
7128
7129
static int
7130
Unpickler_clear(UnpicklerObject *self)
7131
{
7132
Py_CLEAR(self->readline);
7133
Py_CLEAR(self->readinto);
7134
Py_CLEAR(self->read);
7135
Py_CLEAR(self->peek);
7136
Py_CLEAR(self->stack);
7137
Py_CLEAR(self->pers_func);
7138
Py_CLEAR(self->buffers);
7139
if (self->buffer.buf != NULL) {
7140
PyBuffer_Release(&self->buffer);
7141
self->buffer.buf = NULL;
7142
}
7143
7144
_Unpickler_MemoCleanup(self);
7145
PyMem_Free(self->marks);
7146
self->marks = NULL;
7147
PyMem_Free(self->input_line);
7148
self->input_line = NULL;
7149
PyMem_Free(self->encoding);
7150
self->encoding = NULL;
7151
PyMem_Free(self->errors);
7152
self->errors = NULL;
7153
7154
return 0;
7155
}
7156
7157
static void
7158
Unpickler_dealloc(UnpicklerObject *self)
7159
{
7160
PyTypeObject *tp = Py_TYPE(self);
7161
PyObject_GC_UnTrack((PyObject *)self);
7162
(void)Unpickler_clear(self);
7163
tp->tp_free((PyObject *)self);
7164
Py_DECREF(tp);
7165
}
7166
7167
static int
7168
Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
7169
{
7170
Py_VISIT(Py_TYPE(self));
7171
Py_VISIT(self->readline);
7172
Py_VISIT(self->readinto);
7173
Py_VISIT(self->read);
7174
Py_VISIT(self->peek);
7175
Py_VISIT(self->stack);
7176
Py_VISIT(self->pers_func);
7177
Py_VISIT(self->buffers);
7178
return 0;
7179
}
7180
7181
/*[clinic input]
7182
7183
_pickle.Unpickler.__init__
7184
7185
file: object
7186
*
7187
fix_imports: bool = True
7188
encoding: str = 'ASCII'
7189
errors: str = 'strict'
7190
buffers: object(c_default="NULL") = ()
7191
7192
This takes a binary file for reading a pickle data stream.
7193
7194
The protocol version of the pickle is detected automatically, so no
7195
protocol argument is needed. Bytes past the pickled object's
7196
representation are ignored.
7197
7198
The argument *file* must have two methods, a read() method that takes
7199
an integer argument, and a readline() method that requires no
7200
arguments. Both methods should return bytes. Thus *file* can be a
7201
binary file object opened for reading, an io.BytesIO object, or any
7202
other custom object that meets this interface.
7203
7204
Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7205
which are used to control compatibility support for pickle stream
7206
generated by Python 2. If *fix_imports* is True, pickle will try to
7207
map the old Python 2 names to the new names used in Python 3. The
7208
*encoding* and *errors* tell pickle how to decode 8-bit string
7209
instances pickled by Python 2; these default to 'ASCII' and 'strict',
7210
respectively. The *encoding* can be 'bytes' to read these 8-bit
7211
string instances as bytes objects.
7212
[clinic start generated code]*/
7213
7214
static int
7215
_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
7216
int fix_imports, const char *encoding,
7217
const char *errors, PyObject *buffers)
7218
/*[clinic end generated code: output=09f0192649ea3f85 input=ca4c1faea9553121]*/
7219
{
7220
/* In case of multiple __init__() calls, clear previous content. */
7221
if (self->read != NULL)
7222
(void)Unpickler_clear(self);
7223
7224
if (_Unpickler_SetInputStream(self, file) < 0)
7225
return -1;
7226
7227
if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
7228
return -1;
7229
7230
if (_Unpickler_SetBuffers(self, buffers) < 0)
7231
return -1;
7232
7233
self->fix_imports = fix_imports;
7234
7235
if (init_method_ref((PyObject *)self, &_Py_ID(persistent_load),
7236
&self->pers_func, &self->pers_func_self) < 0)
7237
{
7238
return -1;
7239
}
7240
7241
PyTypeObject *tp = Py_TYPE(self);
7242
PickleState *state = _Pickle_FindStateByType(tp);
7243
self->stack = (Pdata *)Pdata_New(state);
7244
if (self->stack == NULL)
7245
return -1;
7246
7247
self->memo_size = 32;
7248
self->memo = _Unpickler_NewMemo(self->memo_size);
7249
if (self->memo == NULL)
7250
return -1;
7251
7252
self->proto = 0;
7253
7254
return 0;
7255
}
7256
7257
7258
/* Define a proxy object for the Unpickler's internal memo object. This is to
7259
* avoid breaking code like:
7260
* unpickler.memo.clear()
7261
* and
7262
* unpickler.memo = saved_memo
7263
* Is this a good idea? Not really, but we don't want to break code that uses
7264
* it. Note that we don't implement the entire mapping API here. This is
7265
* intentional, as these should be treated as black-box implementation details.
7266
*
7267
* We do, however, have to implement pickling/unpickling support because of
7268
* real-world code like cvs2svn.
7269
*/
7270
7271
/*[clinic input]
7272
_pickle.UnpicklerMemoProxy.clear
7273
7274
Remove all items from memo.
7275
[clinic start generated code]*/
7276
7277
static PyObject *
7278
_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
7279
/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
7280
{
7281
_Unpickler_MemoCleanup(self->unpickler);
7282
self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
7283
if (self->unpickler->memo == NULL)
7284
return NULL;
7285
Py_RETURN_NONE;
7286
}
7287
7288
/*[clinic input]
7289
_pickle.UnpicklerMemoProxy.copy
7290
7291
Copy the memo to a new object.
7292
[clinic start generated code]*/
7293
7294
static PyObject *
7295
_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
7296
/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
7297
{
7298
size_t i;
7299
PyObject *new_memo = PyDict_New();
7300
if (new_memo == NULL)
7301
return NULL;
7302
7303
for (i = 0; i < self->unpickler->memo_size; i++) {
7304
int status;
7305
PyObject *key, *value;
7306
7307
value = self->unpickler->memo[i];
7308
if (value == NULL)
7309
continue;
7310
7311
key = PyLong_FromSsize_t(i);
7312
if (key == NULL)
7313
goto error;
7314
status = PyDict_SetItem(new_memo, key, value);
7315
Py_DECREF(key);
7316
if (status < 0)
7317
goto error;
7318
}
7319
return new_memo;
7320
7321
error:
7322
Py_DECREF(new_memo);
7323
return NULL;
7324
}
7325
7326
/*[clinic input]
7327
_pickle.UnpicklerMemoProxy.__reduce__
7328
7329
Implement pickling support.
7330
[clinic start generated code]*/
7331
7332
static PyObject *
7333
_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
7334
/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
7335
{
7336
PyObject *reduce_value;
7337
PyObject *constructor_args;
7338
PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
7339
if (contents == NULL)
7340
return NULL;
7341
7342
reduce_value = PyTuple_New(2);
7343
if (reduce_value == NULL) {
7344
Py_DECREF(contents);
7345
return NULL;
7346
}
7347
constructor_args = PyTuple_New(1);
7348
if (constructor_args == NULL) {
7349
Py_DECREF(contents);
7350
Py_DECREF(reduce_value);
7351
return NULL;
7352
}
7353
PyTuple_SET_ITEM(constructor_args, 0, contents);
7354
PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
7355
PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
7356
return reduce_value;
7357
}
7358
7359
static PyMethodDef unpicklerproxy_methods[] = {
7360
_PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
7361
_PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
7362
_PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
7363
{NULL, NULL} /* sentinel */
7364
};
7365
7366
static void
7367
UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
7368
{
7369
PyTypeObject *tp = Py_TYPE(self);
7370
PyObject_GC_UnTrack(self);
7371
Py_CLEAR(self->unpickler);
7372
tp->tp_free((PyObject *)self);
7373
Py_DECREF(tp);
7374
}
7375
7376
static int
7377
UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
7378
visitproc visit, void *arg)
7379
{
7380
Py_VISIT(Py_TYPE(self));
7381
Py_VISIT(self->unpickler);
7382
return 0;
7383
}
7384
7385
static int
7386
UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
7387
{
7388
Py_CLEAR(self->unpickler);
7389
return 0;
7390
}
7391
7392
static PyType_Slot unpickler_memoproxy_slots[] = {
7393
{Py_tp_dealloc, UnpicklerMemoProxy_dealloc},
7394
{Py_tp_traverse, UnpicklerMemoProxy_traverse},
7395
{Py_tp_clear, UnpicklerMemoProxy_clear},
7396
{Py_tp_methods, unpicklerproxy_methods},
7397
{Py_tp_hash, PyObject_HashNotImplemented},
7398
{0, NULL},
7399
};
7400
7401
static PyType_Spec unpickler_memoproxy_spec = {
7402
.name = "_pickle.UnpicklerMemoProxy",
7403
.basicsize = sizeof(UnpicklerMemoProxyObject),
7404
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
7405
Py_TPFLAGS_IMMUTABLETYPE),
7406
.slots = unpickler_memoproxy_slots,
7407
};
7408
7409
static PyObject *
7410
UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
7411
{
7412
PickleState *state = _Pickle_FindStateByType(Py_TYPE(unpickler));
7413
UnpicklerMemoProxyObject *self;
7414
self = PyObject_GC_New(UnpicklerMemoProxyObject,
7415
state->UnpicklerMemoProxyType);
7416
if (self == NULL)
7417
return NULL;
7418
self->unpickler = (UnpicklerObject*)Py_NewRef(unpickler);
7419
PyObject_GC_Track(self);
7420
return (PyObject *)self;
7421
}
7422
7423
/*****************************************************************************/
7424
7425
7426
static PyObject *
7427
Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored))
7428
{
7429
return UnpicklerMemoProxy_New(self);
7430
}
7431
7432
static int
7433
Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
7434
{
7435
PyObject **new_memo;
7436
size_t new_memo_size = 0;
7437
7438
if (obj == NULL) {
7439
PyErr_SetString(PyExc_TypeError,
7440
"attribute deletion is not supported");
7441
return -1;
7442
}
7443
7444
PickleState *state = _Pickle_FindStateByType(Py_TYPE(self));
7445
if (Py_IS_TYPE(obj, state->UnpicklerMemoProxyType)) {
7446
UnpicklerObject *unpickler =
7447
((UnpicklerMemoProxyObject *)obj)->unpickler;
7448
7449
new_memo_size = unpickler->memo_size;
7450
new_memo = _Unpickler_NewMemo(new_memo_size);
7451
if (new_memo == NULL)
7452
return -1;
7453
7454
for (size_t i = 0; i < new_memo_size; i++) {
7455
new_memo[i] = Py_XNewRef(unpickler->memo[i]);
7456
}
7457
}
7458
else if (PyDict_Check(obj)) {
7459
Py_ssize_t i = 0;
7460
PyObject *key, *value;
7461
7462
new_memo_size = PyDict_GET_SIZE(obj);
7463
new_memo = _Unpickler_NewMemo(new_memo_size);
7464
if (new_memo == NULL)
7465
return -1;
7466
7467
while (PyDict_Next(obj, &i, &key, &value)) {
7468
Py_ssize_t idx;
7469
if (!PyLong_Check(key)) {
7470
PyErr_SetString(PyExc_TypeError,
7471
"memo key must be integers");
7472
goto error;
7473
}
7474
idx = PyLong_AsSsize_t(key);
7475
if (idx == -1 && PyErr_Occurred())
7476
goto error;
7477
if (idx < 0) {
7478
PyErr_SetString(PyExc_ValueError,
7479
"memo key must be positive integers.");
7480
goto error;
7481
}
7482
if (_Unpickler_MemoPut(self, idx, value) < 0)
7483
goto error;
7484
}
7485
}
7486
else {
7487
PyErr_Format(PyExc_TypeError,
7488
"'memo' attribute must be an UnpicklerMemoProxy object "
7489
"or dict, not %.200s", Py_TYPE(obj)->tp_name);
7490
return -1;
7491
}
7492
7493
_Unpickler_MemoCleanup(self);
7494
self->memo_size = new_memo_size;
7495
self->memo = new_memo;
7496
7497
return 0;
7498
7499
error:
7500
if (new_memo_size) {
7501
for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
7502
Py_XDECREF(new_memo[i]);
7503
}
7504
PyMem_Free(new_memo);
7505
}
7506
return -1;
7507
}
7508
7509
static PyObject *
7510
Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored))
7511
{
7512
if (self->pers_func == NULL) {
7513
PyErr_SetString(PyExc_AttributeError, "persistent_load");
7514
return NULL;
7515
}
7516
return reconstruct_method(self->pers_func, self->pers_func_self);
7517
}
7518
7519
static int
7520
Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored))
7521
{
7522
if (value == NULL) {
7523
PyErr_SetString(PyExc_TypeError,
7524
"attribute deletion is not supported");
7525
return -1;
7526
}
7527
if (!PyCallable_Check(value)) {
7528
PyErr_SetString(PyExc_TypeError,
7529
"persistent_load must be a callable taking "
7530
"one argument");
7531
return -1;
7532
}
7533
7534
self->pers_func_self = NULL;
7535
Py_XSETREF(self->pers_func, Py_NewRef(value));
7536
7537
return 0;
7538
}
7539
7540
static PyGetSetDef Unpickler_getsets[] = {
7541
{"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7542
{"persistent_load", (getter)Unpickler_get_persload,
7543
(setter)Unpickler_set_persload},
7544
{NULL}
7545
};
7546
7547
static PyType_Slot unpickler_type_slots[] = {
7548
{Py_tp_dealloc, Unpickler_dealloc},
7549
{Py_tp_doc, (char *)_pickle_Unpickler___init____doc__},
7550
{Py_tp_traverse, Unpickler_traverse},
7551
{Py_tp_clear, Unpickler_clear},
7552
{Py_tp_methods, Unpickler_methods},
7553
{Py_tp_getset, Unpickler_getsets},
7554
{Py_tp_init, _pickle_Unpickler___init__},
7555
{Py_tp_alloc, PyType_GenericAlloc},
7556
{Py_tp_new, PyType_GenericNew},
7557
{Py_tp_free, PyObject_GC_Del},
7558
{0, NULL},
7559
};
7560
7561
static PyType_Spec unpickler_type_spec = {
7562
.name = "_pickle.Unpickler",
7563
.basicsize = sizeof(UnpicklerObject),
7564
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
7565
Py_TPFLAGS_IMMUTABLETYPE),
7566
.slots = unpickler_type_slots,
7567
};
7568
7569
/*[clinic input]
7570
7571
_pickle.dump
7572
7573
obj: object
7574
file: object
7575
protocol: object = None
7576
*
7577
fix_imports: bool = True
7578
buffer_callback: object = None
7579
7580
Write a pickled representation of obj to the open file object file.
7581
7582
This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7583
be more efficient.
7584
7585
The optional *protocol* argument tells the pickler to use the given
7586
protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
7587
protocol is 4. It was introduced in Python 3.4, and is incompatible
7588
with previous versions.
7589
7590
Specifying a negative protocol version selects the highest protocol
7591
version supported. The higher the protocol used, the more recent the
7592
version of Python needed to read the pickle produced.
7593
7594
The *file* argument must have a write() method that accepts a single
7595
bytes argument. It can thus be a file object opened for binary
7596
writing, an io.BytesIO instance, or any other custom object that meets
7597
this interface.
7598
7599
If *fix_imports* is True and protocol is less than 3, pickle will try
7600
to map the new Python 3 names to the old module names used in Python
7601
2, so that the pickle data stream is readable with Python 2.
7602
7603
If *buffer_callback* is None (the default), buffer views are serialized
7604
into *file* as part of the pickle stream. It is an error if
7605
*buffer_callback* is not None and *protocol* is None or smaller than 5.
7606
7607
[clinic start generated code]*/
7608
7609
static PyObject *
7610
_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
7611
PyObject *protocol, int fix_imports,
7612
PyObject *buffer_callback)
7613
/*[clinic end generated code: output=706186dba996490c input=5ed6653da99cd97c]*/
7614
{
7615
PickleState *state = _Pickle_GetState(module);
7616
PicklerObject *pickler = _Pickler_New(state);
7617
7618
if (pickler == NULL)
7619
return NULL;
7620
7621
if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
7622
goto error;
7623
7624
if (_Pickler_SetOutputStream(pickler, file) < 0)
7625
goto error;
7626
7627
if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0)
7628
goto error;
7629
7630
if (dump(state, pickler, obj) < 0)
7631
goto error;
7632
7633
if (_Pickler_FlushToFile(pickler) < 0)
7634
goto error;
7635
7636
Py_DECREF(pickler);
7637
Py_RETURN_NONE;
7638
7639
error:
7640
Py_XDECREF(pickler);
7641
return NULL;
7642
}
7643
7644
/*[clinic input]
7645
7646
_pickle.dumps
7647
7648
obj: object
7649
protocol: object = None
7650
*
7651
fix_imports: bool = True
7652
buffer_callback: object = None
7653
7654
Return the pickled representation of the object as a bytes object.
7655
7656
The optional *protocol* argument tells the pickler to use the given
7657
protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
7658
protocol is 4. It was introduced in Python 3.4, and is incompatible
7659
with previous versions.
7660
7661
Specifying a negative protocol version selects the highest protocol
7662
version supported. The higher the protocol used, the more recent the
7663
version of Python needed to read the pickle produced.
7664
7665
If *fix_imports* is True and *protocol* is less than 3, pickle will
7666
try to map the new Python 3 names to the old module names used in
7667
Python 2, so that the pickle data stream is readable with Python 2.
7668
7669
If *buffer_callback* is None (the default), buffer views are serialized
7670
into *file* as part of the pickle stream. It is an error if
7671
*buffer_callback* is not None and *protocol* is None or smaller than 5.
7672
7673
[clinic start generated code]*/
7674
7675
static PyObject *
7676
_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
7677
int fix_imports, PyObject *buffer_callback)
7678
/*[clinic end generated code: output=fbab0093a5580fdf input=e543272436c6f987]*/
7679
{
7680
PyObject *result;
7681
PickleState *state = _Pickle_GetState(module);
7682
PicklerObject *pickler = _Pickler_New(state);
7683
7684
if (pickler == NULL)
7685
return NULL;
7686
7687
if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
7688
goto error;
7689
7690
if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0)
7691
goto error;
7692
7693
if (dump(state, pickler, obj) < 0)
7694
goto error;
7695
7696
result = _Pickler_GetString(pickler);
7697
Py_DECREF(pickler);
7698
return result;
7699
7700
error:
7701
Py_XDECREF(pickler);
7702
return NULL;
7703
}
7704
7705
/*[clinic input]
7706
7707
_pickle.load
7708
7709
file: object
7710
*
7711
fix_imports: bool = True
7712
encoding: str = 'ASCII'
7713
errors: str = 'strict'
7714
buffers: object(c_default="NULL") = ()
7715
7716
Read and return an object from the pickle data stored in a file.
7717
7718
This is equivalent to ``Unpickler(file).load()``, but may be more
7719
efficient.
7720
7721
The protocol version of the pickle is detected automatically, so no
7722
protocol argument is needed. Bytes past the pickled object's
7723
representation are ignored.
7724
7725
The argument *file* must have two methods, a read() method that takes
7726
an integer argument, and a readline() method that requires no
7727
arguments. Both methods should return bytes. Thus *file* can be a
7728
binary file object opened for reading, an io.BytesIO object, or any
7729
other custom object that meets this interface.
7730
7731
Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7732
which are used to control compatibility support for pickle stream
7733
generated by Python 2. If *fix_imports* is True, pickle will try to
7734
map the old Python 2 names to the new names used in Python 3. The
7735
*encoding* and *errors* tell pickle how to decode 8-bit string
7736
instances pickled by Python 2; these default to 'ASCII' and 'strict',
7737
respectively. The *encoding* can be 'bytes' to read these 8-bit
7738
string instances as bytes objects.
7739
[clinic start generated code]*/
7740
7741
static PyObject *
7742
_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
7743
const char *encoding, const char *errors,
7744
PyObject *buffers)
7745
/*[clinic end generated code: output=250452d141c23e76 input=46c7c31c92f4f371]*/
7746
{
7747
PyObject *result;
7748
UnpicklerObject *unpickler = _Unpickler_New(module);
7749
7750
if (unpickler == NULL)
7751
return NULL;
7752
7753
if (_Unpickler_SetInputStream(unpickler, file) < 0)
7754
goto error;
7755
7756
if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7757
goto error;
7758
7759
if (_Unpickler_SetBuffers(unpickler, buffers) < 0)
7760
goto error;
7761
7762
unpickler->fix_imports = fix_imports;
7763
7764
PickleState *state = _Pickle_GetState(module);
7765
result = load(state, unpickler);
7766
Py_DECREF(unpickler);
7767
return result;
7768
7769
error:
7770
Py_XDECREF(unpickler);
7771
return NULL;
7772
}
7773
7774
/*[clinic input]
7775
7776
_pickle.loads
7777
7778
data: object
7779
/
7780
*
7781
fix_imports: bool = True
7782
encoding: str = 'ASCII'
7783
errors: str = 'strict'
7784
buffers: object(c_default="NULL") = ()
7785
7786
Read and return an object from the given pickle data.
7787
7788
The protocol version of the pickle is detected automatically, so no
7789
protocol argument is needed. Bytes past the pickled object's
7790
representation are ignored.
7791
7792
Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7793
which are used to control compatibility support for pickle stream
7794
generated by Python 2. If *fix_imports* is True, pickle will try to
7795
map the old Python 2 names to the new names used in Python 3. The
7796
*encoding* and *errors* tell pickle how to decode 8-bit string
7797
instances pickled by Python 2; these default to 'ASCII' and 'strict',
7798
respectively. The *encoding* can be 'bytes' to read these 8-bit
7799
string instances as bytes objects.
7800
[clinic start generated code]*/
7801
7802
static PyObject *
7803
_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
7804
const char *encoding, const char *errors,
7805
PyObject *buffers)
7806
/*[clinic end generated code: output=82ac1e6b588e6d02 input=b3615540d0535087]*/
7807
{
7808
PyObject *result;
7809
UnpicklerObject *unpickler = _Unpickler_New(module);
7810
7811
if (unpickler == NULL)
7812
return NULL;
7813
7814
if (_Unpickler_SetStringInput(unpickler, data) < 0)
7815
goto error;
7816
7817
if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7818
goto error;
7819
7820
if (_Unpickler_SetBuffers(unpickler, buffers) < 0)
7821
goto error;
7822
7823
unpickler->fix_imports = fix_imports;
7824
7825
PickleState *state = _Pickle_GetState(module);
7826
result = load(state, unpickler);
7827
Py_DECREF(unpickler);
7828
return result;
7829
7830
error:
7831
Py_XDECREF(unpickler);
7832
return NULL;
7833
}
7834
7835
static struct PyMethodDef pickle_methods[] = {
7836
_PICKLE_DUMP_METHODDEF
7837
_PICKLE_DUMPS_METHODDEF
7838
_PICKLE_LOAD_METHODDEF
7839
_PICKLE_LOADS_METHODDEF
7840
{NULL, NULL} /* sentinel */
7841
};
7842
7843
static int
7844
pickle_clear(PyObject *m)
7845
{
7846
_Pickle_ClearState(_Pickle_GetState(m));
7847
return 0;
7848
}
7849
7850
static void
7851
pickle_free(PyObject *m)
7852
{
7853
_Pickle_ClearState(_Pickle_GetState(m));
7854
}
7855
7856
static int
7857
pickle_traverse(PyObject *m, visitproc visit, void *arg)
7858
{
7859
PickleState *st = _Pickle_GetState(m);
7860
Py_VISIT(st->PickleError);
7861
Py_VISIT(st->PicklingError);
7862
Py_VISIT(st->UnpicklingError);
7863
Py_VISIT(st->dispatch_table);
7864
Py_VISIT(st->extension_registry);
7865
Py_VISIT(st->extension_cache);
7866
Py_VISIT(st->inverted_registry);
7867
Py_VISIT(st->name_mapping_2to3);
7868
Py_VISIT(st->import_mapping_2to3);
7869
Py_VISIT(st->name_mapping_3to2);
7870
Py_VISIT(st->import_mapping_3to2);
7871
Py_VISIT(st->codecs_encode);
7872
Py_VISIT(st->getattr);
7873
Py_VISIT(st->partial);
7874
Py_VISIT(st->Pickler_Type);
7875
Py_VISIT(st->Unpickler_Type);
7876
Py_VISIT(st->Pdata_Type);
7877
Py_VISIT(st->PicklerMemoProxyType);
7878
Py_VISIT(st->UnpicklerMemoProxyType);
7879
return 0;
7880
}
7881
7882
static int
7883
_pickle_exec(PyObject *m)
7884
{
7885
PickleState *st = _Pickle_GetState(m);
7886
7887
#define CREATE_TYPE(mod, type, spec) \
7888
do { \
7889
type = (PyTypeObject *)PyType_FromMetaclass(NULL, mod, spec, NULL); \
7890
if (type == NULL) { \
7891
return -1; \
7892
} \
7893
} while (0)
7894
7895
CREATE_TYPE(m, st->Pdata_Type, &pdata_spec);
7896
CREATE_TYPE(m, st->PicklerMemoProxyType, &memoproxy_spec);
7897
CREATE_TYPE(m, st->UnpicklerMemoProxyType, &unpickler_memoproxy_spec);
7898
CREATE_TYPE(m, st->Pickler_Type, &pickler_type_spec);
7899
CREATE_TYPE(m, st->Unpickler_Type, &unpickler_type_spec);
7900
7901
#undef CREATE_TYPE
7902
7903
/* Add types */
7904
if (PyModule_AddType(m, &PyPickleBuffer_Type) < 0) {
7905
return -1;
7906
}
7907
if (PyModule_AddType(m, st->Pickler_Type) < 0) {
7908
return -1;
7909
}
7910
if (PyModule_AddType(m, st->Unpickler_Type) < 0) {
7911
return -1;
7912
}
7913
7914
/* Initialize the exceptions. */
7915
st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7916
if (st->PickleError == NULL)
7917
return -1;
7918
st->PicklingError = \
7919
PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7920
if (st->PicklingError == NULL)
7921
return -1;
7922
st->UnpicklingError = \
7923
PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7924
if (st->UnpicklingError == NULL)
7925
return -1;
7926
7927
if (PyModule_AddObjectRef(m, "PickleError", st->PickleError) < 0) {
7928
return -1;
7929
}
7930
if (PyModule_AddObjectRef(m, "PicklingError", st->PicklingError) < 0) {
7931
return -1;
7932
}
7933
if (PyModule_AddObjectRef(m, "UnpicklingError", st->UnpicklingError) < 0) {
7934
return -1;
7935
}
7936
7937
if (_Pickle_InitState(st) < 0)
7938
return -1;
7939
7940
return 0;
7941
}
7942
7943
static PyModuleDef_Slot pickle_slots[] = {
7944
{Py_mod_exec, _pickle_exec},
7945
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
7946
{0, NULL},
7947
};
7948
7949
static struct PyModuleDef _picklemodule = {
7950
PyModuleDef_HEAD_INIT,
7951
.m_name = "_pickle",
7952
.m_doc = pickle_module_doc,
7953
.m_size = sizeof(PickleState),
7954
.m_methods = pickle_methods,
7955
.m_slots = pickle_slots,
7956
.m_traverse = pickle_traverse,
7957
.m_clear = pickle_clear,
7958
.m_free = (freefunc)pickle_free,
7959
};
7960
7961
PyMODINIT_FUNC
7962
PyInit__pickle(void)
7963
{
7964
return PyModuleDef_Init(&_picklemodule);
7965
}
7966
7967