3 #include "structmember.h"
5 PyDoc_STRVAR(cPickle_module_documentation
,
6 "C implementation and optimization of the Python pickle module\n"
8 "cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n");
12 #define Py_eval_input eval_input
13 #endif /* Py_eval_input */
19 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
21 #define WRITE_BUF_SIZE 256
23 /* --------------------------------------------------------------------------
24 NOTES on format codes.
25 XXX much more is needed here
28 BININT1 8-bit unsigned integer; followed by 1 byte.
29 BININT2 16-bit unsigned integer; followed by 2 bytes, little-endian.
30 BININT 32-bit signed integer; followed by 4 bytes, little-endian.
31 INT Integer; natural decimal string conversion, then newline.
32 CAUTION: INT-reading code can't assume that what follows
33 fits in a Python int, because the size of Python ints varies
35 LONG Long (unbounded) integer; repr(i), then newline.
36 -------------------------------------------------------------------------- */
56 #define SHORT_BINSTRING 'U'
58 #define BINUNICODE 'X'
63 #define EMPTY_DICT '}'
68 #define LONG_BINGET 'j'
70 #define EMPTY_LIST ']'
74 #define LONG_BINPUT 'r'
77 #define EMPTY_TUPLE ')'
85 static char MARKv
= MARK
;
87 static PyObject
*PickleError
;
88 static PyObject
*PicklingError
;
89 static PyObject
*UnpickleableError
;
90 static PyObject
*UnpicklingError
;
91 static PyObject
*BadPickleGet
;
94 static PyObject
*dispatch_table
;
95 static PyObject
*safe_constructors
;
96 static PyObject
*empty_tuple
;
98 static PyObject
*__class___str
, *__getinitargs___str
, *__dict___str
,
99 *__getstate___str
, *__setstate___str
, *__name___str
, *__reduce___str
,
100 *write_str
, *__safe_for_unpickling___str
, *append_str
,
101 *read_str
, *readline_str
, *__main___str
, *__basicnew___str
,
102 *copy_reg_str
, *dispatch_table_str
, *safe_constructors_str
;
104 /*************************************************************************
105 Internal Data type for pickle data. */
114 Pdata_dealloc(Pdata
*self
)
119 for (i
=self
->length
, p
=self
->data
; --i
>= 0; p
++) Py_DECREF(*p
);
121 if (self
->data
) free(self
->data
);
126 static PyTypeObject PdataType
= {
127 PyObject_HEAD_INIT(NULL
) 0, "cPickle.Pdata", sizeof(Pdata
), 0,
128 (destructor
)Pdata_dealloc
,
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
132 #define Pdata_Check(O) ((O)->ob_type == &PdataType)
139 if (!( self
= PyObject_New(Pdata
, &PdataType
))) return NULL
;
142 self
->data
=malloc(self
->size
* sizeof(PyObject
*));
143 if (self
->data
) return (PyObject
*)self
;
145 return PyErr_NoMemory();
151 PyErr_SetString(UnpicklingError
, "unpickling stack underflow");
156 Pdata_clear(Pdata
*self
, int clearto
)
161 if (clearto
< 0) return stackUnderflow();
162 if (clearto
>= self
->length
) return 0;
164 for (i
=self
->length
, p
=self
->data
+clearto
; --i
>= clearto
; p
++)
166 self
->length
=clearto
;
173 Pdata_grow(Pdata
*self
)
180 self
->data
= realloc(self
->data
, self
->size
*sizeof(PyObject
*));
189 #define PDATA_POP(D,V) { \
190 if ((D)->length) V=D->data[--((D)->length)]; \
192 PyErr_SetString(UnpicklingError, "bad pickle data"); \
199 Pdata_popTuple(Pdata
*self
, int start
)
204 l
=self
->length
-start
;
205 if (!( r
=PyTuple_New(l
))) return NULL
;
206 for (i
=start
, j
=0 ; j
< l
; i
++, j
++)
207 PyTuple_SET_ITEM(r
, j
, self
->data
[i
]);
214 Pdata_popList(Pdata
*self
, int start
)
219 l
=self
->length
-start
;
220 if (!( r
=PyList_New(l
))) return NULL
;
221 for (i
=start
, j
=0 ; j
< l
; i
++, j
++)
222 PyList_SET_ITEM(r
, j
, self
->data
[i
]);
228 #define PDATA_APPEND_(D,O,ER) { \
229 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
232 #define PDATA_APPEND(D,O,ER) { \
233 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
234 Pdata_grow((Pdata*)(D)) < 0) \
237 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
240 #define PDATA_PUSH(D,O,ER) { \
241 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
242 Pdata_grow((Pdata*)(D)) < 0) { \
246 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
249 /*************************************************************************/
251 #define ARG_TUP(self, o) { \
252 if (self->arg || (self->arg=PyTuple_New(1))) { \
253 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
254 PyTuple_SET_ITEM(self->arg,0,o); \
261 #define FREE_ARG_TUP(self) { \
262 if (self->arg->ob_refcnt > 1) { \
263 Py_DECREF(self->arg); \
268 typedef struct Picklerobject
{
276 PyObject
*inst_pers_func
;
278 int fast
; /* Fast mode doesn't save in memo, don't use if circ ref */
280 int (*write_func
)(struct Picklerobject
*, char *, int);
283 PyObject
*dispatch_table
;
284 int fast_container
; /* count nested container dumps */
288 #ifndef PY_CPICKLE_FAST_LIMIT
289 #define PY_CPICKLE_FAST_LIMIT 50
292 static PyTypeObject Picklertype
;
294 typedef struct Unpicklerobject
{
305 PyObject
*last_string
;
309 int (*read_func
)(struct Unpicklerobject
*, char **, int);
310 int (*readline_func
)(struct Unpicklerobject
*, char **);
313 PyObject
*safe_constructors
;
314 PyObject
*find_class
;
317 static PyTypeObject Unpicklertype
;
319 /* Forward decls that need the above structs */
320 static int save(Picklerobject
*, PyObject
*, int);
321 static int put2(Picklerobject
*, PyObject
*);
324 cPickle_PyMapping_HasKey(PyObject
*o
, PyObject
*key
)
328 if ((v
= PyObject_GetItem(o
,key
))) {
339 cPickle_ErrFormat(PyObject
*ErrType
, char *stringformat
, char *format
, ...)
342 PyObject
*args
=0, *retval
=0;
343 va_start(va
, format
);
345 if (format
) args
= Py_VaBuildValue(format
, va
);
347 if (format
&& ! args
) return NULL
;
348 if (stringformat
&& !(retval
=PyString_FromString(stringformat
)))
354 v
=PyString_Format(retval
, args
);
357 if (! v
) return NULL
;
362 if (args
) retval
=args
;
364 PyErr_SetObject(ErrType
,Py_None
);
367 PyErr_SetObject(ErrType
,retval
);
373 write_file(Picklerobject
*self
, char *s
, int n
)
375 size_t nbyteswritten
;
381 Py_BEGIN_ALLOW_THREADS
382 nbyteswritten
= fwrite(s
, sizeof(char), n
, self
->fp
);
384 if (nbyteswritten
!= (size_t)n
) {
385 PyErr_SetFromErrno(PyExc_IOError
);
393 write_cStringIO(Picklerobject
*self
, char *s
, int n
)
399 if (PycStringIO
->cwrite((PyObject
*)self
->file
, s
, n
) != n
) {
407 write_none(Picklerobject
*self
, char *s
, int n
)
409 if (s
== NULL
) return 0;
414 write_other(Picklerobject
*self
, char *s
, int n
)
416 PyObject
*py_str
= 0, *junk
= 0;
419 if (!( self
->buf_size
)) return 0;
420 py_str
= PyString_FromStringAndSize(self
->write_buf
,
426 if (self
->buf_size
&& (n
+ self
->buf_size
) > WRITE_BUF_SIZE
) {
427 if (write_other(self
, NULL
, 0) < 0)
431 if (n
> WRITE_BUF_SIZE
) {
433 PyString_FromStringAndSize(s
, n
)))
437 memcpy(self
->write_buf
+ self
->buf_size
, s
, n
);
444 /* object with write method */
445 ARG_TUP(self
, py_str
);
447 junk
= PyObject_Call(self
->write
, self
->arg
, NULL
);
450 if (junk
) Py_DECREF(junk
);
454 PDATA_PUSH(self
->file
, py_str
, -1);
462 read_file(Unpicklerobject
*self
, char **s
, int n
)
466 if (self
->buf_size
== 0) {
469 size
= ((n
< 32) ? 32 : n
);
470 if (!( self
->buf
= (char *)malloc(size
* sizeof(char)))) {
475 self
->buf_size
= size
;
477 else if (n
> self
->buf_size
) {
478 self
->buf
= (char *)realloc(self
->buf
, n
* sizeof(char));
487 Py_BEGIN_ALLOW_THREADS
488 nbytesread
= fread(self
->buf
, sizeof(char), n
, self
->fp
);
490 if (nbytesread
!= (size_t)n
) {
491 if (feof(self
->fp
)) {
492 PyErr_SetNone(PyExc_EOFError
);
496 PyErr_SetFromErrno(PyExc_IOError
);
507 readline_file(Unpicklerobject
*self
, char **s
)
511 if (self
->buf_size
== 0) {
512 if (!( self
->buf
= (char *)malloc(40 * sizeof(char)))) {
522 for (; i
< (self
->buf_size
- 1); i
++) {
523 if (feof(self
->fp
) ||
524 (self
->buf
[i
] = getc(self
->fp
)) == '\n') {
525 self
->buf
[i
+ 1] = '\0';
530 self
->buf
= (char *)realloc(self
->buf
,
531 (self
->buf_size
* 2) * sizeof(char));
543 read_cStringIO(Unpicklerobject
*self
, char **s
, int n
)
547 if (PycStringIO
->cread((PyObject
*)self
->file
, &ptr
, n
) != n
) {
548 PyErr_SetNone(PyExc_EOFError
);
559 readline_cStringIO(Unpicklerobject
*self
, char **s
)
564 if ((n
= PycStringIO
->creadline((PyObject
*)self
->file
, &ptr
)) < 0) {
575 read_other(Unpicklerobject
*self
, char **s
, int n
)
577 PyObject
*bytes
, *str
=0;
579 if (!( bytes
= PyInt_FromLong(n
))) return -1;
581 ARG_TUP(self
, bytes
);
583 str
= PyObject_Call(self
->read
, self
->arg
, NULL
);
586 if (! str
) return -1;
588 Py_XDECREF(self
->last_string
);
589 self
->last_string
= str
;
591 if (! (*s
= PyString_AsString(str
))) return -1;
597 readline_other(Unpicklerobject
*self
, char **s
)
602 if (!( str
= PyObject_CallObject(self
->readline
, empty_tuple
))) {
606 if ((str_size
= PyString_Size(str
)) < 0)
609 Py_XDECREF(self
->last_string
);
610 self
->last_string
= str
;
612 if (! (*s
= PyString_AsString(str
)))
620 pystrndup(char *s
, int l
)
623 if (!( r
=malloc((l
+1)*sizeof(char)))) return (char*)PyErr_NoMemory();
631 get(Picklerobject
*self
, PyObject
*id
)
633 PyObject
*value
, *mv
;
638 if (!( mv
= PyDict_GetItem(self
->memo
, id
))) {
639 PyErr_SetObject(PyExc_KeyError
, id
);
643 if (!( value
= PyTuple_GetItem(mv
, 0)))
646 if (!( PyInt_Check(value
))) {
647 PyErr_SetString(PicklingError
, "no int where int expected in memo");
650 c_value
= PyInt_AS_LONG((PyIntObject
*)value
);
654 PyOS_snprintf(s
+ 1, sizeof(s
) - 1, "%ld\n", c_value
);
657 else if (Pdata_Check(self
->file
)) {
658 if (write_other(self
, NULL
, 0) < 0) return -1;
659 PDATA_APPEND(self
->file
, mv
, -1);
665 s
[1] = (int)(c_value
& 0xff);
670 s
[1] = (int)(c_value
& 0xff);
671 s
[2] = (int)((c_value
>> 8) & 0xff);
672 s
[3] = (int)((c_value
>> 16) & 0xff);
673 s
[4] = (int)((c_value
>> 24) & 0xff);
678 if ((*self
->write_func
)(self
, s
, len
) < 0)
686 put(Picklerobject
*self
, PyObject
*ob
)
688 if (ob
->ob_refcnt
< 2 || self
->fast
)
691 return put2(self
, ob
);
696 put2(Picklerobject
*self
, PyObject
*ob
)
702 PyObject
*py_ob_id
= 0, *memo_len
= 0, *t
= 0;
707 if ((p
= PyDict_Size(self
->memo
)) < 0)
710 /* Make sure memo keys are positive! */
713 if (!( py_ob_id
= PyLong_FromVoidPtr(ob
)))
716 if (!( memo_len
= PyInt_FromLong(p
)))
719 if (!( t
= PyTuple_New(2)))
722 PyTuple_SET_ITEM(t
, 0, memo_len
);
724 PyTuple_SET_ITEM(t
, 1, ob
);
727 if (PyDict_SetItem(self
->memo
, py_ob_id
, t
) < 0)
732 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%d\n", p
);
735 else if (Pdata_Check(self
->file
)) {
736 if (write_other(self
, NULL
, 0) < 0) return -1;
737 PDATA_APPEND(self
->file
, memo_len
, -1);
738 res
=0; /* Job well done ;) */
743 c_str
[0] = LONG_BINPUT
;
744 c_str
[1] = (int)(p
& 0xff);
745 c_str
[2] = (int)((p
>> 8) & 0xff);
746 c_str
[3] = (int)((p
>> 16) & 0xff);
747 c_str
[4] = (int)((p
>> 24) & 0xff);
757 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
763 Py_XDECREF(py_ob_id
);
764 Py_XDECREF(memo_len
);
770 #define PyImport_Import cPickle_Import
773 PyImport_Import(PyObject
*module_name
)
775 static PyObject
*silly_list
=0, *__builtins___str
=0, *__import___str
;
776 static PyObject
*standard_builtins
=0;
777 PyObject
*globals
=0, *__import__
=0, *__builtins__
=0, *r
=0;
779 if (!( silly_list
)) {
780 if (!( __import___str
=PyString_FromString("__import__")))
782 if (!( __builtins___str
=PyString_FromString("__builtins__")))
784 if (!( silly_list
=Py_BuildValue("[s]","__doc__")))
788 if ((globals
=PyEval_GetGlobals())) {
790 __builtins__
=PyObject_GetItem(globals
,__builtins___str
);
797 if (!(standard_builtins
||
798 (standard_builtins
=PyImport_ImportModule("__builtin__"))))
801 __builtins__
=standard_builtins
;
802 Py_INCREF(__builtins__
);
803 globals
= Py_BuildValue("{sO}", "__builtins__", __builtins__
);
808 if (PyDict_Check(__builtins__
)) {
809 __import__
=PyObject_GetItem(__builtins__
,__import___str
);
810 if (!__import__
) goto err
;
813 __import__
=PyObject_GetAttr(__builtins__
,__import___str
);
814 if (!__import__
) goto err
;
817 r
=PyObject_CallFunction(__import__
,"OOOO",
818 module_name
, globals
, globals
, silly_list
);
823 Py_DECREF(__builtins__
);
824 Py_DECREF(__import__
);
829 Py_XDECREF(__builtins__
);
830 Py_XDECREF(__import__
);
835 whichmodule(PyObject
*global
, PyObject
*global_name
)
838 PyObject
*module
= 0, *modules_dict
= 0,
839 *global_name_attr
= 0, *name
= 0;
841 module
= PyObject_GetAttrString(global
, "__module__");
842 if (module
) return module
;
845 if (!( modules_dict
= PySys_GetObject("modules")))
849 while ((j
= PyDict_Next(modules_dict
, &i
, &name
, &module
))) {
851 if (PyObject_Compare(name
, __main___str
)==0) continue;
853 global_name_attr
= PyObject_GetAttr(module
, global_name
);
854 if (!global_name_attr
) {
859 if (global_name_attr
!= global
) {
860 Py_DECREF(global_name_attr
);
864 Py_DECREF(global_name_attr
);
869 /* The following implements the rule in pickle.py added in 1.5
870 that used __main__ if no module is found. I don't actually
884 fast_save_enter(Picklerobject
*self
, PyObject
*obj
)
886 /* if fast_container < 0, we're doing an error exit. */
887 if (++self
->fast_container
>= PY_CPICKLE_FAST_LIMIT
) {
888 PyObject
*key
= NULL
;
889 if (self
->fast_memo
== NULL
) {
890 self
->fast_memo
= PyDict_New();
891 if (self
->fast_memo
== NULL
) {
892 self
->fast_container
= -1;
896 key
= PyLong_FromVoidPtr(obj
);
899 if (PyDict_GetItem(self
->fast_memo
, key
)) {
900 PyErr_Format(PyExc_ValueError
,
901 "fast mode: can't pickle cyclic objects including object type %s at %p",
902 obj
->ob_type
->tp_name
, obj
);
903 self
->fast_container
= -1;
906 if (PyDict_SetItem(self
->fast_memo
, key
, Py_None
) < 0) {
907 self
->fast_container
= -1;
915 fast_save_leave(Picklerobject
*self
, PyObject
*obj
)
917 if (self
->fast_container
-- >= PY_CPICKLE_FAST_LIMIT
) {
918 PyObject
*key
= PyLong_FromVoidPtr(obj
);
921 if (PyDict_DelItem(self
->fast_memo
, key
) < 0) {
929 save_none(Picklerobject
*self
, PyObject
*args
)
931 static char none
= NONE
;
932 if ((*self
->write_func
)(self
, &none
, 1) < 0)
939 save_bool(Picklerobject
*self
, PyObject
*args
)
941 static char *buf
[2] = {FALSE
, TRUE
};
942 static char len
[2] = {sizeof(FALSE
)-1, sizeof(TRUE
)-1};
943 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
945 if ((*self
->write_func
)(self
, buf
[l
], len
[l
]) < 0)
952 save_int(Picklerobject
*self
, PyObject
*args
)
955 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
964 /* Text-mode pickle, or long too big to fit in the 4-byte
965 * signed BININT format: store as a string.
968 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%ld\n", l
);
969 if ((*self
->write_func
)(self
, c_str
, strlen(c_str
)) < 0)
973 /* Binary pickle and l fits in a signed 4-byte int. */
974 c_str
[1] = (int)( l
& 0xff);
975 c_str
[2] = (int)((l
>> 8) & 0xff);
976 c_str
[3] = (int)((l
>> 16) & 0xff);
977 c_str
[4] = (int)((l
>> 24) & 0xff);
979 if ((c_str
[4] == 0) && (c_str
[3] == 0)) {
994 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
1003 save_long(Picklerobject
*self
, PyObject
*args
)
1008 static char l
= LONG
;
1010 if (!( repr
= PyObject_Repr(args
)))
1013 if ((size
= PyString_Size(repr
)) < 0)
1016 if ((*self
->write_func
)(self
, &l
, 1) < 0)
1019 if ((*self
->write_func
)(self
,
1020 PyString_AS_STRING((PyStringObject
*)repr
),
1024 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1037 save_float(Picklerobject
*self
, PyObject
*args
)
1039 double x
= PyFloat_AS_DOUBLE((PyFloatObject
*)args
);
1046 unsigned char *p
= (unsigned char *)str
;
1060 /* Normalize f to be in the range [1.0, 2.0) */
1061 if (0.5 <= f
&& f
< 1.0) {
1065 else if (f
== 0.0) {
1069 PyErr_SetString(PyExc_SystemError
,
1070 "frexp() result out of range");
1075 /* XXX 1024 itself is reserved for Inf/NaN */
1076 PyErr_SetString(PyExc_OverflowError
,
1077 "float too large to pack with d format");
1080 else if (e
< -1022) {
1081 /* Gradual underflow */
1082 f
= ldexp(f
, 1022 + e
);
1085 else if (!(e
== 0 && f
== 0.0)) {
1087 f
-= 1.0; /* Get rid of leading 1 */
1090 /* fhi receives the high 28 bits;
1091 flo the low 24 bits (== 52 bits) */
1092 f
*= 268435456.0; /* 2**28 */
1093 fhi
= (long) floor(f
); /* Truncate */
1095 f
*= 16777216.0; /* 2**24 */
1096 flo
= (long) floor(f
+ 0.5); /* Round */
1099 *p
= (s
<<7) | (e
>>4);
1103 *p
= (unsigned char) (((e
&0xF)<<4) | (fhi
>>24));
1107 *p
= (unsigned char) ((fhi
>>16) & 0xFF);
1111 *p
= (unsigned char) ((fhi
>>8) & 0xFF);
1115 *p
= (unsigned char) (fhi
& 0xFF);
1119 *p
= (unsigned char) ((flo
>>16) & 0xFF);
1123 *p
= (unsigned char) ((flo
>>8) & 0xFF);
1127 *p
= (unsigned char) (flo
& 0xFF);
1129 if ((*self
->write_func
)(self
, str
, 9) < 0)
1135 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%.17g\n", x
);
1137 if ((*self
->write_func
)(self
, c_str
, strlen(c_str
)) < 0)
1146 save_string(Picklerobject
*self
, PyObject
*args
, int doput
)
1151 if ((size
= PyString_Size(args
)) < 0)
1157 static char string
= STRING
;
1159 if (!( repr
= PyObject_Repr(args
)))
1162 if ((len
= PyString_Size(repr
)) < 0)
1164 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1166 if ((*self
->write_func
)(self
, &string
, 1) < 0)
1169 if ((*self
->write_func
)(self
, repr_str
, len
) < 0)
1172 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1181 if ((size
= PyString_Size(args
)) < 0)
1185 c_str
[0] = SHORT_BINSTRING
;
1190 c_str
[0] = BINSTRING
;
1191 for (i
= 1; i
< 5; i
++)
1192 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1196 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
1199 if (size
> 128 && Pdata_Check(self
->file
)) {
1200 if (write_other(self
, NULL
, 0) < 0) return -1;
1201 PDATA_APPEND(self
->file
, args
, -1);
1204 if ((*self
->write_func
)(self
,
1205 PyString_AS_STRING((PyStringObject
*)args
), size
) < 0)
1211 if (put(self
, args
) < 0)
1222 #ifdef Py_USING_UNICODE
1223 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1224 backslash and newline characters to \uXXXX escapes. */
1226 modified_EncodeRawUnicodeEscape(const Py_UNICODE
*s
, int size
)
1232 static const char *hexdigit
= "0123456789ABCDEF";
1234 repr
= PyString_FromStringAndSize(NULL
, 6 * size
);
1240 p
= q
= PyString_AS_STRING(repr
);
1241 while (size
-- > 0) {
1242 Py_UNICODE ch
= *s
++;
1243 /* Map 16-bit characters to '\uxxxx' */
1244 if (ch
>= 256 || ch
== '\\' || ch
== '\n') {
1247 *p
++ = hexdigit
[(ch
>> 12) & 0xf];
1248 *p
++ = hexdigit
[(ch
>> 8) & 0xf];
1249 *p
++ = hexdigit
[(ch
>> 4) & 0xf];
1250 *p
++ = hexdigit
[ch
& 15];
1252 /* Copy everything else as-is */
1257 _PyString_Resize(&repr
, p
- q
);
1263 save_unicode(Picklerobject
*self
, PyObject
*args
, int doput
)
1268 if (!PyUnicode_Check(args
))
1273 static char string
= UNICODE
;
1275 repr
= modified_EncodeRawUnicodeEscape(
1276 PyUnicode_AS_UNICODE(args
), PyUnicode_GET_SIZE(args
));
1280 if ((len
= PyString_Size(repr
)) < 0)
1282 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1284 if ((*self
->write_func
)(self
, &string
, 1) < 0)
1287 if ((*self
->write_func
)(self
, repr_str
, len
) < 0)
1290 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1299 if (!( repr
= PyUnicode_AsUTF8String(args
)))
1302 if ((size
= PyString_Size(repr
)) < 0)
1305 c_str
[0] = BINUNICODE
;
1306 for (i
= 1; i
< 5; i
++)
1307 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1310 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
1313 if (size
> 128 && Pdata_Check(self
->file
)) {
1314 if (write_other(self
, NULL
, 0) < 0)
1316 PDATA_APPEND(self
->file
, repr
, -1);
1319 if ((*self
->write_func
)(self
, PyString_AS_STRING(repr
),
1328 if (put(self
, args
) < 0)
1341 save_tuple(Picklerobject
*self
, PyObject
*args
)
1343 PyObject
*element
= 0, *py_tuple_id
= 0;
1344 int len
, i
, res
= -1;
1346 static char tuple
= TUPLE
;
1348 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1351 if ((len
= PyTuple_Size(args
)) < 0)
1354 for (i
= 0; i
< len
; i
++) {
1355 if (!( element
= PyTuple_GET_ITEM((PyTupleObject
*)args
, i
)))
1358 if (save(self
, element
, 0) < 0)
1362 if (!( py_tuple_id
= PyLong_FromVoidPtr(args
)))
1366 if (PyDict_GetItem(self
->memo
, py_tuple_id
)) {
1368 static char pop_mark
= POP_MARK
;
1370 if ((*self
->write_func
)(self
, &pop_mark
, 1) < 0)
1374 static char pop
= POP
;
1376 for (i
= 0; i
<= len
; i
++) {
1377 if ((*self
->write_func
)(self
, &pop
, 1) < 0)
1382 if (get(self
, py_tuple_id
) < 0)
1390 if ((*self
->write_func
)(self
, &tuple
, 1) < 0) {
1394 if (put(self
, args
) < 0)
1400 Py_XDECREF(py_tuple_id
);
1406 save_empty_tuple(Picklerobject
*self
, PyObject
*args
)
1408 static char tuple
= EMPTY_TUPLE
;
1410 return (*self
->write_func
)(self
, &tuple
, 1);
1415 save_list(Picklerobject
*self
, PyObject
*args
)
1417 PyObject
*element
= 0;
1418 int s_len
, len
, i
, using_appends
, res
= -1;
1421 static char append
= APPEND
, appends
= APPENDS
;
1423 if (self
->fast
&& !fast_save_enter(self
, args
))
1436 if ((len
= PyList_Size(args
)) < 0)
1439 if ((*self
->write_func
)(self
, s
, s_len
) < 0)
1443 if (put(self
, args
) < 0)
1447 if (put2(self
, args
) < 0)
1451 if ((using_appends
= (self
->bin
&& (len
> 1))))
1452 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1455 for (i
= 0; i
< len
; i
++) {
1456 if (!( element
= PyList_GET_ITEM((PyListObject
*)args
, i
)))
1459 if (save(self
, element
, 0) < 0)
1462 if (!using_appends
) {
1463 if ((*self
->write_func
)(self
, &append
, 1) < 0)
1468 if (using_appends
) {
1469 if ((*self
->write_func
)(self
, &appends
, 1) < 0)
1476 if (self
->fast
&& !fast_save_leave(self
, args
))
1484 save_dict(Picklerobject
*self
, PyObject
*args
)
1486 PyObject
*key
= 0, *value
= 0;
1487 int i
, len
, res
= -1, using_setitems
;
1490 static char setitem
= SETITEM
, setitems
= SETITEMS
;
1492 if (self
->fast
&& !fast_save_enter(self
, args
))
1505 if ((*self
->write_func
)(self
, s
, len
) < 0)
1508 if ((len
= PyDict_Size(args
)) < 0)
1512 if (put(self
, args
) < 0)
1516 if (put2(self
, args
) < 0)
1520 if ((using_setitems
= (self
->bin
&& (PyDict_Size(args
) > 1))))
1521 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1525 while (PyDict_Next(args
, &i
, &key
, &value
)) {
1526 if (save(self
, key
, 0) < 0)
1529 if (save(self
, value
, 0) < 0)
1532 if (!using_setitems
) {
1533 if ((*self
->write_func
)(self
, &setitem
, 1) < 0)
1538 if (using_setitems
) {
1539 if ((*self
->write_func
)(self
, &setitems
, 1) < 0)
1546 if (self
->fast
&& !fast_save_leave(self
, args
))
1554 save_inst(Picklerobject
*self
, PyObject
*args
)
1556 PyObject
*class = 0, *module
= 0, *name
= 0, *state
= 0,
1557 *getinitargs_func
= 0, *getstate_func
= 0, *class_args
= 0;
1558 char *module_str
, *name_str
;
1559 int module_size
, name_size
, res
= -1;
1561 static char inst
= INST
, obj
= OBJ
, build
= BUILD
;
1563 if (self
->fast
&& !fast_save_enter(self
, args
))
1566 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1569 if (!( class = PyObject_GetAttr(args
, __class___str
)))
1573 if (save(self
, class, 0) < 0)
1577 if ((getinitargs_func
= PyObject_GetAttr(args
, __getinitargs___str
))) {
1578 PyObject
*element
= 0;
1582 PyObject_Call(getinitargs_func
, empty_tuple
, NULL
)))
1585 if ((len
= PyObject_Size(class_args
)) < 0)
1588 for (i
= 0; i
< len
; i
++) {
1589 if (!( element
= PySequence_GetItem(class_args
, i
)))
1592 if (save(self
, element
, 0) < 0) {
1605 if (!( name
= ((PyClassObject
*)class)->cl_name
)) {
1606 PyErr_SetString(PicklingError
, "class has no name");
1610 if (!( module
= whichmodule(class, name
)))
1614 if ((module_size
= PyString_Size(module
)) < 0 ||
1615 (name_size
= PyString_Size(name
)) < 0)
1618 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1619 name_str
= PyString_AS_STRING((PyStringObject
*)name
);
1621 if ((*self
->write_func
)(self
, &inst
, 1) < 0)
1624 if ((*self
->write_func
)(self
, module_str
, module_size
) < 0)
1627 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1630 if ((*self
->write_func
)(self
, name_str
, name_size
) < 0)
1633 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1636 else if ((*self
->write_func
)(self
, &obj
, 1) < 0) {
1640 if ((getstate_func
= PyObject_GetAttr(args
, __getstate___str
))) {
1641 state
= PyObject_Call(getstate_func
, empty_tuple
, NULL
);
1648 if (!( state
= PyObject_GetAttr(args
, __dict___str
))) {
1655 if (!PyDict_Check(state
)) {
1656 if (put2(self
, args
) < 0)
1660 if (put(self
, args
) < 0)
1664 if (save(self
, state
, 0) < 0)
1667 if ((*self
->write_func
)(self
, &build
, 1) < 0)
1673 if (self
->fast
&& !fast_save_leave(self
, args
))
1679 Py_XDECREF(getinitargs_func
);
1680 Py_XDECREF(getstate_func
);
1681 Py_XDECREF(class_args
);
1688 save_global(Picklerobject
*self
, PyObject
*args
, PyObject
*name
)
1690 PyObject
*global_name
= 0, *module
= 0, *mod
= 0, *klass
= 0;
1691 char *name_str
, *module_str
;
1692 int module_size
, name_size
, res
= -1;
1694 static char global
= GLOBAL
;
1698 Py_INCREF(global_name
);
1701 if (!( global_name
= PyObject_GetAttr(args
, __name___str
)))
1705 if (!( module
= whichmodule(args
, global_name
)))
1708 if ((module_size
= PyString_Size(module
)) < 0 ||
1709 (name_size
= PyString_Size(global_name
)) < 0)
1712 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1713 name_str
= PyString_AS_STRING((PyStringObject
*)global_name
);
1715 mod
= PyImport_ImportModule(module_str
);
1717 /* Py_ErrClear(); ?? */
1718 cPickle_ErrFormat(PicklingError
,
1719 "Can't pickle %s: it's not found as %s.%s",
1720 "OSS", args
, module
, global_name
);
1723 klass
= PyObject_GetAttrString(mod
, name_str
);
1724 if (klass
== NULL
) {
1725 cPickle_ErrFormat(PicklingError
,
1726 "Can't pickle %s: it's not found as %s.%s",
1727 "OSS", args
, module
, global_name
);
1730 if (klass
!= args
) {
1732 cPickle_ErrFormat(PicklingError
,
1733 "Can't pickle %s: it's not the same object as %s.%s",
1734 "OSS", args
, module
, global_name
);
1739 if ((*self
->write_func
)(self
, &global
, 1) < 0)
1742 if ((*self
->write_func
)(self
, module_str
, module_size
) < 0)
1745 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1748 if ((*self
->write_func
)(self
, name_str
, name_size
) < 0)
1751 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1754 if (put(self
, args
) < 0)
1761 Py_XDECREF(global_name
);
1768 save_pers(Picklerobject
*self
, PyObject
*args
, PyObject
*f
)
1773 static char persid
= PERSID
, binpersid
= BINPERSID
;
1776 ARG_TUP(self
, args
);
1778 pid
= PyObject_Call(f
, self
->arg
, NULL
);
1781 if (! pid
) return -1;
1783 if (pid
!= Py_None
) {
1785 if (!PyString_Check(pid
)) {
1786 PyErr_SetString(PicklingError
,
1787 "persistent id must be string");
1791 if ((*self
->write_func
)(self
, &persid
, 1) < 0)
1794 if ((size
= PyString_Size(pid
)) < 0)
1797 if ((*self
->write_func
)(self
,
1798 PyString_AS_STRING((PyStringObject
*)pid
), size
) < 0)
1801 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1807 else if (save(self
, pid
, 1) >= 0) {
1808 if ((*self
->write_func
)(self
, &binpersid
, 1) < 0)
1827 save_reduce(Picklerobject
*self
, PyObject
*callable
,
1828 PyObject
*tup
, PyObject
*state
, PyObject
*ob
)
1830 static char reduce
= REDUCE
, build
= BUILD
;
1832 if (save(self
, callable
, 0) < 0)
1835 if (save(self
, tup
, 0) < 0)
1838 if ((*self
->write_func
)(self
, &reduce
, 1) < 0)
1842 if (state
&& !PyDict_Check(state
)) {
1843 if (put2(self
, ob
) < 0)
1847 if (put(self
, ob
) < 0)
1853 if (save(self
, state
, 0) < 0)
1856 if ((*self
->write_func
)(self
, &build
, 1) < 0)
1864 save(Picklerobject
*self
, PyObject
*args
, int pers_save
)
1867 PyObject
*py_ob_id
= 0, *__reduce__
= 0, *t
= 0, *arg_tup
= 0,
1868 *callable
= 0, *state
= 0;
1869 int res
= -1, tmp
, size
;
1871 if (self
->nesting
++ > Py_GetRecursionLimit()){
1872 PyErr_SetString(PyExc_RuntimeError
,
1873 "maximum recursion depth exceeded");
1877 if (!pers_save
&& self
->pers_func
) {
1878 if ((tmp
= save_pers(self
, args
, self
->pers_func
)) != 0) {
1884 if (args
== Py_None
) {
1885 res
= save_none(self
, args
);
1889 type
= args
->ob_type
;
1891 switch (type
->tp_name
[0]) {
1893 if (args
== Py_False
|| args
== Py_True
) {
1894 res
= save_bool(self
, args
);
1899 if (type
== &PyInt_Type
) {
1900 res
= save_int(self
, args
);
1906 if (type
== &PyLong_Type
) {
1907 res
= save_long(self
, args
);
1913 if (type
== &PyFloat_Type
) {
1914 res
= save_float(self
, args
);
1920 if (type
== &PyTuple_Type
&& PyTuple_Size(args
)==0) {
1921 if (self
->bin
) res
= save_empty_tuple(self
, args
);
1922 else res
= save_tuple(self
, args
);
1928 if ((type
== &PyString_Type
) && (PyString_GET_SIZE(args
) < 2)) {
1929 res
= save_string(self
, args
, 0);
1933 #ifdef Py_USING_UNICODE
1935 if ((type
== &PyUnicode_Type
) && (PyString_GET_SIZE(args
) < 2)) {
1936 res
= save_unicode(self
, args
, 0);
1942 if (args
->ob_refcnt
> 1) {
1943 if (!( py_ob_id
= PyLong_FromVoidPtr(args
)))
1946 if (PyDict_GetItem(self
->memo
, py_ob_id
)) {
1947 if (get(self
, py_ob_id
) < 0)
1955 switch (type
->tp_name
[0]) {
1957 if (type
== &PyString_Type
) {
1958 res
= save_string(self
, args
, 1);
1963 #ifdef Py_USING_UNICODE
1965 if (type
== &PyUnicode_Type
) {
1966 res
= save_unicode(self
, args
, 1);
1973 if (type
== &PyTuple_Type
) {
1974 res
= save_tuple(self
, args
);
1977 if (type
== &PyType_Type
) {
1978 res
= save_global(self
, args
, NULL
);
1984 if (type
== &PyList_Type
) {
1985 res
= save_list(self
, args
);
1991 if (type
== &PyDict_Type
) {
1992 res
= save_dict(self
, args
);
1998 if (type
== &PyInstance_Type
) {
1999 res
= save_inst(self
, args
);
2005 if (type
== &PyClass_Type
) {
2006 res
= save_global(self
, args
, NULL
);
2012 if (type
== &PyFunction_Type
) {
2013 res
= save_global(self
, args
, NULL
);
2019 if (type
== &PyCFunction_Type
) {
2020 res
= save_global(self
, args
, NULL
);
2025 if (!pers_save
&& self
->inst_pers_func
) {
2026 if ((tmp
= save_pers(self
, args
, self
->inst_pers_func
)) != 0) {
2032 if (PyType_IsSubtype(type
, &PyType_Type
)) {
2033 res
= save_global(self
, args
, NULL
);
2037 if ((__reduce__
= PyDict_GetItem(dispatch_table
, (PyObject
*)type
))) {
2038 Py_INCREF(__reduce__
);
2041 ARG_TUP(self
, args
);
2043 t
= PyObject_Call(__reduce__
, self
->arg
, NULL
);
2046 if (! t
) goto finally
;
2051 if ((__reduce__
= PyObject_GetAttr(args
, __reduce___str
))) {
2052 t
= PyObject_Call(__reduce__
, empty_tuple
, NULL
);
2062 if (PyString_Check(t
)) {
2063 res
= save_global(self
, args
, t
);
2067 if (!PyTuple_Check(t
)) {
2068 cPickle_ErrFormat(PicklingError
, "Value returned by %s must "
2069 "be a tuple", "O", __reduce__
);
2073 size
= PyTuple_Size(t
);
2075 if ((size
!= 3) && (size
!= 2)) {
2076 cPickle_ErrFormat(PicklingError
, "tuple returned by %s must "
2077 "contain only two or three elements", "O", __reduce__
);
2081 callable
= PyTuple_GET_ITEM(t
, 0);
2083 arg_tup
= PyTuple_GET_ITEM(t
, 1);
2086 state
= PyTuple_GET_ITEM(t
, 2);
2089 if (!( PyTuple_Check(arg_tup
) || arg_tup
==Py_None
)) {
2090 cPickle_ErrFormat(PicklingError
, "Second element of tuple "
2091 "returned by %s must be a tuple", "O", __reduce__
);
2095 res
= save_reduce(self
, callable
, arg_tup
, state
, args
);
2099 PyErr_SetObject(UnpickleableError
, args
);
2103 Py_XDECREF(py_ob_id
);
2104 Py_XDECREF(__reduce__
);
2112 dump(Picklerobject
*self
, PyObject
*args
)
2114 static char stop
= STOP
;
2116 if (save(self
, args
, 0) < 0)
2119 if ((*self
->write_func
)(self
, &stop
, 1) < 0)
2122 if ((*self
->write_func
)(self
, NULL
, 0) < 0)
2129 Pickle_clear_memo(Picklerobject
*self
, PyObject
*args
)
2132 PyDict_Clear(self
->memo
);
2138 Pickle_getvalue(Picklerobject
*self
, PyObject
*args
)
2140 int l
, i
, rsize
, ssize
, clear
=1, lm
;
2143 char *s
, *p
, *have_get
;
2146 /* Can be called by Python code or C code */
2147 if (args
&& !PyArg_ParseTuple(args
, "|i:getvalue", &clear
))
2150 /* Check to make sure we are based on a list */
2151 if (! Pdata_Check(self
->file
)) {
2152 PyErr_SetString(PicklingError
,
2153 "Attempt to getvalue() a non-list-based pickler");
2157 /* flush write buffer */
2158 if (write_other(self
, NULL
, 0) < 0) return NULL
;
2160 data
=(Pdata
*)self
->file
;
2163 /* set up an array to hold get/put status */
2164 if ((lm
=PyDict_Size(self
->memo
)) < 0) return NULL
;
2166 if (! (have_get
=malloc((lm
)*sizeof(char)))) return PyErr_NoMemory();
2167 memset(have_get
,0,lm
);
2169 /* Scan for gets. */
2170 for (rsize
=0, i
=l
; --i
>= 0; ) {
2173 if (PyString_Check(k
)) {
2174 rsize
+= PyString_GET_SIZE(k
);
2177 else if (PyInt_Check(k
)) { /* put */
2178 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
2179 if (ik
>= lm
|| ik
==0) {
2180 PyErr_SetString(PicklingError
,
2181 "Invalid get data");
2184 if (have_get
[ik
]) { /* with matching get */
2185 if (ik
< 256) rsize
+= 2;
2190 else if (! (PyTuple_Check(k
) &&
2191 PyTuple_GET_SIZE(k
) == 2 &&
2192 PyInt_Check((k
=PyTuple_GET_ITEM(k
,0))))
2194 PyErr_SetString(PicklingError
,
2195 "Unexpected data in internal list");
2200 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
2201 if (ik
>= lm
|| ik
==0) {
2202 PyErr_SetString(PicklingError
,
2203 "Invalid get data");
2207 if (ik
< 256) rsize
+= 2;
2213 /* Now generate the result */
2214 if (!( r
=PyString_FromStringAndSize(NULL
,rsize
))) goto err
;
2215 s
=PyString_AS_STRING((PyStringObject
*)r
);
2217 for (i
=0; i
<l
; i
++) {
2220 if (PyString_Check(k
)) {
2221 ssize
=PyString_GET_SIZE(k
);
2223 p
=PyString_AS_STRING((PyStringObject
*)k
);
2224 while (--ssize
>= 0) *s
++=*p
++;
2228 else if (PyTuple_Check(k
)) { /* get */
2229 ik
=PyInt_AS_LONG((PyIntObject
*)PyTuple_GET_ITEM(k
,0));
2232 *s
++ = (int)(ik
& 0xff);
2236 *s
++ = (int)(ik
& 0xff);
2237 *s
++ = (int)((ik
>> 8) & 0xff);
2238 *s
++ = (int)((ik
>> 16) & 0xff);
2239 *s
++ = (int)((ik
>> 24) & 0xff);
2244 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
2246 if (have_get
[ik
]) { /* with matching get */
2249 *s
++ = (int)(ik
& 0xff);
2253 *s
++ = (int)(ik
& 0xff);
2254 *s
++ = (int)((ik
>> 8) & 0xff);
2255 *s
++ = (int)((ik
>> 16) & 0xff);
2256 *s
++ = (int)((ik
>> 24) & 0xff);
2264 PyDict_Clear(self
->memo
);
2265 Pdata_clear(data
,0);
2276 Pickler_dump(Picklerobject
*self
, PyObject
*args
)
2281 if (!( PyArg_ParseTuple(args
, "O|i:dump", &ob
, &get
)))
2284 if (dump(self
, ob
) < 0)
2287 if (get
) return Pickle_getvalue(self
, NULL
);
2289 /* XXX Why does dump() return self? */
2291 return (PyObject
*)self
;
2295 static struct PyMethodDef Pickler_methods
[] =
2297 {"dump", (PyCFunction
)Pickler_dump
, METH_VARARGS
,
2298 PyDoc_STR("dump(object) -- "
2299 "Write an object in pickle format to the object's pickle stream")},
2300 {"clear_memo", (PyCFunction
)Pickle_clear_memo
, METH_NOARGS
,
2301 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2302 {"getvalue", (PyCFunction
)Pickle_getvalue
, METH_VARARGS
,
2303 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2304 {NULL
, NULL
} /* sentinel */
2308 static Picklerobject
*
2309 newPicklerobject(PyObject
*file
, int bin
)
2311 Picklerobject
*self
;
2313 if (!( self
= PyObject_New(Picklerobject
, &Picklertype
)))
2320 self
->pers_func
= NULL
;
2321 self
->inst_pers_func
= NULL
;
2322 self
->write_buf
= NULL
;
2326 self
->fast_container
= 0;
2327 self
->fast_memo
= NULL
;
2329 self
->dispatch_table
= NULL
;
2336 if (!( self
->file
= file
))
2339 if (!( self
->memo
= PyDict_New()))
2342 if (PyFile_Check(file
)) {
2343 self
->fp
= PyFile_AsFile(file
);
2344 if (self
->fp
== NULL
) {
2345 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed file");
2348 self
->write_func
= write_file
;
2350 else if (PycStringIO_OutputCheck(file
)) {
2351 self
->write_func
= write_cStringIO
;
2353 else if (file
== Py_None
) {
2354 self
->write_func
= write_none
;
2357 self
->write_func
= write_other
;
2359 if (! Pdata_Check(file
)) {
2360 self
->write
= PyObject_GetAttr(file
, write_str
);
2363 PyErr_SetString(PyExc_TypeError
,
2364 "argument must have 'write' "
2370 if (!( self
->write_buf
=
2371 (char *)malloc(WRITE_BUF_SIZE
* sizeof(char)))) {
2377 if (PyEval_GetRestricted()) {
2378 /* Restricted execution, get private tables */
2381 if (!( m
=PyImport_Import(copy_reg_str
))) goto err
;
2382 self
->dispatch_table
=PyObject_GetAttr(m
, dispatch_table_str
);
2384 if (!( self
->dispatch_table
)) goto err
;
2387 self
->dispatch_table
=dispatch_table
;
2388 Py_INCREF(dispatch_table
);
2394 Py_DECREF((PyObject
*)self
);
2400 get_Pickler(PyObject
*self
, PyObject
*args
)
2402 PyObject
*file
= NULL
;
2405 if (!PyArg_ParseTuple(args
, "|i:Pickler", &bin
)) {
2408 if (!PyArg_ParseTuple(args
, "O|i:Pickler", &file
, &bin
))
2411 return (PyObject
*)newPicklerobject(file
, bin
);
2416 Pickler_dealloc(Picklerobject
*self
)
2418 Py_XDECREF(self
->write
);
2419 Py_XDECREF(self
->memo
);
2420 Py_XDECREF(self
->fast_memo
);
2421 Py_XDECREF(self
->arg
);
2422 Py_XDECREF(self
->file
);
2423 Py_XDECREF(self
->pers_func
);
2424 Py_XDECREF(self
->inst_pers_func
);
2425 Py_XDECREF(self
->dispatch_table
);
2427 if (self
->write_buf
) {
2428 free(self
->write_buf
);
2435 Pickler_get_pers_func(Picklerobject
*p
)
2437 if (p
->pers_func
== NULL
)
2438 PyErr_SetString(PyExc_AttributeError
, "persistent_id");
2440 Py_INCREF(p
->pers_func
);
2441 return p
->pers_func
;
2445 Pickler_set_pers_func(Picklerobject
*p
, PyObject
*v
)
2448 PyErr_SetString(PyExc_TypeError
,
2449 "attribute deletion is not supported");
2452 Py_XDECREF(p
->pers_func
);
2459 Pickler_set_inst_pers_func(Picklerobject
*p
, PyObject
*v
)
2462 PyErr_SetString(PyExc_TypeError
,
2463 "attribute deletion is not supported");
2466 Py_XDECREF(p
->inst_pers_func
);
2468 p
->inst_pers_func
= v
;
2473 Pickler_get_memo(Picklerobject
*p
)
2475 if (p
->memo
== NULL
)
2476 PyErr_SetString(PyExc_AttributeError
, "memo");
2483 Pickler_set_memo(Picklerobject
*p
, PyObject
*v
)
2486 PyErr_SetString(PyExc_TypeError
,
2487 "attribute deletion is not supported");
2490 if (!PyDict_Check(v
)) {
2491 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
2494 Py_XDECREF(p
->memo
);
2501 Pickler_get_error(Picklerobject
*p
)
2503 /* why is this an attribute on the Pickler? */
2504 Py_INCREF(PicklingError
);
2505 return PicklingError
;
2508 static PyMemberDef Pickler_members
[] = {
2509 {"binary", T_INT
, offsetof(Picklerobject
, bin
)},
2510 {"fast", T_INT
, offsetof(Picklerobject
, fast
)},
2514 static PyGetSetDef Pickler_getsets
[] = {
2515 {"persistent_id", (getter
)Pickler_get_pers_func
,
2516 (setter
)Pickler_set_pers_func
},
2517 {"inst_persistent_id", NULL
, (setter
)Pickler_set_inst_pers_func
},
2518 {"memo", (getter
)Pickler_get_memo
, (setter
)Pickler_set_memo
},
2519 {"PicklingError", (getter
)Pickler_get_error
, NULL
},
2523 PyDoc_STRVAR(Picklertype__doc__
,
2524 "Objects that know how to pickle objects\n");
2526 static PyTypeObject Picklertype
= {
2527 PyObject_HEAD_INIT(NULL
)
2529 "cPickle.Pickler", /*tp_name*/
2530 sizeof(Picklerobject
), /*tp_basicsize*/
2532 (destructor
)Pickler_dealloc
, /* tp_dealloc */
2538 0, /* tp_as_number */
2539 0, /* tp_as_sequence */
2540 0, /* tp_as_mapping */
2544 0, /* set below */ /* tp_getattro */
2545 0, /* set below */ /* tp_setattro */
2546 0, /* tp_as_buffer */
2547 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
2548 Picklertype__doc__
, /* tp_doc */
2549 0, /* tp_traverse */
2551 0, /* tp_richcompare */
2552 0, /* tp_weaklistoffset */
2554 0, /* tp_iternext */
2555 Pickler_methods
, /* tp_methods */
2556 Pickler_members
, /* tp_members */
2557 Pickler_getsets
, /* tp_getset */
2561 find_class(PyObject
*py_module_name
, PyObject
*py_global_name
, PyObject
*fc
)
2563 PyObject
*global
= 0, *module
;
2567 PyErr_SetString(UnpicklingError
,
2568 "Global and instance pickles are not supported.");
2571 return PyObject_CallFunction(fc
, "OO", py_module_name
,
2575 module
= PySys_GetObject("modules");
2579 module
= PyDict_GetItem(module
, py_module_name
);
2580 if (module
== NULL
) {
2581 module
= PyImport_Import(py_module_name
);
2584 global
= PyObject_GetAttr(module
, py_global_name
);
2588 global
= PyObject_GetAttr(module
, py_global_name
);
2593 marker(Unpicklerobject
*self
)
2595 if (self
->num_marks
< 1) {
2596 PyErr_SetString(UnpicklingError
, "could not find MARK");
2600 return self
->marks
[--self
->num_marks
];
2605 load_none(Unpicklerobject
*self
)
2607 PDATA_APPEND(self
->stack
, Py_None
, -1);
2614 PyErr_SetString(UnpicklingError
, "pickle data was truncated");
2619 load_int(Unpicklerobject
*self
)
2621 PyObject
*py_int
= 0;
2626 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2627 if (len
< 2) return bad_readline();
2628 if (!( s
=pystrndup(s
,len
))) return -1;
2631 l
= strtol(s
, &endptr
, 0);
2633 if (errno
|| (*endptr
!= '\n') || (endptr
[1] != '\0')) {
2634 /* Hm, maybe we've got something long. Let's try reading
2635 it as a Python long object. */
2637 py_int
= PyLong_FromString(s
, NULL
, 0);
2638 if (py_int
== NULL
) {
2639 PyErr_SetString(PyExc_ValueError
,
2640 "could not convert string to int");
2645 if (len
== 3 && (l
== 0 || l
== 1)) {
2646 if (!( py_int
= PyBool_FromLong(l
))) goto finally
;
2649 if (!( py_int
= PyInt_FromLong(l
))) goto finally
;
2654 PDATA_PUSH(self
->stack
, py_int
, -1);
2665 calc_binint(char *s
, int x
)
2671 for (i
= 0, l
= 0L; i
< x
; i
++) {
2672 c
= (unsigned char)s
[i
];
2673 l
|= (long)c
<< (i
* 8);
2676 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
2677 * is signed, so on a box with longs bigger than 4 bytes we need
2678 * to extend a BININT's sign bit to the full width.
2680 if (x
== 4 && l
& (1L << 31))
2688 load_binintx(Unpicklerobject
*self
, char *s
, int x
)
2690 PyObject
*py_int
= 0;
2693 l
= calc_binint(s
, x
);
2695 if (!( py_int
= PyInt_FromLong(l
)))
2698 PDATA_PUSH(self
->stack
, py_int
, -1);
2704 load_binint(Unpicklerobject
*self
)
2708 if ((*self
->read_func
)(self
, &s
, 4) < 0)
2711 return load_binintx(self
, s
, 4);
2716 load_binint1(Unpicklerobject
*self
)
2720 if ((*self
->read_func
)(self
, &s
, 1) < 0)
2723 return load_binintx(self
, s
, 1);
2728 load_binint2(Unpicklerobject
*self
)
2732 if ((*self
->read_func
)(self
, &s
, 2) < 0)
2735 return load_binintx(self
, s
, 2);
2739 load_long(Unpicklerobject
*self
)
2745 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2746 if (len
< 2) return bad_readline();
2747 if (!( s
=pystrndup(s
,len
))) return -1;
2749 if (!( l
= PyLong_FromString(s
, &end
, 0)))
2753 PDATA_PUSH(self
->stack
, l
, -1);
2764 load_float(Unpicklerobject
*self
)
2766 PyObject
*py_float
= 0;
2771 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2772 if (len
< 2) return bad_readline();
2773 if (!( s
=pystrndup(s
,len
))) return -1;
2776 d
= strtod(s
, &endptr
);
2778 if (errno
|| (endptr
[0] != '\n') || (endptr
[1] != '\0')) {
2779 PyErr_SetString(PyExc_ValueError
,
2780 "could not convert string to float");
2784 if (!( py_float
= PyFloat_FromDouble(d
)))
2788 PDATA_PUSH(self
->stack
, py_float
, -1);
2798 load_binfloat(Unpicklerobject
*self
)
2800 PyObject
*py_float
= 0;
2806 if ((*self
->read_func
)(self
, &p
, 8) < 0)
2811 e
= (*p
& 0x7F) << 4;
2816 fhi
= (*p
& 0xF) << 24;
2820 fhi
|= (*p
& 0xFF) << 16;
2824 fhi
|= (*p
& 0xFF) << 8;
2832 flo
= (*p
& 0xFF) << 16;
2836 flo
|= (*p
& 0xFF) << 8;
2842 x
= (double)fhi
+ (double)flo
/ 16777216.0; /* 2**24 */
2843 x
/= 268435456.0; /* 2**28 */
2845 /* XXX This sadly ignores Inf/NaN */
2857 if (!( py_float
= PyFloat_FromDouble(x
))) return -1;
2859 PDATA_PUSH(self
->stack
, py_float
, -1);
2864 load_string(Unpicklerobject
*self
)
2870 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2871 if (len
< 2) return bad_readline();
2872 if (!( s
=pystrndup(s
,len
))) return -1;
2875 /* Strip outermost quotes */
2876 while (s
[len
-1] <= ' ')
2878 if(s
[0]=='"' && s
[len
-1]=='"'){
2882 } else if(s
[0]=='\'' && s
[len
-1]=='\''){
2888 /********************************************/
2890 str
= PyString_DecodeEscape(p
, len
, NULL
, 0, NULL
);
2892 PDATA_PUSH(self
->stack
, str
, -1);
2900 PyErr_SetString(PyExc_ValueError
,"insecure string pickle");
2906 load_binstring(Unpicklerobject
*self
)
2908 PyObject
*py_string
= 0;
2912 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
2914 l
= calc_binint(s
, 4);
2916 if ((*self
->read_func
)(self
, &s
, l
) < 0)
2919 if (!( py_string
= PyString_FromStringAndSize(s
, l
)))
2922 PDATA_PUSH(self
->stack
, py_string
, -1);
2928 load_short_binstring(Unpicklerobject
*self
)
2930 PyObject
*py_string
= 0;
2934 if ((*self
->read_func
)(self
, &s
, 1) < 0)
2937 l
= (unsigned char)s
[0];
2939 if ((*self
->read_func
)(self
, &s
, l
) < 0) return -1;
2941 if (!( py_string
= PyString_FromStringAndSize(s
, l
))) return -1;
2943 PDATA_PUSH(self
->stack
, py_string
, -1);
2948 #ifdef Py_USING_UNICODE
2950 load_unicode(Unpicklerobject
*self
)
2956 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2957 if (len
< 1) return bad_readline();
2959 if (!( str
= PyUnicode_DecodeRawUnicodeEscape(s
, len
- 1, NULL
)))
2962 PDATA_PUSH(self
->stack
, str
, -1);
2971 #ifdef Py_USING_UNICODE
2973 load_binunicode(Unpicklerobject
*self
)
2979 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
2981 l
= calc_binint(s
, 4);
2983 if ((*self
->read_func
)(self
, &s
, l
) < 0)
2986 if (!( unicode
= PyUnicode_DecodeUTF8(s
, l
, NULL
)))
2989 PDATA_PUSH(self
->stack
, unicode
, -1);
2996 load_tuple(Unpicklerobject
*self
)
3001 if ((i
= marker(self
)) < 0) return -1;
3002 if (!( tup
=Pdata_popTuple(self
->stack
, i
))) return -1;
3003 PDATA_PUSH(self
->stack
, tup
, -1);
3008 load_empty_tuple(Unpicklerobject
*self
)
3012 if (!( tup
=PyTuple_New(0))) return -1;
3013 PDATA_PUSH(self
->stack
, tup
, -1);
3018 load_empty_list(Unpicklerobject
*self
)
3022 if (!( list
=PyList_New(0))) return -1;
3023 PDATA_PUSH(self
->stack
, list
, -1);
3028 load_empty_dict(Unpicklerobject
*self
)
3032 if (!( dict
=PyDict_New())) return -1;
3033 PDATA_PUSH(self
->stack
, dict
, -1);
3039 load_list(Unpicklerobject
*self
)
3044 if ((i
= marker(self
)) < 0) return -1;
3045 if (!( list
=Pdata_popList(self
->stack
, i
))) return -1;
3046 PDATA_PUSH(self
->stack
, list
, -1);
3051 load_dict(Unpicklerobject
*self
)
3053 PyObject
*dict
, *key
, *value
;
3056 if ((i
= marker(self
)) < 0) return -1;
3057 j
=self
->stack
->length
;
3059 if (!( dict
= PyDict_New())) return -1;
3061 for (k
= i
+1; k
< j
; k
+= 2) {
3062 key
=self
->stack
->data
[k
-1];
3063 value
=self
->stack
->data
[k
];
3064 if (PyDict_SetItem(dict
, key
, value
) < 0) {
3069 Pdata_clear(self
->stack
, i
);
3070 PDATA_PUSH(self
->stack
, dict
, -1);
3075 Instance_New(PyObject
*cls
, PyObject
*args
)
3078 PyObject
*safe
=0, *r
=0;
3080 if (PyClass_Check(cls
)) {
3083 if ((l
=PyObject_Size(args
)) < 0) goto err
;
3085 PyObject
*__getinitargs__
;
3087 __getinitargs__
= PyObject_GetAttr(cls
,
3088 __getinitargs___str
);
3089 if (!__getinitargs__
) {
3090 /* We have a class with no __getinitargs__,
3091 so bypass usual construction */
3095 if (!( inst
=PyInstance_NewRaw(cls
, NULL
)))
3099 Py_DECREF(__getinitargs__
);
3102 if ((r
=PyInstance_New(cls
, args
, NULL
))) return r
;
3106 /* Is safe_constructors always a dict? */
3107 has_key
= cPickle_PyMapping_HasKey(safe_constructors
, cls
);
3109 safe
= PyObject_GetAttr(cls
, __safe_for_unpickling___str
);
3111 !PyObject_IsTrue(safe
)) {
3112 cPickle_ErrFormat(UnpicklingError
,
3113 "%s is not safe for unpickling",
3120 if (args
==Py_None
) {
3121 /* Special case, call cls.__basicnew__() */
3124 basicnew
= PyObject_GetAttr(cls
, __basicnew___str
);
3125 if (!basicnew
) return NULL
;
3126 r
=PyObject_CallObject(basicnew
, NULL
);
3127 Py_DECREF(basicnew
);
3131 if ((r
=PyObject_CallObject(cls
, args
))) return r
;
3135 PyObject
*tp
, *v
, *tb
;
3137 PyErr_Fetch(&tp
, &v
, &tb
);
3138 if ((r
=Py_BuildValue("OOO",v
,cls
,args
))) {
3142 PyErr_Restore(tp
,v
,tb
);
3149 load_obj(Unpicklerobject
*self
)
3151 PyObject
*class, *tup
, *obj
=0;
3154 if ((i
= marker(self
)) < 0) return -1;
3155 if (!( tup
=Pdata_popTuple(self
->stack
, i
+1))) return -1;
3156 PDATA_POP(self
->stack
, class);
3158 obj
= Instance_New(class, tup
);
3163 if (! obj
) return -1;
3164 PDATA_PUSH(self
->stack
, obj
, -1);
3170 load_inst(Unpicklerobject
*self
)
3172 PyObject
*tup
, *class=0, *obj
=0, *module_name
, *class_name
;
3176 if ((i
= marker(self
)) < 0) return -1;
3178 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3179 if (len
< 2) return bad_readline();
3180 module_name
= PyString_FromStringAndSize(s
, len
- 1);
3181 if (!module_name
) return -1;
3183 if ((len
= (*self
->readline_func
)(self
, &s
)) >= 0) {
3184 if (len
< 2) return bad_readline();
3185 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3186 class = find_class(module_name
, class_name
,
3188 Py_DECREF(class_name
);
3191 Py_DECREF(module_name
);
3193 if (! class) return -1;
3195 if ((tup
=Pdata_popTuple(self
->stack
, i
))) {
3196 obj
= Instance_New(class, tup
);
3201 if (! obj
) return -1;
3203 PDATA_PUSH(self
->stack
, obj
, -1);
3209 load_global(Unpicklerobject
*self
)
3211 PyObject
*class = 0, *module_name
= 0, *class_name
= 0;
3215 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3216 if (len
< 2) return bad_readline();
3217 module_name
= PyString_FromStringAndSize(s
, len
- 1);
3218 if (!module_name
) return -1;
3220 if ((len
= (*self
->readline_func
)(self
, &s
)) >= 0) {
3221 if (len
< 2) return bad_readline();
3222 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3223 class = find_class(module_name
, class_name
,
3225 Py_DECREF(class_name
);
3228 Py_DECREF(module_name
);
3230 if (! class) return -1;
3231 PDATA_PUSH(self
->stack
, class, -1);
3237 load_persid(Unpicklerobject
*self
)
3243 if (self
->pers_func
) {
3244 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3245 if (len
< 2) return bad_readline();
3247 pid
= PyString_FromStringAndSize(s
, len
- 1);
3248 if (!pid
) return -1;
3250 if (PyList_Check(self
->pers_func
)) {
3251 if (PyList_Append(self
->pers_func
, pid
) < 0) {
3259 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
3265 if (! pid
) return -1;
3267 PDATA_PUSH(self
->stack
, pid
, -1);
3271 PyErr_SetString(UnpicklingError
,
3272 "A load persistent id instruction was encountered,\n"
3273 "but no persistent_load function was specified.");
3279 load_binpersid(Unpicklerobject
*self
)
3283 if (self
->pers_func
) {
3284 PDATA_POP(self
->stack
, pid
);
3285 if (! pid
) return -1;
3287 if (PyList_Check(self
->pers_func
)) {
3288 if (PyList_Append(self
->pers_func
, pid
) < 0) {
3296 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
3300 if (! pid
) return -1;
3303 PDATA_PUSH(self
->stack
, pid
, -1);
3307 PyErr_SetString(UnpicklingError
,
3308 "A load persistent id instruction was encountered,\n"
3309 "but no persistent_load function was specified.");
3316 load_pop(Unpicklerobject
*self
)
3320 if (!( (len
=self
->stack
->length
) > 0 )) return stackUnderflow();
3322 /* Note that we split the (pickle.py) stack into two stacks,
3323 an object stack and a mark stack. We have to be clever and
3324 pop the right one. We do this by looking at the top of the
3328 if ((self
->num_marks
> 0) &&
3329 (self
->marks
[self
->num_marks
- 1] == len
))
3333 Py_DECREF(self
->stack
->data
[len
]);
3334 self
->stack
->length
=len
;
3342 load_pop_mark(Unpicklerobject
*self
)
3346 if ((i
= marker(self
)) < 0)
3349 Pdata_clear(self
->stack
, i
);
3356 load_dup(Unpicklerobject
*self
)
3361 if ((len
= self
->stack
->length
) <= 0) return stackUnderflow();
3362 last
=self
->stack
->data
[len
-1];
3364 PDATA_PUSH(self
->stack
, last
, -1);
3370 load_get(Unpicklerobject
*self
)
3372 PyObject
*py_str
= 0, *value
= 0;
3377 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3378 if (len
< 2) return bad_readline();
3380 if (!( py_str
= PyString_FromStringAndSize(s
, len
- 1))) return -1;
3382 value
= PyDict_GetItem(self
->memo
, py_str
);
3384 PyErr_SetObject(BadPickleGet
, py_str
);
3387 PDATA_APPEND(self
->stack
, value
, -1);
3397 load_binget(Unpicklerobject
*self
)
3399 PyObject
*py_key
= 0, *value
= 0;
3404 if ((*self
->read_func
)(self
, &s
, 1) < 0) return -1;
3406 key
= (unsigned char)s
[0];
3407 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
3409 value
= PyDict_GetItem(self
->memo
, py_key
);
3411 PyErr_SetObject(BadPickleGet
, py_key
);
3414 PDATA_APPEND(self
->stack
, value
, -1);
3424 load_long_binget(Unpicklerobject
*self
)
3426 PyObject
*py_key
= 0, *value
= 0;
3432 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
3434 c
= (unsigned char)s
[0];
3436 c
= (unsigned char)s
[1];
3437 key
|= (long)c
<< 8;
3438 c
= (unsigned char)s
[2];
3439 key
|= (long)c
<< 16;
3440 c
= (unsigned char)s
[3];
3441 key
|= (long)c
<< 24;
3443 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
3445 value
= PyDict_GetItem(self
->memo
, py_key
);
3447 PyErr_SetObject(BadPickleGet
, py_key
);
3450 PDATA_APPEND(self
->stack
, value
, -1);
3460 load_put(Unpicklerobject
*self
)
3462 PyObject
*py_str
= 0, *value
= 0;
3466 if ((l
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3467 if (l
< 2) return bad_readline();
3468 if (!( len
=self
->stack
->length
)) return stackUnderflow();
3469 if (!( py_str
= PyString_FromStringAndSize(s
, l
- 1))) return -1;
3470 value
=self
->stack
->data
[len
-1];
3471 l
=PyDict_SetItem(self
->memo
, py_str
, value
);
3478 load_binput(Unpicklerobject
*self
)
3480 PyObject
*py_key
= 0, *value
= 0;
3485 if ((*self
->read_func
)(self
, &s
, 1) < 0) return -1;
3486 if (!( (len
=self
->stack
->length
) > 0 )) return stackUnderflow();
3488 key
= (unsigned char)s
[0];
3490 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
3491 value
=self
->stack
->data
[len
-1];
3492 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
3499 load_long_binput(Unpicklerobject
*self
)
3501 PyObject
*py_key
= 0, *value
= 0;
3507 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
3508 if (!( len
=self
->stack
->length
)) return stackUnderflow();
3510 c
= (unsigned char)s
[0];
3512 c
= (unsigned char)s
[1];
3513 key
|= (long)c
<< 8;
3514 c
= (unsigned char)s
[2];
3515 key
|= (long)c
<< 16;
3516 c
= (unsigned char)s
[3];
3517 key
|= (long)c
<< 24;
3519 if (!( py_key
= PyInt_FromLong(key
))) return -1;
3520 value
=self
->stack
->data
[len
-1];
3521 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
3528 do_append(Unpicklerobject
*self
, int x
)
3530 PyObject
*value
= 0, *list
= 0, *append_method
= 0;
3533 len
=self
->stack
->length
;
3534 if (!( len
>= x
&& x
> 0 )) return stackUnderflow();
3536 if (len
==x
) return 0;
3538 list
=self
->stack
->data
[x
-1];
3540 if (PyList_Check(list
)) {
3544 slice
=Pdata_popList(self
->stack
, x
);
3545 list_len
= PyList_GET_SIZE(list
);
3546 i
=PyList_SetSlice(list
, list_len
, list_len
, slice
);
3552 if (!( append_method
= PyObject_GetAttr(list
, append_str
)))
3555 for (i
= x
; i
< len
; i
++) {
3558 value
=self
->stack
->data
[i
];
3560 ARG_TUP(self
, value
);
3562 junk
= PyObject_Call(append_method
, self
->arg
,
3567 Pdata_clear(self
->stack
, i
+1);
3568 self
->stack
->length
=x
;
3569 Py_DECREF(append_method
);
3574 self
->stack
->length
=x
;
3575 Py_DECREF(append_method
);
3583 load_append(Unpicklerobject
*self
)
3585 return do_append(self
, self
->stack
->length
- 1);
3590 load_appends(Unpicklerobject
*self
)
3592 return do_append(self
, marker(self
));
3597 do_setitems(Unpicklerobject
*self
, int x
)
3599 PyObject
*value
= 0, *key
= 0, *dict
= 0;
3602 if (!( (len
=self
->stack
->length
) >= x
3603 && x
> 0 )) return stackUnderflow();
3605 dict
=self
->stack
->data
[x
-1];
3607 for (i
= x
+1; i
< len
; i
+= 2) {
3608 key
=self
->stack
->data
[i
-1];
3609 value
=self
->stack
->data
[i
];
3610 if (PyObject_SetItem(dict
, key
, value
) < 0) {
3616 Pdata_clear(self
->stack
, x
);
3623 load_setitem(Unpicklerobject
*self
)
3625 return do_setitems(self
, self
->stack
->length
- 2);
3629 load_setitems(Unpicklerobject
*self
)
3631 return do_setitems(self
, marker(self
));
3636 load_build(Unpicklerobject
*self
)
3638 PyObject
*value
= 0, *inst
= 0, *instdict
= 0, *d_key
= 0, *d_value
= 0,
3639 *junk
= 0, *__setstate__
= 0;
3642 if (self
->stack
->length
< 2) return stackUnderflow();
3643 PDATA_POP(self
->stack
, value
);
3644 if (! value
) return -1;
3645 inst
=self
->stack
->data
[self
->stack
->length
-1];
3647 if ((__setstate__
= PyObject_GetAttr(inst
, __setstate___str
))) {
3648 ARG_TUP(self
, value
);
3650 junk
= PyObject_Call(__setstate__
, self
->arg
, NULL
);
3653 Py_DECREF(__setstate__
);
3654 if (! junk
) return -1;
3660 if ((instdict
= PyObject_GetAttr(inst
, __dict___str
))) {
3662 while (PyDict_Next(value
, &i
, &d_key
, &d_value
)) {
3663 if (PyObject_SetItem(instdict
, d_key
, d_value
) < 0) {
3668 Py_DECREF(instdict
);
3679 load_mark(Unpicklerobject
*self
)
3683 /* Note that we split the (pickle.py) stack into two stacks, an
3684 object stack and a mark stack. Here we push a mark onto the
3688 if ((self
->num_marks
+ 1) >= self
->marks_size
) {
3689 s
=self
->marks_size
+20;
3690 if (s
<= self
->num_marks
) s
=self
->num_marks
+ 1;
3691 if (self
->marks
== NULL
)
3692 self
->marks
=(int *)malloc(s
* sizeof(int));
3694 self
->marks
=(int *)realloc(self
->marks
,
3696 if (! self
->marks
) {
3700 self
->marks_size
= s
;
3703 self
->marks
[self
->num_marks
++] = self
->stack
->length
;
3709 load_reduce(Unpicklerobject
*self
)
3711 PyObject
*callable
= 0, *arg_tup
= 0, *ob
= 0;
3713 PDATA_POP(self
->stack
, arg_tup
);
3714 if (! arg_tup
) return -1;
3715 PDATA_POP(self
->stack
, callable
);
3717 ob
= Instance_New(callable
, arg_tup
);
3718 Py_DECREF(callable
);
3722 if (! ob
) return -1;
3724 PDATA_PUSH(self
->stack
, ob
, -1);
3729 load(Unpicklerobject
*self
)
3731 PyObject
*err
= 0, *val
= 0;
3734 self
->num_marks
= 0;
3735 if (self
->stack
->length
) Pdata_clear(self
->stack
, 0);
3738 if ((*self
->read_func
)(self
, &s
, 1) < 0)
3743 if (load_none(self
) < 0)
3748 if (load_binint(self
) < 0)
3753 if (load_binint1(self
) < 0)
3758 if (load_binint2(self
) < 0)
3763 if (load_int(self
) < 0)
3768 if (load_long(self
) < 0)
3773 if (load_float(self
) < 0)
3778 if (load_binfloat(self
) < 0)
3783 if (load_binstring(self
) < 0)
3787 case SHORT_BINSTRING
:
3788 if (load_short_binstring(self
) < 0)
3793 if (load_string(self
) < 0)
3797 #ifdef Py_USING_UNICODE
3799 if (load_unicode(self
) < 0)
3804 if (load_binunicode(self
) < 0)
3810 if (load_empty_tuple(self
) < 0)
3815 if (load_tuple(self
) < 0)
3820 if (load_empty_list(self
) < 0)
3825 if (load_list(self
) < 0)
3830 if (load_empty_dict(self
) < 0)
3835 if (load_dict(self
) < 0)
3840 if (load_obj(self
) < 0)
3845 if (load_inst(self
) < 0)
3850 if (load_global(self
) < 0)
3855 if (load_append(self
) < 0)
3860 if (load_appends(self
) < 0)
3865 if (load_build(self
) < 0)
3870 if (load_dup(self
) < 0)
3875 if (load_binget(self
) < 0)
3880 if (load_long_binget(self
) < 0)
3885 if (load_get(self
) < 0)
3890 if (load_mark(self
) < 0)
3895 if (load_binput(self
) < 0)
3900 if (load_long_binput(self
) < 0)
3905 if (load_put(self
) < 0)
3910 if (load_pop(self
) < 0)
3915 if (load_pop_mark(self
) < 0)
3920 if (load_setitem(self
) < 0)
3925 if (load_setitems(self
) < 0)
3933 if (load_persid(self
) < 0)
3938 if (load_binpersid(self
) < 0)
3943 if (load_reduce(self
) < 0)
3949 PyErr_SetNone(PyExc_EOFError
);
3953 cPickle_ErrFormat(UnpicklingError
,
3954 "invalid load key, '%s'.",
3962 if ((err
= PyErr_Occurred())) {
3963 if (err
== PyExc_EOFError
) {
3964 PyErr_SetNone(PyExc_EOFError
);
3969 PDATA_POP(self
->stack
, val
);
3974 /* No-load functions to support noload, which is used to
3975 find persistent references. */
3978 noload_obj(Unpicklerobject
*self
)
3982 if ((i
= marker(self
)) < 0) return -1;
3983 return Pdata_clear(self
->stack
, i
+1);
3988 noload_inst(Unpicklerobject
*self
)
3993 if ((i
= marker(self
)) < 0) return -1;
3994 Pdata_clear(self
->stack
, i
);
3995 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3996 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3997 PDATA_APPEND(self
->stack
, Py_None
,-1);
4002 noload_global(Unpicklerobject
*self
)
4006 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
4007 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
4008 PDATA_APPEND(self
->stack
, Py_None
,-1);
4013 noload_reduce(Unpicklerobject
*self
)
4016 if (self
->stack
->length
< 2) return stackUnderflow();
4017 Pdata_clear(self
->stack
, self
->stack
->length
-2);
4018 PDATA_APPEND(self
->stack
, Py_None
,-1);
4023 noload_build(Unpicklerobject
*self
) {
4025 if (self
->stack
->length
< 1) return stackUnderflow();
4026 Pdata_clear(self
->stack
, self
->stack
->length
-1);
4032 noload(Unpicklerobject
*self
)
4034 PyObject
*err
= 0, *val
= 0;
4037 self
->num_marks
= 0;
4038 Pdata_clear(self
->stack
, 0);
4041 if ((*self
->read_func
)(self
, &s
, 1) < 0)
4046 if (load_none(self
) < 0)
4051 if (load_binint(self
) < 0)
4056 if (load_binint1(self
) < 0)
4061 if (load_binint2(self
) < 0)
4066 if (load_int(self
) < 0)
4071 if (load_long(self
) < 0)
4076 if (load_float(self
) < 0)
4081 if (load_binfloat(self
) < 0)
4086 if (load_binstring(self
) < 0)
4090 case SHORT_BINSTRING
:
4091 if (load_short_binstring(self
) < 0)
4096 if (load_string(self
) < 0)
4100 #ifdef Py_USING_UNICODE
4102 if (load_unicode(self
) < 0)
4107 if (load_binunicode(self
) < 0)
4113 if (load_empty_tuple(self
) < 0)
4118 if (load_tuple(self
) < 0)
4123 if (load_empty_list(self
) < 0)
4128 if (load_list(self
) < 0)
4133 if (load_empty_dict(self
) < 0)
4138 if (load_dict(self
) < 0)
4143 if (noload_obj(self
) < 0)
4148 if (noload_inst(self
) < 0)
4153 if (noload_global(self
) < 0)
4158 if (load_append(self
) < 0)
4163 if (load_appends(self
) < 0)
4168 if (noload_build(self
) < 0)
4173 if (load_dup(self
) < 0)
4178 if (load_binget(self
) < 0)
4183 if (load_long_binget(self
) < 0)
4188 if (load_get(self
) < 0)
4193 if (load_mark(self
) < 0)
4198 if (load_binput(self
) < 0)
4203 if (load_long_binput(self
) < 0)
4208 if (load_put(self
) < 0)
4213 if (load_pop(self
) < 0)
4218 if (load_pop_mark(self
) < 0)
4223 if (load_setitem(self
) < 0)
4228 if (load_setitems(self
) < 0)
4236 if (load_persid(self
) < 0)
4241 if (load_binpersid(self
) < 0)
4246 if (noload_reduce(self
) < 0)
4251 cPickle_ErrFormat(UnpicklingError
,
4252 "invalid load key, '%s'.",
4260 if ((err
= PyErr_Occurred())) {
4261 if (err
== PyExc_EOFError
) {
4262 PyErr_SetNone(PyExc_EOFError
);
4267 PDATA_POP(self
->stack
, val
);
4273 Unpickler_load(Unpicklerobject
*self
, PyObject
*args
)
4275 if (!( PyArg_ParseTuple(args
, ":load")))
4282 Unpickler_noload(Unpicklerobject
*self
, PyObject
*args
)
4284 if (!( PyArg_ParseTuple(args
, ":noload")))
4287 return noload(self
);
4291 static struct PyMethodDef Unpickler_methods
[] = {
4292 {"load", (PyCFunction
)Unpickler_load
, METH_VARARGS
,
4293 PyDoc_STR("load() -- Load a pickle")
4295 {"noload", (PyCFunction
)Unpickler_noload
, METH_VARARGS
,
4297 "noload() -- not load a pickle, but go through most of the motions\n"
4299 "This function can be used to read past a pickle without instantiating\n"
4300 "any objects or importing any modules. It can also be used to find all\n"
4301 "persistent references without instantiating any objects or importing\n"
4304 {NULL
, NULL
} /* sentinel */
4308 static Unpicklerobject
*
4309 newUnpicklerobject(PyObject
*f
)
4311 Unpicklerobject
*self
;
4313 if (!( self
= PyObject_New(Unpicklerobject
, &Unpicklertype
)))
4318 self
->stack
= (Pdata
*)Pdata_New();
4319 self
->pers_func
= NULL
;
4320 self
->last_string
= NULL
;
4322 self
->num_marks
= 0;
4323 self
->marks_size
= 0;
4326 self
->readline
= NULL
;
4327 self
->safe_constructors
= NULL
;
4328 self
->find_class
= NULL
;
4330 if (!( self
->memo
= PyDict_New()))
4336 /* Set read, readline based on type of f */
4337 if (PyFile_Check(f
)) {
4338 self
->fp
= PyFile_AsFile(f
);
4339 if (self
->fp
== NULL
) {
4340 PyErr_SetString(PyExc_ValueError
,
4341 "I/O operation on closed file");
4344 self
->read_func
= read_file
;
4345 self
->readline_func
= readline_file
;
4347 else if (PycStringIO_InputCheck(f
)) {
4349 self
->read_func
= read_cStringIO
;
4350 self
->readline_func
= readline_cStringIO
;
4355 self
->read_func
= read_other
;
4356 self
->readline_func
= readline_other
;
4358 if (!( (self
->readline
= PyObject_GetAttr(f
, readline_str
)) &&
4359 (self
->read
= PyObject_GetAttr(f
, read_str
)))) {
4361 PyErr_SetString( PyExc_TypeError
,
4362 "argument must have 'read' and "
4363 "'readline' attributes" );
4368 if (PyEval_GetRestricted()) {
4369 /* Restricted execution, get private tables */
4372 if (!( m
=PyImport_Import(copy_reg_str
))) goto err
;
4373 self
->safe_constructors
=PyObject_GetAttr(m
,
4374 safe_constructors_str
);
4376 if (!( self
->safe_constructors
)) goto err
;
4379 self
->safe_constructors
=safe_constructors
;
4380 Py_INCREF(safe_constructors
);
4386 Py_DECREF((PyObject
*)self
);
4392 get_Unpickler(PyObject
*self
, PyObject
*args
)
4396 if (!( PyArg_ParseTuple(args
, "O:Unpickler", &file
)))
4398 return (PyObject
*)newUnpicklerobject(file
);
4403 Unpickler_dealloc(Unpicklerobject
*self
)
4405 Py_XDECREF(self
->readline
);
4406 Py_XDECREF(self
->read
);
4407 Py_XDECREF(self
->file
);
4408 Py_XDECREF(self
->memo
);
4409 Py_XDECREF(self
->stack
);
4410 Py_XDECREF(self
->pers_func
);
4411 Py_XDECREF(self
->arg
);
4412 Py_XDECREF(self
->last_string
);
4413 Py_XDECREF(self
->safe_constructors
);
4419 if (self
->buf_size
) {
4428 Unpickler_getattr(Unpicklerobject
*self
, char *name
)
4430 if (!strcmp(name
, "persistent_load")) {
4431 if (!self
->pers_func
) {
4432 PyErr_SetString(PyExc_AttributeError
, name
);
4436 Py_INCREF(self
->pers_func
);
4437 return self
->pers_func
;
4440 if (!strcmp(name
, "find_global")) {
4441 if (!self
->find_class
) {
4442 PyErr_SetString(PyExc_AttributeError
, name
);
4446 Py_INCREF(self
->find_class
);
4447 return self
->find_class
;
4450 if (!strcmp(name
, "memo")) {
4452 PyErr_SetString(PyExc_AttributeError
, name
);
4456 Py_INCREF(self
->memo
);
4460 if (!strcmp(name
, "UnpicklingError")) {
4461 Py_INCREF(UnpicklingError
);
4462 return UnpicklingError
;
4465 return Py_FindMethod(Unpickler_methods
, (PyObject
*)self
, name
);
4470 Unpickler_setattr(Unpicklerobject
*self
, char *name
, PyObject
*value
)
4473 if (!strcmp(name
, "persistent_load")) {
4474 Py_XDECREF(self
->pers_func
);
4475 self
->pers_func
= value
;
4480 if (!strcmp(name
, "find_global")) {
4481 Py_XDECREF(self
->find_class
);
4482 self
->find_class
= value
;
4488 PyErr_SetString(PyExc_TypeError
,
4489 "attribute deletion is not supported");
4493 if (strcmp(name
, "memo") == 0) {
4494 if (!PyDict_Check(value
)) {
4495 PyErr_SetString(PyExc_TypeError
,
4496 "memo must be a dictionary");
4499 Py_XDECREF(self
->memo
);
4505 PyErr_SetString(PyExc_AttributeError
, name
);
4511 cpm_dump(PyObject
*self
, PyObject
*args
)
4513 PyObject
*ob
, *file
, *res
= NULL
;
4514 Picklerobject
*pickler
= 0;
4517 if (!( PyArg_ParseTuple(args
, "OO|i", &ob
, &file
, &bin
)))
4520 if (!( pickler
= newPicklerobject(file
, bin
)))
4523 if (dump(pickler
, ob
) < 0)
4530 Py_XDECREF(pickler
);
4537 cpm_dumps(PyObject
*self
, PyObject
*args
)
4539 PyObject
*ob
, *file
= 0, *res
= NULL
;
4540 Picklerobject
*pickler
= 0;
4543 if (!( PyArg_ParseTuple(args
, "O|i:dumps", &ob
, &bin
)))
4546 if (!( file
= PycStringIO
->NewOutput(128)))
4549 if (!( pickler
= newPicklerobject(file
, bin
)))
4552 if (dump(pickler
, ob
) < 0)
4555 res
= PycStringIO
->cgetvalue(file
);
4558 Py_XDECREF(pickler
);
4566 cpm_load(PyObject
*self
, PyObject
*args
)
4568 Unpicklerobject
*unpickler
= 0;
4569 PyObject
*ob
, *res
= NULL
;
4571 if (!( PyArg_ParseTuple(args
, "O:load", &ob
)))
4574 if (!( unpickler
= newUnpicklerobject(ob
)))
4577 res
= load(unpickler
);
4580 Py_XDECREF(unpickler
);
4587 cpm_loads(PyObject
*self
, PyObject
*args
)
4589 PyObject
*ob
, *file
= 0, *res
= NULL
;
4590 Unpicklerobject
*unpickler
= 0;
4592 if (!( PyArg_ParseTuple(args
, "S:loads", &ob
)))
4595 if (!( file
= PycStringIO
->NewInput(ob
)))
4598 if (!( unpickler
= newUnpicklerobject(file
)))
4601 res
= load(unpickler
);
4605 Py_XDECREF(unpickler
);
4611 PyDoc_STRVAR(Unpicklertype__doc__
,
4612 "Objects that know how to unpickle");
4614 static PyTypeObject Unpicklertype
= {
4615 PyObject_HEAD_INIT(NULL
)
4617 "cPickle.Unpickler", /*tp_name*/
4618 sizeof(Unpicklerobject
), /*tp_basicsize*/
4621 (destructor
)Unpickler_dealloc
, /*tp_dealloc*/
4622 (printfunc
)0, /*tp_print*/
4623 (getattrfunc
)Unpickler_getattr
, /*tp_getattr*/
4624 (setattrfunc
)Unpickler_setattr
, /*tp_setattr*/
4625 (cmpfunc
)0, /*tp_compare*/
4626 (reprfunc
)0, /*tp_repr*/
4628 0, /*tp_as_sequence*/
4629 0, /*tp_as_mapping*/
4630 (hashfunc
)0, /*tp_hash*/
4631 (ternaryfunc
)0, /*tp_call*/
4632 (reprfunc
)0, /*tp_str*/
4634 /* Space for future expansion */
4636 Unpicklertype__doc__
/* Documentation string */
4639 static struct PyMethodDef cPickle_methods
[] = {
4640 {"dump", (PyCFunction
)cpm_dump
, METH_VARARGS
,
4641 PyDoc_STR("dump(object, file, [binary]) --"
4642 "Write an object in pickle format to the given file\n"
4644 "If the optional argument, binary, is provided and is true, then the\n"
4645 "pickle will be written in binary format, which is more space and\n"
4646 "computationally efficient. \n")
4648 {"dumps", (PyCFunction
)cpm_dumps
, METH_VARARGS
,
4649 PyDoc_STR("dumps(object, [binary]) --"
4650 "Return a string containing an object in pickle format\n"
4652 "If the optional argument, binary, is provided and is true, then the\n"
4653 "pickle will be written in binary format, which is more space and\n"
4654 "computationally efficient. \n")
4656 {"load", (PyCFunction
)cpm_load
, METH_VARARGS
,
4657 PyDoc_STR("load(file) -- Load a pickle from the given file")},
4658 {"loads", (PyCFunction
)cpm_loads
, METH_VARARGS
,
4659 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
4660 {"Pickler", (PyCFunction
)get_Pickler
, METH_VARARGS
,
4661 PyDoc_STR("Pickler(file, [binary]) -- Create a pickler\n"
4663 "If the optional argument, binary, is provided and is true, then\n"
4664 "pickles will be written in binary format, which is more space and\n"
4665 "computationally efficient. \n")
4667 {"Unpickler", (PyCFunction
)get_Unpickler
, METH_VARARGS
,
4668 PyDoc_STR("Unpickler(file) -- Create an unpickler")},
4673 init_stuff(PyObject
*module_dict
)
4675 PyObject
*copy_reg
, *t
, *r
;
4677 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
4679 INIT_STR(__class__
);
4680 INIT_STR(__getinitargs__
);
4682 INIT_STR(__getstate__
);
4683 INIT_STR(__setstate__
);
4686 INIT_STR(__reduce__
);
4688 INIT_STR(__safe_for_unpickling__
);
4693 INIT_STR(dispatch_table
);
4694 INIT_STR(safe_constructors
);
4695 INIT_STR(__basicnew__
);
4697 if (!( copy_reg
= PyImport_ImportModule("copy_reg")))
4700 /* These next few are special because we want to use different
4701 ones in restricted mode. */
4702 dispatch_table
= PyObject_GetAttr(copy_reg
, dispatch_table_str
);
4703 if (!dispatch_table
)
4706 if (!( safe_constructors
= PyObject_GetAttr(copy_reg
,
4707 safe_constructors_str
)))
4710 Py_DECREF(copy_reg
);
4712 /* Down to here ********************************** */
4714 if (!( empty_tuple
= PyTuple_New(0)))
4718 if (!( t
=PyImport_ImportModule("__builtin__"))) return -1;
4719 if (PyDict_SetItemString(module_dict
, "__builtins__", t
) < 0)
4722 if (!( t
=PyDict_New())) return -1;
4723 if (!( r
=PyRun_String(
4724 "def __init__(self, *args): self.args=args\n\n"
4725 "def __str__(self):\n"
4726 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4728 module_dict
, t
) )) return -1;
4731 PickleError
= PyErr_NewException("cPickle.PickleError", NULL
, t
);
4737 PicklingError
= PyErr_NewException("cPickle.PicklingError",
4742 if (!( t
=PyDict_New())) return -1;
4743 if (!( r
=PyRun_String(
4744 "def __init__(self, *args): self.args=args\n\n"
4745 "def __str__(self):\n"
4747 " a=a and type(a[0]) or '(what)'\n"
4748 " return 'Cannot pickle %s objects' % a\n"
4750 module_dict
, t
) )) return -1;
4753 if (!( UnpickleableError
= PyErr_NewException(
4754 "cPickle.UnpickleableError", PicklingError
, t
)))
4759 if (!( UnpicklingError
= PyErr_NewException("cPickle.UnpicklingError",
4760 PickleError
, NULL
)))
4763 if (!( BadPickleGet
= PyErr_NewException("cPickle.BadPickleGet",
4764 UnpicklingError
, NULL
)))
4767 if (PyDict_SetItemString(module_dict
, "PickleError",
4771 if (PyDict_SetItemString(module_dict
, "PicklingError",
4775 if (PyDict_SetItemString(module_dict
, "UnpicklingError",
4776 UnpicklingError
) < 0)
4779 if (PyDict_SetItemString(module_dict
, "UnpickleableError",
4780 UnpickleableError
) < 0)
4783 if (PyDict_SetItemString(module_dict
, "BadPickleGet",
4792 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
4793 #define PyMODINIT_FUNC void
4798 PyObject
*m
, *d
, *di
, *v
, *k
;
4801 PyObject
*format_version
;
4802 PyObject
*compatible_formats
;
4804 Picklertype
.ob_type
= &PyType_Type
;
4805 Picklertype
.tp_getattro
= PyObject_GenericGetAttr
;
4806 Picklertype
.tp_setattro
= PyObject_GenericSetAttr
;
4807 Unpicklertype
.ob_type
= &PyType_Type
;
4808 PdataType
.ob_type
= &PyType_Type
;
4810 /* Initialize some pieces. We need to do this before module creation,
4811 so we're forced to use a temporary dictionary. :(
4815 if (init_stuff(di
) < 0) return;
4817 /* Create the module and add the functions */
4818 m
= Py_InitModule4("cPickle", cPickle_methods
,
4819 cPickle_module_documentation
,
4820 (PyObject
*)NULL
,PYTHON_API_VERSION
);
4822 /* Add some symbolic constants to the module */
4823 d
= PyModule_GetDict(m
);
4824 PyDict_SetItemString(d
,"__version__", v
= PyString_FromString(rev
));
4827 /* Copy data from di. Waaa. */
4828 for (i
=0; PyDict_Next(di
, &i
, &k
, &v
); ) {
4829 if (PyObject_SetItem(d
, k
, v
) < 0) {
4836 format_version
= PyString_FromString("1.3");
4837 compatible_formats
= Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
4839 PyDict_SetItemString(d
, "format_version", format_version
);
4840 PyDict_SetItemString(d
, "compatible_formats", compatible_formats
);
4841 Py_XDECREF(format_version
);
4842 Py_XDECREF(compatible_formats
);