Continue removing _PyOS_double_to_string, as mentioned in issue 7117.
[python.git] / Modules / cPickle.c
bloba2261cc9da3f8a1d60a9542fffb5aad9ce8112c5
1 #include "Python.h"
2 #include "cStringIO.h"
3 #include "structmember.h"
5 PyDoc_STRVAR(cPickle_module_documentation,
6 "C implementation and optimization of the Python pickle module.");
8 #ifndef Py_eval_input
9 #include <graminit.h>
10 #define Py_eval_input eval_input
11 #endif /* Py_eval_input */
13 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
15 #define WRITE_BUF_SIZE 256
17 /* Bump this when new opcodes are added to the pickle protocol. */
18 #define HIGHEST_PROTOCOL 2
21 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since
22 * all headers have already been included here, we can safely redefine it.
24 #ifdef UNICODE
25 # undef UNICODE
26 #endif
29 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
30 * docs are in pickletools.py.
32 #define MARK '('
33 #define STOP '.'
34 #define POP '0'
35 #define POP_MARK '1'
36 #define DUP '2'
37 #define FLOAT 'F'
38 #define BINFLOAT 'G'
39 #define INT 'I'
40 #define BININT 'J'
41 #define BININT1 'K'
42 #define LONG 'L'
43 #define BININT2 'M'
44 #define NONE 'N'
45 #define PERSID 'P'
46 #define BINPERSID 'Q'
47 #define REDUCE 'R'
48 #define STRING 'S'
49 #define BINSTRING 'T'
50 #define SHORT_BINSTRING 'U'
51 #define UNICODE 'V'
52 #define BINUNICODE 'X'
53 #define APPEND 'a'
54 #define BUILD 'b'
55 #define GLOBAL 'c'
56 #define DICT 'd'
57 #define EMPTY_DICT '}'
58 #define APPENDS 'e'
59 #define GET 'g'
60 #define BINGET 'h'
61 #define INST 'i'
62 #define LONG_BINGET 'j'
63 #define LIST 'l'
64 #define EMPTY_LIST ']'
65 #define OBJ 'o'
66 #define PUT 'p'
67 #define BINPUT 'q'
68 #define LONG_BINPUT 'r'
69 #define SETITEM 's'
70 #define TUPLE 't'
71 #define EMPTY_TUPLE ')'
72 #define SETITEMS 'u'
74 /* Protocol 2. */
75 #define PROTO '\x80' /* identify pickle protocol */
76 #define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
77 #define EXT1 '\x82' /* push object from extension registry; 1-byte index */
78 #define EXT2 '\x83' /* ditto, but 2-byte index */
79 #define EXT4 '\x84' /* ditto, but 4-byte index */
80 #define TUPLE1 '\x85' /* build 1-tuple from stack top */
81 #define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
82 #define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
83 #define NEWTRUE '\x88' /* push True */
84 #define NEWFALSE '\x89' /* push False */
85 #define LONG1 '\x8a' /* push long from < 256 bytes */
86 #define LONG4 '\x8b' /* push really big long */
88 /* There aren't opcodes -- they're ways to pickle bools before protocol 2,
89 * so that unpicklers written before bools were introduced unpickle them
90 * as ints, but unpicklers after can recognize that bools were intended.
91 * Note that protocol 2 added direct ways to pickle bools.
93 #undef TRUE
94 #define TRUE "I01\n"
95 #undef FALSE
96 #define FALSE "I00\n"
98 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
99 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
100 * break if this gets out of synch with pickle.py, but it's unclear that
101 * would help anything either.
103 #define BATCHSIZE 1000
105 static char MARKv = MARK;
107 static PyObject *PickleError;
108 static PyObject *PicklingError;
109 static PyObject *UnpickleableError;
110 static PyObject *UnpicklingError;
111 static PyObject *BadPickleGet;
113 /* As the name says, an empty tuple. */
114 static PyObject *empty_tuple;
116 /* copy_reg.dispatch_table, {type_object: pickling_function} */
117 static PyObject *dispatch_table;
119 /* For EXT[124] opcodes. */
120 /* copy_reg._extension_registry, {(module_name, function_name): code} */
121 static PyObject *extension_registry;
122 /* copy_reg._inverted_registry, {code: (module_name, function_name)} */
123 static PyObject *inverted_registry;
124 /* copy_reg._extension_cache, {code: object} */
125 static PyObject *extension_cache;
127 /* For looking up name pairs in copy_reg._extension_registry. */
128 static PyObject *two_tuple;
130 static PyObject *__class___str, *__getinitargs___str, *__dict___str,
131 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
132 *__reduce_ex___str,
133 *write_str, *append_str,
134 *read_str, *readline_str, *__main___str,
135 *copyreg_str, *dispatch_table_str;
137 /*************************************************************************
138 Internal Data type for pickle data. */
140 typedef struct {
141 PyObject_HEAD
142 int length; /* number of initial slots in data currently used */
143 int size; /* number of slots in data allocated */
144 PyObject **data;
145 } Pdata;
147 static void
148 Pdata_dealloc(Pdata *self)
150 int i;
151 PyObject **p;
153 for (i = self->length, p = self->data; --i >= 0; p++) {
154 Py_DECREF(*p);
156 if (self->data)
157 free(self->data);
158 PyObject_Del(self);
161 static PyTypeObject PdataType = {
162 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
163 (destructor)Pdata_dealloc,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
167 #define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
169 static PyObject *
170 Pdata_New(void)
172 Pdata *self;
174 if (!(self = PyObject_New(Pdata, &PdataType)))
175 return NULL;
176 self->size = 8;
177 self->length = 0;
178 self->data = malloc(self->size * sizeof(PyObject*));
179 if (self->data)
180 return (PyObject*)self;
181 Py_DECREF(self);
182 return PyErr_NoMemory();
185 static int
186 stackUnderflow(void)
188 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
189 return -1;
192 /* Retain only the initial clearto items. If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
195 static int
196 Pdata_clear(Pdata *self, int clearto)
198 int i;
199 PyObject **p;
201 if (clearto < 0) return stackUnderflow();
202 if (clearto >= self->length) return 0;
204 for (i = self->length, p = self->data + clearto;
205 --i >= clearto;
206 p++) {
207 Py_CLEAR(*p);
209 self->length = clearto;
211 return 0;
214 static int
215 Pdata_grow(Pdata *self)
217 int bigger;
218 size_t nbytes;
219 PyObject **tmp;
221 bigger = self->size << 1;
222 if (bigger <= 0) /* was 0, or new value overflows */
223 goto nomemory;
224 if ((int)(size_t)bigger != bigger)
225 goto nomemory;
226 nbytes = (size_t)bigger * sizeof(PyObject *);
227 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
228 goto nomemory;
229 tmp = realloc(self->data, nbytes);
230 if (tmp == NULL)
231 goto nomemory;
232 self->data = tmp;
233 self->size = bigger;
234 return 0;
236 nomemory:
237 PyErr_NoMemory();
238 return -1;
241 /* D is a Pdata*. Pop the topmost element and store it into V, which
242 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
243 * is raised and V is set to NULL. D and V may be evaluated several times.
245 #define PDATA_POP(D, V) { \
246 if ((D)->length) \
247 (V) = (D)->data[--((D)->length)]; \
248 else { \
249 PyErr_SetString(UnpicklingError, "bad pickle data"); \
250 (V) = NULL; \
254 /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
255 * D. If the Pdata stack can't be grown to hold the new value, both
256 * raise MemoryError and execute "return ER". The difference is in ownership
257 * of O after: _PUSH transfers ownership of O from the caller to the stack
258 * (no incref of O is done, and in case of error O is decrefed), while
259 * _APPEND pushes a new reference.
262 /* Push O on stack D, giving ownership of O to the stack. */
263 #define PDATA_PUSH(D, O, ER) { \
264 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
265 Pdata_grow((Pdata*)(D)) < 0) { \
266 Py_DECREF(O); \
267 return ER; \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
272 /* Push O on stack D, pushing a new reference. */
273 #define PDATA_APPEND(D, O, ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
276 return ER; \
277 Py_INCREF(O); \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
282 static PyObject *
283 Pdata_popTuple(Pdata *self, int start)
285 PyObject *r;
286 int i, j, l;
288 l = self->length-start;
289 r = PyTuple_New(l);
290 if (r == NULL)
291 return NULL;
292 for (i = start, j = 0 ; j < l; i++, j++)
293 PyTuple_SET_ITEM(r, j, self->data[i]);
295 self->length = start;
296 return r;
299 static PyObject *
300 Pdata_popList(Pdata *self, int start)
302 PyObject *r;
303 int i, j, l;
305 l=self->length-start;
306 if (!( r=PyList_New(l))) return NULL;
307 for (i=start, j=0 ; j < l; i++, j++)
308 PyList_SET_ITEM(r, j, self->data[i]);
310 self->length=start;
311 return r;
314 /*************************************************************************/
316 #define ARG_TUP(self, o) { \
317 if (self->arg || (self->arg=PyTuple_New(1))) { \
318 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
319 PyTuple_SET_ITEM(self->arg,0,o); \
321 else { \
322 Py_DECREF(o); \
326 #define FREE_ARG_TUP(self) { \
327 if (Py_REFCNT(self->arg) > 1) { \
328 Py_DECREF(self->arg); \
329 self->arg=NULL; \
333 typedef struct Picklerobject {
334 PyObject_HEAD
335 FILE *fp;
336 PyObject *write;
337 PyObject *file;
338 PyObject *memo;
339 PyObject *arg;
340 PyObject *pers_func;
341 PyObject *inst_pers_func;
343 /* pickle protocol number, >= 0 */
344 int proto;
346 /* bool, true if proto > 0 */
347 int bin;
349 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
350 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
351 char *write_buf;
352 int buf_size;
353 PyObject *dispatch_table;
354 int fast_container; /* count nested container dumps */
355 PyObject *fast_memo;
356 } Picklerobject;
358 #ifndef PY_CPICKLE_FAST_LIMIT
359 #define PY_CPICKLE_FAST_LIMIT 50
360 #endif
362 static PyTypeObject Picklertype;
364 typedef struct Unpicklerobject {
365 PyObject_HEAD
366 FILE *fp;
367 PyObject *file;
368 PyObject *readline;
369 PyObject *read;
370 PyObject *memo;
371 PyObject *arg;
372 Pdata *stack;
373 PyObject *mark;
374 PyObject *pers_func;
375 PyObject *last_string;
376 int *marks;
377 int num_marks;
378 int marks_size;
379 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
380 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
381 int buf_size;
382 char *buf;
383 PyObject *find_class;
384 } Unpicklerobject;
386 static PyTypeObject Unpicklertype;
388 /* Forward decls that need the above structs */
389 static int save(Picklerobject *, PyObject *, int);
390 static int put2(Picklerobject *, PyObject *);
392 static
393 PyObject *
394 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
396 va_list va;
397 PyObject *args=0, *retval=0;
398 va_start(va, format);
400 if (format) args = Py_VaBuildValue(format, va);
401 va_end(va);
402 if (format && ! args) return NULL;
403 if (stringformat && !(retval=PyString_FromString(stringformat)))
404 return NULL;
406 if (retval) {
407 if (args) {
408 PyObject *v;
409 v=PyString_Format(retval, args);
410 Py_DECREF(retval);
411 Py_DECREF(args);
412 if (! v) return NULL;
413 retval=v;
416 else
417 if (args) retval=args;
418 else {
419 PyErr_SetObject(ErrType,Py_None);
420 return NULL;
422 PyErr_SetObject(ErrType,retval);
423 Py_DECREF(retval);
424 return NULL;
427 static int
428 write_file(Picklerobject *self, const char *s, Py_ssize_t n)
430 size_t nbyteswritten;
432 if (s == NULL) {
433 return 0;
436 if (n > INT_MAX) {
437 /* String too large */
438 return -1;
441 PyFile_IncUseCount((PyFileObject *)self->file);
442 Py_BEGIN_ALLOW_THREADS
443 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
444 Py_END_ALLOW_THREADS
445 PyFile_DecUseCount((PyFileObject *)self->file);
446 if (nbyteswritten != (size_t)n) {
447 PyErr_SetFromErrno(PyExc_IOError);
448 return -1;
451 return (int)n;
454 static int
455 write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
457 if (s == NULL) {
458 return 0;
461 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
462 return -1;
465 return (int)n;
468 static int
469 write_none(Picklerobject *self, const char *s, Py_ssize_t n)
471 if (s == NULL) return 0;
472 if (n > INT_MAX) return -1;
473 return (int)n;
476 static int
477 write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
479 PyObject *py_str = 0, *junk = 0;
480 int n;
482 if (_n > INT_MAX)
483 return -1;
484 n = (int)_n;
485 if (s == NULL) {
486 if (!( self->buf_size )) return 0;
487 py_str = PyString_FromStringAndSize(self->write_buf,
488 self->buf_size);
489 if (!py_str)
490 return -1;
492 else {
493 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
494 if (write_other(self, NULL, 0) < 0)
495 return -1;
498 if (n > WRITE_BUF_SIZE) {
499 if (!( py_str =
500 PyString_FromStringAndSize(s, n)))
501 return -1;
503 else {
504 memcpy(self->write_buf + self->buf_size, s, n);
505 self->buf_size += n;
506 return n;
510 if (self->write) {
511 /* object with write method */
512 ARG_TUP(self, py_str);
513 if (self->arg) {
514 junk = PyObject_Call(self->write, self->arg, NULL);
515 FREE_ARG_TUP(self);
517 if (junk) Py_DECREF(junk);
518 else return -1;
520 else
521 PDATA_PUSH(self->file, py_str, -1);
523 self->buf_size = 0;
524 return n;
528 static Py_ssize_t
529 read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
531 size_t nbytesread;
533 if (self->buf_size == 0) {
534 int size;
536 size = ((n < 32) ? 32 : n);
537 if (!( self->buf = (char *)malloc(size))) {
538 PyErr_NoMemory();
539 return -1;
542 self->buf_size = size;
544 else if (n > self->buf_size) {
545 char *newbuf = (char *)realloc(self->buf, n);
546 if (!newbuf) {
547 PyErr_NoMemory();
548 return -1;
550 self->buf = newbuf;
551 self->buf_size = n;
554 PyFile_IncUseCount((PyFileObject *)self->file);
555 Py_BEGIN_ALLOW_THREADS
556 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
557 Py_END_ALLOW_THREADS
558 PyFile_DecUseCount((PyFileObject *)self->file);
559 if (nbytesread != (size_t)n) {
560 if (feof(self->fp)) {
561 PyErr_SetNone(PyExc_EOFError);
562 return -1;
565 PyErr_SetFromErrno(PyExc_IOError);
566 return -1;
569 *s = self->buf;
571 return n;
575 static Py_ssize_t
576 readline_file(Unpicklerobject *self, char **s)
578 int i;
580 if (self->buf_size == 0) {
581 if (!( self->buf = (char *)malloc(40))) {
582 PyErr_NoMemory();
583 return -1;
585 self->buf_size = 40;
588 i = 0;
589 while (1) {
590 int bigger;
591 char *newbuf;
592 for (; i < (self->buf_size - 1); i++) {
593 if (feof(self->fp) ||
594 (self->buf[i] = getc(self->fp)) == '\n') {
595 self->buf[i + 1] = '\0';
596 *s = self->buf;
597 return i + 1;
600 bigger = self->buf_size << 1;
601 if (bigger <= 0) { /* overflow */
602 PyErr_NoMemory();
603 return -1;
605 newbuf = (char *)realloc(self->buf, bigger);
606 if (!newbuf) {
607 PyErr_NoMemory();
608 return -1;
610 self->buf = newbuf;
611 self->buf_size = bigger;
616 static Py_ssize_t
617 read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
619 char *ptr;
621 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
622 PyErr_SetNone(PyExc_EOFError);
623 return -1;
626 *s = ptr;
628 return n;
632 static Py_ssize_t
633 readline_cStringIO(Unpicklerobject *self, char **s)
635 Py_ssize_t n;
636 char *ptr;
638 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
639 return -1;
642 *s = ptr;
644 return n;
648 static Py_ssize_t
649 read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
651 PyObject *bytes, *str=0;
653 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
655 ARG_TUP(self, bytes);
656 if (self->arg) {
657 str = PyObject_Call(self->read, self->arg, NULL);
658 FREE_ARG_TUP(self);
660 if (! str) return -1;
662 Py_XDECREF(self->last_string);
663 self->last_string = str;
665 if (! (*s = PyString_AsString(str))) return -1;
667 if (PyString_GET_SIZE(str) != n) {
668 PyErr_SetNone(PyExc_EOFError);
669 return -1;
672 return n;
676 static Py_ssize_t
677 readline_other(Unpicklerobject *self, char **s)
679 PyObject *str;
680 Py_ssize_t str_size;
682 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
683 return -1;
686 if ((str_size = PyString_Size(str)) < 0)
687 return -1;
689 Py_XDECREF(self->last_string);
690 self->last_string = str;
692 if (! (*s = PyString_AsString(str)))
693 return -1;
695 return str_size;
698 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
699 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
700 * The caller is responsible for free()'ing the return value.
702 static char *
703 pystrndup(const char *s, int n)
705 char *r = (char *)malloc(n+1);
706 if (r == NULL)
707 return (char*)PyErr_NoMemory();
708 memcpy(r, s, n);
709 r[n] = 0;
710 return r;
714 static int
715 get(Picklerobject *self, PyObject *id)
717 PyObject *value, *mv;
718 long c_value;
719 char s[30];
720 size_t len;
722 if (!( mv = PyDict_GetItem(self->memo, id))) {
723 PyErr_SetObject(PyExc_KeyError, id);
724 return -1;
727 if (!( value = PyTuple_GetItem(mv, 0)))
728 return -1;
730 if (!( PyInt_Check(value))) {
731 PyErr_SetString(PicklingError, "no int where int expected in memo");
732 return -1;
734 c_value = PyInt_AS_LONG((PyIntObject*)value);
736 if (!self->bin) {
737 s[0] = GET;
738 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
739 len = strlen(s);
741 else if (Pdata_Check(self->file)) {
742 if (write_other(self, NULL, 0) < 0) return -1;
743 PDATA_APPEND(self->file, mv, -1);
744 return 0;
746 else {
747 if (c_value < 256) {
748 s[0] = BINGET;
749 s[1] = (int)(c_value & 0xff);
750 len = 2;
752 else {
753 s[0] = LONG_BINGET;
754 s[1] = (int)(c_value & 0xff);
755 s[2] = (int)((c_value >> 8) & 0xff);
756 s[3] = (int)((c_value >> 16) & 0xff);
757 s[4] = (int)((c_value >> 24) & 0xff);
758 len = 5;
762 if (self->write_func(self, s, len) < 0)
763 return -1;
765 return 0;
769 static int
770 put(Picklerobject *self, PyObject *ob)
772 if (Py_REFCNT(ob) < 2 || self->fast)
773 return 0;
775 return put2(self, ob);
779 static int
780 put2(Picklerobject *self, PyObject *ob)
782 char c_str[30];
783 int p;
784 size_t len;
785 int res = -1;
786 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
788 if (self->fast)
789 return 0;
791 if ((p = PyDict_Size(self->memo)) < 0)
792 goto finally;
794 /* Make sure memo keys are positive! */
795 /* XXX Why?
796 * XXX And does "positive" really mean non-negative?
797 * XXX pickle.py starts with PUT index 0, not 1. This makes for
798 * XXX gratuitous differences between the pickling modules.
800 p++;
802 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
803 goto finally;
805 if (!( memo_len = PyInt_FromLong(p)))
806 goto finally;
808 if (!( t = PyTuple_New(2)))
809 goto finally;
811 PyTuple_SET_ITEM(t, 0, memo_len);
812 Py_INCREF(memo_len);
813 PyTuple_SET_ITEM(t, 1, ob);
814 Py_INCREF(ob);
816 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
817 goto finally;
819 if (!self->bin) {
820 c_str[0] = PUT;
821 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
822 len = strlen(c_str);
824 else if (Pdata_Check(self->file)) {
825 if (write_other(self, NULL, 0) < 0) return -1;
826 PDATA_APPEND(self->file, memo_len, -1);
827 res=0; /* Job well done ;) */
828 goto finally;
830 else {
831 if (p >= 256) {
832 c_str[0] = LONG_BINPUT;
833 c_str[1] = (int)(p & 0xff);
834 c_str[2] = (int)((p >> 8) & 0xff);
835 c_str[3] = (int)((p >> 16) & 0xff);
836 c_str[4] = (int)((p >> 24) & 0xff);
837 len = 5;
839 else {
840 c_str[0] = BINPUT;
841 c_str[1] = p;
842 len = 2;
846 if (self->write_func(self, c_str, len) < 0)
847 goto finally;
849 res = 0;
851 finally:
852 Py_XDECREF(py_ob_id);
853 Py_XDECREF(memo_len);
854 Py_XDECREF(t);
856 return res;
859 static PyObject *
860 whichmodule(PyObject *global, PyObject *global_name)
862 Py_ssize_t i, j;
863 PyObject *module = 0, *modules_dict = 0,
864 *global_name_attr = 0, *name = 0;
866 module = PyObject_GetAttrString(global, "__module__");
867 if (module)
868 return module;
869 if (PyErr_ExceptionMatches(PyExc_AttributeError))
870 PyErr_Clear();
871 else
872 return NULL;
874 if (!( modules_dict = PySys_GetObject("modules")))
875 return NULL;
877 i = 0;
878 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
880 if (PyObject_Compare(name, __main___str)==0) continue;
882 global_name_attr = PyObject_GetAttr(module, global_name);
883 if (!global_name_attr) {
884 if (PyErr_ExceptionMatches(PyExc_AttributeError))
885 PyErr_Clear();
886 else
887 return NULL;
888 continue;
891 if (global_name_attr != global) {
892 Py_DECREF(global_name_attr);
893 continue;
896 Py_DECREF(global_name_attr);
898 break;
901 /* The following implements the rule in pickle.py added in 1.5
902 that used __main__ if no module is found. I don't actually
903 like this rule. jlf
905 if (!j) {
906 j=1;
907 name=__main___str;
910 Py_INCREF(name);
911 return name;
915 static int
916 fast_save_enter(Picklerobject *self, PyObject *obj)
918 /* if fast_container < 0, we're doing an error exit. */
919 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
920 PyObject *key = NULL;
921 if (self->fast_memo == NULL) {
922 self->fast_memo = PyDict_New();
923 if (self->fast_memo == NULL) {
924 self->fast_container = -1;
925 return 0;
928 key = PyLong_FromVoidPtr(obj);
929 if (key == NULL)
930 return 0;
931 if (PyDict_GetItem(self->fast_memo, key)) {
932 Py_DECREF(key);
933 PyErr_Format(PyExc_ValueError,
934 "fast mode: can't pickle cyclic objects "
935 "including object type %s at %p",
936 Py_TYPE(obj)->tp_name, obj);
937 self->fast_container = -1;
938 return 0;
940 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
941 Py_DECREF(key);
942 self->fast_container = -1;
943 return 0;
945 Py_DECREF(key);
947 return 1;
951 fast_save_leave(Picklerobject *self, PyObject *obj)
953 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
954 PyObject *key = PyLong_FromVoidPtr(obj);
955 if (key == NULL)
956 return 0;
957 if (PyDict_DelItem(self->fast_memo, key) < 0) {
958 Py_DECREF(key);
959 return 0;
961 Py_DECREF(key);
963 return 1;
966 static int
967 save_none(Picklerobject *self, PyObject *args)
969 static char none = NONE;
970 if (self->write_func(self, &none, 1) < 0)
971 return -1;
973 return 0;
976 static int
977 save_bool(Picklerobject *self, PyObject *args)
979 static const char *buf[2] = {FALSE, TRUE};
980 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
981 long l = PyInt_AS_LONG((PyIntObject *)args);
983 if (self->proto >= 2) {
984 char opcode = l ? NEWTRUE : NEWFALSE;
985 if (self->write_func(self, &opcode, 1) < 0)
986 return -1;
988 else if (self->write_func(self, buf[l], len[l]) < 0)
989 return -1;
990 return 0;
993 static int
994 save_int(Picklerobject *self, PyObject *args)
996 char c_str[32];
997 long l = PyInt_AS_LONG((PyIntObject *)args);
998 int len = 0;
1000 if (!self->bin
1001 #if SIZEOF_LONG > 4
1002 || l > 0x7fffffffL
1003 || l < -0x80000000L
1004 #endif
1006 /* Text-mode pickle, or long too big to fit in the 4-byte
1007 * signed BININT format: store as a string.
1009 c_str[0] = INT;
1010 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
1011 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1012 return -1;
1014 else {
1015 /* Binary pickle and l fits in a signed 4-byte int. */
1016 c_str[1] = (int)( l & 0xff);
1017 c_str[2] = (int)((l >> 8) & 0xff);
1018 c_str[3] = (int)((l >> 16) & 0xff);
1019 c_str[4] = (int)((l >> 24) & 0xff);
1021 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1022 if (c_str[2] == 0) {
1023 c_str[0] = BININT1;
1024 len = 2;
1026 else {
1027 c_str[0] = BININT2;
1028 len = 3;
1031 else {
1032 c_str[0] = BININT;
1033 len = 5;
1036 if (self->write_func(self, c_str, len) < 0)
1037 return -1;
1040 return 0;
1044 static int
1045 save_long(Picklerobject *self, PyObject *args)
1047 Py_ssize_t size;
1048 int res = -1;
1049 PyObject *repr = NULL;
1051 static char l = LONG;
1053 if (self->proto >= 2) {
1054 /* Linear-time pickling. */
1055 size_t nbits;
1056 size_t nbytes;
1057 unsigned char *pdata;
1058 char c_str[5];
1059 int i;
1060 int sign = _PyLong_Sign(args);
1062 if (sign == 0) {
1063 /* It's 0 -- an empty bytestring. */
1064 c_str[0] = LONG1;
1065 c_str[1] = 0;
1066 i = self->write_func(self, c_str, 2);
1067 if (i < 0) goto finally;
1068 res = 0;
1069 goto finally;
1071 nbits = _PyLong_NumBits(args);
1072 if (nbits == (size_t)-1 && PyErr_Occurred())
1073 goto finally;
1074 /* How many bytes do we need? There are nbits >> 3 full
1075 * bytes of data, and nbits & 7 leftover bits. If there
1076 * are any leftover bits, then we clearly need another
1077 * byte. Wnat's not so obvious is that we *probably*
1078 * need another byte even if there aren't any leftovers:
1079 * the most-significant bit of the most-significant byte
1080 * acts like a sign bit, and it's usually got a sense
1081 * opposite of the one we need. The exception is longs
1082 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1083 * its own 256's-complement, so has the right sign bit
1084 * even without the extra byte. That's a pain to check
1085 * for in advance, though, so we always grab an extra
1086 * byte at the start, and cut it back later if possible.
1088 nbytes = (nbits >> 3) + 1;
1089 if (nbytes > INT_MAX) {
1090 PyErr_SetString(PyExc_OverflowError, "long too large "
1091 "to pickle");
1092 goto finally;
1094 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1095 if (repr == NULL) goto finally;
1096 pdata = (unsigned char *)PyString_AS_STRING(repr);
1097 i = _PyLong_AsByteArray((PyLongObject *)args,
1098 pdata, nbytes,
1099 1 /* little endian */, 1 /* signed */);
1100 if (i < 0) goto finally;
1101 /* If the long is negative, this may be a byte more than
1102 * needed. This is so iff the MSB is all redundant sign
1103 * bits.
1105 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1106 (pdata[nbytes - 2] & 0x80) != 0)
1107 --nbytes;
1109 if (nbytes < 256) {
1110 c_str[0] = LONG1;
1111 c_str[1] = (char)nbytes;
1112 size = 2;
1114 else {
1115 c_str[0] = LONG4;
1116 size = (int)nbytes;
1117 for (i = 1; i < 5; i++) {
1118 c_str[i] = (char)(size & 0xff);
1119 size >>= 8;
1121 size = 5;
1123 i = self->write_func(self, c_str, size);
1124 if (i < 0) goto finally;
1125 i = self->write_func(self, (char *)pdata, (int)nbytes);
1126 if (i < 0) goto finally;
1127 res = 0;
1128 goto finally;
1131 /* proto < 2: write the repr and newline. This is quadratic-time
1132 * (in the number of digits), in both directions.
1134 if (!( repr = PyObject_Repr(args)))
1135 goto finally;
1137 if ((size = PyString_Size(repr)) < 0)
1138 goto finally;
1140 if (self->write_func(self, &l, 1) < 0)
1141 goto finally;
1143 if (self->write_func(self,
1144 PyString_AS_STRING((PyStringObject *)repr),
1145 size) < 0)
1146 goto finally;
1148 if (self->write_func(self, "\n", 1) < 0)
1149 goto finally;
1151 res = 0;
1153 finally:
1154 Py_XDECREF(repr);
1155 return res;
1159 static int
1160 save_float(Picklerobject *self, PyObject *args)
1162 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1164 if (self->bin) {
1165 char str[9];
1166 str[0] = BINFLOAT;
1167 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1168 return -1;
1169 if (self->write_func(self, str, 9) < 0)
1170 return -1;
1172 else {
1173 int result = -1;
1174 char *buf = NULL;
1175 char op = FLOAT;
1177 if (self->write_func(self, &op, 1) < 0)
1178 goto done;
1180 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
1181 if (!buf) {
1182 PyErr_NoMemory();
1183 goto done;
1186 if (self->write_func(self, buf, strlen(buf)) < 0)
1187 goto done;
1189 if (self->write_func(self, "\n", 1) < 0)
1190 goto done;
1192 result = 0;
1193 done:
1194 PyMem_Free(buf);
1195 return result;
1198 return 0;
1202 static int
1203 save_string(Picklerobject *self, PyObject *args, int doput)
1205 int size, len;
1206 PyObject *repr=0;
1208 if ((size = PyString_Size(args)) < 0)
1209 return -1;
1211 if (!self->bin) {
1212 char *repr_str;
1214 static char string = STRING;
1216 if (!( repr = PyObject_Repr(args)))
1217 return -1;
1219 if ((len = PyString_Size(repr)) < 0)
1220 goto err;
1221 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1223 if (self->write_func(self, &string, 1) < 0)
1224 goto err;
1226 if (self->write_func(self, repr_str, len) < 0)
1227 goto err;
1229 if (self->write_func(self, "\n", 1) < 0)
1230 goto err;
1232 Py_XDECREF(repr);
1234 else {
1235 int i;
1236 char c_str[5];
1238 if ((size = PyString_Size(args)) < 0)
1239 return -1;
1241 if (size < 256) {
1242 c_str[0] = SHORT_BINSTRING;
1243 c_str[1] = size;
1244 len = 2;
1246 else if (size <= INT_MAX) {
1247 c_str[0] = BINSTRING;
1248 for (i = 1; i < 5; i++)
1249 c_str[i] = (int)(size >> ((i - 1) * 8));
1250 len = 5;
1252 else
1253 return -1; /* string too large */
1255 if (self->write_func(self, c_str, len) < 0)
1256 return -1;
1258 if (size > 128 && Pdata_Check(self->file)) {
1259 if (write_other(self, NULL, 0) < 0) return -1;
1260 PDATA_APPEND(self->file, args, -1);
1262 else {
1263 if (self->write_func(self,
1264 PyString_AS_STRING(
1265 (PyStringObject *)args),
1266 size) < 0)
1267 return -1;
1271 if (doput)
1272 if (put(self, args) < 0)
1273 return -1;
1275 return 0;
1277 err:
1278 Py_XDECREF(repr);
1279 return -1;
1283 #ifdef Py_USING_UNICODE
1284 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1285 backslash and newline characters to \uXXXX escapes. */
1286 static PyObject *
1287 modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
1289 PyObject *repr;
1290 char *p;
1291 char *q;
1293 static const char *hexdigit = "0123456789abcdef";
1294 #ifdef Py_UNICODE_WIDE
1295 const Py_ssize_t expandsize = 10;
1296 #else
1297 const Py_ssize_t expandsize = 6;
1298 #endif
1300 if (size > PY_SSIZE_T_MAX / expandsize)
1301 return PyErr_NoMemory();
1303 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1304 if (repr == NULL)
1305 return NULL;
1306 if (size == 0)
1307 return repr;
1309 p = q = PyString_AS_STRING(repr);
1310 while (size-- > 0) {
1311 Py_UNICODE ch = *s++;
1312 #ifdef Py_UNICODE_WIDE
1313 /* Map 32-bit characters to '\Uxxxxxxxx' */
1314 if (ch >= 0x10000) {
1315 *p++ = '\\';
1316 *p++ = 'U';
1317 *p++ = hexdigit[(ch >> 28) & 0xf];
1318 *p++ = hexdigit[(ch >> 24) & 0xf];
1319 *p++ = hexdigit[(ch >> 20) & 0xf];
1320 *p++ = hexdigit[(ch >> 16) & 0xf];
1321 *p++ = hexdigit[(ch >> 12) & 0xf];
1322 *p++ = hexdigit[(ch >> 8) & 0xf];
1323 *p++ = hexdigit[(ch >> 4) & 0xf];
1324 *p++ = hexdigit[ch & 15];
1326 else
1327 #else
1328 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1329 if (ch >= 0xD800 && ch < 0xDC00) {
1330 Py_UNICODE ch2;
1331 Py_UCS4 ucs;
1333 ch2 = *s++;
1334 size--;
1335 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1336 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1337 *p++ = '\\';
1338 *p++ = 'U';
1339 *p++ = hexdigit[(ucs >> 28) & 0xf];
1340 *p++ = hexdigit[(ucs >> 24) & 0xf];
1341 *p++ = hexdigit[(ucs >> 20) & 0xf];
1342 *p++ = hexdigit[(ucs >> 16) & 0xf];
1343 *p++ = hexdigit[(ucs >> 12) & 0xf];
1344 *p++ = hexdigit[(ucs >> 8) & 0xf];
1345 *p++ = hexdigit[(ucs >> 4) & 0xf];
1346 *p++ = hexdigit[ucs & 0xf];
1347 continue;
1349 /* Fall through: isolated surrogates are copied as-is */
1350 s--;
1351 size++;
1353 #endif
1354 /* Map 16-bit characters to '\uxxxx' */
1355 if (ch >= 256 || ch == '\\' || ch == '\n') {
1356 *p++ = '\\';
1357 *p++ = 'u';
1358 *p++ = hexdigit[(ch >> 12) & 0xf];
1359 *p++ = hexdigit[(ch >> 8) & 0xf];
1360 *p++ = hexdigit[(ch >> 4) & 0xf];
1361 *p++ = hexdigit[ch & 15];
1363 /* Copy everything else as-is */
1364 else
1365 *p++ = (char) ch;
1367 *p = '\0';
1368 _PyString_Resize(&repr, p - q);
1369 return repr;
1372 static int
1373 save_unicode(Picklerobject *self, PyObject *args, int doput)
1375 Py_ssize_t size, len;
1376 PyObject *repr=0;
1378 if (!PyUnicode_Check(args))
1379 return -1;
1381 if (!self->bin) {
1382 char *repr_str;
1383 static char string = UNICODE;
1385 repr = modified_EncodeRawUnicodeEscape(
1386 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1387 if (!repr)
1388 return -1;
1390 if ((len = PyString_Size(repr)) < 0)
1391 goto err;
1392 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1394 if (self->write_func(self, &string, 1) < 0)
1395 goto err;
1397 if (self->write_func(self, repr_str, len) < 0)
1398 goto err;
1400 if (self->write_func(self, "\n", 1) < 0)
1401 goto err;
1403 Py_XDECREF(repr);
1405 else {
1406 int i;
1407 char c_str[5];
1409 if (!( repr = PyUnicode_AsUTF8String(args)))
1410 return -1;
1412 if ((size = PyString_Size(repr)) < 0)
1413 goto err;
1414 if (size > INT_MAX)
1415 return -1; /* string too large */
1417 c_str[0] = BINUNICODE;
1418 for (i = 1; i < 5; i++)
1419 c_str[i] = (int)(size >> ((i - 1) * 8));
1420 len = 5;
1422 if (self->write_func(self, c_str, len) < 0)
1423 goto err;
1425 if (size > 128 && Pdata_Check(self->file)) {
1426 if (write_other(self, NULL, 0) < 0)
1427 goto err;
1428 PDATA_APPEND(self->file, repr, -1);
1430 else {
1431 if (self->write_func(self, PyString_AS_STRING(repr),
1432 size) < 0)
1433 goto err;
1436 Py_DECREF(repr);
1439 if (doput)
1440 if (put(self, args) < 0)
1441 return -1;
1443 return 0;
1445 err:
1446 Py_XDECREF(repr);
1447 return -1;
1449 #endif
1451 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1452 static int
1453 store_tuple_elements(Picklerobject *self, PyObject *t, int len)
1455 int i;
1456 int res = -1; /* guilty until proved innocent */
1458 assert(PyTuple_Size(t) == len);
1460 for (i = 0; i < len; i++) {
1461 PyObject *element = PyTuple_GET_ITEM(t, i);
1463 if (element == NULL)
1464 goto finally;
1465 if (save(self, element, 0) < 0)
1466 goto finally;
1468 res = 0;
1470 finally:
1471 return res;
1474 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1475 * used across protocols to minimize the space needed to pickle them.
1476 * Tuples are also the only builtin immutable type that can be recursive
1477 * (a tuple can be reached from itself), and that requires some subtle
1478 * magic so that it works in all cases. IOW, this is a long routine.
1480 static int
1481 save_tuple(Picklerobject *self, PyObject *args)
1483 PyObject *py_tuple_id = NULL;
1484 int len, i;
1485 int res = -1;
1487 static char tuple = TUPLE;
1488 static char pop = POP;
1489 static char pop_mark = POP_MARK;
1490 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1492 if ((len = PyTuple_Size(args)) < 0)
1493 goto finally;
1495 if (len == 0) {
1496 char c_str[2];
1498 if (self->proto) {
1499 c_str[0] = EMPTY_TUPLE;
1500 len = 1;
1502 else {
1503 c_str[0] = MARK;
1504 c_str[1] = TUPLE;
1505 len = 2;
1507 if (self->write_func(self, c_str, len) >= 0)
1508 res = 0;
1509 /* Don't memoize an empty tuple. */
1510 goto finally;
1513 /* A non-empty tuple. */
1515 /* id(tuple) isn't in the memo now. If it shows up there after
1516 * saving the tuple elements, the tuple must be recursive, in
1517 * which case we'll pop everything we put on the stack, and fetch
1518 * its value from the memo.
1520 py_tuple_id = PyLong_FromVoidPtr(args);
1521 if (py_tuple_id == NULL)
1522 goto finally;
1524 if (len <= 3 && self->proto >= 2) {
1525 /* Use TUPLE{1,2,3} opcodes. */
1526 if (store_tuple_elements(self, args, len) < 0)
1527 goto finally;
1528 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1529 /* pop the len elements */
1530 for (i = 0; i < len; ++i)
1531 if (self->write_func(self, &pop, 1) < 0)
1532 goto finally;
1533 /* fetch from memo */
1534 if (get(self, py_tuple_id) < 0)
1535 goto finally;
1536 res = 0;
1537 goto finally;
1539 /* Not recursive. */
1540 if (self->write_func(self, len2opcode + len, 1) < 0)
1541 goto finally;
1542 goto memoize;
1545 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1546 * Generate MARK elt1 elt2 ... TUPLE
1548 if (self->write_func(self, &MARKv, 1) < 0)
1549 goto finally;
1551 if (store_tuple_elements(self, args, len) < 0)
1552 goto finally;
1554 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1555 /* pop the stack stuff we pushed */
1556 if (self->bin) {
1557 if (self->write_func(self, &pop_mark, 1) < 0)
1558 goto finally;
1560 else {
1561 /* Note that we pop one more than len, to remove
1562 * the MARK too.
1564 for (i = 0; i <= len; i++)
1565 if (self->write_func(self, &pop, 1) < 0)
1566 goto finally;
1568 /* fetch from memo */
1569 if (get(self, py_tuple_id) >= 0)
1570 res = 0;
1571 goto finally;
1574 /* Not recursive. */
1575 if (self->write_func(self, &tuple, 1) < 0)
1576 goto finally;
1578 memoize:
1579 if (put(self, args) >= 0)
1580 res = 0;
1582 finally:
1583 Py_XDECREF(py_tuple_id);
1584 return res;
1587 /* iter is an iterator giving items, and we batch up chunks of
1588 * MARK item item ... item APPENDS
1589 * opcode sequences. Calling code should have arranged to first create an
1590 * empty list, or list-like object, for the APPENDS to operate on.
1591 * Returns 0 on success, <0 on error.
1593 static int
1594 batch_list(Picklerobject *self, PyObject *iter)
1596 PyObject *obj = NULL;
1597 PyObject *firstitem = NULL;
1598 int i, n;
1600 static char append = APPEND;
1601 static char appends = APPENDS;
1603 assert(iter != NULL);
1605 if (self->proto == 0) {
1606 /* APPENDS isn't available; do one at a time. */
1607 for (;;) {
1608 obj = PyIter_Next(iter);
1609 if (obj == NULL) {
1610 if (PyErr_Occurred())
1611 return -1;
1612 break;
1614 i = save(self, obj, 0);
1615 Py_DECREF(obj);
1616 if (i < 0)
1617 return -1;
1618 if (self->write_func(self, &append, 1) < 0)
1619 return -1;
1621 return 0;
1624 /* proto > 0: write in batches of BATCHSIZE. */
1625 do {
1626 /* Get first item */
1627 firstitem = PyIter_Next(iter);
1628 if (firstitem == NULL) {
1629 if (PyErr_Occurred())
1630 goto BatchFailed;
1632 /* nothing more to add */
1633 break;
1636 /* Try to get a second item */
1637 obj = PyIter_Next(iter);
1638 if (obj == NULL) {
1639 if (PyErr_Occurred())
1640 goto BatchFailed;
1642 /* Only one item to write */
1643 if (save(self, firstitem, 0) < 0)
1644 goto BatchFailed;
1645 if (self->write_func(self, &append, 1) < 0)
1646 goto BatchFailed;
1647 Py_CLEAR(firstitem);
1648 break;
1651 /* More than one item to write */
1653 /* Pump out MARK, items, APPENDS. */
1654 if (self->write_func(self, &MARKv, 1) < 0)
1655 goto BatchFailed;
1657 if (save(self, firstitem, 0) < 0)
1658 goto BatchFailed;
1659 Py_CLEAR(firstitem);
1660 n = 1;
1662 /* Fetch and save up to BATCHSIZE items */
1663 while (obj) {
1664 if (save(self, obj, 0) < 0)
1665 goto BatchFailed;
1666 Py_CLEAR(obj);
1667 n += 1;
1669 if (n == BATCHSIZE)
1670 break;
1672 obj = PyIter_Next(iter);
1673 if (obj == NULL) {
1674 if (PyErr_Occurred())
1675 goto BatchFailed;
1676 break;
1680 if (self->write_func(self, &appends, 1) < 0)
1681 goto BatchFailed;
1683 } while (n == BATCHSIZE);
1684 return 0;
1686 BatchFailed:
1687 Py_XDECREF(firstitem);
1688 Py_XDECREF(obj);
1689 return -1;
1692 static int
1693 save_list(Picklerobject *self, PyObject *args)
1695 int res = -1;
1696 char s[3];
1697 int len;
1698 PyObject *iter;
1700 if (self->fast && !fast_save_enter(self, args))
1701 goto finally;
1703 /* Create an empty list. */
1704 if (self->bin) {
1705 s[0] = EMPTY_LIST;
1706 len = 1;
1708 else {
1709 s[0] = MARK;
1710 s[1] = LIST;
1711 len = 2;
1714 if (self->write_func(self, s, len) < 0)
1715 goto finally;
1717 /* Get list length, and bow out early if empty. */
1718 if ((len = PyList_Size(args)) < 0)
1719 goto finally;
1721 /* Memoize. */
1722 if (len == 0) {
1723 if (put(self, args) >= 0)
1724 res = 0;
1725 goto finally;
1727 if (put2(self, args) < 0)
1728 goto finally;
1730 /* Materialize the list elements. */
1731 iter = PyObject_GetIter(args);
1732 if (iter == NULL)
1733 goto finally;
1735 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1737 res = batch_list(self, iter);
1738 Py_LeaveRecursiveCall();
1740 Py_DECREF(iter);
1742 finally:
1743 if (self->fast && !fast_save_leave(self, args))
1744 res = -1;
1746 return res;
1750 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1751 * MARK key value ... key value SETITEMS
1752 * opcode sequences. Calling code should have arranged to first create an
1753 * empty dict, or dict-like object, for the SETITEMS to operate on.
1754 * Returns 0 on success, <0 on error.
1756 * This is very much like batch_list(). The difference between saving
1757 * elements directly, and picking apart two-tuples, is so long-winded at
1758 * the C level, though, that attempts to combine these routines were too
1759 * ugly to bear.
1761 static int
1762 batch_dict(Picklerobject *self, PyObject *iter)
1764 PyObject *p = NULL;
1765 PyObject *firstitem = NULL;
1766 int i, n;
1768 static char setitem = SETITEM;
1769 static char setitems = SETITEMS;
1771 assert(iter != NULL);
1773 if (self->proto == 0) {
1774 /* SETITEMS isn't available; do one at a time. */
1775 for (;;) {
1776 p = PyIter_Next(iter);
1777 if (p == NULL) {
1778 if (PyErr_Occurred())
1779 return -1;
1780 break;
1782 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1783 PyErr_SetString(PyExc_TypeError, "dict items "
1784 "iterator must return 2-tuples");
1785 return -1;
1787 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1788 if (i >= 0)
1789 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1790 Py_DECREF(p);
1791 if (i < 0)
1792 return -1;
1793 if (self->write_func(self, &setitem, 1) < 0)
1794 return -1;
1796 return 0;
1799 /* proto > 0: write in batches of BATCHSIZE. */
1800 do {
1801 /* Get first item */
1802 firstitem = PyIter_Next(iter);
1803 if (firstitem == NULL) {
1804 if (PyErr_Occurred())
1805 goto BatchFailed;
1807 /* nothing more to add */
1808 break;
1810 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1811 PyErr_SetString(PyExc_TypeError, "dict items "
1812 "iterator must return 2-tuples");
1813 goto BatchFailed;
1816 /* Try to get a second item */
1817 p = PyIter_Next(iter);
1818 if (p == NULL) {
1819 if (PyErr_Occurred())
1820 goto BatchFailed;
1822 /* Only one item to write */
1823 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1824 goto BatchFailed;
1825 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1826 goto BatchFailed;
1827 if (self->write_func(self, &setitem, 1) < 0)
1828 goto BatchFailed;
1829 Py_CLEAR(firstitem);
1830 break;
1833 /* More than one item to write */
1835 /* Pump out MARK, items, SETITEMS. */
1836 if (self->write_func(self, &MARKv, 1) < 0)
1837 goto BatchFailed;
1839 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1840 goto BatchFailed;
1841 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1842 goto BatchFailed;
1843 Py_CLEAR(firstitem);
1844 n = 1;
1846 /* Fetch and save up to BATCHSIZE items */
1847 while (p) {
1848 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1849 PyErr_SetString(PyExc_TypeError, "dict items "
1850 "iterator must return 2-tuples");
1851 goto BatchFailed;
1853 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1854 goto BatchFailed;
1855 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1856 goto BatchFailed;
1857 Py_CLEAR(p);
1858 n += 1;
1860 if (n == BATCHSIZE)
1861 break;
1863 p = PyIter_Next(iter);
1864 if (p == NULL) {
1865 if (PyErr_Occurred())
1866 goto BatchFailed;
1867 break;
1871 if (self->write_func(self, &setitems, 1) < 0)
1872 goto BatchFailed;
1874 } while (n == BATCHSIZE);
1875 return 0;
1877 BatchFailed:
1878 Py_XDECREF(firstitem);
1879 Py_XDECREF(p);
1880 return -1;
1883 /* This is a variant of batch_dict() above that specializes for dicts, with no
1884 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1885 * MARK key value ... key value SETITEMS
1886 * opcode sequences. Calling code should have arranged to first create an
1887 * empty dict, or dict-like object, for the SETITEMS to operate on.
1888 * Returns 0 on success, -1 on error.
1890 * Note that this currently doesn't work for protocol 0.
1892 static int
1893 batch_dict_exact(Picklerobject *self, PyObject *obj)
1895 PyObject *key = NULL, *value = NULL;
1896 int i;
1897 Py_ssize_t dict_size, ppos = 0;
1899 static char setitem = SETITEM;
1900 static char setitems = SETITEMS;
1902 assert(obj != NULL);
1903 assert(self->proto > 0);
1905 dict_size = PyDict_Size(obj);
1907 /* Special-case len(d) == 1 to save space. */
1908 if (dict_size == 1) {
1909 PyDict_Next(obj, &ppos, &key, &value);
1910 if (save(self, key, 0) < 0)
1911 return -1;
1912 if (save(self, value, 0) < 0)
1913 return -1;
1914 if (self->write_func(self, &setitem, 1) < 0)
1915 return -1;
1916 return 0;
1919 /* Write in batches of BATCHSIZE. */
1920 do {
1921 i = 0;
1922 if (self->write_func(self, &MARKv, 1) < 0)
1923 return -1;
1924 while (PyDict_Next(obj, &ppos, &key, &value)) {
1925 if (save(self, key, 0) < 0)
1926 return -1;
1927 if (save(self, value, 0) < 0)
1928 return -1;
1929 if (++i == BATCHSIZE)
1930 break;
1932 if (self->write_func(self, &setitems, 1) < 0)
1933 return -1;
1934 if (PyDict_Size(obj) != dict_size) {
1935 PyErr_Format(
1936 PyExc_RuntimeError,
1937 "dictionary changed size during iteration");
1938 return -1;
1941 } while (i == BATCHSIZE);
1942 return 0;
1945 static int
1946 save_dict(Picklerobject *self, PyObject *args)
1948 int res = -1;
1949 char s[3];
1950 int len;
1952 if (self->fast && !fast_save_enter(self, args))
1953 goto finally;
1955 /* Create an empty dict. */
1956 if (self->bin) {
1957 s[0] = EMPTY_DICT;
1958 len = 1;
1960 else {
1961 s[0] = MARK;
1962 s[1] = DICT;
1963 len = 2;
1966 if (self->write_func(self, s, len) < 0)
1967 goto finally;
1969 /* Get dict size, and bow out early if empty. */
1970 if ((len = PyDict_Size(args)) < 0)
1971 goto finally;
1973 if (len == 0) {
1974 if (put(self, args) >= 0)
1975 res = 0;
1976 goto finally;
1978 if (put2(self, args) < 0)
1979 goto finally;
1981 /* Materialize the dict items. */
1982 if (PyDict_CheckExact(args) && self->proto > 0) {
1983 /* We can take certain shortcuts if we know this is a dict and
1984 not a dict subclass. */
1985 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1986 res = batch_dict_exact(self, args);
1987 Py_LeaveRecursiveCall();
1989 } else {
1990 PyObject *iter = PyObject_CallMethod(args, "iteritems", "()");
1991 if (iter == NULL)
1992 goto finally;
1993 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
1994 res = batch_dict(self, iter);
1995 Py_LeaveRecursiveCall();
1997 Py_DECREF(iter);
2000 finally:
2001 if (self->fast && !fast_save_leave(self, args))
2002 res = -1;
2004 return res;
2008 static int
2009 save_inst(Picklerobject *self, PyObject *args)
2011 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
2012 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
2013 char *module_str, *name_str;
2014 int module_size, name_size, res = -1;
2016 static char inst = INST, obj = OBJ, build = BUILD;
2018 if (self->fast && !fast_save_enter(self, args))
2019 goto finally;
2021 if (self->write_func(self, &MARKv, 1) < 0)
2022 goto finally;
2024 if (!( class = PyObject_GetAttr(args, __class___str)))
2025 goto finally;
2027 if (self->bin) {
2028 if (save(self, class, 0) < 0)
2029 goto finally;
2032 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
2033 PyObject *element = 0;
2034 int i, len;
2036 if (!( class_args =
2037 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
2038 goto finally;
2040 if ((len = PyObject_Size(class_args)) < 0)
2041 goto finally;
2043 for (i = 0; i < len; i++) {
2044 if (!( element = PySequence_GetItem(class_args, i)))
2045 goto finally;
2047 if (save(self, element, 0) < 0) {
2048 Py_DECREF(element);
2049 goto finally;
2052 Py_DECREF(element);
2055 else {
2056 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2057 PyErr_Clear();
2058 else
2059 goto finally;
2062 if (!self->bin) {
2063 if (!( name = ((PyClassObject *)class)->cl_name )) {
2064 PyErr_SetString(PicklingError, "class has no name");
2065 goto finally;
2068 if (!( module = whichmodule(class, name)))
2069 goto finally;
2072 if ((module_size = PyString_Size(module)) < 0 ||
2073 (name_size = PyString_Size(name)) < 0)
2074 goto finally;
2076 module_str = PyString_AS_STRING((PyStringObject *)module);
2077 name_str = PyString_AS_STRING((PyStringObject *)name);
2079 if (self->write_func(self, &inst, 1) < 0)
2080 goto finally;
2082 if (self->write_func(self, module_str, module_size) < 0)
2083 goto finally;
2085 if (self->write_func(self, "\n", 1) < 0)
2086 goto finally;
2088 if (self->write_func(self, name_str, name_size) < 0)
2089 goto finally;
2091 if (self->write_func(self, "\n", 1) < 0)
2092 goto finally;
2094 else if (self->write_func(self, &obj, 1) < 0) {
2095 goto finally;
2098 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2099 state = PyObject_Call(getstate_func, empty_tuple, NULL);
2100 if (!state)
2101 goto finally;
2103 else {
2104 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2105 PyErr_Clear();
2106 else
2107 goto finally;
2109 if (!( state = PyObject_GetAttr(args, __dict___str))) {
2110 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2111 PyErr_Clear();
2112 else
2113 goto finally;
2114 res = 0;
2115 goto finally;
2119 if (!PyDict_Check(state)) {
2120 if (put2(self, args) < 0)
2121 goto finally;
2123 else {
2124 if (put(self, args) < 0)
2125 goto finally;
2128 if (save(self, state, 0) < 0)
2129 goto finally;
2131 if (self->write_func(self, &build, 1) < 0)
2132 goto finally;
2134 res = 0;
2136 finally:
2137 if (self->fast && !fast_save_leave(self, args))
2138 res = -1;
2140 Py_XDECREF(module);
2141 Py_XDECREF(class);
2142 Py_XDECREF(state);
2143 Py_XDECREF(getinitargs_func);
2144 Py_XDECREF(getstate_func);
2145 Py_XDECREF(class_args);
2147 return res;
2151 static int
2152 save_global(Picklerobject *self, PyObject *args, PyObject *name)
2154 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2155 char *name_str, *module_str;
2156 int module_size, name_size, res = -1;
2158 static char global = GLOBAL;
2160 if (name) {
2161 global_name = name;
2162 Py_INCREF(global_name);
2164 else {
2165 if (!( global_name = PyObject_GetAttr(args, __name___str)))
2166 goto finally;
2169 if (!( module = whichmodule(args, global_name)))
2170 goto finally;
2172 if ((module_size = PyString_Size(module)) < 0 ||
2173 (name_size = PyString_Size(global_name)) < 0)
2174 goto finally;
2176 module_str = PyString_AS_STRING((PyStringObject *)module);
2177 name_str = PyString_AS_STRING((PyStringObject *)global_name);
2179 /* XXX This can be doing a relative import. Clearly it shouldn't,
2180 but I don't know how to stop it. :-( */
2181 mod = PyImport_ImportModule(module_str);
2182 if (mod == NULL) {
2183 cPickle_ErrFormat(PicklingError,
2184 "Can't pickle %s: import of module %s "
2185 "failed",
2186 "OS", args, module);
2187 goto finally;
2189 klass = PyObject_GetAttrString(mod, name_str);
2190 if (klass == NULL) {
2191 cPickle_ErrFormat(PicklingError,
2192 "Can't pickle %s: attribute lookup %s.%s "
2193 "failed",
2194 "OSS", args, module, global_name);
2195 goto finally;
2197 if (klass != args) {
2198 Py_DECREF(klass);
2199 cPickle_ErrFormat(PicklingError,
2200 "Can't pickle %s: it's not the same object "
2201 "as %s.%s",
2202 "OSS", args, module, global_name);
2203 goto finally;
2205 Py_DECREF(klass);
2207 if (self->proto >= 2) {
2208 /* See whether this is in the extension registry, and if
2209 * so generate an EXT opcode.
2211 PyObject *py_code; /* extension code as Python object */
2212 long code; /* extension code as C value */
2213 char c_str[5];
2214 int n;
2216 PyTuple_SET_ITEM(two_tuple, 0, module);
2217 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2218 py_code = PyDict_GetItem(extension_registry, two_tuple);
2219 if (py_code == NULL)
2220 goto gen_global; /* not registered */
2222 /* Verify py_code has the right type and value. */
2223 if (!PyInt_Check(py_code)) {
2224 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2225 "extension code %s isn't an integer",
2226 "OO", args, py_code);
2227 goto finally;
2229 code = PyInt_AS_LONG(py_code);
2230 if (code <= 0 || code > 0x7fffffffL) {
2231 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2232 "extension code %ld is out of range",
2233 "Ol", args, code);
2234 goto finally;
2237 /* Generate an EXT opcode. */
2238 if (code <= 0xff) {
2239 c_str[0] = EXT1;
2240 c_str[1] = (char)code;
2241 n = 2;
2243 else if (code <= 0xffff) {
2244 c_str[0] = EXT2;
2245 c_str[1] = (char)(code & 0xff);
2246 c_str[2] = (char)((code >> 8) & 0xff);
2247 n = 3;
2249 else {
2250 c_str[0] = EXT4;
2251 c_str[1] = (char)(code & 0xff);
2252 c_str[2] = (char)((code >> 8) & 0xff);
2253 c_str[3] = (char)((code >> 16) & 0xff);
2254 c_str[4] = (char)((code >> 24) & 0xff);
2255 n = 5;
2258 if (self->write_func(self, c_str, n) >= 0)
2259 res = 0;
2260 goto finally; /* and don't memoize */
2263 gen_global:
2264 if (self->write_func(self, &global, 1) < 0)
2265 goto finally;
2267 if (self->write_func(self, module_str, module_size) < 0)
2268 goto finally;
2270 if (self->write_func(self, "\n", 1) < 0)
2271 goto finally;
2273 if (self->write_func(self, name_str, name_size) < 0)
2274 goto finally;
2276 if (self->write_func(self, "\n", 1) < 0)
2277 goto finally;
2279 if (put(self, args) < 0)
2280 goto finally;
2282 res = 0;
2284 finally:
2285 Py_XDECREF(module);
2286 Py_XDECREF(global_name);
2287 Py_XDECREF(mod);
2289 return res;
2292 static int
2293 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2295 PyObject *pid = 0;
2296 int size, res = -1;
2298 static char persid = PERSID, binpersid = BINPERSID;
2300 Py_INCREF(args);
2301 ARG_TUP(self, args);
2302 if (self->arg) {
2303 pid = PyObject_Call(f, self->arg, NULL);
2304 FREE_ARG_TUP(self);
2306 if (! pid) return -1;
2308 if (pid != Py_None) {
2309 if (!self->bin) {
2310 if (!PyString_Check(pid)) {
2311 PyErr_SetString(PicklingError,
2312 "persistent id must be string");
2313 goto finally;
2316 if (self->write_func(self, &persid, 1) < 0)
2317 goto finally;
2319 if ((size = PyString_Size(pid)) < 0)
2320 goto finally;
2322 if (self->write_func(self,
2323 PyString_AS_STRING(
2324 (PyStringObject *)pid),
2325 size) < 0)
2326 goto finally;
2328 if (self->write_func(self, "\n", 1) < 0)
2329 goto finally;
2331 res = 1;
2332 goto finally;
2334 else if (save(self, pid, 1) >= 0) {
2335 if (self->write_func(self, &binpersid, 1) < 0)
2336 res = -1;
2337 else
2338 res = 1;
2341 goto finally;
2344 res = 0;
2346 finally:
2347 Py_XDECREF(pid);
2349 return res;
2352 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2353 * appropriate __reduce__ method for ob.
2355 static int
2356 save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
2358 PyObject *callable;
2359 PyObject *argtup;
2360 PyObject *state = NULL;
2361 PyObject *listitems = Py_None;
2362 PyObject *dictitems = Py_None;
2363 Py_ssize_t size;
2365 int use_newobj = self->proto >= 2;
2367 static char reduce = REDUCE;
2368 static char build = BUILD;
2369 static char newobj = NEWOBJ;
2371 size = PyTuple_Size(args);
2372 if (size < 2 || size > 5) {
2373 cPickle_ErrFormat(PicklingError, "tuple returned by "
2374 "%s must contain 2 through 5 elements",
2375 "O", fn);
2376 return -1;
2379 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2380 &callable,
2381 &argtup,
2382 &state,
2383 &listitems,
2384 &dictitems))
2385 return -1;
2387 if (!PyTuple_Check(argtup)) {
2388 cPickle_ErrFormat(PicklingError, "Second element of "
2389 "tuple returned by %s must be a tuple",
2390 "O", fn);
2391 return -1;
2394 if (state == Py_None)
2395 state = NULL;
2397 if (listitems == Py_None)
2398 listitems = NULL;
2399 else if (!PyIter_Check(listitems)) {
2400 cPickle_ErrFormat(PicklingError, "Fourth element of "
2401 "tuple returned by %s must be an iterator, not %s",
2402 "Os", fn, Py_TYPE(listitems)->tp_name);
2403 return -1;
2406 if (dictitems == Py_None)
2407 dictitems = NULL;
2408 else if (!PyIter_Check(dictitems)) {
2409 cPickle_ErrFormat(PicklingError, "Fifth element of "
2410 "tuple returned by %s must be an iterator, not %s",
2411 "Os", fn, Py_TYPE(dictitems)->tp_name);
2412 return -1;
2415 /* Protocol 2 special case: if callable's name is __newobj__, use
2416 * NEWOBJ. This consumes a lot of code.
2418 if (use_newobj) {
2419 PyObject *temp = PyObject_GetAttr(callable, __name___str);
2421 if (temp == NULL) {
2422 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2423 PyErr_Clear();
2424 else
2425 return -1;
2426 use_newobj = 0;
2428 else {
2429 use_newobj = PyString_Check(temp) &&
2430 strcmp(PyString_AS_STRING(temp),
2431 "__newobj__") == 0;
2432 Py_DECREF(temp);
2435 if (use_newobj) {
2436 PyObject *cls;
2437 PyObject *newargtup;
2438 int n, i;
2440 /* Sanity checks. */
2441 n = PyTuple_Size(argtup);
2442 if (n < 1) {
2443 PyErr_SetString(PicklingError, "__newobj__ arglist "
2444 "is empty");
2445 return -1;
2448 cls = PyTuple_GET_ITEM(argtup, 0);
2449 if (! PyObject_HasAttrString(cls, "__new__")) {
2450 PyErr_SetString(PicklingError, "args[0] from "
2451 "__newobj__ args has no __new__");
2452 return -1;
2455 /* XXX How could ob be NULL? */
2456 if (ob != NULL) {
2457 PyObject *ob_dot_class;
2459 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2460 if (ob_dot_class == NULL) {
2461 if (PyErr_ExceptionMatches(
2462 PyExc_AttributeError))
2463 PyErr_Clear();
2464 else
2465 return -1;
2467 i = ob_dot_class != cls; /* true iff a problem */
2468 Py_XDECREF(ob_dot_class);
2469 if (i) {
2470 PyErr_SetString(PicklingError, "args[0] from "
2471 "__newobj__ args has the wrong class");
2472 return -1;
2476 /* Save the class and its __new__ arguments. */
2477 if (save(self, cls, 0) < 0)
2478 return -1;
2480 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2481 if (newargtup == NULL)
2482 return -1;
2483 for (i = 1; i < n; ++i) {
2484 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2485 Py_INCREF(temp);
2486 PyTuple_SET_ITEM(newargtup, i-1, temp);
2488 i = save(self, newargtup, 0);
2489 Py_DECREF(newargtup);
2490 if (i < 0)
2491 return -1;
2493 /* Add NEWOBJ opcode. */
2494 if (self->write_func(self, &newobj, 1) < 0)
2495 return -1;
2497 else {
2498 /* Not using NEWOBJ. */
2499 if (save(self, callable, 0) < 0 ||
2500 save(self, argtup, 0) < 0 ||
2501 self->write_func(self, &reduce, 1) < 0)
2502 return -1;
2505 /* Memoize. */
2506 /* XXX How can ob be NULL? */
2507 if (ob != NULL) {
2508 if (state && !PyDict_Check(state)) {
2509 if (put2(self, ob) < 0)
2510 return -1;
2512 else if (put(self, ob) < 0)
2513 return -1;
2517 if (listitems && batch_list(self, listitems) < 0)
2518 return -1;
2520 if (dictitems && batch_dict(self, dictitems) < 0)
2521 return -1;
2523 if (state) {
2524 if (save(self, state, 0) < 0 ||
2525 self->write_func(self, &build, 1) < 0)
2526 return -1;
2529 return 0;
2532 static int
2533 save(Picklerobject *self, PyObject *args, int pers_save)
2535 PyTypeObject *type;
2536 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2537 int res = -1;
2538 int tmp;
2540 if (Py_EnterRecursiveCall(" while pickling an object"))
2541 return -1;
2543 if (!pers_save && self->pers_func) {
2544 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2545 res = tmp;
2546 goto finally;
2550 if (args == Py_None) {
2551 res = save_none(self, args);
2552 goto finally;
2555 type = Py_TYPE(args);
2557 switch (type->tp_name[0]) {
2558 case 'b':
2559 if (args == Py_False || args == Py_True) {
2560 res = save_bool(self, args);
2561 goto finally;
2563 break;
2564 case 'i':
2565 if (type == &PyInt_Type) {
2566 res = save_int(self, args);
2567 goto finally;
2569 break;
2571 case 'l':
2572 if (type == &PyLong_Type) {
2573 res = save_long(self, args);
2574 goto finally;
2576 break;
2578 case 'f':
2579 if (type == &PyFloat_Type) {
2580 res = save_float(self, args);
2581 goto finally;
2583 break;
2585 case 't':
2586 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2587 res = save_tuple(self, args);
2588 goto finally;
2590 break;
2592 case 's':
2593 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2594 res = save_string(self, args, 0);
2595 goto finally;
2597 break;
2599 #ifdef Py_USING_UNICODE
2600 case 'u':
2601 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2602 res = save_unicode(self, args, 0);
2603 goto finally;
2605 break;
2606 #endif
2609 if (Py_REFCNT(args) > 1) {
2610 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2611 goto finally;
2613 if (PyDict_GetItem(self->memo, py_ob_id)) {
2614 if (get(self, py_ob_id) < 0)
2615 goto finally;
2617 res = 0;
2618 goto finally;
2622 switch (type->tp_name[0]) {
2623 case 's':
2624 if (type == &PyString_Type) {
2625 res = save_string(self, args, 1);
2626 goto finally;
2628 break;
2630 #ifdef Py_USING_UNICODE
2631 case 'u':
2632 if (type == &PyUnicode_Type) {
2633 res = save_unicode(self, args, 1);
2634 goto finally;
2636 break;
2637 #endif
2639 case 't':
2640 if (type == &PyTuple_Type) {
2641 res = save_tuple(self, args);
2642 goto finally;
2644 if (type == &PyType_Type) {
2645 res = save_global(self, args, NULL);
2646 goto finally;
2648 break;
2650 case 'l':
2651 if (type == &PyList_Type) {
2652 res = save_list(self, args);
2653 goto finally;
2655 break;
2657 case 'd':
2658 if (type == &PyDict_Type) {
2659 res = save_dict(self, args);
2660 goto finally;
2662 break;
2664 case 'i':
2665 if (type == &PyInstance_Type) {
2666 res = save_inst(self, args);
2667 goto finally;
2669 break;
2671 case 'c':
2672 if (type == &PyClass_Type) {
2673 res = save_global(self, args, NULL);
2674 goto finally;
2676 break;
2678 case 'f':
2679 if (type == &PyFunction_Type) {
2680 res = save_global(self, args, NULL);
2681 if (res && PyErr_ExceptionMatches(PickleError)) {
2682 /* fall back to reduce */
2683 PyErr_Clear();
2684 break;
2686 goto finally;
2688 break;
2690 case 'b':
2691 if (type == &PyCFunction_Type) {
2692 res = save_global(self, args, NULL);
2693 goto finally;
2697 if (!pers_save && self->inst_pers_func) {
2698 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2699 res = tmp;
2700 goto finally;
2704 if (PyType_IsSubtype(type, &PyType_Type)) {
2705 res = save_global(self, args, NULL);
2706 goto finally;
2709 /* Get a reduction callable, and call it. This may come from
2710 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2711 * or the object's __reduce__ method.
2713 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2714 if (__reduce__ != NULL) {
2715 Py_INCREF(__reduce__);
2716 Py_INCREF(args);
2717 ARG_TUP(self, args);
2718 if (self->arg) {
2719 t = PyObject_Call(__reduce__, self->arg, NULL);
2720 FREE_ARG_TUP(self);
2723 else {
2724 /* Check for a __reduce_ex__ method. */
2725 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2726 if (__reduce__ != NULL) {
2727 t = PyInt_FromLong(self->proto);
2728 if (t != NULL) {
2729 ARG_TUP(self, t);
2730 t = NULL;
2731 if (self->arg) {
2732 t = PyObject_Call(__reduce__,
2733 self->arg, NULL);
2734 FREE_ARG_TUP(self);
2738 else {
2739 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2740 PyErr_Clear();
2741 else
2742 goto finally;
2743 /* Check for a __reduce__ method. */
2744 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2745 if (__reduce__ != NULL) {
2746 t = PyObject_Call(__reduce__,
2747 empty_tuple, NULL);
2749 else {
2750 PyErr_SetObject(UnpickleableError, args);
2751 goto finally;
2756 if (t == NULL)
2757 goto finally;
2759 if (PyString_Check(t)) {
2760 res = save_global(self, args, t);
2761 goto finally;
2764 if (!PyTuple_Check(t)) {
2765 cPickle_ErrFormat(PicklingError, "Value returned by "
2766 "%s must be string or tuple",
2767 "O", __reduce__);
2768 goto finally;
2771 res = save_reduce(self, t, __reduce__, args);
2773 finally:
2774 Py_LeaveRecursiveCall();
2775 Py_XDECREF(py_ob_id);
2776 Py_XDECREF(__reduce__);
2777 Py_XDECREF(t);
2779 return res;
2783 static int
2784 dump(Picklerobject *self, PyObject *args)
2786 static char stop = STOP;
2788 if (self->proto >= 2) {
2789 char bytes[2];
2791 bytes[0] = PROTO;
2792 assert(self->proto >= 0 && self->proto < 256);
2793 bytes[1] = (char)self->proto;
2794 if (self->write_func(self, bytes, 2) < 0)
2795 return -1;
2798 if (save(self, args, 0) < 0)
2799 return -1;
2801 if (self->write_func(self, &stop, 1) < 0)
2802 return -1;
2804 if (self->write_func(self, NULL, 0) < 0)
2805 return -1;
2807 return 0;
2810 static PyObject *
2811 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2813 if (self->memo)
2814 PyDict_Clear(self->memo);
2815 Py_INCREF(Py_None);
2816 return Py_None;
2819 static PyObject *
2820 Pickle_getvalue(Picklerobject *self, PyObject *args)
2822 int l, i, rsize, ssize, clear=1, lm;
2823 long ik;
2824 PyObject *k, *r;
2825 char *s, *p, *have_get;
2826 Pdata *data;
2828 /* Can be called by Python code or C code */
2829 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2830 return NULL;
2832 /* Check to make sure we are based on a list */
2833 if (! Pdata_Check(self->file)) {
2834 PyErr_SetString(PicklingError,
2835 "Attempt to getvalue() a non-list-based pickler");
2836 return NULL;
2839 /* flush write buffer */
2840 if (write_other(self, NULL, 0) < 0) return NULL;
2842 data=(Pdata*)self->file;
2843 l=data->length;
2845 /* set up an array to hold get/put status */
2846 lm = PyDict_Size(self->memo);
2847 if (lm < 0) return NULL;
2848 lm++;
2849 have_get = malloc(lm);
2850 if (have_get == NULL) return PyErr_NoMemory();
2851 memset(have_get, 0, lm);
2853 /* Scan for gets. */
2854 for (rsize = 0, i = l; --i >= 0; ) {
2855 k = data->data[i];
2857 if (PyString_Check(k))
2858 rsize += PyString_GET_SIZE(k);
2860 else if (PyInt_Check(k)) { /* put */
2861 ik = PyInt_AS_LONG((PyIntObject*)k);
2862 if (ik >= lm || ik == 0) {
2863 PyErr_SetString(PicklingError,
2864 "Invalid get data");
2865 goto err;
2867 if (have_get[ik]) /* with matching get */
2868 rsize += ik < 256 ? 2 : 5;
2871 else if (! (PyTuple_Check(k) &&
2872 PyTuple_GET_SIZE(k) == 2 &&
2873 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2875 PyErr_SetString(PicklingError,
2876 "Unexpected data in internal list");
2877 goto err;
2880 else { /* put */
2881 ik = PyInt_AS_LONG((PyIntObject *)k);
2882 if (ik >= lm || ik == 0) {
2883 PyErr_SetString(PicklingError,
2884 "Invalid get data");
2885 return NULL;
2887 have_get[ik] = 1;
2888 rsize += ik < 256 ? 2 : 5;
2892 /* Now generate the result */
2893 r = PyString_FromStringAndSize(NULL, rsize);
2894 if (r == NULL) goto err;
2895 s = PyString_AS_STRING((PyStringObject *)r);
2897 for (i = 0; i < l; i++) {
2898 k = data->data[i];
2900 if (PyString_Check(k)) {
2901 ssize = PyString_GET_SIZE(k);
2902 if (ssize) {
2903 p=PyString_AS_STRING((PyStringObject *)k);
2904 while (--ssize >= 0)
2905 *s++ = *p++;
2909 else if (PyTuple_Check(k)) { /* get */
2910 ik = PyInt_AS_LONG((PyIntObject *)
2911 PyTuple_GET_ITEM(k, 0));
2912 if (ik < 256) {
2913 *s++ = BINGET;
2914 *s++ = (int)(ik & 0xff);
2916 else {
2917 *s++ = LONG_BINGET;
2918 *s++ = (int)(ik & 0xff);
2919 *s++ = (int)((ik >> 8) & 0xff);
2920 *s++ = (int)((ik >> 16) & 0xff);
2921 *s++ = (int)((ik >> 24) & 0xff);
2925 else { /* put */
2926 ik = PyInt_AS_LONG((PyIntObject*)k);
2928 if (have_get[ik]) { /* with matching get */
2929 if (ik < 256) {
2930 *s++ = BINPUT;
2931 *s++ = (int)(ik & 0xff);
2933 else {
2934 *s++ = LONG_BINPUT;
2935 *s++ = (int)(ik & 0xff);
2936 *s++ = (int)((ik >> 8) & 0xff);
2937 *s++ = (int)((ik >> 16) & 0xff);
2938 *s++ = (int)((ik >> 24) & 0xff);
2944 if (clear) {
2945 PyDict_Clear(self->memo);
2946 Pdata_clear(data, 0);
2949 free(have_get);
2950 return r;
2951 err:
2952 free(have_get);
2953 return NULL;
2956 static PyObject *
2957 Pickler_dump(Picklerobject *self, PyObject *args)
2959 PyObject *ob;
2960 int get=0;
2962 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2963 return NULL;
2965 if (dump(self, ob) < 0)
2966 return NULL;
2968 if (get) return Pickle_getvalue(self, NULL);
2970 /* XXX Why does dump() return self? */
2971 Py_INCREF(self);
2972 return (PyObject*)self;
2976 static struct PyMethodDef Pickler_methods[] =
2978 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2979 PyDoc_STR("dump(object) -- "
2980 "Write an object in pickle format to the object's pickle stream")},
2981 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2982 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2983 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2984 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2985 {NULL, NULL} /* sentinel */
2989 static Picklerobject *
2990 newPicklerobject(PyObject *file, int proto)
2992 Picklerobject *self;
2994 if (proto < 0)
2995 proto = HIGHEST_PROTOCOL;
2996 if (proto > HIGHEST_PROTOCOL) {
2997 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2998 "the highest available protocol is %d",
2999 proto, HIGHEST_PROTOCOL);
3000 return NULL;
3003 self = PyObject_GC_New(Picklerobject, &Picklertype);
3004 if (self == NULL)
3005 return NULL;
3006 self->proto = proto;
3007 self->bin = proto > 0;
3008 self->fp = NULL;
3009 self->write = NULL;
3010 self->memo = NULL;
3011 self->arg = NULL;
3012 self->pers_func = NULL;
3013 self->inst_pers_func = NULL;
3014 self->write_buf = NULL;
3015 self->fast = 0;
3016 self->fast_container = 0;
3017 self->fast_memo = NULL;
3018 self->buf_size = 0;
3019 self->dispatch_table = NULL;
3021 self->file = NULL;
3022 if (file)
3023 Py_INCREF(file);
3024 else {
3025 file = Pdata_New();
3026 if (file == NULL)
3027 goto err;
3029 self->file = file;
3031 if (!( self->memo = PyDict_New()))
3032 goto err;
3034 if (PyFile_Check(file)) {
3035 self->fp = PyFile_AsFile(file);
3036 if (self->fp == NULL) {
3037 PyErr_SetString(PyExc_ValueError,
3038 "I/O operation on closed file");
3039 goto err;
3041 self->write_func = write_file;
3043 else if (PycStringIO_OutputCheck(file)) {
3044 self->write_func = write_cStringIO;
3046 else if (file == Py_None) {
3047 self->write_func = write_none;
3049 else {
3050 self->write_func = write_other;
3052 if (! Pdata_Check(file)) {
3053 self->write = PyObject_GetAttr(file, write_str);
3054 if (!self->write) {
3055 PyErr_Clear();
3056 PyErr_SetString(PyExc_TypeError,
3057 "argument must have 'write' "
3058 "attribute");
3059 goto err;
3063 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
3064 if (self->write_buf == NULL) {
3065 PyErr_NoMemory();
3066 goto err;
3070 if (PyEval_GetRestricted()) {
3071 /* Restricted execution, get private tables */
3072 PyObject *m = PyImport_Import(copyreg_str);
3074 if (m == NULL)
3075 goto err;
3076 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
3077 Py_DECREF(m);
3078 if (self->dispatch_table == NULL)
3079 goto err;
3081 else {
3082 self->dispatch_table = dispatch_table;
3083 Py_INCREF(dispatch_table);
3085 PyObject_GC_Track(self);
3087 return self;
3089 err:
3090 Py_DECREF(self);
3091 return NULL;
3095 static PyObject *
3096 get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
3098 static char *kwlist[] = {"file", "protocol", NULL};
3099 PyObject *file = NULL;
3100 int proto = 0;
3102 /* XXX
3103 * The documented signature is Pickler(file, protocol=0), but this
3104 * accepts Pickler() and Pickler(integer) too. The meaning then
3105 * is clear as mud, undocumented, and not supported by pickle.py.
3106 * I'm told Zope uses this, but I haven't traced into this code
3107 * far enough to figure out what it means.
3109 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
3110 PyErr_Clear();
3111 proto = 0;
3112 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3113 kwlist, &file, &proto))
3114 return NULL;
3116 return (PyObject *)newPicklerobject(file, proto);
3120 static void
3121 Pickler_dealloc(Picklerobject *self)
3123 PyObject_GC_UnTrack(self);
3124 Py_XDECREF(self->write);
3125 Py_XDECREF(self->memo);
3126 Py_XDECREF(self->fast_memo);
3127 Py_XDECREF(self->arg);
3128 Py_XDECREF(self->file);
3129 Py_XDECREF(self->pers_func);
3130 Py_XDECREF(self->inst_pers_func);
3131 Py_XDECREF(self->dispatch_table);
3132 PyMem_Free(self->write_buf);
3133 Py_TYPE(self)->tp_free((PyObject *)self);
3136 static int
3137 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3139 Py_VISIT(self->write);
3140 Py_VISIT(self->memo);
3141 Py_VISIT(self->fast_memo);
3142 Py_VISIT(self->arg);
3143 Py_VISIT(self->file);
3144 Py_VISIT(self->pers_func);
3145 Py_VISIT(self->inst_pers_func);
3146 Py_VISIT(self->dispatch_table);
3147 return 0;
3150 static int
3151 Pickler_clear(Picklerobject *self)
3153 Py_CLEAR(self->write);
3154 Py_CLEAR(self->memo);
3155 Py_CLEAR(self->fast_memo);
3156 Py_CLEAR(self->arg);
3157 Py_CLEAR(self->file);
3158 Py_CLEAR(self->pers_func);
3159 Py_CLEAR(self->inst_pers_func);
3160 Py_CLEAR(self->dispatch_table);
3161 return 0;
3164 static PyObject *
3165 Pickler_get_pers_func(Picklerobject *p)
3167 if (p->pers_func == NULL)
3168 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3169 else
3170 Py_INCREF(p->pers_func);
3171 return p->pers_func;
3174 static int
3175 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3177 if (v == NULL) {
3178 PyErr_SetString(PyExc_TypeError,
3179 "attribute deletion is not supported");
3180 return -1;
3182 Py_XDECREF(p->pers_func);
3183 Py_INCREF(v);
3184 p->pers_func = v;
3185 return 0;
3188 static int
3189 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3191 if (v == NULL) {
3192 PyErr_SetString(PyExc_TypeError,
3193 "attribute deletion is not supported");
3194 return -1;
3196 Py_XDECREF(p->inst_pers_func);
3197 Py_INCREF(v);
3198 p->inst_pers_func = v;
3199 return 0;
3202 static PyObject *
3203 Pickler_get_memo(Picklerobject *p)
3205 if (p->memo == NULL)
3206 PyErr_SetString(PyExc_AttributeError, "memo");
3207 else
3208 Py_INCREF(p->memo);
3209 return p->memo;
3212 static int
3213 Pickler_set_memo(Picklerobject *p, PyObject *v)
3215 if (v == NULL) {
3216 PyErr_SetString(PyExc_TypeError,
3217 "attribute deletion is not supported");
3218 return -1;
3220 if (!PyDict_Check(v)) {
3221 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3222 return -1;
3224 Py_XDECREF(p->memo);
3225 Py_INCREF(v);
3226 p->memo = v;
3227 return 0;
3230 static PyObject *
3231 Pickler_get_error(Picklerobject *p)
3233 /* why is this an attribute on the Pickler? */
3234 Py_INCREF(PicklingError);
3235 return PicklingError;
3238 static PyMemberDef Pickler_members[] = {
3239 {"binary", T_INT, offsetof(Picklerobject, bin)},
3240 {"fast", T_INT, offsetof(Picklerobject, fast)},
3241 {NULL}
3244 static PyGetSetDef Pickler_getsets[] = {
3245 {"persistent_id", (getter)Pickler_get_pers_func,
3246 (setter)Pickler_set_pers_func},
3247 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3248 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3249 {"PicklingError", (getter)Pickler_get_error, NULL},
3250 {NULL}
3253 PyDoc_STRVAR(Picklertype__doc__,
3254 "Objects that know how to pickle objects\n");
3256 static PyTypeObject Picklertype = {
3257 PyVarObject_HEAD_INIT(NULL, 0)
3258 "cPickle.Pickler", /*tp_name*/
3259 sizeof(Picklerobject), /*tp_basicsize*/
3261 (destructor)Pickler_dealloc, /* tp_dealloc */
3262 0, /* tp_print */
3263 0, /* tp_getattr */
3264 0, /* tp_setattr */
3265 0, /* tp_compare */
3266 0, /* tp_repr */
3267 0, /* tp_as_number */
3268 0, /* tp_as_sequence */
3269 0, /* tp_as_mapping */
3270 0, /* tp_hash */
3271 0, /* tp_call */
3272 0, /* tp_str */
3273 PyObject_GenericGetAttr, /* tp_getattro */
3274 PyObject_GenericSetAttr, /* tp_setattro */
3275 0, /* tp_as_buffer */
3276 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3277 Picklertype__doc__, /* tp_doc */
3278 (traverseproc)Pickler_traverse, /* tp_traverse */
3279 (inquiry)Pickler_clear, /* tp_clear */
3280 0, /* tp_richcompare */
3281 0, /* tp_weaklistoffset */
3282 0, /* tp_iter */
3283 0, /* tp_iternext */
3284 Pickler_methods, /* tp_methods */
3285 Pickler_members, /* tp_members */
3286 Pickler_getsets, /* tp_getset */
3289 static PyObject *
3290 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3292 PyObject *global = 0, *module;
3294 if (fc) {
3295 if (fc==Py_None) {
3296 PyErr_SetString(UnpicklingError, "Global and instance "
3297 "pickles are not supported.");
3298 return NULL;
3300 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3301 py_global_name, NULL);
3304 module = PySys_GetObject("modules");
3305 if (module == NULL)
3306 return NULL;
3308 module = PyDict_GetItem(module, py_module_name);
3309 if (module == NULL) {
3310 module = PyImport_Import(py_module_name);
3311 if (!module)
3312 return NULL;
3313 global = PyObject_GetAttr(module, py_global_name);
3314 Py_DECREF(module);
3316 else
3317 global = PyObject_GetAttr(module, py_global_name);
3318 return global;
3321 static int
3322 marker(Unpicklerobject *self)
3324 if (self->num_marks < 1) {
3325 PyErr_SetString(UnpicklingError, "could not find MARK");
3326 return -1;
3329 return self->marks[--self->num_marks];
3333 static int
3334 load_none(Unpicklerobject *self)
3336 PDATA_APPEND(self->stack, Py_None, -1);
3337 return 0;
3340 static int
3341 bad_readline(void)
3343 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3344 return -1;
3347 static int
3348 load_int(Unpicklerobject *self)
3350 PyObject *py_int = 0;
3351 char *endptr, *s;
3352 int len, res = -1;
3353 long l;
3355 if ((len = self->readline_func(self, &s)) < 0) return -1;
3356 if (len < 2) return bad_readline();
3357 if (!( s=pystrndup(s,len))) return -1;
3359 errno = 0;
3360 l = strtol(s, &endptr, 0);
3362 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3363 /* Hm, maybe we've got something long. Let's try reading
3364 it as a Python long object. */
3365 errno = 0;
3366 py_int = PyLong_FromString(s, NULL, 0);
3367 if (py_int == NULL) {
3368 PyErr_SetString(PyExc_ValueError,
3369 "could not convert string to int");
3370 goto finally;
3373 else {
3374 if (len == 3 && (l == 0 || l == 1)) {
3375 if (!( py_int = PyBool_FromLong(l))) goto finally;
3377 else {
3378 if (!( py_int = PyInt_FromLong(l))) goto finally;
3382 free(s);
3383 PDATA_PUSH(self->stack, py_int, -1);
3384 return 0;
3386 finally:
3387 free(s);
3389 return res;
3392 static int
3393 load_bool(Unpicklerobject *self, PyObject *boolean)
3395 assert(boolean == Py_True || boolean == Py_False);
3396 PDATA_APPEND(self->stack, boolean, -1);
3397 return 0;
3400 /* s contains x bytes of a little-endian integer. Return its value as a
3401 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3402 * int, but when x is 4 it's a signed one. This is an historical source
3403 * of x-platform bugs.
3405 static long
3406 calc_binint(char *s, int x)
3408 unsigned char c;
3409 int i;
3410 long l;
3412 for (i = 0, l = 0L; i < x; i++) {
3413 c = (unsigned char)s[i];
3414 l |= (long)c << (i * 8);
3416 #if SIZEOF_LONG > 4
3417 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3418 * is signed, so on a box with longs bigger than 4 bytes we need
3419 * to extend a BININT's sign bit to the full width.
3421 if (x == 4 && l & (1L << 31))
3422 l |= (~0L) << 32;
3423 #endif
3424 return l;
3428 static int
3429 load_binintx(Unpicklerobject *self, char *s, int x)
3431 PyObject *py_int = 0;
3432 long l;
3434 l = calc_binint(s, x);
3436 if (!( py_int = PyInt_FromLong(l)))
3437 return -1;
3439 PDATA_PUSH(self->stack, py_int, -1);
3440 return 0;
3444 static int
3445 load_binint(Unpicklerobject *self)
3447 char *s;
3449 if (self->read_func(self, &s, 4) < 0)
3450 return -1;
3452 return load_binintx(self, s, 4);
3456 static int
3457 load_binint1(Unpicklerobject *self)
3459 char *s;
3461 if (self->read_func(self, &s, 1) < 0)
3462 return -1;
3464 return load_binintx(self, s, 1);
3468 static int
3469 load_binint2(Unpicklerobject *self)
3471 char *s;
3473 if (self->read_func(self, &s, 2) < 0)
3474 return -1;
3476 return load_binintx(self, s, 2);
3479 static int
3480 load_long(Unpicklerobject *self)
3482 PyObject *l = 0;
3483 char *end, *s;
3484 int len, res = -1;
3486 if ((len = self->readline_func(self, &s)) < 0) return -1;
3487 if (len < 2) return bad_readline();
3488 if (!( s=pystrndup(s,len))) return -1;
3490 if (!( l = PyLong_FromString(s, &end, 0)))
3491 goto finally;
3493 free(s);
3494 PDATA_PUSH(self->stack, l, -1);
3495 return 0;
3497 finally:
3498 free(s);
3500 return res;
3503 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3504 * data following.
3506 static int
3507 load_counted_long(Unpicklerobject *self, int size)
3509 Py_ssize_t i;
3510 char *nbytes;
3511 unsigned char *pdata;
3512 PyObject *along;
3514 assert(size == 1 || size == 4);
3515 i = self->read_func(self, &nbytes, size);
3516 if (i < 0) return -1;
3518 size = calc_binint(nbytes, size);
3519 if (size < 0) {
3520 /* Corrupt or hostile pickle -- we never write one like
3521 * this.
3523 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3524 "byte count");
3525 return -1;
3528 if (size == 0)
3529 along = PyLong_FromLong(0L);
3530 else {
3531 /* Read the raw little-endian bytes & convert. */
3532 i = self->read_func(self, (char **)&pdata, size);
3533 if (i < 0) return -1;
3534 along = _PyLong_FromByteArray(pdata, (size_t)size,
3535 1 /* little endian */, 1 /* signed */);
3537 if (along == NULL)
3538 return -1;
3539 PDATA_PUSH(self->stack, along, -1);
3540 return 0;
3543 static int
3544 load_float(Unpicklerobject *self)
3546 PyObject *py_float = 0;
3547 char *endptr, *s;
3548 int len, res = -1;
3549 double d;
3551 if ((len = self->readline_func(self, &s)) < 0) return -1;
3552 if (len < 2) return bad_readline();
3553 if (!( s=pystrndup(s,len))) return -1;
3555 errno = 0;
3556 d = PyOS_ascii_strtod(s, &endptr);
3558 if ((errno == ERANGE && !(fabs(d) <= 1.0)) ||
3559 (endptr[0] != '\n') || (endptr[1] != '\0')) {
3560 PyErr_SetString(PyExc_ValueError,
3561 "could not convert string to float");
3562 goto finally;
3565 if (!( py_float = PyFloat_FromDouble(d)))
3566 goto finally;
3568 free(s);
3569 PDATA_PUSH(self->stack, py_float, -1);
3570 return 0;
3572 finally:
3573 free(s);
3575 return res;
3578 static int
3579 load_binfloat(Unpicklerobject *self)
3581 PyObject *py_float;
3582 double x;
3583 char *p;
3585 if (self->read_func(self, &p, 8) < 0)
3586 return -1;
3588 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3589 if (x == -1.0 && PyErr_Occurred())
3590 return -1;
3592 py_float = PyFloat_FromDouble(x);
3593 if (py_float == NULL)
3594 return -1;
3596 PDATA_PUSH(self->stack, py_float, -1);
3597 return 0;
3600 static int
3601 load_string(Unpicklerobject *self)
3603 PyObject *str = 0;
3604 int len, res = -1;
3605 char *s, *p;
3607 if ((len = self->readline_func(self, &s)) < 0) return -1;
3608 if (len < 2) return bad_readline();
3609 if (!( s=pystrndup(s,len))) return -1;
3612 /* Strip outermost quotes */
3613 while (s[len-1] <= ' ')
3614 len--;
3615 if(s[0]=='"' && s[len-1]=='"'){
3616 s[len-1] = '\0';
3617 p = s + 1 ;
3618 len -= 2;
3619 } else if(s[0]=='\'' && s[len-1]=='\''){
3620 s[len-1] = '\0';
3621 p = s + 1 ;
3622 len -= 2;
3623 } else
3624 goto insecure;
3625 /********************************************/
3627 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3628 free(s);
3629 if (str) {
3630 PDATA_PUSH(self->stack, str, -1);
3631 res = 0;
3633 return res;
3635 insecure:
3636 free(s);
3637 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3638 return -1;
3642 static int
3643 load_binstring(Unpicklerobject *self)
3645 PyObject *py_string = 0;
3646 long l;
3647 char *s;
3649 if (self->read_func(self, &s, 4) < 0) return -1;
3651 l = calc_binint(s, 4);
3652 if (l < 0) {
3653 /* Corrupt or hostile pickle -- we never write one like
3654 * this.
3656 PyErr_SetString(UnpicklingError,
3657 "BINSTRING pickle has negative byte count");
3658 return -1;
3661 if (self->read_func(self, &s, l) < 0)
3662 return -1;
3664 if (!( py_string = PyString_FromStringAndSize(s, l)))
3665 return -1;
3667 PDATA_PUSH(self->stack, py_string, -1);
3668 return 0;
3672 static int
3673 load_short_binstring(Unpicklerobject *self)
3675 PyObject *py_string = 0;
3676 unsigned char l;
3677 char *s;
3679 if (self->read_func(self, &s, 1) < 0)
3680 return -1;
3682 l = (unsigned char)s[0];
3684 if (self->read_func(self, &s, l) < 0) return -1;
3686 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
3688 PDATA_PUSH(self->stack, py_string, -1);
3689 return 0;
3693 #ifdef Py_USING_UNICODE
3694 static int
3695 load_unicode(Unpicklerobject *self)
3697 PyObject *str = 0;
3698 int len, res = -1;
3699 char *s;
3701 if ((len = self->readline_func(self, &s)) < 0) return -1;
3702 if (len < 1) return bad_readline();
3704 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3705 goto finally;
3707 PDATA_PUSH(self->stack, str, -1);
3708 return 0;
3710 finally:
3711 return res;
3713 #endif
3716 #ifdef Py_USING_UNICODE
3717 static int
3718 load_binunicode(Unpicklerobject *self)
3720 PyObject *unicode;
3721 long l;
3722 char *s;
3724 if (self->read_func(self, &s, 4) < 0) return -1;
3726 l = calc_binint(s, 4);
3727 if (l < 0) {
3728 /* Corrupt or hostile pickle -- we never write one like
3729 * this.
3731 PyErr_SetString(UnpicklingError,
3732 "BINUNICODE pickle has negative byte count");
3733 return -1;
3736 if (self->read_func(self, &s, l) < 0)
3737 return -1;
3739 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3740 return -1;
3742 PDATA_PUSH(self->stack, unicode, -1);
3743 return 0;
3745 #endif
3748 static int
3749 load_tuple(Unpicklerobject *self)
3751 PyObject *tup;
3752 int i;
3754 if ((i = marker(self)) < 0) return -1;
3755 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3756 PDATA_PUSH(self->stack, tup, -1);
3757 return 0;
3760 static int
3761 load_counted_tuple(Unpicklerobject *self, int len)
3763 PyObject *tup = PyTuple_New(len);
3765 if (tup == NULL)
3766 return -1;
3768 while (--len >= 0) {
3769 PyObject *element;
3771 PDATA_POP(self->stack, element);
3772 if (element == NULL)
3773 return -1;
3774 PyTuple_SET_ITEM(tup, len, element);
3776 PDATA_PUSH(self->stack, tup, -1);
3777 return 0;
3780 static int
3781 load_empty_list(Unpicklerobject *self)
3783 PyObject *list;
3785 if (!( list=PyList_New(0))) return -1;
3786 PDATA_PUSH(self->stack, list, -1);
3787 return 0;
3790 static int
3791 load_empty_dict(Unpicklerobject *self)
3793 PyObject *dict;
3795 if (!( dict=PyDict_New())) return -1;
3796 PDATA_PUSH(self->stack, dict, -1);
3797 return 0;
3801 static int
3802 load_list(Unpicklerobject *self)
3804 PyObject *list = 0;
3805 int i;
3807 if ((i = marker(self)) < 0) return -1;
3808 if (!( list=Pdata_popList(self->stack, i))) return -1;
3809 PDATA_PUSH(self->stack, list, -1);
3810 return 0;
3813 static int
3814 load_dict(Unpicklerobject *self)
3816 PyObject *dict, *key, *value;
3817 int i, j, k;
3819 if ((i = marker(self)) < 0) return -1;
3820 j=self->stack->length;
3822 if (!( dict = PyDict_New())) return -1;
3824 for (k = i+1; k < j; k += 2) {
3825 key =self->stack->data[k-1];
3826 value=self->stack->data[k ];
3827 if (PyDict_SetItem(dict, key, value) < 0) {
3828 Py_DECREF(dict);
3829 return -1;
3832 Pdata_clear(self->stack, i);
3833 PDATA_PUSH(self->stack, dict, -1);
3834 return 0;
3837 static PyObject *
3838 Instance_New(PyObject *cls, PyObject *args)
3840 PyObject *r = 0;
3842 if (PyClass_Check(cls)) {
3843 int l;
3845 if ((l=PyObject_Size(args)) < 0) goto err;
3846 if (!( l )) {
3847 PyObject *__getinitargs__;
3849 __getinitargs__ = PyObject_GetAttr(cls,
3850 __getinitargs___str);
3851 if (!__getinitargs__) {
3852 /* We have a class with no __getinitargs__,
3853 so bypass usual construction */
3854 PyObject *inst;
3856 PyErr_Clear();
3857 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3858 goto err;
3859 return inst;
3861 Py_DECREF(__getinitargs__);
3864 if ((r=PyInstance_New(cls, args, NULL))) return r;
3865 else goto err;
3868 if ((r=PyObject_CallObject(cls, args))) return r;
3870 err:
3872 PyObject *tp, *v, *tb, *tmp_value;
3874 PyErr_Fetch(&tp, &v, &tb);
3875 tmp_value = v;
3876 /* NULL occurs when there was a KeyboardInterrupt */
3877 if (tmp_value == NULL)
3878 tmp_value = Py_None;
3879 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3880 Py_XDECREF(v);
3881 v=r;
3883 PyErr_Restore(tp,v,tb);
3885 return NULL;
3889 static int
3890 load_obj(Unpicklerobject *self)
3892 PyObject *class, *tup, *obj=0;
3893 int i;
3895 if ((i = marker(self)) < 0) return -1;
3896 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3897 PDATA_POP(self->stack, class);
3898 if (class) {
3899 obj = Instance_New(class, tup);
3900 Py_DECREF(class);
3902 Py_DECREF(tup);
3904 if (! obj) return -1;
3905 PDATA_PUSH(self->stack, obj, -1);
3906 return 0;
3910 static int
3911 load_inst(Unpicklerobject *self)
3913 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3914 int i, len;
3915 char *s;
3917 if ((i = marker(self)) < 0) return -1;
3919 if ((len = self->readline_func(self, &s)) < 0) return -1;
3920 if (len < 2) return bad_readline();
3921 module_name = PyString_FromStringAndSize(s, len - 1);
3922 if (!module_name) return -1;
3924 if ((len = self->readline_func(self, &s)) >= 0) {
3925 if (len < 2) return bad_readline();
3926 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3927 class = find_class(module_name, class_name,
3928 self->find_class);
3929 Py_DECREF(class_name);
3932 Py_DECREF(module_name);
3934 if (! class) return -1;
3936 if ((tup=Pdata_popTuple(self->stack, i))) {
3937 obj = Instance_New(class, tup);
3938 Py_DECREF(tup);
3940 Py_DECREF(class);
3942 if (! obj) return -1;
3944 PDATA_PUSH(self->stack, obj, -1);
3945 return 0;
3948 static int
3949 load_newobj(Unpicklerobject *self)
3951 PyObject *args = NULL;
3952 PyObject *clsraw = NULL;
3953 PyTypeObject *cls; /* clsraw cast to its true type */
3954 PyObject *obj;
3956 /* Stack is ... cls argtuple, and we want to call
3957 * cls.__new__(cls, *argtuple).
3959 PDATA_POP(self->stack, args);
3960 if (args == NULL) goto Fail;
3961 if (! PyTuple_Check(args)) {
3962 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3963 "tuple.");
3964 goto Fail;
3967 PDATA_POP(self->stack, clsraw);
3968 cls = (PyTypeObject *)clsraw;
3969 if (cls == NULL) goto Fail;
3970 if (! PyType_Check(cls)) {
3971 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3972 "isn't a type object");
3973 goto Fail;
3975 if (cls->tp_new == NULL) {
3976 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3977 "has NULL tp_new");
3978 goto Fail;
3981 /* Call __new__. */
3982 obj = cls->tp_new(cls, args, NULL);
3983 if (obj == NULL) goto Fail;
3985 Py_DECREF(args);
3986 Py_DECREF(clsraw);
3987 PDATA_PUSH(self->stack, obj, -1);
3988 return 0;
3990 Fail:
3991 Py_XDECREF(args);
3992 Py_XDECREF(clsraw);
3993 return -1;
3996 static int
3997 load_global(Unpicklerobject *self)
3999 PyObject *class = 0, *module_name = 0, *class_name = 0;
4000 int len;
4001 char *s;
4003 if ((len = self->readline_func(self, &s)) < 0) return -1;
4004 if (len < 2) return bad_readline();
4005 module_name = PyString_FromStringAndSize(s, len - 1);
4006 if (!module_name) return -1;
4008 if ((len = self->readline_func(self, &s)) >= 0) {
4009 if (len < 2) {
4010 Py_DECREF(module_name);
4011 return bad_readline();
4013 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
4014 class = find_class(module_name, class_name,
4015 self->find_class);
4016 Py_DECREF(class_name);
4019 Py_DECREF(module_name);
4021 if (! class) return -1;
4022 PDATA_PUSH(self->stack, class, -1);
4023 return 0;
4027 static int
4028 load_persid(Unpicklerobject *self)
4030 PyObject *pid = 0;
4031 int len;
4032 char *s;
4034 if (self->pers_func) {
4035 if ((len = self->readline_func(self, &s)) < 0) return -1;
4036 if (len < 2) return bad_readline();
4038 pid = PyString_FromStringAndSize(s, len - 1);
4039 if (!pid) return -1;
4041 if (PyList_Check(self->pers_func)) {
4042 if (PyList_Append(self->pers_func, pid) < 0) {
4043 Py_DECREF(pid);
4044 return -1;
4047 else {
4048 ARG_TUP(self, pid);
4049 if (self->arg) {
4050 pid = PyObject_Call(self->pers_func, self->arg,
4051 NULL);
4052 FREE_ARG_TUP(self);
4056 if (! pid) return -1;
4058 PDATA_PUSH(self->stack, pid, -1);
4059 return 0;
4061 else {
4062 PyErr_SetString(UnpicklingError,
4063 "A load persistent id instruction was encountered,\n"
4064 "but no persistent_load function was specified.");
4065 return -1;
4069 static int
4070 load_binpersid(Unpicklerobject *self)
4072 PyObject *pid = 0;
4074 if (self->pers_func) {
4075 PDATA_POP(self->stack, pid);
4076 if (! pid) return -1;
4078 if (PyList_Check(self->pers_func)) {
4079 if (PyList_Append(self->pers_func, pid) < 0) {
4080 Py_DECREF(pid);
4081 return -1;
4084 else {
4085 ARG_TUP(self, pid);
4086 if (self->arg) {
4087 pid = PyObject_Call(self->pers_func, self->arg,
4088 NULL);
4089 FREE_ARG_TUP(self);
4091 if (! pid) return -1;
4094 PDATA_PUSH(self->stack, pid, -1);
4095 return 0;
4097 else {
4098 PyErr_SetString(UnpicklingError,
4099 "A load persistent id instruction was encountered,\n"
4100 "but no persistent_load function was specified.");
4101 return -1;
4106 static int
4107 load_pop(Unpicklerobject *self)
4109 int len = self->stack->length;
4111 /* Note that we split the (pickle.py) stack into two stacks,
4112 an object stack and a mark stack. We have to be clever and
4113 pop the right one. We do this by looking at the top of the
4114 mark stack first, and only signalling a stack underflow if
4115 the object stack is empty and the mark stack doesn't match
4116 our expectations.
4118 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
4119 self->num_marks--;
4120 } else if (len >= 0) {
4121 len--;
4122 Py_DECREF(self->stack->data[len]);
4123 self->stack->length = len;
4124 } else {
4125 return stackUnderflow();
4127 return 0;
4131 static int
4132 load_pop_mark(Unpicklerobject *self)
4134 int i;
4136 if ((i = marker(self)) < 0)
4137 return -1;
4139 Pdata_clear(self->stack, i);
4141 return 0;
4145 static int
4146 load_dup(Unpicklerobject *self)
4148 PyObject *last;
4149 int len;
4151 if ((len = self->stack->length) <= 0) return stackUnderflow();
4152 last=self->stack->data[len-1];
4153 Py_INCREF(last);
4154 PDATA_PUSH(self->stack, last, -1);
4155 return 0;
4159 static int
4160 load_get(Unpicklerobject *self)
4162 PyObject *py_str = 0, *value = 0;
4163 int len;
4164 char *s;
4165 int rc;
4167 if ((len = self->readline_func(self, &s)) < 0) return -1;
4168 if (len < 2) return bad_readline();
4170 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
4172 value = PyDict_GetItem(self->memo, py_str);
4173 if (! value) {
4174 PyErr_SetObject(BadPickleGet, py_str);
4175 rc = -1;
4177 else {
4178 PDATA_APPEND(self->stack, value, -1);
4179 rc = 0;
4182 Py_DECREF(py_str);
4183 return rc;
4187 static int
4188 load_binget(Unpicklerobject *self)
4190 PyObject *py_key = 0, *value = 0;
4191 unsigned char key;
4192 char *s;
4193 int rc;
4195 if (self->read_func(self, &s, 1) < 0) return -1;
4197 key = (unsigned char)s[0];
4198 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4200 value = PyDict_GetItem(self->memo, py_key);
4201 if (! value) {
4202 PyErr_SetObject(BadPickleGet, py_key);
4203 rc = -1;
4205 else {
4206 PDATA_APPEND(self->stack, value, -1);
4207 rc = 0;
4210 Py_DECREF(py_key);
4211 return rc;
4215 static int
4216 load_long_binget(Unpicklerobject *self)
4218 PyObject *py_key = 0, *value = 0;
4219 unsigned char c;
4220 char *s;
4221 long key;
4222 int rc;
4224 if (self->read_func(self, &s, 4) < 0) return -1;
4226 c = (unsigned char)s[0];
4227 key = (long)c;
4228 c = (unsigned char)s[1];
4229 key |= (long)c << 8;
4230 c = (unsigned char)s[2];
4231 key |= (long)c << 16;
4232 c = (unsigned char)s[3];
4233 key |= (long)c << 24;
4235 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4237 value = PyDict_GetItem(self->memo, py_key);
4238 if (! value) {
4239 PyErr_SetObject(BadPickleGet, py_key);
4240 rc = -1;
4242 else {
4243 PDATA_APPEND(self->stack, value, -1);
4244 rc = 0;
4247 Py_DECREF(py_key);
4248 return rc;
4251 /* Push an object from the extension registry (EXT[124]). nbytes is
4252 * the number of bytes following the opcode, holding the index (code) value.
4254 static int
4255 load_extension(Unpicklerobject *self, int nbytes)
4257 char *codebytes; /* the nbytes bytes after the opcode */
4258 long code; /* calc_binint returns long */
4259 PyObject *py_code; /* code as a Python int */
4260 PyObject *obj; /* the object to push */
4261 PyObject *pair; /* (module_name, class_name) */
4262 PyObject *module_name, *class_name;
4264 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4265 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4266 code = calc_binint(codebytes, nbytes);
4267 if (code <= 0) { /* note that 0 is forbidden */
4268 /* Corrupt or hostile pickle. */
4269 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4270 return -1;
4273 /* Look for the code in the cache. */
4274 py_code = PyInt_FromLong(code);
4275 if (py_code == NULL) return -1;
4276 obj = PyDict_GetItem(extension_cache, py_code);
4277 if (obj != NULL) {
4278 /* Bingo. */
4279 Py_DECREF(py_code);
4280 PDATA_APPEND(self->stack, obj, -1);
4281 return 0;
4284 /* Look up the (module_name, class_name) pair. */
4285 pair = PyDict_GetItem(inverted_registry, py_code);
4286 if (pair == NULL) {
4287 Py_DECREF(py_code);
4288 PyErr_Format(PyExc_ValueError, "unregistered extension "
4289 "code %ld", code);
4290 return -1;
4292 /* Since the extension registry is manipulable via Python code,
4293 * confirm that pair is really a 2-tuple of strings.
4295 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4296 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4297 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4298 Py_DECREF(py_code);
4299 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4300 "isn't a 2-tuple of strings", code);
4301 return -1;
4303 /* Load the object. */
4304 obj = find_class(module_name, class_name, self->find_class);
4305 if (obj == NULL) {
4306 Py_DECREF(py_code);
4307 return -1;
4309 /* Cache code -> obj. */
4310 code = PyDict_SetItem(extension_cache, py_code, obj);
4311 Py_DECREF(py_code);
4312 if (code < 0) {
4313 Py_DECREF(obj);
4314 return -1;
4316 PDATA_PUSH(self->stack, obj, -1);
4317 return 0;
4320 static int
4321 load_put(Unpicklerobject *self)
4323 PyObject *py_str = 0, *value = 0;
4324 int len, l;
4325 char *s;
4327 if ((l = self->readline_func(self, &s)) < 0) return -1;
4328 if (l < 2) return bad_readline();
4329 if (!( len=self->stack->length )) return stackUnderflow();
4330 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4331 value=self->stack->data[len-1];
4332 l=PyDict_SetItem(self->memo, py_str, value);
4333 Py_DECREF(py_str);
4334 return l;
4338 static int
4339 load_binput(Unpicklerobject *self)
4341 PyObject *py_key = 0, *value = 0;
4342 unsigned char key;
4343 char *s;
4344 int len;
4346 if (self->read_func(self, &s, 1) < 0) return -1;
4347 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4349 key = (unsigned char)s[0];
4351 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4352 value=self->stack->data[len-1];
4353 len=PyDict_SetItem(self->memo, py_key, value);
4354 Py_DECREF(py_key);
4355 return len;
4359 static int
4360 load_long_binput(Unpicklerobject *self)
4362 PyObject *py_key = 0, *value = 0;
4363 long key;
4364 unsigned char c;
4365 char *s;
4366 int len;
4368 if (self->read_func(self, &s, 4) < 0) return -1;
4369 if (!( len=self->stack->length )) return stackUnderflow();
4371 c = (unsigned char)s[0];
4372 key = (long)c;
4373 c = (unsigned char)s[1];
4374 key |= (long)c << 8;
4375 c = (unsigned char)s[2];
4376 key |= (long)c << 16;
4377 c = (unsigned char)s[3];
4378 key |= (long)c << 24;
4380 if (!( py_key = PyInt_FromLong(key))) return -1;
4381 value=self->stack->data[len-1];
4382 len=PyDict_SetItem(self->memo, py_key, value);
4383 Py_DECREF(py_key);
4384 return len;
4388 static int
4389 do_append(Unpicklerobject *self, int x)
4391 PyObject *value = 0, *list = 0, *append_method = 0;
4392 int len, i;
4394 len=self->stack->length;
4395 if (!( len >= x && x > 0 )) return stackUnderflow();
4396 /* nothing to do */
4397 if (len==x) return 0;
4399 list=self->stack->data[x-1];
4401 if (PyList_Check(list)) {
4402 PyObject *slice;
4403 int list_len;
4405 slice=Pdata_popList(self->stack, x);
4406 if (! slice) return -1;
4407 list_len = PyList_GET_SIZE(list);
4408 i=PyList_SetSlice(list, list_len, list_len, slice);
4409 Py_DECREF(slice);
4410 return i;
4412 else {
4414 if (!( append_method = PyObject_GetAttr(list, append_str)))
4415 return -1;
4417 for (i = x; i < len; i++) {
4418 PyObject *junk;
4420 value=self->stack->data[i];
4421 junk=0;
4422 ARG_TUP(self, value);
4423 if (self->arg) {
4424 junk = PyObject_Call(append_method, self->arg,
4425 NULL);
4426 FREE_ARG_TUP(self);
4428 if (! junk) {
4429 Pdata_clear(self->stack, i+1);
4430 self->stack->length=x;
4431 Py_DECREF(append_method);
4432 return -1;
4434 Py_DECREF(junk);
4436 self->stack->length=x;
4437 Py_DECREF(append_method);
4440 return 0;
4444 static int
4445 load_append(Unpicklerobject *self)
4447 return do_append(self, self->stack->length - 1);
4451 static int
4452 load_appends(Unpicklerobject *self)
4454 return do_append(self, marker(self));
4458 static int
4459 do_setitems(Unpicklerobject *self, int x)
4461 PyObject *value = 0, *key = 0, *dict = 0;
4462 int len, i, r=0;
4464 if (!( (len=self->stack->length) >= x
4465 && x > 0 )) return stackUnderflow();
4467 dict=self->stack->data[x-1];
4469 for (i = x+1; i < len; i += 2) {
4470 key =self->stack->data[i-1];
4471 value=self->stack->data[i ];
4472 if (PyObject_SetItem(dict, key, value) < 0) {
4473 r=-1;
4474 break;
4478 Pdata_clear(self->stack, x);
4480 return r;
4484 static int
4485 load_setitem(Unpicklerobject *self)
4487 return do_setitems(self, self->stack->length - 2);
4490 static int
4491 load_setitems(Unpicklerobject *self)
4493 return do_setitems(self, marker(self));
4497 static int
4498 load_build(Unpicklerobject *self)
4500 PyObject *state, *inst, *slotstate;
4501 PyObject *__setstate__;
4502 PyObject *d_key, *d_value;
4503 Py_ssize_t i;
4504 int res = -1;
4506 /* Stack is ... instance, state. We want to leave instance at
4507 * the stack top, possibly mutated via instance.__setstate__(state).
4509 if (self->stack->length < 2)
4510 return stackUnderflow();
4511 PDATA_POP(self->stack, state);
4512 if (state == NULL)
4513 return -1;
4514 inst = self->stack->data[self->stack->length - 1];
4516 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4517 if (__setstate__ != NULL) {
4518 PyObject *junk = NULL;
4520 /* The explicit __setstate__ is responsible for everything. */
4521 ARG_TUP(self, state);
4522 if (self->arg) {
4523 junk = PyObject_Call(__setstate__, self->arg, NULL);
4524 FREE_ARG_TUP(self);
4526 Py_DECREF(__setstate__);
4527 if (junk == NULL)
4528 return -1;
4529 Py_DECREF(junk);
4530 return 0;
4532 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4533 return -1;
4534 PyErr_Clear();
4536 /* A default __setstate__. First see whether state embeds a
4537 * slot state dict too (a proto 2 addition).
4539 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4540 PyObject *temp = state;
4541 state = PyTuple_GET_ITEM(temp, 0);
4542 slotstate = PyTuple_GET_ITEM(temp, 1);
4543 Py_INCREF(state);
4544 Py_INCREF(slotstate);
4545 Py_DECREF(temp);
4547 else
4548 slotstate = NULL;
4550 /* Set inst.__dict__ from the state dict (if any). */
4551 if (state != Py_None) {
4552 PyObject *dict;
4553 if (! PyDict_Check(state)) {
4554 PyErr_SetString(UnpicklingError, "state is not a "
4555 "dictionary");
4556 goto finally;
4558 dict = PyObject_GetAttr(inst, __dict___str);
4559 if (dict == NULL)
4560 goto finally;
4562 i = 0;
4563 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4564 /* normally the keys for instance attributes are
4565 interned. we should try to do that here. */
4566 Py_INCREF(d_key);
4567 if (PyString_CheckExact(d_key))
4568 PyString_InternInPlace(&d_key);
4569 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
4570 Py_DECREF(d_key);
4571 goto finally;
4573 Py_DECREF(d_key);
4575 Py_DECREF(dict);
4578 /* Also set instance attributes from the slotstate dict (if any). */
4579 if (slotstate != NULL) {
4580 if (! PyDict_Check(slotstate)) {
4581 PyErr_SetString(UnpicklingError, "slot state is not "
4582 "a dictionary");
4583 goto finally;
4585 i = 0;
4586 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4587 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4588 goto finally;
4591 res = 0;
4593 finally:
4594 Py_DECREF(state);
4595 Py_XDECREF(slotstate);
4596 return res;
4600 static int
4601 load_mark(Unpicklerobject *self)
4603 int s;
4605 /* Note that we split the (pickle.py) stack into two stacks, an
4606 object stack and a mark stack. Here we push a mark onto the
4607 mark stack.
4610 if ((self->num_marks + 1) >= self->marks_size) {
4611 int *marks;
4612 s=self->marks_size+20;
4613 if (s <= self->num_marks) s=self->num_marks + 1;
4614 if (self->marks == NULL)
4615 marks=(int *)malloc(s * sizeof(int));
4616 else
4617 marks=(int *)realloc(self->marks,
4618 s * sizeof(int));
4619 if (!marks) {
4620 PyErr_NoMemory();
4621 return -1;
4623 self->marks = marks;
4624 self->marks_size = s;
4627 self->marks[self->num_marks++] = self->stack->length;
4629 return 0;
4632 static int
4633 load_reduce(Unpicklerobject *self)
4635 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4637 PDATA_POP(self->stack, arg_tup);
4638 if (! arg_tup) return -1;
4639 PDATA_POP(self->stack, callable);
4640 if (callable) {
4641 ob = Instance_New(callable, arg_tup);
4642 Py_DECREF(callable);
4644 Py_DECREF(arg_tup);
4646 if (! ob) return -1;
4648 PDATA_PUSH(self->stack, ob, -1);
4649 return 0;
4652 /* Just raises an error if we don't know the protocol specified. PROTO
4653 * is the first opcode for protocols >= 2.
4655 static int
4656 load_proto(Unpicklerobject *self)
4658 int i;
4659 char *protobyte;
4661 i = self->read_func(self, &protobyte, 1);
4662 if (i < 0)
4663 return -1;
4665 i = calc_binint(protobyte, 1);
4666 /* No point checking for < 0, since calc_binint returns an unsigned
4667 * int when chewing on 1 byte.
4669 assert(i >= 0);
4670 if (i <= HIGHEST_PROTOCOL)
4671 return 0;
4673 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4674 return -1;
4677 static PyObject *
4678 load(Unpicklerobject *self)
4680 PyObject *err = 0, *val = 0;
4681 char *s;
4683 self->num_marks = 0;
4684 if (self->stack->length) Pdata_clear(self->stack, 0);
4686 while (1) {
4687 if (self->read_func(self, &s, 1) < 0)
4688 break;
4690 switch (s[0]) {
4691 case NONE:
4692 if (load_none(self) < 0)
4693 break;
4694 continue;
4696 case BININT:
4697 if (load_binint(self) < 0)
4698 break;
4699 continue;
4701 case BININT1:
4702 if (load_binint1(self) < 0)
4703 break;
4704 continue;
4706 case BININT2:
4707 if (load_binint2(self) < 0)
4708 break;
4709 continue;
4711 case INT:
4712 if (load_int(self) < 0)
4713 break;
4714 continue;
4716 case LONG:
4717 if (load_long(self) < 0)
4718 break;
4719 continue;
4721 case LONG1:
4722 if (load_counted_long(self, 1) < 0)
4723 break;
4724 continue;
4726 case LONG4:
4727 if (load_counted_long(self, 4) < 0)
4728 break;
4729 continue;
4731 case FLOAT:
4732 if (load_float(self) < 0)
4733 break;
4734 continue;
4736 case BINFLOAT:
4737 if (load_binfloat(self) < 0)
4738 break;
4739 continue;
4741 case BINSTRING:
4742 if (load_binstring(self) < 0)
4743 break;
4744 continue;
4746 case SHORT_BINSTRING:
4747 if (load_short_binstring(self) < 0)
4748 break;
4749 continue;
4751 case STRING:
4752 if (load_string(self) < 0)
4753 break;
4754 continue;
4756 #ifdef Py_USING_UNICODE
4757 case UNICODE:
4758 if (load_unicode(self) < 0)
4759 break;
4760 continue;
4762 case BINUNICODE:
4763 if (load_binunicode(self) < 0)
4764 break;
4765 continue;
4766 #endif
4768 case EMPTY_TUPLE:
4769 if (load_counted_tuple(self, 0) < 0)
4770 break;
4771 continue;
4773 case TUPLE1:
4774 if (load_counted_tuple(self, 1) < 0)
4775 break;
4776 continue;
4778 case TUPLE2:
4779 if (load_counted_tuple(self, 2) < 0)
4780 break;
4781 continue;
4783 case TUPLE3:
4784 if (load_counted_tuple(self, 3) < 0)
4785 break;
4786 continue;
4788 case TUPLE:
4789 if (load_tuple(self) < 0)
4790 break;
4791 continue;
4793 case EMPTY_LIST:
4794 if (load_empty_list(self) < 0)
4795 break;
4796 continue;
4798 case LIST:
4799 if (load_list(self) < 0)
4800 break;
4801 continue;
4803 case EMPTY_DICT:
4804 if (load_empty_dict(self) < 0)
4805 break;
4806 continue;
4808 case DICT:
4809 if (load_dict(self) < 0)
4810 break;
4811 continue;
4813 case OBJ:
4814 if (load_obj(self) < 0)
4815 break;
4816 continue;
4818 case INST:
4819 if (load_inst(self) < 0)
4820 break;
4821 continue;
4823 case NEWOBJ:
4824 if (load_newobj(self) < 0)
4825 break;
4826 continue;
4828 case GLOBAL:
4829 if (load_global(self) < 0)
4830 break;
4831 continue;
4833 case APPEND:
4834 if (load_append(self) < 0)
4835 break;
4836 continue;
4838 case APPENDS:
4839 if (load_appends(self) < 0)
4840 break;
4841 continue;
4843 case BUILD:
4844 if (load_build(self) < 0)
4845 break;
4846 continue;
4848 case DUP:
4849 if (load_dup(self) < 0)
4850 break;
4851 continue;
4853 case BINGET:
4854 if (load_binget(self) < 0)
4855 break;
4856 continue;
4858 case LONG_BINGET:
4859 if (load_long_binget(self) < 0)
4860 break;
4861 continue;
4863 case GET:
4864 if (load_get(self) < 0)
4865 break;
4866 continue;
4868 case EXT1:
4869 if (load_extension(self, 1) < 0)
4870 break;
4871 continue;
4873 case EXT2:
4874 if (load_extension(self, 2) < 0)
4875 break;
4876 continue;
4878 case EXT4:
4879 if (load_extension(self, 4) < 0)
4880 break;
4881 continue;
4882 case MARK:
4883 if (load_mark(self) < 0)
4884 break;
4885 continue;
4887 case BINPUT:
4888 if (load_binput(self) < 0)
4889 break;
4890 continue;
4892 case LONG_BINPUT:
4893 if (load_long_binput(self) < 0)
4894 break;
4895 continue;
4897 case PUT:
4898 if (load_put(self) < 0)
4899 break;
4900 continue;
4902 case POP:
4903 if (load_pop(self) < 0)
4904 break;
4905 continue;
4907 case POP_MARK:
4908 if (load_pop_mark(self) < 0)
4909 break;
4910 continue;
4912 case SETITEM:
4913 if (load_setitem(self) < 0)
4914 break;
4915 continue;
4917 case SETITEMS:
4918 if (load_setitems(self) < 0)
4919 break;
4920 continue;
4922 case STOP:
4923 break;
4925 case PERSID:
4926 if (load_persid(self) < 0)
4927 break;
4928 continue;
4930 case BINPERSID:
4931 if (load_binpersid(self) < 0)
4932 break;
4933 continue;
4935 case REDUCE:
4936 if (load_reduce(self) < 0)
4937 break;
4938 continue;
4940 case PROTO:
4941 if (load_proto(self) < 0)
4942 break;
4943 continue;
4945 case NEWTRUE:
4946 if (load_bool(self, Py_True) < 0)
4947 break;
4948 continue;
4950 case NEWFALSE:
4951 if (load_bool(self, Py_False) < 0)
4952 break;
4953 continue;
4955 case '\0':
4956 /* end of file */
4957 PyErr_SetNone(PyExc_EOFError);
4958 break;
4960 default:
4961 cPickle_ErrFormat(UnpicklingError,
4962 "invalid load key, '%s'.",
4963 "c", s[0]);
4964 return NULL;
4967 break;
4970 if ((err = PyErr_Occurred())) {
4971 if (err == PyExc_EOFError) {
4972 PyErr_SetNone(PyExc_EOFError);
4974 return NULL;
4977 PDATA_POP(self->stack, val);
4978 return val;
4982 /* No-load functions to support noload, which is used to
4983 find persistent references. */
4985 static int
4986 noload_obj(Unpicklerobject *self)
4988 int i;
4990 if ((i = marker(self)) < 0) return -1;
4991 return Pdata_clear(self->stack, i+1);
4995 static int
4996 noload_inst(Unpicklerobject *self)
4998 int i;
4999 char *s;
5001 if ((i = marker(self)) < 0) return -1;
5002 Pdata_clear(self->stack, i);
5003 if (self->readline_func(self, &s) < 0) return -1;
5004 if (self->readline_func(self, &s) < 0) return -1;
5005 PDATA_APPEND(self->stack, Py_None, -1);
5006 return 0;
5009 static int
5010 noload_newobj(Unpicklerobject *self)
5012 PyObject *obj;
5014 PDATA_POP(self->stack, obj); /* pop argtuple */
5015 if (obj == NULL) return -1;
5016 Py_DECREF(obj);
5018 PDATA_POP(self->stack, obj); /* pop cls */
5019 if (obj == NULL) return -1;
5020 Py_DECREF(obj);
5022 PDATA_APPEND(self->stack, Py_None, -1);
5023 return 0;
5026 static int
5027 noload_global(Unpicklerobject *self)
5029 char *s;
5031 if (self->readline_func(self, &s) < 0) return -1;
5032 if (self->readline_func(self, &s) < 0) return -1;
5033 PDATA_APPEND(self->stack, Py_None,-1);
5034 return 0;
5037 static int
5038 noload_reduce(Unpicklerobject *self)
5041 if (self->stack->length < 2) return stackUnderflow();
5042 Pdata_clear(self->stack, self->stack->length-2);
5043 PDATA_APPEND(self->stack, Py_None,-1);
5044 return 0;
5047 static int
5048 noload_build(Unpicklerobject *self) {
5050 if (self->stack->length < 1) return stackUnderflow();
5051 Pdata_clear(self->stack, self->stack->length-1);
5052 return 0;
5055 static int
5056 noload_extension(Unpicklerobject *self, int nbytes)
5058 char *codebytes;
5060 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
5061 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
5062 PDATA_APPEND(self->stack, Py_None, -1);
5063 return 0;
5066 static int
5067 noload_append(Unpicklerobject *self)
5069 return Pdata_clear(self->stack, self->stack->length - 1);
5072 static int
5073 noload_appends(Unpicklerobject *self)
5075 int i;
5076 if ((i = marker(self)) < 0) return -1;
5077 return Pdata_clear(self->stack, i);
5080 static int
5081 noload_setitem(Unpicklerobject *self)
5083 return Pdata_clear(self->stack, self->stack->length - 2);
5086 static int
5087 noload_setitems(Unpicklerobject *self)
5089 int i;
5090 if ((i = marker(self)) < 0) return -1;
5091 return Pdata_clear(self->stack, i);
5094 static PyObject *
5095 noload(Unpicklerobject *self)
5097 PyObject *err = 0, *val = 0;
5098 char *s;
5100 self->num_marks = 0;
5101 Pdata_clear(self->stack, 0);
5103 while (1) {
5104 if (self->read_func(self, &s, 1) < 0)
5105 break;
5107 switch (s[0]) {
5108 case NONE:
5109 if (load_none(self) < 0)
5110 break;
5111 continue;
5113 case BININT:
5114 if (load_binint(self) < 0)
5115 break;
5116 continue;
5118 case BININT1:
5119 if (load_binint1(self) < 0)
5120 break;
5121 continue;
5123 case BININT2:
5124 if (load_binint2(self) < 0)
5125 break;
5126 continue;
5128 case INT:
5129 if (load_int(self) < 0)
5130 break;
5131 continue;
5133 case LONG:
5134 if (load_long(self) < 0)
5135 break;
5136 continue;
5138 case LONG1:
5139 if (load_counted_long(self, 1) < 0)
5140 break;
5141 continue;
5143 case LONG4:
5144 if (load_counted_long(self, 4) < 0)
5145 break;
5146 continue;
5148 case FLOAT:
5149 if (load_float(self) < 0)
5150 break;
5151 continue;
5153 case BINFLOAT:
5154 if (load_binfloat(self) < 0)
5155 break;
5156 continue;
5158 case BINSTRING:
5159 if (load_binstring(self) < 0)
5160 break;
5161 continue;
5163 case SHORT_BINSTRING:
5164 if (load_short_binstring(self) < 0)
5165 break;
5166 continue;
5168 case STRING:
5169 if (load_string(self) < 0)
5170 break;
5171 continue;
5173 #ifdef Py_USING_UNICODE
5174 case UNICODE:
5175 if (load_unicode(self) < 0)
5176 break;
5177 continue;
5179 case BINUNICODE:
5180 if (load_binunicode(self) < 0)
5181 break;
5182 continue;
5183 #endif
5185 case EMPTY_TUPLE:
5186 if (load_counted_tuple(self, 0) < 0)
5187 break;
5188 continue;
5190 case TUPLE1:
5191 if (load_counted_tuple(self, 1) < 0)
5192 break;
5193 continue;
5195 case TUPLE2:
5196 if (load_counted_tuple(self, 2) < 0)
5197 break;
5198 continue;
5200 case TUPLE3:
5201 if (load_counted_tuple(self, 3) < 0)
5202 break;
5203 continue;
5205 case TUPLE:
5206 if (load_tuple(self) < 0)
5207 break;
5208 continue;
5210 case EMPTY_LIST:
5211 if (load_empty_list(self) < 0)
5212 break;
5213 continue;
5215 case LIST:
5216 if (load_list(self) < 0)
5217 break;
5218 continue;
5220 case EMPTY_DICT:
5221 if (load_empty_dict(self) < 0)
5222 break;
5223 continue;
5225 case DICT:
5226 if (load_dict(self) < 0)
5227 break;
5228 continue;
5230 case OBJ:
5231 if (noload_obj(self) < 0)
5232 break;
5233 continue;
5235 case INST:
5236 if (noload_inst(self) < 0)
5237 break;
5238 continue;
5240 case NEWOBJ:
5241 if (noload_newobj(self) < 0)
5242 break;
5243 continue;
5245 case GLOBAL:
5246 if (noload_global(self) < 0)
5247 break;
5248 continue;
5250 case APPEND:
5251 if (noload_append(self) < 0)
5252 break;
5253 continue;
5255 case APPENDS:
5256 if (noload_appends(self) < 0)
5257 break;
5258 continue;
5260 case BUILD:
5261 if (noload_build(self) < 0)
5262 break;
5263 continue;
5265 case DUP:
5266 if (load_dup(self) < 0)
5267 break;
5268 continue;
5270 case BINGET:
5271 if (load_binget(self) < 0)
5272 break;
5273 continue;
5275 case LONG_BINGET:
5276 if (load_long_binget(self) < 0)
5277 break;
5278 continue;
5280 case GET:
5281 if (load_get(self) < 0)
5282 break;
5283 continue;
5285 case EXT1:
5286 if (noload_extension(self, 1) < 0)
5287 break;
5288 continue;
5290 case EXT2:
5291 if (noload_extension(self, 2) < 0)
5292 break;
5293 continue;
5295 case EXT4:
5296 if (noload_extension(self, 4) < 0)
5297 break;
5298 continue;
5300 case MARK:
5301 if (load_mark(self) < 0)
5302 break;
5303 continue;
5305 case BINPUT:
5306 if (load_binput(self) < 0)
5307 break;
5308 continue;
5310 case LONG_BINPUT:
5311 if (load_long_binput(self) < 0)
5312 break;
5313 continue;
5315 case PUT:
5316 if (load_put(self) < 0)
5317 break;
5318 continue;
5320 case POP:
5321 if (load_pop(self) < 0)
5322 break;
5323 continue;
5325 case POP_MARK:
5326 if (load_pop_mark(self) < 0)
5327 break;
5328 continue;
5330 case SETITEM:
5331 if (noload_setitem(self) < 0)
5332 break;
5333 continue;
5335 case SETITEMS:
5336 if (noload_setitems(self) < 0)
5337 break;
5338 continue;
5340 case STOP:
5341 break;
5343 case PERSID:
5344 if (load_persid(self) < 0)
5345 break;
5346 continue;
5348 case BINPERSID:
5349 if (load_binpersid(self) < 0)
5350 break;
5351 continue;
5353 case REDUCE:
5354 if (noload_reduce(self) < 0)
5355 break;
5356 continue;
5358 case PROTO:
5359 if (load_proto(self) < 0)
5360 break;
5361 continue;
5363 case NEWTRUE:
5364 if (load_bool(self, Py_True) < 0)
5365 break;
5366 continue;
5368 case NEWFALSE:
5369 if (load_bool(self, Py_False) < 0)
5370 break;
5371 continue;
5372 default:
5373 cPickle_ErrFormat(UnpicklingError,
5374 "invalid load key, '%s'.",
5375 "c", s[0]);
5376 return NULL;
5379 break;
5382 if ((err = PyErr_Occurred())) {
5383 if (err == PyExc_EOFError) {
5384 PyErr_SetNone(PyExc_EOFError);
5386 return NULL;
5389 PDATA_POP(self->stack, val);
5390 return val;
5394 static PyObject *
5395 Unpickler_load(Unpicklerobject *self, PyObject *unused)
5397 return load(self);
5400 static PyObject *
5401 Unpickler_noload(Unpicklerobject *self, PyObject *unused)
5403 return noload(self);
5407 static struct PyMethodDef Unpickler_methods[] = {
5408 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
5409 PyDoc_STR("load() -- Load a pickle")
5411 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
5412 PyDoc_STR(
5413 "noload() -- not load a pickle, but go through most of the motions\n"
5414 "\n"
5415 "This function can be used to read past a pickle without instantiating\n"
5416 "any objects or importing any modules. It can also be used to find all\n"
5417 "persistent references without instantiating any objects or importing\n"
5418 "any modules.\n")
5420 {NULL, NULL} /* sentinel */
5424 static Unpicklerobject *
5425 newUnpicklerobject(PyObject *f)
5427 Unpicklerobject *self;
5429 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5430 return NULL;
5432 self->file = NULL;
5433 self->arg = NULL;
5434 self->stack = (Pdata*)Pdata_New();
5435 self->pers_func = NULL;
5436 self->last_string = NULL;
5437 self->marks = NULL;
5438 self->num_marks = 0;
5439 self->marks_size = 0;
5440 self->buf_size = 0;
5441 self->read = NULL;
5442 self->readline = NULL;
5443 self->find_class = NULL;
5445 if (!( self->memo = PyDict_New()))
5446 goto err;
5448 if (!self->stack)
5449 goto err;
5451 Py_INCREF(f);
5452 self->file = f;
5454 /* Set read, readline based on type of f */
5455 if (PyFile_Check(f)) {
5456 self->fp = PyFile_AsFile(f);
5457 if (self->fp == NULL) {
5458 PyErr_SetString(PyExc_ValueError,
5459 "I/O operation on closed file");
5460 goto err;
5462 self->read_func = read_file;
5463 self->readline_func = readline_file;
5465 else if (PycStringIO_InputCheck(f)) {
5466 self->fp = NULL;
5467 self->read_func = read_cStringIO;
5468 self->readline_func = readline_cStringIO;
5470 else {
5472 self->fp = NULL;
5473 self->read_func = read_other;
5474 self->readline_func = readline_other;
5476 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5477 (self->read = PyObject_GetAttr(f, read_str)))) {
5478 PyErr_Clear();
5479 PyErr_SetString( PyExc_TypeError,
5480 "argument must have 'read' and "
5481 "'readline' attributes" );
5482 goto err;
5485 PyObject_GC_Track(self);
5487 return self;
5489 err:
5490 Py_DECREF((PyObject *)self);
5491 return NULL;
5495 static PyObject *
5496 get_Unpickler(PyObject *self, PyObject *file)
5498 return (PyObject *)newUnpicklerobject(file);
5502 static void
5503 Unpickler_dealloc(Unpicklerobject *self)
5505 PyObject_GC_UnTrack((PyObject *)self);
5506 Py_XDECREF(self->readline);
5507 Py_XDECREF(self->read);
5508 Py_XDECREF(self->file);
5509 Py_XDECREF(self->memo);
5510 Py_XDECREF(self->stack);
5511 Py_XDECREF(self->pers_func);
5512 Py_XDECREF(self->arg);
5513 Py_XDECREF(self->last_string);
5514 Py_XDECREF(self->find_class);
5516 if (self->marks) {
5517 free(self->marks);
5520 if (self->buf_size) {
5521 free(self->buf);
5524 Py_TYPE(self)->tp_free((PyObject *)self);
5527 static int
5528 Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5530 Py_VISIT(self->readline);
5531 Py_VISIT(self->read);
5532 Py_VISIT(self->file);
5533 Py_VISIT(self->memo);
5534 Py_VISIT(self->stack);
5535 Py_VISIT(self->pers_func);
5536 Py_VISIT(self->arg);
5537 Py_VISIT(self->last_string);
5538 Py_VISIT(self->find_class);
5539 return 0;
5542 static int
5543 Unpickler_clear(Unpicklerobject *self)
5545 Py_CLEAR(self->readline);
5546 Py_CLEAR(self->read);
5547 Py_CLEAR(self->file);
5548 Py_CLEAR(self->memo);
5549 Py_CLEAR(self->stack);
5550 Py_CLEAR(self->pers_func);
5551 Py_CLEAR(self->arg);
5552 Py_CLEAR(self->last_string);
5553 Py_CLEAR(self->find_class);
5554 return 0;
5557 static PyObject *
5558 Unpickler_getattr(Unpicklerobject *self, char *name)
5560 if (!strcmp(name, "persistent_load")) {
5561 if (!self->pers_func) {
5562 PyErr_SetString(PyExc_AttributeError, name);
5563 return NULL;
5566 Py_INCREF(self->pers_func);
5567 return self->pers_func;
5570 if (!strcmp(name, "find_global")) {
5571 if (!self->find_class) {
5572 PyErr_SetString(PyExc_AttributeError, name);
5573 return NULL;
5576 Py_INCREF(self->find_class);
5577 return self->find_class;
5580 if (!strcmp(name, "memo")) {
5581 if (!self->memo) {
5582 PyErr_SetString(PyExc_AttributeError, name);
5583 return NULL;
5586 Py_INCREF(self->memo);
5587 return self->memo;
5590 if (!strcmp(name, "UnpicklingError")) {
5591 Py_INCREF(UnpicklingError);
5592 return UnpicklingError;
5595 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5599 static int
5600 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5603 if (!strcmp(name, "persistent_load")) {
5604 Py_XDECREF(self->pers_func);
5605 self->pers_func = value;
5606 Py_XINCREF(value);
5607 return 0;
5610 if (!strcmp(name, "find_global")) {
5611 Py_XDECREF(self->find_class);
5612 self->find_class = value;
5613 Py_XINCREF(value);
5614 return 0;
5617 if (! value) {
5618 PyErr_SetString(PyExc_TypeError,
5619 "attribute deletion is not supported");
5620 return -1;
5623 if (strcmp(name, "memo") == 0) {
5624 if (!PyDict_Check(value)) {
5625 PyErr_SetString(PyExc_TypeError,
5626 "memo must be a dictionary");
5627 return -1;
5629 Py_XDECREF(self->memo);
5630 self->memo = value;
5631 Py_INCREF(value);
5632 return 0;
5635 PyErr_SetString(PyExc_AttributeError, name);
5636 return -1;
5639 /* ---------------------------------------------------------------------------
5640 * Module-level functions.
5643 /* dump(obj, file, protocol=0). */
5644 static PyObject *
5645 cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5647 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5648 PyObject *ob, *file, *res = NULL;
5649 Picklerobject *pickler = 0;
5650 int proto = 0;
5652 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5653 &ob, &file, &proto)))
5654 goto finally;
5656 if (!( pickler = newPicklerobject(file, proto)))
5657 goto finally;
5659 if (dump(pickler, ob) < 0)
5660 goto finally;
5662 Py_INCREF(Py_None);
5663 res = Py_None;
5665 finally:
5666 Py_XDECREF(pickler);
5668 return res;
5672 /* dumps(obj, protocol=0). */
5673 static PyObject *
5674 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5676 static char *kwlist[] = {"obj", "protocol", NULL};
5677 PyObject *ob, *file = 0, *res = NULL;
5678 Picklerobject *pickler = 0;
5679 int proto = 0;
5681 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5682 &ob, &proto)))
5683 goto finally;
5685 if (!( file = PycStringIO->NewOutput(128)))
5686 goto finally;
5688 if (!( pickler = newPicklerobject(file, proto)))
5689 goto finally;
5691 if (dump(pickler, ob) < 0)
5692 goto finally;
5694 res = PycStringIO->cgetvalue(file);
5696 finally:
5697 Py_XDECREF(pickler);
5698 Py_XDECREF(file);
5700 return res;
5704 /* load(fileobj). */
5705 static PyObject *
5706 cpm_load(PyObject *self, PyObject *ob)
5708 Unpicklerobject *unpickler = 0;
5709 PyObject *res = NULL;
5711 if (!( unpickler = newUnpicklerobject(ob)))
5712 goto finally;
5714 res = load(unpickler);
5716 finally:
5717 Py_XDECREF(unpickler);
5719 return res;
5723 /* loads(string) */
5724 static PyObject *
5725 cpm_loads(PyObject *self, PyObject *args)
5727 PyObject *ob, *file = 0, *res = NULL;
5728 Unpicklerobject *unpickler = 0;
5730 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5731 goto finally;
5733 if (!( file = PycStringIO->NewInput(ob)))
5734 goto finally;
5736 if (!( unpickler = newUnpicklerobject(file)))
5737 goto finally;
5739 res = load(unpickler);
5741 finally:
5742 Py_XDECREF(file);
5743 Py_XDECREF(unpickler);
5745 return res;
5749 PyDoc_STRVAR(Unpicklertype__doc__,
5750 "Objects that know how to unpickle");
5752 static PyTypeObject Unpicklertype = {
5753 PyVarObject_HEAD_INIT(NULL, 0)
5754 "cPickle.Unpickler", /*tp_name*/
5755 sizeof(Unpicklerobject), /*tp_basicsize*/
5757 (destructor)Unpickler_dealloc, /* tp_dealloc */
5758 0, /* tp_print */
5759 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5760 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5761 0, /* tp_compare */
5762 0, /* tp_repr */
5763 0, /* tp_as_number */
5764 0, /* tp_as_sequence */
5765 0, /* tp_as_mapping */
5766 0, /* tp_hash */
5767 0, /* tp_call */
5768 0, /* tp_str */
5769 0, /* tp_getattro */
5770 0, /* tp_setattro */
5771 0, /* tp_as_buffer */
5772 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5773 Unpicklertype__doc__, /* tp_doc */
5774 (traverseproc)Unpickler_traverse, /* tp_traverse */
5775 (inquiry)Unpickler_clear, /* tp_clear */
5778 static struct PyMethodDef cPickle_methods[] = {
5779 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5780 PyDoc_STR("dump(obj, file, protocol=0) -- "
5781 "Write an object in pickle format to the given file.\n"
5782 "\n"
5783 "See the Pickler docstring for the meaning of optional argument proto.")
5786 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5787 PyDoc_STR("dumps(obj, protocol=0) -- "
5788 "Return a string containing an object in pickle format.\n"
5789 "\n"
5790 "See the Pickler docstring for the meaning of optional argument proto.")
5793 {"load", (PyCFunction)cpm_load, METH_O,
5794 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5796 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
5797 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5799 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5800 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5801 "\n"
5802 "This takes a file-like object for writing a pickle data stream.\n"
5803 "The optional proto argument tells the pickler to use the given\n"
5804 "protocol; supported protocols are 0, 1, 2. The default\n"
5805 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5806 "only protocol that can be written to a file opened in text\n"
5807 "mode and read back successfully. When using a protocol higher\n"
5808 "than 0, make sure the file is opened in binary mode, both when\n"
5809 "pickling and unpickling.)\n"
5810 "\n"
5811 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5812 "more efficient than protocol 1.\n"
5813 "\n"
5814 "Specifying a negative protocol version selects the highest\n"
5815 "protocol version supported. The higher the protocol used, the\n"
5816 "more recent the version of Python needed to read the pickle\n"
5817 "produced.\n"
5818 "\n"
5819 "The file parameter must have a write() method that accepts a single\n"
5820 "string argument. It can thus be an open file object, a StringIO\n"
5821 "object, or any other custom object that meets this interface.\n")
5824 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
5825 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5827 { NULL, NULL }
5830 static int
5831 init_stuff(PyObject *module_dict)
5833 PyObject *copyreg, *t, *r;
5835 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5837 if (PyType_Ready(&Unpicklertype) < 0)
5838 return -1;
5839 if (PyType_Ready(&Picklertype) < 0)
5840 return -1;
5842 INIT_STR(__class__);
5843 INIT_STR(__getinitargs__);
5844 INIT_STR(__dict__);
5845 INIT_STR(__getstate__);
5846 INIT_STR(__setstate__);
5847 INIT_STR(__name__);
5848 INIT_STR(__main__);
5849 INIT_STR(__reduce__);
5850 INIT_STR(__reduce_ex__);
5851 INIT_STR(write);
5852 INIT_STR(append);
5853 INIT_STR(read);
5854 INIT_STR(readline);
5855 INIT_STR(copyreg);
5856 INIT_STR(dispatch_table);
5858 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5859 return -1;
5861 /* This is special because we want to use a different
5862 one in restricted mode. */
5863 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5864 if (!dispatch_table) return -1;
5866 extension_registry = PyObject_GetAttrString(copyreg,
5867 "_extension_registry");
5868 if (!extension_registry) return -1;
5870 inverted_registry = PyObject_GetAttrString(copyreg,
5871 "_inverted_registry");
5872 if (!inverted_registry) return -1;
5874 extension_cache = PyObject_GetAttrString(copyreg,
5875 "_extension_cache");
5876 if (!extension_cache) return -1;
5878 Py_DECREF(copyreg);
5880 if (!(empty_tuple = PyTuple_New(0)))
5881 return -1;
5883 two_tuple = PyTuple_New(2);
5884 if (two_tuple == NULL)
5885 return -1;
5886 /* We use this temp container with no regard to refcounts, or to
5887 * keeping containees alive. Exempt from GC, because we don't
5888 * want anything looking at two_tuple() by magic.
5890 PyObject_GC_UnTrack(two_tuple);
5892 /* Ugh */
5893 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5894 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5895 return -1;
5897 if (!( t=PyDict_New())) return -1;
5898 if (!( r=PyRun_String(
5899 "def __str__(self):\n"
5900 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5901 Py_file_input,
5902 module_dict, t) )) return -1;
5903 Py_DECREF(r);
5905 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5906 if (!PickleError)
5907 return -1;
5909 Py_DECREF(t);
5911 PicklingError = PyErr_NewException("cPickle.PicklingError",
5912 PickleError, NULL);
5913 if (!PicklingError)
5914 return -1;
5916 if (!( t=PyDict_New())) return -1;
5917 if (!( r=PyRun_String(
5918 "def __str__(self):\n"
5919 " a=self.args\n"
5920 " a=a and type(a[0]) or '(what)'\n"
5921 " return 'Cannot pickle %s objects' % a\n"
5922 , Py_file_input,
5923 module_dict, t) )) return -1;
5924 Py_DECREF(r);
5926 if (!( UnpickleableError = PyErr_NewException(
5927 "cPickle.UnpickleableError", PicklingError, t)))
5928 return -1;
5930 Py_DECREF(t);
5932 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5933 PickleError, NULL)))
5934 return -1;
5936 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5937 UnpicklingError, NULL)))
5938 return -1;
5940 if (PyDict_SetItemString(module_dict, "PickleError",
5941 PickleError) < 0)
5942 return -1;
5944 if (PyDict_SetItemString(module_dict, "PicklingError",
5945 PicklingError) < 0)
5946 return -1;
5948 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5949 UnpicklingError) < 0)
5950 return -1;
5952 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5953 UnpickleableError) < 0)
5954 return -1;
5956 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5957 BadPickleGet) < 0)
5958 return -1;
5960 PycString_IMPORT;
5962 return 0;
5965 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5966 #define PyMODINIT_FUNC void
5967 #endif
5968 PyMODINIT_FUNC
5969 initcPickle(void)
5971 PyObject *m, *d, *di, *v, *k;
5972 Py_ssize_t i;
5973 char *rev = "1.71"; /* XXX when does this change? */
5974 PyObject *format_version;
5975 PyObject *compatible_formats;
5977 /* XXX: Should mention that the pickle module will include the C
5978 XXX: optimized implementation automatically. */
5979 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5980 "Python 3.0", 2) < 0)
5981 return;
5983 Py_TYPE(&Picklertype) = &PyType_Type;
5984 Py_TYPE(&Unpicklertype) = &PyType_Type;
5985 Py_TYPE(&PdataType) = &PyType_Type;
5987 /* Initialize some pieces. We need to do this before module creation,
5988 * so we're forced to use a temporary dictionary. :(
5990 di = PyDict_New();
5991 if (!di) return;
5992 if (init_stuff(di) < 0) return;
5994 /* Create the module and add the functions */
5995 m = Py_InitModule4("cPickle", cPickle_methods,
5996 cPickle_module_documentation,
5997 (PyObject*)NULL,PYTHON_API_VERSION);
5998 if (m == NULL)
5999 return;
6001 /* Add some symbolic constants to the module */
6002 d = PyModule_GetDict(m);
6003 v = PyString_FromString(rev);
6004 PyDict_SetItemString(d, "__version__", v);
6005 Py_XDECREF(v);
6007 /* Copy data from di. Waaa. */
6008 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
6009 if (PyObject_SetItem(d, k, v) < 0) {
6010 Py_DECREF(di);
6011 return;
6014 Py_DECREF(di);
6016 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
6017 if (i < 0)
6018 return;
6020 /* These are purely informational; no code uses them. */
6021 /* File format version we write. */
6022 format_version = PyString_FromString("2.0");
6023 /* Format versions we can read. */
6024 compatible_formats = Py_BuildValue("[sssss]",
6025 "1.0", /* Original protocol 0 */
6026 "1.1", /* Protocol 0 + INST */
6027 "1.2", /* Original protocol 1 */
6028 "1.3", /* Protocol 1 + BINFLOAT */
6029 "2.0"); /* Original protocol 2 */
6030 PyDict_SetItemString(d, "format_version", format_version);
6031 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
6032 Py_XDECREF(format_version);
6033 Py_XDECREF(compatible_formats);