Commit eea29ad9 authored by Jim Fulton's avatar Jim Fulton

Massive update:

  - Added new protocol for binart string pickles that
    takes out unneeded puts.

  - Change unpickler to use internal data structure instead
    of list to reduce unpickling times by about a third.

  - Many cleanups to get rid of obfuscated error handling
    involving 'goto finally' and status vars.

  - Extensive reGuidofication.

  - Fixed binary floating-point pickling bug.
parent 21f2493d
/* /*
* $Id: cPickle.c,v 1.59 1998/10/01 22:31:46 jim Exp $ * $Id: cPickle.c,v 1.60 1998/10/28 22:57:49 jim Exp $
* *
* Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA. * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
* All rights reserved. * All rights reserved.
...@@ -16,12 +16,6 @@ ...@@ -16,12 +16,6 @@
* the documentation and/or other materials provided with the * the documentation and/or other materials provided with the
* distribution. * distribution.
* *
* o All advertising materials mentioning features or use of this
* software must display the following acknowledgement:
*
* This product includes software developed by Digital Creations
* and its contributors.
*
* o Neither the name of Digital Creations nor the names of its * o Neither the name of Digital Creations nor the names of its
* contributors may be used to endorse or promote products derived * contributors may be used to endorse or promote products derived
* from this software without specific prior written permission. * from this software without specific prior written permission.
...@@ -55,7 +49,7 @@ ...@@ -55,7 +49,7 @@
static char cPickle_module_documentation[] = static char cPickle_module_documentation[] =
"C implementation and optimization of the Python pickle module\n" "C implementation and optimization of the Python pickle module\n"
"\n" "\n"
"$Id: cPickle.c,v 1.59 1998/10/01 22:31:46 jim Exp $\n" "$Id: cPickle.c,v 1.60 1998/10/28 22:57:49 jim Exp $\n"
; ;
#include "Python.h" #include "Python.h"
...@@ -123,6 +117,8 @@ static PyObject *atol_func; ...@@ -123,6 +117,8 @@ static PyObject *atol_func;
static PyObject *PicklingError; static PyObject *PicklingError;
static PyObject *UnpicklingError; static PyObject *UnpicklingError;
static PyObject *BadPickleGet;
static PyObject *dispatch_table; static PyObject *dispatch_table;
static PyObject *safe_constructors; static PyObject *safe_constructors;
...@@ -132,11 +128,184 @@ static PyObject *__class___str, *__getinitargs___str, *__dict___str, ...@@ -132,11 +128,184 @@ static PyObject *__class___str, *__getinitargs___str, *__dict___str,
*__getstate___str, *__setstate___str, *__name___str, *__reduce___str, *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
*write_str, *__safe_for_unpickling___str, *append_str, *write_str, *__safe_for_unpickling___str, *append_str,
*read_str, *readline_str, *__main___str, *__basicnew___str, *read_str, *readline_str, *__main___str, *__basicnew___str,
*copy_reg_str, *dispatch_table_str, *safe_constructors_str; *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
static int save(); static int save();
static int put2(); static int put2();
#ifndef PyList_SET_ITEM
#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
#endif
#ifndef PyList_GET_SIZE
#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
#endif
#ifndef PyTuple_SET_ITEM
#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
#endif
#ifndef PyTuple_GET_SIZE
#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
#endif
#ifndef PyString_GET_SIZE
#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
#endif
/*************************************************************************
Internal Data type for pickle data. */
typedef struct {
PyObject_HEAD
int length, size;
PyObject **data;
} Pdata;
static void
Pdata_dealloc(Pdata *self) {
int i;
PyObject **p;
for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
if (self->data) free(self->data);
PyMem_DEL(self);
}
static PyTypeObject PdataType = {
PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
(destructor)Pdata_dealloc,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
};
#define Pdata_Check(O) ((O)->ob_type == &PdataType)
static PyObject *
Pdata_New() {
Pdata *self;
UNLESS (self = PyObject_NEW(Pdata, &PdataType)) return NULL;
self->size=8;
self->length=0;
self->data=malloc(self->size * sizeof(PyObject*));
if (self->data) return (PyObject*)self;
Py_DECREF(self);
return PyErr_NoMemory();
}
static int
stackUnderflow() {
PyErr_SetString(UnpicklingError, "unpickling stack underflow");
return -1;
}
static int
Pdata_clear(Pdata *self, int clearto) {
int i;
PyObject **p;
if (clearto < 0) return stackUnderflow();
if (clearto >= self->length) return 0;
for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
Py_DECREF(*p);
self->length=clearto;
return 0;
}
static int
Pdata_grow(Pdata *self) {
if (! self->size) {
PyErr_NoMemory();
return -1;
}
self->size *= 2;
self->data = realloc(self->data, self->size*sizeof(PyObject*));
if (! self->data) {
self->size = 0;
PyErr_NoMemory();
return -1;
}
return 0;
}
#define PDATA_POP(D,V) { \
if ((D)->length) V=D->data[--((D)->length)]; \
else { \
PyErr_SetString(UnpicklingError, "bad pickle data"); \
V=NULL; \
} \
}
static PyObject *
Pdata_popTuple(Pdata *self, int start) {
PyObject *r;
int i, j, l;
l=self->length-start;
UNLESS (r=PyTuple_New(l)) return NULL;
for (i=start, j=0 ; j < l; )
PyTuple_SET_ITEM(r,j++,self->data[i++]);
self->length=start;
return r;
}
static PyObject *
Pdata_popList(Pdata *self, int start) {
PyObject *r;
int i, j, l;
l=self->length-start;
UNLESS (r=PyList_New(l)) return NULL;
for (i=start, j=0 ; j < l; )
PyList_SET_ITEM(r,j++,self->data[i++]);
self->length=start;
return r;
}
#define PDATA_APPEND_(D,O,ER) { \
if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
}
#define PDATA_APPEND(D,O,ER) { \
if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
Pdata_grow((Pdata*)(D)) < 0) \
return ER; \
Py_INCREF(O); \
((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
}
#define PDATA_PUSH(D,O,ER) { \
if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
Pdata_grow((Pdata*)(D)) < 0) { \
Py_DECREF(O); \
return ER; \
} \
((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
}
/*************************************************************************/
#define ARG_TUP(self, o) { \
if (self->arg || (self->arg=PyTuple_New(1))) { \
Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
PyTuple_SET_ITEM(self->arg,0,o); \
} \
else { \
Py_DECREF(o); \
} \
}
#define FREE_ARG_TUP(self) { \
if (self->arg->ob_refcnt > 1) { \
Py_DECREF(self->arg); \
self->arg=NULL; \
} \
}
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
FILE *fp; FILE *fp;
...@@ -164,7 +333,7 @@ typedef struct { ...@@ -164,7 +333,7 @@ typedef struct {
PyObject *read; PyObject *read;
PyObject *memo; PyObject *memo;
PyObject *arg; PyObject *arg;
PyObject *stack; Pdata *stack;
PyObject *mark; PyObject *mark;
PyObject *pers_func; PyObject *pers_func;
PyObject *last_string; PyObject *last_string;
...@@ -184,7 +353,7 @@ int ...@@ -184,7 +353,7 @@ int
cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) { cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
PyObject *v; PyObject *v;
if((v = PyObject_GetItem(o,key))) { if ((v = PyObject_GetItem(o,key))) {
Py_DECREF(v); Py_DECREF(v);
return 1; return 1;
} }
...@@ -215,23 +384,23 @@ cPickle_ErrFormat(va_alist) va_dcl { ...@@ -215,23 +384,23 @@ cPickle_ErrFormat(va_alist) va_dcl {
format = va_arg(va, char *); format = va_arg(va, char *);
#endif #endif
if(format) args = Py_VaBuildValue(format, va); if (format) args = Py_VaBuildValue(format, va);
va_end(va); va_end(va);
if(format && ! args) return NULL; if (format && ! args) return NULL;
if(stringformat && !(retval=PyString_FromString(stringformat))) return NULL; if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
if(retval) { if (retval) {
if(args) { if (args) {
PyObject *v; PyObject *v;
v=PyString_Format(retval, args); v=PyString_Format(retval, args);
Py_DECREF(retval); Py_DECREF(retval);
Py_DECREF(args); Py_DECREF(args);
if(! v) return NULL; if (! v) return NULL;
retval=v; retval=v;
} }
} }
else else
if(args) retval=args; if (args) retval=args;
else { else {
PyErr_SetObject(ErrType,Py_None); PyErr_SetObject(ErrType,Py_None);
return NULL; return NULL;
...@@ -255,7 +424,6 @@ write_file(Picklerobject *self, char *s, int n) { ...@@ -255,7 +424,6 @@ write_file(Picklerobject *self, char *s, int n) {
return n; return n;
} }
static int static int
write_cStringIO(Picklerobject *self, char *s, int n) { write_cStringIO(Picklerobject *self, char *s, int n) {
if (s == NULL) { if (s == NULL) {
...@@ -269,64 +437,55 @@ write_cStringIO(Picklerobject *self, char *s, int n) { ...@@ -269,64 +437,55 @@ write_cStringIO(Picklerobject *self, char *s, int n) {
return n; return n;
} }
static int static int
write_none(Picklerobject *self, char *s, int n) { write_none(Picklerobject *self, char *s, int n) {
if (s == NULL) return 0; if (s == NULL) return 0;
return n; return n;
} }
static int static int
write_other(Picklerobject *self, char *s, int n) { write_other(Picklerobject *self, char *s, int n) {
PyObject *py_str = 0, *junk = 0; PyObject *py_str = 0, *junk = 0;
int res = -1;
if (s == NULL) { if (s == NULL) {
UNLESS(self->buf_size) return 0; UNLESS (self->buf_size) return 0;
UNLESS(py_str = UNLESS (py_str =
PyString_FromStringAndSize(self->write_buf, self->buf_size)) PyString_FromStringAndSize(self->write_buf, self->buf_size))
goto finally; return -1;
} }
else { else {
if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) { if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
if (write_other(self, NULL, 0) < 0) if (write_other(self, NULL, 0) < 0)
goto finally; return -1;
} }
if (n > WRITE_BUF_SIZE) { if (n > WRITE_BUF_SIZE) {
UNLESS(py_str = UNLESS (py_str =
PyString_FromStringAndSize(s, n)) PyString_FromStringAndSize(s, n))
goto finally; return -1;
} }
else { else {
memcpy(self->write_buf + self->buf_size, s, n); memcpy(self->write_buf + self->buf_size, s, n);
self->buf_size += n; self->buf_size += n;
res = n; return n;
goto finally;
} }
} }
UNLESS(self->arg) if (self->write) {
UNLESS(self->arg = PyTuple_New(1)) /* object with write method */
goto finally; ARG_TUP(self, py_str);
if (self->arg) {
Py_INCREF(py_str); junk = PyObject_CallObject(self->write, self->arg);
if (PyTuple_SetItem(self->arg, 0, py_str) < 0) FREE_ARG_TUP(self);
goto finally; }
if (junk) Py_DECREF(junk);
UNLESS(junk = PyObject_CallObject(self->write, self->arg)) else return -1;
goto finally; }
Py_DECREF(junk); else
PDATA_PUSH(self->file, py_str, -1);
self->buf_size = 0; self->buf_size = 0;
return n;
res = n;
finally:
Py_XDECREF(py_str);
return res;
} }
...@@ -337,7 +496,7 @@ read_file(Unpicklerobject *self, char **s, int n) { ...@@ -337,7 +496,7 @@ read_file(Unpicklerobject *self, char **s, int n) {
int size; int size;
size = ((n < 32) ? 32 : n); size = ((n < 32) ? 32 : n);
UNLESS(self->buf = (char *)malloc(size * sizeof(char))) { UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;
} }
...@@ -345,7 +504,7 @@ read_file(Unpicklerobject *self, char **s, int n) { ...@@ -345,7 +504,7 @@ read_file(Unpicklerobject *self, char **s, int n) {
self->buf_size = size; self->buf_size = size;
} }
else if (n > self->buf_size) { else if (n > self->buf_size) {
UNLESS(self->buf = (char *)realloc(self->buf, n * sizeof(char))) { UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;
} }
...@@ -374,7 +533,7 @@ readline_file(Unpicklerobject *self, char **s) { ...@@ -374,7 +533,7 @@ readline_file(Unpicklerobject *self, char **s) {
int i; int i;
if (self->buf_size == 0) { if (self->buf_size == 0) {
UNLESS(self->buf = (char *)malloc(40 * sizeof(char))) { UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;
} }
...@@ -392,7 +551,7 @@ readline_file(Unpicklerobject *self, char **s) { ...@@ -392,7 +551,7 @@ readline_file(Unpicklerobject *self, char **s) {
} }
} }
UNLESS(self->buf = (char *)realloc(self->buf, UNLESS (self->buf = (char *)realloc(self->buf,
(self->buf_size * 2) * sizeof(char))) { (self->buf_size * 2) * sizeof(char))) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;
...@@ -436,38 +595,23 @@ readline_cStringIO(Unpicklerobject *self, char **s) { ...@@ -436,38 +595,23 @@ readline_cStringIO(Unpicklerobject *self, char **s) {
static int static int
read_other(Unpicklerobject *self, char **s, int n) { read_other(Unpicklerobject *self, char **s, int n) {
PyObject *bytes, *str; PyObject *bytes, *str=0;
int res = -1; int res = -1;
UNLESS(bytes = PyInt_FromLong(n)) { UNLESS (bytes = PyInt_FromLong(n)) return -1;
if (!PyErr_Occurred())
PyErr_SetNone(PyExc_EOFError);
goto finally; ARG_TUP(self, bytes);
if (self->arg) {
str = PyObject_CallObject(self->read, self->arg);
FREE_ARG_TUP(self);
} }
if (! str) return -1;
UNLESS(self->arg)
UNLESS(self->arg = PyTuple_New(1))
goto finally;
Py_INCREF(bytes);
if (PyTuple_SetItem(self->arg, 0, bytes) < 0)
goto finally;
UNLESS(str = PyObject_CallObject(self->read, self->arg))
goto finally;
Py_XDECREF(self->last_string); Py_XDECREF(self->last_string);
self->last_string = str; self->last_string = str;
*s = PyString_AsString(str); if (! (*s = PyString_AsString(str))) return -1;
return n;
res = n;
finally:
Py_XDECREF(bytes);
return res;
} }
...@@ -476,16 +620,18 @@ readline_other(Unpicklerobject *self, char **s) { ...@@ -476,16 +620,18 @@ readline_other(Unpicklerobject *self, char **s) {
PyObject *str; PyObject *str;
int str_size; int str_size;
UNLESS(str = PyObject_CallObject(self->readline, empty_tuple)) { UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
return -1; return -1;
} }
str_size = PyString_Size(str); if ((str_size = PyString_Size(str)) < 0)
return -1;
Py_XDECREF(self->last_string); Py_XDECREF(self->last_string);
self->last_string = str; self->last_string = str;
*s = PyString_AsString(str); if (! (*s = PyString_AsString(str)))
return -1;
return str_size; return str_size;
} }
...@@ -494,7 +640,7 @@ readline_other(Unpicklerobject *self, char **s) { ...@@ -494,7 +640,7 @@ readline_other(Unpicklerobject *self, char **s) {
static char * static char *
pystrndup(char *s, int l) { pystrndup(char *s, int l) {
char *r; char *r;
UNLESS(r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory(); UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
memcpy(r,s,l); memcpy(r,s,l);
r[l]=0; r[l]=0;
return r; return r;
...@@ -503,26 +649,35 @@ pystrndup(char *s, int l) { ...@@ -503,26 +649,35 @@ pystrndup(char *s, int l) {
static int static int
get(Picklerobject *self, PyObject *id) { get(Picklerobject *self, PyObject *id) {
PyObject *value = 0; PyObject *value, *mv;
long c_value; long c_value;
char s[30]; char s[30];
int len; int len;
UNLESS(value = PyDict_GetItem(self->memo, id)) { UNLESS (mv = PyDict_GetItem(self->memo, id)) {
PyErr_SetObject(PyExc_KeyError, id); PyErr_SetObject(PyExc_KeyError, id);
return -1; return -1;
} }
UNLESS(value = PyTuple_GetItem(value, 0)) UNLESS (value = PyTuple_GetItem(mv, 0))
return -1; return -1;
c_value = PyInt_AsLong(value); UNLESS (PyInt_Check(value)) {
PyErr_SetString(PicklingError, "no int where int expected in memo");
return -1;
}
c_value = PyInt_AS_LONG((PyIntObject*)value);
if (!self->bin) { if (!self->bin) {
s[0] = GET; s[0] = GET;
sprintf(s + 1, "%ld\n", c_value); sprintf(s + 1, "%ld\n", c_value);
len = strlen(s); len = strlen(s);
} }
else if (Pdata_Check(self->file)) {
if (write_other(self, NULL, 0) < 0) return -1;
PDATA_APPEND(self->file, mv, -1);
return 0;
}
else { else {
if (c_value < 256) { if (c_value < 256) {
s[0] = BINGET; s[0] = BINGET;
...@@ -566,11 +721,36 @@ put2(Picklerobject *self, PyObject *ob) { ...@@ -566,11 +721,36 @@ put2(Picklerobject *self, PyObject *ob) {
if ((p = PyDict_Size(self->memo)) < 0) if ((p = PyDict_Size(self->memo)) < 0)
goto finally; goto finally;
p++; /* Make sure memo keys are positive! */
UNLESS (py_ob_id = PyInt_FromLong((long)ob))
goto finally;
UNLESS (memo_len = PyInt_FromLong(p))
goto finally;
UNLESS (t = PyTuple_New(2))
goto finally;
PyTuple_SET_ITEM(t, 0, memo_len);
Py_INCREF(memo_len);
PyTuple_SET_ITEM(t, 1, ob);
Py_INCREF(ob);
if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
goto finally;
if (!self->bin) { if (!self->bin) {
c_str[0] = PUT; c_str[0] = PUT;
sprintf(c_str + 1, "%d\n", p); sprintf(c_str + 1, "%d\n", p);
len = strlen(c_str); len = strlen(c_str);
} }
else if (Pdata_Check(self->file)) {
if (write_other(self, NULL, 0) < 0) return -1;
PDATA_APPEND(self->file, memo_len, -1);
res=0; /* Job well done ;) */
goto finally;
}
else { else {
if (p >= 256) { if (p >= 256) {
c_str[0] = LONG_BINPUT; c_str[0] = LONG_BINPUT;
...@@ -590,23 +770,6 @@ put2(Picklerobject *self, PyObject *ob) { ...@@ -590,23 +770,6 @@ put2(Picklerobject *self, PyObject *ob) {
if ((*self->write_func)(self, c_str, len) < 0) if ((*self->write_func)(self, c_str, len) < 0)
goto finally; goto finally;
UNLESS(py_ob_id = PyInt_FromLong((long)ob))
goto finally;
UNLESS(memo_len = PyInt_FromLong(p))
goto finally;
UNLESS(t = PyTuple_New(2))
goto finally;
PyTuple_SET_ITEM(t, 0, memo_len);
Py_INCREF(memo_len);
PyTuple_SET_ITEM(t, 1, ob);
Py_INCREF(ob);
if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
goto finally;
res = 0; res = 0;
finally: finally:
...@@ -625,37 +788,41 @@ PyImport_Import(PyObject *module_name) { ...@@ -625,37 +788,41 @@ PyImport_Import(PyObject *module_name) {
static PyObject *standard_builtins=0; static PyObject *standard_builtins=0;
PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0; PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
UNLESS(silly_list) { UNLESS (silly_list) {
UNLESS(__import___str=PyString_FromString("__import__")) return NULL; UNLESS (__import___str=PyString_FromString("__import__"))
UNLESS(__builtins___str=PyString_FromString("__builtins__")) return NULL; return NULL;
UNLESS(silly_list=Py_BuildValue("[s]","__doc__")) return NULL; UNLESS (__builtins___str=PyString_FromString("__builtins__"))
return NULL;
UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
return NULL;
} }
if((globals=PyEval_GetGlobals())) { if ((globals=PyEval_GetGlobals())) {
Py_INCREF(globals); Py_INCREF(globals);
UNLESS(__builtins__=PyObject_GetItem(globals,__builtins___str)) goto err; UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
goto err;
} }
else { else {
PyErr_Clear(); PyErr_Clear();
UNLESS(standard_builtins || UNLESS (standard_builtins ||
(standard_builtins=PyImport_ImportModule("__builtin__"))) (standard_builtins=PyImport_ImportModule("__builtin__")))
return NULL; return NULL;
__builtins__=standard_builtins; __builtins__=standard_builtins;
Py_INCREF(__builtins__); Py_INCREF(__builtins__);
UNLESS(globals = Py_BuildValue("{sO}", "__builtins__", __builtins__)) UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
goto err; goto err;
} }
if(PyDict_Check(__builtins__)) { if (PyDict_Check(__builtins__)) {
UNLESS(__import__=PyObject_GetItem(__builtins__,__import___str)) goto err; UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
} }
else { else {
UNLESS(__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err; UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
} }
UNLESS(r=PyObject_CallFunction(__import__,"OOOO", UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
module_name, globals, globals, silly_list)) module_name, globals, globals, silly_list))
goto err; goto err;
...@@ -681,15 +848,15 @@ whichmodule(PyObject *global, PyObject *global_name) { ...@@ -681,15 +848,15 @@ whichmodule(PyObject *global, PyObject *global_name) {
if (module) return module; if (module) return module;
PyErr_Clear(); PyErr_Clear();
UNLESS(modules_dict = PySys_GetObject("modules")) UNLESS (modules_dict = PySys_GetObject("modules"))
return NULL; return NULL;
i = 0; i = 0;
while ((j = PyDict_Next(modules_dict, &i, &name, &module))) { while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
if(PyObject_Compare(name, __main___str)==0) continue; if (PyObject_Compare(name, __main___str)==0) continue;
UNLESS(global_name_attr = PyObject_GetAttr(module, global_name)) { UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
PyErr_Clear(); PyErr_Clear();
continue; continue;
} }
...@@ -708,7 +875,7 @@ whichmodule(PyObject *global, PyObject *global_name) { ...@@ -708,7 +875,7 @@ whichmodule(PyObject *global, PyObject *global_name) {
that used __main__ if no module is found. I don't actually that used __main__ if no module is found. I don't actually
like this rule. jlf like this rule. jlf
*/ */
if(!j) { if (!j) {
j=1; j=1;
name=__main___str; name=__main___str;
} }
...@@ -783,7 +950,7 @@ save_long(Picklerobject *self, PyObject *args) { ...@@ -783,7 +950,7 @@ save_long(Picklerobject *self, PyObject *args) {
static char l = LONG; static char l = LONG;
UNLESS(repr = PyObject_Repr(args)) UNLESS (repr = PyObject_Repr(args))
goto finally; goto finally;
if ((size = PyString_Size(repr)) < 0) if ((size = PyString_Size(repr)) < 0)
...@@ -812,7 +979,6 @@ static int ...@@ -812,7 +979,6 @@ static int
save_float(Picklerobject *self, PyObject *args) { save_float(Picklerobject *self, PyObject *args) {
double x = PyFloat_AS_DOUBLE((PyFloatObject *)args); double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
#ifdef FORMAT_1_3
if (self->bin) { if (self->bin) {
int s, e; int s, e;
double f; double f;
...@@ -856,7 +1022,7 @@ save_float(Picklerobject *self, PyObject *args) { ...@@ -856,7 +1022,7 @@ save_float(Picklerobject *self, PyObject *args) {
f = ldexp(f, 1022 + e); f = ldexp(f, 1022 + e);
e = 0; e = 0;
} }
else { else if (!(e == 0 && f == 0.0)) {
e += 1023; e += 1023;
f -= 1.0; /* Get rid of leading 1 */ f -= 1.0; /* Get rid of leading 1 */
} }
...@@ -902,9 +1068,7 @@ save_float(Picklerobject *self, PyObject *args) { ...@@ -902,9 +1068,7 @@ save_float(Picklerobject *self, PyObject *args) {
if ((*self->write_func)(self, str, 9) < 0) if ((*self->write_func)(self, str, 9) < 0)
return -1; return -1;
} }
else else {
#endif
{
char c_str[250]; char c_str[250];
c_str[0] = FLOAT; c_str[0] = FLOAT;
sprintf(c_str + 1, "%.17g\n", x); sprintf(c_str + 1, "%.17g\n", x);
...@@ -920,29 +1084,31 @@ save_float(Picklerobject *self, PyObject *args) { ...@@ -920,29 +1084,31 @@ save_float(Picklerobject *self, PyObject *args) {
static int static int
save_string(Picklerobject *self, PyObject *args, int doput) { save_string(Picklerobject *self, PyObject *args, int doput) {
int size, len; int size, len;
PyObject *repr=0;
size = PyString_Size(args); if ((size = PyString_Size(args)) < 0)
return -1;
if (!self->bin) { if (!self->bin) {
PyObject *repr;
char *repr_str; char *repr_str;
static char string = STRING; static char string = STRING;
UNLESS(repr = PyObject_Repr(args)) UNLESS (repr = PyObject_Repr(args))
return -1; return -1;
if ((len = PyString_Size(repr)) < 0)
goto err;
repr_str = PyString_AS_STRING((PyStringObject *)repr); repr_str = PyString_AS_STRING((PyStringObject *)repr);
len = PyString_Size(repr);
if ((*self->write_func)(self, &string, 1) < 0) if ((*self->write_func)(self, &string, 1) < 0)
return -1; goto err;
if ((*self->write_func)(self, repr_str, len) < 0) if ((*self->write_func)(self, repr_str, len) < 0)
return -1; goto err;
if ((*self->write_func)(self, "\n", 1) < 0) if ((*self->write_func)(self, "\n", 1) < 0)
return -1; goto err;
Py_XDECREF(repr); Py_XDECREF(repr);
} }
...@@ -950,7 +1116,8 @@ save_string(Picklerobject *self, PyObject *args, int doput) { ...@@ -950,7 +1116,8 @@ save_string(Picklerobject *self, PyObject *args, int doput) {
int i; int i;
char c_str[5]; char c_str[5];
size = PyString_Size(args); if ((size = PyString_Size(args)) < 0)
return -1;
if (size < 256) { if (size < 256) {
c_str[0] = SHORT_BINSTRING; c_str[0] = SHORT_BINSTRING;
...@@ -967,16 +1134,26 @@ save_string(Picklerobject *self, PyObject *args, int doput) { ...@@ -967,16 +1134,26 @@ save_string(Picklerobject *self, PyObject *args, int doput) {
if ((*self->write_func)(self, c_str, len) < 0) if ((*self->write_func)(self, c_str, len) < 0)
return -1; return -1;
if (size > 128 && Pdata_Check(self->file)) {
if (write_other(self, NULL, 0) < 0) return -1;
PDATA_APPEND(self->file, args, -1);
}
else {
if ((*self->write_func)(self, if ((*self->write_func)(self,
PyString_AS_STRING((PyStringObject *)args), size) < 0) PyString_AS_STRING((PyStringObject *)args), size) < 0)
return -1; return -1;
} }
}
if (doput) if (doput)
if (put(self, args) < 0) if (put(self, args) < 0)
return -1; return -1;
return 0; return 0;
err:
Py_XDECREF(repr);
return -1;
} }
...@@ -994,14 +1171,14 @@ save_tuple(Picklerobject *self, PyObject *args) { ...@@ -994,14 +1171,14 @@ save_tuple(Picklerobject *self, PyObject *args) {
goto finally; goto finally;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
UNLESS(element = PyTuple_GET_ITEM((PyTupleObject *)args, i)) UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
goto finally; goto finally;
if (save(self, element, 0) < 0) if (save(self, element, 0) < 0)
goto finally; goto finally;
} }
UNLESS(py_tuple_id = PyInt_FromLong((long)args)) UNLESS (py_tuple_id = PyInt_FromLong((long)args))
goto finally; goto finally;
if (len) { if (len) {
...@@ -1063,7 +1240,7 @@ save_list(Picklerobject *self, PyObject *args) { ...@@ -1063,7 +1240,7 @@ save_list(Picklerobject *self, PyObject *args) {
static char append = APPEND, appends = APPENDS; static char append = APPEND, appends = APPENDS;
if(self->bin) { if (self->bin) {
s[0] = EMPTY_LIST; s[0] = EMPTY_LIST;
s_len = 1; s_len = 1;
} }
...@@ -1093,7 +1270,7 @@ save_list(Picklerobject *self, PyObject *args) { ...@@ -1093,7 +1270,7 @@ save_list(Picklerobject *self, PyObject *args) {
goto finally; goto finally;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i)) UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
goto finally; goto finally;
if (save(self, element, 0) < 0) if (save(self, element, 0) < 0)
...@@ -1194,7 +1371,7 @@ save_inst(Picklerobject *self, PyObject *args) { ...@@ -1194,7 +1371,7 @@ save_inst(Picklerobject *self, PyObject *args) {
if ((*self->write_func)(self, &MARKv, 1) < 0) if ((*self->write_func)(self, &MARKv, 1) < 0)
goto finally; goto finally;
UNLESS(class = PyObject_GetAttr(args, __class___str)) UNLESS (class = PyObject_GetAttr(args, __class___str))
goto finally; goto finally;
if (self->bin) { if (self->bin) {
...@@ -1206,7 +1383,7 @@ save_inst(Picklerobject *self, PyObject *args) { ...@@ -1206,7 +1383,7 @@ save_inst(Picklerobject *self, PyObject *args) {
PyObject *element = 0; PyObject *element = 0;
int i, len; int i, len;
UNLESS(class_args = UNLESS (class_args =
PyObject_CallObject(getinitargs_func, empty_tuple)) PyObject_CallObject(getinitargs_func, empty_tuple))
goto finally; goto finally;
...@@ -1214,7 +1391,7 @@ save_inst(Picklerobject *self, PyObject *args) { ...@@ -1214,7 +1391,7 @@ save_inst(Picklerobject *self, PyObject *args) {
goto finally; goto finally;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
UNLESS(element = PySequence_GetItem(class_args, i)) UNLESS (element = PySequence_GetItem(class_args, i))
goto finally; goto finally;
if (save(self, element, 0) < 0) { if (save(self, element, 0) < 0) {
...@@ -1230,18 +1407,21 @@ save_inst(Picklerobject *self, PyObject *args) { ...@@ -1230,18 +1407,21 @@ save_inst(Picklerobject *self, PyObject *args) {
} }
if (!self->bin) { if (!self->bin) {
UNLESS(name = ((PyClassObject *)class)->cl_name) { UNLESS (name = ((PyClassObject *)class)->cl_name) {
PyErr_SetString(PicklingError, "class has no name"); PyErr_SetString(PicklingError, "class has no name");
goto finally; goto finally;
} }
UNLESS(module = whichmodule(class, name)) UNLESS (module = whichmodule(class, name))
goto finally;
if ((module_size = PyString_Size(module)) < 0 ||
(name_size = PyString_Size(name)) < 0)
goto finally; goto finally;
module_str = PyString_AS_STRING((PyStringObject *)module); module_str = PyString_AS_STRING((PyStringObject *)module);
module_size = PyString_Size(module);
name_str = PyString_AS_STRING((PyStringObject *)name); name_str = PyString_AS_STRING((PyStringObject *)name);
name_size = PyString_Size(name);
if ((*self->write_func)(self, &inst, 1) < 0) if ((*self->write_func)(self, &inst, 1) < 0)
goto finally; goto finally;
...@@ -1263,13 +1443,13 @@ save_inst(Picklerobject *self, PyObject *args) { ...@@ -1263,13 +1443,13 @@ save_inst(Picklerobject *self, PyObject *args) {
} }
if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) { if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
UNLESS(state = PyObject_CallObject(getstate_func, empty_tuple)) UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
goto finally; goto finally;
} }
else { else {
PyErr_Clear(); PyErr_Clear();
UNLESS(state = PyObject_GetAttr(args, __dict___str)) { UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
PyErr_Clear(); PyErr_Clear();
res = 0; res = 0;
goto finally; goto finally;
...@@ -1318,17 +1498,19 @@ save_global(Picklerobject *self, PyObject *args, PyObject *name) { ...@@ -1318,17 +1498,19 @@ save_global(Picklerobject *self, PyObject *args, PyObject *name) {
Py_INCREF(global_name); Py_INCREF(global_name);
} }
else { else {
UNLESS(global_name = PyObject_GetAttr(args, __name___str)) UNLESS (global_name = PyObject_GetAttr(args, __name___str))
goto finally; goto finally;
} }
UNLESS(module = whichmodule(args, global_name)) UNLESS (module = whichmodule(args, global_name))
goto finally;
if ((module_size = PyString_Size(module)) < 0 ||
(name_size = PyString_Size(global_name)) < 0)
goto finally; goto finally;
module_str = PyString_AS_STRING((PyStringObject *)module); module_str = PyString_AS_STRING((PyStringObject *)module);
module_size = PyString_Size(module);
name_str = PyString_AS_STRING((PyStringObject *)global_name); name_str = PyString_AS_STRING((PyStringObject *)global_name);
name_size = PyString_Size(global_name);
if ((*self->write_func)(self, &global, 1) < 0) if ((*self->write_func)(self, &global, 1) < 0)
goto finally; goto finally;
...@@ -1364,16 +1546,13 @@ save_pers(Picklerobject *self, PyObject *args, PyObject *f) { ...@@ -1364,16 +1546,13 @@ save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
static char persid = PERSID, binpersid = BINPERSID; static char persid = PERSID, binpersid = BINPERSID;
UNLESS(self->arg)
UNLESS(self->arg = PyTuple_New(1))
goto finally;
Py_INCREF(args); Py_INCREF(args);
if (PyTuple_SetItem(self->arg, 0, args) < 0) ARG_TUP(self, args);
goto finally; if (self->arg) {
pid = PyObject_CallObject(f, self->arg);
UNLESS(pid = PyObject_CallObject(f, self->arg)) FREE_ARG_TUP(self);
goto finally; }
if (! pid) return -1;
if (pid != Py_None) { if (pid != Py_None) {
if (!self->bin) { if (!self->bin) {
...@@ -1499,13 +1678,14 @@ save(Picklerobject *self, PyObject *args, int pers_save) { ...@@ -1499,13 +1678,14 @@ save(Picklerobject *self, PyObject *args, int pers_save) {
case 't': case 't':
if (type == &PyTuple_Type && PyTuple_Size(args)==0) { if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
if(self->bin) res = save_empty_tuple(self, args); if (self->bin) res = save_empty_tuple(self, args);
else res = save_tuple(self, args); else res = save_tuple(self, args);
goto finally; goto finally;
} }
break;
case 's': case 's':
if ((type == &PyString_Type) && (PyString_Size(args) < 2)) { if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
res = save_string(self, args, 0); res = save_string(self, args, 0);
goto finally; goto finally;
} }
...@@ -1517,7 +1697,7 @@ save(Picklerobject *self, PyObject *args, int pers_save) { ...@@ -1517,7 +1697,7 @@ save(Picklerobject *self, PyObject *args, int pers_save) {
ob_id = (long)args; ob_id = (long)args;
UNLESS(py_ob_id = PyInt_FromLong(ob_id)) UNLESS (py_ob_id = PyInt_FromLong(ob_id))
goto finally; goto finally;
if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0) if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
...@@ -1538,42 +1718,49 @@ save(Picklerobject *self, PyObject *args, int pers_save) { ...@@ -1538,42 +1718,49 @@ save(Picklerobject *self, PyObject *args, int pers_save) {
res = save_string(self, args, 1); res = save_string(self, args, 1);
goto finally; goto finally;
} }
break;
case 't': case 't':
if (type == &PyTuple_Type) { if (type == &PyTuple_Type) {
res = save_tuple(self, args); res = save_tuple(self, args);
goto finally; goto finally;
} }
break;
case 'l': case 'l':
if (type == &PyList_Type) { if (type == &PyList_Type) {
res = save_list(self, args); res = save_list(self, args);
goto finally; goto finally;
} }
break;
case 'd': case 'd':
if (type == &PyDict_Type) { if (type == &PyDict_Type) {
res = save_dict(self, args); res = save_dict(self, args);
goto finally; goto finally;
} }
break;
case 'i': case 'i':
if (type == &PyInstance_Type) { if (type == &PyInstance_Type) {
res = save_inst(self, args); res = save_inst(self, args);
goto finally; goto finally;
} }
break;
case 'c': case 'c':
if (type == &PyClass_Type) { if (type == &PyClass_Type) {
res = save_global(self, args, NULL); res = save_global(self, args, NULL);
goto finally; goto finally;
} }
break;
case 'f': case 'f':
if (type == &PyFunction_Type) { if (type == &PyFunction_Type) {
res = save_global(self, args, NULL); res = save_global(self, args, NULL);
goto finally; goto finally;
} }
break;
case 'b': case 'b':
if (type == &PyCFunction_Type) { if (type == &PyCFunction_Type) {
...@@ -1592,22 +1779,19 @@ save(Picklerobject *self, PyObject *args, int pers_save) { ...@@ -1592,22 +1779,19 @@ save(Picklerobject *self, PyObject *args, int pers_save) {
if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) { if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Py_INCREF(__reduce__); Py_INCREF(__reduce__);
UNLESS(self->arg)
UNLESS(self->arg = PyTuple_New(1))
goto finally;
Py_INCREF(args); Py_INCREF(args);
if (PyTuple_SetItem(self->arg, 0, args) < 0) ARG_TUP(self, args);
goto finally; if (self->arg) {
t = PyObject_CallObject(__reduce__, self->arg);
UNLESS(t = PyObject_CallObject(__reduce__, self->arg)) FREE_ARG_TUP(self);
goto finally; }
if (! t) goto finally;
} }
else { else {
PyErr_Clear(); PyErr_Clear();
if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) { if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
UNLESS(t = PyObject_CallObject(__reduce__, empty_tuple)) UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
goto finally; goto finally;
} }
else { else {
...@@ -1643,7 +1827,7 @@ save(Picklerobject *self, PyObject *args, int pers_save) { ...@@ -1643,7 +1827,7 @@ save(Picklerobject *self, PyObject *args, int pers_save) {
state = PyTuple_GET_ITEM(t, 2); state = PyTuple_GET_ITEM(t, 2);
} }
UNLESS(PyTuple_Check(arg_tup) || arg_tup==Py_None) { UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
cPickle_ErrFormat(PicklingError, "Second element of tuple " cPickle_ErrFormat(PicklingError, "Second element of tuple "
"returned by %s must be a tuple", "O", __reduce__); "returned by %s must be a tuple", "O", __reduce__);
goto finally; goto finally;
...@@ -1653,13 +1837,6 @@ save(Picklerobject *self, PyObject *args, int pers_save) { ...@@ -1653,13 +1837,6 @@ save(Picklerobject *self, PyObject *args, int pers_save) {
goto finally; goto finally;
} }
/*
if (PyObject_HasAttrString(args, "__class__")) {
res = save_inst(self, args);
goto finally;
}
*/
cPickle_ErrFormat(PicklingError, "Cannot pickle %s objects.", cPickle_ErrFormat(PicklingError, "Cannot pickle %s objects.",
"O", (PyObject *)type); "O", (PyObject *)type);
...@@ -1689,64 +1866,175 @@ dump(Picklerobject *self, PyObject *args) { ...@@ -1689,64 +1866,175 @@ dump(Picklerobject *self, PyObject *args) {
} }
static PyObject * static PyObject *
Pickler_dump(Picklerobject *self, PyObject *args) { Pickle_clear_memo(Picklerobject *self, PyObject *args) {
PyObject *ob; if (args && ! PyArg_ParseTuple(args,"")) return NULL;
if (self->memo) PyDict_Clear(self->memo);
UNLESS(PyArg_ParseTuple(args, "O", &ob))
return NULL;
if (dump(self, ob) < 0)
return NULL;
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
static PyObject * static PyObject *
dump_special(Picklerobject *self, PyObject *args) { Pickle_getvalue(Picklerobject *self, PyObject *args) {
static char stop = STOP; int l, i, rsize, ssize, clear=1, lm;
long ik, id;
PyObject *k, *r;
char *s, *p, *have_get;
Pdata *data;
PyObject *callable, *arg_tup, *state = NULL; if (args && ! PyArg_ParseTuple(args,"|i",&clear)) return NULL;
UNLESS(PyArg_ParseTuple(args, "OO|O", &callable, &arg_tup, &state)) /* Check to make sure we are based on a list */
if (! Pdata_Check(self->file)) {
PyErr_SetString(PicklingError,
"Attempt to getvalue a non-list-based pickler");
return NULL; return NULL;
}
UNLESS(PyTuple_Check(arg_tup)) { /* flush write buffer */
PyErr_SetString(PicklingError, "Second arg to dump_special must " if (write_other(self, NULL, 0) < 0) return NULL;
"be tuple");
return NULL; data=(Pdata*)self->file;
l=data->length;
/* set up an array to hold get/put status */
if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
lm++;
if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
memset(have_get,0,lm);
/* Scan for gets. */
for (rsize=0, i=l; --i >= 0; ) {
k=data->data[i];
if (PyString_Check(k)) {
rsize += PyString_GET_SIZE(k);
} }
if (save_reduce(self, callable, arg_tup, state, NULL) < 0) else if (PyInt_Check(k)) { /* put */
ik=PyInt_AS_LONG((PyIntObject*)k);
if (ik >= lm || ik==0) {
PyErr_SetString(PicklingError,
"Invalid get data");
return NULL; return NULL;
}
if (have_get[ik]) { /* with matching get */
if (ik < 256) rsize += 2;
else rsize+=5;
}
}
if ((*self->write_func)(self, &stop, 1) < 0) else if (! (PyTuple_Check(k) &&
PyTuple_GET_SIZE(k) == 2 &&
PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
) {
PyErr_SetString(PicklingError,
"Unexpected data in internal list");
return NULL; return NULL;
}
if ((*self->write_func)(self, NULL, 0) < 0) else { /* put */
ik=PyInt_AS_LONG((PyIntObject*)k);
if (ik >= lm || ik==0) {
PyErr_SetString(PicklingError,
"Invalid get data");
return NULL; return NULL;
}
have_get[ik]=1;
if (ik < 256) rsize += 2;
else rsize+=5;
}
Py_INCREF(Py_None); }
return Py_None;
}
static PyObject * /* Now generate the result */
Pickle_clear_memo(Picklerobject *self, PyObject *args) { UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
if(self->memo) PyDict_Clear(self->memo); s=PyString_AS_STRING((PyStringObject*)r);
Py_INCREF(Py_None);
return Py_None;
}
static struct PyMethodDef Pickler_methods[] = { for (i=0; i<l; i++) {
{"dump", (PyCFunction)Pickler_dump, 1, k=data->data[i];
"dump(object) --"
"Write an object in pickle format to the object's pickle stream\n" if (PyString_Check(k)) {
}, ssize=PyString_GET_SIZE(k);
{"dump_special", (PyCFunction)dump_special, 1, if (ssize) {
""}, p=PyString_AS_STRING((PyStringObject*)k);
{"clear_memo", (PyCFunction)Pickle_clear_memo, 1, while (--ssize >= 0) *s++=*p++;
"clear_memo() -- Clear the picklers memo"}, }
}
else if (PyTuple_Check(k)) { /* get */
ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
if (ik < 256) {
*s++ = BINGET;
*s++ = (int)(ik & 0xff);
}
else {
*s++ = LONG_BINGET;
*s++ = (int)(ik & 0xff);
*s++ = (int)((ik >> 8) & 0xff);
*s++ = (int)((ik >> 16) & 0xff);
*s++ = (int)((ik >> 24) & 0xff);
}
}
else { /* put */
ik=PyInt_AS_LONG((PyIntObject*)k);
if (have_get[ik]) { /* with matching get */
if (ik < 256) {
*s++ = BINPUT;
*s++ = (int)(ik & 0xff);
}
else {
*s++ = LONG_BINPUT;
*s++ = (int)(ik & 0xff);
*s++ = (int)((ik >> 8) & 0xff);
*s++ = (int)((ik >> 16) & 0xff);
*s++ = (int)((ik >> 24) & 0xff);
}
}
}
}
if (clear) {
PyDict_Clear(self->memo);
Pdata_clear(data,0);
}
free(have_get);
return r;
err:
free(have_get);
return NULL;
}
static PyObject *
Pickler_dump(Picklerobject *self, PyObject *args) {
PyObject *ob;
int get=0;
UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &get))
return NULL;
if (dump(self, ob) < 0)
return NULL;
if (get) return Pickle_getvalue(self, NULL);
Py_INCREF(self);
return (PyObject*)self;
}
static struct PyMethodDef Pickler_methods[] = {
{"dump", (PyCFunction)Pickler_dump, 1,
"dump(object) --"
"Write an object in pickle format to the object's pickle stream\n"
},
{"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
"clear_memo() -- Clear the picklers memo"},
{"getvalue", (PyCFunction)Pickle_getvalue, 1,
"getvalue() -- Finish picking a list-based pickle"},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
...@@ -1755,7 +2043,7 @@ static Picklerobject * ...@@ -1755,7 +2043,7 @@ static Picklerobject *
newPicklerobject(PyObject *file, int bin) { newPicklerobject(PyObject *file, int bin) {
Picklerobject *self; Picklerobject *self;
UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype)) UNLESS (self = PyObject_NEW(Picklerobject, &Picklertype))
return NULL; return NULL;
self->fp = NULL; self->fp = NULL;
...@@ -1770,10 +2058,14 @@ newPicklerobject(PyObject *file, int bin) { ...@@ -1770,10 +2058,14 @@ newPicklerobject(PyObject *file, int bin) {
self->buf_size = 0; self->buf_size = 0;
self->dispatch_table = NULL; self->dispatch_table = NULL;
if (file)
Py_INCREF(file); Py_INCREF(file);
else
file=Pdata_New(0);
self->file = file; self->file = file;
UNLESS(self->memo = PyDict_New()) { UNLESS (self->memo = PyDict_New()) {
Py_XDECREF((PyObject *)self); Py_XDECREF((PyObject *)self);
return NULL; return NULL;
} }
...@@ -1791,28 +2083,30 @@ newPicklerobject(PyObject *file, int bin) { ...@@ -1791,28 +2083,30 @@ newPicklerobject(PyObject *file, int bin) {
else { else {
self->write_func = write_other; self->write_func = write_other;
UNLESS(self->write = PyObject_GetAttr(file, write_str)) { if (! Pdata_Check(file)) {
UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
PyErr_Clear(); PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "argument must have 'write' " PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
"attribute"); "attribute");
goto err; goto err;
} }
}
UNLESS(self->write_buf = UNLESS (self->write_buf =
(char *)malloc(WRITE_BUF_SIZE * sizeof(char))) { (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
PyErr_NoMemory(); PyErr_NoMemory();
goto err; goto err;
} }
} }
if(PyEval_GetRestricted()) { if (PyEval_GetRestricted()) {
/* Restricted execution, get private tables */ /* Restricted execution, get private tables */
PyObject *m; PyObject *m;
UNLESS(m=PyImport_Import(copy_reg_str)) goto err; UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str); self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
Py_DECREF(m); Py_DECREF(m);
UNLESS(self->dispatch_table) goto err; UNLESS (self->dispatch_table) goto err;
} }
else { else {
self->dispatch_table=dispatch_table; self->dispatch_table=dispatch_table;
...@@ -1829,10 +2123,16 @@ err: ...@@ -1829,10 +2123,16 @@ err:
static PyObject * static PyObject *
get_Pickler(PyObject *self, PyObject *args) { get_Pickler(PyObject *self, PyObject *args) {
PyObject *file; PyObject *file=NULL;
int bin = 0; int bin;
UNLESS(PyArg_ParseTuple(args, "O|i", &file, &bin)) return NULL; bin=1;
if (! PyArg_ParseTuple(args, "|i", &bin)) {
PyErr_Clear();
bin=0;
if (! PyArg_ParseTuple(args, "O|i", &file, &bin))
return NULL;
}
return (PyObject *)newPicklerobject(file, bin); return (PyObject *)newPicklerobject(file, bin);
} }
...@@ -1857,6 +2157,9 @@ Pickler_dealloc(Picklerobject *self) { ...@@ -1857,6 +2157,9 @@ Pickler_dealloc(Picklerobject *self) {
static PyObject * static PyObject *
Pickler_getattr(Picklerobject *self, char *name) { Pickler_getattr(Picklerobject *self, char *name) {
switch (*name) {
case 'p':
if (strcmp(name, "persistent_id") == 0) { if (strcmp(name, "persistent_id") == 0) {
if (!self->pers_func) { if (!self->pers_func) {
PyErr_SetString(PyExc_AttributeError, name); PyErr_SetString(PyExc_AttributeError, name);
...@@ -1866,7 +2169,8 @@ Pickler_getattr(Picklerobject *self, char *name) { ...@@ -1866,7 +2169,8 @@ Pickler_getattr(Picklerobject *self, char *name) {
Py_INCREF(self->pers_func); Py_INCREF(self->pers_func);
return self->pers_func; return self->pers_func;
} }
break;
case 'm':
if (strcmp(name, "memo") == 0) { if (strcmp(name, "memo") == 0) {
if (!self->memo) { if (!self->memo) {
PyErr_SetString(PyExc_AttributeError, name); PyErr_SetString(PyExc_AttributeError, name);
...@@ -1876,18 +2180,28 @@ Pickler_getattr(Picklerobject *self, char *name) { ...@@ -1876,18 +2180,28 @@ Pickler_getattr(Picklerobject *self, char *name) {
Py_INCREF(self->memo); Py_INCREF(self->memo);
return self->memo; return self->memo;
} }
break;
case 'P':
if (strcmp(name, "PicklingError") == 0) { if (strcmp(name, "PicklingError") == 0) {
Py_INCREF(PicklingError); Py_INCREF(PicklingError);
return PicklingError; return PicklingError;
} }
break;
if(strcmp(name, "binary")==0) case 'b':
if (strcmp(name, "binary")==0)
return PyInt_FromLong(self->bin); return PyInt_FromLong(self->bin);
break;
if(strcmp(name, "fast")==0) case 'f':
if (strcmp(name, "fast")==0)
return PyInt_FromLong(self->fast); return PyInt_FromLong(self->fast);
break;
case 'g':
if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
PyErr_SetString(PyExc_AttributeError, name);
return NULL;
}
break;
}
return Py_FindMethod(Pickler_methods, (PyObject *)self, name); return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
} }
...@@ -1895,7 +2209,7 @@ Pickler_getattr(Picklerobject *self, char *name) { ...@@ -1895,7 +2209,7 @@ Pickler_getattr(Picklerobject *self, char *name) {
int int
Pickler_setattr(Picklerobject *self, char *name, PyObject *value) { Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
if(! value) { if (! value) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"attribute deletion is not supported"); "attribute deletion is not supported");
return -1; return -1;
...@@ -1916,7 +2230,7 @@ Pickler_setattr(Picklerobject *self, char *name, PyObject *value) { ...@@ -1916,7 +2230,7 @@ Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
} }
if (strcmp(name, "memo") == 0) { if (strcmp(name, "memo") == 0) {
if(! PyDict_Check(value)) { if (! PyDict_Check(value)) {
PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
return -1; return -1;
} }
...@@ -1926,12 +2240,12 @@ Pickler_setattr(Picklerobject *self, char *name, PyObject *value) { ...@@ -1926,12 +2240,12 @@ Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
return 0; return 0;
} }
if(strcmp(name, "binary")==0) { if (strcmp(name, "binary")==0) {
self->bin=PyObject_IsTrue(value); self->bin=PyObject_IsTrue(value);
return 0; return 0;
} }
if(strcmp(name, "fast")==0) { if (strcmp(name, "fast")==0) {
self->fast=PyObject_IsTrue(value); self->fast=PyObject_IsTrue(value);
return 0; return 0;
} }
...@@ -2012,9 +2326,7 @@ marker(Unpicklerobject *self) { ...@@ -2012,9 +2326,7 @@ marker(Unpicklerobject *self) {
static int static int
load_none(Unpicklerobject *self) { load_none(Unpicklerobject *self) {
if (PyList_Append(self->stack, Py_None) < 0) PDATA_APPEND(self->stack, Py_None, -1);
return -1;
return 0; return 0;
} }
...@@ -2027,7 +2339,7 @@ load_int(Unpicklerobject *self) { ...@@ -2027,7 +2339,7 @@ load_int(Unpicklerobject *self) {
long l; long l;
if ((len = (*self->readline_func)(self, &s)) < 0) return -1; if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
UNLESS(s=pystrndup(s,len)) return -1; UNLESS (s=pystrndup(s,len)) return -1;
errno = 0; errno = 0;
l = strtol(s, &endptr, 0); l = strtol(s, &endptr, 0);
...@@ -2036,7 +2348,7 @@ load_int(Unpicklerobject *self) { ...@@ -2036,7 +2348,7 @@ load_int(Unpicklerobject *self) {
/* Hm, maybe we've got something long. Let's try reading /* Hm, maybe we've got something long. Let's try reading
it as a Python long object. */ it as a Python long object. */
errno=0; errno=0;
UNLESS(py_int=PyLong_FromString(s,&endptr,0)) goto finally; UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
if ((*endptr != '\n') || (endptr[1] != '\0')) { if ((*endptr != '\n') || (endptr[1] != '\0')) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
...@@ -2045,16 +2357,15 @@ load_int(Unpicklerobject *self) { ...@@ -2045,16 +2357,15 @@ load_int(Unpicklerobject *self) {
} }
} }
else { else {
UNLESS(py_int = PyInt_FromLong(l)) goto finally; UNLESS (py_int = PyInt_FromLong(l)) goto finally;
} }
if (PyList_Append(self->stack, py_int) < 0) goto finally; free(s);
PDATA_PUSH(self->stack, py_int, -1);
res = 0; return 0;
finally: finally:
free(s); free(s);
Py_XDECREF(py_int);
return res; return res;
} }
...@@ -2082,16 +2393,10 @@ load_binintx(Unpicklerobject *self, char *s, int x) { ...@@ -2082,16 +2393,10 @@ load_binintx(Unpicklerobject *self, char *s, int x) {
l = calc_binint(s, x); l = calc_binint(s, x);
UNLESS(py_int = PyInt_FromLong(l)) UNLESS (py_int = PyInt_FromLong(l))
return -1; return -1;
if (PyList_Append(self->stack, py_int) < 0) { PDATA_PUSH(self->stack, py_int, -1);
Py_DECREF(py_int);
return -1;
}
Py_DECREF(py_int);
return 0; return 0;
} }
...@@ -2135,19 +2440,17 @@ load_long(Unpicklerobject *self) { ...@@ -2135,19 +2440,17 @@ load_long(Unpicklerobject *self) {
int len, res = -1; int len, res = -1;
if ((len = (*self->readline_func)(self, &s)) < 0) return -1; if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
UNLESS(s=pystrndup(s,len)) return -1; UNLESS (s=pystrndup(s,len)) return -1;
UNLESS(l = PyLong_FromString(s, &end, 0))
goto finally;
if (PyList_Append(self->stack, l) < 0) UNLESS (l = PyLong_FromString(s, &end, 0))
goto finally; goto finally;
res = 0; free(s);
PDATA_PUSH(self->stack, l, -1);
return 0;
finally: finally:
free(s); free(s);
Py_XDECREF(l);
return res; return res;
} }
...@@ -2161,7 +2464,7 @@ load_float(Unpicklerobject *self) { ...@@ -2161,7 +2464,7 @@ load_float(Unpicklerobject *self) {
double d; double d;
if ((len = (*self->readline_func)(self, &s)) < 0) return -1; if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
UNLESS(s=pystrndup(s,len)) return -1; UNLESS (s=pystrndup(s,len)) return -1;
errno = 0; errno = 0;
d = strtod(s, &endptr); d = strtod(s, &endptr);
...@@ -2172,17 +2475,15 @@ load_float(Unpicklerobject *self) { ...@@ -2172,17 +2475,15 @@ load_float(Unpicklerobject *self) {
goto finally; goto finally;
} }
UNLESS(py_float = PyFloat_FromDouble(d)) UNLESS (py_float = PyFloat_FromDouble(d))
goto finally; goto finally;
if (PyList_Append(self->stack, py_float) < 0) free(s);
goto finally; PDATA_PUSH(self->stack, py_float, -1);
return 0;
res = 0;
finally: finally:
free(s); free(s);
Py_XDECREF(py_float);
return res; return res;
} }
...@@ -2246,18 +2547,10 @@ load_binfloat(Unpicklerobject *self) { ...@@ -2246,18 +2547,10 @@ load_binfloat(Unpicklerobject *self) {
if (s) if (s)
x = -x; x = -x;
UNLESS(py_float = PyFloat_FromDouble(x)) UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
goto finally;
if (PyList_Append(self->stack, py_float) < 0)
goto finally;
res = 0;
finally: PDATA_PUSH(self->stack, py_float, -1);
Py_XDECREF(py_float); return 0;
return res;
} }
static int static int
...@@ -2269,39 +2562,36 @@ load_string(Unpicklerobject *self) { ...@@ -2269,39 +2562,36 @@ load_string(Unpicklerobject *self) {
static PyObject *eval_dict = 0; static PyObject *eval_dict = 0;
if ((len = (*self->readline_func)(self, &s)) < 0) return -1; if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
UNLESS(s=pystrndup(s,len)) return -1; UNLESS (s=pystrndup(s,len)) return -1;
/* Check for unquoted quotes (evil strings) */ /* Check for unquoted quotes (evil strings) */
q=*s; q=*s;
if(q != '"' && q != '\'') goto insecure; if (q != '"' && q != '\'') goto insecure;
for(p=s+1, nslash=0; *p; p++) for (p=s+1, nslash=0; *p; p++) {
{ if (*p==q && nslash%2==0) break;
if(*p==q && nslash%2==0) break; if (*p=='\\') nslash++;
if(*p=='\\') nslash++;
else nslash=0; else nslash=0;
} }
if(*p==q) if (*p==q)
{ {
for(p++; *p; p++) if(*p > ' ') goto insecure; for (p++; *p; p++) if (*p > ' ') goto insecure;
} }
else goto insecure; else goto insecure;
/********************************************/ /********************************************/
UNLESS(eval_dict) UNLESS (eval_dict)
UNLESS(eval_dict = Py_BuildValue("{s{}}", "__builtins__")) UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
goto finally; goto finally;
UNLESS(str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict)) UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
goto finally; goto finally;
if (PyList_Append(self->stack, str) < 0) free(s);
goto finally; PDATA_PUSH(self->stack, str, -1);
return 0;
res = 0;
finally: finally:
free(s); free(s);
Py_XDECREF(str);
return res; return res;
...@@ -2319,26 +2609,18 @@ load_binstring(Unpicklerobject *self) { ...@@ -2319,26 +2609,18 @@ load_binstring(Unpicklerobject *self) {
int res = -1; int res = -1;
char *s; char *s;
if ((*self->read_func)(self, &s, 4) < 0) if ((*self->read_func)(self, &s, 4) < 0) return -1;
goto finally;
l = calc_binint(s, 4); l = calc_binint(s, 4);
if ((*self->read_func)(self, &s, l) < 0) if ((*self->read_func)(self, &s, l) < 0)
goto finally; return -1;
UNLESS(py_string = PyString_FromStringAndSize(s, l))
goto finally;
if (PyList_Append(self->stack, py_string) < 0)
goto finally;
res = 0;
finally: UNLESS (py_string = PyString_FromStringAndSize(s, l))
Py_XDECREF(py_string); return -1;
return res; PDATA_PUSH(self->stack, py_string, -1);
return 0;
} }
...@@ -2354,184 +2636,86 @@ load_short_binstring(Unpicklerobject *self) { ...@@ -2354,184 +2636,86 @@ load_short_binstring(Unpicklerobject *self) {
l = (unsigned char)s[0]; l = (unsigned char)s[0];
if ((*self->read_func)(self, &s, l) < 0) if ((*self->read_func)(self, &s, l) < 0) return -1;
goto finally;
UNLESS(py_string = PyString_FromStringAndSize(s, l))
goto finally;
if (PyList_Append(self->stack, py_string) < 0)
goto finally;
res = 0; UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
finally:
Py_XDECREF(py_string);
return res; PDATA_PUSH(self->stack, py_string, -1);
return 0;
} }
static int static int
load_tuple(Unpicklerobject *self) { load_tuple(Unpicklerobject *self) {
PyObject *tup = 0, *slice = 0, *list = 0; PyObject *tup;
int i, j, res = -1; int i;
if ((i = marker(self)) < 0)
goto finally;
if ((j = PyList_Size(self->stack)) < 0)
goto finally;
UNLESS(slice = PyList_GetSlice(self->stack, i, j))
goto finally;
UNLESS(tup = PySequence_Tuple(slice))
goto finally;
UNLESS(list = PyList_New(1))
goto finally;
Py_INCREF(tup);
if (PyList_SetItem(list, 0, tup) < 0)
goto finally;
if (PyList_SetSlice(self->stack, i, j, list) < 0)
goto finally;
res = 0;
finally:
Py_XDECREF(tup);
Py_XDECREF(list);
Py_XDECREF(slice);
return res; if ((i = marker(self)) < 0) return -1;
UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
PDATA_PUSH(self->stack, tup, -1);
return 0;
} }
static int static int
load_empty_tuple(Unpicklerobject *self) { load_empty_tuple(Unpicklerobject *self) {
PyObject *tup = 0; PyObject *tup;
int res;
UNLESS(tup=PyTuple_New(0)) return -1; UNLESS (tup=PyTuple_New(0)) return -1;
res=PyList_Append(self->stack, tup); PDATA_PUSH(self->stack, tup, -1);
Py_DECREF(tup); return 0;
return res;
} }
static int static int
load_empty_list(Unpicklerobject *self) { load_empty_list(Unpicklerobject *self) {
PyObject *list = 0; PyObject *list;
int res;
UNLESS(list=PyList_New(0)) return -1; UNLESS (list=PyList_New(0)) return -1;
res=PyList_Append(self->stack, list); PDATA_PUSH(self->stack, list, -1);
Py_DECREF(list); return 0;
return res;
} }
static int static int
load_empty_dict(Unpicklerobject *self) { load_empty_dict(Unpicklerobject *self) {
PyObject *dict = 0; PyObject *dict;
int res;
UNLESS(dict=PyDict_New()) return -1; UNLESS (dict=PyDict_New()) return -1;
res=PyList_Append(self->stack, dict); PDATA_PUSH(self->stack, dict, -1);
Py_DECREF(dict); return 0;
return res;
} }
static int static int
load_list(Unpicklerobject *self) { load_list(Unpicklerobject *self) {
PyObject *list = 0, *slice = 0; PyObject *list = 0;
int i, j, l, res = -1; int i;
if ((i = marker(self)) < 0)
goto finally;
if ((j = PyList_Size(self->stack)) < 0)
goto finally;
UNLESS(slice = PyList_GetSlice(self->stack, i, j))
goto finally;
if((l=PyList_Size(slice)) < 0)
goto finally;
if(l) {
UNLESS(list = PyList_New(1))
goto finally;
Py_INCREF(slice);
if (PyList_SetItem(list, 0, slice) < 0)
goto finally;
if (PyList_SetSlice(self->stack, i, j, list) < 0)
goto finally;
} else {
if(PyList_Append(self->stack,slice) < 0)
goto finally;
}
res = 0;
finally:
Py_XDECREF(list);
Py_XDECREF(slice);
return res; if ((i = marker(self)) < 0) return -1;
UNLESS (list=Pdata_popList(self->stack, i)) return -1;
PDATA_PUSH(self->stack, list, -1);
return 0;
} }
static int static int
load_dict(Unpicklerobject *self) { load_dict(Unpicklerobject *self) {
PyObject *list = 0, *dict = 0, *key = 0, *value = 0; PyObject *dict, *key, *value;
int i, j, k, res = -1; int i, j, k;
if ((i = marker(self)) < 0)
goto finally;
if ((j = PyList_Size(self->stack)) < 0) if ((i = marker(self)) < 0) return -1;
goto finally; j=self->stack->length;
UNLESS(dict = PyDict_New())
goto finally;
for (k = i; k < j; k += 2) {
UNLESS(key = PyList_GET_ITEM((PyListObject *)self->stack, k))
goto finally;
UNLESS(value = PyList_GET_ITEM((PyListObject *)self->stack, k + 1)) UNLESS (dict = PyDict_New()) return -1;
goto finally;
if (PyDict_SetItem(dict, key, value) < 0) for (k = i+1; k < j; k += 2) {
goto finally; key =self->stack->data[k-1];
value=self->stack->data[k ];
if (PyDict_SetItem(dict, key, value) < 0) {
Py_DECREF(dict);
return -1;
} }
if(j) {
UNLESS(list = PyList_New(1))
goto finally;
Py_INCREF(dict);
if (PyList_SetItem(list, 0, dict) < 0)
goto finally;
if (PyList_SetSlice(self->stack, i, j, list) < 0)
goto finally;
} }
else Pdata_clear(self->stack, i);
if(PyList_Append(self->stack, dict) < 0) PDATA_PUSH(self->stack, dict, -1);
goto finally; return 0;
res = 0;
finally:
Py_XDECREF(dict);
Py_XDECREF(list);
return res;
} }
static PyObject * static PyObject *
...@@ -2542,21 +2726,21 @@ Instance_New(PyObject *cls, PyObject *args) { ...@@ -2542,21 +2726,21 @@ Instance_New(PyObject *cls, PyObject *args) {
if (PyClass_Check(cls)) { if (PyClass_Check(cls)) {
int l; int l;
if((l=PyObject_Length(args)) < 0) goto err; if ((l=PyObject_Length(args)) < 0) goto err;
UNLESS(l) { UNLESS (l) {
PyObject *__getinitargs__; PyObject *__getinitargs__;
UNLESS(__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) { UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
/* We have a class with no __getinitargs__, so bypass usual /* We have a class with no __getinitargs__, so bypass usual
construction */ construction */
PyInstanceObject *inst; PyInstanceObject *inst;
PyErr_Clear(); PyErr_Clear();
UNLESS(inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type)) UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
goto err; goto err;
inst->in_class=(PyClassObject*)cls; inst->in_class=(PyClassObject*)cls;
Py_INCREF(cls); Py_INCREF(cls);
UNLESS(inst->in_dict=PyDict_New()) { UNLESS (inst->in_dict=PyDict_New()) {
Py_DECREF(inst); Py_DECREF(inst);
goto err; goto err;
} }
...@@ -2566,7 +2750,7 @@ Instance_New(PyObject *cls, PyObject *args) { ...@@ -2566,7 +2750,7 @@ Instance_New(PyObject *cls, PyObject *args) {
Py_DECREF(__getinitargs__); Py_DECREF(__getinitargs__);
} }
if((r=PyInstance_New(cls, args, NULL))) return r; if ((r=PyInstance_New(cls, args, NULL))) return r;
else goto err; else goto err;
} }
...@@ -2575,7 +2759,7 @@ Instance_New(PyObject *cls, PyObject *args) { ...@@ -2575,7 +2759,7 @@ Instance_New(PyObject *cls, PyObject *args) {
goto err; goto err;
if (!has_key) if (!has_key)
if(!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) || if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
!PyObject_IsTrue(safe)) { !PyObject_IsTrue(safe)) {
cPickle_ErrFormat(UnpicklingError, cPickle_ErrFormat(UnpicklingError,
"%s is not safe for unpickling", "O", cls); "%s is not safe for unpickling", "O", cls);
...@@ -2583,25 +2767,24 @@ Instance_New(PyObject *cls, PyObject *args) { ...@@ -2583,25 +2767,24 @@ Instance_New(PyObject *cls, PyObject *args) {
return NULL; return NULL;
} }
if(args==Py_None) if (args==Py_None) {
{
/* Special case, call cls.__basicnew__() */ /* Special case, call cls.__basicnew__() */
PyObject *basicnew; PyObject *basicnew;
UNLESS(basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL; UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
r=PyObject_CallObject(basicnew, NULL); r=PyObject_CallObject(basicnew, NULL);
Py_DECREF(basicnew); Py_DECREF(basicnew);
if(r) return r; if (r) return r;
} }
if((r=PyObject_CallObject(cls, args))) return r; if ((r=PyObject_CallObject(cls, args))) return r;
err: err:
{ {
PyObject *tp, *v, *tb; PyObject *tp, *v, *tb;
PyErr_Fetch(&tp, &v, &tb); PyErr_Fetch(&tp, &v, &tb);
if((r=Py_BuildValue("OOO",v,cls,args))) { if ((r=Py_BuildValue("OOO",v,cls,args))) {
Py_XDECREF(v); Py_XDECREF(v);
v=r; v=r;
} }
...@@ -2613,228 +2796,152 @@ err: ...@@ -2613,228 +2796,152 @@ err:
static int static int
load_obj(Unpicklerobject *self) { load_obj(Unpicklerobject *self) {
PyObject *class = 0, *slice = 0, *tup = 0, *obj = 0; PyObject *class, *tup, *obj=0;
int i, len, res = -1; int i;
if ((i = marker(self)) < 0)
goto finally;
if ((len = PyList_Size(self->stack)) < 0)
goto finally;
UNLESS(slice = PyList_GetSlice(self->stack, i + 1, len))
goto finally;
UNLESS(tup = PySequence_Tuple(slice))
goto finally;
class = PyList_GET_ITEM((PyListObject *)self->stack, i);
Py_INCREF(class);
UNLESS(obj = Instance_New(class, tup))
goto finally;
if (DEL_LIST_SLICE(self->stack, i, len) < 0)
goto finally;
if (PyList_Append(self->stack, obj) < 0)
goto finally;
res = 0;
finally:
Py_XDECREF(class); if ((i = marker(self)) < 0) return -1;
Py_XDECREF(slice); UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
Py_XDECREF(tup); PDATA_POP(self->stack, class);
Py_XDECREF(obj); if (class) {
obj = Instance_New(class, tup);
Py_DECREF(class);
}
Py_DECREF(tup);
return res; if (! obj) return -1;
PDATA_PUSH(self->stack, obj, -1);
return 0;
} }
static int static int
load_inst(Unpicklerobject *self) { load_inst(Unpicklerobject *self) {
PyObject *arg_tup = 0, *arg_slice = 0, *class = 0, *obj = 0, PyObject *tup, *class, *obj, *module_name, *class_name;
*module_name = 0, *class_name = 0; int i, j, len;
int i, j, len, res = -1;
char *s; char *s;
if ((i = marker(self)) < 0) goto finally; if ((i = marker(self)) < 0) return -1;
if ((j = PyList_Size(self->stack)) < 0) goto finally;
UNLESS(arg_slice = PyList_GetSlice(self->stack, i, j)) goto finally;
UNLESS(arg_tup = PySequence_Tuple(arg_slice)) goto finally;
if (DEL_LIST_SLICE(self->stack, i, j) < 0) goto finally;
if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
UNLESS(module_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
UNLESS(class_name = PyString_FromStringAndSize(s, len - 1)) goto finally; if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
UNLESS(class = find_class(module_name, class_name)) if ((len = (*self->readline_func)(self, &s)) >= 0) {
goto finally; if (class_name = PyString_FromStringAndSize(s, len - 1)) {
class = find_class(module_name, class_name);
Py_DECREF(class_name);
}
}
Py_DECREF(module_name);
UNLESS(obj = Instance_New(class, arg_tup)) goto finally; if (! class) return -1;
if (PyList_Append(self->stack, obj) < 0) goto finally; if (tup=Pdata_popTuple(self->stack, i)) {
obj = Instance_New(class, tup);
Py_DECREF(tup);
}
Py_DECREF(class);
res = 0; if (! obj) return -1;
finally: PDATA_PUSH(self->stack, obj, -1);
Py_XDECREF(class); return 0;
Py_XDECREF(arg_slice);
Py_XDECREF(arg_tup);
Py_XDECREF(obj);
Py_XDECREF(module_name);
Py_XDECREF(class_name);
return res;
} }
static int static int
load_global(Unpicklerobject *self) { load_global(Unpicklerobject *self) {
PyObject *class = 0, *module_name = 0, *class_name = 0; PyObject *class = 0, *module_name = 0, *class_name = 0;
int res = -1, len; int len;
char *s; char *s;
if ((len = (*self->readline_func)(self, &s)) < 0) if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
goto finally; UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
UNLESS(module_name = PyString_FromStringAndSize(s, len - 1))
goto finally;
if ((len = (*self->readline_func)(self, &s)) < 0)
goto finally;
UNLESS(class_name = PyString_FromStringAndSize(s, len - 1))
goto finally;
UNLESS(class = find_class(module_name, class_name))
goto finally;
if (PyList_Append(self->stack, class) < 0)
goto finally;
res = 0;
finally: if ((len = (*self->readline_func)(self, &s)) >= 0) {
Py_XDECREF(class); if (class_name = PyString_FromStringAndSize(s, len - 1)) {
Py_XDECREF(module_name); class = find_class(module_name, class_name);
Py_XDECREF(class_name); Py_DECREF(class_name);
}
}
Py_DECREF(module_name);
return res; if (! class) return -1;
PDATA_PUSH(self->stack, class, -1);
return 0;
} }
static int static int
load_persid(Unpicklerobject *self) { load_persid(Unpicklerobject *self) {
PyObject *pid = 0, *pers_load_val = 0; PyObject *pid = 0;
int len, res = -1; int len, res = -1;
char *s; char *s;
if (self->pers_func) { if (self->pers_func) {
if ((len = (*self->readline_func)(self, &s)) < 0) if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
goto finally;
UNLESS(pid = PyString_FromStringAndSize(s, len - 1)) UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
goto finally;
if(PyList_Check(self->pers_func)) { if (PyList_Check(self->pers_func)) {
if(PyList_Append(self->pers_func, pid) < 0) goto finally; if (PyList_Append(self->pers_func, pid) < 0) {
pers_load_val=pid; Py_DECREF(pid);
Py_INCREF(pid); return -1;
}
} }
else { else {
UNLESS(self->arg) ARG_TUP(self, pid);
UNLESS(self->arg = PyTuple_New(1)) if (self->arg) {
goto finally; pid = PyObject_CallObject(self->pers_func, self->arg);
FREE_ARG_TUP(self);
}
}
Py_INCREF(pid); if (! pid) return -1;
if (PyTuple_SetItem(self->arg, 0, pid) < 0)
goto finally;
UNLESS(pers_load_val = PDATA_PUSH(self->stack, pid, -1);
PyObject_CallObject(self->pers_func, self->arg)) return 0;
goto finally;
}
if (PyList_Append(self->stack, pers_load_val) < 0)
goto finally;
} }
else { else {
PyErr_SetString(UnpicklingError, PyErr_SetString(UnpicklingError,
"A load persistent id instruction was encountered,\n" "A load persistent id instruction was encountered,\n"
"but no persistent_load function was specified."); "but no persistent_load function was specified.");
goto finally; return -1;
} }
res = 0;
finally:
Py_XDECREF(pid);
Py_XDECREF(pers_load_val);
return res;
} }
static int static int
load_binpersid(Unpicklerobject *self) { load_binpersid(Unpicklerobject *self) {
PyObject *pid = 0, *pers_load_val = 0; PyObject *pid = 0;
int len, res = -1; int len, res = -1;
if (self->pers_func) { if (self->pers_func) {
if ((len = PyList_Size(self->stack)) < 0) PDATA_POP(self->stack, pid);
goto finally; if (! pid) return -1;
pid = PyList_GET_ITEM((PyListObject *)self->stack, len - 1); if (PyList_Check(self->pers_func)) {
Py_INCREF(pid); if (PyList_Append(self->pers_func, pid) < 0) {
Py_DECREF(pid);
if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0) return -1;
goto finally; }
if(PyList_Check(self->pers_func)) {
if(PyList_Append(self->pers_func, pid) < 0) goto finally;
pers_load_val=pid;
Py_INCREF(pid);
} }
else { else {
UNLESS(self->arg) ARG_TUP(self, pid);
UNLESS(self->arg = PyTuple_New(1)) if (self->arg) {
goto finally; pid = PyObject_CallObject(self->pers_func, self->arg);
FREE_ARG_TUP(self);
Py_INCREF(pid);
if (PyTuple_SetItem(self->arg, 0, pid) < 0)
goto finally;
UNLESS(pers_load_val =
PyObject_CallObject(self->pers_func, self->arg))
goto finally;
} }
if (PyList_Append(self->stack, pers_load_val) < 0) if (! pid) return -1;
goto finally; }
PDATA_PUSH(self->stack, pid, -1);
return 0;
} }
else { else {
PyErr_SetString(UnpicklingError, PyErr_SetString(UnpicklingError,
"A load persistent id instruction was encountered,\n" "A load persistent id instruction was encountered,\n"
"but no persistent_load function was specified."); "but no persistent_load function was specified.");
goto finally; return -1;
} }
res = 0;
finally:
Py_XDECREF(pid);
Py_XDECREF(pers_load_val);
return res;
} }
...@@ -2842,14 +2949,13 @@ static int ...@@ -2842,14 +2949,13 @@ static int
load_pop(Unpicklerobject *self) { load_pop(Unpicklerobject *self) {
int len; int len;
if ((len = PyList_Size(self->stack)) < 0) UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
return -1;
if ((self->num_marks > 0) && if ((self->num_marks > 0) &&
(self->marks[self->num_marks - 1] == len)) (self->marks[self->num_marks - 1] == len))
self->num_marks--; self->num_marks--;
else if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0) else
return -1; Py_DECREF(self->stack->data[--(self->stack->length)]);
return 0; return 0;
} }
...@@ -2862,11 +2968,7 @@ load_pop_mark(Unpicklerobject *self) { ...@@ -2862,11 +2968,7 @@ load_pop_mark(Unpicklerobject *self) {
if ((i = marker(self)) < 0) if ((i = marker(self)) < 0)
return -1; return -1;
if ((len = PyList_Size(self->stack)) < 0) Pdata_clear(self->stack, i);
return -1;
if (DEL_LIST_SLICE(self->stack, i, len) < 0)
return -1;
return 0; return 0;
} }
...@@ -2877,15 +2979,10 @@ load_dup(Unpicklerobject *self) { ...@@ -2877,15 +2979,10 @@ load_dup(Unpicklerobject *self) {
PyObject *last; PyObject *last;
int len; int len;
if ((len = PyList_Size(self->stack)) < 0) if ((len = self->stack->length) <= 0) return stackUnderflow();
return -1; last=self->stack->data[len-1];
Py_INCREF(last);
UNLESS(last = PyList_GetItem(self->stack, len - 1)) PDATA_PUSH(self->stack, last, -1);
return -1;
if (PyList_Append(self->stack, last) < 0)
return -1;
return 0; return 0;
} }
...@@ -2896,26 +2993,19 @@ load_get(Unpicklerobject *self) { ...@@ -2896,26 +2993,19 @@ load_get(Unpicklerobject *self) {
int len, res = -1; int len, res = -1;
char *s; char *s;
if ((len = (*self->readline_func)(self, &s)) < 0) if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
goto finally;
UNLESS(py_str = PyString_FromStringAndSize(s, len - 1)) UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
goto finally;
UNLESS(value = PyDict_GetItem(self->memo, py_str)) { value = PyDict_GetItem(self->memo, py_str);
PyErr_SetObject(PyExc_KeyError, py_str); Py_DECREF(py_str);
goto finally; if (! value) {
PyErr_SetObject(BadPickleGet, py_str);
return -1;
} }
if (PyList_Append(self->stack, value) < 0) PDATA_APPEND(self->stack, value, -1);
goto finally; return 0;
res = 0;
finally:
Py_XDECREF(py_str);
return res;
} }
...@@ -2926,28 +3016,20 @@ load_binget(Unpicklerobject *self) { ...@@ -2926,28 +3016,20 @@ load_binget(Unpicklerobject *self) {
int res = -1; int res = -1;
char *s; char *s;
if ((*self->read_func)(self, &s, 1) < 0) if ((*self->read_func)(self, &s, 1) < 0) return -1;
goto finally;
key = (unsigned char)s[0]; key = (unsigned char)s[0];
UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
UNLESS(py_key = PyInt_FromLong((long)key)) value = PyDict_GetItem(self->memo, py_key);
goto finally; Py_DECREF(py_key);
if (! value) {
UNLESS(value = PyDict_GetItem(self->memo, py_key)) { PyErr_SetObject(BadPickleGet, py_key);
PyErr_SetObject(PyExc_KeyError, py_key); return -1;
goto finally;
} }
if (PyList_Append(self->stack, value) < 0) PDATA_APPEND(self->stack, value, -1);
goto finally; return 0;
res = 0;
finally:
Py_XDECREF(py_key);
return res;
} }
...@@ -2958,8 +3040,7 @@ load_long_binget(Unpicklerobject *self) { ...@@ -2958,8 +3040,7 @@ load_long_binget(Unpicklerobject *self) {
long key; long key;
int res = -1; int res = -1;
if ((*self->read_func)(self, &s, 4) < 0) if ((*self->read_func)(self, &s, 4) < 0) return -1;
goto finally;
c = (unsigned char)s[0]; c = (unsigned char)s[0];
key = (long)c; key = (long)c;
...@@ -2970,53 +3051,33 @@ load_long_binget(Unpicklerobject *self) { ...@@ -2970,53 +3051,33 @@ load_long_binget(Unpicklerobject *self) {
c = (unsigned char)s[3]; c = (unsigned char)s[3];
key |= (long)c << 24; key |= (long)c << 24;
UNLESS(py_key = PyInt_FromLong(key)) UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
goto finally;
UNLESS(value = PyDict_GetItem(self->memo, py_key)) { value = PyDict_GetItem(self->memo, py_key);
PyErr_SetObject(PyExc_KeyError, py_key); Py_DECREF(py_key);
goto finally; if (! value) {
PyErr_SetObject(BadPickleGet, py_key);
return -1;
} }
if (PyList_Append(self->stack, value) < 0) PDATA_APPEND(self->stack, value, -1);
goto finally; return 0;
res = 0;
finally:
Py_XDECREF(py_key);
return res;
} }
static int static int
load_put(Unpicklerobject *self) { load_put(Unpicklerobject *self) {
PyObject *py_str = 0, *value = 0; PyObject *py_str = 0, *value = 0;
int len, res = -1; int len, l;
char *s; char *s;
if ((len = (*self->readline_func)(self, &s)) < 0) if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
goto finally; UNLESS (len=self->stack->length) return stackUnderflow();
UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
UNLESS(py_str = PyString_FromStringAndSize(s, len - 1)) value=self->stack->data[len-1];
goto finally; l=PyDict_SetItem(self->memo, py_str, value);
Py_DECREF(py_str);
if ((len = PyList_Size(self->stack)) < 0) return l;
goto finally;
UNLESS(value = PyList_GetItem(self->stack, len - 1))
goto finally;
if (PyDict_SetItem(self->memo, py_str, value) < 0)
goto finally;
res = 0;
finally:
Py_XDECREF(py_str);
return res;
} }
...@@ -3024,31 +3085,18 @@ static int ...@@ -3024,31 +3085,18 @@ static int
load_binput(Unpicklerobject *self) { load_binput(Unpicklerobject *self) {
PyObject *py_key = 0, *value = 0; PyObject *py_key = 0, *value = 0;
unsigned char key, *s; unsigned char key, *s;
int len, res = -1; int len;
if ((*self->read_func)(self, &s, 1) < 0) if ((*self->read_func)(self, &s, 1) < 0) return -1;
goto finally; UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
key = (unsigned char)s[0]; key = (unsigned char)s[0];
UNLESS(py_key = PyInt_FromLong((long)key)) UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
goto finally; value=self->stack->data[len-1];
len=PyDict_SetItem(self->memo, py_key, value);
if ((len = PyList_Size(self->stack)) < 0) Py_DECREF(py_key);
goto finally; return len;
UNLESS(value = PyList_GetItem(self->stack, len - 1))
goto finally;
if (PyDict_SetItem(self->memo, py_key, value) < 0)
goto finally;
res = 0;
finally:
Py_XDECREF(py_key);
return res;
} }
...@@ -3059,8 +3107,8 @@ load_long_binput(Unpicklerobject *self) { ...@@ -3059,8 +3107,8 @@ load_long_binput(Unpicklerobject *self) {
unsigned char c, *s; unsigned char c, *s;
int len, res = -1; int len, res = -1;
if ((*self->read_func)(self, &s, 4) < 0) if ((*self->read_func)(self, &s, 4) < 0) return -1;
goto finally; UNLESS (len=self->stack->length) return stackUnderflow();
c = (unsigned char)s[0]; c = (unsigned char)s[0];
key = (long)c; key = (long)c;
...@@ -3071,24 +3119,11 @@ load_long_binput(Unpicklerobject *self) { ...@@ -3071,24 +3119,11 @@ load_long_binput(Unpicklerobject *self) {
c = (unsigned char)s[3]; c = (unsigned char)s[3];
key |= (long)c << 24; key |= (long)c << 24;
UNLESS(py_key = PyInt_FromLong(key)) UNLESS (py_key = PyInt_FromLong(key)) return -1;
goto finally; value=self->stack->data[len-1];
len=PyDict_SetItem(self->memo, py_key, value);
if ((len = PyList_Size(self->stack)) < 0) Py_DECREF(py_key);
goto finally; return len;
UNLESS(value = PyList_GetItem(self->stack, len - 1))
goto finally;
if (PyDict_SetItem(self->memo, py_key, value) < 0)
goto finally;
res = 0;
finally:
Py_XDECREF(py_key);
return res;
} }
...@@ -3097,69 +3132,55 @@ do_append(Unpicklerobject *self, int x) { ...@@ -3097,69 +3132,55 @@ do_append(Unpicklerobject *self, int x) {
PyObject *value = 0, *list = 0, *append_method = 0; PyObject *value = 0, *list = 0, *append_method = 0;
int len, i; int len, i;
if ((len = PyList_Size(self->stack)) < 0) UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
return -1; if (len==x) return 0; /* nothing to do */
UNLESS(list = PyList_GetItem(self->stack, x - 1)) list=self->stack->data[x-1];
goto err;
if (PyList_Check(list)) { if (PyList_Check(list)) {
PyObject *slice = 0; PyObject *slice;
int list_len; int list_len;
UNLESS(slice = PyList_GetSlice(self->stack, x, len)) slice=Pdata_popList(self->stack, x);
return -1; list_len = PyList_GET_SIZE(list);
i=PyList_SetSlice(list, list_len, list_len, slice);
list_len = PyList_Size(list);
if (PyList_SetSlice(list, list_len, list_len, slice) < 0) {
Py_DECREF(slice);
return -1;
}
Py_DECREF(slice); Py_DECREF(slice);
return i;
} }
else { else {
UNLESS(append_method = PyObject_GetAttr(list, append_str)) UNLESS (append_method = PyObject_GetAttr(list, append_str))
return -1; return -1;
for (i = x; i < len; i++) { for (i = x; i < len; i++) {
PyObject *junk; PyObject *junk;
UNLESS(value = PyList_GetItem(self->stack, i)) value=self->stack->data[i];
junk=0;
ARG_TUP(self, value);
if (self->arg) {
junk = PyObject_CallObject(append_method, self->arg);
FREE_ARG_TUP(self);
}
if (! junk) {
Pdata_clear(self->stack, i+1);
self->stack->length=x;
Py_DECREF(append_method);
return -1; return -1;
}
UNLESS(self->arg)
UNLESS(self->arg = PyTuple_New(1))
goto err;
Py_INCREF(value);
if (PyTuple_SetItem(self->arg, 0, value) < 0)
goto err;
UNLESS(junk = PyObject_CallObject(append_method, self->arg))
goto err;
Py_DECREF(junk); Py_DECREF(junk);
} }
self->stack->length=x;
Py_DECREF(append_method);
} }
if (DEL_LIST_SLICE(self->stack, x, len) < 0)
goto err;
Py_XDECREF(append_method);
return 0; return 0;
err:
Py_XDECREF(append_method);
return -1;
} }
static int static int
load_append(Unpicklerobject *self) { load_append(Unpicklerobject *self) {
return do_append(self, PyList_Size(self->stack) - 1); return do_append(self, self->stack->length - 1);
} }
...@@ -3172,42 +3193,33 @@ load_appends(Unpicklerobject *self) { ...@@ -3172,42 +3193,33 @@ load_appends(Unpicklerobject *self) {
static int static int
do_setitems(Unpicklerobject *self, int x) { do_setitems(Unpicklerobject *self, int x) {
PyObject *value = 0, *key = 0, *dict = 0; PyObject *value = 0, *key = 0, *dict = 0;
int len, i, res = -1; int len, i, r=0;
if ((len = PyList_Size(self->stack)) < 0)
goto finally;
UNLESS(dict = PyList_GetItem(self->stack, x - 1))
goto finally;
for (i = x; i < len; i += 2) { UNLESS ((len=self->stack->length) >= x
UNLESS(key = PyList_GetItem(self->stack, i)) && x > 0) return stackUnderflow();
goto finally;
UNLESS(value = PyList_GetItem(self->stack, i + 1)) dict=self->stack->data[x-1];
goto finally;
if (PyObject_SetItem(dict, key, value) < 0) for (i = x+1; i < len; i += 2) {
goto finally; key =self->stack->data[i-1];
value=self->stack->data[i ];
if (PyObject_SetItem(dict, key, value) < 0) {
r=-1;
break;
}
} }
if (DEL_LIST_SLICE(self->stack, x, len) < 0) Pdata_clear(self->stack, x);
goto finally;
res = 0;
finally:
return res; return r;
} }
static int static int
load_setitem(Unpicklerobject *self) { load_setitem(Unpicklerobject *self) {
return do_setitems(self, PyList_Size(self->stack) - 2); return do_setitems(self, self->stack->length - 2);
} }
static int static int
load_setitems(Unpicklerobject *self) { load_setitems(Unpicklerobject *self) {
return do_setitems(self, marker(self)); return do_setitems(self, marker(self));
...@@ -3218,83 +3230,60 @@ static int ...@@ -3218,83 +3230,60 @@ static int
load_build(Unpicklerobject *self) { load_build(Unpicklerobject *self) {
PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0, PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
*junk = 0, *__setstate__ = 0; *junk = 0, *__setstate__ = 0;
int len, i, res = -1; int len, i, r = 0;
if ((len = PyList_Size(self->stack)) < 0)
goto finally;
UNLESS(value = PyList_GetItem(self->stack, len - 1)) if (self->stack->length < 2) return stackUnderflow();
goto finally; PDATA_POP(self->stack, value);
Py_INCREF(value); if (! value) return -1;
inst=self->stack->data[self->stack->length-1];
if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
goto finally;
UNLESS(inst = PyList_GetItem(self->stack, len - 2)) if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
goto finally; ARG_TUP(self, value);
if (self->arg) {
junk = PyObject_CallObject(__setstate__, self->arg);
FREE_ARG_TUP(self);
}
Py_DECREF(__setstate__);
if (! junk) return -1;
Py_DECREF(junk);
return 0;
}
UNLESS(__setstate__ = PyObject_GetAttr(inst, __setstate___str)) {
PyErr_Clear(); PyErr_Clear();
if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
UNLESS(instdict = PyObject_GetAttr(inst, __dict___str))
goto finally;
i = 0; i = 0;
while (PyDict_Next(value, &i, &d_key, &d_value)) { while (PyDict_Next(value, &i, &d_key, &d_value)) {
if (PyObject_SetItem(instdict, d_key, d_value) < 0) if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
goto finally; r=-1;
break;
} }
} }
else { Py_DECREF(instdict);
UNLESS(self->arg)
UNLESS(self->arg = PyTuple_New(1))
goto finally;
Py_INCREF(value);
if (PyTuple_SetItem(self->arg, 0, value) < 0)
goto finally;
UNLESS(junk = PyObject_CallObject(__setstate__, self->arg))
goto finally;
Py_DECREF(junk);
} }
else r=-1;
res = 0;
finally:
Py_XDECREF(value); Py_XDECREF(value);
Py_XDECREF(instdict);
Py_XDECREF(__setstate__);
return res; return r;
} }
static int static int
load_mark(Unpicklerobject *self) { load_mark(Unpicklerobject *self) {
int len; int s;
if ((len = PyList_Size(self->stack)) < 0) if ((self->num_marks + 1) >= self->marks_size) {
return -1; s=self->marks_size+20;
if (s <= self->num_marks) s=self->num_marks + 1;
if (!self->marks_size) { self->marks =(int *)realloc(self->marks, s * sizeof(int));
self->marks_size = 20; if (! self->marks) {
UNLESS(self->marks = (int *)malloc(self->marks_size * sizeof(int))) {
PyErr_NoMemory();
return -1;
}
}
else if ((self->num_marks + 1) >= self->marks_size) {
UNLESS(self->marks = (int *)realloc(self->marks,
(self->marks_size + 20) * sizeof(int))) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;
} }
self->marks_size = s;
self->marks_size += 20;
} }
self->marks[self->num_marks++] = len; self->marks[self->num_marks++] = self->stack->length;
return 0; return 0;
} }
...@@ -3302,32 +3291,18 @@ load_mark(Unpicklerobject *self) { ...@@ -3302,32 +3291,18 @@ load_mark(Unpicklerobject *self) {
static int static int
load_reduce(Unpicklerobject *self) { load_reduce(Unpicklerobject *self) {
PyObject *callable = 0, *arg_tup = 0, *ob = 0; PyObject *callable = 0, *arg_tup = 0, *ob = 0;
int len, res = -1;
if ((len = PyList_Size(self->stack)) < 0) PDATA_POP(self->stack, arg_tup);
goto finally; if (! arg_tup) return -1;
PDATA_POP(self->stack, callable);
UNLESS(arg_tup = PyList_GetItem(self->stack, len - 1)) if (callable) {
goto finally; ob = Instance_New(callable, arg_tup);
Py_DECREF(callable);
UNLESS(callable = PyList_GetItem(self->stack, len - 2)) }
goto finally; Py_DECREF(arg_tup);
UNLESS(ob = Instance_New(callable, arg_tup))
goto finally;
if (PyList_Append(self->stack, ob) < 0)
goto finally;
if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0)
goto finally;
res = 0;
finally:
Py_XDECREF(ob);
return res; PDATA_PUSH(self->stack, ob, -1);
return 0;
} }
static PyObject * static PyObject *
...@@ -3336,11 +3311,8 @@ load(Unpicklerobject *self) { ...@@ -3336,11 +3311,8 @@ load(Unpicklerobject *self) {
int len; int len;
char *s; char *s;
UNLESS(stack = PyList_New(0))
goto err;
self->stack = stack;
self->num_marks = 0; self->num_marks = 0;
if (self->stack->length) Pdata_clear(self->stack, 0);
while (1) { while (1) {
if ((*self->read_func)(self, &s, 1) < 0) if ((*self->read_func)(self, &s, 1) < 0)
...@@ -3382,12 +3354,10 @@ load(Unpicklerobject *self) { ...@@ -3382,12 +3354,10 @@ load(Unpicklerobject *self) {
break; break;
continue; continue;
#ifdef FORMAT_1_3
case BINFLOAT: case BINFLOAT:
if (load_binfloat(self) < 0) if (load_binfloat(self) < 0)
break; break;
continue; continue;
#endif
case BINSTRING: case BINSTRING:
if (load_binstring(self) < 0) if (load_binstring(self) < 0)
...@@ -3545,34 +3515,21 @@ load(Unpicklerobject *self) { ...@@ -3545,34 +3515,21 @@ load(Unpicklerobject *self) {
default: default:
cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.", cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
"c", s[0]); "c", s[0]);
goto err; return NULL;
} }
break; break;
} }
if ((err = PyErr_Occurred()) == PyExc_EOFError) { if ((err = PyErr_Occurred())) {
if (err == PyExc_EOFError) {
PyErr_SetNone(PyExc_EOFError); PyErr_SetNone(PyExc_EOFError);
goto err; }
return NULL;
} }
if (err) goto err; PDATA_POP(self->stack, val);
if ((len = PyList_Size(stack)) < 0) goto err;
UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
Py_INCREF(val);
Py_DECREF(stack);
self->stack=NULL;
return val; return val;
err:
self->stack=NULL;
Py_XDECREF(stack);
return NULL;
} }
...@@ -3584,8 +3541,7 @@ noload_obj(Unpicklerobject *self) { ...@@ -3584,8 +3541,7 @@ noload_obj(Unpicklerobject *self) {
int i, len; int i, len;
if ((i = marker(self)) < 0) return -1; if ((i = marker(self)) < 0) return -1;
if ((len = PyList_Size(self->stack)) < 0) return -1; return Pdata_clear(self->stack, i+1);
return DEL_LIST_SLICE(self->stack, i+1, len);
} }
...@@ -3595,11 +3551,11 @@ noload_inst(Unpicklerobject *self) { ...@@ -3595,11 +3551,11 @@ noload_inst(Unpicklerobject *self) {
char *s; char *s;
if ((i = marker(self)) < 0) return -1; if ((i = marker(self)) < 0) return -1;
if ((j = PyList_Size(self->stack)) < 0) return -1; Pdata_clear(self->stack, i);
if (DEL_LIST_SLICE(self->stack, i, j) < 0) return -1;
if ((*self->readline_func)(self, &s) < 0) return -1; if ((*self->readline_func)(self, &s) < 0) return -1;
if ((*self->readline_func)(self, &s) < 0) return -1; if ((*self->readline_func)(self, &s) < 0) return -1;
return PyList_Append(self->stack, Py_None); PDATA_APPEND(self->stack, Py_None,-1);
return 0;
} }
static int static int
...@@ -3608,24 +3564,26 @@ noload_global(Unpicklerobject *self) { ...@@ -3608,24 +3564,26 @@ noload_global(Unpicklerobject *self) {
if ((*self->readline_func)(self, &s) < 0) return -1; if ((*self->readline_func)(self, &s) < 0) return -1;
if ((*self->readline_func)(self, &s) < 0) return -1; if ((*self->readline_func)(self, &s) < 0) return -1;
return PyList_Append(self->stack, Py_None); PDATA_APPEND(self->stack, Py_None,-1);
return 0;
} }
static int static int
noload_reduce(Unpicklerobject *self) { noload_reduce(Unpicklerobject *self) {
int len; int len;
if ((len = PyList_Size(self->stack)) < 0) return -1; if (self->stack->length < 2) return stackUnderflow();
if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0) return -1; Pdata_clear(self->stack, self->stack->length-2);
return PyList_Append(self->stack, Py_None); PDATA_APPEND(self->stack, Py_None,-1);
return 0;
} }
static int static int
noload_build(Unpicklerobject *self) { noload_build(Unpicklerobject *self) {
int len; int len;
if ((len = PyList_Size(self->stack)) < 0) return -1; if (self->stack->length < 1) return stackUnderflow();
return DEL_LIST_SLICE(self->stack, len - 1, len); Pdata_clear(self->stack, self->stack->length-1);
} }
...@@ -3635,11 +3593,8 @@ noload(Unpicklerobject *self) { ...@@ -3635,11 +3593,8 @@ noload(Unpicklerobject *self) {
int len; int len;
char *s; char *s;
UNLESS(stack = PyList_New(0))
goto err;
self->stack = stack;
self->num_marks = 0; self->num_marks = 0;
Pdata_clear(self->stack, 0);
while (1) { while (1) {
if ((*self->read_func)(self, &s, 1) < 0) if ((*self->read_func)(self, &s, 1) < 0)
...@@ -3842,40 +3797,27 @@ noload(Unpicklerobject *self) { ...@@ -3842,40 +3797,27 @@ noload(Unpicklerobject *self) {
default: default:
cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.", cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
"c", s[0]); "c", s[0]);
goto err; return NULL;
} }
break; break;
} }
if ((err = PyErr_Occurred()) == PyExc_EOFError) { if ((err = PyErr_Occurred())) {
if (err == PyExc_EOFError) {
PyErr_SetNone(PyExc_EOFError); PyErr_SetNone(PyExc_EOFError);
goto err; }
return NULL;
} }
if (err) goto err; PDATA_POP(self->stack, val);
if ((len = PyList_Size(stack)) < 0) goto err;
UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
Py_INCREF(val);
Py_DECREF(stack);
self->stack=NULL;
return val; return val;
err:
self->stack=NULL;
Py_XDECREF(stack);
return NULL;
} }
static PyObject * static PyObject *
Unpickler_load(Unpicklerobject *self, PyObject *args) { Unpickler_load(Unpicklerobject *self, PyObject *args) {
UNLESS(PyArg_ParseTuple(args, "")) UNLESS (PyArg_ParseTuple(args, ""))
return NULL; return NULL;
return load(self); return load(self);
...@@ -3883,7 +3825,7 @@ Unpickler_load(Unpicklerobject *self, PyObject *args) { ...@@ -3883,7 +3825,7 @@ Unpickler_load(Unpicklerobject *self, PyObject *args) {
static PyObject * static PyObject *
Unpickler_noload(Unpicklerobject *self, PyObject *args) { Unpickler_noload(Unpicklerobject *self, PyObject *args) {
UNLESS(PyArg_ParseTuple(args, "")) UNLESS (PyArg_ParseTuple(args, ""))
return NULL; return NULL;
return noload(self); return noload(self);
...@@ -3910,12 +3852,12 @@ static Unpicklerobject * ...@@ -3910,12 +3852,12 @@ static Unpicklerobject *
newUnpicklerobject(PyObject *f) { newUnpicklerobject(PyObject *f) {
Unpicklerobject *self; Unpicklerobject *self;
UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype)) UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
return NULL; return NULL;
self->file = NULL; self->file = NULL;
self->arg = NULL; self->arg = NULL;
self->stack = NULL; self->stack = (Pdata*)Pdata_New();
self->pers_func = NULL; self->pers_func = NULL;
self->last_string = NULL; self->last_string = NULL;
self->marks = NULL; self->marks = NULL;
...@@ -3925,7 +3867,7 @@ newUnpicklerobject(PyObject *f) { ...@@ -3925,7 +3867,7 @@ newUnpicklerobject(PyObject *f) {
self->read = NULL; self->read = NULL;
self->readline = NULL; self->readline = NULL;
UNLESS(self->memo = PyDict_New()) { UNLESS (self->memo = PyDict_New()) {
Py_XDECREF((PyObject *)self); Py_XDECREF((PyObject *)self);
return NULL; return NULL;
} }
...@@ -3950,7 +3892,7 @@ newUnpicklerobject(PyObject *f) { ...@@ -3950,7 +3892,7 @@ newUnpicklerobject(PyObject *f) {
self->read_func = read_other; self->read_func = read_other;
self->readline_func = readline_other; self->readline_func = readline_other;
UNLESS((self->readline = PyObject_GetAttr(f, readline_str)) && UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
(self->read = PyObject_GetAttr(f, read_str))) { (self->read = PyObject_GetAttr(f, read_str))) {
PyErr_Clear(); PyErr_Clear();
PyErr_SetString( PyExc_TypeError, "argument must have 'read' and " PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
...@@ -3959,14 +3901,14 @@ newUnpicklerobject(PyObject *f) { ...@@ -3959,14 +3901,14 @@ newUnpicklerobject(PyObject *f) {
} }
} }
if(PyEval_GetRestricted()) { if (PyEval_GetRestricted()) {
/* Restricted execution, get private tables */ /* Restricted execution, get private tables */
PyObject *m; PyObject *m;
UNLESS(m=PyImport_Import(copy_reg_str)) goto err; UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str); self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
Py_DECREF(m); Py_DECREF(m);
UNLESS(self->safe_constructors) goto err; UNLESS (self->safe_constructors) goto err;
} }
else { else {
self->safe_constructors=safe_constructors; self->safe_constructors=safe_constructors;
...@@ -3985,7 +3927,7 @@ static PyObject * ...@@ -3985,7 +3927,7 @@ static PyObject *
get_Unpickler(PyObject *self, PyObject *args) { get_Unpickler(PyObject *self, PyObject *args) {
PyObject *file; PyObject *file;
UNLESS(PyArg_ParseTuple(args, "O", &file)) UNLESS (PyArg_ParseTuple(args, "O", &file))
return NULL; return NULL;
return (PyObject *)newUnpicklerobject(file); return (PyObject *)newUnpicklerobject(file);
} }
...@@ -4037,16 +3979,6 @@ Unpickler_getattr(Unpicklerobject *self, char *name) { ...@@ -4037,16 +3979,6 @@ Unpickler_getattr(Unpicklerobject *self, char *name) {
return self->memo; return self->memo;
} }
if (!strcmp(name, "stack")) {
if (!self->stack) {
PyErr_SetString(PyExc_AttributeError, name);
return NULL;
}
Py_INCREF(self->stack);
return self->stack;
}
if (!strcmp(name, "UnpicklingError")) { if (!strcmp(name, "UnpicklingError")) {
Py_INCREF(UnpicklingError); Py_INCREF(UnpicklingError);
return UnpicklingError; return UnpicklingError;
...@@ -4059,7 +3991,7 @@ Unpickler_getattr(Unpicklerobject *self, char *name) { ...@@ -4059,7 +3991,7 @@ Unpickler_getattr(Unpicklerobject *self, char *name) {
static int static int
Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) { Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
if(! value) { if (! value) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"attribute deletion is not supported"); "attribute deletion is not supported");
return -1; return -1;
...@@ -4073,7 +4005,7 @@ Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) { ...@@ -4073,7 +4005,7 @@ Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
} }
if (strcmp(name, "memo") == 0) { if (strcmp(name, "memo") == 0) {
if(! PyDict_Check(value)) { if (! PyDict_Check(value)) {
PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
return -1; return -1;
} }
...@@ -4094,10 +4026,10 @@ cpm_dump(PyObject *self, PyObject *args) { ...@@ -4094,10 +4026,10 @@ cpm_dump(PyObject *self, PyObject *args) {
Picklerobject *pickler = 0; Picklerobject *pickler = 0;
int bin = 0; int bin = 0;
UNLESS(PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin)) UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
goto finally; goto finally;
UNLESS(pickler = newPicklerobject(file, bin)) UNLESS (pickler = newPicklerobject(file, bin))
goto finally; goto finally;
if (dump(pickler, ob) < 0) if (dump(pickler, ob) < 0)
...@@ -4119,13 +4051,13 @@ cpm_dumps(PyObject *self, PyObject *args) { ...@@ -4119,13 +4051,13 @@ cpm_dumps(PyObject *self, PyObject *args) {
Picklerobject *pickler = 0; Picklerobject *pickler = 0;
int bin = 0; int bin = 0;
UNLESS(PyArg_ParseTuple(args, "O|i", &ob, &bin)) UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &bin))
goto finally; goto finally;
UNLESS(file = PycStringIO->NewOutput(128)) UNLESS (file = PycStringIO->NewOutput(128))
goto finally; goto finally;
UNLESS(pickler = newPicklerobject(file, bin)) UNLESS (pickler = newPicklerobject(file, bin))
goto finally; goto finally;
if (dump(pickler, ob) < 0) if (dump(pickler, ob) < 0)
...@@ -4146,10 +4078,10 @@ cpm_load(PyObject *self, PyObject *args) { ...@@ -4146,10 +4078,10 @@ cpm_load(PyObject *self, PyObject *args) {
Unpicklerobject *unpickler = 0; Unpicklerobject *unpickler = 0;
PyObject *ob, *res = NULL; PyObject *ob, *res = NULL;
UNLESS(PyArg_ParseTuple(args, "O", &ob)) UNLESS (PyArg_ParseTuple(args, "O", &ob))
goto finally; goto finally;
UNLESS(unpickler = newUnpicklerobject(ob)) UNLESS (unpickler = newUnpicklerobject(ob))
goto finally; goto finally;
res = load(unpickler); res = load(unpickler);
...@@ -4166,13 +4098,13 @@ cpm_loads(PyObject *self, PyObject *args) { ...@@ -4166,13 +4098,13 @@ cpm_loads(PyObject *self, PyObject *args) {
PyObject *ob, *file = 0, *res = NULL; PyObject *ob, *file = 0, *res = NULL;
Unpicklerobject *unpickler = 0; Unpicklerobject *unpickler = 0;
UNLESS(PyArg_ParseTuple(args, "S", &ob)) UNLESS (PyArg_ParseTuple(args, "S", &ob))
goto finally; goto finally;
UNLESS(file = PycStringIO->NewInput(ob)) UNLESS (file = PycStringIO->NewInput(ob))
goto finally; goto finally;
UNLESS(unpickler = newUnpicklerobject(file)) UNLESS (unpickler = newUnpicklerobject(file))
goto finally; goto finally;
res = load(unpickler); res = load(unpickler);
...@@ -4248,7 +4180,7 @@ static struct PyMethodDef cPickle_methods[] = { ...@@ -4248,7 +4180,7 @@ static struct PyMethodDef cPickle_methods[] = {
#define CHECK_FOR_ERRORS(MESS) \ #define CHECK_FOR_ERRORS(MESS) \
if(PyErr_Occurred()) { \ if (PyErr_Occurred()) { \
PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \ PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \ PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
fprintf(stderr, # MESS ":\n\t"); \ fprintf(stderr, # MESS ":\n\t"); \
...@@ -4284,17 +4216,18 @@ init_stuff(PyObject *module, PyObject *module_dict) { ...@@ -4284,17 +4216,18 @@ init_stuff(PyObject *module, PyObject *module_dict) {
INIT_STR(dispatch_table); INIT_STR(dispatch_table);
INIT_STR(safe_constructors); INIT_STR(safe_constructors);
INIT_STR(__basicnew__); INIT_STR(__basicnew__);
UNLESS (empty_str=PyString_FromString("")) return -1;
UNLESS(copy_reg = PyImport_ImportModule("copy_reg")) UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
return -1; return -1;
/* These next few are special because we want to use different /* These next few are special because we want to use different
ones in restricted mode. */ ones in restricted mode. */
UNLESS(dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str)) UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
return -1; return -1;
UNLESS(safe_constructors = PyObject_GetAttr(copy_reg, UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
safe_constructors_str)) safe_constructors_str))
return -1; return -1;
...@@ -4302,31 +4235,38 @@ init_stuff(PyObject *module, PyObject *module_dict) { ...@@ -4302,31 +4235,38 @@ init_stuff(PyObject *module, PyObject *module_dict) {
/* Down to here ********************************** */ /* Down to here ********************************** */
UNLESS(string = PyImport_ImportModule("string")) UNLESS (string = PyImport_ImportModule("string"))
return -1; return -1;
UNLESS(atol_func = PyObject_GetAttrString(string, "atol")) UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
return -1; return -1;
Py_DECREF(string); Py_DECREF(string);
UNLESS(empty_tuple = PyTuple_New(0)) UNLESS (empty_tuple = PyTuple_New(0))
return -1; return -1;
UNLESS(PicklingError = PyString_FromString("cPickle.PicklingError")) UNLESS (PicklingError = PyString_FromString("cPickle.PicklingError"))
return -1; return -1;
if (PyDict_SetItemString(module_dict, "PicklingError", if (PyDict_SetItemString(module_dict, "PicklingError",
PicklingError) < 0) PicklingError) < 0)
return -1; return -1;
UNLESS(UnpicklingError = PyString_FromString("cPickle.UnpicklingError")) UNLESS (UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
return -1; return -1;
if (PyDict_SetItemString(module_dict, "UnpicklingError", if (PyDict_SetItemString(module_dict, "UnpicklingError",
UnpicklingError) < 0) UnpicklingError) < 0)
return -1; return -1;
UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
return -1;
if (PyDict_SetItemString(module_dict, "BadPickleGet",
BadPickleGet) < 0)
return -1;
PycString_IMPORT; PycString_IMPORT;
return 0; return 0;
...@@ -4335,12 +4275,13 @@ init_stuff(PyObject *module, PyObject *module_dict) { ...@@ -4335,12 +4275,13 @@ init_stuff(PyObject *module, PyObject *module_dict) {
void void
initcPickle() { initcPickle() {
PyObject *m, *d, *v; PyObject *m, *d, *v;
char *rev="$Revision: 1.59 $"; char *rev="$Revision: 1.60 $";
PyObject *format_version; PyObject *format_version;
PyObject *compatible_formats; PyObject *compatible_formats;
Picklertype.ob_type = &PyType_Type; Picklertype.ob_type = &PyType_Type;
Unpicklertype.ob_type = &PyType_Type; Unpicklertype.ob_type = &PyType_Type;
PdataType.ob_type = &PyType_Type;
/* Create the module and add the functions */ /* Create the module and add the functions */
m = Py_InitModule4("cPickle", cPickle_methods, m = Py_InitModule4("cPickle", cPickle_methods,
...@@ -4352,13 +4293,8 @@ initcPickle() { ...@@ -4352,13 +4293,8 @@ initcPickle() {
PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev)); PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Py_XDECREF(v); Py_XDECREF(v);
#ifdef FORMAT_1_3
format_version = PyString_FromString("1.3"); format_version = PyString_FromString("1.3");
compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2"); compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
#else
format_version = PyString_FromString("1.2");
compatible_formats = Py_BuildValue("[ss]", "1.0", "1.1");
#endif
PyDict_SetItemString(d, "format_version", format_version); PyDict_SetItemString(d, "format_version", format_version);
PyDict_SetItemString(d, "compatible_formats", compatible_formats); PyDict_SetItemString(d, "compatible_formats", compatible_formats);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment