3 #include "structmember.h"
5 PyDoc_STRVAR(cPickle_module_documentation
,
6 "C implementation and optimization of the Python pickle module.");
10 #define Py_eval_input eval_input
11 #endif /* Py_eval_input */
13 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
15 #define WRITE_BUF_SIZE 256
17 /* Bump this when new opcodes are added to the pickle protocol. */
18 #define HIGHEST_PROTOCOL 2
21 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
42 #define SHORT_BINSTRING 'U'
44 #define BINUNICODE 'X'
49 #define EMPTY_DICT '}'
54 #define LONG_BINGET 'j'
56 #define EMPTY_LIST ']'
60 #define LONG_BINPUT 'r'
63 #define EMPTY_TUPLE ')'
67 #define PROTO '\x80' /* identify pickle protocol */
68 #define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69 #define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70 #define EXT2 '\x83' /* ditto, but 2-byte index */
71 #define EXT4 '\x84' /* ditto, but 4-byte index */
72 #define TUPLE1 '\x85' /* build 1-tuple from stack top */
73 #define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74 #define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75 #define NEWTRUE '\x88' /* push True */
76 #define NEWFALSE '\x89' /* push False */
77 #define LONG1 '\x8a' /* push long from < 256 bytes */
78 #define LONG4 '\x8b' /* push really big long */
80 /* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
90 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
91 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
95 #define BATCHSIZE 1000
97 static char MARKv
= MARK
;
99 static PyObject
*PickleError
;
100 static PyObject
*PicklingError
;
101 static PyObject
*UnpickleableError
;
102 static PyObject
*UnpicklingError
;
103 static PyObject
*BadPickleGet
;
105 /* As the name says, an empty tuple. */
106 static PyObject
*empty_tuple
;
108 /* copy_reg.dispatch_table, {type_object: pickling_function} */
109 static PyObject
*dispatch_table
;
111 /* For EXT[124] opcodes. */
112 /* copy_reg._extension_registry, {(module_name, function_name): code} */
113 static PyObject
*extension_registry
;
114 /* copy_reg._inverted_registry, {code: (module_name, function_name)} */
115 static PyObject
*inverted_registry
;
116 /* copy_reg._extension_cache, {code: object} */
117 static PyObject
*extension_cache
;
119 /* For looking up name pairs in copy_reg._extension_registry. */
120 static PyObject
*two_tuple
;
122 static PyObject
*__class___str
, *__getinitargs___str
, *__dict___str
,
123 *__getstate___str
, *__setstate___str
, *__name___str
, *__reduce___str
,
125 *write_str
, *append_str
,
126 *read_str
, *readline_str
, *__main___str
, *__basicnew___str
,
127 *copy_reg_str
, *dispatch_table_str
;
129 /*************************************************************************
130 Internal Data type for pickle data. */
134 int length
; /* number of initial slots in data currently used */
135 int size
; /* number of slots in data allocated */
140 Pdata_dealloc(Pdata
*self
)
145 for (i
= self
->length
, p
= self
->data
; --i
>= 0; p
++) {
153 static PyTypeObject PdataType
= {
154 PyObject_HEAD_INIT(NULL
) 0, "cPickle.Pdata", sizeof(Pdata
), 0,
155 (destructor
)Pdata_dealloc
,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
159 #define Pdata_Check(O) ((O)->ob_type == &PdataType)
166 if (!(self
= PyObject_New(Pdata
, &PdataType
)))
170 self
->data
= malloc(self
->size
* sizeof(PyObject
*));
172 return (PyObject
*)self
;
174 return PyErr_NoMemory();
180 PyErr_SetString(UnpicklingError
, "unpickling stack underflow");
184 /* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
188 Pdata_clear(Pdata
*self
, int clearto
)
193 if (clearto
< 0) return stackUnderflow();
194 if (clearto
>= self
->length
) return 0;
196 for (i
= self
->length
, p
= self
->data
+ clearto
;
201 self
->length
= clearto
;
207 Pdata_grow(Pdata
*self
)
212 bigger
= self
->size
<< 1;
213 if (bigger
<= 0) /* was 0, or new value overflows */
215 if ((int)(size_t)bigger
!= bigger
)
217 nbytes
= (size_t)bigger
* sizeof(PyObject
*);
218 if (nbytes
/ sizeof(PyObject
*) != (size_t)bigger
)
220 self
->data
= realloc(self
->data
, nbytes
);
221 if (self
->data
== NULL
)
232 /* D is a Pdata*. Pop the topmost element and store it into V, which
233 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
234 * is raised and V is set to NULL. D and V may be evaluated several times.
236 #define PDATA_POP(D, V) { \
238 (V) = (D)->data[--((D)->length)]; \
240 PyErr_SetString(UnpicklingError, "bad pickle data"); \
245 /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
246 * D. If the Pdata stack can't be grown to hold the new value, both
247 * raise MemoryError and execute "return ER". The difference is in ownership
248 * of O after: _PUSH transfers ownership of O from the caller to the stack
249 * (no incref of O is done, and in case of error O is decrefed), while
250 * _APPEND pushes a new reference.
253 /* Push O on stack D, giving ownership of O to the stack. */
254 #define PDATA_PUSH(D, O, ER) { \
255 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
256 Pdata_grow((Pdata*)(D)) < 0) { \
260 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
263 /* Push O on stack D, pushing a new reference. */
264 #define PDATA_APPEND(D, O, ER) { \
265 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
266 Pdata_grow((Pdata*)(D)) < 0) \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
274 Pdata_popTuple(Pdata
*self
, int start
)
279 l
= self
->length
-start
;
283 for (i
= start
, j
= 0 ; j
< l
; i
++, j
++)
284 PyTuple_SET_ITEM(r
, j
, self
->data
[i
]);
286 self
->length
= start
;
291 Pdata_popList(Pdata
*self
, int start
)
296 l
=self
->length
-start
;
297 if (!( r
=PyList_New(l
))) return NULL
;
298 for (i
=start
, j
=0 ; j
< l
; i
++, j
++)
299 PyList_SET_ITEM(r
, j
, self
->data
[i
]);
305 /*************************************************************************/
307 #define ARG_TUP(self, o) { \
308 if (self->arg || (self->arg=PyTuple_New(1))) { \
309 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
310 PyTuple_SET_ITEM(self->arg,0,o); \
317 #define FREE_ARG_TUP(self) { \
318 if (self->arg->ob_refcnt > 1) { \
319 Py_DECREF(self->arg); \
324 typedef struct Picklerobject
{
332 PyObject
*inst_pers_func
;
334 /* pickle protocol number, >= 0 */
337 /* bool, true if proto > 0 */
340 int fast
; /* Fast mode doesn't save in memo, don't use if circ ref */
342 int (*write_func
)(struct Picklerobject
*, const char *, int);
345 PyObject
*dispatch_table
;
346 int fast_container
; /* count nested container dumps */
350 #ifndef PY_CPICKLE_FAST_LIMIT
351 #define PY_CPICKLE_FAST_LIMIT 50
354 static PyTypeObject Picklertype
;
356 typedef struct Unpicklerobject
{
367 PyObject
*last_string
;
371 int (*read_func
)(struct Unpicklerobject
*, char **, int);
372 int (*readline_func
)(struct Unpicklerobject
*, char **);
375 PyObject
*find_class
;
378 static PyTypeObject Unpicklertype
;
380 /* Forward decls that need the above structs */
381 static int save(Picklerobject
*, PyObject
*, int);
382 static int put2(Picklerobject
*, PyObject
*);
386 cPickle_ErrFormat(PyObject
*ErrType
, char *stringformat
, char *format
, ...)
389 PyObject
*args
=0, *retval
=0;
390 va_start(va
, format
);
392 if (format
) args
= Py_VaBuildValue(format
, va
);
394 if (format
&& ! args
) return NULL
;
395 if (stringformat
&& !(retval
=PyString_FromString(stringformat
)))
401 v
=PyString_Format(retval
, args
);
404 if (! v
) return NULL
;
409 if (args
) retval
=args
;
411 PyErr_SetObject(ErrType
,Py_None
);
414 PyErr_SetObject(ErrType
,retval
);
420 write_file(Picklerobject
*self
, const char *s
, int n
)
422 size_t nbyteswritten
;
428 Py_BEGIN_ALLOW_THREADS
429 nbyteswritten
= fwrite(s
, sizeof(char), n
, self
->fp
);
431 if (nbyteswritten
!= (size_t)n
) {
432 PyErr_SetFromErrno(PyExc_IOError
);
440 write_cStringIO(Picklerobject
*self
, const char *s
, int n
)
446 if (PycStringIO
->cwrite((PyObject
*)self
->file
, s
, n
) != n
) {
454 write_none(Picklerobject
*self
, const char *s
, int n
)
456 if (s
== NULL
) return 0;
461 write_other(Picklerobject
*self
, const char *s
, int n
)
463 PyObject
*py_str
= 0, *junk
= 0;
466 if (!( self
->buf_size
)) return 0;
467 py_str
= PyString_FromStringAndSize(self
->write_buf
,
473 if (self
->buf_size
&& (n
+ self
->buf_size
) > WRITE_BUF_SIZE
) {
474 if (write_other(self
, NULL
, 0) < 0)
478 if (n
> WRITE_BUF_SIZE
) {
480 PyString_FromStringAndSize(s
, n
)))
484 memcpy(self
->write_buf
+ self
->buf_size
, s
, n
);
491 /* object with write method */
492 ARG_TUP(self
, py_str
);
494 junk
= PyObject_Call(self
->write
, self
->arg
, NULL
);
497 if (junk
) Py_DECREF(junk
);
501 PDATA_PUSH(self
->file
, py_str
, -1);
509 read_file(Unpicklerobject
*self
, char **s
, int n
)
513 if (self
->buf_size
== 0) {
516 size
= ((n
< 32) ? 32 : n
);
517 if (!( self
->buf
= (char *)malloc(size
))) {
522 self
->buf_size
= size
;
524 else if (n
> self
->buf_size
) {
525 self
->buf
= (char *)realloc(self
->buf
, n
);
533 Py_BEGIN_ALLOW_THREADS
534 nbytesread
= fread(self
->buf
, sizeof(char), n
, self
->fp
);
536 if (nbytesread
!= (size_t)n
) {
537 if (feof(self
->fp
)) {
538 PyErr_SetNone(PyExc_EOFError
);
542 PyErr_SetFromErrno(PyExc_IOError
);
553 readline_file(Unpicklerobject
*self
, char **s
)
557 if (self
->buf_size
== 0) {
558 if (!( self
->buf
= (char *)malloc(40))) {
568 for (; i
< (self
->buf_size
- 1); i
++) {
569 if (feof(self
->fp
) ||
570 (self
->buf
[i
] = getc(self
->fp
)) == '\n') {
571 self
->buf
[i
+ 1] = '\0';
576 bigger
= self
->buf_size
<< 1;
577 if (bigger
<= 0) { /* overflow */
581 self
->buf
= (char *)realloc(self
->buf
, bigger
);
586 self
->buf_size
= bigger
;
592 read_cStringIO(Unpicklerobject
*self
, char **s
, int n
)
596 if (PycStringIO
->cread((PyObject
*)self
->file
, &ptr
, n
) != n
) {
597 PyErr_SetNone(PyExc_EOFError
);
608 readline_cStringIO(Unpicklerobject
*self
, char **s
)
613 if ((n
= PycStringIO
->creadline((PyObject
*)self
->file
, &ptr
)) < 0) {
624 read_other(Unpicklerobject
*self
, char **s
, int n
)
626 PyObject
*bytes
, *str
=0;
628 if (!( bytes
= PyInt_FromLong(n
))) return -1;
630 ARG_TUP(self
, bytes
);
632 str
= PyObject_Call(self
->read
, self
->arg
, NULL
);
635 if (! str
) return -1;
637 Py_XDECREF(self
->last_string
);
638 self
->last_string
= str
;
640 if (! (*s
= PyString_AsString(str
))) return -1;
646 readline_other(Unpicklerobject
*self
, char **s
)
651 if (!( str
= PyObject_CallObject(self
->readline
, empty_tuple
))) {
655 if ((str_size
= PyString_Size(str
)) < 0)
658 Py_XDECREF(self
->last_string
);
659 self
->last_string
= str
;
661 if (! (*s
= PyString_AsString(str
)))
667 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
668 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
669 * The caller is responsible for free()'ing the return value.
672 pystrndup(const char *s
, int n
)
674 char *r
= (char *)malloc(n
+1);
676 return (char*)PyErr_NoMemory();
684 get(Picklerobject
*self
, PyObject
*id
)
686 PyObject
*value
, *mv
;
691 if (!( mv
= PyDict_GetItem(self
->memo
, id
))) {
692 PyErr_SetObject(PyExc_KeyError
, id
);
696 if (!( value
= PyTuple_GetItem(mv
, 0)))
699 if (!( PyInt_Check(value
))) {
700 PyErr_SetString(PicklingError
, "no int where int expected in memo");
703 c_value
= PyInt_AS_LONG((PyIntObject
*)value
);
707 PyOS_snprintf(s
+ 1, sizeof(s
) - 1, "%ld\n", c_value
);
710 else if (Pdata_Check(self
->file
)) {
711 if (write_other(self
, NULL
, 0) < 0) return -1;
712 PDATA_APPEND(self
->file
, mv
, -1);
718 s
[1] = (int)(c_value
& 0xff);
723 s
[1] = (int)(c_value
& 0xff);
724 s
[2] = (int)((c_value
>> 8) & 0xff);
725 s
[3] = (int)((c_value
>> 16) & 0xff);
726 s
[4] = (int)((c_value
>> 24) & 0xff);
731 if (self
->write_func(self
, s
, len
) < 0)
739 put(Picklerobject
*self
, PyObject
*ob
)
741 if (ob
->ob_refcnt
< 2 || self
->fast
)
744 return put2(self
, ob
);
749 put2(Picklerobject
*self
, PyObject
*ob
)
755 PyObject
*py_ob_id
= 0, *memo_len
= 0, *t
= 0;
760 if ((p
= PyDict_Size(self
->memo
)) < 0)
763 /* Make sure memo keys are positive! */
765 * XXX And does "positive" really mean non-negative?
766 * XXX pickle.py starts with PUT index 0, not 1. This makes for
767 * XXX gratuitous differences between the pickling modules.
771 if (!( py_ob_id
= PyLong_FromVoidPtr(ob
)))
774 if (!( memo_len
= PyInt_FromLong(p
)))
777 if (!( t
= PyTuple_New(2)))
780 PyTuple_SET_ITEM(t
, 0, memo_len
);
782 PyTuple_SET_ITEM(t
, 1, ob
);
785 if (PyDict_SetItem(self
->memo
, py_ob_id
, t
) < 0)
790 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%d\n", p
);
793 else if (Pdata_Check(self
->file
)) {
794 if (write_other(self
, NULL
, 0) < 0) return -1;
795 PDATA_APPEND(self
->file
, memo_len
, -1);
796 res
=0; /* Job well done ;) */
801 c_str
[0] = LONG_BINPUT
;
802 c_str
[1] = (int)(p
& 0xff);
803 c_str
[2] = (int)((p
>> 8) & 0xff);
804 c_str
[3] = (int)((p
>> 16) & 0xff);
805 c_str
[4] = (int)((p
>> 24) & 0xff);
815 if (self
->write_func(self
, c_str
, len
) < 0)
821 Py_XDECREF(py_ob_id
);
822 Py_XDECREF(memo_len
);
829 whichmodule(PyObject
*global
, PyObject
*global_name
)
832 PyObject
*module
= 0, *modules_dict
= 0,
833 *global_name_attr
= 0, *name
= 0;
835 module
= PyObject_GetAttrString(global
, "__module__");
838 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
843 if (!( modules_dict
= PySys_GetObject("modules")))
847 while ((j
= PyDict_Next(modules_dict
, &i
, &name
, &module
))) {
849 if (PyObject_Compare(name
, __main___str
)==0) continue;
851 global_name_attr
= PyObject_GetAttr(module
, global_name
);
852 if (!global_name_attr
) {
853 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
860 if (global_name_attr
!= global
) {
861 Py_DECREF(global_name_attr
);
865 Py_DECREF(global_name_attr
);
870 /* The following implements the rule in pickle.py added in 1.5
871 that used __main__ if no module is found. I don't actually
885 fast_save_enter(Picklerobject
*self
, PyObject
*obj
)
887 /* if fast_container < 0, we're doing an error exit. */
888 if (++self
->fast_container
>= PY_CPICKLE_FAST_LIMIT
) {
889 PyObject
*key
= NULL
;
890 if (self
->fast_memo
== NULL
) {
891 self
->fast_memo
= PyDict_New();
892 if (self
->fast_memo
== NULL
) {
893 self
->fast_container
= -1;
897 key
= PyLong_FromVoidPtr(obj
);
900 if (PyDict_GetItem(self
->fast_memo
, key
)) {
902 PyErr_Format(PyExc_ValueError
,
903 "fast mode: can't pickle cyclic objects "
904 "including object type %s at %p",
905 obj
->ob_type
->tp_name
, obj
);
906 self
->fast_container
= -1;
909 if (PyDict_SetItem(self
->fast_memo
, key
, Py_None
) < 0) {
911 self
->fast_container
= -1;
920 fast_save_leave(Picklerobject
*self
, PyObject
*obj
)
922 if (self
->fast_container
-- >= PY_CPICKLE_FAST_LIMIT
) {
923 PyObject
*key
= PyLong_FromVoidPtr(obj
);
926 if (PyDict_DelItem(self
->fast_memo
, key
) < 0) {
936 save_none(Picklerobject
*self
, PyObject
*args
)
938 static char none
= NONE
;
939 if (self
->write_func(self
, &none
, 1) < 0)
946 save_bool(Picklerobject
*self
, PyObject
*args
)
948 static const char *buf
[2] = {FALSE
, TRUE
};
949 static char len
[2] = {sizeof(FALSE
)-1, sizeof(TRUE
)-1};
950 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
952 if (self
->proto
>= 2) {
953 char opcode
= l
? NEWTRUE
: NEWFALSE
;
954 if (self
->write_func(self
, &opcode
, 1) < 0)
957 else if (self
->write_func(self
, buf
[l
], len
[l
]) < 0)
963 save_int(Picklerobject
*self
, PyObject
*args
)
966 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
975 /* Text-mode pickle, or long too big to fit in the 4-byte
976 * signed BININT format: store as a string.
979 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%ld\n", l
);
980 if (self
->write_func(self
, c_str
, strlen(c_str
)) < 0)
984 /* Binary pickle and l fits in a signed 4-byte int. */
985 c_str
[1] = (int)( l
& 0xff);
986 c_str
[2] = (int)((l
>> 8) & 0xff);
987 c_str
[3] = (int)((l
>> 16) & 0xff);
988 c_str
[4] = (int)((l
>> 24) & 0xff);
990 if ((c_str
[4] == 0) && (c_str
[3] == 0)) {
1005 if (self
->write_func(self
, c_str
, len
) < 0)
1014 save_long(Picklerobject
*self
, PyObject
*args
)
1018 PyObject
*repr
= NULL
;
1020 static char l
= LONG
;
1022 if (self
->proto
>= 2) {
1023 /* Linear-time pickling. */
1026 unsigned char *pdata
;
1029 int sign
= _PyLong_Sign(args
);
1032 /* It's 0 -- an empty bytestring. */
1035 i
= self
->write_func(self
, c_str
, 2);
1036 if (i
< 0) goto finally
;
1040 nbits
= _PyLong_NumBits(args
);
1041 if (nbits
== (size_t)-1 && PyErr_Occurred())
1043 /* How many bytes do we need? There are nbits >> 3 full
1044 * bytes of data, and nbits & 7 leftover bits. If there
1045 * are any leftover bits, then we clearly need another
1046 * byte. Wnat's not so obvious is that we *probably*
1047 * need another byte even if there aren't any leftovers:
1048 * the most-significant bit of the most-significant byte
1049 * acts like a sign bit, and it's usually got a sense
1050 * opposite of the one we need. The exception is longs
1051 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1052 * its own 256's-complement, so has the right sign bit
1053 * even without the extra byte. That's a pain to check
1054 * for in advance, though, so we always grab an extra
1055 * byte at the start, and cut it back later if possible.
1057 nbytes
= (nbits
>> 3) + 1;
1058 if ((int)nbytes
< 0 || (size_t)(int)nbytes
!= nbytes
) {
1059 PyErr_SetString(PyExc_OverflowError
, "long too large "
1063 repr
= PyString_FromStringAndSize(NULL
, (int)nbytes
);
1064 if (repr
== NULL
) goto finally
;
1065 pdata
= (unsigned char *)PyString_AS_STRING(repr
);
1066 i
= _PyLong_AsByteArray((PyLongObject
*)args
,
1068 1 /* little endian */, 1 /* signed */);
1069 if (i
< 0) goto finally
;
1070 /* If the long is negative, this may be a byte more than
1071 * needed. This is so iff the MSB is all redundant sign
1074 if (sign
< 0 && nbytes
> 1 && pdata
[nbytes
- 1] == 0xff &&
1075 (pdata
[nbytes
- 2] & 0x80) != 0)
1080 c_str
[1] = (char)nbytes
;
1086 for (i
= 1; i
< 5; i
++) {
1087 c_str
[i
] = (char)(size
& 0xff);
1092 i
= self
->write_func(self
, c_str
, size
);
1093 if (i
< 0) goto finally
;
1094 i
= self
->write_func(self
, (char *)pdata
, (int)nbytes
);
1095 if (i
< 0) goto finally
;
1100 /* proto < 2: write the repr and newline. This is quadratic-time
1101 * (in the number of digits), in both directions.
1103 if (!( repr
= PyObject_Repr(args
)))
1106 if ((size
= PyString_Size(repr
)) < 0)
1109 if (self
->write_func(self
, &l
, 1) < 0)
1112 if (self
->write_func(self
,
1113 PyString_AS_STRING((PyStringObject
*)repr
),
1117 if (self
->write_func(self
, "\n", 1) < 0)
1129 save_float(Picklerobject
*self
, PyObject
*args
)
1131 double x
= PyFloat_AS_DOUBLE((PyFloatObject
*)args
);
1136 if (_PyFloat_Pack8(x
, (unsigned char *)&str
[1], 0) < 0)
1138 if (self
->write_func(self
, str
, 9) < 0)
1144 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%.17g\n", x
);
1146 if (self
->write_func(self
, c_str
, strlen(c_str
)) < 0)
1155 save_string(Picklerobject
*self
, PyObject
*args
, int doput
)
1160 if ((size
= PyString_Size(args
)) < 0)
1166 static char string
= STRING
;
1168 if (!( repr
= PyObject_Repr(args
)))
1171 if ((len
= PyString_Size(repr
)) < 0)
1173 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1175 if (self
->write_func(self
, &string
, 1) < 0)
1178 if (self
->write_func(self
, repr_str
, len
) < 0)
1181 if (self
->write_func(self
, "\n", 1) < 0)
1190 if ((size
= PyString_Size(args
)) < 0)
1194 c_str
[0] = SHORT_BINSTRING
;
1199 c_str
[0] = BINSTRING
;
1200 for (i
= 1; i
< 5; i
++)
1201 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1205 if (self
->write_func(self
, c_str
, len
) < 0)
1208 if (size
> 128 && Pdata_Check(self
->file
)) {
1209 if (write_other(self
, NULL
, 0) < 0) return -1;
1210 PDATA_APPEND(self
->file
, args
, -1);
1213 if (self
->write_func(self
,
1215 (PyStringObject
*)args
),
1222 if (put(self
, args
) < 0)
1233 #ifdef Py_USING_UNICODE
1234 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1235 backslash and newline characters to \uXXXX escapes. */
1237 modified_EncodeRawUnicodeEscape(const Py_UNICODE
*s
, int size
)
1243 static const char *hexdigit
= "0123456789ABCDEF";
1245 repr
= PyString_FromStringAndSize(NULL
, 6 * size
);
1251 p
= q
= PyString_AS_STRING(repr
);
1252 while (size
-- > 0) {
1253 Py_UNICODE ch
= *s
++;
1254 /* Map 16-bit characters to '\uxxxx' */
1255 if (ch
>= 256 || ch
== '\\' || ch
== '\n') {
1258 *p
++ = hexdigit
[(ch
>> 12) & 0xf];
1259 *p
++ = hexdigit
[(ch
>> 8) & 0xf];
1260 *p
++ = hexdigit
[(ch
>> 4) & 0xf];
1261 *p
++ = hexdigit
[ch
& 15];
1263 /* Copy everything else as-is */
1268 _PyString_Resize(&repr
, p
- q
);
1274 save_unicode(Picklerobject
*self
, PyObject
*args
, int doput
)
1279 if (!PyUnicode_Check(args
))
1284 static char string
= UNICODE
;
1286 repr
= modified_EncodeRawUnicodeEscape(
1287 PyUnicode_AS_UNICODE(args
), PyUnicode_GET_SIZE(args
));
1291 if ((len
= PyString_Size(repr
)) < 0)
1293 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1295 if (self
->write_func(self
, &string
, 1) < 0)
1298 if (self
->write_func(self
, repr_str
, len
) < 0)
1301 if (self
->write_func(self
, "\n", 1) < 0)
1310 if (!( repr
= PyUnicode_AsUTF8String(args
)))
1313 if ((size
= PyString_Size(repr
)) < 0)
1316 c_str
[0] = BINUNICODE
;
1317 for (i
= 1; i
< 5; i
++)
1318 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1321 if (self
->write_func(self
, c_str
, len
) < 0)
1324 if (size
> 128 && Pdata_Check(self
->file
)) {
1325 if (write_other(self
, NULL
, 0) < 0)
1327 PDATA_APPEND(self
->file
, repr
, -1);
1330 if (self
->write_func(self
, PyString_AS_STRING(repr
),
1339 if (put(self
, args
) < 0)
1350 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1352 store_tuple_elements(Picklerobject
*self
, PyObject
*t
, int len
)
1355 int res
= -1; /* guilty until proved innocent */
1357 assert(PyTuple_Size(t
) == len
);
1359 for (i
= 0; i
< len
; i
++) {
1360 PyObject
*element
= PyTuple_GET_ITEM(t
, i
);
1362 if (element
== NULL
)
1364 if (save(self
, element
, 0) < 0)
1373 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1374 * used across protocols to minimize the space needed to pickle them.
1375 * Tuples are also the only builtin immutable type that can be recursive
1376 * (a tuple can be reached from itself), and that requires some subtle
1377 * magic so that it works in all cases. IOW, this is a long routine.
1380 save_tuple(Picklerobject
*self
, PyObject
*args
)
1382 PyObject
*py_tuple_id
= NULL
;
1386 static char tuple
= TUPLE
;
1387 static char pop
= POP
;
1388 static char pop_mark
= POP_MARK
;
1389 static char len2opcode
[] = {EMPTY_TUPLE
, TUPLE1
, TUPLE2
, TUPLE3
};
1391 if ((len
= PyTuple_Size(args
)) < 0)
1398 c_str
[0] = EMPTY_TUPLE
;
1406 if (self
->write_func(self
, c_str
, len
) >= 0)
1408 /* Don't memoize an empty tuple. */
1412 /* A non-empty tuple. */
1414 /* id(tuple) isn't in the memo now. If it shows up there after
1415 * saving the tuple elements, the tuple must be recursive, in
1416 * which case we'll pop everything we put on the stack, and fetch
1417 * its value from the memo.
1419 py_tuple_id
= PyLong_FromVoidPtr(args
);
1420 if (py_tuple_id
== NULL
)
1423 if (len
<= 3 && self
->proto
>= 2) {
1424 /* Use TUPLE{1,2,3} opcodes. */
1425 if (store_tuple_elements(self
, args
, len
) < 0)
1427 if (PyDict_GetItem(self
->memo
, py_tuple_id
)) {
1428 /* pop the len elements */
1429 for (i
= 0; i
< len
; ++i
)
1430 if (self
->write_func(self
, &pop
, 1) < 0)
1432 /* fetch from memo */
1433 if (get(self
, py_tuple_id
) < 0)
1438 /* Not recursive. */
1439 if (self
->write_func(self
, len2opcode
+ len
, 1) < 0)
1444 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1445 * Generate MARK elt1 elt2 ... TUPLE
1447 if (self
->write_func(self
, &MARKv
, 1) < 0)
1450 if (store_tuple_elements(self
, args
, len
) < 0)
1453 if (PyDict_GetItem(self
->memo
, py_tuple_id
)) {
1454 /* pop the stack stuff we pushed */
1456 if (self
->write_func(self
, &pop_mark
, 1) < 0)
1460 /* Note that we pop one more than len, to remove
1463 for (i
= 0; i
<= len
; i
++)
1464 if (self
->write_func(self
, &pop
, 1) < 0)
1467 /* fetch from memo */
1468 if (get(self
, py_tuple_id
) >= 0)
1473 /* Not recursive. */
1474 if (self
->write_func(self
, &tuple
, 1) < 0)
1478 if (put(self
, args
) >= 0)
1482 Py_XDECREF(py_tuple_id
);
1486 /* iter is an iterator giving items, and we batch up chunks of
1487 * MARK item item ... item APPENDS
1488 * opcode sequences. Calling code should have arranged to first create an
1489 * empty list, or list-like object, for the APPENDS to operate on.
1490 * Returns 0 on success, <0 on error.
1493 batch_list(Picklerobject
*self
, PyObject
*iter
)
1496 PyObject
*slice
[BATCHSIZE
];
1499 static char append
= APPEND
;
1500 static char appends
= APPENDS
;
1502 assert(iter
!= NULL
);
1504 if (self
->proto
== 0) {
1505 /* APPENDS isn't available; do one at a time. */
1507 obj
= PyIter_Next(iter
);
1509 if (PyErr_Occurred())
1513 i
= save(self
, obj
, 0);
1517 if (self
->write_func(self
, &append
, 1) < 0)
1523 /* proto > 0: write in batches of BATCHSIZE. */
1525 /* Get next group of (no more than) BATCHSIZE elements. */
1526 for (n
= 0; n
< BATCHSIZE
; ++n
) {
1527 obj
= PyIter_Next(iter
);
1529 if (PyErr_Occurred())
1537 /* Pump out MARK, slice[0:n], APPENDS. */
1538 if (self
->write_func(self
, &MARKv
, 1) < 0)
1540 for (i
= 0; i
< n
; ++i
) {
1541 if (save(self
, slice
[i
], 0) < 0)
1544 if (self
->write_func(self
, &appends
, 1) < 0)
1548 if (save(self
, slice
[0], 0) < 0)
1550 if (self
->write_func(self
, &append
, 1) < 0)
1554 for (i
= 0; i
< n
; ++i
) {
1555 Py_DECREF(slice
[i
]);
1557 } while (n
== BATCHSIZE
);
1562 Py_DECREF(slice
[n
]);
1568 save_list(Picklerobject
*self
, PyObject
*args
)
1575 if (self
->fast
&& !fast_save_enter(self
, args
))
1578 /* Create an empty list. */
1589 if (self
->write_func(self
, s
, len
) < 0)
1592 /* Get list length, and bow out early if empty. */
1593 if ((len
= PyList_Size(args
)) < 0)
1598 if (put(self
, args
) >= 0)
1602 if (put2(self
, args
) < 0)
1605 /* Materialize the list elements. */
1606 iter
= PyObject_GetIter(args
);
1609 res
= batch_list(self
, iter
);
1613 if (self
->fast
&& !fast_save_leave(self
, args
))
1620 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1621 * MARK key value ... key value SETITEMS
1622 * opcode sequences. Calling code should have arranged to first create an
1623 * empty dict, or dict-like object, for the SETITEMS to operate on.
1624 * Returns 0 on success, <0 on error.
1626 * This is very much like batch_list(). The difference between saving
1627 * elements directly, and picking apart two-tuples, is so long-winded at
1628 * the C level, though, that attempts to combine these routines were too
1632 batch_dict(Picklerobject
*self
, PyObject
*iter
)
1635 PyObject
*slice
[BATCHSIZE
];
1638 static char setitem
= SETITEM
;
1639 static char setitems
= SETITEMS
;
1641 assert(iter
!= NULL
);
1643 if (self
->proto
== 0) {
1644 /* SETITEMS isn't available; do one at a time. */
1646 p
= PyIter_Next(iter
);
1648 if (PyErr_Occurred())
1652 if (!PyTuple_Check(p
) || PyTuple_Size(p
) != 2) {
1653 PyErr_SetString(PyExc_TypeError
, "dict items "
1654 "iterator must return 2-tuples");
1657 i
= save(self
, PyTuple_GET_ITEM(p
, 0), 0);
1659 i
= save(self
, PyTuple_GET_ITEM(p
, 1), 0);
1663 if (self
->write_func(self
, &setitem
, 1) < 0)
1669 /* proto > 0: write in batches of BATCHSIZE. */
1671 /* Get next group of (no more than) BATCHSIZE elements. */
1672 for (n
= 0; n
< BATCHSIZE
; ++n
) {
1673 p
= PyIter_Next(iter
);
1675 if (PyErr_Occurred())
1679 if (!PyTuple_Check(p
) || PyTuple_Size(p
) != 2) {
1680 PyErr_SetString(PyExc_TypeError
, "dict items "
1681 "iterator must return 2-tuples");
1688 /* Pump out MARK, slice[0:n], SETITEMS. */
1689 if (self
->write_func(self
, &MARKv
, 1) < 0)
1691 for (i
= 0; i
< n
; ++i
) {
1693 if (save(self
, PyTuple_GET_ITEM(p
, 0), 0) < 0)
1695 if (save(self
, PyTuple_GET_ITEM(p
, 1), 0) < 0)
1698 if (self
->write_func(self
, &setitems
, 1) < 0)
1703 if (save(self
, PyTuple_GET_ITEM(p
, 0), 0) < 0)
1705 if (save(self
, PyTuple_GET_ITEM(p
, 1), 0) < 0)
1707 if (self
->write_func(self
, &setitem
, 1) < 0)
1711 for (i
= 0; i
< n
; ++i
) {
1712 Py_DECREF(slice
[i
]);
1714 } while (n
== BATCHSIZE
);
1719 Py_DECREF(slice
[n
]);
1725 save_dict(Picklerobject
*self
, PyObject
*args
)
1732 if (self
->fast
&& !fast_save_enter(self
, args
))
1735 /* Create an empty dict. */
1746 if (self
->write_func(self
, s
, len
) < 0)
1749 /* Get dict size, and bow out early if empty. */
1750 if ((len
= PyDict_Size(args
)) < 0)
1754 if (put(self
, args
) >= 0)
1758 if (put2(self
, args
) < 0)
1761 /* Materialize the dict items. */
1762 iter
= PyObject_CallMethod(args
, "iteritems", "()");
1765 res
= batch_dict(self
, iter
);
1769 if (self
->fast
&& !fast_save_leave(self
, args
))
1777 save_inst(Picklerobject
*self
, PyObject
*args
)
1779 PyObject
*class = 0, *module
= 0, *name
= 0, *state
= 0,
1780 *getinitargs_func
= 0, *getstate_func
= 0, *class_args
= 0;
1781 char *module_str
, *name_str
;
1782 int module_size
, name_size
, res
= -1;
1784 static char inst
= INST
, obj
= OBJ
, build
= BUILD
;
1786 if (self
->fast
&& !fast_save_enter(self
, args
))
1789 if (self
->write_func(self
, &MARKv
, 1) < 0)
1792 if (!( class = PyObject_GetAttr(args
, __class___str
)))
1796 if (save(self
, class, 0) < 0)
1800 if ((getinitargs_func
= PyObject_GetAttr(args
, __getinitargs___str
))) {
1801 PyObject
*element
= 0;
1805 PyObject_Call(getinitargs_func
, empty_tuple
, NULL
)))
1808 if ((len
= PyObject_Size(class_args
)) < 0)
1811 for (i
= 0; i
< len
; i
++) {
1812 if (!( element
= PySequence_GetItem(class_args
, i
)))
1815 if (save(self
, element
, 0) < 0) {
1824 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
1831 if (!( name
= ((PyClassObject
*)class)->cl_name
)) {
1832 PyErr_SetString(PicklingError
, "class has no name");
1836 if (!( module
= whichmodule(class, name
)))
1840 if ((module_size
= PyString_Size(module
)) < 0 ||
1841 (name_size
= PyString_Size(name
)) < 0)
1844 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1845 name_str
= PyString_AS_STRING((PyStringObject
*)name
);
1847 if (self
->write_func(self
, &inst
, 1) < 0)
1850 if (self
->write_func(self
, module_str
, module_size
) < 0)
1853 if (self
->write_func(self
, "\n", 1) < 0)
1856 if (self
->write_func(self
, name_str
, name_size
) < 0)
1859 if (self
->write_func(self
, "\n", 1) < 0)
1862 else if (self
->write_func(self
, &obj
, 1) < 0) {
1866 if ((getstate_func
= PyObject_GetAttr(args
, __getstate___str
))) {
1867 state
= PyObject_Call(getstate_func
, empty_tuple
, NULL
);
1872 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
1877 if (!( state
= PyObject_GetAttr(args
, __dict___str
))) {
1878 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
1887 if (!PyDict_Check(state
)) {
1888 if (put2(self
, args
) < 0)
1892 if (put(self
, args
) < 0)
1896 if (save(self
, state
, 0) < 0)
1899 if (self
->write_func(self
, &build
, 1) < 0)
1905 if (self
->fast
&& !fast_save_leave(self
, args
))
1911 Py_XDECREF(getinitargs_func
);
1912 Py_XDECREF(getstate_func
);
1913 Py_XDECREF(class_args
);
1920 save_global(Picklerobject
*self
, PyObject
*args
, PyObject
*name
)
1922 PyObject
*global_name
= 0, *module
= 0, *mod
= 0, *klass
= 0;
1923 char *name_str
, *module_str
;
1924 int module_size
, name_size
, res
= -1;
1926 static char global
= GLOBAL
;
1930 Py_INCREF(global_name
);
1933 if (!( global_name
= PyObject_GetAttr(args
, __name___str
)))
1937 if (!( module
= whichmodule(args
, global_name
)))
1940 if ((module_size
= PyString_Size(module
)) < 0 ||
1941 (name_size
= PyString_Size(global_name
)) < 0)
1944 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1945 name_str
= PyString_AS_STRING((PyStringObject
*)global_name
);
1947 /* XXX This can be doing a relative import. Clearly it shouldn't,
1948 but I don't know how to stop it. :-( */
1949 mod
= PyImport_ImportModule(module_str
);
1951 cPickle_ErrFormat(PicklingError
,
1952 "Can't pickle %s: import of module %s "
1954 "OS", args
, module
);
1957 klass
= PyObject_GetAttrString(mod
, name_str
);
1958 if (klass
== NULL
) {
1959 cPickle_ErrFormat(PicklingError
,
1960 "Can't pickle %s: attribute lookup %s.%s "
1962 "OSS", args
, module
, global_name
);
1965 if (klass
!= args
) {
1967 cPickle_ErrFormat(PicklingError
,
1968 "Can't pickle %s: it's not the same object "
1970 "OSS", args
, module
, global_name
);
1975 if (self
->proto
>= 2) {
1976 /* See whether this is in the extension registry, and if
1977 * so generate an EXT opcode.
1979 PyObject
*py_code
; /* extension code as Python object */
1980 long code
; /* extension code as C value */
1984 PyTuple_SET_ITEM(two_tuple
, 0, module
);
1985 PyTuple_SET_ITEM(two_tuple
, 1, global_name
);
1986 py_code
= PyDict_GetItem(extension_registry
, two_tuple
);
1987 if (py_code
== NULL
)
1988 goto gen_global
; /* not registered */
1990 /* Verify py_code has the right type and value. */
1991 if (!PyInt_Check(py_code
)) {
1992 cPickle_ErrFormat(PicklingError
, "Can't pickle %s: "
1993 "extension code %s isn't an integer",
1994 "OO", args
, py_code
);
1997 code
= PyInt_AS_LONG(py_code
);
1998 if (code
<= 0 || code
> 0x7fffffffL
) {
1999 cPickle_ErrFormat(PicklingError
, "Can't pickle %s: "
2000 "extension code %ld is out of range",
2005 /* Generate an EXT opcode. */
2008 c_str
[1] = (char)code
;
2011 else if (code
<= 0xffff) {
2013 c_str
[1] = (char)(code
& 0xff);
2014 c_str
[2] = (char)((code
>> 8) & 0xff);
2019 c_str
[1] = (char)(code
& 0xff);
2020 c_str
[2] = (char)((code
>> 8) & 0xff);
2021 c_str
[3] = (char)((code
>> 16) & 0xff);
2022 c_str
[4] = (char)((code
>> 24) & 0xff);
2026 if (self
->write_func(self
, c_str
, n
) >= 0)
2028 goto finally
; /* and don't memoize */
2032 if (self
->write_func(self
, &global
, 1) < 0)
2035 if (self
->write_func(self
, module_str
, module_size
) < 0)
2038 if (self
->write_func(self
, "\n", 1) < 0)
2041 if (self
->write_func(self
, name_str
, name_size
) < 0)
2044 if (self
->write_func(self
, "\n", 1) < 0)
2047 if (put(self
, args
) < 0)
2054 Py_XDECREF(global_name
);
2061 save_pers(Picklerobject
*self
, PyObject
*args
, PyObject
*f
)
2066 static char persid
= PERSID
, binpersid
= BINPERSID
;
2069 ARG_TUP(self
, args
);
2071 pid
= PyObject_Call(f
, self
->arg
, NULL
);
2074 if (! pid
) return -1;
2076 if (pid
!= Py_None
) {
2078 if (!PyString_Check(pid
)) {
2079 PyErr_SetString(PicklingError
,
2080 "persistent id must be string");
2084 if (self
->write_func(self
, &persid
, 1) < 0)
2087 if ((size
= PyString_Size(pid
)) < 0)
2090 if (self
->write_func(self
,
2092 (PyStringObject
*)pid
),
2096 if (self
->write_func(self
, "\n", 1) < 0)
2102 else if (save(self
, pid
, 1) >= 0) {
2103 if (self
->write_func(self
, &binpersid
, 1) < 0)
2120 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2121 * appropriate __reduce__ method for ob.
2124 save_reduce(Picklerobject
*self
, PyObject
*args
, PyObject
*ob
)
2128 PyObject
*state
= NULL
;
2129 PyObject
*listitems
= NULL
;
2130 PyObject
*dictitems
= NULL
;
2132 int use_newobj
= self
->proto
>= 2;
2134 static char reduce
= REDUCE
;
2135 static char build
= BUILD
;
2136 static char newobj
= NEWOBJ
;
2138 if (! PyArg_UnpackTuple(args
, "save_reduce", 2, 5,
2146 if (!PyTuple_Check(argtup
)) {
2147 PyErr_SetString(PicklingError
,
2148 "args from reduce() should be a tuple");
2152 if (state
== Py_None
)
2154 if (listitems
== Py_None
)
2156 if (dictitems
== Py_None
)
2159 /* Protocol 2 special case: if callable's name is __newobj__, use
2160 * NEWOBJ. This consumes a lot of code.
2163 PyObject
*temp
= PyObject_GetAttr(callable
, __name___str
);
2166 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2173 use_newobj
= PyString_Check(temp
) &&
2174 strcmp(PyString_AS_STRING(temp
),
2181 PyObject
*newargtup
;
2184 /* Sanity checks. */
2185 n
= PyTuple_Size(argtup
);
2187 PyErr_SetString(PicklingError
, "__newobj__ arglist "
2192 cls
= PyTuple_GET_ITEM(argtup
, 0);
2193 if (! PyObject_HasAttrString(cls
, "__new__")) {
2194 PyErr_SetString(PicklingError
, "args[0] from "
2195 "__newobj__ args has no __new__");
2199 /* XXX How could ob be NULL? */
2201 PyObject
*ob_dot_class
;
2203 ob_dot_class
= PyObject_GetAttr(ob
, __class___str
);
2204 if (ob_dot_class
== NULL
) {
2205 if (PyErr_ExceptionMatches(
2206 PyExc_AttributeError
))
2211 i
= ob_dot_class
!= cls
; /* true iff a problem */
2212 Py_XDECREF(ob_dot_class
);
2214 PyErr_SetString(PicklingError
, "args[0] from "
2215 "__newobj__ args has the wrong class");
2220 /* Save the class and its __new__ arguments. */
2221 if (save(self
, cls
, 0) < 0)
2224 newargtup
= PyTuple_New(n
-1); /* argtup[1:] */
2225 if (newargtup
== NULL
)
2227 for (i
= 1; i
< n
; ++i
) {
2228 PyObject
*temp
= PyTuple_GET_ITEM(argtup
, i
);
2230 PyTuple_SET_ITEM(newargtup
, i
-1, temp
);
2232 i
= save(self
, newargtup
, 0) < 0;
2233 Py_DECREF(newargtup
);
2237 /* Add NEWOBJ opcode. */
2238 if (self
->write_func(self
, &newobj
, 1) < 0)
2242 /* Not using NEWOBJ. */
2243 if (save(self
, callable
, 0) < 0 ||
2244 save(self
, argtup
, 0) < 0 ||
2245 self
->write_func(self
, &reduce
, 1) < 0)
2250 /* XXX How can ob be NULL? */
2252 if (state
&& !PyDict_Check(state
)) {
2253 if (put2(self
, ob
) < 0)
2256 else if (put(self
, ob
) < 0)
2261 if (listitems
&& batch_list(self
, listitems
) < 0)
2264 if (dictitems
&& batch_dict(self
, dictitems
) < 0)
2268 if (save(self
, state
, 0) < 0 ||
2269 self
->write_func(self
, &build
, 1) < 0)
2277 save(Picklerobject
*self
, PyObject
*args
, int pers_save
)
2280 PyObject
*py_ob_id
= 0, *__reduce__
= 0, *t
= 0;
2285 if (self
->nesting
++ > Py_GetRecursionLimit()){
2286 PyErr_SetString(PyExc_RuntimeError
,
2287 "maximum recursion depth exceeded");
2291 if (!pers_save
&& self
->pers_func
) {
2292 if ((tmp
= save_pers(self
, args
, self
->pers_func
)) != 0) {
2298 if (args
== Py_None
) {
2299 res
= save_none(self
, args
);
2303 type
= args
->ob_type
;
2305 switch (type
->tp_name
[0]) {
2307 if (args
== Py_False
|| args
== Py_True
) {
2308 res
= save_bool(self
, args
);
2313 if (type
== &PyInt_Type
) {
2314 res
= save_int(self
, args
);
2320 if (type
== &PyLong_Type
) {
2321 res
= save_long(self
, args
);
2327 if (type
== &PyFloat_Type
) {
2328 res
= save_float(self
, args
);
2334 if (type
== &PyTuple_Type
&& PyTuple_Size(args
) == 0) {
2335 res
= save_tuple(self
, args
);
2341 if ((type
== &PyString_Type
) && (PyString_GET_SIZE(args
) < 2)) {
2342 res
= save_string(self
, args
, 0);
2346 #ifdef Py_USING_UNICODE
2348 if ((type
== &PyUnicode_Type
) && (PyString_GET_SIZE(args
) < 2)) {
2349 res
= save_unicode(self
, args
, 0);
2355 if (args
->ob_refcnt
> 1) {
2356 if (!( py_ob_id
= PyLong_FromVoidPtr(args
)))
2359 if (PyDict_GetItem(self
->memo
, py_ob_id
)) {
2360 if (get(self
, py_ob_id
) < 0)
2368 switch (type
->tp_name
[0]) {
2370 if (type
== &PyString_Type
) {
2371 res
= save_string(self
, args
, 1);
2376 #ifdef Py_USING_UNICODE
2378 if (type
== &PyUnicode_Type
) {
2379 res
= save_unicode(self
, args
, 1);
2386 if (type
== &PyTuple_Type
) {
2387 res
= save_tuple(self
, args
);
2390 if (type
== &PyType_Type
) {
2391 res
= save_global(self
, args
, NULL
);
2397 if (type
== &PyList_Type
) {
2398 res
= save_list(self
, args
);
2404 if (type
== &PyDict_Type
) {
2405 res
= save_dict(self
, args
);
2411 if (type
== &PyInstance_Type
) {
2412 res
= save_inst(self
, args
);
2418 if (type
== &PyClass_Type
) {
2419 res
= save_global(self
, args
, NULL
);
2425 if (type
== &PyFunction_Type
) {
2426 res
= save_global(self
, args
, NULL
);
2427 if (res
&& PyErr_ExceptionMatches(PickleError
)) {
2428 /* fall back to reduce */
2437 if (type
== &PyCFunction_Type
) {
2438 res
= save_global(self
, args
, NULL
);
2443 if (!pers_save
&& self
->inst_pers_func
) {
2444 if ((tmp
= save_pers(self
, args
, self
->inst_pers_func
)) != 0) {
2450 if (PyType_IsSubtype(type
, &PyType_Type
)) {
2451 res
= save_global(self
, args
, NULL
);
2455 /* Get a reduction callable, and call it. This may come from
2456 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2457 * or the object's __reduce__ method.
2459 __reduce__
= PyDict_GetItem(dispatch_table
, (PyObject
*)type
);
2460 if (__reduce__
!= NULL
) {
2461 Py_INCREF(__reduce__
);
2463 ARG_TUP(self
, args
);
2465 t
= PyObject_Call(__reduce__
, self
->arg
, NULL
);
2470 /* Check for a __reduce_ex__ method. */
2471 __reduce__
= PyObject_GetAttr(args
, __reduce_ex___str
);
2472 if (__reduce__
!= NULL
) {
2473 t
= PyInt_FromLong(self
->proto
);
2478 t
= PyObject_Call(__reduce__
,
2485 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2489 /* Check for a __reduce__ method. */
2490 __reduce__
= PyObject_GetAttr(args
, __reduce___str
);
2491 if (__reduce__
!= NULL
) {
2492 t
= PyObject_Call(__reduce__
,
2496 PyErr_SetObject(UnpickleableError
, args
);
2505 if (PyString_Check(t
)) {
2506 res
= save_global(self
, args
, t
);
2510 if (! PyTuple_Check(t
)) {
2511 cPickle_ErrFormat(PicklingError
, "Value returned by "
2512 "%s must be string or tuple",
2517 size
= PyTuple_Size(t
);
2518 if (size
< 2 || size
> 5) {
2519 cPickle_ErrFormat(PicklingError
, "tuple returned by "
2520 "%s must contain 2 through 5 elements",
2525 arg_tup
= PyTuple_GET_ITEM(t
, 1);
2526 if (!(PyTuple_Check(arg_tup
) || arg_tup
== Py_None
)) {
2527 cPickle_ErrFormat(PicklingError
, "Second element of "
2528 "tuple returned by %s must be a tuple",
2533 res
= save_reduce(self
, t
, args
);
2537 Py_XDECREF(py_ob_id
);
2538 Py_XDECREF(__reduce__
);
2546 dump(Picklerobject
*self
, PyObject
*args
)
2548 static char stop
= STOP
;
2550 if (self
->proto
>= 2) {
2554 assert(self
->proto
>= 0 && self
->proto
< 256);
2555 bytes
[1] = (char)self
->proto
;
2556 if (self
->write_func(self
, bytes
, 2) < 0)
2560 if (save(self
, args
, 0) < 0)
2563 if (self
->write_func(self
, &stop
, 1) < 0)
2566 if (self
->write_func(self
, NULL
, 0) < 0)
2573 Pickle_clear_memo(Picklerobject
*self
, PyObject
*args
)
2576 PyDict_Clear(self
->memo
);
2582 Pickle_getvalue(Picklerobject
*self
, PyObject
*args
)
2584 int l
, i
, rsize
, ssize
, clear
=1, lm
;
2587 char *s
, *p
, *have_get
;
2590 /* Can be called by Python code or C code */
2591 if (args
&& !PyArg_ParseTuple(args
, "|i:getvalue", &clear
))
2594 /* Check to make sure we are based on a list */
2595 if (! Pdata_Check(self
->file
)) {
2596 PyErr_SetString(PicklingError
,
2597 "Attempt to getvalue() a non-list-based pickler");
2601 /* flush write buffer */
2602 if (write_other(self
, NULL
, 0) < 0) return NULL
;
2604 data
=(Pdata
*)self
->file
;
2607 /* set up an array to hold get/put status */
2608 lm
= PyDict_Size(self
->memo
);
2609 if (lm
< 0) return NULL
;
2611 have_get
= malloc(lm
);
2612 if (have_get
== NULL
) return PyErr_NoMemory();
2613 memset(have_get
, 0, lm
);
2615 /* Scan for gets. */
2616 for (rsize
= 0, i
= l
; --i
>= 0; ) {
2619 if (PyString_Check(k
))
2620 rsize
+= PyString_GET_SIZE(k
);
2622 else if (PyInt_Check(k
)) { /* put */
2623 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2624 if (ik
>= lm
|| ik
== 0) {
2625 PyErr_SetString(PicklingError
,
2626 "Invalid get data");
2629 if (have_get
[ik
]) /* with matching get */
2630 rsize
+= ik
< 256 ? 2 : 5;
2633 else if (! (PyTuple_Check(k
) &&
2634 PyTuple_GET_SIZE(k
) == 2 &&
2635 PyInt_Check((k
= PyTuple_GET_ITEM(k
, 0))))
2637 PyErr_SetString(PicklingError
,
2638 "Unexpected data in internal list");
2643 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2644 if (ik
>= lm
|| ik
== 0) {
2645 PyErr_SetString(PicklingError
,
2646 "Invalid get data");
2650 rsize
+= ik
< 256 ? 2 : 5;
2654 /* Now generate the result */
2655 r
= PyString_FromStringAndSize(NULL
, rsize
);
2656 if (r
== NULL
) goto err
;
2657 s
= PyString_AS_STRING((PyStringObject
*)r
);
2659 for (i
= 0; i
< l
; i
++) {
2662 if (PyString_Check(k
)) {
2663 ssize
= PyString_GET_SIZE(k
);
2665 p
=PyString_AS_STRING((PyStringObject
*)k
);
2666 while (--ssize
>= 0)
2671 else if (PyTuple_Check(k
)) { /* get */
2672 ik
= PyInt_AS_LONG((PyIntObject
*)
2673 PyTuple_GET_ITEM(k
, 0));
2676 *s
++ = (int)(ik
& 0xff);
2680 *s
++ = (int)(ik
& 0xff);
2681 *s
++ = (int)((ik
>> 8) & 0xff);
2682 *s
++ = (int)((ik
>> 16) & 0xff);
2683 *s
++ = (int)((ik
>> 24) & 0xff);
2688 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2690 if (have_get
[ik
]) { /* with matching get */
2693 *s
++ = (int)(ik
& 0xff);
2697 *s
++ = (int)(ik
& 0xff);
2698 *s
++ = (int)((ik
>> 8) & 0xff);
2699 *s
++ = (int)((ik
>> 16) & 0xff);
2700 *s
++ = (int)((ik
>> 24) & 0xff);
2707 PyDict_Clear(self
->memo
);
2708 Pdata_clear(data
, 0);
2719 Pickler_dump(Picklerobject
*self
, PyObject
*args
)
2724 if (!( PyArg_ParseTuple(args
, "O|i:dump", &ob
, &get
)))
2727 if (dump(self
, ob
) < 0)
2730 if (get
) return Pickle_getvalue(self
, NULL
);
2732 /* XXX Why does dump() return self? */
2734 return (PyObject
*)self
;
2738 static struct PyMethodDef Pickler_methods
[] =
2740 {"dump", (PyCFunction
)Pickler_dump
, METH_VARARGS
,
2741 PyDoc_STR("dump(object) -- "
2742 "Write an object in pickle format to the object's pickle stream")},
2743 {"clear_memo", (PyCFunction
)Pickle_clear_memo
, METH_NOARGS
,
2744 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2745 {"getvalue", (PyCFunction
)Pickle_getvalue
, METH_VARARGS
,
2746 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2747 {NULL
, NULL
} /* sentinel */
2751 static Picklerobject
*
2752 newPicklerobject(PyObject
*file
, int proto
)
2754 Picklerobject
*self
;
2757 proto
= HIGHEST_PROTOCOL
;
2758 if (proto
> HIGHEST_PROTOCOL
) {
2759 PyErr_Format(PyExc_ValueError
, "pickle protocol %d asked for; "
2760 "the highest available protocol is %d",
2761 proto
, HIGHEST_PROTOCOL
);
2765 self
= PyObject_GC_New(Picklerobject
, &Picklertype
);
2768 self
->proto
= proto
;
2769 self
->bin
= proto
> 0;
2774 self
->pers_func
= NULL
;
2775 self
->inst_pers_func
= NULL
;
2776 self
->write_buf
= NULL
;
2779 self
->fast_container
= 0;
2780 self
->fast_memo
= NULL
;
2782 self
->dispatch_table
= NULL
;
2794 if (!( self
->memo
= PyDict_New()))
2797 if (PyFile_Check(file
)) {
2798 self
->fp
= PyFile_AsFile(file
);
2799 if (self
->fp
== NULL
) {
2800 PyErr_SetString(PyExc_ValueError
,
2801 "I/O operation on closed file");
2804 self
->write_func
= write_file
;
2806 else if (PycStringIO_OutputCheck(file
)) {
2807 self
->write_func
= write_cStringIO
;
2809 else if (file
== Py_None
) {
2810 self
->write_func
= write_none
;
2813 self
->write_func
= write_other
;
2815 if (! Pdata_Check(file
)) {
2816 self
->write
= PyObject_GetAttr(file
, write_str
);
2819 PyErr_SetString(PyExc_TypeError
,
2820 "argument must have 'write' "
2826 self
->write_buf
= (char *)PyMem_Malloc(WRITE_BUF_SIZE
);
2827 if (self
->write_buf
== NULL
) {
2833 if (PyEval_GetRestricted()) {
2834 /* Restricted execution, get private tables */
2835 PyObject
*m
= PyImport_Import(copy_reg_str
);
2839 self
->dispatch_table
= PyObject_GetAttr(m
, dispatch_table_str
);
2841 if (self
->dispatch_table
== NULL
)
2845 self
->dispatch_table
= dispatch_table
;
2846 Py_INCREF(dispatch_table
);
2848 PyObject_GC_Track(self
);
2859 get_Pickler(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2861 static const char *kwlist
[] = {"file", "protocol", NULL
};
2862 PyObject
*file
= NULL
;
2866 * The documented signature is Pickler(file, protocol=0), but this
2867 * accepts Pickler() and Pickler(integer) too. The meaning then
2868 * is clear as mud, undocumented, and not supported by pickle.py.
2869 * I'm told Zope uses this, but I haven't traced into this code
2870 * far enough to figure out what it means.
2872 if (!PyArg_ParseTuple(args
, "|i:Pickler", &proto
)) {
2875 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|i:Pickler",
2876 kwlist
, &file
, &proto
))
2879 return (PyObject
*)newPicklerobject(file
, proto
);
2884 Pickler_dealloc(Picklerobject
*self
)
2886 PyObject_GC_UnTrack(self
);
2887 Py_XDECREF(self
->write
);
2888 Py_XDECREF(self
->memo
);
2889 Py_XDECREF(self
->fast_memo
);
2890 Py_XDECREF(self
->arg
);
2891 Py_XDECREF(self
->file
);
2892 Py_XDECREF(self
->pers_func
);
2893 Py_XDECREF(self
->inst_pers_func
);
2894 Py_XDECREF(self
->dispatch_table
);
2895 PyMem_Free(self
->write_buf
);
2896 self
->ob_type
->tp_free((PyObject
*)self
);
2900 Pickler_traverse(Picklerobject
*self
, visitproc visit
, void *arg
)
2903 #define VISIT(SLOT) \
2905 err = visit((PyObject *)(SLOT), arg); \
2911 VISIT(self
->fast_memo
);
2914 VISIT(self
->pers_func
);
2915 VISIT(self
->inst_pers_func
);
2916 VISIT(self
->dispatch_table
);
2922 Pickler_clear(Picklerobject
*self
)
2924 #define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL;
2927 CLEAR(self
->fast_memo
);
2930 CLEAR(self
->pers_func
);
2931 CLEAR(self
->inst_pers_func
);
2932 CLEAR(self
->dispatch_table
);
2938 Pickler_get_pers_func(Picklerobject
*p
)
2940 if (p
->pers_func
== NULL
)
2941 PyErr_SetString(PyExc_AttributeError
, "persistent_id");
2943 Py_INCREF(p
->pers_func
);
2944 return p
->pers_func
;
2948 Pickler_set_pers_func(Picklerobject
*p
, PyObject
*v
)
2951 PyErr_SetString(PyExc_TypeError
,
2952 "attribute deletion is not supported");
2955 Py_XDECREF(p
->pers_func
);
2962 Pickler_set_inst_pers_func(Picklerobject
*p
, PyObject
*v
)
2965 PyErr_SetString(PyExc_TypeError
,
2966 "attribute deletion is not supported");
2969 Py_XDECREF(p
->inst_pers_func
);
2971 p
->inst_pers_func
= v
;
2976 Pickler_get_memo(Picklerobject
*p
)
2978 if (p
->memo
== NULL
)
2979 PyErr_SetString(PyExc_AttributeError
, "memo");
2986 Pickler_set_memo(Picklerobject
*p
, PyObject
*v
)
2989 PyErr_SetString(PyExc_TypeError
,
2990 "attribute deletion is not supported");
2993 if (!PyDict_Check(v
)) {
2994 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
2997 Py_XDECREF(p
->memo
);
3004 Pickler_get_error(Picklerobject
*p
)
3006 /* why is this an attribute on the Pickler? */
3007 Py_INCREF(PicklingError
);
3008 return PicklingError
;
3011 static PyMemberDef Pickler_members
[] = {
3012 {"binary", T_INT
, offsetof(Picklerobject
, bin
)},
3013 {"fast", T_INT
, offsetof(Picklerobject
, fast
)},
3017 static PyGetSetDef Pickler_getsets
[] = {
3018 {"persistent_id", (getter
)Pickler_get_pers_func
,
3019 (setter
)Pickler_set_pers_func
},
3020 {"inst_persistent_id", NULL
, (setter
)Pickler_set_inst_pers_func
},
3021 {"memo", (getter
)Pickler_get_memo
, (setter
)Pickler_set_memo
},
3022 {"PicklingError", (getter
)Pickler_get_error
, NULL
},
3026 PyDoc_STRVAR(Picklertype__doc__
,
3027 "Objects that know how to pickle objects\n");
3029 static PyTypeObject Picklertype
= {
3030 PyObject_HEAD_INIT(NULL
)
3032 "cPickle.Pickler", /*tp_name*/
3033 sizeof(Picklerobject
), /*tp_basicsize*/
3035 (destructor
)Pickler_dealloc
, /* tp_dealloc */
3041 0, /* tp_as_number */
3042 0, /* tp_as_sequence */
3043 0, /* tp_as_mapping */
3047 PyObject_GenericGetAttr
, /* tp_getattro */
3048 PyObject_GenericSetAttr
, /* tp_setattro */
3049 0, /* tp_as_buffer */
3050 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
3051 Picklertype__doc__
, /* tp_doc */
3052 (traverseproc
)Pickler_traverse
, /* tp_traverse */
3053 (inquiry
)Pickler_clear
, /* tp_clear */
3054 0, /* tp_richcompare */
3055 0, /* tp_weaklistoffset */
3057 0, /* tp_iternext */
3058 Pickler_methods
, /* tp_methods */
3059 Pickler_members
, /* tp_members */
3060 Pickler_getsets
, /* tp_getset */
3064 find_class(PyObject
*py_module_name
, PyObject
*py_global_name
, PyObject
*fc
)
3066 PyObject
*global
= 0, *module
;
3070 PyErr_SetString(UnpicklingError
, "Global and instance "
3071 "pickles are not supported.");
3074 return PyObject_CallFunction(fc
, "OO", py_module_name
,
3078 module
= PySys_GetObject("modules");
3082 module
= PyDict_GetItem(module
, py_module_name
);
3083 if (module
== NULL
) {
3084 module
= PyImport_Import(py_module_name
);
3087 global
= PyObject_GetAttr(module
, py_global_name
);
3091 global
= PyObject_GetAttr(module
, py_global_name
);
3096 marker(Unpicklerobject
*self
)
3098 if (self
->num_marks
< 1) {
3099 PyErr_SetString(UnpicklingError
, "could not find MARK");
3103 return self
->marks
[--self
->num_marks
];
3108 load_none(Unpicklerobject
*self
)
3110 PDATA_APPEND(self
->stack
, Py_None
, -1);
3117 PyErr_SetString(UnpicklingError
, "pickle data was truncated");
3122 load_int(Unpicklerobject
*self
)
3124 PyObject
*py_int
= 0;
3129 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3130 if (len
< 2) return bad_readline();
3131 if (!( s
=pystrndup(s
,len
))) return -1;
3134 l
= strtol(s
, &endptr
, 0);
3136 if (errno
|| (*endptr
!= '\n') || (endptr
[1] != '\0')) {
3137 /* Hm, maybe we've got something long. Let's try reading
3138 it as a Python long object. */
3140 py_int
= PyLong_FromString(s
, NULL
, 0);
3141 if (py_int
== NULL
) {
3142 PyErr_SetString(PyExc_ValueError
,
3143 "could not convert string to int");
3148 if (len
== 3 && (l
== 0 || l
== 1)) {
3149 if (!( py_int
= PyBool_FromLong(l
))) goto finally
;
3152 if (!( py_int
= PyInt_FromLong(l
))) goto finally
;
3157 PDATA_PUSH(self
->stack
, py_int
, -1);
3167 load_bool(Unpicklerobject
*self
, PyObject
*boolean
)
3169 assert(boolean
== Py_True
|| boolean
== Py_False
);
3170 PDATA_APPEND(self
->stack
, boolean
, -1);
3174 /* s contains x bytes of a little-endian integer. Return its value as a
3175 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3176 * int, but when x is 4 it's a signed one. This is an historical source
3177 * of x-platform bugs.
3180 calc_binint(char *s
, int x
)
3186 for (i
= 0, l
= 0L; i
< x
; i
++) {
3187 c
= (unsigned char)s
[i
];
3188 l
|= (long)c
<< (i
* 8);
3191 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3192 * is signed, so on a box with longs bigger than 4 bytes we need
3193 * to extend a BININT's sign bit to the full width.
3195 if (x
== 4 && l
& (1L << 31))
3203 load_binintx(Unpicklerobject
*self
, char *s
, int x
)
3205 PyObject
*py_int
= 0;
3208 l
= calc_binint(s
, x
);
3210 if (!( py_int
= PyInt_FromLong(l
)))
3213 PDATA_PUSH(self
->stack
, py_int
, -1);
3219 load_binint(Unpicklerobject
*self
)
3223 if (self
->read_func(self
, &s
, 4) < 0)
3226 return load_binintx(self
, s
, 4);
3231 load_binint1(Unpicklerobject
*self
)
3235 if (self
->read_func(self
, &s
, 1) < 0)
3238 return load_binintx(self
, s
, 1);
3243 load_binint2(Unpicklerobject
*self
)
3247 if (self
->read_func(self
, &s
, 2) < 0)
3250 return load_binintx(self
, s
, 2);
3254 load_long(Unpicklerobject
*self
)
3260 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3261 if (len
< 2) return bad_readline();
3262 if (!( s
=pystrndup(s
,len
))) return -1;
3264 if (!( l
= PyLong_FromString(s
, &end
, 0)))
3268 PDATA_PUSH(self
->stack
, l
, -1);
3277 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3281 load_counted_long(Unpicklerobject
*self
, int size
)
3285 unsigned char *pdata
;
3288 assert(size
== 1 || size
== 4);
3289 i
= self
->read_func(self
, &nbytes
, size
);
3290 if (i
< 0) return -1;
3292 size
= calc_binint(nbytes
, size
);
3294 /* Corrupt or hostile pickle -- we never write one like
3297 PyErr_SetString(UnpicklingError
, "LONG pickle has negative "
3303 along
= PyLong_FromLong(0L);
3305 /* Read the raw little-endian bytes & convert. */
3306 i
= self
->read_func(self
, (char **)&pdata
, size
);
3307 if (i
< 0) return -1;
3308 along
= _PyLong_FromByteArray(pdata
, (size_t)size
,
3309 1 /* little endian */, 1 /* signed */);
3313 PDATA_PUSH(self
->stack
, along
, -1);
3318 load_float(Unpicklerobject
*self
)
3320 PyObject
*py_float
= 0;
3325 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3326 if (len
< 2) return bad_readline();
3327 if (!( s
=pystrndup(s
,len
))) return -1;
3330 d
= PyOS_ascii_strtod(s
, &endptr
);
3332 if (errno
|| (endptr
[0] != '\n') || (endptr
[1] != '\0')) {
3333 PyErr_SetString(PyExc_ValueError
,
3334 "could not convert string to float");
3338 if (!( py_float
= PyFloat_FromDouble(d
)))
3342 PDATA_PUSH(self
->stack
, py_float
, -1);
3352 load_binfloat(Unpicklerobject
*self
)
3358 if (self
->read_func(self
, &p
, 8) < 0)
3361 x
= _PyFloat_Unpack8((unsigned char *)p
, 0);
3362 if (x
== -1.0 && PyErr_Occurred())
3365 py_float
= PyFloat_FromDouble(x
);
3366 if (py_float
== NULL
)
3369 PDATA_PUSH(self
->stack
, py_float
, -1);
3374 load_string(Unpicklerobject
*self
)
3380 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3381 if (len
< 2) return bad_readline();
3382 if (!( s
=pystrndup(s
,len
))) return -1;
3385 /* Strip outermost quotes */
3386 while (s
[len
-1] <= ' ')
3388 if(s
[0]=='"' && s
[len
-1]=='"'){
3392 } else if(s
[0]=='\'' && s
[len
-1]=='\''){
3398 /********************************************/
3400 str
= PyString_DecodeEscape(p
, len
, NULL
, 0, NULL
);
3402 PDATA_PUSH(self
->stack
, str
, -1);
3410 PyErr_SetString(PyExc_ValueError
,"insecure string pickle");
3416 load_binstring(Unpicklerobject
*self
)
3418 PyObject
*py_string
= 0;
3422 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3424 l
= calc_binint(s
, 4);
3426 if (self
->read_func(self
, &s
, l
) < 0)
3429 if (!( py_string
= PyString_FromStringAndSize(s
, l
)))
3432 PDATA_PUSH(self
->stack
, py_string
, -1);
3438 load_short_binstring(Unpicklerobject
*self
)
3440 PyObject
*py_string
= 0;
3444 if (self
->read_func(self
, &s
, 1) < 0)
3447 l
= (unsigned char)s
[0];
3449 if (self
->read_func(self
, &s
, l
) < 0) return -1;
3451 if (!( py_string
= PyString_FromStringAndSize(s
, l
))) return -1;
3453 PDATA_PUSH(self
->stack
, py_string
, -1);
3458 #ifdef Py_USING_UNICODE
3460 load_unicode(Unpicklerobject
*self
)
3466 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3467 if (len
< 1) return bad_readline();
3469 if (!( str
= PyUnicode_DecodeRawUnicodeEscape(s
, len
- 1, NULL
)))
3472 PDATA_PUSH(self
->stack
, str
, -1);
3481 #ifdef Py_USING_UNICODE
3483 load_binunicode(Unpicklerobject
*self
)
3489 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3491 l
= calc_binint(s
, 4);
3493 if (self
->read_func(self
, &s
, l
) < 0)
3496 if (!( unicode
= PyUnicode_DecodeUTF8(s
, l
, NULL
)))
3499 PDATA_PUSH(self
->stack
, unicode
, -1);
3506 load_tuple(Unpicklerobject
*self
)
3511 if ((i
= marker(self
)) < 0) return -1;
3512 if (!( tup
=Pdata_popTuple(self
->stack
, i
))) return -1;
3513 PDATA_PUSH(self
->stack
, tup
, -1);
3518 load_counted_tuple(Unpicklerobject
*self
, int len
)
3520 PyObject
*tup
= PyTuple_New(len
);
3525 while (--len
>= 0) {
3528 PDATA_POP(self
->stack
, element
);
3529 if (element
== NULL
)
3531 PyTuple_SET_ITEM(tup
, len
, element
);
3533 PDATA_PUSH(self
->stack
, tup
, -1);
3538 load_empty_list(Unpicklerobject
*self
)
3542 if (!( list
=PyList_New(0))) return -1;
3543 PDATA_PUSH(self
->stack
, list
, -1);
3548 load_empty_dict(Unpicklerobject
*self
)
3552 if (!( dict
=PyDict_New())) return -1;
3553 PDATA_PUSH(self
->stack
, dict
, -1);
3559 load_list(Unpicklerobject
*self
)
3564 if ((i
= marker(self
)) < 0) return -1;
3565 if (!( list
=Pdata_popList(self
->stack
, i
))) return -1;
3566 PDATA_PUSH(self
->stack
, list
, -1);
3571 load_dict(Unpicklerobject
*self
)
3573 PyObject
*dict
, *key
, *value
;
3576 if ((i
= marker(self
)) < 0) return -1;
3577 j
=self
->stack
->length
;
3579 if (!( dict
= PyDict_New())) return -1;
3581 for (k
= i
+1; k
< j
; k
+= 2) {
3582 key
=self
->stack
->data
[k
-1];
3583 value
=self
->stack
->data
[k
];
3584 if (PyDict_SetItem(dict
, key
, value
) < 0) {
3589 Pdata_clear(self
->stack
, i
);
3590 PDATA_PUSH(self
->stack
, dict
, -1);
3595 Instance_New(PyObject
*cls
, PyObject
*args
)
3599 if (PyClass_Check(cls
)) {
3602 if ((l
=PyObject_Size(args
)) < 0) goto err
;
3604 PyObject
*__getinitargs__
;
3606 __getinitargs__
= PyObject_GetAttr(cls
,
3607 __getinitargs___str
);
3608 if (!__getinitargs__
) {
3609 /* We have a class with no __getinitargs__,
3610 so bypass usual construction */
3614 if (!( inst
=PyInstance_NewRaw(cls
, NULL
)))
3618 Py_DECREF(__getinitargs__
);
3621 if ((r
=PyInstance_New(cls
, args
, NULL
))) return r
;
3625 if ((r
=PyObject_CallObject(cls
, args
))) return r
;
3629 PyObject
*tp
, *v
, *tb
;
3631 PyErr_Fetch(&tp
, &v
, &tb
);
3632 if ((r
=PyTuple_Pack(3,v
,cls
,args
))) {
3636 PyErr_Restore(tp
,v
,tb
);
3643 load_obj(Unpicklerobject
*self
)
3645 PyObject
*class, *tup
, *obj
=0;
3648 if ((i
= marker(self
)) < 0) return -1;
3649 if (!( tup
=Pdata_popTuple(self
->stack
, i
+1))) return -1;
3650 PDATA_POP(self
->stack
, class);
3652 obj
= Instance_New(class, tup
);
3657 if (! obj
) return -1;
3658 PDATA_PUSH(self
->stack
, obj
, -1);
3664 load_inst(Unpicklerobject
*self
)
3666 PyObject
*tup
, *class=0, *obj
=0, *module_name
, *class_name
;
3670 if ((i
= marker(self
)) < 0) return -1;
3672 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3673 if (len
< 2) return bad_readline();
3674 module_name
= PyString_FromStringAndSize(s
, len
- 1);
3675 if (!module_name
) return -1;
3677 if ((len
= self
->readline_func(self
, &s
)) >= 0) {
3678 if (len
< 2) return bad_readline();
3679 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3680 class = find_class(module_name
, class_name
,
3682 Py_DECREF(class_name
);
3685 Py_DECREF(module_name
);
3687 if (! class) return -1;
3689 if ((tup
=Pdata_popTuple(self
->stack
, i
))) {
3690 obj
= Instance_New(class, tup
);
3695 if (! obj
) return -1;
3697 PDATA_PUSH(self
->stack
, obj
, -1);
3702 load_newobj(Unpicklerobject
*self
)
3704 PyObject
*args
= NULL
;
3705 PyObject
*clsraw
= NULL
;
3706 PyTypeObject
*cls
; /* clsraw cast to its true type */
3709 /* Stack is ... cls argtuple, and we want to call
3710 * cls.__new__(cls, *argtuple).
3712 PDATA_POP(self
->stack
, args
);
3713 if (args
== NULL
) goto Fail
;
3714 if (! PyTuple_Check(args
)) {
3715 PyErr_SetString(UnpicklingError
, "NEWOBJ expected an arg "
3720 PDATA_POP(self
->stack
, clsraw
);
3721 cls
= (PyTypeObject
*)clsraw
;
3722 if (cls
== NULL
) goto Fail
;
3723 if (! PyType_Check(cls
)) {
3724 PyErr_SetString(UnpicklingError
, "NEWOBJ class argument "
3725 "isn't a type object");
3728 if (cls
->tp_new
== NULL
) {
3729 PyErr_SetString(UnpicklingError
, "NEWOBJ class argument "
3735 obj
= cls
->tp_new(cls
, args
, NULL
);
3736 if (obj
== NULL
) goto Fail
;
3740 PDATA_PUSH(self
->stack
, obj
, -1);
3750 load_global(Unpicklerobject
*self
)
3752 PyObject
*class = 0, *module_name
= 0, *class_name
= 0;
3756 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3757 if (len
< 2) return bad_readline();
3758 module_name
= PyString_FromStringAndSize(s
, len
- 1);
3759 if (!module_name
) return -1;
3761 if ((len
= self
->readline_func(self
, &s
)) >= 0) {
3763 Py_DECREF(module_name
);
3764 return bad_readline();
3766 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3767 class = find_class(module_name
, class_name
,
3769 Py_DECREF(class_name
);
3772 Py_DECREF(module_name
);
3774 if (! class) return -1;
3775 PDATA_PUSH(self
->stack
, class, -1);
3781 load_persid(Unpicklerobject
*self
)
3787 if (self
->pers_func
) {
3788 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3789 if (len
< 2) return bad_readline();
3791 pid
= PyString_FromStringAndSize(s
, len
- 1);
3792 if (!pid
) return -1;
3794 if (PyList_Check(self
->pers_func
)) {
3795 if (PyList_Append(self
->pers_func
, pid
) < 0) {
3803 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
3809 if (! pid
) return -1;
3811 PDATA_PUSH(self
->stack
, pid
, -1);
3815 PyErr_SetString(UnpicklingError
,
3816 "A load persistent id instruction was encountered,\n"
3817 "but no persistent_load function was specified.");
3823 load_binpersid(Unpicklerobject
*self
)
3827 if (self
->pers_func
) {
3828 PDATA_POP(self
->stack
, pid
);
3829 if (! pid
) return -1;
3831 if (PyList_Check(self
->pers_func
)) {
3832 if (PyList_Append(self
->pers_func
, pid
) < 0) {
3840 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
3844 if (! pid
) return -1;
3847 PDATA_PUSH(self
->stack
, pid
, -1);
3851 PyErr_SetString(UnpicklingError
,
3852 "A load persistent id instruction was encountered,\n"
3853 "but no persistent_load function was specified.");
3860 load_pop(Unpicklerobject
*self
)
3864 if (!( (len
=self
->stack
->length
) > 0 )) return stackUnderflow();
3866 /* Note that we split the (pickle.py) stack into two stacks,
3867 an object stack and a mark stack. We have to be clever and
3868 pop the right one. We do this by looking at the top of the
3872 if ((self
->num_marks
> 0) &&
3873 (self
->marks
[self
->num_marks
- 1] == len
))
3877 Py_DECREF(self
->stack
->data
[len
]);
3878 self
->stack
->length
=len
;
3886 load_pop_mark(Unpicklerobject
*self
)
3890 if ((i
= marker(self
)) < 0)
3893 Pdata_clear(self
->stack
, i
);
3900 load_dup(Unpicklerobject
*self
)
3905 if ((len
= self
->stack
->length
) <= 0) return stackUnderflow();
3906 last
=self
->stack
->data
[len
-1];
3908 PDATA_PUSH(self
->stack
, last
, -1);
3914 load_get(Unpicklerobject
*self
)
3916 PyObject
*py_str
= 0, *value
= 0;
3921 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3922 if (len
< 2) return bad_readline();
3924 if (!( py_str
= PyString_FromStringAndSize(s
, len
- 1))) return -1;
3926 value
= PyDict_GetItem(self
->memo
, py_str
);
3928 PyErr_SetObject(BadPickleGet
, py_str
);
3932 PDATA_APPEND(self
->stack
, value
, -1);
3942 load_binget(Unpicklerobject
*self
)
3944 PyObject
*py_key
= 0, *value
= 0;
3949 if (self
->read_func(self
, &s
, 1) < 0) return -1;
3951 key
= (unsigned char)s
[0];
3952 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
3954 value
= PyDict_GetItem(self
->memo
, py_key
);
3956 PyErr_SetObject(BadPickleGet
, py_key
);
3960 PDATA_APPEND(self
->stack
, value
, -1);
3970 load_long_binget(Unpicklerobject
*self
)
3972 PyObject
*py_key
= 0, *value
= 0;
3978 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3980 c
= (unsigned char)s
[0];
3982 c
= (unsigned char)s
[1];
3983 key
|= (long)c
<< 8;
3984 c
= (unsigned char)s
[2];
3985 key
|= (long)c
<< 16;
3986 c
= (unsigned char)s
[3];
3987 key
|= (long)c
<< 24;
3989 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
3991 value
= PyDict_GetItem(self
->memo
, py_key
);
3993 PyErr_SetObject(BadPickleGet
, py_key
);
3997 PDATA_APPEND(self
->stack
, value
, -1);
4005 /* Push an object from the extension registry (EXT[124]). nbytes is
4006 * the number of bytes following the opcode, holding the index (code) value.
4009 load_extension(Unpicklerobject
*self
, int nbytes
)
4011 char *codebytes
; /* the nbytes bytes after the opcode */
4012 long code
; /* calc_binint returns long */
4013 PyObject
*py_code
; /* code as a Python int */
4014 PyObject
*obj
; /* the object to push */
4015 PyObject
*pair
; /* (module_name, class_name) */
4016 PyObject
*module_name
, *class_name
;
4018 assert(nbytes
== 1 || nbytes
== 2 || nbytes
== 4);
4019 if (self
->read_func(self
, &codebytes
, nbytes
) < 0) return -1;
4020 code
= calc_binint(codebytes
, nbytes
);
4021 if (code
<= 0) { /* note that 0 is forbidden */
4022 /* Corrupt or hostile pickle. */
4023 PyErr_SetString(UnpicklingError
, "EXT specifies code <= 0");
4027 /* Look for the code in the cache. */
4028 py_code
= PyInt_FromLong(code
);
4029 if (py_code
== NULL
) return -1;
4030 obj
= PyDict_GetItem(extension_cache
, py_code
);
4034 PDATA_APPEND(self
->stack
, obj
, -1);
4038 /* Look up the (module_name, class_name) pair. */
4039 pair
= PyDict_GetItem(inverted_registry
, py_code
);
4042 PyErr_Format(PyExc_ValueError
, "unregistered extension "
4046 /* Since the extension registry is manipulable via Python code,
4047 * confirm that pair is really a 2-tuple of strings.
4049 if (!PyTuple_Check(pair
) || PyTuple_Size(pair
) != 2 ||
4050 !PyString_Check(module_name
= PyTuple_GET_ITEM(pair
, 0)) ||
4051 !PyString_Check(class_name
= PyTuple_GET_ITEM(pair
, 1))) {
4053 PyErr_Format(PyExc_ValueError
, "_inverted_registry[%ld] "
4054 "isn't a 2-tuple of strings", code
);
4057 /* Load the object. */
4058 obj
= find_class(module_name
, class_name
, self
->find_class
);
4063 /* Cache code -> obj. */
4064 code
= PyDict_SetItem(extension_cache
, py_code
, obj
);
4070 PDATA_PUSH(self
->stack
, obj
, -1);
4075 load_put(Unpicklerobject
*self
)
4077 PyObject
*py_str
= 0, *value
= 0;
4081 if ((l
= self
->readline_func(self
, &s
)) < 0) return -1;
4082 if (l
< 2) return bad_readline();
4083 if (!( len
=self
->stack
->length
)) return stackUnderflow();
4084 if (!( py_str
= PyString_FromStringAndSize(s
, l
- 1))) return -1;
4085 value
=self
->stack
->data
[len
-1];
4086 l
=PyDict_SetItem(self
->memo
, py_str
, value
);
4093 load_binput(Unpicklerobject
*self
)
4095 PyObject
*py_key
= 0, *value
= 0;
4100 if (self
->read_func(self
, &s
, 1) < 0) return -1;
4101 if (!( (len
=self
->stack
->length
) > 0 )) return stackUnderflow();
4103 key
= (unsigned char)s
[0];
4105 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
4106 value
=self
->stack
->data
[len
-1];
4107 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
4114 load_long_binput(Unpicklerobject
*self
)
4116 PyObject
*py_key
= 0, *value
= 0;
4122 if (self
->read_func(self
, &s
, 4) < 0) return -1;
4123 if (!( len
=self
->stack
->length
)) return stackUnderflow();
4125 c
= (unsigned char)s
[0];
4127 c
= (unsigned char)s
[1];
4128 key
|= (long)c
<< 8;
4129 c
= (unsigned char)s
[2];
4130 key
|= (long)c
<< 16;
4131 c
= (unsigned char)s
[3];
4132 key
|= (long)c
<< 24;
4134 if (!( py_key
= PyInt_FromLong(key
))) return -1;
4135 value
=self
->stack
->data
[len
-1];
4136 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
4143 do_append(Unpicklerobject
*self
, int x
)
4145 PyObject
*value
= 0, *list
= 0, *append_method
= 0;
4148 len
=self
->stack
->length
;
4149 if (!( len
>= x
&& x
> 0 )) return stackUnderflow();
4151 if (len
==x
) return 0;
4153 list
=self
->stack
->data
[x
-1];
4155 if (PyList_Check(list
)) {
4159 slice
=Pdata_popList(self
->stack
, x
);
4160 list_len
= PyList_GET_SIZE(list
);
4161 i
=PyList_SetSlice(list
, list_len
, list_len
, slice
);
4167 if (!( append_method
= PyObject_GetAttr(list
, append_str
)))
4170 for (i
= x
; i
< len
; i
++) {
4173 value
=self
->stack
->data
[i
];
4175 ARG_TUP(self
, value
);
4177 junk
= PyObject_Call(append_method
, self
->arg
,
4182 Pdata_clear(self
->stack
, i
+1);
4183 self
->stack
->length
=x
;
4184 Py_DECREF(append_method
);
4189 self
->stack
->length
=x
;
4190 Py_DECREF(append_method
);
4198 load_append(Unpicklerobject
*self
)
4200 return do_append(self
, self
->stack
->length
- 1);
4205 load_appends(Unpicklerobject
*self
)
4207 return do_append(self
, marker(self
));
4212 do_setitems(Unpicklerobject
*self
, int x
)
4214 PyObject
*value
= 0, *key
= 0, *dict
= 0;
4217 if (!( (len
=self
->stack
->length
) >= x
4218 && x
> 0 )) return stackUnderflow();
4220 dict
=self
->stack
->data
[x
-1];
4222 for (i
= x
+1; i
< len
; i
+= 2) {
4223 key
=self
->stack
->data
[i
-1];
4224 value
=self
->stack
->data
[i
];
4225 if (PyObject_SetItem(dict
, key
, value
) < 0) {
4231 Pdata_clear(self
->stack
, x
);
4238 load_setitem(Unpicklerobject
*self
)
4240 return do_setitems(self
, self
->stack
->length
- 2);
4244 load_setitems(Unpicklerobject
*self
)
4246 return do_setitems(self
, marker(self
));
4251 load_build(Unpicklerobject
*self
)
4253 PyObject
*state
, *inst
, *slotstate
;
4254 PyObject
*__setstate__
;
4255 PyObject
*d_key
, *d_value
;
4259 /* Stack is ... instance, state. We want to leave instance at
4260 * the stack top, possibly mutated via instance.__setstate__(state).
4262 if (self
->stack
->length
< 2)
4263 return stackUnderflow();
4264 PDATA_POP(self
->stack
, state
);
4267 inst
= self
->stack
->data
[self
->stack
->length
- 1];
4269 __setstate__
= PyObject_GetAttr(inst
, __setstate___str
);
4270 if (__setstate__
!= NULL
) {
4271 PyObject
*junk
= NULL
;
4273 /* The explicit __setstate__ is responsible for everything. */
4274 ARG_TUP(self
, state
);
4276 junk
= PyObject_Call(__setstate__
, self
->arg
, NULL
);
4279 Py_DECREF(__setstate__
);
4285 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4289 /* A default __setstate__. First see whether state embeds a
4290 * slot state dict too (a proto 2 addition).
4292 if (PyTuple_Check(state
) && PyTuple_Size(state
) == 2) {
4293 PyObject
*temp
= state
;
4294 state
= PyTuple_GET_ITEM(temp
, 0);
4295 slotstate
= PyTuple_GET_ITEM(temp
, 1);
4297 Py_INCREF(slotstate
);
4303 /* Set inst.__dict__ from the state dict (if any). */
4304 if (state
!= Py_None
) {
4306 if (! PyDict_Check(state
)) {
4307 PyErr_SetString(UnpicklingError
, "state is not a "
4311 dict
= PyObject_GetAttr(inst
, __dict___str
);
4316 while (PyDict_Next(state
, &i
, &d_key
, &d_value
)) {
4317 if (PyObject_SetItem(dict
, d_key
, d_value
) < 0)
4323 /* Also set instance attributes from the slotstate dict (if any). */
4324 if (slotstate
!= NULL
) {
4325 if (! PyDict_Check(slotstate
)) {
4326 PyErr_SetString(UnpicklingError
, "slot state is not "
4331 while (PyDict_Next(slotstate
, &i
, &d_key
, &d_value
)) {
4332 if (PyObject_SetAttr(inst
, d_key
, d_value
) < 0)
4340 Py_XDECREF(slotstate
);
4346 load_mark(Unpicklerobject
*self
)
4350 /* Note that we split the (pickle.py) stack into two stacks, an
4351 object stack and a mark stack. Here we push a mark onto the
4355 if ((self
->num_marks
+ 1) >= self
->marks_size
) {
4356 s
=self
->marks_size
+20;
4357 if (s
<= self
->num_marks
) s
=self
->num_marks
+ 1;
4358 if (self
->marks
== NULL
)
4359 self
->marks
=(int *)malloc(s
* sizeof(int));
4361 self
->marks
=(int *)realloc(self
->marks
,
4363 if (! self
->marks
) {
4367 self
->marks_size
= s
;
4370 self
->marks
[self
->num_marks
++] = self
->stack
->length
;
4376 load_reduce(Unpicklerobject
*self
)
4378 PyObject
*callable
= 0, *arg_tup
= 0, *ob
= 0;
4380 PDATA_POP(self
->stack
, arg_tup
);
4381 if (! arg_tup
) return -1;
4382 PDATA_POP(self
->stack
, callable
);
4384 ob
= Instance_New(callable
, arg_tup
);
4385 Py_DECREF(callable
);
4389 if (! ob
) return -1;
4391 PDATA_PUSH(self
->stack
, ob
, -1);
4395 /* Just raises an error if we don't know the protocol specified. PROTO
4396 * is the first opcode for protocols >= 2.
4399 load_proto(Unpicklerobject
*self
)
4404 i
= self
->read_func(self
, &protobyte
, 1);
4408 i
= calc_binint(protobyte
, 1);
4409 /* No point checking for < 0, since calc_binint returns an unsigned
4410 * int when chewing on 1 byte.
4413 if (i
<= HIGHEST_PROTOCOL
)
4416 PyErr_Format(PyExc_ValueError
, "unsupported pickle protocol: %d", i
);
4421 load(Unpicklerobject
*self
)
4423 PyObject
*err
= 0, *val
= 0;
4426 self
->num_marks
= 0;
4427 if (self
->stack
->length
) Pdata_clear(self
->stack
, 0);
4430 if (self
->read_func(self
, &s
, 1) < 0)
4435 if (load_none(self
) < 0)
4440 if (load_binint(self
) < 0)
4445 if (load_binint1(self
) < 0)
4450 if (load_binint2(self
) < 0)
4455 if (load_int(self
) < 0)
4460 if (load_long(self
) < 0)
4465 if (load_counted_long(self
, 1) < 0)
4470 if (load_counted_long(self
, 4) < 0)
4475 if (load_float(self
) < 0)
4480 if (load_binfloat(self
) < 0)
4485 if (load_binstring(self
) < 0)
4489 case SHORT_BINSTRING
:
4490 if (load_short_binstring(self
) < 0)
4495 if (load_string(self
) < 0)
4499 #ifdef Py_USING_UNICODE
4501 if (load_unicode(self
) < 0)
4506 if (load_binunicode(self
) < 0)
4512 if (load_counted_tuple(self
, 0) < 0)
4517 if (load_counted_tuple(self
, 1) < 0)
4522 if (load_counted_tuple(self
, 2) < 0)
4527 if (load_counted_tuple(self
, 3) < 0)
4532 if (load_tuple(self
) < 0)
4537 if (load_empty_list(self
) < 0)
4542 if (load_list(self
) < 0)
4547 if (load_empty_dict(self
) < 0)
4552 if (load_dict(self
) < 0)
4557 if (load_obj(self
) < 0)
4562 if (load_inst(self
) < 0)
4567 if (load_newobj(self
) < 0)
4572 if (load_global(self
) < 0)
4577 if (load_append(self
) < 0)
4582 if (load_appends(self
) < 0)
4587 if (load_build(self
) < 0)
4592 if (load_dup(self
) < 0)
4597 if (load_binget(self
) < 0)
4602 if (load_long_binget(self
) < 0)
4607 if (load_get(self
) < 0)
4612 if (load_extension(self
, 1) < 0)
4617 if (load_extension(self
, 2) < 0)
4622 if (load_extension(self
, 4) < 0)
4626 if (load_mark(self
) < 0)
4631 if (load_binput(self
) < 0)
4636 if (load_long_binput(self
) < 0)
4641 if (load_put(self
) < 0)
4646 if (load_pop(self
) < 0)
4651 if (load_pop_mark(self
) < 0)
4656 if (load_setitem(self
) < 0)
4661 if (load_setitems(self
) < 0)
4669 if (load_persid(self
) < 0)
4674 if (load_binpersid(self
) < 0)
4679 if (load_reduce(self
) < 0)
4684 if (load_proto(self
) < 0)
4689 if (load_bool(self
, Py_True
) < 0)
4694 if (load_bool(self
, Py_False
) < 0)
4700 PyErr_SetNone(PyExc_EOFError
);
4704 cPickle_ErrFormat(UnpicklingError
,
4705 "invalid load key, '%s'.",
4713 if ((err
= PyErr_Occurred())) {
4714 if (err
== PyExc_EOFError
) {
4715 PyErr_SetNone(PyExc_EOFError
);
4720 PDATA_POP(self
->stack
, val
);
4725 /* No-load functions to support noload, which is used to
4726 find persistent references. */
4729 noload_obj(Unpicklerobject
*self
)
4733 if ((i
= marker(self
)) < 0) return -1;
4734 return Pdata_clear(self
->stack
, i
+1);
4739 noload_inst(Unpicklerobject
*self
)
4744 if ((i
= marker(self
)) < 0) return -1;
4745 Pdata_clear(self
->stack
, i
);
4746 if (self
->readline_func(self
, &s
) < 0) return -1;
4747 if (self
->readline_func(self
, &s
) < 0) return -1;
4748 PDATA_APPEND(self
->stack
, Py_None
, -1);
4753 noload_newobj(Unpicklerobject
*self
)
4757 PDATA_POP(self
->stack
, obj
); /* pop argtuple */
4758 if (obj
== NULL
) return -1;
4761 PDATA_POP(self
->stack
, obj
); /* pop cls */
4762 if (obj
== NULL
) return -1;
4765 PDATA_APPEND(self
->stack
, Py_None
, -1);
4770 noload_global(Unpicklerobject
*self
)
4774 if (self
->readline_func(self
, &s
) < 0) return -1;
4775 if (self
->readline_func(self
, &s
) < 0) return -1;
4776 PDATA_APPEND(self
->stack
, Py_None
,-1);
4781 noload_reduce(Unpicklerobject
*self
)
4784 if (self
->stack
->length
< 2) return stackUnderflow();
4785 Pdata_clear(self
->stack
, self
->stack
->length
-2);
4786 PDATA_APPEND(self
->stack
, Py_None
,-1);
4791 noload_build(Unpicklerobject
*self
) {
4793 if (self
->stack
->length
< 1) return stackUnderflow();
4794 Pdata_clear(self
->stack
, self
->stack
->length
-1);
4799 noload_extension(Unpicklerobject
*self
, int nbytes
)
4803 assert(nbytes
== 1 || nbytes
== 2 || nbytes
== 4);
4804 if (self
->read_func(self
, &codebytes
, nbytes
) < 0) return -1;
4805 PDATA_APPEND(self
->stack
, Py_None
, -1);
4811 noload(Unpicklerobject
*self
)
4813 PyObject
*err
= 0, *val
= 0;
4816 self
->num_marks
= 0;
4817 Pdata_clear(self
->stack
, 0);
4820 if (self
->read_func(self
, &s
, 1) < 0)
4825 if (load_none(self
) < 0)
4830 if (load_binint(self
) < 0)
4835 if (load_binint1(self
) < 0)
4840 if (load_binint2(self
) < 0)
4845 if (load_int(self
) < 0)
4850 if (load_long(self
) < 0)
4855 if (load_counted_long(self
, 1) < 0)
4860 if (load_counted_long(self
, 4) < 0)
4865 if (load_float(self
) < 0)
4870 if (load_binfloat(self
) < 0)
4875 if (load_binstring(self
) < 0)
4879 case SHORT_BINSTRING
:
4880 if (load_short_binstring(self
) < 0)
4885 if (load_string(self
) < 0)
4889 #ifdef Py_USING_UNICODE
4891 if (load_unicode(self
) < 0)
4896 if (load_binunicode(self
) < 0)
4902 if (load_counted_tuple(self
, 0) < 0)
4907 if (load_counted_tuple(self
, 1) < 0)
4912 if (load_counted_tuple(self
, 2) < 0)
4917 if (load_counted_tuple(self
, 3) < 0)
4922 if (load_tuple(self
) < 0)
4927 if (load_empty_list(self
) < 0)
4932 if (load_list(self
) < 0)
4937 if (load_empty_dict(self
) < 0)
4942 if (load_dict(self
) < 0)
4947 if (noload_obj(self
) < 0)
4952 if (noload_inst(self
) < 0)
4957 if (noload_newobj(self
) < 0)
4962 if (noload_global(self
) < 0)
4967 if (load_append(self
) < 0)
4972 if (load_appends(self
) < 0)
4977 if (noload_build(self
) < 0)
4982 if (load_dup(self
) < 0)
4987 if (load_binget(self
) < 0)
4992 if (load_long_binget(self
) < 0)
4997 if (load_get(self
) < 0)
5002 if (noload_extension(self
, 1) < 0)
5007 if (noload_extension(self
, 2) < 0)
5012 if (noload_extension(self
, 4) < 0)
5017 if (load_mark(self
) < 0)
5022 if (load_binput(self
) < 0)
5027 if (load_long_binput(self
) < 0)
5032 if (load_put(self
) < 0)
5037 if (load_pop(self
) < 0)
5042 if (load_pop_mark(self
) < 0)
5047 if (load_setitem(self
) < 0)
5052 if (load_setitems(self
) < 0)
5060 if (load_persid(self
) < 0)
5065 if (load_binpersid(self
) < 0)
5070 if (noload_reduce(self
) < 0)
5075 if (load_proto(self
) < 0)
5080 if (load_bool(self
, Py_True
) < 0)
5085 if (load_bool(self
, Py_False
) < 0)
5089 cPickle_ErrFormat(UnpicklingError
,
5090 "invalid load key, '%s'.",
5098 if ((err
= PyErr_Occurred())) {
5099 if (err
== PyExc_EOFError
) {
5100 PyErr_SetNone(PyExc_EOFError
);
5105 PDATA_POP(self
->stack
, val
);
5111 Unpickler_load(Unpicklerobject
*self
, PyObject
*args
)
5113 if (!( PyArg_ParseTuple(args
, ":load")))
5120 Unpickler_noload(Unpicklerobject
*self
, PyObject
*args
)
5122 if (!( PyArg_ParseTuple(args
, ":noload")))
5125 return noload(self
);
5129 static struct PyMethodDef Unpickler_methods
[] = {
5130 {"load", (PyCFunction
)Unpickler_load
, METH_VARARGS
,
5131 PyDoc_STR("load() -- Load a pickle")
5133 {"noload", (PyCFunction
)Unpickler_noload
, METH_VARARGS
,
5135 "noload() -- not load a pickle, but go through most of the motions\n"
5137 "This function can be used to read past a pickle without instantiating\n"
5138 "any objects or importing any modules. It can also be used to find all\n"
5139 "persistent references without instantiating any objects or importing\n"
5142 {NULL
, NULL
} /* sentinel */
5146 static Unpicklerobject
*
5147 newUnpicklerobject(PyObject
*f
)
5149 Unpicklerobject
*self
;
5151 if (!( self
= PyObject_GC_New(Unpicklerobject
, &Unpicklertype
)))
5156 self
->stack
= (Pdata
*)Pdata_New();
5157 self
->pers_func
= NULL
;
5158 self
->last_string
= NULL
;
5160 self
->num_marks
= 0;
5161 self
->marks_size
= 0;
5164 self
->readline
= NULL
;
5165 self
->find_class
= NULL
;
5167 if (!( self
->memo
= PyDict_New()))
5173 /* Set read, readline based on type of f */
5174 if (PyFile_Check(f
)) {
5175 self
->fp
= PyFile_AsFile(f
);
5176 if (self
->fp
== NULL
) {
5177 PyErr_SetString(PyExc_ValueError
,
5178 "I/O operation on closed file");
5181 self
->read_func
= read_file
;
5182 self
->readline_func
= readline_file
;
5184 else if (PycStringIO_InputCheck(f
)) {
5186 self
->read_func
= read_cStringIO
;
5187 self
->readline_func
= readline_cStringIO
;
5192 self
->read_func
= read_other
;
5193 self
->readline_func
= readline_other
;
5195 if (!( (self
->readline
= PyObject_GetAttr(f
, readline_str
)) &&
5196 (self
->read
= PyObject_GetAttr(f
, read_str
)))) {
5198 PyErr_SetString( PyExc_TypeError
,
5199 "argument must have 'read' and "
5200 "'readline' attributes" );
5204 PyObject_GC_Track(self
);
5209 Py_DECREF((PyObject
*)self
);
5215 get_Unpickler(PyObject
*self
, PyObject
*args
)
5219 if (!( PyArg_ParseTuple(args
, "O:Unpickler", &file
)))
5221 return (PyObject
*)newUnpicklerobject(file
);
5226 Unpickler_dealloc(Unpicklerobject
*self
)
5228 PyObject_GC_UnTrack((PyObject
*)self
);
5229 Py_XDECREF(self
->readline
);
5230 Py_XDECREF(self
->read
);
5231 Py_XDECREF(self
->file
);
5232 Py_XDECREF(self
->memo
);
5233 Py_XDECREF(self
->stack
);
5234 Py_XDECREF(self
->pers_func
);
5235 Py_XDECREF(self
->arg
);
5236 Py_XDECREF(self
->last_string
);
5237 Py_XDECREF(self
->find_class
);
5243 if (self
->buf_size
) {
5247 self
->ob_type
->tp_free((PyObject
*)self
);
5251 Unpickler_traverse(Unpicklerobject
*self
, visitproc visit
, void *arg
)
5255 #define VISIT(SLOT) \
5257 err = visit((PyObject *)(SLOT), arg); \
5261 VISIT(self
->readline
);
5266 VISIT(self
->pers_func
);
5268 VISIT(self
->last_string
);
5269 VISIT(self
->find_class
);
5275 Unpickler_clear(Unpicklerobject
*self
)
5277 #define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL
5278 CLEAR(self
->readline
);
5283 CLEAR(self
->pers_func
);
5285 CLEAR(self
->last_string
);
5286 CLEAR(self
->find_class
);
5292 Unpickler_getattr(Unpicklerobject
*self
, char *name
)
5294 if (!strcmp(name
, "persistent_load")) {
5295 if (!self
->pers_func
) {
5296 PyErr_SetString(PyExc_AttributeError
, name
);
5300 Py_INCREF(self
->pers_func
);
5301 return self
->pers_func
;
5304 if (!strcmp(name
, "find_global")) {
5305 if (!self
->find_class
) {
5306 PyErr_SetString(PyExc_AttributeError
, name
);
5310 Py_INCREF(self
->find_class
);
5311 return self
->find_class
;
5314 if (!strcmp(name
, "memo")) {
5316 PyErr_SetString(PyExc_AttributeError
, name
);
5320 Py_INCREF(self
->memo
);
5324 if (!strcmp(name
, "UnpicklingError")) {
5325 Py_INCREF(UnpicklingError
);
5326 return UnpicklingError
;
5329 return Py_FindMethod(Unpickler_methods
, (PyObject
*)self
, name
);
5334 Unpickler_setattr(Unpicklerobject
*self
, char *name
, PyObject
*value
)
5337 if (!strcmp(name
, "persistent_load")) {
5338 Py_XDECREF(self
->pers_func
);
5339 self
->pers_func
= value
;
5344 if (!strcmp(name
, "find_global")) {
5345 Py_XDECREF(self
->find_class
);
5346 self
->find_class
= value
;
5352 PyErr_SetString(PyExc_TypeError
,
5353 "attribute deletion is not supported");
5357 if (strcmp(name
, "memo") == 0) {
5358 if (!PyDict_Check(value
)) {
5359 PyErr_SetString(PyExc_TypeError
,
5360 "memo must be a dictionary");
5363 Py_XDECREF(self
->memo
);
5369 PyErr_SetString(PyExc_AttributeError
, name
);
5373 /* ---------------------------------------------------------------------------
5374 * Module-level functions.
5377 /* dump(obj, file, protocol=0). */
5379 cpm_dump(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5381 static const char *kwlist
[] = {"obj", "file", "protocol", NULL
};
5382 PyObject
*ob
, *file
, *res
= NULL
;
5383 Picklerobject
*pickler
= 0;
5386 if (!( PyArg_ParseTupleAndKeywords(args
, kwds
, "OO|i", kwlist
,
5387 &ob
, &file
, &proto
)))
5390 if (!( pickler
= newPicklerobject(file
, proto
)))
5393 if (dump(pickler
, ob
) < 0)
5400 Py_XDECREF(pickler
);
5406 /* dumps(obj, protocol=0). */
5408 cpm_dumps(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5410 static const char *kwlist
[] = {"obj", "protocol", NULL
};
5411 PyObject
*ob
, *file
= 0, *res
= NULL
;
5412 Picklerobject
*pickler
= 0;
5415 if (!( PyArg_ParseTupleAndKeywords(args
, kwds
, "O|i:dumps", kwlist
,
5419 if (!( file
= PycStringIO
->NewOutput(128)))
5422 if (!( pickler
= newPicklerobject(file
, proto
)))
5425 if (dump(pickler
, ob
) < 0)
5428 res
= PycStringIO
->cgetvalue(file
);
5431 Py_XDECREF(pickler
);
5438 /* load(fileobj). */
5440 cpm_load(PyObject
*self
, PyObject
*args
)
5442 Unpicklerobject
*unpickler
= 0;
5443 PyObject
*ob
, *res
= NULL
;
5445 if (!( PyArg_ParseTuple(args
, "O:load", &ob
)))
5448 if (!( unpickler
= newUnpicklerobject(ob
)))
5451 res
= load(unpickler
);
5454 Py_XDECREF(unpickler
);
5462 cpm_loads(PyObject
*self
, PyObject
*args
)
5464 PyObject
*ob
, *file
= 0, *res
= NULL
;
5465 Unpicklerobject
*unpickler
= 0;
5467 if (!( PyArg_ParseTuple(args
, "S:loads", &ob
)))
5470 if (!( file
= PycStringIO
->NewInput(ob
)))
5473 if (!( unpickler
= newUnpicklerobject(file
)))
5476 res
= load(unpickler
);
5480 Py_XDECREF(unpickler
);
5486 PyDoc_STRVAR(Unpicklertype__doc__
,
5487 "Objects that know how to unpickle");
5489 static PyTypeObject Unpicklertype
= {
5490 PyObject_HEAD_INIT(NULL
)
5492 "cPickle.Unpickler", /*tp_name*/
5493 sizeof(Unpicklerobject
), /*tp_basicsize*/
5495 (destructor
)Unpickler_dealloc
, /* tp_dealloc */
5497 (getattrfunc
)Unpickler_getattr
, /* tp_getattr */
5498 (setattrfunc
)Unpickler_setattr
, /* tp_setattr */
5501 0, /* tp_as_number */
5502 0, /* tp_as_sequence */
5503 0, /* tp_as_mapping */
5507 0, /* tp_getattro */
5508 0, /* tp_setattro */
5509 0, /* tp_as_buffer */
5510 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
5511 Unpicklertype__doc__
, /* tp_doc */
5512 (traverseproc
)Unpickler_traverse
, /* tp_traverse */
5513 (inquiry
)Unpickler_clear
, /* tp_clear */
5516 static struct PyMethodDef cPickle_methods
[] = {
5517 {"dump", (PyCFunction
)cpm_dump
, METH_VARARGS
| METH_KEYWORDS
,
5518 PyDoc_STR("dump(obj, file, protocol=0) -- "
5519 "Write an object in pickle format to the given file.\n"
5521 "See the Pickler docstring for the meaning of optional argument proto.")
5524 {"dumps", (PyCFunction
)cpm_dumps
, METH_VARARGS
| METH_KEYWORDS
,
5525 PyDoc_STR("dumps(obj, protocol=0) -- "
5526 "Return a string containing an object in pickle format.\n"
5528 "See the Pickler docstring for the meaning of optional argument proto.")
5531 {"load", (PyCFunction
)cpm_load
, METH_VARARGS
,
5532 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5534 {"loads", (PyCFunction
)cpm_loads
, METH_VARARGS
,
5535 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5537 {"Pickler", (PyCFunction
)get_Pickler
, METH_VARARGS
| METH_KEYWORDS
,
5538 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5540 "This takes a file-like object for writing a pickle data stream.\n"
5541 "The optional proto argument tells the pickler to use the given\n"
5542 "protocol; supported protocols are 0, 1, 2. The default\n"
5543 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5544 "only protocol that can be written to a file opened in text\n"
5545 "mode and read back successfully. When using a protocol higher\n"
5546 "than 0, make sure the file is opened in binary mode, both when\n"
5547 "pickling and unpickling.)\n"
5549 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5550 "more efficient than protocol 1.\n"
5552 "Specifying a negative protocol version selects the highest\n"
5553 "protocol version supported. The higher the protocol used, the\n"
5554 "more recent the version of Python needed to read the pickle\n"
5557 "The file parameter must have a write() method that accepts a single\n"
5558 "string argument. It can thus be an open file object, a StringIO\n"
5559 "object, or any other custom object that meets this interface.\n")
5562 {"Unpickler", (PyCFunction
)get_Unpickler
, METH_VARARGS
,
5563 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5569 init_stuff(PyObject
*module_dict
)
5571 PyObject
*copy_reg
, *t
, *r
;
5573 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5575 if (PyType_Ready(&Unpicklertype
) < 0)
5577 if (PyType_Ready(&Picklertype
) < 0)
5580 INIT_STR(__class__
);
5581 INIT_STR(__getinitargs__
);
5583 INIT_STR(__getstate__
);
5584 INIT_STR(__setstate__
);
5587 INIT_STR(__reduce__
);
5588 INIT_STR(__reduce_ex__
);
5594 INIT_STR(dispatch_table
);
5595 INIT_STR(__basicnew__
);
5597 if (!( copy_reg
= PyImport_ImportModule("copy_reg")))
5600 /* This is special because we want to use a different
5601 one in restricted mode. */
5602 dispatch_table
= PyObject_GetAttr(copy_reg
, dispatch_table_str
);
5603 if (!dispatch_table
) return -1;
5605 extension_registry
= PyObject_GetAttrString(copy_reg
,
5606 "_extension_registry");
5607 if (!extension_registry
) return -1;
5609 inverted_registry
= PyObject_GetAttrString(copy_reg
,
5610 "_inverted_registry");
5611 if (!inverted_registry
) return -1;
5613 extension_cache
= PyObject_GetAttrString(copy_reg
,
5614 "_extension_cache");
5615 if (!extension_cache
) return -1;
5617 Py_DECREF(copy_reg
);
5619 if (!(empty_tuple
= PyTuple_New(0)))
5622 two_tuple
= PyTuple_New(2);
5623 if (two_tuple
== NULL
)
5625 /* We use this temp container with no regard to refcounts, or to
5626 * keeping containees alive. Exempt from GC, because we don't
5627 * want anything looking at two_tuple() by magic.
5629 PyObject_GC_UnTrack(two_tuple
);
5632 if (!( t
=PyImport_ImportModule("__builtin__"))) return -1;
5633 if (PyDict_SetItemString(module_dict
, "__builtins__", t
) < 0)
5636 if (!( t
=PyDict_New())) return -1;
5637 if (!( r
=PyRun_String(
5638 "def __init__(self, *args): self.args=args\n\n"
5639 "def __str__(self):\n"
5640 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5642 module_dict
, t
) )) return -1;
5645 PickleError
= PyErr_NewException("cPickle.PickleError", NULL
, t
);
5651 PicklingError
= PyErr_NewException("cPickle.PicklingError",
5656 if (!( t
=PyDict_New())) return -1;
5657 if (!( r
=PyRun_String(
5658 "def __init__(self, *args): self.args=args\n\n"
5659 "def __str__(self):\n"
5661 " a=a and type(a[0]) or '(what)'\n"
5662 " return 'Cannot pickle %s objects' % a\n"
5664 module_dict
, t
) )) return -1;
5667 if (!( UnpickleableError
= PyErr_NewException(
5668 "cPickle.UnpickleableError", PicklingError
, t
)))
5673 if (!( UnpicklingError
= PyErr_NewException("cPickle.UnpicklingError",
5674 PickleError
, NULL
)))
5677 if (!( BadPickleGet
= PyErr_NewException("cPickle.BadPickleGet",
5678 UnpicklingError
, NULL
)))
5681 if (PyDict_SetItemString(module_dict
, "PickleError",
5685 if (PyDict_SetItemString(module_dict
, "PicklingError",
5689 if (PyDict_SetItemString(module_dict
, "UnpicklingError",
5690 UnpicklingError
) < 0)
5693 if (PyDict_SetItemString(module_dict
, "UnpickleableError",
5694 UnpickleableError
) < 0)
5697 if (PyDict_SetItemString(module_dict
, "BadPickleGet",
5706 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5707 #define PyMODINIT_FUNC void
5712 PyObject
*m
, *d
, *di
, *v
, *k
;
5714 char *rev
= "1.71"; /* XXX when does this change? */
5715 PyObject
*format_version
;
5716 PyObject
*compatible_formats
;
5718 Picklertype
.ob_type
= &PyType_Type
;
5719 Unpicklertype
.ob_type
= &PyType_Type
;
5720 PdataType
.ob_type
= &PyType_Type
;
5722 /* Initialize some pieces. We need to do this before module creation,
5723 * so we're forced to use a temporary dictionary. :(
5727 if (init_stuff(di
) < 0) return;
5729 /* Create the module and add the functions */
5730 m
= Py_InitModule4("cPickle", cPickle_methods
,
5731 cPickle_module_documentation
,
5732 (PyObject
*)NULL
,PYTHON_API_VERSION
);
5734 /* Add some symbolic constants to the module */
5735 d
= PyModule_GetDict(m
);
5736 v
= PyString_FromString(rev
);
5737 PyDict_SetItemString(d
, "__version__", v
);
5740 /* Copy data from di. Waaa. */
5741 for (i
=0; PyDict_Next(di
, &i
, &k
, &v
); ) {
5742 if (PyObject_SetItem(d
, k
, v
) < 0) {
5749 i
= PyModule_AddIntConstant(m
, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL
);
5753 /* These are purely informational; no code uses them. */
5754 /* File format version we write. */
5755 format_version
= PyString_FromString("2.0");
5756 /* Format versions we can read. */
5757 compatible_formats
= Py_BuildValue("[sssss]",
5758 "1.0", /* Original protocol 0 */
5759 "1.1", /* Protocol 0 + INST */
5760 "1.2", /* Original protocol 1 */
5761 "1.3", /* Protocol 1 + BINFLOAT */
5762 "2.0"); /* Original protocol 2 */
5763 PyDict_SetItemString(d
, "format_version", format_version
);
5764 PyDict_SetItemString(d
, "compatible_formats", compatible_formats
);
5765 Py_XDECREF(format_version
);
5766 Py_XDECREF(compatible_formats
);