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)
1177 if (self
->write_func(self
, &op
, 1) < 0)
1180 buf
= PyOS_double_to_string(x
, 'g', 17, 0, NULL
);
1186 if (self
->write_func(self
, buf
, strlen(buf
)) < 0)
1189 if (self
->write_func(self
, "\n", 1) < 0)
1203 save_string(Picklerobject
*self
, PyObject
*args
, int doput
)
1208 if ((size
= PyString_Size(args
)) < 0)
1214 static char string
= STRING
;
1216 if (!( repr
= PyObject_Repr(args
)))
1219 if ((len
= PyString_Size(repr
)) < 0)
1221 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1223 if (self
->write_func(self
, &string
, 1) < 0)
1226 if (self
->write_func(self
, repr_str
, len
) < 0)
1229 if (self
->write_func(self
, "\n", 1) < 0)
1238 if ((size
= PyString_Size(args
)) < 0)
1242 c_str
[0] = SHORT_BINSTRING
;
1246 else if (size
<= INT_MAX
) {
1247 c_str
[0] = BINSTRING
;
1248 for (i
= 1; i
< 5; i
++)
1249 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1253 return -1; /* string too large */
1255 if (self
->write_func(self
, c_str
, len
) < 0)
1258 if (size
> 128 && Pdata_Check(self
->file
)) {
1259 if (write_other(self
, NULL
, 0) < 0) return -1;
1260 PDATA_APPEND(self
->file
, args
, -1);
1263 if (self
->write_func(self
,
1265 (PyStringObject
*)args
),
1272 if (put(self
, args
) < 0)
1283 #ifdef Py_USING_UNICODE
1284 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1285 backslash and newline characters to \uXXXX escapes. */
1287 modified_EncodeRawUnicodeEscape(const Py_UNICODE
*s
, Py_ssize_t size
)
1293 static const char *hexdigit
= "0123456789abcdef";
1294 #ifdef Py_UNICODE_WIDE
1295 const Py_ssize_t expandsize
= 10;
1297 const Py_ssize_t expandsize
= 6;
1300 if (size
> PY_SSIZE_T_MAX
/ expandsize
)
1301 return PyErr_NoMemory();
1303 repr
= PyString_FromStringAndSize(NULL
, expandsize
* size
);
1309 p
= q
= PyString_AS_STRING(repr
);
1310 while (size
-- > 0) {
1311 Py_UNICODE ch
= *s
++;
1312 #ifdef Py_UNICODE_WIDE
1313 /* Map 32-bit characters to '\Uxxxxxxxx' */
1314 if (ch
>= 0x10000) {
1317 *p
++ = hexdigit
[(ch
>> 28) & 0xf];
1318 *p
++ = hexdigit
[(ch
>> 24) & 0xf];
1319 *p
++ = hexdigit
[(ch
>> 20) & 0xf];
1320 *p
++ = hexdigit
[(ch
>> 16) & 0xf];
1321 *p
++ = hexdigit
[(ch
>> 12) & 0xf];
1322 *p
++ = hexdigit
[(ch
>> 8) & 0xf];
1323 *p
++ = hexdigit
[(ch
>> 4) & 0xf];
1324 *p
++ = hexdigit
[ch
& 15];
1328 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1329 if (ch
>= 0xD800 && ch
< 0xDC00) {
1335 if (ch2
>= 0xDC00 && ch2
<= 0xDFFF) {
1336 ucs
= (((ch
& 0x03FF) << 10) | (ch2
& 0x03FF)) + 0x00010000;
1339 *p
++ = hexdigit
[(ucs
>> 28) & 0xf];
1340 *p
++ = hexdigit
[(ucs
>> 24) & 0xf];
1341 *p
++ = hexdigit
[(ucs
>> 20) & 0xf];
1342 *p
++ = hexdigit
[(ucs
>> 16) & 0xf];
1343 *p
++ = hexdigit
[(ucs
>> 12) & 0xf];
1344 *p
++ = hexdigit
[(ucs
>> 8) & 0xf];
1345 *p
++ = hexdigit
[(ucs
>> 4) & 0xf];
1346 *p
++ = hexdigit
[ucs
& 0xf];
1349 /* Fall through: isolated surrogates are copied as-is */
1354 /* Map 16-bit characters to '\uxxxx' */
1355 if (ch
>= 256 || ch
== '\\' || ch
== '\n') {
1358 *p
++ = hexdigit
[(ch
>> 12) & 0xf];
1359 *p
++ = hexdigit
[(ch
>> 8) & 0xf];
1360 *p
++ = hexdigit
[(ch
>> 4) & 0xf];
1361 *p
++ = hexdigit
[ch
& 15];
1363 /* Copy everything else as-is */
1368 _PyString_Resize(&repr
, p
- q
);
1373 save_unicode(Picklerobject
*self
, PyObject
*args
, int doput
)
1375 Py_ssize_t size
, len
;
1378 if (!PyUnicode_Check(args
))
1383 static char string
= UNICODE
;
1385 repr
= modified_EncodeRawUnicodeEscape(
1386 PyUnicode_AS_UNICODE(args
), PyUnicode_GET_SIZE(args
));
1390 if ((len
= PyString_Size(repr
)) < 0)
1392 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1394 if (self
->write_func(self
, &string
, 1) < 0)
1397 if (self
->write_func(self
, repr_str
, len
) < 0)
1400 if (self
->write_func(self
, "\n", 1) < 0)
1409 if (!( repr
= PyUnicode_AsUTF8String(args
)))
1412 if ((size
= PyString_Size(repr
)) < 0)
1415 return -1; /* string too large */
1417 c_str
[0] = BINUNICODE
;
1418 for (i
= 1; i
< 5; i
++)
1419 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1422 if (self
->write_func(self
, c_str
, len
) < 0)
1425 if (size
> 128 && Pdata_Check(self
->file
)) {
1426 if (write_other(self
, NULL
, 0) < 0)
1428 PDATA_APPEND(self
->file
, repr
, -1);
1431 if (self
->write_func(self
, PyString_AS_STRING(repr
),
1440 if (put(self
, args
) < 0)
1451 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1453 store_tuple_elements(Picklerobject
*self
, PyObject
*t
, int len
)
1456 int res
= -1; /* guilty until proved innocent */
1458 assert(PyTuple_Size(t
) == len
);
1460 for (i
= 0; i
< len
; i
++) {
1461 PyObject
*element
= PyTuple_GET_ITEM(t
, i
);
1463 if (element
== NULL
)
1465 if (save(self
, element
, 0) < 0)
1474 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1475 * used across protocols to minimize the space needed to pickle them.
1476 * Tuples are also the only builtin immutable type that can be recursive
1477 * (a tuple can be reached from itself), and that requires some subtle
1478 * magic so that it works in all cases. IOW, this is a long routine.
1481 save_tuple(Picklerobject
*self
, PyObject
*args
)
1483 PyObject
*py_tuple_id
= NULL
;
1487 static char tuple
= TUPLE
;
1488 static char pop
= POP
;
1489 static char pop_mark
= POP_MARK
;
1490 static char len2opcode
[] = {EMPTY_TUPLE
, TUPLE1
, TUPLE2
, TUPLE3
};
1492 if ((len
= PyTuple_Size(args
)) < 0)
1499 c_str
[0] = EMPTY_TUPLE
;
1507 if (self
->write_func(self
, c_str
, len
) >= 0)
1509 /* Don't memoize an empty tuple. */
1513 /* A non-empty tuple. */
1515 /* id(tuple) isn't in the memo now. If it shows up there after
1516 * saving the tuple elements, the tuple must be recursive, in
1517 * which case we'll pop everything we put on the stack, and fetch
1518 * its value from the memo.
1520 py_tuple_id
= PyLong_FromVoidPtr(args
);
1521 if (py_tuple_id
== NULL
)
1524 if (len
<= 3 && self
->proto
>= 2) {
1525 /* Use TUPLE{1,2,3} opcodes. */
1526 if (store_tuple_elements(self
, args
, len
) < 0)
1528 if (PyDict_GetItem(self
->memo
, py_tuple_id
)) {
1529 /* pop the len elements */
1530 for (i
= 0; i
< len
; ++i
)
1531 if (self
->write_func(self
, &pop
, 1) < 0)
1533 /* fetch from memo */
1534 if (get(self
, py_tuple_id
) < 0)
1539 /* Not recursive. */
1540 if (self
->write_func(self
, len2opcode
+ len
, 1) < 0)
1545 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1546 * Generate MARK elt1 elt2 ... TUPLE
1548 if (self
->write_func(self
, &MARKv
, 1) < 0)
1551 if (store_tuple_elements(self
, args
, len
) < 0)
1554 if (PyDict_GetItem(self
->memo
, py_tuple_id
)) {
1555 /* pop the stack stuff we pushed */
1557 if (self
->write_func(self
, &pop_mark
, 1) < 0)
1561 /* Note that we pop one more than len, to remove
1564 for (i
= 0; i
<= len
; i
++)
1565 if (self
->write_func(self
, &pop
, 1) < 0)
1568 /* fetch from memo */
1569 if (get(self
, py_tuple_id
) >= 0)
1574 /* Not recursive. */
1575 if (self
->write_func(self
, &tuple
, 1) < 0)
1579 if (put(self
, args
) >= 0)
1583 Py_XDECREF(py_tuple_id
);
1587 /* iter is an iterator giving items, and we batch up chunks of
1588 * MARK item item ... item APPENDS
1589 * opcode sequences. Calling code should have arranged to first create an
1590 * empty list, or list-like object, for the APPENDS to operate on.
1591 * Returns 0 on success, <0 on error.
1594 batch_list(Picklerobject
*self
, PyObject
*iter
)
1596 PyObject
*obj
= NULL
;
1597 PyObject
*firstitem
= NULL
;
1600 static char append
= APPEND
;
1601 static char appends
= APPENDS
;
1603 assert(iter
!= NULL
);
1605 if (self
->proto
== 0) {
1606 /* APPENDS isn't available; do one at a time. */
1608 obj
= PyIter_Next(iter
);
1610 if (PyErr_Occurred())
1614 i
= save(self
, obj
, 0);
1618 if (self
->write_func(self
, &append
, 1) < 0)
1624 /* proto > 0: write in batches of BATCHSIZE. */
1626 /* Get first item */
1627 firstitem
= PyIter_Next(iter
);
1628 if (firstitem
== NULL
) {
1629 if (PyErr_Occurred())
1632 /* nothing more to add */
1636 /* Try to get a second item */
1637 obj
= PyIter_Next(iter
);
1639 if (PyErr_Occurred())
1642 /* Only one item to write */
1643 if (save(self
, firstitem
, 0) < 0)
1645 if (self
->write_func(self
, &append
, 1) < 0)
1647 Py_CLEAR(firstitem
);
1651 /* More than one item to write */
1653 /* Pump out MARK, items, APPENDS. */
1654 if (self
->write_func(self
, &MARKv
, 1) < 0)
1657 if (save(self
, firstitem
, 0) < 0)
1659 Py_CLEAR(firstitem
);
1662 /* Fetch and save up to BATCHSIZE items */
1664 if (save(self
, obj
, 0) < 0)
1672 obj
= PyIter_Next(iter
);
1674 if (PyErr_Occurred())
1680 if (self
->write_func(self
, &appends
, 1) < 0)
1683 } while (n
== BATCHSIZE
);
1687 Py_XDECREF(firstitem
);
1693 save_list(Picklerobject
*self
, PyObject
*args
)
1700 if (self
->fast
&& !fast_save_enter(self
, args
))
1703 /* Create an empty list. */
1714 if (self
->write_func(self
, s
, len
) < 0)
1717 /* Get list length, and bow out early if empty. */
1718 if ((len
= PyList_Size(args
)) < 0)
1723 if (put(self
, args
) >= 0)
1727 if (put2(self
, args
) < 0)
1730 /* Materialize the list elements. */
1731 iter
= PyObject_GetIter(args
);
1735 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1737 res
= batch_list(self
, iter
);
1738 Py_LeaveRecursiveCall();
1743 if (self
->fast
&& !fast_save_leave(self
, args
))
1750 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1751 * MARK key value ... key value SETITEMS
1752 * opcode sequences. Calling code should have arranged to first create an
1753 * empty dict, or dict-like object, for the SETITEMS to operate on.
1754 * Returns 0 on success, <0 on error.
1756 * This is very much like batch_list(). The difference between saving
1757 * elements directly, and picking apart two-tuples, is so long-winded at
1758 * the C level, though, that attempts to combine these routines were too
1762 batch_dict(Picklerobject
*self
, PyObject
*iter
)
1765 PyObject
*firstitem
= NULL
;
1768 static char setitem
= SETITEM
;
1769 static char setitems
= SETITEMS
;
1771 assert(iter
!= NULL
);
1773 if (self
->proto
== 0) {
1774 /* SETITEMS isn't available; do one at a time. */
1776 p
= PyIter_Next(iter
);
1778 if (PyErr_Occurred())
1782 if (!PyTuple_Check(p
) || PyTuple_Size(p
) != 2) {
1783 PyErr_SetString(PyExc_TypeError
, "dict items "
1784 "iterator must return 2-tuples");
1787 i
= save(self
, PyTuple_GET_ITEM(p
, 0), 0);
1789 i
= save(self
, PyTuple_GET_ITEM(p
, 1), 0);
1793 if (self
->write_func(self
, &setitem
, 1) < 0)
1799 /* proto > 0: write in batches of BATCHSIZE. */
1801 /* Get first item */
1802 firstitem
= PyIter_Next(iter
);
1803 if (firstitem
== NULL
) {
1804 if (PyErr_Occurred())
1807 /* nothing more to add */
1810 if (!PyTuple_Check(firstitem
) || PyTuple_Size(firstitem
) != 2) {
1811 PyErr_SetString(PyExc_TypeError
, "dict items "
1812 "iterator must return 2-tuples");
1816 /* Try to get a second item */
1817 p
= PyIter_Next(iter
);
1819 if (PyErr_Occurred())
1822 /* Only one item to write */
1823 if (save(self
, PyTuple_GET_ITEM(firstitem
, 0), 0) < 0)
1825 if (save(self
, PyTuple_GET_ITEM(firstitem
, 1), 0) < 0)
1827 if (self
->write_func(self
, &setitem
, 1) < 0)
1829 Py_CLEAR(firstitem
);
1833 /* More than one item to write */
1835 /* Pump out MARK, items, SETITEMS. */
1836 if (self
->write_func(self
, &MARKv
, 1) < 0)
1839 if (save(self
, PyTuple_GET_ITEM(firstitem
, 0), 0) < 0)
1841 if (save(self
, PyTuple_GET_ITEM(firstitem
, 1), 0) < 0)
1843 Py_CLEAR(firstitem
);
1846 /* Fetch and save up to BATCHSIZE items */
1848 if (!PyTuple_Check(p
) || PyTuple_Size(p
) != 2) {
1849 PyErr_SetString(PyExc_TypeError
, "dict items "
1850 "iterator must return 2-tuples");
1853 if (save(self
, PyTuple_GET_ITEM(p
, 0), 0) < 0)
1855 if (save(self
, PyTuple_GET_ITEM(p
, 1), 0) < 0)
1863 p
= PyIter_Next(iter
);
1865 if (PyErr_Occurred())
1871 if (self
->write_func(self
, &setitems
, 1) < 0)
1874 } while (n
== BATCHSIZE
);
1878 Py_XDECREF(firstitem
);
1883 /* This is a variant of batch_dict() above that specializes for dicts, with no
1884 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1885 * MARK key value ... key value SETITEMS
1886 * opcode sequences. Calling code should have arranged to first create an
1887 * empty dict, or dict-like object, for the SETITEMS to operate on.
1888 * Returns 0 on success, -1 on error.
1890 * Note that this currently doesn't work for protocol 0.
1893 batch_dict_exact(Picklerobject
*self
, PyObject
*obj
)
1895 PyObject
*key
= NULL
, *value
= NULL
;
1897 Py_ssize_t dict_size
, ppos
= 0;
1899 static char setitem
= SETITEM
;
1900 static char setitems
= SETITEMS
;
1902 assert(obj
!= NULL
);
1903 assert(self
->proto
> 0);
1905 dict_size
= PyDict_Size(obj
);
1907 /* Special-case len(d) == 1 to save space. */
1908 if (dict_size
== 1) {
1909 PyDict_Next(obj
, &ppos
, &key
, &value
);
1910 if (save(self
, key
, 0) < 0)
1912 if (save(self
, value
, 0) < 0)
1914 if (self
->write_func(self
, &setitem
, 1) < 0)
1919 /* Write in batches of BATCHSIZE. */
1922 if (self
->write_func(self
, &MARKv
, 1) < 0)
1924 while (PyDict_Next(obj
, &ppos
, &key
, &value
)) {
1925 if (save(self
, key
, 0) < 0)
1927 if (save(self
, value
, 0) < 0)
1929 if (++i
== BATCHSIZE
)
1932 if (self
->write_func(self
, &setitems
, 1) < 0)
1934 if (PyDict_Size(obj
) != dict_size
) {
1937 "dictionary changed size during iteration");
1941 } while (i
== BATCHSIZE
);
1946 save_dict(Picklerobject
*self
, PyObject
*args
)
1952 if (self
->fast
&& !fast_save_enter(self
, args
))
1955 /* Create an empty dict. */
1966 if (self
->write_func(self
, s
, len
) < 0)
1969 /* Get dict size, and bow out early if empty. */
1970 if ((len
= PyDict_Size(args
)) < 0)
1974 if (put(self
, args
) >= 0)
1978 if (put2(self
, args
) < 0)
1981 /* Materialize the dict items. */
1982 if (PyDict_CheckExact(args
) && self
->proto
> 0) {
1983 /* We can take certain shortcuts if we know this is a dict and
1984 not a dict subclass. */
1985 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1986 res
= batch_dict_exact(self
, args
);
1987 Py_LeaveRecursiveCall();
1990 PyObject
*iter
= PyObject_CallMethod(args
, "iteritems", "()");
1993 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1994 res
= batch_dict(self
, iter
);
1995 Py_LeaveRecursiveCall();
2001 if (self
->fast
&& !fast_save_leave(self
, args
))
2009 save_inst(Picklerobject
*self
, PyObject
*args
)
2011 PyObject
*class = 0, *module
= 0, *name
= 0, *state
= 0,
2012 *getinitargs_func
= 0, *getstate_func
= 0, *class_args
= 0;
2013 char *module_str
, *name_str
;
2014 int module_size
, name_size
, res
= -1;
2016 static char inst
= INST
, obj
= OBJ
, build
= BUILD
;
2018 if (self
->fast
&& !fast_save_enter(self
, args
))
2021 if (self
->write_func(self
, &MARKv
, 1) < 0)
2024 if (!( class = PyObject_GetAttr(args
, __class___str
)))
2028 if (save(self
, class, 0) < 0)
2032 if ((getinitargs_func
= PyObject_GetAttr(args
, __getinitargs___str
))) {
2033 PyObject
*element
= 0;
2037 PyObject_Call(getinitargs_func
, empty_tuple
, NULL
)))
2040 if ((len
= PyObject_Size(class_args
)) < 0)
2043 for (i
= 0; i
< len
; i
++) {
2044 if (!( element
= PySequence_GetItem(class_args
, i
)))
2047 if (save(self
, element
, 0) < 0) {
2056 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2063 if (!( name
= ((PyClassObject
*)class)->cl_name
)) {
2064 PyErr_SetString(PicklingError
, "class has no name");
2068 if (!( module
= whichmodule(class, name
)))
2072 if ((module_size
= PyString_Size(module
)) < 0 ||
2073 (name_size
= PyString_Size(name
)) < 0)
2076 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
2077 name_str
= PyString_AS_STRING((PyStringObject
*)name
);
2079 if (self
->write_func(self
, &inst
, 1) < 0)
2082 if (self
->write_func(self
, module_str
, module_size
) < 0)
2085 if (self
->write_func(self
, "\n", 1) < 0)
2088 if (self
->write_func(self
, name_str
, name_size
) < 0)
2091 if (self
->write_func(self
, "\n", 1) < 0)
2094 else if (self
->write_func(self
, &obj
, 1) < 0) {
2098 if ((getstate_func
= PyObject_GetAttr(args
, __getstate___str
))) {
2099 state
= PyObject_Call(getstate_func
, empty_tuple
, NULL
);
2104 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2109 if (!( state
= PyObject_GetAttr(args
, __dict___str
))) {
2110 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2119 if (!PyDict_Check(state
)) {
2120 if (put2(self
, args
) < 0)
2124 if (put(self
, args
) < 0)
2128 if (save(self
, state
, 0) < 0)
2131 if (self
->write_func(self
, &build
, 1) < 0)
2137 if (self
->fast
&& !fast_save_leave(self
, args
))
2143 Py_XDECREF(getinitargs_func
);
2144 Py_XDECREF(getstate_func
);
2145 Py_XDECREF(class_args
);
2152 save_global(Picklerobject
*self
, PyObject
*args
, PyObject
*name
)
2154 PyObject
*global_name
= 0, *module
= 0, *mod
= 0, *klass
= 0;
2155 char *name_str
, *module_str
;
2156 int module_size
, name_size
, res
= -1;
2158 static char global
= GLOBAL
;
2162 Py_INCREF(global_name
);
2165 if (!( global_name
= PyObject_GetAttr(args
, __name___str
)))
2169 if (!( module
= whichmodule(args
, global_name
)))
2172 if ((module_size
= PyString_Size(module
)) < 0 ||
2173 (name_size
= PyString_Size(global_name
)) < 0)
2176 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
2177 name_str
= PyString_AS_STRING((PyStringObject
*)global_name
);
2179 /* XXX This can be doing a relative import. Clearly it shouldn't,
2180 but I don't know how to stop it. :-( */
2181 mod
= PyImport_ImportModule(module_str
);
2183 cPickle_ErrFormat(PicklingError
,
2184 "Can't pickle %s: import of module %s "
2186 "OS", args
, module
);
2189 klass
= PyObject_GetAttrString(mod
, name_str
);
2190 if (klass
== NULL
) {
2191 cPickle_ErrFormat(PicklingError
,
2192 "Can't pickle %s: attribute lookup %s.%s "
2194 "OSS", args
, module
, global_name
);
2197 if (klass
!= args
) {
2199 cPickle_ErrFormat(PicklingError
,
2200 "Can't pickle %s: it's not the same object "
2202 "OSS", args
, module
, global_name
);
2207 if (self
->proto
>= 2) {
2208 /* See whether this is in the extension registry, and if
2209 * so generate an EXT opcode.
2211 PyObject
*py_code
; /* extension code as Python object */
2212 long code
; /* extension code as C value */
2216 PyTuple_SET_ITEM(two_tuple
, 0, module
);
2217 PyTuple_SET_ITEM(two_tuple
, 1, global_name
);
2218 py_code
= PyDict_GetItem(extension_registry
, two_tuple
);
2219 if (py_code
== NULL
)
2220 goto gen_global
; /* not registered */
2222 /* Verify py_code has the right type and value. */
2223 if (!PyInt_Check(py_code
)) {
2224 cPickle_ErrFormat(PicklingError
, "Can't pickle %s: "
2225 "extension code %s isn't an integer",
2226 "OO", args
, py_code
);
2229 code
= PyInt_AS_LONG(py_code
);
2230 if (code
<= 0 || code
> 0x7fffffffL
) {
2231 cPickle_ErrFormat(PicklingError
, "Can't pickle %s: "
2232 "extension code %ld is out of range",
2237 /* Generate an EXT opcode. */
2240 c_str
[1] = (char)code
;
2243 else if (code
<= 0xffff) {
2245 c_str
[1] = (char)(code
& 0xff);
2246 c_str
[2] = (char)((code
>> 8) & 0xff);
2251 c_str
[1] = (char)(code
& 0xff);
2252 c_str
[2] = (char)((code
>> 8) & 0xff);
2253 c_str
[3] = (char)((code
>> 16) & 0xff);
2254 c_str
[4] = (char)((code
>> 24) & 0xff);
2258 if (self
->write_func(self
, c_str
, n
) >= 0)
2260 goto finally
; /* and don't memoize */
2264 if (self
->write_func(self
, &global
, 1) < 0)
2267 if (self
->write_func(self
, module_str
, module_size
) < 0)
2270 if (self
->write_func(self
, "\n", 1) < 0)
2273 if (self
->write_func(self
, name_str
, name_size
) < 0)
2276 if (self
->write_func(self
, "\n", 1) < 0)
2279 if (put(self
, args
) < 0)
2286 Py_XDECREF(global_name
);
2293 save_pers(Picklerobject
*self
, PyObject
*args
, PyObject
*f
)
2298 static char persid
= PERSID
, binpersid
= BINPERSID
;
2301 ARG_TUP(self
, args
);
2303 pid
= PyObject_Call(f
, self
->arg
, NULL
);
2306 if (! pid
) return -1;
2308 if (pid
!= Py_None
) {
2310 if (!PyString_Check(pid
)) {
2311 PyErr_SetString(PicklingError
,
2312 "persistent id must be string");
2316 if (self
->write_func(self
, &persid
, 1) < 0)
2319 if ((size
= PyString_Size(pid
)) < 0)
2322 if (self
->write_func(self
,
2324 (PyStringObject
*)pid
),
2328 if (self
->write_func(self
, "\n", 1) < 0)
2334 else if (save(self
, pid
, 1) >= 0) {
2335 if (self
->write_func(self
, &binpersid
, 1) < 0)
2352 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2353 * appropriate __reduce__ method for ob.
2356 save_reduce(Picklerobject
*self
, PyObject
*args
, PyObject
*fn
, PyObject
*ob
)
2360 PyObject
*state
= NULL
;
2361 PyObject
*listitems
= Py_None
;
2362 PyObject
*dictitems
= Py_None
;
2365 int use_newobj
= self
->proto
>= 2;
2367 static char reduce
= REDUCE
;
2368 static char build
= BUILD
;
2369 static char newobj
= NEWOBJ
;
2371 size
= PyTuple_Size(args
);
2372 if (size
< 2 || size
> 5) {
2373 cPickle_ErrFormat(PicklingError
, "tuple returned by "
2374 "%s must contain 2 through 5 elements",
2379 if (! PyArg_UnpackTuple(args
, "save_reduce", 2, 5,
2387 if (!PyTuple_Check(argtup
)) {
2388 cPickle_ErrFormat(PicklingError
, "Second element of "
2389 "tuple returned by %s must be a tuple",
2394 if (state
== Py_None
)
2397 if (listitems
== Py_None
)
2399 else if (!PyIter_Check(listitems
)) {
2400 cPickle_ErrFormat(PicklingError
, "Fourth element of "
2401 "tuple returned by %s must be an iterator, not %s",
2402 "Os", fn
, Py_TYPE(listitems
)->tp_name
);
2406 if (dictitems
== Py_None
)
2408 else if (!PyIter_Check(dictitems
)) {
2409 cPickle_ErrFormat(PicklingError
, "Fifth element of "
2410 "tuple returned by %s must be an iterator, not %s",
2411 "Os", fn
, Py_TYPE(dictitems
)->tp_name
);
2415 /* Protocol 2 special case: if callable's name is __newobj__, use
2416 * NEWOBJ. This consumes a lot of code.
2419 PyObject
*temp
= PyObject_GetAttr(callable
, __name___str
);
2422 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2429 use_newobj
= PyString_Check(temp
) &&
2430 strcmp(PyString_AS_STRING(temp
),
2437 PyObject
*newargtup
;
2440 /* Sanity checks. */
2441 n
= PyTuple_Size(argtup
);
2443 PyErr_SetString(PicklingError
, "__newobj__ arglist "
2448 cls
= PyTuple_GET_ITEM(argtup
, 0);
2449 if (! PyObject_HasAttrString(cls
, "__new__")) {
2450 PyErr_SetString(PicklingError
, "args[0] from "
2451 "__newobj__ args has no __new__");
2455 /* XXX How could ob be NULL? */
2457 PyObject
*ob_dot_class
;
2459 ob_dot_class
= PyObject_GetAttr(ob
, __class___str
);
2460 if (ob_dot_class
== NULL
) {
2461 if (PyErr_ExceptionMatches(
2462 PyExc_AttributeError
))
2467 i
= ob_dot_class
!= cls
; /* true iff a problem */
2468 Py_XDECREF(ob_dot_class
);
2470 PyErr_SetString(PicklingError
, "args[0] from "
2471 "__newobj__ args has the wrong class");
2476 /* Save the class and its __new__ arguments. */
2477 if (save(self
, cls
, 0) < 0)
2480 newargtup
= PyTuple_New(n
-1); /* argtup[1:] */
2481 if (newargtup
== NULL
)
2483 for (i
= 1; i
< n
; ++i
) {
2484 PyObject
*temp
= PyTuple_GET_ITEM(argtup
, i
);
2486 PyTuple_SET_ITEM(newargtup
, i
-1, temp
);
2488 i
= save(self
, newargtup
, 0);
2489 Py_DECREF(newargtup
);
2493 /* Add NEWOBJ opcode. */
2494 if (self
->write_func(self
, &newobj
, 1) < 0)
2498 /* Not using NEWOBJ. */
2499 if (save(self
, callable
, 0) < 0 ||
2500 save(self
, argtup
, 0) < 0 ||
2501 self
->write_func(self
, &reduce
, 1) < 0)
2506 /* XXX How can ob be NULL? */
2508 if (state
&& !PyDict_Check(state
)) {
2509 if (put2(self
, ob
) < 0)
2512 else if (put(self
, ob
) < 0)
2517 if (listitems
&& batch_list(self
, listitems
) < 0)
2520 if (dictitems
&& batch_dict(self
, dictitems
) < 0)
2524 if (save(self
, state
, 0) < 0 ||
2525 self
->write_func(self
, &build
, 1) < 0)
2533 save(Picklerobject
*self
, PyObject
*args
, int pers_save
)
2536 PyObject
*py_ob_id
= 0, *__reduce__
= 0, *t
= 0;
2540 if (Py_EnterRecursiveCall(" while pickling an object"))
2543 if (!pers_save
&& self
->pers_func
) {
2544 if ((tmp
= save_pers(self
, args
, self
->pers_func
)) != 0) {
2550 if (args
== Py_None
) {
2551 res
= save_none(self
, args
);
2555 type
= Py_TYPE(args
);
2557 switch (type
->tp_name
[0]) {
2559 if (args
== Py_False
|| args
== Py_True
) {
2560 res
= save_bool(self
, args
);
2565 if (type
== &PyInt_Type
) {
2566 res
= save_int(self
, args
);
2572 if (type
== &PyLong_Type
) {
2573 res
= save_long(self
, args
);
2579 if (type
== &PyFloat_Type
) {
2580 res
= save_float(self
, args
);
2586 if (type
== &PyTuple_Type
&& PyTuple_Size(args
) == 0) {
2587 res
= save_tuple(self
, args
);
2593 if ((type
== &PyString_Type
) && (PyString_GET_SIZE(args
) < 2)) {
2594 res
= save_string(self
, args
, 0);
2599 #ifdef Py_USING_UNICODE
2601 if ((type
== &PyUnicode_Type
) && (PyString_GET_SIZE(args
) < 2)) {
2602 res
= save_unicode(self
, args
, 0);
2609 if (Py_REFCNT(args
) > 1) {
2610 if (!( py_ob_id
= PyLong_FromVoidPtr(args
)))
2613 if (PyDict_GetItem(self
->memo
, py_ob_id
)) {
2614 if (get(self
, py_ob_id
) < 0)
2622 switch (type
->tp_name
[0]) {
2624 if (type
== &PyString_Type
) {
2625 res
= save_string(self
, args
, 1);
2630 #ifdef Py_USING_UNICODE
2632 if (type
== &PyUnicode_Type
) {
2633 res
= save_unicode(self
, args
, 1);
2640 if (type
== &PyTuple_Type
) {
2641 res
= save_tuple(self
, args
);
2644 if (type
== &PyType_Type
) {
2645 res
= save_global(self
, args
, NULL
);
2651 if (type
== &PyList_Type
) {
2652 res
= save_list(self
, args
);
2658 if (type
== &PyDict_Type
) {
2659 res
= save_dict(self
, args
);
2665 if (type
== &PyInstance_Type
) {
2666 res
= save_inst(self
, args
);
2672 if (type
== &PyClass_Type
) {
2673 res
= save_global(self
, args
, NULL
);
2679 if (type
== &PyFunction_Type
) {
2680 res
= save_global(self
, args
, NULL
);
2681 if (res
&& PyErr_ExceptionMatches(PickleError
)) {
2682 /* fall back to reduce */
2691 if (type
== &PyCFunction_Type
) {
2692 res
= save_global(self
, args
, NULL
);
2697 if (!pers_save
&& self
->inst_pers_func
) {
2698 if ((tmp
= save_pers(self
, args
, self
->inst_pers_func
)) != 0) {
2704 if (PyType_IsSubtype(type
, &PyType_Type
)) {
2705 res
= save_global(self
, args
, NULL
);
2709 /* Get a reduction callable, and call it. This may come from
2710 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2711 * or the object's __reduce__ method.
2713 __reduce__
= PyDict_GetItem(dispatch_table
, (PyObject
*)type
);
2714 if (__reduce__
!= NULL
) {
2715 Py_INCREF(__reduce__
);
2717 ARG_TUP(self
, args
);
2719 t
= PyObject_Call(__reduce__
, self
->arg
, NULL
);
2724 /* Check for a __reduce_ex__ method. */
2725 __reduce__
= PyObject_GetAttr(args
, __reduce_ex___str
);
2726 if (__reduce__
!= NULL
) {
2727 t
= PyInt_FromLong(self
->proto
);
2732 t
= PyObject_Call(__reduce__
,
2739 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
2743 /* Check for a __reduce__ method. */
2744 __reduce__
= PyObject_GetAttr(args
, __reduce___str
);
2745 if (__reduce__
!= NULL
) {
2746 t
= PyObject_Call(__reduce__
,
2750 PyErr_SetObject(UnpickleableError
, args
);
2759 if (PyString_Check(t
)) {
2760 res
= save_global(self
, args
, t
);
2764 if (!PyTuple_Check(t
)) {
2765 cPickle_ErrFormat(PicklingError
, "Value returned by "
2766 "%s must be string or tuple",
2771 res
= save_reduce(self
, t
, __reduce__
, args
);
2774 Py_LeaveRecursiveCall();
2775 Py_XDECREF(py_ob_id
);
2776 Py_XDECREF(__reduce__
);
2784 dump(Picklerobject
*self
, PyObject
*args
)
2786 static char stop
= STOP
;
2788 if (self
->proto
>= 2) {
2792 assert(self
->proto
>= 0 && self
->proto
< 256);
2793 bytes
[1] = (char)self
->proto
;
2794 if (self
->write_func(self
, bytes
, 2) < 0)
2798 if (save(self
, args
, 0) < 0)
2801 if (self
->write_func(self
, &stop
, 1) < 0)
2804 if (self
->write_func(self
, NULL
, 0) < 0)
2811 Pickle_clear_memo(Picklerobject
*self
, PyObject
*args
)
2814 PyDict_Clear(self
->memo
);
2820 Pickle_getvalue(Picklerobject
*self
, PyObject
*args
)
2822 int l
, i
, rsize
, ssize
, clear
=1, lm
;
2825 char *s
, *p
, *have_get
;
2828 /* Can be called by Python code or C code */
2829 if (args
&& !PyArg_ParseTuple(args
, "|i:getvalue", &clear
))
2832 /* Check to make sure we are based on a list */
2833 if (! Pdata_Check(self
->file
)) {
2834 PyErr_SetString(PicklingError
,
2835 "Attempt to getvalue() a non-list-based pickler");
2839 /* flush write buffer */
2840 if (write_other(self
, NULL
, 0) < 0) return NULL
;
2842 data
=(Pdata
*)self
->file
;
2845 /* set up an array to hold get/put status */
2846 lm
= PyDict_Size(self
->memo
);
2847 if (lm
< 0) return NULL
;
2849 have_get
= malloc(lm
);
2850 if (have_get
== NULL
) return PyErr_NoMemory();
2851 memset(have_get
, 0, lm
);
2853 /* Scan for gets. */
2854 for (rsize
= 0, i
= l
; --i
>= 0; ) {
2857 if (PyString_Check(k
))
2858 rsize
+= PyString_GET_SIZE(k
);
2860 else if (PyInt_Check(k
)) { /* put */
2861 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2862 if (ik
>= lm
|| ik
== 0) {
2863 PyErr_SetString(PicklingError
,
2864 "Invalid get data");
2867 if (have_get
[ik
]) /* with matching get */
2868 rsize
+= ik
< 256 ? 2 : 5;
2871 else if (! (PyTuple_Check(k
) &&
2872 PyTuple_GET_SIZE(k
) == 2 &&
2873 PyInt_Check((k
= PyTuple_GET_ITEM(k
, 0))))
2875 PyErr_SetString(PicklingError
,
2876 "Unexpected data in internal list");
2881 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2882 if (ik
>= lm
|| ik
== 0) {
2883 PyErr_SetString(PicklingError
,
2884 "Invalid get data");
2888 rsize
+= ik
< 256 ? 2 : 5;
2892 /* Now generate the result */
2893 r
= PyString_FromStringAndSize(NULL
, rsize
);
2894 if (r
== NULL
) goto err
;
2895 s
= PyString_AS_STRING((PyStringObject
*)r
);
2897 for (i
= 0; i
< l
; i
++) {
2900 if (PyString_Check(k
)) {
2901 ssize
= PyString_GET_SIZE(k
);
2903 p
=PyString_AS_STRING((PyStringObject
*)k
);
2904 while (--ssize
>= 0)
2909 else if (PyTuple_Check(k
)) { /* get */
2910 ik
= PyInt_AS_LONG((PyIntObject
*)
2911 PyTuple_GET_ITEM(k
, 0));
2914 *s
++ = (int)(ik
& 0xff);
2918 *s
++ = (int)(ik
& 0xff);
2919 *s
++ = (int)((ik
>> 8) & 0xff);
2920 *s
++ = (int)((ik
>> 16) & 0xff);
2921 *s
++ = (int)((ik
>> 24) & 0xff);
2926 ik
= PyInt_AS_LONG((PyIntObject
*)k
);
2928 if (have_get
[ik
]) { /* with matching get */
2931 *s
++ = (int)(ik
& 0xff);
2935 *s
++ = (int)(ik
& 0xff);
2936 *s
++ = (int)((ik
>> 8) & 0xff);
2937 *s
++ = (int)((ik
>> 16) & 0xff);
2938 *s
++ = (int)((ik
>> 24) & 0xff);
2945 PyDict_Clear(self
->memo
);
2946 Pdata_clear(data
, 0);
2957 Pickler_dump(Picklerobject
*self
, PyObject
*args
)
2962 if (!( PyArg_ParseTuple(args
, "O|i:dump", &ob
, &get
)))
2965 if (dump(self
, ob
) < 0)
2968 if (get
) return Pickle_getvalue(self
, NULL
);
2970 /* XXX Why does dump() return self? */
2972 return (PyObject
*)self
;
2976 static struct PyMethodDef Pickler_methods
[] =
2978 {"dump", (PyCFunction
)Pickler_dump
, METH_VARARGS
,
2979 PyDoc_STR("dump(object) -- "
2980 "Write an object in pickle format to the object's pickle stream")},
2981 {"clear_memo", (PyCFunction
)Pickle_clear_memo
, METH_NOARGS
,
2982 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2983 {"getvalue", (PyCFunction
)Pickle_getvalue
, METH_VARARGS
,
2984 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2985 {NULL
, NULL
} /* sentinel */
2989 static Picklerobject
*
2990 newPicklerobject(PyObject
*file
, int proto
)
2992 Picklerobject
*self
;
2995 proto
= HIGHEST_PROTOCOL
;
2996 if (proto
> HIGHEST_PROTOCOL
) {
2997 PyErr_Format(PyExc_ValueError
, "pickle protocol %d asked for; "
2998 "the highest available protocol is %d",
2999 proto
, HIGHEST_PROTOCOL
);
3003 self
= PyObject_GC_New(Picklerobject
, &Picklertype
);
3006 self
->proto
= proto
;
3007 self
->bin
= proto
> 0;
3012 self
->pers_func
= NULL
;
3013 self
->inst_pers_func
= NULL
;
3014 self
->write_buf
= NULL
;
3016 self
->fast_container
= 0;
3017 self
->fast_memo
= NULL
;
3019 self
->dispatch_table
= NULL
;
3031 if (!( self
->memo
= PyDict_New()))
3034 if (PyFile_Check(file
)) {
3035 self
->fp
= PyFile_AsFile(file
);
3036 if (self
->fp
== NULL
) {
3037 PyErr_SetString(PyExc_ValueError
,
3038 "I/O operation on closed file");
3041 self
->write_func
= write_file
;
3043 else if (PycStringIO_OutputCheck(file
)) {
3044 self
->write_func
= write_cStringIO
;
3046 else if (file
== Py_None
) {
3047 self
->write_func
= write_none
;
3050 self
->write_func
= write_other
;
3052 if (! Pdata_Check(file
)) {
3053 self
->write
= PyObject_GetAttr(file
, write_str
);
3056 PyErr_SetString(PyExc_TypeError
,
3057 "argument must have 'write' "
3063 self
->write_buf
= (char *)PyMem_Malloc(WRITE_BUF_SIZE
);
3064 if (self
->write_buf
== NULL
) {
3070 if (PyEval_GetRestricted()) {
3071 /* Restricted execution, get private tables */
3072 PyObject
*m
= PyImport_Import(copyreg_str
);
3076 self
->dispatch_table
= PyObject_GetAttr(m
, dispatch_table_str
);
3078 if (self
->dispatch_table
== NULL
)
3082 self
->dispatch_table
= dispatch_table
;
3083 Py_INCREF(dispatch_table
);
3085 PyObject_GC_Track(self
);
3096 get_Pickler(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
3098 static char *kwlist
[] = {"file", "protocol", NULL
};
3099 PyObject
*file
= NULL
;
3103 * The documented signature is Pickler(file, protocol=0), but this
3104 * accepts Pickler() and Pickler(integer) too. The meaning then
3105 * is clear as mud, undocumented, and not supported by pickle.py.
3106 * I'm told Zope uses this, but I haven't traced into this code
3107 * far enough to figure out what it means.
3109 if (!PyArg_ParseTuple(args
, "|i:Pickler", &proto
)) {
3112 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|i:Pickler",
3113 kwlist
, &file
, &proto
))
3116 return (PyObject
*)newPicklerobject(file
, proto
);
3121 Pickler_dealloc(Picklerobject
*self
)
3123 PyObject_GC_UnTrack(self
);
3124 Py_XDECREF(self
->write
);
3125 Py_XDECREF(self
->memo
);
3126 Py_XDECREF(self
->fast_memo
);
3127 Py_XDECREF(self
->arg
);
3128 Py_XDECREF(self
->file
);
3129 Py_XDECREF(self
->pers_func
);
3130 Py_XDECREF(self
->inst_pers_func
);
3131 Py_XDECREF(self
->dispatch_table
);
3132 PyMem_Free(self
->write_buf
);
3133 Py_TYPE(self
)->tp_free((PyObject
*)self
);
3137 Pickler_traverse(Picklerobject
*self
, visitproc visit
, void *arg
)
3139 Py_VISIT(self
->write
);
3140 Py_VISIT(self
->memo
);
3141 Py_VISIT(self
->fast_memo
);
3142 Py_VISIT(self
->arg
);
3143 Py_VISIT(self
->file
);
3144 Py_VISIT(self
->pers_func
);
3145 Py_VISIT(self
->inst_pers_func
);
3146 Py_VISIT(self
->dispatch_table
);
3151 Pickler_clear(Picklerobject
*self
)
3153 Py_CLEAR(self
->write
);
3154 Py_CLEAR(self
->memo
);
3155 Py_CLEAR(self
->fast_memo
);
3156 Py_CLEAR(self
->arg
);
3157 Py_CLEAR(self
->file
);
3158 Py_CLEAR(self
->pers_func
);
3159 Py_CLEAR(self
->inst_pers_func
);
3160 Py_CLEAR(self
->dispatch_table
);
3165 Pickler_get_pers_func(Picklerobject
*p
)
3167 if (p
->pers_func
== NULL
)
3168 PyErr_SetString(PyExc_AttributeError
, "persistent_id");
3170 Py_INCREF(p
->pers_func
);
3171 return p
->pers_func
;
3175 Pickler_set_pers_func(Picklerobject
*p
, PyObject
*v
)
3178 PyErr_SetString(PyExc_TypeError
,
3179 "attribute deletion is not supported");
3182 Py_XDECREF(p
->pers_func
);
3189 Pickler_set_inst_pers_func(Picklerobject
*p
, PyObject
*v
)
3192 PyErr_SetString(PyExc_TypeError
,
3193 "attribute deletion is not supported");
3196 Py_XDECREF(p
->inst_pers_func
);
3198 p
->inst_pers_func
= v
;
3203 Pickler_get_memo(Picklerobject
*p
)
3205 if (p
->memo
== NULL
)
3206 PyErr_SetString(PyExc_AttributeError
, "memo");
3213 Pickler_set_memo(Picklerobject
*p
, PyObject
*v
)
3216 PyErr_SetString(PyExc_TypeError
,
3217 "attribute deletion is not supported");
3220 if (!PyDict_Check(v
)) {
3221 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
3224 Py_XDECREF(p
->memo
);
3231 Pickler_get_error(Picklerobject
*p
)
3233 /* why is this an attribute on the Pickler? */
3234 Py_INCREF(PicklingError
);
3235 return PicklingError
;
3238 static PyMemberDef Pickler_members
[] = {
3239 {"binary", T_INT
, offsetof(Picklerobject
, bin
)},
3240 {"fast", T_INT
, offsetof(Picklerobject
, fast
)},
3244 static PyGetSetDef Pickler_getsets
[] = {
3245 {"persistent_id", (getter
)Pickler_get_pers_func
,
3246 (setter
)Pickler_set_pers_func
},
3247 {"inst_persistent_id", NULL
, (setter
)Pickler_set_inst_pers_func
},
3248 {"memo", (getter
)Pickler_get_memo
, (setter
)Pickler_set_memo
},
3249 {"PicklingError", (getter
)Pickler_get_error
, NULL
},
3253 PyDoc_STRVAR(Picklertype__doc__
,
3254 "Objects that know how to pickle objects\n");
3256 static PyTypeObject Picklertype
= {
3257 PyVarObject_HEAD_INIT(NULL
, 0)
3258 "cPickle.Pickler", /*tp_name*/
3259 sizeof(Picklerobject
), /*tp_basicsize*/
3261 (destructor
)Pickler_dealloc
, /* tp_dealloc */
3267 0, /* tp_as_number */
3268 0, /* tp_as_sequence */
3269 0, /* tp_as_mapping */
3273 PyObject_GenericGetAttr
, /* tp_getattro */
3274 PyObject_GenericSetAttr
, /* tp_setattro */
3275 0, /* tp_as_buffer */
3276 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
3277 Picklertype__doc__
, /* tp_doc */
3278 (traverseproc
)Pickler_traverse
, /* tp_traverse */
3279 (inquiry
)Pickler_clear
, /* tp_clear */
3280 0, /* tp_richcompare */
3281 0, /* tp_weaklistoffset */
3283 0, /* tp_iternext */
3284 Pickler_methods
, /* tp_methods */
3285 Pickler_members
, /* tp_members */
3286 Pickler_getsets
, /* tp_getset */
3290 find_class(PyObject
*py_module_name
, PyObject
*py_global_name
, PyObject
*fc
)
3292 PyObject
*global
= 0, *module
;
3296 PyErr_SetString(UnpicklingError
, "Global and instance "
3297 "pickles are not supported.");
3300 return PyObject_CallFunctionObjArgs(fc
, py_module_name
,
3301 py_global_name
, NULL
);
3304 module
= PySys_GetObject("modules");
3308 module
= PyDict_GetItem(module
, py_module_name
);
3309 if (module
== NULL
) {
3310 module
= PyImport_Import(py_module_name
);
3313 global
= PyObject_GetAttr(module
, py_global_name
);
3317 global
= PyObject_GetAttr(module
, py_global_name
);
3322 marker(Unpicklerobject
*self
)
3324 if (self
->num_marks
< 1) {
3325 PyErr_SetString(UnpicklingError
, "could not find MARK");
3329 return self
->marks
[--self
->num_marks
];
3334 load_none(Unpicklerobject
*self
)
3336 PDATA_APPEND(self
->stack
, Py_None
, -1);
3343 PyErr_SetString(UnpicklingError
, "pickle data was truncated");
3348 load_int(Unpicklerobject
*self
)
3350 PyObject
*py_int
= 0;
3355 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3356 if (len
< 2) return bad_readline();
3357 if (!( s
=pystrndup(s
,len
))) return -1;
3360 l
= strtol(s
, &endptr
, 0);
3362 if (errno
|| (*endptr
!= '\n') || (endptr
[1] != '\0')) {
3363 /* Hm, maybe we've got something long. Let's try reading
3364 it as a Python long object. */
3366 py_int
= PyLong_FromString(s
, NULL
, 0);
3367 if (py_int
== NULL
) {
3368 PyErr_SetString(PyExc_ValueError
,
3369 "could not convert string to int");
3374 if (len
== 3 && (l
== 0 || l
== 1)) {
3375 if (!( py_int
= PyBool_FromLong(l
))) goto finally
;
3378 if (!( py_int
= PyInt_FromLong(l
))) goto finally
;
3383 PDATA_PUSH(self
->stack
, py_int
, -1);
3393 load_bool(Unpicklerobject
*self
, PyObject
*boolean
)
3395 assert(boolean
== Py_True
|| boolean
== Py_False
);
3396 PDATA_APPEND(self
->stack
, boolean
, -1);
3400 /* s contains x bytes of a little-endian integer. Return its value as a
3401 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3402 * int, but when x is 4 it's a signed one. This is an historical source
3403 * of x-platform bugs.
3406 calc_binint(char *s
, int x
)
3412 for (i
= 0, l
= 0L; i
< x
; i
++) {
3413 c
= (unsigned char)s
[i
];
3414 l
|= (long)c
<< (i
* 8);
3417 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3418 * is signed, so on a box with longs bigger than 4 bytes we need
3419 * to extend a BININT's sign bit to the full width.
3421 if (x
== 4 && l
& (1L << 31))
3429 load_binintx(Unpicklerobject
*self
, char *s
, int x
)
3431 PyObject
*py_int
= 0;
3434 l
= calc_binint(s
, x
);
3436 if (!( py_int
= PyInt_FromLong(l
)))
3439 PDATA_PUSH(self
->stack
, py_int
, -1);
3445 load_binint(Unpicklerobject
*self
)
3449 if (self
->read_func(self
, &s
, 4) < 0)
3452 return load_binintx(self
, s
, 4);
3457 load_binint1(Unpicklerobject
*self
)
3461 if (self
->read_func(self
, &s
, 1) < 0)
3464 return load_binintx(self
, s
, 1);
3469 load_binint2(Unpicklerobject
*self
)
3473 if (self
->read_func(self
, &s
, 2) < 0)
3476 return load_binintx(self
, s
, 2);
3480 load_long(Unpicklerobject
*self
)
3486 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3487 if (len
< 2) return bad_readline();
3488 if (!( s
=pystrndup(s
,len
))) return -1;
3490 if (!( l
= PyLong_FromString(s
, &end
, 0)))
3494 PDATA_PUSH(self
->stack
, l
, -1);
3503 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3507 load_counted_long(Unpicklerobject
*self
, int size
)
3511 unsigned char *pdata
;
3514 assert(size
== 1 || size
== 4);
3515 i
= self
->read_func(self
, &nbytes
, size
);
3516 if (i
< 0) return -1;
3518 size
= calc_binint(nbytes
, size
);
3520 /* Corrupt or hostile pickle -- we never write one like
3523 PyErr_SetString(UnpicklingError
, "LONG pickle has negative "
3529 along
= PyLong_FromLong(0L);
3531 /* Read the raw little-endian bytes & convert. */
3532 i
= self
->read_func(self
, (char **)&pdata
, size
);
3533 if (i
< 0) return -1;
3534 along
= _PyLong_FromByteArray(pdata
, (size_t)size
,
3535 1 /* little endian */, 1 /* signed */);
3539 PDATA_PUSH(self
->stack
, along
, -1);
3544 load_float(Unpicklerobject
*self
)
3546 PyObject
*py_float
= 0;
3551 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3552 if (len
< 2) return bad_readline();
3553 if (!( s
=pystrndup(s
,len
))) return -1;
3556 d
= PyOS_ascii_strtod(s
, &endptr
);
3558 if ((errno
== ERANGE
&& !(fabs(d
) <= 1.0)) ||
3559 (endptr
[0] != '\n') || (endptr
[1] != '\0')) {
3560 PyErr_SetString(PyExc_ValueError
,
3561 "could not convert string to float");
3565 if (!( py_float
= PyFloat_FromDouble(d
)))
3569 PDATA_PUSH(self
->stack
, py_float
, -1);
3579 load_binfloat(Unpicklerobject
*self
)
3585 if (self
->read_func(self
, &p
, 8) < 0)
3588 x
= _PyFloat_Unpack8((unsigned char *)p
, 0);
3589 if (x
== -1.0 && PyErr_Occurred())
3592 py_float
= PyFloat_FromDouble(x
);
3593 if (py_float
== NULL
)
3596 PDATA_PUSH(self
->stack
, py_float
, -1);
3601 load_string(Unpicklerobject
*self
)
3607 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3608 if (len
< 2) return bad_readline();
3609 if (!( s
=pystrndup(s
,len
))) return -1;
3612 /* Strip outermost quotes */
3613 while (s
[len
-1] <= ' ')
3615 if(s
[0]=='"' && s
[len
-1]=='"'){
3619 } else if(s
[0]=='\'' && s
[len
-1]=='\''){
3625 /********************************************/
3627 str
= PyString_DecodeEscape(p
, len
, NULL
, 0, NULL
);
3630 PDATA_PUSH(self
->stack
, str
, -1);
3637 PyErr_SetString(PyExc_ValueError
,"insecure string pickle");
3643 load_binstring(Unpicklerobject
*self
)
3645 PyObject
*py_string
= 0;
3649 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3651 l
= calc_binint(s
, 4);
3653 /* Corrupt or hostile pickle -- we never write one like
3656 PyErr_SetString(UnpicklingError
,
3657 "BINSTRING pickle has negative byte count");
3661 if (self
->read_func(self
, &s
, l
) < 0)
3664 if (!( py_string
= PyString_FromStringAndSize(s
, l
)))
3667 PDATA_PUSH(self
->stack
, py_string
, -1);
3673 load_short_binstring(Unpicklerobject
*self
)
3675 PyObject
*py_string
= 0;
3679 if (self
->read_func(self
, &s
, 1) < 0)
3682 l
= (unsigned char)s
[0];
3684 if (self
->read_func(self
, &s
, l
) < 0) return -1;
3686 if (!( py_string
= PyString_FromStringAndSize(s
, l
))) return -1;
3688 PDATA_PUSH(self
->stack
, py_string
, -1);
3693 #ifdef Py_USING_UNICODE
3695 load_unicode(Unpicklerobject
*self
)
3701 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3702 if (len
< 1) return bad_readline();
3704 if (!( str
= PyUnicode_DecodeRawUnicodeEscape(s
, len
- 1, NULL
)))
3707 PDATA_PUSH(self
->stack
, str
, -1);
3716 #ifdef Py_USING_UNICODE
3718 load_binunicode(Unpicklerobject
*self
)
3724 if (self
->read_func(self
, &s
, 4) < 0) return -1;
3726 l
= calc_binint(s
, 4);
3728 /* Corrupt or hostile pickle -- we never write one like
3731 PyErr_SetString(UnpicklingError
,
3732 "BINUNICODE pickle has negative byte count");
3736 if (self
->read_func(self
, &s
, l
) < 0)
3739 if (!( unicode
= PyUnicode_DecodeUTF8(s
, l
, NULL
)))
3742 PDATA_PUSH(self
->stack
, unicode
, -1);
3749 load_tuple(Unpicklerobject
*self
)
3754 if ((i
= marker(self
)) < 0) return -1;
3755 if (!( tup
=Pdata_popTuple(self
->stack
, i
))) return -1;
3756 PDATA_PUSH(self
->stack
, tup
, -1);
3761 load_counted_tuple(Unpicklerobject
*self
, int len
)
3763 PyObject
*tup
= PyTuple_New(len
);
3768 while (--len
>= 0) {
3771 PDATA_POP(self
->stack
, element
);
3772 if (element
== NULL
)
3774 PyTuple_SET_ITEM(tup
, len
, element
);
3776 PDATA_PUSH(self
->stack
, tup
, -1);
3781 load_empty_list(Unpicklerobject
*self
)
3785 if (!( list
=PyList_New(0))) return -1;
3786 PDATA_PUSH(self
->stack
, list
, -1);
3791 load_empty_dict(Unpicklerobject
*self
)
3795 if (!( dict
=PyDict_New())) return -1;
3796 PDATA_PUSH(self
->stack
, dict
, -1);
3802 load_list(Unpicklerobject
*self
)
3807 if ((i
= marker(self
)) < 0) return -1;
3808 if (!( list
=Pdata_popList(self
->stack
, i
))) return -1;
3809 PDATA_PUSH(self
->stack
, list
, -1);
3814 load_dict(Unpicklerobject
*self
)
3816 PyObject
*dict
, *key
, *value
;
3819 if ((i
= marker(self
)) < 0) return -1;
3820 j
=self
->stack
->length
;
3822 if (!( dict
= PyDict_New())) return -1;
3824 for (k
= i
+1; k
< j
; k
+= 2) {
3825 key
=self
->stack
->data
[k
-1];
3826 value
=self
->stack
->data
[k
];
3827 if (PyDict_SetItem(dict
, key
, value
) < 0) {
3832 Pdata_clear(self
->stack
, i
);
3833 PDATA_PUSH(self
->stack
, dict
, -1);
3838 Instance_New(PyObject
*cls
, PyObject
*args
)
3842 if (PyClass_Check(cls
)) {
3845 if ((l
=PyObject_Size(args
)) < 0) goto err
;
3847 PyObject
*__getinitargs__
;
3849 __getinitargs__
= PyObject_GetAttr(cls
,
3850 __getinitargs___str
);
3851 if (!__getinitargs__
) {
3852 /* We have a class with no __getinitargs__,
3853 so bypass usual construction */
3857 if (!( inst
=PyInstance_NewRaw(cls
, NULL
)))
3861 Py_DECREF(__getinitargs__
);
3864 if ((r
=PyInstance_New(cls
, args
, NULL
))) return r
;
3868 if ((r
=PyObject_CallObject(cls
, args
))) return r
;
3872 PyObject
*tp
, *v
, *tb
, *tmp_value
;
3874 PyErr_Fetch(&tp
, &v
, &tb
);
3876 /* NULL occurs when there was a KeyboardInterrupt */
3877 if (tmp_value
== NULL
)
3878 tmp_value
= Py_None
;
3879 if ((r
= PyTuple_Pack(3, tmp_value
, cls
, args
))) {
3883 PyErr_Restore(tp
,v
,tb
);
3890 load_obj(Unpicklerobject
*self
)
3892 PyObject
*class, *tup
, *obj
=0;
3895 if ((i
= marker(self
)) < 0) return -1;
3896 if (!( tup
=Pdata_popTuple(self
->stack
, i
+1))) return -1;
3897 PDATA_POP(self
->stack
, class);
3899 obj
= Instance_New(class, tup
);
3904 if (! obj
) return -1;
3905 PDATA_PUSH(self
->stack
, obj
, -1);
3911 load_inst(Unpicklerobject
*self
)
3913 PyObject
*tup
, *class=0, *obj
=0, *module_name
, *class_name
;
3917 if ((i
= marker(self
)) < 0) return -1;
3919 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
3920 if (len
< 2) return bad_readline();
3921 module_name
= PyString_FromStringAndSize(s
, len
- 1);
3922 if (!module_name
) return -1;
3924 if ((len
= self
->readline_func(self
, &s
)) >= 0) {
3925 if (len
< 2) return bad_readline();
3926 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3927 class = find_class(module_name
, class_name
,
3929 Py_DECREF(class_name
);
3932 Py_DECREF(module_name
);
3934 if (! class) return -1;
3936 if ((tup
=Pdata_popTuple(self
->stack
, i
))) {
3937 obj
= Instance_New(class, tup
);
3942 if (! obj
) return -1;
3944 PDATA_PUSH(self
->stack
, obj
, -1);
3949 load_newobj(Unpicklerobject
*self
)
3951 PyObject
*args
= NULL
;
3952 PyObject
*clsraw
= NULL
;
3953 PyTypeObject
*cls
; /* clsraw cast to its true type */
3956 /* Stack is ... cls argtuple, and we want to call
3957 * cls.__new__(cls, *argtuple).
3959 PDATA_POP(self
->stack
, args
);
3960 if (args
== NULL
) goto Fail
;
3961 if (! PyTuple_Check(args
)) {
3962 PyErr_SetString(UnpicklingError
, "NEWOBJ expected an arg "
3967 PDATA_POP(self
->stack
, clsraw
);
3968 cls
= (PyTypeObject
*)clsraw
;
3969 if (cls
== NULL
) goto Fail
;
3970 if (! PyType_Check(cls
)) {
3971 PyErr_SetString(UnpicklingError
, "NEWOBJ class argument "
3972 "isn't a type object");
3975 if (cls
->tp_new
== NULL
) {
3976 PyErr_SetString(UnpicklingError
, "NEWOBJ class argument "
3982 obj
= cls
->tp_new(cls
, args
, NULL
);
3983 if (obj
== NULL
) goto Fail
;
3987 PDATA_PUSH(self
->stack
, obj
, -1);
3997 load_global(Unpicklerobject
*self
)
3999 PyObject
*class = 0, *module_name
= 0, *class_name
= 0;
4003 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
4004 if (len
< 2) return bad_readline();
4005 module_name
= PyString_FromStringAndSize(s
, len
- 1);
4006 if (!module_name
) return -1;
4008 if ((len
= self
->readline_func(self
, &s
)) >= 0) {
4010 Py_DECREF(module_name
);
4011 return bad_readline();
4013 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
4014 class = find_class(module_name
, class_name
,
4016 Py_DECREF(class_name
);
4019 Py_DECREF(module_name
);
4021 if (! class) return -1;
4022 PDATA_PUSH(self
->stack
, class, -1);
4028 load_persid(Unpicklerobject
*self
)
4034 if (self
->pers_func
) {
4035 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
4036 if (len
< 2) return bad_readline();
4038 pid
= PyString_FromStringAndSize(s
, len
- 1);
4039 if (!pid
) return -1;
4041 if (PyList_Check(self
->pers_func
)) {
4042 if (PyList_Append(self
->pers_func
, pid
) < 0) {
4050 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
4056 if (! pid
) return -1;
4058 PDATA_PUSH(self
->stack
, pid
, -1);
4062 PyErr_SetString(UnpicklingError
,
4063 "A load persistent id instruction was encountered,\n"
4064 "but no persistent_load function was specified.");
4070 load_binpersid(Unpicklerobject
*self
)
4074 if (self
->pers_func
) {
4075 PDATA_POP(self
->stack
, pid
);
4076 if (! pid
) return -1;
4078 if (PyList_Check(self
->pers_func
)) {
4079 if (PyList_Append(self
->pers_func
, pid
) < 0) {
4087 pid
= PyObject_Call(self
->pers_func
, self
->arg
,
4091 if (! pid
) return -1;
4094 PDATA_PUSH(self
->stack
, pid
, -1);
4098 PyErr_SetString(UnpicklingError
,
4099 "A load persistent id instruction was encountered,\n"
4100 "but no persistent_load function was specified.");
4107 load_pop(Unpicklerobject
*self
)
4109 int len
= self
->stack
->length
;
4111 /* Note that we split the (pickle.py) stack into two stacks,
4112 an object stack and a mark stack. We have to be clever and
4113 pop the right one. We do this by looking at the top of the
4114 mark stack first, and only signalling a stack underflow if
4115 the object stack is empty and the mark stack doesn't match
4118 if (self
->num_marks
> 0 && self
->marks
[self
->num_marks
- 1] == len
) {
4120 } else if (len
>= 0) {
4122 Py_DECREF(self
->stack
->data
[len
]);
4123 self
->stack
->length
= len
;
4125 return stackUnderflow();
4132 load_pop_mark(Unpicklerobject
*self
)
4136 if ((i
= marker(self
)) < 0)
4139 Pdata_clear(self
->stack
, i
);
4146 load_dup(Unpicklerobject
*self
)
4151 if ((len
= self
->stack
->length
) <= 0) return stackUnderflow();
4152 last
=self
->stack
->data
[len
-1];
4154 PDATA_PUSH(self
->stack
, last
, -1);
4160 load_get(Unpicklerobject
*self
)
4162 PyObject
*py_str
= 0, *value
= 0;
4167 if ((len
= self
->readline_func(self
, &s
)) < 0) return -1;
4168 if (len
< 2) return bad_readline();
4170 if (!( py_str
= PyString_FromStringAndSize(s
, len
- 1))) return -1;
4172 value
= PyDict_GetItem(self
->memo
, py_str
);
4174 PyErr_SetObject(BadPickleGet
, py_str
);
4178 PDATA_APPEND(self
->stack
, value
, -1);
4188 load_binget(Unpicklerobject
*self
)
4190 PyObject
*py_key
= 0, *value
= 0;
4195 if (self
->read_func(self
, &s
, 1) < 0) return -1;
4197 key
= (unsigned char)s
[0];
4198 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
4200 value
= PyDict_GetItem(self
->memo
, py_key
);
4202 PyErr_SetObject(BadPickleGet
, py_key
);
4206 PDATA_APPEND(self
->stack
, value
, -1);
4216 load_long_binget(Unpicklerobject
*self
)
4218 PyObject
*py_key
= 0, *value
= 0;
4224 if (self
->read_func(self
, &s
, 4) < 0) return -1;
4226 c
= (unsigned char)s
[0];
4228 c
= (unsigned char)s
[1];
4229 key
|= (long)c
<< 8;
4230 c
= (unsigned char)s
[2];
4231 key
|= (long)c
<< 16;
4232 c
= (unsigned char)s
[3];
4233 key
|= (long)c
<< 24;
4235 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
4237 value
= PyDict_GetItem(self
->memo
, py_key
);
4239 PyErr_SetObject(BadPickleGet
, py_key
);
4243 PDATA_APPEND(self
->stack
, value
, -1);
4251 /* Push an object from the extension registry (EXT[124]). nbytes is
4252 * the number of bytes following the opcode, holding the index (code) value.
4255 load_extension(Unpicklerobject
*self
, int nbytes
)
4257 char *codebytes
; /* the nbytes bytes after the opcode */
4258 long code
; /* calc_binint returns long */
4259 PyObject
*py_code
; /* code as a Python int */
4260 PyObject
*obj
; /* the object to push */
4261 PyObject
*pair
; /* (module_name, class_name) */
4262 PyObject
*module_name
, *class_name
;
4264 assert(nbytes
== 1 || nbytes
== 2 || nbytes
== 4);
4265 if (self
->read_func(self
, &codebytes
, nbytes
) < 0) return -1;
4266 code
= calc_binint(codebytes
, nbytes
);
4267 if (code
<= 0) { /* note that 0 is forbidden */
4268 /* Corrupt or hostile pickle. */
4269 PyErr_SetString(UnpicklingError
, "EXT specifies code <= 0");
4273 /* Look for the code in the cache. */
4274 py_code
= PyInt_FromLong(code
);
4275 if (py_code
== NULL
) return -1;
4276 obj
= PyDict_GetItem(extension_cache
, py_code
);
4280 PDATA_APPEND(self
->stack
, obj
, -1);
4284 /* Look up the (module_name, class_name) pair. */
4285 pair
= PyDict_GetItem(inverted_registry
, py_code
);
4288 PyErr_Format(PyExc_ValueError
, "unregistered extension "
4292 /* Since the extension registry is manipulable via Python code,
4293 * confirm that pair is really a 2-tuple of strings.
4295 if (!PyTuple_Check(pair
) || PyTuple_Size(pair
) != 2 ||
4296 !PyString_Check(module_name
= PyTuple_GET_ITEM(pair
, 0)) ||
4297 !PyString_Check(class_name
= PyTuple_GET_ITEM(pair
, 1))) {
4299 PyErr_Format(PyExc_ValueError
, "_inverted_registry[%ld] "
4300 "isn't a 2-tuple of strings", code
);
4303 /* Load the object. */
4304 obj
= find_class(module_name
, class_name
, self
->find_class
);
4309 /* Cache code -> obj. */
4310 code
= PyDict_SetItem(extension_cache
, py_code
, obj
);
4316 PDATA_PUSH(self
->stack
, obj
, -1);
4321 load_put(Unpicklerobject
*self
)
4323 PyObject
*py_str
= 0, *value
= 0;
4327 if ((l
= self
->readline_func(self
, &s
)) < 0) return -1;
4328 if (l
< 2) return bad_readline();
4329 if (!( len
=self
->stack
->length
)) return stackUnderflow();
4330 if (!( py_str
= PyString_FromStringAndSize(s
, l
- 1))) return -1;
4331 value
=self
->stack
->data
[len
-1];
4332 l
=PyDict_SetItem(self
->memo
, py_str
, value
);
4339 load_binput(Unpicklerobject
*self
)
4341 PyObject
*py_key
= 0, *value
= 0;
4346 if (self
->read_func(self
, &s
, 1) < 0) return -1;
4347 if (!( (len
=self
->stack
->length
) > 0 )) return stackUnderflow();
4349 key
= (unsigned char)s
[0];
4351 if (!( py_key
= PyInt_FromLong((long)key
))) return -1;
4352 value
=self
->stack
->data
[len
-1];
4353 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
4360 load_long_binput(Unpicklerobject
*self
)
4362 PyObject
*py_key
= 0, *value
= 0;
4368 if (self
->read_func(self
, &s
, 4) < 0) return -1;
4369 if (!( len
=self
->stack
->length
)) return stackUnderflow();
4371 c
= (unsigned char)s
[0];
4373 c
= (unsigned char)s
[1];
4374 key
|= (long)c
<< 8;
4375 c
= (unsigned char)s
[2];
4376 key
|= (long)c
<< 16;
4377 c
= (unsigned char)s
[3];
4378 key
|= (long)c
<< 24;
4380 if (!( py_key
= PyInt_FromLong(key
))) return -1;
4381 value
=self
->stack
->data
[len
-1];
4382 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
4389 do_append(Unpicklerobject
*self
, int x
)
4391 PyObject
*value
= 0, *list
= 0, *append_method
= 0;
4394 len
=self
->stack
->length
;
4395 if (!( len
>= x
&& x
> 0 )) return stackUnderflow();
4397 if (len
==x
) return 0;
4399 list
=self
->stack
->data
[x
-1];
4401 if (PyList_Check(list
)) {
4405 slice
=Pdata_popList(self
->stack
, x
);
4406 if (! slice
) return -1;
4407 list_len
= PyList_GET_SIZE(list
);
4408 i
=PyList_SetSlice(list
, list_len
, list_len
, slice
);
4414 if (!( append_method
= PyObject_GetAttr(list
, append_str
)))
4417 for (i
= x
; i
< len
; i
++) {
4420 value
=self
->stack
->data
[i
];
4422 ARG_TUP(self
, value
);
4424 junk
= PyObject_Call(append_method
, self
->arg
,
4429 Pdata_clear(self
->stack
, i
+1);
4430 self
->stack
->length
=x
;
4431 Py_DECREF(append_method
);
4436 self
->stack
->length
=x
;
4437 Py_DECREF(append_method
);
4445 load_append(Unpicklerobject
*self
)
4447 return do_append(self
, self
->stack
->length
- 1);
4452 load_appends(Unpicklerobject
*self
)
4454 return do_append(self
, marker(self
));
4459 do_setitems(Unpicklerobject
*self
, int x
)
4461 PyObject
*value
= 0, *key
= 0, *dict
= 0;
4464 if (!( (len
=self
->stack
->length
) >= x
4465 && x
> 0 )) return stackUnderflow();
4467 dict
=self
->stack
->data
[x
-1];
4469 for (i
= x
+1; i
< len
; i
+= 2) {
4470 key
=self
->stack
->data
[i
-1];
4471 value
=self
->stack
->data
[i
];
4472 if (PyObject_SetItem(dict
, key
, value
) < 0) {
4478 Pdata_clear(self
->stack
, x
);
4485 load_setitem(Unpicklerobject
*self
)
4487 return do_setitems(self
, self
->stack
->length
- 2);
4491 load_setitems(Unpicklerobject
*self
)
4493 return do_setitems(self
, marker(self
));
4498 load_build(Unpicklerobject
*self
)
4500 PyObject
*state
, *inst
, *slotstate
;
4501 PyObject
*__setstate__
;
4502 PyObject
*d_key
, *d_value
;
4506 /* Stack is ... instance, state. We want to leave instance at
4507 * the stack top, possibly mutated via instance.__setstate__(state).
4509 if (self
->stack
->length
< 2)
4510 return stackUnderflow();
4511 PDATA_POP(self
->stack
, state
);
4514 inst
= self
->stack
->data
[self
->stack
->length
- 1];
4516 __setstate__
= PyObject_GetAttr(inst
, __setstate___str
);
4517 if (__setstate__
!= NULL
) {
4518 PyObject
*junk
= NULL
;
4520 /* The explicit __setstate__ is responsible for everything. */
4521 ARG_TUP(self
, state
);
4523 junk
= PyObject_Call(__setstate__
, self
->arg
, NULL
);
4526 Py_DECREF(__setstate__
);
4532 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4536 /* A default __setstate__. First see whether state embeds a
4537 * slot state dict too (a proto 2 addition).
4539 if (PyTuple_Check(state
) && PyTuple_Size(state
) == 2) {
4540 PyObject
*temp
= state
;
4541 state
= PyTuple_GET_ITEM(temp
, 0);
4542 slotstate
= PyTuple_GET_ITEM(temp
, 1);
4544 Py_INCREF(slotstate
);
4550 /* Set inst.__dict__ from the state dict (if any). */
4551 if (state
!= Py_None
) {
4553 if (! PyDict_Check(state
)) {
4554 PyErr_SetString(UnpicklingError
, "state is not a "
4558 dict
= PyObject_GetAttr(inst
, __dict___str
);
4563 while (PyDict_Next(state
, &i
, &d_key
, &d_value
)) {
4564 /* normally the keys for instance attributes are
4565 interned. we should try to do that here. */
4567 if (PyString_CheckExact(d_key
))
4568 PyString_InternInPlace(&d_key
);
4569 if (PyObject_SetItem(dict
, d_key
, d_value
) < 0) {
4578 /* Also set instance attributes from the slotstate dict (if any). */
4579 if (slotstate
!= NULL
) {
4580 if (! PyDict_Check(slotstate
)) {
4581 PyErr_SetString(UnpicklingError
, "slot state is not "
4586 while (PyDict_Next(slotstate
, &i
, &d_key
, &d_value
)) {
4587 if (PyObject_SetAttr(inst
, d_key
, d_value
) < 0)
4595 Py_XDECREF(slotstate
);
4601 load_mark(Unpicklerobject
*self
)
4605 /* Note that we split the (pickle.py) stack into two stacks, an
4606 object stack and a mark stack. Here we push a mark onto the
4610 if ((self
->num_marks
+ 1) >= self
->marks_size
) {
4612 s
=self
->marks_size
+20;
4613 if (s
<= self
->num_marks
) s
=self
->num_marks
+ 1;
4614 if (self
->marks
== NULL
)
4615 marks
=(int *)malloc(s
* sizeof(int));
4617 marks
=(int *)realloc(self
->marks
,
4623 self
->marks
= marks
;
4624 self
->marks_size
= s
;
4627 self
->marks
[self
->num_marks
++] = self
->stack
->length
;
4633 load_reduce(Unpicklerobject
*self
)
4635 PyObject
*callable
= 0, *arg_tup
= 0, *ob
= 0;
4637 PDATA_POP(self
->stack
, arg_tup
);
4638 if (! arg_tup
) return -1;
4639 PDATA_POP(self
->stack
, callable
);
4641 ob
= Instance_New(callable
, arg_tup
);
4642 Py_DECREF(callable
);
4646 if (! ob
) return -1;
4648 PDATA_PUSH(self
->stack
, ob
, -1);
4652 /* Just raises an error if we don't know the protocol specified. PROTO
4653 * is the first opcode for protocols >= 2.
4656 load_proto(Unpicklerobject
*self
)
4661 i
= self
->read_func(self
, &protobyte
, 1);
4665 i
= calc_binint(protobyte
, 1);
4666 /* No point checking for < 0, since calc_binint returns an unsigned
4667 * int when chewing on 1 byte.
4670 if (i
<= HIGHEST_PROTOCOL
)
4673 PyErr_Format(PyExc_ValueError
, "unsupported pickle protocol: %d", i
);
4678 load(Unpicklerobject
*self
)
4680 PyObject
*err
= 0, *val
= 0;
4683 self
->num_marks
= 0;
4684 if (self
->stack
->length
) Pdata_clear(self
->stack
, 0);
4687 if (self
->read_func(self
, &s
, 1) < 0)
4692 if (load_none(self
) < 0)
4697 if (load_binint(self
) < 0)
4702 if (load_binint1(self
) < 0)
4707 if (load_binint2(self
) < 0)
4712 if (load_int(self
) < 0)
4717 if (load_long(self
) < 0)
4722 if (load_counted_long(self
, 1) < 0)
4727 if (load_counted_long(self
, 4) < 0)
4732 if (load_float(self
) < 0)
4737 if (load_binfloat(self
) < 0)
4742 if (load_binstring(self
) < 0)
4746 case SHORT_BINSTRING
:
4747 if (load_short_binstring(self
) < 0)
4752 if (load_string(self
) < 0)
4756 #ifdef Py_USING_UNICODE
4758 if (load_unicode(self
) < 0)
4763 if (load_binunicode(self
) < 0)
4769 if (load_counted_tuple(self
, 0) < 0)
4774 if (load_counted_tuple(self
, 1) < 0)
4779 if (load_counted_tuple(self
, 2) < 0)
4784 if (load_counted_tuple(self
, 3) < 0)
4789 if (load_tuple(self
) < 0)
4794 if (load_empty_list(self
) < 0)
4799 if (load_list(self
) < 0)
4804 if (load_empty_dict(self
) < 0)
4809 if (load_dict(self
) < 0)
4814 if (load_obj(self
) < 0)
4819 if (load_inst(self
) < 0)
4824 if (load_newobj(self
) < 0)
4829 if (load_global(self
) < 0)
4834 if (load_append(self
) < 0)
4839 if (load_appends(self
) < 0)
4844 if (load_build(self
) < 0)
4849 if (load_dup(self
) < 0)
4854 if (load_binget(self
) < 0)
4859 if (load_long_binget(self
) < 0)
4864 if (load_get(self
) < 0)
4869 if (load_extension(self
, 1) < 0)
4874 if (load_extension(self
, 2) < 0)
4879 if (load_extension(self
, 4) < 0)
4883 if (load_mark(self
) < 0)
4888 if (load_binput(self
) < 0)
4893 if (load_long_binput(self
) < 0)
4898 if (load_put(self
) < 0)
4903 if (load_pop(self
) < 0)
4908 if (load_pop_mark(self
) < 0)
4913 if (load_setitem(self
) < 0)
4918 if (load_setitems(self
) < 0)
4926 if (load_persid(self
) < 0)
4931 if (load_binpersid(self
) < 0)
4936 if (load_reduce(self
) < 0)
4941 if (load_proto(self
) < 0)
4946 if (load_bool(self
, Py_True
) < 0)
4951 if (load_bool(self
, Py_False
) < 0)
4957 PyErr_SetNone(PyExc_EOFError
);
4961 cPickle_ErrFormat(UnpicklingError
,
4962 "invalid load key, '%s'.",
4970 if ((err
= PyErr_Occurred())) {
4971 if (err
== PyExc_EOFError
) {
4972 PyErr_SetNone(PyExc_EOFError
);
4977 PDATA_POP(self
->stack
, val
);
4982 /* No-load functions to support noload, which is used to
4983 find persistent references. */
4986 noload_obj(Unpicklerobject
*self
)
4990 if ((i
= marker(self
)) < 0) return -1;
4991 return Pdata_clear(self
->stack
, i
+1);
4996 noload_inst(Unpicklerobject
*self
)
5001 if ((i
= marker(self
)) < 0) return -1;
5002 Pdata_clear(self
->stack
, i
);
5003 if (self
->readline_func(self
, &s
) < 0) return -1;
5004 if (self
->readline_func(self
, &s
) < 0) return -1;
5005 PDATA_APPEND(self
->stack
, Py_None
, -1);
5010 noload_newobj(Unpicklerobject
*self
)
5014 PDATA_POP(self
->stack
, obj
); /* pop argtuple */
5015 if (obj
== NULL
) return -1;
5018 PDATA_POP(self
->stack
, obj
); /* pop cls */
5019 if (obj
== NULL
) return -1;
5022 PDATA_APPEND(self
->stack
, Py_None
, -1);
5027 noload_global(Unpicklerobject
*self
)
5031 if (self
->readline_func(self
, &s
) < 0) return -1;
5032 if (self
->readline_func(self
, &s
) < 0) return -1;
5033 PDATA_APPEND(self
->stack
, Py_None
,-1);
5038 noload_reduce(Unpicklerobject
*self
)
5041 if (self
->stack
->length
< 2) return stackUnderflow();
5042 Pdata_clear(self
->stack
, self
->stack
->length
-2);
5043 PDATA_APPEND(self
->stack
, Py_None
,-1);
5048 noload_build(Unpicklerobject
*self
) {
5050 if (self
->stack
->length
< 1) return stackUnderflow();
5051 Pdata_clear(self
->stack
, self
->stack
->length
-1);
5056 noload_extension(Unpicklerobject
*self
, int nbytes
)
5060 assert(nbytes
== 1 || nbytes
== 2 || nbytes
== 4);
5061 if (self
->read_func(self
, &codebytes
, nbytes
) < 0) return -1;
5062 PDATA_APPEND(self
->stack
, Py_None
, -1);
5067 noload_append(Unpicklerobject
*self
)
5069 return Pdata_clear(self
->stack
, self
->stack
->length
- 1);
5073 noload_appends(Unpicklerobject
*self
)
5076 if ((i
= marker(self
)) < 0) return -1;
5077 return Pdata_clear(self
->stack
, i
);
5081 noload_setitem(Unpicklerobject
*self
)
5083 return Pdata_clear(self
->stack
, self
->stack
->length
- 2);
5087 noload_setitems(Unpicklerobject
*self
)
5090 if ((i
= marker(self
)) < 0) return -1;
5091 return Pdata_clear(self
->stack
, i
);
5095 noload(Unpicklerobject
*self
)
5097 PyObject
*err
= 0, *val
= 0;
5100 self
->num_marks
= 0;
5101 Pdata_clear(self
->stack
, 0);
5104 if (self
->read_func(self
, &s
, 1) < 0)
5109 if (load_none(self
) < 0)
5114 if (load_binint(self
) < 0)
5119 if (load_binint1(self
) < 0)
5124 if (load_binint2(self
) < 0)
5129 if (load_int(self
) < 0)
5134 if (load_long(self
) < 0)
5139 if (load_counted_long(self
, 1) < 0)
5144 if (load_counted_long(self
, 4) < 0)
5149 if (load_float(self
) < 0)
5154 if (load_binfloat(self
) < 0)
5159 if (load_binstring(self
) < 0)
5163 case SHORT_BINSTRING
:
5164 if (load_short_binstring(self
) < 0)
5169 if (load_string(self
) < 0)
5173 #ifdef Py_USING_UNICODE
5175 if (load_unicode(self
) < 0)
5180 if (load_binunicode(self
) < 0)
5186 if (load_counted_tuple(self
, 0) < 0)
5191 if (load_counted_tuple(self
, 1) < 0)
5196 if (load_counted_tuple(self
, 2) < 0)
5201 if (load_counted_tuple(self
, 3) < 0)
5206 if (load_tuple(self
) < 0)
5211 if (load_empty_list(self
) < 0)
5216 if (load_list(self
) < 0)
5221 if (load_empty_dict(self
) < 0)
5226 if (load_dict(self
) < 0)
5231 if (noload_obj(self
) < 0)
5236 if (noload_inst(self
) < 0)
5241 if (noload_newobj(self
) < 0)
5246 if (noload_global(self
) < 0)
5251 if (noload_append(self
) < 0)
5256 if (noload_appends(self
) < 0)
5261 if (noload_build(self
) < 0)
5266 if (load_dup(self
) < 0)
5271 if (load_binget(self
) < 0)
5276 if (load_long_binget(self
) < 0)
5281 if (load_get(self
) < 0)
5286 if (noload_extension(self
, 1) < 0)
5291 if (noload_extension(self
, 2) < 0)
5296 if (noload_extension(self
, 4) < 0)
5301 if (load_mark(self
) < 0)
5306 if (load_binput(self
) < 0)
5311 if (load_long_binput(self
) < 0)
5316 if (load_put(self
) < 0)
5321 if (load_pop(self
) < 0)
5326 if (load_pop_mark(self
) < 0)
5331 if (noload_setitem(self
) < 0)
5336 if (noload_setitems(self
) < 0)
5344 if (load_persid(self
) < 0)
5349 if (load_binpersid(self
) < 0)
5354 if (noload_reduce(self
) < 0)
5359 if (load_proto(self
) < 0)
5364 if (load_bool(self
, Py_True
) < 0)
5369 if (load_bool(self
, Py_False
) < 0)
5373 cPickle_ErrFormat(UnpicklingError
,
5374 "invalid load key, '%s'.",
5382 if ((err
= PyErr_Occurred())) {
5383 if (err
== PyExc_EOFError
) {
5384 PyErr_SetNone(PyExc_EOFError
);
5389 PDATA_POP(self
->stack
, val
);
5395 Unpickler_load(Unpicklerobject
*self
, PyObject
*unused
)
5401 Unpickler_noload(Unpicklerobject
*self
, PyObject
*unused
)
5403 return noload(self
);
5407 static struct PyMethodDef Unpickler_methods
[] = {
5408 {"load", (PyCFunction
)Unpickler_load
, METH_NOARGS
,
5409 PyDoc_STR("load() -- Load a pickle")
5411 {"noload", (PyCFunction
)Unpickler_noload
, METH_NOARGS
,
5413 "noload() -- not load a pickle, but go through most of the motions\n"
5415 "This function can be used to read past a pickle without instantiating\n"
5416 "any objects or importing any modules. It can also be used to find all\n"
5417 "persistent references without instantiating any objects or importing\n"
5420 {NULL
, NULL
} /* sentinel */
5424 static Unpicklerobject
*
5425 newUnpicklerobject(PyObject
*f
)
5427 Unpicklerobject
*self
;
5429 if (!( self
= PyObject_GC_New(Unpicklerobject
, &Unpicklertype
)))
5434 self
->stack
= (Pdata
*)Pdata_New();
5435 self
->pers_func
= NULL
;
5436 self
->last_string
= NULL
;
5438 self
->num_marks
= 0;
5439 self
->marks_size
= 0;
5442 self
->readline
= NULL
;
5443 self
->find_class
= NULL
;
5445 if (!( self
->memo
= PyDict_New()))
5454 /* Set read, readline based on type of f */
5455 if (PyFile_Check(f
)) {
5456 self
->fp
= PyFile_AsFile(f
);
5457 if (self
->fp
== NULL
) {
5458 PyErr_SetString(PyExc_ValueError
,
5459 "I/O operation on closed file");
5462 self
->read_func
= read_file
;
5463 self
->readline_func
= readline_file
;
5465 else if (PycStringIO_InputCheck(f
)) {
5467 self
->read_func
= read_cStringIO
;
5468 self
->readline_func
= readline_cStringIO
;
5473 self
->read_func
= read_other
;
5474 self
->readline_func
= readline_other
;
5476 if (!( (self
->readline
= PyObject_GetAttr(f
, readline_str
)) &&
5477 (self
->read
= PyObject_GetAttr(f
, read_str
)))) {
5479 PyErr_SetString( PyExc_TypeError
,
5480 "argument must have 'read' and "
5481 "'readline' attributes" );
5485 PyObject_GC_Track(self
);
5490 Py_DECREF((PyObject
*)self
);
5496 get_Unpickler(PyObject
*self
, PyObject
*file
)
5498 return (PyObject
*)newUnpicklerobject(file
);
5503 Unpickler_dealloc(Unpicklerobject
*self
)
5505 PyObject_GC_UnTrack((PyObject
*)self
);
5506 Py_XDECREF(self
->readline
);
5507 Py_XDECREF(self
->read
);
5508 Py_XDECREF(self
->file
);
5509 Py_XDECREF(self
->memo
);
5510 Py_XDECREF(self
->stack
);
5511 Py_XDECREF(self
->pers_func
);
5512 Py_XDECREF(self
->arg
);
5513 Py_XDECREF(self
->last_string
);
5514 Py_XDECREF(self
->find_class
);
5520 if (self
->buf_size
) {
5524 Py_TYPE(self
)->tp_free((PyObject
*)self
);
5528 Unpickler_traverse(Unpicklerobject
*self
, visitproc visit
, void *arg
)
5530 Py_VISIT(self
->readline
);
5531 Py_VISIT(self
->read
);
5532 Py_VISIT(self
->file
);
5533 Py_VISIT(self
->memo
);
5534 Py_VISIT(self
->stack
);
5535 Py_VISIT(self
->pers_func
);
5536 Py_VISIT(self
->arg
);
5537 Py_VISIT(self
->last_string
);
5538 Py_VISIT(self
->find_class
);
5543 Unpickler_clear(Unpicklerobject
*self
)
5545 Py_CLEAR(self
->readline
);
5546 Py_CLEAR(self
->read
);
5547 Py_CLEAR(self
->file
);
5548 Py_CLEAR(self
->memo
);
5549 Py_CLEAR(self
->stack
);
5550 Py_CLEAR(self
->pers_func
);
5551 Py_CLEAR(self
->arg
);
5552 Py_CLEAR(self
->last_string
);
5553 Py_CLEAR(self
->find_class
);
5558 Unpickler_getattr(Unpicklerobject
*self
, char *name
)
5560 if (!strcmp(name
, "persistent_load")) {
5561 if (!self
->pers_func
) {
5562 PyErr_SetString(PyExc_AttributeError
, name
);
5566 Py_INCREF(self
->pers_func
);
5567 return self
->pers_func
;
5570 if (!strcmp(name
, "find_global")) {
5571 if (!self
->find_class
) {
5572 PyErr_SetString(PyExc_AttributeError
, name
);
5576 Py_INCREF(self
->find_class
);
5577 return self
->find_class
;
5580 if (!strcmp(name
, "memo")) {
5582 PyErr_SetString(PyExc_AttributeError
, name
);
5586 Py_INCREF(self
->memo
);
5590 if (!strcmp(name
, "UnpicklingError")) {
5591 Py_INCREF(UnpicklingError
);
5592 return UnpicklingError
;
5595 return Py_FindMethod(Unpickler_methods
, (PyObject
*)self
, name
);
5600 Unpickler_setattr(Unpicklerobject
*self
, char *name
, PyObject
*value
)
5603 if (!strcmp(name
, "persistent_load")) {
5604 Py_XDECREF(self
->pers_func
);
5605 self
->pers_func
= value
;
5610 if (!strcmp(name
, "find_global")) {
5611 Py_XDECREF(self
->find_class
);
5612 self
->find_class
= value
;
5618 PyErr_SetString(PyExc_TypeError
,
5619 "attribute deletion is not supported");
5623 if (strcmp(name
, "memo") == 0) {
5624 if (!PyDict_Check(value
)) {
5625 PyErr_SetString(PyExc_TypeError
,
5626 "memo must be a dictionary");
5629 Py_XDECREF(self
->memo
);
5635 PyErr_SetString(PyExc_AttributeError
, name
);
5639 /* ---------------------------------------------------------------------------
5640 * Module-level functions.
5643 /* dump(obj, file, protocol=0). */
5645 cpm_dump(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5647 static char *kwlist
[] = {"obj", "file", "protocol", NULL
};
5648 PyObject
*ob
, *file
, *res
= NULL
;
5649 Picklerobject
*pickler
= 0;
5652 if (!( PyArg_ParseTupleAndKeywords(args
, kwds
, "OO|i", kwlist
,
5653 &ob
, &file
, &proto
)))
5656 if (!( pickler
= newPicklerobject(file
, proto
)))
5659 if (dump(pickler
, ob
) < 0)
5666 Py_XDECREF(pickler
);
5672 /* dumps(obj, protocol=0). */
5674 cpm_dumps(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
5676 static char *kwlist
[] = {"obj", "protocol", NULL
};
5677 PyObject
*ob
, *file
= 0, *res
= NULL
;
5678 Picklerobject
*pickler
= 0;
5681 if (!( PyArg_ParseTupleAndKeywords(args
, kwds
, "O|i:dumps", kwlist
,
5685 if (!( file
= PycStringIO
->NewOutput(128)))
5688 if (!( pickler
= newPicklerobject(file
, proto
)))
5691 if (dump(pickler
, ob
) < 0)
5694 res
= PycStringIO
->cgetvalue(file
);
5697 Py_XDECREF(pickler
);
5704 /* load(fileobj). */
5706 cpm_load(PyObject
*self
, PyObject
*ob
)
5708 Unpicklerobject
*unpickler
= 0;
5709 PyObject
*res
= NULL
;
5711 if (!( unpickler
= newUnpicklerobject(ob
)))
5714 res
= load(unpickler
);
5717 Py_XDECREF(unpickler
);
5725 cpm_loads(PyObject
*self
, PyObject
*args
)
5727 PyObject
*ob
, *file
= 0, *res
= NULL
;
5728 Unpicklerobject
*unpickler
= 0;
5730 if (!( PyArg_ParseTuple(args
, "S:loads", &ob
)))
5733 if (!( file
= PycStringIO
->NewInput(ob
)))
5736 if (!( unpickler
= newUnpicklerobject(file
)))
5739 res
= load(unpickler
);
5743 Py_XDECREF(unpickler
);
5749 PyDoc_STRVAR(Unpicklertype__doc__
,
5750 "Objects that know how to unpickle");
5752 static PyTypeObject Unpicklertype
= {
5753 PyVarObject_HEAD_INIT(NULL
, 0)
5754 "cPickle.Unpickler", /*tp_name*/
5755 sizeof(Unpicklerobject
), /*tp_basicsize*/
5757 (destructor
)Unpickler_dealloc
, /* tp_dealloc */
5759 (getattrfunc
)Unpickler_getattr
, /* tp_getattr */
5760 (setattrfunc
)Unpickler_setattr
, /* tp_setattr */
5763 0, /* tp_as_number */
5764 0, /* tp_as_sequence */
5765 0, /* tp_as_mapping */
5769 0, /* tp_getattro */
5770 0, /* tp_setattro */
5771 0, /* tp_as_buffer */
5772 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
5773 Unpicklertype__doc__
, /* tp_doc */
5774 (traverseproc
)Unpickler_traverse
, /* tp_traverse */
5775 (inquiry
)Unpickler_clear
, /* tp_clear */
5778 static struct PyMethodDef cPickle_methods
[] = {
5779 {"dump", (PyCFunction
)cpm_dump
, METH_VARARGS
| METH_KEYWORDS
,
5780 PyDoc_STR("dump(obj, file, protocol=0) -- "
5781 "Write an object in pickle format to the given file.\n"
5783 "See the Pickler docstring for the meaning of optional argument proto.")
5786 {"dumps", (PyCFunction
)cpm_dumps
, METH_VARARGS
| METH_KEYWORDS
,
5787 PyDoc_STR("dumps(obj, protocol=0) -- "
5788 "Return a string containing an object in pickle format.\n"
5790 "See the Pickler docstring for the meaning of optional argument proto.")
5793 {"load", (PyCFunction
)cpm_load
, METH_O
,
5794 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5796 {"loads", (PyCFunction
)cpm_loads
, METH_VARARGS
,
5797 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5799 {"Pickler", (PyCFunction
)get_Pickler
, METH_VARARGS
| METH_KEYWORDS
,
5800 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5802 "This takes a file-like object for writing a pickle data stream.\n"
5803 "The optional proto argument tells the pickler to use the given\n"
5804 "protocol; supported protocols are 0, 1, 2. The default\n"
5805 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5806 "only protocol that can be written to a file opened in text\n"
5807 "mode and read back successfully. When using a protocol higher\n"
5808 "than 0, make sure the file is opened in binary mode, both when\n"
5809 "pickling and unpickling.)\n"
5811 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5812 "more efficient than protocol 1.\n"
5814 "Specifying a negative protocol version selects the highest\n"
5815 "protocol version supported. The higher the protocol used, the\n"
5816 "more recent the version of Python needed to read the pickle\n"
5819 "The file parameter must have a write() method that accepts a single\n"
5820 "string argument. It can thus be an open file object, a StringIO\n"
5821 "object, or any other custom object that meets this interface.\n")
5824 {"Unpickler", (PyCFunction
)get_Unpickler
, METH_O
,
5825 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5831 init_stuff(PyObject
*module_dict
)
5833 PyObject
*copyreg
, *t
, *r
;
5835 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5837 if (PyType_Ready(&Unpicklertype
) < 0)
5839 if (PyType_Ready(&Picklertype
) < 0)
5842 INIT_STR(__class__
);
5843 INIT_STR(__getinitargs__
);
5845 INIT_STR(__getstate__
);
5846 INIT_STR(__setstate__
);
5849 INIT_STR(__reduce__
);
5850 INIT_STR(__reduce_ex__
);
5856 INIT_STR(dispatch_table
);
5858 if (!( copyreg
= PyImport_ImportModule("copy_reg")))
5861 /* This is special because we want to use a different
5862 one in restricted mode. */
5863 dispatch_table
= PyObject_GetAttr(copyreg
, dispatch_table_str
);
5864 if (!dispatch_table
) return -1;
5866 extension_registry
= PyObject_GetAttrString(copyreg
,
5867 "_extension_registry");
5868 if (!extension_registry
) return -1;
5870 inverted_registry
= PyObject_GetAttrString(copyreg
,
5871 "_inverted_registry");
5872 if (!inverted_registry
) return -1;
5874 extension_cache
= PyObject_GetAttrString(copyreg
,
5875 "_extension_cache");
5876 if (!extension_cache
) return -1;
5880 if (!(empty_tuple
= PyTuple_New(0)))
5883 two_tuple
= PyTuple_New(2);
5884 if (two_tuple
== NULL
)
5886 /* We use this temp container with no regard to refcounts, or to
5887 * keeping containees alive. Exempt from GC, because we don't
5888 * want anything looking at two_tuple() by magic.
5890 PyObject_GC_UnTrack(two_tuple
);
5893 if (!( t
=PyImport_ImportModule("__builtin__"))) return -1;
5894 if (PyDict_SetItemString(module_dict
, "__builtins__", t
) < 0)
5897 if (!( t
=PyDict_New())) return -1;
5898 if (!( r
=PyRun_String(
5899 "def __str__(self):\n"
5900 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5902 module_dict
, t
) )) return -1;
5905 PickleError
= PyErr_NewException("cPickle.PickleError", NULL
, t
);
5911 PicklingError
= PyErr_NewException("cPickle.PicklingError",
5916 if (!( t
=PyDict_New())) return -1;
5917 if (!( r
=PyRun_String(
5918 "def __str__(self):\n"
5920 " a=a and type(a[0]) or '(what)'\n"
5921 " return 'Cannot pickle %s objects' % a\n"
5923 module_dict
, t
) )) return -1;
5926 if (!( UnpickleableError
= PyErr_NewException(
5927 "cPickle.UnpickleableError", PicklingError
, t
)))
5932 if (!( UnpicklingError
= PyErr_NewException("cPickle.UnpicklingError",
5933 PickleError
, NULL
)))
5936 if (!( BadPickleGet
= PyErr_NewException("cPickle.BadPickleGet",
5937 UnpicklingError
, NULL
)))
5940 if (PyDict_SetItemString(module_dict
, "PickleError",
5944 if (PyDict_SetItemString(module_dict
, "PicklingError",
5948 if (PyDict_SetItemString(module_dict
, "UnpicklingError",
5949 UnpicklingError
) < 0)
5952 if (PyDict_SetItemString(module_dict
, "UnpickleableError",
5953 UnpickleableError
) < 0)
5956 if (PyDict_SetItemString(module_dict
, "BadPickleGet",
5965 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5966 #define PyMODINIT_FUNC void
5971 PyObject
*m
, *d
, *di
, *v
, *k
;
5973 char *rev
= "1.71"; /* XXX when does this change? */
5974 PyObject
*format_version
;
5975 PyObject
*compatible_formats
;
5977 /* XXX: Should mention that the pickle module will include the C
5978 XXX: optimized implementation automatically. */
5979 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5980 "Python 3.0", 2) < 0)
5983 Py_TYPE(&Picklertype
) = &PyType_Type
;
5984 Py_TYPE(&Unpicklertype
) = &PyType_Type
;
5985 Py_TYPE(&PdataType
) = &PyType_Type
;
5987 /* Initialize some pieces. We need to do this before module creation,
5988 * so we're forced to use a temporary dictionary. :(
5992 if (init_stuff(di
) < 0) return;
5994 /* Create the module and add the functions */
5995 m
= Py_InitModule4("cPickle", cPickle_methods
,
5996 cPickle_module_documentation
,
5997 (PyObject
*)NULL
,PYTHON_API_VERSION
);
6001 /* Add some symbolic constants to the module */
6002 d
= PyModule_GetDict(m
);
6003 v
= PyString_FromString(rev
);
6004 PyDict_SetItemString(d
, "__version__", v
);
6007 /* Copy data from di. Waaa. */
6008 for (i
=0; PyDict_Next(di
, &i
, &k
, &v
); ) {
6009 if (PyObject_SetItem(d
, k
, v
) < 0) {
6016 i
= PyModule_AddIntConstant(m
, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL
);
6020 /* These are purely informational; no code uses them. */
6021 /* File format version we write. */
6022 format_version
= PyString_FromString("2.0");
6023 /* Format versions we can read. */
6024 compatible_formats
= Py_BuildValue("[sssss]",
6025 "1.0", /* Original protocol 0 */
6026 "1.1", /* Protocol 0 + INST */
6027 "1.2", /* Original protocol 1 */
6028 "1.3", /* Protocol 1 + BINFLOAT */
6029 "2.0"); /* Original protocol 2 */
6030 PyDict_SetItemString(d
, "format_version", format_version
);
6031 PyDict_SetItemString(d
, "compatible_formats", compatible_formats
);
6032 Py_XDECREF(format_version
);
6033 Py_XDECREF(compatible_formats
);