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
,
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
)
213 bigger
= self
->size
<< 1;
214 if (bigger
<= 0) /* was 0, or new value overflows */
216 if ((int)(size_t)bigger
!= bigger
)
218 nbytes
= (size_t)bigger
* sizeof(PyObject
*);
219 if (nbytes
/ sizeof(PyObject
*) != (size_t)bigger
)
221 tmp
= realloc(self
->data
, nbytes
);
233 /* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
235 * is raised and V is set to NULL. D and V may be evaluated several times.
237 #define PDATA_POP(D, V) { \
239 (V) = (D)->data[--((D)->length)]; \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
246 /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
254 /* Push O on stack D, giving ownership of O to the stack. */
255 #define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
264 /* Push O on stack D, pushing a new reference. */
265 #define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
275 Pdata_popTuple(Pdata
*self
, int start
)
280 l
= self
->length
-start
;
284 for (i
= start
, j
= 0 ; j
< l
; i
++, j
++)
285 PyTuple_SET_ITEM(r
, j
, self
->data
[i
]);
287 self
->length
= start
;
292 Pdata_popList(Pdata
*self
, int start
)
297 l
=self
->length
-start
;
298 if (!( r
=PyList_New(l
))) return NULL
;
299 for (i
=start
, j
=0 ; j
< l
; i
++, j
++)
300 PyList_SET_ITEM(r
, j
, self
->data
[i
]);
306 /*************************************************************************/
308 #define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
318 #define FREE_ARG_TUP(self) { \
319 if (self->arg->ob_refcnt > 1) { \
320 Py_DECREF(self->arg); \
325 typedef struct Picklerobject
{
333 PyObject
*inst_pers_func
;
335 /* pickle protocol number, >= 0 */
338 /* bool, true if proto > 0 */
341 int fast
; /* Fast mode doesn't save in memo, don't use if circ ref */
343 int (*write_func
)(struct Picklerobject
*, const char *, Py_ssize_t
);
346 PyObject
*dispatch_table
;
347 int fast_container
; /* count nested container dumps */
351 #ifndef PY_CPICKLE_FAST_LIMIT
352 #define PY_CPICKLE_FAST_LIMIT 50
355 static PyTypeObject Picklertype
;
357 typedef struct Unpicklerobject
{
368 PyObject
*last_string
;
372 Py_ssize_t (*read_func
)(struct Unpicklerobject
*, char **, Py_ssize_t
);
373 Py_ssize_t (*readline_func
)(struct Unpicklerobject
*, char **);
376 PyObject
*find_class
;
379 static PyTypeObject Unpicklertype
;
381 /* Forward decls that need the above structs */
382 static int save(Picklerobject
*, PyObject
*, int);
383 static int put2(Picklerobject
*, PyObject
*);
387 cPickle_ErrFormat(PyObject
*ErrType
, char *stringformat
, char *format
, ...)
390 PyObject
*args
=0, *retval
=0;
391 va_start(va
, format
);
393 if (format
) args
= Py_VaBuildValue(format
, va
);
395 if (format
&& ! args
) return NULL
;
396 if (stringformat
&& !(retval
=PyString_FromString(stringformat
)))
402 v
=PyString_Format(retval
, args
);
405 if (! v
) return NULL
;
410 if (args
) retval
=args
;
412 PyErr_SetObject(ErrType
,Py_None
);
415 PyErr_SetObject(ErrType
,retval
);
421 write_file(Picklerobject
*self
, const char *s
, Py_ssize_t n
)
423 size_t nbyteswritten
;
430 /* String too large */
434 Py_BEGIN_ALLOW_THREADS
435 nbyteswritten
= fwrite(s
, sizeof(char), n
, self
->fp
);
437 if (nbyteswritten
!= (size_t)n
) {
438 PyErr_SetFromErrno(PyExc_IOError
);
446 write_cStringIO(Picklerobject
*self
, const char *s
, Py_ssize_t n
)
452 if (PycStringIO
->cwrite((PyObject
*)self
->file
, s
, n
) != n
) {
460 write_none(Picklerobject
*self
, const char *s
, Py_ssize_t n
)
462 if (s
== NULL
) return 0;
463 if (n
> INT_MAX
) return -1;
468 write_other(Picklerobject
*self
, const char *s
, Py_ssize_t _n
)
470 PyObject
*py_str
= 0, *junk
= 0;
477 if (!( self
->buf_size
)) return 0;
478 py_str
= PyString_FromStringAndSize(self
->write_buf
,
484 if (self
->buf_size
&& (n
+ self
->buf_size
) > WRITE_BUF_SIZE
) {
485 if (write_other(self
, NULL
, 0) < 0)
489 if (n
> WRITE_BUF_SIZE
) {
491 PyString_FromStringAndSize(s
, n
)))
495 memcpy(self
->write_buf
+ self
->buf_size
, s
, n
);
502 /* object with write method */
503 ARG_TUP(self
, py_str
);
505 junk
= PyObject_Call(self
->write
, self
->arg
, NULL
);
508 if (junk
) Py_DECREF(junk
);
512 PDATA_PUSH(self
->file
, py_str
, -1);
520 read_file(Unpicklerobject
*self
, char **s
, Py_ssize_t n
)
524 if (self
->buf_size
== 0) {
527 size
= ((n
< 32) ? 32 : n
);
528 if (!( self
->buf
= (char *)malloc(size
))) {
533 self
->buf_size
= size
;
535 else if (n
> self
->buf_size
) {
536 self
->buf
= (char *)realloc(self
->buf
, n
);
544 Py_BEGIN_ALLOW_THREADS
545 nbytesread
= fread(self
->buf
, sizeof(char), n
, self
->fp
);
547 if (nbytesread
!= (size_t)n
) {
548 if (feof(self
->fp
)) {
549 PyErr_SetNone(PyExc_EOFError
);
553 PyErr_SetFromErrno(PyExc_IOError
);
564 readline_file(Unpicklerobject
*self
, char **s
)
568 if (self
->buf_size
== 0) {
569 if (!( self
->buf
= (char *)malloc(40))) {
579 for (; i
< (self
->buf_size
- 1); i
++) {
580 if (feof(self
->fp
) ||
581 (self
->buf
[i
] = getc(self
->fp
)) == '\n') {
582 self
->buf
[i
+ 1] = '\0';
587 bigger
= self
->buf_size
<< 1;
588 if (bigger
<= 0) { /* overflow */
592 self
->buf
= (char *)realloc(self
->buf
, bigger
);
597 self
->buf_size
= bigger
;
603 read_cStringIO(Unpicklerobject
*self
, char **s
, Py_ssize_t n
)
607 if (PycStringIO
->cread((PyObject
*)self
->file
, &ptr
, n
) != n
) {
608 PyErr_SetNone(PyExc_EOFError
);
619 readline_cStringIO(Unpicklerobject
*self
, char **s
)
624 if ((n
= PycStringIO
->creadline((PyObject
*)self
->file
, &ptr
)) < 0) {
635 read_other(Unpicklerobject
*self
, char **s
, Py_ssize_t n
)
637 PyObject
*bytes
, *str
=0;
639 if (!( bytes
= PyInt_FromSsize_t(n
))) return -1;
641 ARG_TUP(self
, bytes
);
643 str
= PyObject_Call(self
->read
, self
->arg
, NULL
);
646 if (! str
) return -1;
648 Py_XDECREF(self
->last_string
);
649 self
->last_string
= str
;
651 if (! (*s
= PyString_AsString(str
))) return -1;
657 readline_other(Unpicklerobject
*self
, char **s
)
662 if (!( str
= PyObject_CallObject(self
->readline
, empty_tuple
))) {
666 if ((str_size
= PyString_Size(str
)) < 0)
669 Py_XDECREF(self
->last_string
);
670 self
->last_string
= str
;
672 if (! (*s
= PyString_AsString(str
)))
678 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
679 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
680 * The caller is responsible for free()'ing the return value.
683 pystrndup(const char *s
, int n
)
685 char *r
= (char *)malloc(n
+1);
687 return (char*)PyErr_NoMemory();
695 get(Picklerobject
*self
, PyObject
*id
)
697 PyObject
*value
, *mv
;
702 if (!( mv
= PyDict_GetItem(self
->memo
, id
))) {
703 PyErr_SetObject(PyExc_KeyError
, id
);
707 if (!( value
= PyTuple_GetItem(mv
, 0)))
710 if (!( PyInt_Check(value
))) {
711 PyErr_SetString(PicklingError
, "no int where int expected in memo");
714 c_value
= PyInt_AS_LONG((PyIntObject
*)value
);
718 PyOS_snprintf(s
+ 1, sizeof(s
) - 1, "%ld\n", c_value
);
721 else if (Pdata_Check(self
->file
)) {
722 if (write_other(self
, NULL
, 0) < 0) return -1;
723 PDATA_APPEND(self
->file
, mv
, -1);
729 s
[1] = (int)(c_value
& 0xff);
734 s
[1] = (int)(c_value
& 0xff);
735 s
[2] = (int)((c_value
>> 8) & 0xff);
736 s
[3] = (int)((c_value
>> 16) & 0xff);
737 s
[4] = (int)((c_value
>> 24) & 0xff);
742 if (self
->write_func(self
, s
, len
) < 0)
750 put(Picklerobject
*self
, PyObject
*ob
)
752 if (ob
->ob_refcnt
< 2 || self
->fast
)
755 return put2(self
, ob
);
760 put2(Picklerobject
*self
, PyObject
*ob
)
766 PyObject
*py_ob_id
= 0, *memo_len
= 0, *t
= 0;
771 if ((p
= PyDict_Size(self
->memo
)) < 0)
774 /* Make sure memo keys are positive! */
776 * XXX And does "positive" really mean non-negative?
777 * XXX pickle.py starts with PUT index 0, not 1. This makes for
778 * XXX gratuitous differences between the pickling modules.
782 if (!( py_ob_id
= PyLong_FromVoidPtr(ob
)))
785 if (!( memo_len
= PyInt_FromLong(p
)))
788 if (!( t
= PyTuple_New(2)))
791 PyTuple_SET_ITEM(t
, 0, memo_len
);
793 PyTuple_SET_ITEM(t
, 1, ob
);
796 if (PyDict_SetItem(self
->memo
, py_ob_id
, t
) < 0)
801 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%d\n", p
);
804 else if (Pdata_Check(self
->file
)) {
805 if (write_other(self
, NULL
, 0) < 0) return -1;
806 PDATA_APPEND(self
->file
, memo_len
, -1);
807 res
=0; /* Job well done ;) */
812 c_str
[0] = LONG_BINPUT
;
813 c_str
[1] = (int)(p
& 0xff);
814 c_str
[2] = (int)((p
>> 8) & 0xff);
815 c_str
[3] = (int)((p
>> 16) & 0xff);
816 c_str
[4] = (int)((p
>> 24) & 0xff);
826 if (self
->write_func(self
, c_str
, len
) < 0)
832 Py_XDECREF(py_ob_id
);
833 Py_XDECREF(memo_len
);
840 whichmodule(PyObject
*global
, PyObject
*global_name
)
843 PyObject
*module
= 0, *modules_dict
= 0,
844 *global_name_attr
= 0, *name
= 0;
846 module
= PyObject_GetAttrString(global
, "__module__");
849 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
854 if (!( modules_dict
= PySys_GetObject("modules")))
858 while ((j
= PyDict_Next(modules_dict
, &i
, &name
, &module
))) {
860 if (PyObject_Compare(name
, __main___str
)==0) continue;
862 global_name_attr
= PyObject_GetAttr(module
, global_name
);
863 if (!global_name_attr
) {
864 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
871 if (global_name_attr
!= global
) {
872 Py_DECREF(global_name_attr
);
876 Py_DECREF(global_name_attr
);
881 /* The following implements the rule in pickle.py added in 1.5
882 that used __main__ if no module is found. I don't actually
896 fast_save_enter(Picklerobject
*self
, PyObject
*obj
)
898 /* if fast_container < 0, we're doing an error exit. */
899 if (++self
->fast_container
>= PY_CPICKLE_FAST_LIMIT
) {
900 PyObject
*key
= NULL
;
901 if (self
->fast_memo
== NULL
) {
902 self
->fast_memo
= PyDict_New();
903 if (self
->fast_memo
== NULL
) {
904 self
->fast_container
= -1;
908 key
= PyLong_FromVoidPtr(obj
);
911 if (PyDict_GetItem(self
->fast_memo
, key
)) {
913 PyErr_Format(PyExc_ValueError
,
914 "fast mode: can't pickle cyclic objects "
915 "including object type %s at %p",
916 obj
->ob_type
->tp_name
, obj
);
917 self
->fast_container
= -1;
920 if (PyDict_SetItem(self
->fast_memo
, key
, Py_None
) < 0) {
922 self
->fast_container
= -1;
931 fast_save_leave(Picklerobject
*self
, PyObject
*obj
)
933 if (self
->fast_container
-- >= PY_CPICKLE_FAST_LIMIT
) {
934 PyObject
*key
= PyLong_FromVoidPtr(obj
);
937 if (PyDict_DelItem(self
->fast_memo
, key
) < 0) {
947 save_none(Picklerobject
*self
, PyObject
*args
)
949 static char none
= NONE
;
950 if (self
->write_func(self
, &none
, 1) < 0)
957 save_bool(Picklerobject
*self
, PyObject
*args
)
959 static const char *buf
[2] = {FALSE
, TRUE
};
960 static char len
[2] = {sizeof(FALSE
)-1, sizeof(TRUE
)-1};
961 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
963 if (self
->proto
>= 2) {
964 char opcode
= l
? NEWTRUE
: NEWFALSE
;
965 if (self
->write_func(self
, &opcode
, 1) < 0)
968 else if (self
->write_func(self
, buf
[l
], len
[l
]) < 0)
974 save_int(Picklerobject
*self
, PyObject
*args
)
977 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
986 /* Text-mode pickle, or long too big to fit in the 4-byte
987 * signed BININT format: store as a string.
990 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%ld\n", l
);
991 if (self
->write_func(self
, c_str
, strlen(c_str
)) < 0)
995 /* Binary pickle and l fits in a signed 4-byte int. */
996 c_str
[1] = (int)( l
& 0xff);
997 c_str
[2] = (int)((l
>> 8) & 0xff);
998 c_str
[3] = (int)((l
>> 16) & 0xff);
999 c_str
[4] = (int)((l
>> 24) & 0xff);
1001 if ((c_str
[4] == 0) && (c_str
[3] == 0)) {
1002 if (c_str
[2] == 0) {
1016 if (self
->write_func(self
, c_str
, len
) < 0)
1025 save_long(Picklerobject
*self
, PyObject
*args
)
1029 PyObject
*repr
= NULL
;
1031 static char l
= LONG
;
1033 if (self
->proto
>= 2) {
1034 /* Linear-time pickling. */
1037 unsigned char *pdata
;
1040 int sign
= _PyLong_Sign(args
);
1043 /* It's 0 -- an empty bytestring. */
1046 i
= self
->write_func(self
, c_str
, 2);
1047 if (i
< 0) goto finally
;
1051 nbits
= _PyLong_NumBits(args
);
1052 if (nbits
== (size_t)-1 && PyErr_Occurred())
1054 /* How many bytes do we need? There are nbits >> 3 full
1055 * bytes of data, and nbits & 7 leftover bits. If there
1056 * are any leftover bits, then we clearly need another
1057 * byte. Wnat's not so obvious is that we *probably*
1058 * need another byte even if there aren't any leftovers:
1059 * the most-significant bit of the most-significant byte
1060 * acts like a sign bit, and it's usually got a sense
1061 * opposite of the one we need. The exception is longs
1062 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1063 * its own 256's-complement, so has the right sign bit
1064 * even without the extra byte. That's a pain to check
1065 * for in advance, though, so we always grab an extra
1066 * byte at the start, and cut it back later if possible.
1068 nbytes
= (nbits
>> 3) + 1;
1069 if ((int)nbytes
< 0 || (size_t)(int)nbytes
!= nbytes
) {
1070 PyErr_SetString(PyExc_OverflowError
, "long too large "
1074 repr
= PyString_FromStringAndSize(NULL
, (int)nbytes
);
1075 if (repr
== NULL
) goto finally
;
1076 pdata
= (unsigned char *)PyString_AS_STRING(repr
);
1077 i
= _PyLong_AsByteArray((PyLongObject
*)args
,
1079 1 /* little endian */, 1 /* signed */);
1080 if (i
< 0) goto finally
;
1081 /* If the long is negative, this may be a byte more than
1082 * needed. This is so iff the MSB is all redundant sign
1085 if (sign
< 0 && nbytes
> 1 && pdata
[nbytes
- 1] == 0xff &&
1086 (pdata
[nbytes
- 2] & 0x80) != 0)
1091 c_str
[1] = (char)nbytes
;
1097 for (i
= 1; i
< 5; i
++) {
1098 c_str
[i
] = (char)(size
& 0xff);
1103 i
= self
->write_func(self
, c_str
, size
);
1104 if (i
< 0) goto finally
;
1105 i
= self
->write_func(self
, (char *)pdata
, (int)nbytes
);
1106 if (i
< 0) goto finally
;
1111 /* proto < 2: write the repr and newline. This is quadratic-time
1112 * (in the number of digits), in both directions.
1114 if (!( repr
= PyObject_Repr(args
)))
1117 if ((size
= PyString_Size(repr
)) < 0)
1120 if (self
->write_func(self
, &l
, 1) < 0)
1123 if (self
->write_func(self
,
1124 PyString_AS_STRING((PyStringObject
*)repr
),
1128 if (self
->write_func(self
, "\n", 1) < 0)
1140 save_float(Picklerobject
*self
, PyObject
*args
)
1142 double x
= PyFloat_AS_DOUBLE((PyFloatObject
*)args
);
1147 if (_PyFloat_Pack8(x
, (unsigned char *)&str
[1], 0) < 0)
1149 if (self
->write_func(self
, str
, 9) < 0)
1155 PyOS_ascii_formatd(c_str
+ 1, sizeof(c_str
) - 2, "%.17g", x
);
1156 /* Extend the formatted string with a newline character */
1157 strcat(c_str
, "\n");
1159 if (self
->write_func(self
, c_str
, strlen(c_str
)) < 0)
1168 save_string(Picklerobject
*self
, PyObject
*args
, int doput
)
1173 if ((size
= PyString_Size(args
)) < 0)
1179 static char string
= STRING
;
1181 if (!( repr
= PyObject_Repr(args
)))
1184 if ((len
= PyString_Size(repr
)) < 0)
1186 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1188 if (self
->write_func(self
, &string
, 1) < 0)
1191 if (self
->write_func(self
, repr_str
, len
) < 0)
1194 if (self
->write_func(self
, "\n", 1) < 0)
1203 if ((size
= PyString_Size(args
)) < 0)
1207 c_str
[0] = SHORT_BINSTRING
;
1212 c_str
[0] = BINSTRING
;
1213 for (i
= 1; i
< 5; i
++)
1214 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1218 if (self
->write_func(self
, c_str
, len
) < 0)
1221 if (size
> 128 && Pdata_Check(self
->file
)) {
1222 if (write_other(self
, NULL
, 0) < 0) return -1;
1223 PDATA_APPEND(self
->file
, args
, -1);
1226 if (self
->write_func(self
,
1228 (PyStringObject
*)args
),
1235 if (put(self
, args
) < 0)
1246 #ifdef Py_USING_UNICODE
1247 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1248 backslash and newline characters to \uXXXX escapes. */
1250 modified_EncodeRawUnicodeEscape(const Py_UNICODE
*s
, int size
)
1256 static const char *hexdigit
= "0123456789ABCDEF";
1258 repr
= PyString_FromStringAndSize(NULL
, 6 * size
);
1264 p
= q
= PyString_AS_STRING(repr
);
1265 while (size
-- > 0) {
1266 Py_UNICODE ch
= *s
++;
1267 /* Map 16-bit characters to '\uxxxx' */
1268 if (ch
>= 256 || ch
== '\\' || ch
== '\n') {
1271 *p
++ = hexdigit
[(ch
>> 12) & 0xf];
1272 *p
++ = hexdigit
[(ch
>> 8) & 0xf];
1273 *p
++ = hexdigit
[(ch
>> 4) & 0xf];
1274 *p
++ = hexdigit
[ch
& 15];
1276 /* Copy everything else as-is */
1281 _PyString_Resize(&repr
, p
- q
);
1287 save_unicode(Picklerobject
*self
, PyObject
*args
, int doput
)
1292 if (!PyUnicode_Check(args
))
1297 static char string
= UNICODE
;
1299 repr
= modified_EncodeRawUnicodeEscape(
1300 PyUnicode_AS_UNICODE(args
), PyUnicode_GET_SIZE(args
));
1304 if ((len
= PyString_Size(repr
)) < 0)
1306 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1308 if (self
->write_func(self
, &string
, 1) < 0)
1311 if (self
->write_func(self
, repr_str
, len
) < 0)
1314 if (self
->write_func(self
, "\n", 1) < 0)
1323 if (!( repr
= PyUnicode_AsUTF8String(args
)))
1326 if ((size
= PyString_Size(repr
)) < 0)
1329 c_str
[0] = BINUNICODE
;
1330 for (i
= 1; i
< 5; i
++)
1331 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1334 if (self
->write_func(self
, c_str
, len
) < 0)
1337 if (size
> 128 && Pdata_Check(self
->file
)) {
1338 if (write_other(self
, NULL
, 0) < 0)
1340 PDATA_APPEND(self
->file
, repr
, -1);
1343 if (self
->write_func(self
, PyString_AS_STRING(repr
),
1352 if (put(self
, args
) < 0)
1363 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1365 store_tuple_elements(Picklerobject
*self
, PyObject
*t
, int len
)
1368 int res
= -1; /* guilty until proved innocent */
1370 assert(PyTuple_Size(t
) == len
);
1372 for (i
= 0; i
< len
; i
++) {
1373 PyObject
*element
= PyTuple_GET_ITEM(t
, i
);
1375 if (element
== NULL
)
1377 if (save(self
, element
, 0) < 0)
1386 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1387 * used across protocols to minimize the space needed to pickle them.
1388 * Tuples are also the only builtin immutable type that can be recursive
1389 * (a tuple can be reached from itself), and that requires some subtle
1390 * magic so that it works in all cases. IOW, this is a long routine.
1393 save_tuple(Picklerobject
*self
, PyObject
*args
)
1395 PyObject
*py_tuple_id
= NULL
;
1399 static char tuple
= TUPLE
;
1400 static char pop
= POP
;
1401 static char pop_mark
= POP_MARK
;
1402 static char len2opcode
[] = {EMPTY_TUPLE
, TUPLE1
, TUPLE2
, TUPLE3
};
1404 if ((len
= PyTuple_Size(args
)) < 0)
1411 c_str
[0] = EMPTY_TUPLE
;
1419 if (self
->write_func(self
, c_str
, len
) >= 0)
1421 /* Don't memoize an empty tuple. */
1425 /* A non-empty tuple. */
1427 /* id(tuple) isn't in the memo now. If it shows up there after
1428 * saving the tuple elements, the tuple must be recursive, in
1429 * which case we'll pop everything we put on the stack, and fetch
1430 * its value from the memo.
1432 py_tuple_id
= PyLong_FromVoidPtr(args
);
1433 if (py_tuple_id
== NULL
)
1436 if (len
<= 3 && self
->proto
>= 2) {
1437 /* Use TUPLE{1,2,3} opcodes. */
1438 if (store_tuple_elements(self
, args
, len
) < 0)
1440 if (PyDict_GetItem(self
->memo
, py_tuple_id
)) {
1441 /* pop the len elements */
1442 for (i
= 0; i
< len
; ++i
)
1443 if (self
->write_func(self
, &pop
, 1) < 0)
1445 /* fetch from memo */
1446 if (get(self
, py_tuple_id
) < 0)
1451 /* Not recursive. */
1452 if (self
->write_func(self
, len2opcode
+ len
, 1) < 0)
1457 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1458 * Generate MARK elt1 elt2 ... TUPLE
1460 if (self
->write_func(self
, &MARKv
, 1) < 0)
1463 if (store_tuple_elements(self
, args
, len
) < 0)
1466 if (PyDict_GetItem(self
->memo
, py_tuple_id
)) {
1467 /* pop the stack stuff we pushed */
1469 if (self
->write_func(self
, &pop_mark
, 1) < 0)
1473 /* Note that we pop one more than len, to remove
1476 for (i
= 0; i
<= len
; i
++)
1477 if (self
->write_func(self
, &pop
, 1) < 0)
1480 /* fetch from memo */
1481 if (get(self
, py_tuple_id
) >= 0)
1486 /* Not recursive. */
1487 if (self
->write_func(self
, &tuple
, 1) < 0)
1491 if (put(self
, args
) >= 0)
1495 Py_XDECREF(py_tuple_id
);
1499 /* iter is an iterator giving items, and we batch up chunks of
1500 * MARK item item ... item APPENDS
1501 * opcode sequences. Calling code should have arranged to first create an
1502 * empty list, or list-like object, for the APPENDS to operate on.
1503 * Returns 0 on success, <0 on error.
1506 batch_list(Picklerobject
*self
, PyObject
*iter
)
1509 PyObject
*slice
[BATCHSIZE
];
1512 static char append
= APPEND
;
1513 static char appends
= APPENDS
;
1515 assert(iter
!= NULL
);
1517 if (self
->proto
== 0) {
1518 /* APPENDS isn't available; do one at a time. */
1520 obj
= PyIter_Next(iter
);
1522 if (PyErr_Occurred())
1526 i
= save(self
, obj
, 0);
1530 if (self
->write_func(self
, &append
, 1) < 0)
1536 /* proto > 0: write in batches of BATCHSIZE. */
1538 /* Get next group of (no more than) BATCHSIZE elements. */
1539 for (n
= 0; n
< BATCHSIZE
; ++n
) {
1540 obj
= PyIter_Next(iter
);
1542 if (PyErr_Occurred())
1550 /* Pump out MARK, slice[0:n], APPENDS. */
1551 if (self
->write_func(self
, &MARKv
, 1) < 0)
1553 for (i
= 0; i
< n
; ++i
) {
1554 if (save(self
, slice
[i
], 0) < 0)
1557 if (self
->write_func(self
, &appends
, 1) < 0)
1561 if (save(self
, slice
[0], 0) < 0)
1563 if (self
->write_func(self
, &append
, 1) < 0)
1567 for (i
= 0; i
< n
; ++i
) {
1568 Py_DECREF(slice
[i
]);
1570 } while (n
== BATCHSIZE
);
1575 Py_DECREF(slice
[n
]);
1581 save_list(Picklerobject
*self
, PyObject
*args
)
1588 if (self
->fast
&& !fast_save_enter(self
, args
))
1591 /* Create an empty list. */
1602 if (self
->write_func(self
, s
, len
) < 0)
1605 /* Get list length, and bow out early if empty. */
1606 if ((len
= PyList_Size(args
)) < 0)
1611 if (put(self
, args
) >= 0)
1615 if (put2(self
, args
) < 0)
1618 /* Materialize the list elements. */
1619 iter
= PyObject_GetIter(args
);
1622 res
= batch_list(self
, iter
);
1626 if (self
->fast
&& !fast_save_leave(self
, args
))
1633 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1634 * MARK key value ... key value SETITEMS
1635 * opcode sequences. Calling code should have arranged to first create an
1636 * empty dict, or dict-like object, for the SETITEMS to operate on.
1637 * Returns 0 on success, <0 on error.
1639 * This is very much like batch_list(). The difference between saving
1640 * elements directly, and picking apart two-tuples, is so long-winded at
1641 * the C level, though, that attempts to combine these routines were too
1645 batch_dict(Picklerobject
*self
, PyObject
*iter
)
1648 PyObject
*slice
[BATCHSIZE
];
1651 static char setitem
= SETITEM
;
1652 static char setitems
= SETITEMS
;
1654 assert(iter
!= NULL
);
1656 if (self
->proto
== 0) {
1657 /* SETITEMS isn't available; do one at a time. */
1659 p
= PyIter_Next(iter
);
1661 if (PyErr_Occurred())
1665 if (!PyTuple_Check(p
) || PyTuple_Size(p
) != 2) {
1666 PyErr_SetString(PyExc_TypeError
, "dict items "
1667 "iterator must return 2-tuples");
1670 i
= save(self
, PyTuple_GET_ITEM(p
, 0), 0);
1672 i
= save(self
, PyTuple_GET_ITEM(p
, 1), 0);
1676 if (self
->write_func(self
, &setitem
, 1) < 0)
1682 /* proto > 0: write in batches of BATCHSIZE. */
1684 /* Get next group of (no more than) BATCHSIZE elements. */
1685 for (n
= 0; n
< BATCHSIZE
; ++n
) {
1686 p
= PyIter_Next(iter
);
1688 if (PyErr_Occurred())
1692 if (!PyTuple_Check(p
) || PyTuple_Size(p
) != 2) {
1693 PyErr_SetString(PyExc_TypeError
, "dict items "
1694 "iterator must return 2-tuples");
1701 /* Pump out MARK, slice[0:n], SETITEMS. */
1702 if (self
->write_func(self
, &MARKv
, 1) < 0)
1704 for (i
= 0; i
< n
; ++i
) {
1706 if (save(self
, PyTuple_GET_ITEM(p
, 0), 0) < 0)
1708 if (save(self
, PyTuple_GET_ITEM(p
, 1), 0) < 0)
1711 if (self
->write_func(self
, &setitems
, 1) < 0)
1716 if (save(self
, PyTuple_GET_ITEM(p
, 0), 0) < 0)
1718 if (save(self
, PyTuple_GET_ITEM(p
, 1), 0) < 0)
1720 if (self
->write_func(self
, &setitem
, 1) < 0)
1724 for (i
= 0; i
< n
; ++i
) {
1725 Py_DECREF(slice
[i
]);
1727 } while (n
== BATCHSIZE
);
1732 Py_DECREF(slice
[n
]);
1738 save_dict(Picklerobject
*self
, PyObject
*args
)
1745 if (self
->fast
&& !fast_save_enter(self
, args
))
1748 /* Create an empty dict. */
1759 if (self
->write_func(self
, s
, len
) < 0)
1762 /* Get dict size, and bow out early if empty. */
1763 if ((len
= PyDict_Size(args
)) < 0)
1767 if (put(self
, args
) >= 0)
1771 if (put2(self
, args
) < 0)
1774 /* Materialize the dict items. */
1775 iter
= PyObject_CallMethod(args
, "iteritems", "()");
1778 res
= batch_dict(self
, iter
);
1782 if (self
->fast
&& !fast_save_leave(self
, args
))
1790 save_inst(Picklerobject
*self
, PyObject
*args
)
1792 PyObject
*class = 0, *module
= 0, *name
= 0, *state
= 0,
1793 *getinitargs_func
= 0, *getstate_func
= 0, *class_args
= 0;
1794 char *module_str
, *name_str
;
1795 int module_size
, name_size
, res
= -1;
1797 static char inst
= INST
, obj
= OBJ
, build
= BUILD
;
1799 if (self
->fast
&& !fast_save_enter(self
, args
))
1802 if (self
->write_func(self
, &MARKv
, 1) < 0)
1805 if (!( class = PyObject_GetAttr(args
, __class___str
)))
1809 if (save(self
, class, 0) < 0)
1813 if ((getinitargs_func
= PyObject_GetAttr(args
, __getinitargs___str
))) {
1814 PyObject
*element
= 0;
1818 PyObject_Call(getinitargs_func
, empty_tuple
, NULL
)))
1821 if ((len
= PyObject_Size(class_args
)) < 0)
1824 for (i
= 0; i
< len
; i
++) {
1825 if (!( element
= PySequence_GetItem(class_args
, i
)))
1828 if (save(self
, element
, 0) < 0) {
1837 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
1844 if (!( name
= ((PyClassObject
*)class)->cl_name
)) {
1845 PyErr_SetString(PicklingError
, "class has no name");
1849 if (!( module
= whichmodule(class, name
)))
1853 if ((module_size
= PyString_Size(module
)) < 0 ||
1854 (name_size
= PyString_Size(name
)) < 0)
1857 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1858 name_str
= PyString_AS_STRING((PyStringObject
*)name
);
1860 if (self
->write_func(self
, &inst
, 1) < 0)
1863 if (self
->write_func(self
, module_str
, module_size
) < 0)
1866 if (self
->write_func(self
, "\n", 1) < 0)
1869 if (self
->write_func(self
, name_str
, name_size
) < 0)
1872 if (self
->write_func(self
, "\n", 1) < 0)
1875 else if (self
->write_func(self
, &obj
, 1) < 0) {
1879 if ((getstate_func
= PyObject_GetAttr(args
, __getstate___str
))) {
1880 state
= PyObject_Call(getstate_func
, empty_tuple
, NULL
);
1885 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
1890 if (!( state
= PyObject_GetAttr(args
, __dict___str
))) {
1891 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
1900 if (!PyDict_Check(state
)) {
1901 if (put2(self
, args
) < 0)
1905 if (put(self
, args
) < 0)
1909 if (save(self
, state
, 0) < 0)
1912 if (self
->write_func(self
, &build
, 1) < 0)
1918 if (self
->fast
&& !fast_save_leave(self
, args
))
1924 Py_XDECREF(getinitargs_func
);
1925 Py_XDECREF(getstate_func
);
1926 Py_XDECREF(class_args
);
1933 save_global(Picklerobject
*self
, PyObject
*args
, PyObject
*name
)
1935 PyObject
*global_name
= 0, *module
= 0, *mod
= 0, *klass
= 0;
1936 char *name_str
, *module_str
;
1937 int module_size
, name_size
, res
= -1;
1939 static char global
= GLOBAL
;
1943 Py_INCREF(global_name
);
1946 if (!( global_name
= PyObject_GetAttr(args
, __name___str
)))
1950 if (!( module
= whichmodule(args
, global_name
)))
1953 if ((module_size
= PyString_Size(module
)) < 0 ||
1954 (name_size
= PyString_Size(global_name
)) < 0)
1957 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1958 name_str
= PyString_AS_STRING((PyStringObject
*)global_name
);
1960 /* XXX This can be doing a relative import. Clearly it shouldn't,
1961 but I don't know how to stop it. :-( */
1962 mod
= PyImport_ImportModule(module_str
);
1964 cPickle_ErrFormat(PicklingError
,
1965 "Can't pickle %s: import of module %s "
1967 "OS", args
, module
);
1970 klass
= PyObject_GetAttrString(mod
, name_str
);
1971 if (klass
== NULL
) {
1972 cPickle_ErrFormat(PicklingError
,
1973 "Can't pickle %s: attribute lookup %s.%s "
1975 "OSS", args
, module
, global_name
);
1978 if (klass
!= args
) {
1980 cPickle_ErrFormat(PicklingError
,
1981 "Can't pickle %s: it's not the same object "
1983 "OSS", args
, module
, global_name
);
1988 if (self
->proto
>= 2) {
1989 /* See whether this is in the extension registry, and if
1990 * so generate an EXT opcode.
1992 PyObject
*py_code
; /* extension code as Python object */
1993 long code
; /* extension code as C value */
1997 PyTuple_SET_ITEM(two_tuple
, 0, module
);
1998 PyTuple_SET_ITEM(two_tuple
, 1, global_name
);
1999 py_code
= PyDict_GetItem(extension_registry
, two_tuple
);
2000 if (py_code
== NULL
)
2001 goto gen_global
; /* not registered */
2003 /* Verify py_code has the right type and value. */
2004 if (!PyInt_Check(py_code
)) {
2005 cPickle_ErrFormat(PicklingError
, "Can't pickle %s: "
2006 "extension code %s isn't an integer",
2007 "OO", args
, py_code
);
2010 code
= PyInt_AS_LONG(py_code
);
2011 if (code
<= 0 || code
> 0x7fffffffL
) {
2012 cPickle_ErrFormat(PicklingError
, "Can't pickle %s: "
2013 "extension code %ld is out of range",
2018 /* Generate an EXT opcode. */
2021 c_str
[1] = (char)code
;
2024 else if (code
<= 0xffff) {
2026 c_str
[1] = (char)(code
& 0xff);
2027 c_str
[2] = (char)((code
>> 8) & 0xff);
2032 c_str
[1] = (char)(code
& 0xff);
2033 c_str
[2] = (char)((code
>> 8) & 0xff);
2034 c_str
[3] = (char)((code
>> 16) & 0xff);
2035 c_str
[4] = (char)((code
>> 24) & 0xff);
2039 if (self
->write_func(self
, c_str
, n
) >= 0)
2041 goto finally
; /* and don't memoize */
2045 if (self
->write_func(self
, &global
, 1) < 0)
2048 if (self
->write_func(self
, module_str
, module_size
) < 0)
2051 if (self
->write_func(self
, "\n", 1) < 0)
2054 if (self
->write_func(self
, name_str
, name_size
) < 0)
2057 if (self
->write_func(self
, "\n", 1) < 0)
2060 if (put(self
, args
) < 0)
2067 Py_XDECREF(global_name
);
2074 save_pers(Picklerobject
*self
, PyObject
*args
, PyObject
*f
)
2079 static char persid
= PERSID
, binpersid
= BINPERSID
;
2082 ARG_TUP(self
, args
);
2084 pid
= PyObject_Call(f
, self
->arg
, NULL
);
2087 if (! pid
) return -1;
2089 if (pid
!= Py_None
) {
2091 if (!PyString_Check(pid
)) {
2092 PyErr_SetString(PicklingError
,
2093 "persistent id must be string");
2097 if (self
->write_func(self
, &persid
, 1) < 0)
2100 if ((size
= PyString_Size(pid
)) < 0)
2103 if (self
->write_func(self
,
2105 (PyStringObject
*)pid
),
2109 if (self
->write_func(self
, "\n", 1) < 0)
2115 else if (save(self
, pid
, 1) >= 0) {
2116 if (self
->write_func(self
, &binpersid
, 1) < 0)
2133 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2134 * appropriate __reduce__ method for ob.
2137 save_reduce(Picklerobject
*self
, PyObject
*args
, PyObject
*ob
)
2141 PyObject
*state
= NULL
;
2142 PyObject
*listitems
= NULL
;
2143 PyObject
*dictitems
= NULL
;
2145 int use_newobj
= self
->proto
>= 2;
2147 static char reduce
= REDUCE
;
2148 static char build
= BUILD
;
2149 static char newobj
= NEWOBJ
;
2151 if (! PyArg_UnpackTuple(args
, "save_reduce", 2, 5,
2159 if (!PyTuple_Check(argtup
)) {
2160 PyErr_SetString(PicklingError
,
2161 "args from reduce() should be a tuple");
2165 if (state
== Py_None
)
2167 if (listitems
== Py_None
)
2169 if (dictitems
== Py_None
)
2172 /* Protocol 2 special case: if callable's name is __newobj__, use
2173 * NEWOBJ. This consumes a lot of code.
2176 PyObject
*temp
= PyObject_GetAttr(callable
, __name___str
);
2179 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2186 use_newobj
= PyString_Check(temp
) &&
2187 strcmp(PyString_AS_STRING(temp
),
2194 PyObject
*newargtup
;
2197 /* Sanity checks. */
2198 n
= PyTuple_Size(argtup
);
2200 PyErr_SetString(PicklingError
, "__newobj__ arglist "
2205 cls
= PyTuple_GET_ITEM(argtup
, 0);
2206 if (! PyObject_HasAttrString(cls
, "__new__")) {
2207 PyErr_SetString(PicklingError
, "args[0] from "
2208 "__newobj__ args has no __new__");
2212 /* XXX How could ob be NULL? */
2214 PyObject
*ob_dot_class
;
2216 ob_dot_class
= PyObject_GetAttr(ob
, __class___str
);
2217 if (ob_dot_class
== NULL
) {
2218 if (PyErr_ExceptionMatches(
2219 PyExc_AttributeError
))
2224 i
= ob_dot_class
!= cls
; /* true iff a problem */
2225 Py_XDECREF(ob_dot_class
);
2227 PyErr_SetString(PicklingError
, "args[0] from "
2228 "__newobj__ args has the wrong class");
2233 /* Save the class and its __new__ arguments. */
2234 if (save(self
, cls
, 0) < 0)
2237 newargtup
= PyTuple_New(n
-1); /* argtup[1:] */
2238 if (newargtup
== NULL
)
2240 for (i
= 1; i
< n
; ++i
) {
2241 PyObject
*temp
= PyTuple_GET_ITEM(argtup
, i
);
2243 PyTuple_SET_ITEM(newargtup
, i
-1, temp
);
2245 i
= save(self
, newargtup
, 0) < 0;
2246 Py_DECREF(newargtup
);
2250 /* Add NEWOBJ opcode. */
2251 if (self
->write_func(self
, &newobj
, 1) < 0)
2255 /* Not using NEWOBJ. */
2256 if (save(self
, callable
, 0) < 0 ||
2257 save(self
, argtup
, 0) < 0 ||
2258 self
->write_func(self
, &reduce
, 1) < 0)
2263 /* XXX How can ob be NULL? */
2265 if (state
&& !PyDict_Check(state
)) {
2266 if (put2(self
, ob
) < 0)
2269 else if (put(self
, ob
) < 0)
2274 if (listitems
&& batch_list(self
, listitems
) < 0)
2277 if (dictitems
&& batch_dict(self
, dictitems
) < 0)
2281 if (save(self
, state
, 0) < 0 ||
2282 self
->write_func(self
, &build
, 1) < 0)
2290 save(Picklerobject
*self
, PyObject
*args
, int pers_save
)
2293 PyObject
*py_ob_id
= 0, *__reduce__
= 0, *t
= 0;
2298 if (self
->nesting
++ > Py_GetRecursionLimit()){
2299 PyErr_SetString(PyExc_RuntimeError
,
2300 "maximum recursion depth exceeded");
2304 if (!pers_save
&& self
->pers_func
) {
2305 if ((tmp
= save_pers(self
, args
, self
->pers_func
)) != 0) {
2311 if (args
== Py_None
) {
2312 res
= save_none(self
, args
);
2316 type
= args
->ob_type
;
2318 switch (type
->tp_name
[0]) {
2320 if (args
== Py_False
|| args
== Py_True
) {
2321 res
= save_bool(self
, args
);
2326 if (type
== &PyInt_Type
) {
2327 res
= save_int(self
, args
);
2333 if (type
== &PyLong_Type
) {
2334 res
= save_long(self
, args
);
2340 if (type
== &PyFloat_Type
) {
2341 res
= save_float(self
, args
);
2347 if (type
== &PyTuple_Type
&& PyTuple_Size(args
) == 0) {
2348 res
= save_tuple(self
, args
);
2354 if ((type
== &PyString_Type
) && (PyString_GET_SIZE(args
) < 2)) {
2355 res
= save_string(self
, args
, 0);
2359 #ifdef Py_USING_UNICODE
2361 if ((type
== &PyUnicode_Type
) && (PyString_GET_SIZE(args
) < 2)) {
2362 res
= save_unicode(self
, args
, 0);
2368 if (args
->ob_refcnt
> 1) {
2369 if (!( py_ob_id
= PyLong_FromVoidPtr(args
)))
2372 if (PyDict_GetItem(self
->memo
, py_ob_id
)) {
2373 if (get(self
, py_ob_id
) < 0)
2381 switch (type
->tp_name
[0]) {
2383 if (type
== &PyString_Type
) {
2384 res
= save_string(self
, args
, 1);
2389 #ifdef Py_USING_UNICODE
2391 if (type
== &PyUnicode_Type
) {
2392 res
= save_unicode(self
, args
, 1);
2399 if (type
== &PyTuple_Type
) {
2400 res
= save_tuple(self
, args
);
2403 if (type
== &PyType_Type
) {
2404 res
= save_global(self
, args
, NULL
);
2410 if (type
== &PyList_Type
) {
2411 res
= save_list(self
, args
);
2417 if (type
== &PyDict_Type
) {
2418 res
= save_dict(self
, args
);
2424 if (type
== &PyInstance_Type
) {
2425 res
= save_inst(self
, args
);
2431 if (type
== &PyClass_Type
) {
2432 res
= save_global(self
, args
, NULL
);
2438 if (type
== &PyFunction_Type
) {
2439 res
= save_global(self
, args
, NULL
);
2440 if (res
&& PyErr_ExceptionMatches(PickleError
)) {
2441 /* fall back to reduce */
2450 if (type
== &PyCFunction_Type
) {
2451 res
= save_global(self
, args
, NULL
);
2456 if (!pers_save
&& self
->inst_pers_func
) {
2457 if ((tmp
= save_pers(self
, args
, self
->inst_pers_func
)) != 0) {
2463 if (PyType_IsSubtype(type
, &PyType_Type
)) {
2464 res
= save_global(self
, args
, NULL
);
2468 /* Get a reduction callable, and call it. This may come from
2469 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2470 * or the object's __reduce__ method.
2472 __reduce__
= PyDict_GetItem(dispatch_table
, (PyObject
*)type
);
2473 if (__reduce__
!= NULL
) {
2474 Py_INCREF(__reduce__
);
2476 ARG_TUP(self
, args
);
2478 t
= PyObject_Call(__reduce__
, self
->arg
, NULL
);
2483 /* Check for a __reduce_ex__ method. */
2484 __reduce__
= PyObject_GetAttr(args
, __reduce_ex___str
);
2485 if (__reduce__
!= NULL
) {
2486 t
= PyInt_FromLong(self
->proto
);
2491 t
= PyObject_Call(__reduce__
,
2498 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2502 /* Check for a __reduce__ method. */
2503 __reduce__
= PyObject_GetAttr(args
, __reduce___str
);
2504 if (__reduce__
!= NULL
) {
2505 t
= PyObject_Call(__reduce__
,
2509 PyErr_SetObject(UnpickleableError
, args
);
2518 if (PyString_Check(t
)) {
2519 res
= save_global(self
, args
, t
);
2523 if (! PyTuple_Check(t
)) {
2524 cPickle_ErrFormat(PicklingError
, "Value returned by "
2525 "%s must be string or tuple",
2530 size
= PyTuple_Size(t
);
2531 if (size
< 2 || size
> 5) {
2532 cPickle_ErrFormat(PicklingError
, "tuple returned by "
2533 "%s must contain 2 through 5 elements",
2538 arg_tup
= PyTuple_GET_ITEM(t
, 1);
2539 if (!(PyTuple_Check(arg_tup
) || arg_tup
== Py_None
)) {
2540 cPickle_ErrFormat(PicklingError
, "Second element of "
2541 "tuple returned by %s must be a tuple",
2546 res
= save_reduce(self
, t
, args
);
2550 Py_XDECREF(py_ob_id
);
2551 Py_XDECREF(__reduce__
);
2559 dump(Picklerobject
*self
, PyObject
*args
)
2561 static char stop
= STOP
;
2563 if (self
->proto
>= 2) {
2567 assert(self
->proto
>= 0 && self
->proto
< 256);
2568 bytes
[1] = (char)self
->proto
;
2569 if (self
->write_func(self
, bytes
, 2) < 0)
2573 if (save(self
, args
, 0) < 0)
2576 if (self
->write_func(self
, &stop
, 1) < 0)
2579 if (self
->write_func(self
, NULL
, 0) < 0)
2586 Pickle_clear_memo(Picklerobject
*self
, PyObject
*args
)
2589 PyDict_Clear(self
->memo
);
2595 Pickle_getvalue(Picklerobject
*self
, PyObject
*args
)
2597 int l
, i
, rsize
, ssize
, clear
=1, lm
;
2600 char *s
, *p
, *have_get
;
2603 /* Can be called by Python code or C code */
2604 if (args
&& !PyArg_ParseTuple(args
, "|i:getvalue", &clear
))
2607 /* Check to make sure we are based on a list */
2608 if (! Pdata_Check(self
->file
)) {
2609 PyErr_SetString(PicklingError
,
2610 "Attempt to getvalue() a non-list-based pickler");
2614 /* flush write buffer */
2615 if (write_other(self
, NULL
, 0) < 0) return NULL
;
2617 data
=(Pdata
*)self
->file
;
2620 /* set up an array to hold get/put status */
2621 lm
= PyDict_Size(self
->memo
);
2622 if (lm
< 0) return NULL
;
2624 have_get
= malloc(lm
);
2625 if (have_get
== NULL
) return PyErr_NoMemory();
2626 memset(have_get
, 0, lm
);
2628 /* Scan for gets. */
2629 for (rsize
= 0, i
= l
; --i
>= 0; ) {
2632 if (PyString_Check(k
))
2633 rsize
+= PyString_GET_SIZE(k
);
2635 else if (PyInt_Check(k
)) { /* put */
2636 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2637 if (ik
>= lm
|| ik
== 0) {
2638 PyErr_SetString(PicklingError
,
2639 "Invalid get data");
2642 if (have_get
[ik
]) /* with matching get */
2643 rsize
+= ik
< 256 ? 2 : 5;
2646 else if (! (PyTuple_Check(k
) &&
2647 PyTuple_GET_SIZE(k
) == 2 &&
2648 PyInt_Check((k
= PyTuple_GET_ITEM(k
, 0))))
2650 PyErr_SetString(PicklingError
,
2651 "Unexpected data in internal list");
2656 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2657 if (ik
>= lm
|| ik
== 0) {
2658 PyErr_SetString(PicklingError
,
2659 "Invalid get data");
2663 rsize
+= ik
< 256 ? 2 : 5;
2667 /* Now generate the result */
2668 r
= PyString_FromStringAndSize(NULL
, rsize
);
2669 if (r
== NULL
) goto err
;
2670 s
= PyString_AS_STRING((PyStringObject
*)r
);
2672 for (i
= 0; i
< l
; i
++) {
2675 if (PyString_Check(k
)) {
2676 ssize
= PyString_GET_SIZE(k
);
2678 p
=PyString_AS_STRING((PyStringObject
*)k
);
2679 while (--ssize
>= 0)
2684 else if (PyTuple_Check(k
)) { /* get */
2685 ik
= PyInt_AS_LONG((PyIntObject
*)
2686 PyTuple_GET_ITEM(k
, 0));
2689 *s
++ = (int)(ik
& 0xff);
2693 *s
++ = (int)(ik
& 0xff);
2694 *s
++ = (int)((ik
>> 8) & 0xff);
2695 *s
++ = (int)((ik
>> 16) & 0xff);
2696 *s
++ = (int)((ik
>> 24) & 0xff);
2701 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2703 if (have_get
[ik
]) { /* with matching get */
2706 *s
++ = (int)(ik
& 0xff);
2710 *s
++ = (int)(ik
& 0xff);
2711 *s
++ = (int)((ik
>> 8) & 0xff);
2712 *s
++ = (int)((ik
>> 16) & 0xff);
2713 *s
++ = (int)((ik
>> 24) & 0xff);
2720 PyDict_Clear(self
->memo
);
2721 Pdata_clear(data
, 0);
2732 Pickler_dump(Picklerobject
*self
, PyObject
*args
)
2737 if (!( PyArg_ParseTuple(args
, "O|i:dump", &ob
, &get
)))
2740 if (dump(self
, ob
) < 0)
2743 if (get
) return Pickle_getvalue(self
, NULL
);
2745 /* XXX Why does dump() return self? */
2747 return (PyObject
*)self
;
2751 static struct PyMethodDef Pickler_methods
[] =
2753 {"dump", (PyCFunction
)Pickler_dump
, METH_VARARGS
,
2754 PyDoc_STR("dump(object) -- "
2755 "Write an object in pickle format to the object's pickle stream")},
2756 {"clear_memo", (PyCFunction
)Pickle_clear_memo
, METH_NOARGS
,
2757 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2758 {"getvalue", (PyCFunction
)Pickle_getvalue
, METH_VARARGS
,
2759 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2760 {NULL
, NULL
} /* sentinel */
2764 static Picklerobject
*
2765 newPicklerobject(PyObject
*file
, int proto
)
2767 Picklerobject
*self
;
2770 proto
= HIGHEST_PROTOCOL
;
2771 if (proto
> HIGHEST_PROTOCOL
) {
2772 PyErr_Format(PyExc_ValueError
, "pickle protocol %d asked for; "
2773 "the highest available protocol is %d",
2774 proto
, HIGHEST_PROTOCOL
);
2778 self
= PyObject_GC_New(Picklerobject
, &Picklertype
);
2781 self
->proto
= proto
;
2782 self
->bin
= proto
> 0;
2787 self
->pers_func
= NULL
;
2788 self
->inst_pers_func
= NULL
;
2789 self
->write_buf
= NULL
;
2792 self
->fast_container
= 0;
2793 self
->fast_memo
= NULL
;
2795 self
->dispatch_table
= NULL
;
2807 if (!( self
->memo
= PyDict_New()))
2810 if (PyFile_Check(file
)) {
2811 self
->fp
= PyFile_AsFile(file
);
2812 if (self
->fp
== NULL
) {
2813 PyErr_SetString(PyExc_ValueError
,
2814 "I/O operation on closed file");
2817 self
->write_func
= write_file
;
2819 else if (PycStringIO_OutputCheck(file
)) {
2820 self
->write_func
= write_cStringIO
;
2822 else if (file
== Py_None
) {
2823 self
->write_func
= write_none
;
2826 self
->write_func
= write_other
;
2828 if (! Pdata_Check(file
)) {
2829 self
->write
= PyObject_GetAttr(file
, write_str
);
2832 PyErr_SetString(PyExc_TypeError
,
2833 "argument must have 'write' "
2839 self
->write_buf
= (char *)PyMem_Malloc(WRITE_BUF_SIZE
);
2840 if (self
->write_buf
== NULL
) {
2846 if (PyEval_GetRestricted()) {
2847 /* Restricted execution, get private tables */
2848 PyObject
*m
= PyImport_Import(copy_reg_str
);
2852 self
->dispatch_table
= PyObject_GetAttr(m
, dispatch_table_str
);
2854 if (self
->dispatch_table
== NULL
)
2858 self
->dispatch_table
= dispatch_table
;
2859 Py_INCREF(dispatch_table
);
2861 PyObject_GC_Track(self
);
2872 get_Pickler(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2874 static char *kwlist
[] = {"file", "protocol", NULL
};
2875 PyObject
*file
= NULL
;
2879 * The documented signature is Pickler(file, protocol=0), but this
2880 * accepts Pickler() and Pickler(integer) too. The meaning then
2881 * is clear as mud, undocumented, and not supported by pickle.py.
2882 * I'm told Zope uses this, but I haven't traced into this code
2883 * far enough to figure out what it means.
2885 if (!PyArg_ParseTuple(args
, "|i:Pickler", &proto
)) {
2888 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|i:Pickler",
2889 kwlist
, &file
, &proto
))
2892 return (PyObject
*)newPicklerobject(file
, proto
);
2897 Pickler_dealloc(Picklerobject
*self
)
2899 PyObject_GC_UnTrack(self
);
2900 Py_XDECREF(self
->write
);
2901 Py_XDECREF(self
->memo
);
2902 Py_XDECREF(self
->fast_memo
);
2903 Py_XDECREF(self
->arg
);
2904 Py_XDECREF(self
->file
);
2905 Py_XDECREF(self
->pers_func
);
2906 Py_XDECREF(self
->inst_pers_func
);
2907 Py_XDECREF(self
->dispatch_table
);
2908 PyMem_Free(self
->write_buf
);
2909 self
->ob_type
->tp_free((PyObject
*)self
);
2913 Pickler_traverse(Picklerobject
*self
, visitproc visit
, void *arg
)
2915 Py_VISIT(self
->write
);
2916 Py_VISIT(self
->memo
);
2917 Py_VISIT(self
->fast_memo
);
2918 Py_VISIT(self
->arg
);
2919 Py_VISIT(self
->file
);
2920 Py_VISIT(self
->pers_func
);
2921 Py_VISIT(self
->inst_pers_func
);
2922 Py_VISIT(self
->dispatch_table
);
2927 Pickler_clear(Picklerobject
*self
)
2929 Py_CLEAR(self
->write
);
2930 Py_CLEAR(self
->memo
);
2931 Py_CLEAR(self
->fast_memo
);
2932 Py_CLEAR(self
->arg
);
2933 Py_CLEAR(self
->file
);
2934 Py_CLEAR(self
->pers_func
);
2935 Py_CLEAR(self
->inst_pers_func
);
2936 Py_CLEAR(self
->dispatch_table
);
2941 Pickler_get_pers_func(Picklerobject
*p
)
2943 if (p
->pers_func
== NULL
)
2944 PyErr_SetString(PyExc_AttributeError
, "persistent_id");
2946 Py_INCREF(p
->pers_func
);
2947 return p
->pers_func
;
2951 Pickler_set_pers_func(Picklerobject
*p
, PyObject
*v
)
2954 PyErr_SetString(PyExc_TypeError
,
2955 "attribute deletion is not supported");
2958 Py_XDECREF(p
->pers_func
);
2965 Pickler_set_inst_pers_func(Picklerobject
*p
, PyObject
*v
)
2968 PyErr_SetString(PyExc_TypeError
,
2969 "attribute deletion is not supported");
2972 Py_XDECREF(p
->inst_pers_func
);
2974 p
->inst_pers_func
= v
;
2979 Pickler_get_memo(Picklerobject
*p
)
2981 if (p
->memo
== NULL
)
2982 PyErr_SetString(PyExc_AttributeError
, "memo");
2989 Pickler_set_memo(Picklerobject
*p
, PyObject
*v
)
2992 PyErr_SetString(PyExc_TypeError
,
2993 "attribute deletion is not supported");
2996 if (!PyDict_Check(v
)) {
2997 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
3000 Py_XDECREF(p
->memo
);
3007 Pickler_get_error(Picklerobject
*p
)
3009 /* why is this an attribute on the Pickler? */
3010 Py_INCREF(PicklingError
);
3011 return PicklingError
;
3014 static PyMemberDef Pickler_members
[] = {
3015 {"binary", T_INT
, offsetof(Picklerobject
, bin
)},
3016 {"fast", T_INT
, offsetof(Picklerobject
, fast
)},
3020 static PyGetSetDef Pickler_getsets
[] = {
3021 {"persistent_id", (getter
)Pickler_get_pers_func
,
3022 (setter
)Pickler_set_pers_func
},
3023 {"inst_persistent_id", NULL
, (setter
)Pickler_set_inst_pers_func
},
3024 {"memo", (getter
)Pickler_get_memo
, (setter
)Pickler_set_memo
},
3025 {"PicklingError", (getter
)Pickler_get_error
, NULL
},
3029 PyDoc_STRVAR(Picklertype__doc__
,
3030 "Objects that know how to pickle objects\n");
3032 static PyTypeObject Picklertype
= {
3033 PyObject_HEAD_INIT(NULL
)
3035 "cPickle.Pickler", /*tp_name*/
3036 sizeof(Picklerobject
), /*tp_basicsize*/
3038 (destructor
)Pickler_dealloc
, /* tp_dealloc */
3044 0, /* tp_as_number */
3045 0, /* tp_as_sequence */
3046 0, /* tp_as_mapping */
3050 PyObject_GenericGetAttr
, /* tp_getattro */
3051 PyObject_GenericSetAttr
, /* tp_setattro */
3052 0, /* tp_as_buffer */
3053 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
3054 Picklertype__doc__
, /* tp_doc */
3055 (traverseproc
)Pickler_traverse
, /* tp_traverse */
3056 (inquiry
)Pickler_clear
, /* tp_clear */
3057 0, /* tp_richcompare */
3058 0, /* tp_weaklistoffset */
3060 0, /* tp_iternext */
3061 Pickler_methods
, /* tp_methods */
3062 Pickler_members
, /* tp_members */
3063 Pickler_getsets
, /* tp_getset */
3067 find_class(PyObject
*py_module_name
, PyObject
*py_global_name
, PyObject
*fc
)
3069 PyObject
*global
= 0, *module
;
3073 PyErr_SetString(UnpicklingError
, "Global and instance "
3074 "pickles are not supported.");
3077 return PyObject_CallFunctionObjArgs(fc
, py_module_name
,
3078 py_global_name
, NULL
);
3081 module
= PySys_GetObject("modules");
3085 module
= PyDict_GetItem(module
, py_module_name
);
3086 if (module
== NULL
) {
3087 module
= PyImport_Import(py_module_name
);
3090 global
= PyObject_GetAttr(module
, py_global_name
);
3094 global
= PyObject_GetAttr(module
, py_global_name
);
3099 marker(Unpicklerobject
*self
)
3101 if (self
->num_marks
< 1) {
3102 PyErr_SetString(UnpicklingError
, "could not find MARK");
3106 return self
->marks
[--self
->num_marks
];
3111 load_none(Unpicklerobject
*self
)
3113 PDATA_APPEND(self
->stack
, Py_None
, -1);
3120 PyErr_SetString(UnpicklingError
, "pickle data was truncated");
3125 load_int(Unpicklerobject
*self
)
3127 PyObject
*py_int
= 0;
3132 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3133 if (len
< 2) return bad_readline();
3134 if (!( s
=pystrndup(s
,len
))) return -1;
3137 l
= strtol(s
, &endptr
, 0);
3139 if (errno
|| (*endptr
!= '\n') || (endptr
[1] != '\0')) {
3140 /* Hm, maybe we've got something long. Let's try reading
3141 it as a Python long object. */
3143 py_int
= PyLong_FromString(s
, NULL
, 0);
3144 if (py_int
== NULL
) {
3145 PyErr_SetString(PyExc_ValueError
,
3146 "could not convert string to int");
3151 if (len
== 3 && (l
== 0 || l
== 1)) {
3152 if (!( py_int
= PyBool_FromLong(l
))) goto finally
;
3155 if (!( py_int
= PyInt_FromLong(l
))) goto finally
;
3160 PDATA_PUSH(self
->stack
, py_int
, -1);
3170 load_bool(Unpicklerobject
*self
, PyObject
*boolean
)
3172 assert(boolean
== Py_True
|| boolean
== Py_False
);
3173 PDATA_APPEND(self
->stack
, boolean
, -1);
3177 /* s contains x bytes of a little-endian integer. Return its value as a
3178 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3179 * int, but when x is 4 it's a signed one. This is an historical source
3180 * of x-platform bugs.
3183 calc_binint(char *s
, int x
)
3189 for (i
= 0, l
= 0L; i
< x
; i
++) {
3190 c
= (unsigned char)s
[i
];
3191 l
|= (long)c
<< (i
* 8);
3194 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3195 * is signed, so on a box with longs bigger than 4 bytes we need
3196 * to extend a BININT's sign bit to the full width.
3198 if (x
== 4 && l
& (1L << 31))
3206 load_binintx(Unpicklerobject
*self
, char *s
, int x
)
3208 PyObject
*py_int
= 0;
3211 l
= calc_binint(s
, x
);
3213 if (!( py_int
= PyInt_FromLong(l
)))
3216 PDATA_PUSH(self
->stack
, py_int
, -1);
3222 load_binint(Unpicklerobject
*self
)
3226 if (self
->read_func(self
, &s
, 4) < 0)
3229 return load_binintx(self
, s
, 4);
3234 load_binint1(Unpicklerobject
*self
)
3238 if (self
->read_func(self
, &s
, 1) < 0)
3241 return load_binintx(self
, s
, 1);
3246 load_binint2(Unpicklerobject
*self
)
3250 if (self
->read_func(self
, &s
, 2) < 0)
3253 return load_binintx(self
, s
, 2);
3257 load_long(Unpicklerobject
*self
)
3263 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3264 if (len
< 2) return bad_readline();
3265 if (!( s
=pystrndup(s
,len
))) return -1;
3267 if (!( l
= PyLong_FromString(s
, &end
, 0)))
3271 PDATA_PUSH(self
->stack
, l
, -1);
3280 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3284 load_counted_long(Unpicklerobject
*self
, int size
)
3288 unsigned char *pdata
;
3291 assert(size
== 1 || size
== 4);
3292 i
= self
->read_func(self
, &nbytes
, size
);
3293 if (i
< 0) return -1;
3295 size
= calc_binint(nbytes
, size
);
3297 /* Corrupt or hostile pickle -- we never write one like
3300 PyErr_SetString(UnpicklingError
, "LONG pickle has negative "
3306 along
= PyLong_FromLong(0L);
3308 /* Read the raw little-endian bytes & convert. */
3309 i
= self
->read_func(self
, (char **)&pdata
, size
);
3310 if (i
< 0) return -1;
3311 along
= _PyLong_FromByteArray(pdata
, (size_t)size
,
3312 1 /* little endian */, 1 /* signed */);
3316 PDATA_PUSH(self
->stack
, along
, -1);
3321 load_float(Unpicklerobject
*self
)
3323 PyObject
*py_float
= 0;
3328 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3329 if (len
< 2) return bad_readline();
3330 if (!( s
=pystrndup(s
,len
))) return -1;
3333 d
= PyOS_ascii_strtod(s
, &endptr
);
3335 if (errno
|| (endptr
[0] != '\n') || (endptr
[1] != '\0')) {
3336 PyErr_SetString(PyExc_ValueError
,
3337 "could not convert string to float");
3341 if (!( py_float
= PyFloat_FromDouble(d
)))
3345 PDATA_PUSH(self
->stack
, py_float
, -1);
3355 load_binfloat(Unpicklerobject
*self
)
3361 if (self
->read_func(self
, &p
, 8) < 0)
3364 x
= _PyFloat_Unpack8((unsigned char *)p
, 0);
3365 if (x
== -1.0 && PyErr_Occurred())
3368 py_float
= PyFloat_FromDouble(x
);
3369 if (py_float
== NULL
)
3372 PDATA_PUSH(self
->stack
, py_float
, -1);
3377 load_string(Unpicklerobject
*self
)
3383 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3384 if (len
< 2) return bad_readline();
3385 if (!( s
=pystrndup(s
,len
))) return -1;
3388 /* Strip outermost quotes */
3389 while (s
[len
-1] <= ' ')
3391 if(s
[0]=='"' && s
[len
-1]=='"'){
3395 } else if(s
[0]=='\'' && s
[len
-1]=='\''){
3401 /********************************************/
3403 str
= PyString_DecodeEscape(p
, len
, NULL
, 0, NULL
);
3406 PDATA_PUSH(self
->stack
, str
, -1);
3413 PyErr_SetString(PyExc_ValueError
,"insecure string pickle");
3419 load_binstring(Unpicklerobject
*self
)
3421 PyObject
*py_string
= 0;
3425 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3427 l
= calc_binint(s
, 4);
3429 if (self
->read_func(self
, &s
, l
) < 0)
3432 if (!( py_string
= PyString_FromStringAndSize(s
, l
)))
3435 PDATA_PUSH(self
->stack
, py_string
, -1);
3441 load_short_binstring(Unpicklerobject
*self
)
3443 PyObject
*py_string
= 0;
3447 if (self
->read_func(self
, &s
, 1) < 0)
3450 l
= (unsigned char)s
[0];
3452 if (self
->read_func(self
, &s
, l
) < 0) return -1;
3454 if (!( py_string
= PyString_FromStringAndSize(s
, l
))) return -1;
3456 PDATA_PUSH(self
->stack
, py_string
, -1);
3461 #ifdef Py_USING_UNICODE
3463 load_unicode(Unpicklerobject
*self
)
3469 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3470 if (len
< 1) return bad_readline();
3472 if (!( str
= PyUnicode_DecodeRawUnicodeEscape(s
, len
- 1, NULL
)))
3475 PDATA_PUSH(self
->stack
, str
, -1);
3484 #ifdef Py_USING_UNICODE
3486 load_binunicode(Unpicklerobject
*self
)
3492 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3494 l
= calc_binint(s
, 4);
3496 if (self
->read_func(self
, &s
, l
) < 0)
3499 if (!( unicode
= PyUnicode_DecodeUTF8(s
, l
, NULL
)))
3502 PDATA_PUSH(self
->stack
, unicode
, -1);
3509 load_tuple(Unpicklerobject
*self
)
3514 if ((i
= marker(self
)) < 0) return -1;
3515 if (!( tup
=Pdata_popTuple(self
->stack
, i
))) return -1;
3516 PDATA_PUSH(self
->stack
, tup
, -1);
3521 load_counted_tuple(Unpicklerobject
*self
, int len
)
3523 PyObject
*tup
= PyTuple_New(len
);
3528 while (--len
>= 0) {
3531 PDATA_POP(self
->stack
, element
);
3532 if (element
== NULL
)
3534 PyTuple_SET_ITEM(tup
, len
, element
);
3536 PDATA_PUSH(self
->stack
, tup
, -1);
3541 load_empty_list(Unpicklerobject
*self
)
3545 if (!( list
=PyList_New(0))) return -1;
3546 PDATA_PUSH(self
->stack
, list
, -1);
3551 load_empty_dict(Unpicklerobject
*self
)
3555 if (!( dict
=PyDict_New())) return -1;
3556 PDATA_PUSH(self
->stack
, dict
, -1);
3562 load_list(Unpicklerobject
*self
)
3567 if ((i
= marker(self
)) < 0) return -1;
3568 if (!( list
=Pdata_popList(self
->stack
, i
))) return -1;
3569 PDATA_PUSH(self
->stack
, list
, -1);
3574 load_dict(Unpicklerobject
*self
)
3576 PyObject
*dict
, *key
, *value
;
3579 if ((i
= marker(self
)) < 0) return -1;
3580 j
=self
->stack
->length
;
3582 if (!( dict
= PyDict_New())) return -1;
3584 for (k
= i
+1; k
< j
; k
+= 2) {
3585 key
=self
->stack
->data
[k
-1];
3586 value
=self
->stack
->data
[k
];
3587 if (PyDict_SetItem(dict
, key
, value
) < 0) {
3592 Pdata_clear(self
->stack
, i
);
3593 PDATA_PUSH(self
->stack
, dict
, -1);
3598 Instance_New(PyObject
*cls
, PyObject
*args
)
3602 if (PyClass_Check(cls
)) {
3605 if ((l
=PyObject_Size(args
)) < 0) goto err
;
3607 PyObject
*__getinitargs__
;
3609 __getinitargs__
= PyObject_GetAttr(cls
,
3610 __getinitargs___str
);
3611 if (!__getinitargs__
) {
3612 /* We have a class with no __getinitargs__,
3613 so bypass usual construction */
3617 if (!( inst
=PyInstance_NewRaw(cls
, NULL
)))
3621 Py_DECREF(__getinitargs__
);
3624 if ((r
=PyInstance_New(cls
, args
, NULL
))) return r
;
3628 if ((r
=PyObject_CallObject(cls
, args
))) return r
;
3632 PyObject
*tp
, *v
, *tb
, *tmp_value
;
3634 PyErr_Fetch(&tp
, &v
, &tb
);
3636 /* NULL occurs when there was a KeyboardInterrupt */
3637 if (tmp_value
== NULL
)
3638 tmp_value
= Py_None
;
3639 if ((r
= PyTuple_Pack(3, tmp_value
, cls
, args
))) {
3643 PyErr_Restore(tp
,v
,tb
);
3650 load_obj(Unpicklerobject
*self
)
3652 PyObject
*class, *tup
, *obj
=0;
3655 if ((i
= marker(self
)) < 0) return -1;
3656 if (!( tup
=Pdata_popTuple(self
->stack
, i
+1))) return -1;
3657 PDATA_POP(self
->stack
, class);
3659 obj
= Instance_New(class, tup
);
3664 if (! obj
) return -1;
3665 PDATA_PUSH(self
->stack
, obj
, -1);
3671 load_inst(Unpicklerobject
*self
)
3673 PyObject
*tup
, *class=0, *obj
=0, *module_name
, *class_name
;
3677 if ((i
= marker(self
)) < 0) return -1;
3679 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3680 if (len
< 2) return bad_readline();
3681 module_name
= PyString_FromStringAndSize(s
, len
- 1);
3682 if (!module_name
) return -1;
3684 if ((len
= self
->readline_func(self
, &s
)) >= 0) {
3685 if (len
< 2) return bad_readline();
3686 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3687 class = find_class(module_name
, class_name
,
3689 Py_DECREF(class_name
);
3692 Py_DECREF(module_name
);
3694 if (! class) return -1;
3696 if ((tup
=Pdata_popTuple(self
->stack
, i
))) {
3697 obj
= Instance_New(class, tup
);
3702 if (! obj
) return -1;
3704 PDATA_PUSH(self
->stack
, obj
, -1);
3709 load_newobj(Unpicklerobject
*self
)
3711 PyObject
*args
= NULL
;
3712 PyObject
*clsraw
= NULL
;
3713 PyTypeObject
*cls
; /* clsraw cast to its true type */
3716 /* Stack is ... cls argtuple, and we want to call
3717 * cls.__new__(cls, *argtuple).
3719 PDATA_POP(self
->stack
, args
);
3720 if (args
== NULL
) goto Fail
;
3721 if (! PyTuple_Check(args
)) {
3722 PyErr_SetString(UnpicklingError
, "NEWOBJ expected an arg "
3727 PDATA_POP(self
->stack
, clsraw
);
3728 cls
= (PyTypeObject
*)clsraw
;
3729 if (cls
== NULL
) goto Fail
;
3730 if (! PyType_Check(cls
)) {
3731 PyErr_SetString(UnpicklingError
, "NEWOBJ class argument "
3732 "isn't a type object");
3735 if (cls
->tp_new
== NULL
) {
3736 PyErr_SetString(UnpicklingError
, "NEWOBJ class argument "
3742 obj
= cls
->tp_new(cls
, args
, NULL
);
3743 if (obj
== NULL
) goto Fail
;
3747 PDATA_PUSH(self
->stack
, obj
, -1);
3757 load_global(Unpicklerobject
*self
)
3759 PyObject
*class = 0, *module_name
= 0, *class_name
= 0;
3763 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3764 if (len
< 2) return bad_readline();
3765 module_name
= PyString_FromStringAndSize(s
, len
- 1);
3766 if (!module_name
) return -1;
3768 if ((len
= self
->readline_func(self
, &s
)) >= 0) {
3770 Py_DECREF(module_name
);
3771 return bad_readline();
3773 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3774 class = find_class(module_name
, class_name
,
3776 Py_DECREF(class_name
);
3779 Py_DECREF(module_name
);
3781 if (! class) return -1;
3782 PDATA_PUSH(self
->stack
, class, -1);
3788 load_persid(Unpicklerobject
*self
)
3794 if (self
->pers_func
) {
3795 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3796 if (len
< 2) return bad_readline();
3798 pid
= PyString_FromStringAndSize(s
, len
- 1);
3799 if (!pid
) return -1;
3801 if (PyList_Check(self
->pers_func
)) {
3802 if (PyList_Append(self
->pers_func
, pid
) < 0) {
3810 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
3816 if (! pid
) return -1;
3818 PDATA_PUSH(self
->stack
, pid
, -1);
3822 PyErr_SetString(UnpicklingError
,
3823 "A load persistent id instruction was encountered,\n"
3824 "but no persistent_load function was specified.");
3830 load_binpersid(Unpicklerobject
*self
)
3834 if (self
->pers_func
) {
3835 PDATA_POP(self
->stack
, pid
);
3836 if (! pid
) return -1;
3838 if (PyList_Check(self
->pers_func
)) {
3839 if (PyList_Append(self
->pers_func
, pid
) < 0) {
3847 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
3851 if (! pid
) return -1;
3854 PDATA_PUSH(self
->stack
, pid
, -1);
3858 PyErr_SetString(UnpicklingError
,
3859 "A load persistent id instruction was encountered,\n"
3860 "but no persistent_load function was specified.");
3867 load_pop(Unpicklerobject
*self
)
3871 if (!( (len
=self
->stack
->length
) > 0 )) return stackUnderflow();
3873 /* Note that we split the (pickle.py) stack into two stacks,
3874 an object stack and a mark stack. We have to be clever and
3875 pop the right one. We do this by looking at the top of the
3879 if ((self
->num_marks
> 0) &&
3880 (self
->marks
[self
->num_marks
- 1] == len
))
3884 Py_DECREF(self
->stack
->data
[len
]);
3885 self
->stack
->length
=len
;
3893 load_pop_mark(Unpicklerobject
*self
)
3897 if ((i
= marker(self
)) < 0)
3900 Pdata_clear(self
->stack
, i
);
3907 load_dup(Unpicklerobject
*self
)
3912 if ((len
= self
->stack
->length
) <= 0) return stackUnderflow();
3913 last
=self
->stack
->data
[len
-1];
3915 PDATA_PUSH(self
->stack
, last
, -1);
3921 load_get(Unpicklerobject
*self
)
3923 PyObject
*py_str
= 0, *value
= 0;
3928 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3929 if (len
< 2) return bad_readline();
3931 if (!( py_str
= PyString_FromStringAndSize(s
, len
- 1))) return -1;
3933 value
= PyDict_GetItem(self
->memo
, py_str
);
3935 PyErr_SetObject(BadPickleGet
, py_str
);
3939 PDATA_APPEND(self
->stack
, value
, -1);
3949 load_binget(Unpicklerobject
*self
)
3951 PyObject
*py_key
= 0, *value
= 0;
3956 if (self
->read_func(self
, &s
, 1) < 0) return -1;
3958 key
= (unsigned char)s
[0];
3959 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
3961 value
= PyDict_GetItem(self
->memo
, py_key
);
3963 PyErr_SetObject(BadPickleGet
, py_key
);
3967 PDATA_APPEND(self
->stack
, value
, -1);
3977 load_long_binget(Unpicklerobject
*self
)
3979 PyObject
*py_key
= 0, *value
= 0;
3985 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3987 c
= (unsigned char)s
[0];
3989 c
= (unsigned char)s
[1];
3990 key
|= (long)c
<< 8;
3991 c
= (unsigned char)s
[2];
3992 key
|= (long)c
<< 16;
3993 c
= (unsigned char)s
[3];
3994 key
|= (long)c
<< 24;
3996 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
3998 value
= PyDict_GetItem(self
->memo
, py_key
);
4000 PyErr_SetObject(BadPickleGet
, py_key
);
4004 PDATA_APPEND(self
->stack
, value
, -1);
4012 /* Push an object from the extension registry (EXT[124]). nbytes is
4013 * the number of bytes following the opcode, holding the index (code) value.
4016 load_extension(Unpicklerobject
*self
, int nbytes
)
4018 char *codebytes
; /* the nbytes bytes after the opcode */
4019 long code
; /* calc_binint returns long */
4020 PyObject
*py_code
; /* code as a Python int */
4021 PyObject
*obj
; /* the object to push */
4022 PyObject
*pair
; /* (module_name, class_name) */
4023 PyObject
*module_name
, *class_name
;
4025 assert(nbytes
== 1 || nbytes
== 2 || nbytes
== 4);
4026 if (self
->read_func(self
, &codebytes
, nbytes
) < 0) return -1;
4027 code
= calc_binint(codebytes
, nbytes
);
4028 if (code
<= 0) { /* note that 0 is forbidden */
4029 /* Corrupt or hostile pickle. */
4030 PyErr_SetString(UnpicklingError
, "EXT specifies code <= 0");
4034 /* Look for the code in the cache. */
4035 py_code
= PyInt_FromLong(code
);
4036 if (py_code
== NULL
) return -1;
4037 obj
= PyDict_GetItem(extension_cache
, py_code
);
4041 PDATA_APPEND(self
->stack
, obj
, -1);
4045 /* Look up the (module_name, class_name) pair. */
4046 pair
= PyDict_GetItem(inverted_registry
, py_code
);
4049 PyErr_Format(PyExc_ValueError
, "unregistered extension "
4053 /* Since the extension registry is manipulable via Python code,
4054 * confirm that pair is really a 2-tuple of strings.
4056 if (!PyTuple_Check(pair
) || PyTuple_Size(pair
) != 2 ||
4057 !PyString_Check(module_name
= PyTuple_GET_ITEM(pair
, 0)) ||
4058 !PyString_Check(class_name
= PyTuple_GET_ITEM(pair
, 1))) {
4060 PyErr_Format(PyExc_ValueError
, "_inverted_registry[%ld] "
4061 "isn't a 2-tuple of strings", code
);
4064 /* Load the object. */
4065 obj
= find_class(module_name
, class_name
, self
->find_class
);
4070 /* Cache code -> obj. */
4071 code
= PyDict_SetItem(extension_cache
, py_code
, obj
);
4077 PDATA_PUSH(self
->stack
, obj
, -1);
4082 load_put(Unpicklerobject
*self
)
4084 PyObject
*py_str
= 0, *value
= 0;
4088 if ((l
= self
->readline_func(self
, &s
)) < 0) return -1;
4089 if (l
< 2) return bad_readline();
4090 if (!( len
=self
->stack
->length
)) return stackUnderflow();
4091 if (!( py_str
= PyString_FromStringAndSize(s
, l
- 1))) return -1;
4092 value
=self
->stack
->data
[len
-1];
4093 l
=PyDict_SetItem(self
->memo
, py_str
, value
);
4100 load_binput(Unpicklerobject
*self
)
4102 PyObject
*py_key
= 0, *value
= 0;
4107 if (self
->read_func(self
, &s
, 1) < 0) return -1;
4108 if (!( (len
=self
->stack
->length
) > 0 )) return stackUnderflow();
4110 key
= (unsigned char)s
[0];
4112 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
4113 value
=self
->stack
->data
[len
-1];
4114 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
4121 load_long_binput(Unpicklerobject
*self
)
4123 PyObject
*py_key
= 0, *value
= 0;
4129 if (self
->read_func(self
, &s
, 4) < 0) return -1;
4130 if (!( len
=self
->stack
->length
)) return stackUnderflow();
4132 c
= (unsigned char)s
[0];
4134 c
= (unsigned char)s
[1];
4135 key
|= (long)c
<< 8;
4136 c
= (unsigned char)s
[2];
4137 key
|= (long)c
<< 16;
4138 c
= (unsigned char)s
[3];
4139 key
|= (long)c
<< 24;
4141 if (!( py_key
= PyInt_FromLong(key
))) return -1;
4142 value
=self
->stack
->data
[len
-1];
4143 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
4150 do_append(Unpicklerobject
*self
, int x
)
4152 PyObject
*value
= 0, *list
= 0, *append_method
= 0;
4155 len
=self
->stack
->length
;
4156 if (!( len
>= x
&& x
> 0 )) return stackUnderflow();
4158 if (len
==x
) return 0;
4160 list
=self
->stack
->data
[x
-1];
4162 if (PyList_Check(list
)) {
4166 slice
=Pdata_popList(self
->stack
, x
);
4167 if (! slice
) return -1;
4168 list_len
= PyList_GET_SIZE(list
);
4169 i
=PyList_SetSlice(list
, list_len
, list_len
, slice
);
4175 if (!( append_method
= PyObject_GetAttr(list
, append_str
)))
4178 for (i
= x
; i
< len
; i
++) {
4181 value
=self
->stack
->data
[i
];
4183 ARG_TUP(self
, value
);
4185 junk
= PyObject_Call(append_method
, self
->arg
,
4190 Pdata_clear(self
->stack
, i
+1);
4191 self
->stack
->length
=x
;
4192 Py_DECREF(append_method
);
4197 self
->stack
->length
=x
;
4198 Py_DECREF(append_method
);
4206 load_append(Unpicklerobject
*self
)
4208 return do_append(self
, self
->stack
->length
- 1);
4213 load_appends(Unpicklerobject
*self
)
4215 return do_append(self
, marker(self
));
4220 do_setitems(Unpicklerobject
*self
, int x
)
4222 PyObject
*value
= 0, *key
= 0, *dict
= 0;
4225 if (!( (len
=self
->stack
->length
) >= x
4226 && x
> 0 )) return stackUnderflow();
4228 dict
=self
->stack
->data
[x
-1];
4230 for (i
= x
+1; i
< len
; i
+= 2) {
4231 key
=self
->stack
->data
[i
-1];
4232 value
=self
->stack
->data
[i
];
4233 if (PyObject_SetItem(dict
, key
, value
) < 0) {
4239 Pdata_clear(self
->stack
, x
);
4246 load_setitem(Unpicklerobject
*self
)
4248 return do_setitems(self
, self
->stack
->length
- 2);
4252 load_setitems(Unpicklerobject
*self
)
4254 return do_setitems(self
, marker(self
));
4259 load_build(Unpicklerobject
*self
)
4261 PyObject
*state
, *inst
, *slotstate
;
4262 PyObject
*__setstate__
;
4263 PyObject
*d_key
, *d_value
;
4267 /* Stack is ... instance, state. We want to leave instance at
4268 * the stack top, possibly mutated via instance.__setstate__(state).
4270 if (self
->stack
->length
< 2)
4271 return stackUnderflow();
4272 PDATA_POP(self
->stack
, state
);
4275 inst
= self
->stack
->data
[self
->stack
->length
- 1];
4277 __setstate__
= PyObject_GetAttr(inst
, __setstate___str
);
4278 if (__setstate__
!= NULL
) {
4279 PyObject
*junk
= NULL
;
4281 /* The explicit __setstate__ is responsible for everything. */
4282 ARG_TUP(self
, state
);
4284 junk
= PyObject_Call(__setstate__
, self
->arg
, NULL
);
4287 Py_DECREF(__setstate__
);
4293 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4297 /* A default __setstate__. First see whether state embeds a
4298 * slot state dict too (a proto 2 addition).
4300 if (PyTuple_Check(state
) && PyTuple_Size(state
) == 2) {
4301 PyObject
*temp
= state
;
4302 state
= PyTuple_GET_ITEM(temp
, 0);
4303 slotstate
= PyTuple_GET_ITEM(temp
, 1);
4305 Py_INCREF(slotstate
);
4311 /* Set inst.__dict__ from the state dict (if any). */
4312 if (state
!= Py_None
) {
4314 if (! PyDict_Check(state
)) {
4315 PyErr_SetString(UnpicklingError
, "state is not a "
4319 dict
= PyObject_GetAttr(inst
, __dict___str
);
4324 while (PyDict_Next(state
, &i
, &d_key
, &d_value
)) {
4325 if (PyObject_SetItem(dict
, d_key
, d_value
) < 0)
4331 /* Also set instance attributes from the slotstate dict (if any). */
4332 if (slotstate
!= NULL
) {
4333 if (! PyDict_Check(slotstate
)) {
4334 PyErr_SetString(UnpicklingError
, "slot state is not "
4339 while (PyDict_Next(slotstate
, &i
, &d_key
, &d_value
)) {
4340 if (PyObject_SetAttr(inst
, d_key
, d_value
) < 0)
4348 Py_XDECREF(slotstate
);
4354 load_mark(Unpicklerobject
*self
)
4358 /* Note that we split the (pickle.py) stack into two stacks, an
4359 object stack and a mark stack. Here we push a mark onto the
4363 if ((self
->num_marks
+ 1) >= self
->marks_size
) {
4364 s
=self
->marks_size
+20;
4365 if (s
<= self
->num_marks
) s
=self
->num_marks
+ 1;
4366 if (self
->marks
== NULL
)
4367 self
->marks
=(int *)malloc(s
* sizeof(int));
4369 self
->marks
=(int *)realloc(self
->marks
,
4371 if (! self
->marks
) {
4375 self
->marks_size
= s
;
4378 self
->marks
[self
->num_marks
++] = self
->stack
->length
;
4384 load_reduce(Unpicklerobject
*self
)
4386 PyObject
*callable
= 0, *arg_tup
= 0, *ob
= 0;
4388 PDATA_POP(self
->stack
, arg_tup
);
4389 if (! arg_tup
) return -1;
4390 PDATA_POP(self
->stack
, callable
);
4392 ob
= Instance_New(callable
, arg_tup
);
4393 Py_DECREF(callable
);
4397 if (! ob
) return -1;
4399 PDATA_PUSH(self
->stack
, ob
, -1);
4403 /* Just raises an error if we don't know the protocol specified. PROTO
4404 * is the first opcode for protocols >= 2.
4407 load_proto(Unpicklerobject
*self
)
4412 i
= self
->read_func(self
, &protobyte
, 1);
4416 i
= calc_binint(protobyte
, 1);
4417 /* No point checking for < 0, since calc_binint returns an unsigned
4418 * int when chewing on 1 byte.
4421 if (i
<= HIGHEST_PROTOCOL
)
4424 PyErr_Format(PyExc_ValueError
, "unsupported pickle protocol: %d", i
);
4429 load(Unpicklerobject
*self
)
4431 PyObject
*err
= 0, *val
= 0;
4434 self
->num_marks
= 0;
4435 if (self
->stack
->length
) Pdata_clear(self
->stack
, 0);
4438 if (self
->read_func(self
, &s
, 1) < 0)
4443 if (load_none(self
) < 0)
4448 if (load_binint(self
) < 0)
4453 if (load_binint1(self
) < 0)
4458 if (load_binint2(self
) < 0)
4463 if (load_int(self
) < 0)
4468 if (load_long(self
) < 0)
4473 if (load_counted_long(self
, 1) < 0)
4478 if (load_counted_long(self
, 4) < 0)
4483 if (load_float(self
) < 0)
4488 if (load_binfloat(self
) < 0)
4493 if (load_binstring(self
) < 0)
4497 case SHORT_BINSTRING
:
4498 if (load_short_binstring(self
) < 0)
4503 if (load_string(self
) < 0)
4507 #ifdef Py_USING_UNICODE
4509 if (load_unicode(self
) < 0)
4514 if (load_binunicode(self
) < 0)
4520 if (load_counted_tuple(self
, 0) < 0)
4525 if (load_counted_tuple(self
, 1) < 0)
4530 if (load_counted_tuple(self
, 2) < 0)
4535 if (load_counted_tuple(self
, 3) < 0)
4540 if (load_tuple(self
) < 0)
4545 if (load_empty_list(self
) < 0)
4550 if (load_list(self
) < 0)
4555 if (load_empty_dict(self
) < 0)
4560 if (load_dict(self
) < 0)
4565 if (load_obj(self
) < 0)
4570 if (load_inst(self
) < 0)
4575 if (load_newobj(self
) < 0)
4580 if (load_global(self
) < 0)
4585 if (load_append(self
) < 0)
4590 if (load_appends(self
) < 0)
4595 if (load_build(self
) < 0)
4600 if (load_dup(self
) < 0)
4605 if (load_binget(self
) < 0)
4610 if (load_long_binget(self
) < 0)
4615 if (load_get(self
) < 0)
4620 if (load_extension(self
, 1) < 0)
4625 if (load_extension(self
, 2) < 0)
4630 if (load_extension(self
, 4) < 0)
4634 if (load_mark(self
) < 0)
4639 if (load_binput(self
) < 0)
4644 if (load_long_binput(self
) < 0)
4649 if (load_put(self
) < 0)
4654 if (load_pop(self
) < 0)
4659 if (load_pop_mark(self
) < 0)
4664 if (load_setitem(self
) < 0)
4669 if (load_setitems(self
) < 0)
4677 if (load_persid(self
) < 0)
4682 if (load_binpersid(self
) < 0)
4687 if (load_reduce(self
) < 0)
4692 if (load_proto(self
) < 0)
4697 if (load_bool(self
, Py_True
) < 0)
4702 if (load_bool(self
, Py_False
) < 0)
4708 PyErr_SetNone(PyExc_EOFError
);
4712 cPickle_ErrFormat(UnpicklingError
,
4713 "invalid load key, '%s'.",
4721 if ((err
= PyErr_Occurred())) {
4722 if (err
== PyExc_EOFError
) {
4723 PyErr_SetNone(PyExc_EOFError
);
4728 PDATA_POP(self
->stack
, val
);
4733 /* No-load functions to support noload, which is used to
4734 find persistent references. */
4737 noload_obj(Unpicklerobject
*self
)
4741 if ((i
= marker(self
)) < 0) return -1;
4742 return Pdata_clear(self
->stack
, i
+1);
4747 noload_inst(Unpicklerobject
*self
)
4752 if ((i
= marker(self
)) < 0) return -1;
4753 Pdata_clear(self
->stack
, i
);
4754 if (self
->readline_func(self
, &s
) < 0) return -1;
4755 if (self
->readline_func(self
, &s
) < 0) return -1;
4756 PDATA_APPEND(self
->stack
, Py_None
, -1);
4761 noload_newobj(Unpicklerobject
*self
)
4765 PDATA_POP(self
->stack
, obj
); /* pop argtuple */
4766 if (obj
== NULL
) return -1;
4769 PDATA_POP(self
->stack
, obj
); /* pop cls */
4770 if (obj
== NULL
) return -1;
4773 PDATA_APPEND(self
->stack
, Py_None
, -1);
4778 noload_global(Unpicklerobject
*self
)
4782 if (self
->readline_func(self
, &s
) < 0) return -1;
4783 if (self
->readline_func(self
, &s
) < 0) return -1;
4784 PDATA_APPEND(self
->stack
, Py_None
,-1);
4789 noload_reduce(Unpicklerobject
*self
)
4792 if (self
->stack
->length
< 2) return stackUnderflow();
4793 Pdata_clear(self
->stack
, self
->stack
->length
-2);
4794 PDATA_APPEND(self
->stack
, Py_None
,-1);
4799 noload_build(Unpicklerobject
*self
) {
4801 if (self
->stack
->length
< 1) return stackUnderflow();
4802 Pdata_clear(self
->stack
, self
->stack
->length
-1);
4807 noload_extension(Unpicklerobject
*self
, int nbytes
)
4811 assert(nbytes
== 1 || nbytes
== 2 || nbytes
== 4);
4812 if (self
->read_func(self
, &codebytes
, nbytes
) < 0) return -1;
4813 PDATA_APPEND(self
->stack
, Py_None
, -1);
4819 noload(Unpicklerobject
*self
)
4821 PyObject
*err
= 0, *val
= 0;
4824 self
->num_marks
= 0;
4825 Pdata_clear(self
->stack
, 0);
4828 if (self
->read_func(self
, &s
, 1) < 0)
4833 if (load_none(self
) < 0)
4838 if (load_binint(self
) < 0)
4843 if (load_binint1(self
) < 0)
4848 if (load_binint2(self
) < 0)
4853 if (load_int(self
) < 0)
4858 if (load_long(self
) < 0)
4863 if (load_counted_long(self
, 1) < 0)
4868 if (load_counted_long(self
, 4) < 0)
4873 if (load_float(self
) < 0)
4878 if (load_binfloat(self
) < 0)
4883 if (load_binstring(self
) < 0)
4887 case SHORT_BINSTRING
:
4888 if (load_short_binstring(self
) < 0)
4893 if (load_string(self
) < 0)
4897 #ifdef Py_USING_UNICODE
4899 if (load_unicode(self
) < 0)
4904 if (load_binunicode(self
) < 0)
4910 if (load_counted_tuple(self
, 0) < 0)
4915 if (load_counted_tuple(self
, 1) < 0)
4920 if (load_counted_tuple(self
, 2) < 0)
4925 if (load_counted_tuple(self
, 3) < 0)
4930 if (load_tuple(self
) < 0)
4935 if (load_empty_list(self
) < 0)
4940 if (load_list(self
) < 0)
4945 if (load_empty_dict(self
) < 0)
4950 if (load_dict(self
) < 0)
4955 if (noload_obj(self
) < 0)
4960 if (noload_inst(self
) < 0)
4965 if (noload_newobj(self
) < 0)
4970 if (noload_global(self
) < 0)
4975 if (load_append(self
) < 0)
4980 if (load_appends(self
) < 0)
4985 if (noload_build(self
) < 0)
4990 if (load_dup(self
) < 0)
4995 if (load_binget(self
) < 0)
5000 if (load_long_binget(self
) < 0)
5005 if (load_get(self
) < 0)
5010 if (noload_extension(self
, 1) < 0)
5015 if (noload_extension(self
, 2) < 0)
5020 if (noload_extension(self
, 4) < 0)
5025 if (load_mark(self
) < 0)
5030 if (load_binput(self
) < 0)
5035 if (load_long_binput(self
) < 0)
5040 if (load_put(self
) < 0)
5045 if (load_pop(self
) < 0)
5050 if (load_pop_mark(self
) < 0)
5055 if (load_setitem(self
) < 0)
5060 if (load_setitems(self
) < 0)
5068 if (load_persid(self
) < 0)
5073 if (load_binpersid(self
) < 0)
5078 if (noload_reduce(self
) < 0)
5083 if (load_proto(self
) < 0)
5088 if (load_bool(self
, Py_True
) < 0)
5093 if (load_bool(self
, Py_False
) < 0)
5097 cPickle_ErrFormat(UnpicklingError
,
5098 "invalid load key, '%s'.",
5106 if ((err
= PyErr_Occurred())) {
5107 if (err
== PyExc_EOFError
) {
5108 PyErr_SetNone(PyExc_EOFError
);
5113 PDATA_POP(self
->stack
, val
);
5119 Unpickler_load(Unpicklerobject
*self
, PyObject
*unused
)
5125 Unpickler_noload(Unpicklerobject
*self
, PyObject
*unused
)
5127 return noload(self
);
5131 static struct PyMethodDef Unpickler_methods
[] = {
5132 {"load", (PyCFunction
)Unpickler_load
, METH_NOARGS
,
5133 PyDoc_STR("load() -- Load a pickle")
5135 {"noload", (PyCFunction
)Unpickler_noload
, METH_NOARGS
,
5137 "noload() -- not load a pickle, but go through most of the motions\n"
5139 "This function can be used to read past a pickle without instantiating\n"
5140 "any objects or importing any modules. It can also be used to find all\n"
5141 "persistent references without instantiating any objects or importing\n"
5144 {NULL
, NULL
} /* sentinel */
5148 static Unpicklerobject
*
5149 newUnpicklerobject(PyObject
*f
)
5151 Unpicklerobject
*self
;
5153 if (!( self
= PyObject_GC_New(Unpicklerobject
, &Unpicklertype
)))
5158 self
->stack
= (Pdata
*)Pdata_New();
5159 self
->pers_func
= NULL
;
5160 self
->last_string
= NULL
;
5162 self
->num_marks
= 0;
5163 self
->marks_size
= 0;
5166 self
->readline
= NULL
;
5167 self
->find_class
= NULL
;
5169 if (!( self
->memo
= PyDict_New()))
5178 /* Set read, readline based on type of f */
5179 if (PyFile_Check(f
)) {
5180 self
->fp
= PyFile_AsFile(f
);
5181 if (self
->fp
== NULL
) {
5182 PyErr_SetString(PyExc_ValueError
,
5183 "I/O operation on closed file");
5186 self
->read_func
= read_file
;
5187 self
->readline_func
= readline_file
;
5189 else if (PycStringIO_InputCheck(f
)) {
5191 self
->read_func
= read_cStringIO
;
5192 self
->readline_func
= readline_cStringIO
;
5197 self
->read_func
= read_other
;
5198 self
->readline_func
= readline_other
;
5200 if (!( (self
->readline
= PyObject_GetAttr(f
, readline_str
)) &&
5201 (self
->read
= PyObject_GetAttr(f
, read_str
)))) {
5203 PyErr_SetString( PyExc_TypeError
,
5204 "argument must have 'read' and "
5205 "'readline' attributes" );
5209 PyObject_GC_Track(self
);
5214 Py_DECREF((PyObject
*)self
);
5220 get_Unpickler(PyObject
*self
, PyObject
*file
)
5222 return (PyObject
*)newUnpicklerobject(file
);
5227 Unpickler_dealloc(Unpicklerobject
*self
)
5229 PyObject_GC_UnTrack((PyObject
*)self
);
5230 Py_XDECREF(self
->readline
);
5231 Py_XDECREF(self
->read
);
5232 Py_XDECREF(self
->file
);
5233 Py_XDECREF(self
->memo
);
5234 Py_XDECREF(self
->stack
);
5235 Py_XDECREF(self
->pers_func
);
5236 Py_XDECREF(self
->arg
);
5237 Py_XDECREF(self
->last_string
);
5238 Py_XDECREF(self
->find_class
);
5244 if (self
->buf_size
) {
5248 self
->ob_type
->tp_free((PyObject
*)self
);
5252 Unpickler_traverse(Unpicklerobject
*self
, visitproc visit
, void *arg
)
5254 Py_VISIT(self
->readline
);
5255 Py_VISIT(self
->read
);
5256 Py_VISIT(self
->file
);
5257 Py_VISIT(self
->memo
);
5258 Py_VISIT(self
->stack
);
5259 Py_VISIT(self
->pers_func
);
5260 Py_VISIT(self
->arg
);
5261 Py_VISIT(self
->last_string
);
5262 Py_VISIT(self
->find_class
);
5267 Unpickler_clear(Unpicklerobject
*self
)
5269 Py_CLEAR(self
->readline
);
5270 Py_CLEAR(self
->read
);
5271 Py_CLEAR(self
->file
);
5272 Py_CLEAR(self
->memo
);
5273 Py_CLEAR(self
->stack
);
5274 Py_CLEAR(self
->pers_func
);
5275 Py_CLEAR(self
->arg
);
5276 Py_CLEAR(self
->last_string
);
5277 Py_CLEAR(self
->find_class
);
5282 Unpickler_getattr(Unpicklerobject
*self
, char *name
)
5284 if (!strcmp(name
, "persistent_load")) {
5285 if (!self
->pers_func
) {
5286 PyErr_SetString(PyExc_AttributeError
, name
);
5290 Py_INCREF(self
->pers_func
);
5291 return self
->pers_func
;
5294 if (!strcmp(name
, "find_global")) {
5295 if (!self
->find_class
) {
5296 PyErr_SetString(PyExc_AttributeError
, name
);
5300 Py_INCREF(self
->find_class
);
5301 return self
->find_class
;
5304 if (!strcmp(name
, "memo")) {
5306 PyErr_SetString(PyExc_AttributeError
, name
);
5310 Py_INCREF(self
->memo
);
5314 if (!strcmp(name
, "UnpicklingError")) {
5315 Py_INCREF(UnpicklingError
);
5316 return UnpicklingError
;
5319 return Py_FindMethod(Unpickler_methods
, (PyObject
*)self
, name
);
5324 Unpickler_setattr(Unpicklerobject
*self
, char *name
, PyObject
*value
)
5327 if (!strcmp(name
, "persistent_load")) {
5328 Py_XDECREF(self
->pers_func
);
5329 self
->pers_func
= value
;
5334 if (!strcmp(name
, "find_global")) {
5335 Py_XDECREF(self
->find_class
);
5336 self
->find_class
= value
;
5342 PyErr_SetString(PyExc_TypeError
,
5343 "attribute deletion is not supported");
5347 if (strcmp(name
, "memo") == 0) {
5348 if (!PyDict_Check(value
)) {
5349 PyErr_SetString(PyExc_TypeError
,
5350 "memo must be a dictionary");
5353 Py_XDECREF(self
->memo
);
5359 PyErr_SetString(PyExc_AttributeError
, name
);
5363 /* ---------------------------------------------------------------------------
5364 * Module-level functions.
5367 /* dump(obj, file, protocol=0). */
5369 cpm_dump(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5371 static char *kwlist
[] = {"obj", "file", "protocol", NULL
};
5372 PyObject
*ob
, *file
, *res
= NULL
;
5373 Picklerobject
*pickler
= 0;
5376 if (!( PyArg_ParseTupleAndKeywords(args
, kwds
, "OO|i", kwlist
,
5377 &ob
, &file
, &proto
)))
5380 if (!( pickler
= newPicklerobject(file
, proto
)))
5383 if (dump(pickler
, ob
) < 0)
5390 Py_XDECREF(pickler
);
5396 /* dumps(obj, protocol=0). */
5398 cpm_dumps(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5400 static char *kwlist
[] = {"obj", "protocol", NULL
};
5401 PyObject
*ob
, *file
= 0, *res
= NULL
;
5402 Picklerobject
*pickler
= 0;
5405 if (!( PyArg_ParseTupleAndKeywords(args
, kwds
, "O|i:dumps", kwlist
,
5409 if (!( file
= PycStringIO
->NewOutput(128)))
5412 if (!( pickler
= newPicklerobject(file
, proto
)))
5415 if (dump(pickler
, ob
) < 0)
5418 res
= PycStringIO
->cgetvalue(file
);
5421 Py_XDECREF(pickler
);
5428 /* load(fileobj). */
5430 cpm_load(PyObject
*self
, PyObject
*ob
)
5432 Unpicklerobject
*unpickler
= 0;
5433 PyObject
*res
= NULL
;
5435 if (!( unpickler
= newUnpicklerobject(ob
)))
5438 res
= load(unpickler
);
5441 Py_XDECREF(unpickler
);
5449 cpm_loads(PyObject
*self
, PyObject
*args
)
5451 PyObject
*ob
, *file
= 0, *res
= NULL
;
5452 Unpicklerobject
*unpickler
= 0;
5454 if (!( PyArg_ParseTuple(args
, "S:loads", &ob
)))
5457 if (!( file
= PycStringIO
->NewInput(ob
)))
5460 if (!( unpickler
= newUnpicklerobject(file
)))
5463 res
= load(unpickler
);
5467 Py_XDECREF(unpickler
);
5473 PyDoc_STRVAR(Unpicklertype__doc__
,
5474 "Objects that know how to unpickle");
5476 static PyTypeObject Unpicklertype
= {
5477 PyObject_HEAD_INIT(NULL
)
5479 "cPickle.Unpickler", /*tp_name*/
5480 sizeof(Unpicklerobject
), /*tp_basicsize*/
5482 (destructor
)Unpickler_dealloc
, /* tp_dealloc */
5484 (getattrfunc
)Unpickler_getattr
, /* tp_getattr */
5485 (setattrfunc
)Unpickler_setattr
, /* tp_setattr */
5488 0, /* tp_as_number */
5489 0, /* tp_as_sequence */
5490 0, /* tp_as_mapping */
5494 0, /* tp_getattro */
5495 0, /* tp_setattro */
5496 0, /* tp_as_buffer */
5497 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
5498 Unpicklertype__doc__
, /* tp_doc */
5499 (traverseproc
)Unpickler_traverse
, /* tp_traverse */
5500 (inquiry
)Unpickler_clear
, /* tp_clear */
5503 static struct PyMethodDef cPickle_methods
[] = {
5504 {"dump", (PyCFunction
)cpm_dump
, METH_VARARGS
| METH_KEYWORDS
,
5505 PyDoc_STR("dump(obj, file, protocol=0) -- "
5506 "Write an object in pickle format to the given file.\n"
5508 "See the Pickler docstring for the meaning of optional argument proto.")
5511 {"dumps", (PyCFunction
)cpm_dumps
, METH_VARARGS
| METH_KEYWORDS
,
5512 PyDoc_STR("dumps(obj, protocol=0) -- "
5513 "Return a string containing an object in pickle format.\n"
5515 "See the Pickler docstring for the meaning of optional argument proto.")
5518 {"load", (PyCFunction
)cpm_load
, METH_O
,
5519 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5521 {"loads", (PyCFunction
)cpm_loads
, METH_VARARGS
,
5522 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5524 {"Pickler", (PyCFunction
)get_Pickler
, METH_VARARGS
| METH_KEYWORDS
,
5525 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5527 "This takes a file-like object for writing a pickle data stream.\n"
5528 "The optional proto argument tells the pickler to use the given\n"
5529 "protocol; supported protocols are 0, 1, 2. The default\n"
5530 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5531 "only protocol that can be written to a file opened in text\n"
5532 "mode and read back successfully. When using a protocol higher\n"
5533 "than 0, make sure the file is opened in binary mode, both when\n"
5534 "pickling and unpickling.)\n"
5536 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5537 "more efficient than protocol 1.\n"
5539 "Specifying a negative protocol version selects the highest\n"
5540 "protocol version supported. The higher the protocol used, the\n"
5541 "more recent the version of Python needed to read the pickle\n"
5544 "The file parameter must have a write() method that accepts a single\n"
5545 "string argument. It can thus be an open file object, a StringIO\n"
5546 "object, or any other custom object that meets this interface.\n")
5549 {"Unpickler", (PyCFunction
)get_Unpickler
, METH_O
,
5550 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5556 init_stuff(PyObject
*module_dict
)
5558 PyObject
*copy_reg
, *t
, *r
;
5560 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5562 if (PyType_Ready(&Unpicklertype
) < 0)
5564 if (PyType_Ready(&Picklertype
) < 0)
5567 INIT_STR(__class__
);
5568 INIT_STR(__getinitargs__
);
5570 INIT_STR(__getstate__
);
5571 INIT_STR(__setstate__
);
5574 INIT_STR(__reduce__
);
5575 INIT_STR(__reduce_ex__
);
5581 INIT_STR(dispatch_table
);
5583 if (!( copy_reg
= PyImport_ImportModule("copy_reg")))
5586 /* This is special because we want to use a different
5587 one in restricted mode. */
5588 dispatch_table
= PyObject_GetAttr(copy_reg
, dispatch_table_str
);
5589 if (!dispatch_table
) return -1;
5591 extension_registry
= PyObject_GetAttrString(copy_reg
,
5592 "_extension_registry");
5593 if (!extension_registry
) return -1;
5595 inverted_registry
= PyObject_GetAttrString(copy_reg
,
5596 "_inverted_registry");
5597 if (!inverted_registry
) return -1;
5599 extension_cache
= PyObject_GetAttrString(copy_reg
,
5600 "_extension_cache");
5601 if (!extension_cache
) return -1;
5603 Py_DECREF(copy_reg
);
5605 if (!(empty_tuple
= PyTuple_New(0)))
5608 two_tuple
= PyTuple_New(2);
5609 if (two_tuple
== NULL
)
5611 /* We use this temp container with no regard to refcounts, or to
5612 * keeping containees alive. Exempt from GC, because we don't
5613 * want anything looking at two_tuple() by magic.
5615 PyObject_GC_UnTrack(two_tuple
);
5618 if (!( t
=PyImport_ImportModule("__builtin__"))) return -1;
5619 if (PyDict_SetItemString(module_dict
, "__builtins__", t
) < 0)
5622 if (!( t
=PyDict_New())) return -1;
5623 if (!( r
=PyRun_String(
5624 "def __str__(self):\n"
5625 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5627 module_dict
, t
) )) return -1;
5630 PickleError
= PyErr_NewException("cPickle.PickleError", NULL
, t
);
5636 PicklingError
= PyErr_NewException("cPickle.PicklingError",
5641 if (!( t
=PyDict_New())) return -1;
5642 if (!( r
=PyRun_String(
5643 "def __str__(self):\n"
5645 " a=a and type(a[0]) or '(what)'\n"
5646 " return 'Cannot pickle %s objects' % a\n"
5648 module_dict
, t
) )) return -1;
5651 if (!( UnpickleableError
= PyErr_NewException(
5652 "cPickle.UnpickleableError", PicklingError
, t
)))
5657 if (!( UnpicklingError
= PyErr_NewException("cPickle.UnpicklingError",
5658 PickleError
, NULL
)))
5661 if (!( BadPickleGet
= PyErr_NewException("cPickle.BadPickleGet",
5662 UnpicklingError
, NULL
)))
5665 if (PyDict_SetItemString(module_dict
, "PickleError",
5669 if (PyDict_SetItemString(module_dict
, "PicklingError",
5673 if (PyDict_SetItemString(module_dict
, "UnpicklingError",
5674 UnpicklingError
) < 0)
5677 if (PyDict_SetItemString(module_dict
, "UnpickleableError",
5678 UnpickleableError
) < 0)
5681 if (PyDict_SetItemString(module_dict
, "BadPickleGet",
5690 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5691 #define PyMODINIT_FUNC void
5696 PyObject
*m
, *d
, *di
, *v
, *k
;
5698 char *rev
= "1.71"; /* XXX when does this change? */
5699 PyObject
*format_version
;
5700 PyObject
*compatible_formats
;
5702 Picklertype
.ob_type
= &PyType_Type
;
5703 Unpicklertype
.ob_type
= &PyType_Type
;
5704 PdataType
.ob_type
= &PyType_Type
;
5706 /* Initialize some pieces. We need to do this before module creation,
5707 * so we're forced to use a temporary dictionary. :(
5711 if (init_stuff(di
) < 0) return;
5713 /* Create the module and add the functions */
5714 m
= Py_InitModule4("cPickle", cPickle_methods
,
5715 cPickle_module_documentation
,
5716 (PyObject
*)NULL
,PYTHON_API_VERSION
);
5720 /* Add some symbolic constants to the module */
5721 d
= PyModule_GetDict(m
);
5722 v
= PyString_FromString(rev
);
5723 PyDict_SetItemString(d
, "__version__", v
);
5726 /* Copy data from di. Waaa. */
5727 for (i
=0; PyDict_Next(di
, &i
, &k
, &v
); ) {
5728 if (PyObject_SetItem(d
, k
, v
) < 0) {
5735 i
= PyModule_AddIntConstant(m
, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL
);
5739 /* These are purely informational; no code uses them. */
5740 /* File format version we write. */
5741 format_version
= PyString_FromString("2.0");
5742 /* Format versions we can read. */
5743 compatible_formats
= Py_BuildValue("[sssss]",
5744 "1.0", /* Original protocol 0 */
5745 "1.1", /* Protocol 0 + INST */
5746 "1.2", /* Original protocol 1 */
5747 "1.3", /* Protocol 1 + BINFLOAT */
5748 "2.0"); /* Original protocol 2 */
5749 PyDict_SetItemString(d
, "format_version", format_version
);
5750 PyDict_SetItemString(d
, "compatible_formats", compatible_formats
);
5751 Py_XDECREF(format_version
);
5752 Py_XDECREF(compatible_formats
);