Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Modules/cPickle.c

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