Added updates with respect to recent changes to TimedRotatingFileHandler.
[python.git] / Modules / cPickle.c
blob2a05c0645e4e57057b382c36ad5ec274ce2906f9
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 *copy_reg_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 nesting;
343 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
344 char *write_buf;
345 int buf_size;
346 PyObject *dispatch_table;
347 int fast_container; /* count nested container dumps */
348 PyObject *fast_memo;
349 } Picklerobject;
351 #ifndef PY_CPICKLE_FAST_LIMIT
352 #define PY_CPICKLE_FAST_LIMIT 50
353 #endif
355 static PyTypeObject Picklertype;
357 typedef struct Unpicklerobject {
358 PyObject_HEAD
359 FILE *fp;
360 PyObject *file;
361 PyObject *readline;
362 PyObject *read;
363 PyObject *memo;
364 PyObject *arg;
365 Pdata *stack;
366 PyObject *mark;
367 PyObject *pers_func;
368 PyObject *last_string;
369 int *marks;
370 int num_marks;
371 int marks_size;
372 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
373 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
374 int buf_size;
375 char *buf;
376 PyObject *find_class;
377 } Unpicklerobject;
379 static PyTypeObject Unpicklertype;
381 /* Forward decls that need the above structs */
382 static int save(Picklerobject *, PyObject *, int);
383 static int put2(Picklerobject *, PyObject *);
385 static
386 PyObject *
387 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
389 va_list va;
390 PyObject *args=0, *retval=0;
391 va_start(va, format);
393 if (format) args = Py_VaBuildValue(format, va);
394 va_end(va);
395 if (format && ! args) return NULL;
396 if (stringformat && !(retval=PyString_FromString(stringformat)))
397 return NULL;
399 if (retval) {
400 if (args) {
401 PyObject *v;
402 v=PyString_Format(retval, args);
403 Py_DECREF(retval);
404 Py_DECREF(args);
405 if (! v) return NULL;
406 retval=v;
409 else
410 if (args) retval=args;
411 else {
412 PyErr_SetObject(ErrType,Py_None);
413 return NULL;
415 PyErr_SetObject(ErrType,retval);
416 Py_DECREF(retval);
417 return NULL;
420 static int
421 write_file(Picklerobject *self, const char *s, Py_ssize_t n)
423 size_t nbyteswritten;
425 if (s == NULL) {
426 return 0;
429 if (n > INT_MAX) {
430 /* String too large */
431 return -1;
434 Py_BEGIN_ALLOW_THREADS
435 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
436 Py_END_ALLOW_THREADS
437 if (nbyteswritten != (size_t)n) {
438 PyErr_SetFromErrno(PyExc_IOError);
439 return -1;
442 return (int)n;
445 static int
446 write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
448 if (s == NULL) {
449 return 0;
452 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
453 return -1;
456 return (int)n;
459 static int
460 write_none(Picklerobject *self, const char *s, Py_ssize_t n)
462 if (s == NULL) return 0;
463 if (n > INT_MAX) return -1;
464 return (int)n;
467 static int
468 write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
470 PyObject *py_str = 0, *junk = 0;
471 int n;
473 if (_n > INT_MAX)
474 return -1;
475 n = (int)_n;
476 if (s == NULL) {
477 if (!( self->buf_size )) return 0;
478 py_str = PyString_FromStringAndSize(self->write_buf,
479 self->buf_size);
480 if (!py_str)
481 return -1;
483 else {
484 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
485 if (write_other(self, NULL, 0) < 0)
486 return -1;
489 if (n > WRITE_BUF_SIZE) {
490 if (!( py_str =
491 PyString_FromStringAndSize(s, n)))
492 return -1;
494 else {
495 memcpy(self->write_buf + self->buf_size, s, n);
496 self->buf_size += n;
497 return n;
501 if (self->write) {
502 /* object with write method */
503 ARG_TUP(self, py_str);
504 if (self->arg) {
505 junk = PyObject_Call(self->write, self->arg, NULL);
506 FREE_ARG_TUP(self);
508 if (junk) Py_DECREF(junk);
509 else return -1;
511 else
512 PDATA_PUSH(self->file, py_str, -1);
514 self->buf_size = 0;
515 return n;
519 static Py_ssize_t
520 read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
522 size_t nbytesread;
524 if (self->buf_size == 0) {
525 int size;
527 size = ((n < 32) ? 32 : n);
528 if (!( self->buf = (char *)malloc(size))) {
529 PyErr_NoMemory();
530 return -1;
533 self->buf_size = size;
535 else if (n > self->buf_size) {
536 char *newbuf = (char *)realloc(self->buf, n);
537 if (!newbuf) {
538 PyErr_NoMemory();
539 return -1;
541 self->buf = newbuf;
542 self->buf_size = n;
545 Py_BEGIN_ALLOW_THREADS
546 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
547 Py_END_ALLOW_THREADS
548 if (nbytesread != (size_t)n) {
549 if (feof(self->fp)) {
550 PyErr_SetNone(PyExc_EOFError);
551 return -1;
554 PyErr_SetFromErrno(PyExc_IOError);
555 return -1;
558 *s = self->buf;
560 return n;
564 static Py_ssize_t
565 readline_file(Unpicklerobject *self, char **s)
567 int i;
569 if (self->buf_size == 0) {
570 if (!( self->buf = (char *)malloc(40))) {
571 PyErr_NoMemory();
572 return -1;
574 self->buf_size = 40;
577 i = 0;
578 while (1) {
579 int bigger;
580 char *newbuf;
581 for (; i < (self->buf_size - 1); i++) {
582 if (feof(self->fp) ||
583 (self->buf[i] = getc(self->fp)) == '\n') {
584 self->buf[i + 1] = '\0';
585 *s = self->buf;
586 return i + 1;
589 bigger = self->buf_size << 1;
590 if (bigger <= 0) { /* overflow */
591 PyErr_NoMemory();
592 return -1;
594 newbuf = (char *)realloc(self->buf, bigger);
595 if (!newbuf) {
596 PyErr_NoMemory();
597 return -1;
599 self->buf = newbuf;
600 self->buf_size = bigger;
605 static Py_ssize_t
606 read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
608 char *ptr;
610 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
611 PyErr_SetNone(PyExc_EOFError);
612 return -1;
615 *s = ptr;
617 return n;
621 static Py_ssize_t
622 readline_cStringIO(Unpicklerobject *self, char **s)
624 Py_ssize_t n;
625 char *ptr;
627 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
628 return -1;
631 *s = ptr;
633 return n;
637 static Py_ssize_t
638 read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
640 PyObject *bytes, *str=0;
642 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
644 ARG_TUP(self, bytes);
645 if (self->arg) {
646 str = PyObject_Call(self->read, self->arg, NULL);
647 FREE_ARG_TUP(self);
649 if (! str) return -1;
651 Py_XDECREF(self->last_string);
652 self->last_string = str;
654 if (! (*s = PyString_AsString(str))) return -1;
655 return n;
659 static Py_ssize_t
660 readline_other(Unpicklerobject *self, char **s)
662 PyObject *str;
663 Py_ssize_t str_size;
665 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
666 return -1;
669 if ((str_size = PyString_Size(str)) < 0)
670 return -1;
672 Py_XDECREF(self->last_string);
673 self->last_string = str;
675 if (! (*s = PyString_AsString(str)))
676 return -1;
678 return str_size;
681 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
682 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
683 * The caller is responsible for free()'ing the return value.
685 static char *
686 pystrndup(const char *s, int n)
688 char *r = (char *)malloc(n+1);
689 if (r == NULL)
690 return (char*)PyErr_NoMemory();
691 memcpy(r, s, n);
692 r[n] = 0;
693 return r;
697 static int
698 get(Picklerobject *self, PyObject *id)
700 PyObject *value, *mv;
701 long c_value;
702 char s[30];
703 size_t len;
705 if (!( mv = PyDict_GetItem(self->memo, id))) {
706 PyErr_SetObject(PyExc_KeyError, id);
707 return -1;
710 if (!( value = PyTuple_GetItem(mv, 0)))
711 return -1;
713 if (!( PyInt_Check(value))) {
714 PyErr_SetString(PicklingError, "no int where int expected in memo");
715 return -1;
717 c_value = PyInt_AS_LONG((PyIntObject*)value);
719 if (!self->bin) {
720 s[0] = GET;
721 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
722 len = strlen(s);
724 else if (Pdata_Check(self->file)) {
725 if (write_other(self, NULL, 0) < 0) return -1;
726 PDATA_APPEND(self->file, mv, -1);
727 return 0;
729 else {
730 if (c_value < 256) {
731 s[0] = BINGET;
732 s[1] = (int)(c_value & 0xff);
733 len = 2;
735 else {
736 s[0] = LONG_BINGET;
737 s[1] = (int)(c_value & 0xff);
738 s[2] = (int)((c_value >> 8) & 0xff);
739 s[3] = (int)((c_value >> 16) & 0xff);
740 s[4] = (int)((c_value >> 24) & 0xff);
741 len = 5;
745 if (self->write_func(self, s, len) < 0)
746 return -1;
748 return 0;
752 static int
753 put(Picklerobject *self, PyObject *ob)
755 if (Py_REFCNT(ob) < 2 || self->fast)
756 return 0;
758 return put2(self, ob);
762 static int
763 put2(Picklerobject *self, PyObject *ob)
765 char c_str[30];
766 int p;
767 size_t len;
768 int res = -1;
769 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
771 if (self->fast)
772 return 0;
774 if ((p = PyDict_Size(self->memo)) < 0)
775 goto finally;
777 /* Make sure memo keys are positive! */
778 /* XXX Why?
779 * XXX And does "positive" really mean non-negative?
780 * XXX pickle.py starts with PUT index 0, not 1. This makes for
781 * XXX gratuitous differences between the pickling modules.
783 p++;
785 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
786 goto finally;
788 if (!( memo_len = PyInt_FromLong(p)))
789 goto finally;
791 if (!( t = PyTuple_New(2)))
792 goto finally;
794 PyTuple_SET_ITEM(t, 0, memo_len);
795 Py_INCREF(memo_len);
796 PyTuple_SET_ITEM(t, 1, ob);
797 Py_INCREF(ob);
799 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
800 goto finally;
802 if (!self->bin) {
803 c_str[0] = PUT;
804 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
805 len = strlen(c_str);
807 else if (Pdata_Check(self->file)) {
808 if (write_other(self, NULL, 0) < 0) return -1;
809 PDATA_APPEND(self->file, memo_len, -1);
810 res=0; /* Job well done ;) */
811 goto finally;
813 else {
814 if (p >= 256) {
815 c_str[0] = LONG_BINPUT;
816 c_str[1] = (int)(p & 0xff);
817 c_str[2] = (int)((p >> 8) & 0xff);
818 c_str[3] = (int)((p >> 16) & 0xff);
819 c_str[4] = (int)((p >> 24) & 0xff);
820 len = 5;
822 else {
823 c_str[0] = BINPUT;
824 c_str[1] = p;
825 len = 2;
829 if (self->write_func(self, c_str, len) < 0)
830 goto finally;
832 res = 0;
834 finally:
835 Py_XDECREF(py_ob_id);
836 Py_XDECREF(memo_len);
837 Py_XDECREF(t);
839 return res;
842 static PyObject *
843 whichmodule(PyObject *global, PyObject *global_name)
845 Py_ssize_t i, j;
846 PyObject *module = 0, *modules_dict = 0,
847 *global_name_attr = 0, *name = 0;
849 module = PyObject_GetAttrString(global, "__module__");
850 if (module)
851 return module;
852 if (PyErr_ExceptionMatches(PyExc_AttributeError))
853 PyErr_Clear();
854 else
855 return NULL;
857 if (!( modules_dict = PySys_GetObject("modules")))
858 return NULL;
860 i = 0;
861 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
863 if (PyObject_Compare(name, __main___str)==0) continue;
865 global_name_attr = PyObject_GetAttr(module, global_name);
866 if (!global_name_attr) {
867 if (PyErr_ExceptionMatches(PyExc_AttributeError))
868 PyErr_Clear();
869 else
870 return NULL;
871 continue;
874 if (global_name_attr != global) {
875 Py_DECREF(global_name_attr);
876 continue;
879 Py_DECREF(global_name_attr);
881 break;
884 /* The following implements the rule in pickle.py added in 1.5
885 that used __main__ if no module is found. I don't actually
886 like this rule. jlf
888 if (!j) {
889 j=1;
890 name=__main___str;
893 Py_INCREF(name);
894 return name;
898 static int
899 fast_save_enter(Picklerobject *self, PyObject *obj)
901 /* if fast_container < 0, we're doing an error exit. */
902 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
903 PyObject *key = NULL;
904 if (self->fast_memo == NULL) {
905 self->fast_memo = PyDict_New();
906 if (self->fast_memo == NULL) {
907 self->fast_container = -1;
908 return 0;
911 key = PyLong_FromVoidPtr(obj);
912 if (key == NULL)
913 return 0;
914 if (PyDict_GetItem(self->fast_memo, key)) {
915 Py_DECREF(key);
916 PyErr_Format(PyExc_ValueError,
917 "fast mode: can't pickle cyclic objects "
918 "including object type %s at %p",
919 Py_TYPE(obj)->tp_name, obj);
920 self->fast_container = -1;
921 return 0;
923 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
924 Py_DECREF(key);
925 self->fast_container = -1;
926 return 0;
928 Py_DECREF(key);
930 return 1;
934 fast_save_leave(Picklerobject *self, PyObject *obj)
936 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
937 PyObject *key = PyLong_FromVoidPtr(obj);
938 if (key == NULL)
939 return 0;
940 if (PyDict_DelItem(self->fast_memo, key) < 0) {
941 Py_DECREF(key);
942 return 0;
944 Py_DECREF(key);
946 return 1;
949 static int
950 save_none(Picklerobject *self, PyObject *args)
952 static char none = NONE;
953 if (self->write_func(self, &none, 1) < 0)
954 return -1;
956 return 0;
959 static int
960 save_bool(Picklerobject *self, PyObject *args)
962 static const char *buf[2] = {FALSE, TRUE};
963 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
964 long l = PyInt_AS_LONG((PyIntObject *)args);
966 if (self->proto >= 2) {
967 char opcode = l ? NEWTRUE : NEWFALSE;
968 if (self->write_func(self, &opcode, 1) < 0)
969 return -1;
971 else if (self->write_func(self, buf[l], len[l]) < 0)
972 return -1;
973 return 0;
976 static int
977 save_int(Picklerobject *self, PyObject *args)
979 char c_str[32];
980 long l = PyInt_AS_LONG((PyIntObject *)args);
981 int len = 0;
983 if (!self->bin
984 #if SIZEOF_LONG > 4
985 || l > 0x7fffffffL
986 || l < -0x80000000L
987 #endif
989 /* Text-mode pickle, or long too big to fit in the 4-byte
990 * signed BININT format: store as a string.
992 c_str[0] = INT;
993 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
994 if (self->write_func(self, c_str, strlen(c_str)) < 0)
995 return -1;
997 else {
998 /* Binary pickle and l fits in a signed 4-byte int. */
999 c_str[1] = (int)( l & 0xff);
1000 c_str[2] = (int)((l >> 8) & 0xff);
1001 c_str[3] = (int)((l >> 16) & 0xff);
1002 c_str[4] = (int)((l >> 24) & 0xff);
1004 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1005 if (c_str[2] == 0) {
1006 c_str[0] = BININT1;
1007 len = 2;
1009 else {
1010 c_str[0] = BININT2;
1011 len = 3;
1014 else {
1015 c_str[0] = BININT;
1016 len = 5;
1019 if (self->write_func(self, c_str, len) < 0)
1020 return -1;
1023 return 0;
1027 static int
1028 save_long(Picklerobject *self, PyObject *args)
1030 Py_ssize_t size;
1031 int res = -1;
1032 PyObject *repr = NULL;
1034 static char l = LONG;
1036 if (self->proto >= 2) {
1037 /* Linear-time pickling. */
1038 size_t nbits;
1039 size_t nbytes;
1040 unsigned char *pdata;
1041 char c_str[5];
1042 int i;
1043 int sign = _PyLong_Sign(args);
1045 if (sign == 0) {
1046 /* It's 0 -- an empty bytestring. */
1047 c_str[0] = LONG1;
1048 c_str[1] = 0;
1049 i = self->write_func(self, c_str, 2);
1050 if (i < 0) goto finally;
1051 res = 0;
1052 goto finally;
1054 nbits = _PyLong_NumBits(args);
1055 if (nbits == (size_t)-1 && PyErr_Occurred())
1056 goto finally;
1057 /* How many bytes do we need? There are nbits >> 3 full
1058 * bytes of data, and nbits & 7 leftover bits. If there
1059 * are any leftover bits, then we clearly need another
1060 * byte. Wnat's not so obvious is that we *probably*
1061 * need another byte even if there aren't any leftovers:
1062 * the most-significant bit of the most-significant byte
1063 * acts like a sign bit, and it's usually got a sense
1064 * opposite of the one we need. The exception is longs
1065 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1066 * its own 256's-complement, so has the right sign bit
1067 * even without the extra byte. That's a pain to check
1068 * for in advance, though, so we always grab an extra
1069 * byte at the start, and cut it back later if possible.
1071 nbytes = (nbits >> 3) + 1;
1072 if (nbytes > INT_MAX) {
1073 PyErr_SetString(PyExc_OverflowError, "long too large "
1074 "to pickle");
1075 goto finally;
1077 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1078 if (repr == NULL) goto finally;
1079 pdata = (unsigned char *)PyString_AS_STRING(repr);
1080 i = _PyLong_AsByteArray((PyLongObject *)args,
1081 pdata, nbytes,
1082 1 /* little endian */, 1 /* signed */);
1083 if (i < 0) goto finally;
1084 /* If the long is negative, this may be a byte more than
1085 * needed. This is so iff the MSB is all redundant sign
1086 * bits.
1088 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1089 (pdata[nbytes - 2] & 0x80) != 0)
1090 --nbytes;
1092 if (nbytes < 256) {
1093 c_str[0] = LONG1;
1094 c_str[1] = (char)nbytes;
1095 size = 2;
1097 else {
1098 c_str[0] = LONG4;
1099 size = (int)nbytes;
1100 for (i = 1; i < 5; i++) {
1101 c_str[i] = (char)(size & 0xff);
1102 size >>= 8;
1104 size = 5;
1106 i = self->write_func(self, c_str, size);
1107 if (i < 0) goto finally;
1108 i = self->write_func(self, (char *)pdata, (int)nbytes);
1109 if (i < 0) goto finally;
1110 res = 0;
1111 goto finally;
1114 /* proto < 2: write the repr and newline. This is quadratic-time
1115 * (in the number of digits), in both directions.
1117 if (!( repr = PyObject_Repr(args)))
1118 goto finally;
1120 if ((size = PyString_Size(repr)) < 0)
1121 goto finally;
1123 if (self->write_func(self, &l, 1) < 0)
1124 goto finally;
1126 if (self->write_func(self,
1127 PyString_AS_STRING((PyStringObject *)repr),
1128 size) < 0)
1129 goto finally;
1131 if (self->write_func(self, "\n", 1) < 0)
1132 goto finally;
1134 res = 0;
1136 finally:
1137 Py_XDECREF(repr);
1138 return res;
1142 static int
1143 save_float(Picklerobject *self, PyObject *args)
1145 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1147 if (self->bin) {
1148 char str[9];
1149 str[0] = BINFLOAT;
1150 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1151 return -1;
1152 if (self->write_func(self, str, 9) < 0)
1153 return -1;
1155 else {
1156 char c_str[250];
1157 c_str[0] = FLOAT;
1158 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1159 /* Extend the formatted string with a newline character */
1160 strcat(c_str, "\n");
1162 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1163 return -1;
1166 return 0;
1170 static int
1171 save_string(Picklerobject *self, PyObject *args, int doput)
1173 int size, len;
1174 PyObject *repr=0;
1176 if ((size = PyString_Size(args)) < 0)
1177 return -1;
1179 if (!self->bin) {
1180 char *repr_str;
1182 static char string = STRING;
1184 if (!( repr = PyObject_Repr(args)))
1185 return -1;
1187 if ((len = PyString_Size(repr)) < 0)
1188 goto err;
1189 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1191 if (self->write_func(self, &string, 1) < 0)
1192 goto err;
1194 if (self->write_func(self, repr_str, len) < 0)
1195 goto err;
1197 if (self->write_func(self, "\n", 1) < 0)
1198 goto err;
1200 Py_XDECREF(repr);
1202 else {
1203 int i;
1204 char c_str[5];
1206 if ((size = PyString_Size(args)) < 0)
1207 return -1;
1209 if (size < 256) {
1210 c_str[0] = SHORT_BINSTRING;
1211 c_str[1] = size;
1212 len = 2;
1214 else if (size <= INT_MAX) {
1215 c_str[0] = BINSTRING;
1216 for (i = 1; i < 5; i++)
1217 c_str[i] = (int)(size >> ((i - 1) * 8));
1218 len = 5;
1220 else
1221 return -1; /* string too large */
1223 if (self->write_func(self, c_str, len) < 0)
1224 return -1;
1226 if (size > 128 && Pdata_Check(self->file)) {
1227 if (write_other(self, NULL, 0) < 0) return -1;
1228 PDATA_APPEND(self->file, args, -1);
1230 else {
1231 if (self->write_func(self,
1232 PyString_AS_STRING(
1233 (PyStringObject *)args),
1234 size) < 0)
1235 return -1;
1239 if (doput)
1240 if (put(self, args) < 0)
1241 return -1;
1243 return 0;
1245 err:
1246 Py_XDECREF(repr);
1247 return -1;
1251 #ifdef Py_USING_UNICODE
1252 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1253 backslash and newline characters to \uXXXX escapes. */
1254 static PyObject *
1255 modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1257 PyObject *repr;
1258 char *p;
1259 char *q;
1261 static const char *hexdigit = "0123456789ABCDEF";
1263 repr = PyString_FromStringAndSize(NULL, 6 * size);
1264 if (repr == NULL)
1265 return NULL;
1266 if (size == 0)
1267 return repr;
1269 p = q = PyString_AS_STRING(repr);
1270 while (size-- > 0) {
1271 Py_UNICODE ch = *s++;
1272 /* Map 16-bit characters to '\uxxxx' */
1273 if (ch >= 256 || ch == '\\' || ch == '\n') {
1274 *p++ = '\\';
1275 *p++ = 'u';
1276 *p++ = hexdigit[(ch >> 12) & 0xf];
1277 *p++ = hexdigit[(ch >> 8) & 0xf];
1278 *p++ = hexdigit[(ch >> 4) & 0xf];
1279 *p++ = hexdigit[ch & 15];
1281 /* Copy everything else as-is */
1282 else
1283 *p++ = (char) ch;
1285 *p = '\0';
1286 _PyString_Resize(&repr, p - q);
1287 return repr;
1291 static int
1292 save_unicode(Picklerobject *self, PyObject *args, int doput)
1294 Py_ssize_t size, len;
1295 PyObject *repr=0;
1297 if (!PyUnicode_Check(args))
1298 return -1;
1300 if (!self->bin) {
1301 char *repr_str;
1302 static char string = UNICODE;
1304 repr = modified_EncodeRawUnicodeEscape(
1305 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1306 if (!repr)
1307 return -1;
1309 if ((len = PyString_Size(repr)) < 0)
1310 goto err;
1311 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1313 if (self->write_func(self, &string, 1) < 0)
1314 goto err;
1316 if (self->write_func(self, repr_str, len) < 0)
1317 goto err;
1319 if (self->write_func(self, "\n", 1) < 0)
1320 goto err;
1322 Py_XDECREF(repr);
1324 else {
1325 int i;
1326 char c_str[5];
1328 if (!( repr = PyUnicode_AsUTF8String(args)))
1329 return -1;
1331 if ((size = PyString_Size(repr)) < 0)
1332 goto err;
1333 if (size > INT_MAX)
1334 return -1; /* string too large */
1336 c_str[0] = BINUNICODE;
1337 for (i = 1; i < 5; i++)
1338 c_str[i] = (int)(size >> ((i - 1) * 8));
1339 len = 5;
1341 if (self->write_func(self, c_str, len) < 0)
1342 goto err;
1344 if (size > 128 && Pdata_Check(self->file)) {
1345 if (write_other(self, NULL, 0) < 0)
1346 goto err;
1347 PDATA_APPEND(self->file, repr, -1);
1349 else {
1350 if (self->write_func(self, PyString_AS_STRING(repr),
1351 size) < 0)
1352 goto err;
1355 Py_DECREF(repr);
1358 if (doput)
1359 if (put(self, args) < 0)
1360 return -1;
1362 return 0;
1364 err:
1365 Py_XDECREF(repr);
1366 return -1;
1368 #endif
1370 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1371 static int
1372 store_tuple_elements(Picklerobject *self, PyObject *t, int len)
1374 int i;
1375 int res = -1; /* guilty until proved innocent */
1377 assert(PyTuple_Size(t) == len);
1379 for (i = 0; i < len; i++) {
1380 PyObject *element = PyTuple_GET_ITEM(t, i);
1382 if (element == NULL)
1383 goto finally;
1384 if (save(self, element, 0) < 0)
1385 goto finally;
1387 res = 0;
1389 finally:
1390 return res;
1393 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1394 * used across protocols to minimize the space needed to pickle them.
1395 * Tuples are also the only builtin immutable type that can be recursive
1396 * (a tuple can be reached from itself), and that requires some subtle
1397 * magic so that it works in all cases. IOW, this is a long routine.
1399 static int
1400 save_tuple(Picklerobject *self, PyObject *args)
1402 PyObject *py_tuple_id = NULL;
1403 int len, i;
1404 int res = -1;
1406 static char tuple = TUPLE;
1407 static char pop = POP;
1408 static char pop_mark = POP_MARK;
1409 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1411 if ((len = PyTuple_Size(args)) < 0)
1412 goto finally;
1414 if (len == 0) {
1415 char c_str[2];
1417 if (self->proto) {
1418 c_str[0] = EMPTY_TUPLE;
1419 len = 1;
1421 else {
1422 c_str[0] = MARK;
1423 c_str[1] = TUPLE;
1424 len = 2;
1426 if (self->write_func(self, c_str, len) >= 0)
1427 res = 0;
1428 /* Don't memoize an empty tuple. */
1429 goto finally;
1432 /* A non-empty tuple. */
1434 /* id(tuple) isn't in the memo now. If it shows up there after
1435 * saving the tuple elements, the tuple must be recursive, in
1436 * which case we'll pop everything we put on the stack, and fetch
1437 * its value from the memo.
1439 py_tuple_id = PyLong_FromVoidPtr(args);
1440 if (py_tuple_id == NULL)
1441 goto finally;
1443 if (len <= 3 && self->proto >= 2) {
1444 /* Use TUPLE{1,2,3} opcodes. */
1445 if (store_tuple_elements(self, args, len) < 0)
1446 goto finally;
1447 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1448 /* pop the len elements */
1449 for (i = 0; i < len; ++i)
1450 if (self->write_func(self, &pop, 1) < 0)
1451 goto finally;
1452 /* fetch from memo */
1453 if (get(self, py_tuple_id) < 0)
1454 goto finally;
1455 res = 0;
1456 goto finally;
1458 /* Not recursive. */
1459 if (self->write_func(self, len2opcode + len, 1) < 0)
1460 goto finally;
1461 goto memoize;
1464 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1465 * Generate MARK elt1 elt2 ... TUPLE
1467 if (self->write_func(self, &MARKv, 1) < 0)
1468 goto finally;
1470 if (store_tuple_elements(self, args, len) < 0)
1471 goto finally;
1473 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1474 /* pop the stack stuff we pushed */
1475 if (self->bin) {
1476 if (self->write_func(self, &pop_mark, 1) < 0)
1477 goto finally;
1479 else {
1480 /* Note that we pop one more than len, to remove
1481 * the MARK too.
1483 for (i = 0; i <= len; i++)
1484 if (self->write_func(self, &pop, 1) < 0)
1485 goto finally;
1487 /* fetch from memo */
1488 if (get(self, py_tuple_id) >= 0)
1489 res = 0;
1490 goto finally;
1493 /* Not recursive. */
1494 if (self->write_func(self, &tuple, 1) < 0)
1495 goto finally;
1497 memoize:
1498 if (put(self, args) >= 0)
1499 res = 0;
1501 finally:
1502 Py_XDECREF(py_tuple_id);
1503 return res;
1506 /* iter is an iterator giving items, and we batch up chunks of
1507 * MARK item item ... item APPENDS
1508 * opcode sequences. Calling code should have arranged to first create an
1509 * empty list, or list-like object, for the APPENDS to operate on.
1510 * Returns 0 on success, <0 on error.
1512 static int
1513 batch_list(Picklerobject *self, PyObject *iter)
1515 PyObject *obj;
1516 PyObject *slice[BATCHSIZE];
1517 int i, n;
1519 static char append = APPEND;
1520 static char appends = APPENDS;
1522 assert(iter != NULL);
1524 if (self->proto == 0) {
1525 /* APPENDS isn't available; do one at a time. */
1526 for (;;) {
1527 obj = PyIter_Next(iter);
1528 if (obj == NULL) {
1529 if (PyErr_Occurred())
1530 return -1;
1531 break;
1533 i = save(self, obj, 0);
1534 Py_DECREF(obj);
1535 if (i < 0)
1536 return -1;
1537 if (self->write_func(self, &append, 1) < 0)
1538 return -1;
1540 return 0;
1543 /* proto > 0: write in batches of BATCHSIZE. */
1544 do {
1545 /* Get next group of (no more than) BATCHSIZE elements. */
1546 for (n = 0; n < BATCHSIZE; ++n) {
1547 obj = PyIter_Next(iter);
1548 if (obj == NULL) {
1549 if (PyErr_Occurred())
1550 goto BatchFailed;
1551 break;
1553 slice[n] = obj;
1556 if (n > 1) {
1557 /* Pump out MARK, slice[0:n], APPENDS. */
1558 if (self->write_func(self, &MARKv, 1) < 0)
1559 goto BatchFailed;
1560 for (i = 0; i < n; ++i) {
1561 if (save(self, slice[i], 0) < 0)
1562 goto BatchFailed;
1564 if (self->write_func(self, &appends, 1) < 0)
1565 goto BatchFailed;
1567 else if (n == 1) {
1568 if (save(self, slice[0], 0) < 0)
1569 goto BatchFailed;
1570 if (self->write_func(self, &append, 1) < 0)
1571 goto BatchFailed;
1574 for (i = 0; i < n; ++i) {
1575 Py_DECREF(slice[i]);
1577 } while (n == BATCHSIZE);
1578 return 0;
1580 BatchFailed:
1581 while (--n >= 0) {
1582 Py_DECREF(slice[n]);
1584 return -1;
1587 static int
1588 save_list(Picklerobject *self, PyObject *args)
1590 int res = -1;
1591 char s[3];
1592 int len;
1593 PyObject *iter;
1595 if (self->fast && !fast_save_enter(self, args))
1596 goto finally;
1598 /* Create an empty list. */
1599 if (self->bin) {
1600 s[0] = EMPTY_LIST;
1601 len = 1;
1603 else {
1604 s[0] = MARK;
1605 s[1] = LIST;
1606 len = 2;
1609 if (self->write_func(self, s, len) < 0)
1610 goto finally;
1612 /* Get list length, and bow out early if empty. */
1613 if ((len = PyList_Size(args)) < 0)
1614 goto finally;
1616 /* Memoize. */
1617 if (len == 0) {
1618 if (put(self, args) >= 0)
1619 res = 0;
1620 goto finally;
1622 if (put2(self, args) < 0)
1623 goto finally;
1625 /* Materialize the list elements. */
1626 iter = PyObject_GetIter(args);
1627 if (iter == NULL)
1628 goto finally;
1629 res = batch_list(self, iter);
1630 Py_DECREF(iter);
1632 finally:
1633 if (self->fast && !fast_save_leave(self, args))
1634 res = -1;
1636 return res;
1640 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1641 * MARK key value ... key value SETITEMS
1642 * opcode sequences. Calling code should have arranged to first create an
1643 * empty dict, or dict-like object, for the SETITEMS to operate on.
1644 * Returns 0 on success, <0 on error.
1646 * This is very much like batch_list(). The difference between saving
1647 * elements directly, and picking apart two-tuples, is so long-winded at
1648 * the C level, though, that attempts to combine these routines were too
1649 * ugly to bear.
1651 static int
1652 batch_dict(Picklerobject *self, PyObject *iter)
1654 PyObject *p;
1655 PyObject *slice[BATCHSIZE];
1656 int i, n;
1658 static char setitem = SETITEM;
1659 static char setitems = SETITEMS;
1661 assert(iter != NULL);
1663 if (self->proto == 0) {
1664 /* SETITEMS isn't available; do one at a time. */
1665 for (;;) {
1666 p = PyIter_Next(iter);
1667 if (p == NULL) {
1668 if (PyErr_Occurred())
1669 return -1;
1670 break;
1672 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1673 PyErr_SetString(PyExc_TypeError, "dict items "
1674 "iterator must return 2-tuples");
1675 return -1;
1677 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1678 if (i >= 0)
1679 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1680 Py_DECREF(p);
1681 if (i < 0)
1682 return -1;
1683 if (self->write_func(self, &setitem, 1) < 0)
1684 return -1;
1686 return 0;
1689 /* proto > 0: write in batches of BATCHSIZE. */
1690 do {
1691 /* Get next group of (no more than) BATCHSIZE elements. */
1692 for (n = 0; n < BATCHSIZE; ++n) {
1693 p = PyIter_Next(iter);
1694 if (p == NULL) {
1695 if (PyErr_Occurred())
1696 goto BatchFailed;
1697 break;
1699 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1700 PyErr_SetString(PyExc_TypeError, "dict items "
1701 "iterator must return 2-tuples");
1702 goto BatchFailed;
1704 slice[n] = p;
1707 if (n > 1) {
1708 /* Pump out MARK, slice[0:n], SETITEMS. */
1709 if (self->write_func(self, &MARKv, 1) < 0)
1710 goto BatchFailed;
1711 for (i = 0; i < n; ++i) {
1712 p = slice[i];
1713 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1714 goto BatchFailed;
1715 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1716 goto BatchFailed;
1718 if (self->write_func(self, &setitems, 1) < 0)
1719 goto BatchFailed;
1721 else if (n == 1) {
1722 p = slice[0];
1723 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1724 goto BatchFailed;
1725 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1726 goto BatchFailed;
1727 if (self->write_func(self, &setitem, 1) < 0)
1728 goto BatchFailed;
1731 for (i = 0; i < n; ++i) {
1732 Py_DECREF(slice[i]);
1734 } while (n == BATCHSIZE);
1735 return 0;
1737 BatchFailed:
1738 while (--n >= 0) {
1739 Py_DECREF(slice[n]);
1741 return -1;
1744 static int
1745 save_dict(Picklerobject *self, PyObject *args)
1747 int res = -1;
1748 char s[3];
1749 int len;
1750 PyObject *iter;
1752 if (self->fast && !fast_save_enter(self, args))
1753 goto finally;
1755 /* Create an empty dict. */
1756 if (self->bin) {
1757 s[0] = EMPTY_DICT;
1758 len = 1;
1760 else {
1761 s[0] = MARK;
1762 s[1] = DICT;
1763 len = 2;
1766 if (self->write_func(self, s, len) < 0)
1767 goto finally;
1769 /* Get dict size, and bow out early if empty. */
1770 if ((len = PyDict_Size(args)) < 0)
1771 goto finally;
1773 if (len == 0) {
1774 if (put(self, args) >= 0)
1775 res = 0;
1776 goto finally;
1778 if (put2(self, args) < 0)
1779 goto finally;
1781 /* Materialize the dict items. */
1782 iter = PyObject_CallMethod(args, "iteritems", "()");
1783 if (iter == NULL)
1784 goto finally;
1785 res = batch_dict(self, iter);
1786 Py_DECREF(iter);
1788 finally:
1789 if (self->fast && !fast_save_leave(self, args))
1790 res = -1;
1792 return res;
1796 static int
1797 save_inst(Picklerobject *self, PyObject *args)
1799 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1800 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1801 char *module_str, *name_str;
1802 int module_size, name_size, res = -1;
1804 static char inst = INST, obj = OBJ, build = BUILD;
1806 if (self->fast && !fast_save_enter(self, args))
1807 goto finally;
1809 if (self->write_func(self, &MARKv, 1) < 0)
1810 goto finally;
1812 if (!( class = PyObject_GetAttr(args, __class___str)))
1813 goto finally;
1815 if (self->bin) {
1816 if (save(self, class, 0) < 0)
1817 goto finally;
1820 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1821 PyObject *element = 0;
1822 int i, len;
1824 if (!( class_args =
1825 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
1826 goto finally;
1828 if ((len = PyObject_Size(class_args)) < 0)
1829 goto finally;
1831 for (i = 0; i < len; i++) {
1832 if (!( element = PySequence_GetItem(class_args, i)))
1833 goto finally;
1835 if (save(self, element, 0) < 0) {
1836 Py_DECREF(element);
1837 goto finally;
1840 Py_DECREF(element);
1843 else {
1844 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1845 PyErr_Clear();
1846 else
1847 goto finally;
1850 if (!self->bin) {
1851 if (!( name = ((PyClassObject *)class)->cl_name )) {
1852 PyErr_SetString(PicklingError, "class has no name");
1853 goto finally;
1856 if (!( module = whichmodule(class, name)))
1857 goto finally;
1860 if ((module_size = PyString_Size(module)) < 0 ||
1861 (name_size = PyString_Size(name)) < 0)
1862 goto finally;
1864 module_str = PyString_AS_STRING((PyStringObject *)module);
1865 name_str = PyString_AS_STRING((PyStringObject *)name);
1867 if (self->write_func(self, &inst, 1) < 0)
1868 goto finally;
1870 if (self->write_func(self, module_str, module_size) < 0)
1871 goto finally;
1873 if (self->write_func(self, "\n", 1) < 0)
1874 goto finally;
1876 if (self->write_func(self, name_str, name_size) < 0)
1877 goto finally;
1879 if (self->write_func(self, "\n", 1) < 0)
1880 goto finally;
1882 else if (self->write_func(self, &obj, 1) < 0) {
1883 goto finally;
1886 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1887 state = PyObject_Call(getstate_func, empty_tuple, NULL);
1888 if (!state)
1889 goto finally;
1891 else {
1892 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1893 PyErr_Clear();
1894 else
1895 goto finally;
1897 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1898 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1899 PyErr_Clear();
1900 else
1901 goto finally;
1902 res = 0;
1903 goto finally;
1907 if (!PyDict_Check(state)) {
1908 if (put2(self, args) < 0)
1909 goto finally;
1911 else {
1912 if (put(self, args) < 0)
1913 goto finally;
1916 if (save(self, state, 0) < 0)
1917 goto finally;
1919 if (self->write_func(self, &build, 1) < 0)
1920 goto finally;
1922 res = 0;
1924 finally:
1925 if (self->fast && !fast_save_leave(self, args))
1926 res = -1;
1928 Py_XDECREF(module);
1929 Py_XDECREF(class);
1930 Py_XDECREF(state);
1931 Py_XDECREF(getinitargs_func);
1932 Py_XDECREF(getstate_func);
1933 Py_XDECREF(class_args);
1935 return res;
1939 static int
1940 save_global(Picklerobject *self, PyObject *args, PyObject *name)
1942 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
1943 char *name_str, *module_str;
1944 int module_size, name_size, res = -1;
1946 static char global = GLOBAL;
1948 if (name) {
1949 global_name = name;
1950 Py_INCREF(global_name);
1952 else {
1953 if (!( global_name = PyObject_GetAttr(args, __name___str)))
1954 goto finally;
1957 if (!( module = whichmodule(args, global_name)))
1958 goto finally;
1960 if ((module_size = PyString_Size(module)) < 0 ||
1961 (name_size = PyString_Size(global_name)) < 0)
1962 goto finally;
1964 module_str = PyString_AS_STRING((PyStringObject *)module);
1965 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1967 /* XXX This can be doing a relative import. Clearly it shouldn't,
1968 but I don't know how to stop it. :-( */
1969 mod = PyImport_ImportModule(module_str);
1970 if (mod == NULL) {
1971 cPickle_ErrFormat(PicklingError,
1972 "Can't pickle %s: import of module %s "
1973 "failed",
1974 "OS", args, module);
1975 goto finally;
1977 klass = PyObject_GetAttrString(mod, name_str);
1978 if (klass == NULL) {
1979 cPickle_ErrFormat(PicklingError,
1980 "Can't pickle %s: attribute lookup %s.%s "
1981 "failed",
1982 "OSS", args, module, global_name);
1983 goto finally;
1985 if (klass != args) {
1986 Py_DECREF(klass);
1987 cPickle_ErrFormat(PicklingError,
1988 "Can't pickle %s: it's not the same object "
1989 "as %s.%s",
1990 "OSS", args, module, global_name);
1991 goto finally;
1993 Py_DECREF(klass);
1995 if (self->proto >= 2) {
1996 /* See whether this is in the extension registry, and if
1997 * so generate an EXT opcode.
1999 PyObject *py_code; /* extension code as Python object */
2000 long code; /* extension code as C value */
2001 char c_str[5];
2002 int n;
2004 PyTuple_SET_ITEM(two_tuple, 0, module);
2005 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2006 py_code = PyDict_GetItem(extension_registry, two_tuple);
2007 if (py_code == NULL)
2008 goto gen_global; /* not registered */
2010 /* Verify py_code has the right type and value. */
2011 if (!PyInt_Check(py_code)) {
2012 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2013 "extension code %s isn't an integer",
2014 "OO", args, py_code);
2015 goto finally;
2017 code = PyInt_AS_LONG(py_code);
2018 if (code <= 0 || code > 0x7fffffffL) {
2019 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2020 "extension code %ld is out of range",
2021 "Ol", args, code);
2022 goto finally;
2025 /* Generate an EXT opcode. */
2026 if (code <= 0xff) {
2027 c_str[0] = EXT1;
2028 c_str[1] = (char)code;
2029 n = 2;
2031 else if (code <= 0xffff) {
2032 c_str[0] = EXT2;
2033 c_str[1] = (char)(code & 0xff);
2034 c_str[2] = (char)((code >> 8) & 0xff);
2035 n = 3;
2037 else {
2038 c_str[0] = EXT4;
2039 c_str[1] = (char)(code & 0xff);
2040 c_str[2] = (char)((code >> 8) & 0xff);
2041 c_str[3] = (char)((code >> 16) & 0xff);
2042 c_str[4] = (char)((code >> 24) & 0xff);
2043 n = 5;
2046 if (self->write_func(self, c_str, n) >= 0)
2047 res = 0;
2048 goto finally; /* and don't memoize */
2051 gen_global:
2052 if (self->write_func(self, &global, 1) < 0)
2053 goto finally;
2055 if (self->write_func(self, module_str, module_size) < 0)
2056 goto finally;
2058 if (self->write_func(self, "\n", 1) < 0)
2059 goto finally;
2061 if (self->write_func(self, name_str, name_size) < 0)
2062 goto finally;
2064 if (self->write_func(self, "\n", 1) < 0)
2065 goto finally;
2067 if (put(self, args) < 0)
2068 goto finally;
2070 res = 0;
2072 finally:
2073 Py_XDECREF(module);
2074 Py_XDECREF(global_name);
2075 Py_XDECREF(mod);
2077 return res;
2080 static int
2081 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2083 PyObject *pid = 0;
2084 int size, res = -1;
2086 static char persid = PERSID, binpersid = BINPERSID;
2088 Py_INCREF(args);
2089 ARG_TUP(self, args);
2090 if (self->arg) {
2091 pid = PyObject_Call(f, self->arg, NULL);
2092 FREE_ARG_TUP(self);
2094 if (! pid) return -1;
2096 if (pid != Py_None) {
2097 if (!self->bin) {
2098 if (!PyString_Check(pid)) {
2099 PyErr_SetString(PicklingError,
2100 "persistent id must be string");
2101 goto finally;
2104 if (self->write_func(self, &persid, 1) < 0)
2105 goto finally;
2107 if ((size = PyString_Size(pid)) < 0)
2108 goto finally;
2110 if (self->write_func(self,
2111 PyString_AS_STRING(
2112 (PyStringObject *)pid),
2113 size) < 0)
2114 goto finally;
2116 if (self->write_func(self, "\n", 1) < 0)
2117 goto finally;
2119 res = 1;
2120 goto finally;
2122 else if (save(self, pid, 1) >= 0) {
2123 if (self->write_func(self, &binpersid, 1) < 0)
2124 res = -1;
2125 else
2126 res = 1;
2129 goto finally;
2132 res = 0;
2134 finally:
2135 Py_XDECREF(pid);
2137 return res;
2140 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2141 * appropriate __reduce__ method for ob.
2143 static int
2144 save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
2146 PyObject *callable;
2147 PyObject *argtup;
2148 PyObject *state = NULL;
2149 PyObject *listitems = NULL;
2150 PyObject *dictitems = NULL;
2152 int use_newobj = self->proto >= 2;
2154 static char reduce = REDUCE;
2155 static char build = BUILD;
2156 static char newobj = NEWOBJ;
2158 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2159 &callable,
2160 &argtup,
2161 &state,
2162 &listitems,
2163 &dictitems))
2164 return -1;
2166 if (!PyTuple_Check(argtup)) {
2167 PyErr_SetString(PicklingError,
2168 "args from reduce() should be a tuple");
2169 return -1;
2172 if (state == Py_None)
2173 state = NULL;
2174 if (listitems == Py_None)
2175 listitems = NULL;
2176 if (dictitems == Py_None)
2177 dictitems = NULL;
2179 /* Protocol 2 special case: if callable's name is __newobj__, use
2180 * NEWOBJ. This consumes a lot of code.
2182 if (use_newobj) {
2183 PyObject *temp = PyObject_GetAttr(callable, __name___str);
2185 if (temp == NULL) {
2186 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2187 PyErr_Clear();
2188 else
2189 return -1;
2190 use_newobj = 0;
2192 else {
2193 use_newobj = PyString_Check(temp) &&
2194 strcmp(PyString_AS_STRING(temp),
2195 "__newobj__") == 0;
2196 Py_DECREF(temp);
2199 if (use_newobj) {
2200 PyObject *cls;
2201 PyObject *newargtup;
2202 int n, i;
2204 /* Sanity checks. */
2205 n = PyTuple_Size(argtup);
2206 if (n < 1) {
2207 PyErr_SetString(PicklingError, "__newobj__ arglist "
2208 "is empty");
2209 return -1;
2212 cls = PyTuple_GET_ITEM(argtup, 0);
2213 if (! PyObject_HasAttrString(cls, "__new__")) {
2214 PyErr_SetString(PicklingError, "args[0] from "
2215 "__newobj__ args has no __new__");
2216 return -1;
2219 /* XXX How could ob be NULL? */
2220 if (ob != NULL) {
2221 PyObject *ob_dot_class;
2223 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2224 if (ob_dot_class == NULL) {
2225 if (PyErr_ExceptionMatches(
2226 PyExc_AttributeError))
2227 PyErr_Clear();
2228 else
2229 return -1;
2231 i = ob_dot_class != cls; /* true iff a problem */
2232 Py_XDECREF(ob_dot_class);
2233 if (i) {
2234 PyErr_SetString(PicklingError, "args[0] from "
2235 "__newobj__ args has the wrong class");
2236 return -1;
2240 /* Save the class and its __new__ arguments. */
2241 if (save(self, cls, 0) < 0)
2242 return -1;
2244 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2245 if (newargtup == NULL)
2246 return -1;
2247 for (i = 1; i < n; ++i) {
2248 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2249 Py_INCREF(temp);
2250 PyTuple_SET_ITEM(newargtup, i-1, temp);
2252 i = save(self, newargtup, 0);
2253 Py_DECREF(newargtup);
2254 if (i < 0)
2255 return -1;
2257 /* Add NEWOBJ opcode. */
2258 if (self->write_func(self, &newobj, 1) < 0)
2259 return -1;
2261 else {
2262 /* Not using NEWOBJ. */
2263 if (save(self, callable, 0) < 0 ||
2264 save(self, argtup, 0) < 0 ||
2265 self->write_func(self, &reduce, 1) < 0)
2266 return -1;
2269 /* Memoize. */
2270 /* XXX How can ob be NULL? */
2271 if (ob != NULL) {
2272 if (state && !PyDict_Check(state)) {
2273 if (put2(self, ob) < 0)
2274 return -1;
2276 else if (put(self, ob) < 0)
2277 return -1;
2281 if (listitems && batch_list(self, listitems) < 0)
2282 return -1;
2284 if (dictitems && batch_dict(self, dictitems) < 0)
2285 return -1;
2287 if (state) {
2288 if (save(self, state, 0) < 0 ||
2289 self->write_func(self, &build, 1) < 0)
2290 return -1;
2293 return 0;
2296 static int
2297 save(Picklerobject *self, PyObject *args, int pers_save)
2299 PyTypeObject *type;
2300 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2301 PyObject *arg_tup;
2302 int res = -1;
2303 int tmp, size;
2305 if (self->nesting++ > Py_GetRecursionLimit()){
2306 PyErr_SetString(PyExc_RuntimeError,
2307 "maximum recursion depth exceeded");
2308 goto finally;
2311 if (!pers_save && self->pers_func) {
2312 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2313 res = tmp;
2314 goto finally;
2318 if (args == Py_None) {
2319 res = save_none(self, args);
2320 goto finally;
2323 type = Py_TYPE(args);
2325 switch (type->tp_name[0]) {
2326 case 'b':
2327 if (args == Py_False || args == Py_True) {
2328 res = save_bool(self, args);
2329 goto finally;
2331 break;
2332 case 'i':
2333 if (type == &PyInt_Type) {
2334 res = save_int(self, args);
2335 goto finally;
2337 break;
2339 case 'l':
2340 if (type == &PyLong_Type) {
2341 res = save_long(self, args);
2342 goto finally;
2344 break;
2346 case 'f':
2347 if (type == &PyFloat_Type) {
2348 res = save_float(self, args);
2349 goto finally;
2351 break;
2353 case 't':
2354 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2355 res = save_tuple(self, args);
2356 goto finally;
2358 break;
2360 case 's':
2361 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2362 res = save_string(self, args, 0);
2363 goto finally;
2366 #ifdef Py_USING_UNICODE
2367 case 'u':
2368 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2369 res = save_unicode(self, args, 0);
2370 goto finally;
2372 #endif
2375 if (Py_REFCNT(args) > 1) {
2376 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2377 goto finally;
2379 if (PyDict_GetItem(self->memo, py_ob_id)) {
2380 if (get(self, py_ob_id) < 0)
2381 goto finally;
2383 res = 0;
2384 goto finally;
2388 switch (type->tp_name[0]) {
2389 case 's':
2390 if (type == &PyString_Type) {
2391 res = save_string(self, args, 1);
2392 goto finally;
2394 break;
2396 #ifdef Py_USING_UNICODE
2397 case 'u':
2398 if (type == &PyUnicode_Type) {
2399 res = save_unicode(self, args, 1);
2400 goto finally;
2402 break;
2403 #endif
2405 case 't':
2406 if (type == &PyTuple_Type) {
2407 res = save_tuple(self, args);
2408 goto finally;
2410 if (type == &PyType_Type) {
2411 res = save_global(self, args, NULL);
2412 goto finally;
2414 break;
2416 case 'l':
2417 if (type == &PyList_Type) {
2418 res = save_list(self, args);
2419 goto finally;
2421 break;
2423 case 'd':
2424 if (type == &PyDict_Type) {
2425 res = save_dict(self, args);
2426 goto finally;
2428 break;
2430 case 'i':
2431 if (type == &PyInstance_Type) {
2432 res = save_inst(self, args);
2433 goto finally;
2435 break;
2437 case 'c':
2438 if (type == &PyClass_Type) {
2439 res = save_global(self, args, NULL);
2440 goto finally;
2442 break;
2444 case 'f':
2445 if (type == &PyFunction_Type) {
2446 res = save_global(self, args, NULL);
2447 if (res && PyErr_ExceptionMatches(PickleError)) {
2448 /* fall back to reduce */
2449 PyErr_Clear();
2450 break;
2452 goto finally;
2454 break;
2456 case 'b':
2457 if (type == &PyCFunction_Type) {
2458 res = save_global(self, args, NULL);
2459 goto finally;
2463 if (!pers_save && self->inst_pers_func) {
2464 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2465 res = tmp;
2466 goto finally;
2470 if (PyType_IsSubtype(type, &PyType_Type)) {
2471 res = save_global(self, args, NULL);
2472 goto finally;
2475 /* Get a reduction callable, and call it. This may come from
2476 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2477 * or the object's __reduce__ method.
2479 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2480 if (__reduce__ != NULL) {
2481 Py_INCREF(__reduce__);
2482 Py_INCREF(args);
2483 ARG_TUP(self, args);
2484 if (self->arg) {
2485 t = PyObject_Call(__reduce__, self->arg, NULL);
2486 FREE_ARG_TUP(self);
2489 else {
2490 /* Check for a __reduce_ex__ method. */
2491 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2492 if (__reduce__ != NULL) {
2493 t = PyInt_FromLong(self->proto);
2494 if (t != NULL) {
2495 ARG_TUP(self, t);
2496 t = NULL;
2497 if (self->arg) {
2498 t = PyObject_Call(__reduce__,
2499 self->arg, NULL);
2500 FREE_ARG_TUP(self);
2504 else {
2505 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2506 PyErr_Clear();
2507 else
2508 goto finally;
2509 /* Check for a __reduce__ method. */
2510 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2511 if (__reduce__ != NULL) {
2512 t = PyObject_Call(__reduce__,
2513 empty_tuple, NULL);
2515 else {
2516 PyErr_SetObject(UnpickleableError, args);
2517 goto finally;
2522 if (t == NULL)
2523 goto finally;
2525 if (PyString_Check(t)) {
2526 res = save_global(self, args, t);
2527 goto finally;
2530 if (! PyTuple_Check(t)) {
2531 cPickle_ErrFormat(PicklingError, "Value returned by "
2532 "%s must be string or tuple",
2533 "O", __reduce__);
2534 goto finally;
2537 size = PyTuple_Size(t);
2538 if (size < 2 || size > 5) {
2539 cPickle_ErrFormat(PicklingError, "tuple returned by "
2540 "%s must contain 2 through 5 elements",
2541 "O", __reduce__);
2542 goto finally;
2545 arg_tup = PyTuple_GET_ITEM(t, 1);
2546 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2547 cPickle_ErrFormat(PicklingError, "Second element of "
2548 "tuple returned by %s must be a tuple",
2549 "O", __reduce__);
2550 goto finally;
2553 res = save_reduce(self, t, args);
2555 finally:
2556 self->nesting--;
2557 Py_XDECREF(py_ob_id);
2558 Py_XDECREF(__reduce__);
2559 Py_XDECREF(t);
2561 return res;
2565 static int
2566 dump(Picklerobject *self, PyObject *args)
2568 static char stop = STOP;
2570 if (self->proto >= 2) {
2571 char bytes[2];
2573 bytes[0] = PROTO;
2574 assert(self->proto >= 0 && self->proto < 256);
2575 bytes[1] = (char)self->proto;
2576 if (self->write_func(self, bytes, 2) < 0)
2577 return -1;
2580 if (save(self, args, 0) < 0)
2581 return -1;
2583 if (self->write_func(self, &stop, 1) < 0)
2584 return -1;
2586 if (self->write_func(self, NULL, 0) < 0)
2587 return -1;
2589 return 0;
2592 static PyObject *
2593 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2595 if (self->memo)
2596 PyDict_Clear(self->memo);
2597 Py_INCREF(Py_None);
2598 return Py_None;
2601 static PyObject *
2602 Pickle_getvalue(Picklerobject *self, PyObject *args)
2604 int l, i, rsize, ssize, clear=1, lm;
2605 long ik;
2606 PyObject *k, *r;
2607 char *s, *p, *have_get;
2608 Pdata *data;
2610 /* Can be called by Python code or C code */
2611 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2612 return NULL;
2614 /* Check to make sure we are based on a list */
2615 if (! Pdata_Check(self->file)) {
2616 PyErr_SetString(PicklingError,
2617 "Attempt to getvalue() a non-list-based pickler");
2618 return NULL;
2621 /* flush write buffer */
2622 if (write_other(self, NULL, 0) < 0) return NULL;
2624 data=(Pdata*)self->file;
2625 l=data->length;
2627 /* set up an array to hold get/put status */
2628 lm = PyDict_Size(self->memo);
2629 if (lm < 0) return NULL;
2630 lm++;
2631 have_get = malloc(lm);
2632 if (have_get == NULL) return PyErr_NoMemory();
2633 memset(have_get, 0, lm);
2635 /* Scan for gets. */
2636 for (rsize = 0, i = l; --i >= 0; ) {
2637 k = data->data[i];
2639 if (PyString_Check(k))
2640 rsize += PyString_GET_SIZE(k);
2642 else if (PyInt_Check(k)) { /* put */
2643 ik = PyInt_AS_LONG((PyIntObject*)k);
2644 if (ik >= lm || ik == 0) {
2645 PyErr_SetString(PicklingError,
2646 "Invalid get data");
2647 goto err;
2649 if (have_get[ik]) /* with matching get */
2650 rsize += ik < 256 ? 2 : 5;
2653 else if (! (PyTuple_Check(k) &&
2654 PyTuple_GET_SIZE(k) == 2 &&
2655 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2657 PyErr_SetString(PicklingError,
2658 "Unexpected data in internal list");
2659 goto err;
2662 else { /* put */
2663 ik = PyInt_AS_LONG((PyIntObject *)k);
2664 if (ik >= lm || ik == 0) {
2665 PyErr_SetString(PicklingError,
2666 "Invalid get data");
2667 return NULL;
2669 have_get[ik] = 1;
2670 rsize += ik < 256 ? 2 : 5;
2674 /* Now generate the result */
2675 r = PyString_FromStringAndSize(NULL, rsize);
2676 if (r == NULL) goto err;
2677 s = PyString_AS_STRING((PyStringObject *)r);
2679 for (i = 0; i < l; i++) {
2680 k = data->data[i];
2682 if (PyString_Check(k)) {
2683 ssize = PyString_GET_SIZE(k);
2684 if (ssize) {
2685 p=PyString_AS_STRING((PyStringObject *)k);
2686 while (--ssize >= 0)
2687 *s++ = *p++;
2691 else if (PyTuple_Check(k)) { /* get */
2692 ik = PyInt_AS_LONG((PyIntObject *)
2693 PyTuple_GET_ITEM(k, 0));
2694 if (ik < 256) {
2695 *s++ = BINGET;
2696 *s++ = (int)(ik & 0xff);
2698 else {
2699 *s++ = LONG_BINGET;
2700 *s++ = (int)(ik & 0xff);
2701 *s++ = (int)((ik >> 8) & 0xff);
2702 *s++ = (int)((ik >> 16) & 0xff);
2703 *s++ = (int)((ik >> 24) & 0xff);
2707 else { /* put */
2708 ik = PyInt_AS_LONG((PyIntObject*)k);
2710 if (have_get[ik]) { /* with matching get */
2711 if (ik < 256) {
2712 *s++ = BINPUT;
2713 *s++ = (int)(ik & 0xff);
2715 else {
2716 *s++ = LONG_BINPUT;
2717 *s++ = (int)(ik & 0xff);
2718 *s++ = (int)((ik >> 8) & 0xff);
2719 *s++ = (int)((ik >> 16) & 0xff);
2720 *s++ = (int)((ik >> 24) & 0xff);
2726 if (clear) {
2727 PyDict_Clear(self->memo);
2728 Pdata_clear(data, 0);
2731 free(have_get);
2732 return r;
2733 err:
2734 free(have_get);
2735 return NULL;
2738 static PyObject *
2739 Pickler_dump(Picklerobject *self, PyObject *args)
2741 PyObject *ob;
2742 int get=0;
2744 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2745 return NULL;
2747 if (dump(self, ob) < 0)
2748 return NULL;
2750 if (get) return Pickle_getvalue(self, NULL);
2752 /* XXX Why does dump() return self? */
2753 Py_INCREF(self);
2754 return (PyObject*)self;
2758 static struct PyMethodDef Pickler_methods[] =
2760 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2761 PyDoc_STR("dump(object) -- "
2762 "Write an object in pickle format to the object's pickle stream")},
2763 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2764 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2765 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2766 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2767 {NULL, NULL} /* sentinel */
2771 static Picklerobject *
2772 newPicklerobject(PyObject *file, int proto)
2774 Picklerobject *self;
2776 if (proto < 0)
2777 proto = HIGHEST_PROTOCOL;
2778 if (proto > HIGHEST_PROTOCOL) {
2779 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2780 "the highest available protocol is %d",
2781 proto, HIGHEST_PROTOCOL);
2782 return NULL;
2785 self = PyObject_GC_New(Picklerobject, &Picklertype);
2786 if (self == NULL)
2787 return NULL;
2788 self->proto = proto;
2789 self->bin = proto > 0;
2790 self->fp = NULL;
2791 self->write = NULL;
2792 self->memo = NULL;
2793 self->arg = NULL;
2794 self->pers_func = NULL;
2795 self->inst_pers_func = NULL;
2796 self->write_buf = NULL;
2797 self->fast = 0;
2798 self->nesting = 0;
2799 self->fast_container = 0;
2800 self->fast_memo = NULL;
2801 self->buf_size = 0;
2802 self->dispatch_table = NULL;
2804 self->file = NULL;
2805 if (file)
2806 Py_INCREF(file);
2807 else {
2808 file = Pdata_New();
2809 if (file == NULL)
2810 goto err;
2812 self->file = file;
2814 if (!( self->memo = PyDict_New()))
2815 goto err;
2817 if (PyFile_Check(file)) {
2818 self->fp = PyFile_AsFile(file);
2819 if (self->fp == NULL) {
2820 PyErr_SetString(PyExc_ValueError,
2821 "I/O operation on closed file");
2822 goto err;
2824 self->write_func = write_file;
2826 else if (PycStringIO_OutputCheck(file)) {
2827 self->write_func = write_cStringIO;
2829 else if (file == Py_None) {
2830 self->write_func = write_none;
2832 else {
2833 self->write_func = write_other;
2835 if (! Pdata_Check(file)) {
2836 self->write = PyObject_GetAttr(file, write_str);
2837 if (!self->write) {
2838 PyErr_Clear();
2839 PyErr_SetString(PyExc_TypeError,
2840 "argument must have 'write' "
2841 "attribute");
2842 goto err;
2846 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2847 if (self->write_buf == NULL) {
2848 PyErr_NoMemory();
2849 goto err;
2853 if (PyEval_GetRestricted()) {
2854 /* Restricted execution, get private tables */
2855 PyObject *m = PyImport_Import(copy_reg_str);
2857 if (m == NULL)
2858 goto err;
2859 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
2860 Py_DECREF(m);
2861 if (self->dispatch_table == NULL)
2862 goto err;
2864 else {
2865 self->dispatch_table = dispatch_table;
2866 Py_INCREF(dispatch_table);
2868 PyObject_GC_Track(self);
2870 return self;
2872 err:
2873 Py_DECREF(self);
2874 return NULL;
2878 static PyObject *
2879 get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
2881 static char *kwlist[] = {"file", "protocol", NULL};
2882 PyObject *file = NULL;
2883 int proto = 0;
2885 /* XXX
2886 * The documented signature is Pickler(file, protocol=0), but this
2887 * accepts Pickler() and Pickler(integer) too. The meaning then
2888 * is clear as mud, undocumented, and not supported by pickle.py.
2889 * I'm told Zope uses this, but I haven't traced into this code
2890 * far enough to figure out what it means.
2892 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
2893 PyErr_Clear();
2894 proto = 0;
2895 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2896 kwlist, &file, &proto))
2897 return NULL;
2899 return (PyObject *)newPicklerobject(file, proto);
2903 static void
2904 Pickler_dealloc(Picklerobject *self)
2906 PyObject_GC_UnTrack(self);
2907 Py_XDECREF(self->write);
2908 Py_XDECREF(self->memo);
2909 Py_XDECREF(self->fast_memo);
2910 Py_XDECREF(self->arg);
2911 Py_XDECREF(self->file);
2912 Py_XDECREF(self->pers_func);
2913 Py_XDECREF(self->inst_pers_func);
2914 Py_XDECREF(self->dispatch_table);
2915 PyMem_Free(self->write_buf);
2916 Py_TYPE(self)->tp_free((PyObject *)self);
2919 static int
2920 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2922 Py_VISIT(self->write);
2923 Py_VISIT(self->memo);
2924 Py_VISIT(self->fast_memo);
2925 Py_VISIT(self->arg);
2926 Py_VISIT(self->file);
2927 Py_VISIT(self->pers_func);
2928 Py_VISIT(self->inst_pers_func);
2929 Py_VISIT(self->dispatch_table);
2930 return 0;
2933 static int
2934 Pickler_clear(Picklerobject *self)
2936 Py_CLEAR(self->write);
2937 Py_CLEAR(self->memo);
2938 Py_CLEAR(self->fast_memo);
2939 Py_CLEAR(self->arg);
2940 Py_CLEAR(self->file);
2941 Py_CLEAR(self->pers_func);
2942 Py_CLEAR(self->inst_pers_func);
2943 Py_CLEAR(self->dispatch_table);
2944 return 0;
2947 static PyObject *
2948 Pickler_get_pers_func(Picklerobject *p)
2950 if (p->pers_func == NULL)
2951 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2952 else
2953 Py_INCREF(p->pers_func);
2954 return p->pers_func;
2957 static int
2958 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2960 if (v == NULL) {
2961 PyErr_SetString(PyExc_TypeError,
2962 "attribute deletion is not supported");
2963 return -1;
2965 Py_XDECREF(p->pers_func);
2966 Py_INCREF(v);
2967 p->pers_func = v;
2968 return 0;
2971 static int
2972 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2974 if (v == NULL) {
2975 PyErr_SetString(PyExc_TypeError,
2976 "attribute deletion is not supported");
2977 return -1;
2979 Py_XDECREF(p->inst_pers_func);
2980 Py_INCREF(v);
2981 p->inst_pers_func = v;
2982 return 0;
2985 static PyObject *
2986 Pickler_get_memo(Picklerobject *p)
2988 if (p->memo == NULL)
2989 PyErr_SetString(PyExc_AttributeError, "memo");
2990 else
2991 Py_INCREF(p->memo);
2992 return p->memo;
2995 static int
2996 Pickler_set_memo(Picklerobject *p, PyObject *v)
2998 if (v == NULL) {
2999 PyErr_SetString(PyExc_TypeError,
3000 "attribute deletion is not supported");
3001 return -1;
3003 if (!PyDict_Check(v)) {
3004 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3005 return -1;
3007 Py_XDECREF(p->memo);
3008 Py_INCREF(v);
3009 p->memo = v;
3010 return 0;
3013 static PyObject *
3014 Pickler_get_error(Picklerobject *p)
3016 /* why is this an attribute on the Pickler? */
3017 Py_INCREF(PicklingError);
3018 return PicklingError;
3021 static PyMemberDef Pickler_members[] = {
3022 {"binary", T_INT, offsetof(Picklerobject, bin)},
3023 {"fast", T_INT, offsetof(Picklerobject, fast)},
3024 {NULL}
3027 static PyGetSetDef Pickler_getsets[] = {
3028 {"persistent_id", (getter)Pickler_get_pers_func,
3029 (setter)Pickler_set_pers_func},
3030 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3031 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3032 {"PicklingError", (getter)Pickler_get_error, NULL},
3033 {NULL}
3036 PyDoc_STRVAR(Picklertype__doc__,
3037 "Objects that know how to pickle objects\n");
3039 static PyTypeObject Picklertype = {
3040 PyVarObject_HEAD_INIT(NULL, 0)
3041 "cPickle.Pickler", /*tp_name*/
3042 sizeof(Picklerobject), /*tp_basicsize*/
3044 (destructor)Pickler_dealloc, /* tp_dealloc */
3045 0, /* tp_print */
3046 0, /* tp_getattr */
3047 0, /* tp_setattr */
3048 0, /* tp_compare */
3049 0, /* tp_repr */
3050 0, /* tp_as_number */
3051 0, /* tp_as_sequence */
3052 0, /* tp_as_mapping */
3053 0, /* tp_hash */
3054 0, /* tp_call */
3055 0, /* tp_str */
3056 PyObject_GenericGetAttr, /* tp_getattro */
3057 PyObject_GenericSetAttr, /* tp_setattro */
3058 0, /* tp_as_buffer */
3059 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3060 Picklertype__doc__, /* tp_doc */
3061 (traverseproc)Pickler_traverse, /* tp_traverse */
3062 (inquiry)Pickler_clear, /* tp_clear */
3063 0, /* tp_richcompare */
3064 0, /* tp_weaklistoffset */
3065 0, /* tp_iter */
3066 0, /* tp_iternext */
3067 Pickler_methods, /* tp_methods */
3068 Pickler_members, /* tp_members */
3069 Pickler_getsets, /* tp_getset */
3072 static PyObject *
3073 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3075 PyObject *global = 0, *module;
3077 if (fc) {
3078 if (fc==Py_None) {
3079 PyErr_SetString(UnpicklingError, "Global and instance "
3080 "pickles are not supported.");
3081 return NULL;
3083 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3084 py_global_name, NULL);
3087 module = PySys_GetObject("modules");
3088 if (module == NULL)
3089 return NULL;
3091 module = PyDict_GetItem(module, py_module_name);
3092 if (module == NULL) {
3093 module = PyImport_Import(py_module_name);
3094 if (!module)
3095 return NULL;
3096 global = PyObject_GetAttr(module, py_global_name);
3097 Py_DECREF(module);
3099 else
3100 global = PyObject_GetAttr(module, py_global_name);
3101 return global;
3104 static int
3105 marker(Unpicklerobject *self)
3107 if (self->num_marks < 1) {
3108 PyErr_SetString(UnpicklingError, "could not find MARK");
3109 return -1;
3112 return self->marks[--self->num_marks];
3116 static int
3117 load_none(Unpicklerobject *self)
3119 PDATA_APPEND(self->stack, Py_None, -1);
3120 return 0;
3123 static int
3124 bad_readline(void)
3126 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3127 return -1;
3130 static int
3131 load_int(Unpicklerobject *self)
3133 PyObject *py_int = 0;
3134 char *endptr, *s;
3135 int len, res = -1;
3136 long l;
3138 if ((len = self->readline_func(self, &s)) < 0) return -1;
3139 if (len < 2) return bad_readline();
3140 if (!( s=pystrndup(s,len))) return -1;
3142 errno = 0;
3143 l = strtol(s, &endptr, 0);
3145 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3146 /* Hm, maybe we've got something long. Let's try reading
3147 it as a Python long object. */
3148 errno = 0;
3149 py_int = PyLong_FromString(s, NULL, 0);
3150 if (py_int == NULL) {
3151 PyErr_SetString(PyExc_ValueError,
3152 "could not convert string to int");
3153 goto finally;
3156 else {
3157 if (len == 3 && (l == 0 || l == 1)) {
3158 if (!( py_int = PyBool_FromLong(l))) goto finally;
3160 else {
3161 if (!( py_int = PyInt_FromLong(l))) goto finally;
3165 free(s);
3166 PDATA_PUSH(self->stack, py_int, -1);
3167 return 0;
3169 finally:
3170 free(s);
3172 return res;
3175 static int
3176 load_bool(Unpicklerobject *self, PyObject *boolean)
3178 assert(boolean == Py_True || boolean == Py_False);
3179 PDATA_APPEND(self->stack, boolean, -1);
3180 return 0;
3183 /* s contains x bytes of a little-endian integer. Return its value as a
3184 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3185 * int, but when x is 4 it's a signed one. This is an historical source
3186 * of x-platform bugs.
3188 static long
3189 calc_binint(char *s, int x)
3191 unsigned char c;
3192 int i;
3193 long l;
3195 for (i = 0, l = 0L; i < x; i++) {
3196 c = (unsigned char)s[i];
3197 l |= (long)c << (i * 8);
3199 #if SIZEOF_LONG > 4
3200 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3201 * is signed, so on a box with longs bigger than 4 bytes we need
3202 * to extend a BININT's sign bit to the full width.
3204 if (x == 4 && l & (1L << 31))
3205 l |= (~0L) << 32;
3206 #endif
3207 return l;
3211 static int
3212 load_binintx(Unpicklerobject *self, char *s, int x)
3214 PyObject *py_int = 0;
3215 long l;
3217 l = calc_binint(s, x);
3219 if (!( py_int = PyInt_FromLong(l)))
3220 return -1;
3222 PDATA_PUSH(self->stack, py_int, -1);
3223 return 0;
3227 static int
3228 load_binint(Unpicklerobject *self)
3230 char *s;
3232 if (self->read_func(self, &s, 4) < 0)
3233 return -1;
3235 return load_binintx(self, s, 4);
3239 static int
3240 load_binint1(Unpicklerobject *self)
3242 char *s;
3244 if (self->read_func(self, &s, 1) < 0)
3245 return -1;
3247 return load_binintx(self, s, 1);
3251 static int
3252 load_binint2(Unpicklerobject *self)
3254 char *s;
3256 if (self->read_func(self, &s, 2) < 0)
3257 return -1;
3259 return load_binintx(self, s, 2);
3262 static int
3263 load_long(Unpicklerobject *self)
3265 PyObject *l = 0;
3266 char *end, *s;
3267 int len, res = -1;
3269 if ((len = self->readline_func(self, &s)) < 0) return -1;
3270 if (len < 2) return bad_readline();
3271 if (!( s=pystrndup(s,len))) return -1;
3273 if (!( l = PyLong_FromString(s, &end, 0)))
3274 goto finally;
3276 free(s);
3277 PDATA_PUSH(self->stack, l, -1);
3278 return 0;
3280 finally:
3281 free(s);
3283 return res;
3286 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3287 * data following.
3289 static int
3290 load_counted_long(Unpicklerobject *self, int size)
3292 Py_ssize_t i;
3293 char *nbytes;
3294 unsigned char *pdata;
3295 PyObject *along;
3297 assert(size == 1 || size == 4);
3298 i = self->read_func(self, &nbytes, size);
3299 if (i < 0) return -1;
3301 size = calc_binint(nbytes, size);
3302 if (size < 0) {
3303 /* Corrupt or hostile pickle -- we never write one like
3304 * this.
3306 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3307 "byte count");
3308 return -1;
3311 if (size == 0)
3312 along = PyLong_FromLong(0L);
3313 else {
3314 /* Read the raw little-endian bytes & convert. */
3315 i = self->read_func(self, (char **)&pdata, size);
3316 if (i < 0) return -1;
3317 along = _PyLong_FromByteArray(pdata, (size_t)size,
3318 1 /* little endian */, 1 /* signed */);
3320 if (along == NULL)
3321 return -1;
3322 PDATA_PUSH(self->stack, along, -1);
3323 return 0;
3326 static int
3327 load_float(Unpicklerobject *self)
3329 PyObject *py_float = 0;
3330 char *endptr, *s;
3331 int len, res = -1;
3332 double d;
3334 if ((len = self->readline_func(self, &s)) < 0) return -1;
3335 if (len < 2) return bad_readline();
3336 if (!( s=pystrndup(s,len))) return -1;
3338 errno = 0;
3339 d = PyOS_ascii_strtod(s, &endptr);
3341 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3342 PyErr_SetString(PyExc_ValueError,
3343 "could not convert string to float");
3344 goto finally;
3347 if (!( py_float = PyFloat_FromDouble(d)))
3348 goto finally;
3350 free(s);
3351 PDATA_PUSH(self->stack, py_float, -1);
3352 return 0;
3354 finally:
3355 free(s);
3357 return res;
3360 static int
3361 load_binfloat(Unpicklerobject *self)
3363 PyObject *py_float;
3364 double x;
3365 char *p;
3367 if (self->read_func(self, &p, 8) < 0)
3368 return -1;
3370 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3371 if (x == -1.0 && PyErr_Occurred())
3372 return -1;
3374 py_float = PyFloat_FromDouble(x);
3375 if (py_float == NULL)
3376 return -1;
3378 PDATA_PUSH(self->stack, py_float, -1);
3379 return 0;
3382 static int
3383 load_string(Unpicklerobject *self)
3385 PyObject *str = 0;
3386 int len, res = -1;
3387 char *s, *p;
3389 if ((len = self->readline_func(self, &s)) < 0) return -1;
3390 if (len < 2) return bad_readline();
3391 if (!( s=pystrndup(s,len))) return -1;
3394 /* Strip outermost quotes */
3395 while (s[len-1] <= ' ')
3396 len--;
3397 if(s[0]=='"' && s[len-1]=='"'){
3398 s[len-1] = '\0';
3399 p = s + 1 ;
3400 len -= 2;
3401 } else if(s[0]=='\'' && s[len-1]=='\''){
3402 s[len-1] = '\0';
3403 p = s + 1 ;
3404 len -= 2;
3405 } else
3406 goto insecure;
3407 /********************************************/
3409 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3410 free(s);
3411 if (str) {
3412 PDATA_PUSH(self->stack, str, -1);
3413 res = 0;
3415 return res;
3417 insecure:
3418 free(s);
3419 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3420 return -1;
3424 static int
3425 load_binstring(Unpicklerobject *self)
3427 PyObject *py_string = 0;
3428 long l;
3429 char *s;
3431 if (self->read_func(self, &s, 4) < 0) return -1;
3433 l = calc_binint(s, 4);
3435 if (self->read_func(self, &s, l) < 0)
3436 return -1;
3438 if (!( py_string = PyString_FromStringAndSize(s, l)))
3439 return -1;
3441 PDATA_PUSH(self->stack, py_string, -1);
3442 return 0;
3446 static int
3447 load_short_binstring(Unpicklerobject *self)
3449 PyObject *py_string = 0;
3450 unsigned char l;
3451 char *s;
3453 if (self->read_func(self, &s, 1) < 0)
3454 return -1;
3456 l = (unsigned char)s[0];
3458 if (self->read_func(self, &s, l) < 0) return -1;
3460 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
3462 PDATA_PUSH(self->stack, py_string, -1);
3463 return 0;
3467 #ifdef Py_USING_UNICODE
3468 static int
3469 load_unicode(Unpicklerobject *self)
3471 PyObject *str = 0;
3472 int len, res = -1;
3473 char *s;
3475 if ((len = self->readline_func(self, &s)) < 0) return -1;
3476 if (len < 1) return bad_readline();
3478 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3479 goto finally;
3481 PDATA_PUSH(self->stack, str, -1);
3482 return 0;
3484 finally:
3485 return res;
3487 #endif
3490 #ifdef Py_USING_UNICODE
3491 static int
3492 load_binunicode(Unpicklerobject *self)
3494 PyObject *unicode;
3495 long l;
3496 char *s;
3498 if (self->read_func(self, &s, 4) < 0) return -1;
3500 l = calc_binint(s, 4);
3502 if (self->read_func(self, &s, l) < 0)
3503 return -1;
3505 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3506 return -1;
3508 PDATA_PUSH(self->stack, unicode, -1);
3509 return 0;
3511 #endif
3514 static int
3515 load_tuple(Unpicklerobject *self)
3517 PyObject *tup;
3518 int i;
3520 if ((i = marker(self)) < 0) return -1;
3521 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3522 PDATA_PUSH(self->stack, tup, -1);
3523 return 0;
3526 static int
3527 load_counted_tuple(Unpicklerobject *self, int len)
3529 PyObject *tup = PyTuple_New(len);
3531 if (tup == NULL)
3532 return -1;
3534 while (--len >= 0) {
3535 PyObject *element;
3537 PDATA_POP(self->stack, element);
3538 if (element == NULL)
3539 return -1;
3540 PyTuple_SET_ITEM(tup, len, element);
3542 PDATA_PUSH(self->stack, tup, -1);
3543 return 0;
3546 static int
3547 load_empty_list(Unpicklerobject *self)
3549 PyObject *list;
3551 if (!( list=PyList_New(0))) return -1;
3552 PDATA_PUSH(self->stack, list, -1);
3553 return 0;
3556 static int
3557 load_empty_dict(Unpicklerobject *self)
3559 PyObject *dict;
3561 if (!( dict=PyDict_New())) return -1;
3562 PDATA_PUSH(self->stack, dict, -1);
3563 return 0;
3567 static int
3568 load_list(Unpicklerobject *self)
3570 PyObject *list = 0;
3571 int i;
3573 if ((i = marker(self)) < 0) return -1;
3574 if (!( list=Pdata_popList(self->stack, i))) return -1;
3575 PDATA_PUSH(self->stack, list, -1);
3576 return 0;
3579 static int
3580 load_dict(Unpicklerobject *self)
3582 PyObject *dict, *key, *value;
3583 int i, j, k;
3585 if ((i = marker(self)) < 0) return -1;
3586 j=self->stack->length;
3588 if (!( dict = PyDict_New())) return -1;
3590 for (k = i+1; k < j; k += 2) {
3591 key =self->stack->data[k-1];
3592 value=self->stack->data[k ];
3593 if (PyDict_SetItem(dict, key, value) < 0) {
3594 Py_DECREF(dict);
3595 return -1;
3598 Pdata_clear(self->stack, i);
3599 PDATA_PUSH(self->stack, dict, -1);
3600 return 0;
3603 static PyObject *
3604 Instance_New(PyObject *cls, PyObject *args)
3606 PyObject *r = 0;
3608 if (PyClass_Check(cls)) {
3609 int l;
3611 if ((l=PyObject_Size(args)) < 0) goto err;
3612 if (!( l )) {
3613 PyObject *__getinitargs__;
3615 __getinitargs__ = PyObject_GetAttr(cls,
3616 __getinitargs___str);
3617 if (!__getinitargs__) {
3618 /* We have a class with no __getinitargs__,
3619 so bypass usual construction */
3620 PyObject *inst;
3622 PyErr_Clear();
3623 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3624 goto err;
3625 return inst;
3627 Py_DECREF(__getinitargs__);
3630 if ((r=PyInstance_New(cls, args, NULL))) return r;
3631 else goto err;
3634 if ((r=PyObject_CallObject(cls, args))) return r;
3636 err:
3638 PyObject *tp, *v, *tb, *tmp_value;
3640 PyErr_Fetch(&tp, &v, &tb);
3641 tmp_value = v;
3642 /* NULL occurs when there was a KeyboardInterrupt */
3643 if (tmp_value == NULL)
3644 tmp_value = Py_None;
3645 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3646 Py_XDECREF(v);
3647 v=r;
3649 PyErr_Restore(tp,v,tb);
3651 return NULL;
3655 static int
3656 load_obj(Unpicklerobject *self)
3658 PyObject *class, *tup, *obj=0;
3659 int i;
3661 if ((i = marker(self)) < 0) return -1;
3662 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3663 PDATA_POP(self->stack, class);
3664 if (class) {
3665 obj = Instance_New(class, tup);
3666 Py_DECREF(class);
3668 Py_DECREF(tup);
3670 if (! obj) return -1;
3671 PDATA_PUSH(self->stack, obj, -1);
3672 return 0;
3676 static int
3677 load_inst(Unpicklerobject *self)
3679 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3680 int i, len;
3681 char *s;
3683 if ((i = marker(self)) < 0) return -1;
3685 if ((len = self->readline_func(self, &s)) < 0) return -1;
3686 if (len < 2) return bad_readline();
3687 module_name = PyString_FromStringAndSize(s, len - 1);
3688 if (!module_name) return -1;
3690 if ((len = self->readline_func(self, &s)) >= 0) {
3691 if (len < 2) return bad_readline();
3692 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3693 class = find_class(module_name, class_name,
3694 self->find_class);
3695 Py_DECREF(class_name);
3698 Py_DECREF(module_name);
3700 if (! class) return -1;
3702 if ((tup=Pdata_popTuple(self->stack, i))) {
3703 obj = Instance_New(class, tup);
3704 Py_DECREF(tup);
3706 Py_DECREF(class);
3708 if (! obj) return -1;
3710 PDATA_PUSH(self->stack, obj, -1);
3711 return 0;
3714 static int
3715 load_newobj(Unpicklerobject *self)
3717 PyObject *args = NULL;
3718 PyObject *clsraw = NULL;
3719 PyTypeObject *cls; /* clsraw cast to its true type */
3720 PyObject *obj;
3722 /* Stack is ... cls argtuple, and we want to call
3723 * cls.__new__(cls, *argtuple).
3725 PDATA_POP(self->stack, args);
3726 if (args == NULL) goto Fail;
3727 if (! PyTuple_Check(args)) {
3728 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3729 "tuple.");
3730 goto Fail;
3733 PDATA_POP(self->stack, clsraw);
3734 cls = (PyTypeObject *)clsraw;
3735 if (cls == NULL) goto Fail;
3736 if (! PyType_Check(cls)) {
3737 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3738 "isn't a type object");
3739 goto Fail;
3741 if (cls->tp_new == NULL) {
3742 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3743 "has NULL tp_new");
3744 goto Fail;
3747 /* Call __new__. */
3748 obj = cls->tp_new(cls, args, NULL);
3749 if (obj == NULL) goto Fail;
3751 Py_DECREF(args);
3752 Py_DECREF(clsraw);
3753 PDATA_PUSH(self->stack, obj, -1);
3754 return 0;
3756 Fail:
3757 Py_XDECREF(args);
3758 Py_XDECREF(clsraw);
3759 return -1;
3762 static int
3763 load_global(Unpicklerobject *self)
3765 PyObject *class = 0, *module_name = 0, *class_name = 0;
3766 int len;
3767 char *s;
3769 if ((len = self->readline_func(self, &s)) < 0) return -1;
3770 if (len < 2) return bad_readline();
3771 module_name = PyString_FromStringAndSize(s, len - 1);
3772 if (!module_name) return -1;
3774 if ((len = self->readline_func(self, &s)) >= 0) {
3775 if (len < 2) {
3776 Py_DECREF(module_name);
3777 return bad_readline();
3779 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3780 class = find_class(module_name, class_name,
3781 self->find_class);
3782 Py_DECREF(class_name);
3785 Py_DECREF(module_name);
3787 if (! class) return -1;
3788 PDATA_PUSH(self->stack, class, -1);
3789 return 0;
3793 static int
3794 load_persid(Unpicklerobject *self)
3796 PyObject *pid = 0;
3797 int len;
3798 char *s;
3800 if (self->pers_func) {
3801 if ((len = self->readline_func(self, &s)) < 0) return -1;
3802 if (len < 2) return bad_readline();
3804 pid = PyString_FromStringAndSize(s, len - 1);
3805 if (!pid) return -1;
3807 if (PyList_Check(self->pers_func)) {
3808 if (PyList_Append(self->pers_func, pid) < 0) {
3809 Py_DECREF(pid);
3810 return -1;
3813 else {
3814 ARG_TUP(self, pid);
3815 if (self->arg) {
3816 pid = PyObject_Call(self->pers_func, self->arg,
3817 NULL);
3818 FREE_ARG_TUP(self);
3822 if (! pid) return -1;
3824 PDATA_PUSH(self->stack, pid, -1);
3825 return 0;
3827 else {
3828 PyErr_SetString(UnpicklingError,
3829 "A load persistent id instruction was encountered,\n"
3830 "but no persistent_load function was specified.");
3831 return -1;
3835 static int
3836 load_binpersid(Unpicklerobject *self)
3838 PyObject *pid = 0;
3840 if (self->pers_func) {
3841 PDATA_POP(self->stack, pid);
3842 if (! pid) return -1;
3844 if (PyList_Check(self->pers_func)) {
3845 if (PyList_Append(self->pers_func, pid) < 0) {
3846 Py_DECREF(pid);
3847 return -1;
3850 else {
3851 ARG_TUP(self, pid);
3852 if (self->arg) {
3853 pid = PyObject_Call(self->pers_func, self->arg,
3854 NULL);
3855 FREE_ARG_TUP(self);
3857 if (! pid) return -1;
3860 PDATA_PUSH(self->stack, pid, -1);
3861 return 0;
3863 else {
3864 PyErr_SetString(UnpicklingError,
3865 "A load persistent id instruction was encountered,\n"
3866 "but no persistent_load function was specified.");
3867 return -1;
3872 static int
3873 load_pop(Unpicklerobject *self)
3875 int len;
3877 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3879 /* Note that we split the (pickle.py) stack into two stacks,
3880 an object stack and a mark stack. We have to be clever and
3881 pop the right one. We do this by looking at the top of the
3882 mark stack.
3885 if ((self->num_marks > 0) &&
3886 (self->marks[self->num_marks - 1] == len))
3887 self->num_marks--;
3888 else {
3889 len--;
3890 Py_DECREF(self->stack->data[len]);
3891 self->stack->length=len;
3894 return 0;
3898 static int
3899 load_pop_mark(Unpicklerobject *self)
3901 int i;
3903 if ((i = marker(self)) < 0)
3904 return -1;
3906 Pdata_clear(self->stack, i);
3908 return 0;
3912 static int
3913 load_dup(Unpicklerobject *self)
3915 PyObject *last;
3916 int len;
3918 if ((len = self->stack->length) <= 0) return stackUnderflow();
3919 last=self->stack->data[len-1];
3920 Py_INCREF(last);
3921 PDATA_PUSH(self->stack, last, -1);
3922 return 0;
3926 static int
3927 load_get(Unpicklerobject *self)
3929 PyObject *py_str = 0, *value = 0;
3930 int len;
3931 char *s;
3932 int rc;
3934 if ((len = self->readline_func(self, &s)) < 0) return -1;
3935 if (len < 2) return bad_readline();
3937 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
3939 value = PyDict_GetItem(self->memo, py_str);
3940 if (! value) {
3941 PyErr_SetObject(BadPickleGet, py_str);
3942 rc = -1;
3944 else {
3945 PDATA_APPEND(self->stack, value, -1);
3946 rc = 0;
3949 Py_DECREF(py_str);
3950 return rc;
3954 static int
3955 load_binget(Unpicklerobject *self)
3957 PyObject *py_key = 0, *value = 0;
3958 unsigned char key;
3959 char *s;
3960 int rc;
3962 if (self->read_func(self, &s, 1) < 0) return -1;
3964 key = (unsigned char)s[0];
3965 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3967 value = PyDict_GetItem(self->memo, py_key);
3968 if (! value) {
3969 PyErr_SetObject(BadPickleGet, py_key);
3970 rc = -1;
3972 else {
3973 PDATA_APPEND(self->stack, value, -1);
3974 rc = 0;
3977 Py_DECREF(py_key);
3978 return rc;
3982 static int
3983 load_long_binget(Unpicklerobject *self)
3985 PyObject *py_key = 0, *value = 0;
3986 unsigned char c;
3987 char *s;
3988 long key;
3989 int rc;
3991 if (self->read_func(self, &s, 4) < 0) return -1;
3993 c = (unsigned char)s[0];
3994 key = (long)c;
3995 c = (unsigned char)s[1];
3996 key |= (long)c << 8;
3997 c = (unsigned char)s[2];
3998 key |= (long)c << 16;
3999 c = (unsigned char)s[3];
4000 key |= (long)c << 24;
4002 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4004 value = PyDict_GetItem(self->memo, py_key);
4005 if (! value) {
4006 PyErr_SetObject(BadPickleGet, py_key);
4007 rc = -1;
4009 else {
4010 PDATA_APPEND(self->stack, value, -1);
4011 rc = 0;
4014 Py_DECREF(py_key);
4015 return rc;
4018 /* Push an object from the extension registry (EXT[124]). nbytes is
4019 * the number of bytes following the opcode, holding the index (code) value.
4021 static int
4022 load_extension(Unpicklerobject *self, int nbytes)
4024 char *codebytes; /* the nbytes bytes after the opcode */
4025 long code; /* calc_binint returns long */
4026 PyObject *py_code; /* code as a Python int */
4027 PyObject *obj; /* the object to push */
4028 PyObject *pair; /* (module_name, class_name) */
4029 PyObject *module_name, *class_name;
4031 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4032 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4033 code = calc_binint(codebytes, nbytes);
4034 if (code <= 0) { /* note that 0 is forbidden */
4035 /* Corrupt or hostile pickle. */
4036 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4037 return -1;
4040 /* Look for the code in the cache. */
4041 py_code = PyInt_FromLong(code);
4042 if (py_code == NULL) return -1;
4043 obj = PyDict_GetItem(extension_cache, py_code);
4044 if (obj != NULL) {
4045 /* Bingo. */
4046 Py_DECREF(py_code);
4047 PDATA_APPEND(self->stack, obj, -1);
4048 return 0;
4051 /* Look up the (module_name, class_name) pair. */
4052 pair = PyDict_GetItem(inverted_registry, py_code);
4053 if (pair == NULL) {
4054 Py_DECREF(py_code);
4055 PyErr_Format(PyExc_ValueError, "unregistered extension "
4056 "code %ld", code);
4057 return -1;
4059 /* Since the extension registry is manipulable via Python code,
4060 * confirm that pair is really a 2-tuple of strings.
4062 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4063 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4064 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4065 Py_DECREF(py_code);
4066 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4067 "isn't a 2-tuple of strings", code);
4068 return -1;
4070 /* Load the object. */
4071 obj = find_class(module_name, class_name, self->find_class);
4072 if (obj == NULL) {
4073 Py_DECREF(py_code);
4074 return -1;
4076 /* Cache code -> obj. */
4077 code = PyDict_SetItem(extension_cache, py_code, obj);
4078 Py_DECREF(py_code);
4079 if (code < 0) {
4080 Py_DECREF(obj);
4081 return -1;
4083 PDATA_PUSH(self->stack, obj, -1);
4084 return 0;
4087 static int
4088 load_put(Unpicklerobject *self)
4090 PyObject *py_str = 0, *value = 0;
4091 int len, l;
4092 char *s;
4094 if ((l = self->readline_func(self, &s)) < 0) return -1;
4095 if (l < 2) return bad_readline();
4096 if (!( len=self->stack->length )) return stackUnderflow();
4097 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4098 value=self->stack->data[len-1];
4099 l=PyDict_SetItem(self->memo, py_str, value);
4100 Py_DECREF(py_str);
4101 return l;
4105 static int
4106 load_binput(Unpicklerobject *self)
4108 PyObject *py_key = 0, *value = 0;
4109 unsigned char key;
4110 char *s;
4111 int len;
4113 if (self->read_func(self, &s, 1) < 0) return -1;
4114 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4116 key = (unsigned char)s[0];
4118 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4119 value=self->stack->data[len-1];
4120 len=PyDict_SetItem(self->memo, py_key, value);
4121 Py_DECREF(py_key);
4122 return len;
4126 static int
4127 load_long_binput(Unpicklerobject *self)
4129 PyObject *py_key = 0, *value = 0;
4130 long key;
4131 unsigned char c;
4132 char *s;
4133 int len;
4135 if (self->read_func(self, &s, 4) < 0) return -1;
4136 if (!( len=self->stack->length )) return stackUnderflow();
4138 c = (unsigned char)s[0];
4139 key = (long)c;
4140 c = (unsigned char)s[1];
4141 key |= (long)c << 8;
4142 c = (unsigned char)s[2];
4143 key |= (long)c << 16;
4144 c = (unsigned char)s[3];
4145 key |= (long)c << 24;
4147 if (!( py_key = PyInt_FromLong(key))) return -1;
4148 value=self->stack->data[len-1];
4149 len=PyDict_SetItem(self->memo, py_key, value);
4150 Py_DECREF(py_key);
4151 return len;
4155 static int
4156 do_append(Unpicklerobject *self, int x)
4158 PyObject *value = 0, *list = 0, *append_method = 0;
4159 int len, i;
4161 len=self->stack->length;
4162 if (!( len >= x && x > 0 )) return stackUnderflow();
4163 /* nothing to do */
4164 if (len==x) return 0;
4166 list=self->stack->data[x-1];
4168 if (PyList_Check(list)) {
4169 PyObject *slice;
4170 int list_len;
4172 slice=Pdata_popList(self->stack, x);
4173 if (! slice) return -1;
4174 list_len = PyList_GET_SIZE(list);
4175 i=PyList_SetSlice(list, list_len, list_len, slice);
4176 Py_DECREF(slice);
4177 return i;
4179 else {
4181 if (!( append_method = PyObject_GetAttr(list, append_str)))
4182 return -1;
4184 for (i = x; i < len; i++) {
4185 PyObject *junk;
4187 value=self->stack->data[i];
4188 junk=0;
4189 ARG_TUP(self, value);
4190 if (self->arg) {
4191 junk = PyObject_Call(append_method, self->arg,
4192 NULL);
4193 FREE_ARG_TUP(self);
4195 if (! junk) {
4196 Pdata_clear(self->stack, i+1);
4197 self->stack->length=x;
4198 Py_DECREF(append_method);
4199 return -1;
4201 Py_DECREF(junk);
4203 self->stack->length=x;
4204 Py_DECREF(append_method);
4207 return 0;
4211 static int
4212 load_append(Unpicklerobject *self)
4214 return do_append(self, self->stack->length - 1);
4218 static int
4219 load_appends(Unpicklerobject *self)
4221 return do_append(self, marker(self));
4225 static int
4226 do_setitems(Unpicklerobject *self, int x)
4228 PyObject *value = 0, *key = 0, *dict = 0;
4229 int len, i, r=0;
4231 if (!( (len=self->stack->length) >= x
4232 && x > 0 )) return stackUnderflow();
4234 dict=self->stack->data[x-1];
4236 for (i = x+1; i < len; i += 2) {
4237 key =self->stack->data[i-1];
4238 value=self->stack->data[i ];
4239 if (PyObject_SetItem(dict, key, value) < 0) {
4240 r=-1;
4241 break;
4245 Pdata_clear(self->stack, x);
4247 return r;
4251 static int
4252 load_setitem(Unpicklerobject *self)
4254 return do_setitems(self, self->stack->length - 2);
4257 static int
4258 load_setitems(Unpicklerobject *self)
4260 return do_setitems(self, marker(self));
4264 static int
4265 load_build(Unpicklerobject *self)
4267 PyObject *state, *inst, *slotstate;
4268 PyObject *__setstate__;
4269 PyObject *d_key, *d_value;
4270 Py_ssize_t i;
4271 int res = -1;
4273 /* Stack is ... instance, state. We want to leave instance at
4274 * the stack top, possibly mutated via instance.__setstate__(state).
4276 if (self->stack->length < 2)
4277 return stackUnderflow();
4278 PDATA_POP(self->stack, state);
4279 if (state == NULL)
4280 return -1;
4281 inst = self->stack->data[self->stack->length - 1];
4283 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4284 if (__setstate__ != NULL) {
4285 PyObject *junk = NULL;
4287 /* The explicit __setstate__ is responsible for everything. */
4288 ARG_TUP(self, state);
4289 if (self->arg) {
4290 junk = PyObject_Call(__setstate__, self->arg, NULL);
4291 FREE_ARG_TUP(self);
4293 Py_DECREF(__setstate__);
4294 if (junk == NULL)
4295 return -1;
4296 Py_DECREF(junk);
4297 return 0;
4299 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4300 return -1;
4301 PyErr_Clear();
4303 /* A default __setstate__. First see whether state embeds a
4304 * slot state dict too (a proto 2 addition).
4306 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4307 PyObject *temp = state;
4308 state = PyTuple_GET_ITEM(temp, 0);
4309 slotstate = PyTuple_GET_ITEM(temp, 1);
4310 Py_INCREF(state);
4311 Py_INCREF(slotstate);
4312 Py_DECREF(temp);
4314 else
4315 slotstate = NULL;
4317 /* Set inst.__dict__ from the state dict (if any). */
4318 if (state != Py_None) {
4319 PyObject *dict;
4320 if (! PyDict_Check(state)) {
4321 PyErr_SetString(UnpicklingError, "state is not a "
4322 "dictionary");
4323 goto finally;
4325 dict = PyObject_GetAttr(inst, __dict___str);
4326 if (dict == NULL)
4327 goto finally;
4329 i = 0;
4330 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4331 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4332 goto finally;
4334 Py_DECREF(dict);
4337 /* Also set instance attributes from the slotstate dict (if any). */
4338 if (slotstate != NULL) {
4339 if (! PyDict_Check(slotstate)) {
4340 PyErr_SetString(UnpicklingError, "slot state is not "
4341 "a dictionary");
4342 goto finally;
4344 i = 0;
4345 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4346 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4347 goto finally;
4350 res = 0;
4352 finally:
4353 Py_DECREF(state);
4354 Py_XDECREF(slotstate);
4355 return res;
4359 static int
4360 load_mark(Unpicklerobject *self)
4362 int s;
4364 /* Note that we split the (pickle.py) stack into two stacks, an
4365 object stack and a mark stack. Here we push a mark onto the
4366 mark stack.
4369 if ((self->num_marks + 1) >= self->marks_size) {
4370 int *marks;
4371 s=self->marks_size+20;
4372 if (s <= self->num_marks) s=self->num_marks + 1;
4373 if (self->marks == NULL)
4374 marks=(int *)malloc(s * sizeof(int));
4375 else
4376 marks=(int *)realloc(self->marks,
4377 s * sizeof(int));
4378 if (!marks) {
4379 PyErr_NoMemory();
4380 return -1;
4382 self->marks = marks;
4383 self->marks_size = s;
4386 self->marks[self->num_marks++] = self->stack->length;
4388 return 0;
4391 static int
4392 load_reduce(Unpicklerobject *self)
4394 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4396 PDATA_POP(self->stack, arg_tup);
4397 if (! arg_tup) return -1;
4398 PDATA_POP(self->stack, callable);
4399 if (callable) {
4400 ob = Instance_New(callable, arg_tup);
4401 Py_DECREF(callable);
4403 Py_DECREF(arg_tup);
4405 if (! ob) return -1;
4407 PDATA_PUSH(self->stack, ob, -1);
4408 return 0;
4411 /* Just raises an error if we don't know the protocol specified. PROTO
4412 * is the first opcode for protocols >= 2.
4414 static int
4415 load_proto(Unpicklerobject *self)
4417 int i;
4418 char *protobyte;
4420 i = self->read_func(self, &protobyte, 1);
4421 if (i < 0)
4422 return -1;
4424 i = calc_binint(protobyte, 1);
4425 /* No point checking for < 0, since calc_binint returns an unsigned
4426 * int when chewing on 1 byte.
4428 assert(i >= 0);
4429 if (i <= HIGHEST_PROTOCOL)
4430 return 0;
4432 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4433 return -1;
4436 static PyObject *
4437 load(Unpicklerobject *self)
4439 PyObject *err = 0, *val = 0;
4440 char *s;
4442 self->num_marks = 0;
4443 if (self->stack->length) Pdata_clear(self->stack, 0);
4445 while (1) {
4446 if (self->read_func(self, &s, 1) < 0)
4447 break;
4449 switch (s[0]) {
4450 case NONE:
4451 if (load_none(self) < 0)
4452 break;
4453 continue;
4455 case BININT:
4456 if (load_binint(self) < 0)
4457 break;
4458 continue;
4460 case BININT1:
4461 if (load_binint1(self) < 0)
4462 break;
4463 continue;
4465 case BININT2:
4466 if (load_binint2(self) < 0)
4467 break;
4468 continue;
4470 case INT:
4471 if (load_int(self) < 0)
4472 break;
4473 continue;
4475 case LONG:
4476 if (load_long(self) < 0)
4477 break;
4478 continue;
4480 case LONG1:
4481 if (load_counted_long(self, 1) < 0)
4482 break;
4483 continue;
4485 case LONG4:
4486 if (load_counted_long(self, 4) < 0)
4487 break;
4488 continue;
4490 case FLOAT:
4491 if (load_float(self) < 0)
4492 break;
4493 continue;
4495 case BINFLOAT:
4496 if (load_binfloat(self) < 0)
4497 break;
4498 continue;
4500 case BINSTRING:
4501 if (load_binstring(self) < 0)
4502 break;
4503 continue;
4505 case SHORT_BINSTRING:
4506 if (load_short_binstring(self) < 0)
4507 break;
4508 continue;
4510 case STRING:
4511 if (load_string(self) < 0)
4512 break;
4513 continue;
4515 #ifdef Py_USING_UNICODE
4516 case UNICODE:
4517 if (load_unicode(self) < 0)
4518 break;
4519 continue;
4521 case BINUNICODE:
4522 if (load_binunicode(self) < 0)
4523 break;
4524 continue;
4525 #endif
4527 case EMPTY_TUPLE:
4528 if (load_counted_tuple(self, 0) < 0)
4529 break;
4530 continue;
4532 case TUPLE1:
4533 if (load_counted_tuple(self, 1) < 0)
4534 break;
4535 continue;
4537 case TUPLE2:
4538 if (load_counted_tuple(self, 2) < 0)
4539 break;
4540 continue;
4542 case TUPLE3:
4543 if (load_counted_tuple(self, 3) < 0)
4544 break;
4545 continue;
4547 case TUPLE:
4548 if (load_tuple(self) < 0)
4549 break;
4550 continue;
4552 case EMPTY_LIST:
4553 if (load_empty_list(self) < 0)
4554 break;
4555 continue;
4557 case LIST:
4558 if (load_list(self) < 0)
4559 break;
4560 continue;
4562 case EMPTY_DICT:
4563 if (load_empty_dict(self) < 0)
4564 break;
4565 continue;
4567 case DICT:
4568 if (load_dict(self) < 0)
4569 break;
4570 continue;
4572 case OBJ:
4573 if (load_obj(self) < 0)
4574 break;
4575 continue;
4577 case INST:
4578 if (load_inst(self) < 0)
4579 break;
4580 continue;
4582 case NEWOBJ:
4583 if (load_newobj(self) < 0)
4584 break;
4585 continue;
4587 case GLOBAL:
4588 if (load_global(self) < 0)
4589 break;
4590 continue;
4592 case APPEND:
4593 if (load_append(self) < 0)
4594 break;
4595 continue;
4597 case APPENDS:
4598 if (load_appends(self) < 0)
4599 break;
4600 continue;
4602 case BUILD:
4603 if (load_build(self) < 0)
4604 break;
4605 continue;
4607 case DUP:
4608 if (load_dup(self) < 0)
4609 break;
4610 continue;
4612 case BINGET:
4613 if (load_binget(self) < 0)
4614 break;
4615 continue;
4617 case LONG_BINGET:
4618 if (load_long_binget(self) < 0)
4619 break;
4620 continue;
4622 case GET:
4623 if (load_get(self) < 0)
4624 break;
4625 continue;
4627 case EXT1:
4628 if (load_extension(self, 1) < 0)
4629 break;
4630 continue;
4632 case EXT2:
4633 if (load_extension(self, 2) < 0)
4634 break;
4635 continue;
4637 case EXT4:
4638 if (load_extension(self, 4) < 0)
4639 break;
4640 continue;
4641 case MARK:
4642 if (load_mark(self) < 0)
4643 break;
4644 continue;
4646 case BINPUT:
4647 if (load_binput(self) < 0)
4648 break;
4649 continue;
4651 case LONG_BINPUT:
4652 if (load_long_binput(self) < 0)
4653 break;
4654 continue;
4656 case PUT:
4657 if (load_put(self) < 0)
4658 break;
4659 continue;
4661 case POP:
4662 if (load_pop(self) < 0)
4663 break;
4664 continue;
4666 case POP_MARK:
4667 if (load_pop_mark(self) < 0)
4668 break;
4669 continue;
4671 case SETITEM:
4672 if (load_setitem(self) < 0)
4673 break;
4674 continue;
4676 case SETITEMS:
4677 if (load_setitems(self) < 0)
4678 break;
4679 continue;
4681 case STOP:
4682 break;
4684 case PERSID:
4685 if (load_persid(self) < 0)
4686 break;
4687 continue;
4689 case BINPERSID:
4690 if (load_binpersid(self) < 0)
4691 break;
4692 continue;
4694 case REDUCE:
4695 if (load_reduce(self) < 0)
4696 break;
4697 continue;
4699 case PROTO:
4700 if (load_proto(self) < 0)
4701 break;
4702 continue;
4704 case NEWTRUE:
4705 if (load_bool(self, Py_True) < 0)
4706 break;
4707 continue;
4709 case NEWFALSE:
4710 if (load_bool(self, Py_False) < 0)
4711 break;
4712 continue;
4714 case '\0':
4715 /* end of file */
4716 PyErr_SetNone(PyExc_EOFError);
4717 break;
4719 default:
4720 cPickle_ErrFormat(UnpicklingError,
4721 "invalid load key, '%s'.",
4722 "c", s[0]);
4723 return NULL;
4726 break;
4729 if ((err = PyErr_Occurred())) {
4730 if (err == PyExc_EOFError) {
4731 PyErr_SetNone(PyExc_EOFError);
4733 return NULL;
4736 PDATA_POP(self->stack, val);
4737 return val;
4741 /* No-load functions to support noload, which is used to
4742 find persistent references. */
4744 static int
4745 noload_obj(Unpicklerobject *self)
4747 int i;
4749 if ((i = marker(self)) < 0) return -1;
4750 return Pdata_clear(self->stack, i+1);
4754 static int
4755 noload_inst(Unpicklerobject *self)
4757 int i;
4758 char *s;
4760 if ((i = marker(self)) < 0) return -1;
4761 Pdata_clear(self->stack, i);
4762 if (self->readline_func(self, &s) < 0) return -1;
4763 if (self->readline_func(self, &s) < 0) return -1;
4764 PDATA_APPEND(self->stack, Py_None, -1);
4765 return 0;
4768 static int
4769 noload_newobj(Unpicklerobject *self)
4771 PyObject *obj;
4773 PDATA_POP(self->stack, obj); /* pop argtuple */
4774 if (obj == NULL) return -1;
4775 Py_DECREF(obj);
4777 PDATA_POP(self->stack, obj); /* pop cls */
4778 if (obj == NULL) return -1;
4779 Py_DECREF(obj);
4781 PDATA_APPEND(self->stack, Py_None, -1);
4782 return 0;
4785 static int
4786 noload_global(Unpicklerobject *self)
4788 char *s;
4790 if (self->readline_func(self, &s) < 0) return -1;
4791 if (self->readline_func(self, &s) < 0) return -1;
4792 PDATA_APPEND(self->stack, Py_None,-1);
4793 return 0;
4796 static int
4797 noload_reduce(Unpicklerobject *self)
4800 if (self->stack->length < 2) return stackUnderflow();
4801 Pdata_clear(self->stack, self->stack->length-2);
4802 PDATA_APPEND(self->stack, Py_None,-1);
4803 return 0;
4806 static int
4807 noload_build(Unpicklerobject *self) {
4809 if (self->stack->length < 1) return stackUnderflow();
4810 Pdata_clear(self->stack, self->stack->length-1);
4811 return 0;
4814 static int
4815 noload_extension(Unpicklerobject *self, int nbytes)
4817 char *codebytes;
4819 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4820 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4821 PDATA_APPEND(self->stack, Py_None, -1);
4822 return 0;
4826 static PyObject *
4827 noload(Unpicklerobject *self)
4829 PyObject *err = 0, *val = 0;
4830 char *s;
4832 self->num_marks = 0;
4833 Pdata_clear(self->stack, 0);
4835 while (1) {
4836 if (self->read_func(self, &s, 1) < 0)
4837 break;
4839 switch (s[0]) {
4840 case NONE:
4841 if (load_none(self) < 0)
4842 break;
4843 continue;
4845 case BININT:
4846 if (load_binint(self) < 0)
4847 break;
4848 continue;
4850 case BININT1:
4851 if (load_binint1(self) < 0)
4852 break;
4853 continue;
4855 case BININT2:
4856 if (load_binint2(self) < 0)
4857 break;
4858 continue;
4860 case INT:
4861 if (load_int(self) < 0)
4862 break;
4863 continue;
4865 case LONG:
4866 if (load_long(self) < 0)
4867 break;
4868 continue;
4870 case LONG1:
4871 if (load_counted_long(self, 1) < 0)
4872 break;
4873 continue;
4875 case LONG4:
4876 if (load_counted_long(self, 4) < 0)
4877 break;
4878 continue;
4880 case FLOAT:
4881 if (load_float(self) < 0)
4882 break;
4883 continue;
4885 case BINFLOAT:
4886 if (load_binfloat(self) < 0)
4887 break;
4888 continue;
4890 case BINSTRING:
4891 if (load_binstring(self) < 0)
4892 break;
4893 continue;
4895 case SHORT_BINSTRING:
4896 if (load_short_binstring(self) < 0)
4897 break;
4898 continue;
4900 case STRING:
4901 if (load_string(self) < 0)
4902 break;
4903 continue;
4905 #ifdef Py_USING_UNICODE
4906 case UNICODE:
4907 if (load_unicode(self) < 0)
4908 break;
4909 continue;
4911 case BINUNICODE:
4912 if (load_binunicode(self) < 0)
4913 break;
4914 continue;
4915 #endif
4917 case EMPTY_TUPLE:
4918 if (load_counted_tuple(self, 0) < 0)
4919 break;
4920 continue;
4922 case TUPLE1:
4923 if (load_counted_tuple(self, 1) < 0)
4924 break;
4925 continue;
4927 case TUPLE2:
4928 if (load_counted_tuple(self, 2) < 0)
4929 break;
4930 continue;
4932 case TUPLE3:
4933 if (load_counted_tuple(self, 3) < 0)
4934 break;
4935 continue;
4937 case TUPLE:
4938 if (load_tuple(self) < 0)
4939 break;
4940 continue;
4942 case EMPTY_LIST:
4943 if (load_empty_list(self) < 0)
4944 break;
4945 continue;
4947 case LIST:
4948 if (load_list(self) < 0)
4949 break;
4950 continue;
4952 case EMPTY_DICT:
4953 if (load_empty_dict(self) < 0)
4954 break;
4955 continue;
4957 case DICT:
4958 if (load_dict(self) < 0)
4959 break;
4960 continue;
4962 case OBJ:
4963 if (noload_obj(self) < 0)
4964 break;
4965 continue;
4967 case INST:
4968 if (noload_inst(self) < 0)
4969 break;
4970 continue;
4972 case NEWOBJ:
4973 if (noload_newobj(self) < 0)
4974 break;
4975 continue;
4977 case GLOBAL:
4978 if (noload_global(self) < 0)
4979 break;
4980 continue;
4982 case APPEND:
4983 if (load_append(self) < 0)
4984 break;
4985 continue;
4987 case APPENDS:
4988 if (load_appends(self) < 0)
4989 break;
4990 continue;
4992 case BUILD:
4993 if (noload_build(self) < 0)
4994 break;
4995 continue;
4997 case DUP:
4998 if (load_dup(self) < 0)
4999 break;
5000 continue;
5002 case BINGET:
5003 if (load_binget(self) < 0)
5004 break;
5005 continue;
5007 case LONG_BINGET:
5008 if (load_long_binget(self) < 0)
5009 break;
5010 continue;
5012 case GET:
5013 if (load_get(self) < 0)
5014 break;
5015 continue;
5017 case EXT1:
5018 if (noload_extension(self, 1) < 0)
5019 break;
5020 continue;
5022 case EXT2:
5023 if (noload_extension(self, 2) < 0)
5024 break;
5025 continue;
5027 case EXT4:
5028 if (noload_extension(self, 4) < 0)
5029 break;
5030 continue;
5032 case MARK:
5033 if (load_mark(self) < 0)
5034 break;
5035 continue;
5037 case BINPUT:
5038 if (load_binput(self) < 0)
5039 break;
5040 continue;
5042 case LONG_BINPUT:
5043 if (load_long_binput(self) < 0)
5044 break;
5045 continue;
5047 case PUT:
5048 if (load_put(self) < 0)
5049 break;
5050 continue;
5052 case POP:
5053 if (load_pop(self) < 0)
5054 break;
5055 continue;
5057 case POP_MARK:
5058 if (load_pop_mark(self) < 0)
5059 break;
5060 continue;
5062 case SETITEM:
5063 if (load_setitem(self) < 0)
5064 break;
5065 continue;
5067 case SETITEMS:
5068 if (load_setitems(self) < 0)
5069 break;
5070 continue;
5072 case STOP:
5073 break;
5075 case PERSID:
5076 if (load_persid(self) < 0)
5077 break;
5078 continue;
5080 case BINPERSID:
5081 if (load_binpersid(self) < 0)
5082 break;
5083 continue;
5085 case REDUCE:
5086 if (noload_reduce(self) < 0)
5087 break;
5088 continue;
5090 case PROTO:
5091 if (load_proto(self) < 0)
5092 break;
5093 continue;
5095 case NEWTRUE:
5096 if (load_bool(self, Py_True) < 0)
5097 break;
5098 continue;
5100 case NEWFALSE:
5101 if (load_bool(self, Py_False) < 0)
5102 break;
5103 continue;
5104 default:
5105 cPickle_ErrFormat(UnpicklingError,
5106 "invalid load key, '%s'.",
5107 "c", s[0]);
5108 return NULL;
5111 break;
5114 if ((err = PyErr_Occurred())) {
5115 if (err == PyExc_EOFError) {
5116 PyErr_SetNone(PyExc_EOFError);
5118 return NULL;
5121 PDATA_POP(self->stack, val);
5122 return val;
5126 static PyObject *
5127 Unpickler_load(Unpicklerobject *self, PyObject *unused)
5129 return load(self);
5132 static PyObject *
5133 Unpickler_noload(Unpicklerobject *self, PyObject *unused)
5135 return noload(self);
5139 static struct PyMethodDef Unpickler_methods[] = {
5140 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
5141 PyDoc_STR("load() -- Load a pickle")
5143 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
5144 PyDoc_STR(
5145 "noload() -- not load a pickle, but go through most of the motions\n"
5146 "\n"
5147 "This function can be used to read past a pickle without instantiating\n"
5148 "any objects or importing any modules. It can also be used to find all\n"
5149 "persistent references without instantiating any objects or importing\n"
5150 "any modules.\n")
5152 {NULL, NULL} /* sentinel */
5156 static Unpicklerobject *
5157 newUnpicklerobject(PyObject *f)
5159 Unpicklerobject *self;
5161 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5162 return NULL;
5164 self->file = NULL;
5165 self->arg = NULL;
5166 self->stack = (Pdata*)Pdata_New();
5167 self->pers_func = NULL;
5168 self->last_string = NULL;
5169 self->marks = NULL;
5170 self->num_marks = 0;
5171 self->marks_size = 0;
5172 self->buf_size = 0;
5173 self->read = NULL;
5174 self->readline = NULL;
5175 self->find_class = NULL;
5177 if (!( self->memo = PyDict_New()))
5178 goto err;
5180 if (!self->stack)
5181 goto err;
5183 Py_INCREF(f);
5184 self->file = f;
5186 /* Set read, readline based on type of f */
5187 if (PyFile_Check(f)) {
5188 self->fp = PyFile_AsFile(f);
5189 if (self->fp == NULL) {
5190 PyErr_SetString(PyExc_ValueError,
5191 "I/O operation on closed file");
5192 goto err;
5194 self->read_func = read_file;
5195 self->readline_func = readline_file;
5197 else if (PycStringIO_InputCheck(f)) {
5198 self->fp = NULL;
5199 self->read_func = read_cStringIO;
5200 self->readline_func = readline_cStringIO;
5202 else {
5204 self->fp = NULL;
5205 self->read_func = read_other;
5206 self->readline_func = readline_other;
5208 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5209 (self->read = PyObject_GetAttr(f, read_str)))) {
5210 PyErr_Clear();
5211 PyErr_SetString( PyExc_TypeError,
5212 "argument must have 'read' and "
5213 "'readline' attributes" );
5214 goto err;
5217 PyObject_GC_Track(self);
5219 return self;
5221 err:
5222 Py_DECREF((PyObject *)self);
5223 return NULL;
5227 static PyObject *
5228 get_Unpickler(PyObject *self, PyObject *file)
5230 return (PyObject *)newUnpicklerobject(file);
5234 static void
5235 Unpickler_dealloc(Unpicklerobject *self)
5237 PyObject_GC_UnTrack((PyObject *)self);
5238 Py_XDECREF(self->readline);
5239 Py_XDECREF(self->read);
5240 Py_XDECREF(self->file);
5241 Py_XDECREF(self->memo);
5242 Py_XDECREF(self->stack);
5243 Py_XDECREF(self->pers_func);
5244 Py_XDECREF(self->arg);
5245 Py_XDECREF(self->last_string);
5246 Py_XDECREF(self->find_class);
5248 if (self->marks) {
5249 free(self->marks);
5252 if (self->buf_size) {
5253 free(self->buf);
5256 Py_TYPE(self)->tp_free((PyObject *)self);
5259 static int
5260 Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5262 Py_VISIT(self->readline);
5263 Py_VISIT(self->read);
5264 Py_VISIT(self->file);
5265 Py_VISIT(self->memo);
5266 Py_VISIT(self->stack);
5267 Py_VISIT(self->pers_func);
5268 Py_VISIT(self->arg);
5269 Py_VISIT(self->last_string);
5270 Py_VISIT(self->find_class);
5271 return 0;
5274 static int
5275 Unpickler_clear(Unpicklerobject *self)
5277 Py_CLEAR(self->readline);
5278 Py_CLEAR(self->read);
5279 Py_CLEAR(self->file);
5280 Py_CLEAR(self->memo);
5281 Py_CLEAR(self->stack);
5282 Py_CLEAR(self->pers_func);
5283 Py_CLEAR(self->arg);
5284 Py_CLEAR(self->last_string);
5285 Py_CLEAR(self->find_class);
5286 return 0;
5289 static PyObject *
5290 Unpickler_getattr(Unpicklerobject *self, char *name)
5292 if (!strcmp(name, "persistent_load")) {
5293 if (!self->pers_func) {
5294 PyErr_SetString(PyExc_AttributeError, name);
5295 return NULL;
5298 Py_INCREF(self->pers_func);
5299 return self->pers_func;
5302 if (!strcmp(name, "find_global")) {
5303 if (!self->find_class) {
5304 PyErr_SetString(PyExc_AttributeError, name);
5305 return NULL;
5308 Py_INCREF(self->find_class);
5309 return self->find_class;
5312 if (!strcmp(name, "memo")) {
5313 if (!self->memo) {
5314 PyErr_SetString(PyExc_AttributeError, name);
5315 return NULL;
5318 Py_INCREF(self->memo);
5319 return self->memo;
5322 if (!strcmp(name, "UnpicklingError")) {
5323 Py_INCREF(UnpicklingError);
5324 return UnpicklingError;
5327 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5331 static int
5332 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5335 if (!strcmp(name, "persistent_load")) {
5336 Py_XDECREF(self->pers_func);
5337 self->pers_func = value;
5338 Py_XINCREF(value);
5339 return 0;
5342 if (!strcmp(name, "find_global")) {
5343 Py_XDECREF(self->find_class);
5344 self->find_class = value;
5345 Py_XINCREF(value);
5346 return 0;
5349 if (! value) {
5350 PyErr_SetString(PyExc_TypeError,
5351 "attribute deletion is not supported");
5352 return -1;
5355 if (strcmp(name, "memo") == 0) {
5356 if (!PyDict_Check(value)) {
5357 PyErr_SetString(PyExc_TypeError,
5358 "memo must be a dictionary");
5359 return -1;
5361 Py_XDECREF(self->memo);
5362 self->memo = value;
5363 Py_INCREF(value);
5364 return 0;
5367 PyErr_SetString(PyExc_AttributeError, name);
5368 return -1;
5371 /* ---------------------------------------------------------------------------
5372 * Module-level functions.
5375 /* dump(obj, file, protocol=0). */
5376 static PyObject *
5377 cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5379 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5380 PyObject *ob, *file, *res = NULL;
5381 Picklerobject *pickler = 0;
5382 int proto = 0;
5384 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5385 &ob, &file, &proto)))
5386 goto finally;
5388 if (!( pickler = newPicklerobject(file, proto)))
5389 goto finally;
5391 if (dump(pickler, ob) < 0)
5392 goto finally;
5394 Py_INCREF(Py_None);
5395 res = Py_None;
5397 finally:
5398 Py_XDECREF(pickler);
5400 return res;
5404 /* dumps(obj, protocol=0). */
5405 static PyObject *
5406 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5408 static char *kwlist[] = {"obj", "protocol", NULL};
5409 PyObject *ob, *file = 0, *res = NULL;
5410 Picklerobject *pickler = 0;
5411 int proto = 0;
5413 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5414 &ob, &proto)))
5415 goto finally;
5417 if (!( file = PycStringIO->NewOutput(128)))
5418 goto finally;
5420 if (!( pickler = newPicklerobject(file, proto)))
5421 goto finally;
5423 if (dump(pickler, ob) < 0)
5424 goto finally;
5426 res = PycStringIO->cgetvalue(file);
5428 finally:
5429 Py_XDECREF(pickler);
5430 Py_XDECREF(file);
5432 return res;
5436 /* load(fileobj). */
5437 static PyObject *
5438 cpm_load(PyObject *self, PyObject *ob)
5440 Unpicklerobject *unpickler = 0;
5441 PyObject *res = NULL;
5443 if (!( unpickler = newUnpicklerobject(ob)))
5444 goto finally;
5446 res = load(unpickler);
5448 finally:
5449 Py_XDECREF(unpickler);
5451 return res;
5455 /* loads(string) */
5456 static PyObject *
5457 cpm_loads(PyObject *self, PyObject *args)
5459 PyObject *ob, *file = 0, *res = NULL;
5460 Unpicklerobject *unpickler = 0;
5462 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5463 goto finally;
5465 if (!( file = PycStringIO->NewInput(ob)))
5466 goto finally;
5468 if (!( unpickler = newUnpicklerobject(file)))
5469 goto finally;
5471 res = load(unpickler);
5473 finally:
5474 Py_XDECREF(file);
5475 Py_XDECREF(unpickler);
5477 return res;
5481 PyDoc_STRVAR(Unpicklertype__doc__,
5482 "Objects that know how to unpickle");
5484 static PyTypeObject Unpicklertype = {
5485 PyVarObject_HEAD_INIT(NULL, 0)
5486 "cPickle.Unpickler", /*tp_name*/
5487 sizeof(Unpicklerobject), /*tp_basicsize*/
5489 (destructor)Unpickler_dealloc, /* tp_dealloc */
5490 0, /* tp_print */
5491 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5492 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5493 0, /* tp_compare */
5494 0, /* tp_repr */
5495 0, /* tp_as_number */
5496 0, /* tp_as_sequence */
5497 0, /* tp_as_mapping */
5498 0, /* tp_hash */
5499 0, /* tp_call */
5500 0, /* tp_str */
5501 0, /* tp_getattro */
5502 0, /* tp_setattro */
5503 0, /* tp_as_buffer */
5504 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5505 Unpicklertype__doc__, /* tp_doc */
5506 (traverseproc)Unpickler_traverse, /* tp_traverse */
5507 (inquiry)Unpickler_clear, /* tp_clear */
5510 static struct PyMethodDef cPickle_methods[] = {
5511 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5512 PyDoc_STR("dump(obj, file, protocol=0) -- "
5513 "Write an object in pickle format to the given file.\n"
5514 "\n"
5515 "See the Pickler docstring for the meaning of optional argument proto.")
5518 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5519 PyDoc_STR("dumps(obj, protocol=0) -- "
5520 "Return a string containing an object in pickle format.\n"
5521 "\n"
5522 "See the Pickler docstring for the meaning of optional argument proto.")
5525 {"load", (PyCFunction)cpm_load, METH_O,
5526 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5528 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
5529 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5531 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5532 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5533 "\n"
5534 "This takes a file-like object for writing a pickle data stream.\n"
5535 "The optional proto argument tells the pickler to use the given\n"
5536 "protocol; supported protocols are 0, 1, 2. The default\n"
5537 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5538 "only protocol that can be written to a file opened in text\n"
5539 "mode and read back successfully. When using a protocol higher\n"
5540 "than 0, make sure the file is opened in binary mode, both when\n"
5541 "pickling and unpickling.)\n"
5542 "\n"
5543 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5544 "more efficient than protocol 1.\n"
5545 "\n"
5546 "Specifying a negative protocol version selects the highest\n"
5547 "protocol version supported. The higher the protocol used, the\n"
5548 "more recent the version of Python needed to read the pickle\n"
5549 "produced.\n"
5550 "\n"
5551 "The file parameter must have a write() method that accepts a single\n"
5552 "string argument. It can thus be an open file object, a StringIO\n"
5553 "object, or any other custom object that meets this interface.\n")
5556 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
5557 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5559 { NULL, NULL }
5562 static int
5563 init_stuff(PyObject *module_dict)
5565 PyObject *copy_reg, *t, *r;
5567 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5569 if (PyType_Ready(&Unpicklertype) < 0)
5570 return -1;
5571 if (PyType_Ready(&Picklertype) < 0)
5572 return -1;
5574 INIT_STR(__class__);
5575 INIT_STR(__getinitargs__);
5576 INIT_STR(__dict__);
5577 INIT_STR(__getstate__);
5578 INIT_STR(__setstate__);
5579 INIT_STR(__name__);
5580 INIT_STR(__main__);
5581 INIT_STR(__reduce__);
5582 INIT_STR(__reduce_ex__);
5583 INIT_STR(write);
5584 INIT_STR(append);
5585 INIT_STR(read);
5586 INIT_STR(readline);
5587 INIT_STR(copy_reg);
5588 INIT_STR(dispatch_table);
5590 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
5591 return -1;
5593 /* This is special because we want to use a different
5594 one in restricted mode. */
5595 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
5596 if (!dispatch_table) return -1;
5598 extension_registry = PyObject_GetAttrString(copy_reg,
5599 "_extension_registry");
5600 if (!extension_registry) return -1;
5602 inverted_registry = PyObject_GetAttrString(copy_reg,
5603 "_inverted_registry");
5604 if (!inverted_registry) return -1;
5606 extension_cache = PyObject_GetAttrString(copy_reg,
5607 "_extension_cache");
5608 if (!extension_cache) return -1;
5610 Py_DECREF(copy_reg);
5612 if (!(empty_tuple = PyTuple_New(0)))
5613 return -1;
5615 two_tuple = PyTuple_New(2);
5616 if (two_tuple == NULL)
5617 return -1;
5618 /* We use this temp container with no regard to refcounts, or to
5619 * keeping containees alive. Exempt from GC, because we don't
5620 * want anything looking at two_tuple() by magic.
5622 PyObject_GC_UnTrack(two_tuple);
5624 /* Ugh */
5625 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5626 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5627 return -1;
5629 if (!( t=PyDict_New())) return -1;
5630 if (!( r=PyRun_String(
5631 "def __str__(self):\n"
5632 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5633 Py_file_input,
5634 module_dict, t) )) return -1;
5635 Py_DECREF(r);
5637 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5638 if (!PickleError)
5639 return -1;
5641 Py_DECREF(t);
5643 PicklingError = PyErr_NewException("cPickle.PicklingError",
5644 PickleError, NULL);
5645 if (!PicklingError)
5646 return -1;
5648 if (!( t=PyDict_New())) return -1;
5649 if (!( r=PyRun_String(
5650 "def __str__(self):\n"
5651 " a=self.args\n"
5652 " a=a and type(a[0]) or '(what)'\n"
5653 " return 'Cannot pickle %s objects' % a\n"
5654 , Py_file_input,
5655 module_dict, t) )) return -1;
5656 Py_DECREF(r);
5658 if (!( UnpickleableError = PyErr_NewException(
5659 "cPickle.UnpickleableError", PicklingError, t)))
5660 return -1;
5662 Py_DECREF(t);
5664 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5665 PickleError, NULL)))
5666 return -1;
5668 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5669 UnpicklingError, NULL)))
5670 return -1;
5672 if (PyDict_SetItemString(module_dict, "PickleError",
5673 PickleError) < 0)
5674 return -1;
5676 if (PyDict_SetItemString(module_dict, "PicklingError",
5677 PicklingError) < 0)
5678 return -1;
5680 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5681 UnpicklingError) < 0)
5682 return -1;
5684 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5685 UnpickleableError) < 0)
5686 return -1;
5688 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5689 BadPickleGet) < 0)
5690 return -1;
5692 PycString_IMPORT;
5694 return 0;
5697 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5698 #define PyMODINIT_FUNC void
5699 #endif
5700 PyMODINIT_FUNC
5701 initcPickle(void)
5703 PyObject *m, *d, *di, *v, *k;
5704 Py_ssize_t i;
5705 char *rev = "1.71"; /* XXX when does this change? */
5706 PyObject *format_version;
5707 PyObject *compatible_formats;
5709 Py_TYPE(&Picklertype) = &PyType_Type;
5710 Py_TYPE(&Unpicklertype) = &PyType_Type;
5711 Py_TYPE(&PdataType) = &PyType_Type;
5713 /* Initialize some pieces. We need to do this before module creation,
5714 * so we're forced to use a temporary dictionary. :(
5716 di = PyDict_New();
5717 if (!di) return;
5718 if (init_stuff(di) < 0) return;
5720 /* Create the module and add the functions */
5721 m = Py_InitModule4("cPickle", cPickle_methods,
5722 cPickle_module_documentation,
5723 (PyObject*)NULL,PYTHON_API_VERSION);
5724 if (m == NULL)
5725 return;
5727 /* Add some symbolic constants to the module */
5728 d = PyModule_GetDict(m);
5729 v = PyString_FromString(rev);
5730 PyDict_SetItemString(d, "__version__", v);
5731 Py_XDECREF(v);
5733 /* Copy data from di. Waaa. */
5734 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5735 if (PyObject_SetItem(d, k, v) < 0) {
5736 Py_DECREF(di);
5737 return;
5740 Py_DECREF(di);
5742 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5743 if (i < 0)
5744 return;
5746 /* These are purely informational; no code uses them. */
5747 /* File format version we write. */
5748 format_version = PyString_FromString("2.0");
5749 /* Format versions we can read. */
5750 compatible_formats = Py_BuildValue("[sssss]",
5751 "1.0", /* Original protocol 0 */
5752 "1.1", /* Protocol 0 + INST */
5753 "1.2", /* Original protocol 1 */
5754 "1.3", /* Protocol 1 + BINFLOAT */
5755 "2.0"); /* Original protocol 2 */
5756 PyDict_SetItemString(d, "format_version", format_version);
5757 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5758 Py_XDECREF(format_version);
5759 Py_XDECREF(compatible_formats);