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 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since
22 * all headers have already been included here, we can safely redefine it.
29 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
30 * docs are in pickletools.py.
50 #define SHORT_BINSTRING 'U'
52 #define BINUNICODE 'X'
57 #define EMPTY_DICT '}'
62 #define LONG_BINGET 'j'
64 #define EMPTY_LIST ']'
68 #define LONG_BINPUT 'r'
71 #define EMPTY_TUPLE ')'
75 #define PROTO '\x80' /* identify pickle protocol */
76 #define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
77 #define EXT1 '\x82' /* push object from extension registry; 1-byte index */
78 #define EXT2 '\x83' /* ditto, but 2-byte index */
79 #define EXT4 '\x84' /* ditto, but 4-byte index */
80 #define TUPLE1 '\x85' /* build 1-tuple from stack top */
81 #define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
82 #define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
83 #define NEWTRUE '\x88' /* push True */
84 #define NEWFALSE '\x89' /* push False */
85 #define LONG1 '\x8a' /* push long from < 256 bytes */
86 #define LONG4 '\x8b' /* push really big long */
88 /* There aren't opcodes -- they're ways to pickle bools before protocol 2,
89 * so that unpicklers written before bools were introduced unpickle them
90 * as ints, but unpicklers after can recognize that bools were intended.
91 * Note that protocol 2 added direct ways to pickle bools.
98 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
99 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
100 * break if this gets out of synch with pickle.py, but it's unclear that
101 * would help anything either.
103 #define BATCHSIZE 1000
105 static char MARKv
= MARK
;
107 static PyObject
*PickleError
;
108 static PyObject
*PicklingError
;
109 static PyObject
*UnpickleableError
;
110 static PyObject
*UnpicklingError
;
111 static PyObject
*BadPickleGet
;
113 /* As the name says, an empty tuple. */
114 static PyObject
*empty_tuple
;
116 /* copy_reg.dispatch_table, {type_object: pickling_function} */
117 static PyObject
*dispatch_table
;
119 /* For EXT[124] opcodes. */
120 /* copy_reg._extension_registry, {(module_name, function_name): code} */
121 static PyObject
*extension_registry
;
122 /* copy_reg._inverted_registry, {code: (module_name, function_name)} */
123 static PyObject
*inverted_registry
;
124 /* copy_reg._extension_cache, {code: object} */
125 static PyObject
*extension_cache
;
127 /* For looking up name pairs in copy_reg._extension_registry. */
128 static PyObject
*two_tuple
;
130 static PyObject
*__class___str
, *__getinitargs___str
, *__dict___str
,
131 *__getstate___str
, *__setstate___str
, *__name___str
, *__reduce___str
,
133 *write_str
, *append_str
,
134 *read_str
, *readline_str
, *__main___str
,
135 *copyreg_str
, *dispatch_table_str
;
137 /*************************************************************************
138 Internal Data type for pickle data. */
142 int length
; /* number of initial slots in data currently used */
143 int size
; /* number of slots in data allocated */
148 Pdata_dealloc(Pdata
*self
)
153 for (i
= self
->length
, p
= self
->data
; --i
>= 0; p
++) {
161 static PyTypeObject PdataType
= {
162 PyVarObject_HEAD_INIT(NULL
, 0) "cPickle.Pdata", sizeof(Pdata
), 0,
163 (destructor
)Pdata_dealloc
,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
167 #define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
174 if (!(self
= PyObject_New(Pdata
, &PdataType
)))
178 self
->data
= malloc(self
->size
* sizeof(PyObject
*));
180 return (PyObject
*)self
;
182 return PyErr_NoMemory();
188 PyErr_SetString(UnpicklingError
, "unpickling stack underflow");
192 /* Retain only the initial clearto items. If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
196 Pdata_clear(Pdata
*self
, int clearto
)
201 if (clearto
< 0) return stackUnderflow();
202 if (clearto
>= self
->length
) return 0;
204 for (i
= self
->length
, p
= self
->data
+ clearto
;
209 self
->length
= clearto
;
215 Pdata_grow(Pdata
*self
)
221 bigger
= self
->size
<< 1;
222 if (bigger
<= 0) /* was 0, or new value overflows */
224 if ((int)(size_t)bigger
!= bigger
)
226 nbytes
= (size_t)bigger
* sizeof(PyObject
*);
227 if (nbytes
/ sizeof(PyObject
*) != (size_t)bigger
)
229 tmp
= realloc(self
->data
, nbytes
);
241 /* D is a Pdata*. Pop the topmost element and store it into V, which
242 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
243 * is raised and V is set to NULL. D and V may be evaluated several times.
245 #define PDATA_POP(D, V) { \
247 (V) = (D)->data[--((D)->length)]; \
249 PyErr_SetString(UnpicklingError, "bad pickle data"); \
254 /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
255 * D. If the Pdata stack can't be grown to hold the new value, both
256 * raise MemoryError and execute "return ER". The difference is in ownership
257 * of O after: _PUSH transfers ownership of O from the caller to the stack
258 * (no incref of O is done, and in case of error O is decrefed), while
259 * _APPEND pushes a new reference.
262 /* Push O on stack D, giving ownership of O to the stack. */
263 #define PDATA_PUSH(D, O, ER) { \
264 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
265 Pdata_grow((Pdata*)(D)) < 0) { \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
272 /* Push O on stack D, pushing a new reference. */
273 #define PDATA_APPEND(D, O, ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
283 Pdata_popTuple(Pdata
*self
, int start
)
288 l
= self
->length
-start
;
292 for (i
= start
, j
= 0 ; j
< l
; i
++, j
++)
293 PyTuple_SET_ITEM(r
, j
, self
->data
[i
]);
295 self
->length
= start
;
300 Pdata_popList(Pdata
*self
, int start
)
305 l
=self
->length
-start
;
306 if (!( r
=PyList_New(l
))) return NULL
;
307 for (i
=start
, j
=0 ; j
< l
; i
++, j
++)
308 PyList_SET_ITEM(r
, j
, self
->data
[i
]);
314 /*************************************************************************/
316 #define ARG_TUP(self, o) { \
317 if (self->arg || (self->arg=PyTuple_New(1))) { \
318 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
319 PyTuple_SET_ITEM(self->arg,0,o); \
326 #define FREE_ARG_TUP(self) { \
327 if (Py_REFCNT(self->arg) > 1) { \
328 Py_DECREF(self->arg); \
333 typedef struct Picklerobject
{
341 PyObject
*inst_pers_func
;
343 /* pickle protocol number, >= 0 */
346 /* bool, true if proto > 0 */
349 int fast
; /* Fast mode doesn't save in memo, don't use if circ ref */
350 int (*write_func
)(struct Picklerobject
*, const char *, Py_ssize_t
);
353 PyObject
*dispatch_table
;
354 int fast_container
; /* count nested container dumps */
358 #ifndef PY_CPICKLE_FAST_LIMIT
359 #define PY_CPICKLE_FAST_LIMIT 50
362 static PyTypeObject Picklertype
;
364 typedef struct Unpicklerobject
{
375 PyObject
*last_string
;
379 Py_ssize_t (*read_func
)(struct Unpicklerobject
*, char **, Py_ssize_t
);
380 Py_ssize_t (*readline_func
)(struct Unpicklerobject
*, char **);
383 PyObject
*find_class
;
386 static PyTypeObject Unpicklertype
;
388 /* Forward decls that need the above structs */
389 static int save(Picklerobject
*, PyObject
*, int);
390 static int put2(Picklerobject
*, PyObject
*);
394 cPickle_ErrFormat(PyObject
*ErrType
, char *stringformat
, char *format
, ...)
397 PyObject
*args
=0, *retval
=0;
398 va_start(va
, format
);
400 if (format
) args
= Py_VaBuildValue(format
, va
);
402 if (format
&& ! args
) return NULL
;
403 if (stringformat
&& !(retval
=PyString_FromString(stringformat
)))
409 v
=PyString_Format(retval
, args
);
412 if (! v
) return NULL
;
417 if (args
) retval
=args
;
419 PyErr_SetObject(ErrType
,Py_None
);
422 PyErr_SetObject(ErrType
,retval
);
428 write_file(Picklerobject
*self
, const char *s
, Py_ssize_t n
)
430 size_t nbyteswritten
;
437 /* String too large */
441 PyFile_IncUseCount((PyFileObject
*)self
->file
);
442 Py_BEGIN_ALLOW_THREADS
443 nbyteswritten
= fwrite(s
, sizeof(char), n
, self
->fp
);
445 PyFile_DecUseCount((PyFileObject
*)self
->file
);
446 if (nbyteswritten
!= (size_t)n
) {
447 PyErr_SetFromErrno(PyExc_IOError
);
455 write_cStringIO(Picklerobject
*self
, const char *s
, Py_ssize_t n
)
461 if (PycStringIO
->cwrite((PyObject
*)self
->file
, s
, n
) != n
) {
469 write_none(Picklerobject
*self
, const char *s
, Py_ssize_t n
)
471 if (s
== NULL
) return 0;
472 if (n
> INT_MAX
) return -1;
477 write_other(Picklerobject
*self
, const char *s
, Py_ssize_t _n
)
479 PyObject
*py_str
= 0, *junk
= 0;
486 if (!( self
->buf_size
)) return 0;
487 py_str
= PyString_FromStringAndSize(self
->write_buf
,
493 if (self
->buf_size
&& (n
+ self
->buf_size
) > WRITE_BUF_SIZE
) {
494 if (write_other(self
, NULL
, 0) < 0)
498 if (n
> WRITE_BUF_SIZE
) {
500 PyString_FromStringAndSize(s
, n
)))
504 memcpy(self
->write_buf
+ self
->buf_size
, s
, n
);
511 /* object with write method */
512 ARG_TUP(self
, py_str
);
514 junk
= PyObject_Call(self
->write
, self
->arg
, NULL
);
517 if (junk
) Py_DECREF(junk
);
521 PDATA_PUSH(self
->file
, py_str
, -1);
529 read_file(Unpicklerobject
*self
, char **s
, Py_ssize_t n
)
533 if (self
->buf_size
== 0) {
536 size
= ((n
< 32) ? 32 : n
);
537 if (!( self
->buf
= (char *)malloc(size
))) {
542 self
->buf_size
= size
;
544 else if (n
> self
->buf_size
) {
545 char *newbuf
= (char *)realloc(self
->buf
, n
);
554 PyFile_IncUseCount((PyFileObject
*)self
->file
);
555 Py_BEGIN_ALLOW_THREADS
556 nbytesread
= fread(self
->buf
, sizeof(char), n
, self
->fp
);
558 PyFile_DecUseCount((PyFileObject
*)self
->file
);
559 if (nbytesread
!= (size_t)n
) {
560 if (feof(self
->fp
)) {
561 PyErr_SetNone(PyExc_EOFError
);
565 PyErr_SetFromErrno(PyExc_IOError
);
576 readline_file(Unpicklerobject
*self
, char **s
)
580 if (self
->buf_size
== 0) {
581 if (!( self
->buf
= (char *)malloc(40))) {
592 for (; i
< (self
->buf_size
- 1); i
++) {
593 if (feof(self
->fp
) ||
594 (self
->buf
[i
] = getc(self
->fp
)) == '\n') {
595 self
->buf
[i
+ 1] = '\0';
600 bigger
= self
->buf_size
<< 1;
601 if (bigger
<= 0) { /* overflow */
605 newbuf
= (char *)realloc(self
->buf
, bigger
);
611 self
->buf_size
= bigger
;
617 read_cStringIO(Unpicklerobject
*self
, char **s
, Py_ssize_t n
)
621 if (PycStringIO
->cread((PyObject
*)self
->file
, &ptr
, n
) != n
) {
622 PyErr_SetNone(PyExc_EOFError
);
633 readline_cStringIO(Unpicklerobject
*self
, char **s
)
638 if ((n
= PycStringIO
->creadline((PyObject
*)self
->file
, &ptr
)) < 0) {
649 read_other(Unpicklerobject
*self
, char **s
, Py_ssize_t n
)
651 PyObject
*bytes
, *str
=0;
653 if (!( bytes
= PyInt_FromSsize_t(n
))) return -1;
655 ARG_TUP(self
, bytes
);
657 str
= PyObject_Call(self
->read
, self
->arg
, NULL
);
660 if (! str
) return -1;
662 Py_XDECREF(self
->last_string
);
663 self
->last_string
= str
;
665 if (! (*s
= PyString_AsString(str
))) return -1;
667 if (PyString_GET_SIZE(str
) != n
) {
668 PyErr_SetNone(PyExc_EOFError
);
677 readline_other(Unpicklerobject
*self
, char **s
)
682 if (!( str
= PyObject_CallObject(self
->readline
, empty_tuple
))) {
686 if ((str_size
= PyString_Size(str
)) < 0)
689 Py_XDECREF(self
->last_string
);
690 self
->last_string
= str
;
692 if (! (*s
= PyString_AsString(str
)))
698 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
699 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
700 * The caller is responsible for free()'ing the return value.
703 pystrndup(const char *s
, int n
)
705 char *r
= (char *)malloc(n
+1);
707 return (char*)PyErr_NoMemory();
715 get(Picklerobject
*self
, PyObject
*id
)
717 PyObject
*value
, *mv
;
722 if (!( mv
= PyDict_GetItem(self
->memo
, id
))) {
723 PyErr_SetObject(PyExc_KeyError
, id
);
727 if (!( value
= PyTuple_GetItem(mv
, 0)))
730 if (!( PyInt_Check(value
))) {
731 PyErr_SetString(PicklingError
, "no int where int expected in memo");
734 c_value
= PyInt_AS_LONG((PyIntObject
*)value
);
738 PyOS_snprintf(s
+ 1, sizeof(s
) - 1, "%ld\n", c_value
);
741 else if (Pdata_Check(self
->file
)) {
742 if (write_other(self
, NULL
, 0) < 0) return -1;
743 PDATA_APPEND(self
->file
, mv
, -1);
749 s
[1] = (int)(c_value
& 0xff);
754 s
[1] = (int)(c_value
& 0xff);
755 s
[2] = (int)((c_value
>> 8) & 0xff);
756 s
[3] = (int)((c_value
>> 16) & 0xff);
757 s
[4] = (int)((c_value
>> 24) & 0xff);
762 if (self
->write_func(self
, s
, len
) < 0)
770 put(Picklerobject
*self
, PyObject
*ob
)
772 if (Py_REFCNT(ob
) < 2 || self
->fast
)
775 return put2(self
, ob
);
780 put2(Picklerobject
*self
, PyObject
*ob
)
786 PyObject
*py_ob_id
= 0, *memo_len
= 0, *t
= 0;
791 if ((p
= PyDict_Size(self
->memo
)) < 0)
794 /* Make sure memo keys are positive! */
796 * XXX And does "positive" really mean non-negative?
797 * XXX pickle.py starts with PUT index 0, not 1. This makes for
798 * XXX gratuitous differences between the pickling modules.
802 if (!( py_ob_id
= PyLong_FromVoidPtr(ob
)))
805 if (!( memo_len
= PyInt_FromLong(p
)))
808 if (!( t
= PyTuple_New(2)))
811 PyTuple_SET_ITEM(t
, 0, memo_len
);
813 PyTuple_SET_ITEM(t
, 1, ob
);
816 if (PyDict_SetItem(self
->memo
, py_ob_id
, t
) < 0)
821 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%d\n", p
);
824 else if (Pdata_Check(self
->file
)) {
825 if (write_other(self
, NULL
, 0) < 0) return -1;
826 PDATA_APPEND(self
->file
, memo_len
, -1);
827 res
=0; /* Job well done ;) */
832 c_str
[0] = LONG_BINPUT
;
833 c_str
[1] = (int)(p
& 0xff);
834 c_str
[2] = (int)((p
>> 8) & 0xff);
835 c_str
[3] = (int)((p
>> 16) & 0xff);
836 c_str
[4] = (int)((p
>> 24) & 0xff);
846 if (self
->write_func(self
, c_str
, len
) < 0)
852 Py_XDECREF(py_ob_id
);
853 Py_XDECREF(memo_len
);
860 whichmodule(PyObject
*global
, PyObject
*global_name
)
863 PyObject
*module
= 0, *modules_dict
= 0,
864 *global_name_attr
= 0, *name
= 0;
866 module
= PyObject_GetAttrString(global
, "__module__");
869 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
874 if (!( modules_dict
= PySys_GetObject("modules")))
878 while ((j
= PyDict_Next(modules_dict
, &i
, &name
, &module
))) {
880 if (PyObject_Compare(name
, __main___str
)==0) continue;
882 global_name_attr
= PyObject_GetAttr(module
, global_name
);
883 if (!global_name_attr
) {
884 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
891 if (global_name_attr
!= global
) {
892 Py_DECREF(global_name_attr
);
896 Py_DECREF(global_name_attr
);
901 /* The following implements the rule in pickle.py added in 1.5
902 that used __main__ if no module is found. I don't actually
916 fast_save_enter(Picklerobject
*self
, PyObject
*obj
)
918 /* if fast_container < 0, we're doing an error exit. */
919 if (++self
->fast_container
>= PY_CPICKLE_FAST_LIMIT
) {
920 PyObject
*key
= NULL
;
921 if (self
->fast_memo
== NULL
) {
922 self
->fast_memo
= PyDict_New();
923 if (self
->fast_memo
== NULL
) {
924 self
->fast_container
= -1;
928 key
= PyLong_FromVoidPtr(obj
);
931 if (PyDict_GetItem(self
->fast_memo
, key
)) {
933 PyErr_Format(PyExc_ValueError
,
934 "fast mode: can't pickle cyclic objects "
935 "including object type %s at %p",
936 Py_TYPE(obj
)->tp_name
, obj
);
937 self
->fast_container
= -1;
940 if (PyDict_SetItem(self
->fast_memo
, key
, Py_None
) < 0) {
942 self
->fast_container
= -1;
951 fast_save_leave(Picklerobject
*self
, PyObject
*obj
)
953 if (self
->fast_container
-- >= PY_CPICKLE_FAST_LIMIT
) {
954 PyObject
*key
= PyLong_FromVoidPtr(obj
);
957 if (PyDict_DelItem(self
->fast_memo
, key
) < 0) {
967 save_none(Picklerobject
*self
, PyObject
*args
)
969 static char none
= NONE
;
970 if (self
->write_func(self
, &none
, 1) < 0)
977 save_bool(Picklerobject
*self
, PyObject
*args
)
979 static const char *buf
[2] = {FALSE
, TRUE
};
980 static char len
[2] = {sizeof(FALSE
)-1, sizeof(TRUE
)-1};
981 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
983 if (self
->proto
>= 2) {
984 char opcode
= l
? NEWTRUE
: NEWFALSE
;
985 if (self
->write_func(self
, &opcode
, 1) < 0)
988 else if (self
->write_func(self
, buf
[l
], len
[l
]) < 0)
994 save_int(Picklerobject
*self
, PyObject
*args
)
997 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
1006 /* Text-mode pickle, or long too big to fit in the 4-byte
1007 * signed BININT format: store as a string.
1010 PyOS_snprintf(c_str
+ 1, sizeof(c_str
) - 1, "%ld\n", l
);
1011 if (self
->write_func(self
, c_str
, strlen(c_str
)) < 0)
1015 /* Binary pickle and l fits in a signed 4-byte int. */
1016 c_str
[1] = (int)( l
& 0xff);
1017 c_str
[2] = (int)((l
>> 8) & 0xff);
1018 c_str
[3] = (int)((l
>> 16) & 0xff);
1019 c_str
[4] = (int)((l
>> 24) & 0xff);
1021 if ((c_str
[4] == 0) && (c_str
[3] == 0)) {
1022 if (c_str
[2] == 0) {
1036 if (self
->write_func(self
, c_str
, len
) < 0)
1045 save_long(Picklerobject
*self
, PyObject
*args
)
1049 PyObject
*repr
= NULL
;
1051 static char l
= LONG
;
1053 if (self
->proto
>= 2) {
1054 /* Linear-time pickling. */
1057 unsigned char *pdata
;
1060 int sign
= _PyLong_Sign(args
);
1063 /* It's 0 -- an empty bytestring. */
1066 i
= self
->write_func(self
, c_str
, 2);
1067 if (i
< 0) goto finally
;
1071 nbits
= _PyLong_NumBits(args
);
1072 if (nbits
== (size_t)-1 && PyErr_Occurred())
1074 /* How many bytes do we need? There are nbits >> 3 full
1075 * bytes of data, and nbits & 7 leftover bits. If there
1076 * are any leftover bits, then we clearly need another
1077 * byte. Wnat's not so obvious is that we *probably*
1078 * need another byte even if there aren't any leftovers:
1079 * the most-significant bit of the most-significant byte
1080 * acts like a sign bit, and it's usually got a sense
1081 * opposite of the one we need. The exception is longs
1082 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1083 * its own 256's-complement, so has the right sign bit
1084 * even without the extra byte. That's a pain to check
1085 * for in advance, though, so we always grab an extra
1086 * byte at the start, and cut it back later if possible.
1088 nbytes
= (nbits
>> 3) + 1;
1089 if (nbytes
> INT_MAX
) {
1090 PyErr_SetString(PyExc_OverflowError
, "long too large "
1094 repr
= PyString_FromStringAndSize(NULL
, (int)nbytes
);
1095 if (repr
== NULL
) goto finally
;
1096 pdata
= (unsigned char *)PyString_AS_STRING(repr
);
1097 i
= _PyLong_AsByteArray((PyLongObject
*)args
,
1099 1 /* little endian */, 1 /* signed */);
1100 if (i
< 0) goto finally
;
1101 /* If the long is negative, this may be a byte more than
1102 * needed. This is so iff the MSB is all redundant sign
1105 if (sign
< 0 && nbytes
> 1 && pdata
[nbytes
- 1] == 0xff &&
1106 (pdata
[nbytes
- 2] & 0x80) != 0)
1111 c_str
[1] = (char)nbytes
;
1117 for (i
= 1; i
< 5; i
++) {
1118 c_str
[i
] = (char)(size
& 0xff);
1123 i
= self
->write_func(self
, c_str
, size
);
1124 if (i
< 0) goto finally
;
1125 i
= self
->write_func(self
, (char *)pdata
, (int)nbytes
);
1126 if (i
< 0) goto finally
;
1131 /* proto < 2: write the repr and newline. This is quadratic-time
1132 * (in the number of digits), in both directions.
1134 if (!( repr
= PyObject_Repr(args
)))
1137 if ((size
= PyString_Size(repr
)) < 0)
1140 if (self
->write_func(self
, &l
, 1) < 0)
1143 if (self
->write_func(self
,
1144 PyString_AS_STRING((PyStringObject
*)repr
),
1148 if (self
->write_func(self
, "\n", 1) < 0)
1160 save_float(Picklerobject
*self
, PyObject
*args
)
1162 double x
= PyFloat_AS_DOUBLE((PyFloatObject
*)args
);
1167 if (_PyFloat_Pack8(x
, (unsigned char *)&str
[1], 0) < 0)
1169 if (self
->write_func(self
, str
, 9) < 0)
1175 _PyOS_double_to_string(c_str
+ 1, sizeof(c_str
) - 2, x
, 'g',
1177 /* Extend the formatted string with a newline character */
1178 strcat(c_str
, "\n");
1180 if (self
->write_func(self
, c_str
, strlen(c_str
)) < 0)
1189 save_string(Picklerobject
*self
, PyObject
*args
, int doput
)
1194 if ((size
= PyString_Size(args
)) < 0)
1200 static char string
= STRING
;
1202 if (!( repr
= PyObject_Repr(args
)))
1205 if ((len
= PyString_Size(repr
)) < 0)
1207 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1209 if (self
->write_func(self
, &string
, 1) < 0)
1212 if (self
->write_func(self
, repr_str
, len
) < 0)
1215 if (self
->write_func(self
, "\n", 1) < 0)
1224 if ((size
= PyString_Size(args
)) < 0)
1228 c_str
[0] = SHORT_BINSTRING
;
1232 else if (size
<= INT_MAX
) {
1233 c_str
[0] = BINSTRING
;
1234 for (i
= 1; i
< 5; i
++)
1235 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1239 return -1; /* string too large */
1241 if (self
->write_func(self
, c_str
, len
) < 0)
1244 if (size
> 128 && Pdata_Check(self
->file
)) {
1245 if (write_other(self
, NULL
, 0) < 0) return -1;
1246 PDATA_APPEND(self
->file
, args
, -1);
1249 if (self
->write_func(self
,
1251 (PyStringObject
*)args
),
1258 if (put(self
, args
) < 0)
1269 #ifdef Py_USING_UNICODE
1270 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1271 backslash and newline characters to \uXXXX escapes. */
1273 modified_EncodeRawUnicodeEscape(const Py_UNICODE
*s
, Py_ssize_t size
)
1279 static const char *hexdigit
= "0123456789abcdef";
1280 #ifdef Py_UNICODE_WIDE
1281 const Py_ssize_t expandsize
= 10;
1283 const Py_ssize_t expandsize
= 6;
1286 if (size
> PY_SSIZE_T_MAX
/ expandsize
)
1287 return PyErr_NoMemory();
1289 repr
= PyString_FromStringAndSize(NULL
, expandsize
* size
);
1295 p
= q
= PyString_AS_STRING(repr
);
1296 while (size
-- > 0) {
1297 Py_UNICODE ch
= *s
++;
1298 #ifdef Py_UNICODE_WIDE
1299 /* Map 32-bit characters to '\Uxxxxxxxx' */
1300 if (ch
>= 0x10000) {
1303 *p
++ = hexdigit
[(ch
>> 28) & 0xf];
1304 *p
++ = hexdigit
[(ch
>> 24) & 0xf];
1305 *p
++ = hexdigit
[(ch
>> 20) & 0xf];
1306 *p
++ = hexdigit
[(ch
>> 16) & 0xf];
1307 *p
++ = hexdigit
[(ch
>> 12) & 0xf];
1308 *p
++ = hexdigit
[(ch
>> 8) & 0xf];
1309 *p
++ = hexdigit
[(ch
>> 4) & 0xf];
1310 *p
++ = hexdigit
[ch
& 15];
1314 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1315 if (ch
>= 0xD800 && ch
< 0xDC00) {
1321 if (ch2
>= 0xDC00 && ch2
<= 0xDFFF) {
1322 ucs
= (((ch
& 0x03FF) << 10) | (ch2
& 0x03FF)) + 0x00010000;
1325 *p
++ = hexdigit
[(ucs
>> 28) & 0xf];
1326 *p
++ = hexdigit
[(ucs
>> 24) & 0xf];
1327 *p
++ = hexdigit
[(ucs
>> 20) & 0xf];
1328 *p
++ = hexdigit
[(ucs
>> 16) & 0xf];
1329 *p
++ = hexdigit
[(ucs
>> 12) & 0xf];
1330 *p
++ = hexdigit
[(ucs
>> 8) & 0xf];
1331 *p
++ = hexdigit
[(ucs
>> 4) & 0xf];
1332 *p
++ = hexdigit
[ucs
& 0xf];
1335 /* Fall through: isolated surrogates are copied as-is */
1340 /* Map 16-bit characters to '\uxxxx' */
1341 if (ch
>= 256 || ch
== '\\' || ch
== '\n') {
1344 *p
++ = hexdigit
[(ch
>> 12) & 0xf];
1345 *p
++ = hexdigit
[(ch
>> 8) & 0xf];
1346 *p
++ = hexdigit
[(ch
>> 4) & 0xf];
1347 *p
++ = hexdigit
[ch
& 15];
1349 /* Copy everything else as-is */
1354 _PyString_Resize(&repr
, p
- q
);
1359 save_unicode(Picklerobject
*self
, PyObject
*args
, int doput
)
1361 Py_ssize_t size
, len
;
1364 if (!PyUnicode_Check(args
))
1369 static char string
= UNICODE
;
1371 repr
= modified_EncodeRawUnicodeEscape(
1372 PyUnicode_AS_UNICODE(args
), PyUnicode_GET_SIZE(args
));
1376 if ((len
= PyString_Size(repr
)) < 0)
1378 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1380 if (self
->write_func(self
, &string
, 1) < 0)
1383 if (self
->write_func(self
, repr_str
, len
) < 0)
1386 if (self
->write_func(self
, "\n", 1) < 0)
1395 if (!( repr
= PyUnicode_AsUTF8String(args
)))
1398 if ((size
= PyString_Size(repr
)) < 0)
1401 return -1; /* string too large */
1403 c_str
[0] = BINUNICODE
;
1404 for (i
= 1; i
< 5; i
++)
1405 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1408 if (self
->write_func(self
, c_str
, len
) < 0)
1411 if (size
> 128 && Pdata_Check(self
->file
)) {
1412 if (write_other(self
, NULL
, 0) < 0)
1414 PDATA_APPEND(self
->file
, repr
, -1);
1417 if (self
->write_func(self
, PyString_AS_STRING(repr
),
1426 if (put(self
, args
) < 0)
1437 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1439 store_tuple_elements(Picklerobject
*self
, PyObject
*t
, int len
)
1442 int res
= -1; /* guilty until proved innocent */
1444 assert(PyTuple_Size(t
) == len
);
1446 for (i
= 0; i
< len
; i
++) {
1447 PyObject
*element
= PyTuple_GET_ITEM(t
, i
);
1449 if (element
== NULL
)
1451 if (save(self
, element
, 0) < 0)
1460 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1461 * used across protocols to minimize the space needed to pickle them.
1462 * Tuples are also the only builtin immutable type that can be recursive
1463 * (a tuple can be reached from itself), and that requires some subtle
1464 * magic so that it works in all cases. IOW, this is a long routine.
1467 save_tuple(Picklerobject
*self
, PyObject
*args
)
1469 PyObject
*py_tuple_id
= NULL
;
1473 static char tuple
= TUPLE
;
1474 static char pop
= POP
;
1475 static char pop_mark
= POP_MARK
;
1476 static char len2opcode
[] = {EMPTY_TUPLE
, TUPLE1
, TUPLE2
, TUPLE3
};
1478 if ((len
= PyTuple_Size(args
)) < 0)
1485 c_str
[0] = EMPTY_TUPLE
;
1493 if (self
->write_func(self
, c_str
, len
) >= 0)
1495 /* Don't memoize an empty tuple. */
1499 /* A non-empty tuple. */
1501 /* id(tuple) isn't in the memo now. If it shows up there after
1502 * saving the tuple elements, the tuple must be recursive, in
1503 * which case we'll pop everything we put on the stack, and fetch
1504 * its value from the memo.
1506 py_tuple_id
= PyLong_FromVoidPtr(args
);
1507 if (py_tuple_id
== NULL
)
1510 if (len
<= 3 && self
->proto
>= 2) {
1511 /* Use TUPLE{1,2,3} opcodes. */
1512 if (store_tuple_elements(self
, args
, len
) < 0)
1514 if (PyDict_GetItem(self
->memo
, py_tuple_id
)) {
1515 /* pop the len elements */
1516 for (i
= 0; i
< len
; ++i
)
1517 if (self
->write_func(self
, &pop
, 1) < 0)
1519 /* fetch from memo */
1520 if (get(self
, py_tuple_id
) < 0)
1525 /* Not recursive. */
1526 if (self
->write_func(self
, len2opcode
+ len
, 1) < 0)
1531 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1532 * Generate MARK elt1 elt2 ... TUPLE
1534 if (self
->write_func(self
, &MARKv
, 1) < 0)
1537 if (store_tuple_elements(self
, args
, len
) < 0)
1540 if (PyDict_GetItem(self
->memo
, py_tuple_id
)) {
1541 /* pop the stack stuff we pushed */
1543 if (self
->write_func(self
, &pop_mark
, 1) < 0)
1547 /* Note that we pop one more than len, to remove
1550 for (i
= 0; i
<= len
; i
++)
1551 if (self
->write_func(self
, &pop
, 1) < 0)
1554 /* fetch from memo */
1555 if (get(self
, py_tuple_id
) >= 0)
1560 /* Not recursive. */
1561 if (self
->write_func(self
, &tuple
, 1) < 0)
1565 if (put(self
, args
) >= 0)
1569 Py_XDECREF(py_tuple_id
);
1573 /* iter is an iterator giving items, and we batch up chunks of
1574 * MARK item item ... item APPENDS
1575 * opcode sequences. Calling code should have arranged to first create an
1576 * empty list, or list-like object, for the APPENDS to operate on.
1577 * Returns 0 on success, <0 on error.
1580 batch_list(Picklerobject
*self
, PyObject
*iter
)
1582 PyObject
*obj
= NULL
;
1583 PyObject
*firstitem
= NULL
;
1586 static char append
= APPEND
;
1587 static char appends
= APPENDS
;
1589 assert(iter
!= NULL
);
1591 if (self
->proto
== 0) {
1592 /* APPENDS isn't available; do one at a time. */
1594 obj
= PyIter_Next(iter
);
1596 if (PyErr_Occurred())
1600 i
= save(self
, obj
, 0);
1604 if (self
->write_func(self
, &append
, 1) < 0)
1610 /* proto > 0: write in batches of BATCHSIZE. */
1612 /* Get first item */
1613 firstitem
= PyIter_Next(iter
);
1614 if (firstitem
== NULL
) {
1615 if (PyErr_Occurred())
1618 /* nothing more to add */
1622 /* Try to get a second item */
1623 obj
= PyIter_Next(iter
);
1625 if (PyErr_Occurred())
1628 /* Only one item to write */
1629 if (save(self
, firstitem
, 0) < 0)
1631 if (self
->write_func(self
, &append
, 1) < 0)
1633 Py_CLEAR(firstitem
);
1637 /* More than one item to write */
1639 /* Pump out MARK, items, APPENDS. */
1640 if (self
->write_func(self
, &MARKv
, 1) < 0)
1643 if (save(self
, firstitem
, 0) < 0)
1645 Py_CLEAR(firstitem
);
1648 /* Fetch and save up to BATCHSIZE items */
1650 if (save(self
, obj
, 0) < 0)
1658 obj
= PyIter_Next(iter
);
1660 if (PyErr_Occurred())
1666 if (self
->write_func(self
, &appends
, 1) < 0)
1669 } while (n
== BATCHSIZE
);
1673 Py_XDECREF(firstitem
);
1679 save_list(Picklerobject
*self
, PyObject
*args
)
1686 if (self
->fast
&& !fast_save_enter(self
, args
))
1689 /* Create an empty list. */
1700 if (self
->write_func(self
, s
, len
) < 0)
1703 /* Get list length, and bow out early if empty. */
1704 if ((len
= PyList_Size(args
)) < 0)
1709 if (put(self
, args
) >= 0)
1713 if (put2(self
, args
) < 0)
1716 /* Materialize the list elements. */
1717 iter
= PyObject_GetIter(args
);
1721 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1723 res
= batch_list(self
, iter
);
1724 Py_LeaveRecursiveCall();
1729 if (self
->fast
&& !fast_save_leave(self
, args
))
1736 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1737 * MARK key value ... key value SETITEMS
1738 * opcode sequences. Calling code should have arranged to first create an
1739 * empty dict, or dict-like object, for the SETITEMS to operate on.
1740 * Returns 0 on success, <0 on error.
1742 * This is very much like batch_list(). The difference between saving
1743 * elements directly, and picking apart two-tuples, is so long-winded at
1744 * the C level, though, that attempts to combine these routines were too
1748 batch_dict(Picklerobject
*self
, PyObject
*iter
)
1751 PyObject
*firstitem
= NULL
;
1754 static char setitem
= SETITEM
;
1755 static char setitems
= SETITEMS
;
1757 assert(iter
!= NULL
);
1759 if (self
->proto
== 0) {
1760 /* SETITEMS isn't available; do one at a time. */
1762 p
= PyIter_Next(iter
);
1764 if (PyErr_Occurred())
1768 if (!PyTuple_Check(p
) || PyTuple_Size(p
) != 2) {
1769 PyErr_SetString(PyExc_TypeError
, "dict items "
1770 "iterator must return 2-tuples");
1773 i
= save(self
, PyTuple_GET_ITEM(p
, 0), 0);
1775 i
= save(self
, PyTuple_GET_ITEM(p
, 1), 0);
1779 if (self
->write_func(self
, &setitem
, 1) < 0)
1785 /* proto > 0: write in batches of BATCHSIZE. */
1787 /* Get first item */
1788 firstitem
= PyIter_Next(iter
);
1789 if (firstitem
== NULL
) {
1790 if (PyErr_Occurred())
1793 /* nothing more to add */
1796 if (!PyTuple_Check(firstitem
) || PyTuple_Size(firstitem
) != 2) {
1797 PyErr_SetString(PyExc_TypeError
, "dict items "
1798 "iterator must return 2-tuples");
1802 /* Try to get a second item */
1803 p
= PyIter_Next(iter
);
1805 if (PyErr_Occurred())
1808 /* Only one item to write */
1809 if (save(self
, PyTuple_GET_ITEM(firstitem
, 0), 0) < 0)
1811 if (save(self
, PyTuple_GET_ITEM(firstitem
, 1), 0) < 0)
1813 if (self
->write_func(self
, &setitem
, 1) < 0)
1815 Py_CLEAR(firstitem
);
1819 /* More than one item to write */
1821 /* Pump out MARK, items, SETITEMS. */
1822 if (self
->write_func(self
, &MARKv
, 1) < 0)
1825 if (save(self
, PyTuple_GET_ITEM(firstitem
, 0), 0) < 0)
1827 if (save(self
, PyTuple_GET_ITEM(firstitem
, 1), 0) < 0)
1829 Py_CLEAR(firstitem
);
1832 /* Fetch and save up to BATCHSIZE items */
1834 if (!PyTuple_Check(p
) || PyTuple_Size(p
) != 2) {
1835 PyErr_SetString(PyExc_TypeError
, "dict items "
1836 "iterator must return 2-tuples");
1839 if (save(self
, PyTuple_GET_ITEM(p
, 0), 0) < 0)
1841 if (save(self
, PyTuple_GET_ITEM(p
, 1), 0) < 0)
1849 p
= PyIter_Next(iter
);
1851 if (PyErr_Occurred())
1857 if (self
->write_func(self
, &setitems
, 1) < 0)
1860 } while (n
== BATCHSIZE
);
1864 Py_XDECREF(firstitem
);
1869 /* This is a variant of batch_dict() above that specializes for dicts, with no
1870 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1871 * MARK key value ... key value SETITEMS
1872 * opcode sequences. Calling code should have arranged to first create an
1873 * empty dict, or dict-like object, for the SETITEMS to operate on.
1874 * Returns 0 on success, -1 on error.
1876 * Note that this currently doesn't work for protocol 0.
1879 batch_dict_exact(Picklerobject
*self
, PyObject
*obj
)
1881 PyObject
*key
= NULL
, *value
= NULL
;
1883 Py_ssize_t dict_size
, ppos
= 0;
1885 static char setitem
= SETITEM
;
1886 static char setitems
= SETITEMS
;
1888 assert(obj
!= NULL
);
1889 assert(self
->proto
> 0);
1891 dict_size
= PyDict_Size(obj
);
1893 /* Special-case len(d) == 1 to save space. */
1894 if (dict_size
== 1) {
1895 PyDict_Next(obj
, &ppos
, &key
, &value
);
1896 if (save(self
, key
, 0) < 0)
1898 if (save(self
, value
, 0) < 0)
1900 if (self
->write_func(self
, &setitem
, 1) < 0)
1905 /* Write in batches of BATCHSIZE. */
1908 if (self
->write_func(self
, &MARKv
, 1) < 0)
1910 while (PyDict_Next(obj
, &ppos
, &key
, &value
)) {
1911 if (save(self
, key
, 0) < 0)
1913 if (save(self
, value
, 0) < 0)
1915 if (++i
== BATCHSIZE
)
1918 if (self
->write_func(self
, &setitems
, 1) < 0)
1920 if (PyDict_Size(obj
) != dict_size
) {
1923 "dictionary changed size during iteration");
1927 } while (i
== BATCHSIZE
);
1932 save_dict(Picklerobject
*self
, PyObject
*args
)
1938 if (self
->fast
&& !fast_save_enter(self
, args
))
1941 /* Create an empty dict. */
1952 if (self
->write_func(self
, s
, len
) < 0)
1955 /* Get dict size, and bow out early if empty. */
1956 if ((len
= PyDict_Size(args
)) < 0)
1960 if (put(self
, args
) >= 0)
1964 if (put2(self
, args
) < 0)
1967 /* Materialize the dict items. */
1968 if (PyDict_CheckExact(args
) && self
->proto
> 0) {
1969 /* We can take certain shortcuts if we know this is a dict and
1970 not a dict subclass. */
1971 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1972 res
= batch_dict_exact(self
, args
);
1973 Py_LeaveRecursiveCall();
1976 PyObject
*iter
= PyObject_CallMethod(args
, "iteritems", "()");
1979 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1980 res
= batch_dict(self
, iter
);
1981 Py_LeaveRecursiveCall();
1987 if (self
->fast
&& !fast_save_leave(self
, args
))
1995 save_inst(Picklerobject
*self
, PyObject
*args
)
1997 PyObject
*class = 0, *module
= 0, *name
= 0, *state
= 0,
1998 *getinitargs_func
= 0, *getstate_func
= 0, *class_args
= 0;
1999 char *module_str
, *name_str
;
2000 int module_size
, name_size
, res
= -1;
2002 static char inst
= INST
, obj
= OBJ
, build
= BUILD
;
2004 if (self
->fast
&& !fast_save_enter(self
, args
))
2007 if (self
->write_func(self
, &MARKv
, 1) < 0)
2010 if (!( class = PyObject_GetAttr(args
, __class___str
)))
2014 if (save(self
, class, 0) < 0)
2018 if ((getinitargs_func
= PyObject_GetAttr(args
, __getinitargs___str
))) {
2019 PyObject
*element
= 0;
2023 PyObject_Call(getinitargs_func
, empty_tuple
, NULL
)))
2026 if ((len
= PyObject_Size(class_args
)) < 0)
2029 for (i
= 0; i
< len
; i
++) {
2030 if (!( element
= PySequence_GetItem(class_args
, i
)))
2033 if (save(self
, element
, 0) < 0) {
2042 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2049 if (!( name
= ((PyClassObject
*)class)->cl_name
)) {
2050 PyErr_SetString(PicklingError
, "class has no name");
2054 if (!( module
= whichmodule(class, name
)))
2058 if ((module_size
= PyString_Size(module
)) < 0 ||
2059 (name_size
= PyString_Size(name
)) < 0)
2062 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
2063 name_str
= PyString_AS_STRING((PyStringObject
*)name
);
2065 if (self
->write_func(self
, &inst
, 1) < 0)
2068 if (self
->write_func(self
, module_str
, module_size
) < 0)
2071 if (self
->write_func(self
, "\n", 1) < 0)
2074 if (self
->write_func(self
, name_str
, name_size
) < 0)
2077 if (self
->write_func(self
, "\n", 1) < 0)
2080 else if (self
->write_func(self
, &obj
, 1) < 0) {
2084 if ((getstate_func
= PyObject_GetAttr(args
, __getstate___str
))) {
2085 state
= PyObject_Call(getstate_func
, empty_tuple
, NULL
);
2090 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2095 if (!( state
= PyObject_GetAttr(args
, __dict___str
))) {
2096 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2105 if (!PyDict_Check(state
)) {
2106 if (put2(self
, args
) < 0)
2110 if (put(self
, args
) < 0)
2114 if (save(self
, state
, 0) < 0)
2117 if (self
->write_func(self
, &build
, 1) < 0)
2123 if (self
->fast
&& !fast_save_leave(self
, args
))
2129 Py_XDECREF(getinitargs_func
);
2130 Py_XDECREF(getstate_func
);
2131 Py_XDECREF(class_args
);
2138 save_global(Picklerobject
*self
, PyObject
*args
, PyObject
*name
)
2140 PyObject
*global_name
= 0, *module
= 0, *mod
= 0, *klass
= 0;
2141 char *name_str
, *module_str
;
2142 int module_size
, name_size
, res
= -1;
2144 static char global
= GLOBAL
;
2148 Py_INCREF(global_name
);
2151 if (!( global_name
= PyObject_GetAttr(args
, __name___str
)))
2155 if (!( module
= whichmodule(args
, global_name
)))
2158 if ((module_size
= PyString_Size(module
)) < 0 ||
2159 (name_size
= PyString_Size(global_name
)) < 0)
2162 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
2163 name_str
= PyString_AS_STRING((PyStringObject
*)global_name
);
2165 /* XXX This can be doing a relative import. Clearly it shouldn't,
2166 but I don't know how to stop it. :-( */
2167 mod
= PyImport_ImportModule(module_str
);
2169 cPickle_ErrFormat(PicklingError
,
2170 "Can't pickle %s: import of module %s "
2172 "OS", args
, module
);
2175 klass
= PyObject_GetAttrString(mod
, name_str
);
2176 if (klass
== NULL
) {
2177 cPickle_ErrFormat(PicklingError
,
2178 "Can't pickle %s: attribute lookup %s.%s "
2180 "OSS", args
, module
, global_name
);
2183 if (klass
!= args
) {
2185 cPickle_ErrFormat(PicklingError
,
2186 "Can't pickle %s: it's not the same object "
2188 "OSS", args
, module
, global_name
);
2193 if (self
->proto
>= 2) {
2194 /* See whether this is in the extension registry, and if
2195 * so generate an EXT opcode.
2197 PyObject
*py_code
; /* extension code as Python object */
2198 long code
; /* extension code as C value */
2202 PyTuple_SET_ITEM(two_tuple
, 0, module
);
2203 PyTuple_SET_ITEM(two_tuple
, 1, global_name
);
2204 py_code
= PyDict_GetItem(extension_registry
, two_tuple
);
2205 if (py_code
== NULL
)
2206 goto gen_global
; /* not registered */
2208 /* Verify py_code has the right type and value. */
2209 if (!PyInt_Check(py_code
)) {
2210 cPickle_ErrFormat(PicklingError
, "Can't pickle %s: "
2211 "extension code %s isn't an integer",
2212 "OO", args
, py_code
);
2215 code
= PyInt_AS_LONG(py_code
);
2216 if (code
<= 0 || code
> 0x7fffffffL
) {
2217 cPickle_ErrFormat(PicklingError
, "Can't pickle %s: "
2218 "extension code %ld is out of range",
2223 /* Generate an EXT opcode. */
2226 c_str
[1] = (char)code
;
2229 else if (code
<= 0xffff) {
2231 c_str
[1] = (char)(code
& 0xff);
2232 c_str
[2] = (char)((code
>> 8) & 0xff);
2237 c_str
[1] = (char)(code
& 0xff);
2238 c_str
[2] = (char)((code
>> 8) & 0xff);
2239 c_str
[3] = (char)((code
>> 16) & 0xff);
2240 c_str
[4] = (char)((code
>> 24) & 0xff);
2244 if (self
->write_func(self
, c_str
, n
) >= 0)
2246 goto finally
; /* and don't memoize */
2250 if (self
->write_func(self
, &global
, 1) < 0)
2253 if (self
->write_func(self
, module_str
, module_size
) < 0)
2256 if (self
->write_func(self
, "\n", 1) < 0)
2259 if (self
->write_func(self
, name_str
, name_size
) < 0)
2262 if (self
->write_func(self
, "\n", 1) < 0)
2265 if (put(self
, args
) < 0)
2272 Py_XDECREF(global_name
);
2279 save_pers(Picklerobject
*self
, PyObject
*args
, PyObject
*f
)
2284 static char persid
= PERSID
, binpersid
= BINPERSID
;
2287 ARG_TUP(self
, args
);
2289 pid
= PyObject_Call(f
, self
->arg
, NULL
);
2292 if (! pid
) return -1;
2294 if (pid
!= Py_None
) {
2296 if (!PyString_Check(pid
)) {
2297 PyErr_SetString(PicklingError
,
2298 "persistent id must be string");
2302 if (self
->write_func(self
, &persid
, 1) < 0)
2305 if ((size
= PyString_Size(pid
)) < 0)
2308 if (self
->write_func(self
,
2310 (PyStringObject
*)pid
),
2314 if (self
->write_func(self
, "\n", 1) < 0)
2320 else if (save(self
, pid
, 1) >= 0) {
2321 if (self
->write_func(self
, &binpersid
, 1) < 0)
2338 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2339 * appropriate __reduce__ method for ob.
2342 save_reduce(Picklerobject
*self
, PyObject
*args
, PyObject
*fn
, PyObject
*ob
)
2346 PyObject
*state
= NULL
;
2347 PyObject
*listitems
= Py_None
;
2348 PyObject
*dictitems
= Py_None
;
2351 int use_newobj
= self
->proto
>= 2;
2353 static char reduce
= REDUCE
;
2354 static char build
= BUILD
;
2355 static char newobj
= NEWOBJ
;
2357 size
= PyTuple_Size(args
);
2358 if (size
< 2 || size
> 5) {
2359 cPickle_ErrFormat(PicklingError
, "tuple returned by "
2360 "%s must contain 2 through 5 elements",
2365 if (! PyArg_UnpackTuple(args
, "save_reduce", 2, 5,
2373 if (!PyTuple_Check(argtup
)) {
2374 cPickle_ErrFormat(PicklingError
, "Second element of "
2375 "tuple returned by %s must be a tuple",
2380 if (state
== Py_None
)
2383 if (listitems
== Py_None
)
2385 else if (!PyIter_Check(listitems
)) {
2386 cPickle_ErrFormat(PicklingError
, "Fourth element of "
2387 "tuple returned by %s must be an iterator, not %s",
2388 "Os", fn
, Py_TYPE(listitems
)->tp_name
);
2392 if (dictitems
== Py_None
)
2394 else if (!PyIter_Check(dictitems
)) {
2395 cPickle_ErrFormat(PicklingError
, "Fifth element of "
2396 "tuple returned by %s must be an iterator, not %s",
2397 "Os", fn
, Py_TYPE(dictitems
)->tp_name
);
2401 /* Protocol 2 special case: if callable's name is __newobj__, use
2402 * NEWOBJ. This consumes a lot of code.
2405 PyObject
*temp
= PyObject_GetAttr(callable
, __name___str
);
2408 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2415 use_newobj
= PyString_Check(temp
) &&
2416 strcmp(PyString_AS_STRING(temp
),
2423 PyObject
*newargtup
;
2426 /* Sanity checks. */
2427 n
= PyTuple_Size(argtup
);
2429 PyErr_SetString(PicklingError
, "__newobj__ arglist "
2434 cls
= PyTuple_GET_ITEM(argtup
, 0);
2435 if (! PyObject_HasAttrString(cls
, "__new__")) {
2436 PyErr_SetString(PicklingError
, "args[0] from "
2437 "__newobj__ args has no __new__");
2441 /* XXX How could ob be NULL? */
2443 PyObject
*ob_dot_class
;
2445 ob_dot_class
= PyObject_GetAttr(ob
, __class___str
);
2446 if (ob_dot_class
== NULL
) {
2447 if (PyErr_ExceptionMatches(
2448 PyExc_AttributeError
))
2453 i
= ob_dot_class
!= cls
; /* true iff a problem */
2454 Py_XDECREF(ob_dot_class
);
2456 PyErr_SetString(PicklingError
, "args[0] from "
2457 "__newobj__ args has the wrong class");
2462 /* Save the class and its __new__ arguments. */
2463 if (save(self
, cls
, 0) < 0)
2466 newargtup
= PyTuple_New(n
-1); /* argtup[1:] */
2467 if (newargtup
== NULL
)
2469 for (i
= 1; i
< n
; ++i
) {
2470 PyObject
*temp
= PyTuple_GET_ITEM(argtup
, i
);
2472 PyTuple_SET_ITEM(newargtup
, i
-1, temp
);
2474 i
= save(self
, newargtup
, 0);
2475 Py_DECREF(newargtup
);
2479 /* Add NEWOBJ opcode. */
2480 if (self
->write_func(self
, &newobj
, 1) < 0)
2484 /* Not using NEWOBJ. */
2485 if (save(self
, callable
, 0) < 0 ||
2486 save(self
, argtup
, 0) < 0 ||
2487 self
->write_func(self
, &reduce
, 1) < 0)
2492 /* XXX How can ob be NULL? */
2494 if (state
&& !PyDict_Check(state
)) {
2495 if (put2(self
, ob
) < 0)
2498 else if (put(self
, ob
) < 0)
2503 if (listitems
&& batch_list(self
, listitems
) < 0)
2506 if (dictitems
&& batch_dict(self
, dictitems
) < 0)
2510 if (save(self
, state
, 0) < 0 ||
2511 self
->write_func(self
, &build
, 1) < 0)
2519 save(Picklerobject
*self
, PyObject
*args
, int pers_save
)
2522 PyObject
*py_ob_id
= 0, *__reduce__
= 0, *t
= 0;
2526 if (Py_EnterRecursiveCall(" while pickling an object"))
2529 if (!pers_save
&& self
->pers_func
) {
2530 if ((tmp
= save_pers(self
, args
, self
->pers_func
)) != 0) {
2536 if (args
== Py_None
) {
2537 res
= save_none(self
, args
);
2541 type
= Py_TYPE(args
);
2543 switch (type
->tp_name
[0]) {
2545 if (args
== Py_False
|| args
== Py_True
) {
2546 res
= save_bool(self
, args
);
2551 if (type
== &PyInt_Type
) {
2552 res
= save_int(self
, args
);
2558 if (type
== &PyLong_Type
) {
2559 res
= save_long(self
, args
);
2565 if (type
== &PyFloat_Type
) {
2566 res
= save_float(self
, args
);
2572 if (type
== &PyTuple_Type
&& PyTuple_Size(args
) == 0) {
2573 res
= save_tuple(self
, args
);
2579 if ((type
== &PyString_Type
) && (PyString_GET_SIZE(args
) < 2)) {
2580 res
= save_string(self
, args
, 0);
2585 #ifdef Py_USING_UNICODE
2587 if ((type
== &PyUnicode_Type
) && (PyString_GET_SIZE(args
) < 2)) {
2588 res
= save_unicode(self
, args
, 0);
2595 if (Py_REFCNT(args
) > 1) {
2596 if (!( py_ob_id
= PyLong_FromVoidPtr(args
)))
2599 if (PyDict_GetItem(self
->memo
, py_ob_id
)) {
2600 if (get(self
, py_ob_id
) < 0)
2608 switch (type
->tp_name
[0]) {
2610 if (type
== &PyString_Type
) {
2611 res
= save_string(self
, args
, 1);
2616 #ifdef Py_USING_UNICODE
2618 if (type
== &PyUnicode_Type
) {
2619 res
= save_unicode(self
, args
, 1);
2626 if (type
== &PyTuple_Type
) {
2627 res
= save_tuple(self
, args
);
2630 if (type
== &PyType_Type
) {
2631 res
= save_global(self
, args
, NULL
);
2637 if (type
== &PyList_Type
) {
2638 res
= save_list(self
, args
);
2644 if (type
== &PyDict_Type
) {
2645 res
= save_dict(self
, args
);
2651 if (type
== &PyInstance_Type
) {
2652 res
= save_inst(self
, args
);
2658 if (type
== &PyClass_Type
) {
2659 res
= save_global(self
, args
, NULL
);
2665 if (type
== &PyFunction_Type
) {
2666 res
= save_global(self
, args
, NULL
);
2667 if (res
&& PyErr_ExceptionMatches(PickleError
)) {
2668 /* fall back to reduce */
2677 if (type
== &PyCFunction_Type
) {
2678 res
= save_global(self
, args
, NULL
);
2683 if (!pers_save
&& self
->inst_pers_func
) {
2684 if ((tmp
= save_pers(self
, args
, self
->inst_pers_func
)) != 0) {
2690 if (PyType_IsSubtype(type
, &PyType_Type
)) {
2691 res
= save_global(self
, args
, NULL
);
2695 /* Get a reduction callable, and call it. This may come from
2696 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2697 * or the object's __reduce__ method.
2699 __reduce__
= PyDict_GetItem(dispatch_table
, (PyObject
*)type
);
2700 if (__reduce__
!= NULL
) {
2701 Py_INCREF(__reduce__
);
2703 ARG_TUP(self
, args
);
2705 t
= PyObject_Call(__reduce__
, self
->arg
, NULL
);
2710 /* Check for a __reduce_ex__ method. */
2711 __reduce__
= PyObject_GetAttr(args
, __reduce_ex___str
);
2712 if (__reduce__
!= NULL
) {
2713 t
= PyInt_FromLong(self
->proto
);
2718 t
= PyObject_Call(__reduce__
,
2725 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2729 /* Check for a __reduce__ method. */
2730 __reduce__
= PyObject_GetAttr(args
, __reduce___str
);
2731 if (__reduce__
!= NULL
) {
2732 t
= PyObject_Call(__reduce__
,
2736 PyErr_SetObject(UnpickleableError
, args
);
2745 if (PyString_Check(t
)) {
2746 res
= save_global(self
, args
, t
);
2750 if (!PyTuple_Check(t
)) {
2751 cPickle_ErrFormat(PicklingError
, "Value returned by "
2752 "%s must be string or tuple",
2757 res
= save_reduce(self
, t
, __reduce__
, args
);
2760 Py_LeaveRecursiveCall();
2761 Py_XDECREF(py_ob_id
);
2762 Py_XDECREF(__reduce__
);
2770 dump(Picklerobject
*self
, PyObject
*args
)
2772 static char stop
= STOP
;
2774 if (self
->proto
>= 2) {
2778 assert(self
->proto
>= 0 && self
->proto
< 256);
2779 bytes
[1] = (char)self
->proto
;
2780 if (self
->write_func(self
, bytes
, 2) < 0)
2784 if (save(self
, args
, 0) < 0)
2787 if (self
->write_func(self
, &stop
, 1) < 0)
2790 if (self
->write_func(self
, NULL
, 0) < 0)
2797 Pickle_clear_memo(Picklerobject
*self
, PyObject
*args
)
2800 PyDict_Clear(self
->memo
);
2806 Pickle_getvalue(Picklerobject
*self
, PyObject
*args
)
2808 int l
, i
, rsize
, ssize
, clear
=1, lm
;
2811 char *s
, *p
, *have_get
;
2814 /* Can be called by Python code or C code */
2815 if (args
&& !PyArg_ParseTuple(args
, "|i:getvalue", &clear
))
2818 /* Check to make sure we are based on a list */
2819 if (! Pdata_Check(self
->file
)) {
2820 PyErr_SetString(PicklingError
,
2821 "Attempt to getvalue() a non-list-based pickler");
2825 /* flush write buffer */
2826 if (write_other(self
, NULL
, 0) < 0) return NULL
;
2828 data
=(Pdata
*)self
->file
;
2831 /* set up an array to hold get/put status */
2832 lm
= PyDict_Size(self
->memo
);
2833 if (lm
< 0) return NULL
;
2835 have_get
= malloc(lm
);
2836 if (have_get
== NULL
) return PyErr_NoMemory();
2837 memset(have_get
, 0, lm
);
2839 /* Scan for gets. */
2840 for (rsize
= 0, i
= l
; --i
>= 0; ) {
2843 if (PyString_Check(k
))
2844 rsize
+= PyString_GET_SIZE(k
);
2846 else if (PyInt_Check(k
)) { /* put */
2847 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2848 if (ik
>= lm
|| ik
== 0) {
2849 PyErr_SetString(PicklingError
,
2850 "Invalid get data");
2853 if (have_get
[ik
]) /* with matching get */
2854 rsize
+= ik
< 256 ? 2 : 5;
2857 else if (! (PyTuple_Check(k
) &&
2858 PyTuple_GET_SIZE(k
) == 2 &&
2859 PyInt_Check((k
= PyTuple_GET_ITEM(k
, 0))))
2861 PyErr_SetString(PicklingError
,
2862 "Unexpected data in internal list");
2867 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2868 if (ik
>= lm
|| ik
== 0) {
2869 PyErr_SetString(PicklingError
,
2870 "Invalid get data");
2874 rsize
+= ik
< 256 ? 2 : 5;
2878 /* Now generate the result */
2879 r
= PyString_FromStringAndSize(NULL
, rsize
);
2880 if (r
== NULL
) goto err
;
2881 s
= PyString_AS_STRING((PyStringObject
*)r
);
2883 for (i
= 0; i
< l
; i
++) {
2886 if (PyString_Check(k
)) {
2887 ssize
= PyString_GET_SIZE(k
);
2889 p
=PyString_AS_STRING((PyStringObject
*)k
);
2890 while (--ssize
>= 0)
2895 else if (PyTuple_Check(k
)) { /* get */
2896 ik
= PyInt_AS_LONG((PyIntObject
*)
2897 PyTuple_GET_ITEM(k
, 0));
2900 *s
++ = (int)(ik
& 0xff);
2904 *s
++ = (int)(ik
& 0xff);
2905 *s
++ = (int)((ik
>> 8) & 0xff);
2906 *s
++ = (int)((ik
>> 16) & 0xff);
2907 *s
++ = (int)((ik
>> 24) & 0xff);
2912 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2914 if (have_get
[ik
]) { /* with matching get */
2917 *s
++ = (int)(ik
& 0xff);
2921 *s
++ = (int)(ik
& 0xff);
2922 *s
++ = (int)((ik
>> 8) & 0xff);
2923 *s
++ = (int)((ik
>> 16) & 0xff);
2924 *s
++ = (int)((ik
>> 24) & 0xff);
2931 PyDict_Clear(self
->memo
);
2932 Pdata_clear(data
, 0);
2943 Pickler_dump(Picklerobject
*self
, PyObject
*args
)
2948 if (!( PyArg_ParseTuple(args
, "O|i:dump", &ob
, &get
)))
2951 if (dump(self
, ob
) < 0)
2954 if (get
) return Pickle_getvalue(self
, NULL
);
2956 /* XXX Why does dump() return self? */
2958 return (PyObject
*)self
;
2962 static struct PyMethodDef Pickler_methods
[] =
2964 {"dump", (PyCFunction
)Pickler_dump
, METH_VARARGS
,
2965 PyDoc_STR("dump(object) -- "
2966 "Write an object in pickle format to the object's pickle stream")},
2967 {"clear_memo", (PyCFunction
)Pickle_clear_memo
, METH_NOARGS
,
2968 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2969 {"getvalue", (PyCFunction
)Pickle_getvalue
, METH_VARARGS
,
2970 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2971 {NULL
, NULL
} /* sentinel */
2975 static Picklerobject
*
2976 newPicklerobject(PyObject
*file
, int proto
)
2978 Picklerobject
*self
;
2981 proto
= HIGHEST_PROTOCOL
;
2982 if (proto
> HIGHEST_PROTOCOL
) {
2983 PyErr_Format(PyExc_ValueError
, "pickle protocol %d asked for; "
2984 "the highest available protocol is %d",
2985 proto
, HIGHEST_PROTOCOL
);
2989 self
= PyObject_GC_New(Picklerobject
, &Picklertype
);
2992 self
->proto
= proto
;
2993 self
->bin
= proto
> 0;
2998 self
->pers_func
= NULL
;
2999 self
->inst_pers_func
= NULL
;
3000 self
->write_buf
= NULL
;
3002 self
->fast_container
= 0;
3003 self
->fast_memo
= NULL
;
3005 self
->dispatch_table
= NULL
;
3017 if (!( self
->memo
= PyDict_New()))
3020 if (PyFile_Check(file
)) {
3021 self
->fp
= PyFile_AsFile(file
);
3022 if (self
->fp
== NULL
) {
3023 PyErr_SetString(PyExc_ValueError
,
3024 "I/O operation on closed file");
3027 self
->write_func
= write_file
;
3029 else if (PycStringIO_OutputCheck(file
)) {
3030 self
->write_func
= write_cStringIO
;
3032 else if (file
== Py_None
) {
3033 self
->write_func
= write_none
;
3036 self
->write_func
= write_other
;
3038 if (! Pdata_Check(file
)) {
3039 self
->write
= PyObject_GetAttr(file
, write_str
);
3042 PyErr_SetString(PyExc_TypeError
,
3043 "argument must have 'write' "
3049 self
->write_buf
= (char *)PyMem_Malloc(WRITE_BUF_SIZE
);
3050 if (self
->write_buf
== NULL
) {
3056 if (PyEval_GetRestricted()) {
3057 /* Restricted execution, get private tables */
3058 PyObject
*m
= PyImport_Import(copyreg_str
);
3062 self
->dispatch_table
= PyObject_GetAttr(m
, dispatch_table_str
);
3064 if (self
->dispatch_table
== NULL
)
3068 self
->dispatch_table
= dispatch_table
;
3069 Py_INCREF(dispatch_table
);
3071 PyObject_GC_Track(self
);
3082 get_Pickler(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
3084 static char *kwlist
[] = {"file", "protocol", NULL
};
3085 PyObject
*file
= NULL
;
3089 * The documented signature is Pickler(file, protocol=0), but this
3090 * accepts Pickler() and Pickler(integer) too. The meaning then
3091 * is clear as mud, undocumented, and not supported by pickle.py.
3092 * I'm told Zope uses this, but I haven't traced into this code
3093 * far enough to figure out what it means.
3095 if (!PyArg_ParseTuple(args
, "|i:Pickler", &proto
)) {
3098 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|i:Pickler",
3099 kwlist
, &file
, &proto
))
3102 return (PyObject
*)newPicklerobject(file
, proto
);
3107 Pickler_dealloc(Picklerobject
*self
)
3109 PyObject_GC_UnTrack(self
);
3110 Py_XDECREF(self
->write
);
3111 Py_XDECREF(self
->memo
);
3112 Py_XDECREF(self
->fast_memo
);
3113 Py_XDECREF(self
->arg
);
3114 Py_XDECREF(self
->file
);
3115 Py_XDECREF(self
->pers_func
);
3116 Py_XDECREF(self
->inst_pers_func
);
3117 Py_XDECREF(self
->dispatch_table
);
3118 PyMem_Free(self
->write_buf
);
3119 Py_TYPE(self
)->tp_free((PyObject
*)self
);
3123 Pickler_traverse(Picklerobject
*self
, visitproc visit
, void *arg
)
3125 Py_VISIT(self
->write
);
3126 Py_VISIT(self
->memo
);
3127 Py_VISIT(self
->fast_memo
);
3128 Py_VISIT(self
->arg
);
3129 Py_VISIT(self
->file
);
3130 Py_VISIT(self
->pers_func
);
3131 Py_VISIT(self
->inst_pers_func
);
3132 Py_VISIT(self
->dispatch_table
);
3137 Pickler_clear(Picklerobject
*self
)
3139 Py_CLEAR(self
->write
);
3140 Py_CLEAR(self
->memo
);
3141 Py_CLEAR(self
->fast_memo
);
3142 Py_CLEAR(self
->arg
);
3143 Py_CLEAR(self
->file
);
3144 Py_CLEAR(self
->pers_func
);
3145 Py_CLEAR(self
->inst_pers_func
);
3146 Py_CLEAR(self
->dispatch_table
);
3151 Pickler_get_pers_func(Picklerobject
*p
)
3153 if (p
->pers_func
== NULL
)
3154 PyErr_SetString(PyExc_AttributeError
, "persistent_id");
3156 Py_INCREF(p
->pers_func
);
3157 return p
->pers_func
;
3161 Pickler_set_pers_func(Picklerobject
*p
, PyObject
*v
)
3164 PyErr_SetString(PyExc_TypeError
,
3165 "attribute deletion is not supported");
3168 Py_XDECREF(p
->pers_func
);
3175 Pickler_set_inst_pers_func(Picklerobject
*p
, PyObject
*v
)
3178 PyErr_SetString(PyExc_TypeError
,
3179 "attribute deletion is not supported");
3182 Py_XDECREF(p
->inst_pers_func
);
3184 p
->inst_pers_func
= v
;
3189 Pickler_get_memo(Picklerobject
*p
)
3191 if (p
->memo
== NULL
)
3192 PyErr_SetString(PyExc_AttributeError
, "memo");
3199 Pickler_set_memo(Picklerobject
*p
, PyObject
*v
)
3202 PyErr_SetString(PyExc_TypeError
,
3203 "attribute deletion is not supported");
3206 if (!PyDict_Check(v
)) {
3207 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
3210 Py_XDECREF(p
->memo
);
3217 Pickler_get_error(Picklerobject
*p
)
3219 /* why is this an attribute on the Pickler? */
3220 Py_INCREF(PicklingError
);
3221 return PicklingError
;
3224 static PyMemberDef Pickler_members
[] = {
3225 {"binary", T_INT
, offsetof(Picklerobject
, bin
)},
3226 {"fast", T_INT
, offsetof(Picklerobject
, fast
)},
3230 static PyGetSetDef Pickler_getsets
[] = {
3231 {"persistent_id", (getter
)Pickler_get_pers_func
,
3232 (setter
)Pickler_set_pers_func
},
3233 {"inst_persistent_id", NULL
, (setter
)Pickler_set_inst_pers_func
},
3234 {"memo", (getter
)Pickler_get_memo
, (setter
)Pickler_set_memo
},
3235 {"PicklingError", (getter
)Pickler_get_error
, NULL
},
3239 PyDoc_STRVAR(Picklertype__doc__
,
3240 "Objects that know how to pickle objects\n");
3242 static PyTypeObject Picklertype
= {
3243 PyVarObject_HEAD_INIT(NULL
, 0)
3244 "cPickle.Pickler", /*tp_name*/
3245 sizeof(Picklerobject
), /*tp_basicsize*/
3247 (destructor
)Pickler_dealloc
, /* tp_dealloc */
3253 0, /* tp_as_number */
3254 0, /* tp_as_sequence */
3255 0, /* tp_as_mapping */
3259 PyObject_GenericGetAttr
, /* tp_getattro */
3260 PyObject_GenericSetAttr
, /* tp_setattro */
3261 0, /* tp_as_buffer */
3262 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
3263 Picklertype__doc__
, /* tp_doc */
3264 (traverseproc
)Pickler_traverse
, /* tp_traverse */
3265 (inquiry
)Pickler_clear
, /* tp_clear */
3266 0, /* tp_richcompare */
3267 0, /* tp_weaklistoffset */
3269 0, /* tp_iternext */
3270 Pickler_methods
, /* tp_methods */
3271 Pickler_members
, /* tp_members */
3272 Pickler_getsets
, /* tp_getset */
3276 find_class(PyObject
*py_module_name
, PyObject
*py_global_name
, PyObject
*fc
)
3278 PyObject
*global
= 0, *module
;
3282 PyErr_SetString(UnpicklingError
, "Global and instance "
3283 "pickles are not supported.");
3286 return PyObject_CallFunctionObjArgs(fc
, py_module_name
,
3287 py_global_name
, NULL
);
3290 module
= PySys_GetObject("modules");
3294 module
= PyDict_GetItem(module
, py_module_name
);
3295 if (module
== NULL
) {
3296 module
= PyImport_Import(py_module_name
);
3299 global
= PyObject_GetAttr(module
, py_global_name
);
3303 global
= PyObject_GetAttr(module
, py_global_name
);
3308 marker(Unpicklerobject
*self
)
3310 if (self
->num_marks
< 1) {
3311 PyErr_SetString(UnpicklingError
, "could not find MARK");
3315 return self
->marks
[--self
->num_marks
];
3320 load_none(Unpicklerobject
*self
)
3322 PDATA_APPEND(self
->stack
, Py_None
, -1);
3329 PyErr_SetString(UnpicklingError
, "pickle data was truncated");
3334 load_int(Unpicklerobject
*self
)
3336 PyObject
*py_int
= 0;
3341 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3342 if (len
< 2) return bad_readline();
3343 if (!( s
=pystrndup(s
,len
))) return -1;
3346 l
= strtol(s
, &endptr
, 0);
3348 if (errno
|| (*endptr
!= '\n') || (endptr
[1] != '\0')) {
3349 /* Hm, maybe we've got something long. Let's try reading
3350 it as a Python long object. */
3352 py_int
= PyLong_FromString(s
, NULL
, 0);
3353 if (py_int
== NULL
) {
3354 PyErr_SetString(PyExc_ValueError
,
3355 "could not convert string to int");
3360 if (len
== 3 && (l
== 0 || l
== 1)) {
3361 if (!( py_int
= PyBool_FromLong(l
))) goto finally
;
3364 if (!( py_int
= PyInt_FromLong(l
))) goto finally
;
3369 PDATA_PUSH(self
->stack
, py_int
, -1);
3379 load_bool(Unpicklerobject
*self
, PyObject
*boolean
)
3381 assert(boolean
== Py_True
|| boolean
== Py_False
);
3382 PDATA_APPEND(self
->stack
, boolean
, -1);
3386 /* s contains x bytes of a little-endian integer. Return its value as a
3387 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3388 * int, but when x is 4 it's a signed one. This is an historical source
3389 * of x-platform bugs.
3392 calc_binint(char *s
, int x
)
3398 for (i
= 0, l
= 0L; i
< x
; i
++) {
3399 c
= (unsigned char)s
[i
];
3400 l
|= (long)c
<< (i
* 8);
3403 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3404 * is signed, so on a box with longs bigger than 4 bytes we need
3405 * to extend a BININT's sign bit to the full width.
3407 if (x
== 4 && l
& (1L << 31))
3415 load_binintx(Unpicklerobject
*self
, char *s
, int x
)
3417 PyObject
*py_int
= 0;
3420 l
= calc_binint(s
, x
);
3422 if (!( py_int
= PyInt_FromLong(l
)))
3425 PDATA_PUSH(self
->stack
, py_int
, -1);
3431 load_binint(Unpicklerobject
*self
)
3435 if (self
->read_func(self
, &s
, 4) < 0)
3438 return load_binintx(self
, s
, 4);
3443 load_binint1(Unpicklerobject
*self
)
3447 if (self
->read_func(self
, &s
, 1) < 0)
3450 return load_binintx(self
, s
, 1);
3455 load_binint2(Unpicklerobject
*self
)
3459 if (self
->read_func(self
, &s
, 2) < 0)
3462 return load_binintx(self
, s
, 2);
3466 load_long(Unpicklerobject
*self
)
3472 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3473 if (len
< 2) return bad_readline();
3474 if (!( s
=pystrndup(s
,len
))) return -1;
3476 if (!( l
= PyLong_FromString(s
, &end
, 0)))
3480 PDATA_PUSH(self
->stack
, l
, -1);
3489 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3493 load_counted_long(Unpicklerobject
*self
, int size
)
3497 unsigned char *pdata
;
3500 assert(size
== 1 || size
== 4);
3501 i
= self
->read_func(self
, &nbytes
, size
);
3502 if (i
< 0) return -1;
3504 size
= calc_binint(nbytes
, size
);
3506 /* Corrupt or hostile pickle -- we never write one like
3509 PyErr_SetString(UnpicklingError
, "LONG pickle has negative "
3515 along
= PyLong_FromLong(0L);
3517 /* Read the raw little-endian bytes & convert. */
3518 i
= self
->read_func(self
, (char **)&pdata
, size
);
3519 if (i
< 0) return -1;
3520 along
= _PyLong_FromByteArray(pdata
, (size_t)size
,
3521 1 /* little endian */, 1 /* signed */);
3525 PDATA_PUSH(self
->stack
, along
, -1);
3530 load_float(Unpicklerobject
*self
)
3532 PyObject
*py_float
= 0;
3537 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3538 if (len
< 2) return bad_readline();
3539 if (!( s
=pystrndup(s
,len
))) return -1;
3542 d
= PyOS_ascii_strtod(s
, &endptr
);
3544 if ((errno
== ERANGE
&& !(fabs(d
) <= 1.0)) ||
3545 (endptr
[0] != '\n') || (endptr
[1] != '\0')) {
3546 PyErr_SetString(PyExc_ValueError
,
3547 "could not convert string to float");
3551 if (!( py_float
= PyFloat_FromDouble(d
)))
3555 PDATA_PUSH(self
->stack
, py_float
, -1);
3565 load_binfloat(Unpicklerobject
*self
)
3571 if (self
->read_func(self
, &p
, 8) < 0)
3574 x
= _PyFloat_Unpack8((unsigned char *)p
, 0);
3575 if (x
== -1.0 && PyErr_Occurred())
3578 py_float
= PyFloat_FromDouble(x
);
3579 if (py_float
== NULL
)
3582 PDATA_PUSH(self
->stack
, py_float
, -1);
3587 load_string(Unpicklerobject
*self
)
3593 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3594 if (len
< 2) return bad_readline();
3595 if (!( s
=pystrndup(s
,len
))) return -1;
3598 /* Strip outermost quotes */
3599 while (s
[len
-1] <= ' ')
3601 if(s
[0]=='"' && s
[len
-1]=='"'){
3605 } else if(s
[0]=='\'' && s
[len
-1]=='\''){
3611 /********************************************/
3613 str
= PyString_DecodeEscape(p
, len
, NULL
, 0, NULL
);
3616 PDATA_PUSH(self
->stack
, str
, -1);
3623 PyErr_SetString(PyExc_ValueError
,"insecure string pickle");
3629 load_binstring(Unpicklerobject
*self
)
3631 PyObject
*py_string
= 0;
3635 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3637 l
= calc_binint(s
, 4);
3639 /* Corrupt or hostile pickle -- we never write one like
3642 PyErr_SetString(UnpicklingError
,
3643 "BINSTRING pickle has negative byte count");
3647 if (self
->read_func(self
, &s
, l
) < 0)
3650 if (!( py_string
= PyString_FromStringAndSize(s
, l
)))
3653 PDATA_PUSH(self
->stack
, py_string
, -1);
3659 load_short_binstring(Unpicklerobject
*self
)
3661 PyObject
*py_string
= 0;
3665 if (self
->read_func(self
, &s
, 1) < 0)
3668 l
= (unsigned char)s
[0];
3670 if (self
->read_func(self
, &s
, l
) < 0) return -1;
3672 if (!( py_string
= PyString_FromStringAndSize(s
, l
))) return -1;
3674 PDATA_PUSH(self
->stack
, py_string
, -1);
3679 #ifdef Py_USING_UNICODE
3681 load_unicode(Unpicklerobject
*self
)
3687 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3688 if (len
< 1) return bad_readline();
3690 if (!( str
= PyUnicode_DecodeRawUnicodeEscape(s
, len
- 1, NULL
)))
3693 PDATA_PUSH(self
->stack
, str
, -1);
3702 #ifdef Py_USING_UNICODE
3704 load_binunicode(Unpicklerobject
*self
)
3710 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3712 l
= calc_binint(s
, 4);
3714 /* Corrupt or hostile pickle -- we never write one like
3717 PyErr_SetString(UnpicklingError
,
3718 "BINUNICODE pickle has negative byte count");
3722 if (self
->read_func(self
, &s
, l
) < 0)
3725 if (!( unicode
= PyUnicode_DecodeUTF8(s
, l
, NULL
)))
3728 PDATA_PUSH(self
->stack
, unicode
, -1);
3735 load_tuple(Unpicklerobject
*self
)
3740 if ((i
= marker(self
)) < 0) return -1;
3741 if (!( tup
=Pdata_popTuple(self
->stack
, i
))) return -1;
3742 PDATA_PUSH(self
->stack
, tup
, -1);
3747 load_counted_tuple(Unpicklerobject
*self
, int len
)
3749 PyObject
*tup
= PyTuple_New(len
);
3754 while (--len
>= 0) {
3757 PDATA_POP(self
->stack
, element
);
3758 if (element
== NULL
)
3760 PyTuple_SET_ITEM(tup
, len
, element
);
3762 PDATA_PUSH(self
->stack
, tup
, -1);
3767 load_empty_list(Unpicklerobject
*self
)
3771 if (!( list
=PyList_New(0))) return -1;
3772 PDATA_PUSH(self
->stack
, list
, -1);
3777 load_empty_dict(Unpicklerobject
*self
)
3781 if (!( dict
=PyDict_New())) return -1;
3782 PDATA_PUSH(self
->stack
, dict
, -1);
3788 load_list(Unpicklerobject
*self
)
3793 if ((i
= marker(self
)) < 0) return -1;
3794 if (!( list
=Pdata_popList(self
->stack
, i
))) return -1;
3795 PDATA_PUSH(self
->stack
, list
, -1);
3800 load_dict(Unpicklerobject
*self
)
3802 PyObject
*dict
, *key
, *value
;
3805 if ((i
= marker(self
)) < 0) return -1;
3806 j
=self
->stack
->length
;
3808 if (!( dict
= PyDict_New())) return -1;
3810 for (k
= i
+1; k
< j
; k
+= 2) {
3811 key
=self
->stack
->data
[k
-1];
3812 value
=self
->stack
->data
[k
];
3813 if (PyDict_SetItem(dict
, key
, value
) < 0) {
3818 Pdata_clear(self
->stack
, i
);
3819 PDATA_PUSH(self
->stack
, dict
, -1);
3824 Instance_New(PyObject
*cls
, PyObject
*args
)
3828 if (PyClass_Check(cls
)) {
3831 if ((l
=PyObject_Size(args
)) < 0) goto err
;
3833 PyObject
*__getinitargs__
;
3835 __getinitargs__
= PyObject_GetAttr(cls
,
3836 __getinitargs___str
);
3837 if (!__getinitargs__
) {
3838 /* We have a class with no __getinitargs__,
3839 so bypass usual construction */
3843 if (!( inst
=PyInstance_NewRaw(cls
, NULL
)))
3847 Py_DECREF(__getinitargs__
);
3850 if ((r
=PyInstance_New(cls
, args
, NULL
))) return r
;
3854 if ((r
=PyObject_CallObject(cls
, args
))) return r
;
3858 PyObject
*tp
, *v
, *tb
, *tmp_value
;
3860 PyErr_Fetch(&tp
, &v
, &tb
);
3862 /* NULL occurs when there was a KeyboardInterrupt */
3863 if (tmp_value
== NULL
)
3864 tmp_value
= Py_None
;
3865 if ((r
= PyTuple_Pack(3, tmp_value
, cls
, args
))) {
3869 PyErr_Restore(tp
,v
,tb
);
3876 load_obj(Unpicklerobject
*self
)
3878 PyObject
*class, *tup
, *obj
=0;
3881 if ((i
= marker(self
)) < 0) return -1;
3882 if (!( tup
=Pdata_popTuple(self
->stack
, i
+1))) return -1;
3883 PDATA_POP(self
->stack
, class);
3885 obj
= Instance_New(class, tup
);
3890 if (! obj
) return -1;
3891 PDATA_PUSH(self
->stack
, obj
, -1);
3897 load_inst(Unpicklerobject
*self
)
3899 PyObject
*tup
, *class=0, *obj
=0, *module_name
, *class_name
;
3903 if ((i
= marker(self
)) < 0) return -1;
3905 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3906 if (len
< 2) return bad_readline();
3907 module_name
= PyString_FromStringAndSize(s
, len
- 1);
3908 if (!module_name
) return -1;
3910 if ((len
= self
->readline_func(self
, &s
)) >= 0) {
3911 if (len
< 2) return bad_readline();
3912 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3913 class = find_class(module_name
, class_name
,
3915 Py_DECREF(class_name
);
3918 Py_DECREF(module_name
);
3920 if (! class) return -1;
3922 if ((tup
=Pdata_popTuple(self
->stack
, i
))) {
3923 obj
= Instance_New(class, tup
);
3928 if (! obj
) return -1;
3930 PDATA_PUSH(self
->stack
, obj
, -1);
3935 load_newobj(Unpicklerobject
*self
)
3937 PyObject
*args
= NULL
;
3938 PyObject
*clsraw
= NULL
;
3939 PyTypeObject
*cls
; /* clsraw cast to its true type */
3942 /* Stack is ... cls argtuple, and we want to call
3943 * cls.__new__(cls, *argtuple).
3945 PDATA_POP(self
->stack
, args
);
3946 if (args
== NULL
) goto Fail
;
3947 if (! PyTuple_Check(args
)) {
3948 PyErr_SetString(UnpicklingError
, "NEWOBJ expected an arg "
3953 PDATA_POP(self
->stack
, clsraw
);
3954 cls
= (PyTypeObject
*)clsraw
;
3955 if (cls
== NULL
) goto Fail
;
3956 if (! PyType_Check(cls
)) {
3957 PyErr_SetString(UnpicklingError
, "NEWOBJ class argument "
3958 "isn't a type object");
3961 if (cls
->tp_new
== NULL
) {
3962 PyErr_SetString(UnpicklingError
, "NEWOBJ class argument "
3968 obj
= cls
->tp_new(cls
, args
, NULL
);
3969 if (obj
== NULL
) goto Fail
;
3973 PDATA_PUSH(self
->stack
, obj
, -1);
3983 load_global(Unpicklerobject
*self
)
3985 PyObject
*class = 0, *module_name
= 0, *class_name
= 0;
3989 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3990 if (len
< 2) return bad_readline();
3991 module_name
= PyString_FromStringAndSize(s
, len
- 1);
3992 if (!module_name
) return -1;
3994 if ((len
= self
->readline_func(self
, &s
)) >= 0) {
3996 Py_DECREF(module_name
);
3997 return bad_readline();
3999 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
4000 class = find_class(module_name
, class_name
,
4002 Py_DECREF(class_name
);
4005 Py_DECREF(module_name
);
4007 if (! class) return -1;
4008 PDATA_PUSH(self
->stack
, class, -1);
4014 load_persid(Unpicklerobject
*self
)
4020 if (self
->pers_func
) {
4021 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
4022 if (len
< 2) return bad_readline();
4024 pid
= PyString_FromStringAndSize(s
, len
- 1);
4025 if (!pid
) return -1;
4027 if (PyList_Check(self
->pers_func
)) {
4028 if (PyList_Append(self
->pers_func
, pid
) < 0) {
4036 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
4042 if (! pid
) return -1;
4044 PDATA_PUSH(self
->stack
, pid
, -1);
4048 PyErr_SetString(UnpicklingError
,
4049 "A load persistent id instruction was encountered,\n"
4050 "but no persistent_load function was specified.");
4056 load_binpersid(Unpicklerobject
*self
)
4060 if (self
->pers_func
) {
4061 PDATA_POP(self
->stack
, pid
);
4062 if (! pid
) return -1;
4064 if (PyList_Check(self
->pers_func
)) {
4065 if (PyList_Append(self
->pers_func
, pid
) < 0) {
4073 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
4077 if (! pid
) return -1;
4080 PDATA_PUSH(self
->stack
, pid
, -1);
4084 PyErr_SetString(UnpicklingError
,
4085 "A load persistent id instruction was encountered,\n"
4086 "but no persistent_load function was specified.");
4093 load_pop(Unpicklerobject
*self
)
4095 int len
= self
->stack
->length
;
4097 /* Note that we split the (pickle.py) stack into two stacks,
4098 an object stack and a mark stack. We have to be clever and
4099 pop the right one. We do this by looking at the top of the
4100 mark stack first, and only signalling a stack underflow if
4101 the object stack is empty and the mark stack doesn't match
4104 if (self
->num_marks
> 0 && self
->marks
[self
->num_marks
- 1] == len
) {
4106 } else if (len
>= 0) {
4108 Py_DECREF(self
->stack
->data
[len
]);
4109 self
->stack
->length
= len
;
4111 return stackUnderflow();
4118 load_pop_mark(Unpicklerobject
*self
)
4122 if ((i
= marker(self
)) < 0)
4125 Pdata_clear(self
->stack
, i
);
4132 load_dup(Unpicklerobject
*self
)
4137 if ((len
= self
->stack
->length
) <= 0) return stackUnderflow();
4138 last
=self
->stack
->data
[len
-1];
4140 PDATA_PUSH(self
->stack
, last
, -1);
4146 load_get(Unpicklerobject
*self
)
4148 PyObject
*py_str
= 0, *value
= 0;
4153 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
4154 if (len
< 2) return bad_readline();
4156 if (!( py_str
= PyString_FromStringAndSize(s
, len
- 1))) return -1;
4158 value
= PyDict_GetItem(self
->memo
, py_str
);
4160 PyErr_SetObject(BadPickleGet
, py_str
);
4164 PDATA_APPEND(self
->stack
, value
, -1);
4174 load_binget(Unpicklerobject
*self
)
4176 PyObject
*py_key
= 0, *value
= 0;
4181 if (self
->read_func(self
, &s
, 1) < 0) return -1;
4183 key
= (unsigned char)s
[0];
4184 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
4186 value
= PyDict_GetItem(self
->memo
, py_key
);
4188 PyErr_SetObject(BadPickleGet
, py_key
);
4192 PDATA_APPEND(self
->stack
, value
, -1);
4202 load_long_binget(Unpicklerobject
*self
)
4204 PyObject
*py_key
= 0, *value
= 0;
4210 if (self
->read_func(self
, &s
, 4) < 0) return -1;
4212 c
= (unsigned char)s
[0];
4214 c
= (unsigned char)s
[1];
4215 key
|= (long)c
<< 8;
4216 c
= (unsigned char)s
[2];
4217 key
|= (long)c
<< 16;
4218 c
= (unsigned char)s
[3];
4219 key
|= (long)c
<< 24;
4221 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
4223 value
= PyDict_GetItem(self
->memo
, py_key
);
4225 PyErr_SetObject(BadPickleGet
, py_key
);
4229 PDATA_APPEND(self
->stack
, value
, -1);
4237 /* Push an object from the extension registry (EXT[124]). nbytes is
4238 * the number of bytes following the opcode, holding the index (code) value.
4241 load_extension(Unpicklerobject
*self
, int nbytes
)
4243 char *codebytes
; /* the nbytes bytes after the opcode */
4244 long code
; /* calc_binint returns long */
4245 PyObject
*py_code
; /* code as a Python int */
4246 PyObject
*obj
; /* the object to push */
4247 PyObject
*pair
; /* (module_name, class_name) */
4248 PyObject
*module_name
, *class_name
;
4250 assert(nbytes
== 1 || nbytes
== 2 || nbytes
== 4);
4251 if (self
->read_func(self
, &codebytes
, nbytes
) < 0) return -1;
4252 code
= calc_binint(codebytes
, nbytes
);
4253 if (code
<= 0) { /* note that 0 is forbidden */
4254 /* Corrupt or hostile pickle. */
4255 PyErr_SetString(UnpicklingError
, "EXT specifies code <= 0");
4259 /* Look for the code in the cache. */
4260 py_code
= PyInt_FromLong(code
);
4261 if (py_code
== NULL
) return -1;
4262 obj
= PyDict_GetItem(extension_cache
, py_code
);
4266 PDATA_APPEND(self
->stack
, obj
, -1);
4270 /* Look up the (module_name, class_name) pair. */
4271 pair
= PyDict_GetItem(inverted_registry
, py_code
);
4274 PyErr_Format(PyExc_ValueError
, "unregistered extension "
4278 /* Since the extension registry is manipulable via Python code,
4279 * confirm that pair is really a 2-tuple of strings.
4281 if (!PyTuple_Check(pair
) || PyTuple_Size(pair
) != 2 ||
4282 !PyString_Check(module_name
= PyTuple_GET_ITEM(pair
, 0)) ||
4283 !PyString_Check(class_name
= PyTuple_GET_ITEM(pair
, 1))) {
4285 PyErr_Format(PyExc_ValueError
, "_inverted_registry[%ld] "
4286 "isn't a 2-tuple of strings", code
);
4289 /* Load the object. */
4290 obj
= find_class(module_name
, class_name
, self
->find_class
);
4295 /* Cache code -> obj. */
4296 code
= PyDict_SetItem(extension_cache
, py_code
, obj
);
4302 PDATA_PUSH(self
->stack
, obj
, -1);
4307 load_put(Unpicklerobject
*self
)
4309 PyObject
*py_str
= 0, *value
= 0;
4313 if ((l
= self
->readline_func(self
, &s
)) < 0) return -1;
4314 if (l
< 2) return bad_readline();
4315 if (!( len
=self
->stack
->length
)) return stackUnderflow();
4316 if (!( py_str
= PyString_FromStringAndSize(s
, l
- 1))) return -1;
4317 value
=self
->stack
->data
[len
-1];
4318 l
=PyDict_SetItem(self
->memo
, py_str
, value
);
4325 load_binput(Unpicklerobject
*self
)
4327 PyObject
*py_key
= 0, *value
= 0;
4332 if (self
->read_func(self
, &s
, 1) < 0) return -1;
4333 if (!( (len
=self
->stack
->length
) > 0 )) return stackUnderflow();
4335 key
= (unsigned char)s
[0];
4337 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
4338 value
=self
->stack
->data
[len
-1];
4339 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
4346 load_long_binput(Unpicklerobject
*self
)
4348 PyObject
*py_key
= 0, *value
= 0;
4354 if (self
->read_func(self
, &s
, 4) < 0) return -1;
4355 if (!( len
=self
->stack
->length
)) return stackUnderflow();
4357 c
= (unsigned char)s
[0];
4359 c
= (unsigned char)s
[1];
4360 key
|= (long)c
<< 8;
4361 c
= (unsigned char)s
[2];
4362 key
|= (long)c
<< 16;
4363 c
= (unsigned char)s
[3];
4364 key
|= (long)c
<< 24;
4366 if (!( py_key
= PyInt_FromLong(key
))) return -1;
4367 value
=self
->stack
->data
[len
-1];
4368 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
4375 do_append(Unpicklerobject
*self
, int x
)
4377 PyObject
*value
= 0, *list
= 0, *append_method
= 0;
4380 len
=self
->stack
->length
;
4381 if (!( len
>= x
&& x
> 0 )) return stackUnderflow();
4383 if (len
==x
) return 0;
4385 list
=self
->stack
->data
[x
-1];
4387 if (PyList_Check(list
)) {
4391 slice
=Pdata_popList(self
->stack
, x
);
4392 if (! slice
) return -1;
4393 list_len
= PyList_GET_SIZE(list
);
4394 i
=PyList_SetSlice(list
, list_len
, list_len
, slice
);
4400 if (!( append_method
= PyObject_GetAttr(list
, append_str
)))
4403 for (i
= x
; i
< len
; i
++) {
4406 value
=self
->stack
->data
[i
];
4408 ARG_TUP(self
, value
);
4410 junk
= PyObject_Call(append_method
, self
->arg
,
4415 Pdata_clear(self
->stack
, i
+1);
4416 self
->stack
->length
=x
;
4417 Py_DECREF(append_method
);
4422 self
->stack
->length
=x
;
4423 Py_DECREF(append_method
);
4431 load_append(Unpicklerobject
*self
)
4433 return do_append(self
, self
->stack
->length
- 1);
4438 load_appends(Unpicklerobject
*self
)
4440 return do_append(self
, marker(self
));
4445 do_setitems(Unpicklerobject
*self
, int x
)
4447 PyObject
*value
= 0, *key
= 0, *dict
= 0;
4450 if (!( (len
=self
->stack
->length
) >= x
4451 && x
> 0 )) return stackUnderflow();
4453 dict
=self
->stack
->data
[x
-1];
4455 for (i
= x
+1; i
< len
; i
+= 2) {
4456 key
=self
->stack
->data
[i
-1];
4457 value
=self
->stack
->data
[i
];
4458 if (PyObject_SetItem(dict
, key
, value
) < 0) {
4464 Pdata_clear(self
->stack
, x
);
4471 load_setitem(Unpicklerobject
*self
)
4473 return do_setitems(self
, self
->stack
->length
- 2);
4477 load_setitems(Unpicklerobject
*self
)
4479 return do_setitems(self
, marker(self
));
4484 load_build(Unpicklerobject
*self
)
4486 PyObject
*state
, *inst
, *slotstate
;
4487 PyObject
*__setstate__
;
4488 PyObject
*d_key
, *d_value
;
4492 /* Stack is ... instance, state. We want to leave instance at
4493 * the stack top, possibly mutated via instance.__setstate__(state).
4495 if (self
->stack
->length
< 2)
4496 return stackUnderflow();
4497 PDATA_POP(self
->stack
, state
);
4500 inst
= self
->stack
->data
[self
->stack
->length
- 1];
4502 __setstate__
= PyObject_GetAttr(inst
, __setstate___str
);
4503 if (__setstate__
!= NULL
) {
4504 PyObject
*junk
= NULL
;
4506 /* The explicit __setstate__ is responsible for everything. */
4507 ARG_TUP(self
, state
);
4509 junk
= PyObject_Call(__setstate__
, self
->arg
, NULL
);
4512 Py_DECREF(__setstate__
);
4518 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4522 /* A default __setstate__. First see whether state embeds a
4523 * slot state dict too (a proto 2 addition).
4525 if (PyTuple_Check(state
) && PyTuple_Size(state
) == 2) {
4526 PyObject
*temp
= state
;
4527 state
= PyTuple_GET_ITEM(temp
, 0);
4528 slotstate
= PyTuple_GET_ITEM(temp
, 1);
4530 Py_INCREF(slotstate
);
4536 /* Set inst.__dict__ from the state dict (if any). */
4537 if (state
!= Py_None
) {
4539 if (! PyDict_Check(state
)) {
4540 PyErr_SetString(UnpicklingError
, "state is not a "
4544 dict
= PyObject_GetAttr(inst
, __dict___str
);
4549 while (PyDict_Next(state
, &i
, &d_key
, &d_value
)) {
4550 /* normally the keys for instance attributes are
4551 interned. we should try to do that here. */
4553 if (PyString_CheckExact(d_key
))
4554 PyString_InternInPlace(&d_key
);
4555 if (PyObject_SetItem(dict
, d_key
, d_value
) < 0) {
4564 /* Also set instance attributes from the slotstate dict (if any). */
4565 if (slotstate
!= NULL
) {
4566 if (! PyDict_Check(slotstate
)) {
4567 PyErr_SetString(UnpicklingError
, "slot state is not "
4572 while (PyDict_Next(slotstate
, &i
, &d_key
, &d_value
)) {
4573 if (PyObject_SetAttr(inst
, d_key
, d_value
) < 0)
4581 Py_XDECREF(slotstate
);
4587 load_mark(Unpicklerobject
*self
)
4591 /* Note that we split the (pickle.py) stack into two stacks, an
4592 object stack and a mark stack. Here we push a mark onto the
4596 if ((self
->num_marks
+ 1) >= self
->marks_size
) {
4598 s
=self
->marks_size
+20;
4599 if (s
<= self
->num_marks
) s
=self
->num_marks
+ 1;
4600 if (self
->marks
== NULL
)
4601 marks
=(int *)malloc(s
* sizeof(int));
4603 marks
=(int *)realloc(self
->marks
,
4609 self
->marks
= marks
;
4610 self
->marks_size
= s
;
4613 self
->marks
[self
->num_marks
++] = self
->stack
->length
;
4619 load_reduce(Unpicklerobject
*self
)
4621 PyObject
*callable
= 0, *arg_tup
= 0, *ob
= 0;
4623 PDATA_POP(self
->stack
, arg_tup
);
4624 if (! arg_tup
) return -1;
4625 PDATA_POP(self
->stack
, callable
);
4627 ob
= Instance_New(callable
, arg_tup
);
4628 Py_DECREF(callable
);
4632 if (! ob
) return -1;
4634 PDATA_PUSH(self
->stack
, ob
, -1);
4638 /* Just raises an error if we don't know the protocol specified. PROTO
4639 * is the first opcode for protocols >= 2.
4642 load_proto(Unpicklerobject
*self
)
4647 i
= self
->read_func(self
, &protobyte
, 1);
4651 i
= calc_binint(protobyte
, 1);
4652 /* No point checking for < 0, since calc_binint returns an unsigned
4653 * int when chewing on 1 byte.
4656 if (i
<= HIGHEST_PROTOCOL
)
4659 PyErr_Format(PyExc_ValueError
, "unsupported pickle protocol: %d", i
);
4664 load(Unpicklerobject
*self
)
4666 PyObject
*err
= 0, *val
= 0;
4669 self
->num_marks
= 0;
4670 if (self
->stack
->length
) Pdata_clear(self
->stack
, 0);
4673 if (self
->read_func(self
, &s
, 1) < 0)
4678 if (load_none(self
) < 0)
4683 if (load_binint(self
) < 0)
4688 if (load_binint1(self
) < 0)
4693 if (load_binint2(self
) < 0)
4698 if (load_int(self
) < 0)
4703 if (load_long(self
) < 0)
4708 if (load_counted_long(self
, 1) < 0)
4713 if (load_counted_long(self
, 4) < 0)
4718 if (load_float(self
) < 0)
4723 if (load_binfloat(self
) < 0)
4728 if (load_binstring(self
) < 0)
4732 case SHORT_BINSTRING
:
4733 if (load_short_binstring(self
) < 0)
4738 if (load_string(self
) < 0)
4742 #ifdef Py_USING_UNICODE
4744 if (load_unicode(self
) < 0)
4749 if (load_binunicode(self
) < 0)
4755 if (load_counted_tuple(self
, 0) < 0)
4760 if (load_counted_tuple(self
, 1) < 0)
4765 if (load_counted_tuple(self
, 2) < 0)
4770 if (load_counted_tuple(self
, 3) < 0)
4775 if (load_tuple(self
) < 0)
4780 if (load_empty_list(self
) < 0)
4785 if (load_list(self
) < 0)
4790 if (load_empty_dict(self
) < 0)
4795 if (load_dict(self
) < 0)
4800 if (load_obj(self
) < 0)
4805 if (load_inst(self
) < 0)
4810 if (load_newobj(self
) < 0)
4815 if (load_global(self
) < 0)
4820 if (load_append(self
) < 0)
4825 if (load_appends(self
) < 0)
4830 if (load_build(self
) < 0)
4835 if (load_dup(self
) < 0)
4840 if (load_binget(self
) < 0)
4845 if (load_long_binget(self
) < 0)
4850 if (load_get(self
) < 0)
4855 if (load_extension(self
, 1) < 0)
4860 if (load_extension(self
, 2) < 0)
4865 if (load_extension(self
, 4) < 0)
4869 if (load_mark(self
) < 0)
4874 if (load_binput(self
) < 0)
4879 if (load_long_binput(self
) < 0)
4884 if (load_put(self
) < 0)
4889 if (load_pop(self
) < 0)
4894 if (load_pop_mark(self
) < 0)
4899 if (load_setitem(self
) < 0)
4904 if (load_setitems(self
) < 0)
4912 if (load_persid(self
) < 0)
4917 if (load_binpersid(self
) < 0)
4922 if (load_reduce(self
) < 0)
4927 if (load_proto(self
) < 0)
4932 if (load_bool(self
, Py_True
) < 0)
4937 if (load_bool(self
, Py_False
) < 0)
4943 PyErr_SetNone(PyExc_EOFError
);
4947 cPickle_ErrFormat(UnpicklingError
,
4948 "invalid load key, '%s'.",
4956 if ((err
= PyErr_Occurred())) {
4957 if (err
== PyExc_EOFError
) {
4958 PyErr_SetNone(PyExc_EOFError
);
4963 PDATA_POP(self
->stack
, val
);
4968 /* No-load functions to support noload, which is used to
4969 find persistent references. */
4972 noload_obj(Unpicklerobject
*self
)
4976 if ((i
= marker(self
)) < 0) return -1;
4977 return Pdata_clear(self
->stack
, i
+1);
4982 noload_inst(Unpicklerobject
*self
)
4987 if ((i
= marker(self
)) < 0) return -1;
4988 Pdata_clear(self
->stack
, i
);
4989 if (self
->readline_func(self
, &s
) < 0) return -1;
4990 if (self
->readline_func(self
, &s
) < 0) return -1;
4991 PDATA_APPEND(self
->stack
, Py_None
, -1);
4996 noload_newobj(Unpicklerobject
*self
)
5000 PDATA_POP(self
->stack
, obj
); /* pop argtuple */
5001 if (obj
== NULL
) return -1;
5004 PDATA_POP(self
->stack
, obj
); /* pop cls */
5005 if (obj
== NULL
) return -1;
5008 PDATA_APPEND(self
->stack
, Py_None
, -1);
5013 noload_global(Unpicklerobject
*self
)
5017 if (self
->readline_func(self
, &s
) < 0) return -1;
5018 if (self
->readline_func(self
, &s
) < 0) return -1;
5019 PDATA_APPEND(self
->stack
, Py_None
,-1);
5024 noload_reduce(Unpicklerobject
*self
)
5027 if (self
->stack
->length
< 2) return stackUnderflow();
5028 Pdata_clear(self
->stack
, self
->stack
->length
-2);
5029 PDATA_APPEND(self
->stack
, Py_None
,-1);
5034 noload_build(Unpicklerobject
*self
) {
5036 if (self
->stack
->length
< 1) return stackUnderflow();
5037 Pdata_clear(self
->stack
, self
->stack
->length
-1);
5042 noload_extension(Unpicklerobject
*self
, int nbytes
)
5046 assert(nbytes
== 1 || nbytes
== 2 || nbytes
== 4);
5047 if (self
->read_func(self
, &codebytes
, nbytes
) < 0) return -1;
5048 PDATA_APPEND(self
->stack
, Py_None
, -1);
5054 noload(Unpicklerobject
*self
)
5056 PyObject
*err
= 0, *val
= 0;
5059 self
->num_marks
= 0;
5060 Pdata_clear(self
->stack
, 0);
5063 if (self
->read_func(self
, &s
, 1) < 0)
5068 if (load_none(self
) < 0)
5073 if (load_binint(self
) < 0)
5078 if (load_binint1(self
) < 0)
5083 if (load_binint2(self
) < 0)
5088 if (load_int(self
) < 0)
5093 if (load_long(self
) < 0)
5098 if (load_counted_long(self
, 1) < 0)
5103 if (load_counted_long(self
, 4) < 0)
5108 if (load_float(self
) < 0)
5113 if (load_binfloat(self
) < 0)
5118 if (load_binstring(self
) < 0)
5122 case SHORT_BINSTRING
:
5123 if (load_short_binstring(self
) < 0)
5128 if (load_string(self
) < 0)
5132 #ifdef Py_USING_UNICODE
5134 if (load_unicode(self
) < 0)
5139 if (load_binunicode(self
) < 0)
5145 if (load_counted_tuple(self
, 0) < 0)
5150 if (load_counted_tuple(self
, 1) < 0)
5155 if (load_counted_tuple(self
, 2) < 0)
5160 if (load_counted_tuple(self
, 3) < 0)
5165 if (load_tuple(self
) < 0)
5170 if (load_empty_list(self
) < 0)
5175 if (load_list(self
) < 0)
5180 if (load_empty_dict(self
) < 0)
5185 if (load_dict(self
) < 0)
5190 if (noload_obj(self
) < 0)
5195 if (noload_inst(self
) < 0)
5200 if (noload_newobj(self
) < 0)
5205 if (noload_global(self
) < 0)
5210 if (load_append(self
) < 0)
5215 if (load_appends(self
) < 0)
5220 if (noload_build(self
) < 0)
5225 if (load_dup(self
) < 0)
5230 if (load_binget(self
) < 0)
5235 if (load_long_binget(self
) < 0)
5240 if (load_get(self
) < 0)
5245 if (noload_extension(self
, 1) < 0)
5250 if (noload_extension(self
, 2) < 0)
5255 if (noload_extension(self
, 4) < 0)
5260 if (load_mark(self
) < 0)
5265 if (load_binput(self
) < 0)
5270 if (load_long_binput(self
) < 0)
5275 if (load_put(self
) < 0)
5280 if (load_pop(self
) < 0)
5285 if (load_pop_mark(self
) < 0)
5290 if (load_setitem(self
) < 0)
5295 if (load_setitems(self
) < 0)
5303 if (load_persid(self
) < 0)
5308 if (load_binpersid(self
) < 0)
5313 if (noload_reduce(self
) < 0)
5318 if (load_proto(self
) < 0)
5323 if (load_bool(self
, Py_True
) < 0)
5328 if (load_bool(self
, Py_False
) < 0)
5332 cPickle_ErrFormat(UnpicklingError
,
5333 "invalid load key, '%s'.",
5341 if ((err
= PyErr_Occurred())) {
5342 if (err
== PyExc_EOFError
) {
5343 PyErr_SetNone(PyExc_EOFError
);
5348 PDATA_POP(self
->stack
, val
);
5354 Unpickler_load(Unpicklerobject
*self
, PyObject
*unused
)
5360 Unpickler_noload(Unpicklerobject
*self
, PyObject
*unused
)
5362 return noload(self
);
5366 static struct PyMethodDef Unpickler_methods
[] = {
5367 {"load", (PyCFunction
)Unpickler_load
, METH_NOARGS
,
5368 PyDoc_STR("load() -- Load a pickle")
5370 {"noload", (PyCFunction
)Unpickler_noload
, METH_NOARGS
,
5372 "noload() -- not load a pickle, but go through most of the motions\n"
5374 "This function can be used to read past a pickle without instantiating\n"
5375 "any objects or importing any modules. It can also be used to find all\n"
5376 "persistent references without instantiating any objects or importing\n"
5379 {NULL
, NULL
} /* sentinel */
5383 static Unpicklerobject
*
5384 newUnpicklerobject(PyObject
*f
)
5386 Unpicklerobject
*self
;
5388 if (!( self
= PyObject_GC_New(Unpicklerobject
, &Unpicklertype
)))
5393 self
->stack
= (Pdata
*)Pdata_New();
5394 self
->pers_func
= NULL
;
5395 self
->last_string
= NULL
;
5397 self
->num_marks
= 0;
5398 self
->marks_size
= 0;
5401 self
->readline
= NULL
;
5402 self
->find_class
= NULL
;
5404 if (!( self
->memo
= PyDict_New()))
5413 /* Set read, readline based on type of f */
5414 if (PyFile_Check(f
)) {
5415 self
->fp
= PyFile_AsFile(f
);
5416 if (self
->fp
== NULL
) {
5417 PyErr_SetString(PyExc_ValueError
,
5418 "I/O operation on closed file");
5421 self
->read_func
= read_file
;
5422 self
->readline_func
= readline_file
;
5424 else if (PycStringIO_InputCheck(f
)) {
5426 self
->read_func
= read_cStringIO
;
5427 self
->readline_func
= readline_cStringIO
;
5432 self
->read_func
= read_other
;
5433 self
->readline_func
= readline_other
;
5435 if (!( (self
->readline
= PyObject_GetAttr(f
, readline_str
)) &&
5436 (self
->read
= PyObject_GetAttr(f
, read_str
)))) {
5438 PyErr_SetString( PyExc_TypeError
,
5439 "argument must have 'read' and "
5440 "'readline' attributes" );
5444 PyObject_GC_Track(self
);
5449 Py_DECREF((PyObject
*)self
);
5455 get_Unpickler(PyObject
*self
, PyObject
*file
)
5457 return (PyObject
*)newUnpicklerobject(file
);
5462 Unpickler_dealloc(Unpicklerobject
*self
)
5464 PyObject_GC_UnTrack((PyObject
*)self
);
5465 Py_XDECREF(self
->readline
);
5466 Py_XDECREF(self
->read
);
5467 Py_XDECREF(self
->file
);
5468 Py_XDECREF(self
->memo
);
5469 Py_XDECREF(self
->stack
);
5470 Py_XDECREF(self
->pers_func
);
5471 Py_XDECREF(self
->arg
);
5472 Py_XDECREF(self
->last_string
);
5473 Py_XDECREF(self
->find_class
);
5479 if (self
->buf_size
) {
5483 Py_TYPE(self
)->tp_free((PyObject
*)self
);
5487 Unpickler_traverse(Unpicklerobject
*self
, visitproc visit
, void *arg
)
5489 Py_VISIT(self
->readline
);
5490 Py_VISIT(self
->read
);
5491 Py_VISIT(self
->file
);
5492 Py_VISIT(self
->memo
);
5493 Py_VISIT(self
->stack
);
5494 Py_VISIT(self
->pers_func
);
5495 Py_VISIT(self
->arg
);
5496 Py_VISIT(self
->last_string
);
5497 Py_VISIT(self
->find_class
);
5502 Unpickler_clear(Unpicklerobject
*self
)
5504 Py_CLEAR(self
->readline
);
5505 Py_CLEAR(self
->read
);
5506 Py_CLEAR(self
->file
);
5507 Py_CLEAR(self
->memo
);
5508 Py_CLEAR(self
->stack
);
5509 Py_CLEAR(self
->pers_func
);
5510 Py_CLEAR(self
->arg
);
5511 Py_CLEAR(self
->last_string
);
5512 Py_CLEAR(self
->find_class
);
5517 Unpickler_getattr(Unpicklerobject
*self
, char *name
)
5519 if (!strcmp(name
, "persistent_load")) {
5520 if (!self
->pers_func
) {
5521 PyErr_SetString(PyExc_AttributeError
, name
);
5525 Py_INCREF(self
->pers_func
);
5526 return self
->pers_func
;
5529 if (!strcmp(name
, "find_global")) {
5530 if (!self
->find_class
) {
5531 PyErr_SetString(PyExc_AttributeError
, name
);
5535 Py_INCREF(self
->find_class
);
5536 return self
->find_class
;
5539 if (!strcmp(name
, "memo")) {
5541 PyErr_SetString(PyExc_AttributeError
, name
);
5545 Py_INCREF(self
->memo
);
5549 if (!strcmp(name
, "UnpicklingError")) {
5550 Py_INCREF(UnpicklingError
);
5551 return UnpicklingError
;
5554 return Py_FindMethod(Unpickler_methods
, (PyObject
*)self
, name
);
5559 Unpickler_setattr(Unpicklerobject
*self
, char *name
, PyObject
*value
)
5562 if (!strcmp(name
, "persistent_load")) {
5563 Py_XDECREF(self
->pers_func
);
5564 self
->pers_func
= value
;
5569 if (!strcmp(name
, "find_global")) {
5570 Py_XDECREF(self
->find_class
);
5571 self
->find_class
= value
;
5577 PyErr_SetString(PyExc_TypeError
,
5578 "attribute deletion is not supported");
5582 if (strcmp(name
, "memo") == 0) {
5583 if (!PyDict_Check(value
)) {
5584 PyErr_SetString(PyExc_TypeError
,
5585 "memo must be a dictionary");
5588 Py_XDECREF(self
->memo
);
5594 PyErr_SetString(PyExc_AttributeError
, name
);
5598 /* ---------------------------------------------------------------------------
5599 * Module-level functions.
5602 /* dump(obj, file, protocol=0). */
5604 cpm_dump(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5606 static char *kwlist
[] = {"obj", "file", "protocol", NULL
};
5607 PyObject
*ob
, *file
, *res
= NULL
;
5608 Picklerobject
*pickler
= 0;
5611 if (!( PyArg_ParseTupleAndKeywords(args
, kwds
, "OO|i", kwlist
,
5612 &ob
, &file
, &proto
)))
5615 if (!( pickler
= newPicklerobject(file
, proto
)))
5618 if (dump(pickler
, ob
) < 0)
5625 Py_XDECREF(pickler
);
5631 /* dumps(obj, protocol=0). */
5633 cpm_dumps(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5635 static char *kwlist
[] = {"obj", "protocol", NULL
};
5636 PyObject
*ob
, *file
= 0, *res
= NULL
;
5637 Picklerobject
*pickler
= 0;
5640 if (!( PyArg_ParseTupleAndKeywords(args
, kwds
, "O|i:dumps", kwlist
,
5644 if (!( file
= PycStringIO
->NewOutput(128)))
5647 if (!( pickler
= newPicklerobject(file
, proto
)))
5650 if (dump(pickler
, ob
) < 0)
5653 res
= PycStringIO
->cgetvalue(file
);
5656 Py_XDECREF(pickler
);
5663 /* load(fileobj). */
5665 cpm_load(PyObject
*self
, PyObject
*ob
)
5667 Unpicklerobject
*unpickler
= 0;
5668 PyObject
*res
= NULL
;
5670 if (!( unpickler
= newUnpicklerobject(ob
)))
5673 res
= load(unpickler
);
5676 Py_XDECREF(unpickler
);
5684 cpm_loads(PyObject
*self
, PyObject
*args
)
5686 PyObject
*ob
, *file
= 0, *res
= NULL
;
5687 Unpicklerobject
*unpickler
= 0;
5689 if (!( PyArg_ParseTuple(args
, "S:loads", &ob
)))
5692 if (!( file
= PycStringIO
->NewInput(ob
)))
5695 if (!( unpickler
= newUnpicklerobject(file
)))
5698 res
= load(unpickler
);
5702 Py_XDECREF(unpickler
);
5708 PyDoc_STRVAR(Unpicklertype__doc__
,
5709 "Objects that know how to unpickle");
5711 static PyTypeObject Unpicklertype
= {
5712 PyVarObject_HEAD_INIT(NULL
, 0)
5713 "cPickle.Unpickler", /*tp_name*/
5714 sizeof(Unpicklerobject
), /*tp_basicsize*/
5716 (destructor
)Unpickler_dealloc
, /* tp_dealloc */
5718 (getattrfunc
)Unpickler_getattr
, /* tp_getattr */
5719 (setattrfunc
)Unpickler_setattr
, /* tp_setattr */
5722 0, /* tp_as_number */
5723 0, /* tp_as_sequence */
5724 0, /* tp_as_mapping */
5728 0, /* tp_getattro */
5729 0, /* tp_setattro */
5730 0, /* tp_as_buffer */
5731 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
5732 Unpicklertype__doc__
, /* tp_doc */
5733 (traverseproc
)Unpickler_traverse
, /* tp_traverse */
5734 (inquiry
)Unpickler_clear
, /* tp_clear */
5737 static struct PyMethodDef cPickle_methods
[] = {
5738 {"dump", (PyCFunction
)cpm_dump
, METH_VARARGS
| METH_KEYWORDS
,
5739 PyDoc_STR("dump(obj, file, protocol=0) -- "
5740 "Write an object in pickle format to the given file.\n"
5742 "See the Pickler docstring for the meaning of optional argument proto.")
5745 {"dumps", (PyCFunction
)cpm_dumps
, METH_VARARGS
| METH_KEYWORDS
,
5746 PyDoc_STR("dumps(obj, protocol=0) -- "
5747 "Return a string containing an object in pickle format.\n"
5749 "See the Pickler docstring for the meaning of optional argument proto.")
5752 {"load", (PyCFunction
)cpm_load
, METH_O
,
5753 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5755 {"loads", (PyCFunction
)cpm_loads
, METH_VARARGS
,
5756 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5758 {"Pickler", (PyCFunction
)get_Pickler
, METH_VARARGS
| METH_KEYWORDS
,
5759 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5761 "This takes a file-like object for writing a pickle data stream.\n"
5762 "The optional proto argument tells the pickler to use the given\n"
5763 "protocol; supported protocols are 0, 1, 2. The default\n"
5764 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5765 "only protocol that can be written to a file opened in text\n"
5766 "mode and read back successfully. When using a protocol higher\n"
5767 "than 0, make sure the file is opened in binary mode, both when\n"
5768 "pickling and unpickling.)\n"
5770 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5771 "more efficient than protocol 1.\n"
5773 "Specifying a negative protocol version selects the highest\n"
5774 "protocol version supported. The higher the protocol used, the\n"
5775 "more recent the version of Python needed to read the pickle\n"
5778 "The file parameter must have a write() method that accepts a single\n"
5779 "string argument. It can thus be an open file object, a StringIO\n"
5780 "object, or any other custom object that meets this interface.\n")
5783 {"Unpickler", (PyCFunction
)get_Unpickler
, METH_O
,
5784 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5790 init_stuff(PyObject
*module_dict
)
5792 PyObject
*copyreg
, *t
, *r
;
5794 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5796 if (PyType_Ready(&Unpicklertype
) < 0)
5798 if (PyType_Ready(&Picklertype
) < 0)
5801 INIT_STR(__class__
);
5802 INIT_STR(__getinitargs__
);
5804 INIT_STR(__getstate__
);
5805 INIT_STR(__setstate__
);
5808 INIT_STR(__reduce__
);
5809 INIT_STR(__reduce_ex__
);
5815 INIT_STR(dispatch_table
);
5817 if (!( copyreg
= PyImport_ImportModule("copy_reg")))
5820 /* This is special because we want to use a different
5821 one in restricted mode. */
5822 dispatch_table
= PyObject_GetAttr(copyreg
, dispatch_table_str
);
5823 if (!dispatch_table
) return -1;
5825 extension_registry
= PyObject_GetAttrString(copyreg
,
5826 "_extension_registry");
5827 if (!extension_registry
) return -1;
5829 inverted_registry
= PyObject_GetAttrString(copyreg
,
5830 "_inverted_registry");
5831 if (!inverted_registry
) return -1;
5833 extension_cache
= PyObject_GetAttrString(copyreg
,
5834 "_extension_cache");
5835 if (!extension_cache
) return -1;
5839 if (!(empty_tuple
= PyTuple_New(0)))
5842 two_tuple
= PyTuple_New(2);
5843 if (two_tuple
== NULL
)
5845 /* We use this temp container with no regard to refcounts, or to
5846 * keeping containees alive. Exempt from GC, because we don't
5847 * want anything looking at two_tuple() by magic.
5849 PyObject_GC_UnTrack(two_tuple
);
5852 if (!( t
=PyImport_ImportModule("__builtin__"))) return -1;
5853 if (PyDict_SetItemString(module_dict
, "__builtins__", t
) < 0)
5856 if (!( t
=PyDict_New())) return -1;
5857 if (!( r
=PyRun_String(
5858 "def __str__(self):\n"
5859 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5861 module_dict
, t
) )) return -1;
5864 PickleError
= PyErr_NewException("cPickle.PickleError", NULL
, t
);
5870 PicklingError
= PyErr_NewException("cPickle.PicklingError",
5875 if (!( t
=PyDict_New())) return -1;
5876 if (!( r
=PyRun_String(
5877 "def __str__(self):\n"
5879 " a=a and type(a[0]) or '(what)'\n"
5880 " return 'Cannot pickle %s objects' % a\n"
5882 module_dict
, t
) )) return -1;
5885 if (!( UnpickleableError
= PyErr_NewException(
5886 "cPickle.UnpickleableError", PicklingError
, t
)))
5891 if (!( UnpicklingError
= PyErr_NewException("cPickle.UnpicklingError",
5892 PickleError
, NULL
)))
5895 if (!( BadPickleGet
= PyErr_NewException("cPickle.BadPickleGet",
5896 UnpicklingError
, NULL
)))
5899 if (PyDict_SetItemString(module_dict
, "PickleError",
5903 if (PyDict_SetItemString(module_dict
, "PicklingError",
5907 if (PyDict_SetItemString(module_dict
, "UnpicklingError",
5908 UnpicklingError
) < 0)
5911 if (PyDict_SetItemString(module_dict
, "UnpickleableError",
5912 UnpickleableError
) < 0)
5915 if (PyDict_SetItemString(module_dict
, "BadPickleGet",
5924 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5925 #define PyMODINIT_FUNC void
5930 PyObject
*m
, *d
, *di
, *v
, *k
;
5932 char *rev
= "1.71"; /* XXX when does this change? */
5933 PyObject
*format_version
;
5934 PyObject
*compatible_formats
;
5936 /* XXX: Should mention that the pickle module will include the C
5937 XXX: optimized implementation automatically. */
5938 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5939 "Python 3.0", 2) < 0)
5942 Py_TYPE(&Picklertype
) = &PyType_Type
;
5943 Py_TYPE(&Unpicklertype
) = &PyType_Type
;
5944 Py_TYPE(&PdataType
) = &PyType_Type
;
5946 /* Initialize some pieces. We need to do this before module creation,
5947 * so we're forced to use a temporary dictionary. :(
5951 if (init_stuff(di
) < 0) return;
5953 /* Create the module and add the functions */
5954 m
= Py_InitModule4("cPickle", cPickle_methods
,
5955 cPickle_module_documentation
,
5956 (PyObject
*)NULL
,PYTHON_API_VERSION
);
5960 /* Add some symbolic constants to the module */
5961 d
= PyModule_GetDict(m
);
5962 v
= PyString_FromString(rev
);
5963 PyDict_SetItemString(d
, "__version__", v
);
5966 /* Copy data from di. Waaa. */
5967 for (i
=0; PyDict_Next(di
, &i
, &k
, &v
); ) {
5968 if (PyObject_SetItem(d
, k
, v
) < 0) {
5975 i
= PyModule_AddIntConstant(m
, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL
);
5979 /* These are purely informational; no code uses them. */
5980 /* File format version we write. */
5981 format_version
= PyString_FromString("2.0");
5982 /* Format versions we can read. */
5983 compatible_formats
= Py_BuildValue("[sssss]",
5984 "1.0", /* Original protocol 0 */
5985 "1.1", /* Protocol 0 + INST */
5986 "1.2", /* Original protocol 1 */
5987 "1.3", /* Protocol 1 + BINFLOAT */
5988 "2.0"); /* Original protocol 2 */
5989 PyDict_SetItemString(d
, "format_version", format_version
);
5990 PyDict_SetItemString(d
, "compatible_formats", compatible_formats
);
5991 Py_XDECREF(format_version
);
5992 Py_XDECREF(compatible_formats
);