Convert from long to Py_ssize_t.
[python.git] / Modules / cPickle.c
blob7f4ad7eb870d0ebe4e1611402c75efb32d4defcb
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;
1519 PyObject *slice[BATCHSIZE];
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 next group of (no more than) BATCHSIZE elements. */
1549 for (n = 0; n < BATCHSIZE; ++n) {
1550 obj = PyIter_Next(iter);
1551 if (obj == NULL) {
1552 if (PyErr_Occurred())
1553 goto BatchFailed;
1554 break;
1556 slice[n] = obj;
1559 if (n > 1) {
1560 /* Pump out MARK, slice[0:n], APPENDS. */
1561 if (self->write_func(self, &MARKv, 1) < 0)
1562 goto BatchFailed;
1563 for (i = 0; i < n; ++i) {
1564 if (save(self, slice[i], 0) < 0)
1565 goto BatchFailed;
1567 if (self->write_func(self, &appends, 1) < 0)
1568 goto BatchFailed;
1570 else if (n == 1) {
1571 if (save(self, slice[0], 0) < 0)
1572 goto BatchFailed;
1573 if (self->write_func(self, &append, 1) < 0)
1574 goto BatchFailed;
1577 for (i = 0; i < n; ++i) {
1578 Py_DECREF(slice[i]);
1580 } while (n == BATCHSIZE);
1581 return 0;
1583 BatchFailed:
1584 while (--n >= 0) {
1585 Py_DECREF(slice[n]);
1587 return -1;
1590 static int
1591 save_list(Picklerobject *self, PyObject *args)
1593 int res = -1;
1594 char s[3];
1595 int len;
1596 PyObject *iter;
1598 if (self->fast && !fast_save_enter(self, args))
1599 goto finally;
1601 /* Create an empty list. */
1602 if (self->bin) {
1603 s[0] = EMPTY_LIST;
1604 len = 1;
1606 else {
1607 s[0] = MARK;
1608 s[1] = LIST;
1609 len = 2;
1612 if (self->write_func(self, s, len) < 0)
1613 goto finally;
1615 /* Get list length, and bow out early if empty. */
1616 if ((len = PyList_Size(args)) < 0)
1617 goto finally;
1619 /* Memoize. */
1620 if (len == 0) {
1621 if (put(self, args) >= 0)
1622 res = 0;
1623 goto finally;
1625 if (put2(self, args) < 0)
1626 goto finally;
1628 /* Materialize the list elements. */
1629 iter = PyObject_GetIter(args);
1630 if (iter == NULL)
1631 goto finally;
1633 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1635 res = batch_list(self, iter);
1636 Py_LeaveRecursiveCall();
1638 Py_DECREF(iter);
1640 finally:
1641 if (self->fast && !fast_save_leave(self, args))
1642 res = -1;
1644 return res;
1648 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1649 * MARK key value ... key value SETITEMS
1650 * opcode sequences. Calling code should have arranged to first create an
1651 * empty dict, or dict-like object, for the SETITEMS to operate on.
1652 * Returns 0 on success, <0 on error.
1654 * This is very much like batch_list(). The difference between saving
1655 * elements directly, and picking apart two-tuples, is so long-winded at
1656 * the C level, though, that attempts to combine these routines were too
1657 * ugly to bear.
1659 static int
1660 batch_dict(Picklerobject *self, PyObject *iter)
1662 PyObject *p;
1663 PyObject *slice[BATCHSIZE];
1664 int i, n;
1666 static char setitem = SETITEM;
1667 static char setitems = SETITEMS;
1669 assert(iter != NULL);
1671 if (self->proto == 0) {
1672 /* SETITEMS isn't available; do one at a time. */
1673 for (;;) {
1674 p = PyIter_Next(iter);
1675 if (p == NULL) {
1676 if (PyErr_Occurred())
1677 return -1;
1678 break;
1680 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1681 PyErr_SetString(PyExc_TypeError, "dict items "
1682 "iterator must return 2-tuples");
1683 return -1;
1685 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1686 if (i >= 0)
1687 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1688 Py_DECREF(p);
1689 if (i < 0)
1690 return -1;
1691 if (self->write_func(self, &setitem, 1) < 0)
1692 return -1;
1694 return 0;
1697 /* proto > 0: write in batches of BATCHSIZE. */
1698 do {
1699 /* Get next group of (no more than) BATCHSIZE elements. */
1700 for (n = 0; n < BATCHSIZE; ++n) {
1701 p = PyIter_Next(iter);
1702 if (p == NULL) {
1703 if (PyErr_Occurred())
1704 goto BatchFailed;
1705 break;
1707 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1708 PyErr_SetString(PyExc_TypeError, "dict items "
1709 "iterator must return 2-tuples");
1710 goto BatchFailed;
1712 slice[n] = p;
1715 if (n > 1) {
1716 /* Pump out MARK, slice[0:n], SETITEMS. */
1717 if (self->write_func(self, &MARKv, 1) < 0)
1718 goto BatchFailed;
1719 for (i = 0; i < n; ++i) {
1720 p = slice[i];
1721 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1722 goto BatchFailed;
1723 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1724 goto BatchFailed;
1726 if (self->write_func(self, &setitems, 1) < 0)
1727 goto BatchFailed;
1729 else if (n == 1) {
1730 p = slice[0];
1731 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1732 goto BatchFailed;
1733 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1734 goto BatchFailed;
1735 if (self->write_func(self, &setitem, 1) < 0)
1736 goto BatchFailed;
1739 for (i = 0; i < n; ++i) {
1740 Py_DECREF(slice[i]);
1742 } while (n == BATCHSIZE);
1743 return 0;
1745 BatchFailed:
1746 while (--n >= 0) {
1747 Py_DECREF(slice[n]);
1749 return -1;
1752 static int
1753 save_dict(Picklerobject *self, PyObject *args)
1755 int res = -1;
1756 char s[3];
1757 int len;
1758 PyObject *iter;
1760 if (self->fast && !fast_save_enter(self, args))
1761 goto finally;
1763 /* Create an empty dict. */
1764 if (self->bin) {
1765 s[0] = EMPTY_DICT;
1766 len = 1;
1768 else {
1769 s[0] = MARK;
1770 s[1] = DICT;
1771 len = 2;
1774 if (self->write_func(self, s, len) < 0)
1775 goto finally;
1777 /* Get dict size, and bow out early if empty. */
1778 if ((len = PyDict_Size(args)) < 0)
1779 goto finally;
1781 if (len == 0) {
1782 if (put(self, args) >= 0)
1783 res = 0;
1784 goto finally;
1786 if (put2(self, args) < 0)
1787 goto finally;
1789 /* Materialize the dict items. */
1790 iter = PyObject_CallMethod(args, "iteritems", "()");
1791 if (iter == NULL)
1792 goto finally;
1793 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1795 res = batch_dict(self, iter);
1796 Py_LeaveRecursiveCall();
1798 Py_DECREF(iter);
1800 finally:
1801 if (self->fast && !fast_save_leave(self, args))
1802 res = -1;
1804 return res;
1808 static int
1809 save_inst(Picklerobject *self, PyObject *args)
1811 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1812 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1813 char *module_str, *name_str;
1814 int module_size, name_size, res = -1;
1816 static char inst = INST, obj = OBJ, build = BUILD;
1818 if (self->fast && !fast_save_enter(self, args))
1819 goto finally;
1821 if (self->write_func(self, &MARKv, 1) < 0)
1822 goto finally;
1824 if (!( class = PyObject_GetAttr(args, __class___str)))
1825 goto finally;
1827 if (self->bin) {
1828 if (save(self, class, 0) < 0)
1829 goto finally;
1832 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1833 PyObject *element = 0;
1834 int i, len;
1836 if (!( class_args =
1837 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
1838 goto finally;
1840 if ((len = PyObject_Size(class_args)) < 0)
1841 goto finally;
1843 for (i = 0; i < len; i++) {
1844 if (!( element = PySequence_GetItem(class_args, i)))
1845 goto finally;
1847 if (save(self, element, 0) < 0) {
1848 Py_DECREF(element);
1849 goto finally;
1852 Py_DECREF(element);
1855 else {
1856 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1857 PyErr_Clear();
1858 else
1859 goto finally;
1862 if (!self->bin) {
1863 if (!( name = ((PyClassObject *)class)->cl_name )) {
1864 PyErr_SetString(PicklingError, "class has no name");
1865 goto finally;
1868 if (!( module = whichmodule(class, name)))
1869 goto finally;
1872 if ((module_size = PyString_Size(module)) < 0 ||
1873 (name_size = PyString_Size(name)) < 0)
1874 goto finally;
1876 module_str = PyString_AS_STRING((PyStringObject *)module);
1877 name_str = PyString_AS_STRING((PyStringObject *)name);
1879 if (self->write_func(self, &inst, 1) < 0)
1880 goto finally;
1882 if (self->write_func(self, module_str, module_size) < 0)
1883 goto finally;
1885 if (self->write_func(self, "\n", 1) < 0)
1886 goto finally;
1888 if (self->write_func(self, name_str, name_size) < 0)
1889 goto finally;
1891 if (self->write_func(self, "\n", 1) < 0)
1892 goto finally;
1894 else if (self->write_func(self, &obj, 1) < 0) {
1895 goto finally;
1898 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1899 state = PyObject_Call(getstate_func, empty_tuple, NULL);
1900 if (!state)
1901 goto finally;
1903 else {
1904 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1905 PyErr_Clear();
1906 else
1907 goto finally;
1909 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1910 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1911 PyErr_Clear();
1912 else
1913 goto finally;
1914 res = 0;
1915 goto finally;
1919 if (!PyDict_Check(state)) {
1920 if (put2(self, args) < 0)
1921 goto finally;
1923 else {
1924 if (put(self, args) < 0)
1925 goto finally;
1928 if (save(self, state, 0) < 0)
1929 goto finally;
1931 if (self->write_func(self, &build, 1) < 0)
1932 goto finally;
1934 res = 0;
1936 finally:
1937 if (self->fast && !fast_save_leave(self, args))
1938 res = -1;
1940 Py_XDECREF(module);
1941 Py_XDECREF(class);
1942 Py_XDECREF(state);
1943 Py_XDECREF(getinitargs_func);
1944 Py_XDECREF(getstate_func);
1945 Py_XDECREF(class_args);
1947 return res;
1951 static int
1952 save_global(Picklerobject *self, PyObject *args, PyObject *name)
1954 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
1955 char *name_str, *module_str;
1956 int module_size, name_size, res = -1;
1958 static char global = GLOBAL;
1960 if (name) {
1961 global_name = name;
1962 Py_INCREF(global_name);
1964 else {
1965 if (!( global_name = PyObject_GetAttr(args, __name___str)))
1966 goto finally;
1969 if (!( module = whichmodule(args, global_name)))
1970 goto finally;
1972 if ((module_size = PyString_Size(module)) < 0 ||
1973 (name_size = PyString_Size(global_name)) < 0)
1974 goto finally;
1976 module_str = PyString_AS_STRING((PyStringObject *)module);
1977 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1979 /* XXX This can be doing a relative import. Clearly it shouldn't,
1980 but I don't know how to stop it. :-( */
1981 mod = PyImport_ImportModule(module_str);
1982 if (mod == NULL) {
1983 cPickle_ErrFormat(PicklingError,
1984 "Can't pickle %s: import of module %s "
1985 "failed",
1986 "OS", args, module);
1987 goto finally;
1989 klass = PyObject_GetAttrString(mod, name_str);
1990 if (klass == NULL) {
1991 cPickle_ErrFormat(PicklingError,
1992 "Can't pickle %s: attribute lookup %s.%s "
1993 "failed",
1994 "OSS", args, module, global_name);
1995 goto finally;
1997 if (klass != args) {
1998 Py_DECREF(klass);
1999 cPickle_ErrFormat(PicklingError,
2000 "Can't pickle %s: it's not the same object "
2001 "as %s.%s",
2002 "OSS", args, module, global_name);
2003 goto finally;
2005 Py_DECREF(klass);
2007 if (self->proto >= 2) {
2008 /* See whether this is in the extension registry, and if
2009 * so generate an EXT opcode.
2011 PyObject *py_code; /* extension code as Python object */
2012 long code; /* extension code as C value */
2013 char c_str[5];
2014 int n;
2016 PyTuple_SET_ITEM(two_tuple, 0, module);
2017 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2018 py_code = PyDict_GetItem(extension_registry, two_tuple);
2019 if (py_code == NULL)
2020 goto gen_global; /* not registered */
2022 /* Verify py_code has the right type and value. */
2023 if (!PyInt_Check(py_code)) {
2024 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2025 "extension code %s isn't an integer",
2026 "OO", args, py_code);
2027 goto finally;
2029 code = PyInt_AS_LONG(py_code);
2030 if (code <= 0 || code > 0x7fffffffL) {
2031 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2032 "extension code %ld is out of range",
2033 "Ol", args, code);
2034 goto finally;
2037 /* Generate an EXT opcode. */
2038 if (code <= 0xff) {
2039 c_str[0] = EXT1;
2040 c_str[1] = (char)code;
2041 n = 2;
2043 else if (code <= 0xffff) {
2044 c_str[0] = EXT2;
2045 c_str[1] = (char)(code & 0xff);
2046 c_str[2] = (char)((code >> 8) & 0xff);
2047 n = 3;
2049 else {
2050 c_str[0] = EXT4;
2051 c_str[1] = (char)(code & 0xff);
2052 c_str[2] = (char)((code >> 8) & 0xff);
2053 c_str[3] = (char)((code >> 16) & 0xff);
2054 c_str[4] = (char)((code >> 24) & 0xff);
2055 n = 5;
2058 if (self->write_func(self, c_str, n) >= 0)
2059 res = 0;
2060 goto finally; /* and don't memoize */
2063 gen_global:
2064 if (self->write_func(self, &global, 1) < 0)
2065 goto finally;
2067 if (self->write_func(self, module_str, module_size) < 0)
2068 goto finally;
2070 if (self->write_func(self, "\n", 1) < 0)
2071 goto finally;
2073 if (self->write_func(self, name_str, name_size) < 0)
2074 goto finally;
2076 if (self->write_func(self, "\n", 1) < 0)
2077 goto finally;
2079 if (put(self, args) < 0)
2080 goto finally;
2082 res = 0;
2084 finally:
2085 Py_XDECREF(module);
2086 Py_XDECREF(global_name);
2087 Py_XDECREF(mod);
2089 return res;
2092 static int
2093 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2095 PyObject *pid = 0;
2096 int size, res = -1;
2098 static char persid = PERSID, binpersid = BINPERSID;
2100 Py_INCREF(args);
2101 ARG_TUP(self, args);
2102 if (self->arg) {
2103 pid = PyObject_Call(f, self->arg, NULL);
2104 FREE_ARG_TUP(self);
2106 if (! pid) return -1;
2108 if (pid != Py_None) {
2109 if (!self->bin) {
2110 if (!PyString_Check(pid)) {
2111 PyErr_SetString(PicklingError,
2112 "persistent id must be string");
2113 goto finally;
2116 if (self->write_func(self, &persid, 1) < 0)
2117 goto finally;
2119 if ((size = PyString_Size(pid)) < 0)
2120 goto finally;
2122 if (self->write_func(self,
2123 PyString_AS_STRING(
2124 (PyStringObject *)pid),
2125 size) < 0)
2126 goto finally;
2128 if (self->write_func(self, "\n", 1) < 0)
2129 goto finally;
2131 res = 1;
2132 goto finally;
2134 else if (save(self, pid, 1) >= 0) {
2135 if (self->write_func(self, &binpersid, 1) < 0)
2136 res = -1;
2137 else
2138 res = 1;
2141 goto finally;
2144 res = 0;
2146 finally:
2147 Py_XDECREF(pid);
2149 return res;
2152 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2153 * appropriate __reduce__ method for ob.
2155 static int
2156 save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
2158 PyObject *callable;
2159 PyObject *argtup;
2160 PyObject *state = NULL;
2161 PyObject *listitems = NULL;
2162 PyObject *dictitems = NULL;
2164 int use_newobj = self->proto >= 2;
2166 static char reduce = REDUCE;
2167 static char build = BUILD;
2168 static char newobj = NEWOBJ;
2170 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2171 &callable,
2172 &argtup,
2173 &state,
2174 &listitems,
2175 &dictitems))
2176 return -1;
2178 if (!PyTuple_Check(argtup)) {
2179 PyErr_SetString(PicklingError,
2180 "args from reduce() should be a tuple");
2181 return -1;
2184 if (state == Py_None)
2185 state = NULL;
2186 if (listitems == Py_None)
2187 listitems = NULL;
2188 if (dictitems == Py_None)
2189 dictitems = NULL;
2191 /* Protocol 2 special case: if callable's name is __newobj__, use
2192 * NEWOBJ. This consumes a lot of code.
2194 if (use_newobj) {
2195 PyObject *temp = PyObject_GetAttr(callable, __name___str);
2197 if (temp == NULL) {
2198 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2199 PyErr_Clear();
2200 else
2201 return -1;
2202 use_newobj = 0;
2204 else {
2205 use_newobj = PyString_Check(temp) &&
2206 strcmp(PyString_AS_STRING(temp),
2207 "__newobj__") == 0;
2208 Py_DECREF(temp);
2211 if (use_newobj) {
2212 PyObject *cls;
2213 PyObject *newargtup;
2214 int n, i;
2216 /* Sanity checks. */
2217 n = PyTuple_Size(argtup);
2218 if (n < 1) {
2219 PyErr_SetString(PicklingError, "__newobj__ arglist "
2220 "is empty");
2221 return -1;
2224 cls = PyTuple_GET_ITEM(argtup, 0);
2225 if (! PyObject_HasAttrString(cls, "__new__")) {
2226 PyErr_SetString(PicklingError, "args[0] from "
2227 "__newobj__ args has no __new__");
2228 return -1;
2231 /* XXX How could ob be NULL? */
2232 if (ob != NULL) {
2233 PyObject *ob_dot_class;
2235 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2236 if (ob_dot_class == NULL) {
2237 if (PyErr_ExceptionMatches(
2238 PyExc_AttributeError))
2239 PyErr_Clear();
2240 else
2241 return -1;
2243 i = ob_dot_class != cls; /* true iff a problem */
2244 Py_XDECREF(ob_dot_class);
2245 if (i) {
2246 PyErr_SetString(PicklingError, "args[0] from "
2247 "__newobj__ args has the wrong class");
2248 return -1;
2252 /* Save the class and its __new__ arguments. */
2253 if (save(self, cls, 0) < 0)
2254 return -1;
2256 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2257 if (newargtup == NULL)
2258 return -1;
2259 for (i = 1; i < n; ++i) {
2260 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2261 Py_INCREF(temp);
2262 PyTuple_SET_ITEM(newargtup, i-1, temp);
2264 i = save(self, newargtup, 0);
2265 Py_DECREF(newargtup);
2266 if (i < 0)
2267 return -1;
2269 /* Add NEWOBJ opcode. */
2270 if (self->write_func(self, &newobj, 1) < 0)
2271 return -1;
2273 else {
2274 /* Not using NEWOBJ. */
2275 if (save(self, callable, 0) < 0 ||
2276 save(self, argtup, 0) < 0 ||
2277 self->write_func(self, &reduce, 1) < 0)
2278 return -1;
2281 /* Memoize. */
2282 /* XXX How can ob be NULL? */
2283 if (ob != NULL) {
2284 if (state && !PyDict_Check(state)) {
2285 if (put2(self, ob) < 0)
2286 return -1;
2288 else if (put(self, ob) < 0)
2289 return -1;
2293 if (listitems && batch_list(self, listitems) < 0)
2294 return -1;
2296 if (dictitems && batch_dict(self, dictitems) < 0)
2297 return -1;
2299 if (state) {
2300 if (save(self, state, 0) < 0 ||
2301 self->write_func(self, &build, 1) < 0)
2302 return -1;
2305 return 0;
2308 static int
2309 save(Picklerobject *self, PyObject *args, int pers_save)
2311 PyTypeObject *type;
2312 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2313 PyObject *arg_tup;
2314 int res = -1;
2315 int tmp, size;
2317 if (Py_EnterRecursiveCall(" while pickling an object"))
2318 return -1;
2320 if (!pers_save && self->pers_func) {
2321 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2322 res = tmp;
2323 goto finally;
2327 if (args == Py_None) {
2328 res = save_none(self, args);
2329 goto finally;
2332 type = Py_TYPE(args);
2334 switch (type->tp_name[0]) {
2335 case 'b':
2336 if (args == Py_False || args == Py_True) {
2337 res = save_bool(self, args);
2338 goto finally;
2340 break;
2341 case 'i':
2342 if (type == &PyInt_Type) {
2343 res = save_int(self, args);
2344 goto finally;
2346 break;
2348 case 'l':
2349 if (type == &PyLong_Type) {
2350 res = save_long(self, args);
2351 goto finally;
2353 break;
2355 case 'f':
2356 if (type == &PyFloat_Type) {
2357 res = save_float(self, args);
2358 goto finally;
2360 break;
2362 case 't':
2363 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2364 res = save_tuple(self, args);
2365 goto finally;
2367 break;
2369 case 's':
2370 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2371 res = save_string(self, args, 0);
2372 goto finally;
2374 break;
2376 #ifdef Py_USING_UNICODE
2377 case 'u':
2378 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2379 res = save_unicode(self, args, 0);
2380 goto finally;
2382 break;
2383 #endif
2386 if (Py_REFCNT(args) > 1) {
2387 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2388 goto finally;
2390 if (PyDict_GetItem(self->memo, py_ob_id)) {
2391 if (get(self, py_ob_id) < 0)
2392 goto finally;
2394 res = 0;
2395 goto finally;
2399 switch (type->tp_name[0]) {
2400 case 's':
2401 if (type == &PyString_Type) {
2402 res = save_string(self, args, 1);
2403 goto finally;
2405 break;
2407 #ifdef Py_USING_UNICODE
2408 case 'u':
2409 if (type == &PyUnicode_Type) {
2410 res = save_unicode(self, args, 1);
2411 goto finally;
2413 break;
2414 #endif
2416 case 't':
2417 if (type == &PyTuple_Type) {
2418 res = save_tuple(self, args);
2419 goto finally;
2421 if (type == &PyType_Type) {
2422 res = save_global(self, args, NULL);
2423 goto finally;
2425 break;
2427 case 'l':
2428 if (type == &PyList_Type) {
2429 res = save_list(self, args);
2430 goto finally;
2432 break;
2434 case 'd':
2435 if (type == &PyDict_Type) {
2436 res = save_dict(self, args);
2437 goto finally;
2439 break;
2441 case 'i':
2442 if (type == &PyInstance_Type) {
2443 res = save_inst(self, args);
2444 goto finally;
2446 break;
2448 case 'c':
2449 if (type == &PyClass_Type) {
2450 res = save_global(self, args, NULL);
2451 goto finally;
2453 break;
2455 case 'f':
2456 if (type == &PyFunction_Type) {
2457 res = save_global(self, args, NULL);
2458 if (res && PyErr_ExceptionMatches(PickleError)) {
2459 /* fall back to reduce */
2460 PyErr_Clear();
2461 break;
2463 goto finally;
2465 break;
2467 case 'b':
2468 if (type == &PyCFunction_Type) {
2469 res = save_global(self, args, NULL);
2470 goto finally;
2474 if (!pers_save && self->inst_pers_func) {
2475 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2476 res = tmp;
2477 goto finally;
2481 if (PyType_IsSubtype(type, &PyType_Type)) {
2482 res = save_global(self, args, NULL);
2483 goto finally;
2486 /* Get a reduction callable, and call it. This may come from
2487 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2488 * or the object's __reduce__ method.
2490 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2491 if (__reduce__ != NULL) {
2492 Py_INCREF(__reduce__);
2493 Py_INCREF(args);
2494 ARG_TUP(self, args);
2495 if (self->arg) {
2496 t = PyObject_Call(__reduce__, self->arg, NULL);
2497 FREE_ARG_TUP(self);
2500 else {
2501 /* Check for a __reduce_ex__ method. */
2502 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2503 if (__reduce__ != NULL) {
2504 t = PyInt_FromLong(self->proto);
2505 if (t != NULL) {
2506 ARG_TUP(self, t);
2507 t = NULL;
2508 if (self->arg) {
2509 t = PyObject_Call(__reduce__,
2510 self->arg, NULL);
2511 FREE_ARG_TUP(self);
2515 else {
2516 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2517 PyErr_Clear();
2518 else
2519 goto finally;
2520 /* Check for a __reduce__ method. */
2521 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2522 if (__reduce__ != NULL) {
2523 t = PyObject_Call(__reduce__,
2524 empty_tuple, NULL);
2526 else {
2527 PyErr_SetObject(UnpickleableError, args);
2528 goto finally;
2533 if (t == NULL)
2534 goto finally;
2536 if (PyString_Check(t)) {
2537 res = save_global(self, args, t);
2538 goto finally;
2541 if (! PyTuple_Check(t)) {
2542 cPickle_ErrFormat(PicklingError, "Value returned by "
2543 "%s must be string or tuple",
2544 "O", __reduce__);
2545 goto finally;
2548 size = PyTuple_Size(t);
2549 if (size < 2 || size > 5) {
2550 cPickle_ErrFormat(PicklingError, "tuple returned by "
2551 "%s must contain 2 through 5 elements",
2552 "O", __reduce__);
2553 goto finally;
2556 arg_tup = PyTuple_GET_ITEM(t, 1);
2557 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2558 cPickle_ErrFormat(PicklingError, "Second element of "
2559 "tuple returned by %s must be a tuple",
2560 "O", __reduce__);
2561 goto finally;
2564 res = save_reduce(self, t, args);
2566 finally:
2567 Py_LeaveRecursiveCall();
2568 Py_XDECREF(py_ob_id);
2569 Py_XDECREF(__reduce__);
2570 Py_XDECREF(t);
2572 return res;
2576 static int
2577 dump(Picklerobject *self, PyObject *args)
2579 static char stop = STOP;
2581 if (self->proto >= 2) {
2582 char bytes[2];
2584 bytes[0] = PROTO;
2585 assert(self->proto >= 0 && self->proto < 256);
2586 bytes[1] = (char)self->proto;
2587 if (self->write_func(self, bytes, 2) < 0)
2588 return -1;
2591 if (save(self, args, 0) < 0)
2592 return -1;
2594 if (self->write_func(self, &stop, 1) < 0)
2595 return -1;
2597 if (self->write_func(self, NULL, 0) < 0)
2598 return -1;
2600 return 0;
2603 static PyObject *
2604 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2606 if (self->memo)
2607 PyDict_Clear(self->memo);
2608 Py_INCREF(Py_None);
2609 return Py_None;
2612 static PyObject *
2613 Pickle_getvalue(Picklerobject *self, PyObject *args)
2615 int l, i, rsize, ssize, clear=1, lm;
2616 long ik;
2617 PyObject *k, *r;
2618 char *s, *p, *have_get;
2619 Pdata *data;
2621 /* Can be called by Python code or C code */
2622 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2623 return NULL;
2625 /* Check to make sure we are based on a list */
2626 if (! Pdata_Check(self->file)) {
2627 PyErr_SetString(PicklingError,
2628 "Attempt to getvalue() a non-list-based pickler");
2629 return NULL;
2632 /* flush write buffer */
2633 if (write_other(self, NULL, 0) < 0) return NULL;
2635 data=(Pdata*)self->file;
2636 l=data->length;
2638 /* set up an array to hold get/put status */
2639 lm = PyDict_Size(self->memo);
2640 if (lm < 0) return NULL;
2641 lm++;
2642 have_get = malloc(lm);
2643 if (have_get == NULL) return PyErr_NoMemory();
2644 memset(have_get, 0, lm);
2646 /* Scan for gets. */
2647 for (rsize = 0, i = l; --i >= 0; ) {
2648 k = data->data[i];
2650 if (PyString_Check(k))
2651 rsize += PyString_GET_SIZE(k);
2653 else if (PyInt_Check(k)) { /* put */
2654 ik = PyInt_AS_LONG((PyIntObject*)k);
2655 if (ik >= lm || ik == 0) {
2656 PyErr_SetString(PicklingError,
2657 "Invalid get data");
2658 goto err;
2660 if (have_get[ik]) /* with matching get */
2661 rsize += ik < 256 ? 2 : 5;
2664 else if (! (PyTuple_Check(k) &&
2665 PyTuple_GET_SIZE(k) == 2 &&
2666 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2668 PyErr_SetString(PicklingError,
2669 "Unexpected data in internal list");
2670 goto err;
2673 else { /* put */
2674 ik = PyInt_AS_LONG((PyIntObject *)k);
2675 if (ik >= lm || ik == 0) {
2676 PyErr_SetString(PicklingError,
2677 "Invalid get data");
2678 return NULL;
2680 have_get[ik] = 1;
2681 rsize += ik < 256 ? 2 : 5;
2685 /* Now generate the result */
2686 r = PyString_FromStringAndSize(NULL, rsize);
2687 if (r == NULL) goto err;
2688 s = PyString_AS_STRING((PyStringObject *)r);
2690 for (i = 0; i < l; i++) {
2691 k = data->data[i];
2693 if (PyString_Check(k)) {
2694 ssize = PyString_GET_SIZE(k);
2695 if (ssize) {
2696 p=PyString_AS_STRING((PyStringObject *)k);
2697 while (--ssize >= 0)
2698 *s++ = *p++;
2702 else if (PyTuple_Check(k)) { /* get */
2703 ik = PyInt_AS_LONG((PyIntObject *)
2704 PyTuple_GET_ITEM(k, 0));
2705 if (ik < 256) {
2706 *s++ = BINGET;
2707 *s++ = (int)(ik & 0xff);
2709 else {
2710 *s++ = LONG_BINGET;
2711 *s++ = (int)(ik & 0xff);
2712 *s++ = (int)((ik >> 8) & 0xff);
2713 *s++ = (int)((ik >> 16) & 0xff);
2714 *s++ = (int)((ik >> 24) & 0xff);
2718 else { /* put */
2719 ik = PyInt_AS_LONG((PyIntObject*)k);
2721 if (have_get[ik]) { /* with matching get */
2722 if (ik < 256) {
2723 *s++ = BINPUT;
2724 *s++ = (int)(ik & 0xff);
2726 else {
2727 *s++ = LONG_BINPUT;
2728 *s++ = (int)(ik & 0xff);
2729 *s++ = (int)((ik >> 8) & 0xff);
2730 *s++ = (int)((ik >> 16) & 0xff);
2731 *s++ = (int)((ik >> 24) & 0xff);
2737 if (clear) {
2738 PyDict_Clear(self->memo);
2739 Pdata_clear(data, 0);
2742 free(have_get);
2743 return r;
2744 err:
2745 free(have_get);
2746 return NULL;
2749 static PyObject *
2750 Pickler_dump(Picklerobject *self, PyObject *args)
2752 PyObject *ob;
2753 int get=0;
2755 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2756 return NULL;
2758 if (dump(self, ob) < 0)
2759 return NULL;
2761 if (get) return Pickle_getvalue(self, NULL);
2763 /* XXX Why does dump() return self? */
2764 Py_INCREF(self);
2765 return (PyObject*)self;
2769 static struct PyMethodDef Pickler_methods[] =
2771 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2772 PyDoc_STR("dump(object) -- "
2773 "Write an object in pickle format to the object's pickle stream")},
2774 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2775 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2776 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2777 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2778 {NULL, NULL} /* sentinel */
2782 static Picklerobject *
2783 newPicklerobject(PyObject *file, int proto)
2785 Picklerobject *self;
2787 if (proto < 0)
2788 proto = HIGHEST_PROTOCOL;
2789 if (proto > HIGHEST_PROTOCOL) {
2790 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2791 "the highest available protocol is %d",
2792 proto, HIGHEST_PROTOCOL);
2793 return NULL;
2796 self = PyObject_GC_New(Picklerobject, &Picklertype);
2797 if (self == NULL)
2798 return NULL;
2799 self->proto = proto;
2800 self->bin = proto > 0;
2801 self->fp = NULL;
2802 self->write = NULL;
2803 self->memo = NULL;
2804 self->arg = NULL;
2805 self->pers_func = NULL;
2806 self->inst_pers_func = NULL;
2807 self->write_buf = NULL;
2808 self->fast = 0;
2809 self->fast_container = 0;
2810 self->fast_memo = NULL;
2811 self->buf_size = 0;
2812 self->dispatch_table = NULL;
2814 self->file = NULL;
2815 if (file)
2816 Py_INCREF(file);
2817 else {
2818 file = Pdata_New();
2819 if (file == NULL)
2820 goto err;
2822 self->file = file;
2824 if (!( self->memo = PyDict_New()))
2825 goto err;
2827 if (PyFile_Check(file)) {
2828 self->fp = PyFile_AsFile(file);
2829 if (self->fp == NULL) {
2830 PyErr_SetString(PyExc_ValueError,
2831 "I/O operation on closed file");
2832 goto err;
2834 self->write_func = write_file;
2836 else if (PycStringIO_OutputCheck(file)) {
2837 self->write_func = write_cStringIO;
2839 else if (file == Py_None) {
2840 self->write_func = write_none;
2842 else {
2843 self->write_func = write_other;
2845 if (! Pdata_Check(file)) {
2846 self->write = PyObject_GetAttr(file, write_str);
2847 if (!self->write) {
2848 PyErr_Clear();
2849 PyErr_SetString(PyExc_TypeError,
2850 "argument must have 'write' "
2851 "attribute");
2852 goto err;
2856 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2857 if (self->write_buf == NULL) {
2858 PyErr_NoMemory();
2859 goto err;
2863 if (PyEval_GetRestricted()) {
2864 /* Restricted execution, get private tables */
2865 PyObject *m = PyImport_Import(copyreg_str);
2867 if (m == NULL)
2868 goto err;
2869 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
2870 Py_DECREF(m);
2871 if (self->dispatch_table == NULL)
2872 goto err;
2874 else {
2875 self->dispatch_table = dispatch_table;
2876 Py_INCREF(dispatch_table);
2878 PyObject_GC_Track(self);
2880 return self;
2882 err:
2883 Py_DECREF(self);
2884 return NULL;
2888 static PyObject *
2889 get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
2891 static char *kwlist[] = {"file", "protocol", NULL};
2892 PyObject *file = NULL;
2893 int proto = 0;
2895 /* XXX
2896 * The documented signature is Pickler(file, protocol=0), but this
2897 * accepts Pickler() and Pickler(integer) too. The meaning then
2898 * is clear as mud, undocumented, and not supported by pickle.py.
2899 * I'm told Zope uses this, but I haven't traced into this code
2900 * far enough to figure out what it means.
2902 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
2903 PyErr_Clear();
2904 proto = 0;
2905 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2906 kwlist, &file, &proto))
2907 return NULL;
2909 return (PyObject *)newPicklerobject(file, proto);
2913 static void
2914 Pickler_dealloc(Picklerobject *self)
2916 PyObject_GC_UnTrack(self);
2917 Py_XDECREF(self->write);
2918 Py_XDECREF(self->memo);
2919 Py_XDECREF(self->fast_memo);
2920 Py_XDECREF(self->arg);
2921 Py_XDECREF(self->file);
2922 Py_XDECREF(self->pers_func);
2923 Py_XDECREF(self->inst_pers_func);
2924 Py_XDECREF(self->dispatch_table);
2925 PyMem_Free(self->write_buf);
2926 Py_TYPE(self)->tp_free((PyObject *)self);
2929 static int
2930 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2932 Py_VISIT(self->write);
2933 Py_VISIT(self->memo);
2934 Py_VISIT(self->fast_memo);
2935 Py_VISIT(self->arg);
2936 Py_VISIT(self->file);
2937 Py_VISIT(self->pers_func);
2938 Py_VISIT(self->inst_pers_func);
2939 Py_VISIT(self->dispatch_table);
2940 return 0;
2943 static int
2944 Pickler_clear(Picklerobject *self)
2946 Py_CLEAR(self->write);
2947 Py_CLEAR(self->memo);
2948 Py_CLEAR(self->fast_memo);
2949 Py_CLEAR(self->arg);
2950 Py_CLEAR(self->file);
2951 Py_CLEAR(self->pers_func);
2952 Py_CLEAR(self->inst_pers_func);
2953 Py_CLEAR(self->dispatch_table);
2954 return 0;
2957 static PyObject *
2958 Pickler_get_pers_func(Picklerobject *p)
2960 if (p->pers_func == NULL)
2961 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2962 else
2963 Py_INCREF(p->pers_func);
2964 return p->pers_func;
2967 static int
2968 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2970 if (v == NULL) {
2971 PyErr_SetString(PyExc_TypeError,
2972 "attribute deletion is not supported");
2973 return -1;
2975 Py_XDECREF(p->pers_func);
2976 Py_INCREF(v);
2977 p->pers_func = v;
2978 return 0;
2981 static int
2982 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2984 if (v == NULL) {
2985 PyErr_SetString(PyExc_TypeError,
2986 "attribute deletion is not supported");
2987 return -1;
2989 Py_XDECREF(p->inst_pers_func);
2990 Py_INCREF(v);
2991 p->inst_pers_func = v;
2992 return 0;
2995 static PyObject *
2996 Pickler_get_memo(Picklerobject *p)
2998 if (p->memo == NULL)
2999 PyErr_SetString(PyExc_AttributeError, "memo");
3000 else
3001 Py_INCREF(p->memo);
3002 return p->memo;
3005 static int
3006 Pickler_set_memo(Picklerobject *p, PyObject *v)
3008 if (v == NULL) {
3009 PyErr_SetString(PyExc_TypeError,
3010 "attribute deletion is not supported");
3011 return -1;
3013 if (!PyDict_Check(v)) {
3014 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3015 return -1;
3017 Py_XDECREF(p->memo);
3018 Py_INCREF(v);
3019 p->memo = v;
3020 return 0;
3023 static PyObject *
3024 Pickler_get_error(Picklerobject *p)
3026 /* why is this an attribute on the Pickler? */
3027 Py_INCREF(PicklingError);
3028 return PicklingError;
3031 static PyMemberDef Pickler_members[] = {
3032 {"binary", T_INT, offsetof(Picklerobject, bin)},
3033 {"fast", T_INT, offsetof(Picklerobject, fast)},
3034 {NULL}
3037 static PyGetSetDef Pickler_getsets[] = {
3038 {"persistent_id", (getter)Pickler_get_pers_func,
3039 (setter)Pickler_set_pers_func},
3040 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3041 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3042 {"PicklingError", (getter)Pickler_get_error, NULL},
3043 {NULL}
3046 PyDoc_STRVAR(Picklertype__doc__,
3047 "Objects that know how to pickle objects\n");
3049 static PyTypeObject Picklertype = {
3050 PyVarObject_HEAD_INIT(NULL, 0)
3051 "cPickle.Pickler", /*tp_name*/
3052 sizeof(Picklerobject), /*tp_basicsize*/
3054 (destructor)Pickler_dealloc, /* tp_dealloc */
3055 0, /* tp_print */
3056 0, /* tp_getattr */
3057 0, /* tp_setattr */
3058 0, /* tp_compare */
3059 0, /* tp_repr */
3060 0, /* tp_as_number */
3061 0, /* tp_as_sequence */
3062 0, /* tp_as_mapping */
3063 0, /* tp_hash */
3064 0, /* tp_call */
3065 0, /* tp_str */
3066 PyObject_GenericGetAttr, /* tp_getattro */
3067 PyObject_GenericSetAttr, /* tp_setattro */
3068 0, /* tp_as_buffer */
3069 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3070 Picklertype__doc__, /* tp_doc */
3071 (traverseproc)Pickler_traverse, /* tp_traverse */
3072 (inquiry)Pickler_clear, /* tp_clear */
3073 0, /* tp_richcompare */
3074 0, /* tp_weaklistoffset */
3075 0, /* tp_iter */
3076 0, /* tp_iternext */
3077 Pickler_methods, /* tp_methods */
3078 Pickler_members, /* tp_members */
3079 Pickler_getsets, /* tp_getset */
3082 static PyObject *
3083 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3085 PyObject *global = 0, *module;
3087 if (fc) {
3088 if (fc==Py_None) {
3089 PyErr_SetString(UnpicklingError, "Global and instance "
3090 "pickles are not supported.");
3091 return NULL;
3093 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3094 py_global_name, NULL);
3097 module = PySys_GetObject("modules");
3098 if (module == NULL)
3099 return NULL;
3101 module = PyDict_GetItem(module, py_module_name);
3102 if (module == NULL) {
3103 module = PyImport_Import(py_module_name);
3104 if (!module)
3105 return NULL;
3106 global = PyObject_GetAttr(module, py_global_name);
3107 Py_DECREF(module);
3109 else
3110 global = PyObject_GetAttr(module, py_global_name);
3111 return global;
3114 static int
3115 marker(Unpicklerobject *self)
3117 if (self->num_marks < 1) {
3118 PyErr_SetString(UnpicklingError, "could not find MARK");
3119 return -1;
3122 return self->marks[--self->num_marks];
3126 static int
3127 load_none(Unpicklerobject *self)
3129 PDATA_APPEND(self->stack, Py_None, -1);
3130 return 0;
3133 static int
3134 bad_readline(void)
3136 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3137 return -1;
3140 static int
3141 load_int(Unpicklerobject *self)
3143 PyObject *py_int = 0;
3144 char *endptr, *s;
3145 int len, res = -1;
3146 long l;
3148 if ((len = self->readline_func(self, &s)) < 0) return -1;
3149 if (len < 2) return bad_readline();
3150 if (!( s=pystrndup(s,len))) return -1;
3152 errno = 0;
3153 l = strtol(s, &endptr, 0);
3155 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3156 /* Hm, maybe we've got something long. Let's try reading
3157 it as a Python long object. */
3158 errno = 0;
3159 py_int = PyLong_FromString(s, NULL, 0);
3160 if (py_int == NULL) {
3161 PyErr_SetString(PyExc_ValueError,
3162 "could not convert string to int");
3163 goto finally;
3166 else {
3167 if (len == 3 && (l == 0 || l == 1)) {
3168 if (!( py_int = PyBool_FromLong(l))) goto finally;
3170 else {
3171 if (!( py_int = PyInt_FromLong(l))) goto finally;
3175 free(s);
3176 PDATA_PUSH(self->stack, py_int, -1);
3177 return 0;
3179 finally:
3180 free(s);
3182 return res;
3185 static int
3186 load_bool(Unpicklerobject *self, PyObject *boolean)
3188 assert(boolean == Py_True || boolean == Py_False);
3189 PDATA_APPEND(self->stack, boolean, -1);
3190 return 0;
3193 /* s contains x bytes of a little-endian integer. Return its value as a
3194 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3195 * int, but when x is 4 it's a signed one. This is an historical source
3196 * of x-platform bugs.
3198 static long
3199 calc_binint(char *s, int x)
3201 unsigned char c;
3202 int i;
3203 long l;
3205 for (i = 0, l = 0L; i < x; i++) {
3206 c = (unsigned char)s[i];
3207 l |= (long)c << (i * 8);
3209 #if SIZEOF_LONG > 4
3210 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3211 * is signed, so on a box with longs bigger than 4 bytes we need
3212 * to extend a BININT's sign bit to the full width.
3214 if (x == 4 && l & (1L << 31))
3215 l |= (~0L) << 32;
3216 #endif
3217 return l;
3221 static int
3222 load_binintx(Unpicklerobject *self, char *s, int x)
3224 PyObject *py_int = 0;
3225 long l;
3227 l = calc_binint(s, x);
3229 if (!( py_int = PyInt_FromLong(l)))
3230 return -1;
3232 PDATA_PUSH(self->stack, py_int, -1);
3233 return 0;
3237 static int
3238 load_binint(Unpicklerobject *self)
3240 char *s;
3242 if (self->read_func(self, &s, 4) < 0)
3243 return -1;
3245 return load_binintx(self, s, 4);
3249 static int
3250 load_binint1(Unpicklerobject *self)
3252 char *s;
3254 if (self->read_func(self, &s, 1) < 0)
3255 return -1;
3257 return load_binintx(self, s, 1);
3261 static int
3262 load_binint2(Unpicklerobject *self)
3264 char *s;
3266 if (self->read_func(self, &s, 2) < 0)
3267 return -1;
3269 return load_binintx(self, s, 2);
3272 static int
3273 load_long(Unpicklerobject *self)
3275 PyObject *l = 0;
3276 char *end, *s;
3277 int len, res = -1;
3279 if ((len = self->readline_func(self, &s)) < 0) return -1;
3280 if (len < 2) return bad_readline();
3281 if (!( s=pystrndup(s,len))) return -1;
3283 if (!( l = PyLong_FromString(s, &end, 0)))
3284 goto finally;
3286 free(s);
3287 PDATA_PUSH(self->stack, l, -1);
3288 return 0;
3290 finally:
3291 free(s);
3293 return res;
3296 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3297 * data following.
3299 static int
3300 load_counted_long(Unpicklerobject *self, int size)
3302 Py_ssize_t i;
3303 char *nbytes;
3304 unsigned char *pdata;
3305 PyObject *along;
3307 assert(size == 1 || size == 4);
3308 i = self->read_func(self, &nbytes, size);
3309 if (i < 0) return -1;
3311 size = calc_binint(nbytes, size);
3312 if (size < 0) {
3313 /* Corrupt or hostile pickle -- we never write one like
3314 * this.
3316 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3317 "byte count");
3318 return -1;
3321 if (size == 0)
3322 along = PyLong_FromLong(0L);
3323 else {
3324 /* Read the raw little-endian bytes & convert. */
3325 i = self->read_func(self, (char **)&pdata, size);
3326 if (i < 0) return -1;
3327 along = _PyLong_FromByteArray(pdata, (size_t)size,
3328 1 /* little endian */, 1 /* signed */);
3330 if (along == NULL)
3331 return -1;
3332 PDATA_PUSH(self->stack, along, -1);
3333 return 0;
3336 static int
3337 load_float(Unpicklerobject *self)
3339 PyObject *py_float = 0;
3340 char *endptr, *s;
3341 int len, res = -1;
3342 double d;
3344 if ((len = self->readline_func(self, &s)) < 0) return -1;
3345 if (len < 2) return bad_readline();
3346 if (!( s=pystrndup(s,len))) return -1;
3348 errno = 0;
3349 d = PyOS_ascii_strtod(s, &endptr);
3351 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3352 PyErr_SetString(PyExc_ValueError,
3353 "could not convert string to float");
3354 goto finally;
3357 if (!( py_float = PyFloat_FromDouble(d)))
3358 goto finally;
3360 free(s);
3361 PDATA_PUSH(self->stack, py_float, -1);
3362 return 0;
3364 finally:
3365 free(s);
3367 return res;
3370 static int
3371 load_binfloat(Unpicklerobject *self)
3373 PyObject *py_float;
3374 double x;
3375 char *p;
3377 if (self->read_func(self, &p, 8) < 0)
3378 return -1;
3380 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3381 if (x == -1.0 && PyErr_Occurred())
3382 return -1;
3384 py_float = PyFloat_FromDouble(x);
3385 if (py_float == NULL)
3386 return -1;
3388 PDATA_PUSH(self->stack, py_float, -1);
3389 return 0;
3392 static int
3393 load_string(Unpicklerobject *self)
3395 PyObject *str = 0;
3396 int len, res = -1;
3397 char *s, *p;
3399 if ((len = self->readline_func(self, &s)) < 0) return -1;
3400 if (len < 2) return bad_readline();
3401 if (!( s=pystrndup(s,len))) return -1;
3404 /* Strip outermost quotes */
3405 while (s[len-1] <= ' ')
3406 len--;
3407 if(s[0]=='"' && s[len-1]=='"'){
3408 s[len-1] = '\0';
3409 p = s + 1 ;
3410 len -= 2;
3411 } else if(s[0]=='\'' && s[len-1]=='\''){
3412 s[len-1] = '\0';
3413 p = s + 1 ;
3414 len -= 2;
3415 } else
3416 goto insecure;
3417 /********************************************/
3419 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3420 free(s);
3421 if (str) {
3422 PDATA_PUSH(self->stack, str, -1);
3423 res = 0;
3425 return res;
3427 insecure:
3428 free(s);
3429 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3430 return -1;
3434 static int
3435 load_binstring(Unpicklerobject *self)
3437 PyObject *py_string = 0;
3438 long l;
3439 char *s;
3441 if (self->read_func(self, &s, 4) < 0) return -1;
3443 l = calc_binint(s, 4);
3444 if (l < 0) {
3445 /* Corrupt or hostile pickle -- we never write one like
3446 * this.
3448 PyErr_SetString(UnpicklingError,
3449 "BINSTRING pickle has negative byte count");
3450 return -1;
3453 if (self->read_func(self, &s, l) < 0)
3454 return -1;
3456 if (!( py_string = PyString_FromStringAndSize(s, l)))
3457 return -1;
3459 PDATA_PUSH(self->stack, py_string, -1);
3460 return 0;
3464 static int
3465 load_short_binstring(Unpicklerobject *self)
3467 PyObject *py_string = 0;
3468 unsigned char l;
3469 char *s;
3471 if (self->read_func(self, &s, 1) < 0)
3472 return -1;
3474 l = (unsigned char)s[0];
3476 if (self->read_func(self, &s, l) < 0) return -1;
3478 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
3480 PDATA_PUSH(self->stack, py_string, -1);
3481 return 0;
3485 #ifdef Py_USING_UNICODE
3486 static int
3487 load_unicode(Unpicklerobject *self)
3489 PyObject *str = 0;
3490 int len, res = -1;
3491 char *s;
3493 if ((len = self->readline_func(self, &s)) < 0) return -1;
3494 if (len < 1) return bad_readline();
3496 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3497 goto finally;
3499 PDATA_PUSH(self->stack, str, -1);
3500 return 0;
3502 finally:
3503 return res;
3505 #endif
3508 #ifdef Py_USING_UNICODE
3509 static int
3510 load_binunicode(Unpicklerobject *self)
3512 PyObject *unicode;
3513 long l;
3514 char *s;
3516 if (self->read_func(self, &s, 4) < 0) return -1;
3518 l = calc_binint(s, 4);
3519 if (l < 0) {
3520 /* Corrupt or hostile pickle -- we never write one like
3521 * this.
3523 PyErr_SetString(UnpicklingError,
3524 "BINUNICODE pickle has negative byte count");
3525 return -1;
3528 if (self->read_func(self, &s, l) < 0)
3529 return -1;
3531 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3532 return -1;
3534 PDATA_PUSH(self->stack, unicode, -1);
3535 return 0;
3537 #endif
3540 static int
3541 load_tuple(Unpicklerobject *self)
3543 PyObject *tup;
3544 int i;
3546 if ((i = marker(self)) < 0) return -1;
3547 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3548 PDATA_PUSH(self->stack, tup, -1);
3549 return 0;
3552 static int
3553 load_counted_tuple(Unpicklerobject *self, int len)
3555 PyObject *tup = PyTuple_New(len);
3557 if (tup == NULL)
3558 return -1;
3560 while (--len >= 0) {
3561 PyObject *element;
3563 PDATA_POP(self->stack, element);
3564 if (element == NULL)
3565 return -1;
3566 PyTuple_SET_ITEM(tup, len, element);
3568 PDATA_PUSH(self->stack, tup, -1);
3569 return 0;
3572 static int
3573 load_empty_list(Unpicklerobject *self)
3575 PyObject *list;
3577 if (!( list=PyList_New(0))) return -1;
3578 PDATA_PUSH(self->stack, list, -1);
3579 return 0;
3582 static int
3583 load_empty_dict(Unpicklerobject *self)
3585 PyObject *dict;
3587 if (!( dict=PyDict_New())) return -1;
3588 PDATA_PUSH(self->stack, dict, -1);
3589 return 0;
3593 static int
3594 load_list(Unpicklerobject *self)
3596 PyObject *list = 0;
3597 int i;
3599 if ((i = marker(self)) < 0) return -1;
3600 if (!( list=Pdata_popList(self->stack, i))) return -1;
3601 PDATA_PUSH(self->stack, list, -1);
3602 return 0;
3605 static int
3606 load_dict(Unpicklerobject *self)
3608 PyObject *dict, *key, *value;
3609 int i, j, k;
3611 if ((i = marker(self)) < 0) return -1;
3612 j=self->stack->length;
3614 if (!( dict = PyDict_New())) return -1;
3616 for (k = i+1; k < j; k += 2) {
3617 key =self->stack->data[k-1];
3618 value=self->stack->data[k ];
3619 if (PyDict_SetItem(dict, key, value) < 0) {
3620 Py_DECREF(dict);
3621 return -1;
3624 Pdata_clear(self->stack, i);
3625 PDATA_PUSH(self->stack, dict, -1);
3626 return 0;
3629 static PyObject *
3630 Instance_New(PyObject *cls, PyObject *args)
3632 PyObject *r = 0;
3634 if (PyClass_Check(cls)) {
3635 int l;
3637 if ((l=PyObject_Size(args)) < 0) goto err;
3638 if (!( l )) {
3639 PyObject *__getinitargs__;
3641 __getinitargs__ = PyObject_GetAttr(cls,
3642 __getinitargs___str);
3643 if (!__getinitargs__) {
3644 /* We have a class with no __getinitargs__,
3645 so bypass usual construction */
3646 PyObject *inst;
3648 PyErr_Clear();
3649 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3650 goto err;
3651 return inst;
3653 Py_DECREF(__getinitargs__);
3656 if ((r=PyInstance_New(cls, args, NULL))) return r;
3657 else goto err;
3660 if ((r=PyObject_CallObject(cls, args))) return r;
3662 err:
3664 PyObject *tp, *v, *tb, *tmp_value;
3666 PyErr_Fetch(&tp, &v, &tb);
3667 tmp_value = v;
3668 /* NULL occurs when there was a KeyboardInterrupt */
3669 if (tmp_value == NULL)
3670 tmp_value = Py_None;
3671 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3672 Py_XDECREF(v);
3673 v=r;
3675 PyErr_Restore(tp,v,tb);
3677 return NULL;
3681 static int
3682 load_obj(Unpicklerobject *self)
3684 PyObject *class, *tup, *obj=0;
3685 int i;
3687 if ((i = marker(self)) < 0) return -1;
3688 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3689 PDATA_POP(self->stack, class);
3690 if (class) {
3691 obj = Instance_New(class, tup);
3692 Py_DECREF(class);
3694 Py_DECREF(tup);
3696 if (! obj) return -1;
3697 PDATA_PUSH(self->stack, obj, -1);
3698 return 0;
3702 static int
3703 load_inst(Unpicklerobject *self)
3705 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3706 int i, len;
3707 char *s;
3709 if ((i = marker(self)) < 0) return -1;
3711 if ((len = self->readline_func(self, &s)) < 0) return -1;
3712 if (len < 2) return bad_readline();
3713 module_name = PyString_FromStringAndSize(s, len - 1);
3714 if (!module_name) return -1;
3716 if ((len = self->readline_func(self, &s)) >= 0) {
3717 if (len < 2) return bad_readline();
3718 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3719 class = find_class(module_name, class_name,
3720 self->find_class);
3721 Py_DECREF(class_name);
3724 Py_DECREF(module_name);
3726 if (! class) return -1;
3728 if ((tup=Pdata_popTuple(self->stack, i))) {
3729 obj = Instance_New(class, tup);
3730 Py_DECREF(tup);
3732 Py_DECREF(class);
3734 if (! obj) return -1;
3736 PDATA_PUSH(self->stack, obj, -1);
3737 return 0;
3740 static int
3741 load_newobj(Unpicklerobject *self)
3743 PyObject *args = NULL;
3744 PyObject *clsraw = NULL;
3745 PyTypeObject *cls; /* clsraw cast to its true type */
3746 PyObject *obj;
3748 /* Stack is ... cls argtuple, and we want to call
3749 * cls.__new__(cls, *argtuple).
3751 PDATA_POP(self->stack, args);
3752 if (args == NULL) goto Fail;
3753 if (! PyTuple_Check(args)) {
3754 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3755 "tuple.");
3756 goto Fail;
3759 PDATA_POP(self->stack, clsraw);
3760 cls = (PyTypeObject *)clsraw;
3761 if (cls == NULL) goto Fail;
3762 if (! PyType_Check(cls)) {
3763 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3764 "isn't a type object");
3765 goto Fail;
3767 if (cls->tp_new == NULL) {
3768 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3769 "has NULL tp_new");
3770 goto Fail;
3773 /* Call __new__. */
3774 obj = cls->tp_new(cls, args, NULL);
3775 if (obj == NULL) goto Fail;
3777 Py_DECREF(args);
3778 Py_DECREF(clsraw);
3779 PDATA_PUSH(self->stack, obj, -1);
3780 return 0;
3782 Fail:
3783 Py_XDECREF(args);
3784 Py_XDECREF(clsraw);
3785 return -1;
3788 static int
3789 load_global(Unpicklerobject *self)
3791 PyObject *class = 0, *module_name = 0, *class_name = 0;
3792 int len;
3793 char *s;
3795 if ((len = self->readline_func(self, &s)) < 0) return -1;
3796 if (len < 2) return bad_readline();
3797 module_name = PyString_FromStringAndSize(s, len - 1);
3798 if (!module_name) return -1;
3800 if ((len = self->readline_func(self, &s)) >= 0) {
3801 if (len < 2) {
3802 Py_DECREF(module_name);
3803 return bad_readline();
3805 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3806 class = find_class(module_name, class_name,
3807 self->find_class);
3808 Py_DECREF(class_name);
3811 Py_DECREF(module_name);
3813 if (! class) return -1;
3814 PDATA_PUSH(self->stack, class, -1);
3815 return 0;
3819 static int
3820 load_persid(Unpicklerobject *self)
3822 PyObject *pid = 0;
3823 int len;
3824 char *s;
3826 if (self->pers_func) {
3827 if ((len = self->readline_func(self, &s)) < 0) return -1;
3828 if (len < 2) return bad_readline();
3830 pid = PyString_FromStringAndSize(s, len - 1);
3831 if (!pid) return -1;
3833 if (PyList_Check(self->pers_func)) {
3834 if (PyList_Append(self->pers_func, pid) < 0) {
3835 Py_DECREF(pid);
3836 return -1;
3839 else {
3840 ARG_TUP(self, pid);
3841 if (self->arg) {
3842 pid = PyObject_Call(self->pers_func, self->arg,
3843 NULL);
3844 FREE_ARG_TUP(self);
3848 if (! pid) return -1;
3850 PDATA_PUSH(self->stack, pid, -1);
3851 return 0;
3853 else {
3854 PyErr_SetString(UnpicklingError,
3855 "A load persistent id instruction was encountered,\n"
3856 "but no persistent_load function was specified.");
3857 return -1;
3861 static int
3862 load_binpersid(Unpicklerobject *self)
3864 PyObject *pid = 0;
3866 if (self->pers_func) {
3867 PDATA_POP(self->stack, pid);
3868 if (! pid) return -1;
3870 if (PyList_Check(self->pers_func)) {
3871 if (PyList_Append(self->pers_func, pid) < 0) {
3872 Py_DECREF(pid);
3873 return -1;
3876 else {
3877 ARG_TUP(self, pid);
3878 if (self->arg) {
3879 pid = PyObject_Call(self->pers_func, self->arg,
3880 NULL);
3881 FREE_ARG_TUP(self);
3883 if (! pid) return -1;
3886 PDATA_PUSH(self->stack, pid, -1);
3887 return 0;
3889 else {
3890 PyErr_SetString(UnpicklingError,
3891 "A load persistent id instruction was encountered,\n"
3892 "but no persistent_load function was specified.");
3893 return -1;
3898 static int
3899 load_pop(Unpicklerobject *self)
3901 int len;
3903 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3905 /* Note that we split the (pickle.py) stack into two stacks,
3906 an object stack and a mark stack. We have to be clever and
3907 pop the right one. We do this by looking at the top of the
3908 mark stack.
3911 if ((self->num_marks > 0) &&
3912 (self->marks[self->num_marks - 1] == len))
3913 self->num_marks--;
3914 else {
3915 len--;
3916 Py_DECREF(self->stack->data[len]);
3917 self->stack->length=len;
3920 return 0;
3924 static int
3925 load_pop_mark(Unpicklerobject *self)
3927 int i;
3929 if ((i = marker(self)) < 0)
3930 return -1;
3932 Pdata_clear(self->stack, i);
3934 return 0;
3938 static int
3939 load_dup(Unpicklerobject *self)
3941 PyObject *last;
3942 int len;
3944 if ((len = self->stack->length) <= 0) return stackUnderflow();
3945 last=self->stack->data[len-1];
3946 Py_INCREF(last);
3947 PDATA_PUSH(self->stack, last, -1);
3948 return 0;
3952 static int
3953 load_get(Unpicklerobject *self)
3955 PyObject *py_str = 0, *value = 0;
3956 int len;
3957 char *s;
3958 int rc;
3960 if ((len = self->readline_func(self, &s)) < 0) return -1;
3961 if (len < 2) return bad_readline();
3963 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
3965 value = PyDict_GetItem(self->memo, py_str);
3966 if (! value) {
3967 PyErr_SetObject(BadPickleGet, py_str);
3968 rc = -1;
3970 else {
3971 PDATA_APPEND(self->stack, value, -1);
3972 rc = 0;
3975 Py_DECREF(py_str);
3976 return rc;
3980 static int
3981 load_binget(Unpicklerobject *self)
3983 PyObject *py_key = 0, *value = 0;
3984 unsigned char key;
3985 char *s;
3986 int rc;
3988 if (self->read_func(self, &s, 1) < 0) return -1;
3990 key = (unsigned char)s[0];
3991 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3993 value = PyDict_GetItem(self->memo, py_key);
3994 if (! value) {
3995 PyErr_SetObject(BadPickleGet, py_key);
3996 rc = -1;
3998 else {
3999 PDATA_APPEND(self->stack, value, -1);
4000 rc = 0;
4003 Py_DECREF(py_key);
4004 return rc;
4008 static int
4009 load_long_binget(Unpicklerobject *self)
4011 PyObject *py_key = 0, *value = 0;
4012 unsigned char c;
4013 char *s;
4014 long key;
4015 int rc;
4017 if (self->read_func(self, &s, 4) < 0) return -1;
4019 c = (unsigned char)s[0];
4020 key = (long)c;
4021 c = (unsigned char)s[1];
4022 key |= (long)c << 8;
4023 c = (unsigned char)s[2];
4024 key |= (long)c << 16;
4025 c = (unsigned char)s[3];
4026 key |= (long)c << 24;
4028 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4030 value = PyDict_GetItem(self->memo, py_key);
4031 if (! value) {
4032 PyErr_SetObject(BadPickleGet, py_key);
4033 rc = -1;
4035 else {
4036 PDATA_APPEND(self->stack, value, -1);
4037 rc = 0;
4040 Py_DECREF(py_key);
4041 return rc;
4044 /* Push an object from the extension registry (EXT[124]). nbytes is
4045 * the number of bytes following the opcode, holding the index (code) value.
4047 static int
4048 load_extension(Unpicklerobject *self, int nbytes)
4050 char *codebytes; /* the nbytes bytes after the opcode */
4051 long code; /* calc_binint returns long */
4052 PyObject *py_code; /* code as a Python int */
4053 PyObject *obj; /* the object to push */
4054 PyObject *pair; /* (module_name, class_name) */
4055 PyObject *module_name, *class_name;
4057 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4058 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4059 code = calc_binint(codebytes, nbytes);
4060 if (code <= 0) { /* note that 0 is forbidden */
4061 /* Corrupt or hostile pickle. */
4062 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4063 return -1;
4066 /* Look for the code in the cache. */
4067 py_code = PyInt_FromLong(code);
4068 if (py_code == NULL) return -1;
4069 obj = PyDict_GetItem(extension_cache, py_code);
4070 if (obj != NULL) {
4071 /* Bingo. */
4072 Py_DECREF(py_code);
4073 PDATA_APPEND(self->stack, obj, -1);
4074 return 0;
4077 /* Look up the (module_name, class_name) pair. */
4078 pair = PyDict_GetItem(inverted_registry, py_code);
4079 if (pair == NULL) {
4080 Py_DECREF(py_code);
4081 PyErr_Format(PyExc_ValueError, "unregistered extension "
4082 "code %ld", code);
4083 return -1;
4085 /* Since the extension registry is manipulable via Python code,
4086 * confirm that pair is really a 2-tuple of strings.
4088 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4089 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4090 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4091 Py_DECREF(py_code);
4092 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4093 "isn't a 2-tuple of strings", code);
4094 return -1;
4096 /* Load the object. */
4097 obj = find_class(module_name, class_name, self->find_class);
4098 if (obj == NULL) {
4099 Py_DECREF(py_code);
4100 return -1;
4102 /* Cache code -> obj. */
4103 code = PyDict_SetItem(extension_cache, py_code, obj);
4104 Py_DECREF(py_code);
4105 if (code < 0) {
4106 Py_DECREF(obj);
4107 return -1;
4109 PDATA_PUSH(self->stack, obj, -1);
4110 return 0;
4113 static int
4114 load_put(Unpicklerobject *self)
4116 PyObject *py_str = 0, *value = 0;
4117 int len, l;
4118 char *s;
4120 if ((l = self->readline_func(self, &s)) < 0) return -1;
4121 if (l < 2) return bad_readline();
4122 if (!( len=self->stack->length )) return stackUnderflow();
4123 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4124 value=self->stack->data[len-1];
4125 l=PyDict_SetItem(self->memo, py_str, value);
4126 Py_DECREF(py_str);
4127 return l;
4131 static int
4132 load_binput(Unpicklerobject *self)
4134 PyObject *py_key = 0, *value = 0;
4135 unsigned char key;
4136 char *s;
4137 int len;
4139 if (self->read_func(self, &s, 1) < 0) return -1;
4140 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4142 key = (unsigned char)s[0];
4144 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4145 value=self->stack->data[len-1];
4146 len=PyDict_SetItem(self->memo, py_key, value);
4147 Py_DECREF(py_key);
4148 return len;
4152 static int
4153 load_long_binput(Unpicklerobject *self)
4155 PyObject *py_key = 0, *value = 0;
4156 long key;
4157 unsigned char c;
4158 char *s;
4159 int len;
4161 if (self->read_func(self, &s, 4) < 0) return -1;
4162 if (!( len=self->stack->length )) return stackUnderflow();
4164 c = (unsigned char)s[0];
4165 key = (long)c;
4166 c = (unsigned char)s[1];
4167 key |= (long)c << 8;
4168 c = (unsigned char)s[2];
4169 key |= (long)c << 16;
4170 c = (unsigned char)s[3];
4171 key |= (long)c << 24;
4173 if (!( py_key = PyInt_FromLong(key))) return -1;
4174 value=self->stack->data[len-1];
4175 len=PyDict_SetItem(self->memo, py_key, value);
4176 Py_DECREF(py_key);
4177 return len;
4181 static int
4182 do_append(Unpicklerobject *self, int x)
4184 PyObject *value = 0, *list = 0, *append_method = 0;
4185 int len, i;
4187 len=self->stack->length;
4188 if (!( len >= x && x > 0 )) return stackUnderflow();
4189 /* nothing to do */
4190 if (len==x) return 0;
4192 list=self->stack->data[x-1];
4194 if (PyList_Check(list)) {
4195 PyObject *slice;
4196 int list_len;
4198 slice=Pdata_popList(self->stack, x);
4199 if (! slice) return -1;
4200 list_len = PyList_GET_SIZE(list);
4201 i=PyList_SetSlice(list, list_len, list_len, slice);
4202 Py_DECREF(slice);
4203 return i;
4205 else {
4207 if (!( append_method = PyObject_GetAttr(list, append_str)))
4208 return -1;
4210 for (i = x; i < len; i++) {
4211 PyObject *junk;
4213 value=self->stack->data[i];
4214 junk=0;
4215 ARG_TUP(self, value);
4216 if (self->arg) {
4217 junk = PyObject_Call(append_method, self->arg,
4218 NULL);
4219 FREE_ARG_TUP(self);
4221 if (! junk) {
4222 Pdata_clear(self->stack, i+1);
4223 self->stack->length=x;
4224 Py_DECREF(append_method);
4225 return -1;
4227 Py_DECREF(junk);
4229 self->stack->length=x;
4230 Py_DECREF(append_method);
4233 return 0;
4237 static int
4238 load_append(Unpicklerobject *self)
4240 return do_append(self, self->stack->length - 1);
4244 static int
4245 load_appends(Unpicklerobject *self)
4247 return do_append(self, marker(self));
4251 static int
4252 do_setitems(Unpicklerobject *self, int x)
4254 PyObject *value = 0, *key = 0, *dict = 0;
4255 int len, i, r=0;
4257 if (!( (len=self->stack->length) >= x
4258 && x > 0 )) return stackUnderflow();
4260 dict=self->stack->data[x-1];
4262 for (i = x+1; i < len; i += 2) {
4263 key =self->stack->data[i-1];
4264 value=self->stack->data[i ];
4265 if (PyObject_SetItem(dict, key, value) < 0) {
4266 r=-1;
4267 break;
4271 Pdata_clear(self->stack, x);
4273 return r;
4277 static int
4278 load_setitem(Unpicklerobject *self)
4280 return do_setitems(self, self->stack->length - 2);
4283 static int
4284 load_setitems(Unpicklerobject *self)
4286 return do_setitems(self, marker(self));
4290 static int
4291 load_build(Unpicklerobject *self)
4293 PyObject *state, *inst, *slotstate;
4294 PyObject *__setstate__;
4295 PyObject *d_key, *d_value;
4296 Py_ssize_t i;
4297 int res = -1;
4299 /* Stack is ... instance, state. We want to leave instance at
4300 * the stack top, possibly mutated via instance.__setstate__(state).
4302 if (self->stack->length < 2)
4303 return stackUnderflow();
4304 PDATA_POP(self->stack, state);
4305 if (state == NULL)
4306 return -1;
4307 inst = self->stack->data[self->stack->length - 1];
4309 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4310 if (__setstate__ != NULL) {
4311 PyObject *junk = NULL;
4313 /* The explicit __setstate__ is responsible for everything. */
4314 ARG_TUP(self, state);
4315 if (self->arg) {
4316 junk = PyObject_Call(__setstate__, self->arg, NULL);
4317 FREE_ARG_TUP(self);
4319 Py_DECREF(__setstate__);
4320 if (junk == NULL)
4321 return -1;
4322 Py_DECREF(junk);
4323 return 0;
4325 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4326 return -1;
4327 PyErr_Clear();
4329 /* A default __setstate__. First see whether state embeds a
4330 * slot state dict too (a proto 2 addition).
4332 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4333 PyObject *temp = state;
4334 state = PyTuple_GET_ITEM(temp, 0);
4335 slotstate = PyTuple_GET_ITEM(temp, 1);
4336 Py_INCREF(state);
4337 Py_INCREF(slotstate);
4338 Py_DECREF(temp);
4340 else
4341 slotstate = NULL;
4343 /* Set inst.__dict__ from the state dict (if any). */
4344 if (state != Py_None) {
4345 PyObject *dict;
4346 if (! PyDict_Check(state)) {
4347 PyErr_SetString(UnpicklingError, "state is not a "
4348 "dictionary");
4349 goto finally;
4351 dict = PyObject_GetAttr(inst, __dict___str);
4352 if (dict == NULL)
4353 goto finally;
4355 i = 0;
4356 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4357 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4358 goto finally;
4360 Py_DECREF(dict);
4363 /* Also set instance attributes from the slotstate dict (if any). */
4364 if (slotstate != NULL) {
4365 if (! PyDict_Check(slotstate)) {
4366 PyErr_SetString(UnpicklingError, "slot state is not "
4367 "a dictionary");
4368 goto finally;
4370 i = 0;
4371 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4372 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4373 goto finally;
4376 res = 0;
4378 finally:
4379 Py_DECREF(state);
4380 Py_XDECREF(slotstate);
4381 return res;
4385 static int
4386 load_mark(Unpicklerobject *self)
4388 int s;
4390 /* Note that we split the (pickle.py) stack into two stacks, an
4391 object stack and a mark stack. Here we push a mark onto the
4392 mark stack.
4395 if ((self->num_marks + 1) >= self->marks_size) {
4396 int *marks;
4397 s=self->marks_size+20;
4398 if (s <= self->num_marks) s=self->num_marks + 1;
4399 if (self->marks == NULL)
4400 marks=(int *)malloc(s * sizeof(int));
4401 else
4402 marks=(int *)realloc(self->marks,
4403 s * sizeof(int));
4404 if (!marks) {
4405 PyErr_NoMemory();
4406 return -1;
4408 self->marks = marks;
4409 self->marks_size = s;
4412 self->marks[self->num_marks++] = self->stack->length;
4414 return 0;
4417 static int
4418 load_reduce(Unpicklerobject *self)
4420 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4422 PDATA_POP(self->stack, arg_tup);
4423 if (! arg_tup) return -1;
4424 PDATA_POP(self->stack, callable);
4425 if (callable) {
4426 ob = Instance_New(callable, arg_tup);
4427 Py_DECREF(callable);
4429 Py_DECREF(arg_tup);
4431 if (! ob) return -1;
4433 PDATA_PUSH(self->stack, ob, -1);
4434 return 0;
4437 /* Just raises an error if we don't know the protocol specified. PROTO
4438 * is the first opcode for protocols >= 2.
4440 static int
4441 load_proto(Unpicklerobject *self)
4443 int i;
4444 char *protobyte;
4446 i = self->read_func(self, &protobyte, 1);
4447 if (i < 0)
4448 return -1;
4450 i = calc_binint(protobyte, 1);
4451 /* No point checking for < 0, since calc_binint returns an unsigned
4452 * int when chewing on 1 byte.
4454 assert(i >= 0);
4455 if (i <= HIGHEST_PROTOCOL)
4456 return 0;
4458 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4459 return -1;
4462 static PyObject *
4463 load(Unpicklerobject *self)
4465 PyObject *err = 0, *val = 0;
4466 char *s;
4468 self->num_marks = 0;
4469 if (self->stack->length) Pdata_clear(self->stack, 0);
4471 while (1) {
4472 if (self->read_func(self, &s, 1) < 0)
4473 break;
4475 switch (s[0]) {
4476 case NONE:
4477 if (load_none(self) < 0)
4478 break;
4479 continue;
4481 case BININT:
4482 if (load_binint(self) < 0)
4483 break;
4484 continue;
4486 case BININT1:
4487 if (load_binint1(self) < 0)
4488 break;
4489 continue;
4491 case BININT2:
4492 if (load_binint2(self) < 0)
4493 break;
4494 continue;
4496 case INT:
4497 if (load_int(self) < 0)
4498 break;
4499 continue;
4501 case LONG:
4502 if (load_long(self) < 0)
4503 break;
4504 continue;
4506 case LONG1:
4507 if (load_counted_long(self, 1) < 0)
4508 break;
4509 continue;
4511 case LONG4:
4512 if (load_counted_long(self, 4) < 0)
4513 break;
4514 continue;
4516 case FLOAT:
4517 if (load_float(self) < 0)
4518 break;
4519 continue;
4521 case BINFLOAT:
4522 if (load_binfloat(self) < 0)
4523 break;
4524 continue;
4526 case BINSTRING:
4527 if (load_binstring(self) < 0)
4528 break;
4529 continue;
4531 case SHORT_BINSTRING:
4532 if (load_short_binstring(self) < 0)
4533 break;
4534 continue;
4536 case STRING:
4537 if (load_string(self) < 0)
4538 break;
4539 continue;
4541 #ifdef Py_USING_UNICODE
4542 case UNICODE:
4543 if (load_unicode(self) < 0)
4544 break;
4545 continue;
4547 case BINUNICODE:
4548 if (load_binunicode(self) < 0)
4549 break;
4550 continue;
4551 #endif
4553 case EMPTY_TUPLE:
4554 if (load_counted_tuple(self, 0) < 0)
4555 break;
4556 continue;
4558 case TUPLE1:
4559 if (load_counted_tuple(self, 1) < 0)
4560 break;
4561 continue;
4563 case TUPLE2:
4564 if (load_counted_tuple(self, 2) < 0)
4565 break;
4566 continue;
4568 case TUPLE3:
4569 if (load_counted_tuple(self, 3) < 0)
4570 break;
4571 continue;
4573 case TUPLE:
4574 if (load_tuple(self) < 0)
4575 break;
4576 continue;
4578 case EMPTY_LIST:
4579 if (load_empty_list(self) < 0)
4580 break;
4581 continue;
4583 case LIST:
4584 if (load_list(self) < 0)
4585 break;
4586 continue;
4588 case EMPTY_DICT:
4589 if (load_empty_dict(self) < 0)
4590 break;
4591 continue;
4593 case DICT:
4594 if (load_dict(self) < 0)
4595 break;
4596 continue;
4598 case OBJ:
4599 if (load_obj(self) < 0)
4600 break;
4601 continue;
4603 case INST:
4604 if (load_inst(self) < 0)
4605 break;
4606 continue;
4608 case NEWOBJ:
4609 if (load_newobj(self) < 0)
4610 break;
4611 continue;
4613 case GLOBAL:
4614 if (load_global(self) < 0)
4615 break;
4616 continue;
4618 case APPEND:
4619 if (load_append(self) < 0)
4620 break;
4621 continue;
4623 case APPENDS:
4624 if (load_appends(self) < 0)
4625 break;
4626 continue;
4628 case BUILD:
4629 if (load_build(self) < 0)
4630 break;
4631 continue;
4633 case DUP:
4634 if (load_dup(self) < 0)
4635 break;
4636 continue;
4638 case BINGET:
4639 if (load_binget(self) < 0)
4640 break;
4641 continue;
4643 case LONG_BINGET:
4644 if (load_long_binget(self) < 0)
4645 break;
4646 continue;
4648 case GET:
4649 if (load_get(self) < 0)
4650 break;
4651 continue;
4653 case EXT1:
4654 if (load_extension(self, 1) < 0)
4655 break;
4656 continue;
4658 case EXT2:
4659 if (load_extension(self, 2) < 0)
4660 break;
4661 continue;
4663 case EXT4:
4664 if (load_extension(self, 4) < 0)
4665 break;
4666 continue;
4667 case MARK:
4668 if (load_mark(self) < 0)
4669 break;
4670 continue;
4672 case BINPUT:
4673 if (load_binput(self) < 0)
4674 break;
4675 continue;
4677 case LONG_BINPUT:
4678 if (load_long_binput(self) < 0)
4679 break;
4680 continue;
4682 case PUT:
4683 if (load_put(self) < 0)
4684 break;
4685 continue;
4687 case POP:
4688 if (load_pop(self) < 0)
4689 break;
4690 continue;
4692 case POP_MARK:
4693 if (load_pop_mark(self) < 0)
4694 break;
4695 continue;
4697 case SETITEM:
4698 if (load_setitem(self) < 0)
4699 break;
4700 continue;
4702 case SETITEMS:
4703 if (load_setitems(self) < 0)
4704 break;
4705 continue;
4707 case STOP:
4708 break;
4710 case PERSID:
4711 if (load_persid(self) < 0)
4712 break;
4713 continue;
4715 case BINPERSID:
4716 if (load_binpersid(self) < 0)
4717 break;
4718 continue;
4720 case REDUCE:
4721 if (load_reduce(self) < 0)
4722 break;
4723 continue;
4725 case PROTO:
4726 if (load_proto(self) < 0)
4727 break;
4728 continue;
4730 case NEWTRUE:
4731 if (load_bool(self, Py_True) < 0)
4732 break;
4733 continue;
4735 case NEWFALSE:
4736 if (load_bool(self, Py_False) < 0)
4737 break;
4738 continue;
4740 case '\0':
4741 /* end of file */
4742 PyErr_SetNone(PyExc_EOFError);
4743 break;
4745 default:
4746 cPickle_ErrFormat(UnpicklingError,
4747 "invalid load key, '%s'.",
4748 "c", s[0]);
4749 return NULL;
4752 break;
4755 if ((err = PyErr_Occurred())) {
4756 if (err == PyExc_EOFError) {
4757 PyErr_SetNone(PyExc_EOFError);
4759 return NULL;
4762 PDATA_POP(self->stack, val);
4763 return val;
4767 /* No-load functions to support noload, which is used to
4768 find persistent references. */
4770 static int
4771 noload_obj(Unpicklerobject *self)
4773 int i;
4775 if ((i = marker(self)) < 0) return -1;
4776 return Pdata_clear(self->stack, i+1);
4780 static int
4781 noload_inst(Unpicklerobject *self)
4783 int i;
4784 char *s;
4786 if ((i = marker(self)) < 0) return -1;
4787 Pdata_clear(self->stack, i);
4788 if (self->readline_func(self, &s) < 0) return -1;
4789 if (self->readline_func(self, &s) < 0) return -1;
4790 PDATA_APPEND(self->stack, Py_None, -1);
4791 return 0;
4794 static int
4795 noload_newobj(Unpicklerobject *self)
4797 PyObject *obj;
4799 PDATA_POP(self->stack, obj); /* pop argtuple */
4800 if (obj == NULL) return -1;
4801 Py_DECREF(obj);
4803 PDATA_POP(self->stack, obj); /* pop cls */
4804 if (obj == NULL) return -1;
4805 Py_DECREF(obj);
4807 PDATA_APPEND(self->stack, Py_None, -1);
4808 return 0;
4811 static int
4812 noload_global(Unpicklerobject *self)
4814 char *s;
4816 if (self->readline_func(self, &s) < 0) return -1;
4817 if (self->readline_func(self, &s) < 0) return -1;
4818 PDATA_APPEND(self->stack, Py_None,-1);
4819 return 0;
4822 static int
4823 noload_reduce(Unpicklerobject *self)
4826 if (self->stack->length < 2) return stackUnderflow();
4827 Pdata_clear(self->stack, self->stack->length-2);
4828 PDATA_APPEND(self->stack, Py_None,-1);
4829 return 0;
4832 static int
4833 noload_build(Unpicklerobject *self) {
4835 if (self->stack->length < 1) return stackUnderflow();
4836 Pdata_clear(self->stack, self->stack->length-1);
4837 return 0;
4840 static int
4841 noload_extension(Unpicklerobject *self, int nbytes)
4843 char *codebytes;
4845 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4846 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4847 PDATA_APPEND(self->stack, Py_None, -1);
4848 return 0;
4852 static PyObject *
4853 noload(Unpicklerobject *self)
4855 PyObject *err = 0, *val = 0;
4856 char *s;
4858 self->num_marks = 0;
4859 Pdata_clear(self->stack, 0);
4861 while (1) {
4862 if (self->read_func(self, &s, 1) < 0)
4863 break;
4865 switch (s[0]) {
4866 case NONE:
4867 if (load_none(self) < 0)
4868 break;
4869 continue;
4871 case BININT:
4872 if (load_binint(self) < 0)
4873 break;
4874 continue;
4876 case BININT1:
4877 if (load_binint1(self) < 0)
4878 break;
4879 continue;
4881 case BININT2:
4882 if (load_binint2(self) < 0)
4883 break;
4884 continue;
4886 case INT:
4887 if (load_int(self) < 0)
4888 break;
4889 continue;
4891 case LONG:
4892 if (load_long(self) < 0)
4893 break;
4894 continue;
4896 case LONG1:
4897 if (load_counted_long(self, 1) < 0)
4898 break;
4899 continue;
4901 case LONG4:
4902 if (load_counted_long(self, 4) < 0)
4903 break;
4904 continue;
4906 case FLOAT:
4907 if (load_float(self) < 0)
4908 break;
4909 continue;
4911 case BINFLOAT:
4912 if (load_binfloat(self) < 0)
4913 break;
4914 continue;
4916 case BINSTRING:
4917 if (load_binstring(self) < 0)
4918 break;
4919 continue;
4921 case SHORT_BINSTRING:
4922 if (load_short_binstring(self) < 0)
4923 break;
4924 continue;
4926 case STRING:
4927 if (load_string(self) < 0)
4928 break;
4929 continue;
4931 #ifdef Py_USING_UNICODE
4932 case UNICODE:
4933 if (load_unicode(self) < 0)
4934 break;
4935 continue;
4937 case BINUNICODE:
4938 if (load_binunicode(self) < 0)
4939 break;
4940 continue;
4941 #endif
4943 case EMPTY_TUPLE:
4944 if (load_counted_tuple(self, 0) < 0)
4945 break;
4946 continue;
4948 case TUPLE1:
4949 if (load_counted_tuple(self, 1) < 0)
4950 break;
4951 continue;
4953 case TUPLE2:
4954 if (load_counted_tuple(self, 2) < 0)
4955 break;
4956 continue;
4958 case TUPLE3:
4959 if (load_counted_tuple(self, 3) < 0)
4960 break;
4961 continue;
4963 case TUPLE:
4964 if (load_tuple(self) < 0)
4965 break;
4966 continue;
4968 case EMPTY_LIST:
4969 if (load_empty_list(self) < 0)
4970 break;
4971 continue;
4973 case LIST:
4974 if (load_list(self) < 0)
4975 break;
4976 continue;
4978 case EMPTY_DICT:
4979 if (load_empty_dict(self) < 0)
4980 break;
4981 continue;
4983 case DICT:
4984 if (load_dict(self) < 0)
4985 break;
4986 continue;
4988 case OBJ:
4989 if (noload_obj(self) < 0)
4990 break;
4991 continue;
4993 case INST:
4994 if (noload_inst(self) < 0)
4995 break;
4996 continue;
4998 case NEWOBJ:
4999 if (noload_newobj(self) < 0)
5000 break;
5001 continue;
5003 case GLOBAL:
5004 if (noload_global(self) < 0)
5005 break;
5006 continue;
5008 case APPEND:
5009 if (load_append(self) < 0)
5010 break;
5011 continue;
5013 case APPENDS:
5014 if (load_appends(self) < 0)
5015 break;
5016 continue;
5018 case BUILD:
5019 if (noload_build(self) < 0)
5020 break;
5021 continue;
5023 case DUP:
5024 if (load_dup(self) < 0)
5025 break;
5026 continue;
5028 case BINGET:
5029 if (load_binget(self) < 0)
5030 break;
5031 continue;
5033 case LONG_BINGET:
5034 if (load_long_binget(self) < 0)
5035 break;
5036 continue;
5038 case GET:
5039 if (load_get(self) < 0)
5040 break;
5041 continue;
5043 case EXT1:
5044 if (noload_extension(self, 1) < 0)
5045 break;
5046 continue;
5048 case EXT2:
5049 if (noload_extension(self, 2) < 0)
5050 break;
5051 continue;
5053 case EXT4:
5054 if (noload_extension(self, 4) < 0)
5055 break;
5056 continue;
5058 case MARK:
5059 if (load_mark(self) < 0)
5060 break;
5061 continue;
5063 case BINPUT:
5064 if (load_binput(self) < 0)
5065 break;
5066 continue;
5068 case LONG_BINPUT:
5069 if (load_long_binput(self) < 0)
5070 break;
5071 continue;
5073 case PUT:
5074 if (load_put(self) < 0)
5075 break;
5076 continue;
5078 case POP:
5079 if (load_pop(self) < 0)
5080 break;
5081 continue;
5083 case POP_MARK:
5084 if (load_pop_mark(self) < 0)
5085 break;
5086 continue;
5088 case SETITEM:
5089 if (load_setitem(self) < 0)
5090 break;
5091 continue;
5093 case SETITEMS:
5094 if (load_setitems(self) < 0)
5095 break;
5096 continue;
5098 case STOP:
5099 break;
5101 case PERSID:
5102 if (load_persid(self) < 0)
5103 break;
5104 continue;
5106 case BINPERSID:
5107 if (load_binpersid(self) < 0)
5108 break;
5109 continue;
5111 case REDUCE:
5112 if (noload_reduce(self) < 0)
5113 break;
5114 continue;
5116 case PROTO:
5117 if (load_proto(self) < 0)
5118 break;
5119 continue;
5121 case NEWTRUE:
5122 if (load_bool(self, Py_True) < 0)
5123 break;
5124 continue;
5126 case NEWFALSE:
5127 if (load_bool(self, Py_False) < 0)
5128 break;
5129 continue;
5130 default:
5131 cPickle_ErrFormat(UnpicklingError,
5132 "invalid load key, '%s'.",
5133 "c", s[0]);
5134 return NULL;
5137 break;
5140 if ((err = PyErr_Occurred())) {
5141 if (err == PyExc_EOFError) {
5142 PyErr_SetNone(PyExc_EOFError);
5144 return NULL;
5147 PDATA_POP(self->stack, val);
5148 return val;
5152 static PyObject *
5153 Unpickler_load(Unpicklerobject *self, PyObject *unused)
5155 return load(self);
5158 static PyObject *
5159 Unpickler_noload(Unpicklerobject *self, PyObject *unused)
5161 return noload(self);
5165 static struct PyMethodDef Unpickler_methods[] = {
5166 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
5167 PyDoc_STR("load() -- Load a pickle")
5169 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
5170 PyDoc_STR(
5171 "noload() -- not load a pickle, but go through most of the motions\n"
5172 "\n"
5173 "This function can be used to read past a pickle without instantiating\n"
5174 "any objects or importing any modules. It can also be used to find all\n"
5175 "persistent references without instantiating any objects or importing\n"
5176 "any modules.\n")
5178 {NULL, NULL} /* sentinel */
5182 static Unpicklerobject *
5183 newUnpicklerobject(PyObject *f)
5185 Unpicklerobject *self;
5187 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5188 return NULL;
5190 self->file = NULL;
5191 self->arg = NULL;
5192 self->stack = (Pdata*)Pdata_New();
5193 self->pers_func = NULL;
5194 self->last_string = NULL;
5195 self->marks = NULL;
5196 self->num_marks = 0;
5197 self->marks_size = 0;
5198 self->buf_size = 0;
5199 self->read = NULL;
5200 self->readline = NULL;
5201 self->find_class = NULL;
5203 if (!( self->memo = PyDict_New()))
5204 goto err;
5206 if (!self->stack)
5207 goto err;
5209 Py_INCREF(f);
5210 self->file = f;
5212 /* Set read, readline based on type of f */
5213 if (PyFile_Check(f)) {
5214 self->fp = PyFile_AsFile(f);
5215 if (self->fp == NULL) {
5216 PyErr_SetString(PyExc_ValueError,
5217 "I/O operation on closed file");
5218 goto err;
5220 self->read_func = read_file;
5221 self->readline_func = readline_file;
5223 else if (PycStringIO_InputCheck(f)) {
5224 self->fp = NULL;
5225 self->read_func = read_cStringIO;
5226 self->readline_func = readline_cStringIO;
5228 else {
5230 self->fp = NULL;
5231 self->read_func = read_other;
5232 self->readline_func = readline_other;
5234 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5235 (self->read = PyObject_GetAttr(f, read_str)))) {
5236 PyErr_Clear();
5237 PyErr_SetString( PyExc_TypeError,
5238 "argument must have 'read' and "
5239 "'readline' attributes" );
5240 goto err;
5243 PyObject_GC_Track(self);
5245 return self;
5247 err:
5248 Py_DECREF((PyObject *)self);
5249 return NULL;
5253 static PyObject *
5254 get_Unpickler(PyObject *self, PyObject *file)
5256 return (PyObject *)newUnpicklerobject(file);
5260 static void
5261 Unpickler_dealloc(Unpicklerobject *self)
5263 PyObject_GC_UnTrack((PyObject *)self);
5264 Py_XDECREF(self->readline);
5265 Py_XDECREF(self->read);
5266 Py_XDECREF(self->file);
5267 Py_XDECREF(self->memo);
5268 Py_XDECREF(self->stack);
5269 Py_XDECREF(self->pers_func);
5270 Py_XDECREF(self->arg);
5271 Py_XDECREF(self->last_string);
5272 Py_XDECREF(self->find_class);
5274 if (self->marks) {
5275 free(self->marks);
5278 if (self->buf_size) {
5279 free(self->buf);
5282 Py_TYPE(self)->tp_free((PyObject *)self);
5285 static int
5286 Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5288 Py_VISIT(self->readline);
5289 Py_VISIT(self->read);
5290 Py_VISIT(self->file);
5291 Py_VISIT(self->memo);
5292 Py_VISIT(self->stack);
5293 Py_VISIT(self->pers_func);
5294 Py_VISIT(self->arg);
5295 Py_VISIT(self->last_string);
5296 Py_VISIT(self->find_class);
5297 return 0;
5300 static int
5301 Unpickler_clear(Unpicklerobject *self)
5303 Py_CLEAR(self->readline);
5304 Py_CLEAR(self->read);
5305 Py_CLEAR(self->file);
5306 Py_CLEAR(self->memo);
5307 Py_CLEAR(self->stack);
5308 Py_CLEAR(self->pers_func);
5309 Py_CLEAR(self->arg);
5310 Py_CLEAR(self->last_string);
5311 Py_CLEAR(self->find_class);
5312 return 0;
5315 static PyObject *
5316 Unpickler_getattr(Unpicklerobject *self, char *name)
5318 if (!strcmp(name, "persistent_load")) {
5319 if (!self->pers_func) {
5320 PyErr_SetString(PyExc_AttributeError, name);
5321 return NULL;
5324 Py_INCREF(self->pers_func);
5325 return self->pers_func;
5328 if (!strcmp(name, "find_global")) {
5329 if (!self->find_class) {
5330 PyErr_SetString(PyExc_AttributeError, name);
5331 return NULL;
5334 Py_INCREF(self->find_class);
5335 return self->find_class;
5338 if (!strcmp(name, "memo")) {
5339 if (!self->memo) {
5340 PyErr_SetString(PyExc_AttributeError, name);
5341 return NULL;
5344 Py_INCREF(self->memo);
5345 return self->memo;
5348 if (!strcmp(name, "UnpicklingError")) {
5349 Py_INCREF(UnpicklingError);
5350 return UnpicklingError;
5353 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5357 static int
5358 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5361 if (!strcmp(name, "persistent_load")) {
5362 Py_XDECREF(self->pers_func);
5363 self->pers_func = value;
5364 Py_XINCREF(value);
5365 return 0;
5368 if (!strcmp(name, "find_global")) {
5369 Py_XDECREF(self->find_class);
5370 self->find_class = value;
5371 Py_XINCREF(value);
5372 return 0;
5375 if (! value) {
5376 PyErr_SetString(PyExc_TypeError,
5377 "attribute deletion is not supported");
5378 return -1;
5381 if (strcmp(name, "memo") == 0) {
5382 if (!PyDict_Check(value)) {
5383 PyErr_SetString(PyExc_TypeError,
5384 "memo must be a dictionary");
5385 return -1;
5387 Py_XDECREF(self->memo);
5388 self->memo = value;
5389 Py_INCREF(value);
5390 return 0;
5393 PyErr_SetString(PyExc_AttributeError, name);
5394 return -1;
5397 /* ---------------------------------------------------------------------------
5398 * Module-level functions.
5401 /* dump(obj, file, protocol=0). */
5402 static PyObject *
5403 cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5405 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5406 PyObject *ob, *file, *res = NULL;
5407 Picklerobject *pickler = 0;
5408 int proto = 0;
5410 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5411 &ob, &file, &proto)))
5412 goto finally;
5414 if (!( pickler = newPicklerobject(file, proto)))
5415 goto finally;
5417 if (dump(pickler, ob) < 0)
5418 goto finally;
5420 Py_INCREF(Py_None);
5421 res = Py_None;
5423 finally:
5424 Py_XDECREF(pickler);
5426 return res;
5430 /* dumps(obj, protocol=0). */
5431 static PyObject *
5432 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5434 static char *kwlist[] = {"obj", "protocol", NULL};
5435 PyObject *ob, *file = 0, *res = NULL;
5436 Picklerobject *pickler = 0;
5437 int proto = 0;
5439 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5440 &ob, &proto)))
5441 goto finally;
5443 if (!( file = PycStringIO->NewOutput(128)))
5444 goto finally;
5446 if (!( pickler = newPicklerobject(file, proto)))
5447 goto finally;
5449 if (dump(pickler, ob) < 0)
5450 goto finally;
5452 res = PycStringIO->cgetvalue(file);
5454 finally:
5455 Py_XDECREF(pickler);
5456 Py_XDECREF(file);
5458 return res;
5462 /* load(fileobj). */
5463 static PyObject *
5464 cpm_load(PyObject *self, PyObject *ob)
5466 Unpicklerobject *unpickler = 0;
5467 PyObject *res = NULL;
5469 if (!( unpickler = newUnpicklerobject(ob)))
5470 goto finally;
5472 res = load(unpickler);
5474 finally:
5475 Py_XDECREF(unpickler);
5477 return res;
5481 /* loads(string) */
5482 static PyObject *
5483 cpm_loads(PyObject *self, PyObject *args)
5485 PyObject *ob, *file = 0, *res = NULL;
5486 Unpicklerobject *unpickler = 0;
5488 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5489 goto finally;
5491 if (!( file = PycStringIO->NewInput(ob)))
5492 goto finally;
5494 if (!( unpickler = newUnpicklerobject(file)))
5495 goto finally;
5497 res = load(unpickler);
5499 finally:
5500 Py_XDECREF(file);
5501 Py_XDECREF(unpickler);
5503 return res;
5507 PyDoc_STRVAR(Unpicklertype__doc__,
5508 "Objects that know how to unpickle");
5510 static PyTypeObject Unpicklertype = {
5511 PyVarObject_HEAD_INIT(NULL, 0)
5512 "cPickle.Unpickler", /*tp_name*/
5513 sizeof(Unpicklerobject), /*tp_basicsize*/
5515 (destructor)Unpickler_dealloc, /* tp_dealloc */
5516 0, /* tp_print */
5517 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5518 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5519 0, /* tp_compare */
5520 0, /* tp_repr */
5521 0, /* tp_as_number */
5522 0, /* tp_as_sequence */
5523 0, /* tp_as_mapping */
5524 0, /* tp_hash */
5525 0, /* tp_call */
5526 0, /* tp_str */
5527 0, /* tp_getattro */
5528 0, /* tp_setattro */
5529 0, /* tp_as_buffer */
5530 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5531 Unpicklertype__doc__, /* tp_doc */
5532 (traverseproc)Unpickler_traverse, /* tp_traverse */
5533 (inquiry)Unpickler_clear, /* tp_clear */
5536 static struct PyMethodDef cPickle_methods[] = {
5537 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5538 PyDoc_STR("dump(obj, file, protocol=0) -- "
5539 "Write an object in pickle format to the given file.\n"
5540 "\n"
5541 "See the Pickler docstring for the meaning of optional argument proto.")
5544 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5545 PyDoc_STR("dumps(obj, protocol=0) -- "
5546 "Return a string containing an object in pickle format.\n"
5547 "\n"
5548 "See the Pickler docstring for the meaning of optional argument proto.")
5551 {"load", (PyCFunction)cpm_load, METH_O,
5552 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5554 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
5555 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5557 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5558 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5559 "\n"
5560 "This takes a file-like object for writing a pickle data stream.\n"
5561 "The optional proto argument tells the pickler to use the given\n"
5562 "protocol; supported protocols are 0, 1, 2. The default\n"
5563 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5564 "only protocol that can be written to a file opened in text\n"
5565 "mode and read back successfully. When using a protocol higher\n"
5566 "than 0, make sure the file is opened in binary mode, both when\n"
5567 "pickling and unpickling.)\n"
5568 "\n"
5569 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5570 "more efficient than protocol 1.\n"
5571 "\n"
5572 "Specifying a negative protocol version selects the highest\n"
5573 "protocol version supported. The higher the protocol used, the\n"
5574 "more recent the version of Python needed to read the pickle\n"
5575 "produced.\n"
5576 "\n"
5577 "The file parameter must have a write() method that accepts a single\n"
5578 "string argument. It can thus be an open file object, a StringIO\n"
5579 "object, or any other custom object that meets this interface.\n")
5582 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
5583 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5585 { NULL, NULL }
5588 static int
5589 init_stuff(PyObject *module_dict)
5591 PyObject *copyreg, *t, *r;
5593 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5595 if (PyType_Ready(&Unpicklertype) < 0)
5596 return -1;
5597 if (PyType_Ready(&Picklertype) < 0)
5598 return -1;
5600 INIT_STR(__class__);
5601 INIT_STR(__getinitargs__);
5602 INIT_STR(__dict__);
5603 INIT_STR(__getstate__);
5604 INIT_STR(__setstate__);
5605 INIT_STR(__name__);
5606 INIT_STR(__main__);
5607 INIT_STR(__reduce__);
5608 INIT_STR(__reduce_ex__);
5609 INIT_STR(write);
5610 INIT_STR(append);
5611 INIT_STR(read);
5612 INIT_STR(readline);
5613 INIT_STR(copyreg);
5614 INIT_STR(dispatch_table);
5616 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5617 return -1;
5619 /* This is special because we want to use a different
5620 one in restricted mode. */
5621 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5622 if (!dispatch_table) return -1;
5624 extension_registry = PyObject_GetAttrString(copyreg,
5625 "_extension_registry");
5626 if (!extension_registry) return -1;
5628 inverted_registry = PyObject_GetAttrString(copyreg,
5629 "_inverted_registry");
5630 if (!inverted_registry) return -1;
5632 extension_cache = PyObject_GetAttrString(copyreg,
5633 "_extension_cache");
5634 if (!extension_cache) return -1;
5636 Py_DECREF(copyreg);
5638 if (!(empty_tuple = PyTuple_New(0)))
5639 return -1;
5641 two_tuple = PyTuple_New(2);
5642 if (two_tuple == NULL)
5643 return -1;
5644 /* We use this temp container with no regard to refcounts, or to
5645 * keeping containees alive. Exempt from GC, because we don't
5646 * want anything looking at two_tuple() by magic.
5648 PyObject_GC_UnTrack(two_tuple);
5650 /* Ugh */
5651 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5652 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5653 return -1;
5655 if (!( t=PyDict_New())) return -1;
5656 if (!( r=PyRun_String(
5657 "def __str__(self):\n"
5658 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5659 Py_file_input,
5660 module_dict, t) )) return -1;
5661 Py_DECREF(r);
5663 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5664 if (!PickleError)
5665 return -1;
5667 Py_DECREF(t);
5669 PicklingError = PyErr_NewException("cPickle.PicklingError",
5670 PickleError, NULL);
5671 if (!PicklingError)
5672 return -1;
5674 if (!( t=PyDict_New())) return -1;
5675 if (!( r=PyRun_String(
5676 "def __str__(self):\n"
5677 " a=self.args\n"
5678 " a=a and type(a[0]) or '(what)'\n"
5679 " return 'Cannot pickle %s objects' % a\n"
5680 , Py_file_input,
5681 module_dict, t) )) return -1;
5682 Py_DECREF(r);
5684 if (!( UnpickleableError = PyErr_NewException(
5685 "cPickle.UnpickleableError", PicklingError, t)))
5686 return -1;
5688 Py_DECREF(t);
5690 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5691 PickleError, NULL)))
5692 return -1;
5694 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5695 UnpicklingError, NULL)))
5696 return -1;
5698 if (PyDict_SetItemString(module_dict, "PickleError",
5699 PickleError) < 0)
5700 return -1;
5702 if (PyDict_SetItemString(module_dict, "PicklingError",
5703 PicklingError) < 0)
5704 return -1;
5706 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5707 UnpicklingError) < 0)
5708 return -1;
5710 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5711 UnpickleableError) < 0)
5712 return -1;
5714 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5715 BadPickleGet) < 0)
5716 return -1;
5718 PycString_IMPORT;
5720 return 0;
5723 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5724 #define PyMODINIT_FUNC void
5725 #endif
5726 PyMODINIT_FUNC
5727 initcPickle(void)
5729 PyObject *m, *d, *di, *v, *k;
5730 Py_ssize_t i;
5731 char *rev = "1.71"; /* XXX when does this change? */
5732 PyObject *format_version;
5733 PyObject *compatible_formats;
5735 /* XXX: Should mention that the pickle module will include the C
5736 XXX: optimized implementation automatically. */
5737 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5738 "Python 3.0", 2) < 0)
5739 return;
5741 Py_TYPE(&Picklertype) = &PyType_Type;
5742 Py_TYPE(&Unpicklertype) = &PyType_Type;
5743 Py_TYPE(&PdataType) = &PyType_Type;
5745 /* Initialize some pieces. We need to do this before module creation,
5746 * so we're forced to use a temporary dictionary. :(
5748 di = PyDict_New();
5749 if (!di) return;
5750 if (init_stuff(di) < 0) return;
5752 /* Create the module and add the functions */
5753 m = Py_InitModule4("cPickle", cPickle_methods,
5754 cPickle_module_documentation,
5755 (PyObject*)NULL,PYTHON_API_VERSION);
5756 if (m == NULL)
5757 return;
5759 /* Add some symbolic constants to the module */
5760 d = PyModule_GetDict(m);
5761 v = PyString_FromString(rev);
5762 PyDict_SetItemString(d, "__version__", v);
5763 Py_XDECREF(v);
5765 /* Copy data from di. Waaa. */
5766 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5767 if (PyObject_SetItem(d, k, v) < 0) {
5768 Py_DECREF(di);
5769 return;
5772 Py_DECREF(di);
5774 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5775 if (i < 0)
5776 return;
5778 /* These are purely informational; no code uses them. */
5779 /* File format version we write. */
5780 format_version = PyString_FromString("2.0");
5781 /* Format versions we can read. */
5782 compatible_formats = Py_BuildValue("[sssss]",
5783 "1.0", /* Original protocol 0 */
5784 "1.1", /* Protocol 0 + INST */
5785 "1.2", /* Original protocol 1 */
5786 "1.3", /* Protocol 1 + BINFLOAT */
5787 "2.0"); /* Original protocol 2 */
5788 PyDict_SetItemString(d, "format_version", format_version);
5789 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5790 Py_XDECREF(format_version);
5791 Py_XDECREF(compatible_formats);