The _lsprof module could crash the interpreter if it was given an external
[python.git] / Modules / cPickle.c
blob95d619dc9100612fb969acb2d1094297b11e04cd
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 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
24 #define MARK '('
25 #define STOP '.'
26 #define POP '0'
27 #define POP_MARK '1'
28 #define DUP '2'
29 #define FLOAT 'F'
30 #define BINFLOAT 'G'
31 #define INT 'I'
32 #define BININT 'J'
33 #define BININT1 'K'
34 #define LONG 'L'
35 #define BININT2 'M'
36 #define NONE 'N'
37 #define PERSID 'P'
38 #define BINPERSID 'Q'
39 #define REDUCE 'R'
40 #define STRING 'S'
41 #define BINSTRING 'T'
42 #define SHORT_BINSTRING 'U'
43 #define UNICODE 'V'
44 #define BINUNICODE 'X'
45 #define APPEND 'a'
46 #define BUILD 'b'
47 #define GLOBAL 'c'
48 #define DICT 'd'
49 #define EMPTY_DICT '}'
50 #define APPENDS 'e'
51 #define GET 'g'
52 #define BINGET 'h'
53 #define INST 'i'
54 #define LONG_BINGET 'j'
55 #define LIST 'l'
56 #define EMPTY_LIST ']'
57 #define OBJ 'o'
58 #define PUT 'p'
59 #define BINPUT 'q'
60 #define LONG_BINPUT 'r'
61 #define SETITEM 's'
62 #define TUPLE 't'
63 #define EMPTY_TUPLE ')'
64 #define SETITEMS 'u'
66 /* Protocol 2. */
67 #define PROTO '\x80' /* identify pickle protocol */
68 #define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69 #define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70 #define EXT2 '\x83' /* ditto, but 2-byte index */
71 #define EXT4 '\x84' /* ditto, but 4-byte index */
72 #define TUPLE1 '\x85' /* build 1-tuple from stack top */
73 #define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74 #define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75 #define NEWTRUE '\x88' /* push True */
76 #define NEWFALSE '\x89' /* push False */
77 #define LONG1 '\x8a' /* push long from < 256 bytes */
78 #define LONG4 '\x8b' /* push really big long */
80 /* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
85 #undef TRUE
86 #define TRUE "I01\n"
87 #undef FALSE
88 #define FALSE "I00\n"
90 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
91 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
95 #define BATCHSIZE 1000
97 static char MARKv = MARK;
99 static PyObject *PickleError;
100 static PyObject *PicklingError;
101 static PyObject *UnpickleableError;
102 static PyObject *UnpicklingError;
103 static PyObject *BadPickleGet;
105 /* As the name says, an empty tuple. */
106 static PyObject *empty_tuple;
108 /* copy_reg.dispatch_table, {type_object: pickling_function} */
109 static PyObject *dispatch_table;
111 /* For EXT[124] opcodes. */
112 /* copy_reg._extension_registry, {(module_name, function_name): code} */
113 static PyObject *extension_registry;
114 /* copy_reg._inverted_registry, {code: (module_name, function_name)} */
115 static PyObject *inverted_registry;
116 /* copy_reg._extension_cache, {code: object} */
117 static PyObject *extension_cache;
119 /* For looking up name pairs in copy_reg._extension_registry. */
120 static PyObject *two_tuple;
122 static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
124 *__reduce_ex___str,
125 *write_str, *append_str,
126 *read_str, *readline_str, *__main___str,
127 *copyreg_str, *dispatch_table_str;
129 /*************************************************************************
130 Internal Data type for pickle data. */
132 typedef struct {
133 PyObject_HEAD
134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
136 PyObject **data;
137 } Pdata;
139 static void
140 Pdata_dealloc(Pdata *self)
142 int i;
143 PyObject **p;
145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
148 if (self->data)
149 free(self->data);
150 PyObject_Del(self);
153 static PyTypeObject PdataType = {
154 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
159 #define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
161 static PyObject *
162 Pdata_New(void)
164 Pdata *self;
166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
173 Py_DECREF(self);
174 return PyErr_NoMemory();
177 static int
178 stackUnderflow(void)
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
184 /* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
187 static int
188 Pdata_clear(Pdata *self, int clearto)
190 int i;
191 PyObject **p;
193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
199 Py_CLEAR(*p);
201 self->length = clearto;
203 return 0;
206 static int
207 Pdata_grow(Pdata *self)
209 int bigger;
210 size_t nbytes;
211 PyObject **tmp;
213 bigger = self->size << 1;
214 if (bigger <= 0) /* was 0, or new value overflows */
215 goto nomemory;
216 if ((int)(size_t)bigger != bigger)
217 goto nomemory;
218 nbytes = (size_t)bigger * sizeof(PyObject *);
219 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
220 goto nomemory;
221 tmp = realloc(self->data, nbytes);
222 if (tmp == NULL)
223 goto nomemory;
224 self->data = tmp;
225 self->size = bigger;
226 return 0;
228 nomemory:
229 PyErr_NoMemory();
230 return -1;
233 /* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
235 * is raised and V is set to NULL. D and V may be evaluated several times.
237 #define PDATA_POP(D, V) { \
238 if ((D)->length) \
239 (V) = (D)->data[--((D)->length)]; \
240 else { \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 (V) = NULL; \
246 /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
254 /* Push O on stack D, giving ownership of O to the stack. */
255 #define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
258 Py_DECREF(O); \
259 return ER; \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
264 /* Push O on stack D, pushing a new reference. */
265 #define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
268 return ER; \
269 Py_INCREF(O); \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
274 static PyObject *
275 Pdata_popTuple(Pdata *self, int start)
277 PyObject *r;
278 int i, j, l;
280 l = self->length-start;
281 r = PyTuple_New(l);
282 if (r == NULL)
283 return NULL;
284 for (i = start, j = 0 ; j < l; i++, j++)
285 PyTuple_SET_ITEM(r, j, self->data[i]);
287 self->length = start;
288 return r;
291 static PyObject *
292 Pdata_popList(Pdata *self, int start)
294 PyObject *r;
295 int i, j, l;
297 l=self->length-start;
298 if (!( r=PyList_New(l))) return NULL;
299 for (i=start, j=0 ; j < l; i++, j++)
300 PyList_SET_ITEM(r, j, self->data[i]);
302 self->length=start;
303 return r;
306 /*************************************************************************/
308 #define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
313 else { \
314 Py_DECREF(o); \
318 #define FREE_ARG_TUP(self) { \
319 if (Py_REFCNT(self->arg) > 1) { \
320 Py_DECREF(self->arg); \
321 self->arg=NULL; \
325 typedef struct Picklerobject {
326 PyObject_HEAD
327 FILE *fp;
328 PyObject *write;
329 PyObject *file;
330 PyObject *memo;
331 PyObject *arg;
332 PyObject *pers_func;
333 PyObject *inst_pers_func;
335 /* pickle protocol number, >= 0 */
336 int proto;
338 /* bool, true if proto > 0 */
339 int bin;
341 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
342 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
348 } Picklerobject;
350 #ifndef PY_CPICKLE_FAST_LIMIT
351 #define PY_CPICKLE_FAST_LIMIT 50
352 #endif
354 static PyTypeObject Picklertype;
356 typedef struct Unpicklerobject {
357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
371 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
372 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
373 int buf_size;
374 char *buf;
375 PyObject *find_class;
376 } Unpicklerobject;
378 static PyTypeObject Unpicklertype;
380 /* Forward decls that need the above structs */
381 static int save(Picklerobject *, PyObject *, int);
382 static int put2(Picklerobject *, PyObject *);
384 static
385 PyObject *
386 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
395 if (stringformat && !(retval=PyString_FromString(stringformat)))
396 return NULL;
398 if (retval) {
399 if (args) {
400 PyObject *v;
401 v=PyString_Format(retval, args);
402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
419 static int
420 write_file(Picklerobject *self, const char *s, Py_ssize_t n)
422 size_t nbyteswritten;
424 if (s == NULL) {
425 return 0;
428 if (n > INT_MAX) {
429 /* String too large */
430 return -1;
433 PyFile_IncUseCount((PyFileObject *)self->file);
434 Py_BEGIN_ALLOW_THREADS
435 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
436 Py_END_ALLOW_THREADS
437 PyFile_DecUseCount((PyFileObject *)self->file);
438 if (nbyteswritten != (size_t)n) {
439 PyErr_SetFromErrno(PyExc_IOError);
440 return -1;
443 return (int)n;
446 static int
447 write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
449 if (s == NULL) {
450 return 0;
453 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
454 return -1;
457 return (int)n;
460 static int
461 write_none(Picklerobject *self, const char *s, Py_ssize_t n)
463 if (s == NULL) return 0;
464 if (n > INT_MAX) return -1;
465 return (int)n;
468 static int
469 write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
471 PyObject *py_str = 0, *junk = 0;
472 int n;
474 if (_n > INT_MAX)
475 return -1;
476 n = (int)_n;
477 if (s == NULL) {
478 if (!( self->buf_size )) return 0;
479 py_str = PyString_FromStringAndSize(self->write_buf,
480 self->buf_size);
481 if (!py_str)
482 return -1;
484 else {
485 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
486 if (write_other(self, NULL, 0) < 0)
487 return -1;
490 if (n > WRITE_BUF_SIZE) {
491 if (!( py_str =
492 PyString_FromStringAndSize(s, n)))
493 return -1;
495 else {
496 memcpy(self->write_buf + self->buf_size, s, n);
497 self->buf_size += n;
498 return n;
502 if (self->write) {
503 /* object with write method */
504 ARG_TUP(self, py_str);
505 if (self->arg) {
506 junk = PyObject_Call(self->write, self->arg, NULL);
507 FREE_ARG_TUP(self);
509 if (junk) Py_DECREF(junk);
510 else return -1;
512 else
513 PDATA_PUSH(self->file, py_str, -1);
515 self->buf_size = 0;
516 return n;
520 static Py_ssize_t
521 read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
523 size_t nbytesread;
525 if (self->buf_size == 0) {
526 int size;
528 size = ((n < 32) ? 32 : n);
529 if (!( self->buf = (char *)malloc(size))) {
530 PyErr_NoMemory();
531 return -1;
534 self->buf_size = size;
536 else if (n > self->buf_size) {
537 char *newbuf = (char *)realloc(self->buf, n);
538 if (!newbuf) {
539 PyErr_NoMemory();
540 return -1;
542 self->buf = newbuf;
543 self->buf_size = n;
546 PyFile_IncUseCount((PyFileObject *)self->file);
547 Py_BEGIN_ALLOW_THREADS
548 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
549 Py_END_ALLOW_THREADS
550 PyFile_DecUseCount((PyFileObject *)self->file);
551 if (nbytesread != (size_t)n) {
552 if (feof(self->fp)) {
553 PyErr_SetNone(PyExc_EOFError);
554 return -1;
557 PyErr_SetFromErrno(PyExc_IOError);
558 return -1;
561 *s = self->buf;
563 return n;
567 static Py_ssize_t
568 readline_file(Unpicklerobject *self, char **s)
570 int i;
572 if (self->buf_size == 0) {
573 if (!( self->buf = (char *)malloc(40))) {
574 PyErr_NoMemory();
575 return -1;
577 self->buf_size = 40;
580 i = 0;
581 while (1) {
582 int bigger;
583 char *newbuf;
584 for (; i < (self->buf_size - 1); i++) {
585 if (feof(self->fp) ||
586 (self->buf[i] = getc(self->fp)) == '\n') {
587 self->buf[i + 1] = '\0';
588 *s = self->buf;
589 return i + 1;
592 bigger = self->buf_size << 1;
593 if (bigger <= 0) { /* overflow */
594 PyErr_NoMemory();
595 return -1;
597 newbuf = (char *)realloc(self->buf, bigger);
598 if (!newbuf) {
599 PyErr_NoMemory();
600 return -1;
602 self->buf = newbuf;
603 self->buf_size = bigger;
608 static Py_ssize_t
609 read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
611 char *ptr;
613 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
614 PyErr_SetNone(PyExc_EOFError);
615 return -1;
618 *s = ptr;
620 return n;
624 static Py_ssize_t
625 readline_cStringIO(Unpicklerobject *self, char **s)
627 Py_ssize_t n;
628 char *ptr;
630 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
631 return -1;
634 *s = ptr;
636 return n;
640 static Py_ssize_t
641 read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
643 PyObject *bytes, *str=0;
645 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
647 ARG_TUP(self, bytes);
648 if (self->arg) {
649 str = PyObject_Call(self->read, self->arg, NULL);
650 FREE_ARG_TUP(self);
652 if (! str) return -1;
654 Py_XDECREF(self->last_string);
655 self->last_string = str;
657 if (! (*s = PyString_AsString(str))) return -1;
658 return n;
662 static Py_ssize_t
663 readline_other(Unpicklerobject *self, char **s)
665 PyObject *str;
666 Py_ssize_t str_size;
668 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
669 return -1;
672 if ((str_size = PyString_Size(str)) < 0)
673 return -1;
675 Py_XDECREF(self->last_string);
676 self->last_string = str;
678 if (! (*s = PyString_AsString(str)))
679 return -1;
681 return str_size;
684 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
685 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
686 * The caller is responsible for free()'ing the return value.
688 static char *
689 pystrndup(const char *s, int n)
691 char *r = (char *)malloc(n+1);
692 if (r == NULL)
693 return (char*)PyErr_NoMemory();
694 memcpy(r, s, n);
695 r[n] = 0;
696 return r;
700 static int
701 get(Picklerobject *self, PyObject *id)
703 PyObject *value, *mv;
704 long c_value;
705 char s[30];
706 size_t len;
708 if (!( mv = PyDict_GetItem(self->memo, id))) {
709 PyErr_SetObject(PyExc_KeyError, id);
710 return -1;
713 if (!( value = PyTuple_GetItem(mv, 0)))
714 return -1;
716 if (!( PyInt_Check(value))) {
717 PyErr_SetString(PicklingError, "no int where int expected in memo");
718 return -1;
720 c_value = PyInt_AS_LONG((PyIntObject*)value);
722 if (!self->bin) {
723 s[0] = GET;
724 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
725 len = strlen(s);
727 else if (Pdata_Check(self->file)) {
728 if (write_other(self, NULL, 0) < 0) return -1;
729 PDATA_APPEND(self->file, mv, -1);
730 return 0;
732 else {
733 if (c_value < 256) {
734 s[0] = BINGET;
735 s[1] = (int)(c_value & 0xff);
736 len = 2;
738 else {
739 s[0] = LONG_BINGET;
740 s[1] = (int)(c_value & 0xff);
741 s[2] = (int)((c_value >> 8) & 0xff);
742 s[3] = (int)((c_value >> 16) & 0xff);
743 s[4] = (int)((c_value >> 24) & 0xff);
744 len = 5;
748 if (self->write_func(self, s, len) < 0)
749 return -1;
751 return 0;
755 static int
756 put(Picklerobject *self, PyObject *ob)
758 if (Py_REFCNT(ob) < 2 || self->fast)
759 return 0;
761 return put2(self, ob);
765 static int
766 put2(Picklerobject *self, PyObject *ob)
768 char c_str[30];
769 int p;
770 size_t len;
771 int res = -1;
772 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
774 if (self->fast)
775 return 0;
777 if ((p = PyDict_Size(self->memo)) < 0)
778 goto finally;
780 /* Make sure memo keys are positive! */
781 /* XXX Why?
782 * XXX And does "positive" really mean non-negative?
783 * XXX pickle.py starts with PUT index 0, not 1. This makes for
784 * XXX gratuitous differences between the pickling modules.
786 p++;
788 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
789 goto finally;
791 if (!( memo_len = PyInt_FromLong(p)))
792 goto finally;
794 if (!( t = PyTuple_New(2)))
795 goto finally;
797 PyTuple_SET_ITEM(t, 0, memo_len);
798 Py_INCREF(memo_len);
799 PyTuple_SET_ITEM(t, 1, ob);
800 Py_INCREF(ob);
802 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
803 goto finally;
805 if (!self->bin) {
806 c_str[0] = PUT;
807 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
808 len = strlen(c_str);
810 else if (Pdata_Check(self->file)) {
811 if (write_other(self, NULL, 0) < 0) return -1;
812 PDATA_APPEND(self->file, memo_len, -1);
813 res=0; /* Job well done ;) */
814 goto finally;
816 else {
817 if (p >= 256) {
818 c_str[0] = LONG_BINPUT;
819 c_str[1] = (int)(p & 0xff);
820 c_str[2] = (int)((p >> 8) & 0xff);
821 c_str[3] = (int)((p >> 16) & 0xff);
822 c_str[4] = (int)((p >> 24) & 0xff);
823 len = 5;
825 else {
826 c_str[0] = BINPUT;
827 c_str[1] = p;
828 len = 2;
832 if (self->write_func(self, c_str, len) < 0)
833 goto finally;
835 res = 0;
837 finally:
838 Py_XDECREF(py_ob_id);
839 Py_XDECREF(memo_len);
840 Py_XDECREF(t);
842 return res;
845 static PyObject *
846 whichmodule(PyObject *global, PyObject *global_name)
848 Py_ssize_t i, j;
849 PyObject *module = 0, *modules_dict = 0,
850 *global_name_attr = 0, *name = 0;
852 module = PyObject_GetAttrString(global, "__module__");
853 if (module)
854 return module;
855 if (PyErr_ExceptionMatches(PyExc_AttributeError))
856 PyErr_Clear();
857 else
858 return NULL;
860 if (!( modules_dict = PySys_GetObject("modules")))
861 return NULL;
863 i = 0;
864 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
866 if (PyObject_Compare(name, __main___str)==0) continue;
868 global_name_attr = PyObject_GetAttr(module, global_name);
869 if (!global_name_attr) {
870 if (PyErr_ExceptionMatches(PyExc_AttributeError))
871 PyErr_Clear();
872 else
873 return NULL;
874 continue;
877 if (global_name_attr != global) {
878 Py_DECREF(global_name_attr);
879 continue;
882 Py_DECREF(global_name_attr);
884 break;
887 /* The following implements the rule in pickle.py added in 1.5
888 that used __main__ if no module is found. I don't actually
889 like this rule. jlf
891 if (!j) {
892 j=1;
893 name=__main___str;
896 Py_INCREF(name);
897 return name;
901 static int
902 fast_save_enter(Picklerobject *self, PyObject *obj)
904 /* if fast_container < 0, we're doing an error exit. */
905 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
906 PyObject *key = NULL;
907 if (self->fast_memo == NULL) {
908 self->fast_memo = PyDict_New();
909 if (self->fast_memo == NULL) {
910 self->fast_container = -1;
911 return 0;
914 key = PyLong_FromVoidPtr(obj);
915 if (key == NULL)
916 return 0;
917 if (PyDict_GetItem(self->fast_memo, key)) {
918 Py_DECREF(key);
919 PyErr_Format(PyExc_ValueError,
920 "fast mode: can't pickle cyclic objects "
921 "including object type %s at %p",
922 Py_TYPE(obj)->tp_name, obj);
923 self->fast_container = -1;
924 return 0;
926 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
927 Py_DECREF(key);
928 self->fast_container = -1;
929 return 0;
931 Py_DECREF(key);
933 return 1;
937 fast_save_leave(Picklerobject *self, PyObject *obj)
939 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
940 PyObject *key = PyLong_FromVoidPtr(obj);
941 if (key == NULL)
942 return 0;
943 if (PyDict_DelItem(self->fast_memo, key) < 0) {
944 Py_DECREF(key);
945 return 0;
947 Py_DECREF(key);
949 return 1;
952 static int
953 save_none(Picklerobject *self, PyObject *args)
955 static char none = NONE;
956 if (self->write_func(self, &none, 1) < 0)
957 return -1;
959 return 0;
962 static int
963 save_bool(Picklerobject *self, PyObject *args)
965 static const char *buf[2] = {FALSE, TRUE};
966 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
967 long l = PyInt_AS_LONG((PyIntObject *)args);
969 if (self->proto >= 2) {
970 char opcode = l ? NEWTRUE : NEWFALSE;
971 if (self->write_func(self, &opcode, 1) < 0)
972 return -1;
974 else if (self->write_func(self, buf[l], len[l]) < 0)
975 return -1;
976 return 0;
979 static int
980 save_int(Picklerobject *self, PyObject *args)
982 char c_str[32];
983 long l = PyInt_AS_LONG((PyIntObject *)args);
984 int len = 0;
986 if (!self->bin
987 #if SIZEOF_LONG > 4
988 || l > 0x7fffffffL
989 || l < -0x80000000L
990 #endif
992 /* Text-mode pickle, or long too big to fit in the 4-byte
993 * signed BININT format: store as a string.
995 c_str[0] = INT;
996 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
997 if (self->write_func(self, c_str, strlen(c_str)) < 0)
998 return -1;
1000 else {
1001 /* Binary pickle and l fits in a signed 4-byte int. */
1002 c_str[1] = (int)( l & 0xff);
1003 c_str[2] = (int)((l >> 8) & 0xff);
1004 c_str[3] = (int)((l >> 16) & 0xff);
1005 c_str[4] = (int)((l >> 24) & 0xff);
1007 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1008 if (c_str[2] == 0) {
1009 c_str[0] = BININT1;
1010 len = 2;
1012 else {
1013 c_str[0] = BININT2;
1014 len = 3;
1017 else {
1018 c_str[0] = BININT;
1019 len = 5;
1022 if (self->write_func(self, c_str, len) < 0)
1023 return -1;
1026 return 0;
1030 static int
1031 save_long(Picklerobject *self, PyObject *args)
1033 Py_ssize_t size;
1034 int res = -1;
1035 PyObject *repr = NULL;
1037 static char l = LONG;
1039 if (self->proto >= 2) {
1040 /* Linear-time pickling. */
1041 size_t nbits;
1042 size_t nbytes;
1043 unsigned char *pdata;
1044 char c_str[5];
1045 int i;
1046 int sign = _PyLong_Sign(args);
1048 if (sign == 0) {
1049 /* It's 0 -- an empty bytestring. */
1050 c_str[0] = LONG1;
1051 c_str[1] = 0;
1052 i = self->write_func(self, c_str, 2);
1053 if (i < 0) goto finally;
1054 res = 0;
1055 goto finally;
1057 nbits = _PyLong_NumBits(args);
1058 if (nbits == (size_t)-1 && PyErr_Occurred())
1059 goto finally;
1060 /* How many bytes do we need? There are nbits >> 3 full
1061 * bytes of data, and nbits & 7 leftover bits. If there
1062 * are any leftover bits, then we clearly need another
1063 * byte. Wnat's not so obvious is that we *probably*
1064 * need another byte even if there aren't any leftovers:
1065 * the most-significant bit of the most-significant byte
1066 * acts like a sign bit, and it's usually got a sense
1067 * opposite of the one we need. The exception is longs
1068 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1069 * its own 256's-complement, so has the right sign bit
1070 * even without the extra byte. That's a pain to check
1071 * for in advance, though, so we always grab an extra
1072 * byte at the start, and cut it back later if possible.
1074 nbytes = (nbits >> 3) + 1;
1075 if (nbytes > INT_MAX) {
1076 PyErr_SetString(PyExc_OverflowError, "long too large "
1077 "to pickle");
1078 goto finally;
1080 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1081 if (repr == NULL) goto finally;
1082 pdata = (unsigned char *)PyString_AS_STRING(repr);
1083 i = _PyLong_AsByteArray((PyLongObject *)args,
1084 pdata, nbytes,
1085 1 /* little endian */, 1 /* signed */);
1086 if (i < 0) goto finally;
1087 /* If the long is negative, this may be a byte more than
1088 * needed. This is so iff the MSB is all redundant sign
1089 * bits.
1091 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1092 (pdata[nbytes - 2] & 0x80) != 0)
1093 --nbytes;
1095 if (nbytes < 256) {
1096 c_str[0] = LONG1;
1097 c_str[1] = (char)nbytes;
1098 size = 2;
1100 else {
1101 c_str[0] = LONG4;
1102 size = (int)nbytes;
1103 for (i = 1; i < 5; i++) {
1104 c_str[i] = (char)(size & 0xff);
1105 size >>= 8;
1107 size = 5;
1109 i = self->write_func(self, c_str, size);
1110 if (i < 0) goto finally;
1111 i = self->write_func(self, (char *)pdata, (int)nbytes);
1112 if (i < 0) goto finally;
1113 res = 0;
1114 goto finally;
1117 /* proto < 2: write the repr and newline. This is quadratic-time
1118 * (in the number of digits), in both directions.
1120 if (!( repr = PyObject_Repr(args)))
1121 goto finally;
1123 if ((size = PyString_Size(repr)) < 0)
1124 goto finally;
1126 if (self->write_func(self, &l, 1) < 0)
1127 goto finally;
1129 if (self->write_func(self,
1130 PyString_AS_STRING((PyStringObject *)repr),
1131 size) < 0)
1132 goto finally;
1134 if (self->write_func(self, "\n", 1) < 0)
1135 goto finally;
1137 res = 0;
1139 finally:
1140 Py_XDECREF(repr);
1141 return res;
1145 static int
1146 save_float(Picklerobject *self, PyObject *args)
1148 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1150 if (self->bin) {
1151 char str[9];
1152 str[0] = BINFLOAT;
1153 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1154 return -1;
1155 if (self->write_func(self, str, 9) < 0)
1156 return -1;
1158 else {
1159 char c_str[250];
1160 c_str[0] = FLOAT;
1161 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1162 /* Extend the formatted string with a newline character */
1163 strcat(c_str, "\n");
1165 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1166 return -1;
1169 return 0;
1173 static int
1174 save_string(Picklerobject *self, PyObject *args, int doput)
1176 int size, len;
1177 PyObject *repr=0;
1179 if ((size = PyString_Size(args)) < 0)
1180 return -1;
1182 if (!self->bin) {
1183 char *repr_str;
1185 static char string = STRING;
1187 if (!( repr = PyObject_Repr(args)))
1188 return -1;
1190 if ((len = PyString_Size(repr)) < 0)
1191 goto err;
1192 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1194 if (self->write_func(self, &string, 1) < 0)
1195 goto err;
1197 if (self->write_func(self, repr_str, len) < 0)
1198 goto err;
1200 if (self->write_func(self, "\n", 1) < 0)
1201 goto err;
1203 Py_XDECREF(repr);
1205 else {
1206 int i;
1207 char c_str[5];
1209 if ((size = PyString_Size(args)) < 0)
1210 return -1;
1212 if (size < 256) {
1213 c_str[0] = SHORT_BINSTRING;
1214 c_str[1] = size;
1215 len = 2;
1217 else if (size <= INT_MAX) {
1218 c_str[0] = BINSTRING;
1219 for (i = 1; i < 5; i++)
1220 c_str[i] = (int)(size >> ((i - 1) * 8));
1221 len = 5;
1223 else
1224 return -1; /* string too large */
1226 if (self->write_func(self, c_str, len) < 0)
1227 return -1;
1229 if (size > 128 && Pdata_Check(self->file)) {
1230 if (write_other(self, NULL, 0) < 0) return -1;
1231 PDATA_APPEND(self->file, args, -1);
1233 else {
1234 if (self->write_func(self,
1235 PyString_AS_STRING(
1236 (PyStringObject *)args),
1237 size) < 0)
1238 return -1;
1242 if (doput)
1243 if (put(self, args) < 0)
1244 return -1;
1246 return 0;
1248 err:
1249 Py_XDECREF(repr);
1250 return -1;
1254 #ifdef Py_USING_UNICODE
1255 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1256 backslash and newline characters to \uXXXX escapes. */
1257 static PyObject *
1258 modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1260 PyObject *repr;
1261 char *p;
1262 char *q;
1264 static const char *hexdigit = "0123456789ABCDEF";
1266 repr = PyString_FromStringAndSize(NULL, 6 * size);
1267 if (repr == NULL)
1268 return NULL;
1269 if (size == 0)
1270 return repr;
1272 p = q = PyString_AS_STRING(repr);
1273 while (size-- > 0) {
1274 Py_UNICODE ch = *s++;
1275 /* Map 16-bit characters to '\uxxxx' */
1276 if (ch >= 256 || ch == '\\' || ch == '\n') {
1277 *p++ = '\\';
1278 *p++ = 'u';
1279 *p++ = hexdigit[(ch >> 12) & 0xf];
1280 *p++ = hexdigit[(ch >> 8) & 0xf];
1281 *p++ = hexdigit[(ch >> 4) & 0xf];
1282 *p++ = hexdigit[ch & 15];
1284 /* Copy everything else as-is */
1285 else
1286 *p++ = (char) ch;
1288 *p = '\0';
1289 _PyString_Resize(&repr, p - q);
1290 return repr;
1294 static int
1295 save_unicode(Picklerobject *self, PyObject *args, int doput)
1297 Py_ssize_t size, len;
1298 PyObject *repr=0;
1300 if (!PyUnicode_Check(args))
1301 return -1;
1303 if (!self->bin) {
1304 char *repr_str;
1305 static char string = UNICODE;
1307 repr = modified_EncodeRawUnicodeEscape(
1308 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1309 if (!repr)
1310 return -1;
1312 if ((len = PyString_Size(repr)) < 0)
1313 goto err;
1314 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1316 if (self->write_func(self, &string, 1) < 0)
1317 goto err;
1319 if (self->write_func(self, repr_str, len) < 0)
1320 goto err;
1322 if (self->write_func(self, "\n", 1) < 0)
1323 goto err;
1325 Py_XDECREF(repr);
1327 else {
1328 int i;
1329 char c_str[5];
1331 if (!( repr = PyUnicode_AsUTF8String(args)))
1332 return -1;
1334 if ((size = PyString_Size(repr)) < 0)
1335 goto err;
1336 if (size > INT_MAX)
1337 return -1; /* string too large */
1339 c_str[0] = BINUNICODE;
1340 for (i = 1; i < 5; i++)
1341 c_str[i] = (int)(size >> ((i - 1) * 8));
1342 len = 5;
1344 if (self->write_func(self, c_str, len) < 0)
1345 goto err;
1347 if (size > 128 && Pdata_Check(self->file)) {
1348 if (write_other(self, NULL, 0) < 0)
1349 goto err;
1350 PDATA_APPEND(self->file, repr, -1);
1352 else {
1353 if (self->write_func(self, PyString_AS_STRING(repr),
1354 size) < 0)
1355 goto err;
1358 Py_DECREF(repr);
1361 if (doput)
1362 if (put(self, args) < 0)
1363 return -1;
1365 return 0;
1367 err:
1368 Py_XDECREF(repr);
1369 return -1;
1371 #endif
1373 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1374 static int
1375 store_tuple_elements(Picklerobject *self, PyObject *t, int len)
1377 int i;
1378 int res = -1; /* guilty until proved innocent */
1380 assert(PyTuple_Size(t) == len);
1382 for (i = 0; i < len; i++) {
1383 PyObject *element = PyTuple_GET_ITEM(t, i);
1385 if (element == NULL)
1386 goto finally;
1387 if (save(self, element, 0) < 0)
1388 goto finally;
1390 res = 0;
1392 finally:
1393 return res;
1396 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1397 * used across protocols to minimize the space needed to pickle them.
1398 * Tuples are also the only builtin immutable type that can be recursive
1399 * (a tuple can be reached from itself), and that requires some subtle
1400 * magic so that it works in all cases. IOW, this is a long routine.
1402 static int
1403 save_tuple(Picklerobject *self, PyObject *args)
1405 PyObject *py_tuple_id = NULL;
1406 int len, i;
1407 int res = -1;
1409 static char tuple = TUPLE;
1410 static char pop = POP;
1411 static char pop_mark = POP_MARK;
1412 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1414 if ((len = PyTuple_Size(args)) < 0)
1415 goto finally;
1417 if (len == 0) {
1418 char c_str[2];
1420 if (self->proto) {
1421 c_str[0] = EMPTY_TUPLE;
1422 len = 1;
1424 else {
1425 c_str[0] = MARK;
1426 c_str[1] = TUPLE;
1427 len = 2;
1429 if (self->write_func(self, c_str, len) >= 0)
1430 res = 0;
1431 /* Don't memoize an empty tuple. */
1432 goto finally;
1435 /* A non-empty tuple. */
1437 /* id(tuple) isn't in the memo now. If it shows up there after
1438 * saving the tuple elements, the tuple must be recursive, in
1439 * which case we'll pop everything we put on the stack, and fetch
1440 * its value from the memo.
1442 py_tuple_id = PyLong_FromVoidPtr(args);
1443 if (py_tuple_id == NULL)
1444 goto finally;
1446 if (len <= 3 && self->proto >= 2) {
1447 /* Use TUPLE{1,2,3} opcodes. */
1448 if (store_tuple_elements(self, args, len) < 0)
1449 goto finally;
1450 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1451 /* pop the len elements */
1452 for (i = 0; i < len; ++i)
1453 if (self->write_func(self, &pop, 1) < 0)
1454 goto finally;
1455 /* fetch from memo */
1456 if (get(self, py_tuple_id) < 0)
1457 goto finally;
1458 res = 0;
1459 goto finally;
1461 /* Not recursive. */
1462 if (self->write_func(self, len2opcode + len, 1) < 0)
1463 goto finally;
1464 goto memoize;
1467 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1468 * Generate MARK elt1 elt2 ... TUPLE
1470 if (self->write_func(self, &MARKv, 1) < 0)
1471 goto finally;
1473 if (store_tuple_elements(self, args, len) < 0)
1474 goto finally;
1476 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1477 /* pop the stack stuff we pushed */
1478 if (self->bin) {
1479 if (self->write_func(self, &pop_mark, 1) < 0)
1480 goto finally;
1482 else {
1483 /* Note that we pop one more than len, to remove
1484 * the MARK too.
1486 for (i = 0; i <= len; i++)
1487 if (self->write_func(self, &pop, 1) < 0)
1488 goto finally;
1490 /* fetch from memo */
1491 if (get(self, py_tuple_id) >= 0)
1492 res = 0;
1493 goto finally;
1496 /* Not recursive. */
1497 if (self->write_func(self, &tuple, 1) < 0)
1498 goto finally;
1500 memoize:
1501 if (put(self, args) >= 0)
1502 res = 0;
1504 finally:
1505 Py_XDECREF(py_tuple_id);
1506 return res;
1509 /* iter is an iterator giving items, and we batch up chunks of
1510 * MARK item item ... item APPENDS
1511 * opcode sequences. Calling code should have arranged to first create an
1512 * empty list, or list-like object, for the APPENDS to operate on.
1513 * Returns 0 on success, <0 on error.
1515 static int
1516 batch_list(Picklerobject *self, PyObject *iter)
1518 PyObject *obj = NULL;
1519 PyObject *firstitem = NULL;
1520 int i, n;
1522 static char append = APPEND;
1523 static char appends = APPENDS;
1525 assert(iter != NULL);
1527 if (self->proto == 0) {
1528 /* APPENDS isn't available; do one at a time. */
1529 for (;;) {
1530 obj = PyIter_Next(iter);
1531 if (obj == NULL) {
1532 if (PyErr_Occurred())
1533 return -1;
1534 break;
1536 i = save(self, obj, 0);
1537 Py_DECREF(obj);
1538 if (i < 0)
1539 return -1;
1540 if (self->write_func(self, &append, 1) < 0)
1541 return -1;
1543 return 0;
1546 /* proto > 0: write in batches of BATCHSIZE. */
1547 do {
1548 /* Get first item */
1549 firstitem = PyIter_Next(iter);
1550 if (firstitem == NULL) {
1551 if (PyErr_Occurred())
1552 goto BatchFailed;
1554 /* nothing more to add */
1555 break;
1558 /* Try to get a second item */
1559 obj = PyIter_Next(iter);
1560 if (obj == NULL) {
1561 if (PyErr_Occurred())
1562 goto BatchFailed;
1564 /* Only one item to write */
1565 if (save(self, firstitem, 0) < 0)
1566 goto BatchFailed;
1567 if (self->write_func(self, &append, 1) < 0)
1568 goto BatchFailed;
1569 Py_CLEAR(firstitem);
1570 break;
1573 /* More than one item to write */
1575 /* Pump out MARK, items, APPENDS. */
1576 if (self->write_func(self, &MARKv, 1) < 0)
1577 goto BatchFailed;
1579 if (save(self, firstitem, 0) < 0)
1580 goto BatchFailed;
1581 Py_CLEAR(firstitem);
1582 n = 1;
1584 /* Fetch and save up to BATCHSIZE items */
1585 while (obj) {
1586 if (save(self, obj, 0) < 0)
1587 goto BatchFailed;
1588 Py_CLEAR(obj);
1589 n += 1;
1591 if (n == BATCHSIZE)
1592 break;
1594 obj = PyIter_Next(iter);
1595 if (obj == NULL) {
1596 if (PyErr_Occurred())
1597 goto BatchFailed;
1598 break;
1602 if (self->write_func(self, &appends, 1) < 0)
1603 goto BatchFailed;
1605 } while (n == BATCHSIZE);
1606 return 0;
1608 BatchFailed:
1609 Py_XDECREF(firstitem);
1610 Py_XDECREF(obj);
1611 return -1;
1614 static int
1615 save_list(Picklerobject *self, PyObject *args)
1617 int res = -1;
1618 char s[3];
1619 int len;
1620 PyObject *iter;
1622 if (self->fast && !fast_save_enter(self, args))
1623 goto finally;
1625 /* Create an empty list. */
1626 if (self->bin) {
1627 s[0] = EMPTY_LIST;
1628 len = 1;
1630 else {
1631 s[0] = MARK;
1632 s[1] = LIST;
1633 len = 2;
1636 if (self->write_func(self, s, len) < 0)
1637 goto finally;
1639 /* Get list length, and bow out early if empty. */
1640 if ((len = PyList_Size(args)) < 0)
1641 goto finally;
1643 /* Memoize. */
1644 if (len == 0) {
1645 if (put(self, args) >= 0)
1646 res = 0;
1647 goto finally;
1649 if (put2(self, args) < 0)
1650 goto finally;
1652 /* Materialize the list elements. */
1653 iter = PyObject_GetIter(args);
1654 if (iter == NULL)
1655 goto finally;
1657 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1659 res = batch_list(self, iter);
1660 Py_LeaveRecursiveCall();
1662 Py_DECREF(iter);
1664 finally:
1665 if (self->fast && !fast_save_leave(self, args))
1666 res = -1;
1668 return res;
1672 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1673 * MARK key value ... key value SETITEMS
1674 * opcode sequences. Calling code should have arranged to first create an
1675 * empty dict, or dict-like object, for the SETITEMS to operate on.
1676 * Returns 0 on success, <0 on error.
1678 * This is very much like batch_list(). The difference between saving
1679 * elements directly, and picking apart two-tuples, is so long-winded at
1680 * the C level, though, that attempts to combine these routines were too
1681 * ugly to bear.
1683 static int
1684 batch_dict(Picklerobject *self, PyObject *iter)
1686 PyObject *p = NULL;
1687 PyObject *firstitem = NULL;
1688 int i, n;
1690 static char setitem = SETITEM;
1691 static char setitems = SETITEMS;
1693 assert(iter != NULL);
1695 if (self->proto == 0) {
1696 /* SETITEMS isn't available; do one at a time. */
1697 for (;;) {
1698 p = PyIter_Next(iter);
1699 if (p == NULL) {
1700 if (PyErr_Occurred())
1701 return -1;
1702 break;
1704 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1705 PyErr_SetString(PyExc_TypeError, "dict items "
1706 "iterator must return 2-tuples");
1707 return -1;
1709 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1710 if (i >= 0)
1711 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1712 Py_DECREF(p);
1713 if (i < 0)
1714 return -1;
1715 if (self->write_func(self, &setitem, 1) < 0)
1716 return -1;
1718 return 0;
1721 /* proto > 0: write in batches of BATCHSIZE. */
1722 do {
1723 /* Get first item */
1724 firstitem = PyIter_Next(iter);
1725 if (firstitem == NULL) {
1726 if (PyErr_Occurred())
1727 goto BatchFailed;
1729 /* nothing more to add */
1730 break;
1732 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1733 PyErr_SetString(PyExc_TypeError, "dict items "
1734 "iterator must return 2-tuples");
1735 goto BatchFailed;
1738 /* Try to get a second item */
1739 p = PyIter_Next(iter);
1740 if (p == NULL) {
1741 if (PyErr_Occurred())
1742 goto BatchFailed;
1744 /* Only one item to write */
1745 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1746 goto BatchFailed;
1747 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1748 goto BatchFailed;
1749 if (self->write_func(self, &setitem, 1) < 0)
1750 goto BatchFailed;
1751 Py_CLEAR(firstitem);
1752 break;
1755 /* More than one item to write */
1757 /* Pump out MARK, items, SETITEMS. */
1758 if (self->write_func(self, &MARKv, 1) < 0)
1759 goto BatchFailed;
1761 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1762 goto BatchFailed;
1763 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1764 goto BatchFailed;
1765 Py_CLEAR(firstitem);
1766 n = 1;
1768 /* Fetch and save up to BATCHSIZE items */
1769 while (p) {
1770 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1771 PyErr_SetString(PyExc_TypeError, "dict items "
1772 "iterator must return 2-tuples");
1773 goto BatchFailed;
1775 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1776 goto BatchFailed;
1777 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1778 goto BatchFailed;
1779 Py_CLEAR(p);
1780 n += 1;
1782 if (n == BATCHSIZE)
1783 break;
1785 p = PyIter_Next(iter);
1786 if (p == NULL) {
1787 if (PyErr_Occurred())
1788 goto BatchFailed;
1789 break;
1793 if (self->write_func(self, &setitems, 1) < 0)
1794 goto BatchFailed;
1796 } while (n == BATCHSIZE);
1797 return 0;
1799 BatchFailed:
1800 Py_XDECREF(firstitem);
1801 Py_XDECREF(p);
1802 return -1;
1805 static int
1806 save_dict(Picklerobject *self, PyObject *args)
1808 int res = -1;
1809 char s[3];
1810 int len;
1811 PyObject *iter;
1813 if (self->fast && !fast_save_enter(self, args))
1814 goto finally;
1816 /* Create an empty dict. */
1817 if (self->bin) {
1818 s[0] = EMPTY_DICT;
1819 len = 1;
1821 else {
1822 s[0] = MARK;
1823 s[1] = DICT;
1824 len = 2;
1827 if (self->write_func(self, s, len) < 0)
1828 goto finally;
1830 /* Get dict size, and bow out early if empty. */
1831 if ((len = PyDict_Size(args)) < 0)
1832 goto finally;
1834 if (len == 0) {
1835 if (put(self, args) >= 0)
1836 res = 0;
1837 goto finally;
1839 if (put2(self, args) < 0)
1840 goto finally;
1842 /* Materialize the dict items. */
1843 iter = PyObject_CallMethod(args, "iteritems", "()");
1844 if (iter == NULL)
1845 goto finally;
1846 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1848 res = batch_dict(self, iter);
1849 Py_LeaveRecursiveCall();
1851 Py_DECREF(iter);
1853 finally:
1854 if (self->fast && !fast_save_leave(self, args))
1855 res = -1;
1857 return res;
1861 static int
1862 save_inst(Picklerobject *self, PyObject *args)
1864 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1865 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1866 char *module_str, *name_str;
1867 int module_size, name_size, res = -1;
1869 static char inst = INST, obj = OBJ, build = BUILD;
1871 if (self->fast && !fast_save_enter(self, args))
1872 goto finally;
1874 if (self->write_func(self, &MARKv, 1) < 0)
1875 goto finally;
1877 if (!( class = PyObject_GetAttr(args, __class___str)))
1878 goto finally;
1880 if (self->bin) {
1881 if (save(self, class, 0) < 0)
1882 goto finally;
1885 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1886 PyObject *element = 0;
1887 int i, len;
1889 if (!( class_args =
1890 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
1891 goto finally;
1893 if ((len = PyObject_Size(class_args)) < 0)
1894 goto finally;
1896 for (i = 0; i < len; i++) {
1897 if (!( element = PySequence_GetItem(class_args, i)))
1898 goto finally;
1900 if (save(self, element, 0) < 0) {
1901 Py_DECREF(element);
1902 goto finally;
1905 Py_DECREF(element);
1908 else {
1909 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1910 PyErr_Clear();
1911 else
1912 goto finally;
1915 if (!self->bin) {
1916 if (!( name = ((PyClassObject *)class)->cl_name )) {
1917 PyErr_SetString(PicklingError, "class has no name");
1918 goto finally;
1921 if (!( module = whichmodule(class, name)))
1922 goto finally;
1925 if ((module_size = PyString_Size(module)) < 0 ||
1926 (name_size = PyString_Size(name)) < 0)
1927 goto finally;
1929 module_str = PyString_AS_STRING((PyStringObject *)module);
1930 name_str = PyString_AS_STRING((PyStringObject *)name);
1932 if (self->write_func(self, &inst, 1) < 0)
1933 goto finally;
1935 if (self->write_func(self, module_str, module_size) < 0)
1936 goto finally;
1938 if (self->write_func(self, "\n", 1) < 0)
1939 goto finally;
1941 if (self->write_func(self, name_str, name_size) < 0)
1942 goto finally;
1944 if (self->write_func(self, "\n", 1) < 0)
1945 goto finally;
1947 else if (self->write_func(self, &obj, 1) < 0) {
1948 goto finally;
1951 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1952 state = PyObject_Call(getstate_func, empty_tuple, NULL);
1953 if (!state)
1954 goto finally;
1956 else {
1957 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1958 PyErr_Clear();
1959 else
1960 goto finally;
1962 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1963 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1964 PyErr_Clear();
1965 else
1966 goto finally;
1967 res = 0;
1968 goto finally;
1972 if (!PyDict_Check(state)) {
1973 if (put2(self, args) < 0)
1974 goto finally;
1976 else {
1977 if (put(self, args) < 0)
1978 goto finally;
1981 if (save(self, state, 0) < 0)
1982 goto finally;
1984 if (self->write_func(self, &build, 1) < 0)
1985 goto finally;
1987 res = 0;
1989 finally:
1990 if (self->fast && !fast_save_leave(self, args))
1991 res = -1;
1993 Py_XDECREF(module);
1994 Py_XDECREF(class);
1995 Py_XDECREF(state);
1996 Py_XDECREF(getinitargs_func);
1997 Py_XDECREF(getstate_func);
1998 Py_XDECREF(class_args);
2000 return res;
2004 static int
2005 save_global(Picklerobject *self, PyObject *args, PyObject *name)
2007 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2008 char *name_str, *module_str;
2009 int module_size, name_size, res = -1;
2011 static char global = GLOBAL;
2013 if (name) {
2014 global_name = name;
2015 Py_INCREF(global_name);
2017 else {
2018 if (!( global_name = PyObject_GetAttr(args, __name___str)))
2019 goto finally;
2022 if (!( module = whichmodule(args, global_name)))
2023 goto finally;
2025 if ((module_size = PyString_Size(module)) < 0 ||
2026 (name_size = PyString_Size(global_name)) < 0)
2027 goto finally;
2029 module_str = PyString_AS_STRING((PyStringObject *)module);
2030 name_str = PyString_AS_STRING((PyStringObject *)global_name);
2032 /* XXX This can be doing a relative import. Clearly it shouldn't,
2033 but I don't know how to stop it. :-( */
2034 mod = PyImport_ImportModule(module_str);
2035 if (mod == NULL) {
2036 cPickle_ErrFormat(PicklingError,
2037 "Can't pickle %s: import of module %s "
2038 "failed",
2039 "OS", args, module);
2040 goto finally;
2042 klass = PyObject_GetAttrString(mod, name_str);
2043 if (klass == NULL) {
2044 cPickle_ErrFormat(PicklingError,
2045 "Can't pickle %s: attribute lookup %s.%s "
2046 "failed",
2047 "OSS", args, module, global_name);
2048 goto finally;
2050 if (klass != args) {
2051 Py_DECREF(klass);
2052 cPickle_ErrFormat(PicklingError,
2053 "Can't pickle %s: it's not the same object "
2054 "as %s.%s",
2055 "OSS", args, module, global_name);
2056 goto finally;
2058 Py_DECREF(klass);
2060 if (self->proto >= 2) {
2061 /* See whether this is in the extension registry, and if
2062 * so generate an EXT opcode.
2064 PyObject *py_code; /* extension code as Python object */
2065 long code; /* extension code as C value */
2066 char c_str[5];
2067 int n;
2069 PyTuple_SET_ITEM(two_tuple, 0, module);
2070 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2071 py_code = PyDict_GetItem(extension_registry, two_tuple);
2072 if (py_code == NULL)
2073 goto gen_global; /* not registered */
2075 /* Verify py_code has the right type and value. */
2076 if (!PyInt_Check(py_code)) {
2077 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2078 "extension code %s isn't an integer",
2079 "OO", args, py_code);
2080 goto finally;
2082 code = PyInt_AS_LONG(py_code);
2083 if (code <= 0 || code > 0x7fffffffL) {
2084 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2085 "extension code %ld is out of range",
2086 "Ol", args, code);
2087 goto finally;
2090 /* Generate an EXT opcode. */
2091 if (code <= 0xff) {
2092 c_str[0] = EXT1;
2093 c_str[1] = (char)code;
2094 n = 2;
2096 else if (code <= 0xffff) {
2097 c_str[0] = EXT2;
2098 c_str[1] = (char)(code & 0xff);
2099 c_str[2] = (char)((code >> 8) & 0xff);
2100 n = 3;
2102 else {
2103 c_str[0] = EXT4;
2104 c_str[1] = (char)(code & 0xff);
2105 c_str[2] = (char)((code >> 8) & 0xff);
2106 c_str[3] = (char)((code >> 16) & 0xff);
2107 c_str[4] = (char)((code >> 24) & 0xff);
2108 n = 5;
2111 if (self->write_func(self, c_str, n) >= 0)
2112 res = 0;
2113 goto finally; /* and don't memoize */
2116 gen_global:
2117 if (self->write_func(self, &global, 1) < 0)
2118 goto finally;
2120 if (self->write_func(self, module_str, module_size) < 0)
2121 goto finally;
2123 if (self->write_func(self, "\n", 1) < 0)
2124 goto finally;
2126 if (self->write_func(self, name_str, name_size) < 0)
2127 goto finally;
2129 if (self->write_func(self, "\n", 1) < 0)
2130 goto finally;
2132 if (put(self, args) < 0)
2133 goto finally;
2135 res = 0;
2137 finally:
2138 Py_XDECREF(module);
2139 Py_XDECREF(global_name);
2140 Py_XDECREF(mod);
2142 return res;
2145 static int
2146 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2148 PyObject *pid = 0;
2149 int size, res = -1;
2151 static char persid = PERSID, binpersid = BINPERSID;
2153 Py_INCREF(args);
2154 ARG_TUP(self, args);
2155 if (self->arg) {
2156 pid = PyObject_Call(f, self->arg, NULL);
2157 FREE_ARG_TUP(self);
2159 if (! pid) return -1;
2161 if (pid != Py_None) {
2162 if (!self->bin) {
2163 if (!PyString_Check(pid)) {
2164 PyErr_SetString(PicklingError,
2165 "persistent id must be string");
2166 goto finally;
2169 if (self->write_func(self, &persid, 1) < 0)
2170 goto finally;
2172 if ((size = PyString_Size(pid)) < 0)
2173 goto finally;
2175 if (self->write_func(self,
2176 PyString_AS_STRING(
2177 (PyStringObject *)pid),
2178 size) < 0)
2179 goto finally;
2181 if (self->write_func(self, "\n", 1) < 0)
2182 goto finally;
2184 res = 1;
2185 goto finally;
2187 else if (save(self, pid, 1) >= 0) {
2188 if (self->write_func(self, &binpersid, 1) < 0)
2189 res = -1;
2190 else
2191 res = 1;
2194 goto finally;
2197 res = 0;
2199 finally:
2200 Py_XDECREF(pid);
2202 return res;
2205 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2206 * appropriate __reduce__ method for ob.
2208 static int
2209 save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
2211 PyObject *callable;
2212 PyObject *argtup;
2213 PyObject *state = NULL;
2214 PyObject *listitems = NULL;
2215 PyObject *dictitems = NULL;
2217 int use_newobj = self->proto >= 2;
2219 static char reduce = REDUCE;
2220 static char build = BUILD;
2221 static char newobj = NEWOBJ;
2223 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2224 &callable,
2225 &argtup,
2226 &state,
2227 &listitems,
2228 &dictitems))
2229 return -1;
2231 if (!PyTuple_Check(argtup)) {
2232 PyErr_SetString(PicklingError,
2233 "args from reduce() should be a tuple");
2234 return -1;
2237 if (state == Py_None)
2238 state = NULL;
2239 if (listitems == Py_None)
2240 listitems = NULL;
2241 if (dictitems == Py_None)
2242 dictitems = NULL;
2244 /* Protocol 2 special case: if callable's name is __newobj__, use
2245 * NEWOBJ. This consumes a lot of code.
2247 if (use_newobj) {
2248 PyObject *temp = PyObject_GetAttr(callable, __name___str);
2250 if (temp == NULL) {
2251 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2252 PyErr_Clear();
2253 else
2254 return -1;
2255 use_newobj = 0;
2257 else {
2258 use_newobj = PyString_Check(temp) &&
2259 strcmp(PyString_AS_STRING(temp),
2260 "__newobj__") == 0;
2261 Py_DECREF(temp);
2264 if (use_newobj) {
2265 PyObject *cls;
2266 PyObject *newargtup;
2267 int n, i;
2269 /* Sanity checks. */
2270 n = PyTuple_Size(argtup);
2271 if (n < 1) {
2272 PyErr_SetString(PicklingError, "__newobj__ arglist "
2273 "is empty");
2274 return -1;
2277 cls = PyTuple_GET_ITEM(argtup, 0);
2278 if (! PyObject_HasAttrString(cls, "__new__")) {
2279 PyErr_SetString(PicklingError, "args[0] from "
2280 "__newobj__ args has no __new__");
2281 return -1;
2284 /* XXX How could ob be NULL? */
2285 if (ob != NULL) {
2286 PyObject *ob_dot_class;
2288 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2289 if (ob_dot_class == NULL) {
2290 if (PyErr_ExceptionMatches(
2291 PyExc_AttributeError))
2292 PyErr_Clear();
2293 else
2294 return -1;
2296 i = ob_dot_class != cls; /* true iff a problem */
2297 Py_XDECREF(ob_dot_class);
2298 if (i) {
2299 PyErr_SetString(PicklingError, "args[0] from "
2300 "__newobj__ args has the wrong class");
2301 return -1;
2305 /* Save the class and its __new__ arguments. */
2306 if (save(self, cls, 0) < 0)
2307 return -1;
2309 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2310 if (newargtup == NULL)
2311 return -1;
2312 for (i = 1; i < n; ++i) {
2313 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2314 Py_INCREF(temp);
2315 PyTuple_SET_ITEM(newargtup, i-1, temp);
2317 i = save(self, newargtup, 0);
2318 Py_DECREF(newargtup);
2319 if (i < 0)
2320 return -1;
2322 /* Add NEWOBJ opcode. */
2323 if (self->write_func(self, &newobj, 1) < 0)
2324 return -1;
2326 else {
2327 /* Not using NEWOBJ. */
2328 if (save(self, callable, 0) < 0 ||
2329 save(self, argtup, 0) < 0 ||
2330 self->write_func(self, &reduce, 1) < 0)
2331 return -1;
2334 /* Memoize. */
2335 /* XXX How can ob be NULL? */
2336 if (ob != NULL) {
2337 if (state && !PyDict_Check(state)) {
2338 if (put2(self, ob) < 0)
2339 return -1;
2341 else if (put(self, ob) < 0)
2342 return -1;
2346 if (listitems && batch_list(self, listitems) < 0)
2347 return -1;
2349 if (dictitems && batch_dict(self, dictitems) < 0)
2350 return -1;
2352 if (state) {
2353 if (save(self, state, 0) < 0 ||
2354 self->write_func(self, &build, 1) < 0)
2355 return -1;
2358 return 0;
2361 static int
2362 save(Picklerobject *self, PyObject *args, int pers_save)
2364 PyTypeObject *type;
2365 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2366 PyObject *arg_tup;
2367 int res = -1;
2368 int tmp, size;
2370 if (Py_EnterRecursiveCall(" while pickling an object"))
2371 return -1;
2373 if (!pers_save && self->pers_func) {
2374 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2375 res = tmp;
2376 goto finally;
2380 if (args == Py_None) {
2381 res = save_none(self, args);
2382 goto finally;
2385 type = Py_TYPE(args);
2387 switch (type->tp_name[0]) {
2388 case 'b':
2389 if (args == Py_False || args == Py_True) {
2390 res = save_bool(self, args);
2391 goto finally;
2393 break;
2394 case 'i':
2395 if (type == &PyInt_Type) {
2396 res = save_int(self, args);
2397 goto finally;
2399 break;
2401 case 'l':
2402 if (type == &PyLong_Type) {
2403 res = save_long(self, args);
2404 goto finally;
2406 break;
2408 case 'f':
2409 if (type == &PyFloat_Type) {
2410 res = save_float(self, args);
2411 goto finally;
2413 break;
2415 case 't':
2416 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2417 res = save_tuple(self, args);
2418 goto finally;
2420 break;
2422 case 's':
2423 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2424 res = save_string(self, args, 0);
2425 goto finally;
2427 break;
2429 #ifdef Py_USING_UNICODE
2430 case 'u':
2431 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2432 res = save_unicode(self, args, 0);
2433 goto finally;
2435 break;
2436 #endif
2439 if (Py_REFCNT(args) > 1) {
2440 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2441 goto finally;
2443 if (PyDict_GetItem(self->memo, py_ob_id)) {
2444 if (get(self, py_ob_id) < 0)
2445 goto finally;
2447 res = 0;
2448 goto finally;
2452 switch (type->tp_name[0]) {
2453 case 's':
2454 if (type == &PyString_Type) {
2455 res = save_string(self, args, 1);
2456 goto finally;
2458 break;
2460 #ifdef Py_USING_UNICODE
2461 case 'u':
2462 if (type == &PyUnicode_Type) {
2463 res = save_unicode(self, args, 1);
2464 goto finally;
2466 break;
2467 #endif
2469 case 't':
2470 if (type == &PyTuple_Type) {
2471 res = save_tuple(self, args);
2472 goto finally;
2474 if (type == &PyType_Type) {
2475 res = save_global(self, args, NULL);
2476 goto finally;
2478 break;
2480 case 'l':
2481 if (type == &PyList_Type) {
2482 res = save_list(self, args);
2483 goto finally;
2485 break;
2487 case 'd':
2488 if (type == &PyDict_Type) {
2489 res = save_dict(self, args);
2490 goto finally;
2492 break;
2494 case 'i':
2495 if (type == &PyInstance_Type) {
2496 res = save_inst(self, args);
2497 goto finally;
2499 break;
2501 case 'c':
2502 if (type == &PyClass_Type) {
2503 res = save_global(self, args, NULL);
2504 goto finally;
2506 break;
2508 case 'f':
2509 if (type == &PyFunction_Type) {
2510 res = save_global(self, args, NULL);
2511 if (res && PyErr_ExceptionMatches(PickleError)) {
2512 /* fall back to reduce */
2513 PyErr_Clear();
2514 break;
2516 goto finally;
2518 break;
2520 case 'b':
2521 if (type == &PyCFunction_Type) {
2522 res = save_global(self, args, NULL);
2523 goto finally;
2527 if (!pers_save && self->inst_pers_func) {
2528 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2529 res = tmp;
2530 goto finally;
2534 if (PyType_IsSubtype(type, &PyType_Type)) {
2535 res = save_global(self, args, NULL);
2536 goto finally;
2539 /* Get a reduction callable, and call it. This may come from
2540 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2541 * or the object's __reduce__ method.
2543 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2544 if (__reduce__ != NULL) {
2545 Py_INCREF(__reduce__);
2546 Py_INCREF(args);
2547 ARG_TUP(self, args);
2548 if (self->arg) {
2549 t = PyObject_Call(__reduce__, self->arg, NULL);
2550 FREE_ARG_TUP(self);
2553 else {
2554 /* Check for a __reduce_ex__ method. */
2555 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2556 if (__reduce__ != NULL) {
2557 t = PyInt_FromLong(self->proto);
2558 if (t != NULL) {
2559 ARG_TUP(self, t);
2560 t = NULL;
2561 if (self->arg) {
2562 t = PyObject_Call(__reduce__,
2563 self->arg, NULL);
2564 FREE_ARG_TUP(self);
2568 else {
2569 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2570 PyErr_Clear();
2571 else
2572 goto finally;
2573 /* Check for a __reduce__ method. */
2574 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2575 if (__reduce__ != NULL) {
2576 t = PyObject_Call(__reduce__,
2577 empty_tuple, NULL);
2579 else {
2580 PyErr_SetObject(UnpickleableError, args);
2581 goto finally;
2586 if (t == NULL)
2587 goto finally;
2589 if (PyString_Check(t)) {
2590 res = save_global(self, args, t);
2591 goto finally;
2594 if (! PyTuple_Check(t)) {
2595 cPickle_ErrFormat(PicklingError, "Value returned by "
2596 "%s must be string or tuple",
2597 "O", __reduce__);
2598 goto finally;
2601 size = PyTuple_Size(t);
2602 if (size < 2 || size > 5) {
2603 cPickle_ErrFormat(PicklingError, "tuple returned by "
2604 "%s must contain 2 through 5 elements",
2605 "O", __reduce__);
2606 goto finally;
2609 arg_tup = PyTuple_GET_ITEM(t, 1);
2610 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2611 cPickle_ErrFormat(PicklingError, "Second element of "
2612 "tuple returned by %s must be a tuple",
2613 "O", __reduce__);
2614 goto finally;
2617 res = save_reduce(self, t, args);
2619 finally:
2620 Py_LeaveRecursiveCall();
2621 Py_XDECREF(py_ob_id);
2622 Py_XDECREF(__reduce__);
2623 Py_XDECREF(t);
2625 return res;
2629 static int
2630 dump(Picklerobject *self, PyObject *args)
2632 static char stop = STOP;
2634 if (self->proto >= 2) {
2635 char bytes[2];
2637 bytes[0] = PROTO;
2638 assert(self->proto >= 0 && self->proto < 256);
2639 bytes[1] = (char)self->proto;
2640 if (self->write_func(self, bytes, 2) < 0)
2641 return -1;
2644 if (save(self, args, 0) < 0)
2645 return -1;
2647 if (self->write_func(self, &stop, 1) < 0)
2648 return -1;
2650 if (self->write_func(self, NULL, 0) < 0)
2651 return -1;
2653 return 0;
2656 static PyObject *
2657 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2659 if (self->memo)
2660 PyDict_Clear(self->memo);
2661 Py_INCREF(Py_None);
2662 return Py_None;
2665 static PyObject *
2666 Pickle_getvalue(Picklerobject *self, PyObject *args)
2668 int l, i, rsize, ssize, clear=1, lm;
2669 long ik;
2670 PyObject *k, *r;
2671 char *s, *p, *have_get;
2672 Pdata *data;
2674 /* Can be called by Python code or C code */
2675 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2676 return NULL;
2678 /* Check to make sure we are based on a list */
2679 if (! Pdata_Check(self->file)) {
2680 PyErr_SetString(PicklingError,
2681 "Attempt to getvalue() a non-list-based pickler");
2682 return NULL;
2685 /* flush write buffer */
2686 if (write_other(self, NULL, 0) < 0) return NULL;
2688 data=(Pdata*)self->file;
2689 l=data->length;
2691 /* set up an array to hold get/put status */
2692 lm = PyDict_Size(self->memo);
2693 if (lm < 0) return NULL;
2694 lm++;
2695 have_get = malloc(lm);
2696 if (have_get == NULL) return PyErr_NoMemory();
2697 memset(have_get, 0, lm);
2699 /* Scan for gets. */
2700 for (rsize = 0, i = l; --i >= 0; ) {
2701 k = data->data[i];
2703 if (PyString_Check(k))
2704 rsize += PyString_GET_SIZE(k);
2706 else if (PyInt_Check(k)) { /* put */
2707 ik = PyInt_AS_LONG((PyIntObject*)k);
2708 if (ik >= lm || ik == 0) {
2709 PyErr_SetString(PicklingError,
2710 "Invalid get data");
2711 goto err;
2713 if (have_get[ik]) /* with matching get */
2714 rsize += ik < 256 ? 2 : 5;
2717 else if (! (PyTuple_Check(k) &&
2718 PyTuple_GET_SIZE(k) == 2 &&
2719 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2721 PyErr_SetString(PicklingError,
2722 "Unexpected data in internal list");
2723 goto err;
2726 else { /* put */
2727 ik = PyInt_AS_LONG((PyIntObject *)k);
2728 if (ik >= lm || ik == 0) {
2729 PyErr_SetString(PicklingError,
2730 "Invalid get data");
2731 return NULL;
2733 have_get[ik] = 1;
2734 rsize += ik < 256 ? 2 : 5;
2738 /* Now generate the result */
2739 r = PyString_FromStringAndSize(NULL, rsize);
2740 if (r == NULL) goto err;
2741 s = PyString_AS_STRING((PyStringObject *)r);
2743 for (i = 0; i < l; i++) {
2744 k = data->data[i];
2746 if (PyString_Check(k)) {
2747 ssize = PyString_GET_SIZE(k);
2748 if (ssize) {
2749 p=PyString_AS_STRING((PyStringObject *)k);
2750 while (--ssize >= 0)
2751 *s++ = *p++;
2755 else if (PyTuple_Check(k)) { /* get */
2756 ik = PyInt_AS_LONG((PyIntObject *)
2757 PyTuple_GET_ITEM(k, 0));
2758 if (ik < 256) {
2759 *s++ = BINGET;
2760 *s++ = (int)(ik & 0xff);
2762 else {
2763 *s++ = LONG_BINGET;
2764 *s++ = (int)(ik & 0xff);
2765 *s++ = (int)((ik >> 8) & 0xff);
2766 *s++ = (int)((ik >> 16) & 0xff);
2767 *s++ = (int)((ik >> 24) & 0xff);
2771 else { /* put */
2772 ik = PyInt_AS_LONG((PyIntObject*)k);
2774 if (have_get[ik]) { /* with matching get */
2775 if (ik < 256) {
2776 *s++ = BINPUT;
2777 *s++ = (int)(ik & 0xff);
2779 else {
2780 *s++ = LONG_BINPUT;
2781 *s++ = (int)(ik & 0xff);
2782 *s++ = (int)((ik >> 8) & 0xff);
2783 *s++ = (int)((ik >> 16) & 0xff);
2784 *s++ = (int)((ik >> 24) & 0xff);
2790 if (clear) {
2791 PyDict_Clear(self->memo);
2792 Pdata_clear(data, 0);
2795 free(have_get);
2796 return r;
2797 err:
2798 free(have_get);
2799 return NULL;
2802 static PyObject *
2803 Pickler_dump(Picklerobject *self, PyObject *args)
2805 PyObject *ob;
2806 int get=0;
2808 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2809 return NULL;
2811 if (dump(self, ob) < 0)
2812 return NULL;
2814 if (get) return Pickle_getvalue(self, NULL);
2816 /* XXX Why does dump() return self? */
2817 Py_INCREF(self);
2818 return (PyObject*)self;
2822 static struct PyMethodDef Pickler_methods[] =
2824 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2825 PyDoc_STR("dump(object) -- "
2826 "Write an object in pickle format to the object's pickle stream")},
2827 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2828 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2829 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2830 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2831 {NULL, NULL} /* sentinel */
2835 static Picklerobject *
2836 newPicklerobject(PyObject *file, int proto)
2838 Picklerobject *self;
2840 if (proto < 0)
2841 proto = HIGHEST_PROTOCOL;
2842 if (proto > HIGHEST_PROTOCOL) {
2843 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2844 "the highest available protocol is %d",
2845 proto, HIGHEST_PROTOCOL);
2846 return NULL;
2849 self = PyObject_GC_New(Picklerobject, &Picklertype);
2850 if (self == NULL)
2851 return NULL;
2852 self->proto = proto;
2853 self->bin = proto > 0;
2854 self->fp = NULL;
2855 self->write = NULL;
2856 self->memo = NULL;
2857 self->arg = NULL;
2858 self->pers_func = NULL;
2859 self->inst_pers_func = NULL;
2860 self->write_buf = NULL;
2861 self->fast = 0;
2862 self->fast_container = 0;
2863 self->fast_memo = NULL;
2864 self->buf_size = 0;
2865 self->dispatch_table = NULL;
2867 self->file = NULL;
2868 if (file)
2869 Py_INCREF(file);
2870 else {
2871 file = Pdata_New();
2872 if (file == NULL)
2873 goto err;
2875 self->file = file;
2877 if (!( self->memo = PyDict_New()))
2878 goto err;
2880 if (PyFile_Check(file)) {
2881 self->fp = PyFile_AsFile(file);
2882 if (self->fp == NULL) {
2883 PyErr_SetString(PyExc_ValueError,
2884 "I/O operation on closed file");
2885 goto err;
2887 self->write_func = write_file;
2889 else if (PycStringIO_OutputCheck(file)) {
2890 self->write_func = write_cStringIO;
2892 else if (file == Py_None) {
2893 self->write_func = write_none;
2895 else {
2896 self->write_func = write_other;
2898 if (! Pdata_Check(file)) {
2899 self->write = PyObject_GetAttr(file, write_str);
2900 if (!self->write) {
2901 PyErr_Clear();
2902 PyErr_SetString(PyExc_TypeError,
2903 "argument must have 'write' "
2904 "attribute");
2905 goto err;
2909 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2910 if (self->write_buf == NULL) {
2911 PyErr_NoMemory();
2912 goto err;
2916 if (PyEval_GetRestricted()) {
2917 /* Restricted execution, get private tables */
2918 PyObject *m = PyImport_Import(copyreg_str);
2920 if (m == NULL)
2921 goto err;
2922 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
2923 Py_DECREF(m);
2924 if (self->dispatch_table == NULL)
2925 goto err;
2927 else {
2928 self->dispatch_table = dispatch_table;
2929 Py_INCREF(dispatch_table);
2931 PyObject_GC_Track(self);
2933 return self;
2935 err:
2936 Py_DECREF(self);
2937 return NULL;
2941 static PyObject *
2942 get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
2944 static char *kwlist[] = {"file", "protocol", NULL};
2945 PyObject *file = NULL;
2946 int proto = 0;
2948 /* XXX
2949 * The documented signature is Pickler(file, protocol=0), but this
2950 * accepts Pickler() and Pickler(integer) too. The meaning then
2951 * is clear as mud, undocumented, and not supported by pickle.py.
2952 * I'm told Zope uses this, but I haven't traced into this code
2953 * far enough to figure out what it means.
2955 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
2956 PyErr_Clear();
2957 proto = 0;
2958 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2959 kwlist, &file, &proto))
2960 return NULL;
2962 return (PyObject *)newPicklerobject(file, proto);
2966 static void
2967 Pickler_dealloc(Picklerobject *self)
2969 PyObject_GC_UnTrack(self);
2970 Py_XDECREF(self->write);
2971 Py_XDECREF(self->memo);
2972 Py_XDECREF(self->fast_memo);
2973 Py_XDECREF(self->arg);
2974 Py_XDECREF(self->file);
2975 Py_XDECREF(self->pers_func);
2976 Py_XDECREF(self->inst_pers_func);
2977 Py_XDECREF(self->dispatch_table);
2978 PyMem_Free(self->write_buf);
2979 Py_TYPE(self)->tp_free((PyObject *)self);
2982 static int
2983 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2985 Py_VISIT(self->write);
2986 Py_VISIT(self->memo);
2987 Py_VISIT(self->fast_memo);
2988 Py_VISIT(self->arg);
2989 Py_VISIT(self->file);
2990 Py_VISIT(self->pers_func);
2991 Py_VISIT(self->inst_pers_func);
2992 Py_VISIT(self->dispatch_table);
2993 return 0;
2996 static int
2997 Pickler_clear(Picklerobject *self)
2999 Py_CLEAR(self->write);
3000 Py_CLEAR(self->memo);
3001 Py_CLEAR(self->fast_memo);
3002 Py_CLEAR(self->arg);
3003 Py_CLEAR(self->file);
3004 Py_CLEAR(self->pers_func);
3005 Py_CLEAR(self->inst_pers_func);
3006 Py_CLEAR(self->dispatch_table);
3007 return 0;
3010 static PyObject *
3011 Pickler_get_pers_func(Picklerobject *p)
3013 if (p->pers_func == NULL)
3014 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3015 else
3016 Py_INCREF(p->pers_func);
3017 return p->pers_func;
3020 static int
3021 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3023 if (v == NULL) {
3024 PyErr_SetString(PyExc_TypeError,
3025 "attribute deletion is not supported");
3026 return -1;
3028 Py_XDECREF(p->pers_func);
3029 Py_INCREF(v);
3030 p->pers_func = v;
3031 return 0;
3034 static int
3035 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3037 if (v == NULL) {
3038 PyErr_SetString(PyExc_TypeError,
3039 "attribute deletion is not supported");
3040 return -1;
3042 Py_XDECREF(p->inst_pers_func);
3043 Py_INCREF(v);
3044 p->inst_pers_func = v;
3045 return 0;
3048 static PyObject *
3049 Pickler_get_memo(Picklerobject *p)
3051 if (p->memo == NULL)
3052 PyErr_SetString(PyExc_AttributeError, "memo");
3053 else
3054 Py_INCREF(p->memo);
3055 return p->memo;
3058 static int
3059 Pickler_set_memo(Picklerobject *p, PyObject *v)
3061 if (v == NULL) {
3062 PyErr_SetString(PyExc_TypeError,
3063 "attribute deletion is not supported");
3064 return -1;
3066 if (!PyDict_Check(v)) {
3067 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3068 return -1;
3070 Py_XDECREF(p->memo);
3071 Py_INCREF(v);
3072 p->memo = v;
3073 return 0;
3076 static PyObject *
3077 Pickler_get_error(Picklerobject *p)
3079 /* why is this an attribute on the Pickler? */
3080 Py_INCREF(PicklingError);
3081 return PicklingError;
3084 static PyMemberDef Pickler_members[] = {
3085 {"binary", T_INT, offsetof(Picklerobject, bin)},
3086 {"fast", T_INT, offsetof(Picklerobject, fast)},
3087 {NULL}
3090 static PyGetSetDef Pickler_getsets[] = {
3091 {"persistent_id", (getter)Pickler_get_pers_func,
3092 (setter)Pickler_set_pers_func},
3093 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3094 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3095 {"PicklingError", (getter)Pickler_get_error, NULL},
3096 {NULL}
3099 PyDoc_STRVAR(Picklertype__doc__,
3100 "Objects that know how to pickle objects\n");
3102 static PyTypeObject Picklertype = {
3103 PyVarObject_HEAD_INIT(NULL, 0)
3104 "cPickle.Pickler", /*tp_name*/
3105 sizeof(Picklerobject), /*tp_basicsize*/
3107 (destructor)Pickler_dealloc, /* tp_dealloc */
3108 0, /* tp_print */
3109 0, /* tp_getattr */
3110 0, /* tp_setattr */
3111 0, /* tp_compare */
3112 0, /* tp_repr */
3113 0, /* tp_as_number */
3114 0, /* tp_as_sequence */
3115 0, /* tp_as_mapping */
3116 0, /* tp_hash */
3117 0, /* tp_call */
3118 0, /* tp_str */
3119 PyObject_GenericGetAttr, /* tp_getattro */
3120 PyObject_GenericSetAttr, /* tp_setattro */
3121 0, /* tp_as_buffer */
3122 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3123 Picklertype__doc__, /* tp_doc */
3124 (traverseproc)Pickler_traverse, /* tp_traverse */
3125 (inquiry)Pickler_clear, /* tp_clear */
3126 0, /* tp_richcompare */
3127 0, /* tp_weaklistoffset */
3128 0, /* tp_iter */
3129 0, /* tp_iternext */
3130 Pickler_methods, /* tp_methods */
3131 Pickler_members, /* tp_members */
3132 Pickler_getsets, /* tp_getset */
3135 static PyObject *
3136 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3138 PyObject *global = 0, *module;
3140 if (fc) {
3141 if (fc==Py_None) {
3142 PyErr_SetString(UnpicklingError, "Global and instance "
3143 "pickles are not supported.");
3144 return NULL;
3146 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3147 py_global_name, NULL);
3150 module = PySys_GetObject("modules");
3151 if (module == NULL)
3152 return NULL;
3154 module = PyDict_GetItem(module, py_module_name);
3155 if (module == NULL) {
3156 module = PyImport_Import(py_module_name);
3157 if (!module)
3158 return NULL;
3159 global = PyObject_GetAttr(module, py_global_name);
3160 Py_DECREF(module);
3162 else
3163 global = PyObject_GetAttr(module, py_global_name);
3164 return global;
3167 static int
3168 marker(Unpicklerobject *self)
3170 if (self->num_marks < 1) {
3171 PyErr_SetString(UnpicklingError, "could not find MARK");
3172 return -1;
3175 return self->marks[--self->num_marks];
3179 static int
3180 load_none(Unpicklerobject *self)
3182 PDATA_APPEND(self->stack, Py_None, -1);
3183 return 0;
3186 static int
3187 bad_readline(void)
3189 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3190 return -1;
3193 static int
3194 load_int(Unpicklerobject *self)
3196 PyObject *py_int = 0;
3197 char *endptr, *s;
3198 int len, res = -1;
3199 long l;
3201 if ((len = self->readline_func(self, &s)) < 0) return -1;
3202 if (len < 2) return bad_readline();
3203 if (!( s=pystrndup(s,len))) return -1;
3205 errno = 0;
3206 l = strtol(s, &endptr, 0);
3208 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3209 /* Hm, maybe we've got something long. Let's try reading
3210 it as a Python long object. */
3211 errno = 0;
3212 py_int = PyLong_FromString(s, NULL, 0);
3213 if (py_int == NULL) {
3214 PyErr_SetString(PyExc_ValueError,
3215 "could not convert string to int");
3216 goto finally;
3219 else {
3220 if (len == 3 && (l == 0 || l == 1)) {
3221 if (!( py_int = PyBool_FromLong(l))) goto finally;
3223 else {
3224 if (!( py_int = PyInt_FromLong(l))) goto finally;
3228 free(s);
3229 PDATA_PUSH(self->stack, py_int, -1);
3230 return 0;
3232 finally:
3233 free(s);
3235 return res;
3238 static int
3239 load_bool(Unpicklerobject *self, PyObject *boolean)
3241 assert(boolean == Py_True || boolean == Py_False);
3242 PDATA_APPEND(self->stack, boolean, -1);
3243 return 0;
3246 /* s contains x bytes of a little-endian integer. Return its value as a
3247 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3248 * int, but when x is 4 it's a signed one. This is an historical source
3249 * of x-platform bugs.
3251 static long
3252 calc_binint(char *s, int x)
3254 unsigned char c;
3255 int i;
3256 long l;
3258 for (i = 0, l = 0L; i < x; i++) {
3259 c = (unsigned char)s[i];
3260 l |= (long)c << (i * 8);
3262 #if SIZEOF_LONG > 4
3263 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3264 * is signed, so on a box with longs bigger than 4 bytes we need
3265 * to extend a BININT's sign bit to the full width.
3267 if (x == 4 && l & (1L << 31))
3268 l |= (~0L) << 32;
3269 #endif
3270 return l;
3274 static int
3275 load_binintx(Unpicklerobject *self, char *s, int x)
3277 PyObject *py_int = 0;
3278 long l;
3280 l = calc_binint(s, x);
3282 if (!( py_int = PyInt_FromLong(l)))
3283 return -1;
3285 PDATA_PUSH(self->stack, py_int, -1);
3286 return 0;
3290 static int
3291 load_binint(Unpicklerobject *self)
3293 char *s;
3295 if (self->read_func(self, &s, 4) < 0)
3296 return -1;
3298 return load_binintx(self, s, 4);
3302 static int
3303 load_binint1(Unpicklerobject *self)
3305 char *s;
3307 if (self->read_func(self, &s, 1) < 0)
3308 return -1;
3310 return load_binintx(self, s, 1);
3314 static int
3315 load_binint2(Unpicklerobject *self)
3317 char *s;
3319 if (self->read_func(self, &s, 2) < 0)
3320 return -1;
3322 return load_binintx(self, s, 2);
3325 static int
3326 load_long(Unpicklerobject *self)
3328 PyObject *l = 0;
3329 char *end, *s;
3330 int len, res = -1;
3332 if ((len = self->readline_func(self, &s)) < 0) return -1;
3333 if (len < 2) return bad_readline();
3334 if (!( s=pystrndup(s,len))) return -1;
3336 if (!( l = PyLong_FromString(s, &end, 0)))
3337 goto finally;
3339 free(s);
3340 PDATA_PUSH(self->stack, l, -1);
3341 return 0;
3343 finally:
3344 free(s);
3346 return res;
3349 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3350 * data following.
3352 static int
3353 load_counted_long(Unpicklerobject *self, int size)
3355 Py_ssize_t i;
3356 char *nbytes;
3357 unsigned char *pdata;
3358 PyObject *along;
3360 assert(size == 1 || size == 4);
3361 i = self->read_func(self, &nbytes, size);
3362 if (i < 0) return -1;
3364 size = calc_binint(nbytes, size);
3365 if (size < 0) {
3366 /* Corrupt or hostile pickle -- we never write one like
3367 * this.
3369 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3370 "byte count");
3371 return -1;
3374 if (size == 0)
3375 along = PyLong_FromLong(0L);
3376 else {
3377 /* Read the raw little-endian bytes & convert. */
3378 i = self->read_func(self, (char **)&pdata, size);
3379 if (i < 0) return -1;
3380 along = _PyLong_FromByteArray(pdata, (size_t)size,
3381 1 /* little endian */, 1 /* signed */);
3383 if (along == NULL)
3384 return -1;
3385 PDATA_PUSH(self->stack, along, -1);
3386 return 0;
3389 static int
3390 load_float(Unpicklerobject *self)
3392 PyObject *py_float = 0;
3393 char *endptr, *s;
3394 int len, res = -1;
3395 double d;
3397 if ((len = self->readline_func(self, &s)) < 0) return -1;
3398 if (len < 2) return bad_readline();
3399 if (!( s=pystrndup(s,len))) return -1;
3401 errno = 0;
3402 d = PyOS_ascii_strtod(s, &endptr);
3404 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3405 PyErr_SetString(PyExc_ValueError,
3406 "could not convert string to float");
3407 goto finally;
3410 if (!( py_float = PyFloat_FromDouble(d)))
3411 goto finally;
3413 free(s);
3414 PDATA_PUSH(self->stack, py_float, -1);
3415 return 0;
3417 finally:
3418 free(s);
3420 return res;
3423 static int
3424 load_binfloat(Unpicklerobject *self)
3426 PyObject *py_float;
3427 double x;
3428 char *p;
3430 if (self->read_func(self, &p, 8) < 0)
3431 return -1;
3433 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3434 if (x == -1.0 && PyErr_Occurred())
3435 return -1;
3437 py_float = PyFloat_FromDouble(x);
3438 if (py_float == NULL)
3439 return -1;
3441 PDATA_PUSH(self->stack, py_float, -1);
3442 return 0;
3445 static int
3446 load_string(Unpicklerobject *self)
3448 PyObject *str = 0;
3449 int len, res = -1;
3450 char *s, *p;
3452 if ((len = self->readline_func(self, &s)) < 0) return -1;
3453 if (len < 2) return bad_readline();
3454 if (!( s=pystrndup(s,len))) return -1;
3457 /* Strip outermost quotes */
3458 while (s[len-1] <= ' ')
3459 len--;
3460 if(s[0]=='"' && s[len-1]=='"'){
3461 s[len-1] = '\0';
3462 p = s + 1 ;
3463 len -= 2;
3464 } else if(s[0]=='\'' && s[len-1]=='\''){
3465 s[len-1] = '\0';
3466 p = s + 1 ;
3467 len -= 2;
3468 } else
3469 goto insecure;
3470 /********************************************/
3472 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3473 free(s);
3474 if (str) {
3475 PDATA_PUSH(self->stack, str, -1);
3476 res = 0;
3478 return res;
3480 insecure:
3481 free(s);
3482 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3483 return -1;
3487 static int
3488 load_binstring(Unpicklerobject *self)
3490 PyObject *py_string = 0;
3491 long l;
3492 char *s;
3494 if (self->read_func(self, &s, 4) < 0) return -1;
3496 l = calc_binint(s, 4);
3497 if (l < 0) {
3498 /* Corrupt or hostile pickle -- we never write one like
3499 * this.
3501 PyErr_SetString(UnpicklingError,
3502 "BINSTRING pickle has negative byte count");
3503 return -1;
3506 if (self->read_func(self, &s, l) < 0)
3507 return -1;
3509 if (!( py_string = PyString_FromStringAndSize(s, l)))
3510 return -1;
3512 PDATA_PUSH(self->stack, py_string, -1);
3513 return 0;
3517 static int
3518 load_short_binstring(Unpicklerobject *self)
3520 PyObject *py_string = 0;
3521 unsigned char l;
3522 char *s;
3524 if (self->read_func(self, &s, 1) < 0)
3525 return -1;
3527 l = (unsigned char)s[0];
3529 if (self->read_func(self, &s, l) < 0) return -1;
3531 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
3533 PDATA_PUSH(self->stack, py_string, -1);
3534 return 0;
3538 #ifdef Py_USING_UNICODE
3539 static int
3540 load_unicode(Unpicklerobject *self)
3542 PyObject *str = 0;
3543 int len, res = -1;
3544 char *s;
3546 if ((len = self->readline_func(self, &s)) < 0) return -1;
3547 if (len < 1) return bad_readline();
3549 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3550 goto finally;
3552 PDATA_PUSH(self->stack, str, -1);
3553 return 0;
3555 finally:
3556 return res;
3558 #endif
3561 #ifdef Py_USING_UNICODE
3562 static int
3563 load_binunicode(Unpicklerobject *self)
3565 PyObject *unicode;
3566 long l;
3567 char *s;
3569 if (self->read_func(self, &s, 4) < 0) return -1;
3571 l = calc_binint(s, 4);
3572 if (l < 0) {
3573 /* Corrupt or hostile pickle -- we never write one like
3574 * this.
3576 PyErr_SetString(UnpicklingError,
3577 "BINUNICODE pickle has negative byte count");
3578 return -1;
3581 if (self->read_func(self, &s, l) < 0)
3582 return -1;
3584 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3585 return -1;
3587 PDATA_PUSH(self->stack, unicode, -1);
3588 return 0;
3590 #endif
3593 static int
3594 load_tuple(Unpicklerobject *self)
3596 PyObject *tup;
3597 int i;
3599 if ((i = marker(self)) < 0) return -1;
3600 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3601 PDATA_PUSH(self->stack, tup, -1);
3602 return 0;
3605 static int
3606 load_counted_tuple(Unpicklerobject *self, int len)
3608 PyObject *tup = PyTuple_New(len);
3610 if (tup == NULL)
3611 return -1;
3613 while (--len >= 0) {
3614 PyObject *element;
3616 PDATA_POP(self->stack, element);
3617 if (element == NULL)
3618 return -1;
3619 PyTuple_SET_ITEM(tup, len, element);
3621 PDATA_PUSH(self->stack, tup, -1);
3622 return 0;
3625 static int
3626 load_empty_list(Unpicklerobject *self)
3628 PyObject *list;
3630 if (!( list=PyList_New(0))) return -1;
3631 PDATA_PUSH(self->stack, list, -1);
3632 return 0;
3635 static int
3636 load_empty_dict(Unpicklerobject *self)
3638 PyObject *dict;
3640 if (!( dict=PyDict_New())) return -1;
3641 PDATA_PUSH(self->stack, dict, -1);
3642 return 0;
3646 static int
3647 load_list(Unpicklerobject *self)
3649 PyObject *list = 0;
3650 int i;
3652 if ((i = marker(self)) < 0) return -1;
3653 if (!( list=Pdata_popList(self->stack, i))) return -1;
3654 PDATA_PUSH(self->stack, list, -1);
3655 return 0;
3658 static int
3659 load_dict(Unpicklerobject *self)
3661 PyObject *dict, *key, *value;
3662 int i, j, k;
3664 if ((i = marker(self)) < 0) return -1;
3665 j=self->stack->length;
3667 if (!( dict = PyDict_New())) return -1;
3669 for (k = i+1; k < j; k += 2) {
3670 key =self->stack->data[k-1];
3671 value=self->stack->data[k ];
3672 if (PyDict_SetItem(dict, key, value) < 0) {
3673 Py_DECREF(dict);
3674 return -1;
3677 Pdata_clear(self->stack, i);
3678 PDATA_PUSH(self->stack, dict, -1);
3679 return 0;
3682 static PyObject *
3683 Instance_New(PyObject *cls, PyObject *args)
3685 PyObject *r = 0;
3687 if (PyClass_Check(cls)) {
3688 int l;
3690 if ((l=PyObject_Size(args)) < 0) goto err;
3691 if (!( l )) {
3692 PyObject *__getinitargs__;
3694 __getinitargs__ = PyObject_GetAttr(cls,
3695 __getinitargs___str);
3696 if (!__getinitargs__) {
3697 /* We have a class with no __getinitargs__,
3698 so bypass usual construction */
3699 PyObject *inst;
3701 PyErr_Clear();
3702 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3703 goto err;
3704 return inst;
3706 Py_DECREF(__getinitargs__);
3709 if ((r=PyInstance_New(cls, args, NULL))) return r;
3710 else goto err;
3713 if ((r=PyObject_CallObject(cls, args))) return r;
3715 err:
3717 PyObject *tp, *v, *tb, *tmp_value;
3719 PyErr_Fetch(&tp, &v, &tb);
3720 tmp_value = v;
3721 /* NULL occurs when there was a KeyboardInterrupt */
3722 if (tmp_value == NULL)
3723 tmp_value = Py_None;
3724 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3725 Py_XDECREF(v);
3726 v=r;
3728 PyErr_Restore(tp,v,tb);
3730 return NULL;
3734 static int
3735 load_obj(Unpicklerobject *self)
3737 PyObject *class, *tup, *obj=0;
3738 int i;
3740 if ((i = marker(self)) < 0) return -1;
3741 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3742 PDATA_POP(self->stack, class);
3743 if (class) {
3744 obj = Instance_New(class, tup);
3745 Py_DECREF(class);
3747 Py_DECREF(tup);
3749 if (! obj) return -1;
3750 PDATA_PUSH(self->stack, obj, -1);
3751 return 0;
3755 static int
3756 load_inst(Unpicklerobject *self)
3758 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3759 int i, len;
3760 char *s;
3762 if ((i = marker(self)) < 0) return -1;
3764 if ((len = self->readline_func(self, &s)) < 0) return -1;
3765 if (len < 2) return bad_readline();
3766 module_name = PyString_FromStringAndSize(s, len - 1);
3767 if (!module_name) return -1;
3769 if ((len = self->readline_func(self, &s)) >= 0) {
3770 if (len < 2) return bad_readline();
3771 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3772 class = find_class(module_name, class_name,
3773 self->find_class);
3774 Py_DECREF(class_name);
3777 Py_DECREF(module_name);
3779 if (! class) return -1;
3781 if ((tup=Pdata_popTuple(self->stack, i))) {
3782 obj = Instance_New(class, tup);
3783 Py_DECREF(tup);
3785 Py_DECREF(class);
3787 if (! obj) return -1;
3789 PDATA_PUSH(self->stack, obj, -1);
3790 return 0;
3793 static int
3794 load_newobj(Unpicklerobject *self)
3796 PyObject *args = NULL;
3797 PyObject *clsraw = NULL;
3798 PyTypeObject *cls; /* clsraw cast to its true type */
3799 PyObject *obj;
3801 /* Stack is ... cls argtuple, and we want to call
3802 * cls.__new__(cls, *argtuple).
3804 PDATA_POP(self->stack, args);
3805 if (args == NULL) goto Fail;
3806 if (! PyTuple_Check(args)) {
3807 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3808 "tuple.");
3809 goto Fail;
3812 PDATA_POP(self->stack, clsraw);
3813 cls = (PyTypeObject *)clsraw;
3814 if (cls == NULL) goto Fail;
3815 if (! PyType_Check(cls)) {
3816 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3817 "isn't a type object");
3818 goto Fail;
3820 if (cls->tp_new == NULL) {
3821 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3822 "has NULL tp_new");
3823 goto Fail;
3826 /* Call __new__. */
3827 obj = cls->tp_new(cls, args, NULL);
3828 if (obj == NULL) goto Fail;
3830 Py_DECREF(args);
3831 Py_DECREF(clsraw);
3832 PDATA_PUSH(self->stack, obj, -1);
3833 return 0;
3835 Fail:
3836 Py_XDECREF(args);
3837 Py_XDECREF(clsraw);
3838 return -1;
3841 static int
3842 load_global(Unpicklerobject *self)
3844 PyObject *class = 0, *module_name = 0, *class_name = 0;
3845 int len;
3846 char *s;
3848 if ((len = self->readline_func(self, &s)) < 0) return -1;
3849 if (len < 2) return bad_readline();
3850 module_name = PyString_FromStringAndSize(s, len - 1);
3851 if (!module_name) return -1;
3853 if ((len = self->readline_func(self, &s)) >= 0) {
3854 if (len < 2) {
3855 Py_DECREF(module_name);
3856 return bad_readline();
3858 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3859 class = find_class(module_name, class_name,
3860 self->find_class);
3861 Py_DECREF(class_name);
3864 Py_DECREF(module_name);
3866 if (! class) return -1;
3867 PDATA_PUSH(self->stack, class, -1);
3868 return 0;
3872 static int
3873 load_persid(Unpicklerobject *self)
3875 PyObject *pid = 0;
3876 int len;
3877 char *s;
3879 if (self->pers_func) {
3880 if ((len = self->readline_func(self, &s)) < 0) return -1;
3881 if (len < 2) return bad_readline();
3883 pid = PyString_FromStringAndSize(s, len - 1);
3884 if (!pid) return -1;
3886 if (PyList_Check(self->pers_func)) {
3887 if (PyList_Append(self->pers_func, pid) < 0) {
3888 Py_DECREF(pid);
3889 return -1;
3892 else {
3893 ARG_TUP(self, pid);
3894 if (self->arg) {
3895 pid = PyObject_Call(self->pers_func, self->arg,
3896 NULL);
3897 FREE_ARG_TUP(self);
3901 if (! pid) return -1;
3903 PDATA_PUSH(self->stack, pid, -1);
3904 return 0;
3906 else {
3907 PyErr_SetString(UnpicklingError,
3908 "A load persistent id instruction was encountered,\n"
3909 "but no persistent_load function was specified.");
3910 return -1;
3914 static int
3915 load_binpersid(Unpicklerobject *self)
3917 PyObject *pid = 0;
3919 if (self->pers_func) {
3920 PDATA_POP(self->stack, pid);
3921 if (! pid) return -1;
3923 if (PyList_Check(self->pers_func)) {
3924 if (PyList_Append(self->pers_func, pid) < 0) {
3925 Py_DECREF(pid);
3926 return -1;
3929 else {
3930 ARG_TUP(self, pid);
3931 if (self->arg) {
3932 pid = PyObject_Call(self->pers_func, self->arg,
3933 NULL);
3934 FREE_ARG_TUP(self);
3936 if (! pid) return -1;
3939 PDATA_PUSH(self->stack, pid, -1);
3940 return 0;
3942 else {
3943 PyErr_SetString(UnpicklingError,
3944 "A load persistent id instruction was encountered,\n"
3945 "but no persistent_load function was specified.");
3946 return -1;
3951 static int
3952 load_pop(Unpicklerobject *self)
3954 int len;
3956 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3958 /* Note that we split the (pickle.py) stack into two stacks,
3959 an object stack and a mark stack. We have to be clever and
3960 pop the right one. We do this by looking at the top of the
3961 mark stack.
3964 if ((self->num_marks > 0) &&
3965 (self->marks[self->num_marks - 1] == len))
3966 self->num_marks--;
3967 else {
3968 len--;
3969 Py_DECREF(self->stack->data[len]);
3970 self->stack->length=len;
3973 return 0;
3977 static int
3978 load_pop_mark(Unpicklerobject *self)
3980 int i;
3982 if ((i = marker(self)) < 0)
3983 return -1;
3985 Pdata_clear(self->stack, i);
3987 return 0;
3991 static int
3992 load_dup(Unpicklerobject *self)
3994 PyObject *last;
3995 int len;
3997 if ((len = self->stack->length) <= 0) return stackUnderflow();
3998 last=self->stack->data[len-1];
3999 Py_INCREF(last);
4000 PDATA_PUSH(self->stack, last, -1);
4001 return 0;
4005 static int
4006 load_get(Unpicklerobject *self)
4008 PyObject *py_str = 0, *value = 0;
4009 int len;
4010 char *s;
4011 int rc;
4013 if ((len = self->readline_func(self, &s)) < 0) return -1;
4014 if (len < 2) return bad_readline();
4016 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
4018 value = PyDict_GetItem(self->memo, py_str);
4019 if (! value) {
4020 PyErr_SetObject(BadPickleGet, py_str);
4021 rc = -1;
4023 else {
4024 PDATA_APPEND(self->stack, value, -1);
4025 rc = 0;
4028 Py_DECREF(py_str);
4029 return rc;
4033 static int
4034 load_binget(Unpicklerobject *self)
4036 PyObject *py_key = 0, *value = 0;
4037 unsigned char key;
4038 char *s;
4039 int rc;
4041 if (self->read_func(self, &s, 1) < 0) return -1;
4043 key = (unsigned char)s[0];
4044 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4046 value = PyDict_GetItem(self->memo, py_key);
4047 if (! value) {
4048 PyErr_SetObject(BadPickleGet, py_key);
4049 rc = -1;
4051 else {
4052 PDATA_APPEND(self->stack, value, -1);
4053 rc = 0;
4056 Py_DECREF(py_key);
4057 return rc;
4061 static int
4062 load_long_binget(Unpicklerobject *self)
4064 PyObject *py_key = 0, *value = 0;
4065 unsigned char c;
4066 char *s;
4067 long key;
4068 int rc;
4070 if (self->read_func(self, &s, 4) < 0) return -1;
4072 c = (unsigned char)s[0];
4073 key = (long)c;
4074 c = (unsigned char)s[1];
4075 key |= (long)c << 8;
4076 c = (unsigned char)s[2];
4077 key |= (long)c << 16;
4078 c = (unsigned char)s[3];
4079 key |= (long)c << 24;
4081 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4083 value = PyDict_GetItem(self->memo, py_key);
4084 if (! value) {
4085 PyErr_SetObject(BadPickleGet, py_key);
4086 rc = -1;
4088 else {
4089 PDATA_APPEND(self->stack, value, -1);
4090 rc = 0;
4093 Py_DECREF(py_key);
4094 return rc;
4097 /* Push an object from the extension registry (EXT[124]). nbytes is
4098 * the number of bytes following the opcode, holding the index (code) value.
4100 static int
4101 load_extension(Unpicklerobject *self, int nbytes)
4103 char *codebytes; /* the nbytes bytes after the opcode */
4104 long code; /* calc_binint returns long */
4105 PyObject *py_code; /* code as a Python int */
4106 PyObject *obj; /* the object to push */
4107 PyObject *pair; /* (module_name, class_name) */
4108 PyObject *module_name, *class_name;
4110 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4111 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4112 code = calc_binint(codebytes, nbytes);
4113 if (code <= 0) { /* note that 0 is forbidden */
4114 /* Corrupt or hostile pickle. */
4115 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4116 return -1;
4119 /* Look for the code in the cache. */
4120 py_code = PyInt_FromLong(code);
4121 if (py_code == NULL) return -1;
4122 obj = PyDict_GetItem(extension_cache, py_code);
4123 if (obj != NULL) {
4124 /* Bingo. */
4125 Py_DECREF(py_code);
4126 PDATA_APPEND(self->stack, obj, -1);
4127 return 0;
4130 /* Look up the (module_name, class_name) pair. */
4131 pair = PyDict_GetItem(inverted_registry, py_code);
4132 if (pair == NULL) {
4133 Py_DECREF(py_code);
4134 PyErr_Format(PyExc_ValueError, "unregistered extension "
4135 "code %ld", code);
4136 return -1;
4138 /* Since the extension registry is manipulable via Python code,
4139 * confirm that pair is really a 2-tuple of strings.
4141 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4142 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4143 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4144 Py_DECREF(py_code);
4145 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4146 "isn't a 2-tuple of strings", code);
4147 return -1;
4149 /* Load the object. */
4150 obj = find_class(module_name, class_name, self->find_class);
4151 if (obj == NULL) {
4152 Py_DECREF(py_code);
4153 return -1;
4155 /* Cache code -> obj. */
4156 code = PyDict_SetItem(extension_cache, py_code, obj);
4157 Py_DECREF(py_code);
4158 if (code < 0) {
4159 Py_DECREF(obj);
4160 return -1;
4162 PDATA_PUSH(self->stack, obj, -1);
4163 return 0;
4166 static int
4167 load_put(Unpicklerobject *self)
4169 PyObject *py_str = 0, *value = 0;
4170 int len, l;
4171 char *s;
4173 if ((l = self->readline_func(self, &s)) < 0) return -1;
4174 if (l < 2) return bad_readline();
4175 if (!( len=self->stack->length )) return stackUnderflow();
4176 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4177 value=self->stack->data[len-1];
4178 l=PyDict_SetItem(self->memo, py_str, value);
4179 Py_DECREF(py_str);
4180 return l;
4184 static int
4185 load_binput(Unpicklerobject *self)
4187 PyObject *py_key = 0, *value = 0;
4188 unsigned char key;
4189 char *s;
4190 int len;
4192 if (self->read_func(self, &s, 1) < 0) return -1;
4193 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4195 key = (unsigned char)s[0];
4197 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4198 value=self->stack->data[len-1];
4199 len=PyDict_SetItem(self->memo, py_key, value);
4200 Py_DECREF(py_key);
4201 return len;
4205 static int
4206 load_long_binput(Unpicklerobject *self)
4208 PyObject *py_key = 0, *value = 0;
4209 long key;
4210 unsigned char c;
4211 char *s;
4212 int len;
4214 if (self->read_func(self, &s, 4) < 0) return -1;
4215 if (!( len=self->stack->length )) return stackUnderflow();
4217 c = (unsigned char)s[0];
4218 key = (long)c;
4219 c = (unsigned char)s[1];
4220 key |= (long)c << 8;
4221 c = (unsigned char)s[2];
4222 key |= (long)c << 16;
4223 c = (unsigned char)s[3];
4224 key |= (long)c << 24;
4226 if (!( py_key = PyInt_FromLong(key))) return -1;
4227 value=self->stack->data[len-1];
4228 len=PyDict_SetItem(self->memo, py_key, value);
4229 Py_DECREF(py_key);
4230 return len;
4234 static int
4235 do_append(Unpicklerobject *self, int x)
4237 PyObject *value = 0, *list = 0, *append_method = 0;
4238 int len, i;
4240 len=self->stack->length;
4241 if (!( len >= x && x > 0 )) return stackUnderflow();
4242 /* nothing to do */
4243 if (len==x) return 0;
4245 list=self->stack->data[x-1];
4247 if (PyList_Check(list)) {
4248 PyObject *slice;
4249 int list_len;
4251 slice=Pdata_popList(self->stack, x);
4252 if (! slice) return -1;
4253 list_len = PyList_GET_SIZE(list);
4254 i=PyList_SetSlice(list, list_len, list_len, slice);
4255 Py_DECREF(slice);
4256 return i;
4258 else {
4260 if (!( append_method = PyObject_GetAttr(list, append_str)))
4261 return -1;
4263 for (i = x; i < len; i++) {
4264 PyObject *junk;
4266 value=self->stack->data[i];
4267 junk=0;
4268 ARG_TUP(self, value);
4269 if (self->arg) {
4270 junk = PyObject_Call(append_method, self->arg,
4271 NULL);
4272 FREE_ARG_TUP(self);
4274 if (! junk) {
4275 Pdata_clear(self->stack, i+1);
4276 self->stack->length=x;
4277 Py_DECREF(append_method);
4278 return -1;
4280 Py_DECREF(junk);
4282 self->stack->length=x;
4283 Py_DECREF(append_method);
4286 return 0;
4290 static int
4291 load_append(Unpicklerobject *self)
4293 return do_append(self, self->stack->length - 1);
4297 static int
4298 load_appends(Unpicklerobject *self)
4300 return do_append(self, marker(self));
4304 static int
4305 do_setitems(Unpicklerobject *self, int x)
4307 PyObject *value = 0, *key = 0, *dict = 0;
4308 int len, i, r=0;
4310 if (!( (len=self->stack->length) >= x
4311 && x > 0 )) return stackUnderflow();
4313 dict=self->stack->data[x-1];
4315 for (i = x+1; i < len; i += 2) {
4316 key =self->stack->data[i-1];
4317 value=self->stack->data[i ];
4318 if (PyObject_SetItem(dict, key, value) < 0) {
4319 r=-1;
4320 break;
4324 Pdata_clear(self->stack, x);
4326 return r;
4330 static int
4331 load_setitem(Unpicklerobject *self)
4333 return do_setitems(self, self->stack->length - 2);
4336 static int
4337 load_setitems(Unpicklerobject *self)
4339 return do_setitems(self, marker(self));
4343 static int
4344 load_build(Unpicklerobject *self)
4346 PyObject *state, *inst, *slotstate;
4347 PyObject *__setstate__;
4348 PyObject *d_key, *d_value;
4349 Py_ssize_t i;
4350 int res = -1;
4352 /* Stack is ... instance, state. We want to leave instance at
4353 * the stack top, possibly mutated via instance.__setstate__(state).
4355 if (self->stack->length < 2)
4356 return stackUnderflow();
4357 PDATA_POP(self->stack, state);
4358 if (state == NULL)
4359 return -1;
4360 inst = self->stack->data[self->stack->length - 1];
4362 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4363 if (__setstate__ != NULL) {
4364 PyObject *junk = NULL;
4366 /* The explicit __setstate__ is responsible for everything. */
4367 ARG_TUP(self, state);
4368 if (self->arg) {
4369 junk = PyObject_Call(__setstate__, self->arg, NULL);
4370 FREE_ARG_TUP(self);
4372 Py_DECREF(__setstate__);
4373 if (junk == NULL)
4374 return -1;
4375 Py_DECREF(junk);
4376 return 0;
4378 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4379 return -1;
4380 PyErr_Clear();
4382 /* A default __setstate__. First see whether state embeds a
4383 * slot state dict too (a proto 2 addition).
4385 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4386 PyObject *temp = state;
4387 state = PyTuple_GET_ITEM(temp, 0);
4388 slotstate = PyTuple_GET_ITEM(temp, 1);
4389 Py_INCREF(state);
4390 Py_INCREF(slotstate);
4391 Py_DECREF(temp);
4393 else
4394 slotstate = NULL;
4396 /* Set inst.__dict__ from the state dict (if any). */
4397 if (state != Py_None) {
4398 PyObject *dict;
4399 if (! PyDict_Check(state)) {
4400 PyErr_SetString(UnpicklingError, "state is not a "
4401 "dictionary");
4402 goto finally;
4404 dict = PyObject_GetAttr(inst, __dict___str);
4405 if (dict == NULL)
4406 goto finally;
4408 i = 0;
4409 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4410 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4411 goto finally;
4413 Py_DECREF(dict);
4416 /* Also set instance attributes from the slotstate dict (if any). */
4417 if (slotstate != NULL) {
4418 if (! PyDict_Check(slotstate)) {
4419 PyErr_SetString(UnpicklingError, "slot state is not "
4420 "a dictionary");
4421 goto finally;
4423 i = 0;
4424 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4425 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4426 goto finally;
4429 res = 0;
4431 finally:
4432 Py_DECREF(state);
4433 Py_XDECREF(slotstate);
4434 return res;
4438 static int
4439 load_mark(Unpicklerobject *self)
4441 int s;
4443 /* Note that we split the (pickle.py) stack into two stacks, an
4444 object stack and a mark stack. Here we push a mark onto the
4445 mark stack.
4448 if ((self->num_marks + 1) >= self->marks_size) {
4449 int *marks;
4450 s=self->marks_size+20;
4451 if (s <= self->num_marks) s=self->num_marks + 1;
4452 if (self->marks == NULL)
4453 marks=(int *)malloc(s * sizeof(int));
4454 else
4455 marks=(int *)realloc(self->marks,
4456 s * sizeof(int));
4457 if (!marks) {
4458 PyErr_NoMemory();
4459 return -1;
4461 self->marks = marks;
4462 self->marks_size = s;
4465 self->marks[self->num_marks++] = self->stack->length;
4467 return 0;
4470 static int
4471 load_reduce(Unpicklerobject *self)
4473 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4475 PDATA_POP(self->stack, arg_tup);
4476 if (! arg_tup) return -1;
4477 PDATA_POP(self->stack, callable);
4478 if (callable) {
4479 ob = Instance_New(callable, arg_tup);
4480 Py_DECREF(callable);
4482 Py_DECREF(arg_tup);
4484 if (! ob) return -1;
4486 PDATA_PUSH(self->stack, ob, -1);
4487 return 0;
4490 /* Just raises an error if we don't know the protocol specified. PROTO
4491 * is the first opcode for protocols >= 2.
4493 static int
4494 load_proto(Unpicklerobject *self)
4496 int i;
4497 char *protobyte;
4499 i = self->read_func(self, &protobyte, 1);
4500 if (i < 0)
4501 return -1;
4503 i = calc_binint(protobyte, 1);
4504 /* No point checking for < 0, since calc_binint returns an unsigned
4505 * int when chewing on 1 byte.
4507 assert(i >= 0);
4508 if (i <= HIGHEST_PROTOCOL)
4509 return 0;
4511 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4512 return -1;
4515 static PyObject *
4516 load(Unpicklerobject *self)
4518 PyObject *err = 0, *val = 0;
4519 char *s;
4521 self->num_marks = 0;
4522 if (self->stack->length) Pdata_clear(self->stack, 0);
4524 while (1) {
4525 if (self->read_func(self, &s, 1) < 0)
4526 break;
4528 switch (s[0]) {
4529 case NONE:
4530 if (load_none(self) < 0)
4531 break;
4532 continue;
4534 case BININT:
4535 if (load_binint(self) < 0)
4536 break;
4537 continue;
4539 case BININT1:
4540 if (load_binint1(self) < 0)
4541 break;
4542 continue;
4544 case BININT2:
4545 if (load_binint2(self) < 0)
4546 break;
4547 continue;
4549 case INT:
4550 if (load_int(self) < 0)
4551 break;
4552 continue;
4554 case LONG:
4555 if (load_long(self) < 0)
4556 break;
4557 continue;
4559 case LONG1:
4560 if (load_counted_long(self, 1) < 0)
4561 break;
4562 continue;
4564 case LONG4:
4565 if (load_counted_long(self, 4) < 0)
4566 break;
4567 continue;
4569 case FLOAT:
4570 if (load_float(self) < 0)
4571 break;
4572 continue;
4574 case BINFLOAT:
4575 if (load_binfloat(self) < 0)
4576 break;
4577 continue;
4579 case BINSTRING:
4580 if (load_binstring(self) < 0)
4581 break;
4582 continue;
4584 case SHORT_BINSTRING:
4585 if (load_short_binstring(self) < 0)
4586 break;
4587 continue;
4589 case STRING:
4590 if (load_string(self) < 0)
4591 break;
4592 continue;
4594 #ifdef Py_USING_UNICODE
4595 case UNICODE:
4596 if (load_unicode(self) < 0)
4597 break;
4598 continue;
4600 case BINUNICODE:
4601 if (load_binunicode(self) < 0)
4602 break;
4603 continue;
4604 #endif
4606 case EMPTY_TUPLE:
4607 if (load_counted_tuple(self, 0) < 0)
4608 break;
4609 continue;
4611 case TUPLE1:
4612 if (load_counted_tuple(self, 1) < 0)
4613 break;
4614 continue;
4616 case TUPLE2:
4617 if (load_counted_tuple(self, 2) < 0)
4618 break;
4619 continue;
4621 case TUPLE3:
4622 if (load_counted_tuple(self, 3) < 0)
4623 break;
4624 continue;
4626 case TUPLE:
4627 if (load_tuple(self) < 0)
4628 break;
4629 continue;
4631 case EMPTY_LIST:
4632 if (load_empty_list(self) < 0)
4633 break;
4634 continue;
4636 case LIST:
4637 if (load_list(self) < 0)
4638 break;
4639 continue;
4641 case EMPTY_DICT:
4642 if (load_empty_dict(self) < 0)
4643 break;
4644 continue;
4646 case DICT:
4647 if (load_dict(self) < 0)
4648 break;
4649 continue;
4651 case OBJ:
4652 if (load_obj(self) < 0)
4653 break;
4654 continue;
4656 case INST:
4657 if (load_inst(self) < 0)
4658 break;
4659 continue;
4661 case NEWOBJ:
4662 if (load_newobj(self) < 0)
4663 break;
4664 continue;
4666 case GLOBAL:
4667 if (load_global(self) < 0)
4668 break;
4669 continue;
4671 case APPEND:
4672 if (load_append(self) < 0)
4673 break;
4674 continue;
4676 case APPENDS:
4677 if (load_appends(self) < 0)
4678 break;
4679 continue;
4681 case BUILD:
4682 if (load_build(self) < 0)
4683 break;
4684 continue;
4686 case DUP:
4687 if (load_dup(self) < 0)
4688 break;
4689 continue;
4691 case BINGET:
4692 if (load_binget(self) < 0)
4693 break;
4694 continue;
4696 case LONG_BINGET:
4697 if (load_long_binget(self) < 0)
4698 break;
4699 continue;
4701 case GET:
4702 if (load_get(self) < 0)
4703 break;
4704 continue;
4706 case EXT1:
4707 if (load_extension(self, 1) < 0)
4708 break;
4709 continue;
4711 case EXT2:
4712 if (load_extension(self, 2) < 0)
4713 break;
4714 continue;
4716 case EXT4:
4717 if (load_extension(self, 4) < 0)
4718 break;
4719 continue;
4720 case MARK:
4721 if (load_mark(self) < 0)
4722 break;
4723 continue;
4725 case BINPUT:
4726 if (load_binput(self) < 0)
4727 break;
4728 continue;
4730 case LONG_BINPUT:
4731 if (load_long_binput(self) < 0)
4732 break;
4733 continue;
4735 case PUT:
4736 if (load_put(self) < 0)
4737 break;
4738 continue;
4740 case POP:
4741 if (load_pop(self) < 0)
4742 break;
4743 continue;
4745 case POP_MARK:
4746 if (load_pop_mark(self) < 0)
4747 break;
4748 continue;
4750 case SETITEM:
4751 if (load_setitem(self) < 0)
4752 break;
4753 continue;
4755 case SETITEMS:
4756 if (load_setitems(self) < 0)
4757 break;
4758 continue;
4760 case STOP:
4761 break;
4763 case PERSID:
4764 if (load_persid(self) < 0)
4765 break;
4766 continue;
4768 case BINPERSID:
4769 if (load_binpersid(self) < 0)
4770 break;
4771 continue;
4773 case REDUCE:
4774 if (load_reduce(self) < 0)
4775 break;
4776 continue;
4778 case PROTO:
4779 if (load_proto(self) < 0)
4780 break;
4781 continue;
4783 case NEWTRUE:
4784 if (load_bool(self, Py_True) < 0)
4785 break;
4786 continue;
4788 case NEWFALSE:
4789 if (load_bool(self, Py_False) < 0)
4790 break;
4791 continue;
4793 case '\0':
4794 /* end of file */
4795 PyErr_SetNone(PyExc_EOFError);
4796 break;
4798 default:
4799 cPickle_ErrFormat(UnpicklingError,
4800 "invalid load key, '%s'.",
4801 "c", s[0]);
4802 return NULL;
4805 break;
4808 if ((err = PyErr_Occurred())) {
4809 if (err == PyExc_EOFError) {
4810 PyErr_SetNone(PyExc_EOFError);
4812 return NULL;
4815 PDATA_POP(self->stack, val);
4816 return val;
4820 /* No-load functions to support noload, which is used to
4821 find persistent references. */
4823 static int
4824 noload_obj(Unpicklerobject *self)
4826 int i;
4828 if ((i = marker(self)) < 0) return -1;
4829 return Pdata_clear(self->stack, i+1);
4833 static int
4834 noload_inst(Unpicklerobject *self)
4836 int i;
4837 char *s;
4839 if ((i = marker(self)) < 0) return -1;
4840 Pdata_clear(self->stack, i);
4841 if (self->readline_func(self, &s) < 0) return -1;
4842 if (self->readline_func(self, &s) < 0) return -1;
4843 PDATA_APPEND(self->stack, Py_None, -1);
4844 return 0;
4847 static int
4848 noload_newobj(Unpicklerobject *self)
4850 PyObject *obj;
4852 PDATA_POP(self->stack, obj); /* pop argtuple */
4853 if (obj == NULL) return -1;
4854 Py_DECREF(obj);
4856 PDATA_POP(self->stack, obj); /* pop cls */
4857 if (obj == NULL) return -1;
4858 Py_DECREF(obj);
4860 PDATA_APPEND(self->stack, Py_None, -1);
4861 return 0;
4864 static int
4865 noload_global(Unpicklerobject *self)
4867 char *s;
4869 if (self->readline_func(self, &s) < 0) return -1;
4870 if (self->readline_func(self, &s) < 0) return -1;
4871 PDATA_APPEND(self->stack, Py_None,-1);
4872 return 0;
4875 static int
4876 noload_reduce(Unpicklerobject *self)
4879 if (self->stack->length < 2) return stackUnderflow();
4880 Pdata_clear(self->stack, self->stack->length-2);
4881 PDATA_APPEND(self->stack, Py_None,-1);
4882 return 0;
4885 static int
4886 noload_build(Unpicklerobject *self) {
4888 if (self->stack->length < 1) return stackUnderflow();
4889 Pdata_clear(self->stack, self->stack->length-1);
4890 return 0;
4893 static int
4894 noload_extension(Unpicklerobject *self, int nbytes)
4896 char *codebytes;
4898 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4899 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4900 PDATA_APPEND(self->stack, Py_None, -1);
4901 return 0;
4905 static PyObject *
4906 noload(Unpicklerobject *self)
4908 PyObject *err = 0, *val = 0;
4909 char *s;
4911 self->num_marks = 0;
4912 Pdata_clear(self->stack, 0);
4914 while (1) {
4915 if (self->read_func(self, &s, 1) < 0)
4916 break;
4918 switch (s[0]) {
4919 case NONE:
4920 if (load_none(self) < 0)
4921 break;
4922 continue;
4924 case BININT:
4925 if (load_binint(self) < 0)
4926 break;
4927 continue;
4929 case BININT1:
4930 if (load_binint1(self) < 0)
4931 break;
4932 continue;
4934 case BININT2:
4935 if (load_binint2(self) < 0)
4936 break;
4937 continue;
4939 case INT:
4940 if (load_int(self) < 0)
4941 break;
4942 continue;
4944 case LONG:
4945 if (load_long(self) < 0)
4946 break;
4947 continue;
4949 case LONG1:
4950 if (load_counted_long(self, 1) < 0)
4951 break;
4952 continue;
4954 case LONG4:
4955 if (load_counted_long(self, 4) < 0)
4956 break;
4957 continue;
4959 case FLOAT:
4960 if (load_float(self) < 0)
4961 break;
4962 continue;
4964 case BINFLOAT:
4965 if (load_binfloat(self) < 0)
4966 break;
4967 continue;
4969 case BINSTRING:
4970 if (load_binstring(self) < 0)
4971 break;
4972 continue;
4974 case SHORT_BINSTRING:
4975 if (load_short_binstring(self) < 0)
4976 break;
4977 continue;
4979 case STRING:
4980 if (load_string(self) < 0)
4981 break;
4982 continue;
4984 #ifdef Py_USING_UNICODE
4985 case UNICODE:
4986 if (load_unicode(self) < 0)
4987 break;
4988 continue;
4990 case BINUNICODE:
4991 if (load_binunicode(self) < 0)
4992 break;
4993 continue;
4994 #endif
4996 case EMPTY_TUPLE:
4997 if (load_counted_tuple(self, 0) < 0)
4998 break;
4999 continue;
5001 case TUPLE1:
5002 if (load_counted_tuple(self, 1) < 0)
5003 break;
5004 continue;
5006 case TUPLE2:
5007 if (load_counted_tuple(self, 2) < 0)
5008 break;
5009 continue;
5011 case TUPLE3:
5012 if (load_counted_tuple(self, 3) < 0)
5013 break;
5014 continue;
5016 case TUPLE:
5017 if (load_tuple(self) < 0)
5018 break;
5019 continue;
5021 case EMPTY_LIST:
5022 if (load_empty_list(self) < 0)
5023 break;
5024 continue;
5026 case LIST:
5027 if (load_list(self) < 0)
5028 break;
5029 continue;
5031 case EMPTY_DICT:
5032 if (load_empty_dict(self) < 0)
5033 break;
5034 continue;
5036 case DICT:
5037 if (load_dict(self) < 0)
5038 break;
5039 continue;
5041 case OBJ:
5042 if (noload_obj(self) < 0)
5043 break;
5044 continue;
5046 case INST:
5047 if (noload_inst(self) < 0)
5048 break;
5049 continue;
5051 case NEWOBJ:
5052 if (noload_newobj(self) < 0)
5053 break;
5054 continue;
5056 case GLOBAL:
5057 if (noload_global(self) < 0)
5058 break;
5059 continue;
5061 case APPEND:
5062 if (load_append(self) < 0)
5063 break;
5064 continue;
5066 case APPENDS:
5067 if (load_appends(self) < 0)
5068 break;
5069 continue;
5071 case BUILD:
5072 if (noload_build(self) < 0)
5073 break;
5074 continue;
5076 case DUP:
5077 if (load_dup(self) < 0)
5078 break;
5079 continue;
5081 case BINGET:
5082 if (load_binget(self) < 0)
5083 break;
5084 continue;
5086 case LONG_BINGET:
5087 if (load_long_binget(self) < 0)
5088 break;
5089 continue;
5091 case GET:
5092 if (load_get(self) < 0)
5093 break;
5094 continue;
5096 case EXT1:
5097 if (noload_extension(self, 1) < 0)
5098 break;
5099 continue;
5101 case EXT2:
5102 if (noload_extension(self, 2) < 0)
5103 break;
5104 continue;
5106 case EXT4:
5107 if (noload_extension(self, 4) < 0)
5108 break;
5109 continue;
5111 case MARK:
5112 if (load_mark(self) < 0)
5113 break;
5114 continue;
5116 case BINPUT:
5117 if (load_binput(self) < 0)
5118 break;
5119 continue;
5121 case LONG_BINPUT:
5122 if (load_long_binput(self) < 0)
5123 break;
5124 continue;
5126 case PUT:
5127 if (load_put(self) < 0)
5128 break;
5129 continue;
5131 case POP:
5132 if (load_pop(self) < 0)
5133 break;
5134 continue;
5136 case POP_MARK:
5137 if (load_pop_mark(self) < 0)
5138 break;
5139 continue;
5141 case SETITEM:
5142 if (load_setitem(self) < 0)
5143 break;
5144 continue;
5146 case SETITEMS:
5147 if (load_setitems(self) < 0)
5148 break;
5149 continue;
5151 case STOP:
5152 break;
5154 case PERSID:
5155 if (load_persid(self) < 0)
5156 break;
5157 continue;
5159 case BINPERSID:
5160 if (load_binpersid(self) < 0)
5161 break;
5162 continue;
5164 case REDUCE:
5165 if (noload_reduce(self) < 0)
5166 break;
5167 continue;
5169 case PROTO:
5170 if (load_proto(self) < 0)
5171 break;
5172 continue;
5174 case NEWTRUE:
5175 if (load_bool(self, Py_True) < 0)
5176 break;
5177 continue;
5179 case NEWFALSE:
5180 if (load_bool(self, Py_False) < 0)
5181 break;
5182 continue;
5183 default:
5184 cPickle_ErrFormat(UnpicklingError,
5185 "invalid load key, '%s'.",
5186 "c", s[0]);
5187 return NULL;
5190 break;
5193 if ((err = PyErr_Occurred())) {
5194 if (err == PyExc_EOFError) {
5195 PyErr_SetNone(PyExc_EOFError);
5197 return NULL;
5200 PDATA_POP(self->stack, val);
5201 return val;
5205 static PyObject *
5206 Unpickler_load(Unpicklerobject *self, PyObject *unused)
5208 return load(self);
5211 static PyObject *
5212 Unpickler_noload(Unpicklerobject *self, PyObject *unused)
5214 return noload(self);
5218 static struct PyMethodDef Unpickler_methods[] = {
5219 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
5220 PyDoc_STR("load() -- Load a pickle")
5222 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
5223 PyDoc_STR(
5224 "noload() -- not load a pickle, but go through most of the motions\n"
5225 "\n"
5226 "This function can be used to read past a pickle without instantiating\n"
5227 "any objects or importing any modules. It can also be used to find all\n"
5228 "persistent references without instantiating any objects or importing\n"
5229 "any modules.\n")
5231 {NULL, NULL} /* sentinel */
5235 static Unpicklerobject *
5236 newUnpicklerobject(PyObject *f)
5238 Unpicklerobject *self;
5240 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5241 return NULL;
5243 self->file = NULL;
5244 self->arg = NULL;
5245 self->stack = (Pdata*)Pdata_New();
5246 self->pers_func = NULL;
5247 self->last_string = NULL;
5248 self->marks = NULL;
5249 self->num_marks = 0;
5250 self->marks_size = 0;
5251 self->buf_size = 0;
5252 self->read = NULL;
5253 self->readline = NULL;
5254 self->find_class = NULL;
5256 if (!( self->memo = PyDict_New()))
5257 goto err;
5259 if (!self->stack)
5260 goto err;
5262 Py_INCREF(f);
5263 self->file = f;
5265 /* Set read, readline based on type of f */
5266 if (PyFile_Check(f)) {
5267 self->fp = PyFile_AsFile(f);
5268 if (self->fp == NULL) {
5269 PyErr_SetString(PyExc_ValueError,
5270 "I/O operation on closed file");
5271 goto err;
5273 self->read_func = read_file;
5274 self->readline_func = readline_file;
5276 else if (PycStringIO_InputCheck(f)) {
5277 self->fp = NULL;
5278 self->read_func = read_cStringIO;
5279 self->readline_func = readline_cStringIO;
5281 else {
5283 self->fp = NULL;
5284 self->read_func = read_other;
5285 self->readline_func = readline_other;
5287 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5288 (self->read = PyObject_GetAttr(f, read_str)))) {
5289 PyErr_Clear();
5290 PyErr_SetString( PyExc_TypeError,
5291 "argument must have 'read' and "
5292 "'readline' attributes" );
5293 goto err;
5296 PyObject_GC_Track(self);
5298 return self;
5300 err:
5301 Py_DECREF((PyObject *)self);
5302 return NULL;
5306 static PyObject *
5307 get_Unpickler(PyObject *self, PyObject *file)
5309 return (PyObject *)newUnpicklerobject(file);
5313 static void
5314 Unpickler_dealloc(Unpicklerobject *self)
5316 PyObject_GC_UnTrack((PyObject *)self);
5317 Py_XDECREF(self->readline);
5318 Py_XDECREF(self->read);
5319 Py_XDECREF(self->file);
5320 Py_XDECREF(self->memo);
5321 Py_XDECREF(self->stack);
5322 Py_XDECREF(self->pers_func);
5323 Py_XDECREF(self->arg);
5324 Py_XDECREF(self->last_string);
5325 Py_XDECREF(self->find_class);
5327 if (self->marks) {
5328 free(self->marks);
5331 if (self->buf_size) {
5332 free(self->buf);
5335 Py_TYPE(self)->tp_free((PyObject *)self);
5338 static int
5339 Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5341 Py_VISIT(self->readline);
5342 Py_VISIT(self->read);
5343 Py_VISIT(self->file);
5344 Py_VISIT(self->memo);
5345 Py_VISIT(self->stack);
5346 Py_VISIT(self->pers_func);
5347 Py_VISIT(self->arg);
5348 Py_VISIT(self->last_string);
5349 Py_VISIT(self->find_class);
5350 return 0;
5353 static int
5354 Unpickler_clear(Unpicklerobject *self)
5356 Py_CLEAR(self->readline);
5357 Py_CLEAR(self->read);
5358 Py_CLEAR(self->file);
5359 Py_CLEAR(self->memo);
5360 Py_CLEAR(self->stack);
5361 Py_CLEAR(self->pers_func);
5362 Py_CLEAR(self->arg);
5363 Py_CLEAR(self->last_string);
5364 Py_CLEAR(self->find_class);
5365 return 0;
5368 static PyObject *
5369 Unpickler_getattr(Unpicklerobject *self, char *name)
5371 if (!strcmp(name, "persistent_load")) {
5372 if (!self->pers_func) {
5373 PyErr_SetString(PyExc_AttributeError, name);
5374 return NULL;
5377 Py_INCREF(self->pers_func);
5378 return self->pers_func;
5381 if (!strcmp(name, "find_global")) {
5382 if (!self->find_class) {
5383 PyErr_SetString(PyExc_AttributeError, name);
5384 return NULL;
5387 Py_INCREF(self->find_class);
5388 return self->find_class;
5391 if (!strcmp(name, "memo")) {
5392 if (!self->memo) {
5393 PyErr_SetString(PyExc_AttributeError, name);
5394 return NULL;
5397 Py_INCREF(self->memo);
5398 return self->memo;
5401 if (!strcmp(name, "UnpicklingError")) {
5402 Py_INCREF(UnpicklingError);
5403 return UnpicklingError;
5406 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5410 static int
5411 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5414 if (!strcmp(name, "persistent_load")) {
5415 Py_XDECREF(self->pers_func);
5416 self->pers_func = value;
5417 Py_XINCREF(value);
5418 return 0;
5421 if (!strcmp(name, "find_global")) {
5422 Py_XDECREF(self->find_class);
5423 self->find_class = value;
5424 Py_XINCREF(value);
5425 return 0;
5428 if (! value) {
5429 PyErr_SetString(PyExc_TypeError,
5430 "attribute deletion is not supported");
5431 return -1;
5434 if (strcmp(name, "memo") == 0) {
5435 if (!PyDict_Check(value)) {
5436 PyErr_SetString(PyExc_TypeError,
5437 "memo must be a dictionary");
5438 return -1;
5440 Py_XDECREF(self->memo);
5441 self->memo = value;
5442 Py_INCREF(value);
5443 return 0;
5446 PyErr_SetString(PyExc_AttributeError, name);
5447 return -1;
5450 /* ---------------------------------------------------------------------------
5451 * Module-level functions.
5454 /* dump(obj, file, protocol=0). */
5455 static PyObject *
5456 cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5458 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5459 PyObject *ob, *file, *res = NULL;
5460 Picklerobject *pickler = 0;
5461 int proto = 0;
5463 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5464 &ob, &file, &proto)))
5465 goto finally;
5467 if (!( pickler = newPicklerobject(file, proto)))
5468 goto finally;
5470 if (dump(pickler, ob) < 0)
5471 goto finally;
5473 Py_INCREF(Py_None);
5474 res = Py_None;
5476 finally:
5477 Py_XDECREF(pickler);
5479 return res;
5483 /* dumps(obj, protocol=0). */
5484 static PyObject *
5485 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5487 static char *kwlist[] = {"obj", "protocol", NULL};
5488 PyObject *ob, *file = 0, *res = NULL;
5489 Picklerobject *pickler = 0;
5490 int proto = 0;
5492 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5493 &ob, &proto)))
5494 goto finally;
5496 if (!( file = PycStringIO->NewOutput(128)))
5497 goto finally;
5499 if (!( pickler = newPicklerobject(file, proto)))
5500 goto finally;
5502 if (dump(pickler, ob) < 0)
5503 goto finally;
5505 res = PycStringIO->cgetvalue(file);
5507 finally:
5508 Py_XDECREF(pickler);
5509 Py_XDECREF(file);
5511 return res;
5515 /* load(fileobj). */
5516 static PyObject *
5517 cpm_load(PyObject *self, PyObject *ob)
5519 Unpicklerobject *unpickler = 0;
5520 PyObject *res = NULL;
5522 if (!( unpickler = newUnpicklerobject(ob)))
5523 goto finally;
5525 res = load(unpickler);
5527 finally:
5528 Py_XDECREF(unpickler);
5530 return res;
5534 /* loads(string) */
5535 static PyObject *
5536 cpm_loads(PyObject *self, PyObject *args)
5538 PyObject *ob, *file = 0, *res = NULL;
5539 Unpicklerobject *unpickler = 0;
5541 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5542 goto finally;
5544 if (!( file = PycStringIO->NewInput(ob)))
5545 goto finally;
5547 if (!( unpickler = newUnpicklerobject(file)))
5548 goto finally;
5550 res = load(unpickler);
5552 finally:
5553 Py_XDECREF(file);
5554 Py_XDECREF(unpickler);
5556 return res;
5560 PyDoc_STRVAR(Unpicklertype__doc__,
5561 "Objects that know how to unpickle");
5563 static PyTypeObject Unpicklertype = {
5564 PyVarObject_HEAD_INIT(NULL, 0)
5565 "cPickle.Unpickler", /*tp_name*/
5566 sizeof(Unpicklerobject), /*tp_basicsize*/
5568 (destructor)Unpickler_dealloc, /* tp_dealloc */
5569 0, /* tp_print */
5570 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5571 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5572 0, /* tp_compare */
5573 0, /* tp_repr */
5574 0, /* tp_as_number */
5575 0, /* tp_as_sequence */
5576 0, /* tp_as_mapping */
5577 0, /* tp_hash */
5578 0, /* tp_call */
5579 0, /* tp_str */
5580 0, /* tp_getattro */
5581 0, /* tp_setattro */
5582 0, /* tp_as_buffer */
5583 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5584 Unpicklertype__doc__, /* tp_doc */
5585 (traverseproc)Unpickler_traverse, /* tp_traverse */
5586 (inquiry)Unpickler_clear, /* tp_clear */
5589 static struct PyMethodDef cPickle_methods[] = {
5590 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5591 PyDoc_STR("dump(obj, file, protocol=0) -- "
5592 "Write an object in pickle format to the given file.\n"
5593 "\n"
5594 "See the Pickler docstring for the meaning of optional argument proto.")
5597 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5598 PyDoc_STR("dumps(obj, protocol=0) -- "
5599 "Return a string containing an object in pickle format.\n"
5600 "\n"
5601 "See the Pickler docstring for the meaning of optional argument proto.")
5604 {"load", (PyCFunction)cpm_load, METH_O,
5605 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5607 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
5608 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5610 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5611 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5612 "\n"
5613 "This takes a file-like object for writing a pickle data stream.\n"
5614 "The optional proto argument tells the pickler to use the given\n"
5615 "protocol; supported protocols are 0, 1, 2. The default\n"
5616 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5617 "only protocol that can be written to a file opened in text\n"
5618 "mode and read back successfully. When using a protocol higher\n"
5619 "than 0, make sure the file is opened in binary mode, both when\n"
5620 "pickling and unpickling.)\n"
5621 "\n"
5622 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5623 "more efficient than protocol 1.\n"
5624 "\n"
5625 "Specifying a negative protocol version selects the highest\n"
5626 "protocol version supported. The higher the protocol used, the\n"
5627 "more recent the version of Python needed to read the pickle\n"
5628 "produced.\n"
5629 "\n"
5630 "The file parameter must have a write() method that accepts a single\n"
5631 "string argument. It can thus be an open file object, a StringIO\n"
5632 "object, or any other custom object that meets this interface.\n")
5635 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
5636 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5638 { NULL, NULL }
5641 static int
5642 init_stuff(PyObject *module_dict)
5644 PyObject *copyreg, *t, *r;
5646 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5648 if (PyType_Ready(&Unpicklertype) < 0)
5649 return -1;
5650 if (PyType_Ready(&Picklertype) < 0)
5651 return -1;
5653 INIT_STR(__class__);
5654 INIT_STR(__getinitargs__);
5655 INIT_STR(__dict__);
5656 INIT_STR(__getstate__);
5657 INIT_STR(__setstate__);
5658 INIT_STR(__name__);
5659 INIT_STR(__main__);
5660 INIT_STR(__reduce__);
5661 INIT_STR(__reduce_ex__);
5662 INIT_STR(write);
5663 INIT_STR(append);
5664 INIT_STR(read);
5665 INIT_STR(readline);
5666 INIT_STR(copyreg);
5667 INIT_STR(dispatch_table);
5669 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5670 return -1;
5672 /* This is special because we want to use a different
5673 one in restricted mode. */
5674 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5675 if (!dispatch_table) return -1;
5677 extension_registry = PyObject_GetAttrString(copyreg,
5678 "_extension_registry");
5679 if (!extension_registry) return -1;
5681 inverted_registry = PyObject_GetAttrString(copyreg,
5682 "_inverted_registry");
5683 if (!inverted_registry) return -1;
5685 extension_cache = PyObject_GetAttrString(copyreg,
5686 "_extension_cache");
5687 if (!extension_cache) return -1;
5689 Py_DECREF(copyreg);
5691 if (!(empty_tuple = PyTuple_New(0)))
5692 return -1;
5694 two_tuple = PyTuple_New(2);
5695 if (two_tuple == NULL)
5696 return -1;
5697 /* We use this temp container with no regard to refcounts, or to
5698 * keeping containees alive. Exempt from GC, because we don't
5699 * want anything looking at two_tuple() by magic.
5701 PyObject_GC_UnTrack(two_tuple);
5703 /* Ugh */
5704 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5705 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5706 return -1;
5708 if (!( t=PyDict_New())) return -1;
5709 if (!( r=PyRun_String(
5710 "def __str__(self):\n"
5711 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5712 Py_file_input,
5713 module_dict, t) )) return -1;
5714 Py_DECREF(r);
5716 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5717 if (!PickleError)
5718 return -1;
5720 Py_DECREF(t);
5722 PicklingError = PyErr_NewException("cPickle.PicklingError",
5723 PickleError, NULL);
5724 if (!PicklingError)
5725 return -1;
5727 if (!( t=PyDict_New())) return -1;
5728 if (!( r=PyRun_String(
5729 "def __str__(self):\n"
5730 " a=self.args\n"
5731 " a=a and type(a[0]) or '(what)'\n"
5732 " return 'Cannot pickle %s objects' % a\n"
5733 , Py_file_input,
5734 module_dict, t) )) return -1;
5735 Py_DECREF(r);
5737 if (!( UnpickleableError = PyErr_NewException(
5738 "cPickle.UnpickleableError", PicklingError, t)))
5739 return -1;
5741 Py_DECREF(t);
5743 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5744 PickleError, NULL)))
5745 return -1;
5747 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5748 UnpicklingError, NULL)))
5749 return -1;
5751 if (PyDict_SetItemString(module_dict, "PickleError",
5752 PickleError) < 0)
5753 return -1;
5755 if (PyDict_SetItemString(module_dict, "PicklingError",
5756 PicklingError) < 0)
5757 return -1;
5759 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5760 UnpicklingError) < 0)
5761 return -1;
5763 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5764 UnpickleableError) < 0)
5765 return -1;
5767 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5768 BadPickleGet) < 0)
5769 return -1;
5771 PycString_IMPORT;
5773 return 0;
5776 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5777 #define PyMODINIT_FUNC void
5778 #endif
5779 PyMODINIT_FUNC
5780 initcPickle(void)
5782 PyObject *m, *d, *di, *v, *k;
5783 Py_ssize_t i;
5784 char *rev = "1.71"; /* XXX when does this change? */
5785 PyObject *format_version;
5786 PyObject *compatible_formats;
5788 /* XXX: Should mention that the pickle module will include the C
5789 XXX: optimized implementation automatically. */
5790 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5791 "Python 3.0", 2) < 0)
5792 return;
5794 Py_TYPE(&Picklertype) = &PyType_Type;
5795 Py_TYPE(&Unpicklertype) = &PyType_Type;
5796 Py_TYPE(&PdataType) = &PyType_Type;
5798 /* Initialize some pieces. We need to do this before module creation,
5799 * so we're forced to use a temporary dictionary. :(
5801 di = PyDict_New();
5802 if (!di) return;
5803 if (init_stuff(di) < 0) return;
5805 /* Create the module and add the functions */
5806 m = Py_InitModule4("cPickle", cPickle_methods,
5807 cPickle_module_documentation,
5808 (PyObject*)NULL,PYTHON_API_VERSION);
5809 if (m == NULL)
5810 return;
5812 /* Add some symbolic constants to the module */
5813 d = PyModule_GetDict(m);
5814 v = PyString_FromString(rev);
5815 PyDict_SetItemString(d, "__version__", v);
5816 Py_XDECREF(v);
5818 /* Copy data from di. Waaa. */
5819 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5820 if (PyObject_SetItem(d, k, v) < 0) {
5821 Py_DECREF(di);
5822 return;
5825 Py_DECREF(di);
5827 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5828 if (i < 0)
5829 return;
5831 /* These are purely informational; no code uses them. */
5832 /* File format version we write. */
5833 format_version = PyString_FromString("2.0");
5834 /* Format versions we can read. */
5835 compatible_formats = Py_BuildValue("[sssss]",
5836 "1.0", /* Original protocol 0 */
5837 "1.1", /* Protocol 0 + INST */
5838 "1.2", /* Original protocol 1 */
5839 "1.3", /* Protocol 1 + BINFLOAT */
5840 "2.0"); /* Original protocol 2 */
5841 PyDict_SetItemString(d, "format_version", format_version);
5842 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5843 Py_XDECREF(format_version);
5844 Py_XDECREF(compatible_formats);