Issue 6340: update by Gregor Lingl of his tdemo_chaos demo program.
[python.git] / Modules / cPickle.c
blob486e86fc125484227b9d24fa9e8895424c1d3591
1 #include "Python.h"
2 #include "cStringIO.h"
3 #include "structmember.h"
5 PyDoc_STRVAR(cPickle_module_documentation,
6 "C implementation and optimization of the Python pickle module.");
8 #ifndef Py_eval_input
9 #include <graminit.h>
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.
24 #ifdef UNICODE
25 # undef UNICODE
26 #endif
29 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
30 * docs are in pickletools.py.
32 #define MARK '('
33 #define STOP '.'
34 #define POP '0'
35 #define POP_MARK '1'
36 #define DUP '2'
37 #define FLOAT 'F'
38 #define BINFLOAT 'G'
39 #define INT 'I'
40 #define BININT 'J'
41 #define BININT1 'K'
42 #define LONG 'L'
43 #define BININT2 'M'
44 #define NONE 'N'
45 #define PERSID 'P'
46 #define BINPERSID 'Q'
47 #define REDUCE 'R'
48 #define STRING 'S'
49 #define BINSTRING 'T'
50 #define SHORT_BINSTRING 'U'
51 #define UNICODE 'V'
52 #define BINUNICODE 'X'
53 #define APPEND 'a'
54 #define BUILD 'b'
55 #define GLOBAL 'c'
56 #define DICT 'd'
57 #define EMPTY_DICT '}'
58 #define APPENDS 'e'
59 #define GET 'g'
60 #define BINGET 'h'
61 #define INST 'i'
62 #define LONG_BINGET 'j'
63 #define LIST 'l'
64 #define EMPTY_LIST ']'
65 #define OBJ 'o'
66 #define PUT 'p'
67 #define BINPUT 'q'
68 #define LONG_BINPUT 'r'
69 #define SETITEM 's'
70 #define TUPLE 't'
71 #define EMPTY_TUPLE ')'
72 #define SETITEMS 'u'
74 /* Protocol 2. */
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.
93 #undef TRUE
94 #define TRUE "I01\n"
95 #undef FALSE
96 #define FALSE "I00\n"
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,
132 *__reduce_ex___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. */
140 typedef struct {
141 PyObject_HEAD
142 int length; /* number of initial slots in data currently used */
143 int size; /* number of slots in data allocated */
144 PyObject **data;
145 } Pdata;
147 static void
148 Pdata_dealloc(Pdata *self)
150 int i;
151 PyObject **p;
153 for (i = self->length, p = self->data; --i >= 0; p++) {
154 Py_DECREF(*p);
156 if (self->data)
157 free(self->data);
158 PyObject_Del(self);
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)
169 static PyObject *
170 Pdata_New(void)
172 Pdata *self;
174 if (!(self = PyObject_New(Pdata, &PdataType)))
175 return NULL;
176 self->size = 8;
177 self->length = 0;
178 self->data = malloc(self->size * sizeof(PyObject*));
179 if (self->data)
180 return (PyObject*)self;
181 Py_DECREF(self);
182 return PyErr_NoMemory();
185 static int
186 stackUnderflow(void)
188 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
189 return -1;
192 /* Retain only the initial clearto items. If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
195 static int
196 Pdata_clear(Pdata *self, int clearto)
198 int i;
199 PyObject **p;
201 if (clearto < 0) return stackUnderflow();
202 if (clearto >= self->length) return 0;
204 for (i = self->length, p = self->data + clearto;
205 --i >= clearto;
206 p++) {
207 Py_CLEAR(*p);
209 self->length = clearto;
211 return 0;
214 static int
215 Pdata_grow(Pdata *self)
217 int bigger;
218 size_t nbytes;
219 PyObject **tmp;
221 bigger = self->size << 1;
222 if (bigger <= 0) /* was 0, or new value overflows */
223 goto nomemory;
224 if ((int)(size_t)bigger != bigger)
225 goto nomemory;
226 nbytes = (size_t)bigger * sizeof(PyObject *);
227 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
228 goto nomemory;
229 tmp = realloc(self->data, nbytes);
230 if (tmp == NULL)
231 goto nomemory;
232 self->data = tmp;
233 self->size = bigger;
234 return 0;
236 nomemory:
237 PyErr_NoMemory();
238 return -1;
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) { \
246 if ((D)->length) \
247 (V) = (D)->data[--((D)->length)]; \
248 else { \
249 PyErr_SetString(UnpicklingError, "bad pickle data"); \
250 (V) = NULL; \
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) { \
266 Py_DECREF(O); \
267 return ER; \
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) \
276 return ER; \
277 Py_INCREF(O); \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
282 static PyObject *
283 Pdata_popTuple(Pdata *self, int start)
285 PyObject *r;
286 int i, j, l;
288 l = self->length-start;
289 r = PyTuple_New(l);
290 if (r == NULL)
291 return NULL;
292 for (i = start, j = 0 ; j < l; i++, j++)
293 PyTuple_SET_ITEM(r, j, self->data[i]);
295 self->length = start;
296 return r;
299 static PyObject *
300 Pdata_popList(Pdata *self, int start)
302 PyObject *r;
303 int i, j, l;
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]);
310 self->length=start;
311 return r;
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); \
321 else { \
322 Py_DECREF(o); \
326 #define FREE_ARG_TUP(self) { \
327 if (Py_REFCNT(self->arg) > 1) { \
328 Py_DECREF(self->arg); \
329 self->arg=NULL; \
333 typedef struct Picklerobject {
334 PyObject_HEAD
335 FILE *fp;
336 PyObject *write;
337 PyObject *file;
338 PyObject *memo;
339 PyObject *arg;
340 PyObject *pers_func;
341 PyObject *inst_pers_func;
343 /* pickle protocol number, >= 0 */
344 int proto;
346 /* bool, true if proto > 0 */
347 int bin;
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);
351 char *write_buf;
352 int buf_size;
353 PyObject *dispatch_table;
354 int fast_container; /* count nested container dumps */
355 PyObject *fast_memo;
356 } Picklerobject;
358 #ifndef PY_CPICKLE_FAST_LIMIT
359 #define PY_CPICKLE_FAST_LIMIT 50
360 #endif
362 static PyTypeObject Picklertype;
364 typedef struct Unpicklerobject {
365 PyObject_HEAD
366 FILE *fp;
367 PyObject *file;
368 PyObject *readline;
369 PyObject *read;
370 PyObject *memo;
371 PyObject *arg;
372 Pdata *stack;
373 PyObject *mark;
374 PyObject *pers_func;
375 PyObject *last_string;
376 int *marks;
377 int num_marks;
378 int marks_size;
379 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
380 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
381 int buf_size;
382 char *buf;
383 PyObject *find_class;
384 } Unpicklerobject;
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 *);
392 static
393 PyObject *
394 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
396 va_list va;
397 PyObject *args=0, *retval=0;
398 va_start(va, format);
400 if (format) args = Py_VaBuildValue(format, va);
401 va_end(va);
402 if (format && ! args) return NULL;
403 if (stringformat && !(retval=PyString_FromString(stringformat)))
404 return NULL;
406 if (retval) {
407 if (args) {
408 PyObject *v;
409 v=PyString_Format(retval, args);
410 Py_DECREF(retval);
411 Py_DECREF(args);
412 if (! v) return NULL;
413 retval=v;
416 else
417 if (args) retval=args;
418 else {
419 PyErr_SetObject(ErrType,Py_None);
420 return NULL;
422 PyErr_SetObject(ErrType,retval);
423 Py_DECREF(retval);
424 return NULL;
427 static int
428 write_file(Picklerobject *self, const char *s, Py_ssize_t n)
430 size_t nbyteswritten;
432 if (s == NULL) {
433 return 0;
436 if (n > INT_MAX) {
437 /* String too large */
438 return -1;
441 PyFile_IncUseCount((PyFileObject *)self->file);
442 Py_BEGIN_ALLOW_THREADS
443 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
444 Py_END_ALLOW_THREADS
445 PyFile_DecUseCount((PyFileObject *)self->file);
446 if (nbyteswritten != (size_t)n) {
447 PyErr_SetFromErrno(PyExc_IOError);
448 return -1;
451 return (int)n;
454 static int
455 write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
457 if (s == NULL) {
458 return 0;
461 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
462 return -1;
465 return (int)n;
468 static int
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;
473 return (int)n;
476 static int
477 write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
479 PyObject *py_str = 0, *junk = 0;
480 int n;
482 if (_n > INT_MAX)
483 return -1;
484 n = (int)_n;
485 if (s == NULL) {
486 if (!( self->buf_size )) return 0;
487 py_str = PyString_FromStringAndSize(self->write_buf,
488 self->buf_size);
489 if (!py_str)
490 return -1;
492 else {
493 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
494 if (write_other(self, NULL, 0) < 0)
495 return -1;
498 if (n > WRITE_BUF_SIZE) {
499 if (!( py_str =
500 PyString_FromStringAndSize(s, n)))
501 return -1;
503 else {
504 memcpy(self->write_buf + self->buf_size, s, n);
505 self->buf_size += n;
506 return n;
510 if (self->write) {
511 /* object with write method */
512 ARG_TUP(self, py_str);
513 if (self->arg) {
514 junk = PyObject_Call(self->write, self->arg, NULL);
515 FREE_ARG_TUP(self);
517 if (junk) Py_DECREF(junk);
518 else return -1;
520 else
521 PDATA_PUSH(self->file, py_str, -1);
523 self->buf_size = 0;
524 return n;
528 static Py_ssize_t
529 read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
531 size_t nbytesread;
533 if (self->buf_size == 0) {
534 int size;
536 size = ((n < 32) ? 32 : n);
537 if (!( self->buf = (char *)malloc(size))) {
538 PyErr_NoMemory();
539 return -1;
542 self->buf_size = size;
544 else if (n > self->buf_size) {
545 char *newbuf = (char *)realloc(self->buf, n);
546 if (!newbuf) {
547 PyErr_NoMemory();
548 return -1;
550 self->buf = newbuf;
551 self->buf_size = n;
554 PyFile_IncUseCount((PyFileObject *)self->file);
555 Py_BEGIN_ALLOW_THREADS
556 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
557 Py_END_ALLOW_THREADS
558 PyFile_DecUseCount((PyFileObject *)self->file);
559 if (nbytesread != (size_t)n) {
560 if (feof(self->fp)) {
561 PyErr_SetNone(PyExc_EOFError);
562 return -1;
565 PyErr_SetFromErrno(PyExc_IOError);
566 return -1;
569 *s = self->buf;
571 return n;
575 static Py_ssize_t
576 readline_file(Unpicklerobject *self, char **s)
578 int i;
580 if (self->buf_size == 0) {
581 if (!( self->buf = (char *)malloc(40))) {
582 PyErr_NoMemory();
583 return -1;
585 self->buf_size = 40;
588 i = 0;
589 while (1) {
590 int bigger;
591 char *newbuf;
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';
596 *s = self->buf;
597 return i + 1;
600 bigger = self->buf_size << 1;
601 if (bigger <= 0) { /* overflow */
602 PyErr_NoMemory();
603 return -1;
605 newbuf = (char *)realloc(self->buf, bigger);
606 if (!newbuf) {
607 PyErr_NoMemory();
608 return -1;
610 self->buf = newbuf;
611 self->buf_size = bigger;
616 static Py_ssize_t
617 read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
619 char *ptr;
621 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
622 PyErr_SetNone(PyExc_EOFError);
623 return -1;
626 *s = ptr;
628 return n;
632 static Py_ssize_t
633 readline_cStringIO(Unpicklerobject *self, char **s)
635 Py_ssize_t n;
636 char *ptr;
638 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
639 return -1;
642 *s = ptr;
644 return n;
648 static Py_ssize_t
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);
656 if (self->arg) {
657 str = PyObject_Call(self->read, self->arg, NULL);
658 FREE_ARG_TUP(self);
660 if (! str) return -1;
662 Py_XDECREF(self->last_string);
663 self->last_string = str;
665 if (! (*s = PyString_AsString(str))) return -1;
666 return n;
670 static Py_ssize_t
671 readline_other(Unpicklerobject *self, char **s)
673 PyObject *str;
674 Py_ssize_t str_size;
676 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
677 return -1;
680 if ((str_size = PyString_Size(str)) < 0)
681 return -1;
683 Py_XDECREF(self->last_string);
684 self->last_string = str;
686 if (! (*s = PyString_AsString(str)))
687 return -1;
689 return str_size;
692 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
693 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
694 * The caller is responsible for free()'ing the return value.
696 static char *
697 pystrndup(const char *s, int n)
699 char *r = (char *)malloc(n+1);
700 if (r == NULL)
701 return (char*)PyErr_NoMemory();
702 memcpy(r, s, n);
703 r[n] = 0;
704 return r;
708 static int
709 get(Picklerobject *self, PyObject *id)
711 PyObject *value, *mv;
712 long c_value;
713 char s[30];
714 size_t len;
716 if (!( mv = PyDict_GetItem(self->memo, id))) {
717 PyErr_SetObject(PyExc_KeyError, id);
718 return -1;
721 if (!( value = PyTuple_GetItem(mv, 0)))
722 return -1;
724 if (!( PyInt_Check(value))) {
725 PyErr_SetString(PicklingError, "no int where int expected in memo");
726 return -1;
728 c_value = PyInt_AS_LONG((PyIntObject*)value);
730 if (!self->bin) {
731 s[0] = GET;
732 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
733 len = strlen(s);
735 else if (Pdata_Check(self->file)) {
736 if (write_other(self, NULL, 0) < 0) return -1;
737 PDATA_APPEND(self->file, mv, -1);
738 return 0;
740 else {
741 if (c_value < 256) {
742 s[0] = BINGET;
743 s[1] = (int)(c_value & 0xff);
744 len = 2;
746 else {
747 s[0] = LONG_BINGET;
748 s[1] = (int)(c_value & 0xff);
749 s[2] = (int)((c_value >> 8) & 0xff);
750 s[3] = (int)((c_value >> 16) & 0xff);
751 s[4] = (int)((c_value >> 24) & 0xff);
752 len = 5;
756 if (self->write_func(self, s, len) < 0)
757 return -1;
759 return 0;
763 static int
764 put(Picklerobject *self, PyObject *ob)
766 if (Py_REFCNT(ob) < 2 || self->fast)
767 return 0;
769 return put2(self, ob);
773 static int
774 put2(Picklerobject *self, PyObject *ob)
776 char c_str[30];
777 int p;
778 size_t len;
779 int res = -1;
780 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
782 if (self->fast)
783 return 0;
785 if ((p = PyDict_Size(self->memo)) < 0)
786 goto finally;
788 /* Make sure memo keys are positive! */
789 /* XXX Why?
790 * XXX And does "positive" really mean non-negative?
791 * XXX pickle.py starts with PUT index 0, not 1. This makes for
792 * XXX gratuitous differences between the pickling modules.
794 p++;
796 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
797 goto finally;
799 if (!( memo_len = PyInt_FromLong(p)))
800 goto finally;
802 if (!( t = PyTuple_New(2)))
803 goto finally;
805 PyTuple_SET_ITEM(t, 0, memo_len);
806 Py_INCREF(memo_len);
807 PyTuple_SET_ITEM(t, 1, ob);
808 Py_INCREF(ob);
810 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
811 goto finally;
813 if (!self->bin) {
814 c_str[0] = PUT;
815 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
816 len = strlen(c_str);
818 else if (Pdata_Check(self->file)) {
819 if (write_other(self, NULL, 0) < 0) return -1;
820 PDATA_APPEND(self->file, memo_len, -1);
821 res=0; /* Job well done ;) */
822 goto finally;
824 else {
825 if (p >= 256) {
826 c_str[0] = LONG_BINPUT;
827 c_str[1] = (int)(p & 0xff);
828 c_str[2] = (int)((p >> 8) & 0xff);
829 c_str[3] = (int)((p >> 16) & 0xff);
830 c_str[4] = (int)((p >> 24) & 0xff);
831 len = 5;
833 else {
834 c_str[0] = BINPUT;
835 c_str[1] = p;
836 len = 2;
840 if (self->write_func(self, c_str, len) < 0)
841 goto finally;
843 res = 0;
845 finally:
846 Py_XDECREF(py_ob_id);
847 Py_XDECREF(memo_len);
848 Py_XDECREF(t);
850 return res;
853 static PyObject *
854 whichmodule(PyObject *global, PyObject *global_name)
856 Py_ssize_t i, j;
857 PyObject *module = 0, *modules_dict = 0,
858 *global_name_attr = 0, *name = 0;
860 module = PyObject_GetAttrString(global, "__module__");
861 if (module)
862 return module;
863 if (PyErr_ExceptionMatches(PyExc_AttributeError))
864 PyErr_Clear();
865 else
866 return NULL;
868 if (!( modules_dict = PySys_GetObject("modules")))
869 return NULL;
871 i = 0;
872 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
874 if (PyObject_Compare(name, __main___str)==0) continue;
876 global_name_attr = PyObject_GetAttr(module, global_name);
877 if (!global_name_attr) {
878 if (PyErr_ExceptionMatches(PyExc_AttributeError))
879 PyErr_Clear();
880 else
881 return NULL;
882 continue;
885 if (global_name_attr != global) {
886 Py_DECREF(global_name_attr);
887 continue;
890 Py_DECREF(global_name_attr);
892 break;
895 /* The following implements the rule in pickle.py added in 1.5
896 that used __main__ if no module is found. I don't actually
897 like this rule. jlf
899 if (!j) {
900 j=1;
901 name=__main___str;
904 Py_INCREF(name);
905 return name;
909 static int
910 fast_save_enter(Picklerobject *self, PyObject *obj)
912 /* if fast_container < 0, we're doing an error exit. */
913 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
914 PyObject *key = NULL;
915 if (self->fast_memo == NULL) {
916 self->fast_memo = PyDict_New();
917 if (self->fast_memo == NULL) {
918 self->fast_container = -1;
919 return 0;
922 key = PyLong_FromVoidPtr(obj);
923 if (key == NULL)
924 return 0;
925 if (PyDict_GetItem(self->fast_memo, key)) {
926 Py_DECREF(key);
927 PyErr_Format(PyExc_ValueError,
928 "fast mode: can't pickle cyclic objects "
929 "including object type %s at %p",
930 Py_TYPE(obj)->tp_name, obj);
931 self->fast_container = -1;
932 return 0;
934 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
935 Py_DECREF(key);
936 self->fast_container = -1;
937 return 0;
939 Py_DECREF(key);
941 return 1;
945 fast_save_leave(Picklerobject *self, PyObject *obj)
947 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
948 PyObject *key = PyLong_FromVoidPtr(obj);
949 if (key == NULL)
950 return 0;
951 if (PyDict_DelItem(self->fast_memo, key) < 0) {
952 Py_DECREF(key);
953 return 0;
955 Py_DECREF(key);
957 return 1;
960 static int
961 save_none(Picklerobject *self, PyObject *args)
963 static char none = NONE;
964 if (self->write_func(self, &none, 1) < 0)
965 return -1;
967 return 0;
970 static int
971 save_bool(Picklerobject *self, PyObject *args)
973 static const char *buf[2] = {FALSE, TRUE};
974 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
975 long l = PyInt_AS_LONG((PyIntObject *)args);
977 if (self->proto >= 2) {
978 char opcode = l ? NEWTRUE : NEWFALSE;
979 if (self->write_func(self, &opcode, 1) < 0)
980 return -1;
982 else if (self->write_func(self, buf[l], len[l]) < 0)
983 return -1;
984 return 0;
987 static int
988 save_int(Picklerobject *self, PyObject *args)
990 char c_str[32];
991 long l = PyInt_AS_LONG((PyIntObject *)args);
992 int len = 0;
994 if (!self->bin
995 #if SIZEOF_LONG > 4
996 || l > 0x7fffffffL
997 || l < -0x80000000L
998 #endif
1000 /* Text-mode pickle, or long too big to fit in the 4-byte
1001 * signed BININT format: store as a string.
1003 c_str[0] = INT;
1004 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
1005 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1006 return -1;
1008 else {
1009 /* Binary pickle and l fits in a signed 4-byte int. */
1010 c_str[1] = (int)( l & 0xff);
1011 c_str[2] = (int)((l >> 8) & 0xff);
1012 c_str[3] = (int)((l >> 16) & 0xff);
1013 c_str[4] = (int)((l >> 24) & 0xff);
1015 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1016 if (c_str[2] == 0) {
1017 c_str[0] = BININT1;
1018 len = 2;
1020 else {
1021 c_str[0] = BININT2;
1022 len = 3;
1025 else {
1026 c_str[0] = BININT;
1027 len = 5;
1030 if (self->write_func(self, c_str, len) < 0)
1031 return -1;
1034 return 0;
1038 static int
1039 save_long(Picklerobject *self, PyObject *args)
1041 Py_ssize_t size;
1042 int res = -1;
1043 PyObject *repr = NULL;
1045 static char l = LONG;
1047 if (self->proto >= 2) {
1048 /* Linear-time pickling. */
1049 size_t nbits;
1050 size_t nbytes;
1051 unsigned char *pdata;
1052 char c_str[5];
1053 int i;
1054 int sign = _PyLong_Sign(args);
1056 if (sign == 0) {
1057 /* It's 0 -- an empty bytestring. */
1058 c_str[0] = LONG1;
1059 c_str[1] = 0;
1060 i = self->write_func(self, c_str, 2);
1061 if (i < 0) goto finally;
1062 res = 0;
1063 goto finally;
1065 nbits = _PyLong_NumBits(args);
1066 if (nbits == (size_t)-1 && PyErr_Occurred())
1067 goto finally;
1068 /* How many bytes do we need? There are nbits >> 3 full
1069 * bytes of data, and nbits & 7 leftover bits. If there
1070 * are any leftover bits, then we clearly need another
1071 * byte. Wnat's not so obvious is that we *probably*
1072 * need another byte even if there aren't any leftovers:
1073 * the most-significant bit of the most-significant byte
1074 * acts like a sign bit, and it's usually got a sense
1075 * opposite of the one we need. The exception is longs
1076 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1077 * its own 256's-complement, so has the right sign bit
1078 * even without the extra byte. That's a pain to check
1079 * for in advance, though, so we always grab an extra
1080 * byte at the start, and cut it back later if possible.
1082 nbytes = (nbits >> 3) + 1;
1083 if (nbytes > INT_MAX) {
1084 PyErr_SetString(PyExc_OverflowError, "long too large "
1085 "to pickle");
1086 goto finally;
1088 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1089 if (repr == NULL) goto finally;
1090 pdata = (unsigned char *)PyString_AS_STRING(repr);
1091 i = _PyLong_AsByteArray((PyLongObject *)args,
1092 pdata, nbytes,
1093 1 /* little endian */, 1 /* signed */);
1094 if (i < 0) goto finally;
1095 /* If the long is negative, this may be a byte more than
1096 * needed. This is so iff the MSB is all redundant sign
1097 * bits.
1099 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1100 (pdata[nbytes - 2] & 0x80) != 0)
1101 --nbytes;
1103 if (nbytes < 256) {
1104 c_str[0] = LONG1;
1105 c_str[1] = (char)nbytes;
1106 size = 2;
1108 else {
1109 c_str[0] = LONG4;
1110 size = (int)nbytes;
1111 for (i = 1; i < 5; i++) {
1112 c_str[i] = (char)(size & 0xff);
1113 size >>= 8;
1115 size = 5;
1117 i = self->write_func(self, c_str, size);
1118 if (i < 0) goto finally;
1119 i = self->write_func(self, (char *)pdata, (int)nbytes);
1120 if (i < 0) goto finally;
1121 res = 0;
1122 goto finally;
1125 /* proto < 2: write the repr and newline. This is quadratic-time
1126 * (in the number of digits), in both directions.
1128 if (!( repr = PyObject_Repr(args)))
1129 goto finally;
1131 if ((size = PyString_Size(repr)) < 0)
1132 goto finally;
1134 if (self->write_func(self, &l, 1) < 0)
1135 goto finally;
1137 if (self->write_func(self,
1138 PyString_AS_STRING((PyStringObject *)repr),
1139 size) < 0)
1140 goto finally;
1142 if (self->write_func(self, "\n", 1) < 0)
1143 goto finally;
1145 res = 0;
1147 finally:
1148 Py_XDECREF(repr);
1149 return res;
1153 static int
1154 save_float(Picklerobject *self, PyObject *args)
1156 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1158 if (self->bin) {
1159 char str[9];
1160 str[0] = BINFLOAT;
1161 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1162 return -1;
1163 if (self->write_func(self, str, 9) < 0)
1164 return -1;
1166 else {
1167 char c_str[250];
1168 c_str[0] = FLOAT;
1169 _PyOS_double_to_string(c_str + 1, sizeof(c_str) - 2, x, 'g',
1170 17, 0, NULL);
1171 /* Extend the formatted string with a newline character */
1172 strcat(c_str, "\n");
1174 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1175 return -1;
1178 return 0;
1182 static int
1183 save_string(Picklerobject *self, PyObject *args, int doput)
1185 int size, len;
1186 PyObject *repr=0;
1188 if ((size = PyString_Size(args)) < 0)
1189 return -1;
1191 if (!self->bin) {
1192 char *repr_str;
1194 static char string = STRING;
1196 if (!( repr = PyObject_Repr(args)))
1197 return -1;
1199 if ((len = PyString_Size(repr)) < 0)
1200 goto err;
1201 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1203 if (self->write_func(self, &string, 1) < 0)
1204 goto err;
1206 if (self->write_func(self, repr_str, len) < 0)
1207 goto err;
1209 if (self->write_func(self, "\n", 1) < 0)
1210 goto err;
1212 Py_XDECREF(repr);
1214 else {
1215 int i;
1216 char c_str[5];
1218 if ((size = PyString_Size(args)) < 0)
1219 return -1;
1221 if (size < 256) {
1222 c_str[0] = SHORT_BINSTRING;
1223 c_str[1] = size;
1224 len = 2;
1226 else if (size <= INT_MAX) {
1227 c_str[0] = BINSTRING;
1228 for (i = 1; i < 5; i++)
1229 c_str[i] = (int)(size >> ((i - 1) * 8));
1230 len = 5;
1232 else
1233 return -1; /* string too large */
1235 if (self->write_func(self, c_str, len) < 0)
1236 return -1;
1238 if (size > 128 && Pdata_Check(self->file)) {
1239 if (write_other(self, NULL, 0) < 0) return -1;
1240 PDATA_APPEND(self->file, args, -1);
1242 else {
1243 if (self->write_func(self,
1244 PyString_AS_STRING(
1245 (PyStringObject *)args),
1246 size) < 0)
1247 return -1;
1251 if (doput)
1252 if (put(self, args) < 0)
1253 return -1;
1255 return 0;
1257 err:
1258 Py_XDECREF(repr);
1259 return -1;
1263 #ifdef Py_USING_UNICODE
1264 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1265 backslash and newline characters to \uXXXX escapes. */
1266 static PyObject *
1267 modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
1269 PyObject *repr;
1270 char *p;
1271 char *q;
1273 static const char *hexdigit = "0123456789abcdef";
1274 #ifdef Py_UNICODE_WIDE
1275 const Py_ssize_t expandsize = 10;
1276 #else
1277 const Py_ssize_t expandsize = 6;
1278 #endif
1280 if (size > PY_SSIZE_T_MAX / expandsize)
1281 return PyErr_NoMemory();
1283 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1284 if (repr == NULL)
1285 return NULL;
1286 if (size == 0)
1287 return repr;
1289 p = q = PyString_AS_STRING(repr);
1290 while (size-- > 0) {
1291 Py_UNICODE ch = *s++;
1292 #ifdef Py_UNICODE_WIDE
1293 /* Map 32-bit characters to '\Uxxxxxxxx' */
1294 if (ch >= 0x10000) {
1295 *p++ = '\\';
1296 *p++ = 'U';
1297 *p++ = hexdigit[(ch >> 28) & 0xf];
1298 *p++ = hexdigit[(ch >> 24) & 0xf];
1299 *p++ = hexdigit[(ch >> 20) & 0xf];
1300 *p++ = hexdigit[(ch >> 16) & 0xf];
1301 *p++ = hexdigit[(ch >> 12) & 0xf];
1302 *p++ = hexdigit[(ch >> 8) & 0xf];
1303 *p++ = hexdigit[(ch >> 4) & 0xf];
1304 *p++ = hexdigit[ch & 15];
1306 else
1307 #else
1308 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1309 if (ch >= 0xD800 && ch < 0xDC00) {
1310 Py_UNICODE ch2;
1311 Py_UCS4 ucs;
1313 ch2 = *s++;
1314 size--;
1315 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1316 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1317 *p++ = '\\';
1318 *p++ = 'U';
1319 *p++ = hexdigit[(ucs >> 28) & 0xf];
1320 *p++ = hexdigit[(ucs >> 24) & 0xf];
1321 *p++ = hexdigit[(ucs >> 20) & 0xf];
1322 *p++ = hexdigit[(ucs >> 16) & 0xf];
1323 *p++ = hexdigit[(ucs >> 12) & 0xf];
1324 *p++ = hexdigit[(ucs >> 8) & 0xf];
1325 *p++ = hexdigit[(ucs >> 4) & 0xf];
1326 *p++ = hexdigit[ucs & 0xf];
1327 continue;
1329 /* Fall through: isolated surrogates are copied as-is */
1330 s--;
1331 size++;
1333 #endif
1334 /* Map 16-bit characters to '\uxxxx' */
1335 if (ch >= 256 || ch == '\\' || ch == '\n') {
1336 *p++ = '\\';
1337 *p++ = 'u';
1338 *p++ = hexdigit[(ch >> 12) & 0xf];
1339 *p++ = hexdigit[(ch >> 8) & 0xf];
1340 *p++ = hexdigit[(ch >> 4) & 0xf];
1341 *p++ = hexdigit[ch & 15];
1343 /* Copy everything else as-is */
1344 else
1345 *p++ = (char) ch;
1347 *p = '\0';
1348 _PyString_Resize(&repr, p - q);
1349 return repr;
1352 static int
1353 save_unicode(Picklerobject *self, PyObject *args, int doput)
1355 Py_ssize_t size, len;
1356 PyObject *repr=0;
1358 if (!PyUnicode_Check(args))
1359 return -1;
1361 if (!self->bin) {
1362 char *repr_str;
1363 static char string = UNICODE;
1365 repr = modified_EncodeRawUnicodeEscape(
1366 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1367 if (!repr)
1368 return -1;
1370 if ((len = PyString_Size(repr)) < 0)
1371 goto err;
1372 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1374 if (self->write_func(self, &string, 1) < 0)
1375 goto err;
1377 if (self->write_func(self, repr_str, len) < 0)
1378 goto err;
1380 if (self->write_func(self, "\n", 1) < 0)
1381 goto err;
1383 Py_XDECREF(repr);
1385 else {
1386 int i;
1387 char c_str[5];
1389 if (!( repr = PyUnicode_AsUTF8String(args)))
1390 return -1;
1392 if ((size = PyString_Size(repr)) < 0)
1393 goto err;
1394 if (size > INT_MAX)
1395 return -1; /* string too large */
1397 c_str[0] = BINUNICODE;
1398 for (i = 1; i < 5; i++)
1399 c_str[i] = (int)(size >> ((i - 1) * 8));
1400 len = 5;
1402 if (self->write_func(self, c_str, len) < 0)
1403 goto err;
1405 if (size > 128 && Pdata_Check(self->file)) {
1406 if (write_other(self, NULL, 0) < 0)
1407 goto err;
1408 PDATA_APPEND(self->file, repr, -1);
1410 else {
1411 if (self->write_func(self, PyString_AS_STRING(repr),
1412 size) < 0)
1413 goto err;
1416 Py_DECREF(repr);
1419 if (doput)
1420 if (put(self, args) < 0)
1421 return -1;
1423 return 0;
1425 err:
1426 Py_XDECREF(repr);
1427 return -1;
1429 #endif
1431 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1432 static int
1433 store_tuple_elements(Picklerobject *self, PyObject *t, int len)
1435 int i;
1436 int res = -1; /* guilty until proved innocent */
1438 assert(PyTuple_Size(t) == len);
1440 for (i = 0; i < len; i++) {
1441 PyObject *element = PyTuple_GET_ITEM(t, i);
1443 if (element == NULL)
1444 goto finally;
1445 if (save(self, element, 0) < 0)
1446 goto finally;
1448 res = 0;
1450 finally:
1451 return res;
1454 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1455 * used across protocols to minimize the space needed to pickle them.
1456 * Tuples are also the only builtin immutable type that can be recursive
1457 * (a tuple can be reached from itself), and that requires some subtle
1458 * magic so that it works in all cases. IOW, this is a long routine.
1460 static int
1461 save_tuple(Picklerobject *self, PyObject *args)
1463 PyObject *py_tuple_id = NULL;
1464 int len, i;
1465 int res = -1;
1467 static char tuple = TUPLE;
1468 static char pop = POP;
1469 static char pop_mark = POP_MARK;
1470 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1472 if ((len = PyTuple_Size(args)) < 0)
1473 goto finally;
1475 if (len == 0) {
1476 char c_str[2];
1478 if (self->proto) {
1479 c_str[0] = EMPTY_TUPLE;
1480 len = 1;
1482 else {
1483 c_str[0] = MARK;
1484 c_str[1] = TUPLE;
1485 len = 2;
1487 if (self->write_func(self, c_str, len) >= 0)
1488 res = 0;
1489 /* Don't memoize an empty tuple. */
1490 goto finally;
1493 /* A non-empty tuple. */
1495 /* id(tuple) isn't in the memo now. If it shows up there after
1496 * saving the tuple elements, the tuple must be recursive, in
1497 * which case we'll pop everything we put on the stack, and fetch
1498 * its value from the memo.
1500 py_tuple_id = PyLong_FromVoidPtr(args);
1501 if (py_tuple_id == NULL)
1502 goto finally;
1504 if (len <= 3 && self->proto >= 2) {
1505 /* Use TUPLE{1,2,3} opcodes. */
1506 if (store_tuple_elements(self, args, len) < 0)
1507 goto finally;
1508 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1509 /* pop the len elements */
1510 for (i = 0; i < len; ++i)
1511 if (self->write_func(self, &pop, 1) < 0)
1512 goto finally;
1513 /* fetch from memo */
1514 if (get(self, py_tuple_id) < 0)
1515 goto finally;
1516 res = 0;
1517 goto finally;
1519 /* Not recursive. */
1520 if (self->write_func(self, len2opcode + len, 1) < 0)
1521 goto finally;
1522 goto memoize;
1525 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1526 * Generate MARK elt1 elt2 ... TUPLE
1528 if (self->write_func(self, &MARKv, 1) < 0)
1529 goto finally;
1531 if (store_tuple_elements(self, args, len) < 0)
1532 goto finally;
1534 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1535 /* pop the stack stuff we pushed */
1536 if (self->bin) {
1537 if (self->write_func(self, &pop_mark, 1) < 0)
1538 goto finally;
1540 else {
1541 /* Note that we pop one more than len, to remove
1542 * the MARK too.
1544 for (i = 0; i <= len; i++)
1545 if (self->write_func(self, &pop, 1) < 0)
1546 goto finally;
1548 /* fetch from memo */
1549 if (get(self, py_tuple_id) >= 0)
1550 res = 0;
1551 goto finally;
1554 /* Not recursive. */
1555 if (self->write_func(self, &tuple, 1) < 0)
1556 goto finally;
1558 memoize:
1559 if (put(self, args) >= 0)
1560 res = 0;
1562 finally:
1563 Py_XDECREF(py_tuple_id);
1564 return res;
1567 /* iter is an iterator giving items, and we batch up chunks of
1568 * MARK item item ... item APPENDS
1569 * opcode sequences. Calling code should have arranged to first create an
1570 * empty list, or list-like object, for the APPENDS to operate on.
1571 * Returns 0 on success, <0 on error.
1573 static int
1574 batch_list(Picklerobject *self, PyObject *iter)
1576 PyObject *obj = NULL;
1577 PyObject *firstitem = NULL;
1578 int i, n;
1580 static char append = APPEND;
1581 static char appends = APPENDS;
1583 assert(iter != NULL);
1585 if (self->proto == 0) {
1586 /* APPENDS isn't available; do one at a time. */
1587 for (;;) {
1588 obj = PyIter_Next(iter);
1589 if (obj == NULL) {
1590 if (PyErr_Occurred())
1591 return -1;
1592 break;
1594 i = save(self, obj, 0);
1595 Py_DECREF(obj);
1596 if (i < 0)
1597 return -1;
1598 if (self->write_func(self, &append, 1) < 0)
1599 return -1;
1601 return 0;
1604 /* proto > 0: write in batches of BATCHSIZE. */
1605 do {
1606 /* Get first item */
1607 firstitem = PyIter_Next(iter);
1608 if (firstitem == NULL) {
1609 if (PyErr_Occurred())
1610 goto BatchFailed;
1612 /* nothing more to add */
1613 break;
1616 /* Try to get a second item */
1617 obj = PyIter_Next(iter);
1618 if (obj == NULL) {
1619 if (PyErr_Occurred())
1620 goto BatchFailed;
1622 /* Only one item to write */
1623 if (save(self, firstitem, 0) < 0)
1624 goto BatchFailed;
1625 if (self->write_func(self, &append, 1) < 0)
1626 goto BatchFailed;
1627 Py_CLEAR(firstitem);
1628 break;
1631 /* More than one item to write */
1633 /* Pump out MARK, items, APPENDS. */
1634 if (self->write_func(self, &MARKv, 1) < 0)
1635 goto BatchFailed;
1637 if (save(self, firstitem, 0) < 0)
1638 goto BatchFailed;
1639 Py_CLEAR(firstitem);
1640 n = 1;
1642 /* Fetch and save up to BATCHSIZE items */
1643 while (obj) {
1644 if (save(self, obj, 0) < 0)
1645 goto BatchFailed;
1646 Py_CLEAR(obj);
1647 n += 1;
1649 if (n == BATCHSIZE)
1650 break;
1652 obj = PyIter_Next(iter);
1653 if (obj == NULL) {
1654 if (PyErr_Occurred())
1655 goto BatchFailed;
1656 break;
1660 if (self->write_func(self, &appends, 1) < 0)
1661 goto BatchFailed;
1663 } while (n == BATCHSIZE);
1664 return 0;
1666 BatchFailed:
1667 Py_XDECREF(firstitem);
1668 Py_XDECREF(obj);
1669 return -1;
1672 static int
1673 save_list(Picklerobject *self, PyObject *args)
1675 int res = -1;
1676 char s[3];
1677 int len;
1678 PyObject *iter;
1680 if (self->fast && !fast_save_enter(self, args))
1681 goto finally;
1683 /* Create an empty list. */
1684 if (self->bin) {
1685 s[0] = EMPTY_LIST;
1686 len = 1;
1688 else {
1689 s[0] = MARK;
1690 s[1] = LIST;
1691 len = 2;
1694 if (self->write_func(self, s, len) < 0)
1695 goto finally;
1697 /* Get list length, and bow out early if empty. */
1698 if ((len = PyList_Size(args)) < 0)
1699 goto finally;
1701 /* Memoize. */
1702 if (len == 0) {
1703 if (put(self, args) >= 0)
1704 res = 0;
1705 goto finally;
1707 if (put2(self, args) < 0)
1708 goto finally;
1710 /* Materialize the list elements. */
1711 iter = PyObject_GetIter(args);
1712 if (iter == NULL)
1713 goto finally;
1715 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1717 res = batch_list(self, iter);
1718 Py_LeaveRecursiveCall();
1720 Py_DECREF(iter);
1722 finally:
1723 if (self->fast && !fast_save_leave(self, args))
1724 res = -1;
1726 return res;
1730 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1731 * MARK key value ... key value SETITEMS
1732 * opcode sequences. Calling code should have arranged to first create an
1733 * empty dict, or dict-like object, for the SETITEMS to operate on.
1734 * Returns 0 on success, <0 on error.
1736 * This is very much like batch_list(). The difference between saving
1737 * elements directly, and picking apart two-tuples, is so long-winded at
1738 * the C level, though, that attempts to combine these routines were too
1739 * ugly to bear.
1741 static int
1742 batch_dict(Picklerobject *self, PyObject *iter)
1744 PyObject *p = NULL;
1745 PyObject *firstitem = NULL;
1746 int i, n;
1748 static char setitem = SETITEM;
1749 static char setitems = SETITEMS;
1751 assert(iter != NULL);
1753 if (self->proto == 0) {
1754 /* SETITEMS isn't available; do one at a time. */
1755 for (;;) {
1756 p = PyIter_Next(iter);
1757 if (p == NULL) {
1758 if (PyErr_Occurred())
1759 return -1;
1760 break;
1762 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1763 PyErr_SetString(PyExc_TypeError, "dict items "
1764 "iterator must return 2-tuples");
1765 return -1;
1767 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1768 if (i >= 0)
1769 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1770 Py_DECREF(p);
1771 if (i < 0)
1772 return -1;
1773 if (self->write_func(self, &setitem, 1) < 0)
1774 return -1;
1776 return 0;
1779 /* proto > 0: write in batches of BATCHSIZE. */
1780 do {
1781 /* Get first item */
1782 firstitem = PyIter_Next(iter);
1783 if (firstitem == NULL) {
1784 if (PyErr_Occurred())
1785 goto BatchFailed;
1787 /* nothing more to add */
1788 break;
1790 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1791 PyErr_SetString(PyExc_TypeError, "dict items "
1792 "iterator must return 2-tuples");
1793 goto BatchFailed;
1796 /* Try to get a second item */
1797 p = PyIter_Next(iter);
1798 if (p == NULL) {
1799 if (PyErr_Occurred())
1800 goto BatchFailed;
1802 /* Only one item to write */
1803 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1804 goto BatchFailed;
1805 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1806 goto BatchFailed;
1807 if (self->write_func(self, &setitem, 1) < 0)
1808 goto BatchFailed;
1809 Py_CLEAR(firstitem);
1810 break;
1813 /* More than one item to write */
1815 /* Pump out MARK, items, SETITEMS. */
1816 if (self->write_func(self, &MARKv, 1) < 0)
1817 goto BatchFailed;
1819 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1820 goto BatchFailed;
1821 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1822 goto BatchFailed;
1823 Py_CLEAR(firstitem);
1824 n = 1;
1826 /* Fetch and save up to BATCHSIZE items */
1827 while (p) {
1828 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1829 PyErr_SetString(PyExc_TypeError, "dict items "
1830 "iterator must return 2-tuples");
1831 goto BatchFailed;
1833 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1834 goto BatchFailed;
1835 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1836 goto BatchFailed;
1837 Py_CLEAR(p);
1838 n += 1;
1840 if (n == BATCHSIZE)
1841 break;
1843 p = PyIter_Next(iter);
1844 if (p == NULL) {
1845 if (PyErr_Occurred())
1846 goto BatchFailed;
1847 break;
1851 if (self->write_func(self, &setitems, 1) < 0)
1852 goto BatchFailed;
1854 } while (n == BATCHSIZE);
1855 return 0;
1857 BatchFailed:
1858 Py_XDECREF(firstitem);
1859 Py_XDECREF(p);
1860 return -1;
1863 /* This is a variant of batch_dict() above that specializes for dicts, with no
1864 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1865 * MARK key value ... key value SETITEMS
1866 * opcode sequences. Calling code should have arranged to first create an
1867 * empty dict, or dict-like object, for the SETITEMS to operate on.
1868 * Returns 0 on success, -1 on error.
1870 * Note that this currently doesn't work for protocol 0.
1872 static int
1873 batch_dict_exact(Picklerobject *self, PyObject *obj)
1875 PyObject *key = NULL, *value = NULL;
1876 int i;
1877 Py_ssize_t dict_size, ppos = 0;
1879 static char setitem = SETITEM;
1880 static char setitems = SETITEMS;
1882 assert(obj != NULL);
1883 assert(self->proto > 0);
1885 dict_size = PyDict_Size(obj);
1887 /* Special-case len(d) == 1 to save space. */
1888 if (dict_size == 1) {
1889 PyDict_Next(obj, &ppos, &key, &value);
1890 if (save(self, key, 0) < 0)
1891 return -1;
1892 if (save(self, value, 0) < 0)
1893 return -1;
1894 if (self->write_func(self, &setitem, 1) < 0)
1895 return -1;
1896 return 0;
1899 /* Write in batches of BATCHSIZE. */
1900 do {
1901 i = 0;
1902 if (self->write_func(self, &MARKv, 1) < 0)
1903 return -1;
1904 while (PyDict_Next(obj, &ppos, &key, &value)) {
1905 if (save(self, key, 0) < 0)
1906 return -1;
1907 if (save(self, value, 0) < 0)
1908 return -1;
1909 if (++i == BATCHSIZE)
1910 break;
1912 if (self->write_func(self, &setitems, 1) < 0)
1913 return -1;
1914 if (PyDict_Size(obj) != dict_size) {
1915 PyErr_Format(
1916 PyExc_RuntimeError,
1917 "dictionary changed size during iteration");
1918 return -1;
1921 } while (i == BATCHSIZE);
1922 return 0;
1925 static int
1926 save_dict(Picklerobject *self, PyObject *args)
1928 int res = -1;
1929 char s[3];
1930 int len;
1932 if (self->fast && !fast_save_enter(self, args))
1933 goto finally;
1935 /* Create an empty dict. */
1936 if (self->bin) {
1937 s[0] = EMPTY_DICT;
1938 len = 1;
1940 else {
1941 s[0] = MARK;
1942 s[1] = DICT;
1943 len = 2;
1946 if (self->write_func(self, s, len) < 0)
1947 goto finally;
1949 /* Get dict size, and bow out early if empty. */
1950 if ((len = PyDict_Size(args)) < 0)
1951 goto finally;
1953 if (len == 0) {
1954 if (put(self, args) >= 0)
1955 res = 0;
1956 goto finally;
1958 if (put2(self, args) < 0)
1959 goto finally;
1961 /* Materialize the dict items. */
1962 if (PyDict_CheckExact(args) && self->proto > 0) {
1963 /* We can take certain shortcuts if we know this is a dict and
1964 not a dict subclass. */
1965 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1966 res = batch_dict_exact(self, args);
1967 Py_LeaveRecursiveCall();
1969 } else {
1970 PyObject *iter = PyObject_CallMethod(args, "iteritems", "()");
1971 if (iter == NULL)
1972 goto finally;
1973 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1974 res = batch_dict(self, iter);
1975 Py_LeaveRecursiveCall();
1977 Py_DECREF(iter);
1980 finally:
1981 if (self->fast && !fast_save_leave(self, args))
1982 res = -1;
1984 return res;
1988 static int
1989 save_inst(Picklerobject *self, PyObject *args)
1991 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1992 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1993 char *module_str, *name_str;
1994 int module_size, name_size, res = -1;
1996 static char inst = INST, obj = OBJ, build = BUILD;
1998 if (self->fast && !fast_save_enter(self, args))
1999 goto finally;
2001 if (self->write_func(self, &MARKv, 1) < 0)
2002 goto finally;
2004 if (!( class = PyObject_GetAttr(args, __class___str)))
2005 goto finally;
2007 if (self->bin) {
2008 if (save(self, class, 0) < 0)
2009 goto finally;
2012 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
2013 PyObject *element = 0;
2014 int i, len;
2016 if (!( class_args =
2017 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
2018 goto finally;
2020 if ((len = PyObject_Size(class_args)) < 0)
2021 goto finally;
2023 for (i = 0; i < len; i++) {
2024 if (!( element = PySequence_GetItem(class_args, i)))
2025 goto finally;
2027 if (save(self, element, 0) < 0) {
2028 Py_DECREF(element);
2029 goto finally;
2032 Py_DECREF(element);
2035 else {
2036 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2037 PyErr_Clear();
2038 else
2039 goto finally;
2042 if (!self->bin) {
2043 if (!( name = ((PyClassObject *)class)->cl_name )) {
2044 PyErr_SetString(PicklingError, "class has no name");
2045 goto finally;
2048 if (!( module = whichmodule(class, name)))
2049 goto finally;
2052 if ((module_size = PyString_Size(module)) < 0 ||
2053 (name_size = PyString_Size(name)) < 0)
2054 goto finally;
2056 module_str = PyString_AS_STRING((PyStringObject *)module);
2057 name_str = PyString_AS_STRING((PyStringObject *)name);
2059 if (self->write_func(self, &inst, 1) < 0)
2060 goto finally;
2062 if (self->write_func(self, module_str, module_size) < 0)
2063 goto finally;
2065 if (self->write_func(self, "\n", 1) < 0)
2066 goto finally;
2068 if (self->write_func(self, name_str, name_size) < 0)
2069 goto finally;
2071 if (self->write_func(self, "\n", 1) < 0)
2072 goto finally;
2074 else if (self->write_func(self, &obj, 1) < 0) {
2075 goto finally;
2078 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2079 state = PyObject_Call(getstate_func, empty_tuple, NULL);
2080 if (!state)
2081 goto finally;
2083 else {
2084 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2085 PyErr_Clear();
2086 else
2087 goto finally;
2089 if (!( state = PyObject_GetAttr(args, __dict___str))) {
2090 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2091 PyErr_Clear();
2092 else
2093 goto finally;
2094 res = 0;
2095 goto finally;
2099 if (!PyDict_Check(state)) {
2100 if (put2(self, args) < 0)
2101 goto finally;
2103 else {
2104 if (put(self, args) < 0)
2105 goto finally;
2108 if (save(self, state, 0) < 0)
2109 goto finally;
2111 if (self->write_func(self, &build, 1) < 0)
2112 goto finally;
2114 res = 0;
2116 finally:
2117 if (self->fast && !fast_save_leave(self, args))
2118 res = -1;
2120 Py_XDECREF(module);
2121 Py_XDECREF(class);
2122 Py_XDECREF(state);
2123 Py_XDECREF(getinitargs_func);
2124 Py_XDECREF(getstate_func);
2125 Py_XDECREF(class_args);
2127 return res;
2131 static int
2132 save_global(Picklerobject *self, PyObject *args, PyObject *name)
2134 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2135 char *name_str, *module_str;
2136 int module_size, name_size, res = -1;
2138 static char global = GLOBAL;
2140 if (name) {
2141 global_name = name;
2142 Py_INCREF(global_name);
2144 else {
2145 if (!( global_name = PyObject_GetAttr(args, __name___str)))
2146 goto finally;
2149 if (!( module = whichmodule(args, global_name)))
2150 goto finally;
2152 if ((module_size = PyString_Size(module)) < 0 ||
2153 (name_size = PyString_Size(global_name)) < 0)
2154 goto finally;
2156 module_str = PyString_AS_STRING((PyStringObject *)module);
2157 name_str = PyString_AS_STRING((PyStringObject *)global_name);
2159 /* XXX This can be doing a relative import. Clearly it shouldn't,
2160 but I don't know how to stop it. :-( */
2161 mod = PyImport_ImportModule(module_str);
2162 if (mod == NULL) {
2163 cPickle_ErrFormat(PicklingError,
2164 "Can't pickle %s: import of module %s "
2165 "failed",
2166 "OS", args, module);
2167 goto finally;
2169 klass = PyObject_GetAttrString(mod, name_str);
2170 if (klass == NULL) {
2171 cPickle_ErrFormat(PicklingError,
2172 "Can't pickle %s: attribute lookup %s.%s "
2173 "failed",
2174 "OSS", args, module, global_name);
2175 goto finally;
2177 if (klass != args) {
2178 Py_DECREF(klass);
2179 cPickle_ErrFormat(PicklingError,
2180 "Can't pickle %s: it's not the same object "
2181 "as %s.%s",
2182 "OSS", args, module, global_name);
2183 goto finally;
2185 Py_DECREF(klass);
2187 if (self->proto >= 2) {
2188 /* See whether this is in the extension registry, and if
2189 * so generate an EXT opcode.
2191 PyObject *py_code; /* extension code as Python object */
2192 long code; /* extension code as C value */
2193 char c_str[5];
2194 int n;
2196 PyTuple_SET_ITEM(two_tuple, 0, module);
2197 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2198 py_code = PyDict_GetItem(extension_registry, two_tuple);
2199 if (py_code == NULL)
2200 goto gen_global; /* not registered */
2202 /* Verify py_code has the right type and value. */
2203 if (!PyInt_Check(py_code)) {
2204 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2205 "extension code %s isn't an integer",
2206 "OO", args, py_code);
2207 goto finally;
2209 code = PyInt_AS_LONG(py_code);
2210 if (code <= 0 || code > 0x7fffffffL) {
2211 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2212 "extension code %ld is out of range",
2213 "Ol", args, code);
2214 goto finally;
2217 /* Generate an EXT opcode. */
2218 if (code <= 0xff) {
2219 c_str[0] = EXT1;
2220 c_str[1] = (char)code;
2221 n = 2;
2223 else if (code <= 0xffff) {
2224 c_str[0] = EXT2;
2225 c_str[1] = (char)(code & 0xff);
2226 c_str[2] = (char)((code >> 8) & 0xff);
2227 n = 3;
2229 else {
2230 c_str[0] = EXT4;
2231 c_str[1] = (char)(code & 0xff);
2232 c_str[2] = (char)((code >> 8) & 0xff);
2233 c_str[3] = (char)((code >> 16) & 0xff);
2234 c_str[4] = (char)((code >> 24) & 0xff);
2235 n = 5;
2238 if (self->write_func(self, c_str, n) >= 0)
2239 res = 0;
2240 goto finally; /* and don't memoize */
2243 gen_global:
2244 if (self->write_func(self, &global, 1) < 0)
2245 goto finally;
2247 if (self->write_func(self, module_str, module_size) < 0)
2248 goto finally;
2250 if (self->write_func(self, "\n", 1) < 0)
2251 goto finally;
2253 if (self->write_func(self, name_str, name_size) < 0)
2254 goto finally;
2256 if (self->write_func(self, "\n", 1) < 0)
2257 goto finally;
2259 if (put(self, args) < 0)
2260 goto finally;
2262 res = 0;
2264 finally:
2265 Py_XDECREF(module);
2266 Py_XDECREF(global_name);
2267 Py_XDECREF(mod);
2269 return res;
2272 static int
2273 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2275 PyObject *pid = 0;
2276 int size, res = -1;
2278 static char persid = PERSID, binpersid = BINPERSID;
2280 Py_INCREF(args);
2281 ARG_TUP(self, args);
2282 if (self->arg) {
2283 pid = PyObject_Call(f, self->arg, NULL);
2284 FREE_ARG_TUP(self);
2286 if (! pid) return -1;
2288 if (pid != Py_None) {
2289 if (!self->bin) {
2290 if (!PyString_Check(pid)) {
2291 PyErr_SetString(PicklingError,
2292 "persistent id must be string");
2293 goto finally;
2296 if (self->write_func(self, &persid, 1) < 0)
2297 goto finally;
2299 if ((size = PyString_Size(pid)) < 0)
2300 goto finally;
2302 if (self->write_func(self,
2303 PyString_AS_STRING(
2304 (PyStringObject *)pid),
2305 size) < 0)
2306 goto finally;
2308 if (self->write_func(self, "\n", 1) < 0)
2309 goto finally;
2311 res = 1;
2312 goto finally;
2314 else if (save(self, pid, 1) >= 0) {
2315 if (self->write_func(self, &binpersid, 1) < 0)
2316 res = -1;
2317 else
2318 res = 1;
2321 goto finally;
2324 res = 0;
2326 finally:
2327 Py_XDECREF(pid);
2329 return res;
2332 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2333 * appropriate __reduce__ method for ob.
2335 static int
2336 save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
2338 PyObject *callable;
2339 PyObject *argtup;
2340 PyObject *state = NULL;
2341 PyObject *listitems = Py_None;
2342 PyObject *dictitems = Py_None;
2343 Py_ssize_t size;
2345 int use_newobj = self->proto >= 2;
2347 static char reduce = REDUCE;
2348 static char build = BUILD;
2349 static char newobj = NEWOBJ;
2351 size = PyTuple_Size(args);
2352 if (size < 2 || size > 5) {
2353 cPickle_ErrFormat(PicklingError, "tuple returned by "
2354 "%s must contain 2 through 5 elements",
2355 "O", fn);
2356 return -1;
2359 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2360 &callable,
2361 &argtup,
2362 &state,
2363 &listitems,
2364 &dictitems))
2365 return -1;
2367 if (!PyTuple_Check(argtup)) {
2368 cPickle_ErrFormat(PicklingError, "Second element of "
2369 "tuple returned by %s must be a tuple",
2370 "O", fn);
2371 return -1;
2374 if (state == Py_None)
2375 state = NULL;
2377 if (listitems == Py_None)
2378 listitems = NULL;
2379 else if (!PyIter_Check(listitems)) {
2380 cPickle_ErrFormat(PicklingError, "Fourth element of "
2381 "tuple returned by %s must be an iterator, not %s",
2382 "Os", fn, Py_TYPE(listitems)->tp_name);
2383 return -1;
2386 if (dictitems == Py_None)
2387 dictitems = NULL;
2388 else if (!PyIter_Check(dictitems)) {
2389 cPickle_ErrFormat(PicklingError, "Fifth element of "
2390 "tuple returned by %s must be an iterator, not %s",
2391 "Os", fn, Py_TYPE(dictitems)->tp_name);
2392 return -1;
2395 /* Protocol 2 special case: if callable's name is __newobj__, use
2396 * NEWOBJ. This consumes a lot of code.
2398 if (use_newobj) {
2399 PyObject *temp = PyObject_GetAttr(callable, __name___str);
2401 if (temp == NULL) {
2402 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2403 PyErr_Clear();
2404 else
2405 return -1;
2406 use_newobj = 0;
2408 else {
2409 use_newobj = PyString_Check(temp) &&
2410 strcmp(PyString_AS_STRING(temp),
2411 "__newobj__") == 0;
2412 Py_DECREF(temp);
2415 if (use_newobj) {
2416 PyObject *cls;
2417 PyObject *newargtup;
2418 int n, i;
2420 /* Sanity checks. */
2421 n = PyTuple_Size(argtup);
2422 if (n < 1) {
2423 PyErr_SetString(PicklingError, "__newobj__ arglist "
2424 "is empty");
2425 return -1;
2428 cls = PyTuple_GET_ITEM(argtup, 0);
2429 if (! PyObject_HasAttrString(cls, "__new__")) {
2430 PyErr_SetString(PicklingError, "args[0] from "
2431 "__newobj__ args has no __new__");
2432 return -1;
2435 /* XXX How could ob be NULL? */
2436 if (ob != NULL) {
2437 PyObject *ob_dot_class;
2439 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2440 if (ob_dot_class == NULL) {
2441 if (PyErr_ExceptionMatches(
2442 PyExc_AttributeError))
2443 PyErr_Clear();
2444 else
2445 return -1;
2447 i = ob_dot_class != cls; /* true iff a problem */
2448 Py_XDECREF(ob_dot_class);
2449 if (i) {
2450 PyErr_SetString(PicklingError, "args[0] from "
2451 "__newobj__ args has the wrong class");
2452 return -1;
2456 /* Save the class and its __new__ arguments. */
2457 if (save(self, cls, 0) < 0)
2458 return -1;
2460 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2461 if (newargtup == NULL)
2462 return -1;
2463 for (i = 1; i < n; ++i) {
2464 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2465 Py_INCREF(temp);
2466 PyTuple_SET_ITEM(newargtup, i-1, temp);
2468 i = save(self, newargtup, 0);
2469 Py_DECREF(newargtup);
2470 if (i < 0)
2471 return -1;
2473 /* Add NEWOBJ opcode. */
2474 if (self->write_func(self, &newobj, 1) < 0)
2475 return -1;
2477 else {
2478 /* Not using NEWOBJ. */
2479 if (save(self, callable, 0) < 0 ||
2480 save(self, argtup, 0) < 0 ||
2481 self->write_func(self, &reduce, 1) < 0)
2482 return -1;
2485 /* Memoize. */
2486 /* XXX How can ob be NULL? */
2487 if (ob != NULL) {
2488 if (state && !PyDict_Check(state)) {
2489 if (put2(self, ob) < 0)
2490 return -1;
2492 else if (put(self, ob) < 0)
2493 return -1;
2497 if (listitems && batch_list(self, listitems) < 0)
2498 return -1;
2500 if (dictitems && batch_dict(self, dictitems) < 0)
2501 return -1;
2503 if (state) {
2504 if (save(self, state, 0) < 0 ||
2505 self->write_func(self, &build, 1) < 0)
2506 return -1;
2509 return 0;
2512 static int
2513 save(Picklerobject *self, PyObject *args, int pers_save)
2515 PyTypeObject *type;
2516 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2517 int res = -1;
2518 int tmp;
2520 if (Py_EnterRecursiveCall(" while pickling an object"))
2521 return -1;
2523 if (!pers_save && self->pers_func) {
2524 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2525 res = tmp;
2526 goto finally;
2530 if (args == Py_None) {
2531 res = save_none(self, args);
2532 goto finally;
2535 type = Py_TYPE(args);
2537 switch (type->tp_name[0]) {
2538 case 'b':
2539 if (args == Py_False || args == Py_True) {
2540 res = save_bool(self, args);
2541 goto finally;
2543 break;
2544 case 'i':
2545 if (type == &PyInt_Type) {
2546 res = save_int(self, args);
2547 goto finally;
2549 break;
2551 case 'l':
2552 if (type == &PyLong_Type) {
2553 res = save_long(self, args);
2554 goto finally;
2556 break;
2558 case 'f':
2559 if (type == &PyFloat_Type) {
2560 res = save_float(self, args);
2561 goto finally;
2563 break;
2565 case 't':
2566 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2567 res = save_tuple(self, args);
2568 goto finally;
2570 break;
2572 case 's':
2573 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2574 res = save_string(self, args, 0);
2575 goto finally;
2577 break;
2579 #ifdef Py_USING_UNICODE
2580 case 'u':
2581 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2582 res = save_unicode(self, args, 0);
2583 goto finally;
2585 break;
2586 #endif
2589 if (Py_REFCNT(args) > 1) {
2590 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2591 goto finally;
2593 if (PyDict_GetItem(self->memo, py_ob_id)) {
2594 if (get(self, py_ob_id) < 0)
2595 goto finally;
2597 res = 0;
2598 goto finally;
2602 switch (type->tp_name[0]) {
2603 case 's':
2604 if (type == &PyString_Type) {
2605 res = save_string(self, args, 1);
2606 goto finally;
2608 break;
2610 #ifdef Py_USING_UNICODE
2611 case 'u':
2612 if (type == &PyUnicode_Type) {
2613 res = save_unicode(self, args, 1);
2614 goto finally;
2616 break;
2617 #endif
2619 case 't':
2620 if (type == &PyTuple_Type) {
2621 res = save_tuple(self, args);
2622 goto finally;
2624 if (type == &PyType_Type) {
2625 res = save_global(self, args, NULL);
2626 goto finally;
2628 break;
2630 case 'l':
2631 if (type == &PyList_Type) {
2632 res = save_list(self, args);
2633 goto finally;
2635 break;
2637 case 'd':
2638 if (type == &PyDict_Type) {
2639 res = save_dict(self, args);
2640 goto finally;
2642 break;
2644 case 'i':
2645 if (type == &PyInstance_Type) {
2646 res = save_inst(self, args);
2647 goto finally;
2649 break;
2651 case 'c':
2652 if (type == &PyClass_Type) {
2653 res = save_global(self, args, NULL);
2654 goto finally;
2656 break;
2658 case 'f':
2659 if (type == &PyFunction_Type) {
2660 res = save_global(self, args, NULL);
2661 if (res && PyErr_ExceptionMatches(PickleError)) {
2662 /* fall back to reduce */
2663 PyErr_Clear();
2664 break;
2666 goto finally;
2668 break;
2670 case 'b':
2671 if (type == &PyCFunction_Type) {
2672 res = save_global(self, args, NULL);
2673 goto finally;
2677 if (!pers_save && self->inst_pers_func) {
2678 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2679 res = tmp;
2680 goto finally;
2684 if (PyType_IsSubtype(type, &PyType_Type)) {
2685 res = save_global(self, args, NULL);
2686 goto finally;
2689 /* Get a reduction callable, and call it. This may come from
2690 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2691 * or the object's __reduce__ method.
2693 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2694 if (__reduce__ != NULL) {
2695 Py_INCREF(__reduce__);
2696 Py_INCREF(args);
2697 ARG_TUP(self, args);
2698 if (self->arg) {
2699 t = PyObject_Call(__reduce__, self->arg, NULL);
2700 FREE_ARG_TUP(self);
2703 else {
2704 /* Check for a __reduce_ex__ method. */
2705 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2706 if (__reduce__ != NULL) {
2707 t = PyInt_FromLong(self->proto);
2708 if (t != NULL) {
2709 ARG_TUP(self, t);
2710 t = NULL;
2711 if (self->arg) {
2712 t = PyObject_Call(__reduce__,
2713 self->arg, NULL);
2714 FREE_ARG_TUP(self);
2718 else {
2719 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2720 PyErr_Clear();
2721 else
2722 goto finally;
2723 /* Check for a __reduce__ method. */
2724 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2725 if (__reduce__ != NULL) {
2726 t = PyObject_Call(__reduce__,
2727 empty_tuple, NULL);
2729 else {
2730 PyErr_SetObject(UnpickleableError, args);
2731 goto finally;
2736 if (t == NULL)
2737 goto finally;
2739 if (PyString_Check(t)) {
2740 res = save_global(self, args, t);
2741 goto finally;
2744 if (!PyTuple_Check(t)) {
2745 cPickle_ErrFormat(PicklingError, "Value returned by "
2746 "%s must be string or tuple",
2747 "O", __reduce__);
2748 goto finally;
2751 res = save_reduce(self, t, __reduce__, args);
2753 finally:
2754 Py_LeaveRecursiveCall();
2755 Py_XDECREF(py_ob_id);
2756 Py_XDECREF(__reduce__);
2757 Py_XDECREF(t);
2759 return res;
2763 static int
2764 dump(Picklerobject *self, PyObject *args)
2766 static char stop = STOP;
2768 if (self->proto >= 2) {
2769 char bytes[2];
2771 bytes[0] = PROTO;
2772 assert(self->proto >= 0 && self->proto < 256);
2773 bytes[1] = (char)self->proto;
2774 if (self->write_func(self, bytes, 2) < 0)
2775 return -1;
2778 if (save(self, args, 0) < 0)
2779 return -1;
2781 if (self->write_func(self, &stop, 1) < 0)
2782 return -1;
2784 if (self->write_func(self, NULL, 0) < 0)
2785 return -1;
2787 return 0;
2790 static PyObject *
2791 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2793 if (self->memo)
2794 PyDict_Clear(self->memo);
2795 Py_INCREF(Py_None);
2796 return Py_None;
2799 static PyObject *
2800 Pickle_getvalue(Picklerobject *self, PyObject *args)
2802 int l, i, rsize, ssize, clear=1, lm;
2803 long ik;
2804 PyObject *k, *r;
2805 char *s, *p, *have_get;
2806 Pdata *data;
2808 /* Can be called by Python code or C code */
2809 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2810 return NULL;
2812 /* Check to make sure we are based on a list */
2813 if (! Pdata_Check(self->file)) {
2814 PyErr_SetString(PicklingError,
2815 "Attempt to getvalue() a non-list-based pickler");
2816 return NULL;
2819 /* flush write buffer */
2820 if (write_other(self, NULL, 0) < 0) return NULL;
2822 data=(Pdata*)self->file;
2823 l=data->length;
2825 /* set up an array to hold get/put status */
2826 lm = PyDict_Size(self->memo);
2827 if (lm < 0) return NULL;
2828 lm++;
2829 have_get = malloc(lm);
2830 if (have_get == NULL) return PyErr_NoMemory();
2831 memset(have_get, 0, lm);
2833 /* Scan for gets. */
2834 for (rsize = 0, i = l; --i >= 0; ) {
2835 k = data->data[i];
2837 if (PyString_Check(k))
2838 rsize += PyString_GET_SIZE(k);
2840 else if (PyInt_Check(k)) { /* put */
2841 ik = PyInt_AS_LONG((PyIntObject*)k);
2842 if (ik >= lm || ik == 0) {
2843 PyErr_SetString(PicklingError,
2844 "Invalid get data");
2845 goto err;
2847 if (have_get[ik]) /* with matching get */
2848 rsize += ik < 256 ? 2 : 5;
2851 else if (! (PyTuple_Check(k) &&
2852 PyTuple_GET_SIZE(k) == 2 &&
2853 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2855 PyErr_SetString(PicklingError,
2856 "Unexpected data in internal list");
2857 goto err;
2860 else { /* put */
2861 ik = PyInt_AS_LONG((PyIntObject *)k);
2862 if (ik >= lm || ik == 0) {
2863 PyErr_SetString(PicklingError,
2864 "Invalid get data");
2865 return NULL;
2867 have_get[ik] = 1;
2868 rsize += ik < 256 ? 2 : 5;
2872 /* Now generate the result */
2873 r = PyString_FromStringAndSize(NULL, rsize);
2874 if (r == NULL) goto err;
2875 s = PyString_AS_STRING((PyStringObject *)r);
2877 for (i = 0; i < l; i++) {
2878 k = data->data[i];
2880 if (PyString_Check(k)) {
2881 ssize = PyString_GET_SIZE(k);
2882 if (ssize) {
2883 p=PyString_AS_STRING((PyStringObject *)k);
2884 while (--ssize >= 0)
2885 *s++ = *p++;
2889 else if (PyTuple_Check(k)) { /* get */
2890 ik = PyInt_AS_LONG((PyIntObject *)
2891 PyTuple_GET_ITEM(k, 0));
2892 if (ik < 256) {
2893 *s++ = BINGET;
2894 *s++ = (int)(ik & 0xff);
2896 else {
2897 *s++ = LONG_BINGET;
2898 *s++ = (int)(ik & 0xff);
2899 *s++ = (int)((ik >> 8) & 0xff);
2900 *s++ = (int)((ik >> 16) & 0xff);
2901 *s++ = (int)((ik >> 24) & 0xff);
2905 else { /* put */
2906 ik = PyInt_AS_LONG((PyIntObject*)k);
2908 if (have_get[ik]) { /* with matching get */
2909 if (ik < 256) {
2910 *s++ = BINPUT;
2911 *s++ = (int)(ik & 0xff);
2913 else {
2914 *s++ = LONG_BINPUT;
2915 *s++ = (int)(ik & 0xff);
2916 *s++ = (int)((ik >> 8) & 0xff);
2917 *s++ = (int)((ik >> 16) & 0xff);
2918 *s++ = (int)((ik >> 24) & 0xff);
2924 if (clear) {
2925 PyDict_Clear(self->memo);
2926 Pdata_clear(data, 0);
2929 free(have_get);
2930 return r;
2931 err:
2932 free(have_get);
2933 return NULL;
2936 static PyObject *
2937 Pickler_dump(Picklerobject *self, PyObject *args)
2939 PyObject *ob;
2940 int get=0;
2942 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2943 return NULL;
2945 if (dump(self, ob) < 0)
2946 return NULL;
2948 if (get) return Pickle_getvalue(self, NULL);
2950 /* XXX Why does dump() return self? */
2951 Py_INCREF(self);
2952 return (PyObject*)self;
2956 static struct PyMethodDef Pickler_methods[] =
2958 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2959 PyDoc_STR("dump(object) -- "
2960 "Write an object in pickle format to the object's pickle stream")},
2961 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2962 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2963 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2964 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2965 {NULL, NULL} /* sentinel */
2969 static Picklerobject *
2970 newPicklerobject(PyObject *file, int proto)
2972 Picklerobject *self;
2974 if (proto < 0)
2975 proto = HIGHEST_PROTOCOL;
2976 if (proto > HIGHEST_PROTOCOL) {
2977 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2978 "the highest available protocol is %d",
2979 proto, HIGHEST_PROTOCOL);
2980 return NULL;
2983 self = PyObject_GC_New(Picklerobject, &Picklertype);
2984 if (self == NULL)
2985 return NULL;
2986 self->proto = proto;
2987 self->bin = proto > 0;
2988 self->fp = NULL;
2989 self->write = NULL;
2990 self->memo = NULL;
2991 self->arg = NULL;
2992 self->pers_func = NULL;
2993 self->inst_pers_func = NULL;
2994 self->write_buf = NULL;
2995 self->fast = 0;
2996 self->fast_container = 0;
2997 self->fast_memo = NULL;
2998 self->buf_size = 0;
2999 self->dispatch_table = NULL;
3001 self->file = NULL;
3002 if (file)
3003 Py_INCREF(file);
3004 else {
3005 file = Pdata_New();
3006 if (file == NULL)
3007 goto err;
3009 self->file = file;
3011 if (!( self->memo = PyDict_New()))
3012 goto err;
3014 if (PyFile_Check(file)) {
3015 self->fp = PyFile_AsFile(file);
3016 if (self->fp == NULL) {
3017 PyErr_SetString(PyExc_ValueError,
3018 "I/O operation on closed file");
3019 goto err;
3021 self->write_func = write_file;
3023 else if (PycStringIO_OutputCheck(file)) {
3024 self->write_func = write_cStringIO;
3026 else if (file == Py_None) {
3027 self->write_func = write_none;
3029 else {
3030 self->write_func = write_other;
3032 if (! Pdata_Check(file)) {
3033 self->write = PyObject_GetAttr(file, write_str);
3034 if (!self->write) {
3035 PyErr_Clear();
3036 PyErr_SetString(PyExc_TypeError,
3037 "argument must have 'write' "
3038 "attribute");
3039 goto err;
3043 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
3044 if (self->write_buf == NULL) {
3045 PyErr_NoMemory();
3046 goto err;
3050 if (PyEval_GetRestricted()) {
3051 /* Restricted execution, get private tables */
3052 PyObject *m = PyImport_Import(copyreg_str);
3054 if (m == NULL)
3055 goto err;
3056 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
3057 Py_DECREF(m);
3058 if (self->dispatch_table == NULL)
3059 goto err;
3061 else {
3062 self->dispatch_table = dispatch_table;
3063 Py_INCREF(dispatch_table);
3065 PyObject_GC_Track(self);
3067 return self;
3069 err:
3070 Py_DECREF(self);
3071 return NULL;
3075 static PyObject *
3076 get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
3078 static char *kwlist[] = {"file", "protocol", NULL};
3079 PyObject *file = NULL;
3080 int proto = 0;
3082 /* XXX
3083 * The documented signature is Pickler(file, protocol=0), but this
3084 * accepts Pickler() and Pickler(integer) too. The meaning then
3085 * is clear as mud, undocumented, and not supported by pickle.py.
3086 * I'm told Zope uses this, but I haven't traced into this code
3087 * far enough to figure out what it means.
3089 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
3090 PyErr_Clear();
3091 proto = 0;
3092 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3093 kwlist, &file, &proto))
3094 return NULL;
3096 return (PyObject *)newPicklerobject(file, proto);
3100 static void
3101 Pickler_dealloc(Picklerobject *self)
3103 PyObject_GC_UnTrack(self);
3104 Py_XDECREF(self->write);
3105 Py_XDECREF(self->memo);
3106 Py_XDECREF(self->fast_memo);
3107 Py_XDECREF(self->arg);
3108 Py_XDECREF(self->file);
3109 Py_XDECREF(self->pers_func);
3110 Py_XDECREF(self->inst_pers_func);
3111 Py_XDECREF(self->dispatch_table);
3112 PyMem_Free(self->write_buf);
3113 Py_TYPE(self)->tp_free((PyObject *)self);
3116 static int
3117 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3119 Py_VISIT(self->write);
3120 Py_VISIT(self->memo);
3121 Py_VISIT(self->fast_memo);
3122 Py_VISIT(self->arg);
3123 Py_VISIT(self->file);
3124 Py_VISIT(self->pers_func);
3125 Py_VISIT(self->inst_pers_func);
3126 Py_VISIT(self->dispatch_table);
3127 return 0;
3130 static int
3131 Pickler_clear(Picklerobject *self)
3133 Py_CLEAR(self->write);
3134 Py_CLEAR(self->memo);
3135 Py_CLEAR(self->fast_memo);
3136 Py_CLEAR(self->arg);
3137 Py_CLEAR(self->file);
3138 Py_CLEAR(self->pers_func);
3139 Py_CLEAR(self->inst_pers_func);
3140 Py_CLEAR(self->dispatch_table);
3141 return 0;
3144 static PyObject *
3145 Pickler_get_pers_func(Picklerobject *p)
3147 if (p->pers_func == NULL)
3148 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3149 else
3150 Py_INCREF(p->pers_func);
3151 return p->pers_func;
3154 static int
3155 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3157 if (v == NULL) {
3158 PyErr_SetString(PyExc_TypeError,
3159 "attribute deletion is not supported");
3160 return -1;
3162 Py_XDECREF(p->pers_func);
3163 Py_INCREF(v);
3164 p->pers_func = v;
3165 return 0;
3168 static int
3169 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3171 if (v == NULL) {
3172 PyErr_SetString(PyExc_TypeError,
3173 "attribute deletion is not supported");
3174 return -1;
3176 Py_XDECREF(p->inst_pers_func);
3177 Py_INCREF(v);
3178 p->inst_pers_func = v;
3179 return 0;
3182 static PyObject *
3183 Pickler_get_memo(Picklerobject *p)
3185 if (p->memo == NULL)
3186 PyErr_SetString(PyExc_AttributeError, "memo");
3187 else
3188 Py_INCREF(p->memo);
3189 return p->memo;
3192 static int
3193 Pickler_set_memo(Picklerobject *p, PyObject *v)
3195 if (v == NULL) {
3196 PyErr_SetString(PyExc_TypeError,
3197 "attribute deletion is not supported");
3198 return -1;
3200 if (!PyDict_Check(v)) {
3201 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3202 return -1;
3204 Py_XDECREF(p->memo);
3205 Py_INCREF(v);
3206 p->memo = v;
3207 return 0;
3210 static PyObject *
3211 Pickler_get_error(Picklerobject *p)
3213 /* why is this an attribute on the Pickler? */
3214 Py_INCREF(PicklingError);
3215 return PicklingError;
3218 static PyMemberDef Pickler_members[] = {
3219 {"binary", T_INT, offsetof(Picklerobject, bin)},
3220 {"fast", T_INT, offsetof(Picklerobject, fast)},
3221 {NULL}
3224 static PyGetSetDef Pickler_getsets[] = {
3225 {"persistent_id", (getter)Pickler_get_pers_func,
3226 (setter)Pickler_set_pers_func},
3227 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3228 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3229 {"PicklingError", (getter)Pickler_get_error, NULL},
3230 {NULL}
3233 PyDoc_STRVAR(Picklertype__doc__,
3234 "Objects that know how to pickle objects\n");
3236 static PyTypeObject Picklertype = {
3237 PyVarObject_HEAD_INIT(NULL, 0)
3238 "cPickle.Pickler", /*tp_name*/
3239 sizeof(Picklerobject), /*tp_basicsize*/
3241 (destructor)Pickler_dealloc, /* tp_dealloc */
3242 0, /* tp_print */
3243 0, /* tp_getattr */
3244 0, /* tp_setattr */
3245 0, /* tp_compare */
3246 0, /* tp_repr */
3247 0, /* tp_as_number */
3248 0, /* tp_as_sequence */
3249 0, /* tp_as_mapping */
3250 0, /* tp_hash */
3251 0, /* tp_call */
3252 0, /* tp_str */
3253 PyObject_GenericGetAttr, /* tp_getattro */
3254 PyObject_GenericSetAttr, /* tp_setattro */
3255 0, /* tp_as_buffer */
3256 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3257 Picklertype__doc__, /* tp_doc */
3258 (traverseproc)Pickler_traverse, /* tp_traverse */
3259 (inquiry)Pickler_clear, /* tp_clear */
3260 0, /* tp_richcompare */
3261 0, /* tp_weaklistoffset */
3262 0, /* tp_iter */
3263 0, /* tp_iternext */
3264 Pickler_methods, /* tp_methods */
3265 Pickler_members, /* tp_members */
3266 Pickler_getsets, /* tp_getset */
3269 static PyObject *
3270 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3272 PyObject *global = 0, *module;
3274 if (fc) {
3275 if (fc==Py_None) {
3276 PyErr_SetString(UnpicklingError, "Global and instance "
3277 "pickles are not supported.");
3278 return NULL;
3280 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3281 py_global_name, NULL);
3284 module = PySys_GetObject("modules");
3285 if (module == NULL)
3286 return NULL;
3288 module = PyDict_GetItem(module, py_module_name);
3289 if (module == NULL) {
3290 module = PyImport_Import(py_module_name);
3291 if (!module)
3292 return NULL;
3293 global = PyObject_GetAttr(module, py_global_name);
3294 Py_DECREF(module);
3296 else
3297 global = PyObject_GetAttr(module, py_global_name);
3298 return global;
3301 static int
3302 marker(Unpicklerobject *self)
3304 if (self->num_marks < 1) {
3305 PyErr_SetString(UnpicklingError, "could not find MARK");
3306 return -1;
3309 return self->marks[--self->num_marks];
3313 static int
3314 load_none(Unpicklerobject *self)
3316 PDATA_APPEND(self->stack, Py_None, -1);
3317 return 0;
3320 static int
3321 bad_readline(void)
3323 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3324 return -1;
3327 static int
3328 load_int(Unpicklerobject *self)
3330 PyObject *py_int = 0;
3331 char *endptr, *s;
3332 int len, res = -1;
3333 long l;
3335 if ((len = self->readline_func(self, &s)) < 0) return -1;
3336 if (len < 2) return bad_readline();
3337 if (!( s=pystrndup(s,len))) return -1;
3339 errno = 0;
3340 l = strtol(s, &endptr, 0);
3342 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3343 /* Hm, maybe we've got something long. Let's try reading
3344 it as a Python long object. */
3345 errno = 0;
3346 py_int = PyLong_FromString(s, NULL, 0);
3347 if (py_int == NULL) {
3348 PyErr_SetString(PyExc_ValueError,
3349 "could not convert string to int");
3350 goto finally;
3353 else {
3354 if (len == 3 && (l == 0 || l == 1)) {
3355 if (!( py_int = PyBool_FromLong(l))) goto finally;
3357 else {
3358 if (!( py_int = PyInt_FromLong(l))) goto finally;
3362 free(s);
3363 PDATA_PUSH(self->stack, py_int, -1);
3364 return 0;
3366 finally:
3367 free(s);
3369 return res;
3372 static int
3373 load_bool(Unpicklerobject *self, PyObject *boolean)
3375 assert(boolean == Py_True || boolean == Py_False);
3376 PDATA_APPEND(self->stack, boolean, -1);
3377 return 0;
3380 /* s contains x bytes of a little-endian integer. Return its value as a
3381 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3382 * int, but when x is 4 it's a signed one. This is an historical source
3383 * of x-platform bugs.
3385 static long
3386 calc_binint(char *s, int x)
3388 unsigned char c;
3389 int i;
3390 long l;
3392 for (i = 0, l = 0L; i < x; i++) {
3393 c = (unsigned char)s[i];
3394 l |= (long)c << (i * 8);
3396 #if SIZEOF_LONG > 4
3397 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3398 * is signed, so on a box with longs bigger than 4 bytes we need
3399 * to extend a BININT's sign bit to the full width.
3401 if (x == 4 && l & (1L << 31))
3402 l |= (~0L) << 32;
3403 #endif
3404 return l;
3408 static int
3409 load_binintx(Unpicklerobject *self, char *s, int x)
3411 PyObject *py_int = 0;
3412 long l;
3414 l = calc_binint(s, x);
3416 if (!( py_int = PyInt_FromLong(l)))
3417 return -1;
3419 PDATA_PUSH(self->stack, py_int, -1);
3420 return 0;
3424 static int
3425 load_binint(Unpicklerobject *self)
3427 char *s;
3429 if (self->read_func(self, &s, 4) < 0)
3430 return -1;
3432 return load_binintx(self, s, 4);
3436 static int
3437 load_binint1(Unpicklerobject *self)
3439 char *s;
3441 if (self->read_func(self, &s, 1) < 0)
3442 return -1;
3444 return load_binintx(self, s, 1);
3448 static int
3449 load_binint2(Unpicklerobject *self)
3451 char *s;
3453 if (self->read_func(self, &s, 2) < 0)
3454 return -1;
3456 return load_binintx(self, s, 2);
3459 static int
3460 load_long(Unpicklerobject *self)
3462 PyObject *l = 0;
3463 char *end, *s;
3464 int len, res = -1;
3466 if ((len = self->readline_func(self, &s)) < 0) return -1;
3467 if (len < 2) return bad_readline();
3468 if (!( s=pystrndup(s,len))) return -1;
3470 if (!( l = PyLong_FromString(s, &end, 0)))
3471 goto finally;
3473 free(s);
3474 PDATA_PUSH(self->stack, l, -1);
3475 return 0;
3477 finally:
3478 free(s);
3480 return res;
3483 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3484 * data following.
3486 static int
3487 load_counted_long(Unpicklerobject *self, int size)
3489 Py_ssize_t i;
3490 char *nbytes;
3491 unsigned char *pdata;
3492 PyObject *along;
3494 assert(size == 1 || size == 4);
3495 i = self->read_func(self, &nbytes, size);
3496 if (i < 0) return -1;
3498 size = calc_binint(nbytes, size);
3499 if (size < 0) {
3500 /* Corrupt or hostile pickle -- we never write one like
3501 * this.
3503 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3504 "byte count");
3505 return -1;
3508 if (size == 0)
3509 along = PyLong_FromLong(0L);
3510 else {
3511 /* Read the raw little-endian bytes & convert. */
3512 i = self->read_func(self, (char **)&pdata, size);
3513 if (i < 0) return -1;
3514 along = _PyLong_FromByteArray(pdata, (size_t)size,
3515 1 /* little endian */, 1 /* signed */);
3517 if (along == NULL)
3518 return -1;
3519 PDATA_PUSH(self->stack, along, -1);
3520 return 0;
3523 static int
3524 load_float(Unpicklerobject *self)
3526 PyObject *py_float = 0;
3527 char *endptr, *s;
3528 int len, res = -1;
3529 double d;
3531 if ((len = self->readline_func(self, &s)) < 0) return -1;
3532 if (len < 2) return bad_readline();
3533 if (!( s=pystrndup(s,len))) return -1;
3535 errno = 0;
3536 d = PyOS_ascii_strtod(s, &endptr);
3538 if ((errno == ERANGE && !(fabs(d) <= 1.0)) ||
3539 (endptr[0] != '\n') || (endptr[1] != '\0')) {
3540 PyErr_SetString(PyExc_ValueError,
3541 "could not convert string to float");
3542 goto finally;
3545 if (!( py_float = PyFloat_FromDouble(d)))
3546 goto finally;
3548 free(s);
3549 PDATA_PUSH(self->stack, py_float, -1);
3550 return 0;
3552 finally:
3553 free(s);
3555 return res;
3558 static int
3559 load_binfloat(Unpicklerobject *self)
3561 PyObject *py_float;
3562 double x;
3563 char *p;
3565 if (self->read_func(self, &p, 8) < 0)
3566 return -1;
3568 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3569 if (x == -1.0 && PyErr_Occurred())
3570 return -1;
3572 py_float = PyFloat_FromDouble(x);
3573 if (py_float == NULL)
3574 return -1;
3576 PDATA_PUSH(self->stack, py_float, -1);
3577 return 0;
3580 static int
3581 load_string(Unpicklerobject *self)
3583 PyObject *str = 0;
3584 int len, res = -1;
3585 char *s, *p;
3587 if ((len = self->readline_func(self, &s)) < 0) return -1;
3588 if (len < 2) return bad_readline();
3589 if (!( s=pystrndup(s,len))) return -1;
3592 /* Strip outermost quotes */
3593 while (s[len-1] <= ' ')
3594 len--;
3595 if(s[0]=='"' && s[len-1]=='"'){
3596 s[len-1] = '\0';
3597 p = s + 1 ;
3598 len -= 2;
3599 } else if(s[0]=='\'' && s[len-1]=='\''){
3600 s[len-1] = '\0';
3601 p = s + 1 ;
3602 len -= 2;
3603 } else
3604 goto insecure;
3605 /********************************************/
3607 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3608 free(s);
3609 if (str) {
3610 PDATA_PUSH(self->stack, str, -1);
3611 res = 0;
3613 return res;
3615 insecure:
3616 free(s);
3617 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3618 return -1;
3622 static int
3623 load_binstring(Unpicklerobject *self)
3625 PyObject *py_string = 0;
3626 long l;
3627 char *s;
3629 if (self->read_func(self, &s, 4) < 0) return -1;
3631 l = calc_binint(s, 4);
3632 if (l < 0) {
3633 /* Corrupt or hostile pickle -- we never write one like
3634 * this.
3636 PyErr_SetString(UnpicklingError,
3637 "BINSTRING pickle has negative byte count");
3638 return -1;
3641 if (self->read_func(self, &s, l) < 0)
3642 return -1;
3644 if (!( py_string = PyString_FromStringAndSize(s, l)))
3645 return -1;
3647 PDATA_PUSH(self->stack, py_string, -1);
3648 return 0;
3652 static int
3653 load_short_binstring(Unpicklerobject *self)
3655 PyObject *py_string = 0;
3656 unsigned char l;
3657 char *s;
3659 if (self->read_func(self, &s, 1) < 0)
3660 return -1;
3662 l = (unsigned char)s[0];
3664 if (self->read_func(self, &s, l) < 0) return -1;
3666 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
3668 PDATA_PUSH(self->stack, py_string, -1);
3669 return 0;
3673 #ifdef Py_USING_UNICODE
3674 static int
3675 load_unicode(Unpicklerobject *self)
3677 PyObject *str = 0;
3678 int len, res = -1;
3679 char *s;
3681 if ((len = self->readline_func(self, &s)) < 0) return -1;
3682 if (len < 1) return bad_readline();
3684 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3685 goto finally;
3687 PDATA_PUSH(self->stack, str, -1);
3688 return 0;
3690 finally:
3691 return res;
3693 #endif
3696 #ifdef Py_USING_UNICODE
3697 static int
3698 load_binunicode(Unpicklerobject *self)
3700 PyObject *unicode;
3701 long l;
3702 char *s;
3704 if (self->read_func(self, &s, 4) < 0) return -1;
3706 l = calc_binint(s, 4);
3707 if (l < 0) {
3708 /* Corrupt or hostile pickle -- we never write one like
3709 * this.
3711 PyErr_SetString(UnpicklingError,
3712 "BINUNICODE pickle has negative byte count");
3713 return -1;
3716 if (self->read_func(self, &s, l) < 0)
3717 return -1;
3719 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3720 return -1;
3722 PDATA_PUSH(self->stack, unicode, -1);
3723 return 0;
3725 #endif
3728 static int
3729 load_tuple(Unpicklerobject *self)
3731 PyObject *tup;
3732 int i;
3734 if ((i = marker(self)) < 0) return -1;
3735 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3736 PDATA_PUSH(self->stack, tup, -1);
3737 return 0;
3740 static int
3741 load_counted_tuple(Unpicklerobject *self, int len)
3743 PyObject *tup = PyTuple_New(len);
3745 if (tup == NULL)
3746 return -1;
3748 while (--len >= 0) {
3749 PyObject *element;
3751 PDATA_POP(self->stack, element);
3752 if (element == NULL)
3753 return -1;
3754 PyTuple_SET_ITEM(tup, len, element);
3756 PDATA_PUSH(self->stack, tup, -1);
3757 return 0;
3760 static int
3761 load_empty_list(Unpicklerobject *self)
3763 PyObject *list;
3765 if (!( list=PyList_New(0))) return -1;
3766 PDATA_PUSH(self->stack, list, -1);
3767 return 0;
3770 static int
3771 load_empty_dict(Unpicklerobject *self)
3773 PyObject *dict;
3775 if (!( dict=PyDict_New())) return -1;
3776 PDATA_PUSH(self->stack, dict, -1);
3777 return 0;
3781 static int
3782 load_list(Unpicklerobject *self)
3784 PyObject *list = 0;
3785 int i;
3787 if ((i = marker(self)) < 0) return -1;
3788 if (!( list=Pdata_popList(self->stack, i))) return -1;
3789 PDATA_PUSH(self->stack, list, -1);
3790 return 0;
3793 static int
3794 load_dict(Unpicklerobject *self)
3796 PyObject *dict, *key, *value;
3797 int i, j, k;
3799 if ((i = marker(self)) < 0) return -1;
3800 j=self->stack->length;
3802 if (!( dict = PyDict_New())) return -1;
3804 for (k = i+1; k < j; k += 2) {
3805 key =self->stack->data[k-1];
3806 value=self->stack->data[k ];
3807 if (PyDict_SetItem(dict, key, value) < 0) {
3808 Py_DECREF(dict);
3809 return -1;
3812 Pdata_clear(self->stack, i);
3813 PDATA_PUSH(self->stack, dict, -1);
3814 return 0;
3817 static PyObject *
3818 Instance_New(PyObject *cls, PyObject *args)
3820 PyObject *r = 0;
3822 if (PyClass_Check(cls)) {
3823 int l;
3825 if ((l=PyObject_Size(args)) < 0) goto err;
3826 if (!( l )) {
3827 PyObject *__getinitargs__;
3829 __getinitargs__ = PyObject_GetAttr(cls,
3830 __getinitargs___str);
3831 if (!__getinitargs__) {
3832 /* We have a class with no __getinitargs__,
3833 so bypass usual construction */
3834 PyObject *inst;
3836 PyErr_Clear();
3837 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3838 goto err;
3839 return inst;
3841 Py_DECREF(__getinitargs__);
3844 if ((r=PyInstance_New(cls, args, NULL))) return r;
3845 else goto err;
3848 if ((r=PyObject_CallObject(cls, args))) return r;
3850 err:
3852 PyObject *tp, *v, *tb, *tmp_value;
3854 PyErr_Fetch(&tp, &v, &tb);
3855 tmp_value = v;
3856 /* NULL occurs when there was a KeyboardInterrupt */
3857 if (tmp_value == NULL)
3858 tmp_value = Py_None;
3859 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3860 Py_XDECREF(v);
3861 v=r;
3863 PyErr_Restore(tp,v,tb);
3865 return NULL;
3869 static int
3870 load_obj(Unpicklerobject *self)
3872 PyObject *class, *tup, *obj=0;
3873 int i;
3875 if ((i = marker(self)) < 0) return -1;
3876 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3877 PDATA_POP(self->stack, class);
3878 if (class) {
3879 obj = Instance_New(class, tup);
3880 Py_DECREF(class);
3882 Py_DECREF(tup);
3884 if (! obj) return -1;
3885 PDATA_PUSH(self->stack, obj, -1);
3886 return 0;
3890 static int
3891 load_inst(Unpicklerobject *self)
3893 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3894 int i, len;
3895 char *s;
3897 if ((i = marker(self)) < 0) return -1;
3899 if ((len = self->readline_func(self, &s)) < 0) return -1;
3900 if (len < 2) return bad_readline();
3901 module_name = PyString_FromStringAndSize(s, len - 1);
3902 if (!module_name) return -1;
3904 if ((len = self->readline_func(self, &s)) >= 0) {
3905 if (len < 2) return bad_readline();
3906 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3907 class = find_class(module_name, class_name,
3908 self->find_class);
3909 Py_DECREF(class_name);
3912 Py_DECREF(module_name);
3914 if (! class) return -1;
3916 if ((tup=Pdata_popTuple(self->stack, i))) {
3917 obj = Instance_New(class, tup);
3918 Py_DECREF(tup);
3920 Py_DECREF(class);
3922 if (! obj) return -1;
3924 PDATA_PUSH(self->stack, obj, -1);
3925 return 0;
3928 static int
3929 load_newobj(Unpicklerobject *self)
3931 PyObject *args = NULL;
3932 PyObject *clsraw = NULL;
3933 PyTypeObject *cls; /* clsraw cast to its true type */
3934 PyObject *obj;
3936 /* Stack is ... cls argtuple, and we want to call
3937 * cls.__new__(cls, *argtuple).
3939 PDATA_POP(self->stack, args);
3940 if (args == NULL) goto Fail;
3941 if (! PyTuple_Check(args)) {
3942 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3943 "tuple.");
3944 goto Fail;
3947 PDATA_POP(self->stack, clsraw);
3948 cls = (PyTypeObject *)clsraw;
3949 if (cls == NULL) goto Fail;
3950 if (! PyType_Check(cls)) {
3951 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3952 "isn't a type object");
3953 goto Fail;
3955 if (cls->tp_new == NULL) {
3956 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3957 "has NULL tp_new");
3958 goto Fail;
3961 /* Call __new__. */
3962 obj = cls->tp_new(cls, args, NULL);
3963 if (obj == NULL) goto Fail;
3965 Py_DECREF(args);
3966 Py_DECREF(clsraw);
3967 PDATA_PUSH(self->stack, obj, -1);
3968 return 0;
3970 Fail:
3971 Py_XDECREF(args);
3972 Py_XDECREF(clsraw);
3973 return -1;
3976 static int
3977 load_global(Unpicklerobject *self)
3979 PyObject *class = 0, *module_name = 0, *class_name = 0;
3980 int len;
3981 char *s;
3983 if ((len = self->readline_func(self, &s)) < 0) return -1;
3984 if (len < 2) return bad_readline();
3985 module_name = PyString_FromStringAndSize(s, len - 1);
3986 if (!module_name) return -1;
3988 if ((len = self->readline_func(self, &s)) >= 0) {
3989 if (len < 2) {
3990 Py_DECREF(module_name);
3991 return bad_readline();
3993 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3994 class = find_class(module_name, class_name,
3995 self->find_class);
3996 Py_DECREF(class_name);
3999 Py_DECREF(module_name);
4001 if (! class) return -1;
4002 PDATA_PUSH(self->stack, class, -1);
4003 return 0;
4007 static int
4008 load_persid(Unpicklerobject *self)
4010 PyObject *pid = 0;
4011 int len;
4012 char *s;
4014 if (self->pers_func) {
4015 if ((len = self->readline_func(self, &s)) < 0) return -1;
4016 if (len < 2) return bad_readline();
4018 pid = PyString_FromStringAndSize(s, len - 1);
4019 if (!pid) return -1;
4021 if (PyList_Check(self->pers_func)) {
4022 if (PyList_Append(self->pers_func, pid) < 0) {
4023 Py_DECREF(pid);
4024 return -1;
4027 else {
4028 ARG_TUP(self, pid);
4029 if (self->arg) {
4030 pid = PyObject_Call(self->pers_func, self->arg,
4031 NULL);
4032 FREE_ARG_TUP(self);
4036 if (! pid) return -1;
4038 PDATA_PUSH(self->stack, pid, -1);
4039 return 0;
4041 else {
4042 PyErr_SetString(UnpicklingError,
4043 "A load persistent id instruction was encountered,\n"
4044 "but no persistent_load function was specified.");
4045 return -1;
4049 static int
4050 load_binpersid(Unpicklerobject *self)
4052 PyObject *pid = 0;
4054 if (self->pers_func) {
4055 PDATA_POP(self->stack, pid);
4056 if (! pid) return -1;
4058 if (PyList_Check(self->pers_func)) {
4059 if (PyList_Append(self->pers_func, pid) < 0) {
4060 Py_DECREF(pid);
4061 return -1;
4064 else {
4065 ARG_TUP(self, pid);
4066 if (self->arg) {
4067 pid = PyObject_Call(self->pers_func, self->arg,
4068 NULL);
4069 FREE_ARG_TUP(self);
4071 if (! pid) return -1;
4074 PDATA_PUSH(self->stack, pid, -1);
4075 return 0;
4077 else {
4078 PyErr_SetString(UnpicklingError,
4079 "A load persistent id instruction was encountered,\n"
4080 "but no persistent_load function was specified.");
4081 return -1;
4086 static int
4087 load_pop(Unpicklerobject *self)
4089 int len = self->stack->length;
4091 /* Note that we split the (pickle.py) stack into two stacks,
4092 an object stack and a mark stack. We have to be clever and
4093 pop the right one. We do this by looking at the top of the
4094 mark stack first, and only signalling a stack underflow if
4095 the object stack is empty and the mark stack doesn't match
4096 our expectations.
4098 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
4099 self->num_marks--;
4100 } else if (len >= 0) {
4101 len--;
4102 Py_DECREF(self->stack->data[len]);
4103 self->stack->length = len;
4104 } else {
4105 return stackUnderflow();
4107 return 0;
4111 static int
4112 load_pop_mark(Unpicklerobject *self)
4114 int i;
4116 if ((i = marker(self)) < 0)
4117 return -1;
4119 Pdata_clear(self->stack, i);
4121 return 0;
4125 static int
4126 load_dup(Unpicklerobject *self)
4128 PyObject *last;
4129 int len;
4131 if ((len = self->stack->length) <= 0) return stackUnderflow();
4132 last=self->stack->data[len-1];
4133 Py_INCREF(last);
4134 PDATA_PUSH(self->stack, last, -1);
4135 return 0;
4139 static int
4140 load_get(Unpicklerobject *self)
4142 PyObject *py_str = 0, *value = 0;
4143 int len;
4144 char *s;
4145 int rc;
4147 if ((len = self->readline_func(self, &s)) < 0) return -1;
4148 if (len < 2) return bad_readline();
4150 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
4152 value = PyDict_GetItem(self->memo, py_str);
4153 if (! value) {
4154 PyErr_SetObject(BadPickleGet, py_str);
4155 rc = -1;
4157 else {
4158 PDATA_APPEND(self->stack, value, -1);
4159 rc = 0;
4162 Py_DECREF(py_str);
4163 return rc;
4167 static int
4168 load_binget(Unpicklerobject *self)
4170 PyObject *py_key = 0, *value = 0;
4171 unsigned char key;
4172 char *s;
4173 int rc;
4175 if (self->read_func(self, &s, 1) < 0) return -1;
4177 key = (unsigned char)s[0];
4178 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4180 value = PyDict_GetItem(self->memo, py_key);
4181 if (! value) {
4182 PyErr_SetObject(BadPickleGet, py_key);
4183 rc = -1;
4185 else {
4186 PDATA_APPEND(self->stack, value, -1);
4187 rc = 0;
4190 Py_DECREF(py_key);
4191 return rc;
4195 static int
4196 load_long_binget(Unpicklerobject *self)
4198 PyObject *py_key = 0, *value = 0;
4199 unsigned char c;
4200 char *s;
4201 long key;
4202 int rc;
4204 if (self->read_func(self, &s, 4) < 0) return -1;
4206 c = (unsigned char)s[0];
4207 key = (long)c;
4208 c = (unsigned char)s[1];
4209 key |= (long)c << 8;
4210 c = (unsigned char)s[2];
4211 key |= (long)c << 16;
4212 c = (unsigned char)s[3];
4213 key |= (long)c << 24;
4215 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4217 value = PyDict_GetItem(self->memo, py_key);
4218 if (! value) {
4219 PyErr_SetObject(BadPickleGet, py_key);
4220 rc = -1;
4222 else {
4223 PDATA_APPEND(self->stack, value, -1);
4224 rc = 0;
4227 Py_DECREF(py_key);
4228 return rc;
4231 /* Push an object from the extension registry (EXT[124]). nbytes is
4232 * the number of bytes following the opcode, holding the index (code) value.
4234 static int
4235 load_extension(Unpicklerobject *self, int nbytes)
4237 char *codebytes; /* the nbytes bytes after the opcode */
4238 long code; /* calc_binint returns long */
4239 PyObject *py_code; /* code as a Python int */
4240 PyObject *obj; /* the object to push */
4241 PyObject *pair; /* (module_name, class_name) */
4242 PyObject *module_name, *class_name;
4244 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4245 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4246 code = calc_binint(codebytes, nbytes);
4247 if (code <= 0) { /* note that 0 is forbidden */
4248 /* Corrupt or hostile pickle. */
4249 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4250 return -1;
4253 /* Look for the code in the cache. */
4254 py_code = PyInt_FromLong(code);
4255 if (py_code == NULL) return -1;
4256 obj = PyDict_GetItem(extension_cache, py_code);
4257 if (obj != NULL) {
4258 /* Bingo. */
4259 Py_DECREF(py_code);
4260 PDATA_APPEND(self->stack, obj, -1);
4261 return 0;
4264 /* Look up the (module_name, class_name) pair. */
4265 pair = PyDict_GetItem(inverted_registry, py_code);
4266 if (pair == NULL) {
4267 Py_DECREF(py_code);
4268 PyErr_Format(PyExc_ValueError, "unregistered extension "
4269 "code %ld", code);
4270 return -1;
4272 /* Since the extension registry is manipulable via Python code,
4273 * confirm that pair is really a 2-tuple of strings.
4275 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4276 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4277 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4278 Py_DECREF(py_code);
4279 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4280 "isn't a 2-tuple of strings", code);
4281 return -1;
4283 /* Load the object. */
4284 obj = find_class(module_name, class_name, self->find_class);
4285 if (obj == NULL) {
4286 Py_DECREF(py_code);
4287 return -1;
4289 /* Cache code -> obj. */
4290 code = PyDict_SetItem(extension_cache, py_code, obj);
4291 Py_DECREF(py_code);
4292 if (code < 0) {
4293 Py_DECREF(obj);
4294 return -1;
4296 PDATA_PUSH(self->stack, obj, -1);
4297 return 0;
4300 static int
4301 load_put(Unpicklerobject *self)
4303 PyObject *py_str = 0, *value = 0;
4304 int len, l;
4305 char *s;
4307 if ((l = self->readline_func(self, &s)) < 0) return -1;
4308 if (l < 2) return bad_readline();
4309 if (!( len=self->stack->length )) return stackUnderflow();
4310 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4311 value=self->stack->data[len-1];
4312 l=PyDict_SetItem(self->memo, py_str, value);
4313 Py_DECREF(py_str);
4314 return l;
4318 static int
4319 load_binput(Unpicklerobject *self)
4321 PyObject *py_key = 0, *value = 0;
4322 unsigned char key;
4323 char *s;
4324 int len;
4326 if (self->read_func(self, &s, 1) < 0) return -1;
4327 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4329 key = (unsigned char)s[0];
4331 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4332 value=self->stack->data[len-1];
4333 len=PyDict_SetItem(self->memo, py_key, value);
4334 Py_DECREF(py_key);
4335 return len;
4339 static int
4340 load_long_binput(Unpicklerobject *self)
4342 PyObject *py_key = 0, *value = 0;
4343 long key;
4344 unsigned char c;
4345 char *s;
4346 int len;
4348 if (self->read_func(self, &s, 4) < 0) return -1;
4349 if (!( len=self->stack->length )) return stackUnderflow();
4351 c = (unsigned char)s[0];
4352 key = (long)c;
4353 c = (unsigned char)s[1];
4354 key |= (long)c << 8;
4355 c = (unsigned char)s[2];
4356 key |= (long)c << 16;
4357 c = (unsigned char)s[3];
4358 key |= (long)c << 24;
4360 if (!( py_key = PyInt_FromLong(key))) return -1;
4361 value=self->stack->data[len-1];
4362 len=PyDict_SetItem(self->memo, py_key, value);
4363 Py_DECREF(py_key);
4364 return len;
4368 static int
4369 do_append(Unpicklerobject *self, int x)
4371 PyObject *value = 0, *list = 0, *append_method = 0;
4372 int len, i;
4374 len=self->stack->length;
4375 if (!( len >= x && x > 0 )) return stackUnderflow();
4376 /* nothing to do */
4377 if (len==x) return 0;
4379 list=self->stack->data[x-1];
4381 if (PyList_Check(list)) {
4382 PyObject *slice;
4383 int list_len;
4385 slice=Pdata_popList(self->stack, x);
4386 if (! slice) return -1;
4387 list_len = PyList_GET_SIZE(list);
4388 i=PyList_SetSlice(list, list_len, list_len, slice);
4389 Py_DECREF(slice);
4390 return i;
4392 else {
4394 if (!( append_method = PyObject_GetAttr(list, append_str)))
4395 return -1;
4397 for (i = x; i < len; i++) {
4398 PyObject *junk;
4400 value=self->stack->data[i];
4401 junk=0;
4402 ARG_TUP(self, value);
4403 if (self->arg) {
4404 junk = PyObject_Call(append_method, self->arg,
4405 NULL);
4406 FREE_ARG_TUP(self);
4408 if (! junk) {
4409 Pdata_clear(self->stack, i+1);
4410 self->stack->length=x;
4411 Py_DECREF(append_method);
4412 return -1;
4414 Py_DECREF(junk);
4416 self->stack->length=x;
4417 Py_DECREF(append_method);
4420 return 0;
4424 static int
4425 load_append(Unpicklerobject *self)
4427 return do_append(self, self->stack->length - 1);
4431 static int
4432 load_appends(Unpicklerobject *self)
4434 return do_append(self, marker(self));
4438 static int
4439 do_setitems(Unpicklerobject *self, int x)
4441 PyObject *value = 0, *key = 0, *dict = 0;
4442 int len, i, r=0;
4444 if (!( (len=self->stack->length) >= x
4445 && x > 0 )) return stackUnderflow();
4447 dict=self->stack->data[x-1];
4449 for (i = x+1; i < len; i += 2) {
4450 key =self->stack->data[i-1];
4451 value=self->stack->data[i ];
4452 if (PyObject_SetItem(dict, key, value) < 0) {
4453 r=-1;
4454 break;
4458 Pdata_clear(self->stack, x);
4460 return r;
4464 static int
4465 load_setitem(Unpicklerobject *self)
4467 return do_setitems(self, self->stack->length - 2);
4470 static int
4471 load_setitems(Unpicklerobject *self)
4473 return do_setitems(self, marker(self));
4477 static int
4478 load_build(Unpicklerobject *self)
4480 PyObject *state, *inst, *slotstate;
4481 PyObject *__setstate__;
4482 PyObject *d_key, *d_value;
4483 Py_ssize_t i;
4484 int res = -1;
4486 /* Stack is ... instance, state. We want to leave instance at
4487 * the stack top, possibly mutated via instance.__setstate__(state).
4489 if (self->stack->length < 2)
4490 return stackUnderflow();
4491 PDATA_POP(self->stack, state);
4492 if (state == NULL)
4493 return -1;
4494 inst = self->stack->data[self->stack->length - 1];
4496 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4497 if (__setstate__ != NULL) {
4498 PyObject *junk = NULL;
4500 /* The explicit __setstate__ is responsible for everything. */
4501 ARG_TUP(self, state);
4502 if (self->arg) {
4503 junk = PyObject_Call(__setstate__, self->arg, NULL);
4504 FREE_ARG_TUP(self);
4506 Py_DECREF(__setstate__);
4507 if (junk == NULL)
4508 return -1;
4509 Py_DECREF(junk);
4510 return 0;
4512 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4513 return -1;
4514 PyErr_Clear();
4516 /* A default __setstate__. First see whether state embeds a
4517 * slot state dict too (a proto 2 addition).
4519 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4520 PyObject *temp = state;
4521 state = PyTuple_GET_ITEM(temp, 0);
4522 slotstate = PyTuple_GET_ITEM(temp, 1);
4523 Py_INCREF(state);
4524 Py_INCREF(slotstate);
4525 Py_DECREF(temp);
4527 else
4528 slotstate = NULL;
4530 /* Set inst.__dict__ from the state dict (if any). */
4531 if (state != Py_None) {
4532 PyObject *dict;
4533 if (! PyDict_Check(state)) {
4534 PyErr_SetString(UnpicklingError, "state is not a "
4535 "dictionary");
4536 goto finally;
4538 dict = PyObject_GetAttr(inst, __dict___str);
4539 if (dict == NULL)
4540 goto finally;
4542 i = 0;
4543 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4544 /* normally the keys for instance attributes are
4545 interned. we should try to do that here. */
4546 Py_INCREF(d_key);
4547 if (PyString_CheckExact(d_key))
4548 PyString_InternInPlace(&d_key);
4549 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
4550 Py_DECREF(d_key);
4551 goto finally;
4553 Py_DECREF(d_key);
4555 Py_DECREF(dict);
4558 /* Also set instance attributes from the slotstate dict (if any). */
4559 if (slotstate != NULL) {
4560 if (! PyDict_Check(slotstate)) {
4561 PyErr_SetString(UnpicklingError, "slot state is not "
4562 "a dictionary");
4563 goto finally;
4565 i = 0;
4566 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4567 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4568 goto finally;
4571 res = 0;
4573 finally:
4574 Py_DECREF(state);
4575 Py_XDECREF(slotstate);
4576 return res;
4580 static int
4581 load_mark(Unpicklerobject *self)
4583 int s;
4585 /* Note that we split the (pickle.py) stack into two stacks, an
4586 object stack and a mark stack. Here we push a mark onto the
4587 mark stack.
4590 if ((self->num_marks + 1) >= self->marks_size) {
4591 int *marks;
4592 s=self->marks_size+20;
4593 if (s <= self->num_marks) s=self->num_marks + 1;
4594 if (self->marks == NULL)
4595 marks=(int *)malloc(s * sizeof(int));
4596 else
4597 marks=(int *)realloc(self->marks,
4598 s * sizeof(int));
4599 if (!marks) {
4600 PyErr_NoMemory();
4601 return -1;
4603 self->marks = marks;
4604 self->marks_size = s;
4607 self->marks[self->num_marks++] = self->stack->length;
4609 return 0;
4612 static int
4613 load_reduce(Unpicklerobject *self)
4615 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4617 PDATA_POP(self->stack, arg_tup);
4618 if (! arg_tup) return -1;
4619 PDATA_POP(self->stack, callable);
4620 if (callable) {
4621 ob = Instance_New(callable, arg_tup);
4622 Py_DECREF(callable);
4624 Py_DECREF(arg_tup);
4626 if (! ob) return -1;
4628 PDATA_PUSH(self->stack, ob, -1);
4629 return 0;
4632 /* Just raises an error if we don't know the protocol specified. PROTO
4633 * is the first opcode for protocols >= 2.
4635 static int
4636 load_proto(Unpicklerobject *self)
4638 int i;
4639 char *protobyte;
4641 i = self->read_func(self, &protobyte, 1);
4642 if (i < 0)
4643 return -1;
4645 i = calc_binint(protobyte, 1);
4646 /* No point checking for < 0, since calc_binint returns an unsigned
4647 * int when chewing on 1 byte.
4649 assert(i >= 0);
4650 if (i <= HIGHEST_PROTOCOL)
4651 return 0;
4653 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4654 return -1;
4657 static PyObject *
4658 load(Unpicklerobject *self)
4660 PyObject *err = 0, *val = 0;
4661 char *s;
4663 self->num_marks = 0;
4664 if (self->stack->length) Pdata_clear(self->stack, 0);
4666 while (1) {
4667 if (self->read_func(self, &s, 1) < 0)
4668 break;
4670 switch (s[0]) {
4671 case NONE:
4672 if (load_none(self) < 0)
4673 break;
4674 continue;
4676 case BININT:
4677 if (load_binint(self) < 0)
4678 break;
4679 continue;
4681 case BININT1:
4682 if (load_binint1(self) < 0)
4683 break;
4684 continue;
4686 case BININT2:
4687 if (load_binint2(self) < 0)
4688 break;
4689 continue;
4691 case INT:
4692 if (load_int(self) < 0)
4693 break;
4694 continue;
4696 case LONG:
4697 if (load_long(self) < 0)
4698 break;
4699 continue;
4701 case LONG1:
4702 if (load_counted_long(self, 1) < 0)
4703 break;
4704 continue;
4706 case LONG4:
4707 if (load_counted_long(self, 4) < 0)
4708 break;
4709 continue;
4711 case FLOAT:
4712 if (load_float(self) < 0)
4713 break;
4714 continue;
4716 case BINFLOAT:
4717 if (load_binfloat(self) < 0)
4718 break;
4719 continue;
4721 case BINSTRING:
4722 if (load_binstring(self) < 0)
4723 break;
4724 continue;
4726 case SHORT_BINSTRING:
4727 if (load_short_binstring(self) < 0)
4728 break;
4729 continue;
4731 case STRING:
4732 if (load_string(self) < 0)
4733 break;
4734 continue;
4736 #ifdef Py_USING_UNICODE
4737 case UNICODE:
4738 if (load_unicode(self) < 0)
4739 break;
4740 continue;
4742 case BINUNICODE:
4743 if (load_binunicode(self) < 0)
4744 break;
4745 continue;
4746 #endif
4748 case EMPTY_TUPLE:
4749 if (load_counted_tuple(self, 0) < 0)
4750 break;
4751 continue;
4753 case TUPLE1:
4754 if (load_counted_tuple(self, 1) < 0)
4755 break;
4756 continue;
4758 case TUPLE2:
4759 if (load_counted_tuple(self, 2) < 0)
4760 break;
4761 continue;
4763 case TUPLE3:
4764 if (load_counted_tuple(self, 3) < 0)
4765 break;
4766 continue;
4768 case TUPLE:
4769 if (load_tuple(self) < 0)
4770 break;
4771 continue;
4773 case EMPTY_LIST:
4774 if (load_empty_list(self) < 0)
4775 break;
4776 continue;
4778 case LIST:
4779 if (load_list(self) < 0)
4780 break;
4781 continue;
4783 case EMPTY_DICT:
4784 if (load_empty_dict(self) < 0)
4785 break;
4786 continue;
4788 case DICT:
4789 if (load_dict(self) < 0)
4790 break;
4791 continue;
4793 case OBJ:
4794 if (load_obj(self) < 0)
4795 break;
4796 continue;
4798 case INST:
4799 if (load_inst(self) < 0)
4800 break;
4801 continue;
4803 case NEWOBJ:
4804 if (load_newobj(self) < 0)
4805 break;
4806 continue;
4808 case GLOBAL:
4809 if (load_global(self) < 0)
4810 break;
4811 continue;
4813 case APPEND:
4814 if (load_append(self) < 0)
4815 break;
4816 continue;
4818 case APPENDS:
4819 if (load_appends(self) < 0)
4820 break;
4821 continue;
4823 case BUILD:
4824 if (load_build(self) < 0)
4825 break;
4826 continue;
4828 case DUP:
4829 if (load_dup(self) < 0)
4830 break;
4831 continue;
4833 case BINGET:
4834 if (load_binget(self) < 0)
4835 break;
4836 continue;
4838 case LONG_BINGET:
4839 if (load_long_binget(self) < 0)
4840 break;
4841 continue;
4843 case GET:
4844 if (load_get(self) < 0)
4845 break;
4846 continue;
4848 case EXT1:
4849 if (load_extension(self, 1) < 0)
4850 break;
4851 continue;
4853 case EXT2:
4854 if (load_extension(self, 2) < 0)
4855 break;
4856 continue;
4858 case EXT4:
4859 if (load_extension(self, 4) < 0)
4860 break;
4861 continue;
4862 case MARK:
4863 if (load_mark(self) < 0)
4864 break;
4865 continue;
4867 case BINPUT:
4868 if (load_binput(self) < 0)
4869 break;
4870 continue;
4872 case LONG_BINPUT:
4873 if (load_long_binput(self) < 0)
4874 break;
4875 continue;
4877 case PUT:
4878 if (load_put(self) < 0)
4879 break;
4880 continue;
4882 case POP:
4883 if (load_pop(self) < 0)
4884 break;
4885 continue;
4887 case POP_MARK:
4888 if (load_pop_mark(self) < 0)
4889 break;
4890 continue;
4892 case SETITEM:
4893 if (load_setitem(self) < 0)
4894 break;
4895 continue;
4897 case SETITEMS:
4898 if (load_setitems(self) < 0)
4899 break;
4900 continue;
4902 case STOP:
4903 break;
4905 case PERSID:
4906 if (load_persid(self) < 0)
4907 break;
4908 continue;
4910 case BINPERSID:
4911 if (load_binpersid(self) < 0)
4912 break;
4913 continue;
4915 case REDUCE:
4916 if (load_reduce(self) < 0)
4917 break;
4918 continue;
4920 case PROTO:
4921 if (load_proto(self) < 0)
4922 break;
4923 continue;
4925 case NEWTRUE:
4926 if (load_bool(self, Py_True) < 0)
4927 break;
4928 continue;
4930 case NEWFALSE:
4931 if (load_bool(self, Py_False) < 0)
4932 break;
4933 continue;
4935 case '\0':
4936 /* end of file */
4937 PyErr_SetNone(PyExc_EOFError);
4938 break;
4940 default:
4941 cPickle_ErrFormat(UnpicklingError,
4942 "invalid load key, '%s'.",
4943 "c", s[0]);
4944 return NULL;
4947 break;
4950 if ((err = PyErr_Occurred())) {
4951 if (err == PyExc_EOFError) {
4952 PyErr_SetNone(PyExc_EOFError);
4954 return NULL;
4957 PDATA_POP(self->stack, val);
4958 return val;
4962 /* No-load functions to support noload, which is used to
4963 find persistent references. */
4965 static int
4966 noload_obj(Unpicklerobject *self)
4968 int i;
4970 if ((i = marker(self)) < 0) return -1;
4971 return Pdata_clear(self->stack, i+1);
4975 static int
4976 noload_inst(Unpicklerobject *self)
4978 int i;
4979 char *s;
4981 if ((i = marker(self)) < 0) return -1;
4982 Pdata_clear(self->stack, i);
4983 if (self->readline_func(self, &s) < 0) return -1;
4984 if (self->readline_func(self, &s) < 0) return -1;
4985 PDATA_APPEND(self->stack, Py_None, -1);
4986 return 0;
4989 static int
4990 noload_newobj(Unpicklerobject *self)
4992 PyObject *obj;
4994 PDATA_POP(self->stack, obj); /* pop argtuple */
4995 if (obj == NULL) return -1;
4996 Py_DECREF(obj);
4998 PDATA_POP(self->stack, obj); /* pop cls */
4999 if (obj == NULL) return -1;
5000 Py_DECREF(obj);
5002 PDATA_APPEND(self->stack, Py_None, -1);
5003 return 0;
5006 static int
5007 noload_global(Unpicklerobject *self)
5009 char *s;
5011 if (self->readline_func(self, &s) < 0) return -1;
5012 if (self->readline_func(self, &s) < 0) return -1;
5013 PDATA_APPEND(self->stack, Py_None,-1);
5014 return 0;
5017 static int
5018 noload_reduce(Unpicklerobject *self)
5021 if (self->stack->length < 2) return stackUnderflow();
5022 Pdata_clear(self->stack, self->stack->length-2);
5023 PDATA_APPEND(self->stack, Py_None,-1);
5024 return 0;
5027 static int
5028 noload_build(Unpicklerobject *self) {
5030 if (self->stack->length < 1) return stackUnderflow();
5031 Pdata_clear(self->stack, self->stack->length-1);
5032 return 0;
5035 static int
5036 noload_extension(Unpicklerobject *self, int nbytes)
5038 char *codebytes;
5040 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
5041 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
5042 PDATA_APPEND(self->stack, Py_None, -1);
5043 return 0;
5047 static PyObject *
5048 noload(Unpicklerobject *self)
5050 PyObject *err = 0, *val = 0;
5051 char *s;
5053 self->num_marks = 0;
5054 Pdata_clear(self->stack, 0);
5056 while (1) {
5057 if (self->read_func(self, &s, 1) < 0)
5058 break;
5060 switch (s[0]) {
5061 case NONE:
5062 if (load_none(self) < 0)
5063 break;
5064 continue;
5066 case BININT:
5067 if (load_binint(self) < 0)
5068 break;
5069 continue;
5071 case BININT1:
5072 if (load_binint1(self) < 0)
5073 break;
5074 continue;
5076 case BININT2:
5077 if (load_binint2(self) < 0)
5078 break;
5079 continue;
5081 case INT:
5082 if (load_int(self) < 0)
5083 break;
5084 continue;
5086 case LONG:
5087 if (load_long(self) < 0)
5088 break;
5089 continue;
5091 case LONG1:
5092 if (load_counted_long(self, 1) < 0)
5093 break;
5094 continue;
5096 case LONG4:
5097 if (load_counted_long(self, 4) < 0)
5098 break;
5099 continue;
5101 case FLOAT:
5102 if (load_float(self) < 0)
5103 break;
5104 continue;
5106 case BINFLOAT:
5107 if (load_binfloat(self) < 0)
5108 break;
5109 continue;
5111 case BINSTRING:
5112 if (load_binstring(self) < 0)
5113 break;
5114 continue;
5116 case SHORT_BINSTRING:
5117 if (load_short_binstring(self) < 0)
5118 break;
5119 continue;
5121 case STRING:
5122 if (load_string(self) < 0)
5123 break;
5124 continue;
5126 #ifdef Py_USING_UNICODE
5127 case UNICODE:
5128 if (load_unicode(self) < 0)
5129 break;
5130 continue;
5132 case BINUNICODE:
5133 if (load_binunicode(self) < 0)
5134 break;
5135 continue;
5136 #endif
5138 case EMPTY_TUPLE:
5139 if (load_counted_tuple(self, 0) < 0)
5140 break;
5141 continue;
5143 case TUPLE1:
5144 if (load_counted_tuple(self, 1) < 0)
5145 break;
5146 continue;
5148 case TUPLE2:
5149 if (load_counted_tuple(self, 2) < 0)
5150 break;
5151 continue;
5153 case TUPLE3:
5154 if (load_counted_tuple(self, 3) < 0)
5155 break;
5156 continue;
5158 case TUPLE:
5159 if (load_tuple(self) < 0)
5160 break;
5161 continue;
5163 case EMPTY_LIST:
5164 if (load_empty_list(self) < 0)
5165 break;
5166 continue;
5168 case LIST:
5169 if (load_list(self) < 0)
5170 break;
5171 continue;
5173 case EMPTY_DICT:
5174 if (load_empty_dict(self) < 0)
5175 break;
5176 continue;
5178 case DICT:
5179 if (load_dict(self) < 0)
5180 break;
5181 continue;
5183 case OBJ:
5184 if (noload_obj(self) < 0)
5185 break;
5186 continue;
5188 case INST:
5189 if (noload_inst(self) < 0)
5190 break;
5191 continue;
5193 case NEWOBJ:
5194 if (noload_newobj(self) < 0)
5195 break;
5196 continue;
5198 case GLOBAL:
5199 if (noload_global(self) < 0)
5200 break;
5201 continue;
5203 case APPEND:
5204 if (load_append(self) < 0)
5205 break;
5206 continue;
5208 case APPENDS:
5209 if (load_appends(self) < 0)
5210 break;
5211 continue;
5213 case BUILD:
5214 if (noload_build(self) < 0)
5215 break;
5216 continue;
5218 case DUP:
5219 if (load_dup(self) < 0)
5220 break;
5221 continue;
5223 case BINGET:
5224 if (load_binget(self) < 0)
5225 break;
5226 continue;
5228 case LONG_BINGET:
5229 if (load_long_binget(self) < 0)
5230 break;
5231 continue;
5233 case GET:
5234 if (load_get(self) < 0)
5235 break;
5236 continue;
5238 case EXT1:
5239 if (noload_extension(self, 1) < 0)
5240 break;
5241 continue;
5243 case EXT2:
5244 if (noload_extension(self, 2) < 0)
5245 break;
5246 continue;
5248 case EXT4:
5249 if (noload_extension(self, 4) < 0)
5250 break;
5251 continue;
5253 case MARK:
5254 if (load_mark(self) < 0)
5255 break;
5256 continue;
5258 case BINPUT:
5259 if (load_binput(self) < 0)
5260 break;
5261 continue;
5263 case LONG_BINPUT:
5264 if (load_long_binput(self) < 0)
5265 break;
5266 continue;
5268 case PUT:
5269 if (load_put(self) < 0)
5270 break;
5271 continue;
5273 case POP:
5274 if (load_pop(self) < 0)
5275 break;
5276 continue;
5278 case POP_MARK:
5279 if (load_pop_mark(self) < 0)
5280 break;
5281 continue;
5283 case SETITEM:
5284 if (load_setitem(self) < 0)
5285 break;
5286 continue;
5288 case SETITEMS:
5289 if (load_setitems(self) < 0)
5290 break;
5291 continue;
5293 case STOP:
5294 break;
5296 case PERSID:
5297 if (load_persid(self) < 0)
5298 break;
5299 continue;
5301 case BINPERSID:
5302 if (load_binpersid(self) < 0)
5303 break;
5304 continue;
5306 case REDUCE:
5307 if (noload_reduce(self) < 0)
5308 break;
5309 continue;
5311 case PROTO:
5312 if (load_proto(self) < 0)
5313 break;
5314 continue;
5316 case NEWTRUE:
5317 if (load_bool(self, Py_True) < 0)
5318 break;
5319 continue;
5321 case NEWFALSE:
5322 if (load_bool(self, Py_False) < 0)
5323 break;
5324 continue;
5325 default:
5326 cPickle_ErrFormat(UnpicklingError,
5327 "invalid load key, '%s'.",
5328 "c", s[0]);
5329 return NULL;
5332 break;
5335 if ((err = PyErr_Occurred())) {
5336 if (err == PyExc_EOFError) {
5337 PyErr_SetNone(PyExc_EOFError);
5339 return NULL;
5342 PDATA_POP(self->stack, val);
5343 return val;
5347 static PyObject *
5348 Unpickler_load(Unpicklerobject *self, PyObject *unused)
5350 return load(self);
5353 static PyObject *
5354 Unpickler_noload(Unpicklerobject *self, PyObject *unused)
5356 return noload(self);
5360 static struct PyMethodDef Unpickler_methods[] = {
5361 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
5362 PyDoc_STR("load() -- Load a pickle")
5364 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
5365 PyDoc_STR(
5366 "noload() -- not load a pickle, but go through most of the motions\n"
5367 "\n"
5368 "This function can be used to read past a pickle without instantiating\n"
5369 "any objects or importing any modules. It can also be used to find all\n"
5370 "persistent references without instantiating any objects or importing\n"
5371 "any modules.\n")
5373 {NULL, NULL} /* sentinel */
5377 static Unpicklerobject *
5378 newUnpicklerobject(PyObject *f)
5380 Unpicklerobject *self;
5382 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5383 return NULL;
5385 self->file = NULL;
5386 self->arg = NULL;
5387 self->stack = (Pdata*)Pdata_New();
5388 self->pers_func = NULL;
5389 self->last_string = NULL;
5390 self->marks = NULL;
5391 self->num_marks = 0;
5392 self->marks_size = 0;
5393 self->buf_size = 0;
5394 self->read = NULL;
5395 self->readline = NULL;
5396 self->find_class = NULL;
5398 if (!( self->memo = PyDict_New()))
5399 goto err;
5401 if (!self->stack)
5402 goto err;
5404 Py_INCREF(f);
5405 self->file = f;
5407 /* Set read, readline based on type of f */
5408 if (PyFile_Check(f)) {
5409 self->fp = PyFile_AsFile(f);
5410 if (self->fp == NULL) {
5411 PyErr_SetString(PyExc_ValueError,
5412 "I/O operation on closed file");
5413 goto err;
5415 self->read_func = read_file;
5416 self->readline_func = readline_file;
5418 else if (PycStringIO_InputCheck(f)) {
5419 self->fp = NULL;
5420 self->read_func = read_cStringIO;
5421 self->readline_func = readline_cStringIO;
5423 else {
5425 self->fp = NULL;
5426 self->read_func = read_other;
5427 self->readline_func = readline_other;
5429 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5430 (self->read = PyObject_GetAttr(f, read_str)))) {
5431 PyErr_Clear();
5432 PyErr_SetString( PyExc_TypeError,
5433 "argument must have 'read' and "
5434 "'readline' attributes" );
5435 goto err;
5438 PyObject_GC_Track(self);
5440 return self;
5442 err:
5443 Py_DECREF((PyObject *)self);
5444 return NULL;
5448 static PyObject *
5449 get_Unpickler(PyObject *self, PyObject *file)
5451 return (PyObject *)newUnpicklerobject(file);
5455 static void
5456 Unpickler_dealloc(Unpicklerobject *self)
5458 PyObject_GC_UnTrack((PyObject *)self);
5459 Py_XDECREF(self->readline);
5460 Py_XDECREF(self->read);
5461 Py_XDECREF(self->file);
5462 Py_XDECREF(self->memo);
5463 Py_XDECREF(self->stack);
5464 Py_XDECREF(self->pers_func);
5465 Py_XDECREF(self->arg);
5466 Py_XDECREF(self->last_string);
5467 Py_XDECREF(self->find_class);
5469 if (self->marks) {
5470 free(self->marks);
5473 if (self->buf_size) {
5474 free(self->buf);
5477 Py_TYPE(self)->tp_free((PyObject *)self);
5480 static int
5481 Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5483 Py_VISIT(self->readline);
5484 Py_VISIT(self->read);
5485 Py_VISIT(self->file);
5486 Py_VISIT(self->memo);
5487 Py_VISIT(self->stack);
5488 Py_VISIT(self->pers_func);
5489 Py_VISIT(self->arg);
5490 Py_VISIT(self->last_string);
5491 Py_VISIT(self->find_class);
5492 return 0;
5495 static int
5496 Unpickler_clear(Unpicklerobject *self)
5498 Py_CLEAR(self->readline);
5499 Py_CLEAR(self->read);
5500 Py_CLEAR(self->file);
5501 Py_CLEAR(self->memo);
5502 Py_CLEAR(self->stack);
5503 Py_CLEAR(self->pers_func);
5504 Py_CLEAR(self->arg);
5505 Py_CLEAR(self->last_string);
5506 Py_CLEAR(self->find_class);
5507 return 0;
5510 static PyObject *
5511 Unpickler_getattr(Unpicklerobject *self, char *name)
5513 if (!strcmp(name, "persistent_load")) {
5514 if (!self->pers_func) {
5515 PyErr_SetString(PyExc_AttributeError, name);
5516 return NULL;
5519 Py_INCREF(self->pers_func);
5520 return self->pers_func;
5523 if (!strcmp(name, "find_global")) {
5524 if (!self->find_class) {
5525 PyErr_SetString(PyExc_AttributeError, name);
5526 return NULL;
5529 Py_INCREF(self->find_class);
5530 return self->find_class;
5533 if (!strcmp(name, "memo")) {
5534 if (!self->memo) {
5535 PyErr_SetString(PyExc_AttributeError, name);
5536 return NULL;
5539 Py_INCREF(self->memo);
5540 return self->memo;
5543 if (!strcmp(name, "UnpicklingError")) {
5544 Py_INCREF(UnpicklingError);
5545 return UnpicklingError;
5548 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5552 static int
5553 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5556 if (!strcmp(name, "persistent_load")) {
5557 Py_XDECREF(self->pers_func);
5558 self->pers_func = value;
5559 Py_XINCREF(value);
5560 return 0;
5563 if (!strcmp(name, "find_global")) {
5564 Py_XDECREF(self->find_class);
5565 self->find_class = value;
5566 Py_XINCREF(value);
5567 return 0;
5570 if (! value) {
5571 PyErr_SetString(PyExc_TypeError,
5572 "attribute deletion is not supported");
5573 return -1;
5576 if (strcmp(name, "memo") == 0) {
5577 if (!PyDict_Check(value)) {
5578 PyErr_SetString(PyExc_TypeError,
5579 "memo must be a dictionary");
5580 return -1;
5582 Py_XDECREF(self->memo);
5583 self->memo = value;
5584 Py_INCREF(value);
5585 return 0;
5588 PyErr_SetString(PyExc_AttributeError, name);
5589 return -1;
5592 /* ---------------------------------------------------------------------------
5593 * Module-level functions.
5596 /* dump(obj, file, protocol=0). */
5597 static PyObject *
5598 cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5600 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5601 PyObject *ob, *file, *res = NULL;
5602 Picklerobject *pickler = 0;
5603 int proto = 0;
5605 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5606 &ob, &file, &proto)))
5607 goto finally;
5609 if (!( pickler = newPicklerobject(file, proto)))
5610 goto finally;
5612 if (dump(pickler, ob) < 0)
5613 goto finally;
5615 Py_INCREF(Py_None);
5616 res = Py_None;
5618 finally:
5619 Py_XDECREF(pickler);
5621 return res;
5625 /* dumps(obj, protocol=0). */
5626 static PyObject *
5627 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5629 static char *kwlist[] = {"obj", "protocol", NULL};
5630 PyObject *ob, *file = 0, *res = NULL;
5631 Picklerobject *pickler = 0;
5632 int proto = 0;
5634 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5635 &ob, &proto)))
5636 goto finally;
5638 if (!( file = PycStringIO->NewOutput(128)))
5639 goto finally;
5641 if (!( pickler = newPicklerobject(file, proto)))
5642 goto finally;
5644 if (dump(pickler, ob) < 0)
5645 goto finally;
5647 res = PycStringIO->cgetvalue(file);
5649 finally:
5650 Py_XDECREF(pickler);
5651 Py_XDECREF(file);
5653 return res;
5657 /* load(fileobj). */
5658 static PyObject *
5659 cpm_load(PyObject *self, PyObject *ob)
5661 Unpicklerobject *unpickler = 0;
5662 PyObject *res = NULL;
5664 if (!( unpickler = newUnpicklerobject(ob)))
5665 goto finally;
5667 res = load(unpickler);
5669 finally:
5670 Py_XDECREF(unpickler);
5672 return res;
5676 /* loads(string) */
5677 static PyObject *
5678 cpm_loads(PyObject *self, PyObject *args)
5680 PyObject *ob, *file = 0, *res = NULL;
5681 Unpicklerobject *unpickler = 0;
5683 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5684 goto finally;
5686 if (!( file = PycStringIO->NewInput(ob)))
5687 goto finally;
5689 if (!( unpickler = newUnpicklerobject(file)))
5690 goto finally;
5692 res = load(unpickler);
5694 finally:
5695 Py_XDECREF(file);
5696 Py_XDECREF(unpickler);
5698 return res;
5702 PyDoc_STRVAR(Unpicklertype__doc__,
5703 "Objects that know how to unpickle");
5705 static PyTypeObject Unpicklertype = {
5706 PyVarObject_HEAD_INIT(NULL, 0)
5707 "cPickle.Unpickler", /*tp_name*/
5708 sizeof(Unpicklerobject), /*tp_basicsize*/
5710 (destructor)Unpickler_dealloc, /* tp_dealloc */
5711 0, /* tp_print */
5712 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5713 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5714 0, /* tp_compare */
5715 0, /* tp_repr */
5716 0, /* tp_as_number */
5717 0, /* tp_as_sequence */
5718 0, /* tp_as_mapping */
5719 0, /* tp_hash */
5720 0, /* tp_call */
5721 0, /* tp_str */
5722 0, /* tp_getattro */
5723 0, /* tp_setattro */
5724 0, /* tp_as_buffer */
5725 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5726 Unpicklertype__doc__, /* tp_doc */
5727 (traverseproc)Unpickler_traverse, /* tp_traverse */
5728 (inquiry)Unpickler_clear, /* tp_clear */
5731 static struct PyMethodDef cPickle_methods[] = {
5732 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5733 PyDoc_STR("dump(obj, file, protocol=0) -- "
5734 "Write an object in pickle format to the given file.\n"
5735 "\n"
5736 "See the Pickler docstring for the meaning of optional argument proto.")
5739 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5740 PyDoc_STR("dumps(obj, protocol=0) -- "
5741 "Return a string containing an object in pickle format.\n"
5742 "\n"
5743 "See the Pickler docstring for the meaning of optional argument proto.")
5746 {"load", (PyCFunction)cpm_load, METH_O,
5747 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5749 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
5750 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5752 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5753 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5754 "\n"
5755 "This takes a file-like object for writing a pickle data stream.\n"
5756 "The optional proto argument tells the pickler to use the given\n"
5757 "protocol; supported protocols are 0, 1, 2. The default\n"
5758 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5759 "only protocol that can be written to a file opened in text\n"
5760 "mode and read back successfully. When using a protocol higher\n"
5761 "than 0, make sure the file is opened in binary mode, both when\n"
5762 "pickling and unpickling.)\n"
5763 "\n"
5764 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5765 "more efficient than protocol 1.\n"
5766 "\n"
5767 "Specifying a negative protocol version selects the highest\n"
5768 "protocol version supported. The higher the protocol used, the\n"
5769 "more recent the version of Python needed to read the pickle\n"
5770 "produced.\n"
5771 "\n"
5772 "The file parameter must have a write() method that accepts a single\n"
5773 "string argument. It can thus be an open file object, a StringIO\n"
5774 "object, or any other custom object that meets this interface.\n")
5777 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
5778 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5780 { NULL, NULL }
5783 static int
5784 init_stuff(PyObject *module_dict)
5786 PyObject *copyreg, *t, *r;
5788 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5790 if (PyType_Ready(&Unpicklertype) < 0)
5791 return -1;
5792 if (PyType_Ready(&Picklertype) < 0)
5793 return -1;
5795 INIT_STR(__class__);
5796 INIT_STR(__getinitargs__);
5797 INIT_STR(__dict__);
5798 INIT_STR(__getstate__);
5799 INIT_STR(__setstate__);
5800 INIT_STR(__name__);
5801 INIT_STR(__main__);
5802 INIT_STR(__reduce__);
5803 INIT_STR(__reduce_ex__);
5804 INIT_STR(write);
5805 INIT_STR(append);
5806 INIT_STR(read);
5807 INIT_STR(readline);
5808 INIT_STR(copyreg);
5809 INIT_STR(dispatch_table);
5811 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5812 return -1;
5814 /* This is special because we want to use a different
5815 one in restricted mode. */
5816 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5817 if (!dispatch_table) return -1;
5819 extension_registry = PyObject_GetAttrString(copyreg,
5820 "_extension_registry");
5821 if (!extension_registry) return -1;
5823 inverted_registry = PyObject_GetAttrString(copyreg,
5824 "_inverted_registry");
5825 if (!inverted_registry) return -1;
5827 extension_cache = PyObject_GetAttrString(copyreg,
5828 "_extension_cache");
5829 if (!extension_cache) return -1;
5831 Py_DECREF(copyreg);
5833 if (!(empty_tuple = PyTuple_New(0)))
5834 return -1;
5836 two_tuple = PyTuple_New(2);
5837 if (two_tuple == NULL)
5838 return -1;
5839 /* We use this temp container with no regard to refcounts, or to
5840 * keeping containees alive. Exempt from GC, because we don't
5841 * want anything looking at two_tuple() by magic.
5843 PyObject_GC_UnTrack(two_tuple);
5845 /* Ugh */
5846 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5847 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5848 return -1;
5850 if (!( t=PyDict_New())) return -1;
5851 if (!( r=PyRun_String(
5852 "def __str__(self):\n"
5853 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5854 Py_file_input,
5855 module_dict, t) )) return -1;
5856 Py_DECREF(r);
5858 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5859 if (!PickleError)
5860 return -1;
5862 Py_DECREF(t);
5864 PicklingError = PyErr_NewException("cPickle.PicklingError",
5865 PickleError, NULL);
5866 if (!PicklingError)
5867 return -1;
5869 if (!( t=PyDict_New())) return -1;
5870 if (!( r=PyRun_String(
5871 "def __str__(self):\n"
5872 " a=self.args\n"
5873 " a=a and type(a[0]) or '(what)'\n"
5874 " return 'Cannot pickle %s objects' % a\n"
5875 , Py_file_input,
5876 module_dict, t) )) return -1;
5877 Py_DECREF(r);
5879 if (!( UnpickleableError = PyErr_NewException(
5880 "cPickle.UnpickleableError", PicklingError, t)))
5881 return -1;
5883 Py_DECREF(t);
5885 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5886 PickleError, NULL)))
5887 return -1;
5889 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5890 UnpicklingError, NULL)))
5891 return -1;
5893 if (PyDict_SetItemString(module_dict, "PickleError",
5894 PickleError) < 0)
5895 return -1;
5897 if (PyDict_SetItemString(module_dict, "PicklingError",
5898 PicklingError) < 0)
5899 return -1;
5901 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5902 UnpicklingError) < 0)
5903 return -1;
5905 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5906 UnpickleableError) < 0)
5907 return -1;
5909 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5910 BadPickleGet) < 0)
5911 return -1;
5913 PycString_IMPORT;
5915 return 0;
5918 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5919 #define PyMODINIT_FUNC void
5920 #endif
5921 PyMODINIT_FUNC
5922 initcPickle(void)
5924 PyObject *m, *d, *di, *v, *k;
5925 Py_ssize_t i;
5926 char *rev = "1.71"; /* XXX when does this change? */
5927 PyObject *format_version;
5928 PyObject *compatible_formats;
5930 /* XXX: Should mention that the pickle module will include the C
5931 XXX: optimized implementation automatically. */
5932 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5933 "Python 3.0", 2) < 0)
5934 return;
5936 Py_TYPE(&Picklertype) = &PyType_Type;
5937 Py_TYPE(&Unpicklertype) = &PyType_Type;
5938 Py_TYPE(&PdataType) = &PyType_Type;
5940 /* Initialize some pieces. We need to do this before module creation,
5941 * so we're forced to use a temporary dictionary. :(
5943 di = PyDict_New();
5944 if (!di) return;
5945 if (init_stuff(di) < 0) return;
5947 /* Create the module and add the functions */
5948 m = Py_InitModule4("cPickle", cPickle_methods,
5949 cPickle_module_documentation,
5950 (PyObject*)NULL,PYTHON_API_VERSION);
5951 if (m == NULL)
5952 return;
5954 /* Add some symbolic constants to the module */
5955 d = PyModule_GetDict(m);
5956 v = PyString_FromString(rev);
5957 PyDict_SetItemString(d, "__version__", v);
5958 Py_XDECREF(v);
5960 /* Copy data from di. Waaa. */
5961 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5962 if (PyObject_SetItem(d, k, v) < 0) {
5963 Py_DECREF(di);
5964 return;
5967 Py_DECREF(di);
5969 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5970 if (i < 0)
5971 return;
5973 /* These are purely informational; no code uses them. */
5974 /* File format version we write. */
5975 format_version = PyString_FromString("2.0");
5976 /* Format versions we can read. */
5977 compatible_formats = Py_BuildValue("[sssss]",
5978 "1.0", /* Original protocol 0 */
5979 "1.1", /* Protocol 0 + INST */
5980 "1.2", /* Original protocol 1 */
5981 "1.3", /* Protocol 1 + BINFLOAT */
5982 "2.0"); /* Original protocol 2 */
5983 PyDict_SetItemString(d, "format_version", format_version);
5984 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5985 Py_XDECREF(format_version);
5986 Py_XDECREF(compatible_formats);