1#include "Python.h"
2#include "cStringIO.h"
3#include "structmember.h"
4
5PyDoc_STRVAR(cPickle_module_documentation,
6"C implementation and optimization of the Python pickle module.");
7
8#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
11#endif /* Py_eval_input */
12
13#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
14
15#define WRITE_BUF_SIZE 256
16
17/* Bump this when new opcodes are added to the pickle protocol. */
18#define HIGHEST_PROTOCOL 2
19
20/*
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.
23 */
24#ifdef UNICODE
25#  undef UNICODE
26#endif
27
28/*
29 * Pickle opcodes.  These must be kept in synch with pickle.py.  Extensive
30 * docs are in pickletools.py.
31 */
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'
73
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 */
87
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.
92 */
93#undef TRUE
94#define TRUE        "I01\n"
95#undef FALSE
96#define FALSE       "I00\n"
97
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.
102 */
103#define BATCHSIZE 1000
104
105static char MARKv = MARK;
106
107static PyObject *PickleError;
108static PyObject *PicklingError;
109static PyObject *UnpickleableError;
110static PyObject *UnpicklingError;
111static PyObject *BadPickleGet;
112
113/* As the name says, an empty tuple. */
114static PyObject *empty_tuple;
115
116/* copy_reg.dispatch_table, {type_object: pickling_function} */
117static PyObject *dispatch_table;
118
119/* For EXT[124] opcodes. */
120/* copy_reg._extension_registry, {(module_name, function_name): code} */
121static PyObject *extension_registry;
122/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
123static PyObject *inverted_registry;
124/* copy_reg._extension_cache, {code: object} */
125static PyObject *extension_cache;
126
127/* For looking up name pairs in copy_reg._extension_registry. */
128static PyObject *two_tuple;
129
130static 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  *dispatch_table_str;
136
137/*************************************************************************
138 Internal Data type for pickle data.                                     */
139
140typedef struct {
141    PyObject_HEAD
142    Py_ssize_t length;  /* number of initial slots in data currently used */
143    Py_ssize_t size;    /* number of slots in data allocated */
144    PyObject **data;
145} Pdata;
146
147static void
148Pdata_dealloc(Pdata *self)
149{
150    Py_ssize_t i;
151    PyObject **p;
152
153    for (i = self->length, p = self->data; --i >= 0; p++) {
154        Py_DECREF(*p);
155    }
156    if (self->data)
157        free(self->data);
158    PyObject_Del(self);
159}
160
161static 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, ""
165};
166
167#define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
168
169static PyObject *
170Pdata_New(void)
171{
172    Pdata *self;
173
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();
183}
184
185static int
186stackUnderflow(void)
187{
188    PyErr_SetString(UnpicklingError, "unpickling stack underflow");
189    return -1;
190}
191
192/* Retain only the initial clearto items.  If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
194 */
195static int
196Pdata_clear(Pdata *self, Py_ssize_t clearto)
197{
198    Py_ssize_t i;
199    PyObject **p;
200
201    if (clearto < 0) return stackUnderflow();
202    if (clearto >= self->length) return 0;
203
204    for (i = self->length, p = self->data + clearto;
205         --i >= clearto;
206         p++) {
207        Py_CLEAR(*p);
208    }
209    self->length = clearto;
210
211    return 0;
212}
213
214static int
215Pdata_grow(Pdata *self)
216{
217    Py_ssize_t bigger;
218    Py_ssize_t nbytes;
219
220    PyObject **tmp;
221
222    if (self->size > (PY_SSIZE_T_MAX >> 1))
223        goto nomemory;
224    bigger = self->size << 1;
225    if (bigger > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
226        goto nomemory;
227    nbytes = bigger * sizeof(PyObject *);
228    tmp = realloc(self->data, nbytes);
229    if (tmp == NULL)
230        goto nomemory;
231    self->data = tmp;
232    self->size = bigger;
233    return 0;
234
235  nomemory:
236    PyErr_NoMemory();
237    return -1;
238}
239
240/* D is a Pdata*.  Pop the topmost element and store it into V, which
241 * must be an lvalue holding PyObject*.  On stack underflow, UnpicklingError
242 * is raised and V is set to NULL.  D and V may be evaluated several times.
243 */
244#define PDATA_POP(D, V) {                                       \
245    if ((D)->length)                                            \
246        (V) = (D)->data[--((D)->length)];                       \
247    else {                                                      \
248        PyErr_SetString(UnpicklingError, "bad pickle data");            \
249        (V) = NULL;                                             \
250    }                                                           \
251}
252
253/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
254 * D.  If the Pdata stack can't be grown to hold the new value, both
255 * raise MemoryError and execute "return ER".  The difference is in ownership
256 * of O after:  _PUSH transfers ownership of O from the caller to the stack
257 * (no incref of O is done, and in case of error O is decrefed), while
258 * _APPEND pushes a new reference.
259 */
260
261/* Push O on stack D, giving ownership of O to the stack. */
262#define PDATA_PUSH(D, O, ER) {                                  \
263    if (((Pdata*)(D))->length == ((Pdata*)(D))->size &&         \
264        Pdata_grow((Pdata*)(D)) < 0) {                          \
265        Py_DECREF(O);                                           \
266        return ER;                                              \
267    }                                                           \
268    ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O);         \
269}
270
271/* Push O on stack D, pushing a new reference. */
272#define PDATA_APPEND(D, O, ER) {                                \
273    if (((Pdata*)(D))->length == ((Pdata*)(D))->size &&         \
274        Pdata_grow((Pdata*)(D)) < 0)                            \
275        return ER;                                              \
276    Py_INCREF(O);                                               \
277    ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O);         \
278}
279
280
281static PyObject *
282Pdata_popTuple(Pdata *self, Py_ssize_t start)
283{
284    PyObject *r;
285    Py_ssize_t i, j, l;
286
287    l = self->length-start;
288    r = PyTuple_New(l);
289    if (r == NULL)
290        return NULL;
291    for (i = start, j = 0 ; j < l; i++, j++)
292        PyTuple_SET_ITEM(r, j, self->data[i]);
293
294    self->length = start;
295    return r;
296}
297
298static PyObject *
299Pdata_popList(Pdata *self, Py_ssize_t start)
300{
301    PyObject *r;
302    Py_ssize_t i, j, l;
303
304    l=self->length-start;
305    if (!( r=PyList_New(l)))  return NULL;
306    for (i=start, j=0 ; j < l; i++, j++)
307        PyList_SET_ITEM(r, j, self->data[i]);
308
309    self->length=start;
310    return r;
311}
312
313/*************************************************************************/
314
315#define ARG_TUP(self, o) {                          \
316  if (self->arg || (self->arg=PyTuple_New(1))) {    \
317      Py_XDECREF(PyTuple_GET_ITEM(self->arg,0));    \
318      PyTuple_SET_ITEM(self->arg,0,o);              \
319  }                                                 \
320  else {                                            \
321      Py_DECREF(o);                                 \
322  }                                                 \
323}
324
325#define FREE_ARG_TUP(self) {                        \
326    if (Py_REFCNT(self->arg) > 1) {                 \
327      Py_CLEAR(self->arg);                          \
328    }                                               \
329  }
330
331typedef struct Picklerobject {
332    PyObject_HEAD
333    FILE *fp;
334    PyObject *write;
335    PyObject *file;
336    PyObject *memo;
337    PyObject *arg;
338    PyObject *pers_func;
339    PyObject *inst_pers_func;
340
341    /* pickle protocol number, >= 0 */
342    int proto;
343
344    /* bool, true if proto > 0 */
345    int bin;
346
347    int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
348    Py_ssize_t (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
349    char *write_buf;
350    Py_ssize_t buf_size;
351    PyObject *dispatch_table;
352    int fast_container; /* count nested container dumps */
353    PyObject *fast_memo;
354} Picklerobject;
355
356#ifndef PY_CPICKLE_FAST_LIMIT
357#define PY_CPICKLE_FAST_LIMIT 50
358#endif
359
360static PyTypeObject Picklertype;
361
362typedef struct Unpicklerobject {
363    PyObject_HEAD
364    FILE *fp;
365    PyObject *file;
366    PyObject *readline;
367    PyObject *read;
368    PyObject *memo;
369    PyObject *arg;
370    Pdata *stack;
371    PyObject *mark;
372    PyObject *pers_func;
373    PyObject *last_string;
374    Py_ssize_t *marks;
375    Py_ssize_t num_marks;
376    Py_ssize_t marks_size;
377    Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
378    Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
379    Py_ssize_t buf_size;
380    char *buf;
381    PyObject *find_class;
382} Unpicklerobject;
383
384static PyTypeObject Unpicklertype;
385
386/* Forward decls that need the above structs */
387static int save(Picklerobject *, PyObject *, int);
388static int put2(Picklerobject *, PyObject *);
389
390static
391PyObject *
392cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
393{
394    va_list va;
395    PyObject *args=0, *retval=0;
396    va_start(va, format);
397
398    if (format) args = Py_VaBuildValue(format, va);
399    va_end(va);
400    if (format && ! args) return NULL;
401    if (stringformat && !(retval=PyString_FromString(stringformat)))
402        return NULL;
403
404    if (retval) {
405        if (args) {
406            PyObject *v;
407            v=PyString_Format(retval, args);
408            Py_DECREF(retval);
409            Py_DECREF(args);
410            if (! v) return NULL;
411            retval=v;
412        }
413    }
414    else
415        if (args) retval=args;
416        else {
417            PyErr_SetObject(ErrType,Py_None);
418            return NULL;
419        }
420    PyErr_SetObject(ErrType,retval);
421    Py_DECREF(retval);
422    return NULL;
423}
424
425static Py_ssize_t
426write_file(Picklerobject *self, const char *s, Py_ssize_t  n)
427{
428    size_t nbyteswritten;
429
430    if (s == NULL) {
431        return 0;
432    }
433
434    PyFile_IncUseCount((PyFileObject *)self->file);
435    Py_BEGIN_ALLOW_THREADS
436    nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
437    Py_END_ALLOW_THREADS
438    PyFile_DecUseCount((PyFileObject *)self->file);
439    if (nbyteswritten != (size_t)n) {
440        PyErr_SetFromErrno(PyExc_IOError);
441        return -1;
442    }
443
444    return n;
445}
446
447static Py_ssize_t
448write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t  n)
449{
450    Py_ssize_t len = n;
451
452    if (s == NULL) {
453        return 0;
454    }
455
456    while (n > INT_MAX) {
457        if (PycStringIO->cwrite((PyObject *)self->file, s, INT_MAX) != INT_MAX) {
458            return -1;
459        }
460        n -= INT_MAX;
461    }
462
463    if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
464        return -1;
465    }
466
467    return len;
468}
469
470static Py_ssize_t
471write_none(Picklerobject *self, const char *s, Py_ssize_t  n)
472{
473    if (s == NULL) return 0;
474    return n;
475}
476
477static Py_ssize_t
478write_other(Picklerobject *self, const char *s, Py_ssize_t  n)
479{
480    PyObject *py_str = 0, *junk = 0;
481
482    if (s == NULL) {
483        if (!( self->buf_size ))  return 0;
484        py_str = PyString_FromStringAndSize(self->write_buf,
485                                            self->buf_size);
486        if (!py_str)
487            return -1;
488    }
489    else {
490        if (self->buf_size && n > WRITE_BUF_SIZE - self->buf_size) {
491            if (write_other(self, NULL, 0) < 0)
492                return -1;
493        }
494
495        if (n > WRITE_BUF_SIZE) {
496            if (!( py_str =
497                   PyString_FromStringAndSize(s, n)))
498                return -1;
499        }
500        else {
501            memcpy(self->write_buf + self->buf_size, s, n);
502            self->buf_size += n;
503            return n;
504        }
505    }
506
507    if (self->write) {
508        /* object with write method */
509        ARG_TUP(self, py_str);
510        if (self->arg) {
511            junk = PyObject_Call(self->write, self->arg, NULL);
512            FREE_ARG_TUP(self);
513        }
514        if (junk) Py_DECREF(junk);
515        else return -1;
516    }
517    else
518        PDATA_PUSH(self->file, py_str, -1);
519
520    self->buf_size = 0;
521    return n;
522}
523
524
525static Py_ssize_t
526read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
527{
528    size_t nbytesread;
529
530    if (self->buf_size == 0) {
531        Py_ssize_t size;
532
533        size = ((n < 32) ? 32 : n);
534        if (!( self->buf = (char *)malloc(size))) {
535            PyErr_NoMemory();
536            return -1;
537        }
538
539        self->buf_size = size;
540    }
541    else if (n > self->buf_size) {
542        char *newbuf = (char *)realloc(self->buf, n);
543        if (!newbuf)  {
544            PyErr_NoMemory();
545            return -1;
546        }
547        self->buf = newbuf;
548        self->buf_size = n;
549    }
550
551    PyFile_IncUseCount((PyFileObject *)self->file);
552    Py_BEGIN_ALLOW_THREADS
553    nbytesread = fread(self->buf, sizeof(char), n, self->fp);
554    Py_END_ALLOW_THREADS
555    PyFile_DecUseCount((PyFileObject *)self->file);
556    if (nbytesread != (size_t)n) {
557        if (feof(self->fp)) {
558            PyErr_SetNone(PyExc_EOFError);
559            return -1;
560        }
561
562        PyErr_SetFromErrno(PyExc_IOError);
563        return -1;
564    }
565
566    *s = self->buf;
567
568    return n;
569}
570
571
572static Py_ssize_t
573readline_file(Unpicklerobject *self, char **s)
574{
575    Py_ssize_t i;
576
577    if (self->buf_size == 0) {
578        if (!( self->buf = (char *)malloc(40))) {
579            PyErr_NoMemory();
580            return -1;
581        }
582        self->buf_size = 40;
583    }
584
585    i = 0;
586    while (1) {
587        Py_ssize_t bigger;
588        char *newbuf;
589        for (; i < (self->buf_size - 1); i++) {
590            if (feof(self->fp) ||
591                (self->buf[i] = getc(self->fp)) == '\n') {
592                self->buf[i + 1] = '\0';
593                *s = self->buf;
594                return i + 1;
595            }
596        }
597        if (self->buf_size > (PY_SSIZE_T_MAX >> 1)) {
598            PyErr_NoMemory();
599            return -1;
600        }
601        bigger = self->buf_size << 1;
602        newbuf = (char *)realloc(self->buf, bigger);
603        if (newbuf == NULL)  {
604            PyErr_NoMemory();
605            return -1;
606        }
607        self->buf = newbuf;
608        self->buf_size = bigger;
609    }
610}
611
612
613static Py_ssize_t
614read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t  n)
615{
616    Py_ssize_t len = n;
617    char *start, *end = NULL;
618
619    while (1) {
620        int k;
621        char *ptr;
622        if (n > INT_MAX)
623            k = INT_MAX;
624        else
625            k = (int)n;
626        if (PycStringIO->cread((PyObject *)self->file, &ptr, k) != k) {
627            PyErr_SetNone(PyExc_EOFError);
628            return -1;
629        }
630        if (end == NULL)
631            start = ptr;
632        else if (ptr != end) {
633            /* non-continuous area */
634            return -1;
635        }
636        if (n <= INT_MAX)
637            break;
638        end = ptr + INT_MAX;
639        n -= INT_MAX;
640    }
641
642    *s = start;
643
644    return len;
645}
646
647
648static Py_ssize_t
649readline_cStringIO(Unpicklerobject *self, char **s)
650{
651    Py_ssize_t n = 0;
652    char *start = NULL, *end = NULL;
653
654    while (1) {
655        int k;
656        char *ptr;
657        if ((k = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
658            return -1;
659        }
660        n += k;
661        if (end == NULL)
662            start = ptr;
663        else if (ptr != end) {
664            /* non-continuous area */
665            return -1;
666        }
667        if (k == 0 || ptr[k - 1] == '\n')
668            break;
669        end = ptr + k;
670    }
671
672    *s = start;
673
674    return n;
675}
676
677
678static Py_ssize_t
679read_other(Unpicklerobject *self, char **s, Py_ssize_t  n)
680{
681    PyObject *bytes, *str=0;
682
683    if (!( bytes = PyInt_FromSsize_t(n)))  return -1;
684
685    ARG_TUP(self, bytes);
686    if (self->arg) {
687        str = PyObject_Call(self->read, self->arg, NULL);
688        FREE_ARG_TUP(self);
689    }
690    if (! str) return -1;
691
692    Py_XDECREF(self->last_string);
693    self->last_string = str;
694
695    if (! (*s = PyString_AsString(str))) return -1;
696
697    if (PyString_GET_SIZE(str) != n) {
698        PyErr_SetNone(PyExc_EOFError);
699        return -1;
700    }
701
702    return n;
703}
704
705
706static Py_ssize_t
707readline_other(Unpicklerobject *self, char **s)
708{
709    PyObject *str;
710    Py_ssize_t str_size;
711
712    if (!( str = PyObject_CallObject(self->readline, empty_tuple)))  {
713        return -1;
714    }
715
716    if ((str_size = PyString_Size(str)) < 0)
717        return -1;
718
719    Py_XDECREF(self->last_string);
720    self->last_string = str;
721
722    if (! (*s = PyString_AsString(str)))
723        return -1;
724
725    return str_size;
726}
727
728/* Copy the first n bytes from s into newly malloc'ed memory, plus a
729 * trailing 0 byte.  Return a pointer to that, or NULL if out of memory.
730 * The caller is responsible for free()'ing the return value.
731 */
732static char *
733pystrndup(const char *s, Py_ssize_t n)
734{
735    char *r = (char *)malloc(n+1);
736    if (r == NULL)
737        return (char*)PyErr_NoMemory();
738    memcpy(r, s, n);
739    r[n] = 0;
740    return r;
741}
742
743
744static int
745get(Picklerobject *self, PyObject *id)
746{
747    PyObject *value, *mv;
748    Py_ssize_t c_value;
749    char s[30];
750    size_t len;
751
752    if (!( mv = PyDict_GetItem(self->memo, id)))  {
753        PyErr_SetObject(PyExc_KeyError, id);
754        return -1;
755    }
756
757    if (!( value = PyTuple_GetItem(mv, 0)))
758        return -1;
759
760    if (!( PyInt_Check(value)))  {
761        PyErr_SetString(PicklingError, "no int where int expected in memo");
762        return -1;
763    }
764    c_value = PyInt_AS_LONG((PyIntObject*)value);
765
766    if (!self->bin) {
767        s[0] = GET;
768        PyOS_snprintf(s + 1, sizeof(s) - 1,
769                      "%" PY_FORMAT_SIZE_T "d\n", c_value);
770        len = strlen(s);
771    }
772    else if (Pdata_Check(self->file)) {
773        if (write_other(self, NULL, 0) < 0) return -1;
774        PDATA_APPEND(self->file, mv, -1);
775        return 0;
776    }
777    else {
778        if (c_value < 256) {
779            s[0] = BINGET;
780            s[1] = (int)(c_value & 0xff);
781            len = 2;
782        }
783        else {
784            s[0] = LONG_BINGET;
785            s[1] = (int)(c_value & 0xff);
786            s[2] = (int)((c_value >> 8)  & 0xff);
787            s[3] = (int)((c_value >> 16) & 0xff);
788            s[4] = (int)((c_value >> 24) & 0xff);
789            len = 5;
790        }
791    }
792
793    if (self->write_func(self, s, len) < 0)
794        return -1;
795
796    return 0;
797}
798
799
800static int
801put(Picklerobject *self, PyObject *ob)
802{
803    if (Py_REFCNT(ob) < 2 || self->fast)
804        return 0;
805
806    return put2(self, ob);
807}
808
809
810static int
811put2(Picklerobject *self, PyObject *ob)
812{
813    char c_str[30];
814    Py_ssize_t len, p;
815    int res = -1;
816    PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
817
818    if (self->fast)
819        return 0;
820
821    if ((p = PyDict_Size(self->memo)) < 0)
822        goto finally;
823
824    /* Make sure memo keys are positive! */
825    /* XXX Why?
826     * XXX And does "positive" really mean non-negative?
827     * XXX pickle.py starts with PUT index 0, not 1.  This makes for
828     * XXX gratuitous differences between the pickling modules.
829     */
830    p++;
831
832    if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
833        goto finally;
834
835    if (!( memo_len = PyInt_FromLong(p)))
836        goto finally;
837
838    if (!( t = PyTuple_New(2)))
839        goto finally;
840
841    PyTuple_SET_ITEM(t, 0, memo_len);
842    Py_INCREF(memo_len);
843    PyTuple_SET_ITEM(t, 1, ob);
844    Py_INCREF(ob);
845
846    if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
847        goto finally;
848
849    if (!self->bin) {
850        c_str[0] = PUT;
851        PyOS_snprintf(c_str + 1, sizeof(c_str) - 1,
852                      "%" PY_FORMAT_SIZE_T "d\n", p);
853        len = strlen(c_str);
854    }
855    else if (Pdata_Check(self->file)) {
856        if (write_other(self, NULL, 0) < 0) return -1;
857        PDATA_APPEND(self->file, memo_len, -1);
858        res=0;          /* Job well done ;) */
859        goto finally;
860    }
861    else {
862        if (p >= 256) {
863            c_str[0] = LONG_BINPUT;
864            c_str[1] = (int)(p & 0xff);
865            c_str[2] = (int)((p >> 8)  & 0xff);
866            c_str[3] = (int)((p >> 16) & 0xff);
867            c_str[4] = (int)((p >> 24) & 0xff);
868            len = 5;
869        }
870        else {
871            c_str[0] = BINPUT;
872            c_str[1] = p;
873            len = 2;
874        }
875    }
876
877    if (self->write_func(self, c_str, len) < 0)
878        goto finally;
879
880    res = 0;
881
882  finally:
883    Py_XDECREF(py_ob_id);
884    Py_XDECREF(memo_len);
885    Py_XDECREF(t);
886
887    return res;
888}
889
890static PyObject *
891whichmodule(PyObject *global, PyObject *global_name)
892{
893    Py_ssize_t i, j;
894    PyObject *module = 0, *modules_dict = 0,
895        *global_name_attr = 0, *name = 0;
896
897    module = PyObject_GetAttrString(global, "__module__");
898    if (module)
899        return module;
900    if (PyErr_ExceptionMatches(PyExc_AttributeError))
901        PyErr_Clear();
902    else
903        return NULL;
904
905    if (!( modules_dict = PySys_GetObject("modules")))
906        return NULL;
907
908    i = 0;
909    while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
910
911        if (PyObject_Compare(name, __main___str)==0) continue;
912
913        global_name_attr = PyObject_GetAttr(module, global_name);
914        if (!global_name_attr)  {
915            if (PyErr_ExceptionMatches(PyExc_AttributeError))
916                PyErr_Clear();
917            else
918                return NULL;
919            continue;
920        }
921
922        if (global_name_attr != global) {
923            Py_DECREF(global_name_attr);
924            continue;
925        }
926
927        Py_DECREF(global_name_attr);
928
929        break;
930    }
931
932    /* The following implements the rule in pickle.py added in 1.5
933       that used __main__ if no module is found.  I don't actually
934       like this rule. jlf
935    */
936    if (!j) {
937        name=__main___str;
938    }
939
940    Py_INCREF(name);
941    return name;
942}
943
944
945static int
946fast_save_enter(Picklerobject *self, PyObject *obj)
947{
948    /* if fast_container < 0, we're doing an error exit. */
949    if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
950        PyObject *key = NULL;
951        if (self->fast_memo == NULL) {
952            self->fast_memo = PyDict_New();
953            if (self->fast_memo == NULL) {
954                self->fast_container = -1;
955                return 0;
956            }
957        }
958        key = PyLong_FromVoidPtr(obj);
959        if (key == NULL)
960            return 0;
961        if (PyDict_GetItem(self->fast_memo, key)) {
962            Py_DECREF(key);
963            PyErr_Format(PyExc_ValueError,
964                         "fast mode: can't pickle cyclic objects "
965                         "including object type %s at %p",
966                         Py_TYPE(obj)->tp_name, obj);
967            self->fast_container = -1;
968            return 0;
969        }
970        if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
971            Py_DECREF(key);
972            self->fast_container = -1;
973            return 0;
974        }
975        Py_DECREF(key);
976    }
977    return 1;
978}
979
980int
981fast_save_leave(Picklerobject *self, PyObject *obj)
982{
983    if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
984        PyObject *key = PyLong_FromVoidPtr(obj);
985        if (key == NULL)
986            return 0;
987        if (PyDict_DelItem(self->fast_memo, key) < 0) {
988            Py_DECREF(key);
989            return 0;
990        }
991        Py_DECREF(key);
992    }
993    return 1;
994}
995
996static int
997save_none(Picklerobject *self, PyObject *args)
998{
999    static char none = NONE;
1000    if (self->write_func(self, &none, 1) < 0)
1001        return -1;
1002
1003    return 0;
1004}
1005
1006static int
1007save_bool(Picklerobject *self, PyObject *args)
1008{
1009    static const char *buf[2] = {FALSE, TRUE};
1010    static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
1011    long l = PyInt_AS_LONG((PyIntObject *)args);
1012
1013    if (self->proto >= 2) {
1014        char opcode = l ? NEWTRUE : NEWFALSE;
1015        if (self->write_func(self, &opcode, 1) < 0)
1016            return -1;
1017    }
1018    else if (self->write_func(self, buf[l], len[l]) < 0)
1019        return -1;
1020    return 0;
1021}
1022
1023static int
1024save_int(Picklerobject *self, PyObject *args)
1025{
1026    char c_str[32];
1027    long l = PyInt_AS_LONG((PyIntObject *)args);
1028    Py_ssize_t len = 0;
1029
1030    if (!self->bin
1031#if SIZEOF_LONG > 4
1032        || l >  0x7fffffffL
1033        || l < -0x80000000L
1034#endif
1035        ) {
1036        /* Text-mode pickle, or long too big to fit in the 4-byte
1037         * signed BININT format:  store as a string.
1038         */
1039        c_str[0] = INT;
1040        PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
1041        if (self->write_func(self, c_str, strlen(c_str)) < 0)
1042            return -1;
1043    }
1044    else {
1045        /* Binary pickle and l fits in a signed 4-byte int. */
1046        c_str[1] = (int)( l        & 0xff);
1047        c_str[2] = (int)((l >> 8)  & 0xff);
1048        c_str[3] = (int)((l >> 16) & 0xff);
1049        c_str[4] = (int)((l >> 24) & 0xff);
1050
1051        if ((c_str[4] == 0) && (c_str[3] == 0)) {
1052            if (c_str[2] == 0) {
1053                c_str[0] = BININT1;
1054                len = 2;
1055            }
1056            else {
1057                c_str[0] = BININT2;
1058                len = 3;
1059            }
1060        }
1061        else {
1062            c_str[0] = BININT;
1063            len = 5;
1064        }
1065
1066        if (self->write_func(self, c_str, len) < 0)
1067            return -1;
1068    }
1069
1070    return 0;
1071}
1072
1073
1074static int
1075save_long(Picklerobject *self, PyObject *args)
1076{
1077    Py_ssize_t size;
1078    int res = -1;
1079    PyObject *repr = NULL;
1080
1081    static char l = LONG;
1082
1083    if (self->proto >= 2) {
1084        /* Linear-time pickling. */
1085        size_t nbits;
1086        size_t nbytes;
1087        unsigned char *pdata;
1088        char c_str[5];
1089        int i;
1090        int sign = _PyLong_Sign(args);
1091
1092        if (sign == 0) {
1093            /* It's 0 -- an empty bytestring. */
1094            c_str[0] = LONG1;
1095            c_str[1] = 0;
1096            i = self->write_func(self, c_str, 2);
1097            if (i < 0) goto finally;
1098            res = 0;
1099            goto finally;
1100        }
1101        nbits = _PyLong_NumBits(args);
1102        if (nbits == (size_t)-1 && PyErr_Occurred())
1103            goto finally;
1104        /* How many bytes do we need?  There are nbits >> 3 full
1105         * bytes of data, and nbits & 7 leftover bits.  If there
1106         * are any leftover bits, then we clearly need another
1107         * byte.  Wnat's not so obvious is that we *probably*
1108         * need another byte even if there aren't any leftovers:
1109         * the most-significant bit of the most-significant byte
1110         * acts like a sign bit, and it's usually got a sense
1111         * opposite of the one we need.  The exception is longs
1112         * of the form -(2**(8*j-1)) for j > 0.  Such a long is
1113         * its own 256's-complement, so has the right sign bit
1114         * even without the extra byte.  That's a pain to check
1115         * for in advance, though, so we always grab an extra
1116         * byte at the start, and cut it back later if possible.
1117         */
1118        nbytes = (nbits >> 3) + 1;
1119        if (nbytes > INT_MAX) {
1120            PyErr_SetString(PyExc_OverflowError, "long too large "
1121                "to pickle");
1122            goto finally;
1123        }
1124        repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1125        if (repr == NULL) goto finally;
1126        pdata = (unsigned char *)PyString_AS_STRING(repr);
1127        i = _PyLong_AsByteArray((PyLongObject *)args,
1128                        pdata, nbytes,
1129                        1 /* little endian */, 1 /* signed */);
1130        if (i < 0) goto finally;
1131        /* If the long is negative, this may be a byte more than
1132         * needed.  This is so iff the MSB is all redundant sign
1133         * bits.
1134         */
1135        if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1136            (pdata[nbytes - 2] & 0x80) != 0)
1137            --nbytes;
1138
1139        if (nbytes < 256) {
1140            c_str[0] = LONG1;
1141            c_str[1] = (char)nbytes;
1142            size = 2;
1143        }
1144        else {
1145            c_str[0] = LONG4;
1146            size = (int)nbytes;
1147            for (i = 1; i < 5; i++) {
1148                c_str[i] = (char)(size & 0xff);
1149                size >>= 8;
1150            }
1151            size = 5;
1152        }
1153        i = self->write_func(self, c_str, size);
1154        if (i < 0) goto finally;
1155        i = self->write_func(self, (char *)pdata, (int)nbytes);
1156        if (i < 0) goto finally;
1157        res = 0;
1158        goto finally;
1159    }
1160
1161    /* proto < 2:  write the repr and newline.  This is quadratic-time
1162     * (in the number of digits), in both directions.
1163     */
1164    if (!( repr = PyObject_Repr(args)))
1165        goto finally;
1166
1167    if ((size = PyString_Size(repr)) < 0)
1168        goto finally;
1169
1170    if (self->write_func(self, &l, 1) < 0)
1171        goto finally;
1172
1173    if (self->write_func(self,
1174                         PyString_AS_STRING((PyStringObject *)repr),
1175                                            size) < 0)
1176        goto finally;
1177
1178    if (self->write_func(self, "\n", 1) < 0)
1179        goto finally;
1180
1181    res = 0;
1182
1183  finally:
1184    Py_XDECREF(repr);
1185    return res;
1186}
1187
1188
1189static int
1190save_float(Picklerobject *self, PyObject *args)
1191{
1192    double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1193
1194    if (self->bin) {
1195        char str[9];
1196        str[0] = BINFLOAT;
1197        if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1198            return -1;
1199        if (self->write_func(self, str, 9) < 0)
1200            return -1;
1201    }
1202    else {
1203        int result = -1;
1204        char *buf = NULL;
1205        char op = FLOAT;
1206
1207        if (self->write_func(self, &op, 1) < 0)
1208            goto done;
1209
1210        buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
1211        if (!buf) {
1212            PyErr_NoMemory();
1213            goto done;
1214        }
1215
1216        if (self->write_func(self, buf, strlen(buf)) < 0)
1217            goto done;
1218
1219        if (self->write_func(self, "\n", 1) < 0)
1220            goto done;
1221
1222        result = 0;
1223done:
1224        PyMem_Free(buf);
1225        return result;
1226    }
1227
1228    return 0;
1229}
1230
1231
1232static int
1233save_string(Picklerobject *self, PyObject *args, int doput)
1234{
1235    Py_ssize_t size, len;
1236    PyObject *repr=0;
1237
1238    if ((size = PyString_Size(args)) < 0)
1239        return -1;
1240
1241    if (!self->bin) {
1242        char *repr_str;
1243
1244        static char string = STRING;
1245
1246        if (!( repr = PyObject_Repr(args)))
1247            return -1;
1248
1249        if ((len = PyString_Size(repr)) < 0)
1250            goto err;
1251        repr_str = PyString_AS_STRING((PyStringObject *)repr);
1252
1253        if (self->write_func(self, &string, 1) < 0)
1254            goto err;
1255
1256        if (self->write_func(self, repr_str, len) < 0)
1257            goto err;
1258
1259        if (self->write_func(self, "\n", 1) < 0)
1260            goto err;
1261
1262        Py_XDECREF(repr);
1263    }
1264    else {
1265        int i;
1266        char c_str[5];
1267
1268        if (size < 256) {
1269            c_str[0] = SHORT_BINSTRING;
1270            c_str[1] = size;
1271            len = 2;
1272        }
1273        else if (size <= INT_MAX) {
1274            c_str[0] = BINSTRING;
1275            for (i = 1; i < 5; i++)
1276                c_str[i] = (int)(size >> ((i - 1) * 8));
1277            len = 5;
1278        }
1279        else
1280            return -1;    /* string too large */
1281
1282        if (self->write_func(self, c_str, len) < 0)
1283            return -1;
1284
1285        if (size > 128 && Pdata_Check(self->file)) {
1286            if (write_other(self, NULL, 0) < 0) return -1;
1287            PDATA_APPEND(self->file, args, -1);
1288        }
1289        else {
1290            if (self->write_func(self,
1291                                 PyString_AS_STRING(
1292                                    (PyStringObject *)args),
1293                                 size) < 0)
1294                return -1;
1295        }
1296    }
1297
1298    if (doput)
1299        if (put(self, args) < 0)
1300            return -1;
1301
1302    return 0;
1303
1304  err:
1305    Py_XDECREF(repr);
1306    return -1;
1307}
1308
1309
1310#ifdef Py_USING_UNICODE
1311/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1312   backslash and newline characters to \uXXXX escapes. */
1313static PyObject *
1314modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
1315{
1316    PyObject *repr;
1317    char *p;
1318    char *q;
1319
1320    static const char *hexdigit = "0123456789abcdef";
1321#ifdef Py_UNICODE_WIDE
1322    const Py_ssize_t expandsize = 10;
1323#else
1324    const Py_ssize_t expandsize = 6;
1325#endif
1326
1327    if (size > PY_SSIZE_T_MAX / expandsize)
1328    return PyErr_NoMemory();
1329
1330    repr = PyString_FromStringAndSize(NULL, expandsize * size);
1331    if (repr == NULL)
1332    return NULL;
1333    if (size == 0)
1334    return repr;
1335
1336    p = q = PyString_AS_STRING(repr);
1337    while (size-- > 0) {
1338    Py_UNICODE ch = *s++;
1339#ifdef Py_UNICODE_WIDE
1340    /* Map 32-bit characters to '\Uxxxxxxxx' */
1341    if (ch >= 0x10000) {
1342        *p++ = '\\';
1343        *p++ = 'U';
1344        *p++ = hexdigit[(ch >> 28) & 0xf];
1345        *p++ = hexdigit[(ch >> 24) & 0xf];
1346        *p++ = hexdigit[(ch >> 20) & 0xf];
1347        *p++ = hexdigit[(ch >> 16) & 0xf];
1348        *p++ = hexdigit[(ch >> 12) & 0xf];
1349        *p++ = hexdigit[(ch >> 8) & 0xf];
1350        *p++ = hexdigit[(ch >> 4) & 0xf];
1351        *p++ = hexdigit[ch & 15];
1352    }
1353    else
1354#else
1355    /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1356    if (ch >= 0xD800 && ch < 0xDC00) {
1357        Py_UNICODE ch2;
1358        Py_UCS4 ucs;
1359
1360        ch2 = *s++;
1361        size--;
1362        if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1363        ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1364        *p++ = '\\';
1365        *p++ = 'U';
1366        *p++ = hexdigit[(ucs >> 28) & 0xf];
1367        *p++ = hexdigit[(ucs >> 24) & 0xf];
1368        *p++ = hexdigit[(ucs >> 20) & 0xf];
1369        *p++ = hexdigit[(ucs >> 16) & 0xf];
1370        *p++ = hexdigit[(ucs >> 12) & 0xf];
1371        *p++ = hexdigit[(ucs >> 8) & 0xf];
1372        *p++ = hexdigit[(ucs >> 4) & 0xf];
1373        *p++ = hexdigit[ucs & 0xf];
1374        continue;
1375        }
1376        /* Fall through: isolated surrogates are copied as-is */
1377        s--;
1378        size++;
1379    }
1380#endif
1381    /* Map 16-bit characters to '\uxxxx' */
1382    if (ch >= 256 || ch == '\\' || ch == '\n') {
1383        *p++ = '\\';
1384        *p++ = 'u';
1385        *p++ = hexdigit[(ch >> 12) & 0xf];
1386        *p++ = hexdigit[(ch >> 8) & 0xf];
1387        *p++ = hexdigit[(ch >> 4) & 0xf];
1388        *p++ = hexdigit[ch & 15];
1389    }
1390    /* Copy everything else as-is */
1391    else
1392        *p++ = (char) ch;
1393    }
1394    *p = '\0';
1395    _PyString_Resize(&repr, p - q);
1396    return repr;
1397}
1398
1399static int
1400save_unicode(Picklerobject *self, PyObject *args, int doput)
1401{
1402    Py_ssize_t size, len;
1403    PyObject *repr=0;
1404
1405    if (!PyUnicode_Check(args))
1406        return -1;
1407
1408    if (!self->bin) {
1409        char *repr_str;
1410        static char string = UNICODE;
1411
1412        repr = modified_EncodeRawUnicodeEscape(
1413            PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1414        if (!repr)
1415            return -1;
1416
1417        if ((len = PyString_Size(repr)) < 0)
1418            goto err;
1419        repr_str = PyString_AS_STRING((PyStringObject *)repr);
1420
1421        if (self->write_func(self, &string, 1) < 0)
1422            goto err;
1423
1424        if (self->write_func(self, repr_str, len) < 0)
1425            goto err;
1426
1427        if (self->write_func(self, "\n", 1) < 0)
1428            goto err;
1429
1430        Py_XDECREF(repr);
1431    }
1432    else {
1433        int i;
1434        char c_str[5];
1435
1436        if (!( repr = PyUnicode_AsUTF8String(args)))
1437            return -1;
1438
1439        if ((size = PyString_Size(repr)) < 0)
1440            goto err;
1441        if (size > INT_MAX)
1442            return -1;   /* string too large */
1443
1444        c_str[0] = BINUNICODE;
1445        for (i = 1; i < 5; i++)
1446            c_str[i] = (int)(size >> ((i - 1) * 8));
1447        len = 5;
1448
1449        if (self->write_func(self, c_str, len) < 0)
1450            goto err;
1451
1452        if (size > 128 && Pdata_Check(self->file)) {
1453            if (write_other(self, NULL, 0) < 0)
1454                goto err;
1455            PDATA_APPEND(self->file, repr, -1);
1456        }
1457        else {
1458            if (self->write_func(self, PyString_AS_STRING(repr),
1459                                 size) < 0)
1460                goto err;
1461        }
1462
1463        Py_DECREF(repr);
1464    }
1465
1466    if (doput)
1467        if (put(self, args) < 0)
1468            return -1;
1469
1470    return 0;
1471
1472  err:
1473    Py_XDECREF(repr);
1474    return -1;
1475}
1476#endif
1477
1478/* A helper for save_tuple.  Push the len elements in tuple t on the stack. */
1479static int
1480store_tuple_elements(Picklerobject *self, PyObject *t, int len)
1481{
1482    Py_ssize_t i;
1483    int res = -1;       /* guilty until proved innocent */
1484
1485    assert(PyTuple_Size(t) == len);
1486
1487    for (i = 0; i < len; i++) {
1488        PyObject *element = PyTuple_GET_ITEM(t, i);
1489
1490        if (element == NULL)
1491            goto finally;
1492        if (save(self, element, 0) < 0)
1493            goto finally;
1494    }
1495    res = 0;
1496
1497  finally:
1498    return res;
1499}
1500
1501/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1502 * used across protocols to minimize the space needed to pickle them.
1503 * Tuples are also the only builtin immutable type that can be recursive
1504 * (a tuple can be reached from itself), and that requires some subtle
1505 * magic so that it works in all cases.  IOW, this is a long routine.
1506 */
1507static int
1508save_tuple(Picklerobject *self, PyObject *args)
1509{
1510    PyObject *py_tuple_id = NULL;
1511    Py_ssize_t len, i;
1512    int res = -1;
1513
1514    static char tuple = TUPLE;
1515    static char pop = POP;
1516    static char pop_mark = POP_MARK;
1517    static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1518
1519    if ((len = PyTuple_Size(args)) < 0)
1520        goto finally;
1521
1522    if (len == 0) {
1523        char c_str[2];
1524
1525        if (self->proto) {
1526            c_str[0] = EMPTY_TUPLE;
1527            len = 1;
1528        }
1529        else {
1530            c_str[0] = MARK;
1531            c_str[1] = TUPLE;
1532            len = 2;
1533        }
1534        if (self->write_func(self, c_str, len) >= 0)
1535            res = 0;
1536        /* Don't memoize an empty tuple. */
1537        goto finally;
1538    }
1539
1540    /* A non-empty tuple. */
1541
1542    /* id(tuple) isn't in the memo now.  If it shows up there after
1543     * saving the tuple elements, the tuple must be recursive, in
1544     * which case we'll pop everything we put on the stack, and fetch
1545     * its value from the memo.
1546     */
1547    py_tuple_id = PyLong_FromVoidPtr(args);
1548    if (py_tuple_id == NULL)
1549        goto finally;
1550
1551    if (len <= 3 && self->proto >= 2) {
1552        /* Use TUPLE{1,2,3} opcodes. */
1553        if (store_tuple_elements(self, args, len) < 0)
1554            goto finally;
1555        if (PyDict_GetItem(self->memo, py_tuple_id)) {
1556            /* pop the len elements */
1557            for (i = 0; i < len; ++i)
1558                if (self->write_func(self, &pop, 1) < 0)
1559                    goto finally;
1560            /* fetch from memo */
1561            if (get(self, py_tuple_id) < 0)
1562                goto finally;
1563            res = 0;
1564            goto finally;
1565        }
1566        /* Not recursive. */
1567        if (self->write_func(self, len2opcode + len, 1) < 0)
1568            goto finally;
1569        goto memoize;
1570    }
1571
1572    /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1573     * Generate MARK elt1 elt2 ... TUPLE
1574     */
1575    if (self->write_func(self, &MARKv, 1) < 0)
1576        goto finally;
1577
1578    if (store_tuple_elements(self, args, len) < 0)
1579        goto finally;
1580
1581    if (PyDict_GetItem(self->memo, py_tuple_id)) {
1582        /* pop the stack stuff we pushed */
1583        if (self->bin) {
1584            if (self->write_func(self, &pop_mark, 1) < 0)
1585                goto finally;
1586        }
1587        else {
1588            /* Note that we pop one more than len, to remove
1589             * the MARK too.
1590             */
1591            for (i = 0; i <= len; i++)
1592                if (self->write_func(self, &pop, 1) < 0)
1593                    goto finally;
1594        }
1595        /* fetch from memo */
1596        if (get(self, py_tuple_id) >= 0)
1597            res = 0;
1598        goto finally;
1599    }
1600
1601    /* Not recursive. */
1602    if (self->write_func(self, &tuple, 1) < 0)
1603        goto finally;
1604
1605  memoize:
1606    if (put(self, args) >= 0)
1607        res = 0;
1608
1609  finally:
1610    Py_XDECREF(py_tuple_id);
1611    return res;
1612}
1613
1614/* iter is an iterator giving items, and we batch up chunks of
1615 *     MARK item item ... item APPENDS
1616 * opcode sequences.  Calling code should have arranged to first create an
1617 * empty list, or list-like object, for the APPENDS to operate on.
1618 * Returns 0 on success, <0 on error.
1619 */
1620static int
1621batch_list(Picklerobject *self, PyObject *iter)
1622{
1623    PyObject *obj = NULL;
1624    PyObject *firstitem = NULL;
1625    int i, n;
1626
1627    static char append = APPEND;
1628    static char appends = APPENDS;
1629
1630    assert(iter != NULL);
1631
1632    if (self->proto == 0) {
1633        /* APPENDS isn't available; do one at a time. */
1634        for (;;) {
1635            obj = PyIter_Next(iter);
1636            if (obj == NULL) {
1637                if (PyErr_Occurred())
1638                    return -1;
1639                break;
1640            }
1641            i = save(self, obj, 0);
1642            Py_DECREF(obj);
1643            if (i < 0)
1644                return -1;
1645            if (self->write_func(self, &append, 1) < 0)
1646                return -1;
1647        }
1648        return 0;
1649    }
1650
1651    /* proto > 0:  write in batches of BATCHSIZE. */
1652    do {
1653        /* Get first item */
1654        firstitem = PyIter_Next(iter);
1655        if (firstitem == NULL) {
1656            if (PyErr_Occurred())
1657                goto BatchFailed;
1658
1659            /* nothing more to add */
1660            break;
1661        }
1662
1663        /* Try to get a second item */
1664        obj = PyIter_Next(iter);
1665        if (obj == NULL) {
1666            if (PyErr_Occurred())
1667                goto BatchFailed;
1668
1669            /* Only one item to write */
1670            if (save(self, firstitem, 0) < 0)
1671                goto BatchFailed;
1672            if (self->write_func(self, &append, 1) < 0)
1673                goto BatchFailed;
1674            Py_CLEAR(firstitem);
1675            break;
1676        }
1677
1678        /* More than one item to write */
1679
1680        /* Pump out MARK, items, APPENDS. */
1681        if (self->write_func(self, &MARKv, 1) < 0)
1682            goto BatchFailed;
1683
1684        if (save(self, firstitem, 0) < 0)
1685            goto BatchFailed;
1686        Py_CLEAR(firstitem);
1687        n = 1;
1688
1689        /* Fetch and save up to BATCHSIZE items */
1690        while (obj) {
1691            if (save(self, obj, 0) < 0)
1692                goto BatchFailed;
1693            Py_CLEAR(obj);
1694            n += 1;
1695
1696            if (n == BATCHSIZE)
1697                break;
1698
1699            obj = PyIter_Next(iter);
1700            if (obj == NULL) {
1701                if (PyErr_Occurred())
1702                    goto BatchFailed;
1703                break;
1704            }
1705        }
1706
1707        if (self->write_func(self, &appends, 1) < 0)
1708            goto BatchFailed;
1709
1710    } while (n == BATCHSIZE);
1711    return 0;
1712
1713BatchFailed:
1714    Py_XDECREF(firstitem);
1715    Py_XDECREF(obj);
1716    return -1;
1717}
1718
1719static int
1720save_list(Picklerobject *self, PyObject *args)
1721{
1722    int res = -1;
1723    char s[3];
1724    Py_ssize_t len;
1725    PyObject *iter;
1726
1727    if (self->fast && !fast_save_enter(self, args))
1728        goto finally;
1729
1730    /* Create an empty list. */
1731    if (self->bin) {
1732        s[0] = EMPTY_LIST;
1733        len = 1;
1734    }
1735    else {
1736        s[0] = MARK;
1737        s[1] = LIST;
1738        len = 2;
1739    }
1740
1741    if (self->write_func(self, s, len) < 0)
1742        goto finally;
1743
1744    /* Get list length, and bow out early if empty. */
1745    if ((len = PyList_Size(args)) < 0)
1746        goto finally;
1747
1748    /* Memoize. */
1749    if (len == 0) {
1750        if (put(self, args) >= 0)
1751            res = 0;
1752        goto finally;
1753    }
1754    if (put2(self, args) < 0)
1755        goto finally;
1756
1757    /* Materialize the list elements. */
1758    iter = PyObject_GetIter(args);
1759    if (iter == NULL)
1760        goto finally;
1761
1762    if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1763    {
1764        res = batch_list(self, iter);
1765        Py_LeaveRecursiveCall();
1766    }
1767    Py_DECREF(iter);
1768
1769  finally:
1770    if (self->fast && !fast_save_leave(self, args))
1771        res = -1;
1772
1773    return res;
1774}
1775
1776
1777/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1778 *     MARK key value ... key value SETITEMS
1779 * opcode sequences.  Calling code should have arranged to first create an
1780 * empty dict, or dict-like object, for the SETITEMS to operate on.
1781 * Returns 0 on success, <0 on error.
1782 *
1783 * This is very much like batch_list().  The difference between saving
1784 * elements directly, and picking apart two-tuples, is so long-winded at
1785 * the C level, though, that attempts to combine these routines were too
1786 * ugly to bear.
1787 */
1788static int
1789batch_dict(Picklerobject *self, PyObject *iter)
1790{
1791    PyObject *p = NULL;
1792    PyObject *firstitem = NULL;
1793    int i, n;
1794
1795    static char setitem = SETITEM;
1796    static char setitems = SETITEMS;
1797
1798    assert(iter != NULL);
1799
1800    if (self->proto == 0) {
1801        /* SETITEMS isn't available; do one at a time. */
1802        for (;;) {
1803            p = PyIter_Next(iter);
1804            if (p == NULL) {
1805                if (PyErr_Occurred())
1806                    return -1;
1807                break;
1808            }
1809            if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1810                PyErr_SetString(PyExc_TypeError, "dict items "
1811                    "iterator must return 2-tuples");
1812                return -1;
1813            }
1814            i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1815            if (i >= 0)
1816                i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1817            Py_DECREF(p);
1818            if (i < 0)
1819                return -1;
1820            if (self->write_func(self, &setitem, 1) < 0)
1821                return -1;
1822        }
1823        return 0;
1824    }
1825
1826    /* proto > 0:  write in batches of BATCHSIZE. */
1827    do {
1828        /* Get first item */
1829        firstitem = PyIter_Next(iter);
1830        if (firstitem == NULL) {
1831            if (PyErr_Occurred())
1832                goto BatchFailed;
1833
1834            /* nothing more to add */
1835            break;
1836        }
1837        if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1838            PyErr_SetString(PyExc_TypeError, "dict items "
1839                            "iterator must return 2-tuples");
1840            goto BatchFailed;
1841        }
1842
1843        /* Try to get a second item */
1844        p = PyIter_Next(iter);
1845        if (p == NULL) {
1846            if (PyErr_Occurred())
1847                goto BatchFailed;
1848
1849            /* Only one item to write */
1850            if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1851                goto BatchFailed;
1852            if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1853                goto BatchFailed;
1854            if (self->write_func(self, &setitem, 1) < 0)
1855                goto BatchFailed;
1856            Py_CLEAR(firstitem);
1857            break;
1858        }
1859
1860        /* More than one item to write */
1861
1862        /* Pump out MARK, items, SETITEMS. */
1863        if (self->write_func(self, &MARKv, 1) < 0)
1864            goto BatchFailed;
1865
1866        if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1867            goto BatchFailed;
1868        if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1869            goto BatchFailed;
1870        Py_CLEAR(firstitem);
1871        n = 1;
1872
1873        /* Fetch and save up to BATCHSIZE items */
1874        while (p) {
1875            if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1876                PyErr_SetString(PyExc_TypeError, "dict items "
1877                    "iterator must return 2-tuples");
1878                goto BatchFailed;
1879            }
1880            if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1881                goto BatchFailed;
1882            if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1883                goto BatchFailed;
1884            Py_CLEAR(p);
1885            n += 1;
1886
1887            if (n == BATCHSIZE)
1888                break;
1889
1890            p = PyIter_Next(iter);
1891            if (p == NULL) {
1892                if (PyErr_Occurred())
1893                    goto BatchFailed;
1894                break;
1895            }
1896        }
1897
1898        if (self->write_func(self, &setitems, 1) < 0)
1899            goto BatchFailed;
1900
1901    } while (n == BATCHSIZE);
1902    return 0;
1903
1904BatchFailed:
1905    Py_XDECREF(firstitem);
1906    Py_XDECREF(p);
1907    return -1;
1908}
1909
1910/* This is a variant of batch_dict() above that specializes for dicts, with no
1911 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1912 *     MARK key value ... key value SETITEMS
1913 * opcode sequences.  Calling code should have arranged to first create an
1914 * empty dict, or dict-like object, for the SETITEMS to operate on.
1915 * Returns 0 on success, -1 on error.
1916 *
1917 * Note that this currently doesn't work for protocol 0.
1918 */
1919static int
1920batch_dict_exact(Picklerobject *self, PyObject *obj)
1921{
1922    PyObject *key = NULL, *value = NULL;
1923    int i;
1924    Py_ssize_t dict_size, ppos = 0;
1925
1926    static char setitem = SETITEM;
1927    static char setitems = SETITEMS;
1928
1929    assert(obj != NULL);
1930    assert(self->proto > 0);
1931
1932    dict_size = PyDict_Size(obj);
1933
1934    /* Special-case len(d) == 1 to save space. */
1935    if (dict_size == 1) {
1936        PyDict_Next(obj, &ppos, &key, &value);
1937        if (save(self, key, 0) < 0)
1938            return -1;
1939        if (save(self, value, 0) < 0)
1940            return -1;
1941        if (self->write_func(self, &setitem, 1) < 0)
1942            return -1;
1943        return 0;
1944    }
1945
1946    /* Write in batches of BATCHSIZE. */
1947    do {
1948        i = 0;
1949        if (self->write_func(self, &MARKv, 1) < 0)
1950            return -1;
1951        while (PyDict_Next(obj, &ppos, &key, &value)) {
1952            if (save(self, key, 0) < 0)
1953                return -1;
1954            if (save(self, value, 0) < 0)
1955                return -1;
1956            if (++i == BATCHSIZE)
1957                break;
1958        }
1959        if (self->write_func(self, &setitems, 1) < 0)
1960            return -1;
1961        if (PyDict_Size(obj) != dict_size) {
1962            PyErr_Format(
1963                PyExc_RuntimeError,
1964                "dictionary changed size during iteration");
1965            return -1;
1966        }
1967
1968    } while (i == BATCHSIZE);
1969    return 0;
1970}
1971
1972static int
1973save_dict(Picklerobject *self, PyObject *args)
1974{
1975    int res = -1;
1976    char s[3];
1977    Py_ssize_t len;
1978
1979    if (self->fast && !fast_save_enter(self, args))
1980        goto finally;
1981
1982    /* Create an empty dict. */
1983    if (self->bin) {
1984        s[0] = EMPTY_DICT;
1985        len = 1;
1986    }
1987    else {
1988        s[0] = MARK;
1989        s[1] = DICT;
1990        len = 2;
1991    }
1992
1993    if (self->write_func(self, s, len) < 0)
1994        goto finally;
1995
1996    /* Get dict size, and bow out early if empty. */
1997    if ((len = PyDict_Size(args)) < 0)
1998        goto finally;
1999
2000    if (len == 0) {
2001        if (put(self, args) >= 0)
2002            res = 0;
2003        goto finally;
2004    }
2005    if (put2(self, args) < 0)
2006        goto finally;
2007
2008    /* Materialize the dict items. */
2009    if (PyDict_CheckExact(args) && self->proto > 0) {
2010        /* We can take certain shortcuts if we know this is a dict and
2011           not a dict subclass. */
2012        if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2013            res = batch_dict_exact(self, args);
2014            Py_LeaveRecursiveCall();
2015        }
2016    } else {
2017        PyObject *iter = PyObject_CallMethod(args, "iteritems", "()");
2018        if (iter == NULL)
2019            goto finally;
2020        if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2021            res = batch_dict(self, iter);
2022            Py_LeaveRecursiveCall();
2023        }
2024        Py_DECREF(iter);
2025    }
2026
2027  finally:
2028    if (self->fast && !fast_save_leave(self, args))
2029        res = -1;
2030
2031    return res;
2032}
2033
2034
2035static int
2036save_inst(Picklerobject *self, PyObject *args)
2037{
2038    PyObject *class = 0, *module = 0, *name = 0, *state = 0,
2039        *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
2040    char *module_str, *name_str;
2041    int module_size, name_size, res = -1;
2042
2043    static char inst = INST, obj = OBJ, build = BUILD;
2044
2045    if (self->fast && !fast_save_enter(self, args))
2046        goto finally;
2047
2048    if (self->write_func(self, &MARKv, 1) < 0)
2049        goto finally;
2050
2051    if (!( class = PyObject_GetAttr(args, __class___str)))
2052        goto finally;
2053
2054    if (self->bin) {
2055        if (save(self, class, 0) < 0)
2056            goto finally;
2057    }
2058
2059    if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
2060        PyObject *element = 0;
2061        Py_ssize_t i, len;
2062
2063        if (!( class_args =
2064               PyObject_Call(getinitargs_func, empty_tuple, NULL)))
2065            goto finally;
2066
2067        if ((len = PyObject_Size(class_args)) < 0)
2068            goto finally;
2069
2070        for (i = 0; i < len; i++) {
2071            if (!( element = PySequence_GetItem(class_args, i)))
2072                goto finally;
2073
2074            if (save(self, element, 0) < 0) {
2075                Py_DECREF(element);
2076                goto finally;
2077            }
2078
2079            Py_DECREF(element);
2080        }
2081    }
2082    else {
2083        if (PyErr_ExceptionMatches(PyExc_AttributeError))
2084            PyErr_Clear();
2085        else
2086            goto finally;
2087    }
2088
2089    if (!self->bin) {
2090        if (!( name = ((PyClassObject *)class)->cl_name ))  {
2091            PyErr_SetString(PicklingError, "class has no name");
2092            goto finally;
2093        }
2094
2095        if (!( module = whichmodule(class, name)))
2096            goto finally;
2097
2098
2099        if ((module_size = PyString_Size(module)) < 0 ||
2100            (name_size = PyString_Size(name)) < 0)
2101            goto finally;
2102
2103        module_str = PyString_AS_STRING((PyStringObject *)module);
2104        name_str   = PyString_AS_STRING((PyStringObject *)name);
2105
2106        if (self->write_func(self, &inst, 1) < 0)
2107            goto finally;
2108
2109        if (self->write_func(self, module_str, module_size) < 0)
2110            goto finally;
2111
2112        if (self->write_func(self, "\n", 1) < 0)
2113            goto finally;
2114
2115        if (self->write_func(self, name_str, name_size) < 0)
2116            goto finally;
2117
2118        if (self->write_func(self, "\n", 1) < 0)
2119            goto finally;
2120    }
2121    else if (self->write_func(self, &obj, 1) < 0) {
2122        goto finally;
2123    }
2124
2125    if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2126        state = PyObject_Call(getstate_func, empty_tuple, NULL);
2127        if (!state)
2128            goto finally;
2129    }
2130    else {
2131        if (PyErr_ExceptionMatches(PyExc_AttributeError))
2132            PyErr_Clear();
2133        else
2134            goto finally;
2135
2136        if (!( state = PyObject_GetAttr(args, __dict___str)))  {
2137            if (PyErr_ExceptionMatches(PyExc_AttributeError))
2138                PyErr_Clear();
2139            else
2140                goto finally;
2141            res = 0;
2142            goto finally;
2143        }
2144    }
2145
2146    if (!PyDict_Check(state)) {
2147        if (put2(self, args) < 0)
2148            goto finally;
2149    }
2150    else {
2151        if (put(self, args) < 0)
2152            goto finally;
2153    }
2154
2155    if (save(self, state, 0) < 0)
2156        goto finally;
2157
2158    if (self->write_func(self, &build, 1) < 0)
2159        goto finally;
2160
2161    res = 0;
2162
2163  finally:
2164    if (self->fast && !fast_save_leave(self, args))
2165        res = -1;
2166
2167    Py_XDECREF(module);
2168    Py_XDECREF(class);
2169    Py_XDECREF(state);
2170    Py_XDECREF(getinitargs_func);
2171    Py_XDECREF(getstate_func);
2172    Py_XDECREF(class_args);
2173
2174    return res;
2175}
2176
2177
2178static int
2179save_global(Picklerobject *self, PyObject *args, PyObject *name)
2180{
2181    PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2182    char *name_str, *module_str;
2183    int module_size, name_size, res = -1;
2184
2185    static char global = GLOBAL;
2186
2187    if (name) {
2188        global_name = name;
2189        Py_INCREF(global_name);
2190    }
2191    else {
2192        if (!( global_name = PyObject_GetAttr(args, __name___str)))
2193            goto finally;
2194    }
2195
2196    if (!( module = whichmodule(args, global_name)))
2197        goto finally;
2198
2199    if ((module_size = PyString_Size(module)) < 0 ||
2200        (name_size = PyString_Size(global_name)) < 0)
2201        goto finally;
2202
2203    module_str = PyString_AS_STRING((PyStringObject *)module);
2204    name_str   = PyString_AS_STRING((PyStringObject *)global_name);
2205
2206    /* XXX This can be doing a relative import.  Clearly it shouldn't,
2207       but I don't know how to stop it. :-( */
2208    mod = PyImport_ImportModule(module_str);
2209    if (mod == NULL) {
2210        cPickle_ErrFormat(PicklingError,
2211                          "Can't pickle %s: import of module %s "
2212                          "failed",
2213                          "OS", args, module);
2214        goto finally;
2215    }
2216    klass = PyObject_GetAttrString(mod, name_str);
2217    if (klass == NULL) {
2218        cPickle_ErrFormat(PicklingError,
2219                          "Can't pickle %s: attribute lookup %s.%s "
2220                          "failed",
2221                          "OSS", args, module, global_name);
2222        goto finally;
2223    }
2224    if (klass != args) {
2225        Py_DECREF(klass);
2226        cPickle_ErrFormat(PicklingError,
2227                          "Can't pickle %s: it's not the same object "
2228                                "as %s.%s",
2229                          "OSS", args, module, global_name);
2230        goto finally;
2231    }
2232    Py_DECREF(klass);
2233
2234    if (self->proto >= 2) {
2235        /* See whether this is in the extension registry, and if
2236         * so generate an EXT opcode.
2237         */
2238        PyObject *py_code;              /* extension code as Python object */
2239        long code;                      /* extension code as C value */
2240        char c_str[5];
2241        int n;
2242
2243        PyTuple_SET_ITEM(two_tuple, 0, module);
2244        PyTuple_SET_ITEM(two_tuple, 1, global_name);
2245        py_code = PyDict_GetItem(extension_registry, two_tuple);
2246        if (py_code == NULL)
2247            goto gen_global;                    /* not registered */
2248
2249        /* Verify py_code has the right type and value. */
2250        if (!PyInt_Check(py_code)) {
2251            cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2252                "extension code %s isn't an integer",
2253                "OO", args, py_code);
2254            goto finally;
2255        }
2256        code = PyInt_AS_LONG(py_code);
2257        if (code <= 0 ||  code > 0x7fffffffL) {
2258            cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2259                "extension code %ld is out of range",
2260                "Ol", args, code);
2261            goto finally;
2262        }
2263
2264        /* Generate an EXT opcode. */
2265        if (code <= 0xff) {
2266            c_str[0] = EXT1;
2267            c_str[1] = (char)code;
2268            n = 2;
2269        }
2270        else if (code <= 0xffff) {
2271            c_str[0] = EXT2;
2272            c_str[1] = (char)(code & 0xff);
2273            c_str[2] = (char)((code >> 8) & 0xff);
2274            n = 3;
2275        }
2276        else {
2277            c_str[0] = EXT4;
2278            c_str[1] = (char)(code & 0xff);
2279            c_str[2] = (char)((code >> 8) & 0xff);
2280            c_str[3] = (char)((code >> 16) & 0xff);
2281            c_str[4] = (char)((code >> 24) & 0xff);
2282            n = 5;
2283        }
2284
2285        if (self->write_func(self, c_str, n) >= 0)
2286            res = 0;
2287        goto finally;           /* and don't memoize */
2288    }
2289
2290  gen_global:
2291    if (self->write_func(self, &global, 1) < 0)
2292        goto finally;
2293
2294    if (self->write_func(self, module_str, module_size) < 0)
2295        goto finally;
2296
2297    if (self->write_func(self, "\n", 1) < 0)
2298        goto finally;
2299
2300    if (self->write_func(self, name_str, name_size) < 0)
2301        goto finally;
2302
2303    if (self->write_func(self, "\n", 1) < 0)
2304        goto finally;
2305
2306    if (put(self, args) < 0)
2307        goto finally;
2308
2309    res = 0;
2310
2311  finally:
2312    Py_XDECREF(module);
2313    Py_XDECREF(global_name);
2314    Py_XDECREF(mod);
2315
2316    return res;
2317}
2318
2319static int
2320save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2321{
2322    PyObject *pid = 0;
2323    Py_ssize_t size;
2324    int res = -1;
2325
2326    static char persid = PERSID, binpersid = BINPERSID;
2327
2328    Py_INCREF(args);
2329    ARG_TUP(self, args);
2330    if (self->arg) {
2331        pid = PyObject_Call(f, self->arg, NULL);
2332        FREE_ARG_TUP(self);
2333    }
2334    if (! pid) return -1;
2335
2336    if (pid != Py_None) {
2337        if (!self->bin) {
2338            if (!PyString_Check(pid)) {
2339                PyErr_SetString(PicklingError,
2340                                "persistent id must be string");
2341                goto finally;
2342            }
2343
2344            if (self->write_func(self, &persid, 1) < 0)
2345                goto finally;
2346
2347            if ((size = PyString_Size(pid)) < 0)
2348                goto finally;
2349
2350            if (self->write_func(self,
2351                                 PyString_AS_STRING(
2352                                    (PyStringObject *)pid),
2353                                 size) < 0)
2354                goto finally;
2355
2356            if (self->write_func(self, "\n", 1) < 0)
2357                goto finally;
2358
2359            res = 1;
2360            goto finally;
2361        }
2362        else if (save(self, pid, 1) >= 0) {
2363            if (self->write_func(self, &binpersid, 1) < 0)
2364                res = -1;
2365            else
2366                res = 1;
2367        }
2368
2369        goto finally;
2370    }
2371
2372    res = 0;
2373
2374  finally:
2375    Py_XDECREF(pid);
2376
2377    return res;
2378}
2379
2380/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2381 * appropriate __reduce__ method for ob.
2382 */
2383static int
2384save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
2385{
2386    PyObject *callable;
2387    PyObject *argtup;
2388    PyObject *state = NULL;
2389    PyObject *listitems = Py_None;
2390    PyObject *dictitems = Py_None;
2391    Py_ssize_t size;
2392
2393    int use_newobj = self->proto >= 2;
2394
2395    static char reduce = REDUCE;
2396    static char build = BUILD;
2397    static char newobj = NEWOBJ;
2398
2399    size = PyTuple_Size(args);
2400    if (size < 2 || size > 5) {
2401        cPickle_ErrFormat(PicklingError, "tuple returned by "
2402            "%s must contain 2 through 5 elements",
2403            "O", fn);
2404        return -1;
2405    }
2406
2407    if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2408                            &callable,
2409                            &argtup,
2410                            &state,
2411                            &listitems,
2412                            &dictitems))
2413        return -1;
2414
2415    if (!PyTuple_Check(argtup)) {
2416        cPickle_ErrFormat(PicklingError, "Second element of "
2417            "tuple returned by %s must be a tuple",
2418            "O", fn);
2419        return -1;
2420    }
2421
2422    if (state == Py_None)
2423        state = NULL;
2424
2425    if (listitems == Py_None)
2426        listitems = NULL;
2427    else if (!PyIter_Check(listitems)) {
2428        cPickle_ErrFormat(PicklingError, "Fourth element of "
2429            "tuple returned by %s must be an iterator, not %s",
2430            "Os", fn, Py_TYPE(listitems)->tp_name);
2431        return -1;
2432    }
2433
2434    if (dictitems == Py_None)
2435        dictitems = NULL;
2436    else if (!PyIter_Check(dictitems)) {
2437        cPickle_ErrFormat(PicklingError, "Fifth element of "
2438            "tuple returned by %s must be an iterator, not %s",
2439            "Os", fn, Py_TYPE(dictitems)->tp_name);
2440        return -1;
2441    }
2442
2443    /* Protocol 2 special case: if callable's name is __newobj__, use
2444     * NEWOBJ.  This consumes a lot of code.
2445     */
2446    if (use_newobj) {
2447        PyObject *temp = PyObject_GetAttr(callable, __name___str);
2448
2449        if (temp == NULL) {
2450            if (PyErr_ExceptionMatches(PyExc_AttributeError))
2451                PyErr_Clear();
2452            else
2453                return -1;
2454            use_newobj = 0;
2455        }
2456        else {
2457            use_newobj = PyString_Check(temp) &&
2458                         strcmp(PyString_AS_STRING(temp),
2459                                "__newobj__") == 0;
2460            Py_DECREF(temp);
2461        }
2462    }
2463    if (use_newobj) {
2464        PyObject *cls;
2465        PyObject *newargtup;
2466        Py_ssize_t n, i;
2467
2468        /* Sanity checks. */
2469        n = PyTuple_Size(argtup);
2470        if (n < 1) {
2471            PyErr_SetString(PicklingError, "__newobj__ arglist "
2472                "is empty");
2473            return -1;
2474        }
2475
2476        cls = PyTuple_GET_ITEM(argtup, 0);
2477        if (! PyObject_HasAttrString(cls, "__new__")) {
2478            PyErr_SetString(PicklingError, "args[0] from "
2479                "__newobj__ args has no __new__");
2480            return -1;
2481        }
2482
2483        /* XXX How could ob be NULL? */
2484        if (ob != NULL) {
2485            PyObject *ob_dot_class;
2486
2487            ob_dot_class = PyObject_GetAttr(ob, __class___str);
2488            if (ob_dot_class == NULL) {
2489                if (PyErr_ExceptionMatches(
2490                            PyExc_AttributeError))
2491                    PyErr_Clear();
2492                else
2493                    return -1;
2494            }
2495            i = ob_dot_class != cls; /* true iff a problem */
2496            Py_XDECREF(ob_dot_class);
2497            if (i) {
2498                PyErr_SetString(PicklingError, "args[0] from "
2499                    "__newobj__ args has the wrong class");
2500                return -1;
2501            }
2502        }
2503
2504        /* Save the class and its __new__ arguments. */
2505        if (save(self, cls, 0) < 0)
2506            return -1;
2507
2508        newargtup = PyTuple_New(n-1);  /* argtup[1:] */
2509        if (newargtup == NULL)
2510            return -1;
2511        for (i = 1; i < n; ++i) {
2512            PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2513            Py_INCREF(temp);
2514            PyTuple_SET_ITEM(newargtup, i-1, temp);
2515        }
2516        i = save(self, newargtup, 0);
2517        Py_DECREF(newargtup);
2518        if (i < 0)
2519            return -1;
2520
2521        /* Add NEWOBJ opcode. */
2522        if (self->write_func(self, &newobj, 1) < 0)
2523            return -1;
2524    }
2525    else {
2526        /* Not using NEWOBJ. */
2527        if (save(self, callable, 0) < 0 ||
2528            save(self, argtup, 0) < 0 ||
2529            self->write_func(self, &reduce, 1) < 0)
2530            return -1;
2531    }
2532
2533    /* Memoize. */
2534    /* XXX How can ob be NULL? */
2535    if (ob != NULL) {
2536        if (state && !PyDict_Check(state)) {
2537            if (put2(self, ob) < 0)
2538                return -1;
2539        }
2540        else if (put(self, ob) < 0)
2541                        return -1;
2542    }
2543
2544
2545    if (listitems && batch_list(self, listitems) < 0)
2546        return -1;
2547
2548    if (dictitems && batch_dict(self, dictitems) < 0)
2549        return -1;
2550
2551    if (state) {
2552        if (save(self, state, 0) < 0 ||
2553            self->write_func(self, &build, 1) < 0)
2554            return -1;
2555    }
2556
2557    return 0;
2558}
2559
2560static int
2561save(Picklerobject *self, PyObject *args, int pers_save)
2562{
2563    PyTypeObject *type;
2564    PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2565    int res = -1;
2566    int tmp;
2567
2568    if (Py_EnterRecursiveCall(" while pickling an object"))
2569        return -1;
2570
2571    if (!pers_save && self->pers_func) {
2572        if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2573            res = tmp;
2574            goto finally;
2575        }
2576    }
2577
2578    if (args == Py_None) {
2579        res = save_none(self, args);
2580        goto finally;
2581    }
2582
2583    type = Py_TYPE(args);
2584
2585    switch (type->tp_name[0]) {
2586    case 'b':
2587        if (args == Py_False || args == Py_True) {
2588            res = save_bool(self, args);
2589            goto finally;
2590        }
2591        break;
2592    case 'i':
2593        if (type == &PyInt_Type) {
2594            res = save_int(self, args);
2595            goto finally;
2596        }
2597        break;
2598
2599    case 'l':
2600        if (type == &PyLong_Type) {
2601            res = save_long(self, args);
2602            goto finally;
2603        }
2604        break;
2605
2606    case 'f':
2607        if (type == &PyFloat_Type) {
2608            res = save_float(self, args);
2609            goto finally;
2610        }
2611        break;
2612
2613    case 't':
2614        if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2615            res = save_tuple(self, args);
2616            goto finally;
2617        }
2618        break;
2619
2620    case 's':
2621        if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2622            res = save_string(self, args, 0);
2623            goto finally;
2624        }
2625        break;
2626
2627#ifdef Py_USING_UNICODE
2628    case 'u':
2629        if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2630            res = save_unicode(self, args, 0);
2631            goto finally;
2632        }
2633        break;
2634#endif
2635    }
2636
2637    if (Py_REFCNT(args) > 1) {
2638        if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2639            goto finally;
2640
2641        if (PyDict_GetItem(self->memo, py_ob_id)) {
2642            if (get(self, py_ob_id) < 0)
2643                goto finally;
2644
2645            res = 0;
2646            goto finally;
2647        }
2648    }
2649
2650    switch (type->tp_name[0]) {
2651    case 's':
2652        if (type == &PyString_Type) {
2653            res = save_string(self, args, 1);
2654            goto finally;
2655        }
2656        break;
2657
2658#ifdef Py_USING_UNICODE
2659    case 'u':
2660        if (type == &PyUnicode_Type) {
2661            res = save_unicode(self, args, 1);
2662            goto finally;
2663        }
2664        break;
2665#endif
2666
2667    case 't':
2668        if (type == &PyTuple_Type) {
2669            res = save_tuple(self, args);
2670            goto finally;
2671        }
2672        if (type == &PyType_Type) {
2673            res = save_global(self, args, NULL);
2674            goto finally;
2675        }
2676        break;
2677
2678    case 'l':
2679        if (type == &PyList_Type) {
2680            res = save_list(self, args);
2681            goto finally;
2682        }
2683        break;
2684
2685    case 'd':
2686        if (type == &PyDict_Type) {
2687            res = save_dict(self, args);
2688            goto finally;
2689        }
2690        break;
2691
2692    case 'i':
2693        if (type == &PyInstance_Type) {
2694            res = save_inst(self, args);
2695            goto finally;
2696        }
2697        break;
2698
2699    case 'c':
2700        if (type == &PyClass_Type) {
2701            res = save_global(self, args, NULL);
2702            goto finally;
2703        }
2704        break;
2705
2706    case 'f':
2707        if (type == &PyFunction_Type) {
2708            res = save_global(self, args, NULL);
2709            if (res && PyErr_ExceptionMatches(PickleError)) {
2710                /* fall back to reduce */
2711                PyErr_Clear();
2712                break;
2713            }
2714            goto finally;
2715        }
2716        break;
2717
2718    case 'b':
2719        if (type == &PyCFunction_Type) {
2720            res = save_global(self, args, NULL);
2721            goto finally;
2722        }
2723    }
2724
2725    if (!pers_save && self->inst_pers_func) {
2726        if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2727            res = tmp;
2728            goto finally;
2729        }
2730    }
2731
2732    /* Get a reduction callable, and call it.  This may come from
2733     * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2734     * or the object's __reduce__ method.
2735     */
2736    __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2737    if (__reduce__ != NULL) {
2738        Py_INCREF(__reduce__);
2739        Py_INCREF(args);
2740        ARG_TUP(self, args);
2741        if (self->arg) {
2742            t = PyObject_Call(__reduce__, self->arg, NULL);
2743            FREE_ARG_TUP(self);
2744        }
2745    }
2746    else {
2747        if (PyType_IsSubtype(type, &PyType_Type)) {
2748            res = save_global(self, args, NULL);
2749            goto finally;
2750        }
2751
2752        /* Check for a __reduce_ex__ method. */
2753        __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2754        if (__reduce__ != NULL) {
2755            t = PyInt_FromLong(self->proto);
2756            if (t != NULL) {
2757                ARG_TUP(self, t);
2758                t = NULL;
2759                if (self->arg) {
2760                    t = PyObject_Call(__reduce__,
2761                                      self->arg, NULL);
2762                    FREE_ARG_TUP(self);
2763                }
2764            }
2765        }
2766        else {
2767            if (PyErr_ExceptionMatches(PyExc_AttributeError))
2768                PyErr_Clear();
2769            else
2770                goto finally;
2771            /* Check for a __reduce__ method. */
2772            __reduce__ = PyObject_GetAttr(args, __reduce___str);
2773            if (__reduce__ != NULL) {
2774                t = PyObject_Call(__reduce__,
2775                                  empty_tuple, NULL);
2776            }
2777            else {
2778                PyErr_SetObject(UnpickleableError, args);
2779                goto finally;
2780            }
2781        }
2782    }
2783
2784    if (t == NULL)
2785        goto finally;
2786
2787    if (PyString_Check(t)) {
2788        res = save_global(self, args, t);
2789        goto finally;
2790    }
2791
2792    if (!PyTuple_Check(t)) {
2793        cPickle_ErrFormat(PicklingError, "Value returned by "
2794                        "%s must be string or tuple",
2795                        "O", __reduce__);
2796        goto finally;
2797    }
2798
2799    res = save_reduce(self, t, __reduce__, args);
2800
2801  finally:
2802    Py_LeaveRecursiveCall();
2803    Py_XDECREF(py_ob_id);
2804    Py_XDECREF(__reduce__);
2805    Py_XDECREF(t);
2806
2807    return res;
2808}
2809
2810
2811static int
2812dump(Picklerobject *self, PyObject *args)
2813{
2814    static char stop = STOP;
2815
2816    if (self->proto >= 2) {
2817        char bytes[2];
2818
2819        bytes[0] = PROTO;
2820        assert(self->proto >= 0 && self->proto < 256);
2821        bytes[1] = (char)self->proto;
2822        if (self->write_func(self, bytes, 2) < 0)
2823            return -1;
2824    }
2825
2826    if (save(self, args, 0) < 0)
2827        return -1;
2828
2829    if (self->write_func(self, &stop, 1) < 0)
2830        return -1;
2831
2832    if (self->write_func(self, NULL, 0) < 0)
2833        return -1;
2834
2835    return 0;
2836}
2837
2838static PyObject *
2839Pickle_clear_memo(Picklerobject *self, PyObject *args)
2840{
2841    if (self->memo)
2842        PyDict_Clear(self->memo);
2843    Py_INCREF(Py_None);
2844    return Py_None;
2845}
2846
2847static PyObject *
2848Pickle_getvalue(Picklerobject *self, PyObject *args)
2849{
2850    Py_ssize_t l, i, rsize, ssize, clear=1, lm;
2851    long ik;
2852    PyObject *k, *r;
2853    char *s, *p, *have_get;
2854    Pdata *data;
2855
2856    /* Can be called by Python code or C code */
2857    if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2858        return NULL;
2859
2860    /* Check to make sure we are based on a list */
2861    if (! Pdata_Check(self->file)) {
2862        PyErr_SetString(PicklingError,
2863                        "Attempt to getvalue() a non-list-based pickler");
2864        return NULL;
2865    }
2866
2867    /* flush write buffer */
2868    if (write_other(self, NULL, 0) < 0) return NULL;
2869
2870    data=(Pdata*)self->file;
2871    l=data->length;
2872
2873    /* set up an array to hold get/put status */
2874    lm = PyDict_Size(self->memo);
2875    if (lm < 0) return NULL;
2876    lm++;
2877    have_get = malloc(lm);
2878    if (have_get == NULL) return PyErr_NoMemory();
2879    memset(have_get, 0, lm);
2880
2881    /* Scan for gets. */
2882    for (rsize = 0, i = l; --i >= 0; ) {
2883        k = data->data[i];
2884
2885        if (PyString_Check(k))
2886            rsize += PyString_GET_SIZE(k);
2887
2888        else if (PyInt_Check(k)) { /* put */
2889            ik = PyInt_AS_LONG((PyIntObject*)k);
2890            if (ik >= lm || ik == 0) {
2891                PyErr_SetString(PicklingError,
2892                                "Invalid get data");
2893                goto err;
2894            }
2895            if (have_get[ik]) /* with matching get */
2896                rsize += ik < 256 ? 2 : 5;
2897        }
2898
2899        else if (! (PyTuple_Check(k) &&
2900                    PyTuple_GET_SIZE(k) == 2 &&
2901                    PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2902            ) {
2903            PyErr_SetString(PicklingError,
2904                            "Unexpected data in internal list");
2905            goto err;
2906        }
2907
2908        else { /* put */
2909            ik = PyInt_AS_LONG((PyIntObject *)k);
2910            if (ik >= lm || ik == 0) {
2911                PyErr_SetString(PicklingError,
2912                                "Invalid get data");
2913                return NULL;
2914            }
2915            have_get[ik] = 1;
2916            rsize += ik < 256 ? 2 : 5;
2917        }
2918    }
2919
2920    /* Now generate the result */
2921    r = PyString_FromStringAndSize(NULL, rsize);
2922    if (r == NULL) goto err;
2923    s = PyString_AS_STRING((PyStringObject *)r);
2924
2925    for (i = 0; i < l; i++) {
2926        k = data->data[i];
2927
2928        if (PyString_Check(k)) {
2929            ssize = PyString_GET_SIZE(k);
2930            if (ssize) {
2931                p=PyString_AS_STRING((PyStringObject *)k);
2932                while (--ssize >= 0)
2933                    *s++ = *p++;
2934            }
2935        }
2936
2937        else if (PyTuple_Check(k)) { /* get */
2938            ik = PyInt_AS_LONG((PyIntObject *)
2939                                PyTuple_GET_ITEM(k, 0));
2940            if (ik < 256) {
2941                *s++ = BINGET;
2942                *s++ = (int)(ik & 0xff);
2943            }
2944            else {
2945                *s++ = LONG_BINGET;
2946                *s++ = (int)(ik & 0xff);
2947                *s++ = (int)((ik >> 8)  & 0xff);
2948                *s++ = (int)((ik >> 16) & 0xff);
2949                *s++ = (int)((ik >> 24) & 0xff);
2950            }
2951        }
2952
2953        else { /* put */
2954            ik = PyInt_AS_LONG((PyIntObject*)k);
2955
2956            if (have_get[ik]) { /* with matching get */
2957                if (ik < 256) {
2958                    *s++ = BINPUT;
2959                    *s++ = (int)(ik & 0xff);
2960                }
2961                else {
2962                    *s++ = LONG_BINPUT;
2963                    *s++ = (int)(ik & 0xff);
2964                    *s++ = (int)((ik >> 8)  & 0xff);
2965                    *s++ = (int)((ik >> 16) & 0xff);
2966                    *s++ = (int)((ik >> 24) & 0xff);
2967                }
2968            }
2969        }
2970    }
2971
2972    if (clear) {
2973        PyDict_Clear(self->memo);
2974        Pdata_clear(data, 0);
2975    }
2976
2977    free(have_get);
2978    return r;
2979  err:
2980    free(have_get);
2981    return NULL;
2982}
2983
2984static PyObject *
2985Pickler_dump(Picklerobject *self, PyObject *args)
2986{
2987    PyObject *ob;
2988    int get=0;
2989
2990    if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2991        return NULL;
2992
2993    if (dump(self, ob) < 0)
2994        return NULL;
2995
2996    if (get) return Pickle_getvalue(self, NULL);
2997
2998    /* XXX Why does dump() return self? */
2999    Py_INCREF(self);
3000    return (PyObject*)self;
3001}
3002
3003
3004static struct PyMethodDef Pickler_methods[] =
3005{
3006  {"dump",          (PyCFunction)Pickler_dump,  METH_VARARGS,
3007   PyDoc_STR("dump(object) -- "
3008   "Write an object in pickle format to the object's pickle stream")},
3009  {"clear_memo",  (PyCFunction)Pickle_clear_memo,  METH_NOARGS,
3010   PyDoc_STR("clear_memo() -- Clear the picklers memo")},
3011  {"getvalue",  (PyCFunction)Pickle_getvalue,  METH_VARARGS,
3012   PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
3013  {NULL,                NULL}           /* sentinel */
3014};
3015
3016
3017static Picklerobject *
3018newPicklerobject(PyObject *file, int proto)
3019{
3020    Picklerobject *self;
3021
3022    if (proto < 0)
3023        proto = HIGHEST_PROTOCOL;
3024    if (proto > HIGHEST_PROTOCOL) {
3025        PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
3026                     "the highest available protocol is %d",
3027                     proto, HIGHEST_PROTOCOL);
3028        return NULL;
3029    }
3030
3031    self = PyObject_GC_New(Picklerobject, &Picklertype);
3032    if (self == NULL)
3033        return NULL;
3034    self->proto = proto;
3035    self->bin = proto > 0;
3036    self->fp = NULL;
3037    self->write = NULL;
3038    self->memo = NULL;
3039    self->arg = NULL;
3040    self->pers_func = NULL;
3041    self->inst_pers_func = NULL;
3042    self->write_buf = NULL;
3043    self->fast = 0;
3044    self->fast_container = 0;
3045    self->fast_memo = NULL;
3046    self->buf_size = 0;
3047    self->dispatch_table = NULL;
3048
3049    self->file = NULL;
3050    if (file)
3051        Py_INCREF(file);
3052    else {
3053        file = Pdata_New();
3054        if (file == NULL)
3055            goto err;
3056    }
3057    self->file = file;
3058
3059    if (!( self->memo = PyDict_New()))
3060        goto err;
3061
3062    if (PyFile_Check(file)) {
3063        self->fp = PyFile_AsFile(file);
3064        if (self->fp == NULL) {
3065            PyErr_SetString(PyExc_ValueError,
3066                            "I/O operation on closed file");
3067            goto err;
3068        }
3069        self->write_func = write_file;
3070    }
3071    else if (PycStringIO_OutputCheck(file)) {
3072        self->write_func = write_cStringIO;
3073    }
3074    else if (file == Py_None) {
3075        self->write_func = write_none;
3076    }
3077    else {
3078        self->write_func = write_other;
3079
3080        if (! Pdata_Check(file)) {
3081            self->write = PyObject_GetAttr(file, write_str);
3082            if (!self->write)  {
3083                PyErr_Clear();
3084                PyErr_SetString(PyExc_TypeError,
3085                                "argument must have 'write' "
3086                                "attribute");
3087                goto err;
3088            }
3089        }
3090
3091        self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
3092        if (self->write_buf == NULL) {
3093            PyErr_NoMemory();
3094            goto err;
3095        }
3096    }
3097
3098    if (PyEval_GetRestricted()) {
3099        /* Restricted execution, get private tables */
3100        PyObject *m = PyImport_ImportModule("copy_reg");
3101
3102        if (m == NULL)
3103            goto err;
3104        self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
3105        Py_DECREF(m);
3106        if (self->dispatch_table == NULL)
3107            goto err;
3108    }
3109    else {
3110        self->dispatch_table = dispatch_table;
3111        Py_INCREF(dispatch_table);
3112    }
3113    PyObject_GC_Track(self);
3114
3115    return self;
3116
3117  err:
3118    Py_DECREF(self);
3119    return NULL;
3120}
3121
3122
3123static PyObject *
3124get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
3125{
3126    static char *kwlist[] = {"file", "protocol", NULL};
3127    PyObject *file = NULL;
3128    int proto = 0;
3129
3130    /* XXX
3131     * The documented signature is Pickler(file, protocol=0), but this
3132     * accepts Pickler() and Pickler(integer) too.  The meaning then
3133     * is clear as mud, undocumented, and not supported by pickle.py.
3134     * I'm told Zope uses this, but I haven't traced into this code
3135     * far enough to figure out what it means.
3136     */
3137    if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
3138        PyErr_Clear();
3139        proto = 0;
3140        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3141                    kwlist, &file, &proto))
3142            return NULL;
3143    }
3144    return (PyObject *)newPicklerobject(file, proto);
3145}
3146
3147
3148static void
3149Pickler_dealloc(Picklerobject *self)
3150{
3151    PyObject_GC_UnTrack(self);
3152    Py_XDECREF(self->write);
3153    Py_XDECREF(self->memo);
3154    Py_XDECREF(self->fast_memo);
3155    Py_XDECREF(self->arg);
3156    Py_XDECREF(self->file);
3157    Py_XDECREF(self->pers_func);
3158    Py_XDECREF(self->inst_pers_func);
3159    Py_XDECREF(self->dispatch_table);
3160    PyMem_Free(self->write_buf);
3161    Py_TYPE(self)->tp_free((PyObject *)self);
3162}
3163
3164static int
3165Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3166{
3167    Py_VISIT(self->write);
3168    Py_VISIT(self->memo);
3169    Py_VISIT(self->fast_memo);
3170    Py_VISIT(self->arg);
3171    Py_VISIT(self->file);
3172    Py_VISIT(self->pers_func);
3173    Py_VISIT(self->inst_pers_func);
3174    Py_VISIT(self->dispatch_table);
3175    return 0;
3176}
3177
3178static int
3179Pickler_clear(Picklerobject *self)
3180{
3181    Py_CLEAR(self->write);
3182    Py_CLEAR(self->memo);
3183    Py_CLEAR(self->fast_memo);
3184    Py_CLEAR(self->arg);
3185    Py_CLEAR(self->file);
3186    Py_CLEAR(self->pers_func);
3187    Py_CLEAR(self->inst_pers_func);
3188    Py_CLEAR(self->dispatch_table);
3189    return 0;
3190}
3191
3192static PyObject *
3193Pickler_get_pers_func(Picklerobject *p)
3194{
3195    if (p->pers_func == NULL)
3196        PyErr_SetString(PyExc_AttributeError, "persistent_id");
3197    else
3198        Py_INCREF(p->pers_func);
3199    return p->pers_func;
3200}
3201
3202static int
3203Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3204{
3205    if (v == NULL) {
3206        PyErr_SetString(PyExc_TypeError,
3207                        "attribute deletion is not supported");
3208        return -1;
3209    }
3210    Py_XDECREF(p->pers_func);
3211    Py_INCREF(v);
3212    p->pers_func = v;
3213    return 0;
3214}
3215
3216static int
3217Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3218{
3219    if (v == NULL) {
3220        PyErr_SetString(PyExc_TypeError,
3221                        "attribute deletion is not supported");
3222        return -1;
3223    }
3224    Py_XDECREF(p->inst_pers_func);
3225    Py_INCREF(v);
3226    p->inst_pers_func = v;
3227    return 0;
3228}
3229
3230static PyObject *
3231Pickler_get_memo(Picklerobject *p)
3232{
3233    if (p->memo == NULL)
3234        PyErr_SetString(PyExc_AttributeError, "memo");
3235    else
3236        Py_INCREF(p->memo);
3237    return p->memo;
3238}
3239
3240static int
3241Pickler_set_memo(Picklerobject *p, PyObject *v)
3242{
3243    if (v == NULL) {
3244        PyErr_SetString(PyExc_TypeError,
3245                        "attribute deletion is not supported");
3246        return -1;
3247    }
3248    if (!PyDict_Check(v)) {
3249        PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3250        return -1;
3251    }
3252    Py_XDECREF(p->memo);
3253    Py_INCREF(v);
3254    p->memo = v;
3255    return 0;
3256}
3257
3258static PyObject *
3259Pickler_get_error(Picklerobject *p)
3260{
3261    /* why is this an attribute on the Pickler? */
3262    Py_INCREF(PicklingError);
3263    return PicklingError;
3264}
3265
3266static PyMemberDef Pickler_members[] = {
3267    {"binary", T_INT, offsetof(Picklerobject, bin)},
3268    {"fast", T_INT, offsetof(Picklerobject, fast)},
3269    {NULL}
3270};
3271
3272static PyGetSetDef Pickler_getsets[] = {
3273    {"persistent_id", (getter)Pickler_get_pers_func,
3274                     (setter)Pickler_set_pers_func},
3275    {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3276    {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3277    {"PicklingError", (getter)Pickler_get_error, NULL},
3278    {NULL}
3279};
3280
3281PyDoc_STRVAR(Picklertype__doc__,
3282"Objects that know how to pickle objects\n");
3283
3284static PyTypeObject Picklertype = {
3285    PyVarObject_HEAD_INIT(NULL, 0)
3286    "cPickle.Pickler",            /*tp_name*/
3287    sizeof(Picklerobject),              /*tp_basicsize*/
3288    0,
3289    (destructor)Pickler_dealloc,        /* tp_dealloc */
3290    0,                                  /* tp_print */
3291    0,                                  /* tp_getattr */
3292    0,                                  /* tp_setattr */
3293    0,                                  /* tp_compare */
3294    0,                                  /* tp_repr */
3295    0,                                  /* tp_as_number */
3296    0,                                  /* tp_as_sequence */
3297    0,                                  /* tp_as_mapping */
3298    0,                                  /* tp_hash */
3299    0,                                  /* tp_call */
3300    0,                                  /* tp_str */
3301    PyObject_GenericGetAttr,            /* tp_getattro */
3302    PyObject_GenericSetAttr,            /* tp_setattro */
3303    0,                                  /* tp_as_buffer */
3304    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3305    Picklertype__doc__,                 /* tp_doc */
3306    (traverseproc)Pickler_traverse,     /* tp_traverse */
3307    (inquiry)Pickler_clear,             /* tp_clear */
3308    0,                                  /* tp_richcompare */
3309    0,                                  /* tp_weaklistoffset */
3310    0,                                  /* tp_iter */
3311    0,                                  /* tp_iternext */
3312    Pickler_methods,                    /* tp_methods */
3313    Pickler_members,                    /* tp_members */
3314    Pickler_getsets,                    /* tp_getset */
3315};
3316
3317static PyObject *
3318find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3319{
3320    PyObject *global = 0, *module;
3321
3322    if (fc) {
3323        if (fc==Py_None) {
3324            PyErr_SetString(UnpicklingError, "Global and instance "
3325                            "pickles are not supported.");
3326            return NULL;
3327        }
3328        return PyObject_CallFunctionObjArgs(fc, py_module_name,
3329                                            py_global_name, NULL);
3330    }
3331
3332    module = PySys_GetObject("modules");
3333    if (module == NULL)
3334        return NULL;
3335
3336    module = PyDict_GetItem(module, py_module_name);
3337    if (module == NULL) {
3338        module = PyImport_Import(py_module_name);
3339        if (!module)
3340            return NULL;
3341        global = PyObject_GetAttr(module, py_global_name);
3342        Py_DECREF(module);
3343    }
3344    else
3345        global = PyObject_GetAttr(module, py_global_name);
3346    return global;
3347}
3348
3349static Py_ssize_t
3350marker(Unpicklerobject *self)
3351{
3352    if (self->num_marks < 1) {
3353        PyErr_SetString(UnpicklingError, "could not find MARK");
3354        return -1;
3355    }
3356
3357    return self->marks[--self->num_marks];
3358}
3359
3360
3361static int
3362load_none(Unpicklerobject *self)
3363{
3364    PDATA_APPEND(self->stack, Py_None, -1);
3365    return 0;
3366}
3367
3368static int
3369bad_readline(void)
3370{
3371    PyErr_SetString(UnpicklingError, "pickle data was truncated");
3372    return -1;
3373}
3374
3375static int
3376load_int(Unpicklerobject *self)
3377{
3378    PyObject *py_int = 0;
3379    char *endptr, *s;
3380    Py_ssize_t len;
3381    int res = -1;
3382    long l;
3383
3384    if ((len = self->readline_func(self, &s)) < 0) return -1;
3385    if (len < 2) return bad_readline();
3386    if (!( s=pystrndup(s,len)))  return -1;
3387
3388    errno = 0;
3389    l = strtol(s, &endptr, 0);
3390
3391    if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3392        /* Hm, maybe we've got something long.  Let's try reading
3393           it as a Python long object. */
3394        errno = 0;
3395        py_int = PyLong_FromString(s, NULL, 0);
3396        if (py_int == NULL) {
3397            PyErr_SetString(PyExc_ValueError,
3398                            "could not convert string to int");
3399            goto finally;
3400        }
3401    }
3402    else {
3403        if (len == 3 && (l == 0 || l == 1)) {
3404            if (!( py_int = PyBool_FromLong(l)))  goto finally;
3405        }
3406        else {
3407            if (!( py_int = PyInt_FromLong(l)))  goto finally;
3408        }
3409    }
3410
3411    free(s);
3412    PDATA_PUSH(self->stack, py_int, -1);
3413    return 0;
3414
3415  finally:
3416    free(s);
3417
3418    return res;
3419}
3420
3421static int
3422load_bool(Unpicklerobject *self, PyObject *boolean)
3423{
3424    assert(boolean == Py_True || boolean == Py_False);
3425    PDATA_APPEND(self->stack, boolean, -1);
3426    return 0;
3427}
3428
3429/* s contains x bytes of a little-endian integer.  Return its value as a
3430 * C int.  Obscure:  when x is 1 or 2, this is an unsigned little-endian
3431 * int, but when x is 4 it's a signed one.  This is an historical source
3432 * of x-platform bugs.
3433 */
3434static long
3435calc_binint(char *s, int x)
3436{
3437    unsigned char c;
3438    int i;
3439    long l;
3440
3441    for (i = 0, l = 0L; i < x; i++) {
3442        c = (unsigned char)s[i];
3443        l |= (long)c << (i * 8);
3444    }
3445#if SIZEOF_LONG > 4
3446    /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3447     * is signed, so on a box with longs bigger than 4 bytes we need
3448     * to extend a BININT's sign bit to the full width.
3449     */
3450    if (x == 4 && l & (1L << 31))
3451        l |= (~0L) << 32;
3452#endif
3453    return l;
3454}
3455
3456
3457static int
3458load_binintx(Unpicklerobject *self, char *s, int  x)
3459{
3460    PyObject *py_int = 0;
3461    long l;
3462
3463    l = calc_binint(s, x);
3464
3465    if (!( py_int = PyInt_FromLong(l)))
3466        return -1;
3467
3468    PDATA_PUSH(self->stack, py_int, -1);
3469    return 0;
3470}
3471
3472
3473static int
3474load_binint(Unpicklerobject *self)
3475{
3476    char *s;
3477
3478    if (self->read_func(self, &s, 4) < 0)
3479        return -1;
3480
3481    return load_binintx(self, s, 4);
3482}
3483
3484
3485static int
3486load_binint1(Unpicklerobject *self)
3487{
3488    char *s;
3489
3490    if (self->read_func(self, &s, 1) < 0)
3491        return -1;
3492
3493    return load_binintx(self, s, 1);
3494}
3495
3496
3497static int
3498load_binint2(Unpicklerobject *self)
3499{
3500    char *s;
3501
3502    if (self->read_func(self, &s, 2) < 0)
3503        return -1;
3504
3505    return load_binintx(self, s, 2);
3506}
3507
3508static int
3509load_long(Unpicklerobject *self)
3510{
3511    PyObject *l = 0;
3512    char *end, *s;
3513    Py_ssize_t len;
3514    int res = -1;
3515
3516    if ((len = self->readline_func(self, &s)) < 0) return -1;
3517    if (len < 2) return bad_readline();
3518    if (!( s=pystrndup(s,len)))  return -1;
3519
3520    if (!( l = PyLong_FromString(s, &end, 0)))
3521        goto finally;
3522
3523    free(s);
3524    PDATA_PUSH(self->stack, l, -1);
3525    return 0;
3526
3527  finally:
3528    free(s);
3529
3530    return res;
3531}
3532
3533/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3534 * data following.
3535 */
3536static int
3537load_counted_long(Unpicklerobject *self, int size)
3538{
3539    Py_ssize_t i;
3540    char *nbytes;
3541    unsigned char *pdata;
3542    PyObject *along;
3543
3544    assert(size == 1 || size == 4);
3545    i = self->read_func(self, &nbytes, size);
3546    if (i < 0) return -1;
3547
3548    size = calc_binint(nbytes, size);
3549    if (size < 0) {
3550        /* Corrupt or hostile pickle -- we never write one like
3551         * this.
3552         */
3553        PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3554                        "byte count");
3555        return -1;
3556    }
3557
3558    if (size == 0)
3559        along = PyLong_FromLong(0L);
3560    else {
3561        /* Read the raw little-endian bytes & convert. */
3562        i = self->read_func(self, (char **)&pdata, size);
3563        if (i < 0) return -1;
3564        along = _PyLong_FromByteArray(pdata, (size_t)size,
3565                        1 /* little endian */, 1 /* signed */);
3566    }
3567    if (along == NULL)
3568        return -1;
3569    PDATA_PUSH(self->stack, along, -1);
3570    return 0;
3571}
3572
3573static int
3574load_float(Unpicklerobject *self)
3575{
3576    PyObject *py_float = 0;
3577    char *endptr, *s;
3578    Py_ssize_t len;
3579    int res = -1;
3580    double d;
3581
3582    if ((len = self->readline_func(self, &s)) < 0) return -1;
3583    if (len < 2) return bad_readline();
3584    if (!( s=pystrndup(s,len)))  return -1;
3585
3586    d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
3587
3588    if (d == -1.0 && PyErr_Occurred()) {
3589        goto finally;
3590    } else if ((endptr[0] != '\n') || (endptr[1] != '\0')) {
3591        PyErr_SetString(PyExc_ValueError,
3592                        "could not convert string to float");
3593        goto finally;
3594    }
3595
3596    if (!( py_float = PyFloat_FromDouble(d)))
3597        goto finally;
3598
3599    free(s);
3600    PDATA_PUSH(self->stack, py_float, -1);
3601    return 0;
3602
3603  finally:
3604    free(s);
3605
3606    return res;
3607}
3608
3609static int
3610load_binfloat(Unpicklerobject *self)
3611{
3612    PyObject *py_float;
3613    double x;
3614    char *p;
3615
3616    if (self->read_func(self, &p, 8) < 0)
3617        return -1;
3618
3619    x = _PyFloat_Unpack8((unsigned char *)p, 0);
3620    if (x == -1.0 && PyErr_Occurred())
3621        return -1;
3622
3623    py_float = PyFloat_FromDouble(x);
3624    if (py_float == NULL)
3625        return -1;
3626
3627    PDATA_PUSH(self->stack, py_float, -1);
3628    return 0;
3629}
3630
3631static int
3632load_string(Unpicklerobject *self)
3633{
3634    PyObject *str = 0;
3635    Py_ssize_t len;
3636    int res = -1;
3637    char *s, *p;
3638
3639    if ((len = self->readline_func(self, &s)) < 0) return -1;
3640    if (len < 2) return bad_readline();
3641    if (!( s=pystrndup(s,len)))  return -1;
3642
3643
3644    /* Strip outermost quotes */
3645    while (len > 0 && s[len-1] <= ' ')
3646        len--;
3647    if (len > 1 && s[0]=='"' && s[len-1]=='"') {
3648        s[len-1] = '\0';
3649        p = s + 1 ;
3650        len -= 2;
3651    }
3652    else if (len > 1 && s[0]=='\'' && s[len-1]=='\'') {
3653        s[len-1] = '\0';
3654        p = s + 1 ;
3655        len -= 2;
3656    }
3657    else
3658        goto insecure;
3659    /********************************************/
3660
3661    str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3662    free(s);
3663    if (str) {
3664        PDATA_PUSH(self->stack, str, -1);
3665        res = 0;
3666    }
3667    return res;
3668
3669  insecure:
3670    free(s);
3671    PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3672    return -1;
3673}
3674
3675
3676static int
3677load_binstring(Unpicklerobject *self)
3678{
3679    PyObject *py_string = 0;
3680    Py_ssize_t l;
3681    char *s;
3682
3683    if (self->read_func(self, &s, 4) < 0) return -1;
3684
3685    l = calc_binint(s, 4);
3686    if (l < 0) {
3687        /* Corrupt or hostile pickle -- we never write one like
3688         * this.
3689         */
3690        PyErr_SetString(UnpicklingError,
3691                        "BINSTRING pickle has negative byte count");
3692        return -1;
3693    }
3694
3695    if (self->read_func(self, &s, l) < 0)
3696        return -1;
3697
3698    if (!( py_string = PyString_FromStringAndSize(s, l)))
3699        return -1;
3700
3701    PDATA_PUSH(self->stack, py_string, -1);
3702    return 0;
3703}
3704
3705
3706static int
3707load_short_binstring(Unpicklerobject *self)
3708{
3709    PyObject *py_string = 0;
3710    unsigned char l;
3711    char *s;
3712
3713    if (self->read_func(self, &s, 1) < 0)
3714        return -1;
3715
3716    l = (unsigned char)s[0];
3717
3718    if (self->read_func(self, &s, l) < 0) return -1;
3719
3720    if (!( py_string = PyString_FromStringAndSize(s, l)))  return -1;
3721
3722    PDATA_PUSH(self->stack, py_string, -1);
3723    return 0;
3724}
3725
3726
3727#ifdef Py_USING_UNICODE
3728static int
3729load_unicode(Unpicklerobject *self)
3730{
3731    PyObject *str = 0;
3732    Py_ssize_t len;
3733    char *s;
3734
3735    if ((len = self->readline_func(self, &s)) < 0) return -1;
3736    if (len < 1) return bad_readline();
3737
3738    if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3739        return -1;
3740
3741    PDATA_PUSH(self->stack, str, -1);
3742    return 0;
3743}
3744#endif
3745
3746
3747#ifdef Py_USING_UNICODE
3748static int
3749load_binunicode(Unpicklerobject *self)
3750{
3751    PyObject *unicode;
3752    Py_ssize_t l;
3753    char *s;
3754
3755    if (self->read_func(self, &s, 4) < 0) return -1;
3756
3757    l = calc_binint(s, 4);
3758    if (l < 0) {
3759        /* Corrupt or hostile pickle -- we never write one like
3760         * this.
3761         */
3762        PyErr_SetString(UnpicklingError,
3763                        "BINUNICODE pickle has negative byte count");
3764        return -1;
3765    }
3766
3767    if (self->read_func(self, &s, l) < 0)
3768        return -1;
3769
3770    if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3771        return -1;
3772
3773    PDATA_PUSH(self->stack, unicode, -1);
3774    return 0;
3775}
3776#endif
3777
3778
3779static int
3780load_tuple(Unpicklerobject *self)
3781{
3782    PyObject *tup;
3783    Py_ssize_t i;
3784
3785    if ((i = marker(self)) < 0) return -1;
3786    if (!( tup=Pdata_popTuple(self->stack, i)))  return -1;
3787    PDATA_PUSH(self->stack, tup, -1);
3788    return 0;
3789}
3790
3791static int
3792load_counted_tuple(Unpicklerobject *self, int len)
3793{
3794    PyObject *tup = PyTuple_New(len);
3795
3796    if (tup == NULL)
3797        return -1;
3798
3799    while (--len >= 0) {
3800        PyObject *element;
3801
3802        PDATA_POP(self->stack, element);
3803        if (element == NULL)
3804            return -1;
3805        PyTuple_SET_ITEM(tup, len, element);
3806    }
3807    PDATA_PUSH(self->stack, tup, -1);
3808    return 0;
3809}
3810
3811static int
3812load_empty_list(Unpicklerobject *self)
3813{
3814    PyObject *list;
3815
3816    if (!( list=PyList_New(0)))  return -1;
3817    PDATA_PUSH(self->stack, list, -1);
3818    return 0;
3819}
3820
3821static int
3822load_empty_dict(Unpicklerobject *self)
3823{
3824    PyObject *dict;
3825
3826    if (!( dict=PyDict_New()))  return -1;
3827    PDATA_PUSH(self->stack, dict, -1);
3828    return 0;
3829}
3830
3831
3832static int
3833load_list(Unpicklerobject *self)
3834{
3835    PyObject *list = 0;
3836    Py_ssize_t i;
3837
3838    if ((i = marker(self)) < 0) return -1;
3839    if (!( list=Pdata_popList(self->stack, i)))  return -1;
3840    PDATA_PUSH(self->stack, list, -1);
3841    return 0;
3842}
3843
3844static int
3845load_dict(Unpicklerobject *self)
3846{
3847    PyObject *dict, *key, *value;
3848    Py_ssize_t i, j, k;
3849
3850    if ((i = marker(self)) < 0) return -1;
3851    j=self->stack->length;
3852
3853    if (!( dict = PyDict_New()))  return -1;
3854
3855    for (k = i+1; k < j; k += 2) {
3856        key  =self->stack->data[k-1];
3857        value=self->stack->data[k  ];
3858        if (PyDict_SetItem(dict, key, value) < 0) {
3859            Py_DECREF(dict);
3860            return -1;
3861        }
3862    }
3863    Pdata_clear(self->stack, i);
3864    PDATA_PUSH(self->stack, dict, -1);
3865    return 0;
3866}
3867
3868static PyObject *
3869Instance_New(PyObject *cls, PyObject *args)
3870{
3871    PyObject *r = 0;
3872
3873    if (PyClass_Check(cls)) {
3874        int l;
3875
3876        if ((l=PyObject_Size(args)) < 0) goto err;
3877        if (!( l ))  {
3878            PyObject *__getinitargs__;
3879
3880            __getinitargs__ = PyObject_GetAttr(cls,
3881                                       __getinitargs___str);
3882            if (!__getinitargs__)  {
3883                /* We have a class with no __getinitargs__,
3884                   so bypass usual construction  */
3885                PyObject *inst;
3886
3887                PyErr_Clear();
3888                if (!( inst=PyInstance_NewRaw(cls, NULL)))
3889                    goto err;
3890                return inst;
3891            }
3892            Py_DECREF(__getinitargs__);
3893        }
3894
3895        if ((r=PyInstance_New(cls, args, NULL))) return r;
3896        else goto err;
3897    }
3898
3899    if ((r=PyObject_CallObject(cls, args))) return r;
3900
3901  err:
3902    {
3903        PyObject *tp, *v, *tb, *tmp_value;
3904
3905        PyErr_Fetch(&tp, &v, &tb);
3906        tmp_value = v;
3907        /* NULL occurs when there was a KeyboardInterrupt */
3908        if (tmp_value == NULL)
3909            tmp_value = Py_None;
3910        if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3911            Py_XDECREF(v);
3912            v=r;
3913        }
3914        PyErr_Restore(tp,v,tb);
3915    }
3916    return NULL;
3917}
3918
3919
3920static int
3921load_obj(Unpicklerobject *self)
3922{
3923    PyObject *class, *tup, *obj=0;
3924    Py_ssize_t i;
3925
3926    if ((i = marker(self)) < 0) return -1;
3927    if (!( tup=Pdata_popTuple(self->stack, i+1)))  return -1;
3928    PDATA_POP(self->stack, class);
3929    if (class) {
3930        obj = Instance_New(class, tup);
3931        Py_DECREF(class);
3932    }
3933    Py_DECREF(tup);
3934
3935    if (! obj) return -1;
3936    PDATA_PUSH(self->stack, obj, -1);
3937    return 0;
3938}
3939
3940
3941static int
3942load_inst(Unpicklerobject *self)
3943{
3944    PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3945    Py_ssize_t i, len;
3946    char *s;
3947
3948    if ((i = marker(self)) < 0) return -1;
3949
3950    if ((len = self->readline_func(self, &s)) < 0) return -1;
3951    if (len < 2) return bad_readline();
3952    module_name = PyString_FromStringAndSize(s, len - 1);
3953    if (!module_name)  return -1;
3954
3955    if ((len = self->readline_func(self, &s)) >= 0) {
3956        if (len < 2) return bad_readline();
3957        if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3958            class = find_class(module_name, class_name,
3959                               self->find_class);
3960            Py_DECREF(class_name);
3961        }
3962    }
3963    Py_DECREF(module_name);
3964
3965    if (! class) return -1;
3966
3967    if ((tup=Pdata_popTuple(self->stack, i))) {
3968        obj = Instance_New(class, tup);
3969        Py_DECREF(tup);
3970    }
3971    Py_DECREF(class);
3972
3973    if (! obj) return -1;
3974
3975    PDATA_PUSH(self->stack, obj, -1);
3976    return 0;
3977}
3978
3979static int
3980load_newobj(Unpicklerobject *self)
3981{
3982    PyObject *args = NULL;
3983    PyObject *clsraw = NULL;
3984    PyTypeObject *cls;          /* clsraw cast to its true type */
3985    PyObject *obj;
3986
3987    /* Stack is ... cls argtuple, and we want to call
3988     * cls.__new__(cls, *argtuple).
3989     */
3990    PDATA_POP(self->stack, args);
3991    if (args == NULL) goto Fail;
3992    if (! PyTuple_Check(args)) {
3993        PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3994                                         "tuple.");
3995        goto Fail;
3996    }
3997
3998    PDATA_POP(self->stack, clsraw);
3999    cls = (PyTypeObject *)clsraw;
4000    if (cls == NULL) goto Fail;
4001    if (! PyType_Check(cls)) {
4002        PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4003                                         "isn't a type object");
4004        goto Fail;
4005    }
4006    if (cls->tp_new == NULL) {
4007        PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4008                                         "has NULL tp_new");
4009        goto Fail;
4010    }
4011
4012    /* Call __new__. */
4013    obj = cls->tp_new(cls, args, NULL);
4014    if (obj == NULL) goto Fail;
4015
4016    Py_DECREF(args);
4017    Py_DECREF(clsraw);
4018    PDATA_PUSH(self->stack, obj, -1);
4019    return 0;
4020
4021 Fail:
4022    Py_XDECREF(args);
4023    Py_XDECREF(clsraw);
4024    return -1;
4025}
4026
4027static int
4028load_global(Unpicklerobject *self)
4029{
4030    PyObject *class = 0, *module_name = 0, *class_name = 0;
4031    Py_ssize_t len;
4032    char *s;
4033
4034    if ((len = self->readline_func(self, &s)) < 0) return -1;
4035    if (len < 2) return bad_readline();
4036    module_name = PyString_FromStringAndSize(s, len - 1);
4037    if (!module_name)  return -1;
4038
4039    if ((len = self->readline_func(self, &s)) >= 0) {
4040        if (len < 2) {
4041            Py_DECREF(module_name);
4042            return bad_readline();
4043        }
4044        if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
4045            class = find_class(module_name, class_name,
4046                               self->find_class);
4047            Py_DECREF(class_name);
4048        }
4049    }
4050    Py_DECREF(module_name);
4051
4052    if (! class) return -1;
4053    PDATA_PUSH(self->stack, class, -1);
4054    return 0;
4055}
4056
4057
4058static int
4059load_persid(Unpicklerobject *self)
4060{
4061    PyObject *pid = 0;
4062    Py_ssize_t len;
4063    char *s;
4064
4065    if (self->pers_func) {
4066        if ((len = self->readline_func(self, &s)) < 0) return -1;
4067        if (len < 2) return bad_readline();
4068
4069        pid = PyString_FromStringAndSize(s, len - 1);
4070        if (!pid)  return -1;
4071
4072        if (PyList_Check(self->pers_func)) {
4073            if (PyList_Append(self->pers_func, pid) < 0) {
4074                Py_DECREF(pid);
4075                return -1;
4076            }
4077        }
4078        else {
4079            ARG_TUP(self, pid);
4080            if (self->arg) {
4081                pid = PyObject_Call(self->pers_func, self->arg,
4082                                    NULL);
4083                FREE_ARG_TUP(self);
4084            }
4085        }
4086
4087        if (! pid) return -1;
4088
4089        PDATA_PUSH(self->stack, pid, -1);
4090        return 0;
4091    }
4092    else {
4093        PyErr_SetString(UnpicklingError,
4094                        "A load persistent id instruction was encountered,\n"
4095                        "but no persistent_load function was specified.");
4096        return -1;
4097    }
4098}
4099
4100static int
4101load_binpersid(Unpicklerobject *self)
4102{
4103    PyObject *pid = 0;
4104
4105    if (self->pers_func) {
4106        PDATA_POP(self->stack, pid);
4107        if (! pid) return -1;
4108
4109        if (PyList_Check(self->pers_func)) {
4110            if (PyList_Append(self->pers_func, pid) < 0) {
4111                Py_DECREF(pid);
4112                return -1;
4113            }
4114        }
4115        else {
4116            ARG_TUP(self, pid);
4117            if (self->arg) {
4118                pid = PyObject_Call(self->pers_func, self->arg,
4119                                    NULL);
4120                FREE_ARG_TUP(self);
4121            }
4122            if (! pid) return -1;
4123        }
4124
4125        PDATA_PUSH(self->stack, pid, -1);
4126        return 0;
4127    }
4128    else {
4129        PyErr_SetString(UnpicklingError,
4130                        "A load persistent id instruction was encountered,\n"
4131                        "but no persistent_load function was specified.");
4132        return -1;
4133    }
4134}
4135
4136
4137static int
4138load_pop(Unpicklerobject *self)
4139{
4140    Py_ssize_t len = self->stack->length;
4141
4142    /* Note that we split the (pickle.py) stack into two stacks,
4143       an object stack and a mark stack. We have to be clever and
4144       pop the right one. We do this by looking at the top of the
4145       mark stack first, and only signalling a stack underflow if
4146       the object stack is empty and the mark stack doesn't match
4147       our expectations.
4148    */
4149    if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
4150        self->num_marks--;
4151    } else if (len > 0) {
4152        len--;
4153        Py_DECREF(self->stack->data[len]);
4154        self->stack->length = len;
4155    } else {
4156        return stackUnderflow();
4157    }
4158    return 0;
4159}
4160
4161
4162static int
4163load_pop_mark(Unpicklerobject *self)
4164{
4165    Py_ssize_t i;
4166
4167    if ((i = marker(self)) < 0)
4168        return -1;
4169
4170    Pdata_clear(self->stack, i);
4171
4172    return 0;
4173}
4174
4175
4176static int
4177load_dup(Unpicklerobject *self)
4178{
4179    PyObject *last;
4180    Py_ssize_t len;
4181
4182    if ((len = self->stack->length) <= 0) return stackUnderflow();
4183    last=self->stack->data[len-1];
4184    Py_INCREF(last);
4185    PDATA_PUSH(self->stack, last, -1);
4186    return 0;
4187}
4188
4189
4190static int
4191load_get(Unpicklerobject *self)
4192{
4193    PyObject *py_str = 0, *value = 0;
4194    Py_ssize_t len;
4195    char *s;
4196    int rc;
4197
4198    if ((len = self->readline_func(self, &s)) < 0) return -1;
4199    if (len < 2) return bad_readline();
4200
4201    if (!( py_str = PyString_FromStringAndSize(s, len - 1)))  return -1;
4202
4203    value = PyDict_GetItem(self->memo, py_str);
4204    if (! value) {
4205        PyErr_SetObject(BadPickleGet, py_str);
4206        rc = -1;
4207    }
4208    else {
4209        PDATA_APPEND(self->stack, value, -1);
4210        rc = 0;
4211    }
4212
4213    Py_DECREF(py_str);
4214    return rc;
4215}
4216
4217
4218static int
4219load_binget(Unpicklerobject *self)
4220{
4221    PyObject *py_key = 0, *value = 0;
4222    unsigned char key;
4223    char *s;
4224    int rc;
4225
4226    if (self->read_func(self, &s, 1) < 0) return -1;
4227
4228    key = (unsigned char)s[0];
4229    if (!( py_key = PyInt_FromLong((long)key)))  return -1;
4230
4231    value = PyDict_GetItem(self->memo, py_key);
4232    if (! value) {
4233        PyErr_SetObject(BadPickleGet, py_key);
4234        rc = -1;
4235    }
4236    else {
4237        PDATA_APPEND(self->stack, value, -1);
4238        rc = 0;
4239    }
4240
4241    Py_DECREF(py_key);
4242    return rc;
4243}
4244
4245
4246static int
4247load_long_binget(Unpicklerobject *self)
4248{
4249    PyObject *py_key = 0, *value = 0;
4250    unsigned char c;
4251    char *s;
4252    Py_ssize_t key;
4253    int rc;
4254
4255    if (self->read_func(self, &s, 4) < 0) return -1;
4256
4257    c = (unsigned char)s[0];
4258    key = (long)c;
4259    c = (unsigned char)s[1];
4260    key |= (long)c << 8;
4261    c = (unsigned char)s[2];
4262    key |= (long)c << 16;
4263    c = (unsigned char)s[3];
4264    key |= (long)c << 24;
4265
4266    if (!( py_key = PyInt_FromLong((long)key)))  return -1;
4267
4268    value = PyDict_GetItem(self->memo, py_key);
4269    if (! value) {
4270        PyErr_SetObject(BadPickleGet, py_key);
4271        rc = -1;
4272    }
4273    else {
4274        PDATA_APPEND(self->stack, value, -1);
4275        rc = 0;
4276    }
4277
4278    Py_DECREF(py_key);
4279    return rc;
4280}
4281
4282/* Push an object from the extension registry (EXT[124]).  nbytes is
4283 * the number of bytes following the opcode, holding the index (code) value.
4284 */
4285static int
4286load_extension(Unpicklerobject *self, int nbytes)
4287{
4288    char *codebytes;            /* the nbytes bytes after the opcode */
4289    long code;                  /* calc_binint returns long */
4290    PyObject *py_code;          /* code as a Python int */
4291    PyObject *obj;              /* the object to push */
4292    PyObject *pair;             /* (module_name, class_name) */
4293    PyObject *module_name, *class_name;
4294
4295    assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4296    if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4297    code = calc_binint(codebytes,  nbytes);
4298    if (code <= 0) {                    /* note that 0 is forbidden */
4299        /* Corrupt or hostile pickle. */
4300        PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4301        return -1;
4302    }
4303
4304    /* Look for the code in the cache. */
4305    py_code = PyInt_FromLong(code);
4306    if (py_code == NULL) return -1;
4307    obj = PyDict_GetItem(extension_cache, py_code);
4308    if (obj != NULL) {
4309        /* Bingo. */
4310        Py_DECREF(py_code);
4311        PDATA_APPEND(self->stack, obj, -1);
4312        return 0;
4313    }
4314
4315    /* Look up the (module_name, class_name) pair. */
4316    pair = PyDict_GetItem(inverted_registry, py_code);
4317    if (pair == NULL) {
4318        Py_DECREF(py_code);
4319        PyErr_Format(PyExc_ValueError, "unregistered extension "
4320                     "code %ld", code);
4321        return -1;
4322    }
4323    /* Since the extension registry is manipulable via Python code,
4324     * confirm that pair is really a 2-tuple of strings.
4325     */
4326    if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4327        !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4328        !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4329        Py_DECREF(py_code);
4330        PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4331                     "isn't a 2-tuple of strings", code);
4332        return -1;
4333    }
4334    /* Load the object. */
4335    obj = find_class(module_name, class_name, self->find_class);
4336    if (obj == NULL) {
4337        Py_DECREF(py_code);
4338        return -1;
4339    }
4340    /* Cache code -> obj. */
4341    code = PyDict_SetItem(extension_cache, py_code, obj);
4342    Py_DECREF(py_code);
4343    if (code < 0) {
4344        Py_DECREF(obj);
4345        return -1;
4346    }
4347    PDATA_PUSH(self->stack, obj, -1);
4348    return 0;
4349}
4350
4351static int
4352load_put(Unpicklerobject *self)
4353{
4354    PyObject *py_str = 0, *value = 0;
4355    Py_ssize_t len, l;
4356    char *s;
4357
4358    if ((l = self->readline_func(self, &s)) < 0) return -1;
4359    if (l < 2) return bad_readline();
4360    if (!( len=self->stack->length ))  return stackUnderflow();
4361    if (!( py_str = PyString_FromStringAndSize(s, l - 1)))  return -1;
4362    value=self->stack->data[len-1];
4363    l=PyDict_SetItem(self->memo, py_str, value);
4364    Py_DECREF(py_str);
4365    return l;
4366}
4367
4368
4369static int
4370load_binput(Unpicklerobject *self)
4371{
4372    PyObject *py_key = 0, *value = 0;
4373    unsigned char key;
4374    char *s;
4375    Py_ssize_t len;
4376
4377    if (self->read_func(self, &s, 1) < 0) return -1;
4378    if (!( (len=self->stack->length) > 0 ))  return stackUnderflow();
4379
4380    key = (unsigned char)s[0];
4381
4382    if (!( py_key = PyInt_FromLong((long)key)))  return -1;
4383    value=self->stack->data[len-1];
4384    len=PyDict_SetItem(self->memo, py_key, value);
4385    Py_DECREF(py_key);
4386    return len;
4387}
4388
4389
4390static int
4391load_long_binput(Unpicklerobject *self)
4392{
4393    PyObject *py_key = 0, *value = 0;
4394    Py_ssize_t key;
4395    unsigned char c;
4396    char *s;
4397    Py_ssize_t len;
4398
4399    if (self->read_func(self, &s, 4) < 0) return -1;
4400    if (!( len=self->stack->length ))  return stackUnderflow();
4401
4402    c = (unsigned char)s[0];
4403    key = (long)c;
4404    c = (unsigned char)s[1];
4405    key |= (long)c << 8;
4406    c = (unsigned char)s[2];
4407    key |= (long)c << 16;
4408    c = (unsigned char)s[3];
4409    key |= (long)c << 24;
4410
4411    if (!( py_key = PyInt_FromLong(key)))  return -1;
4412    value=self->stack->data[len-1];
4413    len=PyDict_SetItem(self->memo, py_key, value);
4414    Py_DECREF(py_key);
4415    return len;
4416}
4417
4418
4419static int
4420do_append(Unpicklerobject *self, Py_ssize_t  x)
4421{
4422    PyObject *value = 0, *list = 0, *append_method = 0;
4423    Py_ssize_t len, i;
4424
4425    len=self->stack->length;
4426    if (!( len >= x && x > 0 ))  return stackUnderflow();
4427    /* nothing to do */
4428    if (len==x) return 0;
4429
4430    list=self->stack->data[x-1];
4431
4432    if (PyList_Check(list)) {
4433        PyObject *slice;
4434        int list_len;
4435
4436        slice=Pdata_popList(self->stack, x);
4437        if (! slice) return -1;
4438        list_len = PyList_GET_SIZE(list);
4439        i=PyList_SetSlice(list, list_len, list_len, slice);
4440        Py_DECREF(slice);
4441        return i;
4442    }
4443    else {
4444
4445        if (!( append_method = PyObject_GetAttr(list, append_str)))
4446            return -1;
4447
4448        for (i = x; i < len; i++) {
4449            PyObject *junk;
4450
4451            value=self->stack->data[i];
4452            junk=0;
4453            ARG_TUP(self, value);
4454            if (self->arg) {
4455                junk = PyObject_Call(append_method, self->arg,
4456                                     NULL);
4457                FREE_ARG_TUP(self);
4458            }
4459            if (! junk) {
4460                Pdata_clear(self->stack, i+1);
4461                self->stack->length=x;
4462                Py_DECREF(append_method);
4463                return -1;
4464            }
4465            Py_DECREF(junk);
4466        }
4467        self->stack->length=x;
4468        Py_DECREF(append_method);
4469    }
4470
4471    return 0;
4472}
4473
4474
4475static int
4476load_append(Unpicklerobject *self)
4477{
4478    return do_append(self, self->stack->length - 1);
4479}
4480
4481
4482static int
4483load_appends(Unpicklerobject *self)
4484{
4485    return do_append(self, marker(self));
4486}
4487
4488
4489static Py_ssize_t
4490do_setitems(Unpicklerobject *self, Py_ssize_t x)
4491{
4492    PyObject *value = 0, *key = 0, *dict = 0;
4493    Py_ssize_t len, i, r=0;
4494
4495    if (!( (len=self->stack->length) >= x
4496           && x > 0 ))  return stackUnderflow();
4497
4498    dict=self->stack->data[x-1];
4499
4500    for (i = x+1; i < len; i += 2) {
4501        key  =self->stack->data[i-1];
4502        value=self->stack->data[i  ];
4503        if (PyObject_SetItem(dict, key, value) < 0) {
4504            r=-1;
4505            break;
4506        }
4507    }
4508
4509    Pdata_clear(self->stack, x);
4510
4511    return r;
4512}
4513
4514
4515static int
4516load_setitem(Unpicklerobject *self)
4517{
4518    return do_setitems(self, self->stack->length - 2);
4519}
4520
4521static int
4522load_setitems(Unpicklerobject *self)
4523{
4524    return do_setitems(self, marker(self));
4525}
4526
4527
4528static int
4529load_build(Unpicklerobject *self)
4530{
4531    PyObject *state, *inst, *slotstate;
4532    PyObject *__setstate__;
4533    PyObject *d_key, *d_value;
4534    int res = -1;
4535    Py_ssize_t i;
4536
4537    /* Stack is ... instance, state.  We want to leave instance at
4538     * the stack top, possibly mutated via instance.__setstate__(state).
4539     */
4540    if (self->stack->length < 2)
4541        return stackUnderflow();
4542    PDATA_POP(self->stack, state);
4543    if (state == NULL)
4544        return -1;
4545    inst = self->stack->data[self->stack->length - 1];
4546
4547    __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4548    if (__setstate__ != NULL) {
4549        PyObject *junk = NULL;
4550
4551        /* The explicit __setstate__ is responsible for everything. */
4552        ARG_TUP(self, state);
4553        if (self->arg) {
4554            junk = PyObject_Call(__setstate__, self->arg, NULL);
4555            FREE_ARG_TUP(self);
4556        }
4557        Py_DECREF(__setstate__);
4558        if (junk == NULL)
4559            return -1;
4560        Py_DECREF(junk);
4561        return 0;
4562    }
4563    if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4564        return -1;
4565    PyErr_Clear();
4566
4567    /* A default __setstate__.  First see whether state embeds a
4568     * slot state dict too (a proto 2 addition).
4569     */
4570    if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4571        PyObject *temp = state;
4572        state = PyTuple_GET_ITEM(temp, 0);
4573        slotstate = PyTuple_GET_ITEM(temp, 1);
4574        Py_INCREF(state);
4575        Py_INCREF(slotstate);
4576        Py_DECREF(temp);
4577    }
4578    else
4579        slotstate = NULL;
4580
4581    /* Set inst.__dict__ from the state dict (if any). */
4582    if (state != Py_None) {
4583        PyObject *dict;
4584        if (! PyDict_Check(state)) {
4585            PyErr_SetString(UnpicklingError, "state is not a "
4586                            "dictionary");
4587            goto finally;
4588        }
4589        dict = PyObject_GetAttr(inst, __dict___str);
4590        if (dict == NULL)
4591            goto finally;
4592
4593        i = 0;
4594        while (PyDict_Next(state, &i, &d_key, &d_value)) {
4595            /* normally the keys for instance attributes are
4596               interned.  we should try to do that here. */
4597            Py_INCREF(d_key);
4598            if (PyString_CheckExact(d_key))
4599                PyString_InternInPlace(&d_key);
4600            if (PyObject_SetItem(dict, d_key, d_value) < 0) {
4601                Py_DECREF(d_key);
4602                goto finally;
4603            }
4604            Py_DECREF(d_key);
4605        }
4606        Py_DECREF(dict);
4607    }
4608
4609    /* Also set instance attributes from the slotstate dict (if any). */
4610    if (slotstate != NULL) {
4611        if (! PyDict_Check(slotstate)) {
4612            PyErr_SetString(UnpicklingError, "slot state is not "
4613                            "a dictionary");
4614            goto finally;
4615        }
4616        i = 0;
4617        while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4618            if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4619                goto finally;
4620        }
4621    }
4622    res = 0;
4623
4624  finally:
4625    Py_DECREF(state);
4626    Py_XDECREF(slotstate);
4627    return res;
4628}
4629
4630
4631static int
4632load_mark(Unpicklerobject *self)
4633{
4634    Py_ssize_t s;
4635
4636    /* Note that we split the (pickle.py) stack into two stacks, an
4637       object stack and a mark stack. Here we push a mark onto the
4638       mark stack.
4639    */
4640
4641    if ((self->num_marks + 1) >= self->marks_size) {
4642        Py_ssize_t *marks;
4643        s=self->marks_size+20;
4644        if (s <= self->num_marks) s=self->num_marks + 1;
4645        if (self->marks == NULL)
4646            marks=(Py_ssize_t *)malloc(s * sizeof(Py_ssize_t));
4647        else
4648            marks=(Py_ssize_t *)realloc(self->marks,
4649                                        s * sizeof(Py_ssize_t));
4650        if (!marks) {
4651            PyErr_NoMemory();
4652            return -1;
4653        }
4654        self->marks = marks;
4655        self->marks_size = s;
4656    }
4657
4658    self->marks[self->num_marks++] = self->stack->length;
4659
4660    return 0;
4661}
4662
4663static int
4664load_reduce(Unpicklerobject *self)
4665{
4666    PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4667
4668    PDATA_POP(self->stack, arg_tup);
4669    if (! arg_tup) return -1;
4670    PDATA_POP(self->stack, callable);
4671    if (callable) {
4672        ob = Instance_New(callable, arg_tup);
4673        Py_DECREF(callable);
4674    }
4675    Py_DECREF(arg_tup);
4676
4677    if (! ob) return -1;
4678
4679    PDATA_PUSH(self->stack, ob, -1);
4680    return 0;
4681}
4682
4683/* Just raises an error if we don't know the protocol specified.  PROTO
4684 * is the first opcode for protocols >= 2.
4685 */
4686static int
4687load_proto(Unpicklerobject *self)
4688{
4689    int i;
4690    char *protobyte;
4691
4692    i = self->read_func(self, &protobyte, 1);
4693    if (i < 0)
4694        return -1;
4695
4696    i = calc_binint(protobyte, 1);
4697    /* No point checking for < 0, since calc_binint returns an unsigned
4698     * int when chewing on 1 byte.
4699     */
4700    assert(i >= 0);
4701    if (i <= HIGHEST_PROTOCOL)
4702        return 0;
4703
4704    PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4705    return -1;
4706}
4707
4708static PyObject *
4709load(Unpicklerobject *self)
4710{
4711    PyObject *err = 0, *val = 0;
4712    char *s;
4713
4714    self->num_marks = 0;
4715    if (self->stack->length) Pdata_clear(self->stack, 0);
4716
4717    while (1) {
4718        if (self->read_func(self, &s, 1) < 0)
4719            break;
4720
4721        switch (s[0]) {
4722        case NONE:
4723            if (load_none(self) < 0)
4724                break;
4725            continue;
4726
4727        case BININT:
4728            if (load_binint(self) < 0)
4729                break;
4730            continue;
4731
4732        case BININT1:
4733            if (load_binint1(self) < 0)
4734                break;
4735            continue;
4736
4737        case BININT2:
4738            if (load_binint2(self) < 0)
4739                break;
4740            continue;
4741
4742        case INT:
4743            if (load_int(self) < 0)
4744                break;
4745            continue;
4746
4747        case LONG:
4748            if (load_long(self) < 0)
4749                break;
4750            continue;
4751
4752        case LONG1:
4753            if (load_counted_long(self, 1) < 0)
4754                break;
4755            continue;
4756
4757        case LONG4:
4758            if (load_counted_long(self, 4) < 0)
4759                break;
4760            continue;
4761
4762        case FLOAT:
4763            if (load_float(self) < 0)
4764                break;
4765            continue;
4766
4767        case BINFLOAT:
4768            if (load_binfloat(self) < 0)
4769                break;
4770            continue;
4771
4772        case BINSTRING:
4773            if (load_binstring(self) < 0)
4774                break;
4775            continue;
4776
4777        case SHORT_BINSTRING:
4778            if (load_short_binstring(self) < 0)
4779                break;
4780            continue;
4781
4782        case STRING:
4783            if (load_string(self) < 0)
4784                break;
4785            continue;
4786
4787#ifdef Py_USING_UNICODE
4788        case UNICODE:
4789            if (load_unicode(self) < 0)
4790                break;
4791            continue;
4792
4793        case BINUNICODE:
4794            if (load_binunicode(self) < 0)
4795                break;
4796            continue;
4797#endif
4798
4799        case EMPTY_TUPLE:
4800            if (load_counted_tuple(self, 0) < 0)
4801                break;
4802            continue;
4803
4804        case TUPLE1:
4805            if (load_counted_tuple(self, 1) < 0)
4806                break;
4807            continue;
4808
4809        case TUPLE2:
4810            if (load_counted_tuple(self, 2) < 0)
4811                break;
4812            continue;
4813
4814        case TUPLE3:
4815            if (load_counted_tuple(self, 3) < 0)
4816                break;
4817            continue;
4818
4819        case TUPLE:
4820            if (load_tuple(self) < 0)
4821                break;
4822            continue;
4823
4824        case EMPTY_LIST:
4825            if (load_empty_list(self) < 0)
4826                break;
4827            continue;
4828
4829        case LIST:
4830            if (load_list(self) < 0)
4831                break;
4832            continue;
4833
4834        case EMPTY_DICT:
4835            if (load_empty_dict(self) < 0)
4836                break;
4837            continue;
4838
4839        case DICT:
4840            if (load_dict(self) < 0)
4841                break;
4842            continue;
4843
4844        case OBJ:
4845            if (load_obj(self) < 0)
4846                break;
4847            continue;
4848
4849        case INST:
4850            if (load_inst(self) < 0)
4851                break;
4852            continue;
4853
4854        case NEWOBJ:
4855            if (load_newobj(self) < 0)
4856                break;
4857            continue;
4858
4859        case GLOBAL:
4860            if (load_global(self) < 0)
4861                break;
4862            continue;
4863
4864        case APPEND:
4865            if (load_append(self) < 0)
4866                break;
4867            continue;
4868
4869        case APPENDS:
4870            if (load_appends(self) < 0)
4871                break;
4872            continue;
4873
4874        case BUILD:
4875            if (load_build(self) < 0)
4876                break;
4877            continue;
4878
4879        case DUP:
4880            if (load_dup(self) < 0)
4881                break;
4882            continue;
4883
4884        case BINGET:
4885            if (load_binget(self) < 0)
4886                break;
4887            continue;
4888
4889        case LONG_BINGET:
4890            if (load_long_binget(self) < 0)
4891                break;
4892            continue;
4893
4894        case GET:
4895            if (load_get(self) < 0)
4896                break;
4897            continue;
4898
4899        case EXT1:
4900            if (load_extension(self, 1) < 0)
4901                break;
4902            continue;
4903
4904        case EXT2:
4905            if (load_extension(self, 2) < 0)
4906                break;
4907            continue;
4908
4909        case EXT4:
4910            if (load_extension(self, 4) < 0)
4911                break;
4912            continue;
4913        case MARK:
4914            if (load_mark(self) < 0)
4915                break;
4916            continue;
4917
4918        case BINPUT:
4919            if (load_binput(self) < 0)
4920                break;
4921            continue;
4922
4923        case LONG_BINPUT:
4924            if (load_long_binput(self) < 0)
4925                break;
4926            continue;
4927
4928        case PUT:
4929            if (load_put(self) < 0)
4930                break;
4931            continue;
4932
4933        case POP:
4934            if (load_pop(self) < 0)
4935                break;
4936            continue;
4937
4938        case POP_MARK:
4939            if (load_pop_mark(self) < 0)
4940                break;
4941            continue;
4942
4943        case SETITEM:
4944            if (load_setitem(self) < 0)
4945                break;
4946            continue;
4947
4948        case SETITEMS:
4949            if (load_setitems(self) < 0)
4950                break;
4951            continue;
4952
4953        case STOP:
4954            break;
4955
4956        case PERSID:
4957            if (load_persid(self) < 0)
4958                break;
4959            continue;
4960
4961        case BINPERSID:
4962            if (load_binpersid(self) < 0)
4963                break;
4964            continue;
4965
4966        case REDUCE:
4967            if (load_reduce(self) < 0)
4968                break;
4969            continue;
4970
4971        case PROTO:
4972            if (load_proto(self) < 0)
4973                break;
4974            continue;
4975
4976        case NEWTRUE:
4977            if (load_bool(self, Py_True) < 0)
4978                break;
4979            continue;
4980
4981        case NEWFALSE:
4982            if (load_bool(self, Py_False) < 0)
4983                break;
4984            continue;
4985
4986        case '\0':
4987            /* end of file */
4988            PyErr_SetNone(PyExc_EOFError);
4989            break;
4990
4991        default:
4992            cPickle_ErrFormat(UnpicklingError,
4993                              "invalid load key, '%s'.",
4994                              "c", s[0]);
4995            return NULL;
4996        }
4997
4998        break;
4999    }
5000
5001    if ((err = PyErr_Occurred())) {
5002        if (err == PyExc_EOFError) {
5003            PyErr_SetNone(PyExc_EOFError);
5004        }
5005        return NULL;
5006    }
5007
5008    PDATA_POP(self->stack, val);
5009    return val;
5010}
5011
5012
5013/* No-load functions to support noload, which is used to
5014   find persistent references. */
5015
5016static int
5017noload_obj(Unpicklerobject *self)
5018{
5019    Py_ssize_t i;
5020
5021    if ((i = marker(self)) < 0) return -1;
5022    return Pdata_clear(self->stack, i+1);
5023}
5024
5025
5026static int
5027noload_inst(Unpicklerobject *self)
5028{
5029    Py_ssize_t i;
5030    char *s;
5031
5032    if ((i = marker(self)) < 0) return -1;
5033    Pdata_clear(self->stack, i);
5034    if (self->readline_func(self, &s) < 0) return -1;
5035    if (self->readline_func(self, &s) < 0) return -1;
5036    PDATA_APPEND(self->stack, Py_None, -1);
5037    return 0;
5038}
5039
5040static int
5041noload_newobj(Unpicklerobject *self)
5042{
5043    PyObject *obj;
5044
5045    PDATA_POP(self->stack, obj);        /* pop argtuple */
5046    if (obj == NULL) return -1;
5047    Py_DECREF(obj);
5048
5049    PDATA_POP(self->stack, obj);        /* pop cls */
5050    if (obj == NULL) return -1;
5051    Py_DECREF(obj);
5052
5053    PDATA_APPEND(self->stack, Py_None, -1);
5054    return 0;
5055}
5056
5057static int
5058noload_global(Unpicklerobject *self)
5059{
5060    char *s;
5061
5062    if (self->readline_func(self, &s) < 0) return -1;
5063    if (self->readline_func(self, &s) < 0) return -1;
5064    PDATA_APPEND(self->stack, Py_None,-1);
5065    return 0;
5066}
5067
5068static int
5069noload_reduce(Unpicklerobject *self)
5070{
5071
5072    if (self->stack->length < 2) return stackUnderflow();
5073    Pdata_clear(self->stack, self->stack->length-2);
5074    PDATA_APPEND(self->stack, Py_None,-1);
5075    return 0;
5076}
5077
5078static int
5079noload_build(Unpicklerobject *self) {
5080
5081  if (self->stack->length < 1) return stackUnderflow();
5082  Pdata_clear(self->stack, self->stack->length-1);
5083  return 0;
5084}
5085
5086static int
5087noload_extension(Unpicklerobject *self, int nbytes)
5088{
5089    char *codebytes;
5090
5091    assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
5092    if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
5093    PDATA_APPEND(self->stack, Py_None, -1);
5094    return 0;
5095}
5096
5097static int
5098noload_append(Unpicklerobject *self)
5099{
5100    return Pdata_clear(self->stack, self->stack->length - 1);
5101}
5102
5103static int
5104noload_appends(Unpicklerobject *self)
5105{
5106    Py_ssize_t i;
5107    if ((i = marker(self)) < 0) return -1;
5108    return Pdata_clear(self->stack, i);
5109}
5110
5111static int
5112noload_setitem(Unpicklerobject *self)
5113{
5114    return Pdata_clear(self->stack, self->stack->length - 2);
5115}
5116
5117static int
5118noload_setitems(Unpicklerobject *self)
5119{
5120    Py_ssize_t i;
5121    if ((i = marker(self)) < 0) return -1;
5122    return Pdata_clear(self->stack, i);
5123}
5124
5125static PyObject *
5126noload(Unpicklerobject *self)
5127{
5128    PyObject *err = 0, *val = 0;
5129    char *s;
5130
5131    self->num_marks = 0;
5132    Pdata_clear(self->stack, 0);
5133
5134    while (1) {
5135        if (self->read_func(self, &s, 1) < 0)
5136            break;
5137
5138        switch (s[0]) {
5139        case NONE:
5140            if (load_none(self) < 0)
5141                break;
5142            continue;
5143
5144        case BININT:
5145            if (load_binint(self) < 0)
5146                break;
5147            continue;
5148
5149        case BININT1:
5150            if (load_binint1(self) < 0)
5151                break;
5152            continue;
5153
5154        case BININT2:
5155            if (load_binint2(self) < 0)
5156                break;
5157            continue;
5158
5159        case INT:
5160            if (load_int(self) < 0)
5161                break;
5162            continue;
5163
5164        case LONG:
5165            if (load_long(self) < 0)
5166                break;
5167            continue;
5168
5169        case LONG1:
5170            if (load_counted_long(self, 1) < 0)
5171                break;
5172            continue;
5173
5174        case LONG4:
5175            if (load_counted_long(self, 4) < 0)
5176                break;
5177            continue;
5178
5179        case FLOAT:
5180            if (load_float(self) < 0)
5181                break;
5182            continue;
5183
5184        case BINFLOAT:
5185            if (load_binfloat(self) < 0)
5186                break;
5187            continue;
5188
5189        case BINSTRING:
5190            if (load_binstring(self) < 0)
5191                break;
5192            continue;
5193
5194        case SHORT_BINSTRING:
5195            if (load_short_binstring(self) < 0)
5196                break;
5197            continue;
5198
5199        case STRING:
5200            if (load_string(self) < 0)
5201                break;
5202            continue;
5203
5204#ifdef Py_USING_UNICODE
5205        case UNICODE:
5206            if (load_unicode(self) < 0)
5207                break;
5208            continue;
5209
5210        case BINUNICODE:
5211            if (load_binunicode(self) < 0)
5212                break;
5213            continue;
5214#endif
5215
5216        case EMPTY_TUPLE:
5217            if (load_counted_tuple(self, 0) < 0)
5218                break;
5219            continue;
5220
5221        case TUPLE1:
5222            if (load_counted_tuple(self, 1) < 0)
5223                break;
5224            continue;
5225
5226        case TUPLE2:
5227            if (load_counted_tuple(self, 2) < 0)
5228                break;
5229            continue;
5230
5231        case TUPLE3:
5232            if (load_counted_tuple(self, 3) < 0)
5233                break;
5234            continue;
5235
5236        case TUPLE:
5237            if (load_tuple(self) < 0)
5238                break;
5239            continue;
5240
5241        case EMPTY_LIST:
5242            if (load_empty_list(self) < 0)
5243                break;
5244            continue;
5245
5246        case LIST:
5247            if (load_list(self) < 0)
5248                break;
5249            continue;
5250
5251        case EMPTY_DICT:
5252            if (load_empty_dict(self) < 0)
5253                break;
5254            continue;
5255
5256        case DICT:
5257            if (load_dict(self) < 0)
5258                break;
5259            continue;
5260
5261        case OBJ:
5262            if (noload_obj(self) < 0)
5263                break;
5264            continue;
5265
5266        case INST:
5267            if (noload_inst(self) < 0)
5268                break;
5269            continue;
5270
5271        case NEWOBJ:
5272            if (noload_newobj(self) < 0)
5273                break;
5274            continue;
5275
5276        case GLOBAL:
5277            if (noload_global(self) < 0)
5278                break;
5279            continue;
5280
5281        case APPEND:
5282            if (noload_append(self) < 0)
5283                break;
5284            continue;
5285
5286        case APPENDS:
5287            if (noload_appends(self) < 0)
5288                break;
5289            continue;
5290
5291        case BUILD:
5292            if (noload_build(self) < 0)
5293                break;
5294            continue;
5295
5296        case DUP:
5297            if (load_dup(self) < 0)
5298                break;
5299            continue;
5300
5301        case BINGET:
5302            if (load_binget(self) < 0)
5303                break;
5304            continue;
5305
5306        case LONG_BINGET:
5307            if (load_long_binget(self) < 0)
5308                break;
5309            continue;
5310
5311        case GET:
5312            if (load_get(self) < 0)
5313                break;
5314            continue;
5315
5316        case EXT1:
5317            if (noload_extension(self, 1) < 0)
5318                break;
5319            continue;
5320
5321        case EXT2:
5322            if (noload_extension(self, 2) < 0)
5323                break;
5324            continue;
5325
5326        case EXT4:
5327            if (noload_extension(self, 4) < 0)
5328                break;
5329            continue;
5330
5331        case MARK:
5332            if (load_mark(self) < 0)
5333                break;
5334            continue;
5335
5336        case BINPUT:
5337            if (load_binput(self) < 0)
5338                break;
5339            continue;
5340
5341        case LONG_BINPUT:
5342            if (load_long_binput(self) < 0)
5343                break;
5344            continue;
5345
5346        case PUT:
5347            if (load_put(self) < 0)
5348                break;
5349            continue;
5350
5351        case POP:
5352            if (load_pop(self) < 0)
5353                break;
5354            continue;
5355
5356        case POP_MARK:
5357            if (load_pop_mark(self) < 0)
5358                break;
5359            continue;
5360
5361        case SETITEM:
5362            if (noload_setitem(self) < 0)
5363                break;
5364            continue;
5365
5366        case SETITEMS:
5367            if (noload_setitems(self) < 0)
5368                break;
5369            continue;
5370
5371        case STOP:
5372            break;
5373
5374        case PERSID:
5375            if (load_persid(self) < 0)
5376                break;
5377            continue;
5378
5379        case BINPERSID:
5380            if (load_binpersid(self) < 0)
5381                break;
5382            continue;
5383
5384        case REDUCE:
5385            if (noload_reduce(self) < 0)
5386                break;
5387            continue;
5388
5389        case PROTO:
5390            if (load_proto(self) < 0)
5391                break;
5392            continue;
5393
5394        case NEWTRUE:
5395            if (load_bool(self, Py_True) < 0)
5396                break;
5397            continue;
5398
5399        case NEWFALSE:
5400            if (load_bool(self, Py_False) < 0)
5401                break;
5402            continue;
5403        default:
5404            cPickle_ErrFormat(UnpicklingError,
5405                              "invalid load key, '%s'.",
5406                              "c", s[0]);
5407            return NULL;
5408        }
5409
5410        break;
5411    }
5412
5413    if ((err = PyErr_Occurred())) {
5414        if (err == PyExc_EOFError) {
5415            PyErr_SetNone(PyExc_EOFError);
5416        }
5417        return NULL;
5418    }
5419
5420    PDATA_POP(self->stack, val);
5421    return val;
5422}
5423
5424
5425static PyObject *
5426Unpickler_load(Unpicklerobject *self, PyObject *unused)
5427{
5428    return load(self);
5429}
5430
5431static PyObject *
5432Unpickler_noload(Unpicklerobject *self, PyObject *unused)
5433{
5434    return noload(self);
5435}
5436
5437
5438static struct PyMethodDef Unpickler_methods[] = {
5439  {"load",         (PyCFunction)Unpickler_load,   METH_NOARGS,
5440   PyDoc_STR("load() -- Load a pickle")
5441  },
5442  {"noload",         (PyCFunction)Unpickler_noload,   METH_NOARGS,
5443   PyDoc_STR(
5444   "noload() -- not load a pickle, but go through most of the motions\n"
5445   "\n"
5446   "This function can be used to read past a pickle without instantiating\n"
5447   "any objects or importing any modules.  It can also be used to find all\n"
5448   "persistent references without instantiating any objects or importing\n"
5449   "any modules.\n")
5450  },
5451  {NULL,              NULL}           /* sentinel */
5452};
5453
5454
5455static Unpicklerobject *
5456newUnpicklerobject(PyObject *f)
5457{
5458    Unpicklerobject *self;
5459
5460    if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5461        return NULL;
5462
5463    self->file = NULL;
5464    self->arg = NULL;
5465    self->stack = (Pdata*)Pdata_New();
5466    self->pers_func = NULL;
5467    self->last_string = NULL;
5468    self->marks = NULL;
5469    self->num_marks = 0;
5470    self->marks_size = 0;
5471    self->buf_size = 0;
5472    self->read = NULL;
5473    self->readline = NULL;
5474    self->find_class = NULL;
5475
5476    if (!( self->memo = PyDict_New()))
5477        goto err;
5478
5479    if (!self->stack)
5480        goto err;
5481
5482    Py_INCREF(f);
5483    self->file = f;
5484
5485    /* Set read, readline based on type of f */
5486    if (PyFile_Check(f)) {
5487        self->fp = PyFile_AsFile(f);
5488        if (self->fp == NULL) {
5489            PyErr_SetString(PyExc_ValueError,
5490                            "I/O operation on closed file");
5491            goto err;
5492        }
5493        self->read_func = read_file;
5494        self->readline_func = readline_file;
5495    }
5496    else if (PycStringIO_InputCheck(f)) {
5497        self->fp = NULL;
5498        self->read_func = read_cStringIO;
5499        self->readline_func = readline_cStringIO;
5500    }
5501    else {
5502
5503        self->fp = NULL;
5504        self->read_func = read_other;
5505        self->readline_func = readline_other;
5506
5507        if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5508               (self->read = PyObject_GetAttr(f, read_str))))  {
5509            PyErr_Clear();
5510            PyErr_SetString( PyExc_TypeError,
5511                             "argument must have 'read' and "
5512                             "'readline' attributes" );
5513            goto err;
5514        }
5515    }
5516    PyObject_GC_Track(self);
5517
5518    return self;
5519
5520  err:
5521    Py_DECREF((PyObject *)self);
5522    return NULL;
5523}
5524
5525
5526static PyObject *
5527get_Unpickler(PyObject *self, PyObject *file)
5528{
5529    return (PyObject *)newUnpicklerobject(file);
5530}
5531
5532
5533static void
5534Unpickler_dealloc(Unpicklerobject *self)
5535{
5536    PyObject_GC_UnTrack((PyObject *)self);
5537    Py_XDECREF(self->readline);
5538    Py_XDECREF(self->read);
5539    Py_XDECREF(self->file);
5540    Py_XDECREF(self->memo);
5541    Py_XDECREF(self->stack);
5542    Py_XDECREF(self->pers_func);
5543    Py_XDECREF(self->arg);
5544    Py_XDECREF(self->last_string);
5545    Py_XDECREF(self->find_class);
5546
5547    if (self->marks) {
5548        free(self->marks);
5549    }
5550
5551    if (self->buf_size) {
5552        free(self->buf);
5553    }
5554
5555    Py_TYPE(self)->tp_free((PyObject *)self);
5556}
5557
5558static int
5559Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5560{
5561    Py_VISIT(self->readline);
5562    Py_VISIT(self->read);
5563    Py_VISIT(self->file);
5564    Py_VISIT(self->memo);
5565    Py_VISIT(self->stack);
5566    Py_VISIT(self->pers_func);
5567    Py_VISIT(self->arg);
5568    Py_VISIT(self->last_string);
5569    Py_VISIT(self->find_class);
5570    return 0;
5571}
5572
5573static int
5574Unpickler_clear(Unpicklerobject *self)
5575{
5576    Py_CLEAR(self->readline);
5577    Py_CLEAR(self->read);
5578    Py_CLEAR(self->file);
5579    Py_CLEAR(self->memo);
5580    Py_CLEAR(self->stack);
5581    Py_CLEAR(self->pers_func);
5582    Py_CLEAR(self->arg);
5583    Py_CLEAR(self->last_string);
5584    Py_CLEAR(self->find_class);
5585    return 0;
5586}
5587
5588static PyObject *
5589Unpickler_getattr(Unpicklerobject *self, char *name)
5590{
5591    if (!strcmp(name, "persistent_load")) {
5592        if (!self->pers_func) {
5593            PyErr_SetString(PyExc_AttributeError, name);
5594            return NULL;
5595        }
5596
5597        Py_INCREF(self->pers_func);
5598        return self->pers_func;
5599    }
5600
5601    if (!strcmp(name, "find_global")) {
5602        if (!self->find_class) {
5603            PyErr_SetString(PyExc_AttributeError, name);
5604            return NULL;
5605        }
5606
5607        Py_INCREF(self->find_class);
5608        return self->find_class;
5609    }
5610
5611    if (!strcmp(name, "memo")) {
5612        if (!self->memo) {
5613            PyErr_SetString(PyExc_AttributeError, name);
5614            return NULL;
5615        }
5616
5617        Py_INCREF(self->memo);
5618        return self->memo;
5619    }
5620
5621    if (!strcmp(name, "UnpicklingError")) {
5622        Py_INCREF(UnpicklingError);
5623        return UnpicklingError;
5624    }
5625
5626    return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5627}
5628
5629
5630static int
5631Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5632{
5633
5634    if (!strcmp(name, "persistent_load")) {
5635        Py_XDECREF(self->pers_func);
5636        self->pers_func = value;
5637        Py_XINCREF(value);
5638        return 0;
5639    }
5640
5641    if (!strcmp(name, "find_global")) {
5642        Py_XDECREF(self->find_class);
5643        self->find_class = value;
5644        Py_XINCREF(value);
5645        return 0;
5646    }
5647
5648    if (! value) {
5649        PyErr_SetString(PyExc_TypeError,
5650                        "attribute deletion is not supported");
5651        return -1;
5652    }
5653
5654    if (strcmp(name, "memo") == 0) {
5655        if (!PyDict_Check(value)) {
5656            PyErr_SetString(PyExc_TypeError,
5657                            "memo must be a dictionary");
5658            return -1;
5659        }
5660        Py_XDECREF(self->memo);
5661        self->memo = value;
5662        Py_INCREF(value);
5663        return 0;
5664    }
5665
5666    PyErr_SetString(PyExc_AttributeError, name);
5667    return -1;
5668}
5669
5670/* ---------------------------------------------------------------------------
5671 * Module-level functions.
5672 */
5673
5674/* dump(obj, file, protocol=0). */
5675static PyObject *
5676cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5677{
5678    static char *kwlist[] = {"obj", "file", "protocol", NULL};
5679    PyObject *ob, *file, *res = NULL;
5680    Picklerobject *pickler = 0;
5681    int proto = 0;
5682
5683    if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5684               &ob, &file, &proto)))
5685        goto finally;
5686
5687    if (!( pickler = newPicklerobject(file, proto)))
5688        goto finally;
5689
5690    if (dump(pickler, ob) < 0)
5691        goto finally;
5692
5693    Py_INCREF(Py_None);
5694    res = Py_None;
5695
5696  finally:
5697    Py_XDECREF(pickler);
5698
5699    return res;
5700}
5701
5702
5703/* dumps(obj, protocol=0). */
5704static PyObject *
5705cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5706{
5707    static char *kwlist[] = {"obj", "protocol", NULL};
5708    PyObject *ob, *file = 0, *res = NULL;
5709    Picklerobject *pickler = 0;
5710    int proto = 0;
5711
5712    if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5713               &ob, &proto)))
5714        goto finally;
5715
5716    if (!( file = PycStringIO->NewOutput(128)))
5717        goto finally;
5718
5719    if (!( pickler = newPicklerobject(file, proto)))
5720        goto finally;
5721
5722    if (dump(pickler, ob) < 0)
5723        goto finally;
5724
5725    res = PycStringIO->cgetvalue(file);
5726
5727  finally:
5728    Py_XDECREF(pickler);
5729    Py_XDECREF(file);
5730
5731    return res;
5732}
5733
5734
5735/* load(fileobj). */
5736static PyObject *
5737cpm_load(PyObject *self, PyObject *ob)
5738{
5739    Unpicklerobject *unpickler = 0;
5740    PyObject *res = NULL;
5741
5742    if (!( unpickler = newUnpicklerobject(ob)))
5743        goto finally;
5744
5745    res = load(unpickler);
5746
5747  finally:
5748    Py_XDECREF(unpickler);
5749
5750    return res;
5751}
5752
5753
5754/* loads(string) */
5755static PyObject *
5756cpm_loads(PyObject *self, PyObject *args)
5757{
5758    PyObject *ob, *file = 0, *res = NULL;
5759    Unpicklerobject *unpickler = 0;
5760
5761    if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5762        goto finally;
5763
5764    if (!( file = PycStringIO->NewInput(ob)))
5765        goto finally;
5766
5767    if (!( unpickler = newUnpicklerobject(file)))
5768        goto finally;
5769
5770    res = load(unpickler);
5771
5772  finally:
5773    Py_XDECREF(file);
5774    Py_XDECREF(unpickler);
5775
5776    return res;
5777}
5778
5779
5780PyDoc_STRVAR(Unpicklertype__doc__,
5781"Objects that know how to unpickle");
5782
5783static PyTypeObject Unpicklertype = {
5784    PyVarObject_HEAD_INIT(NULL, 0)
5785    "cPickle.Unpickler",                 /*tp_name*/
5786    sizeof(Unpicklerobject),             /*tp_basicsize*/
5787    0,
5788    (destructor)Unpickler_dealloc,      /* tp_dealloc */
5789    0,                                  /* tp_print */
5790    (getattrfunc)Unpickler_getattr,     /* tp_getattr */
5791    (setattrfunc)Unpickler_setattr,     /* tp_setattr */
5792    0,                                  /* tp_compare */
5793    0,                                  /* tp_repr */
5794    0,                                  /* tp_as_number */
5795    0,                                  /* tp_as_sequence */
5796    0,                                  /* tp_as_mapping */
5797    0,                                  /* tp_hash */
5798    0,                                  /* tp_call */
5799    0,                                  /* tp_str */
5800    0,                                  /* tp_getattro */
5801    0,                                  /* tp_setattro */
5802    0,                                  /* tp_as_buffer */
5803    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5804    Unpicklertype__doc__,               /* tp_doc */
5805    (traverseproc)Unpickler_traverse,   /* tp_traverse */
5806    (inquiry)Unpickler_clear,           /* tp_clear */
5807};
5808
5809static struct PyMethodDef cPickle_methods[] = {
5810  {"dump",         (PyCFunction)cpm_dump,         METH_VARARGS | METH_KEYWORDS,
5811   PyDoc_STR("dump(obj, file, protocol=0) -- "
5812   "Write an object in pickle format to the given file.\n"
5813   "\n"
5814   "See the Pickler docstring for the meaning of optional argument proto.")
5815  },
5816
5817  {"dumps",        (PyCFunction)cpm_dumps,        METH_VARARGS | METH_KEYWORDS,
5818   PyDoc_STR("dumps(obj, protocol=0) -- "
5819   "Return a string containing an object in pickle format.\n"
5820   "\n"
5821   "See the Pickler docstring for the meaning of optional argument proto.")
5822  },
5823
5824  {"load",         (PyCFunction)cpm_load,         METH_O,
5825   PyDoc_STR("load(file) -- Load a pickle from the given file")},
5826
5827  {"loads",        (PyCFunction)cpm_loads,        METH_VARARGS,
5828   PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5829
5830  {"Pickler",      (PyCFunction)get_Pickler,      METH_VARARGS | METH_KEYWORDS,
5831   PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5832   "\n"
5833   "This takes a file-like object for writing a pickle data stream.\n"
5834   "The optional proto argument tells the pickler to use the given\n"
5835   "protocol; supported protocols are 0, 1, 2.  The default\n"
5836   "protocol is 0, to be backwards compatible.  (Protocol 0 is the\n"
5837   "only protocol that can be written to a file opened in text\n"
5838   "mode and read back successfully.  When using a protocol higher\n"
5839   "than 0, make sure the file is opened in binary mode, both when\n"
5840   "pickling and unpickling.)\n"
5841   "\n"
5842   "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5843   "more efficient than protocol 1.\n"
5844   "\n"
5845   "Specifying a negative protocol version selects the highest\n"
5846   "protocol version supported.  The higher the protocol used, the\n"
5847   "more recent the version of Python needed to read the pickle\n"
5848   "produced.\n"
5849   "\n"
5850   "The file parameter must have a write() method that accepts a single\n"
5851   "string argument.  It can thus be an open file object, a StringIO\n"
5852   "object, or any other custom object that meets this interface.\n")
5853  },
5854
5855  {"Unpickler",    (PyCFunction)get_Unpickler,    METH_O,
5856   PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5857
5858  { NULL, NULL }
5859};
5860
5861static int
5862init_stuff(PyObject *module_dict)
5863{
5864    PyObject *copyreg, *t, *r;
5865
5866#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S)))  return -1;
5867
5868    if (PyType_Ready(&Unpicklertype) < 0)
5869        return -1;
5870    if (PyType_Ready(&Picklertype) < 0)
5871        return -1;
5872
5873    INIT_STR(__class__);
5874    INIT_STR(__getinitargs__);
5875    INIT_STR(__dict__);
5876    INIT_STR(__getstate__);
5877    INIT_STR(__setstate__);
5878    INIT_STR(__name__);
5879    INIT_STR(__main__);
5880    INIT_STR(__reduce__);
5881    INIT_STR(__reduce_ex__);
5882    INIT_STR(write);
5883    INIT_STR(append);
5884    INIT_STR(read);
5885    INIT_STR(readline);
5886    INIT_STR(dispatch_table);
5887
5888    if (!( copyreg = PyImport_ImportModule("copy_reg")))
5889        return -1;
5890
5891    /* This is special because we want to use a different
5892       one in restricted mode. */
5893    dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5894    if (!dispatch_table) return -1;
5895
5896    extension_registry = PyObject_GetAttrString(copyreg,
5897                            "_extension_registry");
5898    if (!extension_registry) return -1;
5899
5900    inverted_registry = PyObject_GetAttrString(copyreg,
5901                            "_inverted_registry");
5902    if (!inverted_registry) return -1;
5903
5904    extension_cache = PyObject_GetAttrString(copyreg,
5905                            "_extension_cache");
5906    if (!extension_cache) return -1;
5907
5908    Py_DECREF(copyreg);
5909
5910    if (!(empty_tuple = PyTuple_New(0)))
5911        return -1;
5912
5913    two_tuple = PyTuple_New(2);
5914    if (two_tuple == NULL)
5915        return -1;
5916    /* We use this temp container with no regard to refcounts, or to
5917     * keeping containees alive.  Exempt from GC, because we don't
5918     * want anything looking at two_tuple() by magic.
5919     */
5920    PyObject_GC_UnTrack(two_tuple);
5921
5922    /* Ugh */
5923    if (!( t=PyImport_ImportModule("__builtin__")))  return -1;
5924    if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5925        return -1;
5926
5927    if (!( t=PyDict_New()))  return -1;
5928    if (!( r=PyRun_String(
5929                   "def __str__(self):\n"
5930                   "  return self.args and ('%s' % self.args[0]) or '(what)'\n",
5931                   Py_file_input,
5932                   module_dict, t)  ))  return -1;
5933    Py_DECREF(r);
5934
5935    PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5936    if (!PickleError)
5937        return -1;
5938
5939    Py_DECREF(t);
5940
5941    PicklingError = PyErr_NewException("cPickle.PicklingError",
5942                                       PickleError, NULL);
5943    if (!PicklingError)
5944        return -1;
5945
5946    if (!( t=PyDict_New()))  return -1;
5947    if (!( r=PyRun_String(
5948                   "def __str__(self):\n"
5949                   "  a=self.args\n"
5950                   "  a=a and type(a[0]) or '(what)'\n"
5951                   "  return 'Cannot pickle %s objects' % a\n"
5952                   , Py_file_input,
5953                   module_dict, t)  ))  return -1;
5954    Py_DECREF(r);
5955
5956    if (!( UnpickleableError = PyErr_NewException(
5957                   "cPickle.UnpickleableError", PicklingError, t)))
5958        return -1;
5959
5960    Py_DECREF(t);
5961
5962    if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5963                                                PickleError, NULL)))
5964        return -1;
5965
5966    if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5967                                             UnpicklingError, NULL)))
5968        return -1;
5969
5970    if (PyDict_SetItemString(module_dict, "PickleError",
5971                             PickleError) < 0)
5972        return -1;
5973
5974    if (PyDict_SetItemString(module_dict, "PicklingError",
5975                             PicklingError) < 0)
5976        return -1;
5977
5978    if (PyDict_SetItemString(module_dict, "UnpicklingError",
5979                             UnpicklingError) < 0)
5980        return -1;
5981
5982    if (PyDict_SetItemString(module_dict, "UnpickleableError",
5983                             UnpickleableError) < 0)
5984        return -1;
5985
5986    if (PyDict_SetItemString(module_dict, "BadPickleGet",
5987                             BadPickleGet) < 0)
5988        return -1;
5989
5990    PycString_IMPORT;
5991
5992    return 0;
5993}
5994
5995#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
5996#define PyMODINIT_FUNC void
5997#endif
5998PyMODINIT_FUNC
5999initcPickle(void)
6000{
6001    PyObject *m, *d, *di, *v, *k;
6002    Py_ssize_t i;
6003    char *rev = "1.71";         /* XXX when does this change? */
6004    PyObject *format_version;
6005    PyObject *compatible_formats;
6006
6007    Py_TYPE(&Picklertype) = &PyType_Type;
6008    Py_TYPE(&Unpicklertype) = &PyType_Type;
6009    Py_TYPE(&PdataType) = &PyType_Type;
6010
6011    /* Initialize some pieces. We need to do this before module creation,
6012     * so we're forced to use a temporary dictionary. :(
6013     */
6014    di = PyDict_New();
6015    if (!di) return;
6016    if (init_stuff(di) < 0) return;
6017
6018    /* Create the module and add the functions */
6019    m = Py_InitModule4("cPickle", cPickle_methods,
6020                       cPickle_module_documentation,
6021                       (PyObject*)NULL,PYTHON_API_VERSION);
6022    if (m == NULL)
6023        return;
6024
6025    /* Add some symbolic constants to the module */
6026    d = PyModule_GetDict(m);
6027    v = PyString_FromString(rev);
6028    PyDict_SetItemString(d, "__version__", v);
6029    Py_XDECREF(v);
6030
6031    /* Copy data from di. Waaa. */
6032    for (i=0; PyDict_Next(di, &i, &k, &v); ) {
6033        if (PyObject_SetItem(d, k, v) < 0) {
6034            Py_DECREF(di);
6035            return;
6036        }
6037    }
6038    Py_DECREF(di);
6039
6040    i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
6041    if (i < 0)
6042        return;
6043
6044    /* These are purely informational; no code uses them. */
6045    /* File format version we write. */
6046    format_version = PyString_FromString("2.0");
6047    /* Format versions we can read. */
6048    compatible_formats = Py_BuildValue("[sssss]",
6049        "1.0",          /* Original protocol 0 */
6050        "1.1",          /* Protocol 0 + INST */
6051        "1.2",          /* Original protocol 1 */
6052        "1.3",          /* Protocol 1 + BINFLOAT */
6053        "2.0");         /* Original protocol 2 */
6054    PyDict_SetItemString(d, "format_version", format_version);
6055    PyDict_SetItemString(d, "compatible_formats", compatible_formats);
6056    Py_XDECREF(format_version);
6057    Py_XDECREF(compatible_formats);
6058}
6059